~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/session/phone/fakemediaengine.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2004--2011, 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_SESSION_PHONE_FAKEMEDIAENGINE_H_
 
29
#define TALK_SESSION_PHONE_FAKEMEDIAENGINE_H_
 
30
 
 
31
#include <list>
 
32
#include <map>
 
33
#include <set>
 
34
#include <string>
 
35
#include <vector>
 
36
 
 
37
#include "talk/base/buffer.h"
 
38
#include "talk/p2p/base/sessiondescription.h"
 
39
#include "talk/session/phone/mediaengine.h"
 
40
#include "talk/session/phone/streamparams.h"
 
41
#include "talk/session/phone/rtputils.h"
 
42
 
 
43
namespace cricket {
 
44
 
 
45
class FakeMediaEngine;
 
46
class FakeVideoEngine;
 
47
class FakeVoiceEngine;
 
48
 
 
49
// A common helper class that handles sending and receiving RTP/RTCP packets.
 
50
template<class Base>
 
51
class RtpHelper : public Base {
 
52
 public:
 
53
  RtpHelper()
 
54
      : options_(0),
 
55
        sending_(false),
 
56
        playout_(false),
 
57
        fail_set_send_codecs_(false),
 
58
        fail_set_recv_codecs_(false),
 
59
        send_ssrc_(0) {
 
60
  }
 
61
  const std::vector<RtpHeaderExtension>& recv_extensions() {
 
62
    return recv_extensions_;
 
63
  }
 
64
  const std::vector<RtpHeaderExtension>& send_extensions() {
 
65
    return send_extensions_;
 
66
  }
 
67
  bool sending() const { return sending_; }
 
68
  bool playout() const { return playout_; }
 
69
  const std::list<std::string>& rtp_packets() const { return rtp_packets_; }
 
70
  const std::list<std::string>& rtcp_packets() const { return rtcp_packets_; }
 
71
  int options() const { return options_; }
 
72
 
 
73
  bool SendRtp(const void* data, int len) {
 
74
    if (!sending_ || !Base::network_interface_) {
 
75
      return false;
 
76
    }
 
77
    talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
 
78
    return Base::network_interface_->SendPacket(&packet);
 
79
  }
 
80
  bool SendRtcp(const void* data, int len) {
 
81
    if (!Base::network_interface_) {
 
82
      return false;
 
83
    }
 
84
    talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
 
85
    return Base::network_interface_->SendRtcp(&packet);
 
86
  }
 
87
 
 
88
  bool CheckRtp(const void* data, int len) {
 
89
    bool success = !rtp_packets_.empty();
 
90
    if (success) {
 
91
      std::string packet = rtp_packets_.front();
 
92
      rtp_packets_.pop_front();
 
93
      success = (packet == std::string(static_cast<const char*>(data), len));
 
94
    }
 
95
    return success;
 
96
  }
 
97
  bool CheckRtcp(const void* data, int len) {
 
98
    bool success = !rtcp_packets_.empty();
 
99
    if (success) {
 
100
      std::string packet = rtcp_packets_.front();
 
101
      rtcp_packets_.pop_front();
 
102
      success = (packet == std::string(static_cast<const char*>(data), len));
 
103
    }
 
104
    return success;
 
105
  }
 
106
  bool CheckNoRtp() {
 
107
    return rtp_packets_.empty();
 
108
  }
 
109
  bool CheckNoRtcp() {
 
110
    return rtcp_packets_.empty();
 
111
  }
 
112
  virtual bool SetOptions(int options) {
 
113
    options_ = options;
 
114
    return true;
 
115
  }
 
116
  virtual int GetOptions() const {
 
117
    return options_;
 
118
  }
 
119
  virtual bool SetRecvRtpHeaderExtensions(
 
120
      const std::vector<RtpHeaderExtension>& extensions) {
 
121
    recv_extensions_ = extensions;
 
122
    return true;
 
123
  }
 
124
  virtual bool SetSendRtpHeaderExtensions(
 
125
      const std::vector<RtpHeaderExtension>& extensions) {
 
126
    send_extensions_ = extensions;
 
127
    return true;
 
128
  }
 
129
  void set_fail_set_send_codecs(bool fail) {
 
130
    fail_set_send_codecs_ = fail;
 
131
  }
 
132
  void set_fail_set_recv_codecs(bool fail) {
 
133
    fail_set_recv_codecs_ = fail;
 
134
  }
 
135
  virtual bool AddSendStream(const StreamParams& sp) {
 
136
    if (std::find(send_streams_.begin(), send_streams_.end(), sp) !=
 
137
        send_streams_.end()) {
 
138
        return false;
 
139
    }
 
140
    send_streams_.push_back(sp);
 
141
    return true;
 
142
  }
 
143
  virtual bool RemoveSendStream(uint32 ssrc) {
 
144
    return RemoveStreamBySsrc(&send_streams_, ssrc);
 
145
  }
 
146
  virtual bool AddRecvStream(const StreamParams& sp) {
 
147
    if (std::find(receive_streams_.begin(), receive_streams_.end(), sp) !=
 
148
        receive_streams_.end()) {
 
149
        return false;
 
150
    }
 
151
    receive_streams_.push_back(sp);
 
152
    return true;
 
153
  }
 
154
  virtual bool RemoveRecvStream(uint32 ssrc) {
 
155
    return RemoveStreamBySsrc(&receive_streams_, ssrc);
 
156
  }
 
157
  const std::vector<StreamParams>& send_streams() const {
 
158
    return send_streams_;
 
159
  }
 
160
  const std::vector<StreamParams>& recv_streams() const {
 
161
    return receive_streams_;
 
162
  }
 
163
  bool HasRecvStream(uint32 ssrc) const {
 
164
    return GetStreamBySsrc(receive_streams_, ssrc, NULL);
 
165
  }
 
166
 
 
167
  // TODO: This is to support legacy unit test that only check one
 
168
  // sending stream.
 
169
  const uint32 send_ssrc() {
 
170
    if (send_streams_.empty())
 
171
      return 0;
 
172
    return send_streams_[0].first_ssrc();
 
173
  }
 
174
 
 
175
  // TODO: This is to support legacy unit test that only check one
 
176
  // sending stream.
 
177
  const std::string rtcp_cname() {
 
178
    if (send_streams_.empty())
 
179
      return "";
 
180
    return send_streams_[0].cname;
 
181
  }
 
182
 
 
183
 protected:
 
184
  bool set_sending(bool send) {
 
185
    sending_ = send;
 
186
    return true;
 
187
  }
 
188
  void set_playout(bool playout) { playout_ = playout; }
 
189
  virtual void OnPacketReceived(talk_base::Buffer* packet) {
 
190
    rtp_packets_.push_back(std::string(packet->data(), packet->length()));
 
191
  }
 
192
  virtual void OnRtcpReceived(talk_base::Buffer* packet) {
 
193
    rtcp_packets_.push_back(std::string(packet->data(), packet->length()));
 
194
  }
 
195
  bool fail_set_send_codecs() const {
 
196
    return fail_set_send_codecs_;
 
197
  }
 
198
  bool fail_set_recv_codecs() const {
 
199
    return fail_set_recv_codecs_;
 
200
  }
 
201
 
 
202
 private:
 
203
  int options_;
 
204
  bool sending_;
 
205
  bool playout_;
 
206
  std::vector<RtpHeaderExtension> recv_extensions_;
 
207
  std::vector<RtpHeaderExtension> send_extensions_;
 
208
  std::list<std::string> rtp_packets_;
 
209
  std::list<std::string> rtcp_packets_;
 
210
  std::vector<StreamParams> send_streams_;
 
211
  std::vector<StreamParams> receive_streams_;
 
212
  bool fail_set_send_codecs_;
 
213
  bool fail_set_recv_codecs_;
 
214
  uint32 send_ssrc_;
 
215
  std::string rtcp_cname_;
 
216
};
 
217
 
 
218
class FakeVoiceMediaChannel : public RtpHelper<VoiceMediaChannel> {
 
219
 public:
 
220
  typedef std::pair<int, bool> DtmfEvent;
 
221
  explicit FakeVoiceMediaChannel(FakeVoiceEngine* engine)
 
222
      : engine_(engine),
 
223
        muted_(false),
 
224
        fail_set_send_(false),
 
225
        ringback_tone_ssrc_(0),
 
226
        ringback_tone_play_(false),
 
227
        ringback_tone_loop_(false) {
 
228
    output_scalings_[0] = OutputScaling();  // For default channel.
 
229
  }
 
230
  ~FakeVoiceMediaChannel();
 
231
  const std::vector<AudioCodec>& recv_codecs() const { return recv_codecs_; }
 
232
  const std::vector<AudioCodec>& send_codecs() const { return send_codecs_; }
 
233
  const std::vector<AudioCodec>& codecs() const { return send_codecs(); }
 
234
  bool muted() const { return muted_; }
 
235
  const std::vector<DtmfEvent>& dtmf_queue() const { return dtmf_queue_; }
 
236
 
 
237
  uint32 ringback_tone_ssrc() const { return ringback_tone_ssrc_; }
 
238
  bool ringback_tone_play() const { return ringback_tone_play_; }
 
239
  bool ringback_tone_loop() const { return ringback_tone_loop_; }
 
240
 
 
241
  virtual bool SetRecvCodecs(const std::vector<AudioCodec> &codecs) {
 
242
    if (fail_set_recv_codecs()) {
 
243
      // Fake the failure in SetRecvCodecs.
 
244
      return false;
 
245
    }
 
246
    recv_codecs_= codecs;
 
247
    return true;
 
248
  }
 
249
  virtual bool SetSendCodecs(const std::vector<AudioCodec> &codecs) {
 
250
    if (fail_set_send_codecs()) {
 
251
      // Fake the failure in SetSendCodecs.
 
252
      return false;
 
253
    }
 
254
    send_codecs_= codecs;
 
255
    return true;
 
256
  }
 
257
  virtual bool SetPlayout(bool playout) {
 
258
    set_playout(playout);
 
259
    return true;
 
260
  }
 
261
  virtual bool SetSend(SendFlags flag) {
 
262
    if (fail_set_send_) {
 
263
      return false;
 
264
    }
 
265
    return set_sending(flag != SEND_NOTHING);
 
266
  }
 
267
  virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
 
268
  virtual bool Mute(bool on) {
 
269
    muted_ = on;
 
270
    return true;
 
271
  }
 
272
  virtual bool AddRecvStream(const StreamParams& sp) {
 
273
    if (!RtpHelper<VoiceMediaChannel>::AddRecvStream(sp))
 
274
      return false;
 
275
    output_scalings_[sp.first_ssrc()] = OutputScaling();
 
276
    return true;
 
277
  }
 
278
  virtual bool RemoveRecvStream(uint32 ssrc) {
 
279
    if (!RtpHelper<VoiceMediaChannel>::RemoveRecvStream(ssrc))
 
280
      return false;
 
281
    output_scalings_.erase(ssrc);
 
282
    return true;
 
283
  }
 
284
 
 
285
  virtual bool GetActiveStreams(AudioInfo::StreamList* streams) {
 
286
    return true;
 
287
  }
 
288
  virtual int GetOutputLevel() { return 0; }
 
289
 
 
290
  virtual bool SetRingbackTone(const char *buf, int len) { return true; }
 
291
  virtual bool PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
 
292
    ringback_tone_ssrc_ = ssrc;
 
293
    ringback_tone_play_ = play;
 
294
    ringback_tone_loop_ = loop;
 
295
    return true;
 
296
  }
 
297
 
 
298
  virtual bool PressDTMF(int event, bool playout) {
 
299
    dtmf_queue_.push_back(std::make_pair(event, playout));
 
300
    return true;
 
301
  }
 
302
 
 
303
  virtual bool SetOutputScaling(uint32 ssrc, double left, double right) {
 
304
    if (0 == ssrc) {
 
305
      std::map<uint32, OutputScaling>::iterator it;
 
306
      for (it = output_scalings_.begin(); it != output_scalings_.end(); ++it) {
 
307
        it->second.left = left;
 
308
        it->second.right = right;
 
309
      }
 
310
      return true;
 
311
    } else if (output_scalings_.find(ssrc) != output_scalings_.end()) {
 
312
      output_scalings_[ssrc].left = left;
 
313
      output_scalings_[ssrc].right = right;
 
314
      return true;
 
315
    }
 
316
    return false;
 
317
  }
 
318
  virtual bool GetOutputScaling(uint32 ssrc, double* left, double* right) {
 
319
    if (output_scalings_.find(ssrc) == output_scalings_.end()) return false;
 
320
    *left = output_scalings_[ssrc].left;
 
321
    *right = output_scalings_[ssrc].right;
 
322
    return true;
 
323
  }
 
324
 
 
325
  virtual bool GetStats(VoiceMediaInfo* info) { return false; }
 
326
  virtual void GetLastMediaError(uint32* ssrc,
 
327
                                 VoiceMediaChannel::Error* error) {
 
328
    *ssrc = 0;
 
329
    *error = fail_set_send_ ? VoiceMediaChannel::ERROR_REC_DEVICE_OPEN_FAILED
 
330
        : VoiceMediaChannel::ERROR_NONE;
 
331
  }
 
332
 
 
333
  void set_fail_set_send(bool fail) { fail_set_send_ = fail; }
 
334
  void TriggerError(uint32 ssrc, VoiceMediaChannel::Error error) {
 
335
    VoiceMediaChannel::SignalMediaError(ssrc, error);
 
336
  }
 
337
 
 
338
 private:
 
339
  struct OutputScaling {
 
340
    OutputScaling() : left(1.0), right(1.0) {}
 
341
    double left, right;
 
342
  };
 
343
 
 
344
  FakeVoiceEngine* engine_;
 
345
  std::vector<AudioCodec> recv_codecs_;
 
346
  std::vector<AudioCodec> send_codecs_;
 
347
  bool muted_;
 
348
  std::map<uint32, OutputScaling> output_scalings_;
 
349
  std::vector<DtmfEvent> dtmf_queue_;
 
350
  bool fail_set_send_;
 
351
  uint32 ringback_tone_ssrc_;
 
352
  bool ringback_tone_play_;
 
353
  bool ringback_tone_loop_;
 
354
};
 
