• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/media/base/codec.h"
36 #include "talk/media/webrtc/fakewebrtccommon.h"
37 #include "talk/media/webrtc/webrtcvideodecoderfactory.h"
38 #include "talk/media/webrtc/webrtcvideoencoderfactory.h"
39 #include "talk/media/webrtc/webrtcvie.h"
40 #include "webrtc/base/basictypes.h"
41 #include "webrtc/base/gunit.h"
42 #include "webrtc/base/stringutils.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:
FakeWebRtcVideoDecoder()67   FakeWebRtcVideoDecoder()
68       : num_frames_received_(0) {
69   }
70 
InitDecode(const webrtc::VideoCodec *,int32)71   virtual int32 InitDecode(const webrtc::VideoCodec*, int32) {
72     return WEBRTC_VIDEO_CODEC_OK;
73   }
74 
Decode(const webrtc::EncodedImage &,bool,const webrtc::RTPFragmentationHeader *,const webrtc::CodecSpecificInfo *,int64)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 
RegisterDecodeCompleteCallback(webrtc::DecodedImageCallback *)82   virtual int32 RegisterDecodeCompleteCallback(
83       webrtc::DecodedImageCallback*) {
84     return WEBRTC_VIDEO_CODEC_OK;
85   }
86 
Release()87   virtual int32 Release() {
88     return WEBRTC_VIDEO_CODEC_OK;
89   }
90 
Reset()91   virtual int32 Reset() {
92     return WEBRTC_VIDEO_CODEC_OK;
93   }
94 
GetNumFramesReceived()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:
FakeWebRtcVideoDecoderFactory()106   FakeWebRtcVideoDecoderFactory()
107       : num_created_decoders_(0) {
108   }
109 
CreateVideoDecoder(webrtc::VideoCodecType type)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 
DestroyVideoDecoder(webrtc::VideoDecoder * decoder)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 
AddSupportedVideoCodecType(webrtc::VideoCodecType type)128   void AddSupportedVideoCodecType(webrtc::VideoCodecType type) {
129     supported_codec_types_.insert(type);
130   }
131 
GetNumCreatedDecoders()132   int GetNumCreatedDecoders() {
133     return num_created_decoders_;
134   }
135 
decoders()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:
FakeWebRtcVideoEncoder()149   FakeWebRtcVideoEncoder() {}
150 
InitEncode(const webrtc::VideoCodec * codecSettings,int32 numberOfCores,uint32 maxPayloadSize)151   virtual int32 InitEncode(const webrtc::VideoCodec* codecSettings,
152                            int32 numberOfCores,
153                            uint32 maxPayloadSize) {
154     return WEBRTC_VIDEO_CODEC_OK;
155   }
156 
Encode(const webrtc::I420VideoFrame & inputImage,const webrtc::CodecSpecificInfo * codecSpecificInfo,const std::vector<webrtc::VideoFrameType> * frame_types)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 
RegisterEncodeCompleteCallback(webrtc::EncodedImageCallback * callback)164   virtual int32 RegisterEncodeCompleteCallback(
165       webrtc::EncodedImageCallback* callback) {
166     return WEBRTC_VIDEO_CODEC_OK;
167   }
168 
Release()169   virtual int32 Release() {
170     return WEBRTC_VIDEO_CODEC_OK;
171   }
172 
SetChannelParameters(uint32 packetLoss,int rtt)173   virtual int32 SetChannelParameters(uint32 packetLoss,
174                                      int rtt) {
175     return WEBRTC_VIDEO_CODEC_OK;
176   }
177 
SetRates(uint32 newBitRate,uint32 frameRate)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:
FakeWebRtcVideoEncoderFactory()187   FakeWebRtcVideoEncoderFactory()
188       : num_created_encoders_(0) {
189   }
190 
CreateVideoEncoder(webrtc::VideoCodecType type)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 
DestroyVideoEncoder(webrtc::VideoEncoder * encoder)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 
AddObserver(WebRtcVideoEncoderFactory::Observer * observer)209   virtual void AddObserver(WebRtcVideoEncoderFactory::Observer* observer) {
210     bool inserted = observers_.insert(observer).second;
211     EXPECT_TRUE(inserted);
212   }
213 
RemoveObserver(WebRtcVideoEncoderFactory::Observer * observer)214   virtual void RemoveObserver(WebRtcVideoEncoderFactory::Observer* observer) {
215     size_t erased = observers_.erase(observer);
216     EXPECT_EQ(erased, 1UL);
217   }
218 
codecs()219   virtual const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs()
220       const {
221     return codecs_;
222   }
223 
AddSupportedVideoCodecType(webrtc::VideoCodecType type,const std::string & name)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 
NotifyCodecsAvailable()231   void NotifyCodecsAvailable() {
232     std::set<WebRtcVideoEncoderFactory::Observer*>::iterator it;
233     for (it = observers_.begin(); it != observers_.end(); ++it)
234       (*it)->OnCodecsAvailable();
235   }
236 
GetNumCreatedEncoders()237   int GetNumCreatedEncoders() {
238     return num_created_encoders_;
239   }
240 
encoders()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 {
ChannelChannel264     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     // Bandwidth to deduct from estimated uplink capacity.
339     unsigned int reserved_transmit_bitrate_bps_;
340     bool suspend_below_min_bitrate_;
341     webrtc::CpuOveruseObserver* overuse_observer_;
342     webrtc::CpuOveruseOptions overuse_options_;
343     int last_recvd_payload_type_;
344   };
345   class Capturer : public webrtc::ViEExternalCapture {
346    public:
Capturer()347     Capturer() : channel_id_(-1), denoising_(false),
348                  last_capture_time_(0), incoming_frame_num_(0) { }
channel_id()349     int channel_id() const { return channel_id_; }
set_channel_id(int channel_id)350     void set_channel_id(int channel_id) { channel_id_ = channel_id; }
denoising()351     bool denoising() const { return denoising_; }
set_denoising(bool denoising)352     void set_denoising(bool denoising) { denoising_ = denoising; }
last_capture_time()353     int64 last_capture_time() const { return last_capture_time_; }
incoming_frame_num()354     int incoming_frame_num() const { return incoming_frame_num_; }
355 
356     // From ViEExternalCapture
IncomingFrame(unsigned char * videoFrame,unsigned int videoFrameLength,unsigned short width,unsigned short height,webrtc::RawVideoType videoType,unsigned long long captureTime)357     virtual int IncomingFrame(unsigned char* videoFrame,
358                               unsigned int videoFrameLength,
359                               unsigned short width,
360                               unsigned short height,
361                               webrtc::RawVideoType videoType,
362                               unsigned long long captureTime) {
363       return 0;
364     }
IncomingFrameI420(const webrtc::ViEVideoFrameI420 & video_frame,unsigned long long captureTime)365     virtual int IncomingFrameI420(
366         const webrtc::ViEVideoFrameI420& video_frame,
367         unsigned long long captureTime) {
368       last_capture_time_ = captureTime;
369       ++incoming_frame_num_;
370       return 0;
371     }
372 
373    private:
374     int channel_id_;
375     bool denoising_;
376     int64 last_capture_time_;
377     int incoming_frame_num_;
378   };
379 
FakeWebRtcVideoEngine(const cricket::VideoCodec * const * codecs,int num_codecs)380   FakeWebRtcVideoEngine(const cricket::VideoCodec* const* codecs,
381                         int num_codecs)
382       : inited_(false),
383         last_channel_(kViEChannelIdBase - 1),
384         fail_create_channel_(false),
385         last_capturer_(kViECaptureIdBase - 1),
386         fail_alloc_capturer_(false),
387         codecs_(codecs),
388         num_codecs_(num_codecs),
389         num_set_send_codecs_(0) {
390   }
391 
~FakeWebRtcVideoEngine()392   ~FakeWebRtcVideoEngine() {
393     ASSERT(0 == channels_.size());
394     ASSERT(0 == capturers_.size());
395   }
IsInited()396   bool IsInited() const { return inited_; }
397 
GetLastChannel()398   int GetLastChannel() const { return last_channel_; }
GetChannelFromLocalSsrc(int local_ssrc)399   int GetChannelFromLocalSsrc(int local_ssrc) const {
400     // ssrcs_[0] is the default local ssrc.
401     for (std::map<int, Channel*>::const_iterator iter = channels_.begin();
402          iter != channels_.end(); ++iter) {
403       if (local_ssrc == iter->second->ssrcs_[0]) {
404         return iter->first;
405       }
406     }
407     return -1;
408   }
409 
GetNumChannels()410   int GetNumChannels() const { return static_cast<int>(channels_.size()); }
IsChannel(int channel)411   bool IsChannel(int channel) const {
412     return (channels_.find(channel) != channels_.end());
413   }
set_fail_create_channel(bool fail_create_channel)414   void set_fail_create_channel(bool fail_create_channel) {
415     fail_create_channel_ = fail_create_channel;
416   }
417 
GetLastCapturer()418   int GetLastCapturer() const { return last_capturer_; }
GetNumCapturers()419   int GetNumCapturers() const { return static_cast<int>(capturers_.size()); }
GetIncomingFrameNum(int channel_id)420   int GetIncomingFrameNum(int channel_id) const {
421     for (std::map<int, Capturer*>::const_iterator iter = capturers_.begin();
422          iter != capturers_.end(); ++iter) {
423       Capturer* capturer = iter->second;
424       if (capturer->channel_id() == channel_id) {
425         return capturer->incoming_frame_num();
426       }
427     }
428     return -1;
429   }
set_fail_alloc_capturer(bool fail_alloc_capturer)430   void set_fail_alloc_capturer(bool fail_alloc_capturer) {
431     fail_alloc_capturer_ = fail_alloc_capturer;
432   }
GetNumSetSendCodecs()433   int GetNumSetSendCodecs() const { return num_set_send_codecs_; }
434 
GetCaptureId(int channel)435   int GetCaptureId(int channel) const {
436     WEBRTC_ASSERT_CHANNEL(channel);
437     return channels_.find(channel)->second->capture_id_;
438   }
GetOriginalChannelId(int channel)439   int GetOriginalChannelId(int channel) const {
440     WEBRTC_ASSERT_CHANNEL(channel);
441     return channels_.find(channel)->second->original_channel_id_;
442   }
GetHasRenderer(int channel)443   bool GetHasRenderer(int channel) const {
444     WEBRTC_ASSERT_CHANNEL(channel);
445     return channels_.find(channel)->second->has_renderer_;
446   }
GetRenderStarted(int channel)447   bool GetRenderStarted(int channel) const {
448     WEBRTC_ASSERT_CHANNEL(channel);
449     return channels_.find(channel)->second->render_started_;
450   }
GetSend(int channel)451   bool GetSend(int channel) const {
452     WEBRTC_ASSERT_CHANNEL(channel);
453     return channels_.find(channel)->second->send;
454   }
GetReceive(int channel)455   bool GetReceive(int channel) const {
456     WEBRTC_ASSERT_CHANNEL(channel);
457     return channels_.find(channel)->second->receive_;
458   }
GetCaptureChannelId(int capture_id)459   int GetCaptureChannelId(int capture_id) const {
460     WEBRTC_ASSERT_CAPTURER(capture_id);
461     return capturers_.find(capture_id)->second->channel_id();
462   }
GetCaptureDenoising(int capture_id)463   bool GetCaptureDenoising(int capture_id) const {
464     WEBRTC_ASSERT_CAPTURER(capture_id);
465     return capturers_.find(capture_id)->second->denoising();
466   }
GetCaptureLastTimestamp(int capture_id)467   int64 GetCaptureLastTimestamp(int capture_id) const {
468     WEBRTC_ASSERT_CAPTURER(capture_id);
469     return capturers_.find(capture_id)->second->last_capture_time();
470   }
GetRtcpStatus(int channel)471   webrtc::ViERTCPMode GetRtcpStatus(int channel) const {
472     WEBRTC_ASSERT_CHANNEL(channel);
473     return channels_.find(channel)->second->rtcp_status_;
474   }
GetKeyFrameRequestMethod(int channel)475   webrtc::ViEKeyFrameRequestMethod GetKeyFrameRequestMethod(int channel) const {
476     WEBRTC_ASSERT_CHANNEL(channel);
477     return channels_.find(channel)->second->key_frame_request_method_;
478   }
GetTmmbrStatus(int channel)479   bool GetTmmbrStatus(int channel) const {
480     WEBRTC_ASSERT_CHANNEL(channel);
481     return channels_.find(channel)->second->tmmbr_;
482   }
GetRembStatusBwPartition(int channel)483   bool GetRembStatusBwPartition(int channel) const {
484     WEBRTC_ASSERT_CHANNEL(channel);
485     return channels_.find(channel)->second->remb_bw_partition_;
486   }
GetRembStatusContribute(int channel)487   bool GetRembStatusContribute(int channel) const {
488     WEBRTC_ASSERT_CHANNEL(channel);
489     return channels_.find(channel)->second->remb_contribute_;
490   }
GetSendRtpExtensionId(int channel,const std::string & extension)491   int GetSendRtpExtensionId(int channel, const std::string& extension) {
492     WEBRTC_ASSERT_CHANNEL(channel);
493     if (extension == kRtpTimestampOffsetHeaderExtension) {
494       return channels_.find(channel)->second->rtp_offset_send_id_;
495     } else if (extension == kRtpAbsoluteSenderTimeHeaderExtension) {
496       return channels_.find(channel)->second->rtp_absolute_send_time_send_id_;
497     }
498     return -1;
499   }
GetReceiveRtpExtensionId(int channel,const std::string & extension)500   int GetReceiveRtpExtensionId(int channel, const std::string& extension) {
501     WEBRTC_ASSERT_CHANNEL(channel);
502     if (extension == kRtpTimestampOffsetHeaderExtension) {
503       return channels_.find(channel)->second->rtp_offset_receive_id_;
504     } else if (extension == kRtpAbsoluteSenderTimeHeaderExtension) {
505       return
506           channels_.find(channel)->second->rtp_absolute_send_time_receive_id_;
507     }
508     return -1;
509   }
GetTransmissionSmoothingStatus(int channel)510   bool GetTransmissionSmoothingStatus(int channel) {
511     WEBRTC_ASSERT_CHANNEL(channel);
512     return channels_.find(channel)->second->transmission_smoothing_;
513   }
GetSenderTargetDelay(int channel)514   int GetSenderTargetDelay(int channel) {
515     WEBRTC_ASSERT_CHANNEL(channel);
516     return channels_.find(channel)->second->sender_target_delay_;
517   }
GetReceiverTargetDelay(int channel)518   int GetReceiverTargetDelay(int channel) {
519     WEBRTC_ASSERT_CHANNEL(channel);
520     return channels_.find(channel)->second->receiver_target_delay_;
521   }
GetNackStatus(int channel)522   bool GetNackStatus(int channel) const {
523     WEBRTC_ASSERT_CHANNEL(channel);
524     return channels_.find(channel)->second->nack_;
525   }
GetHybridNackFecStatus(int channel)526   bool GetHybridNackFecStatus(int channel) const {
527     WEBRTC_ASSERT_CHANNEL(channel);
528     return channels_.find(channel)->second->hybrid_nack_fec_;
529   }
GetNumSsrcs(int channel)530   int GetNumSsrcs(int channel) const {
531     WEBRTC_ASSERT_CHANNEL(channel);
532     return static_cast<int>(
533         channels_.find(channel)->second->ssrcs_.size());
534   }
GetNumRtxSsrcs(int channel)535   int GetNumRtxSsrcs(int channel) const {
536     WEBRTC_ASSERT_CHANNEL(channel);
537     return static_cast<int>(
538         channels_.find(channel)->second->rtx_ssrcs_.size());
539   }
GetIsTransmitting(int channel)540   bool GetIsTransmitting(int channel) const {
541     WEBRTC_ASSERT_CHANNEL(channel);
542     return channels_.find(channel)->second->can_transmit_;
543   }
GetCpuOveruseObserver(int channel)544   webrtc::CpuOveruseObserver* GetCpuOveruseObserver(int channel) const {
545     WEBRTC_ASSERT_CHANNEL(channel);
546     return channels_.find(channel)->second->overuse_observer_;
547   }
GetCpuOveruseOptions(int channel)548   webrtc::CpuOveruseOptions GetCpuOveruseOptions(int channel) const {
549     WEBRTC_ASSERT_CHANNEL(channel);
550     return channels_.find(channel)->second->overuse_options_;
551   }
GetSsrc(int channel,int idx)552   int GetSsrc(int channel, int idx) const {
553     WEBRTC_ASSERT_CHANNEL(channel);
554     if (channels_.find(channel)->second->ssrcs_.find(idx) ==
555         channels_.find(channel)->second->ssrcs_.end()) {
556       return -1;
557     }
558     return channels_.find(channel)->second->ssrcs_[idx];
559   }
GetRtxSsrc(int channel,int idx)560   int GetRtxSsrc(int channel, int idx) const {
561     WEBRTC_ASSERT_CHANNEL(channel);
562     if (channels_.find(channel)->second->rtx_ssrcs_.find(idx) ==
563         channels_.find(channel)->second->rtx_ssrcs_.end()) {
564       return -1;
565     }
566     return channels_.find(channel)->second->rtx_ssrcs_[idx];
567   }
ReceiveCodecRegistered(int channel,const webrtc::VideoCodec & codec)568   bool ReceiveCodecRegistered(int channel,
569                               const webrtc::VideoCodec& codec) const {
570     WEBRTC_ASSERT_CHANNEL(channel);
571     const std::vector<webrtc::VideoCodec>& codecs =
572       channels_.find(channel)->second->recv_codecs;
573     return std::find(codecs.begin(), codecs.end(), codec) != codecs.end();
574   };
ExternalDecoderRegistered(int channel,unsigned int pl_type)575   bool ExternalDecoderRegistered(int channel,
576                                  unsigned int pl_type) const {
577     WEBRTC_ASSERT_CHANNEL(channel);
578     return channels_.find(channel)->second->
579         ext_decoder_pl_types_.count(pl_type) != 0;
580   };
GetNumExternalDecoderRegistered(int channel)581   int GetNumExternalDecoderRegistered(int channel) const {
582     WEBRTC_ASSERT_CHANNEL(channel);
583     return static_cast<int>(
584         channels_.find(channel)->second->ext_decoder_pl_types_.size());
585   };
ExternalEncoderRegistered(int channel,unsigned int pl_type)586   bool ExternalEncoderRegistered(int channel,
587                                  unsigned int pl_type) const {
588     WEBRTC_ASSERT_CHANNEL(channel);
589     return channels_.find(channel)->second->
590         ext_encoder_pl_types_.count(pl_type) != 0;
591   };
GetNumExternalEncoderRegistered(int channel)592   int GetNumExternalEncoderRegistered(int channel) const {
593     WEBRTC_ASSERT_CHANNEL(channel);
594     return static_cast<int>(
595         channels_.find(channel)->second->ext_encoder_pl_types_.size());
596   };
GetTotalNumExternalEncoderRegistered()597   int GetTotalNumExternalEncoderRegistered() const {
598     std::map<int, Channel*>::const_iterator it;
599     int total_num_registered = 0;
600     for (it = channels_.begin(); it != channels_.end(); ++it)
601       total_num_registered +=
602           static_cast<int>(it->second->ext_encoder_pl_types_.size());
603     return total_num_registered;
604   }
SetSendBitrates(int channel,unsigned int video_bitrate,unsigned int fec_bitrate,unsigned int nack_bitrate)605   void SetSendBitrates(int channel, unsigned int video_bitrate,
606                        unsigned int fec_bitrate, unsigned int nack_bitrate) {
607     WEBRTC_ASSERT_CHANNEL(channel);
608     channels_[channel]->send_video_bitrate_ = video_bitrate;
609     channels_[channel]->send_fec_bitrate_ = fec_bitrate;
610     channels_[channel]->send_nack_bitrate_ = nack_bitrate;
611   }
SetSendBandwidthEstimate(int channel,unsigned int send_bandwidth)612   void SetSendBandwidthEstimate(int channel, unsigned int send_bandwidth) {
613     WEBRTC_ASSERT_CHANNEL(channel);
614     channels_[GetOriginalChannelId(channel)]->send_bandwidth_ = send_bandwidth;
615   }
SetReceiveBandwidthEstimate(int channel,unsigned int receive_bandwidth)616   void SetReceiveBandwidthEstimate(int channel,
617                                    unsigned int receive_bandwidth) {
618     WEBRTC_ASSERT_CHANNEL(channel);
619     channels_[GetOriginalChannelId(channel)]->receive_bandwidth_ =
620         receive_bandwidth;
621   };
GetRtxSendPayloadType(int channel)622   int GetRtxSendPayloadType(int channel) {
623     WEBRTC_CHECK_CHANNEL(channel);
624     return channels_[channel]->rtx_send_payload_type;
625   }
GetRtxRecvPayloadType(int channel)626   int GetRtxRecvPayloadType(int channel) {
627     WEBRTC_CHECK_CHANNEL(channel);
628     return channels_[channel]->rtx_recv_payload_type;
629   }
GetRemoteRtxSsrc(int channel)630   int GetRemoteRtxSsrc(int channel) {
631     WEBRTC_CHECK_CHANNEL(channel);
632     return channels_.find(channel)->second->remote_rtx_ssrc_;
633   }
GetSuspendBelowMinBitrateStatus(int channel)634   bool GetSuspendBelowMinBitrateStatus(int channel) {
635     WEBRTC_ASSERT_CHANNEL(channel);
636     return channels_.find(channel)->second->suspend_below_min_bitrate_;
637   }
GetLastRecvdPayloadType(int channel)638   int GetLastRecvdPayloadType(int channel) const {
639     WEBRTC_CHECK_CHANNEL(channel);
640     return channels_.find(channel)->second->last_recvd_payload_type_;
641   }
GetReservedTransmitBitrate(int channel)642   unsigned int GetReservedTransmitBitrate(int channel) {
643     WEBRTC_ASSERT_CHANNEL(channel);
644     return channels_.find(channel)->second->reserved_transmit_bitrate_bps_;
645   }
646 
647   WEBRTC_STUB(Release, ());
648 
649   // webrtc::ViEBase
650   WEBRTC_FUNC(Init, ()) {
651     inited_ = true;
652     return 0;
653   };
654   WEBRTC_STUB(SetVoiceEngine, (webrtc::VoiceEngine*));
655   WEBRTC_FUNC(CreateChannel, (int& channel)) {  // NOLINT
656     if (fail_create_channel_) {
657       return -1;
658     }
659     if (kViEChannelIdMax == last_channel_) {
660       return -1;
661     }
662     Channel* ch = new Channel();
663     ++last_channel_;
664     // The original channel of the first channel in a group refers to itself
665     // for code simplicity.
666     ch->original_channel_id_ = last_channel_;
667     channels_[last_channel_] = ch;
668     channel = last_channel_;
669     return 0;
670   };
671   WEBRTC_FUNC(CreateChannel, (int& channel, int original_channel)) {
672     WEBRTC_CHECK_CHANNEL(original_channel);
673     if (CreateChannel(channel) != 0) {
674       return -1;
675     }
676     channels_[channel]->original_channel_id_ = original_channel;
677     return 0;
678   }
679   WEBRTC_FUNC(CreateReceiveChannel, (int& channel, int original_channel)) {
680     return CreateChannel(channel, original_channel);
681   }
682   WEBRTC_FUNC(DeleteChannel, (const int channel)) {
683     WEBRTC_CHECK_CHANNEL(channel);
684     // Make sure we deregister all the decoders before deleting a channel.
685     EXPECT_EQ(0, GetNumExternalDecoderRegistered(channel));
686     delete channels_[channel];
687     channels_.erase(channel);
688     return 0;
689   }
690   WEBRTC_FUNC(RegisterCpuOveruseObserver,
691       (int channel, webrtc::CpuOveruseObserver* observer)) {
692     WEBRTC_CHECK_CHANNEL(channel);
693     channels_[channel]->overuse_observer_ = observer;
694     return 0;
695   }
696 #ifdef USE_WEBRTC_DEV_BRANCH
697   WEBRTC_STUB(GetCpuOveruseMetrics, (int, webrtc::CpuOveruseMetrics*));
698 #else
699   WEBRTC_STUB(CpuOveruseMeasures, (int, int*, int*, int*, int*));
700 #endif
701   WEBRTC_FUNC(SetCpuOveruseOptions,
702       (int channel, const webrtc::CpuOveruseOptions& options)) {
703     WEBRTC_CHECK_CHANNEL(channel);
704     channels_[channel]->overuse_options_ = options;
705     return 0;
706   }
707   WEBRTC_STUB(ConnectAudioChannel, (const int, const int));
708   WEBRTC_STUB(DisconnectAudioChannel, (const int));
709   WEBRTC_FUNC(StartSend, (const int channel)) {
710     WEBRTC_CHECK_CHANNEL(channel);
711     channels_[channel]->send = true;
712     return 0;
713   }
714   WEBRTC_FUNC(StopSend, (const int channel)) {
715     WEBRTC_CHECK_CHANNEL(channel);
716     channels_[channel]->send = false;
717     return 0;
718   }
719   WEBRTC_FUNC(StartReceive, (const int channel)) {
720     WEBRTC_CHECK_CHANNEL(channel);
721     channels_[channel]->receive_ = true;
722     return 0;
723   }
724   WEBRTC_FUNC(StopReceive, (const int channel)) {
725     WEBRTC_CHECK_CHANNEL(channel);
726     channels_[channel]->receive_ = false;
727     return 0;
728   }
729   WEBRTC_STUB(GetVersion, (char version[1024]));
730   WEBRTC_STUB(LastError, ());
731 
732   // webrtc::ViECodec
733   WEBRTC_FUNC_CONST(NumberOfCodecs, ()) {
734     return num_codecs_;
735   };
736   WEBRTC_FUNC_CONST(GetCodec, (const unsigned char list_number,
737                                webrtc::VideoCodec& out_codec)) {
738     if (list_number >= NumberOfCodecs()) {
739       return -1;
740     }
741     memset(&out_codec, 0, sizeof(out_codec));
742     const cricket::VideoCodec& c(*codecs_[list_number]);
743     if ("I420" == c.name) {
744       out_codec.codecType = webrtc::kVideoCodecI420;
745     } else if ("VP8" == c.name) {
746       out_codec.codecType = webrtc::kVideoCodecVP8;
747     } else if ("red" == c.name) {
748       out_codec.codecType = webrtc::kVideoCodecRED;
749     } else if ("ulpfec" == c.name) {
750       out_codec.codecType = webrtc::kVideoCodecULPFEC;
751     } else {
752       out_codec.codecType = webrtc::kVideoCodecUnknown;
753     }
754     rtc::strcpyn(out_codec.plName, sizeof(out_codec.plName),
755                        c.name.c_str());
756     out_codec.plType = c.id;
757     out_codec.width = c.width;
758     out_codec.height = c.height;
759     out_codec.startBitrate = kStartVideoBitrate;
760     out_codec.maxBitrate = kMaxVideoBitrate;
761     out_codec.minBitrate = kMinVideoBitrate;
762     out_codec.maxFramerate = c.framerate;
763     return 0;
764   };
765   WEBRTC_FUNC(SetSendCodec, (const int channel,
766                              const webrtc::VideoCodec& codec)) {
767     WEBRTC_CHECK_CHANNEL(channel);
768     channels_[channel]->send_codec = codec;
769     ++num_set_send_codecs_;
770     return 0;
771   };
772   WEBRTC_FUNC_CONST(GetSendCodec, (const int channel,
773                                    webrtc::VideoCodec& codec)) {  // NOLINT
774     WEBRTC_CHECK_CHANNEL(channel);
775     codec = channels_.find(channel)->second->send_codec;
776     return 0;
777   };
778   WEBRTC_FUNC(SetReceiveCodec, (const int channel,
779                                 const webrtc::VideoCodec& codec)) {  // NOLINT
780     WEBRTC_CHECK_CHANNEL(channel);
781     channels_[channel]->recv_codecs.push_back(codec);
782     return 0;
783   };
784   WEBRTC_STUB_CONST(GetReceiveCodec, (const int, webrtc::VideoCodec&));
785   WEBRTC_STUB_CONST(GetCodecConfigParameters, (const int,
786       unsigned char*, unsigned char&));
787   WEBRTC_STUB(SetImageScaleStatus, (const int, const bool));
788   WEBRTC_STUB_CONST(GetSendCodecStastistics, (const int,
789       unsigned int&, unsigned int&));
790   WEBRTC_STUB_CONST(GetReceiveCodecStastistics, (const int,
791       unsigned int&, unsigned int&));
792   WEBRTC_STUB_CONST(GetReceiveSideDelay, (const int video_channel,
793                                           int* delay_ms));
794   WEBRTC_FUNC_CONST(GetCodecTargetBitrate, (const int channel,
795       unsigned int* codec_target_bitrate)) {
796     WEBRTC_CHECK_CHANNEL(channel);
797 
798     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
799     if (it->second->send) {
800       // Assume the encoder produces the expected rate.
801       *codec_target_bitrate = it->second->send_video_bitrate_;
802     } else {
803       *codec_target_bitrate = 0;
804     }
805     return 0;
806   }
GetDiscardedPackets(const int channel)807   virtual unsigned int GetDiscardedPackets(const int channel) const {
808     return 0;
809   }
810 
811   WEBRTC_STUB(SetKeyFrameRequestCallbackStatus, (const int, const bool));
812   WEBRTC_STUB(SetSignalKeyPacketLossStatus, (const int, const bool,
813       const bool));
814   WEBRTC_STUB(RegisterEncoderObserver, (const int,
815       webrtc::ViEEncoderObserver&));
816   WEBRTC_STUB(DeregisterEncoderObserver, (const int));
817   WEBRTC_STUB(RegisterDecoderObserver, (const int,
818       webrtc::ViEDecoderObserver&));
819   WEBRTC_STUB(DeregisterDecoderObserver, (const int));
820   WEBRTC_STUB(SendKeyFrame, (const int));
821   WEBRTC_STUB(WaitForFirstKeyFrame, (const int, const bool));
822   WEBRTC_STUB(StartDebugRecording, (int, const char*));
823   WEBRTC_STUB(StopDebugRecording, (int));
824   WEBRTC_VOID_FUNC(SuspendBelowMinBitrate, (int channel)) {
825     WEBRTC_ASSERT_CHANNEL(channel);
826     channels_[channel]->suspend_below_min_bitrate_ = true;
827   }
828 
829   // webrtc::ViECapture
830   WEBRTC_STUB(NumberOfCaptureDevices, ());
831   WEBRTC_STUB(GetCaptureDevice, (unsigned int, char*,
832       const unsigned int, char*, const unsigned int));
833   WEBRTC_STUB(AllocateCaptureDevice, (const char*, const unsigned int, int&));
834   WEBRTC_FUNC(AllocateExternalCaptureDevice,
835               (int& capture_id, webrtc::ViEExternalCapture*& capture)) {
836     if (fail_alloc_capturer_) {
837       return -1;
838     }
839     if (kViECaptureIdMax == last_capturer_) {
840       return -1;
841     }
842     Capturer* cap = new Capturer();
843     capturers_[++last_capturer_] = cap;
844     capture_id = last_capturer_;
845     capture = cap;
846     return 0;
847   }
848   WEBRTC_STUB(AllocateCaptureDevice, (webrtc::VideoCaptureModule&, int&));
849   WEBRTC_FUNC(ReleaseCaptureDevice, (const int capture_id)) {
850     WEBRTC_CHECK_CAPTURER(capture_id);
851     delete capturers_[capture_id];
852     capturers_.erase(capture_id);
853     return 0;
854   }
855   WEBRTC_FUNC(ConnectCaptureDevice, (const int capture_id,
856                                      const int channel)) {
857     WEBRTC_CHECK_CHANNEL(channel);
858     WEBRTC_CHECK_CAPTURER(capture_id);
859     channels_[channel]->capture_id_ = capture_id;
860     capturers_[capture_id]->set_channel_id(channel);
861     return 0;
862   }
863   WEBRTC_FUNC(DisconnectCaptureDevice, (const int channel)) {
864     WEBRTC_CHECK_CHANNEL(channel);
865     int capture_id = channels_[channel]->capture_id_;
866     WEBRTC_CHECK_CAPTURER(capture_id);
867     channels_[channel]->capture_id_ = -1;
868     capturers_[capture_id]->set_channel_id(-1);
869     return 0;
870   }
871   WEBRTC_STUB(StartCapture, (const int, const webrtc::CaptureCapability&));
872   WEBRTC_STUB(StopCapture, (const int));
873   WEBRTC_STUB(SetRotateCapturedFrames, (const int,
874       const webrtc::RotateCapturedFrame));
875   WEBRTC_STUB(SetCaptureDelay, (const int, const unsigned int));
876   WEBRTC_STUB(NumberOfCapabilities, (const char*, const unsigned int));
877   WEBRTC_STUB(GetCaptureCapability, (const char*, const unsigned int,
878       const unsigned int, webrtc::CaptureCapability&));
879   WEBRTC_STUB(ShowCaptureSettingsDialogBox, (const char*, const unsigned int,
880       const char*, void*, const unsigned int, const unsigned int));
881   WEBRTC_STUB(GetOrientation, (const char*, webrtc::RotateCapturedFrame&));
882   WEBRTC_STUB(EnableBrightnessAlarm, (const int, const bool));
883   WEBRTC_STUB(RegisterObserver, (const int, webrtc::ViECaptureObserver&));
884   WEBRTC_STUB(DeregisterObserver, (const int));
885 
886   // webrtc::ViENetwork
887   WEBRTC_VOID_FUNC(SetNetworkTransmissionState, (const int channel,
888                                                  const bool is_transmitting)) {
889     WEBRTC_ASSERT_CHANNEL(channel);
890     channels_[channel]->can_transmit_ = is_transmitting;
891   }
892   WEBRTC_STUB(RegisterSendTransport, (const int, webrtc::Transport&));
893   WEBRTC_STUB(DeregisterSendTransport, (const int));
894 
895   WEBRTC_FUNC(ReceivedRTPPacket, (const int channel,
896                                   const void* packet,
897                                   const int length,
898                                   const webrtc::PacketTime& packet_time)) {
899     WEBRTC_ASSERT_CHANNEL(channel);
900     ASSERT(length > 1);
901     uint8_t payload_type = static_cast<const uint8_t*>(packet)[1] & 0x7F;
902     channels_[channel]->last_recvd_payload_type_ = payload_type;
903     return 0;
904   }
905 
906   WEBRTC_STUB(ReceivedRTCPPacket, (const int, const void*, const int));
907   // Not using WEBRTC_STUB due to bool return value
IsIPv6Enabled(int channel)908   virtual bool IsIPv6Enabled(int channel) { return true; }
909   WEBRTC_STUB(SetMTU, (int, unsigned int));
910   WEBRTC_STUB(ReceivedBWEPacket, (const int, int64_t, int,
911       const webrtc::RTPHeader&));
SetBandwidthEstimationConfig(int,const webrtc::Config &)912   virtual bool SetBandwidthEstimationConfig(int, const webrtc::Config&) {
913     return true;
914   }
915 
916   // webrtc::ViERender
917   WEBRTC_STUB(RegisterVideoRenderModule, (webrtc::VideoRender&));
918   WEBRTC_STUB(DeRegisterVideoRenderModule, (webrtc::VideoRender&));
919   WEBRTC_STUB(AddRenderer, (const int, void*, const unsigned int, const float,
920       const float, const float, const float));
921   WEBRTC_FUNC(RemoveRenderer, (const int render_id)) {
922     if (IsCapturerId(render_id)) {
923       WEBRTC_CHECK_CAPTURER(render_id);
924       return 0;
925     } else if (IsChannelId(render_id)) {
926       WEBRTC_CHECK_CHANNEL(render_id);
927       channels_[render_id]->has_renderer_ = false;
928       return 0;
929     }
930     return -1;
931   }
932   WEBRTC_FUNC(StartRender, (const int render_id)) {
933     if (IsCapturerId(render_id)) {
934       WEBRTC_CHECK_CAPTURER(render_id);
935       return 0;
936     } else if (IsChannelId(render_id)) {
937       WEBRTC_CHECK_CHANNEL(render_id);
938       channels_[render_id]->render_started_ = true;
939       return 0;
940     }
941     return -1;
942   }
943   WEBRTC_FUNC(StopRender, (const int render_id)) {
944     if (IsCapturerId(render_id)) {
945       WEBRTC_CHECK_CAPTURER(render_id);
946       return 0;
947     } else if (IsChannelId(render_id)) {
948       WEBRTC_CHECK_CHANNEL(render_id);
949       channels_[render_id]->render_started_ = false;
950       return 0;
951     }
952     return -1;
953   }
954   WEBRTC_STUB(SetExpectedRenderDelay, (int render_id, int render_delay));
955   WEBRTC_STUB(ConfigureRender, (int, const unsigned int, const float,
956       const float, const float, const float));
957   WEBRTC_STUB(MirrorRenderStream, (const int, const bool, const bool,
958       const bool));
959   WEBRTC_FUNC(AddRenderer, (const int render_id,
960                             webrtc::RawVideoType video_type,
961                             webrtc::ExternalRenderer* renderer)) {
962     if (IsCapturerId(render_id)) {
963       WEBRTC_CHECK_CAPTURER(render_id);
964       return 0;
965     } else if (IsChannelId(render_id)) {
966       WEBRTC_CHECK_CHANNEL(render_id);
967       channels_[render_id]->has_renderer_ = true;
968       return 0;
969     }
970     return -1;
971   }
972 
973   // webrtc::ViERTP_RTCP
974   WEBRTC_FUNC(SetLocalSSRC, (const int channel,
975                              const unsigned int ssrc,
976                              const webrtc::StreamType usage,
977                              const unsigned char idx)) {
978     WEBRTC_CHECK_CHANNEL(channel);
979     switch (usage) {
980       case webrtc::kViEStreamTypeNormal:
981         channels_[channel]->ssrcs_[idx] = ssrc;
982         break;
983       case webrtc::kViEStreamTypeRtx:
984         channels_[channel]->rtx_ssrcs_[idx] = ssrc;
985         break;
986       default:
987         return -1;
988     }
989     return 0;
990   }
991 
992   WEBRTC_FUNC_CONST(SetRemoteSSRCType, (const int channel,
993         const webrtc::StreamType usage, const unsigned int ssrc)) {
994     WEBRTC_CHECK_CHANNEL(channel);
995     if (usage == webrtc::kViEStreamTypeRtx) {
996       channels_.find(channel)->second->remote_rtx_ssrc_ = ssrc;
997       return 0;
998     }
999     return -1;
1000   }
1001 
1002   WEBRTC_FUNC_CONST(GetLocalSSRC, (const int channel,
1003                                    unsigned int& ssrc)) {
1004     // ssrcs_[0] is the default local ssrc.
1005     WEBRTC_CHECK_CHANNEL(channel);
1006     ssrc = channels_.find(channel)->second->ssrcs_[0];
1007     return 0;
1008   }
1009   WEBRTC_STUB_CONST(GetRemoteSSRC, (const int, unsigned int&));
1010   WEBRTC_STUB_CONST(GetRemoteCSRCs, (const int, unsigned int*));
1011 
1012   WEBRTC_FUNC(SetRtxSendPayloadType, (const int channel,
1013                                       const uint8 payload_type)) {
1014     WEBRTC_CHECK_CHANNEL(channel);
1015     channels_[channel]->rtx_send_payload_type = payload_type;
1016     return 0;
1017   }
1018 
1019 #ifdef USE_WEBRTC_DEV_BRANCH
1020   WEBRTC_STUB(SetPadWithRedundantPayloads, (int, bool));
1021 #endif
1022 
1023   WEBRTC_FUNC(SetRtxReceivePayloadType, (const int channel,
1024                                          const uint8 payload_type)) {
1025     WEBRTC_CHECK_CHANNEL(channel);
1026     channels_[channel]->rtx_recv_payload_type = payload_type;
1027     return 0;
1028   }
1029 
1030   WEBRTC_STUB(SetStartSequenceNumber, (const int, unsigned short));
1031   WEBRTC_FUNC(SetRTCPStatus,
1032               (const int channel, const webrtc::ViERTCPMode mode)) {
1033     WEBRTC_CHECK_CHANNEL(channel);
1034     channels_[channel]->rtcp_status_ = mode;
1035     return 0;
1036   }
1037   WEBRTC_STUB_CONST(GetRTCPStatus, (const int, webrtc::ViERTCPMode&));
1038   WEBRTC_FUNC(SetRTCPCName, (const int channel,
1039                              const char rtcp_cname[KMaxRTCPCNameLength])) {
1040     WEBRTC_CHECK_CHANNEL(channel);
1041     channels_[channel]->cname_.assign(rtcp_cname);
1042     return 0;
1043   }
1044   WEBRTC_FUNC_CONST(GetRTCPCName, (const int channel,
1045                                    char rtcp_cname[KMaxRTCPCNameLength])) {
1046     WEBRTC_CHECK_CHANNEL(channel);
1047     rtc::strcpyn(rtcp_cname, KMaxRTCPCNameLength,
1048                        channels_.find(channel)->second->cname_.c_str());
1049     return 0;
1050   }
1051   WEBRTC_STUB_CONST(GetRemoteRTCPCName, (const int, char*));
1052   WEBRTC_STUB(SendApplicationDefinedRTCPPacket, (const int, const unsigned char,
1053       unsigned int, const char*, unsigned short));
1054   WEBRTC_FUNC(SetNACKStatus, (const int channel, const bool enable)) {
1055     WEBRTC_CHECK_CHANNEL(channel);
1056     channels_[channel]->nack_ = enable;
1057     channels_[channel]->hybrid_nack_fec_ = false;
1058     return 0;
1059   }
1060   WEBRTC_STUB(SetFECStatus, (const int, const bool, const unsigned char,
1061       const unsigned char));
1062   WEBRTC_FUNC(SetHybridNACKFECStatus, (const int channel, const bool enable,
1063       const unsigned char red_type, const unsigned char fec_type)) {
1064     WEBRTC_CHECK_CHANNEL(channel);
1065     if (red_type == fec_type ||
1066         red_type == channels_[channel]->send_codec.plType ||
1067         fec_type == channels_[channel]->send_codec.plType) {
1068       return -1;
1069     }
1070     channels_[channel]->nack_ = false;
1071     channels_[channel]->hybrid_nack_fec_ = enable;
1072     return 0;
1073   }
1074   WEBRTC_FUNC(SetKeyFrameRequestMethod,
1075               (const int channel,
1076                const webrtc::ViEKeyFrameRequestMethod method)) {
1077     WEBRTC_CHECK_CHANNEL(channel);
1078     channels_[channel]->key_frame_request_method_ = method;
1079     return 0;
1080   }
1081   WEBRTC_FUNC(SetSenderBufferingMode, (int channel, int target_delay)) {
1082     WEBRTC_CHECK_CHANNEL(channel);
1083     channels_[channel]->sender_target_delay_ = target_delay;
1084     return 0;
1085   }
1086   WEBRTC_FUNC(SetReceiverBufferingMode, (int channel, int target_delay)) {
1087     WEBRTC_CHECK_CHANNEL(channel);
1088     channels_[channel]->receiver_target_delay_ = target_delay;
1089     return 0;
1090   }
1091   // |Send| and |receive| are stored locally in variables that more clearly
1092   // explain what they mean.
1093   WEBRTC_FUNC(SetRembStatus, (int channel, bool send, bool receive)) {
1094     WEBRTC_CHECK_CHANNEL(channel);
1095     channels_[channel]->remb_contribute_ = receive;
1096     channels_[channel]->remb_bw_partition_ = send;
1097     return 0;
1098   }
1099   WEBRTC_FUNC(SetTMMBRStatus, (const int channel, const bool enable)) {
1100     WEBRTC_CHECK_CHANNEL(channel);
1101     channels_[channel]->tmmbr_ = enable;
1102     return 0;
1103   }
1104   WEBRTC_FUNC(SetSendTimestampOffsetStatus, (int channel, bool enable,
1105       int id)) {
1106     WEBRTC_CHECK_CHANNEL(channel);
1107     channels_[channel]->rtp_offset_send_id_ = (enable) ? id : -1;
1108     return 0;
1109   }
1110   WEBRTC_FUNC(SetReceiveTimestampOffsetStatus, (int channel, bool enable,
1111       int id)) {
1112     WEBRTC_CHECK_CHANNEL(channel);
1113     channels_[channel]->rtp_offset_receive_id_ = (enable) ? id : -1;
1114     return 0;
1115   }
1116   WEBRTC_FUNC(SetSendAbsoluteSendTimeStatus, (int channel, bool enable,
1117       int id)) {
1118     WEBRTC_CHECK_CHANNEL(channel);
1119     channels_[channel]->rtp_absolute_send_time_send_id_ = (enable) ? id : -1;
1120     return 0;
1121   }
1122   WEBRTC_FUNC(SetReceiveAbsoluteSendTimeStatus, (int channel, bool enable,
1123       int id)) {
1124     WEBRTC_CHECK_CHANNEL(channel);
1125     channels_[channel]->rtp_absolute_send_time_receive_id_ = (enable) ? id : -1;
1126     return 0;
1127   }
1128   WEBRTC_STUB(SetRtcpXrRrtrStatus, (int, bool));
1129   WEBRTC_FUNC(SetTransmissionSmoothingStatus, (int channel, bool enable)) {
1130     WEBRTC_CHECK_CHANNEL(channel);
1131     channels_[channel]->transmission_smoothing_ = enable;
1132     return 0;
1133   }
1134   WEBRTC_FUNC(SetReservedTransmitBitrate, (int channel,
1135       unsigned int reserved_transmit_bitrate_bps)) {
1136     WEBRTC_CHECK_CHANNEL(channel);
1137     channels_[channel]->reserved_transmit_bitrate_bps_ =
1138         reserved_transmit_bitrate_bps;
1139     return 0;
1140   }
1141   WEBRTC_STUB_CONST(GetRtcpPacketTypeCounters, (int,
1142       webrtc::RtcpPacketTypeCounter*, webrtc::RtcpPacketTypeCounter*));
1143   WEBRTC_STUB_CONST(GetReceivedRTCPStatistics, (const int, unsigned short&,
1144       unsigned int&, unsigned int&, unsigned int&, int&));
1145   WEBRTC_STUB_CONST(GetSentRTCPStatistics, (const int, unsigned short&,
1146       unsigned int&, unsigned int&, unsigned int&, int&));
1147   WEBRTC_STUB_CONST(GetRTPStatistics, (const int, unsigned int&, unsigned int&,
1148       unsigned int&, unsigned int&));
1149   WEBRTC_STUB_CONST(GetReceiveChannelRtcpStatistics, (const int,
1150       webrtc::RtcpStatistics&, int&));
1151   WEBRTC_STUB_CONST(GetSendChannelRtcpStatistics, (const int,
1152       webrtc::RtcpStatistics&, int&));
1153   WEBRTC_STUB_CONST(GetRtpStatistics, (const int, webrtc::StreamDataCounters&,
1154       webrtc::StreamDataCounters&));
1155   WEBRTC_FUNC_CONST(GetBandwidthUsage, (const int channel,
1156       unsigned int& total_bitrate, unsigned int& video_bitrate,
1157       unsigned int& fec_bitrate, unsigned int& nack_bitrate)) {
1158     WEBRTC_CHECK_CHANNEL(channel);
1159     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
1160     if (it->second->send) {
1161       video_bitrate = it->second->send_video_bitrate_;
1162       fec_bitrate = it->second->send_fec_bitrate_;
1163       nack_bitrate = it->second->send_nack_bitrate_;
1164       total_bitrate = video_bitrate + fec_bitrate + nack_bitrate;
1165     } else {
1166       total_bitrate = 0;
1167       video_bitrate = 0;
1168       fec_bitrate = 0;
1169       nack_bitrate = 0;
1170     }
1171     return 0;
1172   }
1173   WEBRTC_FUNC_CONST(GetEstimatedSendBandwidth, (const int channel,
1174       unsigned int* send_bandwidth_estimate)) {
1175     WEBRTC_CHECK_CHANNEL(channel);
1176     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
1177     // Assume the current video, fec and nack bitrate sums up to our estimate.
1178     if (it->second->send) {
1179       it = channels_.find(GetOriginalChannelId(channel));
1180       *send_bandwidth_estimate = it->second->send_bandwidth_;
1181     } else {
1182       *send_bandwidth_estimate = 0;
1183     }
1184     return 0;
1185   }
1186   WEBRTC_FUNC_CONST(GetEstimatedReceiveBandwidth, (const int channel,
1187       unsigned int* receive_bandwidth_estimate)) {
1188     WEBRTC_CHECK_CHANNEL(channel);
1189     std::map<int, Channel*>::const_iterator it = channels_.find(channel);
1190     if (it->second->receive_) {
1191       it = channels_.find(GetOriginalChannelId(channel));
1192       *receive_bandwidth_estimate = it->second->receive_bandwidth_;
1193     } else {
1194       *receive_bandwidth_estimate = 0;
1195     }
1196     return 0;
1197   }
1198   WEBRTC_STUB(RegisterSendChannelRtcpStatisticsCallback,
1199                     (int, webrtc::RtcpStatisticsCallback*));
1200   WEBRTC_STUB(DeregisterSendChannelRtcpStatisticsCallback,
1201                     (int, webrtc::RtcpStatisticsCallback*));
1202   WEBRTC_STUB(RegisterReceiveChannelRtcpStatisticsCallback,
1203                     (int, webrtc::RtcpStatisticsCallback*));
1204   WEBRTC_STUB(DeregisterReceiveChannelRtcpStatisticsCallback,
1205                     (int, webrtc::RtcpStatisticsCallback*));
1206   WEBRTC_STUB(RegisterSendChannelRtpStatisticsCallback,
1207                     (int, webrtc::StreamDataCountersCallback*));
1208   WEBRTC_STUB(DeregisterSendChannelRtpStatisticsCallback,
1209                     (int, webrtc::StreamDataCountersCallback*));
1210   WEBRTC_STUB(RegisterReceiveChannelRtpStatisticsCallback,
1211                     (int, webrtc::StreamDataCountersCallback*));
1212   WEBRTC_STUB(DeregisterReceiveChannelRtpStatisticsCallback,
1213                     (int, webrtc::StreamDataCountersCallback*));
1214   WEBRTC_STUB(RegisterSendBitrateObserver,
1215                     (int, webrtc::BitrateStatisticsObserver*));
1216   WEBRTC_STUB(DeregisterSendBitrateObserver,
1217                     (int, webrtc::BitrateStatisticsObserver*));
1218   WEBRTC_STUB(RegisterSendFrameCountObserver,
1219                     (int, webrtc::FrameCountObserver*));
1220   WEBRTC_STUB(DeregisterSendFrameCountObserver,
1221                     (int, webrtc::FrameCountObserver*));
1222 
1223   WEBRTC_STUB(StartRTPDump, (const int, const char*, webrtc::RTPDirections));
1224   WEBRTC_STUB(StopRTPDump, (const int, webrtc::RTPDirections));
1225   WEBRTC_STUB(RegisterRTPObserver, (const int, webrtc::ViERTPObserver&));
1226   WEBRTC_STUB(DeregisterRTPObserver, (const int));
1227   WEBRTC_STUB(RegisterRTCPObserver, (const int, webrtc::ViERTCPObserver&));
1228   WEBRTC_STUB(DeregisterRTCPObserver, (const int));
1229 
1230   // webrtc::ViEImageProcess
1231   WEBRTC_STUB(RegisterCaptureEffectFilter, (const int,
1232       webrtc::ViEEffectFilter&));
1233   WEBRTC_STUB(DeregisterCaptureEffectFilter, (const int));
1234   WEBRTC_STUB(RegisterSendEffectFilter, (const int,
1235       webrtc::ViEEffectFilter&));
1236   WEBRTC_STUB(DeregisterSendEffectFilter, (const int));
1237   WEBRTC_STUB(RegisterRenderEffectFilter, (const int,
1238       webrtc::ViEEffectFilter&));
1239   WEBRTC_STUB(DeregisterRenderEffectFilter, (const int));
1240   WEBRTC_STUB(EnableDeflickering, (const int, const bool));
1241   WEBRTC_FUNC(EnableDenoising, (const int capture_id, const bool denoising)) {
1242     WEBRTC_CHECK_CAPTURER(capture_id);
1243     capturers_[capture_id]->set_denoising(denoising);
1244     return 0;
1245   }
1246   WEBRTC_STUB(EnableColorEnhancement, (const int, const bool));
1247   WEBRTC_VOID_STUB(RegisterPreEncodeCallback,
1248                    (int, webrtc::I420FrameCallback*));
1249   WEBRTC_VOID_STUB(DeRegisterPreEncodeCallback, (int));
1250   WEBRTC_VOID_STUB(RegisterPreRenderCallback,
1251                    (int, webrtc::I420FrameCallback*));
1252   WEBRTC_VOID_STUB(DeRegisterPreRenderCallback, (int));
1253   // webrtc::ViEExternalCodec
1254   WEBRTC_FUNC(RegisterExternalSendCodec,
1255       (const int channel, const unsigned char pl_type, webrtc::VideoEncoder*,
1256           bool)) {
1257     WEBRTC_CHECK_CHANNEL(channel);
1258     channels_[channel]->ext_encoder_pl_types_.insert(pl_type);
1259     return 0;
1260   }
1261   WEBRTC_FUNC(DeRegisterExternalSendCodec,
1262       (const int channel, const unsigned char pl_type)) {
1263     WEBRTC_CHECK_CHANNEL(channel);
1264     channels_[channel]->ext_encoder_pl_types_.erase(pl_type);
1265     return 0;
1266   }
1267   WEBRTC_FUNC(RegisterExternalReceiveCodec,
1268       (const int channel, const unsigned int pl_type, webrtc::VideoDecoder*,
1269        bool, int)) {
1270     WEBRTC_CHECK_CHANNEL(channel);
1271     channels_[channel]->ext_decoder_pl_types_.insert(pl_type);
1272     return 0;
1273   }
1274   WEBRTC_FUNC(DeRegisterExternalReceiveCodec,
1275       (const int channel, const unsigned char pl_type)) {
1276     WEBRTC_CHECK_CHANNEL(channel);
1277     channels_[channel]->ext_decoder_pl_types_.erase(pl_type);
1278     return 0;
1279   }
1280 
1281  private:
IsChannelId(int id)1282   bool IsChannelId(int id) const {
1283     return (id >= kViEChannelIdBase && id <= kViEChannelIdMax);
1284   }
IsCapturerId(int id)1285   bool IsCapturerId(int id) const {
1286     return (id >= kViECaptureIdBase && id <= kViECaptureIdMax);
1287   }
1288 
1289   bool inited_;
1290   int last_channel_;
1291   std::map<int, Channel*> channels_;
1292   bool fail_create_channel_;
1293   int last_capturer_;
1294   std::map<int, Capturer*> capturers_;
1295   bool fail_alloc_capturer_;
1296   const cricket::VideoCodec* const* codecs_;
1297   int num_codecs_;
1298   int num_set_send_codecs_;  // how many times we call SetSendCodec().
1299 };
1300 
1301 }  // namespace cricket
1302 
1303 #endif  // TALK_MEDIA_WEBRTC_FAKEWEBRTCVIDEOENGINE_H_
1304