OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/renderer/media/media_stream_impl.h" | 5 #include "content/renderer/media/media_stream_impl.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | |
9 #include "base/synchronization/waitable_event.h" | |
8 #include "content/renderer/media/capture_video_decoder.h" | 10 #include "content/renderer/media/capture_video_decoder.h" |
11 #include "content/renderer/media/media_stream_dependency_factory.h" | |
12 #include "content/renderer/media/media_stream_dispatcher.h" | |
13 #include "content/renderer/media/peer_connection_handler.h" | |
14 #include "content/renderer/media/rtc_video_decoder.h" | |
9 #include "content/renderer/media/video_capture_impl_manager.h" | 15 #include "content/renderer/media/video_capture_impl_manager.h" |
10 #include "googleurl/src/gurl.h" | 16 #include "content/renderer/media/video_capture_module_impl.h" |
17 #include "content/renderer/media/webrtc_audio_device_impl.h" | |
18 #include "content/renderer/p2p/ipc_network_manager.h" | |
19 #include "content/renderer/p2p/ipc_socket_factory.h" | |
20 #include "content/renderer/p2p/socket_dispatcher.h" | |
21 #include "jingle/glue/thread_wrapper.h" | |
11 #include "media/base/message_loop_factory.h" | 22 #include "media/base/message_loop_factory.h" |
12 #include "media/base/pipeline.h" | 23 #include "third_party/libjingle/source/talk/p2p/client/httpportallocator.h" |
24 #include "third_party/libjingle/source/talk/session/phone/dummydevicemanager.h" | |
25 #include "third_party/libjingle/source/talk/session/phone/webrtcmediaengine.h" | |
13 | 26 |
14 namespace { | 27 namespace { |
15 | 28 |
16 static const int kVideoCaptureWidth = 352; | 29 static const int kVideoCaptureWidth = 352; |
17 static const int kVideoCaptureHeight = 288; | 30 static const int kVideoCaptureHeight = 288; |
18 static const int kVideoCaptureFramePerSecond = 30; | 31 static const int kVideoCaptureFramePerSecond = 30; |
19 | 32 |
20 static const int kStartOpenSessionId = 1; | |
21 | |
22 // TODO(wjia): remove this string when full media stream code is checked in. | |
23 static const char kRawMediaScheme[] = "mediastream"; | |
24 | |
25 } // namespace | 33 } // namespace |
26 | 34 |
27 MediaStreamImpl::MediaStreamImpl(VideoCaptureImplManager* vc_manager) | 35 MediaStreamImpl::MediaStreamImpl( |
28 : vc_manager_(vc_manager) { | 36 MediaStreamDispatcher* media_stream_dispatcher, |
29 } | 37 content::P2PSocketDispatcher* p2p_socket_dispatcher, |
30 | 38 VideoCaptureImplManager* vc_manager, |
31 MediaStreamImpl::~MediaStreamImpl() {} | 39 MediaStreamDependencyFactory* dependency_factory) |
40 : dependency_factory_(dependency_factory), | |
41 media_stream_dispatcher_(media_stream_dispatcher), | |
42 media_engine_(NULL), | |
43 p2p_socket_dispatcher_(p2p_socket_dispatcher), | |
44 vc_manager_(vc_manager), | |
45 message_loop_proxy_(base::MessageLoopProxy::current()), | |
46 signaling_thread_(NULL), | |
47 worker_thread_(NULL), | |
48 chrome_worker_thread_("Chrome_libJingle_WorkerThread"), | |
49 vcm_created_(false) { | |
50 } | |
51 | |
52 MediaStreamImpl::~MediaStreamImpl() { | |
53 if (dependency_factory_.get()) | |
54 dependency_factory_->DeletePeerConnectionFactory(); | |
55 } | |
56 | |
57 WebKit::WebPeerConnectionHandler* MediaStreamImpl::CreatePeerConnectionHandler( | |
58 WebKit::WebPeerConnectionHandlerClient* client) { | |
59 if (peer_connection_handler_.get()) { | |
60 VLOG(1) << "A PeerConnection already exists"; | |
61 return NULL; | |
62 } | |
63 | |
64 if (!media_engine_) { | |
65 media_engine_ = dependency_factory_->CreateWebRtcMediaEngine(); | |
66 } | |
67 | |
68 if (!signaling_thread_) { | |
69 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); | |
70 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | |
71 signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); | |
72 } | |
73 | |
74 if (!worker_thread_) { | |
75 if (!chrome_worker_thread_.IsRunning()) { | |
76 if (!chrome_worker_thread_.Start()) { | |
77 LOG(ERROR) << "Could not start worker thread"; | |
78 return NULL; | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
is it ok to not clean up the signaling thread or m
Henrik Grunell
2011/11/08 22:06:41
Good point. Might not be, fixed.
| |
79 } | |
80 } | |
81 base::WaitableEvent event(true, false); | |
82 chrome_worker_thread_.message_loop()->PostTask( | |
83 FROM_HERE, | |
84 base::Bind(&MediaStreamImpl::InitializeWorkerThread, this, | |
85 &worker_thread_, &event)); | |
86 event.Wait(); | |
87 DCHECK(worker_thread_); | |
88 } | |
89 | |
90 if (!dependency_factory_->PeerConnectionFactoryCreated()) { | |
91 ipc_network_manager_.reset( | |
92 new content::IpcNetworkManager(p2p_socket_dispatcher_)); | |
93 ipc_socket_factory_.reset( | |
94 new content::IpcPacketSocketFactory(p2p_socket_dispatcher_)); | |
95 cricket::HttpPortAllocator* port_allocator = new cricket::HttpPortAllocator( | |
96 ipc_network_manager_.get(), ipc_socket_factory_.get(), | |
97 "PeerConnection"); | |
98 // TODO(mallinath): The following flags were added to solve a crash in | |
99 // HttpClient, we should probably remove them after that issue has been | |
100 // investigated. | |
101 port_allocator->set_flags( | |
102 cricket::PORTALLOCATOR_DISABLE_TCP | |
103 | cricket::PORTALLOCATOR_DISABLE_RELAY); | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
| should be on the preceding line and align the fl
Henrik Grunell
2011/11/08 22:06:41
Done.
| |
104 | |
105 // TODO(mallinath): PeerConnectionFactory constructor changed in latest | |
106 // code and it no more accepts config string. Config string must be parsed | |
107 // here and set in HttpPortAllocator. Now using standard google STUN server | |
108 // address. | |
109 std::vector<talk_base::SocketAddress> stun_hosts; | |
110 stun_hosts.push_back(talk_base::SocketAddress("stun.l.google.com", 19302)); | |
111 port_allocator->SetStunHosts(stun_hosts); | |
112 | |
113 if (!dependency_factory_->CreatePeerConnectionFactory(port_allocator, | |
114 media_engine_, | |
115 worker_thread_)) { | |
116 LOG(ERROR) | |
117 << __FUNCTION__ << ": Could not initialize PeerConnection factory"; | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
we typically don't use __FUNCTION__ in logs.
Henrik Grunell
2011/11/08 22:06:41
Done.
| |
118 return NULL; | |
119 } | |
120 } | |
121 | |
122 peer_connection_handler_.reset(new PeerConnectionHandler( | |
123 client, | |
124 this, | |
125 dependency_factory_.get(), | |
126 signaling_thread_)); | |
127 | |
128 return peer_connection_handler_.get(); | |
129 } | |
130 | |
131 void MediaStreamImpl::ClosePeerConnection() { | |
132 rtc_video_decoder_ = NULL; | |
133 media_engine_->SetVideoCaptureModule(NULL); | |
134 vcm_created_ = false; | |
135 peer_connection_handler_.reset(); | |
136 } | |
137 | |
138 bool MediaStreamImpl::SetVideoCaptureModule(const std::string label) { | |
139 if (vcm_created_) | |
140 return true; | |
141 // Set the capture device. | |
142 // TODO(grunell): Instead of using the first track, the selected track | |
143 // should be used. | |
144 int id = media_stream_dispatcher_->video_session_id(label, 0); | |
145 if (id == media_stream::StreamDeviceInfo::kNoId) | |
146 return false; | |
147 webrtc::VideoCaptureModule* vcm = | |
148 new VideoCaptureModuleImpl(id, vc_manager_.get()); | |
149 vcm_created_ = true; | |
150 media_engine_->SetVideoCaptureModule(vcm); | |
151 return true; | |
152 } | |
153 | |
154 // TODO(grunell): Enable and update implementation when available in WebKit. | |
155 //void MediaStreamImpl::generateStream( | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
is it necessary to check in all this commented-out
Henrik Grunell
2011/11/08 22:06:41
Can absolutely be removed; done. (But it will be a
| |
156 // int request_id, | |
157 // WebKit::WebGenerateStreamOptionFlags flags, | |
158 // const WebKit::WebSecurityOrigin& web_security_origin) { | |
159 // bool audio = (flags & WebKit::WebGenerateStreamRequestAudio) != 0; | |
160 // media_stream::StreamOptions::VideoOption video_option = | |
161 // media_stream::StreamOptions::kNoCamera; | |
162 // if ((flags & WebKit::WebGenerateStreamRequestVideoFacingUser) && | |
163 // (flags & WebKit::WebGenerateStreamRequestVideoFacingEnvironment)) { | |
164 // video_option = media_stream::StreamOptions::kFacingBoth; | |
165 // } else { | |
166 // if (flags & WebKit::WebGenerateStreamRequestVideoFacingEnvironment) | |
167 // video_option = media_stream::StreamOptions::kFacingEnvironment; | |
168 // if (flags & WebKit::WebGenerateStreamRequestVideoFacingUser) | |
169 // video_option = media_stream::StreamOptions::kFacingUser; | |
170 // } | |
171 // DVLOG(1) << "MediaStreamImpl::generateStream(" | |
172 // << request_id << ", [ " | |
173 // << (audio ? "audio " : "") | |
174 // << ((flags & WebKit::WebGenerateStreamRequestVideoFacingUser) ? | |
175 // "video_facing_user " : "") | |
176 // << ((flags & | |
177 // WebKit::WebGenerateStreamRequestVideoFacingEnvironment) ? | |
178 // "video_facing_environment " : "") | |
179 // << "], " | |
180 // << static_cast<string16>(web_security_origin.toString()) << ")"; | |
181 // | |
182 // media_stream_dispatcher_->GenerateStream(request_id, this, | |
183 // media_stream::StreamOptions(audio, video_option), | |
184 // UTF16ToUTF8(web_security_origin.toString())); | |
185 //} | |
186 | |
187 // TODO(grunell): Enable and update implementation when available in WebKit. | |
188 //void MediaStreamImpl::stopGeneratedStream( | |
189 // const WebKit::WebLocalMediaStream& stream) { | |
190 // std::string label = UTF16ToUTF8(stream.label()); | |
191 // media_stream_dispatcher_->StopStream(label); | |
192 //} | |
193 | |
194 // TODO(grunell): Enable and update implementation when available in WebKit. | |
195 //void MediaStreamImpl::recordStream( | |
196 // const WebKit::WebMediaStreamRecorder& recorder) { | |
197 // // TODO(grunell): Implement. | |
198 // NOTIMPLEMENTED(); | |
199 //} | |
200 | |
201 // TODO(grunell): Enable and update implementation when available in WebKit. | |
202 //void MediaStreamImpl::getRecordedData( | |
203 // const WebKit::WebMediaStreamRecorder& recorder, | |
204 // int request_id) { | |
205 // // TODO(grunell): Implement. | |
206 // NOTIMPLEMENTED(); | |
207 //} | |
208 | |
209 // TODO(grunell): Enable and update implementation when available in WebKit. | |
210 //void MediaStreamImpl::disposeRecordedData( | |
211 // const WebKit::WebMediaStreamRecorder& recorder) { | |
212 // // TODO(grunell): Implement. | |
213 // NOTIMPLEMENTED(); | |
214 //} | |
215 | |
216 // TODO(grunell): Enable and update implementation when available in WebKit. | |
217 //void MediaStreamImpl::setMediaStreamTrackEnabled( | |
218 // const WebKit::WebMediaStreamTrack& track) { | |
219 // // TODO(grunell): Implement. | |
220 // NOTIMPLEMENTED(); | |
221 //} | |
32 | 222 |
33 scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( | 223 scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( |
34 const GURL& url, media::MessageLoopFactory* message_loop_factory) { | 224 const GURL& url, |
35 bool raw_media = (url.spec().find(kRawMediaScheme) == 0); | 225 media::MessageLoopFactory* message_loop_factory) { |
36 media::VideoDecoder* decoder = NULL; | 226 // TODO(grunell): Enable when this has been re-added in WebKit. This will fail |
37 if (raw_media) { | 227 // meanwhile. |
228 std::string label; | |
229 // std::string label = | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
remove?
Henrik Grunell
2011/11/08 22:06:41
Done.
| |
230 // UTF16ToUTF8(WebKit::WebMediaStreamRegistry::mediaStreamLabel(url)); | |
231 if (label.empty()) | |
232 return NULL; // This is not a valid stream. | |
233 | |
234 scoped_refptr<media::VideoDecoder> decoder; | |
235 if (media_stream_dispatcher_->IsStream(label)) { | |
236 // It's a local stream. | |
237 int video_session_id = media_stream_dispatcher_->video_session_id(label, 0); | |
38 media::VideoCapture::VideoCaptureCapability capability; | 238 media::VideoCapture::VideoCaptureCapability capability; |
39 capability.width = kVideoCaptureWidth; | 239 capability.width = kVideoCaptureWidth; |
40 capability.height = kVideoCaptureHeight; | 240 capability.height = kVideoCaptureHeight; |
41 capability.max_fps = kVideoCaptureFramePerSecond; | 241 capability.max_fps = kVideoCaptureFramePerSecond; |
42 capability.expected_capture_delay = 0; | 242 capability.expected_capture_delay = 0; |
43 capability.raw_type = media::VideoFrame::I420; | 243 capability.raw_type = media::VideoFrame::I420; |
44 capability.interlaced = false; | 244 capability.interlaced = false; |
45 | |
46 decoder = new CaptureVideoDecoder( | 245 decoder = new CaptureVideoDecoder( |
47 message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoder").get(), | 246 message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoderThread"), |
48 kStartOpenSessionId, vc_manager_.get(), capability); | 247 video_session_id, |
248 vc_manager_.get(), | |
249 capability); | |
250 } else { | |
251 // It's a remote stream. | |
252 size_t found = label.rfind("-remote"); | |
253 if (found != std::string::npos) | |
254 label = label.substr(0, found); | |
255 if (rtc_video_decoder_.get()) { | |
256 // The renderer is used by PeerConnection, release it first. | |
257 // TODO(grunell): Call the pc handler. | |
258 // if (native_peer_connection_.get()) | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
remove all code that's commented out like this?
Henrik Grunell
2011/11/08 22:06:41
Fixed the todo; calling PC handler.
| |
259 // native_peer_connection_->SetVideoRenderer(label, NULL); | |
260 } | |
261 rtc_video_decoder_ = new RTCVideoDecoder( | |
262 message_loop_factory->GetMessageLoop("RtcVideoDecoderThread"), | |
263 url.spec()); | |
264 decoder = rtc_video_decoder_; | |
265 // TODO(grunell): Call the pc handler. | |
266 // if (native_peer_connection_.get()) | |
267 // native_peer_connection_->SetVideoRenderer(label, rtc_video_decoder_); | |
49 } | 268 } |
50 return decoder; | 269 return decoder; |
51 } | 270 } |
271 | |
272 void MediaStreamImpl::OnStreamGenerated( | |
273 int request_id, | |
274 const std::string& label, | |
275 const media_stream::StreamDeviceInfoArray& audio_array, | |
276 const media_stream::StreamDeviceInfoArray& video_array) { | |
277 // TODO(grunell): Enable and update implementation when available in WebKit. | |
278 NOTIMPLEMENTED(); | |
279 // WebKit::WebVector<WebKit::WebMediaStreamTrack> web_track_vector( | |
tommi (sloooow) - chröme
2011/11/08 12:27:24
same here
Henrik Grunell
2011/11/08 22:06:41
Done.
| |
280 // audio_array.size() + video_array.size()); | |
281 // | |
282 // WebKit::WebString track_id(WebKit::WebString::fromUTF8("")); | |
283 // WebKit::WebString track_kind(WebKit::WebString::fromUTF8("main")); | |
284 // WebKit::WebString track_label_audio( | |
285 // WebKit::WebString::fromUTF8("AudioDevice")); | |
286 // WebKit::WebString track_label_video( | |
287 // WebKit::WebString::fromUTF8("VideoCapture")); | |
288 // size_t track_num = web_track_vector.size(); | |
289 // while (track_num--) { | |
290 // if (track_num < audio_array.size()) { | |
291 // web_track_vector[track_num].initialize(track_id, | |
292 // track_kind, | |
293 // track_label_audio); | |
294 // } else { | |
295 // web_track_vector[track_num].initialize(track_id, | |
296 // track_kind, | |
297 // track_label_video); | |
298 // } | |
299 // } | |
300 // | |
301 // WebKit::WebMediaStreamTrackList web_track_list; | |
302 // web_track_list.initialize(web_track_vector); | |
303 // | |
304 // WebKit::WebLocalMediaStream web_local_media_stream; | |
305 // web_local_media_stream.initialize(UTF8ToUTF16(label), web_track_list); | |
306 // | |
307 // controller_->streamGenerated(request_id, web_local_media_stream); | |
308 } | |
309 | |
310 void MediaStreamImpl::OnStreamGenerationFailed(int request_id) { | |
311 DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed(" | |
312 << request_id << ")"; | |
313 // TODO(grunell): Enable and update implementation when available in WebKit. | |
314 NOTIMPLEMENTED(); | |
315 // controller_->streamGenerationFailed( | |
316 // request_id, | |
317 // WebKit::WebMediaStreamController::ErrorPermissionDenied); | |
318 } | |
319 | |
320 void MediaStreamImpl::OnVideoDeviceFailed(const std::string& label, | |
321 int index) { | |
322 // TODO(grunell): Enable and update implementation when available in WebKit. | |
323 NOTIMPLEMENTED(); | |
324 DVLOG(1) << "MediaStreamImpl::OnVideoDeviceFailed(" | |
325 << label << ", " << index << ")"; | |
326 // controller_->streamFailed(UTF8ToUTF16(label)); | |
327 } | |
328 | |
329 void MediaStreamImpl::OnAudioDeviceFailed(const std::string& label, | |
330 int index) { | |
331 DVLOG(1) << "MediaStreamImpl::OnAudioDeviceFailed(" | |
332 << label << ", " << index << ")"; | |
333 // TODO(grunell): Enable and update implementation when available in WebKit. | |
334 NOTIMPLEMENTED(); | |
335 // controller_->streamFailed(UTF8ToUTF16(label)); | |
336 } | |
337 | |
338 void MediaStreamImpl::InitializeWorkerThread(talk_base::Thread** thread, | |
339 base::WaitableEvent* event) { | |
340 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); | |
341 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | |
342 *thread = jingle_glue::JingleThreadWrapper::current(); | |
343 event->Signal(); | |
344 } | |
OLD | NEW |