355
 
 
356
class FakeVideoMediaChannel : public RtpHelper<VideoMediaChannel> {
 
357
 public:
 
358
  explicit FakeVideoMediaChannel(FakeVideoEngine* engine)
 
359
      : engine_(engine),
 
360
        muted_(false),
 
361
        screen_casting_(false),
 
362
        screencast_fps_(0),
 
363
        sent_intra_frame_(false),
 
364
        requested_intra_frame_(false) {
 
365
  }
 
366
  ~FakeVideoMediaChannel();
 
367
 
 
368
  const std::vector<VideoCodec>& recv_codecs() const { return recv_codecs_; }
 
369
  const std::vector<VideoCodec>& send_codecs() const { return send_codecs_; }
 
370
  const std::vector<VideoCodec>& codecs() const { return send_codecs(); }
 
371
  bool muted() const { return muted_; }
 
372
  bool rendering() const { return playout(); }
 
373
  const std::map<uint32, VideoRenderer*>& renderers() const {
 
374
    return renderers_;
 
375
  }
 
376
  bool GetSendStreamFormat(uint32 ssrc, VideoFormat* format) {
 
377
    if (send_formats_.find(ssrc) == send_formats_.end()) {
 
378
      return false;
 
379
    }
 
380
    *format = send_formats_[ssrc];
 
381
    return true;
 
382
  }
 
383
  virtual bool SetSendStreamFormat(uint32 ssrc, const VideoFormat& format) {
 
384
    if (send_formats_.find(ssrc) == send_formats_.end()) {
 
385
      return false;
 
386
    }
 
387
    send_formats_[ssrc] = format;
 
388
    return true;
 
389
  }
 
390
 
 
391
  virtual bool AddSendStream(const StreamParams& sp) {
 
392
    if (!RtpHelper<VideoMediaChannel>::AddSendStream(sp)) {
 
393
      return false;
 
394
    }
 
395
    SetSendStreamDefaultFormat(sp.first_ssrc());
 
396
    return true;
 
397
  }
 
398
  virtual bool RemoveSendStream(uint32 ssrc) {
 
399
    send_formats_.erase(ssrc);
 
400
    return RtpHelper<VideoMediaChannel>::RemoveSendStream(ssrc);
 
401
  }
 
402
 
 
403
  virtual bool SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
 
404
    if (fail_set_recv_codecs()) {
 
405
      // Fake the failure in SetRecvCodecs.
 
406
      return false;
 
407
    }
 
408
    recv_codecs_= codecs;
 
409
    return true;
 
410
  }
 
