OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "content/renderer/media/mock_media_stream_dependency_factory.h" | |
11 #include "content/renderer/media/mock_media_stream_impl.h" | |
12 #include "content/renderer/media/mock_web_peer_connection_00_handler_client.h" | |
13 #include "content/renderer/media/mock_peer_connection_impl.h" | |
14 #include "content/renderer/media/peer_connection_handler_jsep.h" | |
15 #include "content/renderer/media/rtc_video_decoder.h" | |
16 #include "jingle/glue/thread_wrapper.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "third_party/libjingle/source/talk/app/webrtc/peerconnection.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebICECandid ateDescriptor.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebICEOption s.h" | |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaHint s.h" | |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amDescriptor.h" | |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amSource.h" | |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSessionDe scriptionDescriptor.h" | |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
26 | |
27 namespace webrtc { | |
28 | |
29 class MockVideoRendererWrapper : public VideoRendererWrapperInterface { | |
30 public: | |
31 virtual cricket::VideoRenderer* renderer() OVERRIDE { return NULL; } | |
32 | |
33 protected: | |
34 virtual ~MockVideoRendererWrapper() {} | |
35 }; | |
36 | |
37 } // namespace webrtc | |
38 | |
39 TEST(PeerConnectionHandlerJsepTest, Basic) { | |
40 MessageLoop loop; | |
41 | |
42 scoped_ptr<WebKit::MockWebPeerConnection00HandlerClient> mock_client( | |
43 new WebKit::MockWebPeerConnection00HandlerClient()); | |
44 scoped_refptr<MockMediaStreamImpl> mock_ms_impl(new MockMediaStreamImpl()); | |
45 scoped_ptr<MockMediaStreamDependencyFactory> mock_dependency_factory( | |
46 new MockMediaStreamDependencyFactory()); | |
47 mock_dependency_factory->CreatePeerConnectionFactory(NULL, | |
48 NULL, | |
49 NULL, | |
50 NULL, | |
51 NULL); | |
52 scoped_ptr<PeerConnectionHandlerJsep> pc_handler( | |
53 new PeerConnectionHandlerJsep(mock_client.get(), | |
54 mock_ms_impl.get(), | |
55 mock_dependency_factory.get())); | |
56 | |
57 WebKit::WebString server_config( | |
58 WebKit::WebString::fromUTF8("STUN stun.l.google.com:19302")); | |
59 WebKit::WebString username; | |
60 pc_handler->initialize(server_config, username); | |
61 EXPECT_TRUE(pc_handler->native_peer_connection_.get()); | |
62 webrtc::MockPeerConnectionImpl* mock_peer_connection = | |
63 static_cast<webrtc::MockPeerConnectionImpl*>( | |
64 pc_handler->native_peer_connection_.get()); | |
65 | |
66 // Create offer. | |
67 WebKit::WebMediaHints hints; | |
68 hints.initialize(true, true); | |
69 WebKit::WebSessionDescriptionDescriptor offer = | |
70 pc_handler->createOffer(hints); | |
71 EXPECT_FALSE(offer.isNull()); | |
72 EXPECT_EQ(mock_peer_connection->kDummyOffer, UTF16ToUTF8(offer.initialSDP())); | |
73 EXPECT_EQ(hints.audio(), mock_peer_connection->hint_audio()); | |
74 EXPECT_EQ(hints.video(), mock_peer_connection->hint_video()); | |
75 | |
76 // Create answer. | |
77 WebKit::WebString offer_string = "offer"; | |
78 hints.reset(); | |
79 hints.initialize(false, false); | |
80 WebKit::WebSessionDescriptionDescriptor answer = | |
81 pc_handler->createAnswer(offer_string, hints); | |
82 EXPECT_FALSE(answer.isNull()); | |
83 EXPECT_EQ(UTF16ToUTF8(offer_string), UTF16ToUTF8(answer.initialSDP())); | |
84 EXPECT_EQ(UTF16ToUTF8(offer_string), mock_peer_connection->description_sdp()); | |
85 EXPECT_EQ(hints.audio(), mock_peer_connection->hint_audio()); | |
86 EXPECT_EQ(hints.video(), mock_peer_connection->hint_video()); | |
87 | |
88 // Set local description. | |
89 PeerConnectionHandlerJsep::Action action = | |
90 PeerConnectionHandlerJsep::ActionSDPOffer; | |
91 WebKit::WebSessionDescriptionDescriptor description; | |
92 WebKit::WebString sdp = "test sdp"; | |
93 description.initialize(sdp); | |
94 EXPECT_TRUE(pc_handler->setLocalDescription(action, description)); | |
95 EXPECT_EQ(webrtc::PeerConnectionInterface::kOffer, | |
96 mock_peer_connection->action()); | |
97 EXPECT_EQ(UTF16ToUTF8(sdp), mock_peer_connection->description_sdp()); | |
98 | |
99 // Get local description. | |
100 description.reset(); | |
101 description = pc_handler->localDescription(); | |
102 EXPECT_FALSE(description.isNull()); | |
103 EXPECT_EQ(UTF16ToUTF8(sdp), UTF16ToUTF8(description.initialSDP())); | |
104 | |
105 // Set remote description. | |
106 action = PeerConnectionHandlerJsep::ActionSDPAnswer; | |
107 sdp = "test sdp 2"; | |
108 description.reset(); | |
109 description.initialize(sdp); | |
110 EXPECT_TRUE(pc_handler->setRemoteDescription(action, description)); | |
111 EXPECT_EQ(webrtc::PeerConnectionInterface::kAnswer, | |
112 mock_peer_connection->action()); | |
113 EXPECT_EQ(UTF16ToUTF8(sdp), mock_peer_connection->description_sdp()); | |
114 | |
115 // Get remote description. | |
116 description.reset(); | |
117 description = pc_handler->remoteDescription(); | |
118 EXPECT_FALSE(description.isNull()); | |
119 EXPECT_EQ(UTF16ToUTF8(sdp), UTF16ToUTF8(description.initialSDP())); | |
120 | |
121 // Start ICE. | |
122 WebKit::WebICEOptions options; | |
123 options.initialize(WebKit::WebICEOptions::CandidateTypeAll); | |
124 EXPECT_TRUE(pc_handler->startIce(options)); | |
125 EXPECT_EQ(webrtc::PeerConnectionInterface::kUseAll, | |
126 mock_peer_connection->ice_options()); | |
127 | |
128 // Process ICE message. | |
129 WebKit::WebICECandidateDescriptor candidate; | |
130 WebKit::WebString label = "test label"; | |
131 sdp = "test sdp"; | |
132 candidate.initialize(label, sdp); | |
133 EXPECT_TRUE(pc_handler->processIceMessage(candidate)); | |
134 EXPECT_EQ(UTF16ToUTF8(label), mock_peer_connection->ice_label()); | |
135 EXPECT_EQ(UTF16ToUTF8(sdp), mock_peer_connection->ice_sdp()); | |
136 | |
137 // Add stream. | |
138 // TODO(grunell): Add an audio track as well. | |
139 std::string stream_label("stream-label"); | |
140 std::string video_track_label("video-label"); | |
141 talk_base::scoped_refptr<webrtc::LocalVideoTrackInterface> local_video_track( | |
142 mock_dependency_factory->CreateLocalVideoTrack(video_track_label, NULL)); | |
143 mock_ms_impl->AddTrack(video_track_label, local_video_track); | |
144 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( | |
145 static_cast<size_t>(1)); | |
tommi (sloooow) - chröme
2012/03/26 12:34:45
1u isn't enough?
Henrik Grunell
2012/03/27 07:22:05
No. We have actually discussed this in a previous
| |
146 source_vector[0].initialize(WebKit::WebString::fromUTF8(video_track_label), | |
147 WebKit::WebMediaStreamSource::TypeVideo, | |
148 WebKit::WebString::fromUTF8("RemoteVideo")); | |
149 WebKit::WebMediaStreamDescriptor local_stream; | |
150 local_stream.initialize(UTF8ToUTF16(stream_label), source_vector); | |
151 pc_handler->addStream(local_stream); | |
152 EXPECT_EQ(stream_label, mock_peer_connection->stream_label()); | |
153 EXPECT_TRUE(mock_peer_connection->stream_changes_committed()); | |
154 | |
155 // On add stream. | |
156 std::string remote_stream_label(stream_label); | |
157 remote_stream_label += "-remote"; | |
158 std::string remote_video_track_label(video_track_label); | |
159 remote_video_track_label += "-remote"; | |
160 // We use a local stream as a remote since for testing purposes we really | |
161 // only need the MediaStreamInterface. | |
162 talk_base::scoped_refptr<webrtc::LocalMediaStreamInterface> remote_stream( | |
163 mock_dependency_factory->CreateLocalMediaStream(remote_stream_label)); | |
164 talk_base::scoped_refptr<webrtc::LocalVideoTrackInterface> remote_video_track( | |
165 mock_dependency_factory->CreateLocalVideoTrack(remote_video_track_label, | |
166 NULL)); | |
167 remote_video_track->set_enabled(true); | |
168 remote_stream->AddTrack(remote_video_track); | |
169 mock_peer_connection->AddRemoteStream(remote_stream); | |
170 pc_handler->OnAddStream(remote_stream); | |
171 EXPECT_EQ(remote_stream_label, mock_client->stream_label()); | |
172 | |
173 // Set renderer. | |
174 talk_base::scoped_refptr<webrtc::MockVideoRendererWrapper> renderer( | |
175 new talk_base::RefCountedObject<webrtc::MockVideoRendererWrapper>()); | |
176 pc_handler->SetVideoRenderer(remote_stream_label, renderer); | |
177 EXPECT_EQ(renderer, static_cast<webrtc::MockLocalVideoTrack*>( | |
178 remote_video_track.get())->renderer()); | |
179 | |
180 // Remove stream. | |
181 WebKit::WebVector<WebKit::WebMediaStreamDescriptor> empty_streams( | |
182 static_cast<size_t>(0)); | |
tommi (sloooow) - chröme
2012/03/26 12:34:45
0u?
Henrik Grunell
2012/03/27 07:22:05
Same here.
| |
183 pc_handler->removeStream(local_stream); | |
184 EXPECT_EQ("", mock_peer_connection->stream_label()); | |
185 mock_peer_connection->ClearStreamChangesCommitted(); | |
186 EXPECT_TRUE(!mock_peer_connection->stream_changes_committed()); | |
187 | |
188 // On remove stream. | |
189 pc_handler->OnRemoveStream(remote_stream); | |
190 EXPECT_TRUE(mock_client->stream_label().empty()); | |
191 | |
192 // Add stream again. | |
193 pc_handler->addStream(local_stream); | |
194 EXPECT_EQ(stream_label, mock_peer_connection->stream_label()); | |
195 EXPECT_TRUE(mock_peer_connection->stream_changes_committed()); | |
196 | |
197 // On state change. | |
198 mock_peer_connection->SetReadyState(webrtc::PeerConnectionInterface::kActive); | |
199 webrtc::PeerConnectionObserver::StateType state = | |
200 webrtc::PeerConnectionObserver::kReadyState; | |
201 pc_handler->OnStateChange(state); | |
202 EXPECT_EQ(WebKit::WebPeerConnection00HandlerClient::ReadyStateActive, | |
203 mock_client->ready_state()); | |
204 | |
205 // On ICE candidate. | |
206 std::string candidate_label = "test label"; | |
207 std::string candidate_sdp = "test sdp"; | |
208 webrtc::IceCandidateInterface* native_candidate = | |
209 mock_dependency_factory->CreateIceCandidate(candidate_label, | |
210 candidate_sdp); | |
211 pc_handler->OnIceCandidate(native_candidate); | |
212 EXPECT_EQ(candidate_label, mock_client->candidate_label()); | |
213 EXPECT_EQ(candidate_sdp, mock_client->candidate_sdp()); | |
214 EXPECT_TRUE(mock_client->more_to_follow()); | |
215 | |
216 // On ICE complete. | |
217 pc_handler->OnIceComplete(); | |
218 EXPECT_TRUE(mock_client->candidate_label().empty()); | |
219 EXPECT_TRUE(mock_client->candidate_sdp().empty()); | |
220 EXPECT_FALSE(mock_client->more_to_follow()); | |
221 | |
222 // Stop. | |
223 pc_handler->stop(); | |
224 EXPECT_FALSE(pc_handler->native_peer_connection_.get()); | |
225 | |
226 // PC handler is expected to be deleted when stop calls | |
227 // MediaStreamImpl::ClosePeerConnection. We own and delete it here instead of | |
228 // in the mock. | |
229 pc_handler.reset(); | |
230 } | |
OLD | NEW |