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; |
scherkus (not reviewing)
2011/11/09 02:29:10
out of curiosity where do these numbers come from?
Henrik Grunell
2011/11/09 20:36:04
It's the CIF resolution. They are used for the loc
| |
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, |
37 content::P2PSocketDispatcher* p2p_socket_dispatcher, | |
38 VideoCaptureImplManager* vc_manager, | |
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) { | |
29 } | 50 } |
30 | 51 |
31 MediaStreamImpl::~MediaStreamImpl() {} | 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 delete media_engine_; | |
tommi (sloooow) - chröme
2011/11/09 10:45:48
scoped_ptr?
Henrik Grunell
2011/11/09 20:36:04
No, since we give ownership to PeerConnectionFacto
| |
79 media_engine_ = NULL; | |
80 signaling_thread_ = NULL; | |
81 return NULL; | |
82 } | |
83 } | |
84 base::WaitableEvent event(true, false); | |
85 chrome_worker_thread_.message_loop()->PostTask( | |
86 FROM_HERE, | |
87 base::Bind(&MediaStreamImpl::InitializeWorkerThread, this, | |
88 &worker_thread_, &event)); | |
89 event.Wait(); | |
90 DCHECK(worker_thread_); | |
91 } | |
92 | |
93 if (!dependency_factory_->PeerConnectionFactoryCreated()) { | |
94 ipc_network_manager_.reset( | |
95 new content::IpcNetworkManager(p2p_socket_dispatcher_)); | |
96 ipc_socket_factory_.reset( | |
97 new content::IpcPacketSocketFactory(p2p_socket_dispatcher_)); | |
98 cricket::HttpPortAllocator* port_allocator = new cricket::HttpPortAllocator( | |
99 ipc_network_manager_.get(), ipc_socket_factory_.get(), | |
100 "PeerConnection"); | |
101 // TODO(mallinath): The following flags were added to solve a crash in | |
102 // HttpClient, we should probably remove them after that issue has been | |
103 // investigated. | |
104 port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | | |
105 cricket::PORTALLOCATOR_DISABLE_RELAY); | |
106 | |
107 // TODO(mallinath): PeerConnectionFactory constructor changed in latest | |
108 // code and it no more accepts config string. Config string must be parsed | |
109 // here and set in HttpPortAllocator. Now using standard google STUN server | |
110 // address. | |
111 std::vector<talk_base::SocketAddress> stun_hosts; | |
112 stun_hosts.push_back(talk_base::SocketAddress("stun.l.google.com", 19302)); | |
113 port_allocator->SetStunHosts(stun_hosts); | |
114 | |
115 if (!dependency_factory_->CreatePeerConnectionFactory(port_allocator, | |
116 media_engine_, | |
117 worker_thread_)) { | |
118 LOG(ERROR) << "Could not initialize PeerConnection factory"; | |
119 return NULL; | |
120 } | |
121 } | |
122 | |
123 peer_connection_handler_.reset(new PeerConnectionHandler( | |
124 client, | |
125 this, | |
126 dependency_factory_.get(), | |
127 signaling_thread_)); | |
128 | |
129 return peer_connection_handler_.get(); | |
130 } | |
131 | |
132 void MediaStreamImpl::ClosePeerConnection() { | |
133 rtc_video_decoder_ = NULL; | |
134 media_engine_->SetVideoCaptureModule(NULL); | |
135 vcm_created_ = false; | |
136 peer_connection_handler_.reset(); | |
137 } | |
138 | |
139 bool MediaStreamImpl::SetVideoCaptureModule(const std::string& label) { | |
140 if (vcm_created_) | |
141 return true; | |
142 // Set the capture device. | |
143 // TODO(grunell): Instead of using the first track, the selected track | |
144 // should be used. | |
145 int id = media_stream_dispatcher_->video_session_id(label, 0); | |
146 if (id == media_stream::StreamDeviceInfo::kNoId) | |
147 return false; | |
148 webrtc::VideoCaptureModule* vcm = | |
149 new VideoCaptureModuleImpl(id, vc_manager_.get()); | |
150 vcm_created_ = true; | |
151 media_engine_->SetVideoCaptureModule(vcm); | |
152 return true; | |
153 } | |
32 | 154 |
33 scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( | 155 scoped_refptr<media::VideoDecoder> MediaStreamImpl::GetVideoDecoder( |
34 const GURL& url, media::MessageLoopFactory* message_loop_factory) { | 156 const GURL& url, |
35 bool raw_media = (url.spec().find(kRawMediaScheme) == 0); | 157 media::MessageLoopFactory* message_loop_factory) { |
36 media::VideoDecoder* decoder = NULL; | 158 // TODO(grunell): Get label from media stream registry in WebKit when re-added |
37 if (raw_media) { | 159 // there. This will fail meanwhile. |
160 std::string label; | |
161 if (label.empty()) | |
162 return NULL; // This is not a valid stream. | |
163 | |
164 scoped_refptr<media::VideoDecoder> decoder; | |
165 if (media_stream_dispatcher_->IsStream(label)) { | |
166 // It's a local stream. | |
167 int video_session_id = media_stream_dispatcher_->video_session_id(label, 0); | |
38 media::VideoCapture::VideoCaptureCapability capability; | 168 media::VideoCapture::VideoCaptureCapability capability; |
39 capability.width = kVideoCaptureWidth; | 169 capability.width = kVideoCaptureWidth; |
40 capability.height = kVideoCaptureHeight; | 170 capability.height = kVideoCaptureHeight; |
41 capability.max_fps = kVideoCaptureFramePerSecond; | 171 capability.max_fps = kVideoCaptureFramePerSecond; |
42 capability.expected_capture_delay = 0; | 172 capability.expected_capture_delay = 0; |
43 capability.raw_type = media::VideoFrame::I420; | 173 capability.raw_type = media::VideoFrame::I420; |
44 capability.interlaced = false; | 174 capability.interlaced = false; |
175 decoder = new CaptureVideoDecoder( | |
176 message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoderThread"), | |
177 video_session_id, | |
178 vc_manager_.get(), | |
179 capability); | |
180 } else { | |
181 // It's a remote stream. | |
182 size_t found = label.rfind("-remote"); | |
183 if (found != std::string::npos) | |
184 label = label.substr(0, found); | |
185 if (rtc_video_decoder_.get()) { | |
186 // The renderer is used by PeerConnection, release it first. | |
187 if (peer_connection_handler_.get()) | |
188 peer_connection_handler_->SetVideoRenderer(label, NULL); | |
45 | 189 |
46 decoder = new CaptureVideoDecoder( | 190 } |
47 message_loop_factory->GetMessageLoopProxy("CaptureVideoDecoder").get(), | 191 rtc_video_decoder_ = new RTCVideoDecoder( |
48 kStartOpenSessionId, vc_manager_.get(), capability); | 192 message_loop_factory->GetMessageLoop("RtcVideoDecoderThread"), |
193 url.spec()); | |
194 decoder = rtc_video_decoder_; | |
195 if (peer_connection_handler_.get()) | |
196 peer_connection_handler_->SetVideoRenderer(label, rtc_video_decoder_); | |
49 } | 197 } |
50 return decoder; | 198 return decoder; |
51 } | 199 } |
200 | |
201 void MediaStreamImpl::OnStreamGenerated( | |
202 int request_id, | |
203 const std::string& label, | |
204 const media_stream::StreamDeviceInfoArray& audio_array, | |
205 const media_stream::StreamDeviceInfoArray& video_array) { | |
206 // TODO(grunell): Implement when available in WebKit. | |
207 NOTIMPLEMENTED(); | |
208 } | |
209 | |
210 void MediaStreamImpl::OnStreamGenerationFailed(int request_id) { | |
211 DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed(" | |
212 << request_id << ")"; | |
213 // TODO(grunell): Implement when available in WebKit. | |
214 NOTIMPLEMENTED(); | |
215 } | |
216 | |
217 void MediaStreamImpl::OnVideoDeviceFailed(const std::string& label, | |
218 int index) { | |
219 DVLOG(1) << "MediaStreamImpl::OnVideoDeviceFailed(" | |
220 << label << ", " << index << ")"; | |
221 // TODO(grunell): Implement when available in WebKit. | |
222 NOTIMPLEMENTED(); | |
223 } | |
224 | |
225 void MediaStreamImpl::OnAudioDeviceFailed(const std::string& label, | |
226 int index) { | |
227 DVLOG(1) << "MediaStreamImpl::OnAudioDeviceFailed(" | |
228 << label << ", " << index << ")"; | |
229 // TODO(grunell): Implement when available in WebKit. | |
230 NOTIMPLEMENTED(); | |
231 } | |
232 | |
233 void MediaStreamImpl::InitializeWorkerThread(talk_base::Thread** thread, | |
234 base::WaitableEvent* event) { | |
235 jingle_glue::JingleThreadWrapper::EnsureForCurrentThread(); | |
236 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | |
237 *thread = jingle_glue::JingleThreadWrapper::current(); | |
238 event->Signal(); | |
239 } | |
OLD | NEW |