You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

859 lines
33 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include <string>
  28. #include "talk/app/webrtc/fakeportallocatorfactory.h"
  29. #include "talk/app/webrtc/jsepsessiondescription.h"
  30. #include "talk/app/webrtc/localvideosource.h"
  31. #include "talk/app/webrtc/mediastreaminterface.h"
  32. #include "talk/app/webrtc/peerconnectioninterface.h"
  33. #include "talk/app/webrtc/test/fakeconstraints.h"
  34. #include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
  35. #include "talk/base/gunit.h"
  36. #include "talk/base/scoped_ptr.h"
  37. #include "talk/base/stringutils.h"
  38. #include "talk/base/thread.h"
  39. #include "talk/media/base/fakevideocapturer.h"
  40. #include "talk/session/media/mediasession.h"
  41. static const char kStreamLabel1[] = "local_stream_1";
  42. static const char kStreamLabel2[] = "local_stream_2";
  43. static const char kStreamLabel3[] = "local_stream_3";
  44. static const int kDefaultStunPort = 3478;
  45. static const char kStunAddressOnly[] = "stun:address";
  46. static const char kStunInvalidPort[] = "stun:address:-1";
  47. static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
  48. static const char kStunAddressPortAndMore2[] = "stun:address:port more";
  49. static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
  50. static const char kTurnUsername[] = "user";
  51. static const char kTurnPassword[] = "password";
  52. static const char kTurnHostname[] = "turn.example.org";
  53. static const uint32 kTimeout = 5000U;
  54. using talk_base::scoped_ptr;
  55. using talk_base::scoped_refptr;
  56. using webrtc::AudioSourceInterface;
  57. using webrtc::AudioTrackInterface;
  58. using webrtc::DataBuffer;
  59. using webrtc::DataChannelInterface;
  60. using webrtc::FakeConstraints;
  61. using webrtc::FakePortAllocatorFactory;
  62. using webrtc::IceCandidateInterface;
  63. using webrtc::LocalMediaStreamInterface;
  64. using webrtc::MediaStreamInterface;
  65. using webrtc::MediaStreamTrackInterface;
  66. using webrtc::MockCreateSessionDescriptionObserver;
  67. using webrtc::MockDataChannelObserver;
  68. using webrtc::MockSetSessionDescriptionObserver;
  69. using webrtc::MockStatsObserver;
  70. using webrtc::PeerConnectionInterface;
  71. using webrtc::PeerConnectionObserver;
  72. using webrtc::PortAllocatorFactoryInterface;
  73. using webrtc::SdpParseError;
  74. using webrtc::SessionDescriptionInterface;
  75. using webrtc::VideoSourceInterface;
  76. using webrtc::VideoTrackInterface;
  77. namespace {
  78. // Gets the first ssrc of given content type from the ContentInfo.
  79. bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
  80. if (!content_info || !ssrc) {
  81. return false;
  82. }
  83. const cricket::MediaContentDescription* media_desc =
  84. static_cast<const cricket::MediaContentDescription*> (
  85. content_info->description);
  86. if (!media_desc || media_desc->streams().empty()) {
  87. return false;
  88. }
  89. *ssrc = media_desc->streams().begin()->first_ssrc();
  90. return true;
  91. }
  92. void SetSsrcToZero(std::string* sdp) {
  93. const char kSdpSsrcAtribute[] = "a=ssrc:";
  94. const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
  95. size_t ssrc_pos = 0;
  96. while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
  97. std::string::npos) {
  98. size_t end_ssrc = sdp->find(" ", ssrc_pos);
  99. sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
  100. ssrc_pos = end_ssrc;
  101. }
  102. }
  103. class MockPeerConnectionObserver : public PeerConnectionObserver {
  104. public:
  105. MockPeerConnectionObserver()
  106. : renegotiation_needed_(false),
  107. ice_complete_(false) {
  108. }
  109. ~MockPeerConnectionObserver() {
  110. }
  111. void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
  112. pc_ = pc;
  113. state_ = pc_->signaling_state();
  114. }
  115. virtual void OnError() {}
  116. virtual void OnSignalingChange(
  117. PeerConnectionInterface::SignalingState new_state) {
  118. EXPECT_EQ(pc_->signaling_state(), new_state);
  119. state_ = new_state;
  120. }
  121. // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
  122. virtual void OnStateChange(StateType state_changed) {
  123. if (pc_.get() == NULL)
  124. return;
  125. switch (state_changed) {
  126. case kSignalingState:
  127. // OnSignalingChange and OnStateChange(kSignalingState) should always
  128. // be called approximately simultaneously. To ease testing, we require
  129. // that they always be called in that order. This check verifies
  130. // that OnSignalingChange has just been called.
  131. EXPECT_EQ(pc_->signaling_state(), state_);
  132. break;
  133. case kIceState:
  134. ADD_FAILURE();
  135. break;
  136. default:
  137. ADD_FAILURE();
  138. break;
  139. }
  140. }
  141. virtual void OnAddStream(MediaStreamInterface* stream) {
  142. last_added_stream_ = stream;
  143. }
  144. virtual void OnRemoveStream(MediaStreamInterface* stream) {
  145. last_removed_stream_ = stream;
  146. }
  147. virtual void OnRenegotiationNeeded() {
  148. renegotiation_needed_ = true;
  149. }
  150. virtual void OnDataChannel(DataChannelInterface* data_channel) {
  151. last_datachannel_ = data_channel;
  152. }
  153. virtual void OnIceConnectionChange(
  154. PeerConnectionInterface::IceConnectionState new_state) {
  155. EXPECT_EQ(pc_->ice_connection_state(), new_state);
  156. }
  157. virtual void OnIceGatheringChange(
  158. PeerConnectionInterface::IceGatheringState new_state) {
  159. EXPECT_EQ(pc_->ice_gathering_state(), new_state);
  160. }
  161. virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
  162. EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
  163. pc_->ice_gathering_state());
  164. std::string sdp;
  165. EXPECT_TRUE(candidate->ToString(&sdp));
  166. EXPECT_LT(0u, sdp.size());
  167. last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
  168. candidate->sdp_mline_index(), sdp, NULL));
  169. EXPECT_TRUE(last_candidate_.get() != NULL);
  170. }
  171. // TODO(bemasc): Remove this once callers transition to OnSignalingChange.
  172. virtual void OnIceComplete() {
  173. ice_complete_ = true;
  174. // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
  175. // be called approximately simultaneously. For ease of testing, this
  176. // check additionally requires that they be called in the above order.
  177. EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
  178. pc_->ice_gathering_state());
  179. }
  180. // Returns the label of the last added stream.
  181. // Empty string if no stream have been added.
  182. std::string GetLastAddedStreamLabel() {
  183. if (last_added_stream_.get())
  184. return last_added_stream_->label();
  185. return "";
  186. }
  187. std::string GetLastRemovedStreamLabel() {
  188. if (last_removed_stream_.get())
  189. return last_removed_stream_->label();
  190. return "";
  191. }
  192. scoped_refptr<PeerConnectionInterface> pc_;
  193. PeerConnectionInterface::SignalingState state_;
  194. scoped_ptr<IceCandidateInterface> last_candidate_;
  195. scoped_refptr<DataChannelInterface> last_datachannel_;
  196. bool renegotiation_needed_;
  197. bool ice_complete_;
  198. private:
  199. scoped_refptr<MediaStreamInterface> last_added_stream_;
  200. scoped_refptr<MediaStreamInterface> last_removed_stream_;
  201. };
  202. } // namespace
  203. class PeerConnectionInterfaceTest : public testing::Test {
  204. protected:
  205. virtual void SetUp() {
  206. pc_factory_ = webrtc::CreatePeerConnectionFactory(
  207. talk_base::Thread::Current(), talk_base::Thread::Current(), NULL);
  208. ASSERT_TRUE(pc_factory_.get() != NULL);
  209. }
  210. void CreatePeerConnection() {
  211. CreatePeerConnection("", "", NULL);
  212. }
  213. void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
  214. CreatePeerConnection("", "", constraints);
  215. }
  216. void CreatePeerConnection(const std::string& uri,
  217. const std::string& password,
  218. webrtc::MediaConstraintsInterface* constraints) {
  219. PeerConnectionInterface::IceServer server;
  220. PeerConnectionInterface::IceServers servers;
  221. server.uri = uri;
  222. server.password = password;
  223. servers.push_back(server);
  224. port_allocator_factory_ = FakePortAllocatorFactory::Create();
  225. pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
  226. port_allocator_factory_.get(),
  227. &observer_);
  228. ASSERT_TRUE(pc_.get() != NULL);
  229. observer_.SetPeerConnectionInterface(pc_.get());
  230. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  231. }
  232. void CreatePeerConnectionWithDifferentConfigurations() {
  233. CreatePeerConnection(kStunAddressOnly, "", NULL);
  234. EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
  235. EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
  236. EXPECT_EQ("address",
  237. port_allocator_factory_->stun_configs()[0].server.hostname());
  238. EXPECT_EQ(kDefaultStunPort,
  239. port_allocator_factory_->stun_configs()[0].server.port());
  240. CreatePeerConnection(kStunInvalidPort, "", NULL);
  241. EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
  242. EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
  243. CreatePeerConnection(kStunAddressPortAndMore1, "", NULL);
  244. EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
  245. EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
  246. CreatePeerConnection(kStunAddressPortAndMore2, "", NULL);
  247. EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
  248. EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
  249. CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
  250. EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
  251. EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
  252. EXPECT_EQ(kTurnUsername,
  253. port_allocator_factory_->turn_configs()[0].username);
  254. EXPECT_EQ(kTurnPassword,
  255. port_allocator_factory_->turn_configs()[0].password);
  256. EXPECT_EQ(kTurnHostname,
  257. port_allocator_factory_->turn_configs()[0].server.hostname());
  258. EXPECT_EQ(kTurnHostname,
  259. port_allocator_factory_->stun_configs()[0].server.hostname());
  260. }
  261. void AddStream(const std::string& label) {
  262. // Create a local stream.
  263. scoped_refptr<LocalMediaStreamInterface> stream(
  264. pc_factory_->CreateLocalMediaStream(label));
  265. scoped_refptr<VideoSourceInterface> video_source(
  266. pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
  267. scoped_refptr<VideoTrackInterface> video_track(
  268. pc_factory_->CreateVideoTrack(label + "v0", video_source));
  269. stream->AddTrack(video_track.get());
  270. EXPECT_TRUE(pc_->AddStream(stream, NULL));
  271. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  272. observer_.renegotiation_needed_ = false;
  273. }
  274. void AddVoiceStream(const std::string& label) {
  275. // Create a local stream.
  276. scoped_refptr<LocalMediaStreamInterface> stream(
  277. pc_factory_->CreateLocalMediaStream(label));
  278. scoped_refptr<AudioTrackInterface> audio_track(
  279. pc_factory_->CreateAudioTrack(label + "a0", NULL));
  280. stream->AddTrack(audio_track.get());
  281. EXPECT_TRUE(pc_->AddStream(stream, NULL));
  282. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  283. observer_.renegotiation_needed_ = false;
  284. }
  285. void AddAudioVideoStream(const std::string& stream_label,
  286. const std::string& audio_track_label,
  287. const std::string& video_track_label) {
  288. // Create a local stream.
  289. scoped_refptr<LocalMediaStreamInterface> stream(
  290. pc_factory_->CreateLocalMediaStream(stream_label));
  291. scoped_refptr<AudioTrackInterface> audio_track(
  292. pc_factory_->CreateAudioTrack(
  293. audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
  294. stream->AddTrack(audio_track.get());
  295. scoped_refptr<VideoTrackInterface> video_track(
  296. pc_factory_->CreateVideoTrack(video_track_label, NULL));
  297. stream->AddTrack(video_track.get());
  298. EXPECT_TRUE(pc_->AddStream(stream, NULL));
  299. EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
  300. observer_.renegotiation_needed_ = false;
  301. }
  302. bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
  303. talk_base::scoped_refptr<MockCreateSessionDescriptionObserver>
  304. observer(new talk_base::RefCountedObject<
  305. MockCreateSessionDescriptionObserver>());
  306. if (offer) {
  307. pc_->CreateOffer(observer, NULL);
  308. } else {
  309. pc_->CreateAnswer(observer, NULL);
  310. }
  311. EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
  312. *desc = observer->release_desc();
  313. return observer->result();
  314. }
  315. bool DoCreateOffer(SessionDescriptionInterface** desc) {
  316. return DoCreateOfferAnswer(desc, true);
  317. }
  318. bool DoCreateAnswer(SessionDescriptionInterface** desc) {
  319. return DoCreateOfferAnswer(desc, false);
  320. }
  321. bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
  322. talk_base::scoped_refptr<MockSetSessionDescriptionObserver>
  323. observer(new talk_base::RefCountedObject<
  324. MockSetSessionDescriptionObserver>());
  325. if (local) {
  326. pc_->SetLocalDescription(observer, desc);
  327. } else {
  328. pc_->SetRemoteDescription(observer, desc);
  329. }
  330. EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
  331. return observer->result();
  332. }
  333. bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
  334. return DoSetSessionDescription(desc, true);
  335. }
  336. bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
  337. return DoSetSessionDescription(desc, false);
  338. }
  339. // Calls PeerConnection::GetStats and check the return value.
  340. // It does not verify the values in the StatReports since a RTCP packet might
  341. // be required.
  342. bool DoGetStats(MediaStreamTrackInterface* track) {
  343. talk_base::scoped_refptr<MockStatsObserver> observer(
  344. new talk_base::RefCountedObject<MockStatsObserver>());
  345. if (!pc_->GetStats(observer, track))
  346. return false;
  347. EXPECT_TRUE_WAIT(observer->called(), kTimeout);
  348. return observer->called();
  349. }
  350. void InitiateCall() {
  351. CreatePeerConnection();
  352. // Create a local stream with audio&video tracks.
  353. AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
  354. CreateOfferReceiveAnswer();
  355. }
  356. void CreateOfferAsRemoteDescription() {
  357. talk_base::scoped_ptr<SessionDescriptionInterface> offer;
  358. EXPECT_TRUE(DoCreateOffer(offer.use()));
  359. std::string sdp;
  360. EXPECT_TRUE(offer->ToString(&sdp));
  361. SessionDescriptionInterface* remote_offer =
  362. webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
  363. sdp, NULL);
  364. EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
  365. EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
  366. }
  367. void CreateAnswerAsLocalDescription() {
  368. scoped_ptr<SessionDescriptionInterface> answer;
  369. EXPECT_TRUE(DoCreateAnswer(answer.use()));
  370. // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
  371. // audio codec change, even if the parameter has nothing to do with
  372. // receiving. Not all parameters are serialized to SDP.
  373. // Since CreatePrAnswerAsLocalDescription serialize/deserialize
  374. // the SessionDescription, it is necessary to do that here to in order to
  375. // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
  376. // https://code.google.com/p/webrtc/issues/detail?id=1356
  377. std::string sdp;
  378. EXPECT_TRUE(answer->ToString(&sdp));
  379. SessionDescriptionInterface* new_answer =
  380. webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
  381. sdp, NULL);
  382. EXPECT_TRUE(DoSetLocalDescription(new_answer));
  383. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  384. }
  385. void CreatePrAnswerAsLocalDescription() {
  386. scoped_ptr<SessionDescriptionInterface> answer;
  387. EXPECT_TRUE(DoCreateAnswer(answer.use()));
  388. std::string sdp;
  389. EXPECT_TRUE(answer->ToString(&sdp));
  390. SessionDescriptionInterface* pr_answer =
  391. webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
  392. sdp, NULL);
  393. EXPECT_TRUE(DoSetLocalDescription(pr_answer));
  394. EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
  395. }
  396. void CreateOfferReceiveAnswer() {
  397. CreateOfferAsLocalDescription();
  398. std::string sdp;
  399. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  400. CreateAnswerAsRemoteDescription(sdp);
  401. }
  402. void CreateOfferAsLocalDescription() {
  403. talk_base::scoped_ptr<SessionDescriptionInterface> offer;
  404. ASSERT_TRUE(DoCreateOffer(offer.use()));
  405. // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
  406. // audio codec change, even if the parameter has nothing to do with
  407. // receiving. Not all parameters are serialized to SDP.
  408. // Since CreatePrAnswerAsLocalDescription serialize/deserialize
  409. // the SessionDescription, it is necessary to do that here to in order to
  410. // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
  411. // https://code.google.com/p/webrtc/issues/detail?id=1356
  412. std::string sdp;
  413. EXPECT_TRUE(offer->ToString(&sdp));
  414. SessionDescriptionInterface* new_offer =
  415. webrtc::CreateSessionDescription(
  416. SessionDescriptionInterface::kOffer,
  417. sdp, NULL);
  418. EXPECT_TRUE(DoSetLocalDescription(new_offer));
  419. EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
  420. }
  421. void CreateAnswerAsRemoteDescription(const std::string& offer) {
  422. webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
  423. SessionDescriptionInterface::kAnswer);
  424. EXPECT_TRUE(answer->Initialize(offer, NULL));
  425. EXPECT_TRUE(DoSetRemoteDescription(answer));
  426. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  427. }
  428. void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& offer) {
  429. webrtc::JsepSessionDescription* pr_answer =
  430. new webrtc::JsepSessionDescription(
  431. SessionDescriptionInterface::kPrAnswer);
  432. EXPECT_TRUE(pr_answer->Initialize(offer, NULL));
  433. EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
  434. EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
  435. webrtc::JsepSessionDescription* answer =
  436. new webrtc::JsepSessionDescription(
  437. SessionDescriptionInterface::kAnswer);
  438. EXPECT_TRUE(answer->Initialize(offer, NULL));
  439. EXPECT_TRUE(DoSetRemoteDescription(answer));
  440. EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
  441. }
  442. // Help function used for waiting until a the last signaled remote stream has
  443. // the same label as |stream_label|. In a few of the tests in this file we
  444. // answer with the same session description as we offer and thus we can
  445. // check if OnAddStream have been called with the same stream as we offer to
  446. // send.
  447. void WaitAndVerifyOnAddStream(const std::string& stream_label) {
  448. EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
  449. }
  450. // Creates an offer and applies it as a local session description.
  451. // Creates an answer with the same SDP an the offer but removes all lines
  452. // that start with a:ssrc"
  453. void CreateOfferReceiveAnswerWithoutSsrc() {
  454. CreateOfferAsLocalDescription();
  455. std::string sdp;
  456. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  457. SetSsrcToZero(&sdp);
  458. CreateAnswerAsRemoteDescription(sdp);
  459. }
  460. scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
  461. scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
  462. scoped_refptr<PeerConnectionInterface> pc_;
  463. MockPeerConnectionObserver observer_;
  464. };
  465. TEST_F(PeerConnectionInterfaceTest,
  466. CreatePeerConnectionWithDifferentConfigurations) {
  467. CreatePeerConnectionWithDifferentConfigurations();
  468. }
  469. TEST_F(PeerConnectionInterfaceTest, AddStreams) {
  470. CreatePeerConnection();
  471. AddStream(kStreamLabel1);
  472. AddVoiceStream(kStreamLabel2);
  473. ASSERT_EQ(2u, pc_->local_streams()->count());
  474. // Fail to add another stream with audio since we already have an audio track.
  475. scoped_refptr<LocalMediaStreamInterface> stream(
  476. pc_factory_->CreateLocalMediaStream(kStreamLabel3));
  477. scoped_refptr<AudioTrackInterface> audio_track(
  478. pc_factory_->CreateAudioTrack(
  479. kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
  480. stream->AddTrack(audio_track.get());
  481. EXPECT_FALSE(pc_->AddStream(stream, NULL));
  482. // Remove the stream with the audio track.
  483. pc_->RemoveStream(pc_->local_streams()->at(1));
  484. // Test that we now can add the stream with the audio track.
  485. EXPECT_TRUE(pc_->AddStream(stream, NULL));
  486. }
  487. TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
  488. CreatePeerConnection();
  489. AddStream(kStreamLabel1);
  490. ASSERT_EQ(1u, pc_->local_streams()->count());
  491. pc_->RemoveStream(pc_->local_streams()->at(0));
  492. EXPECT_EQ(0u, pc_->local_streams()->count());
  493. }
  494. TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
  495. InitiateCall();
  496. WaitAndVerifyOnAddStream(kStreamLabel1);
  497. }
  498. TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
  499. CreatePeerConnection();
  500. AddStream(kStreamLabel1);
  501. CreateOfferAsLocalDescription();
  502. std::string offer;
  503. EXPECT_TRUE(pc_->local_description()->ToString(&offer));
  504. CreatePrAnswerAndAnswerAsRemoteDescription(offer);
  505. WaitAndVerifyOnAddStream(kStreamLabel1);
  506. }
  507. TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
  508. CreatePeerConnection();
  509. AddStream(kStreamLabel1);
  510. CreateOfferAsRemoteDescription();
  511. CreateAnswerAsLocalDescription();
  512. WaitAndVerifyOnAddStream(kStreamLabel1);
  513. }
  514. TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
  515. CreatePeerConnection();
  516. AddStream(kStreamLabel1);
  517. CreateOfferAsRemoteDescription();
  518. CreatePrAnswerAsLocalDescription();
  519. CreateAnswerAsLocalDescription();
  520. WaitAndVerifyOnAddStream(kStreamLabel1);
  521. }
  522. TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
  523. InitiateCall();
  524. ASSERT_EQ(1u, pc_->remote_streams()->count());
  525. pc_->RemoveStream(pc_->local_streams()->at(0));
  526. CreateOfferReceiveAnswer();
  527. EXPECT_EQ(0u, pc_->remote_streams()->count());
  528. AddStream(kStreamLabel1);
  529. CreateOfferReceiveAnswer();
  530. }
  531. // Tests that after negotiating an audio only call, the respondent can perform a
  532. // renegotiation that removes the audio stream.
  533. TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
  534. CreatePeerConnection();
  535. AddVoiceStream(kStreamLabel1);
  536. CreateOfferAsRemoteDescription();
  537. CreateAnswerAsLocalDescription();
  538. ASSERT_EQ(1u, pc_->remote_streams()->count());
  539. pc_->RemoveStream(pc_->local_streams()->at(0));
  540. CreateOfferReceiveAnswer();
  541. EXPECT_EQ(0u, pc_->remote_streams()->count());
  542. }
  543. // Test that candidates are generated and that we can parse our own candidates.
  544. TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
  545. CreatePeerConnection();
  546. EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
  547. // SetRemoteDescription takes ownership of offer.
  548. SessionDescriptionInterface* offer = NULL;
  549. AddStream(kStreamLabel1);
  550. EXPECT_TRUE(DoCreateOffer(&offer));
  551. EXPECT_TRUE(DoSetRemoteDescription(offer));
  552. // SetLocalDescription takes ownership of answer.
  553. SessionDescriptionInterface* answer = NULL;
  554. EXPECT_TRUE(DoCreateAnswer(&answer));
  555. EXPECT_TRUE(DoSetLocalDescription(answer));
  556. EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
  557. EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
  558. EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
  559. }
  560. // Test that the CreateOffer and CreatAnswer will fail if the track labels are
  561. // not unique.
  562. TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
  563. CreatePeerConnection();
  564. // Create a regular offer for the CreateAnswer test later.
  565. SessionDescriptionInterface* offer = NULL;
  566. EXPECT_TRUE(DoCreateOffer(&offer));
  567. EXPECT_TRUE(offer != NULL);
  568. delete offer;
  569. offer = NULL;
  570. // Create a local stream with audio&video tracks having same label.
  571. AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
  572. // Test CreateOffer
  573. EXPECT_FALSE(DoCreateOffer(&offer));
  574. // Test CreateAnswer
  575. SessionDescriptionInterface* answer = NULL;
  576. EXPECT_FALSE(DoCreateAnswer(&answer));
  577. }
  578. // Test that we will get different SSRCs for each tracks in the offer and answer
  579. // we created.
  580. TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
  581. CreatePeerConnection();
  582. // Create a local stream with audio&video tracks having different labels.
  583. AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
  584. // Test CreateOffer
  585. scoped_ptr<SessionDescriptionInterface> offer;
  586. EXPECT_TRUE(DoCreateOffer(offer.use()));
  587. int audio_ssrc = 0;
  588. int video_ssrc = 0;
  589. EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
  590. &audio_ssrc));
  591. EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
  592. &video_ssrc));
  593. EXPECT_NE(audio_ssrc, video_ssrc);
  594. // Test CreateAnswer
  595. EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
  596. scoped_ptr<SessionDescriptionInterface> answer;
  597. EXPECT_TRUE(DoCreateAnswer(answer.use()));
  598. audio_ssrc = 0;
  599. video_ssrc = 0;
  600. EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
  601. &audio_ssrc));
  602. EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
  603. &video_ssrc));
  604. EXPECT_NE(audio_ssrc, video_ssrc);
  605. }
  606. // Test that we can specify a certain track that we want statistics about.
  607. TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
  608. InitiateCall();
  609. ASSERT_LT(0u, pc_->remote_streams()->count());
  610. ASSERT_LT(0u, pc_->remote_streams()->at(0)->audio_tracks()->count());
  611. scoped_refptr<MediaStreamTrackInterface> remote_audio =
  612. pc_->remote_streams()->at(0)->audio_tracks()->at(0);
  613. EXPECT_TRUE(DoGetStats(remote_audio));
  614. // Remove the stream. Since we are sending to our selves the local
  615. // and the remote stream is the same.
  616. pc_->RemoveStream(pc_->local_streams()->at(0));
  617. // Do a re-negotiation.
  618. CreateOfferReceiveAnswer();
  619. ASSERT_EQ(0u, pc_->remote_streams()->count());
  620. // Test that we still can get statistics for the old track. Even if it is not
  621. // sent any longer.
  622. EXPECT_TRUE(DoGetStats(remote_audio));
  623. }
  624. // Test that we can get stats on a video track.
  625. TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
  626. InitiateCall();
  627. ASSERT_LT(0u, pc_->remote_streams()->count());
  628. ASSERT_LT(0u, pc_->remote_streams()->at(0)->video_tracks()->count());
  629. scoped_refptr<MediaStreamTrackInterface> remote_video =
  630. pc_->remote_streams()->at(0)->video_tracks()->at(0);
  631. EXPECT_TRUE(DoGetStats(remote_video));
  632. }
  633. // Test that we don't get statistics for an invalid track.
  634. TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
  635. InitiateCall();
  636. scoped_refptr<AudioTrackInterface> unknown_audio_track(
  637. pc_factory_->CreateAudioTrack("unknown track", NULL));
  638. EXPECT_FALSE(DoGetStats(unknown_audio_track));
  639. }
  640. // This test setup two RTP data channels in loop back.
  641. #ifdef WIN32
  642. // TODO(perkj): Investigate why the transport channel sometimes don't become
  643. // writable on Windows when we try to connect in loop back.
  644. TEST_F(PeerConnectionInterfaceTest, DISABLED_TestDataChannel) {
  645. #else
  646. TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
  647. #endif
  648. FakeConstraints constraints;
  649. constraints.SetAllowRtpDataChannels();
  650. CreatePeerConnection(&constraints);
  651. scoped_refptr<DataChannelInterface> data1 =
  652. pc_->CreateDataChannel("test1", NULL);
  653. scoped_refptr<DataChannelInterface> data2 =
  654. pc_->CreateDataChannel("test2", NULL);
  655. ASSERT_TRUE(data1 != NULL);
  656. talk_base::scoped_ptr<MockDataChannelObserver> observer1(
  657. new MockDataChannelObserver(data1));
  658. talk_base::scoped_ptr<MockDataChannelObserver> observer2(
  659. new MockDataChannelObserver(data2));
  660. EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
  661. EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
  662. std::string data_to_send1 = "testing testing";
  663. std::string data_to_send2 = "testing something else";
  664. EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
  665. CreateOfferReceiveAnswer();
  666. EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
  667. EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
  668. EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
  669. EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
  670. EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
  671. EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
  672. EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
  673. EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
  674. data1->Close();
  675. EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
  676. CreateOfferReceiveAnswer();
  677. EXPECT_FALSE(observer1->IsOpen());
  678. EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
  679. EXPECT_TRUE(observer2->IsOpen());
  680. data_to_send2 = "testing something else again";
  681. EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
  682. EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
  683. }
  684. // This test setup a RTP data channels in loop back and test that a channel is
  685. // opened even if the remote end answer with a zero SSRC.
  686. #ifdef WIN32
  687. // TODO(perkj): Investigate why the transport channel sometimes don't become
  688. // writable on Windows when we try to connect in loop back.
  689. TEST_F(PeerConnectionInterfaceTest, DISABLED_TestSendOnlyDataChannel) {
  690. #else
  691. TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
  692. #endif
  693. FakeConstraints constraints;
  694. constraints.SetAllowRtpDataChannels();
  695. CreatePeerConnection(&constraints);
  696. scoped_refptr<DataChannelInterface> data1 =
  697. pc_->CreateDataChannel("test1", NULL);
  698. talk_base::scoped_ptr<MockDataChannelObserver> observer1(
  699. new MockDataChannelObserver(data1));
  700. CreateOfferReceiveAnswerWithoutSsrc();
  701. EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
  702. data1->Close();
  703. EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
  704. CreateOfferReceiveAnswerWithoutSsrc();
  705. EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
  706. EXPECT_FALSE(observer1->IsOpen());
  707. }
  708. // This test that if a data channel is added in an answer a receive only channel
  709. // channel is created.
  710. TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
  711. FakeConstraints constraints;
  712. constraints.SetAllowRtpDataChannels();
  713. CreatePeerConnection(&constraints);
  714. std::string offer_label = "offer_channel";
  715. scoped_refptr<DataChannelInterface> offer_channel =
  716. pc_->CreateDataChannel(offer_label, NULL);
  717. CreateOfferAsLocalDescription();
  718. // Replace the data channel label in the offer and apply it as an answer.
  719. std::string receive_label = "answer_channel";
  720. std::string sdp;
  721. EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
  722. talk_base::replace_substrs(offer_label.c_str(), offer_label.length(),
  723. receive_label.c_str(), receive_label.length(),
  724. &sdp);
  725. CreateAnswerAsRemoteDescription(sdp);
  726. // Verify that a new incoming data channel has been created and that
  727. // it is open but can't we written to.
  728. ASSERT_TRUE(observer_.last_datachannel_ != NULL);
  729. DataChannelInterface* received_channel = observer_.last_datachannel_;
  730. EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
  731. EXPECT_EQ(receive_label, received_channel->label());
  732. EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
  733. // Verify that the channel we initially offered has been rejected.
  734. EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
  735. // Do another offer / answer exchange and verify that the data channel is
  736. // opened.
  737. CreateOfferReceiveAnswer();
  738. EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
  739. kTimeout);
  740. }
  741. // This test that no data channel is returned if a reliable channel is
  742. // requested.
  743. // TODO(perkj): Remove this test once reliable channels are implemented.
  744. TEST_F(PeerConnectionInterfaceTest, CreateReliableDataChannel) {
  745. FakeConstraints constraints;
  746. constraints.SetAllowRtpDataChannels();
  747. CreatePeerConnection(&constraints);
  748. std::string label = "test";
  749. webrtc::DataChannelInit config;
  750. config.reliable = true;
  751. scoped_refptr<DataChannelInterface> channel =
  752. pc_->CreateDataChannel(label, &config);
  753. EXPECT_TRUE(channel == NULL);
  754. }