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