411
  virtual bool SetSendCodecs(const std::vector<VideoCodec>& codecs) {
 
412
    if (fail_set_send_codecs()) {
 
413
      // Fake the failure in SetSendCodecs.
 
414
      return false;
 
415
    }
 
416
    send_codecs_= codecs;
 
417
 
 
418
    for (std::vector<StreamParams>::const_iterator it = send_streams().begin();
 
419
        it != send_streams().end(); ++it) {
 
420
      SetSendStreamDefaultFormat(it->first_ssrc());
 
421
    }
 
422
    return true;
 
423
  }
 
424
  virtual bool SetRender(bool render) {
 
425
    set_playout(render);
 
426
    return true;
 
427
  }
 
428
  virtual bool SetRenderer(uint32 ssrc, VideoRenderer* r) {
 
429
    if (ssrc != 0 && renderers_.find(ssrc) == renderers_.end()) {
 
430
      return false;
 
431
    }
 
432
    if (ssrc != 0) {
 
433
      renderers_[ssrc] = r;
 
434
    }
 
435
    return true;
 
436
  }
 
437
 
 
438
  virtual bool SetSend(bool send) {
 
439
    return set_sending(send);
 
440
  }
 
441
  virtual bool AddScreencast(uint32 ssrc, const ScreencastId& id, int fps) {
 
442
    screen_casting_ = true;
 
443
    screencast_fps_ = fps;
 
444
    return true;
 
445
  }
 
