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: " << static_cast<int>(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: " << static_cast<int>(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 void ViERTP_RTCPImpl::SetRtpStateForSsrc(int video_channel,
    260                                          uint32_t ssrc,
    261                                          const RtpState& rtp_state) {
    262   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    263   ViEChannel* vie_channel = cs.Channel(video_channel);
    264   if (!vie_channel)
    265     return;
    266 
    267   if (vie_channel->Sending()) {
    268     LOG_F(LS_ERROR) << "channel " << video_channel << " is already sending.";
    269     return;
    270   }
    271   vie_channel->SetRtpStateForSsrc(ssrc, rtp_state);
    272 }
    273 
    274 RtpState ViERTP_RTCPImpl::GetRtpStateForSsrc(int video_channel, uint32_t ssrc) {
    275   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    276   ViEChannel* vie_channel = cs.Channel(video_channel);
    277   if (!vie_channel)
    278     return RtpState();
    279 
    280   return vie_channel->GetRtpStateForSsrc(ssrc);
    281 }
    282 
    283 int ViERTP_RTCPImpl::SetRTCPStatus(const int video_channel,
    284                                    const ViERTCPMode rtcp_mode) {
    285   LOG_F(LS_INFO) << "channel: " << video_channel
    286                  << " mode: " << static_cast<int>(rtcp_mode);
    287   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    288   ViEChannel* vie_channel = cs.Channel(video_channel);
    289   if (!vie_channel) {
    290     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    291     return -1;
    292   }
    293 
    294   RTCPMethod module_mode = ViERTCPModeToRTCPMethod(rtcp_mode);
    295   if (vie_channel->SetRTCPMode(module_mode) != 0) {
    296     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    297     return -1;
    298   }
    299   return 0;
    300 }
    301 
    302 int ViERTP_RTCPImpl::GetRTCPStatus(const int video_channel,
    303                                    ViERTCPMode& rtcp_mode) const {
    304   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    305   ViEChannel* vie_channel = cs.Channel(video_channel);
    306   if (!vie_channel) {
    307     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    308     return -1;
    309   }
    310   RTCPMethod module_mode = kRtcpOff;
    311   if (vie_channel->GetRTCPMode(&module_mode) != 0) {
    312     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    313     return -1;
    314   }
    315   rtcp_mode = RTCPMethodToViERTCPMode(module_mode);
    316   return 0;
    317 }
    318 
    319 int ViERTP_RTCPImpl::SetRTCPCName(const int video_channel,
    320                                   const char rtcp_cname[KMaxRTCPCNameLength]) {
    321   LOG_F(LS_INFO) << "channel: " << video_channel
    322                  << " rtcp_cname: " << rtcp_cname;
    323   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    324   ViEChannel* vie_channel = cs.Channel(video_channel);
    325   if (!vie_channel) {
    326     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    327     return -1;
    328   }
    329   if (vie_channel->Sending()) {
    330     LOG_F(LS_ERROR) << "channel " << video_channel << " is already sending.";
    331     shared_data_->SetLastError(kViERtpRtcpAlreadySending);
    332     return -1;
    333   }
    334   if (vie_channel->SetRTCPCName(rtcp_cname) != 0) {
    335     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    336     return -1;
    337   }
    338   return 0;
    339 }
    340 
    341 int ViERTP_RTCPImpl::GetRemoteRTCPCName(
    342     const int video_channel,
    343     char rtcp_cname[KMaxRTCPCNameLength]) const {
    344   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    345   ViEChannel* vie_channel = cs.Channel(video_channel);
    346   if (!vie_channel) {
    347     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    348     return -1;
    349   }
    350   if (vie_channel->GetRemoteRTCPCName(rtcp_cname) != 0) {
    351     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    352     return -1;
    353   }
    354   return 0;
    355 }
    356 
    357 int ViERTP_RTCPImpl::SendApplicationDefinedRTCPPacket(
    358   const int video_channel,
    359   const unsigned char sub_type,
    360   unsigned int name,
    361   const char* data,
    362   uint16_t data_length_in_bytes) {
    363   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    364   ViEChannel* vie_channel = cs.Channel(video_channel);
    365   if (!vie_channel) {
    366     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    367     return -1;
    368   }
    369   if (!vie_channel->Sending()) {
    370     shared_data_->SetLastError(kViERtpRtcpNotSending);
    371     return -1;
    372   }
    373   RTCPMethod method;
    374   if (vie_channel->GetRTCPMode(&method) != 0 || method == kRtcpOff) {
    375     shared_data_->SetLastError(kViERtpRtcpRtcpDisabled);
    376     return -1;
    377   }
    378   if (vie_channel->SendApplicationDefinedRTCPPacket(
    379         sub_type, name, reinterpret_cast<const uint8_t*>(data),
    380         data_length_in_bytes) != 0) {
    381     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    382     return -1;
    383   }
    384   return 0;
    385 }
    386 
    387 int ViERTP_RTCPImpl::SetNACKStatus(const int video_channel, const bool enable) {
    388   LOG_F(LS_INFO) << "channel: " << video_channel << " "
    389                  << (enable ? "on" : "off");
    390   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    391   ViEChannel* vie_channel = cs.Channel(video_channel);
    392   if (!vie_channel) {
    393     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    394     return -1;
    395   }
    396   if (vie_channel->SetNACKStatus(enable) != 0) {
    397     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    398     return -1;
    399   }
    400 
    401   // Update the encoder
    402   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    403   if (!vie_encoder) {
    404     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    405     return -1;
    406   }
    407   vie_encoder->UpdateProtectionMethod(enable);
    408   return 0;
    409 }
    410 
    411 int ViERTP_RTCPImpl::SetFECStatus(const int video_channel, const bool enable,
    412                                   const unsigned char payload_typeRED,
    413                                   const unsigned char payload_typeFEC) {
    414   LOG_F(LS_INFO) << "channel: " << video_channel
    415                  << " enable: " << (enable ? "on" : "off")
    416                  << " payload_typeRED: " << static_cast<int>(payload_typeRED)
    417                  << " payload_typeFEC: " << static_cast<int>(payload_typeFEC);
    418   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    419   ViEChannel* vie_channel = cs.Channel(video_channel);
    420   if (!vie_channel) {
    421     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    422     return -1;
    423   }
    424   if (vie_channel->SetFECStatus(enable, payload_typeRED,
    425                                 payload_typeFEC) != 0) {
    426     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    427     return -1;
    428   }
    429   // Update the encoder.
    430   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    431   if (!vie_encoder) {
    432     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    433     return -1;
    434   }
    435   vie_encoder->UpdateProtectionMethod(false);
    436   return 0;
    437 }
    438 
    439 int ViERTP_RTCPImpl::SetHybridNACKFECStatus(
    440     const int video_channel,
    441     const bool enable,
    442     const unsigned char payload_typeRED,
    443     const unsigned char payload_typeFEC) {
    444   LOG_F(LS_INFO) << "channel: " << video_channel
    445                  << " enable: " << (enable ? "on" : "off")
    446                  << " payload_typeRED: " << static_cast<int>(payload_typeRED)
    447                  << " payload_typeFEC: " << static_cast<int>(payload_typeFEC);
    448   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    449   ViEChannel* vie_channel = cs.Channel(video_channel);
    450   if (!vie_channel) {
    451     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    452     return -1;
    453   }
    454 
    455   // Update the channel status with hybrid NACK FEC mode.
    456   if (vie_channel->SetHybridNACKFECStatus(enable, payload_typeRED,
    457                                           payload_typeFEC) != 0) {
    458     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    459     return -1;
    460   }
    461 
    462   // Update the encoder.
    463   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    464   if (!vie_encoder) {
    465     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    466     return -1;
    467   }
    468   vie_encoder->UpdateProtectionMethod(enable);
    469   return 0;
    470 }
    471 
    472 int ViERTP_RTCPImpl::SetSenderBufferingMode(int video_channel,
    473                                             int target_delay_ms) {
    474   LOG_F(LS_INFO) << "channel: " << video_channel
    475                  << " target_delay_ms: " << target_delay_ms;
    476   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    477   ViEChannel* vie_channel = cs.Channel(video_channel);
    478   if (!vie_channel) {
    479     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    480     return -1;
    481   }
    482   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    483   if (!vie_encoder) {
    484     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    485     return -1;
    486   }
    487 
    488   // Update the channel with buffering mode settings.
    489   if (vie_channel->SetSenderBufferingMode(target_delay_ms) != 0) {
    490     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    491     return -1;
    492   }
    493 
    494   // Update the encoder's buffering mode settings.
    495   vie_encoder->SetSenderBufferingMode(target_delay_ms);
    496   return 0;
    497 }
    498 
    499 int ViERTP_RTCPImpl::SetReceiverBufferingMode(int video_channel,
    500                                               int target_delay_ms) {
    501   LOG_F(LS_INFO) << "channel: " << video_channel
    502                  << " target_delay_ms: " << target_delay_ms;
    503   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    504   ViEChannel* vie_channel = cs.Channel(video_channel);
    505   if (!vie_channel) {
    506     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    507     return -1;
    508   }
    509 
    510   // Update the channel with buffering mode settings.
    511   if (vie_channel->SetReceiverBufferingMode(target_delay_ms) != 0) {
    512     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    513     return -1;
    514   }
    515   return 0;
    516 }
    517 
    518 int ViERTP_RTCPImpl::SetKeyFrameRequestMethod(
    519   const int video_channel,
    520   const ViEKeyFrameRequestMethod method) {
    521   LOG_F(LS_INFO) << "channel: " << video_channel
    522                  << " method: " << static_cast<int>(method);
    523 
    524   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    525   ViEChannel* vie_channel = cs.Channel(video_channel);
    526   if (!vie_channel) {
    527     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    528     return -1;
    529   }
    530   KeyFrameRequestMethod module_method = APIRequestToModuleRequest(method);
    531   if (vie_channel->SetKeyFrameRequestMethod(module_method) != 0) {
    532     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    533     return -1;
    534   }
    535   return 0;
    536 }
    537 
    538 int ViERTP_RTCPImpl::SetTMMBRStatus(const int video_channel,
    539                                     const bool enable) {
    540   LOG_F(LS_INFO) << "channel: " << video_channel
    541                  << "enable: " << (enable ? "on" : "off");
    542   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    543   ViEChannel* vie_channel = cs.Channel(video_channel);
    544   if (!vie_channel) {
    545     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    546     return -1;
    547   }
    548   if (vie_channel->EnableTMMBR(enable) != 0) {
    549     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    550     return -1;
    551   }
    552   return 0;
    553 }
    554 
    555 int ViERTP_RTCPImpl::SetRembStatus(int video_channel,
    556                                    bool sender,
    557                                    bool receiver) {
    558   LOG_F(LS_INFO) << "channel: " << video_channel
    559                  << " sender: " << (sender ? "on" : "off")
    560                  << " receiver: " << (receiver ? "on" : "off");
    561   if (!shared_data_->channel_manager()->SetRembStatus(video_channel, sender,
    562                                                       receiver)) {
    563     return -1;
    564   }
    565   return 0;
    566 }
    567 
    568 int ViERTP_RTCPImpl::SetSendTimestampOffsetStatus(int video_channel,
    569                                                   bool enable,
    570                                                   int id) {
    571   LOG_F(LS_INFO) << "channel: " << video_channel
    572                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    573 
    574   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    575   ViEChannel* vie_channel = cs.Channel(video_channel);
    576   if (!vie_channel) {
    577     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    578     return -1;
    579   }
    580   if (vie_channel->SetSendTimestampOffsetStatus(enable, id) != 0) {
    581     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    582     return -1;
    583   }
    584   return 0;
    585 }
    586 
    587 int ViERTP_RTCPImpl::SetReceiveTimestampOffsetStatus(int video_channel,
    588                                                      bool enable,
    589                                                      int id) {
    590   LOG_F(LS_INFO) << "channel: " << video_channel
    591                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    592   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    593   ViEChannel* vie_channel = cs.Channel(video_channel);
    594   if (!vie_channel) {
    595     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    596     return -1;
    597   }
    598   if (vie_channel->SetReceiveTimestampOffsetStatus(enable, id) != 0) {
    599     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    600     return -1;
    601   }
    602   return 0;
    603 }
    604 
    605 int ViERTP_RTCPImpl::SetSendAbsoluteSendTimeStatus(int video_channel,
    606                                                    bool enable,
    607                                                    int id) {
    608   LOG_F(LS_INFO) << "channel: " << video_channel
    609                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    610 
    611   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    612   ViEChannel* vie_channel = cs.Channel(video_channel);
    613   if (!vie_channel) {
    614     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    615     return -1;
    616   }
    617   if (vie_channel->SetSendAbsoluteSendTimeStatus(enable, id) != 0) {
    618     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    619     return -1;
    620   }
    621   return 0;
    622 }
    623 
    624 int ViERTP_RTCPImpl::SetReceiveAbsoluteSendTimeStatus(int video_channel,
    625                                                       bool enable,
    626                                                       int id) {
    627   LOG_F(LS_INFO) << "channel: " << video_channel
    628                  << "enable: " << (enable ? "on" : "off") << " id: " << id;
    629   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    630   ViEChannel* vie_channel = cs.Channel(video_channel);
    631   if (!vie_channel) {
    632     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    633     return -1;
    634   }
    635   if (vie_channel->SetReceiveAbsoluteSendTimeStatus(enable, id) != 0) {
    636     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    637     return -1;
    638   }
    639   return 0;
    640 }
    641 
    642 int ViERTP_RTCPImpl::SetRtcpXrRrtrStatus(int video_channel, bool enable) {
    643   LOG_F(LS_INFO) << "channel: " << video_channel
    644                  << " enable: " << (enable ? "on" : "off");
    645 
    646   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    647   ViEChannel* vie_channel = cs.Channel(video_channel);
    648   if (!vie_channel) {
    649     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    650     return -1;
    651   }
    652   vie_channel->SetRtcpXrRrtrStatus(enable);
    653   return 0;
    654 }
    655 
    656 int ViERTP_RTCPImpl::SetTransmissionSmoothingStatus(int video_channel,
    657                                                     bool enable) {
    658   LOG_F(LS_INFO) << "channel: " << video_channel
    659                  << " enable: " << (enable ? "on" : "off");
    660   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    661   ViEChannel* vie_channel = cs.Channel(video_channel);
    662   if (!vie_channel) {
    663     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    664     return -1;
    665   }
    666   vie_channel->SetTransmissionSmoothingStatus(enable);
    667   return 0;
    668 }
    669 
    670 int ViERTP_RTCPImpl::SetMinTransmitBitrate(int video_channel,
    671                                            int min_transmit_bitrate_kbps) {
    672   LOG_F(LS_INFO) << "channel: " << video_channel
    673                  << " min_transmit_bitrate_kbps: " << min_transmit_bitrate_kbps;
    674   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    675   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    676   if (vie_encoder == NULL)
    677     return -1;
    678   vie_encoder->SetMinTransmitBitrate(min_transmit_bitrate_kbps);
    679   return 0;
    680 }
    681 
    682 int ViERTP_RTCPImpl::SetReservedTransmitBitrate(
    683     int video_channel, unsigned int reserved_transmit_bitrate_bps) {
    684   LOG_F(LS_INFO) << "channel: " << video_channel
    685                  << " reserved_transmit_bitrate_bps: "
    686                  << reserved_transmit_bitrate_bps;
    687   if (!shared_data_->channel_manager()->SetReservedTransmitBitrate(
    688       video_channel, reserved_transmit_bitrate_bps)) {
    689     return -1;
    690   }
    691   return 0;
    692 }
    693 
    694 int ViERTP_RTCPImpl::GetReceiveChannelRtcpStatistics(
    695     const int video_channel,
    696     RtcpStatistics& basic_stats,
    697     int& rtt_ms) const {
    698   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    699   ViEChannel* vie_channel = cs.Channel(video_channel);
    700   if (!vie_channel) {
    701     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    702     return -1;
    703   }
    704 
    705   // TODO(sprang): Clean this up when stats struct is propagated all the way.
    706   uint16_t frac_lost;
    707   if (vie_channel->GetReceivedRtcpStatistics(
    708           &frac_lost,
    709           &basic_stats.cumulative_lost,
    710           &basic_stats.extended_max_sequence_number,
    711           &basic_stats.jitter,
    712           &rtt_ms) != 0) {
    713     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    714     return -1;
    715   }
    716   basic_stats.fraction_lost = frac_lost;
    717   return 0;
    718 }
    719 
    720 int ViERTP_RTCPImpl::GetSendChannelRtcpStatistics(const int video_channel,
    721                                                   RtcpStatistics& basic_stats,
    722                                                   int& rtt_ms) const {
    723   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    724   ViEChannel* vie_channel = cs.Channel(video_channel);
    725   if (!vie_channel) {
    726     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    727     return -1;
    728   }
    729 
    730   // TODO(sprang): Clean this up when stats struct is propagated all the way.
    731   uint16_t frac_lost;
    732   if (vie_channel->GetSendRtcpStatistics(
    733           &frac_lost,
    734           &basic_stats.cumulative_lost,
    735           &basic_stats.extended_max_sequence_number,
    736           &basic_stats.jitter,
    737           &rtt_ms) != 0) {
    738     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    739     return -1;
    740   }
    741   basic_stats.fraction_lost = frac_lost;
    742   return 0;
    743 }
    744 
    745 int ViERTP_RTCPImpl::GetRtpStatistics(const int video_channel,
    746                                       StreamDataCounters& sent,
    747                                       StreamDataCounters& received) const {
    748   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    749   ViEChannel* vie_channel = cs.Channel(video_channel);
    750   if (!vie_channel) {
    751     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    752     return -1;
    753   }
    754   if (vie_channel->GetRtpStatistics(&sent.bytes,
    755                                     &sent.packets,
    756                                     &received.bytes,
    757                                     &received.packets) != 0) {
    758     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    759     return -1;
    760   }
    761   return 0;
    762 }
    763 
    764 int ViERTP_RTCPImpl::GetRtcpPacketTypeCounters(
    765     int video_channel,
    766     RtcpPacketTypeCounter* packets_sent,
    767     RtcpPacketTypeCounter* packets_received) const {
    768   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    769   ViEChannel* vie_channel = cs.Channel(video_channel);
    770   if (!vie_channel) {
    771     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    772     return -1;
    773   }
    774   vie_channel->GetRtcpPacketTypeCounters(packets_sent, packets_received);
    775   return 0;
    776 }
    777 
    778 int ViERTP_RTCPImpl::GetBandwidthUsage(const int video_channel,
    779                                        unsigned int& total_bitrate_sent,
    780                                        unsigned int& video_bitrate_sent,
    781                                        unsigned int& fec_bitrate_sent,
    782                                        unsigned int& nackBitrateSent) const {
    783   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    784   ViEChannel* vie_channel = cs.Channel(video_channel);
    785   if (!vie_channel) {
    786     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    787     return -1;
    788   }
    789   vie_channel->GetBandwidthUsage(&total_bitrate_sent,
    790                                  &video_bitrate_sent,
    791                                  &fec_bitrate_sent,
    792                                  &nackBitrateSent);
    793   return 0;
    794 }
    795 
    796 int ViERTP_RTCPImpl::GetEstimatedSendBandwidth(
    797     const int video_channel,
    798     unsigned int* estimated_bandwidth) const {
    799   if (!shared_data_->channel_manager()->GetEstimatedSendBandwidth(
    800       video_channel, estimated_bandwidth)) {
    801     return -1;
    802   }
    803   return 0;
    804 }
    805 
    806 int ViERTP_RTCPImpl::GetEstimatedReceiveBandwidth(
    807     const int video_channel,
    808     unsigned int* estimated_bandwidth) const {
    809   if (!shared_data_->channel_manager()->GetEstimatedReceiveBandwidth(
    810       video_channel, estimated_bandwidth)) {
    811     return -1;
    812   }
    813   return 0;
    814 }
    815 
    816 int ViERTP_RTCPImpl::GetReceiveBandwidthEstimatorStats(
    817     const int video_channel,
    818     ReceiveBandwidthEstimatorStats* output) const {
    819   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    820   ViEChannel* vie_channel = cs.Channel(video_channel);
    821   if (!vie_channel) {
    822     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    823     return -1;
    824   }
    825   vie_channel->GetReceiveBandwidthEstimatorStats(output);
    826   return 0;
    827 }
    828 
    829 int ViERTP_RTCPImpl::GetPacerQueuingDelayMs(
    830     const int video_channel, int* delay_ms) const {
    831   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    832   ViEEncoder* vie_encoder = cs.Encoder(video_channel);
    833   if (!vie_encoder) {
    834     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    835     return -1;
    836   }
    837   *delay_ms = vie_encoder->PacerQueuingDelayMs();
    838   return 0;
    839 }
    840 
    841 int ViERTP_RTCPImpl::StartRTPDump(const int video_channel,
    842                                   const char file_nameUTF8[1024],
    843                                   RTPDirections direction) {
    844   LOG_F(LS_INFO) << "channel: " << video_channel
    845                  << " filename: " << file_nameUTF8
    846                  << " direction: " << static_cast<int>(direction);
    847   assert(FileWrapper::kMaxFileNameSize == 1024);
    848   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    849   ViEChannel* vie_channel = cs.Channel(video_channel);
    850   if (!vie_channel) {
    851     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    852     return -1;
    853   }
    854   if (vie_channel->StartRTPDump(file_nameUTF8, direction) != 0) {
    855     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    856     return -1;
    857   }
    858   return 0;
    859 }
    860 
    861 int ViERTP_RTCPImpl::StopRTPDump(const int video_channel,
    862                                  RTPDirections direction) {
    863   LOG_F(LS_INFO) << "channel: " << video_channel
    864                  << " direction: " << static_cast<int>(direction);
    865   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    866   ViEChannel* vie_channel = cs.Channel(video_channel);
    867   if (!vie_channel) {
    868     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    869     return -1;
    870   }
    871   if (vie_channel->StopRTPDump(direction) != 0) {
    872     shared_data_->SetLastError(kViERtpRtcpUnknownError);
    873     return -1;
    874   }
    875   return 0;
    876 }
    877 
    878 int ViERTP_RTCPImpl::RegisterRTPObserver(const int video_channel,
    879                                          ViERTPObserver& observer) {
    880   LOG_F(LS_INFO) << "channel " << video_channel;
    881   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    882   ViEChannel* vie_channel = cs.Channel(video_channel);
    883   if (!vie_channel) {
    884     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    885     return -1;
    886   }
    887   if (vie_channel->RegisterRtpObserver(&observer) != 0) {
    888     shared_data_->SetLastError(kViERtpRtcpObserverAlreadyRegistered);
    889     return -1;
    890   }
    891   return 0;
    892 }
    893 
    894 int ViERTP_RTCPImpl::DeregisterRTPObserver(const int video_channel) {
    895   LOG_F(LS_INFO) << "channel " << video_channel;
    896   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    897   ViEChannel* vie_channel = cs.Channel(video_channel);
    898   if (!vie_channel) {
    899     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    900     return -1;
    901   }
    902   if (vie_channel->RegisterRtpObserver(NULL) != 0) {
    903     shared_data_->SetLastError(kViERtpRtcpObserverNotRegistered);
    904     return -1;
    905   }
    906   return 0;
    907 }
    908 
    909 int ViERTP_RTCPImpl::RegisterRTCPObserver(const int video_channel,
    910                                           ViERTCPObserver& observer) {
    911   LOG_F(LS_INFO) << "channel " << video_channel;
    912   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    913   ViEChannel* vie_channel = cs.Channel(video_channel);
    914   if (!vie_channel) {
    915     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    916     return -1;
    917   }
    918   if (vie_channel->RegisterRtcpObserver(&observer) != 0) {
    919     shared_data_->SetLastError(kViERtpRtcpObserverAlreadyRegistered);
    920     return -1;
    921   }
    922   return 0;
    923 }
    924 
    925 int ViERTP_RTCPImpl::DeregisterRTCPObserver(const int video_channel) {
    926   LOG_F(LS_INFO) << "channel " << video_channel;
    927   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    928   ViEChannel* vie_channel = cs.Channel(video_channel);
    929   if (!vie_channel) {
    930     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    931     return -1;
    932   }
    933   if (vie_channel->RegisterRtcpObserver(NULL) != 0) {
    934     shared_data_->SetLastError(kViERtpRtcpObserverNotRegistered);
    935     return -1;
    936   }
    937   return 0;
    938 }
    939 
    940 int ViERTP_RTCPImpl::RegisterSendChannelRtcpStatisticsCallback(
    941   int video_channel, RtcpStatisticsCallback* callback) {
    942   LOG_F(LS_INFO) << "channel " << video_channel;
    943   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    944   ViEChannel* vie_channel = cs.Channel(video_channel);
    945   if (!vie_channel) {
    946     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    947     return -1;
    948   }
    949   vie_channel->RegisterSendChannelRtcpStatisticsCallback(callback);
    950   return 0;
    951 }
    952 
    953 int ViERTP_RTCPImpl::DeregisterSendChannelRtcpStatisticsCallback(
    954     int video_channel, RtcpStatisticsCallback* callback) {
    955   LOG_F(LS_INFO) << "channel " << video_channel;
    956   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    957   ViEChannel* vie_channel = cs.Channel(video_channel);
    958   if (!vie_channel) {
    959     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
    960     return -1;
    961   }
    962   vie_channel->RegisterSendChannelRtcpStatisticsCallback(NULL);
    963   return 0;
    964 }
    965 
    966 int ViERTP_RTCPImpl::RegisterReceiveChannelRtcpStatisticsCallback(
    967     const int video_channel,
    968     RtcpStatisticsCallback* callback) {
    969   LOG_F(LS_INFO) << "channel " << video_channel;
    970   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    971   ViEChannel* vie_channel = cs.Channel(video_channel);
    972   assert(vie_channel != NULL);
    973   vie_channel->RegisterReceiveChannelRtcpStatisticsCallback(callback);
    974   return 0;
    975 }
    976 
    977 int ViERTP_RTCPImpl::DeregisterReceiveChannelRtcpStatisticsCallback(
    978     const int video_channel,
    979     RtcpStatisticsCallback* callback) {
    980   LOG_F(LS_INFO) << "channel " << video_channel;
    981   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    982   ViEChannel* vie_channel = cs.Channel(video_channel);
    983   assert(vie_channel != NULL);
    984   vie_channel->RegisterReceiveChannelRtcpStatisticsCallback(NULL);
    985   return 0;
    986 }
    987 
    988 int ViERTP_RTCPImpl::RegisterSendChannelRtpStatisticsCallback(
    989     int video_channel, StreamDataCountersCallback* callback) {
    990   LOG_F(LS_INFO) << "channel " << video_channel;
    991   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
    992   ViEChannel* vie_channel = cs.Channel(video_channel);
    993   assert(vie_channel != NULL);
    994   vie_channel->RegisterSendChannelRtpStatisticsCallback(callback);
    995   return 0;
    996 }
    997 
    998 int ViERTP_RTCPImpl::DeregisterSendChannelRtpStatisticsCallback(
    999     int video_channel, StreamDataCountersCallback* callback) {
   1000   LOG_F(LS_INFO) << "channel " << video_channel;
   1001   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1002   ViEChannel* vie_channel = cs.Channel(video_channel);
   1003   assert(vie_channel != NULL);
   1004   vie_channel->RegisterSendChannelRtpStatisticsCallback(NULL);
   1005   return 0;
   1006 }
   1007 
   1008 int ViERTP_RTCPImpl::RegisterReceiveChannelRtpStatisticsCallback(
   1009     const int video_channel,
   1010     StreamDataCountersCallback* callback) {
   1011   LOG_F(LS_INFO) << "channel " << video_channel;
   1012   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1013   ViEChannel* vie_channel = cs.Channel(video_channel);
   1014   assert(vie_channel != NULL);
   1015   vie_channel->RegisterReceiveChannelRtpStatisticsCallback(callback);
   1016   return 0;
   1017 }
   1018 
   1019 int ViERTP_RTCPImpl::DeregisterReceiveChannelRtpStatisticsCallback(
   1020     const int video_channel,
   1021     StreamDataCountersCallback* callback) {
   1022   LOG_F(LS_INFO) << "channel " << video_channel;
   1023   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1024   ViEChannel* vie_channel = cs.Channel(video_channel);
   1025   assert(vie_channel != NULL);
   1026   vie_channel->RegisterReceiveChannelRtpStatisticsCallback(NULL);
   1027   return 0;
   1028 }
   1029 
   1030 // Called whenever the send bitrate is updated.
   1031 int ViERTP_RTCPImpl::RegisterSendBitrateObserver(
   1032     const int video_channel,
   1033     BitrateStatisticsObserver* observer) {
   1034   LOG_F(LS_INFO) << "channel " << video_channel;
   1035   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1036   ViEChannel* vie_channel = cs.Channel(video_channel);
   1037   assert(vie_channel != NULL);
   1038   vie_channel->RegisterSendBitrateObserver(observer);
   1039   return 0;
   1040 }
   1041 
   1042 int ViERTP_RTCPImpl::DeregisterSendBitrateObserver(
   1043     const int video_channel,
   1044     BitrateStatisticsObserver* observer) {
   1045   LOG_F(LS_INFO) << "channel " << video_channel;
   1046   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1047   ViEChannel* vie_channel = cs.Channel(video_channel);
   1048   assert(vie_channel != NULL);
   1049   vie_channel->RegisterSendBitrateObserver(NULL);
   1050   return 0;
   1051 }
   1052 
   1053 int ViERTP_RTCPImpl::RegisterSendFrameCountObserver(
   1054     int video_channel, FrameCountObserver* callback) {
   1055   LOG_F(LS_INFO) << "channel " << video_channel;
   1056   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1057   ViEChannel* vie_channel = cs.Channel(video_channel);
   1058   if (!vie_channel) {
   1059     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
   1060     return -1;
   1061   }
   1062   vie_channel->RegisterSendFrameCountObserver(callback);
   1063   return 0;
   1064 }
   1065 
   1066 int ViERTP_RTCPImpl::DeregisterSendFrameCountObserver(
   1067     int video_channel, FrameCountObserver* callback) {
   1068   LOG_F(LS_INFO) << "channel " << video_channel;
   1069   ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
   1070   ViEChannel* vie_channel = cs.Channel(video_channel);
   1071   if (!vie_channel) {
   1072     shared_data_->SetLastError(kViERtpRtcpInvalidChannelId);
   1073     return -1;
   1074   }
   1075   vie_channel->RegisterSendFrameCountObserver(NULL);
   1076   return 0;
   1077 }
   1078 }  // namespace webrtc
   1079