1/*
2 * libjingle
3 * Copyright 2012, 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#include <stdio.h>
29
30#include <algorithm>
31#include <list>
32#include <map>
33#include <vector>
34
35#include "talk/app/webrtc/dtmfsender.h"
36#include "talk/app/webrtc/fakeportallocatorfactory.h"
37#include "talk/app/webrtc/localaudiosource.h"
38#include "talk/app/webrtc/mediastreaminterface.h"
39#include "talk/app/webrtc/peerconnectionfactory.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
41#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
42#include "talk/app/webrtc/test/fakeconstraints.h"
43#include "talk/app/webrtc/test/fakedtlsidentityservice.h"
44#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
45#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
46#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
47#include "talk/app/webrtc/videosourceinterface.h"
48#include "talk/media/webrtc/fakewebrtcvideoengine.h"
49#include "talk/p2p/base/constants.h"
50#include "talk/p2p/base/sessiondescription.h"
51#include "talk/session/media/mediasession.h"
52#include "webrtc/base/gunit.h"
53#include "webrtc/base/scoped_ptr.h"
54#include "webrtc/base/ssladapter.h"
55#include "webrtc/base/sslstreamadapter.h"
56#include "webrtc/base/thread.h"
57
58#define MAYBE_SKIP_TEST(feature)                    \
59  if (!(feature())) {                               \
60    LOG(LS_INFO) << "Feature disabled... skipping"; \
61    return;                                         \
62  }
63
64using cricket::ContentInfo;
65using cricket::FakeWebRtcVideoDecoder;
66using cricket::FakeWebRtcVideoDecoderFactory;
67using cricket::FakeWebRtcVideoEncoder;
68using cricket::FakeWebRtcVideoEncoderFactory;
69using cricket::MediaContentDescription;
70using webrtc::DataBuffer;
71using webrtc::DataChannelInterface;
72using webrtc::DtmfSender;
73using webrtc::DtmfSenderInterface;
74using webrtc::DtmfSenderObserverInterface;
75using webrtc::FakeConstraints;
76using webrtc::MediaConstraintsInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
82using webrtc::PeerConnectionInterface;
83using webrtc::SessionDescriptionInterface;
84using webrtc::StreamCollectionInterface;
85
86static const int kMaxWaitMs = 2000;
87// Disable for TSan v2, see
88// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
89// This declaration is also #ifdef'd as it causes uninitialized-variable
90// warnings.
91#if !defined(THREAD_SANITIZER)
92static const int kMaxWaitForStatsMs = 3000;
93static const int kMaxWaitForRembMs = 5000;
94#endif
95static const int kMaxWaitForFramesMs = 10000;
96static const int kEndAudioFrameCount = 3;
97static const int kEndVideoFrameCount = 3;
98
99static const char kStreamLabelBase[] = "stream_label";
100static const char kVideoTrackLabelBase[] = "video_track";
101static const char kAudioTrackLabelBase[] = "audio_track";
102static const char kDataChannelLabel[] = "data_channel";
103
104static void RemoveLinesFromSdp(const std::string& line_start,
105                               std::string* sdp) {
106  const char kSdpLineEnd[] = "\r\n";
107  size_t ssrc_pos = 0;
108  while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
109      std::string::npos) {
110    size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
111    sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
112  }
113}
114
115class SignalingMessageReceiver {
116 public:
117 protected:
118  SignalingMessageReceiver() {}
119  virtual ~SignalingMessageReceiver() {}
120};
121
122class JsepMessageReceiver : public SignalingMessageReceiver {
123 public:
124  virtual void ReceiveSdpMessage(const std::string& type,
125                                 std::string& msg) = 0;
126  virtual void ReceiveIceMessage(const std::string& sdp_mid,
127                                 int sdp_mline_index,
128                                 const std::string& msg) = 0;
129
130 protected:
131  JsepMessageReceiver() {}
132  virtual ~JsepMessageReceiver() {}
133};
134
135template <typename MessageReceiver>
136class PeerConnectionTestClientBase
137    : public webrtc::PeerConnectionObserver,
138      public MessageReceiver {
139 public:
140  ~PeerConnectionTestClientBase() {
141    while (!fake_video_renderers_.empty()) {
142      RenderMap::iterator it = fake_video_renderers_.begin();
143      delete it->second;
144      fake_video_renderers_.erase(it);
145    }
146  }
147
148  virtual void Negotiate()  = 0;
149
150  virtual void Negotiate(bool audio, bool video)  = 0;
151
152  virtual void SetVideoConstraints(
153      const webrtc::FakeConstraints& video_constraint) {
154    video_constraints_ = video_constraint;
155  }
156
157  void AddMediaStream(bool audio, bool video) {
158    std::string stream_label = kStreamLabelBase +
159        rtc::ToString<int>(
160            static_cast<int>(peer_connection_->local_streams()->count()));
161    rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
162        peer_connection_factory_->CreateLocalMediaStream(stream_label);
163
164    if (audio && can_receive_audio()) {
165      FakeConstraints constraints;
166      // Disable highpass filter so that we can get all the test audio frames.
167      constraints.AddMandatory(
168          MediaConstraintsInterface::kHighpassFilter, false);
169      rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
170          peer_connection_factory_->CreateAudioSource(&constraints);
171      // TODO(perkj): Test audio source when it is implemented. Currently audio
172      // always use the default input.
173      std::string label = stream_label + kAudioTrackLabelBase;
174      rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
175          peer_connection_factory_->CreateAudioTrack(label, source));
176      stream->AddTrack(audio_track);
177    }
178    if (video && can_receive_video()) {
179      stream->AddTrack(CreateLocalVideoTrack(stream_label));
180    }
181
182    EXPECT_TRUE(peer_connection_->AddStream(stream, NULL));
183  }
184
185  size_t NumberOfLocalMediaStreams() {
186    return peer_connection_->local_streams()->count();
187  }
188
189  bool SessionActive() {
190    return peer_connection_->signaling_state() ==
191        webrtc::PeerConnectionInterface::kStable;
192  }
193
194  void set_signaling_message_receiver(
195      MessageReceiver* signaling_message_receiver) {
196    signaling_message_receiver_ = signaling_message_receiver;
197  }
198
199  void EnableVideoDecoderFactory() {
200    video_decoder_factory_enabled_ = true;
201    fake_video_decoder_factory_->AddSupportedVideoCodecType(
202        webrtc::kVideoCodecVP8);
203  }
204
205  bool AudioFramesReceivedCheck(int number_of_frames) const {
206    return number_of_frames <= fake_audio_capture_module_->frames_received();
207  }
208
209  bool VideoFramesReceivedCheck(int number_of_frames) {
210    if (video_decoder_factory_enabled_) {
211      const std::vector<FakeWebRtcVideoDecoder*>& decoders
212          = fake_video_decoder_factory_->decoders();
213      if (decoders.empty()) {
214        return number_of_frames <= 0;
215      }
216
217      for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
218           it = decoders.begin(); it != decoders.end(); ++it) {
219        if (number_of_frames > (*it)->GetNumFramesReceived()) {
220          return false;
221        }
222      }
223      return true;
224    } else {
225      if (fake_video_renderers_.empty()) {
226        return number_of_frames <= 0;
227      }
228
229      for (RenderMap::const_iterator it = fake_video_renderers_.begin();
230           it != fake_video_renderers_.end(); ++it) {
231        if (number_of_frames > it->second->num_rendered_frames()) {
232          return false;
233        }
234      }
235      return true;
236    }
237  }
238  // Verify the CreateDtmfSender interface
239  void VerifyDtmf() {
240    rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
241    rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
242
243    // We can't create a DTMF sender with an invalid audio track or a non local
244    // track.
245    EXPECT_TRUE(peer_connection_->CreateDtmfSender(NULL) == NULL);
246    rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
247        peer_connection_factory_->CreateAudioTrack("dummy_track",
248                                                   NULL));
249    EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == NULL);
250
251    // We should be able to create a DTMF sender from a local track.
252    webrtc::AudioTrackInterface* localtrack =
253        peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
254    dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
255    EXPECT_TRUE(dtmf_sender.get() != NULL);
256    dtmf_sender->RegisterObserver(observer.get());
257
258    // Test the DtmfSender object just created.
259    EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
260    EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
261
262    // We don't need to verify that the DTMF tones are actually sent out because
263    // that is already covered by the tests of the lower level components.
264
265    EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
266    std::vector<std::string> tones;
267    tones.push_back("1");
268    tones.push_back("a");
269    tones.push_back("");
270    observer->Verify(tones);
271
272    dtmf_sender->UnregisterObserver();
273  }
274
275  // Verifies that the SessionDescription have rejected the appropriate media
276  // content.
277  void VerifyRejectedMediaInSessionDescription() {
278    ASSERT_TRUE(peer_connection_->remote_description() != NULL);
279    ASSERT_TRUE(peer_connection_->local_description() != NULL);
280    const cricket::SessionDescription* remote_desc =
281        peer_connection_->remote_description()->description();
282    const cricket::SessionDescription* local_desc =
283        peer_connection_->local_description()->description();
284
285    const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
286    if (remote_audio_content) {
287      const ContentInfo* audio_content =
288          GetFirstAudioContent(local_desc);
289      EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
290    }
291
292    const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
293    if (remote_video_content) {
294      const ContentInfo* video_content =
295          GetFirstVideoContent(local_desc);
296      EXPECT_EQ(can_receive_video(), !video_content->rejected);
297    }
298  }
299
300  void SetExpectIceRestart(bool expect_restart) {
301    expect_ice_restart_ = expect_restart;
302  }
303
304  bool ExpectIceRestart() const { return expect_ice_restart_; }
305
306  void VerifyLocalIceUfragAndPassword() {
307    ASSERT_TRUE(peer_connection_->local_description() != NULL);
308    const cricket::SessionDescription* desc =
309        peer_connection_->local_description()->description();
310    const cricket::ContentInfos& contents = desc->contents();
311
312    for (size_t index = 0; index < contents.size(); ++index) {
313      if (contents[index].rejected)
314        continue;
315      const cricket::TransportDescription* transport_desc =
316          desc->GetTransportDescriptionByName(contents[index].name);
317
318      std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
319          ice_ufrag_pwd_.find(static_cast<int>(index));
320      if (ufragpair_it == ice_ufrag_pwd_.end()) {
321        ASSERT_FALSE(ExpectIceRestart());
322        ice_ufrag_pwd_[static_cast<int>(index)] =
323            IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
324      } else if (ExpectIceRestart()) {
325        const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
326        EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
327        EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
328      } else {
329        const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
330        EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
331        EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
332      }
333    }
334  }
335
336  int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
337    rtc::scoped_refptr<MockStatsObserver>
338        observer(new rtc::RefCountedObject<MockStatsObserver>());
339    EXPECT_TRUE(peer_connection_->GetStats(
340        observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
341    EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
342    return observer->AudioOutputLevel();
343  }
344
345  int GetAudioInputLevelStats() {
346    rtc::scoped_refptr<MockStatsObserver>
347        observer(new rtc::RefCountedObject<MockStatsObserver>());
348    EXPECT_TRUE(peer_connection_->GetStats(
349        observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
350    EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
351    return observer->AudioInputLevel();
352  }
353
354  int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
355    rtc::scoped_refptr<MockStatsObserver>
356    observer(new rtc::RefCountedObject<MockStatsObserver>());
357    EXPECT_TRUE(peer_connection_->GetStats(
358        observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
359    EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
360    return observer->BytesReceived();
361  }
362
363  int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
364    rtc::scoped_refptr<MockStatsObserver>
365    observer(new rtc::RefCountedObject<MockStatsObserver>());
366    EXPECT_TRUE(peer_connection_->GetStats(
367        observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
368    EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
369    return observer->BytesSent();
370  }
371
372  int GetAvailableReceivedBandwidthStats() {
373    rtc::scoped_refptr<MockStatsObserver>
374        observer(new rtc::RefCountedObject<MockStatsObserver>());
375    EXPECT_TRUE(peer_connection_->GetStats(
376        observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
377    EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
378    int bw = observer->AvailableReceiveBandwidth();
379    return bw;
380  }
381
382  int rendered_width() {
383    EXPECT_FALSE(fake_video_renderers_.empty());
384    return fake_video_renderers_.empty() ? 1 :
385        fake_video_renderers_.begin()->second->width();
386  }
387
388  int rendered_height() {
389    EXPECT_FALSE(fake_video_renderers_.empty());
390    return fake_video_renderers_.empty() ? 1 :
391        fake_video_renderers_.begin()->second->height();
392  }
393
394  size_t number_of_remote_streams() {
395    if (!pc())
396      return 0;
397    return pc()->remote_streams()->count();
398  }
399
400  StreamCollectionInterface* remote_streams() {
401    if (!pc()) {
402      ADD_FAILURE();
403      return NULL;
404    }
405    return pc()->remote_streams();
406  }
407
408  StreamCollectionInterface* local_streams() {
409    if (!pc()) {
410      ADD_FAILURE();
411      return NULL;
412    }
413    return pc()->local_streams();
414  }
415
416  webrtc::PeerConnectionInterface::SignalingState signaling_state() {
417    return pc()->signaling_state();
418  }
419
420  webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
421    return pc()->ice_connection_state();
422  }
423
424  webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
425    return pc()->ice_gathering_state();
426  }
427
428  // PeerConnectionObserver callbacks.
429  virtual void OnError() {}
430  virtual void OnMessage(const std::string&) {}
431  virtual void OnSignalingMessage(const std::string& /*msg*/) {}
432  virtual void OnSignalingChange(
433      webrtc::PeerConnectionInterface::SignalingState new_state) {
434    EXPECT_EQ(peer_connection_->signaling_state(), new_state);
435  }
436  virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
437    for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
438      const std::string id = media_stream->GetVideoTracks()[i]->id();
439      ASSERT_TRUE(fake_video_renderers_.find(id) ==
440          fake_video_renderers_.end());
441      fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
442          media_stream->GetVideoTracks()[i]);
443    }
444  }
445  virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
446  virtual void OnRenegotiationNeeded() {}
447  virtual void OnIceConnectionChange(
448      webrtc::PeerConnectionInterface::IceConnectionState new_state) {
449    EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
450  }
451  virtual void OnIceGatheringChange(
452      webrtc::PeerConnectionInterface::IceGatheringState new_state) {
453    EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
454  }
455  virtual void OnIceCandidate(
456      const webrtc::IceCandidateInterface* /*candidate*/) {}
457
458  webrtc::PeerConnectionInterface* pc() {
459    return peer_connection_.get();
460  }
461  void StopVideoCapturers() {
462    for (std::vector<cricket::VideoCapturer*>::iterator it =
463        video_capturers_.begin(); it != video_capturers_.end(); ++it) {
464      (*it)->Stop();
465    }
466  }
467
468 protected:
469  explicit PeerConnectionTestClientBase(const std::string& id)
470      : id_(id),
471        expect_ice_restart_(false),
472        fake_video_decoder_factory_(NULL),
473        fake_video_encoder_factory_(NULL),
474        video_decoder_factory_enabled_(false),
475        signaling_message_receiver_(NULL) {
476  }
477  bool Init(const MediaConstraintsInterface* constraints) {
478    EXPECT_TRUE(!peer_connection_);
479    EXPECT_TRUE(!peer_connection_factory_);
480    allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
481    if (!allocator_factory_) {
482      return false;
483    }
484    fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
485        rtc::Thread::Current());
486
487    if (fake_audio_capture_module_ == NULL) {
488      return false;
489    }
490    fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
491    fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
492    peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
493        rtc::Thread::Current(), rtc::Thread::Current(),
494        fake_audio_capture_module_, fake_video_encoder_factory_,
495        fake_video_decoder_factory_);
496    if (!peer_connection_factory_) {
497      return false;
498    }
499    peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
500                                            constraints);
501    return peer_connection_.get() != NULL;
502  }
503  virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
504      CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
505                           const MediaConstraintsInterface* constraints) = 0;
506  MessageReceiver* signaling_message_receiver() {
507    return signaling_message_receiver_;
508  }
509  webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
510    return peer_connection_factory_.get();
511  }
512
513  virtual bool can_receive_audio() = 0;
514  virtual bool can_receive_video() = 0;
515  const std::string& id() const { return id_; }
516
517 private:
518  class DummyDtmfObserver : public DtmfSenderObserverInterface {
519   public:
520    DummyDtmfObserver() : completed_(false) {}
521
522    // Implements DtmfSenderObserverInterface.
523    void OnToneChange(const std::string& tone) {
524      tones_.push_back(tone);
525      if (tone.empty()) {
526        completed_ = true;
527      }
528    }
529
530    void Verify(const std::vector<std::string>& tones) const {
531      ASSERT_TRUE(tones_.size() == tones.size());
532      EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
533    }
534
535    bool completed() const { return completed_; }
536
537   private:
538    bool completed_;
539    std::vector<std::string> tones_;
540  };
541
542  rtc::scoped_refptr<webrtc::VideoTrackInterface>
543  CreateLocalVideoTrack(const std::string stream_label) {
544    // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
545    FakeConstraints source_constraints = video_constraints_;
546    source_constraints.SetMandatoryMaxFrameRate(10);
547
548    cricket::FakeVideoCapturer* fake_capturer =
549        new webrtc::FakePeriodicVideoCapturer();
550    video_capturers_.push_back(fake_capturer);
551    rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
552        peer_connection_factory_->CreateVideoSource(
553            fake_capturer, &source_constraints);
554    std::string label = stream_label + kVideoTrackLabelBase;
555    return peer_connection_factory_->CreateVideoTrack(label, source);
556  }
557
558  std::string id_;
559
560  rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
561      allocator_factory_;
562  rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
563  rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
564      peer_connection_factory_;
565
566  typedef std::pair<std::string, std::string> IceUfragPwdPair;
567  std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
568  bool expect_ice_restart_;
569
570  // Needed to keep track of number of frames send.
571  rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
572  // Needed to keep track of number of frames received.
573  typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
574  RenderMap fake_video_renderers_;
575  // Needed to keep track of number of frames received when external decoder
576  // used.
577  FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
578  FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
579  bool video_decoder_factory_enabled_;
580  webrtc::FakeConstraints video_constraints_;
581
582  // For remote peer communication.
583  MessageReceiver* signaling_message_receiver_;
584
585  // Store references to the video capturers we've created, so that we can stop
586  // them, if required.
587  std::vector<cricket::VideoCapturer*> video_capturers_;
588};
589
590class JsepTestClient
591    : public PeerConnectionTestClientBase<JsepMessageReceiver> {
592 public:
593  static JsepTestClient* CreateClient(
594      const std::string& id,
595      const MediaConstraintsInterface* constraints) {
596    JsepTestClient* client(new JsepTestClient(id));
597    if (!client->Init(constraints)) {
598      delete client;
599      return NULL;
600    }
601    return client;
602  }
603  ~JsepTestClient() {}
604
605  virtual void Negotiate() {
606    Negotiate(true, true);
607  }
608  virtual void Negotiate(bool audio, bool video) {
609    rtc::scoped_ptr<SessionDescriptionInterface> offer;
610    ASSERT_TRUE(DoCreateOffer(offer.use()));
611
612    if (offer->description()->GetContentByName("audio")) {
613      offer->description()->GetContentByName("audio")->rejected = !audio;
614    }
615    if (offer->description()->GetContentByName("video")) {
616      offer->description()->GetContentByName("video")->rejected = !video;
617    }
618
619    std::string sdp;
620    EXPECT_TRUE(offer->ToString(&sdp));
621    EXPECT_TRUE(DoSetLocalDescription(offer.release()));
622    signaling_message_receiver()->ReceiveSdpMessage(
623        webrtc::SessionDescriptionInterface::kOffer, sdp);
624  }
625  // JsepMessageReceiver callback.
626  virtual void ReceiveSdpMessage(const std::string& type,
627                                 std::string& msg) {
628    FilterIncomingSdpMessage(&msg);
629    if (type == webrtc::SessionDescriptionInterface::kOffer) {
630      HandleIncomingOffer(msg);
631    } else {
632      HandleIncomingAnswer(msg);
633    }
634  }
635  // JsepMessageReceiver callback.
636  virtual void ReceiveIceMessage(const std::string& sdp_mid,
637                                 int sdp_mline_index,
638                                 const std::string& msg) {
639    LOG(INFO) << id() << "ReceiveIceMessage";
640    rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
641        webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
642    EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
643  }
644  // Implements PeerConnectionObserver functions needed by Jsep.
645  virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
646    LOG(INFO) << id() << "OnIceCandidate";
647
648    std::string ice_sdp;
649    EXPECT_TRUE(candidate->ToString(&ice_sdp));
650    if (signaling_message_receiver() == NULL) {
651      // Remote party may be deleted.
652      return;
653    }
654    signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
655        candidate->sdp_mline_index(), ice_sdp);
656  }
657
658  void IceRestart() {
659    session_description_constraints_.SetMandatoryIceRestart(true);
660    SetExpectIceRestart(true);
661  }
662
663  void SetReceiveAudioVideo(bool audio, bool video) {
664    SetReceiveAudio(audio);
665    SetReceiveVideo(video);
666    ASSERT_EQ(audio, can_receive_audio());
667    ASSERT_EQ(video, can_receive_video());
668  }
669
670  void SetReceiveAudio(bool audio) {
671    if (audio && can_receive_audio())
672      return;
673    session_description_constraints_.SetMandatoryReceiveAudio(audio);
674  }
675
676  void SetReceiveVideo(bool video) {
677    if (video && can_receive_video())
678      return;
679    session_description_constraints_.SetMandatoryReceiveVideo(video);
680  }
681
682  void RemoveMsidFromReceivedSdp(bool remove) {
683    remove_msid_ = remove;
684  }
685
686  void RemoveSdesCryptoFromReceivedSdp(bool remove) {
687    remove_sdes_ = remove;
688  }
689
690  void RemoveBundleFromReceivedSdp(bool remove) {
691    remove_bundle_ = remove;
692  }
693
694  virtual bool can_receive_audio() {
695    bool value;
696    if (webrtc::FindConstraint(&session_description_constraints_,
697        MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
698      return value;
699    }
700    return true;
701  }
702
703  virtual bool can_receive_video() {
704    bool value;
705    if (webrtc::FindConstraint(&session_description_constraints_,
706        MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
707      return value;
708    }
709    return true;
710  }
711
712  virtual void OnIceComplete() {
713    LOG(INFO) << id() << "OnIceComplete";
714  }
715
716  virtual void OnDataChannel(DataChannelInterface* data_channel) {
717    LOG(INFO) << id() << "OnDataChannel";
718    data_channel_ = data_channel;
719    data_observer_.reset(new MockDataChannelObserver(data_channel));
720  }
721
722  void CreateDataChannel() {
723    data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
724                                                         NULL);
725    ASSERT_TRUE(data_channel_.get() != NULL);
726    data_observer_.reset(new MockDataChannelObserver(data_channel_));
727  }
728
729  DataChannelInterface* data_channel() { return data_channel_; }
730  const MockDataChannelObserver* data_observer() const {
731    return data_observer_.get();
732  }
733
734 protected:
735  explicit JsepTestClient(const std::string& id)
736      : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
737        remove_msid_(false),
738        remove_bundle_(false),
739        remove_sdes_(false) {
740  }
741
742  virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
743      CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
744                           const MediaConstraintsInterface* constraints) {
745    // CreatePeerConnection with IceServers.
746    webrtc::PeerConnectionInterface::IceServers ice_servers;
747    webrtc::PeerConnectionInterface::IceServer ice_server;
748    ice_server.uri = "stun:stun.l.google.com:19302";
749    ice_servers.push_back(ice_server);
750
751    FakeIdentityService* dtls_service =
752        rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
753            new FakeIdentityService() : NULL;
754    return peer_connection_factory()->CreatePeerConnection(
755        ice_servers, constraints, factory, dtls_service, this);
756  }
757
758  void HandleIncomingOffer(const std::string& msg) {
759    LOG(INFO) << id() << "HandleIncomingOffer ";
760    if (NumberOfLocalMediaStreams() == 0) {
761      // If we are not sending any streams ourselves it is time to add some.
762      AddMediaStream(true, true);
763    }
764    rtc::scoped_ptr<SessionDescriptionInterface> desc(
765         webrtc::CreateSessionDescription("offer", msg, NULL));
766    EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
767    rtc::scoped_ptr<SessionDescriptionInterface> answer;
768    EXPECT_TRUE(DoCreateAnswer(answer.use()));
769    std::string sdp;
770    EXPECT_TRUE(answer->ToString(&sdp));
771    EXPECT_TRUE(DoSetLocalDescription(answer.release()));
772    if (signaling_message_receiver()) {
773      signaling_message_receiver()->ReceiveSdpMessage(
774          webrtc::SessionDescriptionInterface::kAnswer, sdp);
775    }
776  }
777
778  void HandleIncomingAnswer(const std::string& msg) {
779    LOG(INFO) << id() << "HandleIncomingAnswer";
780    rtc::scoped_ptr<SessionDescriptionInterface> desc(
781         webrtc::CreateSessionDescription("answer", msg, NULL));
782    EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
783  }
784
785  bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
786                           bool offer) {
787    rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
788        observer(new rtc::RefCountedObject<
789            MockCreateSessionDescriptionObserver>());
790    if (offer) {
791      pc()->CreateOffer(observer, &session_description_constraints_);
792    } else {
793      pc()->CreateAnswer(observer, &session_description_constraints_);
794    }
795    EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
796    *desc = observer->release_desc();
797    if (observer->result() && ExpectIceRestart()) {
798      EXPECT_EQ(0u, (*desc)->candidates(0)->count());
799    }
800    return observer->result();
801  }
802
803  bool DoCreateOffer(SessionDescriptionInterface** desc) {
804    return DoCreateOfferAnswer(desc, true);
805  }
806
807  bool DoCreateAnswer(SessionDescriptionInterface** desc) {
808    return DoCreateOfferAnswer(desc, false);
809  }
810
811  bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
812    rtc::scoped_refptr<MockSetSessionDescriptionObserver>
813            observer(new rtc::RefCountedObject<
814                MockSetSessionDescriptionObserver>());
815    LOG(INFO) << id() << "SetLocalDescription ";
816    pc()->SetLocalDescription(observer, desc);
817    // Ignore the observer result. If we wait for the result with
818    // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
819    // before the offer which is an error.
820    // The reason is that EXPECT_TRUE_WAIT uses
821    // rtc::Thread::Current()->ProcessMessages(1);
822    // ProcessMessages waits at least 1ms but processes all messages before
823    // returning. Since this test is synchronous and send messages to the remote
824    // peer whenever a callback is invoked, this can lead to messages being
825    // sent to the remote peer in the wrong order.
826    // TODO(perkj): Find a way to check the result without risking that the
827    // order of sent messages are changed. Ex- by posting all messages that are
828    // sent to the remote peer.
829    return true;
830  }
831
832  bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
833    rtc::scoped_refptr<MockSetSessionDescriptionObserver>
834        observer(new rtc::RefCountedObject<
835            MockSetSessionDescriptionObserver>());
836    LOG(INFO) << id() << "SetRemoteDescription ";
837    pc()->SetRemoteDescription(observer, desc);
838    EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
839    return observer->result();
840  }
841
842  // This modifies all received SDP messages before they are processed.
843  void FilterIncomingSdpMessage(std::string* sdp) {
844    if (remove_msid_) {
845      const char kSdpSsrcAttribute[] = "a=ssrc:";
846      RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
847      const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
848      RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
849    }
850    if (remove_bundle_) {
851      const char kSdpBundleAttribute[] = "a=group:BUNDLE";
852      RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
853    }
854    if (remove_sdes_) {
855      const char kSdpSdesCryptoAttribute[] = "a=crypto";
856      RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
857    }
858  }
859
860 private:
861  webrtc::FakeConstraints session_description_constraints_;
862  bool remove_msid_;  // True if MSID should be removed in received SDP.
863  bool remove_bundle_;  // True if bundle should be removed in received SDP.
864  bool remove_sdes_;  // True if a=crypto should be removed in received SDP.
865
866  rtc::scoped_refptr<DataChannelInterface> data_channel_;
867  rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
868};
869
870template <typename SignalingClass>
871class P2PTestConductor : public testing::Test {
872 public:
873  bool SessionActive() {
874    return initiating_client_->SessionActive() &&
875        receiving_client_->SessionActive();
876  }
877  // Return true if the number of frames provided have been received or it is
878  // known that that will never occur (e.g. no frames will be sent or
879  // captured).
880  bool FramesNotPending(int audio_frames_to_receive,
881                        int video_frames_to_receive) {
882    return VideoFramesReceivedCheck(video_frames_to_receive) &&
883        AudioFramesReceivedCheck(audio_frames_to_receive);
884  }
885  bool AudioFramesReceivedCheck(int frames_received) {
886    return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
887        receiving_client_->AudioFramesReceivedCheck(frames_received);
888  }
889  bool VideoFramesReceivedCheck(int frames_received) {
890    return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
891        receiving_client_->VideoFramesReceivedCheck(frames_received);
892  }
893  void VerifyDtmf() {
894    initiating_client_->VerifyDtmf();
895    receiving_client_->VerifyDtmf();
896  }
897
898  void TestUpdateOfferWithRejectedContent() {
899    initiating_client_->Negotiate(true, false);
900    EXPECT_TRUE_WAIT(
901        FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
902        kMaxWaitForFramesMs);
903    // There shouldn't be any more video frame after the new offer is
904    // negotiated.
905    EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
906  }
907
908  void VerifyRenderedSize(int width, int height) {
909    EXPECT_EQ(width, receiving_client()->rendered_width());
910    EXPECT_EQ(height, receiving_client()->rendered_height());
911    EXPECT_EQ(width, initializing_client()->rendered_width());
912    EXPECT_EQ(height, initializing_client()->rendered_height());
913  }
914
915  void VerifySessionDescriptions() {
916    initiating_client_->VerifyRejectedMediaInSessionDescription();
917    receiving_client_->VerifyRejectedMediaInSessionDescription();
918    initiating_client_->VerifyLocalIceUfragAndPassword();
919    receiving_client_->VerifyLocalIceUfragAndPassword();
920  }
921
922  P2PTestConductor() {
923    rtc::InitializeSSL(NULL);
924  }
925  ~P2PTestConductor() {
926    if (initiating_client_) {
927      initiating_client_->set_signaling_message_receiver(NULL);
928    }
929    if (receiving_client_) {
930      receiving_client_->set_signaling_message_receiver(NULL);
931    }
932    rtc::CleanupSSL();
933  }
934
935  bool CreateTestClients() {
936    return CreateTestClients(NULL, NULL);
937  }
938
939  bool CreateTestClients(MediaConstraintsInterface* init_constraints,
940                         MediaConstraintsInterface* recv_constraints) {
941    initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
942                                                          init_constraints));
943    receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
944                                                         recv_constraints));
945    if (!initiating_client_ || !receiving_client_) {
946      return false;
947    }
948    initiating_client_->set_signaling_message_receiver(receiving_client_.get());
949    receiving_client_->set_signaling_message_receiver(initiating_client_.get());
950    return true;
951  }
952
953  void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
954                           const webrtc::FakeConstraints& recv_constraints) {
955    initiating_client_->SetVideoConstraints(init_constraints);
956    receiving_client_->SetVideoConstraints(recv_constraints);
957  }
958
959  void EnableVideoDecoderFactory() {
960    initiating_client_->EnableVideoDecoderFactory();
961    receiving_client_->EnableVideoDecoderFactory();
962  }
963
964  // This test sets up a call between two parties. Both parties send static
965  // frames to each other. Once the test is finished the number of sent frames
966  // is compared to the number of received frames.
967  void LocalP2PTest() {
968    if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
969      initiating_client_->AddMediaStream(true, true);
970    }
971    initiating_client_->Negotiate();
972    const int kMaxWaitForActivationMs = 5000;
973    // Assert true is used here since next tests are guaranteed to fail and
974    // would eat up 5 seconds.
975    ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
976    VerifySessionDescriptions();
977
978
979    int audio_frame_count = kEndAudioFrameCount;
980    // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
981    if (!initiating_client_->can_receive_audio() ||
982        !receiving_client_->can_receive_audio()) {
983      audio_frame_count = -1;
984    }
985    int video_frame_count = kEndVideoFrameCount;
986    if (!initiating_client_->can_receive_video() ||
987        !receiving_client_->can_receive_video()) {
988      video_frame_count = -1;
989    }
990
991    if (audio_frame_count != -1 || video_frame_count != -1) {
992      // Audio or video is expected to flow, so both clients should reach the
993      // Connected state, and the offerer (ICE controller) should proceed to
994      // Completed.
995      // Note: These tests have been observed to fail under heavy load at
996      // shorter timeouts, so they may be flaky.
997      EXPECT_EQ_WAIT(
998          webrtc::PeerConnectionInterface::kIceConnectionCompleted,
999          initiating_client_->ice_connection_state(),
1000          kMaxWaitForFramesMs);
1001      EXPECT_EQ_WAIT(
1002          webrtc::PeerConnectionInterface::kIceConnectionConnected,
1003          receiving_client_->ice_connection_state(),
1004          kMaxWaitForFramesMs);
1005    }
1006
1007    if (initiating_client_->can_receive_audio() ||
1008        initiating_client_->can_receive_video()) {
1009      // The initiating client can receive media, so it must produce candidates
1010      // that will serve as destinations for that media.
1011      // TODO(bemasc): Understand why the state is not already Complete here, as
1012      // seems to be the case for the receiving client. This may indicate a bug
1013      // in the ICE gathering system.
1014      EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1015                initiating_client_->ice_gathering_state());
1016    }
1017    if (receiving_client_->can_receive_audio() ||
1018        receiving_client_->can_receive_video()) {
1019      EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1020                     receiving_client_->ice_gathering_state(),
1021                     kMaxWaitForFramesMs);
1022    }
1023
1024    EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1025                     kMaxWaitForFramesMs);
1026  }
1027
1028  void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1029    // Messages may get lost on the unreliable DataChannel, so we send multiple
1030    // times to avoid test flakiness.
1031    static const size_t kSendAttempts = 5;
1032
1033    for (size_t i = 0; i < kSendAttempts; ++i) {
1034      dc->Send(DataBuffer(data));
1035    }
1036  }
1037
1038  // Wait until 'size' bytes of audio has been seen by the receiver, on the
1039  // first audio stream.
1040  void WaitForAudioData(int size) {
1041    const int kMaxWaitForAudioDataMs = 10000;
1042
1043    StreamCollectionInterface* local_streams =
1044        initializing_client()->local_streams();
1045    ASSERT_GT(local_streams->count(), 0u);
1046    ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1047    MediaStreamTrackInterface* local_audio_track =
1048        local_streams->at(0)->GetAudioTracks()[0];
1049
1050    // Wait until *any* audio has been received.
1051    EXPECT_TRUE_WAIT(
1052        receiving_client()->GetBytesReceivedStats(local_audio_track) > 0,
1053        kMaxWaitForAudioDataMs);
1054
1055    // Wait until 'size' number of bytes have been received.
1056    size += receiving_client()->GetBytesReceivedStats(local_audio_track);
1057    EXPECT_TRUE_WAIT(
1058        receiving_client()->GetBytesReceivedStats(local_audio_track) > size,
1059        kMaxWaitForAudioDataMs);
1060  }
1061
1062  SignalingClass* initializing_client() { return initiating_client_.get(); }
1063  SignalingClass* receiving_client() { return receiving_client_.get(); }
1064
1065 private:
1066  rtc::scoped_ptr<SignalingClass> initiating_client_;
1067  rtc::scoped_ptr<SignalingClass> receiving_client_;
1068};
1069typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1070
1071// Disable for TSan v2, see
1072// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1073#if !defined(THREAD_SANITIZER)
1074
1075// This test sets up a Jsep call between two parties and test Dtmf.
1076// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1077// See issue webrtc/2378.
1078TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
1079  ASSERT_TRUE(CreateTestClients());
1080  LocalP2PTest();
1081  VerifyDtmf();
1082}
1083
1084// This test sets up a Jsep call between two parties and test that we can get a
1085// video aspect ratio of 16:9.
1086TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1087  ASSERT_TRUE(CreateTestClients());
1088  FakeConstraints constraint;
1089  double requested_ratio = 640.0/360;
1090  constraint.SetMandatoryMinAspectRatio(requested_ratio);
1091  SetVideoConstraints(constraint, constraint);
1092  LocalP2PTest();
1093
1094  ASSERT_LE(0, initializing_client()->rendered_height());
1095  double initiating_video_ratio =
1096      static_cast<double>(initializing_client()->rendered_width()) /
1097      initializing_client()->rendered_height();
1098  EXPECT_LE(requested_ratio, initiating_video_ratio);
1099
1100  ASSERT_LE(0, receiving_client()->rendered_height());
1101  double receiving_video_ratio =
1102      static_cast<double>(receiving_client()->rendered_width()) /
1103      receiving_client()->rendered_height();
1104  EXPECT_LE(requested_ratio, receiving_video_ratio);
1105}
1106
1107// This test sets up a Jsep call between two parties and test that the
1108// received video has a resolution of 1280*720.
1109// TODO(mallinath): Enable when
1110// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1111TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1112  ASSERT_TRUE(CreateTestClients());
1113  FakeConstraints constraint;
1114  constraint.SetMandatoryMinWidth(1280);
1115  constraint.SetMandatoryMinHeight(720);
1116  SetVideoConstraints(constraint, constraint);
1117  LocalP2PTest();
1118  VerifyRenderedSize(1280, 720);
1119}
1120
1121// This test sets up a call between two endpoints that are configured to use
1122// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1123TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
1124  MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1125  FakeConstraints setup_constraints;
1126  setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1127                                 true);
1128  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1129  LocalP2PTest();
1130  VerifyRenderedSize(640, 480);
1131}
1132
1133// This test sets up a audio call initially and then upgrades to audio/video,
1134// using DTLS.
1135TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
1136  MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1137  FakeConstraints setup_constraints;
1138  setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1139                                 true);
1140  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1141  receiving_client()->SetReceiveAudioVideo(true, false);
1142  LocalP2PTest();
1143  receiving_client()->SetReceiveAudioVideo(true, true);
1144  receiving_client()->Negotiate();
1145}
1146
1147// This test sets up a call between two endpoints that are configured to use
1148// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1149// negotiated and used for transport.
1150TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
1151  MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1152  FakeConstraints setup_constraints;
1153  setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1154                                 true);
1155  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1156  receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1157  LocalP2PTest();
1158  VerifyRenderedSize(640, 480);
1159}
1160
1161// This test sets up a Jsep call between two parties, and the callee only
1162// accept to receive video.
1163// BUG=https://code.google.com/p/webrtc/issues/detail?id=2288
1164TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerVideo) {
1165  ASSERT_TRUE(CreateTestClients());
1166  receiving_client()->SetReceiveAudioVideo(false, true);
1167  LocalP2PTest();
1168}
1169
1170// This test sets up a Jsep call between two parties, and the callee only
1171// accept to receive audio.
1172TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerAudio) {
1173  ASSERT_TRUE(CreateTestClients());
1174  receiving_client()->SetReceiveAudioVideo(true, false);
1175  LocalP2PTest();
1176}
1177
1178// This test sets up a Jsep call between two parties, and the callee reject both
1179// audio and video.
1180TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1181  ASSERT_TRUE(CreateTestClients());
1182  receiving_client()->SetReceiveAudioVideo(false, false);
1183  LocalP2PTest();
1184}
1185
1186// This test sets up an audio and video call between two parties. After the call
1187// runs for a while (10 frames), the caller sends an update offer with video
1188// being rejected. Once the re-negotiation is done, the video flow should stop
1189// and the audio flow should continue.
1190// Disabled due to b/14955157.
1191TEST_F(JsepPeerConnectionP2PTestClient,
1192       DISABLED_UpdateOfferWithRejectedContent) {
1193  ASSERT_TRUE(CreateTestClients());
1194  LocalP2PTest();
1195  TestUpdateOfferWithRejectedContent();
1196}
1197
1198// This test sets up a Jsep call between two parties. The MSID is removed from
1199// the SDP strings from the caller.
1200// Disabled due to b/14955157.
1201TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
1202  ASSERT_TRUE(CreateTestClients());
1203  receiving_client()->RemoveMsidFromReceivedSdp(true);
1204  // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1205  // audio and video is muxed when MSID is disabled. Remove
1206  // SetRemoveBundleFromSdp once
1207  // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1208  receiving_client()->RemoveBundleFromReceivedSdp(true);
1209  LocalP2PTest();
1210}
1211
1212// This test sets up a Jsep call between two parties and the initiating peer
1213// sends two steams.
1214// TODO(perkj): Disabled due to
1215// https://code.google.com/p/webrtc/issues/detail?id=1454
1216TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1217  ASSERT_TRUE(CreateTestClients());
1218  // Set optional video constraint to max 320pixels to decrease CPU usage.
1219  FakeConstraints constraint;
1220  constraint.SetOptionalMaxWidth(320);
1221  SetVideoConstraints(constraint, constraint);
1222  initializing_client()->AddMediaStream(true, true);
1223  initializing_client()->AddMediaStream(false, true);
1224  ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1225  LocalP2PTest();
1226  EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1227}
1228
1229// Test that we can receive the audio output level from a remote audio track.
1230TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1231  ASSERT_TRUE(CreateTestClients());
1232  LocalP2PTest();
1233
1234  StreamCollectionInterface* remote_streams =
1235      initializing_client()->remote_streams();
1236  ASSERT_GT(remote_streams->count(), 0u);
1237  ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1238  MediaStreamTrackInterface* remote_audio_track =
1239      remote_streams->at(0)->GetAudioTracks()[0];
1240
1241  // Get the audio output level stats. Note that the level is not available
1242  // until a RTCP packet has been received.
1243  EXPECT_TRUE_WAIT(
1244      initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1245      kMaxWaitForStatsMs);
1246}
1247
1248// Test that an audio input level is reported.
1249TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1250  ASSERT_TRUE(CreateTestClients());
1251  LocalP2PTest();
1252
1253  // Get the audio input level stats.  The level should be available very
1254  // soon after the test starts.
1255  EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1256      kMaxWaitForStatsMs);
1257}
1258
1259// Test that we can get incoming byte counts from both audio and video tracks.
1260TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1261  ASSERT_TRUE(CreateTestClients());
1262  LocalP2PTest();
1263
1264  StreamCollectionInterface* remote_streams =
1265      initializing_client()->remote_streams();
1266  ASSERT_GT(remote_streams->count(), 0u);
1267  ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1268  MediaStreamTrackInterface* remote_audio_track =
1269      remote_streams->at(0)->GetAudioTracks()[0];
1270  EXPECT_TRUE_WAIT(
1271      initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1272      kMaxWaitForStatsMs);
1273
1274  MediaStreamTrackInterface* remote_video_track =
1275      remote_streams->at(0)->GetVideoTracks()[0];
1276  EXPECT_TRUE_WAIT(
1277      initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1278      kMaxWaitForStatsMs);
1279}
1280
1281// Test that we can get outgoing byte counts from both audio and video tracks.
1282TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1283  ASSERT_TRUE(CreateTestClients());
1284  LocalP2PTest();
1285
1286  StreamCollectionInterface* local_streams =
1287      initializing_client()->local_streams();
1288  ASSERT_GT(local_streams->count(), 0u);
1289  ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1290  MediaStreamTrackInterface* local_audio_track =
1291      local_streams->at(0)->GetAudioTracks()[0];
1292  EXPECT_TRUE_WAIT(
1293      initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1294      kMaxWaitForStatsMs);
1295
1296  MediaStreamTrackInterface* local_video_track =
1297      local_streams->at(0)->GetVideoTracks()[0];
1298  EXPECT_TRUE_WAIT(
1299      initializing_client()->GetBytesSentStats(local_video_track) > 0,
1300      kMaxWaitForStatsMs);
1301}
1302
1303// This test sets up a call between two parties with audio, video and data.
1304TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1305  FakeConstraints setup_constraints;
1306  setup_constraints.SetAllowRtpDataChannels();
1307  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1308  initializing_client()->CreateDataChannel();
1309  LocalP2PTest();
1310  ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1311  ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1312  EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1313                   kMaxWaitMs);
1314  EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1315                   kMaxWaitMs);
1316
1317  std::string data = "hello world";
1318
1319  SendRtpData(initializing_client()->data_channel(), data);
1320  EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1321                 kMaxWaitMs);
1322
1323  SendRtpData(receiving_client()->data_channel(), data);
1324  EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1325                 kMaxWaitMs);
1326
1327  receiving_client()->data_channel()->Close();
1328  // Send new offer and answer.
1329  receiving_client()->Negotiate();
1330  EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1331  EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1332}
1333
1334// This test sets up a call between two parties and creates a data channel.
1335// The test tests that received data is buffered unless an observer has been
1336// registered.
1337// Rtp data channels can receive data before the underlying
1338// transport has detected that a channel is writable and thus data can be
1339// received before the data channel state changes to open. That is hard to test
1340// but the same buffering is used in that case.
1341TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1342  FakeConstraints setup_constraints;
1343  setup_constraints.SetAllowRtpDataChannels();
1344  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1345  initializing_client()->CreateDataChannel();
1346  initializing_client()->Negotiate();
1347
1348  ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1349  ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1350  EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1351                   kMaxWaitMs);
1352  EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1353                 receiving_client()->data_channel()->state(), kMaxWaitMs);
1354
1355  // Unregister the existing observer.
1356  receiving_client()->data_channel()->UnregisterObserver();
1357
1358  std::string data = "hello world";
1359  SendRtpData(initializing_client()->data_channel(), data);
1360
1361  // Wait a while to allow the sent data to arrive before an observer is
1362  // registered..
1363  rtc::Thread::Current()->ProcessMessages(100);
1364
1365  MockDataChannelObserver new_observer(receiving_client()->data_channel());
1366  EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1367}
1368
1369// This test sets up a call between two parties with audio, video and but only
1370// the initiating client support data.
1371TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
1372  FakeConstraints setup_constraints_1;
1373  setup_constraints_1.SetAllowRtpDataChannels();
1374  // Must disable DTLS to make negotiation succeed.
1375  setup_constraints_1.SetMandatory(
1376      MediaConstraintsInterface::kEnableDtlsSrtp, false);
1377  FakeConstraints setup_constraints_2;
1378  setup_constraints_2.SetMandatory(
1379      MediaConstraintsInterface::kEnableDtlsSrtp, false);
1380  ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
1381  initializing_client()->CreateDataChannel();
1382  LocalP2PTest();
1383  EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1384  EXPECT_FALSE(receiving_client()->data_channel());
1385  EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1386}
1387
1388// This test sets up a call between two parties with audio, video. When audio
1389// and video is setup and flowing and data channel is negotiated.
1390TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1391  FakeConstraints setup_constraints;
1392  setup_constraints.SetAllowRtpDataChannels();
1393  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1394  LocalP2PTest();
1395  initializing_client()->CreateDataChannel();
1396  // Send new offer and answer.
1397  initializing_client()->Negotiate();
1398  ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1399  ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1400  EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1401                   kMaxWaitMs);
1402  EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1403                   kMaxWaitMs);
1404}
1405
1406// This test sets up a Jsep call with SCTP DataChannel and verifies the
1407// negotiation is completed without error.
1408#ifdef HAVE_SCTP
1409TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
1410  MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1411  FakeConstraints constraints;
1412  constraints.SetMandatory(
1413      MediaConstraintsInterface::kEnableDtlsSrtp, true);
1414  ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1415  initializing_client()->CreateDataChannel();
1416  initializing_client()->Negotiate(false, false);
1417}
1418#endif
1419
1420// This test sets up a call between two parties with audio, and video.
1421// During the call, the initializing side restart ice and the test verifies that
1422// new ice candidates are generated and audio and video still can flow.
1423TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1424  ASSERT_TRUE(CreateTestClients());
1425
1426  // Negotiate and wait for ice completion and make sure audio and video plays.
1427  LocalP2PTest();
1428
1429  // Create a SDP string of the first audio candidate for both clients.
1430  const webrtc::IceCandidateCollection* audio_candidates_initiator =
1431      initializing_client()->pc()->local_description()->candidates(0);
1432  const webrtc::IceCandidateCollection* audio_candidates_receiver =
1433      receiving_client()->pc()->local_description()->candidates(0);
1434  ASSERT_GT(audio_candidates_initiator->count(), 0u);
1435  ASSERT_GT(audio_candidates_receiver->count(), 0u);
1436  std::string initiator_candidate;
1437  EXPECT_TRUE(
1438      audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1439  std::string receiver_candidate;
1440  EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1441
1442  // Restart ice on the initializing client.
1443  receiving_client()->SetExpectIceRestart(true);
1444  initializing_client()->IceRestart();
1445
1446  // Negotiate and wait for ice completion again and make sure audio and video
1447  // plays.
1448  LocalP2PTest();
1449
1450  // Create a SDP string of the first audio candidate for both clients again.
1451  const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1452      initializing_client()->pc()->local_description()->candidates(0);
1453  const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1454      receiving_client()->pc()->local_description()->candidates(0);
1455  ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1456  ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1457  std::string initiator_candidate_restart;
1458  EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1459      &initiator_candidate_restart));
1460  std::string receiver_candidate_restart;
1461  EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1462      &receiver_candidate_restart));
1463
1464  // Verify that the first candidates in the local session descriptions has
1465  // changed.
1466  EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1467  EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1468}
1469
1470
1471// This test sets up a Jsep call between two parties with external
1472// VideoDecoderFactory.
1473// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1474// See issue webrtc/2378.
1475TEST_F(JsepPeerConnectionP2PTestClient,
1476       DISABLED_LocalP2PTestWithVideoDecoderFactory) {
1477  ASSERT_TRUE(CreateTestClients());
1478  EnableVideoDecoderFactory();
1479  LocalP2PTest();
1480}
1481
1482// Test receive bandwidth stats with only audio enabled at receiver.
1483TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsAudio) {
1484  ASSERT_TRUE(CreateTestClients());
1485  receiving_client()->SetReceiveAudioVideo(true, false);
1486  LocalP2PTest();
1487
1488  // Wait until we have received some audio data. Following REMB shoud be zero.
1489  WaitForAudioData(10000);
1490  EXPECT_EQ_WAIT(
1491      receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1492      kMaxWaitForRembMs);
1493}
1494
1495// Test receive bandwidth stats with combined BWE.
1496TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsCombined) {
1497  FakeConstraints setup_constraints;
1498  setup_constraints.AddOptional(
1499      MediaConstraintsInterface::kCombinedAudioVideoBwe, true);
1500  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1501  initializing_client()->AddMediaStream(true, true);
1502  initializing_client()->AddMediaStream(false, true);
1503  initializing_client()->AddMediaStream(false, true);
1504  initializing_client()->AddMediaStream(false, true);
1505  LocalP2PTest();
1506
1507  // Run until a non-zero bw is reported.
1508  EXPECT_TRUE_WAIT(receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1509                   kMaxWaitForRembMs);
1510
1511  // Halt video capturers, then run until we have gotten some audio. Following
1512  // REMB should be non-zero.
1513  initializing_client()->StopVideoCapturers();
1514  WaitForAudioData(10000);
1515  EXPECT_TRUE_WAIT(
1516      receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1517      kMaxWaitForRembMs);
1518}
1519
1520// Test receive bandwidth stats with 1 video, 3 audio streams but no combined
1521// BWE.
1522TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsNotCombined) {
1523  FakeConstraints setup_constraints;
1524  setup_constraints.AddOptional(
1525      MediaConstraintsInterface::kCombinedAudioVideoBwe, false);
1526  ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1527  initializing_client()->AddMediaStream(true, true);
1528  initializing_client()->AddMediaStream(false, true);
1529  initializing_client()->AddMediaStream(false, true);
1530  initializing_client()->AddMediaStream(false, true);
1531  LocalP2PTest();
1532
1533  // Run until a non-zero bw is reported.
1534  EXPECT_TRUE_WAIT(receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1535                   kMaxWaitForRembMs);
1536
1537  // Halt video capturers, then run until we have gotten some audio. Following
1538  // REMB should be zero.
1539  initializing_client()->StopVideoCapturers();
1540  WaitForAudioData(10000);
1541  EXPECT_EQ_WAIT(
1542      receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1543      kMaxWaitForRembMs);
1544}
1545
1546#endif // if !defined(THREAD_SANITIZER)
1547