446
  virtual bool RemoveScreencast(uint32 ssrc) {
 
447
    screen_casting_ = false;
 
448
    screencast_fps_ = 0;
 
449
    return true;
 
450
  }
 
451
  virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
 
452
  virtual bool Mute(bool on) {
 
453
    muted_ = on;
 
454
    return true;
 
455
  }
 
456
  virtual bool AddRecvStream(const StreamParams& sp) {
 
457
    if (!RtpHelper<VideoMediaChannel>::AddRecvStream(sp))
 
458
      return false;
 
459
    renderers_[sp.first_ssrc()] = NULL;
 
460
    return true;
 
461
  }
 
462
  virtual bool RemoveRecvStream(uint32 ssrc) {
 
463
    if (!RtpHelper<VideoMediaChannel>::RemoveRecvStream(ssrc))
 
464
      return false;
 
465
    renderers_.erase(ssrc);
 
466
    return true;
 
467
  }
 
468
 
 
469
  virtual bool GetStats(VideoMediaInfo* info) { return false; }
 
470
  virtual bool SendIntraFrame() {
 
471
    sent_intra_frame_= true;
 
472
    return true;
 
473
  }
 
474
  virtual bool RequestIntraFrame() {
 
475
    requested_intra_frame_ = true;
 
476
    return true;
 
477
  }
 
