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