Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: content/renderer/media/media_stream_impl_unittest.cc

Issue 10383151: Refactor MediaStreamImpl and PeerConnection glue implementation after WebKit changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and added NON_EXPORTED_BASE to the base of MediaStreamExtradata. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "content/renderer/media/media_stream_impl.h" 8 #include "content/renderer/media/media_stream_impl.h"
9 #include "content/renderer/media/mock_media_stream_dependency_factory.h" 9 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
10 #include "content/renderer/media/mock_media_stream_dispatcher.h" 10 #include "content/renderer/media/mock_media_stream_dispatcher.h"
11 #include "content/renderer/media/mock_web_peer_connection_00_handler_client.h" 11 #include "content/renderer/media/mock_web_peer_connection_00_handler_client.h"
12 #include "content/renderer/media/mock_web_peer_connection_handler_client.h" 12 #include "content/renderer/media/mock_web_peer_connection_handler_client.h"
13 #include "content/renderer/media/peer_connection_handler.h" 13 #include "content/renderer/media/peer_connection_handler.h"
14 #include "content/renderer/media/peer_connection_handler_jsep.h" 14 #include "content/renderer/media/peer_connection_handler_jsep.h"
15 #include "content/renderer/media/video_capture_impl_manager.h" 15 #include "content/renderer/media/video_capture_impl_manager.h"
16 #include "content/renderer/p2p/socket_dispatcher.h" 16 #include "content/renderer/p2p/socket_dispatcher.h"
17 #include "media/base/message_loop_factory.h" 17 #include "media/base/message_loop_factory.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amSource.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amSource.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne ction00Handler.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne ction00Handler.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne ctionHandler.h" 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebPeerConne ctionHandler.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" 22 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
23 23
24 // TODO(perkj): Currently a media stream is identified by its sources. 24 class MediaStreamImplUnderTest : public MediaStreamImpl {
25 // This is currently being changed in WebKit. 25 public:
26 // Remove the creation of WebKitSourceVectors when that has landed. 26 MediaStreamImplUnderTest(MediaStreamDispatcher* media_stream_dispatcher,
27 static std::string CreateTrackLabel( 27 content::P2PSocketDispatcher* p2p_socket_dispatcher,
28 const std::string& manager_label, 28 VideoCaptureImplManager* vc_manager,
29 int session_id, 29 MediaStreamDependencyFactory* dependency_factory)
30 bool is_video) { 30 : MediaStreamImpl(NULL, media_stream_dispatcher, p2p_socket_dispatcher,
31 std::string track_label = manager_label; 31 vc_manager, dependency_factory) {
32 if (is_video) {
33 track_label += "#video-";
34 } else {
35 track_label += "#audio-";
36 } 32 }
37 track_label += session_id;
38 return track_label;
39 }
40 33
41 // TODO(perkj): Currently a media stream is identified by its sources. 34 virtual void CompleteGetUserMediaRequest(
42 // This is currently being changed in WebKit. 35 const WebKit::WebMediaStreamDescriptor& stream,
43 // Remove the creation of WebKitSourceVectors when that has landed. 36 WebKit::WebUserMediaRequest* request) {
44 static void CreateWebKitSourceVector( 37 last_generated_stream_ = stream;
45 const std::string& label, 38 }
46 const media_stream::StreamDeviceInfoArray& devices,
47 WebKit::WebMediaStreamSource::Type type,
48 WebKit::WebVector<WebKit::WebMediaStreamSource>& webkit_sources) {
49 ASSERT(devices.size() == webkit_sources.size());
50 39
51 for (size_t i = 0; i < devices.size(); ++i) { 40 virtual WebKit::WebMediaStreamDescriptor GetMediaStream(const GURL& url) {
52 std::string track_label = CreateTrackLabel( 41 return last_generated_stream_;
53 label, devices[i].session_id,
54 type == WebKit::WebMediaStreamSource::TypeVideo);
55 webkit_sources[i].initialize(
56 UTF8ToUTF16(track_label),
57 type,
58 UTF8ToUTF16(devices[i].name));
59 } 42 }
60 } 43
44 const WebKit::WebMediaStreamDescriptor& last_generated_stream() {
45 return last_generated_stream_;
46 }
47
48 private:
49 WebKit::WebMediaStreamDescriptor last_generated_stream_;
50 };
61 51
62 class MediaStreamImplTest : public ::testing::Test { 52 class MediaStreamImplTest : public ::testing::Test {
63 public: 53 public:
64 void SetUp() { 54 void SetUp() {
65 // Create our test object. 55 // Create our test object.
66 ms_dispatcher_.reset(new MockMediaStreamDispatcher()); 56 ms_dispatcher_.reset(new MockMediaStreamDispatcher());
67 p2p_socket_dispatcher_.reset(new content::P2PSocketDispatcher(NULL)); 57 p2p_socket_dispatcher_.reset(new content::P2PSocketDispatcher(NULL));
68 scoped_refptr<VideoCaptureImplManager> vc_manager( 58 scoped_refptr<VideoCaptureImplManager> vc_manager(
69 new VideoCaptureImplManager()); 59 new VideoCaptureImplManager());
70 MockMediaStreamDependencyFactory* dependency_factory = 60 MockMediaStreamDependencyFactory* dependency_factory =
71 new MockMediaStreamDependencyFactory(vc_manager); 61 new MockMediaStreamDependencyFactory(vc_manager);
72 ms_impl_.reset(new MediaStreamImpl(NULL, 62 ms_impl_.reset(new MediaStreamImplUnderTest(ms_dispatcher_.get(),
73 ms_dispatcher_.get(), 63 p2p_socket_dispatcher_.get(),
74 p2p_socket_dispatcher_.get(), 64 vc_manager.get(),
75 vc_manager.get(), 65 dependency_factory));
76 dependency_factory));
77 } 66 }
78 67
79 void TearDown() { 68 void TearDown() {
80 // Make sure the message created by 69 // Make sure the message created by
81 // P2PSocketDispatcher::AsyncMessageSender::Send is handled before 70 // P2PSocketDispatcher::AsyncMessageSender::Send is handled before
82 // tear down to avoid a memory leak. 71 // tear down to avoid a memory leak.
83 loop_.RunAllPending(); 72 loop_.RunAllPending();
84 } 73 }
85 74
86 WebKit::WebMediaStreamDescriptor RequestLocalMediaStream(bool audio, 75 WebKit::WebMediaStreamDescriptor RequestLocalMediaStream(bool audio,
87 bool video) { 76 bool video) {
88 WebKit::WebUserMediaRequest user_media_request; 77 WebKit::WebUserMediaRequest user_media_request;
89 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_sources( 78 WebKit::WebVector<WebKit::WebMediaStreamSource> audio_sources(
90 audio ? static_cast<size_t>(1) : 0); 79 audio ? static_cast<size_t>(1) : 0);
91 WebKit::WebVector<WebKit::WebMediaStreamSource> video_sources( 80 WebKit::WebVector<WebKit::WebMediaStreamSource> video_sources(
92 video ? static_cast<size_t>(1) : 0); 81 video ? static_cast<size_t>(1) : 0);
93 ms_impl_->requestUserMedia(user_media_request, audio_sources, 82 ms_impl_->requestUserMedia(user_media_request, audio_sources,
94 video_sources); 83 video_sources);
95 84
96 ms_impl_->OnStreamGenerated(ms_dispatcher_->request_id(), 85 ms_impl_->OnStreamGenerated(ms_dispatcher_->request_id(),
97 ms_dispatcher_->stream_label(), 86 ms_dispatcher_->stream_label(),
98 ms_dispatcher_->audio_array(), 87 ms_dispatcher_->audio_array(),
99 ms_dispatcher_->video_array()); 88 ms_dispatcher_->video_array());
100 89
101 // TODO(perkj): Currently a media stream is identified by its sources. 90 WebKit::WebMediaStreamDescriptor desc = ms_impl_->last_generated_stream();
102 // This is currently beeing changed in WebKit. 91 MediaStreamExtraData* extra_data = static_cast<MediaStreamExtraData*>(
103 // Remove the creation of WebKitSourceVectors when that has landed. 92 desc.extraData());
104 if (audio) { 93 if (!extra_data || !extra_data->local_stream()) {
105 CreateWebKitSourceVector(ms_dispatcher_->stream_label(), 94 ADD_FAILURE();
106 ms_dispatcher_->audio_array(), 95 return desc;
107 WebKit::WebMediaStreamSource::TypeAudio,
108 audio_sources);
109 } 96 }
110 if (video) { 97
111 CreateWebKitSourceVector(ms_dispatcher_->stream_label(), 98 if (audio)
112 ms_dispatcher_->video_array(), 99 EXPECT_EQ(1u, extra_data->local_stream()->audio_tracks()->count());
113 WebKit::WebMediaStreamSource::TypeVideo, 100 if (video)
114 video_sources); 101 EXPECT_EQ(1u, extra_data->local_stream()->video_tracks()->count());
115 }
116 WebKit::WebMediaStreamDescriptor desc;
117 desc.initialize(UTF8ToUTF16(ms_dispatcher_->stream_label()),
118 audio_sources, video_sources);
119 return desc; 102 return desc;
120 } 103 }
121 104
122 protected: 105 protected:
123 MessageLoop loop_; 106 MessageLoop loop_;
124 scoped_ptr<MockMediaStreamDispatcher> ms_dispatcher_; 107 scoped_ptr<MockMediaStreamDispatcher> ms_dispatcher_;
125 scoped_ptr<content::P2PSocketDispatcher> p2p_socket_dispatcher_; 108 scoped_ptr<content::P2PSocketDispatcher> p2p_socket_dispatcher_;
126 scoped_ptr<MediaStreamImpl> ms_impl_; 109 scoped_ptr<MediaStreamImplUnderTest> ms_impl_;
127 }; 110 };
128 111
129 TEST_F(MediaStreamImplTest, Basic) { 112 TEST_F(MediaStreamImplTest, CreatePeerConnection) {
113 // Create ROAP PeerConnection.j
130 WebKit::MockWebPeerConnectionHandlerClient client; 114 WebKit::MockWebPeerConnectionHandlerClient client;
131 WebKit::WebPeerConnectionHandler* pc_handler = 115 scoped_ptr<WebKit::WebPeerConnectionHandler> pc_handler(
132 ms_impl_->CreatePeerConnectionHandler(&client); 116 ms_impl_->CreatePeerConnectionHandler(&client));
133 EXPECT_EQ(1u, ms_impl_->peer_connection_handlers_.size()); 117 pc_handler.reset();
134 118
135 // Delete PC handler explicitly after closing to mimic WebKit behavior. 119 // Create JSEP PeerConnection.
136 ms_impl_->ClosePeerConnection(
137 static_cast<PeerConnectionHandler*>(pc_handler));
138 EXPECT_TRUE(ms_impl_->peer_connection_handlers_.empty());
139 delete pc_handler;
140
141 WebKit::MockWebPeerConnection00HandlerClient client_jsep; 120 WebKit::MockWebPeerConnection00HandlerClient client_jsep;
142 WebKit::WebPeerConnection00Handler* pc_handler_jsep = 121 scoped_ptr<WebKit::WebPeerConnection00Handler> pc_handler_jsep(
143 ms_impl_->CreatePeerConnectionHandlerJsep(&client_jsep); 122 ms_impl_->CreatePeerConnectionHandlerJsep(&client_jsep));
144 EXPECT_EQ(1u, ms_impl_->peer_connection_handlers_.size()); 123 pc_handler_jsep.reset();
145
146 // Delete PC handler explicitly after closing to mimic WebKit behavior.
147 ms_impl_->ClosePeerConnection(
148 static_cast<PeerConnectionHandlerJsep*>(pc_handler_jsep));
149 EXPECT_TRUE(ms_impl_->peer_connection_handlers_.empty());
150 delete pc_handler_jsep;
151 }
152
153 TEST_F(MediaStreamImplTest, MultiplePeerConnections) {
154 WebKit::MockWebPeerConnectionHandlerClient client;
155 WebKit::WebPeerConnectionHandler* pc_handler =
156 ms_impl_->CreatePeerConnectionHandler(&client);
157 EXPECT_EQ(1u, ms_impl_->peer_connection_handlers_.size());
158
159 WebKit::MockWebPeerConnection00HandlerClient client_jsep;
160 WebKit::WebPeerConnection00Handler* pc_handler_jsep =
161 ms_impl_->CreatePeerConnectionHandlerJsep(&client_jsep);
162 EXPECT_EQ(2u, ms_impl_->peer_connection_handlers_.size());
163
164 // Delete PC handler explicitly after closing to mimic WebKit behavior.
165 ms_impl_->ClosePeerConnection(
166 static_cast<PeerConnectionHandler*>(pc_handler));
167 EXPECT_EQ(1u, ms_impl_->peer_connection_handlers_.size());
168 delete pc_handler;
169
170 // Delete PC handler explicitly after closing to mimic WebKit behavior.
171 ms_impl_->ClosePeerConnection(
172 static_cast<PeerConnectionHandlerJsep*>(pc_handler_jsep));
173 EXPECT_TRUE(ms_impl_->peer_connection_handlers_.empty());
174 delete pc_handler_jsep;
175 } 124 }
176 125
177 TEST_F(MediaStreamImplTest, LocalMediaStream) { 126 TEST_F(MediaStreamImplTest, LocalMediaStream) {
178 // Test a stream with both audio and video. 127 // Test a stream with both audio and video.
179 WebKit::WebMediaStreamDescriptor mixed_desc = RequestLocalMediaStream(true, 128 WebKit::WebMediaStreamDescriptor mixed_desc = RequestLocalMediaStream(true,
180 true); 129 true);
181 webrtc::LocalMediaStreamInterface* mixed_stream = 130 // Create a renderer for the stream.
182 ms_impl_->GetLocalMediaStream(mixed_desc);
183 ASSERT_TRUE(mixed_stream != NULL);
184 EXPECT_EQ(1u, mixed_stream->audio_tracks()->count());
185 EXPECT_EQ(1u, mixed_stream->video_tracks()->count());
186
187 // Create a renderer for the stream
188 scoped_ptr<media::MessageLoopFactory> message_loop_factory( 131 scoped_ptr<media::MessageLoopFactory> message_loop_factory(
189 new media::MessageLoopFactory()); 132 new media::MessageLoopFactory());
190 scoped_refptr<media::VideoDecoder> mixed_decoder( 133 scoped_refptr<media::VideoDecoder> mixed_decoder(
191 ms_impl_->CreateLocalVideoDecoder(mixed_stream, 134 ms_impl_->GetVideoDecoder(GURL(), message_loop_factory.get()));
192 message_loop_factory.get()));
193 EXPECT_TRUE(mixed_decoder.get() != NULL); 135 EXPECT_TRUE(mixed_decoder.get() != NULL);
194 136
195 // Test a stream with audio only. 137 // Test a stream with audio only.
196 WebKit::WebMediaStreamDescriptor audio_desc = RequestLocalMediaStream(true, 138 WebKit::WebMediaStreamDescriptor audio_desc = RequestLocalMediaStream(true,
197 false); 139 false);
198 webrtc::LocalMediaStreamInterface* audio_stream =
199 ms_impl_->GetLocalMediaStream(audio_desc);
200 ASSERT_TRUE(audio_stream != NULL);
201 EXPECT_EQ(1u, audio_stream->audio_tracks()->count());
202 EXPECT_EQ(0u, audio_stream->video_tracks()->count());
203
204 scoped_refptr<media::VideoDecoder> audio_decoder( 140 scoped_refptr<media::VideoDecoder> audio_decoder(
205 ms_impl_->CreateLocalVideoDecoder(audio_stream, 141 ms_impl_->GetVideoDecoder(GURL(), message_loop_factory.get()));
206 message_loop_factory.get()));
207 EXPECT_TRUE(audio_decoder.get() == NULL); 142 EXPECT_TRUE(audio_decoder.get() == NULL);
208 143
209 // Test a stream with video only. 144 // Test a stream with video only.
210 WebKit::WebMediaStreamDescriptor video_desc = RequestLocalMediaStream(false, 145 WebKit::WebMediaStreamDescriptor video_desc = RequestLocalMediaStream(false,
211 true); 146 true);
212 webrtc::LocalMediaStreamInterface* video_stream_ =
213 ms_impl_->GetLocalMediaStream(video_desc);
214 ASSERT_TRUE(video_stream_ != NULL);
215 EXPECT_EQ(0u, video_stream_->audio_tracks()->count());
216 EXPECT_EQ(1u, video_stream_->video_tracks()->count());
217
218 scoped_refptr<media::VideoDecoder> video_decoder( 147 scoped_refptr<media::VideoDecoder> video_decoder(
219 ms_impl_->CreateLocalVideoDecoder(video_stream_, 148 ms_impl_->GetVideoDecoder(GURL(), message_loop_factory.get()));
220 message_loop_factory.get()));
221 EXPECT_TRUE(video_decoder.get() != NULL); 149 EXPECT_TRUE(video_decoder.get() != NULL);
222 150
223 // Stop generated local streams. 151 // Stop generated local streams.
224 EXPECT_TRUE(ms_impl_->StopLocalMediaStream(mixed_desc)); 152 ms_impl_->StopLocalMediaStream(mixed_desc);
225 EXPECT_TRUE(ms_impl_->GetLocalMediaStream(mixed_desc) == NULL); 153 EXPECT_EQ(1, ms_dispatcher_->stop_stream_counter());
226 154 ms_impl_->StopLocalMediaStream(audio_desc);
227 EXPECT_TRUE(ms_impl_->StopLocalMediaStream(audio_desc)); 155 EXPECT_EQ(2, ms_dispatcher_->stop_stream_counter());
228 EXPECT_TRUE(ms_impl_->GetLocalMediaStream(audio_desc) == NULL);
229 156
230 // Test that the MediaStreams are deleted if the owning WebFrame is deleted. 157 // Test that the MediaStreams are deleted if the owning WebFrame is deleted.
231 // In the unit test the owning frame is NULL. 158 // In the unit test the owning frame is NULL.
232 ms_impl_->FrameWillClose(NULL); 159 ms_impl_->FrameWillClose(NULL);
233 EXPECT_TRUE(ms_impl_->GetLocalMediaStream(video_desc) == NULL); 160 EXPECT_EQ(3, ms_dispatcher_->stop_stream_counter());
234 } 161 }
OLDNEW
« no previous file with comments | « content/renderer/media/media_stream_impl.cc ('k') | content/renderer/media/mock_media_stream_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698