478
  void set_sent_intra_frame(bool v) { sent_intra_frame_ = v; }
 
479
  bool sent_intra_frame() const { return sent_intra_frame_; }
 
480
  void set_requested_intra_frame(bool v) { requested_intra_frame_ = v; }
 
481
  bool requested_intra_frame() const { return requested_intra_frame_; }
 
482
  bool screen_casting() const { return screen_casting_; }
 
483
  int screencast_fps() const { return screencast_fps_; }
 
484
 
 
485
 private:
 
486
  // Be default, each send stream uses the first send codec format.
 
487
  void SetSendStreamDefaultFormat(uint32 ssrc) {
 
488
    if (!send_codecs_.empty()) {
 
489
      send_formats_[ssrc] = VideoFormat(
 
490
           send_codecs_[0].width,
 
491
           send_codecs_[0].height,
 
492
           cricket::VideoFormat::FpsToInterval(send_codecs_[0].framerate),
 
493
           cricket::FOURCC_I420);
 
494
    }
 
495
  }
 
496
 
 
497
  FakeVideoEngine* engine_;
 
498
  std::vector<VideoCodec> recv_codecs_;
 
499
  std::vector<VideoCodec> send_codecs_;
 
500
  std::map<uint32, VideoRenderer*> renderers_;
 
501
  std::map<uint32, VideoFormat> send_formats_;
 
502
  bool muted_;
 
503
  bool screen_casting_;
 
504
  int screencast_fps_;
 
505
  bool sent_intra_frame_;
 
506
  bool requested_intra_frame_;
 
507
};
 
508
 
 
509
class FakeSoundclipMedia : public SoundclipMedia {
 
510
 public:
 
511
  virtual bool PlaySound(const char *buf, int len, int flags) {
 
512
    return true;
 
513
  }
 
514
};
 
515
 
 
516
class FakeDataMediaChannel : public RtpHelper<DataMediaChannel> {
 
517
 public:
 
518
  explicit FakeDataMediaChannel(void* unused)
 
519
      : muted_(false) {
 
520
  }
 
521
  ~FakeDataMediaChannel() {}
 
522
  const std::vector<DataCodec>& recv_codecs() const { return recv_codecs_; }
 
523
  const std::vector<DataCodec>& send_codecs() const { return send_codecs_; }
 
524
  const std::vector<DataCodec>& codecs() const { return send_codecs(); }
 
525
  bool muted() const { return muted_; }
 
526
 
 
527
  virtual bool SetRecvCodecs(const std::vector<DataCodec> &codecs) {
 
528
    if (fail_set_recv_codecs()) {
 
529
      // Fake the failure in SetRecvCodecs.
 
530
      return false;
 
531
    }
 
532
    recv_codecs_= codecs;
 
533
    return true;
 
534
  }
 
535
  virtual bool SetSendCodecs(const std::vector<DataCodec> &codecs) {
 
536
    if (fail_set_send_codecs()) {
 
537
      // Fake the failure in SetSendCodecs.
 
538
      return false;
 
539
    }
 
540
    send_codecs_= codecs;
 
541
    return true;
 
542
  }
 
543
  virtual bool SetSend(bool send) {
 
544
    return set_sending(send);
 
545
  }
 
546
  virtual bool SetReceive(bool receive) {
 
547
    set_playout(receive);
 
548
    return true;
 
549
  }
 
550
 
 
551
  virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
 
552
  virtual bool Mute(bool on) {
 
553
    muted_ = on;
 
554
    return true;
 
555
  }
 
556
  virtual bool AddRecvStream(const StreamParams& sp) {
 
557
    if (!RtpHelper<DataMediaChannel>::AddRecvStream(sp))
 
558
      return false;
 
559
    return true;
 
560
  }
 
561
  virtual bool RemoveRecvStream(uint32 ssrc) {
 
562
    if (!RtpHelper<DataMediaChannel>::RemoveRecvStream(ssrc))
 
563
      return false;
 
564
    return true;
 
565
  }
 
566
 
 
567
  virtual bool SendData(
 
568
      const SendDataParams& params, const std::string& data) {
 
569
    last_sent_data_params_ = params;
 
570
    last_sent_data_ = data;
 
571
    return true;
 
572
  }
 
573
 
 
574
  SendDataParams last_sent_data_params() { return last_sent_data_params_; }
 
575
  std::string last_sent_data() { return last_sent_data_; }
 
576
 
 
577
 private:
 
578
  std::vector<DataCodec> recv_codecs_;
 
579
  std::vector<DataCodec> send_codecs_;
 
580
  bool muted_;
 
581
  SendDataParams last_sent_data_params_;
 
582
  std::string last_sent_data_;
 
583
};
 
