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 #ifndef CONTENT_RENDERER_MEDIA_STREAM_MEDIA_STREAM_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_ |
6 #define CONTENT_RENDERER_MEDIA_STREAM_MEDIA_STREAM_IMPL_H_ | 6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_ |
| 7 |
| 8 #include <string> |
7 | 9 |
8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/gtest_prod_util.h" |
9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop_proxy.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
| 17 #include "third_party/libjingle/source/talk/app/webrtc/peerconnection.h" |
| 18 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectionfactory.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamClient.
h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPeerConnection.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
10 #include "webkit/glue/media/media_stream_client.h" | 22 #include "webkit/glue/media/media_stream_client.h" |
11 | 23 |
| 24 namespace base { |
| 25 class WaitableEvent; |
| 26 } |
| 27 |
| 28 namespace content { |
| 29 class IpcNetworkManager; |
| 30 class IpcPacketSocketFactory; |
| 31 class P2PSocketDispatcher; |
| 32 } |
| 33 |
| 34 namespace cricket { |
| 35 class HttpPortAllocator; |
| 36 class WebRtcMediaEngine; |
| 37 } |
| 38 |
| 39 namespace talk_base { |
| 40 class Thread; |
| 41 } |
| 42 |
| 43 namespace WebKit { |
| 44 class WebLocalMediaStream; |
| 45 class WebMediaStreamRecorder; |
| 46 class WebMediaStreamController; |
| 47 class WebMediaStreamTrack; |
| 48 class WebSecurityOrigin; |
| 49 } |
| 50 |
| 51 class MediaStreamDispatcher; |
| 52 class MediaStreamDependencyFactory; |
12 class VideoCaptureImplManager; | 53 class VideoCaptureImplManager; |
13 | 54 class RTCVideoDecoder; |
14 // A implementation of StreamClient to provide supporting functions, such as | 55 |
15 // GetVideoDecoder. | 56 // MediaStreamImpl is a delegate for the Media Stream API messages used by |
| 57 // WebKit. It ties together WebKit, native PeerConnection in libjingle and |
| 58 // MediaStreamManager (via MediaStreamDispatcher and MediaStreamDispatcherHost) |
| 59 // in the browser process. |
16 class MediaStreamImpl | 60 class MediaStreamImpl |
17 : public webkit_glue::MediaStreamClient, | 61 : public WebKit::WebMediaStreamClient, |
| 62 public webkit_glue::MediaStreamClient, |
| 63 public MediaStreamDispatcherEventHandler, |
| 64 public webrtc::PeerConnectionObserver, |
18 public base::RefCountedThreadSafe<MediaStreamImpl> { | 65 public base::RefCountedThreadSafe<MediaStreamImpl> { |
19 public: | 66 public: |
20 explicit MediaStreamImpl(VideoCaptureImplManager* vc_manager); | 67 MediaStreamImpl( |
| 68 MediaStreamDispatcher* media_stream_dispatcher, |
| 69 content::P2PSocketDispatcher* p2p_socket_dispatcher, |
| 70 VideoCaptureImplManager* vc_manager, |
| 71 MediaStreamDependencyFactory* dependency_factory); |
21 virtual ~MediaStreamImpl(); | 72 virtual ~MediaStreamImpl(); |
22 | 73 |
23 // Implement webkit_glue::StreamClient. | 74 // WebKit::WebMediaStreamClient implementation. |
| 75 virtual void setController( |
| 76 WebKit::WebMediaStreamController* controller) OVERRIDE; |
| 77 virtual void shutdown() OVERRIDE; |
| 78 virtual void generateStream( |
| 79 int request_id, |
| 80 WebKit::WebGenerateStreamOptionFlags flags, |
| 81 const WebKit::WebSecurityOrigin& web_security_origin) OVERRIDE; |
| 82 virtual void stopGeneratedStream( |
| 83 const WebKit::WebLocalMediaStream& stream) OVERRIDE; |
| 84 virtual void recordStream( |
| 85 const WebKit::WebMediaStreamRecorder& recorder) OVERRIDE; |
| 86 virtual void getRecordedData( |
| 87 const WebKit::WebMediaStreamRecorder& recorder, |
| 88 int request_id) OVERRIDE; |
| 89 virtual void disposeRecordedData( |
| 90 const WebKit::WebMediaStreamRecorder& recorder) OVERRIDE; |
| 91 virtual void setMediaStreamTrackEnabled( |
| 92 const WebKit::WebMediaStreamTrack& track) OVERRIDE; |
| 93 virtual void processSignalingMessage( |
| 94 const WebKit::WebPeerConnection& web_peer_connection, |
| 95 const WebKit::WebString& message) OVERRIDE; |
| 96 virtual void message( |
| 97 const WebKit::WebPeerConnection& web_peer_connection, |
| 98 const WebKit::WebString& message) OVERRIDE; |
| 99 virtual void addStream( |
| 100 const WebKit::WebPeerConnection& web_peer_connection, |
| 101 const WebKit::WebMediaStream& stream) OVERRIDE; |
| 102 virtual void newPeerConnection( |
| 103 const WebKit::WebPeerConnection& web_peer_connection, |
| 104 const WebKit::WebString& configuration) OVERRIDE; |
| 105 virtual void closePeerConnection( |
| 106 const WebKit::WebPeerConnection& web_peer_connection) OVERRIDE; |
| 107 virtual void startNegotiation( |
| 108 const WebKit::WebPeerConnection& web_peer_connection) OVERRIDE; |
| 109 virtual void removeStream( |
| 110 const WebKit::WebPeerConnection& web_peer_connection, |
| 111 const WebKit::WebMediaStream& stream) OVERRIDE; |
| 112 virtual void commitStreamChanges( |
| 113 const WebKit::WebPeerConnection& web_peer_connection) OVERRIDE; |
| 114 |
| 115 // Implement webkit_glue::MediaStreamClient. |
24 virtual scoped_refptr<media::VideoDecoder> GetVideoDecoder( | 116 virtual scoped_refptr<media::VideoDecoder> GetVideoDecoder( |
25 const GURL& url, media::MessageLoopFactory* message_loop_factory); | 117 const GURL& url, |
| 118 media::MessageLoopFactory* message_loop_factory) OVERRIDE; |
| 119 |
| 120 // Implement MediaStreamDispatcherEventHandler. |
| 121 virtual void OnStreamGenerated( |
| 122 int request_id, |
| 123 const std::string& label, |
| 124 const media_stream::StreamDeviceInfoArray& audio_array, |
| 125 const media_stream::StreamDeviceInfoArray& video_array) OVERRIDE; |
| 126 virtual void OnStreamGenerationFailed(int request_id) OVERRIDE; |
| 127 virtual void OnVideoDeviceFailed( |
| 128 const std::string& label, |
| 129 int index) OVERRIDE; |
| 130 virtual void OnAudioDeviceFailed( |
| 131 const std::string& label, |
| 132 int index) OVERRIDE; |
| 133 |
| 134 // Implement webrtc::PeerConnectionObserver |
| 135 virtual void OnSignalingMessage(const std::string& msg) OVERRIDE; |
| 136 virtual void OnAddStream(const std::string& stream_id, bool video) OVERRIDE; |
| 137 virtual void OnRemoveStream( |
| 138 const std::string& stream_id, |
| 139 bool video) OVERRIDE; |
26 | 140 |
27 private: | 141 private: |
| 142 FRIEND_TEST(MediaStreamImplTest, Basic); |
| 143 |
| 144 void OnAddStreamCallback(const std::string& stream_label); |
| 145 void OnRemoveStreamCallback(const std::string& stream_label); |
| 146 |
| 147 void InitializeWorkerThread( |
| 148 talk_base::Thread** thread, |
| 149 base::WaitableEvent* event); |
| 150 |
| 151 // The controller_ is valid for the lifetime of the underlying |
| 152 // WebCore::WebMediaStreamController. shutdown() is invoked when the |
| 153 // controller is destroyed. Additionally, MediaStreamImpl has the same |
| 154 // lifetime as the controller since it is owned by RenderView. |
| 155 scoped_ptr<WebKit::WebMediaStreamController> controller_; |
| 156 |
| 157 // dependency_factory_ is owned by us. |
| 158 scoped_ptr<MediaStreamDependencyFactory> dependency_factory_; |
| 159 |
| 160 // media_stream_dispatcher_ is a weak reference, owned by RenderView. It's |
| 161 // valid for the lifetime of RenderView. |
| 162 MediaStreamDispatcher* media_stream_dispatcher_; |
| 163 |
| 164 // The media_engine_ is owned by pc_factory_ and is valid for the lifetime of |
| 165 // pc_factory_. |
| 166 cricket::WebRtcMediaEngine* media_engine_; |
| 167 |
| 168 // p2p_socket_dispatcher_ is a weak reference, owned by RenderView. It's valid |
| 169 // for the lifetime of RenderView. |
| 170 content::P2PSocketDispatcher* p2p_socket_dispatcher_; |
| 171 |
28 scoped_refptr<VideoCaptureImplManager> vc_manager_; | 172 scoped_refptr<VideoCaptureImplManager> vc_manager_; |
| 173 scoped_ptr<content::IpcNetworkManager> ipc_network_manager_; |
| 174 scoped_ptr<content::IpcPacketSocketFactory> ipc_socket_factory_; |
| 175 |
| 176 // TODO(grunell): Support several PeerConnections. |
| 177 // web_peer_connection_ is WebKit's PeerConnection object. |
| 178 // native_peer_connection_ is the native PeerConnection object, |
| 179 // it handles the ICE processing and media engine. |
| 180 WebKit::WebPeerConnection web_peer_connection_; |
| 181 scoped_ptr<webrtc::PeerConnection> native_peer_connection_; |
| 182 |
| 183 scoped_refptr<RTCVideoDecoder> rtc_video_decoder_; |
| 184 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 185 |
| 186 // PeerConnection threads. signaling_thread_ is created from the |
| 187 // "current" chrome thread. |
| 188 talk_base::Thread* signaling_thread_; |
| 189 talk_base::Thread* worker_thread_; |
| 190 base::Thread chrome_worker_thread_; |
| 191 |
| 192 // Currently, a stream in WebKit has audio and/or video and has one label. |
| 193 // Local and remote streams have different labels. |
| 194 // In native PeerConnection, a stream has audio or video (not both), and they |
| 195 // have separate labels. A remote stream has the same label as the |
| 196 // corresponding local stream. Hence the workarounds in the implementation to |
| 197 // handle this. It could look like this: |
| 198 // WebKit: Local, audio and video: label "foo". |
| 199 // Remote, audio and video: label "foo-remote". |
| 200 // Libjingle: Local and remote, audio: label "foo-audio". |
| 201 // Local and remote, video: label "foo". |
| 202 // TODO(grunell): This shall be removed or changed when native PeerConnection |
| 203 // has been updated to closer follow the specification. |
| 204 std::string local_label_; // Label used in WebKit |
| 205 std::string remote_label_; // Label used in WebKit |
| 206 |
| 207 // Call states. Possible transitions: |
| 208 // NOT_STARTED -> INITIATING -> SENDING_AND_RECEIVING |
| 209 // NOT_STARTED -> RECEIVING |
| 210 // RECEIVING -> NOT_STARTED |
| 211 // RECEIVING -> SENDING_AND_RECEIVING |
| 212 // SENDING_AND_RECEIVING -> NOT_STARTED |
| 213 // Note that when in state SENDING_AND_RECEIVING, the other side may or may |
| 214 // not send media. Thus, this state does not necessarily mean full duplex. |
| 215 // TODO(grunell): This shall be removed or changed when native PeerConnection |
| 216 // has been updated to closer follow the specification. |
| 217 enum CallState { |
| 218 NOT_STARTED, |
| 219 INITIATING, |
| 220 RECEIVING, |
| 221 SENDING_AND_RECEIVING |
| 222 }; |
| 223 CallState call_state_; |
| 224 |
| 225 // Make sure we only create the video capture module once. This is also |
| 226 // temporary and will be handled differently when several PeerConnections |
| 227 // and/or streams is supported. |
| 228 // TODO(grunell): This shall be removed or changed when native PeerConnection |
| 229 // has been updated to closer follow the specification. |
| 230 bool vcm_created_; |
29 | 231 |
30 DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl); | 232 DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl); |
31 }; | 233 }; |
32 | 234 |
33 #endif // CONTENT_RENDERER_MEDIA_STREAM_MEDIA_STREAM_IMPL_H_ | 235 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_ |
OLD | NEW |