Chromium Code Reviews| 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 <utility> |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/synchronization/waitable_event.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 8 #include "content/renderer/media/capture_video_decoder.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" | |
| 9 #include "content/renderer/media/video_capture_impl_manager.h" | 18 #include "content/renderer/media/video_capture_impl_manager.h" |
| 10 #include "googleurl/src/gurl.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" | |
| 11 #include "media/base/message_loop_factory.h" | 25 #include "media/base/message_loop_factory.h" |
| 12 #include "media/base/pipeline.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/WebMediaStreamDescrip tor.h" | |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistr y.h" | |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamSource. h" | |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | |
| 13 | 33 |
| 14 namespace { | 34 namespace { |
| 15 | 35 |
| 16 static const int kVideoCaptureWidth = 352; | 36 static const int kVideoCaptureWidth = 352; |
| 17 static const int kVideoCaptureHeight = 288; | 37 static const int kVideoCaptureHeight = 288; |
| 18 static const int kVideoCaptureFramePerSecond = 30; | 38 static const int kVideoCaptureFramePerSecond = 30; |
| 19 | 39 |
| 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 | 40 } // namespace |
| 26 | 41 |
| 27 MediaStreamImpl::MediaStreamImpl(VideoCaptureImplManager* vc_manager) | 42 int MediaStreamImpl::next_request_id_ = 0; |
| 28 : vc_manager_(vc_manager) { | 43 |
| 29 } | 44 MediaStreamImpl::MediaStreamImpl( |
| 30 | 45 MediaStreamDispatcher* media_stream_dispatcher, |
| 31 MediaStreamImpl::~MediaStreamImpl() {} | 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), | |
| 51 media_engine_(NULL), | |
| 52 p2p_socket_dispatcher_(p2p_socket_dispatcher), | |
| 53 vc_manager_(vc_manager), | |
| 54 port_allocator_(NULL), | |
| 55 peer_connection_handler_(NULL), | |
| 56 message_loop_proxy_(base::MessageLoopProxy::current()), | |
| 57 signaling_thread_(NULL), | |
| 58 worker_thread_(NULL), | |
| 59 chrome_worker_thread_("Chrome_libJingle_WorkerThread"), | |
| 60 vcm_created_(false) { | |
| 61 } | |
| 62 | |
| 63 MediaStreamImpl::~MediaStreamImpl() { | |
| 64 if (dependency_factory_.get()) | |
| 65 dependency_factory_->DeletePeerConnectionFactory(); | |
| 66 } | |
| 67 | |
| 68 WebKit::WebPeerConnectionHandler* MediaStreamImpl::CreatePeerConnectionHandler( | |
| 69 WebKit::WebPeerConnectionHandlerClient* client) { | |
| 70 if (peer_connection_handler_) { | |
| 71 VLOG(1) << "A PeerConnection already exists"; | |
|
scherkus (not reviewing)
2011/11/23 22:52:04
DVLOG?
Henrik Grunell
2011/11/24 11:32:59
Yes better. Fixed.
| |
| 72 return NULL; | |
| 73 } | |
| 74 | |
| 75 if (!media_engine_) { | |
| 76 media_engine_ = dependency_factory_->CreateWebRtcMediaEngine(); | |
| 77 } | |
| 78 | |
| 79 if (!signaling_thread_) { | |
| 80 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); | |
| 81 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | |
| 82 signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); | |
| 83 } | |
| 84 | |
| 85 if (!worker_thread_) { | |
| 86 if (!chrome_worker_thread_.IsRunning()) { | |
| 87 if (!chrome_worker_thread_.Start()) { | |
| 88 LOG(ERROR) << "Could not start worker thread"; | |
| 89 delete media_engine_; | |
| 90 media_engine_ = NULL; | |
| 91 signaling_thread_ = NULL; | |
| 92 return NULL; | |
| 93 } | |
| 94 } | |
| 95 base::WaitableEvent event(true, false); | |
| 96 chrome_worker_thread_.message_loop()->PostTask( | |
| 97 FROM_HERE, | |
| 98 base::Bind(&MediaStreamImpl::InitializeWorkerThread, this, | |
| 99 &worker_thread_, &event)); | |
| 100 event.Wait(); | |
| 101 DCHECK(worker_thread_); | |
| 102 } | |
| 103 | |
| 104 if (!dependency_factory_->PeerConnectionFactoryCreated()) { | |
| 105 ipc_network_manager_.reset( | |
| 106 new content::IpcNetworkManager(p2p_socket_dispatcher_)); | |
| 107 ipc_socket_factory_.reset( | |
| 108 new content::IpcPacketSocketFactory(p2p_socket_dispatcher_)); | |
| 109 port_allocator_ = new cricket::HttpPortAllocator( | |
| 110 ipc_network_manager_.get(), | |
| 111 ipc_socket_factory_.get(), | |
| 112 "PeerConnection"); | |
| 113 // TODO(mallinath): The following flags were added to solve a crash in | |
| 114 // HttpClient, we should probably remove them after that issue has been | |
| 115 // investigated. | |
| 116 port_allocator_->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | | |
| 117 cricket::PORTALLOCATOR_DISABLE_RELAY); | |
| 118 | |
| 119 if (!dependency_factory_->CreatePeerConnectionFactory(port_allocator_, | |
| 120 media_engine_, | |
| 121 worker_thread_)) { | |
| 122 LOG(ERROR) << "Could not initialize PeerConnection factory"; | |
| 123 return NULL; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 peer_connection_handler_ = new PeerConnectionHandler( | |
| 128 client, | |
| 129 this, | |
| 130 dependency_factory_.get(), | |
| 131 signaling_thread_, | |
| 132 port_allocator_); | |
| 133 | |
| 134 return peer_connection_handler_; | |
| 135 } | |
| 136 | |
| 137 void MediaStreamImpl::ClosePeerConnection() { | |
| 138 rtc_video_decoder_ = NULL; | |
| 139 media_engine_->SetVideoCaptureModule(NULL); | |
| 140 vcm_created_ = false; | |
| 141 peer_connection_handler_ = NULL; | |
| 142 } | |
| 143 | |
| 144 bool MediaStreamImpl::SetVideoCaptureModule(const std::string& label) { | |
| 145 if (vcm_created_) | |
| 146 return true; | |
| 147 // Set the capture device. | |
| 148 // TODO(grunell): Instead of using the first track, the selected track | |
| 149 // should be used. | |
| 150 int id = media_stream_dispatcher_->video_session_id(label, 0); | |
| 151 if (id == media_stream::StreamDeviceInfo::kNoId) | |
| 152 return false; | |
| 153 webrtc::VideoCaptureModule* vcm = | |
| 154 new VideoCaptureModuleImpl(id, vc_manager_.get()); | |
| 155 vcm_created_ = true; | |
| 156 media_engine_->SetVideoCaptureModule(vcm); | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 void MediaStreamImpl::pageDestroyed() { | |
| 161 std::list<std::string>::iterator it; | |
| 162 for (it = stream_labels_.begin() ; it != stream_labels_.end(); it++) | |
| 163 media_stream_dispatcher_->StopStream(*it); | |
| 164 stream_labels_.clear(); | |
| 165 } | |
| 166 | |
| 167 void MediaStreamImpl::requestUserMedia( | |
| 168 const WebKit::WebUserMediaRequest& user_media_request, | |
| 169 const WebKit::WebVector<WebKit::WebMediaStreamSource>& | |
| 170 media_stream_source_vector) { | |
| 171 DCHECK(!user_media_request.isNull()); | |
| 172 | |
| 173 int request_id = next_request_id_++; | |
| 174 | |
| 175 bool audio = user_media_request.audio(); | |
| 176 media_stream::StreamOptions::VideoOption video_option = | |
| 177 media_stream::StreamOptions::kNoCamera; | |
| 178 if (user_media_request.video()) { | |
| 179 // If no preference is set, use user facing camera. | |
| 180 video_option = media_stream::StreamOptions::kFacingUser; | |
| 181 if (user_media_request.cameraPreferenceUser() && | |
| 182 user_media_request.cameraPreferenceEnvironment()) { | |
| 183 video_option = media_stream::StreamOptions::kFacingBoth; | |
| 184 } else if (user_media_request.cameraPreferenceEnvironment()) { | |
| 185 video_option = media_stream::StreamOptions::kFacingEnvironment; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 std::string security_origin = UTF16ToUTF8( | |
| 190 user_media_request.securityOrigin().toString()); | |
| 191 | |
| 192 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " | |
| 193 << (audio ? "audio " : "") | |
| 194 << ((user_media_request.cameraPreferenceUser()) ? | |
| 195 "video_facing_user " : "") | |
| 196 << ((user_media_request.cameraPreferenceEnvironment()) ? | |
| 197 "video_facing_environment " : "") << "], " | |
| 198 << security_origin << ")"; | |
| 199 | |
| 200 user_media_requests_.insert( | |
| 201 std::pair<int, WebKit::WebUserMediaRequest>( | |
| 202 request_id, user_media_request)); | |
| 203 | |
| 204 media_stream_dispatcher_->GenerateStream( | |
| 205 request_id, | |
| 206 this, | |
| 207 media_stream::StreamOptions(audio, video_option), | |
| 208 security_origin); | |
| 209 } | |
| 210 | |
| 211 void MediaStreamImpl::cancelUserMediaRequest( | |
| 212 const WebKit::WebUserMediaRequest& user_media_request) { | |
| 213 // TODO(grunell): Implement. | |
| 214 NOTIMPLEMENTED(); | |
| 215 } | |
| 32 | 216 |
| 33 scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( | 217 scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( |
| 34 const GURL& url, media::MessageLoopFactory* message_loop_factory) { | 218 const GURL& url, |
| 35 bool raw_media = (url.spec().find(kRawMediaScheme) == 0); | 219 media::MessageLoopFactory* message_loop_factory) { |
| 36 media::VideoDecoder* decoder = NULL; | 220 WebKit::WebMediaStreamDescriptor descriptor( |
| 37 if (raw_media) { | 221 WebKit::WebMediaStreamRegistry::lookupMediaStreamDescriptor(url)); |
| 222 if (descriptor.isNull()) | |
| 223 return NULL; // This is not a valid stream. | |
| 224 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector; | |
| 225 descriptor.sources(source_vector); | |
| 226 std::string label; | |
| 227 for (size_t i = 0; i < source_vector.size(); ++i) { | |
| 228 if (source_vector[i].type() == WebKit::WebMediaStreamSource::TypeVideo) { | |
| 229 label = UTF16ToUTF8(source_vector[i].id()); | |
| 230 break; | |
| 231 } | |
| 232 } | |
| 233 if (label.empty()) | |
| 234 return NULL; | |
| 235 | |
| 236 scoped_refptr<media::VideoDecoder> decoder; | |
| 237 if (media_stream_dispatcher_->IsStream(label)) { | |
| 238 // It's a local stream. | |
| 239 int video_session_id = media_stream_dispatcher_->video_session_id(label, 0); | |
| 38 media::VideoCapture::VideoCaptureCapability capability; | 240 media::VideoCapture::VideoCaptureCapability capability; |
| 39 capability.width = kVideoCaptureWidth; | 241 capability.width = kVideoCaptureWidth; |
| 40 capability.height = kVideoCaptureHeight; | 242 capability.height = kVideoCaptureHeight; |
| 41 capability.max_fps = kVideoCaptureFramePerSecond; | 243 capability.max_fps = kVideoCaptureFramePerSecond; |
| 42 capability.expected_capture_delay = 0; | 244 capability.expected_capture_delay = 0; |
| 43 capability.raw_type = media::VideoFrame::I420; | 245 capability.raw_type = media::VideoFrame::I420; |
| 44 capability.interlaced = false; | 246 capability.interlaced = false; |
| 45 | |
| 46 decoder = new CaptureVideoDecoder( | 247 decoder = new CaptureVideoDecoder( |
| 47 message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoder").get(), | 248 message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoderThread"), |
| 48 kStartOpenSessionId, vc_manager_.get(), capability); | 249 video_session_id, |
| 250 vc_manager_.get(), | |
| 251 capability); | |
| 252 } else { | |
| 253 // It's a remote stream. | |
| 254 size_t found = label.rfind("-remote"); | |
| 255 if (found != std::string::npos) | |
| 256 label = label.substr(0, found); | |
| 257 if (rtc_video_decoder_.get()) { | |
| 258 // The renderer is used by PeerConnection, release it first. | |
| 259 if (peer_connection_handler_) | |
| 260 peer_connection_handler_->SetVideoRenderer(label, NULL); | |
| 261 } | |
| 262 rtc_video_decoder_ = new RTCVideoDecoder( | |
| 263 message_loop_factory->GetMessageLoop("RtcVideoDecoderThread"), | |
| 264 url.spec()); | |
| 265 decoder = rtc_video_decoder_; | |
| 266 if (peer_connection_handler_) | |
| 267 peer_connection_handler_->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 // We only support max one audio track and one video track. If the UI | |
| 278 // for selecting device starts to allow several devices, we must implement | |
| 279 // handling for this. | |
| 280 DCHECK_LE(audio_array.size(), 1u); | |
| 281 DCHECK_LE(video_array.size(), 1u); | |
| 282 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( | |
| 283 audio_array.size() + video_array.size()); | |
| 284 | |
| 285 WebKit::WebString track_label_audio(UTF8ToUTF16("AudioDevice")); | |
| 286 WebKit::WebString track_label_video(UTF8ToUTF16("VideoCapture")); | |
| 287 size_t track_num = source_vector.size(); | |
| 288 while (track_num--) { | |
| 289 if (track_num < audio_array.size()) { | |
| 290 source_vector[track_num].initialize( | |
| 291 UTF8ToUTF16(label), | |
| 292 WebKit::WebMediaStreamSource::TypeAudio, | |
| 293 track_label_audio); | |
| 294 } else { | |
| 295 source_vector[track_num].initialize( | |
| 296 UTF8ToUTF16(label), | |
| 297 WebKit::WebMediaStreamSource::TypeVideo, | |
| 298 track_label_video); | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 MediaRequestMap::iterator it = user_media_requests_.find(request_id); | |
| 303 if (it == user_media_requests_.end()) { | |
| 304 DVLOG(1) << "Request ID not found"; | |
| 305 return; | |
| 306 } | |
| 307 WebKit::WebUserMediaRequest user_media_request = it->second; | |
| 308 user_media_requests_.erase(it); | |
| 309 stream_labels_.push_back(label); | |
| 310 | |
| 311 user_media_request.requestSucceeded(source_vector); | |
| 312 } | |
| 313 | |
| 314 void MediaStreamImpl::OnStreamGenerationFailed(int request_id) { | |
| 315 DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed(" | |
| 316 << request_id << ")"; | |
|
scherkus (not reviewing)
2011/11/23 22:52:04
nit: alignment
Henrik Grunell
2011/11/24 11:32:59
Done.
| |
| 317 MediaRequestMap::iterator it = user_media_requests_.find(request_id); | |
| 318 if (it == user_media_requests_.end()) { | |
| 319 DVLOG(1) << "Request ID not found"; | |
| 320 return; | |
| 321 } | |
| 322 WebKit::WebUserMediaRequest user_media_request = it->second; | |
| 323 user_media_requests_.erase(it); | |
| 324 | |
| 325 user_media_request.requestFailed(); | |
| 326 } | |
| 327 | |
| 328 void MediaStreamImpl::OnVideoDeviceFailed(const std::string& label, | |
| 329 int index) { | |
| 330 DVLOG(1) << "MediaStreamImpl::OnVideoDeviceFailed(" | |
| 331 << label << ", " << index << ")"; | |
|
scherkus (not reviewing)
2011/11/23 22:52:04
ditto
Henrik Grunell
2011/11/24 11:32:59
Done.
| |
| 332 // TODO(grunell): Implement. Currently not supported in WebKit. | |
| 333 NOTIMPLEMENTED(); | |
| 334 } | |
| 335 | |
| 336 void MediaStreamImpl::OnAudioDeviceFailed(const std::string& label, | |
| 337 int index) { | |
| 338 DVLOG(1) << "MediaStreamImpl::OnAudioDeviceFailed(" | |
| 339 << label << ", " << index << ")"; | |
|
scherkus (not reviewing)
2011/11/23 22:52:04
ditto
Henrik Grunell
2011/11/24 11:32:59
Done.
| |
| 340 // TODO(grunell): Implement. Currently not supported in WebKit. | |
| 341 NOTIMPLEMENTED(); | |
| 342 } | |
| 343 | |
| 344 void MediaStreamImpl::InitializeWorkerThread(talk_base::Thread** thread, | |
| 345 base::WaitableEvent* event) { | |
| 346 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); | |
| 347 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | |
| 348 *thread = jingle_glue::JingleThreadWrapper::current(); | |
| 349 event->Signal(); | |
| 350 } | |
| OLD | NEW |