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