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.
5 #include "content/renderer/media/media_stream_impl.h"
10 #include "base/logging.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/utf_string_conversions.h"
13 #include "content/renderer/media/capture_video_decoder.h"
14 #include "content/renderer/media/media_stream_dependency_factory.h"
15 #include "content/renderer/media/media_stream_dispatcher.h"
16 #include "content/renderer/media/peer_connection_handler.h"
17 #include "content/renderer/media/rtc_video_decoder.h"
18 #include "content/renderer/media/video_capture_impl_manager.h"
19 #include "content/renderer/media/video_capture_module_impl.h"
20 #include "content/renderer/media/webrtc_audio_device_impl.h"
21 #include "content/renderer/p2p/ipc_network_manager.h"
22 #include "content/renderer/p2p/ipc_socket_factory.h"
23 #include "content/renderer/p2p/socket_dispatcher.h"
24 #include "jingle/glue/thread_wrapper.h"
25 #include "media/base/message_loop_factory.h"
26 #include "third_party/libjingle/source/talk/p2p/client/httpportallocator.h"
27 #include "third_party/libjingle/source/talk/session/phone/dummydevicemanager.h"
28 #include "third_party/libjingle/source/talk/session/phone/webrtcmediaengine.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStreamDescriptor.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistry.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStreamSource.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
36 static const int kVideoCaptureWidth
= 352;
37 static const int kVideoCaptureHeight
= 288;
38 static const int kVideoCaptureFramePerSecond
= 30;
42 int MediaStreamImpl::next_request_id_
= 0;
44 MediaStreamImpl::MediaStreamImpl(
45 MediaStreamDispatcher
* media_stream_dispatcher
,
46 content::P2PSocketDispatcher
* p2p_socket_dispatcher
,
47 VideoCaptureImplManager
* vc_manager
,
48 MediaStreamDependencyFactory
* dependency_factory
)
49 : dependency_factory_(dependency_factory
),
50 media_stream_dispatcher_(media_stream_dispatcher
),
52 p2p_socket_dispatcher_(p2p_socket_dispatcher
),
53 network_manager_(NULL
),
54 vc_manager_(vc_manager
),
55 peer_connection_handler_(NULL
),
56 message_loop_proxy_(base::MessageLoopProxy::current()),
57 signaling_thread_(NULL
),
59 chrome_worker_thread_("Chrome_libJingle_WorkerThread"),
63 MediaStreamImpl::~MediaStreamImpl() {
64 DCHECK(!peer_connection_handler_
);
65 if (dependency_factory_
.get())
66 dependency_factory_
->DeletePeerConnectionFactory();
67 if (network_manager_
) {
68 // The network manager needs to free its resources on the thread they were
69 // created, which is the worked thread.
70 if (chrome_worker_thread_
.IsRunning()) {
71 chrome_worker_thread_
.message_loop()->PostTask(FROM_HERE
, base::Bind(
72 &MediaStreamImpl::DeleteIpcNetworkManager
,
73 base::Unretained(this)));
75 NOTREACHED() << "Worker thread not running.";
80 WebKit::WebPeerConnectionHandler
* MediaStreamImpl::CreatePeerConnectionHandler(
81 WebKit::WebPeerConnectionHandlerClient
* client
) {
82 DCHECK(CalledOnValidThread());
83 if (peer_connection_handler_
) {
84 DVLOG(1) << "A PeerConnection already exists";
89 media_engine_
= dependency_factory_
->CreateWebRtcMediaEngine();
92 if (!signaling_thread_
) {
93 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread();
94 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
95 signaling_thread_
= jingle_glue::JingleThreadWrapper::current();
98 if (!worker_thread_
) {
99 if (!chrome_worker_thread_
.IsRunning()) {
100 if (!chrome_worker_thread_
.Start()) {
101 LOG(ERROR
) << "Could not start worker thread";
102 delete media_engine_
;
103 media_engine_
= NULL
;
104 signaling_thread_
= NULL
;
108 base::WaitableEvent
event(true, false);
109 chrome_worker_thread_
.message_loop()->PostTask(
111 base::Bind(&MediaStreamImpl::InitializeWorkerThread
, this,
112 &worker_thread_
, &event
));
114 DCHECK(worker_thread_
);
117 if (!network_manager_
)
118 network_manager_
= new content::IpcNetworkManager(p2p_socket_dispatcher_
);
120 if (!socket_factory_
.get()) {
121 socket_factory_
.reset(
122 new content::IpcPacketSocketFactory(p2p_socket_dispatcher_
));
125 if (!dependency_factory_
->PeerConnectionFactoryCreated()) {
126 if (!dependency_factory_
->CreatePeerConnectionFactory(media_engine_
,
128 LOG(ERROR
) << "Could not initialize PeerConnection factory";
133 peer_connection_handler_
= new PeerConnectionHandler(
136 dependency_factory_
.get(),
138 p2p_socket_dispatcher_
,
140 socket_factory_
.get());
142 return peer_connection_handler_
;
145 void MediaStreamImpl::ClosePeerConnection() {
146 DCHECK(CalledOnValidThread());
147 rtc_video_decoder_
= NULL
;
148 media_engine_
->SetVideoCaptureModule(NULL
);
149 vcm_created_
= false;
150 peer_connection_handler_
= NULL
;
153 bool MediaStreamImpl::SetVideoCaptureModule(const std::string
& label
) {
154 DCHECK(CalledOnValidThread());
157 // Set the capture device.
158 // TODO(grunell): Instead of using the first track, the selected track
160 int id
= media_stream_dispatcher_
->video_session_id(label
, 0);
161 if (id
== media_stream::StreamDeviceInfo::kNoId
)
163 webrtc::VideoCaptureModule
* vcm
=
164 new VideoCaptureModuleImpl(id
, vc_manager_
.get());
166 media_engine_
->SetVideoCaptureModule(vcm
);
170 void MediaStreamImpl::requestUserMedia(
171 const WebKit::WebUserMediaRequest
& user_media_request
,
172 const WebKit::WebVector
<WebKit::WebMediaStreamSource
>&
173 media_stream_source_vector
) {
174 DCHECK(CalledOnValidThread());
175 DCHECK(!user_media_request
.isNull());
177 int request_id
= next_request_id_
++;
179 bool audio
= user_media_request
.audio();
180 media_stream::StreamOptions::VideoOption video_option
=
181 media_stream::StreamOptions::kNoCamera
;
182 if (user_media_request
.video()) {
183 // If no preference is set, use user facing camera.
184 video_option
= media_stream::StreamOptions::kFacingUser
;
185 if (user_media_request
.cameraPreferenceUser() &&
186 user_media_request
.cameraPreferenceEnvironment()) {
187 video_option
= media_stream::StreamOptions::kFacingBoth
;
188 } else if (user_media_request
.cameraPreferenceEnvironment()) {
189 video_option
= media_stream::StreamOptions::kFacingEnvironment
;
193 std::string security_origin
= UTF16ToUTF8(
194 user_media_request
.securityOrigin().toString());
196 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id
<< ", [ "
197 << (audio
? "audio " : "")
198 << ((user_media_request
.cameraPreferenceUser()) ?
199 "video_facing_user " : "")
200 << ((user_media_request
.cameraPreferenceEnvironment()) ?
201 "video_facing_environment " : "") << "], "
202 << security_origin
<< ")";
204 user_media_requests_
.insert(
205 std::pair
<int, WebKit::WebUserMediaRequest
>(
206 request_id
, user_media_request
));
208 media_stream_dispatcher_
->GenerateStream(
211 media_stream::StreamOptions(audio
, video_option
),
215 void MediaStreamImpl::cancelUserMediaRequest(
216 const WebKit::WebUserMediaRequest
& user_media_request
) {
217 // TODO(grunell): Implement.
221 scoped_refptr
<media::VideoDecoder
> MediaStreamImpl::GetVideoDecoder(
223 media::MessageLoopFactory
* message_loop_factory
) {
224 DCHECK(CalledOnValidThread());
225 WebKit::WebMediaStreamDescriptor
descriptor(
226 WebKit::WebMediaStreamRegistry::lookupMediaStreamDescriptor(url
));
227 if (descriptor
.isNull())
228 return NULL
; // This is not a valid stream.
229 WebKit::WebVector
<WebKit::WebMediaStreamSource
> source_vector
;
230 descriptor
.sources(source_vector
);
232 for (size_t i
= 0; i
< source_vector
.size(); ++i
) {
233 if (source_vector
[i
].type() == WebKit::WebMediaStreamSource::TypeVideo
) {
234 label
= UTF16ToUTF8(source_vector
[i
].id());
241 scoped_refptr
<media::VideoDecoder
> decoder
;
242 if (media_stream_dispatcher_
->IsStream(label
)) {
243 // It's a local stream.
244 int video_session_id
= media_stream_dispatcher_
->video_session_id(label
, 0);
245 media::VideoCapture::VideoCaptureCapability capability
;
246 capability
.width
= kVideoCaptureWidth
;
247 capability
.height
= kVideoCaptureHeight
;
248 capability
.max_fps
= kVideoCaptureFramePerSecond
;
249 capability
.expected_capture_delay
= 0;
250 capability
.raw_type
= media::VideoFrame::I420
;
251 capability
.interlaced
= false;
252 decoder
= new CaptureVideoDecoder(
253 message_loop_factory
->GetMessageLoopProxy("CaptureVideoDecoderThread"),
258 // It's a remote stream.
259 size_t found
= label
.rfind("-remote");
260 if (found
!= std::string::npos
)
261 label
= label
.substr(0, found
);
262 if (rtc_video_decoder_
.get()) {
263 // The renderer is used by PeerConnection, release it first.
264 if (peer_connection_handler_
)
265 peer_connection_handler_
->SetVideoRenderer(label
, NULL
);
267 rtc_video_decoder_
= new RTCVideoDecoder(
268 message_loop_factory
->GetMessageLoop("RtcVideoDecoderThread"),
270 decoder
= rtc_video_decoder_
;
271 if (peer_connection_handler_
)
272 peer_connection_handler_
->SetVideoRenderer(label
, rtc_video_decoder_
);
277 void MediaStreamImpl::OnStreamGenerated(
279 const std::string
& label
,
280 const media_stream::StreamDeviceInfoArray
& audio_array
,
281 const media_stream::StreamDeviceInfoArray
& video_array
) {
282 DCHECK(CalledOnValidThread());
284 // We only support max one audio track and one video track. If the UI
285 // for selecting device starts to allow several devices, we must implement
286 // handling for this.
287 DCHECK_LE(audio_array
.size(), 1u);
288 DCHECK_LE(video_array
.size(), 1u);
289 WebKit::WebVector
<WebKit::WebMediaStreamSource
> source_vector(
290 audio_array
.size() + video_array
.size());
292 WebKit::WebString
track_label_audio(UTF8ToUTF16("AudioDevice"));
293 WebKit::WebString
track_label_video(UTF8ToUTF16("VideoCapture"));
294 size_t track_num
= source_vector
.size();
295 while (track_num
--) {
296 if (track_num
< audio_array
.size()) {
297 source_vector
[track_num
].initialize(
299 WebKit::WebMediaStreamSource::TypeAudio
,
302 source_vector
[track_num
].initialize(
304 WebKit::WebMediaStreamSource::TypeVideo
,
309 MediaRequestMap::iterator it
= user_media_requests_
.find(request_id
);
310 if (it
== user_media_requests_
.end()) {
311 DVLOG(1) << "Request ID not found";
314 WebKit::WebUserMediaRequest user_media_request
= it
->second
;
315 user_media_requests_
.erase(it
);
316 stream_labels_
.push_back(label
);
318 user_media_request
.requestSucceeded(source_vector
);
321 void MediaStreamImpl::OnStreamGenerationFailed(int request_id
) {
322 DCHECK(CalledOnValidThread());
323 DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed("
324 << request_id
<< ")";
325 MediaRequestMap::iterator it
= user_media_requests_
.find(request_id
);
326 if (it
== user_media_requests_
.end()) {
327 DVLOG(1) << "Request ID not found";
330 WebKit::WebUserMediaRequest user_media_request
= it
->second
;
331 user_media_requests_
.erase(it
);
333 user_media_request
.requestFailed();
336 void MediaStreamImpl::OnVideoDeviceFailed(const std::string
& label
,
338 DCHECK(CalledOnValidThread());
339 DVLOG(1) << "MediaStreamImpl::OnVideoDeviceFailed("
340 << label
<< ", " << index
<< ")";
341 // TODO(grunell): Implement. Currently not supported in WebKit.
345 void MediaStreamImpl::OnAudioDeviceFailed(const std::string
& label
,
347 DCHECK(CalledOnValidThread());
348 DVLOG(1) << "MediaStreamImpl::OnAudioDeviceFailed("
349 << label
<< ", " << index
<< ")";
350 // TODO(grunell): Implement. Currently not supported in WebKit.
354 void MediaStreamImpl::InitializeWorkerThread(talk_base::Thread
** thread
,
355 base::WaitableEvent
* event
) {
356 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread();
357 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
358 *thread
= jingle_glue::JingleThreadWrapper::current();
362 void MediaStreamImpl::DeleteIpcNetworkManager() {
363 DCHECK_EQ(MessageLoop::current(), chrome_worker_thread_
.message_loop());
364 delete network_manager_
;
365 network_manager_
= NULL
;