Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2010 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_MEDIA_WEBRTC_FAKEWEBRTCVIDEOENGINE_H_
     29 #define TALK_MEDIA_WEBRTC_FAKEWEBRTCVIDEOENGINE_H_
     30 
     31 #include <map>
     32 #include <set>
     33 #include <vector>
     34 
     35 #include "talk/base/basictypes.h"
     36 #include "talk/base/gunit.h"
     37 #include "talk/base/stringutils.h"
     38 #include "talk/media/base/codec.h"
     39 #include "talk/media/webrtc/fakewebrtccommon.h"
     40 #include "talk/media/webrtc/webrtcvideodecoderfactory.h"
     41 #include "talk/media/webrtc/webrtcvideoencoderfactory.h"
     42 #include "talk/media/webrtc/webrtcvie.h"
     43 
     44 namespace cricket {
     45 
     46 #define WEBRTC_CHECK_CAPTURER(capturer) \
     47   if (capturers_.find(capturer) == capturers_.end()) return -1;
     48 
     49 #define WEBRTC_ASSERT_CAPTURER(capturer) \
     50   ASSERT(capturers_.find(capturer) != capturers_.end());
     51 
     52 static const int kMinVideoBitrate = 100;
     53 static const int kStartVideoBitrate = 300;
     54 static const int kMaxVideoBitrate = 1000;
     55 
     56 // WebRtc channel id and capture id share the same number space.
     57 // This is how AddRenderer(renderId, ...) is able to tell if it is adding a
     58 // renderer for a channel or it is adding a renderer for a capturer.
     59 static const int kViEChannelIdBase = 0;
     60 static const int kViEChannelIdMax = 1000;
     61 static const int kViECaptureIdBase = 10000;  // Make sure there is a gap.
     62 static const int kViECaptureIdMax = 11000;
     63 
     64 // Fake class for mocking out webrtc::VideoDecoder
     65 class FakeWebRtcVideoDecoder : public webrtc::VideoDecoder {
     66  public:
     67   FakeWebRtcVideoDecoder()
     68       : num_frames_received_(0) {
     69   }
     70 
     71   virtual int32 InitDecode(const webrtc::VideoCodec*, int32) {
     72     return WEBRTC_VIDEO_CODEC_OK;
     73   }
     74 
     75   virtual int32 Decode(
     76       const webrtc::EncodedImage&, bool, const webrtc::RTPFragmentationHeader*,
     77       const webrtc::CodecSpecificInfo*, int64) {
     78     num_frames_received_++;
     79     return WEBRTC_VIDEO_CODEC_OK;
     80   }
     81 
     82   virtual int32 RegisterDecodeCompleteCallback(
     83       webrtc::DecodedImageCallback*) {
     84     return WEBRTC_VIDEO_CODEC_OK;
     85   }
     86 
     87   virtual int32 Release() {
     88     return WEBRTC_VIDEO_CODEC_OK;
     89   }
     90 
     91   virtual int32 Reset() {
     92     return WEBRTC_VIDEO_CODEC_OK;
     93   }
     94 
     95   int GetNumFramesReceived() const {
     96     return num_frames_received_;
     97   }
     98 
     99  private:
    100   int num_frames_received_;
    101 };
    102 
    103 // Fake class for mocking out WebRtcVideoDecoderFactory.
    104 class FakeWebRtcVideoDecoderFactory : public WebRtcVideoDecoderFactory {
    105  public:
    106   FakeWebRtcVideoDecoderFactory()
    107       : num_created_decoders_(0) {
    108   }
    109 
    110   virtual webrtc::VideoDecoder* CreateVideoDecoder(
    111       webrtc::VideoCodecType type) {
    112     if (supported_codec_types_.count(type) == 0) {
    113       return NULL;
    114     }
    115     FakeWebRtcVideoDecoder* decoder = new FakeWebRtcVideoDecoder();
    116     decoders_.push_back(decoder);
    117     num_created_decoders_++;
    118     return decoder;
    119   }
    120 
    121   virtual void DestroyVideoDecoder(webrtc::VideoDecoder* decoder) {
    122     decoders_.erase(
    123         std::remove(decoders_.begin(), decoders_.end(), decoder),
    124         decoders_.end());
    125     delete decoder;
    126   }
    127 
    128   void AddSupportedVideoCodecType(webrtc::VideoCodecType type) {
    129     supported_codec_types_.insert(type);
    130   }
    131 
    132   int GetNumCreatedDecoders() {
    133     return num_created_decoders_;
    134   }
    135 
    136   const std::vector<FakeWebRtcVideoDecoder*>& decoders() {
    137     return decoders_;
    138   }
    139 
    140  private:
    141   std::set<webrtc::VideoCodecType> supported_codec_types_;
    142   std::vector<FakeWebRtcVideoDecoder*> decoders_;
    143   int num_created_decoders_;
    144 };
    145 
    146 // Fake class for mocking out webrtc::VideoEnoder
    147 class FakeWebRtcVideoEncoder : public webrtc::VideoEncoder {
    148  public:
    149   FakeWebRtcVideoEncoder() {}
    150 
    151   virtual int32 InitEncode(const webrtc::VideoCodec* codecSettings,
    152                            int32 numberOfCores,
    153                            uint32 maxPayloadSize) {
    154     return WEBRTC_VIDEO_CODEC_OK;
    155   }
    156 
    157   virtual int32 Encode(
    158       const webrtc::I420VideoFrame& inputImage,
    159             const webrtc::CodecSpecificInfo* codecSpecificInfo,
    160             const std::vector<webrtc::VideoFrameType>* frame_types) {
    161     return WEBRTC_VIDEO_CODEC_OK;
    162   }
    163 
    164   virtual int32 RegisterEncodeCompleteCallback(
    165       webrtc::EncodedImageCallback* callback) {
    166     return WEBRTC_VIDEO_CODEC_OK;
    167   }
    168 
    169   virtual int32 Release() {
    170     return WEBRTC_VIDEO_CODEC_OK;
    171   }
    172 
    173   virtual int32 SetChannelParameters(uint32 packetLoss,
    174                                      int rtt) {
    175     return WEBRTC_VIDEO_CODEC_OK;
    176   }
    177 
    178   virtual int32 SetRates(uint32 newBitRate,
    179                          uint32 frameRate) {
    180     return WEBRTC_VIDEO_CODEC_OK;
    181   }
    182 };
    183 
    184 // Fake class for mocking out WebRtcVideoEncoderFactory.
    185 class FakeWebRtcVideoEncoderFactory : public WebRtcVideoEncoderFactory {
    186  public:
    187   FakeWebRtcVideoEncoderFactory()
    188       : num_created_encoders_(0) {
    189   }
    190 
    191   virtual webrtc::VideoEncoder* CreateVideoEncoder(
    192       webrtc::VideoCodecType type) {
    193     if (supported_codec_types_.count(type) == 0) {
    194       return NULL;
    195     }
    196     FakeWebRtcVideoEncoder* encoder = new FakeWebRtcVideoEncoder();
    197     encoders_.push_back(encoder);
    198     num_created_encoders_++;
    199     return encoder;
    200   }
    201 
    202   virtual void DestroyVideoEncoder(webrtc::VideoEncoder* encoder) {
    203     encoders_.erase(
    204         std::remove(encoders_.begin(), encoders_.end(), encoder),
    205         encoders_.end());
    206     delete encoder;
    207   }
    208 
    209   virtual void AddObserver(WebRtcVideoEncoderFactory::Observer* observer) {
    210     bool inserted = observers_.insert(observer).second;
    211     EXPECT_TRUE(inserted);
    212   }
    213 
    214   virtual void RemoveObserver(WebRtcVideoEncoderFactory::Observer* observer) {
    215     size_t erased = observers_.erase(observer);
    216     EXPECT_EQ(erased, 1UL);
    217   }
    218 
    219   virtual const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs()
    220       const {
    221     return codecs_;
    222   }
    223 
    224   void AddSupportedVideoCodecType(webrtc::VideoCodecType type,
    225                                   const std::string& name) {
    226     supported_codec_types_.insert(type);
    227     codecs_.push_back(
    228         WebRtcVideoEncoderFactory::VideoCodec(type, name, 1280, 720, 30));
    229   }
    230 
    231   void NotifyCodecsAvailable() {
    232     std::set<WebRtcVideoEncoderFactory::Observer*>::iterator it;
    233     for (it = observers_.begin(); it != observers_.end(); ++it)
    234       (*it)->OnCodecsAvailable();
    235   }
    236 
    237   int GetNumCreatedEncoders() {
    238     return num_created_encoders_;
    239   }
    240 
    241   const std::vector<FakeWebRtcVideoEncoder*>& encoders() {
    242     return encoders_;
    243   }
    244 
    245  private:
    246   std::set<webrtc::VideoCodecType> supported_codec_types_;
    247   std::vector<WebRtcVideoEncoderFactory::VideoCodec> codecs_;
    248   std::vector<FakeWebRtcVideoEncoder*> encoders_;
    249   std::set<WebRtcVideoEncoderFactory::Observer*> observers_;
    250   int num_created_encoders_;
    251 };
    252 
    253 class FakeWebRtcVideoEngine
    254     : public webrtc::ViEBase,
    255       public webrtc::ViECodec,
    256       public webrtc::ViECapture,
    257       public webrtc::ViENetwork,
    258       public webrtc::ViERender,
    259       public webrtc::ViERTP_RTCP,
    260       public webrtc::ViEImageProcess,
    261       public webrtc::ViEExternalCodec {
    262  public:
    263   struct Channel {
    264     Channel()
    265         : capture_id_(-1),
    266           original_channel_id_(-1),
    267           has_renderer_(false),
    268           render_started_(false),
    269           send(false),
    270           receive_(false),
    271           can_transmit_(true),
    272           remote_rtx_ssrc_(-1),
    273           rtx_send_payload_type(-1),
    274           rtx_recv_payload_type(-1),
    275           rtcp_status_(webrtc::kRtcpNone),
    276           key_frame_request_method_(webrtc::kViEKeyFrameRequestNone),
    277           tmmbr_(false),
    278           remb_contribute_(false),
    279           remb_bw_partition_(false),
    280           rtp_offset_send_id_(-1),
    281           rtp_offset_receive_id_(-1),
    282           rtp_absolute_send_time_send_id_(-1),
    283           rtp_absolute_send_time_receive_id_(-1),
    284           sender_target_delay_(0),
    285           receiver_target_delay_(0),
    286           transmission_smoothing_(false),
    287           nack_(false),
    288           hybrid_nack_fec_(false),
    289           send_video_bitrate_(0),
    290           send_fec_bitrate_(0),
    291           send_nack_bitrate_(0),
    292           send_bandwidth_(0),
    293           receive_bandwidth_(0),
    294           reserved_transmit_bitrate_bps_(0),
    295           suspend_below_min_bitrate_(false),
    296           overuse_observer_(NULL),
    297           last_recvd_payload_type_(-1) {
    298       ssrcs_[0] = 0;  // default ssrc.
    299       memset(&send_codec, 0, sizeof(send_codec));
    300       memset(&overuse_options_, 0, sizeof(overuse_options_));
    301     }
    302     int capture_id_;
    303     int original_channel_id_;
    304     bool has_renderer_;
    305     bool render_started_;
    306     bool send;
    307     bool receive_;
    308     bool can_transmit_;
    309     std::map<int, int> ssrcs_;
    310     std::map<int, int> rtx_ssrcs_;
    311     int remote_rtx_ssrc_;
    312     int rtx_send_payload_type;
    313     int rtx_recv_payload_type;
    314     std::string cname_;
    315     webrtc::ViERTCPMode rtcp_status_;
    316     webrtc::ViEKeyFrameRequestMethod key_frame_request_method_;
    317     bool tmmbr_;
    318     bool remb_contribute_;   // This channel contributes to the remb report.
    319     bool remb_bw_partition_; // This channel is allocated part of total bw.
    320     int rtp_offset_send_id_;
    321     int rtp_offset_receive_id_;
    322     int rtp_absolute_send_time_send_id_;
    323     int rtp_absolute_send_time_receive_id_;
    324     int sender_target_delay_;
    325     int receiver_target_delay_;
    326     bool transmission_smoothing_;
    327     bool nack_;
    328     bool hybrid_nack_fec_;
    329     std::vector<webrtc::VideoCodec> recv_codecs;
    330     std::set<unsigned int> ext_decoder_pl_types_;
    331     std::set<unsigned int> ext_encoder_pl_types_;
    332     webrtc::VideoCodec send_codec;
    333     unsigned int send_video_bitrate_;
    334     unsigned int send_fec_bitrate_;
    335     unsigned int send_nack_bitrate_;
    336     unsigned int send_bandwidth_;
    337     unsigned int receive_bandwidth_;
    338     unsigned int reserved_transmit_bitrate_bps_;
    339     bool suspend_below_min_bitrate_;
    340     webrtc::CpuOveruseObserver* overuse_observer_;
    341     webrtc::CpuOveruseOptions overuse_options_;
    342     int last_recvd_payload_type_;
    343   };
    344   class Capturer : public webrtc::ViEExternalCapture {
    345    public:
    346     Capturer() : channel_id_(-1), denoising_(false),
    347                  last_capture_time_(0), incoming_frame_num_(0) { }
    348     int channel_id() const { return channel_id_; }
    349     void set_channel_id(int channel_id) { channel_id_ = channel_id; }
    350     bool denoising() const { return denoising_; }
    351     void set_denoising(bool denoising) { denoising_ = denoising; }
    352     int64 last_capture_time() const { return last_capture_time_; }
    353     int incoming_frame_num() const { return incoming_frame_num_; }
    354 
    355     // From ViEExternalCapture
    356     virtual int IncomingFrame(unsigned char* videoFrame,
    357                               unsigned int videoFrameLength,
    358                               unsigned short width,
    359                               unsigned short height,
    360                               webrtc::RawVideoType videoType,
    361                               unsigned long long captureTime) {
    362       return 0;
    363     }
    364     virtual int IncomingFrameI420(
    365         const webrtc::ViEVideoFrameI420& video_frame,
    366         unsigned long long captureTime) {
    367       last_capture_time_ = captureTime;
    368       ++incoming_frame_num_;
    369       return 0;
    370     }
    371 
    372    private:
    373     int channel_id_;
    374     bool denoising_;
    375     int64 last_capture_time_;
    376     int incoming_frame_num_;
    377   };
    378 
    379   FakeWebRtcVideoEngine(const cricket::VideoCodec* const* codecs,
    380                         int num_codecs)
    381       : inited_(false),
    382         last_channel_(kViEChannelIdBase - 1),
    383         fail_create_channel_(false),
    384         last_capturer_(kViECaptureIdBase - 1),
    385         fail_alloc_capturer_(false),
    386         codecs_(codecs),
    387         num_codecs_(num_codecs),
    388         num_set_send_codecs_(0) {
    389   }
    390 
    391   ~FakeWebRtcVideoEngine() {
    392     ASSERT(0 == channels_.size());
    393     ASSERT(0 == capturers_.size());
    394   }
    395   bool IsInited() const { return inited_; }
    396 
    397   int GetLastChannel() const { return last_channel_; }
    398   int GetChannelFromLocalSsrc(int local_ssrc) const {
    399     // ssrcs_[0] is the default local ssrc.
    400     for (std::map<int, Channel*>::const_iterator iter = channels_.begin();
    401          iter != channels_.end(); ++iter) {
    402       if (local_ssrc == iter->second->ssrcs_[0]) {
    403         return iter->first;
    404       }
    405     }
    406     return -1;
    407   }
    408 
    409   int GetNumChannels() const { return static_cast<int>(channels_.size()); }
    410   bool IsChannel(int channel) const {
    411     return (channels_.find(channel) != channels_.end());
    412   }
    413   void set_fail_create_channel(bool fail_create_channel) {
    414     fail_create_channel_ = fail_create_channel;
    415   }
    416 
    417   int GetLastCapturer() const { return last_capturer_; }
    418   int GetNumCapturers() const { return static_cast<int>(capturers_.size()); }
    419   int GetIncomingFrameNum(int channel_id) const {
    420     for (std::map<int, Capturer*>::const_iterator iter = capturers_.begin();
    421          iter != capturers_.end(); ++iter) {
    422       Capturer* capturer = iter->second;
    423       if (capturer->channel_id() == channel_id) {
    424         return capturer->incoming_frame_num();
    425       }
    426     }
    427     return -1;
    428   }
    429   void set_fail_alloc_capturer(bool fail_alloc_capturer) {
    430     fail_alloc_capturer_ = fail_alloc_capturer;
    431   }
    432   int GetNumSetSendCodecs() const { return num_set_send_codecs_; }
    433 
    434   int GetCaptureId(int channel) const {
    435     WEBRTC_ASSERT_CHANNEL(channel);
    436     return channels_.find(channel)->second->capture_id_;
    437   }
    438   int GetOriginalChannelId(int channel) const {
    439     WEBRTC_ASSERT_CHANNEL(channel);
    440     return channels_.find(channel)->second->original_channel_id_;
    441   }
    442   bool GetHasRenderer(int channel) const {
    443     WEBRTC_ASSERT_CHANNEL(channel);
    444     return channels_.find(channel)->second->has_renderer_;
    445   }
    446   bool GetRenderStarted(int channel) const {
    447     WEBRTC_ASSERT_CHANNEL(channel);
    448     return channels_.find(channel)->second->render_started_;
    449   }
    450   bool GetSend(int channel) const {
    451     WEBRTC_ASSERT_CHANNEL(channel);
    452     return channels_.find(channel)->second->send;
    453   }
    454   bool GetReceive(int channel) const {
    455     WEBRTC_ASSERT_CHANNEL(channel);
    456     return channels_.find(channel)->second->receive_;
    457   }
    458   int GetCaptureChannelId(int capture_id) const {
    459     WEBRTC_ASSERT_CAPTURER(capture_id);
    460     return capturers_.find(capture_id)->second->channel_id();
    461   }
    462   bool GetCaptureDenoising(int capture_id) const {
    463     WEBRTC_ASSERT_CAPTURER(capture_id);
    464     return capturers_.find(capture_id)->second->denoising();
    465   }
    466   int64 GetCaptureLastTimestamp(int capture_id) const {
    467     WEBRTC_ASSERT_CAPTURER(capture_id);
    468     return capturers_.find(capture_id)->second->last_capture_time();
    469   }
    470   webrtc::ViERTCPMode GetRtcpStatus(int channel) const {
    471     WEBRTC_ASSERT_CHANNEL(channel);
    472     return channels_.find(channel)->second->rtcp_status_;
    473   }
    474   webrtc::ViEKeyFrameRequestMethod GetKeyFrameRequestMethod(int channel) const {
    475     WEBRTC_ASSERT_CHANNEL(channel);
    476     return channels_.find(channel)->second->key_frame_request_method_;
    477   }
    478   bool GetTmmbrStatus(int channel) const {
    479     WEBRTC_ASSERT_CHANNEL(channel);
    480     return channels_.find(channel)->second->tmmbr_;
    481   }
    482   bool GetRembStatusBwPartition(int channel) const {
    483     WEBRTC_ASSERT_CHANNEL(channel);
    484     return channels_.find(channel)->second->remb_bw_partition_;
    485   }
    486   bool GetRembStatusContribute(int channel) const {
    487     WEBRTC_ASSERT_CHANNEL(channel);
    488     return channels_.find(channel)->second->remb_contribute_;
    489   }
    490   int GetSendRtpExtensionId(int channel, const std::string& extension) {
    491     WEBRTC_ASSERT_CHANNEL(channel);
    492     if (extension == kRtpTimestampOffsetHeaderExtension) {
    493       return channels_.find(channel)->second->rtp_offset_send_id_;
    494     } else if (extension == kRtpAbsoluteSenderTimeHeaderExtension) {
    495       return channels_.find(channel)->second->rtp_absolute_send_time_send_id_;
    496     }
    497     return -1;
    498   }
    499   int GetReceiveRtpExtensionId(int channel, const std::string& extension) {
    500     WEBRTC_ASSERT_CHANNEL(channel);
    501     if (extension == kRtpTimestampOffsetHeaderExtension) {
    502       return channels_.find(channel)->second->rtp_offset_receive_id_;
    503     } else if (extension == kRtpAbsoluteSenderTimeHeaderExtension) {
    504       return
    505           channels_.find(channel)->second->rtp_absolute_send_time_receive_id_;
    506     }
    507     return -1;
    508   }
    509   bool GetTransmissionSmoothingStatus(int channel) {
    510     WEBRTC_ASSERT_CHANNEL(channel);
    511     return channels_.find(channel)->second->transmission_smoothing_;
    512   }
    513   int GetSenderTargetDelay(int channel) {
    514     WEBRTC_ASSERT_CHANNEL(channel);
    515     return channels_.find(channel)->second->sender_target_delay_;
    516   }
    517   int GetReceiverTargetDelay(int channel) {
    518     WEBRTC_ASSERT_CHANNEL(channel);
    519     return channels_.find(channel)->second->receiver_target_delay_;
    520   }
    521   bool GetNackStatus(int channel) const {
    522     WEBRTC_ASSERT_CHANNEL(channel);
    523     return channels_.find(channel)->second->nack_;
    524   }
    525   bool GetHybridNackFecStatus(int channel) const {
    526     WEBRTC_ASSERT_CHANNEL(channel);
    527     return channels_.find(channel)->second->hybrid_nack_fec_;
    528   }
    529   int GetNumSsrcs(int channel) const {
    530     WEBRTC_ASSERT_CHANNEL(channel);
    531     return static_cast<int>(
    532         channels_.find(channel)->second->ssrcs_.size());
    533   }
    534   int GetNumRtxSsrcs(int channel) const {
    535     WEBRTC_ASSERT_CHANNEL(channel);
    536     return static_cast<int>(
    537         channels_.find(channel)->second->rtx_ssrcs_.size());
    538   }
    539   bool GetIsTransmitting(int channel) const {
    540     WEBRTC_ASSERT_CHANNEL(channel);
    541     return channels_.find(channel)->second->can_transmit_;
    542   }
    543   webrtc::CpuOveruseObserver* GetCpuOveruseObserver(int channel) const {
    544     WEBRTC_ASSERT_CHANNEL(channel);
    545     return channels_.find(channel)->second->overuse_observer_;
    546   }
    547   webrtc::CpuOveruseOptions GetCpuOveruseOptions(int channel) const {
    548     WEBRTC_ASSERT_CHANNEL(channel);
    549     return channels_.find(channel)->second->overuse_options_;
    550   }
    551   int GetRtxSsrc(int channel, int simulcast_idx) const {
    552     WEBRTC_ASSERT_CHANNEL(channel);
    553     if (channels_.find(channel)->second->rtx_ssrcs_.find(simulcast_idx) ==
    554         channels_.find(channel)->second->rtx_ssrcs_.end()) {
    555       return -1;
    556     }
    557     return channels_.find(channel)->second->rtx_ssrcs_[simulcast_idx];
    558   }
    559   bool ReceiveCodecRegistered(int channel,
    560                               const webrtc::VideoCodec& codec) const {
    561     WEBRTC_ASSERT_CHANNEL(channel);
    562     const std::vector<webrtc::VideoCodec>& codecs =
    563       channels_.find(channel)->second->recv_codecs;
    564     return std::find(codecs.begin(), codecs.end(), codec) != codecs.end();
    565   };
    566   bool ExternalDecoderRegistered(int channel,
    567                                  unsigned int pl_type) const {
    568     WEBRTC_ASSERT_CHANNEL(channel);
    569     return channels_.find(channel)->second->
    570         ext_decoder_pl_types_.count(pl_type) != 0;
    571   };
    572   int GetNumExternalDecoderRegistered(int channel) const {
    573     WEBRTC_ASSERT_CHANNEL(channel);
    574     return static_cast<int>(
    575         channels_.find(channel)->second->ext_decoder_pl_types_.size());
    576   };
    577   bool ExternalEncoderRegistered(int channel,
    578                                  unsigned int pl_type) const {
    579     WEBRTC_ASSERT_CHANNEL(channel);
    580     return channels_.find(channel)->second->
    581         ext_encoder_pl_types_.count(pl_type) != 0;
    582   };
    583   int GetNumExternalEncoderRegistered(int channel) const {
    584     WEBRTC_ASSERT_CHANNEL(channel);
    585     return static_cast<int>(
    586         channels_.find(channel)->second->ext_encoder_pl_types_.size());
    587   };
    588   int GetTotalNumExternalEncoderRegistered() const {
    589     std::map<int, Channel*>::const_iterator it;
    590     int total_num_registered = 0;
    591     for (it = channels_.begin(); it != channels_.end(); ++it)
    592       total_num_registered +=
    593           static_cast<int>(it->second->ext_encoder_pl_types_.size());
    594     return total_num_registered;
    595   }
    596   void SetSendBitrates(int channel, unsigned int video_bitrate,
    597                        unsigned int fec_bitrate, unsigned int nack_bitrate) {
    598     WEBRTC_ASSERT_CHANNEL(channel);
    599     channels_[channel]->send_video_bitrate_ = video_bitrate;
    600     channels_[channel]->send_fec_bitrate_ = fec_bitrate;
    601     channels_[channel]->send_nack_bitrate_ = nack_bitrate;
    602   }
    603   void SetSendBandwidthEstimate(int channel, unsigned int send_bandwidth) {
    604     WEBRTC_ASSERT_CHANNEL(channel);
    605     channels_[GetOriginalChannelId(channel)]->send_bandwidth_ = send_bandwidth;
    606   }
    607   void SetReceiveBandwidthEstimate(int channel,
    608                                    unsigned int receive_bandwidth) {
    609     WEBRTC_ASSERT_CHANNEL(channel);
    610     channels_[GetOriginalChannelId(channel)]->receive_bandwidth_ =
    611         receive_bandwidth;
    612   };
    613   int GetRtxSendPayloadType(int channel) {
    614     WEBRTC_CHECK_CHANNEL(channel);
    615     return channels_[channel]->rtx_send_payload_type;
    616   }
    617   int GetRtxRecvPayloadType(int channel) {
    618     WEBRTC_CHECK_CHANNEL(channel);
    619     return channels_[channel]->rtx_recv_payload_type;
    620   }
    621   int GetRemoteRtxSsrc(int channel) {
    622     WEBRTC_CHECK_CHANNEL(channel);
    623     return channels_.find(channel)->second->remote_rtx_ssrc_;
    624   }
    625   bool GetSuspendBelowMinBitrateStatus(int channel) {
    626     WEBRTC_ASSERT_CHANNEL(channel);
    627     return channels_.find(channel)->second->suspend_below_min_bitrate_;
    628   }
    629   int GetLastRecvdPayloadType(int channel) const {
    630     WEBRTC_CHECK_CHANNEL(channel);
    631     return channels_.find(channel)->second->last_recvd_payload_type_;
    632   }
    633   unsigned int GetReservedTransmitBitrate(int channel) {
    634     WEBRTC_ASSERT_CHANNEL(channel);
    635     return channels_.find(channel)->second->reserved_transmit_bitrate_bps_;
    636   }
    637 
    638   WEBRTC_STUB(Release, ());
    639 
    640   // webrtc::ViEBase
    641   WEBRTC_FUNC(Init, ()) {
    642     inited_ = true;
    643     return 0;
    644   };
    645   WEBRTC_STUB(SetVoiceEngine, (webrtc::VoiceEngine*));
    646   WEBRTC_FUNC(CreateChannel, (int& channel)) {  // NOLINT
    647     if (fail_create_channel_) {
    648       return -1;
    649     }
    650     if (kViEChannelIdMax == last_channel_) {
    651       return -1;
    652     }
    653     Channel* ch = new Channel();
    654     ++last_channel_;
    655     // The original channel of the first channel in a group refers to itself
    656     // for code simplicity.
    657     ch->original_channel_id_ = last_channel_;
    658     channels_[last_channel_] = ch;
    659     channel = last_channel_;
    660     return 0;
    661   };
    662   WEBRTC_FUNC(CreateChannel, (int& channel, int original_channel)) {
    663     WEBRTC_CHECK_CHANNEL(original_channel);
    664     if (CreateChannel(channel) != 0) {
    665       return -1;
    666     }
    667     channels_[channel]->original_channel_id_ = original_channel;
    668     return 0;
    669   }
    670   WEBRTC_FUNC(CreateReceiveChannel, (int& channel, int original_channel)) {
    671     return CreateChannel(channel, original_channel);
    672   }
    673   WEBRTC_FUNC(DeleteChannel, (const int channel)) {
    674     WEBRTC_CHECK_CHANNEL(channel);
    675     // Make sure we deregister all the decoders before deleting a channel.
    676     EXPECT_EQ(0, GetNumExternalDecoderRegistered(channel));
    677     delete channels_[channel];
    678     channels_.erase(channel);
    679     return 0;
    680   }
    681   WEBRTC_FUNC(RegisterCpuOveruseObserver,
    682       (int channel, webrtc::CpuOveruseObserver* observer)) {
    683     WEBRTC_CHECK_CHANNEL(channel);
    684     channels_[channel]->overuse_observer_ = observer;
    685     return 0;
    686   }
    687   WEBRTC_STUB(CpuOveruseMeasures, (int, int*, int*, int*, int*));
    688   WEBRTC_FUNC(SetCpuOveruseOptions,
    689       (int channel, const webrtc::CpuOveruseOptions& options)) {
    690     WEBRTC_CHECK_CHANNEL(channel);
    691     channels_[channel]->overuse_options_ = options;
    692     return 0;
    693   }
    694   WEBRTC_STUB(ConnectAudioChannel, (const int, const int));
    695   WEBRTC_STUB(DisconnectAudioChannel, (const int));
    696   WEBRTC_FUNC(StartSend, (const int channel)) {
    697     WEBRTC_CHECK_CHANNEL(channel);
    698     channels_[channel]->send = true;
    699     return 0;
    700   }
    701   WEBRTC_FUNC(StopSend, (const int channel)) {
    702     WEBRTC_CHECK_CHANNEL(channel);
    703     channels_[channel]->send = false;
    704     return 0;
    705   }
    706   WEBRTC_FUNC(StartReceive, (const int channel)) {
    707     WEBRTC_CHECK_CHANNEL(channel);
    708     channels_[channel]->receive_ = true;
    709     return 0;
    710   }
    711   WEBRTC_FUNC(StopReceive, (const int channel)) {
    712     WEBRTC_CHECK_CHANNEL(channel);
    713     channels_[channel]->receive_ = false;
    714     return 0;
    715   }
    716   WEBRTC_STUB(GetVersion, (char version[1024]));
    717   WEBRTC_STUB(LastError, ());
    718 
    719   // webrtc::ViECodec
    720   WEBRTC_FUNC_CONST(NumberOfCodecs, ()) {
    721     return num_codecs_;
    722   };
    723   WEBRTC_FUNC_CONST(GetCodec, (const unsigned char list_number,
    724                                webrtc::VideoCodec& out_codec)) {
    725     if (list_number >= NumberOfCodecs()) {
    726       return -1;
    727     }
    728     memset(&out_codec, 0, sizeof(out_codec));
    729     const cricket::VideoCodec& c(*codecs_[list_number]);
    730     if ("I420" == c.name) {
    731       out_codec.codecType = webrtc::kVideoCodecI420;
    732     } else if ("VP8" == c.name) {
    733       out_codec.codecType = webrtc::kVideoCodecVP8;
    734     } else if ("red" == c.name) {
    735       out_codec.codecType = webrtc::kVideoCodecRED;
    736     } else if ("ulpfec" == c.name) {
    737       out_codec.codecType = webrtc::kVideoCodecULPFEC;
    738     } else {
    739       out_codec.codecType = webrtc::kVideoCodecUnknown;
    740     }
    741     talk_base::strcpyn(out_codec.plName, sizeof(out_codec.plName),
    742                        c.name.c_str());
    743     out_codec.plType = c.id;
    744     out_codec.width = c.width;
    745     out_codec.height = c.height;
    746     out_codec.startBitrate = kStartVideoBitrate;
    747     out_codec.maxBitrate = kMaxVideoBitrate;
    748     out_codec.minBitrate = kMinVideoBitrate;
    749     out_codec.maxFramerate = c.framerate;
    750     return 0;
    751   };
    752   WEBRTC_FUNC(SetSendCodec, (const int channel,
    753                              const webrtc::VideoCodec& codec)) {
    754     WEBRTC_CHECK_CHANNEL(channel);
    755     channels_[channel]->send_codec = codec;
    756     ++num_set_send_codecs_;
    757     return 0;
    758   };
    759   WEBRTC_FUNC_CONST(GetSendCodec, (const int channel,
    760                                    webrtc::VideoCodec& codec)) {  // NOLINT
    761     WEBRTC_CHECK_CHANNEL(channel);
    762     codec = channels_.find(channel)->second->send_codec;
    763     return 0;
    764   };
    765   WEBRTC_FUNC(SetReceiveCodec, (const int channel,
    766                                 const webrtc::VideoCodec& codec)) {  // NOLINT
    767     WEBRTC_CHECK_CHANNEL(channel);
    768     channels_[channel]->recv_codecs.push_back(codec);
    769     return 0;
    770   };
    771   WEBRTC_STUB_CONST(GetReceiveCodec, (const int, webrtc::VideoCodec&));
    772   WEBRTC_STUB_CONST(GetCodecConfigParameters, (const int,
    773       unsigned char*, unsigned char&));
    774   WEBRTC_STUB(SetImageScaleStatus, (const int, const bool));
    775   WEBRTC_STUB_CONST(GetSendCodecStastistics, (const int,
    776       unsigned int&, unsigned int&));
    777   WEBRTC_STUB_CONST(GetReceiveCodecStastistics, (const int,
    778       unsigned int&, unsigned int&));
    779   WEBRTC_STUB_CONST(GetReceiveSideDelay, (const int video_channel,
    780                                           int* delay_ms));
    781   WEBRTC_FUNC_CONST(GetCodecTargetBitrate, (const int channel,
    782       unsigned int* codec_target_bitrate)) {
    783     WEBRTC_CHECK_CHANNEL(channel);
    784 
    785     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
    786     if (it->second->send) {
    787       // Assume the encoder produces the expected rate.
    788       *codec_target_bitrate = it->second->send_video_bitrate_;
    789     } else {
    790       *codec_target_bitrate = 0;
    791     }
    792     return 0;
    793   }
    794   virtual unsigned int GetDiscardedPackets(const int channel) const {
    795     return 0;
    796   }
    797 
    798   WEBRTC_STUB(SetKeyFrameRequestCallbackStatus, (const int, const bool));
    799   WEBRTC_STUB(SetSignalKeyPacketLossStatus, (const int, const bool,
    800       const bool));
    801   WEBRTC_STUB(RegisterEncoderObserver, (const int,
    802       webrtc::ViEEncoderObserver&));
    803   WEBRTC_STUB(DeregisterEncoderObserver, (const int));
    804   WEBRTC_STUB(RegisterDecoderObserver, (const int,
    805       webrtc::ViEDecoderObserver&));
    806   WEBRTC_STUB(DeregisterDecoderObserver, (const int));
    807   WEBRTC_STUB(SendKeyFrame, (const int));
    808   WEBRTC_STUB(WaitForFirstKeyFrame, (const int, const bool));
    809   WEBRTC_STUB(StartDebugRecording, (int, const char*));
    810   WEBRTC_STUB(StopDebugRecording, (int));
    811   WEBRTC_VOID_FUNC(SuspendBelowMinBitrate, (int channel)) {
    812     WEBRTC_ASSERT_CHANNEL(channel);
    813     channels_[channel]->suspend_below_min_bitrate_ = true;
    814   }
    815 
    816   // webrtc::ViECapture
    817   WEBRTC_STUB(NumberOfCaptureDevices, ());
    818   WEBRTC_STUB(GetCaptureDevice, (unsigned int, char*,
    819       const unsigned int, char*, const unsigned int));
    820   WEBRTC_STUB(AllocateCaptureDevice, (const char*, const unsigned int, int&));
    821   WEBRTC_FUNC(AllocateExternalCaptureDevice,
    822               (int& capture_id, webrtc::ViEExternalCapture*& capture)) {
    823     if (fail_alloc_capturer_) {
    824       return -1;
    825     }
    826     if (kViECaptureIdMax == last_capturer_) {
    827       return -1;
    828     }
    829     Capturer* cap = new Capturer();
    830     capturers_[++last_capturer_] = cap;
    831     capture_id = last_capturer_;
    832     capture = cap;
    833     return 0;
    834   }
    835   WEBRTC_STUB(AllocateCaptureDevice, (webrtc::VideoCaptureModule&, int&));
    836   WEBRTC_FUNC(ReleaseCaptureDevice, (const int capture_id)) {
    837     WEBRTC_CHECK_CAPTURER(capture_id);
    838     delete capturers_[capture_id];
    839     capturers_.erase(capture_id);
    840     return 0;
    841   }
    842   WEBRTC_FUNC(ConnectCaptureDevice, (const int capture_id,
    843                                      const int channel)) {
    844     WEBRTC_CHECK_CHANNEL(channel);
    845     WEBRTC_CHECK_CAPTURER(capture_id);
    846     channels_[channel]->capture_id_ = capture_id;
    847     capturers_[capture_id]->set_channel_id(channel);
    848     return 0;
    849   }
    850   WEBRTC_FUNC(DisconnectCaptureDevice, (const int channel)) {
    851     WEBRTC_CHECK_CHANNEL(channel);
    852     int capture_id = channels_[channel]->capture_id_;
    853     WEBRTC_CHECK_CAPTURER(capture_id);
    854     channels_[channel]->capture_id_ = -1;
    855     capturers_[capture_id]->set_channel_id(-1);
    856     return 0;
    857   }
    858   WEBRTC_STUB(StartCapture, (const int, const webrtc::CaptureCapability&));
    859   WEBRTC_STUB(StopCapture, (const int));
    860   WEBRTC_STUB(SetRotateCapturedFrames, (const int,
    861       const webrtc::RotateCapturedFrame));
    862   WEBRTC_STUB(SetCaptureDelay, (const int, const unsigned int));
    863   WEBRTC_STUB(NumberOfCapabilities, (const char*, const unsigned int));
    864   WEBRTC_STUB(GetCaptureCapability, (const char*, const unsigned int,
    865       const unsigned int, webrtc::CaptureCapability&));
    866   WEBRTC_STUB(ShowCaptureSettingsDialogBox, (const char*, const unsigned int,
    867       const char*, void*, const unsigned int, const unsigned int));
    868   WEBRTC_STUB(GetOrientation, (const char*, webrtc::RotateCapturedFrame&));
    869   WEBRTC_STUB(EnableBrightnessAlarm, (const int, const bool));
    870   WEBRTC_STUB(RegisterObserver, (const int, webrtc::ViECaptureObserver&));
    871   WEBRTC_STUB(DeregisterObserver, (const int));
    872 
    873   // webrtc::ViENetwork
    874   WEBRTC_VOID_FUNC(SetNetworkTransmissionState, (const int channel,
    875                                                  const bool is_transmitting)) {
    876     WEBRTC_ASSERT_CHANNEL(channel);
    877     channels_[channel]->can_transmit_ = is_transmitting;
    878   }
    879   WEBRTC_STUB(RegisterSendTransport, (const int, webrtc::Transport&));
    880   WEBRTC_STUB(DeregisterSendTransport, (const int));
    881 
    882   WEBRTC_FUNC(ReceivedRTPPacket, (const int channel,
    883                                   const void* packet,
    884                                   const int length,
    885                                   const webrtc::PacketTime& packet_time)) {
    886     WEBRTC_ASSERT_CHANNEL(channel);
    887     ASSERT(length > 1);
    888     uint8_t payload_type = static_cast<const uint8_t*>(packet)[1] & 0x7F;
    889     channels_[channel]->last_recvd_payload_type_ = payload_type;
    890     return 0;
    891   }
    892 
    893   WEBRTC_STUB(ReceivedRTCPPacket, (const int, const void*, const int));
    894   // Not using WEBRTC_STUB due to bool return value
    895   virtual bool IsIPv6Enabled(int channel) { return true; }
    896   WEBRTC_STUB(SetMTU, (int, unsigned int));
    897   WEBRTC_STUB(ReceivedBWEPacket, (const int, int64_t, int,
    898       const webrtc::RTPHeader&));
    899 
    900   // webrtc::ViERender
    901   WEBRTC_STUB(RegisterVideoRenderModule, (webrtc::VideoRender&));
    902   WEBRTC_STUB(DeRegisterVideoRenderModule, (webrtc::VideoRender&));
    903   WEBRTC_STUB(AddRenderer, (const int, void*, const unsigned int, const float,
    904       const float, const float, const float));
    905   WEBRTC_FUNC(RemoveRenderer, (const int render_id)) {
    906     if (IsCapturerId(render_id)) {
    907       WEBRTC_CHECK_CAPTURER(render_id);
    908       return 0;
    909     } else if (IsChannelId(render_id)) {
    910       WEBRTC_CHECK_CHANNEL(render_id);
    911       channels_[render_id]->has_renderer_ = false;
    912       return 0;
    913     }
    914     return -1;
    915   }
    916   WEBRTC_FUNC(StartRender, (const int render_id)) {
    917     if (IsCapturerId(render_id)) {
    918       WEBRTC_CHECK_CAPTURER(render_id);
    919       return 0;
    920     } else if (IsChannelId(render_id)) {
    921       WEBRTC_CHECK_CHANNEL(render_id);
    922       channels_[render_id]->render_started_ = true;
    923       return 0;
    924     }
    925     return -1;
    926   }
    927   WEBRTC_FUNC(StopRender, (const int render_id)) {
    928     if (IsCapturerId(render_id)) {
    929       WEBRTC_CHECK_CAPTURER(render_id);
    930       return 0;
    931     } else if (IsChannelId(render_id)) {
    932       WEBRTC_CHECK_CHANNEL(render_id);
    933       channels_[render_id]->render_started_ = false;
    934       return 0;
    935     }
    936     return -1;
    937   }
    938   WEBRTC_STUB(SetExpectedRenderDelay, (int render_id, int render_delay));
    939   WEBRTC_STUB(ConfigureRender, (int, const unsigned int, const float,
    940       const float, const float, const float));
    941   WEBRTC_STUB(MirrorRenderStream, (const int, const bool, const bool,
    942       const bool));
    943   WEBRTC_FUNC(AddRenderer, (const int render_id,
    944                             webrtc::RawVideoType video_type,
    945                             webrtc::ExternalRenderer* renderer)) {
    946     if (IsCapturerId(render_id)) {
    947       WEBRTC_CHECK_CAPTURER(render_id);
    948       return 0;
    949     } else if (IsChannelId(render_id)) {
    950       WEBRTC_CHECK_CHANNEL(render_id);
    951       channels_[render_id]->has_renderer_ = true;
    952       return 0;
    953     }
    954     return -1;
    955   }
    956 
    957   // webrtc::ViERTP_RTCP
    958   WEBRTC_FUNC(SetLocalSSRC, (const int channel,
    959                              const unsigned int ssrc,
    960                              const webrtc::StreamType usage,
    961                              const unsigned char idx)) {
    962     WEBRTC_CHECK_CHANNEL(channel);
    963     switch (usage) {
    964       case webrtc::kViEStreamTypeNormal:
    965         channels_[channel]->ssrcs_[idx] = ssrc;
    966         break;
    967       case webrtc::kViEStreamTypeRtx:
    968         channels_[channel]->rtx_ssrcs_[idx] = ssrc;
    969         break;
    970       default:
    971         return -1;
    972     }
    973     return 0;
    974   }
    975 
    976   WEBRTC_FUNC_CONST(SetRemoteSSRCType, (const int channel,
    977         const webrtc::StreamType usage, const unsigned int ssrc)) {
    978     WEBRTC_CHECK_CHANNEL(channel);
    979     if (usage == webrtc::kViEStreamTypeRtx) {
    980       channels_.find(channel)->second->remote_rtx_ssrc_ = ssrc;
    981       return 0;
    982     }
    983     return -1;
    984   }
    985 
    986   WEBRTC_FUNC_CONST(GetLocalSSRC, (const int channel,
    987                                    unsigned int& ssrc)) {
    988     // ssrcs_[0] is the default local ssrc.
    989     WEBRTC_CHECK_CHANNEL(channel);
    990     ssrc = channels_.find(channel)->second->ssrcs_[0];
    991     return 0;
    992   }
    993   WEBRTC_STUB_CONST(GetRemoteSSRC, (const int, unsigned int&));
    994   WEBRTC_STUB_CONST(GetRemoteCSRCs, (const int, unsigned int*));
    995 
    996   WEBRTC_FUNC(SetRtxSendPayloadType, (const int channel,
    997                                       const uint8 payload_type)) {
    998     WEBRTC_CHECK_CHANNEL(channel);
    999     channels_[channel]->rtx_send_payload_type = payload_type;
   1000     return 0;
   1001   }
   1002 
   1003 #ifdef USE_WEBRTC_DEV_BRANCH
   1004   WEBRTC_STUB(SetPadWithRedundantPayloads, (int, bool));
   1005 #endif
   1006 
   1007   WEBRTC_FUNC(SetRtxReceivePayloadType, (const int channel,
   1008                                          const uint8 payload_type)) {
   1009     WEBRTC_CHECK_CHANNEL(channel);
   1010     channels_[channel]->rtx_recv_payload_type = payload_type;
   1011     return 0;
   1012   }
   1013 
   1014   WEBRTC_STUB(SetStartSequenceNumber, (const int, unsigned short));
   1015   WEBRTC_FUNC(SetRTCPStatus,
   1016               (const int channel, const webrtc::ViERTCPMode mode)) {
   1017     WEBRTC_CHECK_CHANNEL(channel);
   1018     channels_[channel]->rtcp_status_ = mode;
   1019     return 0;
   1020   }
   1021   WEBRTC_STUB_CONST(GetRTCPStatus, (const int, webrtc::ViERTCPMode&));
   1022   WEBRTC_FUNC(SetRTCPCName, (const int channel,
   1023                              const char rtcp_cname[KMaxRTCPCNameLength])) {
   1024     WEBRTC_CHECK_CHANNEL(channel);
   1025     channels_[channel]->cname_.assign(rtcp_cname);
   1026     return 0;
   1027   }
   1028   WEBRTC_FUNC_CONST(GetRTCPCName, (const int channel,
   1029                                    char rtcp_cname[KMaxRTCPCNameLength])) {
   1030     WEBRTC_CHECK_CHANNEL(channel);
   1031     talk_base::strcpyn(rtcp_cname, KMaxRTCPCNameLength,
   1032                        channels_.find(channel)->second->cname_.c_str());
   1033     return 0;
   1034   }
   1035   WEBRTC_STUB_CONST(GetRemoteRTCPCName, (const int, char*));
   1036   WEBRTC_STUB(SendApplicationDefinedRTCPPacket, (const int, const unsigned char,
   1037       unsigned int, const char*, unsigned short));
   1038   WEBRTC_FUNC(SetNACKStatus, (const int channel, const bool enable)) {
   1039     WEBRTC_CHECK_CHANNEL(channel);
   1040     channels_[channel]->nack_ = enable;
   1041     channels_[channel]->hybrid_nack_fec_ = false;
   1042     return 0;
   1043   }
   1044   WEBRTC_STUB(SetFECStatus, (const int, const bool, const unsigned char,
   1045       const unsigned char));
   1046   WEBRTC_FUNC(SetHybridNACKFECStatus, (const int channel, const bool enable,
   1047       const unsigned char red_type, const unsigned char fec_type)) {
   1048     WEBRTC_CHECK_CHANNEL(channel);
   1049     if (red_type == fec_type ||
   1050         red_type == channels_[channel]->send_codec.plType ||
   1051         fec_type == channels_[channel]->send_codec.plType) {
   1052       return -1;
   1053     }
   1054     channels_[channel]->nack_ = false;
   1055     channels_[channel]->hybrid_nack_fec_ = enable;
   1056     return 0;
   1057   }
   1058   WEBRTC_FUNC(SetKeyFrameRequestMethod,
   1059               (const int channel,
   1060                const webrtc::ViEKeyFrameRequestMethod method)) {
   1061     WEBRTC_CHECK_CHANNEL(channel);
   1062     channels_[channel]->key_frame_request_method_ = method;
   1063     return 0;
   1064   }
   1065   WEBRTC_FUNC(SetSenderBufferingMode, (int channel, int target_delay)) {
   1066     WEBRTC_CHECK_CHANNEL(channel);
   1067     channels_[channel]->sender_target_delay_ = target_delay;
   1068     return 0;
   1069   }
   1070   WEBRTC_FUNC(SetReceiverBufferingMode, (int channel, int target_delay)) {
   1071     WEBRTC_CHECK_CHANNEL(channel);
   1072     channels_[channel]->receiver_target_delay_ = target_delay;
   1073     return 0;
   1074   }
   1075   // |Send| and |receive| are stored locally in variables that more clearly
   1076   // explain what they mean.
   1077   WEBRTC_FUNC(SetRembStatus, (int channel, bool send, bool receive)) {
   1078     WEBRTC_CHECK_CHANNEL(channel);
   1079     channels_[channel]->remb_contribute_ = receive;
   1080     channels_[channel]->remb_bw_partition_ = send;
   1081     return 0;
   1082   }
   1083   WEBRTC_FUNC(SetTMMBRStatus, (const int channel, const bool enable)) {
   1084     WEBRTC_CHECK_CHANNEL(channel);
   1085     channels_[channel]->tmmbr_ = enable;
   1086     return 0;
   1087   }
   1088   WEBRTC_FUNC(SetSendTimestampOffsetStatus, (int channel, bool enable,
   1089       int id)) {
   1090     WEBRTC_CHECK_CHANNEL(channel);
   1091     channels_[channel]->rtp_offset_send_id_ = (enable) ? id : -1;
   1092     return 0;
   1093   }
   1094   WEBRTC_FUNC(SetReceiveTimestampOffsetStatus, (int channel, bool enable,
   1095       int id)) {
   1096     WEBRTC_CHECK_CHANNEL(channel);
   1097     channels_[channel]->rtp_offset_receive_id_ = (enable) ? id : -1;
   1098     return 0;
   1099   }
   1100   WEBRTC_FUNC(SetSendAbsoluteSendTimeStatus, (int channel, bool enable,
   1101       int id)) {
   1102     WEBRTC_CHECK_CHANNEL(channel);
   1103     channels_[channel]->rtp_absolute_send_time_send_id_ = (enable) ? id : -1;
   1104     return 0;
   1105   }
   1106   WEBRTC_FUNC(SetReceiveAbsoluteSendTimeStatus, (int channel, bool enable,
   1107       int id)) {
   1108     WEBRTC_CHECK_CHANNEL(channel);
   1109     channels_[channel]->rtp_absolute_send_time_receive_id_ = (enable) ? id : -1;
   1110     return 0;
   1111   }
   1112   WEBRTC_STUB(SetRtcpXrRrtrStatus, (int, bool));
   1113   WEBRTC_FUNC(SetTransmissionSmoothingStatus, (int channel, bool enable)) {
   1114     WEBRTC_CHECK_CHANNEL(channel);
   1115     channels_[channel]->transmission_smoothing_ = enable;
   1116     return 0;
   1117   }
   1118   WEBRTC_FUNC(SetReservedTransmitBitrate, (int channel,
   1119       unsigned int reserved_transmit_bitrate_bps)) {
   1120     WEBRTC_CHECK_CHANNEL(channel);
   1121     channels_[channel]->reserved_transmit_bitrate_bps_ =
   1122         reserved_transmit_bitrate_bps;
   1123     return 0;
   1124   }
   1125   WEBRTC_STUB_CONST(GetRtcpPacketTypeCounters, (int,
   1126       webrtc::RtcpPacketTypeCounter*, webrtc::RtcpPacketTypeCounter*));
   1127   WEBRTC_STUB_CONST(GetReceivedRTCPStatistics, (const int, unsigned short&,
   1128       unsigned int&, unsigned int&, unsigned int&, int&));
   1129   WEBRTC_STUB_CONST(GetSentRTCPStatistics, (const int, unsigned short&,
   1130       unsigned int&, unsigned int&, unsigned int&, int&));
   1131   WEBRTC_STUB_CONST(GetRTPStatistics, (const int, unsigned int&, unsigned int&,
   1132       unsigned int&, unsigned int&));
   1133   WEBRTC_STUB_CONST(GetReceiveChannelRtcpStatistics, (const int,
   1134       webrtc::RtcpStatistics&, int&));
   1135   WEBRTC_STUB_CONST(GetSendChannelRtcpStatistics, (const int,
   1136       webrtc::RtcpStatistics&, int&));
   1137   WEBRTC_STUB_CONST(GetRtpStatistics, (const int, webrtc::StreamDataCounters&,
   1138       webrtc::StreamDataCounters&));
   1139   WEBRTC_FUNC_CONST(GetBandwidthUsage, (const int channel,
   1140       unsigned int& total_bitrate, unsigned int& video_bitrate,
   1141       unsigned int& fec_bitrate, unsigned int& nack_bitrate)) {
   1142     WEBRTC_CHECK_CHANNEL(channel);
   1143     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
   1144     if (it->second->send) {
   1145       video_bitrate = it->second->send_video_bitrate_;
   1146       fec_bitrate = it->second->send_fec_bitrate_;
   1147       nack_bitrate = it->second->send_nack_bitrate_;
   1148       total_bitrate = video_bitrate + fec_bitrate + nack_bitrate;
   1149     } else {
   1150       total_bitrate = 0;
   1151       video_bitrate = 0;
   1152       fec_bitrate = 0;
   1153       nack_bitrate = 0;
   1154     }
   1155     return 0;
   1156   }
   1157   WEBRTC_FUNC_CONST(GetEstimatedSendBandwidth, (const int channel,
   1158       unsigned int* send_bandwidth_estimate)) {
   1159     WEBRTC_CHECK_CHANNEL(channel);
   1160     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
   1161     // Assume the current video, fec and nack bitrate sums up to our estimate.
   1162     if (it->second->send) {
   1163       it = channels_.find(GetOriginalChannelId(channel));
   1164       *send_bandwidth_estimate = it->second->send_bandwidth_;
   1165     } else {
   1166       *send_bandwidth_estimate = 0;
   1167     }
   1168     return 0;
   1169   }
   1170   WEBRTC_FUNC_CONST(GetEstimatedReceiveBandwidth, (const int channel,
   1171       unsigned int* receive_bandwidth_estimate)) {
   1172     WEBRTC_CHECK_CHANNEL(channel);
   1173     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
   1174     if (it->second->receive_) {
   1175       it = channels_.find(GetOriginalChannelId(channel));
   1176       *receive_bandwidth_estimate = it->second->receive_bandwidth_;
   1177     } else {
   1178       *receive_bandwidth_estimate = 0;
   1179     }
   1180     return 0;
   1181   }
   1182   WEBRTC_STUB(RegisterSendChannelRtcpStatisticsCallback,
   1183                     (int, webrtc::RtcpStatisticsCallback*));
   1184   WEBRTC_STUB(DeregisterSendChannelRtcpStatisticsCallback,
   1185                     (int, webrtc::RtcpStatisticsCallback*));
   1186   WEBRTC_STUB(RegisterReceiveChannelRtcpStatisticsCallback,
   1187                     (int, webrtc::RtcpStatisticsCallback*));
   1188   WEBRTC_STUB(DeregisterReceiveChannelRtcpStatisticsCallback,
   1189                     (int, webrtc::RtcpStatisticsCallback*));
   1190   WEBRTC_STUB(RegisterSendChannelRtpStatisticsCallback,
   1191                     (int, webrtc::StreamDataCountersCallback*));
   1192   WEBRTC_STUB(DeregisterSendChannelRtpStatisticsCallback,
   1193                     (int, webrtc::StreamDataCountersCallback*));
   1194   WEBRTC_STUB(RegisterReceiveChannelRtpStatisticsCallback,
   1195                     (int, webrtc::StreamDataCountersCallback*));
   1196   WEBRTC_STUB(DeregisterReceiveChannelRtpStatisticsCallback,
   1197                     (int, webrtc::StreamDataCountersCallback*));
   1198   WEBRTC_STUB(RegisterSendBitrateObserver,
   1199                     (int, webrtc::BitrateStatisticsObserver*));
   1200   WEBRTC_STUB(DeregisterSendBitrateObserver,
   1201                     (int, webrtc::BitrateStatisticsObserver*));
   1202   WEBRTC_STUB(RegisterSendFrameCountObserver,
   1203                     (int, webrtc::FrameCountObserver*));
   1204   WEBRTC_STUB(DeregisterSendFrameCountObserver,
   1205                     (int, webrtc::FrameCountObserver*));
   1206 
   1207   WEBRTC_STUB(StartRTPDump, (const int, const char*, webrtc::RTPDirections));
   1208   WEBRTC_STUB(StopRTPDump, (const int, webrtc::RTPDirections));
   1209   WEBRTC_STUB(RegisterRTPObserver, (const int, webrtc::ViERTPObserver&));
   1210   WEBRTC_STUB(DeregisterRTPObserver, (const int));
   1211   WEBRTC_STUB(RegisterRTCPObserver, (const int, webrtc::ViERTCPObserver&));
   1212   WEBRTC_STUB(DeregisterRTCPObserver, (const int));
   1213 
   1214   // webrtc::ViEImageProcess
   1215   WEBRTC_STUB(RegisterCaptureEffectFilter, (const int,
   1216       webrtc::ViEEffectFilter&));
   1217   WEBRTC_STUB(DeregisterCaptureEffectFilter, (const int));
   1218   WEBRTC_STUB(RegisterSendEffectFilter, (const int,
   1219       webrtc::ViEEffectFilter&));
   1220   WEBRTC_STUB(DeregisterSendEffectFilter, (const int));
   1221   WEBRTC_STUB(RegisterRenderEffectFilter, (const int,
   1222       webrtc::ViEEffectFilter&));
   1223   WEBRTC_STUB(DeregisterRenderEffectFilter, (const int));
   1224   WEBRTC_STUB(EnableDeflickering, (const int, const bool));
   1225   WEBRTC_FUNC(EnableDenoising, (const int capture_id, const bool denoising)) {
   1226     WEBRTC_CHECK_CAPTURER(capture_id);
   1227     capturers_[capture_id]->set_denoising(denoising);
   1228     return 0;
   1229   }
   1230   WEBRTC_STUB(EnableColorEnhancement, (const int, const bool));
   1231   WEBRTC_VOID_STUB(RegisterPreEncodeCallback,
   1232                    (int, webrtc::I420FrameCallback*));
   1233   WEBRTC_VOID_STUB(DeRegisterPreEncodeCallback, (int));
   1234   WEBRTC_VOID_STUB(RegisterPreRenderCallback,
   1235                    (int, webrtc::I420FrameCallback*));
   1236   WEBRTC_VOID_STUB(DeRegisterPreRenderCallback, (int));
   1237   // webrtc::ViEExternalCodec
   1238   WEBRTC_FUNC(RegisterExternalSendCodec,
   1239       (const int channel, const unsigned char pl_type, webrtc::VideoEncoder*,
   1240           bool)) {
   1241     WEBRTC_CHECK_CHANNEL(channel);
   1242     channels_[channel]->ext_encoder_pl_types_.insert(pl_type);
   1243     return 0;
   1244   }
   1245   WEBRTC_FUNC(DeRegisterExternalSendCodec,
   1246       (const int channel, const unsigned char pl_type)) {
   1247     WEBRTC_CHECK_CHANNEL(channel);
   1248     channels_[channel]->ext_encoder_pl_types_.erase(pl_type);
   1249     return 0;
   1250   }
   1251   WEBRTC_FUNC(RegisterExternalReceiveCodec,
   1252       (const int channel, const unsigned int pl_type, webrtc::VideoDecoder*,
   1253        bool, int)) {
   1254     WEBRTC_CHECK_CHANNEL(channel);
   1255     channels_[channel]->ext_decoder_pl_types_.insert(pl_type);
   1256     return 0;
   1257   }
   1258   WEBRTC_FUNC(DeRegisterExternalReceiveCodec,
   1259       (const int channel, const unsigned char pl_type)) {
   1260     WEBRTC_CHECK_CHANNEL(channel);
   1261     channels_[channel]->ext_decoder_pl_types_.erase(pl_type);
   1262     return 0;
   1263   }
   1264 
   1265  private:
   1266   bool IsChannelId(int id) const {
   1267     return (id >= kViEChannelIdBase && id <= kViEChannelIdMax);
   1268   }
   1269   bool IsCapturerId(int id) const {
   1270     return (id >= kViECaptureIdBase && id <= kViECaptureIdMax);
   1271   }
   1272 
   1273   bool inited_;
   1274   int last_channel_;
   1275   std::map<int, Channel*> channels_;
   1276   bool fail_create_channel_;
   1277   int last_capturer_;
   1278   std::map<int, Capturer*> capturers_;
   1279   bool fail_alloc_capturer_;
   1280   const cricket::VideoCodec* const* codecs_;
   1281   int num_codecs_;
   1282   int num_set_send_codecs_;  // how many times we call SetSendCodec().
   1283 };
   1284 
   1285 }  // namespace cricket
   1286 
   1287 #endif  // TALK_MEDIA_WEBRTC_FAKEWEBRTCVIDEOENGINE_H_
   1288