Home | History | Annotate | Download | only in video_engine
      1 /*
      2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/video_engine/vie_rtp_rtcp_impl.h"
     12 
     13 #include "webrtc/engine_configurations.h"
     14 #include "webrtc/system_wrappers/interface/file_wrapper.h"
     15 #include "webrtc/system_wrappers/interface/logging.h"
     16 #include "webrtc/video_engine/include/vie_errors.h"
     17 #include "webrtc/video_engine/vie_channel.h"
     18 #include "webrtc/video_engine/vie_channel_manager.h"
     19 #include "webrtc/video_engine/vie_defines.h"
     20 #include "webrtc/video_engine/vie_encoder.h"
     21 #include "webrtc/video_engine/vie_impl.h"
     22 #include "webrtc/video_engine/vie_shared_data.h"
     23 
     24 namespace webrtc {
     25 
     26 // Helper methods for converting between module format and ViE API format.
     27 
     28 static RTCPMethod ViERTCPModeToRTCPMethod(ViERTCPMode api_mode) {
     29   switch (api_mode) {
     30     case kRtcpNone:
     31       return kRtcpOff;
     32 
     33     case kRtcpCompound_RFC4585:
     34       return kRtcpCompound;
     35 
     36     case kRtcpNonCompound_RFC5506:
     37       return kRtcpNonCompound;
     38   }
     39   assert(false);
     40   return kRtcpOff;
     41 }
     42 
     43 static ViERTCPMode RTCPMethodToViERTCPMode(RTCPMethod module_method) {
     44   switch (module_method) {
     45     case kRtcpOff:
     46       return kRtcpNone;
     47 
     48     case kRtcpCompound:
     49       return kRtcpCompound_RFC4585;
     50 
     51     case kRtcpNonCompound:
     52       return kRtcpNonCompound_RFC5506;
     53   }
     54   assert(false);
     55   return kRtcpNone;
     56 }
     57 
     58 static KeyFrameRequestMethod APIRequestToModuleRequest(
     59   ViEKeyFrameRequestMethod api_method) {
     60   switch (api_method) {
     61     case kViEKeyFrameRequestNone:
     62       return kKeyFrameReqFirRtp;
     63 
     64     case kViEKeyFrameRequestPliRtcp:
     65       return kKeyFrameReqPliRtcp;
     66 
     67     case kViEKeyFrameRequestFirRtp:
     68       return kKeyFrameReqFirRtp;
     69 
     70     case kViEKeyFrameRequestFirRtcp:
     71       return kKeyFrameReqFirRtcp;
     72   }
     73   assert(false);
     74   return kKeyFrameReqFirRtp;
     75 }
     76 
     77 ViERTP_RTCP* ViERTP_RTCP::GetInterface(VideoEngine* video_engine) {
     78 #ifdef WEBRTC_VIDEO_ENGINE_RTP_RTCP_API
     79   if (!video_engine) {
     80     return NULL;
     81   }
     82   VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
     83   ViERTP_RTCPImpl* vie_rtpimpl = vie_impl;
     84   // Increase ref count.
     85   (*vie_rtpimpl)++;
     86   return vie_rtpimpl;
     87 #else
     88   return NULL;
     89 #endif
     90 }
     91 
     92 int ViERTP_RTCPImpl::Release() {
     93   // Decrease ref count.
     94   (*this)--;
     95 
     96   int32_t ref_count = GetCount();
     97   if (ref_count < 0) {
     98     LOG(LS_ERROR) << "ViERTP_RTCP released too many times.";
     99     shared_data_->SetLastError(kViEAPIDoesNotExist);
    100     return -1;
    101   }
    102   return ref_count;
    103 }
    104 
    105 ViERTP_RTCPImpl::ViERTP_RTCPImpl(ViESharedData* shared_data)
    106     : shared_data_(shared_data) {}
    107 
    108 ViERTP_RTCPImpl::~ViERTP_RTCPImpl() {}
    109 
    110 int ViERTP_RTCPImpl::SetLocalSSRC(const int video_channel,
    111                                   const unsigned int SSRC,
    112                                   const StreamType usage,
    113                                   const unsigned char simulcast_idx) {
    114   LOG_F(LS_INFO) << "channel: " << video_channel << " ssrc: " << SSRC << "";
    115   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    116   ViEChannel* vie_channel = cs.Channel(video_channel);
    117   if (!vie_channel) {
    118     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    119     return -1;
    120   }
    121   if (vie_channel->SetSSRC(SSRC, usage, simulcast_idx) != 0) {
    122     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    123     return -1;
    124   }
    125   return 0;
    126 }
    127 
    128 int ViERTP_RTCPImpl::SetRemoteSSRCType(const int videoChannel,
    129                                        const StreamType usage,
    130                                        const unsigned int SSRC) const {
    131   LOG_F(LS_INFO) << "channel: " << videoChannel
    132                  << " usage: " << static_cast<int>(usage) << " ssrc: " << SSRC;
    133 
    134   // Get the channel
    135   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    136   ViEChannel* ptrViEChannel = cs.Channel(videoChannel);
    137   if (ptrViEChannel == NULL) {
    138     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    139     return -1;
    140   }
    141   if (ptrViEChannel->SetRemoteSSRCType(usage, SSRC) != 0) {
    142     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    143     return -1;
    144   }
    145   return 0;
    146 }
    147 
    148 int ViERTP_RTCPImpl::GetLocalSSRC(const int video_channel,
    149                                   unsigned int& SSRC) const {
    150   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    151   ViEChannel* vie_channel = cs.Channel(video_channel);
    152   if (!vie_channel) {
    153     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    154     return -1;
    155   }
    156   uint8_t idx = 0;
    157   if (vie_channel->GetLocalSSRC(idx, &SSRC) != 0) {
    158     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    159     return -1;
    160   }
    161   return 0;
    162 }
    163 
    164 int ViERTP_RTCPImpl::GetRemoteSSRC(const int video_channel,
    165                                    unsigned int& SSRC) const {
    166   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    167   ViEChannel* vie_channel = cs.Channel(video_channel);
    168   if (!vie_channel) {
    169     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    170     return -1;
    171   }
    172   if (vie_channel->GetRemoteSSRC(&SSRC) != 0) {
    173     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    174     return -1;
    175   }
    176   return 0;
    177 }
    178 
    179 int ViERTP_RTCPImpl::GetRemoteCSRCs(const int video_channel,
    180                                     unsigned int CSRCs[kRtpCsrcSize]) const {
    181   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    182   ViEChannel* vie_channel = cs.Channel(video_channel);
    183   if (!vie_channel) {
    184     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    185     return -1;
    186   }
    187   if (vie_channel->GetRemoteCSRC(CSRCs) != 0) {
    188     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    189     return -1;
    190   }
    191   return 0;
    192 }
    193 
    194 int ViERTP_RTCPImpl::SetRtxSendPayloadType(const int video_channel,
    195                                            const uint8_t payload_type) {
    196   LOG_F(LS_INFO) << "channel: " << video_channel
    197                  << " payload_type: " << payload_type;
    198   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    199   ViEChannel* vie_channel = cs.Channel(video_channel);
    200   if (!vie_channel) {
    201     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    202     return -1;
    203   }
    204   if (vie_channel->SetRtxSendPayloadType(payload_type) != 0) {
    205     return -1;
    206   }
    207   return 0;
    208 }
    209 
    210 int ViERTP_RTCPImpl::SetPadWithRedundantPayloads(int video_channel,
    211                                                  bool enable) {
    212   LOG_F(LS_INFO) << "channel: " << video_channel
    213                  << " pad with redundant payloads: " << (enable ? "enable" :
    214                  "disable");
    215   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    216   ViEChannel* vie_channel = cs.Channel(video_channel);
    217   if (!vie_channel) {
    218     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    219     return -1;
    220   }
    221   vie_channel->SetPadWithRedundantPayloads(enable);
    222   return 0;
    223 }
    224 
    225 int ViERTP_RTCPImpl::SetRtxReceivePayloadType(const int video_channel,
    226                                               const uint8_t payload_type) {
    227   LOG_F(LS_INFO) << "channel: " << video_channel
    228                << " payload_type: " << payload_type;
    229   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    230   ViEChannel* vie_channel = cs.Channel(video_channel);
    231   if (!vie_channel) {
    232     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    233     return -1;
    234   }
    235   vie_channel->SetRtxReceivePayloadType(payload_type);
    236   return 0;
    237 }
    238 
    239 int ViERTP_RTCPImpl::SetStartSequenceNumber(const int video_channel,
    240                                             uint16_t sequence_number) {
    241   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    242   ViEChannel* vie_channel = cs.Channel(video_channel);
    243   if (!vie_channel) {
    244     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    245     return -1;
    246   }
    247   if (vie_channel->Sending()) {
    248     LOG_F(LS_ERROR) << "channel " << video_channel << " is already sending.";
    249     shared_data_->SetLastError(kViERtpRtcpAlreadySending);
    250     return -1;
    251   }
    252   if (vie_channel->SetStartSequenceNumber(sequence_number) != 0) {
    253     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    254     return -1;
    255   }
    256   return 0;
    257 }
    258 
    259 int ViERTP_RTCPImpl::SetRTCPStatus(const int video_channel,
    260                                    const ViERTCPMode rtcp_mode) {
    261   LOG_F(LS_INFO) << "channel: " << video_channel
    262                  << " mode: " << static_cast<int>(rtcp_mode);
    263   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    264   ViEChannel* vie_channel = cs.Channel(video_channel);
    265   if (!vie_channel) {
    266     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    267     return -1;
    268   }
    269 
    270   RTCPMethod module_mode = ViERTCPModeToRTCPMethod(rtcp_mode);
    271   if (vie_channel->SetRTCPMode(module_mode) != 0) {
    272     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    273     return -1;
    274   }
    275   return 0;
    276 }
    277 
    278 int ViERTP_RTCPImpl::GetRTCPStatus(const int video_channel,
    279                                    ViERTCPMode& rtcp_mode) const {
    280   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    281   ViEChannel* vie_channel = cs.Channel(video_channel);
    282   if (!vie_channel) {
    283     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    284     return -1;
    285   }
    286   RTCPMethod module_mode = kRtcpOff;
    287   if (vie_channel->GetRTCPMode(&module_mode) != 0) {
    288     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    289     return -1;
    290   }
    291   rtcp_mode = RTCPMethodToViERTCPMode(module_mode);
    292   return 0;
    293 }
    294 
    295 int ViERTP_RTCPImpl::SetRTCPCName(const int video_channel,
    296                                   const char rtcp_cname[KMaxRTCPCNameLength]) {
    297   LOG_F(LS_INFO) << "channel: " << video_channel
    298                  << " rtcp_cname: " << rtcp_cname;
    299   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    300   ViEChannel* vie_channel = cs.Channel(video_channel);
    301   if (!vie_channel) {
    302     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    303     return -1;
    304   }
    305   if (vie_channel->Sending()) {
    306     LOG_F(LS_ERROR) << "channel " << video_channel << " is already sending.";
    307     shared_data_->SetLastError(kViERtpRtcpAlreadySending);
    308     return -1;
    309   }
    310   if (vie_channel->SetRTCPCName(rtcp_cname) != 0) {
    311     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    312     return -1;
    313   }
    314   return 0;
    315 }
    316 
    317 int ViERTP_RTCPImpl::GetRTCPCName(const int video_channel,
    318                                   char rtcp_cname[KMaxRTCPCNameLength]) const {
    319   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    320   ViEChannel* vie_channel = cs.Channel(video_channel);
    321   if (!vie_channel) {
    322     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    323     return -1;
    324   }
    325   if (vie_channel->GetRTCPCName(rtcp_cname) != 0) {
    326     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    327     return -1;
    328   }
    329   return 0;
    330 }
    331 
    332 int ViERTP_RTCPImpl::GetRemoteRTCPCName(
    333     const int video_channel,
    334     char rtcp_cname[KMaxRTCPCNameLength]) const {
    335   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    336   ViEChannel* vie_channel = cs.Channel(video_channel);
    337   if (!vie_channel) {
    338     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    339     return -1;
    340   }
    341   if (vie_channel->GetRemoteRTCPCName(rtcp_cname) != 0) {
    342     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    343     return -1;
    344   }
    345   return 0;
    346 }
    347 
    348 int ViERTP_RTCPImpl::SendApplicationDefinedRTCPPacket(
    349   const int video_channel,
    350   const unsigned char sub_type,
    351   unsigned int name,
    352   const char* data,
    353   uint16_t data_length_in_bytes) {
    354   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    355   ViEChannel* vie_channel = cs.Channel(video_channel);
    356   if (!vie_channel) {
    357     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    358     return -1;
    359   }
    360   if (!vie_channel->Sending()) {
    361     shared_data_->SetLastError(kViERtpRtcpNotSending);
    362     return -1;
    363   }
    364   RTCPMethod method;
    365   if (vie_channel->GetRTCPMode(&method) != 0 || method == kRtcpOff) {
    366     shared_data_->SetLastError(kViERtpRtcpRtcpDisabled);
    367     return -1;
    368   }
    369   if (vie_channel->SendApplicationDefinedRTCPPacket(
    370         sub_type, name, reinterpret_cast<const uint8_t*>(data),
    371         data_length_in_bytes) != 0) {
    372     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    373     return -1;
    374   }
    375   return 0;
    376 }
    377 
    378 int ViERTP_RTCPImpl::SetNACKStatus(const int video_channel, const bool enable) {
    379   LOG_F(LS_INFO) << "channel: " << video_channel << " "
    380                  << (enable ? "on" : "off");
    381   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    382   ViEChannel* vie_channel = cs.Channel(video_channel);
    383   if (!vie_channel) {
    384     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    385     return -1;
    386   }
    387   if (vie_channel->SetNACKStatus(enable) != 0) {
    388     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    389     return -1;
    390   }
    391 
    392   // Update the encoder
    393   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    394   if (!vie_encoder) {
    395     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    396     return -1;
    397   }
    398   vie_encoder->UpdateProtectionMethod(enable);
    399   return 0;
    400 }
    401 
    402 int ViERTP_RTCPImpl::SetFECStatus(const int video_channel, const bool enable,
    403                                   const unsigned char payload_typeRED,
    404                                   const unsigned char payload_typeFEC) {
    405   LOG_F(LS_INFO) << "channel: " << video_channel
    406                  << " enable: " << (enable ? "on" : "off")
    407                  << " payload_typeRED: " << payload_typeRED
    408                  << " payload_typeFEC: " << payload_typeFEC;
    409   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    410   ViEChannel* vie_channel = cs.Channel(video_channel);
    411   if (!vie_channel) {
    412     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    413     return -1;
    414   }
    415   if (vie_channel->SetFECStatus(enable, payload_typeRED,
    416                                 payload_typeFEC) != 0) {
    417     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    418     return -1;
    419   }
    420   // Update the encoder.
    421   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    422   if (!vie_encoder) {
    423     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    424     return -1;
    425   }
    426   vie_encoder->UpdateProtectionMethod(false);
    427   return 0;
    428 }
    429 
    430 int ViERTP_RTCPImpl::SetHybridNACKFECStatus(
    431     const int video_channel,
    432     const bool enable,
    433     const unsigned char payload_typeRED,
    434     const unsigned char payload_typeFEC) {
    435   LOG_F(LS_INFO) << "channel: " << video_channel
    436                  << " enable: " << (enable ? "on" : "off")
    437                  << " payload_typeRED: " << payload_typeRED
    438                  << " payload_typeFEC: " << payload_typeFEC;
    439   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    440   ViEChannel* vie_channel = cs.Channel(video_channel);
    441   if (!vie_channel) {
    442     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    443     return -1;
    444   }
    445 
    446   // Update the channel status with hybrid NACK FEC mode.
    447   if (vie_channel->SetHybridNACKFECStatus(enable, payload_typeRED,
    448                                           payload_typeFEC) != 0) {
    449     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    450     return -1;
    451   }
    452 
    453   // Update the encoder.
    454   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    455   if (!vie_encoder) {
    456     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    457     return -1;
    458   }
    459   vie_encoder->UpdateProtectionMethod(enable);
    460   return 0;
    461 }
    462 
    463 int ViERTP_RTCPImpl::SetSenderBufferingMode(int video_channel,
    464                                             int target_delay_ms) {
    465   LOG_F(LS_INFO) << "channel: " << video_channel
    466                  << " target_delay_ms: " << target_delay_ms;
    467   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    468   ViEChannel* vie_channel = cs.Channel(video_channel);
    469   if (!vie_channel) {
    470     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    471     return -1;
    472   }
    473   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    474   if (!vie_encoder) {
    475     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    476     return -1;
    477   }
    478 
    479   // Update the channel with buffering mode settings.
    480   if (vie_channel->SetSenderBufferingMode(target_delay_ms) != 0) {
    481     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    482     return -1;
    483   }
    484 
    485   // Update the encoder's buffering mode settings.
    486   vie_encoder->SetSenderBufferingMode(target_delay_ms);
    487   return 0;
    488 }
    489 
    490 int ViERTP_RTCPImpl::SetReceiverBufferingMode(int video_channel,
    491                                               int target_delay_ms) {
    492   LOG_F(LS_INFO) << "channel: " << video_channel
    493                  << " target_delay_ms: " << target_delay_ms;
    494   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    495   ViEChannel* vie_channel = cs.Channel(video_channel);
    496   if (!vie_channel) {
    497     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    498     return -1;
    499   }
    500 
    501   // Update the channel with buffering mode settings.
    502   if (vie_channel->SetReceiverBufferingMode(target_delay_ms) != 0) {
    503     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    504     return -1;
    505   }
    506   return 0;
    507 }
    508 
    509 int ViERTP_RTCPImpl::SetKeyFrameRequestMethod(
    510   const int video_channel,
    511   const ViEKeyFrameRequestMethod method) {
    512   LOG_F(LS_INFO) << "channel: " << video_channel
    513                  << " method: " << static_cast<int>(method);
    514 
    515   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    516   ViEChannel* vie_channel = cs.Channel(video_channel);
    517   if (!vie_channel) {
    518     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    519     return -1;
    520   }
    521   KeyFrameRequestMethod module_method = APIRequestToModuleRequest(method);
    522   if (vie_channel->SetKeyFrameRequestMethod(module_method) != 0) {
    523     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    524     return -1;
    525   }
    526   return 0;
    527 }
    528 
    529 int ViERTP_RTCPImpl::SetTMMBRStatus(const int video_channel,
    530                                     const bool enable) {
    531   LOG_F(LS_INFO) << "channel: " << video_channel
    532                  << "enable: " << (enable ? "on" : "off");
    533   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    534   ViEChannel* vie_channel = cs.Channel(video_channel);
    535   if (!vie_channel) {
    536     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    537     return -1;
    538   }
    539   if (vie_channel->EnableTMMBR(enable) != 0) {
    540     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    541     return -1;
    542   }
    543   return 0;
    544 }
    545 
    546 int ViERTP_RTCPImpl::SetRembStatus(int video_channel,
    547                                    bool sender,
    548                                    bool receiver) {
    549   LOG_F(LS_INFO) << "channel: " << video_channel
    550                  << " sender: " << (sender ? "on" : "off")
    551                  << " receiver: " << (receiver ? "on" : "off");
    552   if (!shared_data_->channel_manager()->SetRembStatus(video_channel, sender,
    553                                                       receiver)) {
    554     return -1;
    555   }
    556   return 0;
    557 }
    558 
    559 int ViERTP_RTCPImpl::SetSendTimestampOffsetStatus(int video_channel,
    560                                                   bool enable,
    561                                                   int id) {
    562   LOG_F(LS_INFO) << "channel: " << video_channel
    563                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    564 
    565   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    566   ViEChannel* vie_channel = cs.Channel(video_channel);
    567   if (!vie_channel) {
    568     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    569     return -1;
    570   }
    571   if (vie_channel->SetSendTimestampOffsetStatus(enable, id) != 0) {
    572     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    573     return -1;
    574   }
    575   return 0;
    576 }
    577 
    578 int ViERTP_RTCPImpl::SetReceiveTimestampOffsetStatus(int video_channel,
    579                                                      bool enable,
    580                                                      int id) {
    581   LOG_F(LS_INFO) << "channel: " << video_channel
    582                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    583   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    584   ViEChannel* vie_channel = cs.Channel(video_channel);
    585   if (!vie_channel) {
    586     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    587     return -1;
    588   }
    589   if (vie_channel->SetReceiveTimestampOffsetStatus(enable, id) != 0) {
    590     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    591     return -1;
    592   }
    593   return 0;
    594 }
    595 
    596 int ViERTP_RTCPImpl::SetSendAbsoluteSendTimeStatus(int video_channel,
    597                                                    bool enable,
    598                                                    int id) {
    599   LOG_F(LS_INFO) << "channel: " << video_channel
    600                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    601 
    602   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    603   ViEChannel* vie_channel = cs.Channel(video_channel);
    604   if (!vie_channel) {
    605     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    606     return -1;
    607   }
    608   if (vie_channel->SetSendAbsoluteSendTimeStatus(enable, id) != 0) {
    609     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    610     return -1;
    611   }
    612   return 0;
    613 }
    614 
    615 int ViERTP_RTCPImpl::SetReceiveAbsoluteSendTimeStatus(int video_channel,
    616                                                       bool enable,
    617                                                       int id) {
    618   LOG_F(LS_INFO) << "channel: " << video_channel
    619                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    620   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    621   ViEChannel* vie_channel = cs.Channel(video_channel);
    622   if (!vie_channel) {
    623     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    624     return -1;
    625   }
    626   if (vie_channel->SetReceiveAbsoluteSendTimeStatus(enable, id) != 0) {
    627     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    628     return -1;
    629   }
    630   return 0;
    631 }
    632 
    633 int ViERTP_RTCPImpl::SetRtcpXrRrtrStatus(int video_channel, bool enable) {
    634   LOG_F(LS_INFO) << "channel: " << video_channel
    635                  << " enable: " << (enable ? "on" : "off");
    636 
    637   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    638   ViEChannel* vie_channel = cs.Channel(video_channel);
    639   if (!vie_channel) {
    640     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    641     return -1;
    642   }
    643   vie_channel->SetRtcpXrRrtrStatus(enable);
    644   return 0;
    645 }
    646 
    647 int ViERTP_RTCPImpl::SetTransmissionSmoothingStatus(int video_channel,
    648                                                     bool enable) {
    649   LOG_F(LS_INFO) << "channel: " << video_channel
    650                  << " enable: " << (enable ? "on" : "off");
    651   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    652   ViEChannel* vie_channel = cs.Channel(video_channel);
    653   if (!vie_channel) {
    654     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    655     return -1;
    656   }
    657   vie_channel->SetTransmissionSmoothingStatus(enable);
    658   return 0;
    659 }
    660 
    661 int ViERTP_RTCPImpl::SetMinTransmitBitrate(int video_channel,
    662                                            int min_transmit_bitrate_kbps) {
    663   LOG_F(LS_INFO) << "channel: " << video_channel
    664                  << " min_transmit_bitrate_kbps: " << min_transmit_bitrate_kbps;
    665   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    666   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    667   if (vie_encoder == NULL)
    668     return -1;
    669   vie_encoder->SetMinTransmitBitrate(min_transmit_bitrate_kbps);
    670   return 0;
    671 }
    672 
    673 int ViERTP_RTCPImpl::SetReservedTransmitBitrate(
    674     int video_channel, unsigned int reserved_transmit_bitrate_bps) {
    675   LOG_F(LS_INFO) << "channel: " << video_channel
    676                  << " reserved_transmit_bitrate_bps: "
    677                  << reserved_transmit_bitrate_bps;
    678   if (!shared_data_->channel_manager()->SetReservedTransmitBitrate(
    679       video_channel, reserved_transmit_bitrate_bps)) {
    680     return -1;
    681   }
    682   return 0;
    683 }
    684 
    685 int ViERTP_RTCPImpl::GetReceiveChannelRtcpStatistics(
    686     const int video_channel,
    687     RtcpStatistics& basic_stats,
    688     int& rtt_ms) const {
    689   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    690   ViEChannel* vie_channel = cs.Channel(video_channel);
    691   if (!vie_channel) {
    692     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    693     return -1;
    694   }
    695 
    696   // TODO(sprang): Clean this up when stats struct is propagated all the way.
    697   uint16_t frac_lost;
    698   if (vie_channel->GetReceivedRtcpStatistics(
    699           &frac_lost,
    700           &basic_stats.cumulative_lost,
    701           &basic_stats.extended_max_sequence_number,
    702           &basic_stats.jitter,
    703           &rtt_ms) != 0) {
    704     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    705     return -1;
    706   }
    707   basic_stats.fraction_lost = frac_lost;
    708   return 0;
    709 }
    710 
    711 int ViERTP_RTCPImpl::GetSendChannelRtcpStatistics(const int video_channel,
    712                                                   RtcpStatistics& basic_stats,
    713                                                   int& rtt_ms) const {
    714   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    715   ViEChannel* vie_channel = cs.Channel(video_channel);
    716   if (!vie_channel) {
    717     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    718     return -1;
    719   }
    720 
    721   // TODO(sprang): Clean this up when stats struct is propagated all the way.
    722   uint16_t frac_lost;
    723   if (vie_channel->GetSendRtcpStatistics(
    724           &frac_lost,
    725           &basic_stats.cumulative_lost,
    726           &basic_stats.extended_max_sequence_number,
    727           &basic_stats.jitter,
    728           &rtt_ms) != 0) {
    729     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    730     return -1;
    731   }
    732   basic_stats.fraction_lost = frac_lost;
    733   return 0;
    734 }
    735 
    736 int ViERTP_RTCPImpl::GetRtpStatistics(const int video_channel,
    737                                       StreamDataCounters& sent,
    738                                       StreamDataCounters& received) const {
    739   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    740   ViEChannel* vie_channel = cs.Channel(video_channel);
    741   if (!vie_channel) {
    742     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    743     return -1;
    744   }
    745   if (vie_channel->GetRtpStatistics(&sent.bytes,
    746                                     &sent.packets,
    747                                     &received.bytes,
    748                                     &received.packets) != 0) {
    749     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    750     return -1;
    751   }
    752   return 0;
    753 }
    754 
    755 int ViERTP_RTCPImpl::GetRtcpPacketTypeCounters(
    756     int video_channel,
    757     RtcpPacketTypeCounter* packets_sent,
    758     RtcpPacketTypeCounter* packets_received) const {
    759   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    760   ViEChannel* vie_channel = cs.Channel(video_channel);
    761   if (!vie_channel) {
    762     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    763     return -1;
    764   }
    765   vie_channel->GetRtcpPacketTypeCounters(packets_sent, packets_received);
    766   return 0;
    767 }
    768 
    769 int ViERTP_RTCPImpl::GetBandwidthUsage(const int video_channel,
    770                                        unsigned int& total_bitrate_sent,
    771                                        unsigned int& video_bitrate_sent,
    772                                        unsigned int& fec_bitrate_sent,
    773                                        unsigned int& nackBitrateSent) const {
    774   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    775   ViEChannel* vie_channel = cs.Channel(video_channel);
    776   if (!vie_channel) {
    777     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    778     return -1;
    779   }
    780   vie_channel->GetBandwidthUsage(&total_bitrate_sent,
    781                                  &video_bitrate_sent,
    782                                  &fec_bitrate_sent,
    783                                  &nackBitrateSent);
    784   return 0;
    785 }
    786 
    787 int ViERTP_RTCPImpl::GetEstimatedSendBandwidth(
    788     const int video_channel,
    789     unsigned int* estimated_bandwidth) const {
    790   if (!shared_data_->channel_manager()->GetEstimatedSendBandwidth(
    791       video_channel, estimated_bandwidth)) {
    792     return -1;
    793   }
    794   return 0;
    795 }
    796 
    797 int ViERTP_RTCPImpl::GetEstimatedReceiveBandwidth(
    798     const int video_channel,
    799     unsigned int* estimated_bandwidth) const {
    800   if (!shared_data_->channel_manager()->GetEstimatedReceiveBandwidth(
    801       video_channel, estimated_bandwidth)) {
    802     return -1;
    803   }
    804   return 0;
    805 }
    806 
    807 int ViERTP_RTCPImpl::GetReceiveBandwidthEstimatorStats(
    808     const int video_channel,
    809     ReceiveBandwidthEstimatorStats* output) const {
    810   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    811   ViEChannel* vie_channel = cs.Channel(video_channel);
    812   if (!vie_channel) {
    813     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    814     return -1;
    815   }
    816   vie_channel->GetReceiveBandwidthEstimatorStats(output);
    817   return 0;
    818 }
    819 
    820 int ViERTP_RTCPImpl::GetPacerQueuingDelayMs(
    821     const int video_channel, int* delay_ms) const {
    822   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    823   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    824   if (!vie_encoder) {
    825     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    826     return -1;
    827   }
    828   *delay_ms = vie_encoder->PacerQueuingDelayMs();
    829   return 0;
    830 }
    831 
    832 int ViERTP_RTCPImpl::StartRTPDump(const int video_channel,
    833                                   const char file_nameUTF8[1024],
    834                                   RTPDirections direction) {
    835   LOG_F(LS_INFO) << "channel: " << video_channel
    836                  << " filename: " << file_nameUTF8
    837                  << " direction: " << static_cast<int>(direction);
    838   assert(FileWrapper::kMaxFileNameSize == 1024);
    839   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    840   ViEChannel* vie_channel = cs.Channel(video_channel);
    841   if (!vie_channel) {
    842     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    843     return -1;
    844   }
    845   if (vie_channel->StartRTPDump(file_nameUTF8, direction) != 0) {
    846     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    847     return -1;
    848   }
    849   return 0;
    850 }
    851 
    852 int ViERTP_RTCPImpl::StopRTPDump(const int video_channel,
    853                                  RTPDirections direction) {
    854   LOG_F(LS_INFO) << "channel: " << video_channel
    855                  << " direction: " << static_cast<int>(direction);
    856   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    857   ViEChannel* vie_channel = cs.Channel(video_channel);
    858   if (!vie_channel) {
    859     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    860     return -1;
    861   }
    862   if (vie_channel->StopRTPDump(direction) != 0) {
    863     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    864     return -1;
    865   }
    866   return 0;
    867 }
    868 
    869 int ViERTP_RTCPImpl::RegisterRTPObserver(const int video_channel,
    870                                          ViERTPObserver& observer) {
    871   LOG_F(LS_INFO) << "channel " << video_channel;
    872   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    873   ViEChannel* vie_channel = cs.Channel(video_channel);
    874   if (!vie_channel) {
    875     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    876     return -1;
    877   }
    878   if (vie_channel->RegisterRtpObserver(&observer) != 0) {
    879     shared_data_->SetLastError(kViERtpRtcpObserverAlreadyRegistered);
    880     return -1;
    881   }
    882   return 0;
    883 }
    884 
    885 int ViERTP_RTCPImpl::DeregisterRTPObserver(const int video_channel) {
    886   LOG_F(LS_INFO) << "channel " << video_channel;
    887   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    888   ViEChannel* vie_channel = cs.Channel(video_channel);
    889   if (!vie_channel) {
    890     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    891     return -1;
    892   }
    893   if (vie_channel->RegisterRtpObserver(NULL) != 0) {
    894     shared_data_->SetLastError(kViERtpRtcpObserverNotRegistered);
    895     return -1;
    896   }
    897   return 0;
    898 }
    899 
    900 int ViERTP_RTCPImpl::RegisterRTCPObserver(const int video_channel,
    901                                           ViERTCPObserver& observer) {
    902   LOG_F(LS_INFO) << "channel " << video_channel;
    903   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    904   ViEChannel* vie_channel = cs.Channel(video_channel);
    905   if (!vie_channel) {
    906     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    907     return -1;
    908   }
    909   if (vie_channel->RegisterRtcpObserver(&observer) != 0) {
    910     shared_data_->SetLastError(kViERtpRtcpObserverAlreadyRegistered);
    911     return -1;
    912   }
    913   return 0;
    914 }
    915 
    916 int ViERTP_RTCPImpl::DeregisterRTCPObserver(const int video_channel) {
    917   LOG_F(LS_INFO) << "channel " << video_channel;
    918   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    919   ViEChannel* vie_channel = cs.Channel(video_channel);
    920   if (!vie_channel) {
    921     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    922     return -1;
    923   }
    924   if (vie_channel->RegisterRtcpObserver(NULL) != 0) {
    925     shared_data_->SetLastError(kViERtpRtcpObserverNotRegistered);
    926     return -1;
    927   }
    928   return 0;
    929 }
    930 
    931 int ViERTP_RTCPImpl::RegisterSendChannelRtcpStatisticsCallback(
    932   int video_channel, RtcpStatisticsCallback* callback) {
    933   LOG_F(LS_INFO) << "channel " << video_channel;
    934   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    935   ViEChannel* vie_channel = cs.Channel(video_channel);
    936   if (!vie_channel) {
    937     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    938     return -1;
    939   }
    940   vie_channel->RegisterSendChannelRtcpStatisticsCallback(callback);
    941   return 0;
    942 }
    943 
    944 int ViERTP_RTCPImpl::DeregisterSendChannelRtcpStatisticsCallback(
    945     int video_channel, RtcpStatisticsCallback* callback) {
    946   LOG_F(LS_INFO) << "channel " << video_channel;
    947   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    948   ViEChannel* vie_channel = cs.Channel(video_channel);
    949   if (!vie_channel) {
    950     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    951     return -1;
    952   }
    953   vie_channel->RegisterSendChannelRtcpStatisticsCallback(NULL);
    954   return 0;
    955 }
    956 
    957 int ViERTP_RTCPImpl::RegisterReceiveChannelRtcpStatisticsCallback(
    958     const int video_channel,
    959     RtcpStatisticsCallback* callback) {
    960   LOG_F(LS_INFO) << "channel " << video_channel;
    961   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    962   ViEChannel* vie_channel = cs.Channel(video_channel);
    963   assert(vie_channel != NULL);
    964   vie_channel->RegisterReceiveChannelRtcpStatisticsCallback(callback);
    965   return 0;
    966 }
    967 
    968 int ViERTP_RTCPImpl::DeregisterReceiveChannelRtcpStatisticsCallback(
    969     const int video_channel,
    970     RtcpStatisticsCallback* callback) {
    971   LOG_F(LS_INFO) << "channel " << video_channel;
    972   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    973   ViEChannel* vie_channel = cs.Channel(video_channel);
    974   assert(vie_channel != NULL);
    975   vie_channel->RegisterReceiveChannelRtcpStatisticsCallback(NULL);
    976   return 0;
    977 }
    978 
    979 int ViERTP_RTCPImpl::RegisterSendChannelRtpStatisticsCallback(
    980     int video_channel, StreamDataCountersCallback* callback) {
    981   LOG_F(LS_INFO) << "channel " << video_channel;
    982   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    983   ViEChannel* vie_channel = cs.Channel(video_channel);
    984   assert(vie_channel != NULL);
    985   vie_channel->RegisterSendChannelRtpStatisticsCallback(callback);
    986   return 0;
    987 }
    988 
    989 int ViERTP_RTCPImpl::DeregisterSendChannelRtpStatisticsCallback(
    990     int video_channel, StreamDataCountersCallback* callback) {
    991   LOG_F(LS_INFO) << "channel " << video_channel;
    992   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    993   ViEChannel* vie_channel = cs.Channel(video_channel);
    994   assert(vie_channel != NULL);
    995   vie_channel->RegisterSendChannelRtpStatisticsCallback(NULL);
    996   return 0;
    997 }
    998 
    999 int ViERTP_RTCPImpl::RegisterReceiveChannelRtpStatisticsCallback(
   1000     const int video_channel,
   1001     StreamDataCountersCallback* callback) {
   1002   LOG_F(LS_INFO) << "channel " << video_channel;
   1003   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1004   ViEChannel* vie_channel = cs.Channel(video_channel);
   1005   assert(vie_channel != NULL);
   1006   vie_channel->RegisterReceiveChannelRtpStatisticsCallback(callback);
   1007   return 0;
   1008 }
   1009 
   1010 int ViERTP_RTCPImpl::DeregisterReceiveChannelRtpStatisticsCallback(
   1011     const int video_channel,
   1012     StreamDataCountersCallback* callback) {
   1013   LOG_F(LS_INFO) << "channel " << video_channel;
   1014   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1015   ViEChannel* vie_channel = cs.Channel(video_channel);
   1016   assert(vie_channel != NULL);
   1017   vie_channel->RegisterReceiveChannelRtpStatisticsCallback(NULL);
   1018   return 0;
   1019 }
   1020 
   1021 // Called whenever the send bitrate is updated.
   1022 int ViERTP_RTCPImpl::RegisterSendBitrateObserver(
   1023     const int video_channel,
   1024     BitrateStatisticsObserver* observer) {
   1025   LOG_F(LS_INFO) << "channel " << video_channel;
   1026   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1027   ViEChannel* vie_channel = cs.Channel(video_channel);
   1028   assert(vie_channel != NULL);
   1029   vie_channel->RegisterSendBitrateObserver(observer);
   1030   return 0;
   1031 }
   1032 
   1033 int ViERTP_RTCPImpl::DeregisterSendBitrateObserver(
   1034     const int video_channel,
   1035     BitrateStatisticsObserver* observer) {
   1036   LOG_F(LS_INFO) << "channel " << video_channel;
   1037   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1038   ViEChannel* vie_channel = cs.Channel(video_channel);
   1039   assert(vie_channel != NULL);
   1040   vie_channel->RegisterSendBitrateObserver(NULL);
   1041   return 0;
   1042 }
   1043 
   1044 int ViERTP_RTCPImpl::RegisterSendFrameCountObserver(
   1045     int video_channel, FrameCountObserver* callback) {
   1046   LOG_F(LS_INFO) << "channel " << video_channel;
   1047   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1048   ViEChannel* vie_channel = cs.Channel(video_channel);
   1049   if (!vie_channel) {
   1050     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
   1051     return -1;
   1052   }
   1053   vie_channel->RegisterSendFrameCountObserver(callback);
   1054   return 0;
   1055 }
   1056 
   1057 int ViERTP_RTCPImpl::DeregisterSendFrameCountObserver(
   1058     int video_channel, FrameCountObserver* callback) {
   1059   LOG_F(LS_INFO) << "channel " << video_channel;
   1060   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1061   ViEChannel* vie_channel = cs.Channel(video_channel);
   1062   if (!vie_channel) {
   1063     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
   1064     return -1;
   1065   }
   1066   vie_channel->RegisterSendFrameCountObserver(NULL);
   1067   return 0;
   1068 }
   1069 }  // namespace webrtc
   1070