584
 
 
585
// A base class for all of the shared parts between FakeVoiceEngine
 
586
// and FakeVideoEngine.
 
587
class FakeBaseEngine {
 
588
 public:
 
589
  FakeBaseEngine()
 
590
      : loglevel_(-1),
 
591
        options_(0),
 
592
        options_changed_(false),
 
593
        fail_create_channel_(false) {
 
594
  }
 
595
 
 
596
  bool Init() { return true; }
 
597
  void Terminate() {}
 
598
 
 
599
  bool SetOptions(int options) {
 
600
    options_ = options;
 
601
    options_changed_ = true;
 
602
    return true;
 
603
  }
 
604
 
 
605
  void SetLogging(int level, const char* filter) {
 
606
    loglevel_ = level;
 
607
    logfilter_ = filter;
 
608
  }
 
609
 
 
610
  void set_fail_create_channel(bool fail) { fail_create_channel_ = fail; }
 
611
 
 
612
 protected:
 
613
  int loglevel_;
 
614
  std::string logfilter_;
 
615
  int options_;
 
616
  // Flag used by optionsmessagehandler_unittest for checking whether any
 
617
  // relevant setting has been updated.
 
618
  // TODO: Replace with explicit checks of before & after values.
 
619
  bool options_changed_;
 
620
  bool fail_create_channel_;
 
621
};
 
622
 
 
623
class FakeVoiceEngine : public FakeBaseEngine {
 
624
 public:
 
625
  FakeVoiceEngine()
 
626
      : output_volume_(-1),
 
627
        rx_processor_(NULL),
 
628
        tx_processor_(NULL) {
 
629
  }
 
630
 
 
631
  int GetCapabilities() {
 
632
    return AUDIO_SEND | AUDIO_RECV;
 
633
  }
 
634
 
 
635
  VoiceMediaChannel* CreateChannel() {
 
636
    if (fail_create_channel_) {
 
637
      return NULL;
 
638
    }
 
639
 
 
640
    FakeVoiceMediaChannel* ch = new FakeVoiceMediaChannel(this);
 
641
    channels_.push_back(ch);
 
642
    return ch;
 
643
  }
 
644
  FakeVoiceMediaChannel* GetChannel(size_t index) {
 
645
    return (channels_.size() > index) ? channels_[index] : NULL;
 
646
  }
 
647
  void UnregisterChannel(VoiceMediaChannel* channel) {
 
648
    channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
 
649
  }
 
650
  SoundclipMedia* CreateSoundclip() {
 
651
    return new FakeSoundclipMedia();
 
652
  }
 
653
 
 
654
  const std::vector<AudioCodec>& codecs() {
 
655
    return codecs_;
 
656
  }
 
657
  void SetCodecs(const std::vector<AudioCodec> codecs) {
 
658
    codecs_ = codecs;
 
659
  }
 
660
 
 
661
  bool SetDevices(const Device* in_device,
 
662
                          const Device* out_device) {
 
663
    in_device_ = (in_device) ? in_device->name : "";
 
664
    out_device_ = (out_device) ? out_device->name : "";
 
665
    options_changed_ = true;
 
666
    return true;
 
667
  }
 
668
 
 
669
  bool GetOutputVolume(int* level) {
 
670
    *level = output_volume_;
 
671
    return true;
 
672
  }
 
673
 
 
674
  bool SetOutputVolume(int level) {
 
675
    output_volume_ = level;
 
676
    options_changed_ = true;
 
677
    return true;
 
678
  }
 
679
 
 
680
  int GetInputLevel() {
 
681
    return 0;
 
682
  }
 
683
 
 
684
  bool SetLocalMonitor(bool enable) {
 
685
    return true;
 
686
  }
 
687
 
 
688
  bool RegisterProcessor(uint32 ssrc,
 
689
                         VoiceProcessor* voice_processor,
 
690
                         MediaProcessorDirection direction) {
 
691
    if (direction == MPD_RX) {
 
692
      rx_processor_ = voice_processor;
 
693
      return true;
 
694
    } else if (direction == MPD_TX) {
 
695
      tx_processor_ = voice_processor;
 
696
      return true;
 
697
    }
 
698
    return false;
 
699
  }
 
700
 
 
701
  bool UnregisterProcessor(uint32 ssrc,
 
702
                           VoiceProcessor* voice_processor,
 
703
                           MediaProcessorDirection direction) {
 
704
    bool unregistered = false;
 
705
    if (direction & MPD_RX) {
 
706
      rx_processor_ = NULL;
 
707
      unregistered = true;
 
708
    }
 
709
    if (direction & MPD_TX) {
 
710
      tx_processor_ = NULL;
 
711
      unregistered = true;
 
712
    }
 
713
    return unregistered;
 
714
  }
 
715
 
 
716
 private:
 
717
  std::vector<FakeVoiceMediaChannel*> channels_;
 
718
  std::vector<AudioCodec> codecs_;
 
719
  int output_volume_;
 
720
  std::string in_device_;
 
721
  std::string out_device_;
 
722
  VoiceProcessor* rx_processor_;
 
723
  VoiceProcessor* tx_processor_;
 
724
 
 
725
  friend class FakeMediaEngine;
 
726
};
 
