Index: content/renderer/media/media_stream_impl.cc |
diff --git a/content/renderer/media/media_stream_impl.cc b/content/renderer/media/media_stream_impl.cc |
index 7573e78c92a41037accf09060e1e5548fc4f6ce4..931d2de7481d16543863d4e84e1142ec630bba96 100644 |
--- a/content/renderer/media/media_stream_impl.cc |
+++ b/content/renderer/media/media_stream_impl.cc |
@@ -4,12 +4,25 @@ |
#include "content/renderer/media/media_stream_impl.h" |
-#include "base/string_util.h" |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "base/synchronization/waitable_event.h" |
#include "content/renderer/media/capture_video_decoder.h" |
+#include "content/renderer/media/media_stream_dependency_factory.h" |
+#include "content/renderer/media/media_stream_dispatcher.h" |
+#include "content/renderer/media/peer_connection_handler.h" |
+#include "content/renderer/media/rtc_video_decoder.h" |
#include "content/renderer/media/video_capture_impl_manager.h" |
-#include "googleurl/src/gurl.h" |
+#include "content/renderer/media/video_capture_module_impl.h" |
+#include "content/renderer/media/webrtc_audio_device_impl.h" |
+#include "content/renderer/p2p/ipc_network_manager.h" |
+#include "content/renderer/p2p/ipc_socket_factory.h" |
+#include "content/renderer/p2p/socket_dispatcher.h" |
+#include "jingle/glue/thread_wrapper.h" |
#include "media/base/message_loop_factory.h" |
-#include "media/base/pipeline.h" |
+#include "third_party/libjingle/source/talk/p2p/client/httpportallocator.h" |
+#include "third_party/libjingle/source/talk/session/phone/dummydevicemanager.h" |
+#include "third_party/libjingle/source/talk/session/phone/webrtcmediaengine.h" |
namespace { |
@@ -17,24 +30,211 @@ static const int kVideoCaptureWidth = 352; |
static const int kVideoCaptureHeight = 288; |
static const int kVideoCaptureFramePerSecond = 30; |
-static const int kStartOpenSessionId = 1; |
+} // namespace |
-// TODO(wjia): remove this string when full media stream code is checked in. |
-static const char kRawMediaScheme[] = "mediastream"; |
+MediaStreamImpl::MediaStreamImpl( |
+ MediaStreamDispatcher* media_stream_dispatcher, |
+ content::P2PSocketDispatcher* p2p_socket_dispatcher, |
+ VideoCaptureImplManager* vc_manager, |
+ MediaStreamDependencyFactory* dependency_factory) |
+ : dependency_factory_(dependency_factory), |
+ media_stream_dispatcher_(media_stream_dispatcher), |
+ media_engine_(NULL), |
+ p2p_socket_dispatcher_(p2p_socket_dispatcher), |
+ vc_manager_(vc_manager), |
+ message_loop_proxy_(base::MessageLoopProxy::current()), |
+ signaling_thread_(NULL), |
+ worker_thread_(NULL), |
+ chrome_worker_thread_("Chrome_libJingle_WorkerThread"), |
+ vcm_created_(false) { |
+} |
-} // namespace |
+MediaStreamImpl::~MediaStreamImpl() { |
+ if (dependency_factory_.get()) |
+ dependency_factory_->DeletePeerConnectionFactory(); |
+} |
+ |
+WebKit::WebPeerConnectionHandler* MediaStreamImpl::CreatePeerConnectionHandler( |
+ WebKit::WebPeerConnectionHandlerClient* client) { |
+ if (peer_connection_handler_.get()) { |
+ VLOG(1) << "A PeerConnection already exists"; |
+ return NULL; |
+ } |
+ |
+ if (!media_engine_) { |
+ media_engine_ = dependency_factory_->CreateWebRtcMediaEngine(); |
+ } |
+ |
+ if (!signaling_thread_) { |
+ jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); |
+ jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
+ signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); |
+ } |
+ |
+ if (!worker_thread_) { |
+ if (!chrome_worker_thread_.IsRunning()) { |
+ if (!chrome_worker_thread_.Start()) { |
+ LOG(ERROR) << "Could not start worker thread"; |
+ 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.
|
+ } |
+ } |
+ base::WaitableEvent event(true, false); |
+ chrome_worker_thread_.message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&MediaStreamImpl::InitializeWorkerThread, this, |
+ &worker_thread_, &event)); |
+ event.Wait(); |
+ DCHECK(worker_thread_); |
+ } |
+ |
+ if (!dependency_factory_->PeerConnectionFactoryCreated()) { |
+ ipc_network_manager_.reset( |
+ new content::IpcNetworkManager(p2p_socket_dispatcher_)); |
+ ipc_socket_factory_.reset( |
+ new content::IpcPacketSocketFactory(p2p_socket_dispatcher_)); |
+ cricket::HttpPortAllocator* port_allocator = new cricket::HttpPortAllocator( |
+ ipc_network_manager_.get(), ipc_socket_factory_.get(), |
+ "PeerConnection"); |
+ // TODO(mallinath): The following flags were added to solve a crash in |
+ // HttpClient, we should probably remove them after that issue has been |
+ // investigated. |
+ port_allocator->set_flags( |
+ cricket::PORTALLOCATOR_DISABLE_TCP |
+ | 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.
|
+ |
+ // TODO(mallinath): PeerConnectionFactory constructor changed in latest |
+ // code and it no more accepts config string. Config string must be parsed |
+ // here and set in HttpPortAllocator. Now using standard google STUN server |
+ // address. |
+ std::vector<talk_base::SocketAddress> stun_hosts; |
+ stun_hosts.push_back(talk_base::SocketAddress("stun.l.google.com", 19302)); |
+ port_allocator->SetStunHosts(stun_hosts); |
+ |
+ if (!dependency_factory_->CreatePeerConnectionFactory(port_allocator, |
+ media_engine_, |
+ worker_thread_)) { |
+ LOG(ERROR) |
+ << __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.
|
+ return NULL; |
+ } |
+ } |
+ |
+ peer_connection_handler_.reset(new PeerConnectionHandler( |
+ client, |
+ this, |
+ dependency_factory_.get(), |
+ signaling_thread_)); |
+ |
+ return peer_connection_handler_.get(); |
+} |
-MediaStreamImpl::MediaStreamImpl(VideoCaptureImplManager* vc_manager) |
- : vc_manager_(vc_manager) { |
+void MediaStreamImpl::ClosePeerConnection() { |
+ rtc_video_decoder_ = NULL; |
+ media_engine_->SetVideoCaptureModule(NULL); |
+ vcm_created_ = false; |
+ peer_connection_handler_.reset(); |
} |
-MediaStreamImpl::~MediaStreamImpl() {} |
+bool MediaStreamImpl::SetVideoCaptureModule(const std::string label) { |
+ if (vcm_created_) |
+ return true; |
+ // Set the capture device. |
+ // TODO(grunell): Instead of using the first track, the selected track |
+ // should be used. |
+ int id = media_stream_dispatcher_->video_session_id(label, 0); |
+ if (id == media_stream::StreamDeviceInfo::kNoId) |
+ return false; |
+ webrtc::VideoCaptureModule* vcm = |
+ new VideoCaptureModuleImpl(id, vc_manager_.get()); |
+ vcm_created_ = true; |
+ media_engine_->SetVideoCaptureModule(vcm); |
+ return true; |
+} |
+ |
+// TODO(grunell): Enable and update implementation when available in WebKit. |
+//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
|
+// int request_id, |
+// WebKit::WebGenerateStreamOptionFlags flags, |
+// const WebKit::WebSecurityOrigin& web_security_origin) { |
+// bool audio = (flags & WebKit::WebGenerateStreamRequestAudio) != 0; |
+// media_stream::StreamOptions::VideoOption video_option = |
+// media_stream::StreamOptions::kNoCamera; |
+// if ((flags & WebKit::WebGenerateStreamRequestVideoFacingUser) && |
+// (flags & WebKit::WebGenerateStreamRequestVideoFacingEnvironment)) { |
+// video_option = media_stream::StreamOptions::kFacingBoth; |
+// } else { |
+// if (flags & WebKit::WebGenerateStreamRequestVideoFacingEnvironment) |
+// video_option = media_stream::StreamOptions::kFacingEnvironment; |
+// if (flags & WebKit::WebGenerateStreamRequestVideoFacingUser) |
+// video_option = media_stream::StreamOptions::kFacingUser; |
+// } |
+// DVLOG(1) << "MediaStreamImpl::generateStream(" |
+// << request_id << ", [ " |
+// << (audio ? "audio " : "") |
+// << ((flags & WebKit::WebGenerateStreamRequestVideoFacingUser) ? |
+// "video_facing_user " : "") |
+// << ((flags & |
+// WebKit::WebGenerateStreamRequestVideoFacingEnvironment) ? |
+// "video_facing_environment " : "") |
+// << "], " |
+// << static_cast<string16>(web_security_origin.toString()) << ")"; |
+// |
+// media_stream_dispatcher_->GenerateStream(request_id, this, |
+// media_stream::StreamOptions(audio, video_option), |
+// UTF16ToUTF8(web_security_origin.toString())); |
+//} |
+ |
+// TODO(grunell): Enable and update implementation when available in WebKit. |
+//void MediaStreamImpl::stopGeneratedStream( |
+// const WebKit::WebLocalMediaStream& stream) { |
+// std::string label = UTF16ToUTF8(stream.label()); |
+// media_stream_dispatcher_->StopStream(label); |
+//} |
+ |
+// TODO(grunell): Enable and update implementation when available in WebKit. |
+//void MediaStreamImpl::recordStream( |
+// const WebKit::WebMediaStreamRecorder& recorder) { |
+// // TODO(grunell): Implement. |
+// NOTIMPLEMENTED(); |
+//} |
+ |
+// TODO(grunell): Enable and update implementation when available in WebKit. |
+//void MediaStreamImpl::getRecordedData( |
+// const WebKit::WebMediaStreamRecorder& recorder, |
+// int request_id) { |
+// // TODO(grunell): Implement. |
+// NOTIMPLEMENTED(); |
+//} |
+ |
+// TODO(grunell): Enable and update implementation when available in WebKit. |
+//void MediaStreamImpl::disposeRecordedData( |
+// const WebKit::WebMediaStreamRecorder& recorder) { |
+// // TODO(grunell): Implement. |
+// NOTIMPLEMENTED(); |
+//} |
+ |
+// TODO(grunell): Enable and update implementation when available in WebKit. |
+//void MediaStreamImpl::setMediaStreamTrackEnabled( |
+// const WebKit::WebMediaStreamTrack& track) { |
+// // TODO(grunell): Implement. |
+// NOTIMPLEMENTED(); |
+//} |
scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( |
- const GURL& url, media::MessageLoopFactory* message_loop_factory) { |
- bool raw_media = (url.spec().find(kRawMediaScheme) == 0); |
- media::VideoDecoder* decoder = NULL; |
- if (raw_media) { |
+ const GURL& url, |
+ media::MessageLoopFactory* message_loop_factory) { |
+ // TODO(grunell): Enable when this has been re-added in WebKit. This will fail |
+ // meanwhile. |
+ std::string label; |
+// std::string label = |
tommi (sloooow) - chröme
2011/11/08 12:27:24
remove?
Henrik Grunell
2011/11/08 22:06:41
Done.
|
+// UTF16ToUTF8(WebKit::WebMediaStreamRegistry::mediaStreamLabel(url)); |
+ if (label.empty()) |
+ return NULL; // This is not a valid stream. |
+ |
+ scoped_refptr<media::VideoDecoder> decoder; |
+ if (media_stream_dispatcher_->IsStream(label)) { |
+ // It's a local stream. |
+ int video_session_id = media_stream_dispatcher_->video_session_id(label, 0); |
media::VideoCapture::VideoCaptureCapability capability; |
capability.width = kVideoCaptureWidth; |
capability.height = kVideoCaptureHeight; |
@@ -42,10 +242,103 @@ scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( |
capability.expected_capture_delay = 0; |
capability.raw_type = media::VideoFrame::I420; |
capability.interlaced = false; |
- |
decoder = new CaptureVideoDecoder( |
- message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoder").get(), |
- kStartOpenSessionId, vc_manager_.get(), capability); |
+ message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoderThread"), |
+ video_session_id, |
+ vc_manager_.get(), |
+ capability); |
+ } else { |
+ // It's a remote stream. |
+ size_t found = label.rfind("-remote"); |
+ if (found != std::string::npos) |
+ label = label.substr(0, found); |
+ if (rtc_video_decoder_.get()) { |
+ // The renderer is used by PeerConnection, release it first. |
+ // TODO(grunell): Call the pc handler. |
+// 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.
|
+// native_peer_connection_->SetVideoRenderer(label, NULL); |
+ } |
+ rtc_video_decoder_ = new RTCVideoDecoder( |
+ message_loop_factory->GetMessageLoop("RtcVideoDecoderThread"), |
+ url.spec()); |
+ decoder = rtc_video_decoder_; |
+ // TODO(grunell): Call the pc handler. |
+// if (native_peer_connection_.get()) |
+// native_peer_connection_->SetVideoRenderer(label, rtc_video_decoder_); |
} |
return decoder; |
} |
+ |
+void MediaStreamImpl::OnStreamGenerated( |
+ int request_id, |
+ const std::string& label, |
+ const media_stream::StreamDeviceInfoArray& audio_array, |
+ const media_stream::StreamDeviceInfoArray& video_array) { |
+ // TODO(grunell): Enable and update implementation when available in WebKit. |
+ NOTIMPLEMENTED(); |
+// 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.
|
+// audio_array.size() + video_array.size()); |
+// |
+// WebKit::WebString track_id(WebKit::WebString::fromUTF8("")); |
+// WebKit::WebString track_kind(WebKit::WebString::fromUTF8("main")); |
+// WebKit::WebString track_label_audio( |
+// WebKit::WebString::fromUTF8("AudioDevice")); |
+// WebKit::WebString track_label_video( |
+// WebKit::WebString::fromUTF8("VideoCapture")); |
+// size_t track_num = web_track_vector.size(); |
+// while (track_num--) { |
+// if (track_num < audio_array.size()) { |
+// web_track_vector[track_num].initialize(track_id, |
+// track_kind, |
+// track_label_audio); |
+// } else { |
+// web_track_vector[track_num].initialize(track_id, |
+// track_kind, |
+// track_label_video); |
+// } |
+// } |
+// |
+// WebKit::WebMediaStreamTrackList web_track_list; |
+// web_track_list.initialize(web_track_vector); |
+// |
+// WebKit::WebLocalMediaStream web_local_media_stream; |
+// web_local_media_stream.initialize(UTF8ToUTF16(label), web_track_list); |
+// |
+// controller_->streamGenerated(request_id, web_local_media_stream); |
+} |
+ |
+void MediaStreamImpl::OnStreamGenerationFailed(int request_id) { |
+ DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed(" |
+ << request_id << ")"; |
+ // TODO(grunell): Enable and update implementation when available in WebKit. |
+ NOTIMPLEMENTED(); |
+// controller_->streamGenerationFailed( |
+// request_id, |
+// WebKit::WebMediaStreamController::ErrorPermissionDenied); |
+} |
+ |
+void MediaStreamImpl::OnVideoDeviceFailed(const std::string& label, |
+ int index) { |
+ // TODO(grunell): Enable and update implementation when available in WebKit. |
+ NOTIMPLEMENTED(); |
+ DVLOG(1) << "MediaStreamImpl::OnVideoDeviceFailed(" |
+ << label << ", " << index << ")"; |
+// controller_->streamFailed(UTF8ToUTF16(label)); |
+} |
+ |
+void MediaStreamImpl::OnAudioDeviceFailed(const std::string& label, |
+ int index) { |
+ DVLOG(1) << "MediaStreamImpl::OnAudioDeviceFailed(" |
+ << label << ", " << index << ")"; |
+ // TODO(grunell): Enable and update implementation when available in WebKit. |
+ NOTIMPLEMENTED(); |
+// controller_->streamFailed(UTF8ToUTF16(label)); |
+} |
+ |
+void MediaStreamImpl::InitializeWorkerThread(talk_base::Thread** thread, |
+ base::WaitableEvent* event) { |
+ jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); |
+ jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
+ *thread = jingle_glue::JingleThreadWrapper::current(); |
+ event->Signal(); |
+} |