727
 
 
728
class FakeVideoEngine : public FakeBaseEngine {
 
729
 public:
 
730
  FakeVideoEngine()
 
731
      : renderer_(NULL),
 
732
        capture_(false),
 
733
        processor_(NULL) {
 
734
  }
 
735
 
 
736
  int GetCapabilities() {
 
737
    return VIDEO_SEND | VIDEO_RECV;
 
738
  }
 
739
  bool SetDefaultEncoderConfig(const VideoEncoderConfig& config) {
 
740
    default_encoder_config_ = config;
 
741
    return true;
 
742
  }
 
743
  const VideoEncoderConfig& default_encoder_config() const {
 
744
    return default_encoder_config_;
 
745
  }
 
746
 
 
747
  VideoMediaChannel* CreateChannel(VoiceMediaChannel* channel) {
 
748
    if (fail_create_channel_) {
 
749
      return NULL;
 
750
    }
 
751
 
 
752
    FakeVideoMediaChannel* ch = new FakeVideoMediaChannel(this);
 
753
    channels_.push_back(ch);
 
754
    return ch;
 
755
  }
 
756
  FakeVideoMediaChannel* GetChannel(size_t index) {
 
757
    return (channels_.size() > index) ? channels_[index] : NULL;
 
758
  }
 
759
  void UnregisterChannel(VideoMediaChannel* channel) {
 
760
    channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
 
761
  }
 
762
 
 
763
  const std::vector<VideoCodec>& codecs() const {
 
764
    return codecs_;
 
765
  }
 
766
  bool FindCodec(const VideoCodec& in) {
 
767
    for (size_t i = 0; i < codecs_.size(); ++i) {
 
768
      if (codecs_[i].Matches(in)) {
 
769
        return true;
 
770
      }
 
771
    }
 
772
    return false;
 
773
  }
 
774
  void SetCodecs(const std::vector<VideoCodec> codecs) {
 
775
    codecs_ = codecs;
 
776
  }
 
777
 
 
778
  bool SetCaptureDevice(const Device* device) {
 
779
    in_device_ = (device) ? device->name : "";
 
780
    options_changed_ = true;
 
781
    return true;
 
782
  }
 
783
  bool SetLocalRenderer(VideoRenderer* r) {
 
784
    renderer_ = r;
 
785
    return true;
 
786
  }
 
787
  bool SetVideoCapturer(VideoCapturer* /*capturer*/, uint32 /*ssrc*/) {
 
788
    return false;
 
789
  }
 
790
  CaptureResult SetCapture(bool capture) {
 
791
    capture_ = capture;
 
792
    return CR_SUCCESS;
 
793
  }
 
794
  bool RegisterProcessor(VideoProcessor* video_processor) {
 
795
    processor_ = video_processor;
 
796
    return true;
 
797
  }
 
798
 
 
799
  bool UnregisterProcessor(VideoProcessor* video_processor) {
 
800
    processor_ = NULL;
 
801
    return true;
 
802
  }
 
803
 
 
804
  sigslot::signal2<VideoCapturer*, CaptureResult> SignalCaptureResult;
 
805
 
 
806
 private:
 
807
  std::vector<FakeVideoMediaChannel*> channels_;
 
808
  std::vector<VideoCodec> codecs_;
 
809
  VideoEncoderConfig default_encoder_config_;
 
810
  std::string in_device_;
 
811
  VideoRenderer* renderer_;
 
812
  bool capture_;
 
813
  VideoProcessor* processor_;
 
814
 
 
815
  friend class FakeMediaEngine;
 
816
};
 
817
 
 
818
class FakeMediaEngine
 
819
    : public CompositeMediaEngine<FakeVoiceEngine, FakeVideoEngine> {
 
820
 public:
 
821
  FakeMediaEngine() {
 
822
    voice_ = FakeVoiceEngine();
 
823
    video_ = FakeVideoEngine();
 
824
  }
 
825
  virtual ~FakeMediaEngine() {}
 
826
 
 
827
  virtual void SetAudioCodecs(const std::vector<AudioCodec> codecs) {
 
828
    voice_.SetCodecs(codecs);
 
829
  }
 
830
 
 
831
  virtual void SetVideoCodecs(const std::vector<VideoCodec> codecs) {
 
832
    video_.SetCodecs(codecs);
 
833
  }
 
834
 
 
835
  FakeVoiceMediaChannel* GetVoiceChannel(size_t index) {
 
836
    return voice_.GetChannel(index);
 
837
  }
 
838
 
 
839
  FakeVideoMediaChannel* GetVideoChannel(size_t index) {
 
840
    return video_.GetChannel(index);
 
841
  }
 
842
 
 
843
  int audio_options() const { return voice_.options_; }
 
844
  int output_volume() const { return voice_.output_volume_; }
 
845
  const VideoEncoderConfig& default_video_encoder_config() const {
 
846
    return video_.default_encoder_config_;
 
847
  }
 
848
  const std::string& audio_in_device() const { return voice_.in_device_; }
 
849
  const std::string& audio_out_device() const { return voice_.out_device_; }
 
850
  const std::string& video_in_device() const { return video_.in_device_; }
 
851
  VideoRenderer* local_renderer() { return video_.renderer_; }
 
852
  int voice_loglevel() const { return voice_.loglevel_; }
 
853
  const std::string& voice_logfilter() const { return voice_.logfilter_; }
 
854
  int video_loglevel() const { return video_.loglevel_; }
 
855
  const std::string& video_logfilter() const { return video_.logfilter_; }
 
856
  bool capture() const { return video_.capture_; }
 
857
  bool options_changed() const {
 
858
    return voice_.options_changed_ || video_.options_changed_;
 
859
  }
 
860
  void clear_options_changed() {
 
861
    video_.options_changed_ = false;
 
862
    voice_.options_changed_ = false;
 
863
  }
 
864
  void set_fail_create_channel(bool fail) {
 
865
    voice_.set_fail_create_channel(fail);
 
866
    video_.set_fail_create_channel(fail);
 
867
  }
 
868
  bool video_processor_registered () const {return video_.processor_ != NULL;}
 
869
  bool voice_processor_registered(MediaProcessorDirection direction) const {
 
870
    if (direction == MPD_RX) {
 
871
      return voice_.rx_processor_ != NULL;
 
872
    } else if (direction == MPD_TX) {
 
873
      return voice_.tx_processor_ != NULL;
 
874
    }
 
875
    return false;
 
876
  }
 
877
};
 
878
 
 
879
// CompositeMediaEngine with FakeVoiceEngine to expose SetAudioCodecs to
 
880
// establish a media connectionwith minimum set of audio codes required
 
881
template<class VIDEO>
 
882
class CompositeMediaEngineWithFakeVoiceEngine
 
883
    : public CompositeMediaEngine<FakeVoiceEngine, VIDEO> {
 
884
 public:
 
885
  CompositeMediaEngineWithFakeVoiceEngine() {}
 
886
  virtual ~CompositeMediaEngineWithFakeVoiceEngine() {}
 
887
 
 
888
  virtual void SetAudioCodecs(const std::vector<AudioCodec>& codecs) {
 
889
    CompositeMediaEngine<FakeVoiceEngine, VIDEO>::voice_.SetCodecs(codecs);
 
890
  }
 
891
};
 
892
 
 
893
// Have to come afterwards due to declaration order
 
894
inline FakeVoiceMediaChannel::~FakeVoiceMediaChannel() {
 
895
  if (engine_) {
 
896
    engine_->UnregisterChannel(this);
 
897
  }
 
898
}
 
899
 
 
900
inline FakeVideoMediaChannel::~FakeVideoMediaChannel() {
 
901
  if (engine_) {
 
902
    engine_->UnregisterChannel(this);
 
903
  }
 
904
}
 
905
 
 
906
class FakeDataEngine : public DataEngineInterface {
 
907
 public:
 
908
  virtual DataMediaChannel* CreateChannel() {
 
909
    FakeDataMediaChannel* ch = new FakeDataMediaChannel(this);
 
910
    channels_.push_back(ch);
 
911
    return ch;
 
912
  }
 
913
 
 
914
  FakeDataMediaChannel* GetChannel(size_t index) {
 
915
    return (channels_.size() > index) ? channels_[index] : NULL;
 
916
  }
 
917
 
 
918
  void UnregisterChannel(DataMediaChannel* channel) {
 
919
    channels_.erase(std::find(channels_.begin(), channels_.end(), channel));
 
920
  }
 
921
 
 
922
  virtual void SetDataCodecs(const std::vector<DataCodec>& data_codecs) {
 
923
    data_codecs_ = data_codecs;
 
924
  }
 
925
 
 
926
  virtual const std::vector<DataCodec>& data_codecs() {
 
927
    return data_codecs_;
 
928
  }
 
929
 
 
930
 private:
 
931
  std::vector<FakeDataMediaChannel*> channels_;
 
932
  std::vector<DataCodec> data_codecs_;
 
933
};
 
934
 
 
935
}  // namespace cricket
 
936
 
 
937
#endif  // TALK_SESSION_PHONE_FAKEMEDIAENGINE_H_