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 <list> |
| 9 #include <map> |
| 10 #include <string> |
7 | 11 |
8 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" |
9 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop_proxy.h" |
| 17 #include "base/threading/non_thread_safe.h" |
| 18 #include "base/threading/thread.h" |
| 19 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebUserMediaClient.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebUserMediaRequest.h
" |
10 #include "webkit/media/media_stream_client.h" | 22 #include "webkit/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 WebRtcMediaEngine; |
| 36 } |
| 37 |
| 38 namespace talk_base { |
| 39 class Thread; |
| 40 } |
| 41 |
| 42 namespace WebKit { |
| 43 class WebPeerConnectionHandler; |
| 44 class WebPeerConnectionHandlerClient; |
| 45 } |
| 46 |
| 47 class MediaStreamDispatcher; |
| 48 class MediaStreamDependencyFactory; |
| 49 class PeerConnectionHandler; |
12 class VideoCaptureImplManager; | 50 class VideoCaptureImplManager; |
| 51 class RTCVideoDecoder; |
13 | 52 |
14 // A implementation of StreamClient to provide supporting functions, such as | 53 // MediaStreamImpl is a delegate for the Media Stream API messages used by |
15 // GetVideoDecoder. | 54 // WebKit. It ties together WebKit, native PeerConnection in libjingle and |
| 55 // MediaStreamManager (via MediaStreamDispatcher and MediaStreamDispatcherHost) |
| 56 // in the browser process. It must be created, called and destroyed on the |
| 57 // render thread. |
16 class MediaStreamImpl | 58 class MediaStreamImpl |
17 : public webkit_media::MediaStreamClient, | 59 : public WebKit::WebUserMediaClient, |
| 60 public webkit_media::MediaStreamClient, |
| 61 public MediaStreamDispatcherEventHandler, |
| 62 public base::NonThreadSafe, |
18 public base::RefCountedThreadSafe<MediaStreamImpl> { | 63 public base::RefCountedThreadSafe<MediaStreamImpl> { |
19 public: | 64 public: |
20 explicit MediaStreamImpl(VideoCaptureImplManager* vc_manager); | 65 MediaStreamImpl( |
| 66 MediaStreamDispatcher* media_stream_dispatcher, |
| 67 content::P2PSocketDispatcher* p2p_socket_dispatcher, |
| 68 VideoCaptureImplManager* vc_manager, |
| 69 MediaStreamDependencyFactory* dependency_factory); |
21 virtual ~MediaStreamImpl(); | 70 virtual ~MediaStreamImpl(); |
22 | 71 |
23 // Implement webkit_media::StreamClient. | 72 virtual WebKit::WebPeerConnectionHandler* CreatePeerConnectionHandler( |
| 73 WebKit::WebPeerConnectionHandlerClient* client); |
| 74 virtual void ClosePeerConnection(); |
| 75 |
| 76 // Returns true if created successfully or already exists, false otherwise. |
| 77 virtual bool SetVideoCaptureModule(const std::string& label); |
| 78 |
| 79 // WebKit::WebUserMediaClient implementation |
| 80 virtual void requestUserMedia( |
| 81 const WebKit::WebUserMediaRequest& user_media_request, |
| 82 const WebKit::WebVector<WebKit::WebMediaStreamSource>& |
| 83 media_stream_source_vector) OVERRIDE; |
| 84 virtual void cancelUserMediaRequest( |
| 85 const WebKit::WebUserMediaRequest& user_media_request) OVERRIDE; |
| 86 |
| 87 // webkit_media::MediaStreamClient implementation. |
24 virtual scoped_refptr<media::VideoDecoder> GetVideoDecoder( | 88 virtual scoped_refptr<media::VideoDecoder> GetVideoDecoder( |
25 const GURL& url, | 89 const GURL& url, |
26 media::MessageLoopFactory* message_loop_factory) OVERRIDE; | 90 media::MessageLoopFactory* message_loop_factory) OVERRIDE; |
27 | 91 |
| 92 // MediaStreamDispatcherEventHandler implementation. |
| 93 virtual void OnStreamGenerated( |
| 94 int request_id, |
| 95 const std::string& label, |
| 96 const media_stream::StreamDeviceInfoArray& audio_array, |
| 97 const media_stream::StreamDeviceInfoArray& video_array) OVERRIDE; |
| 98 virtual void OnStreamGenerationFailed(int request_id) OVERRIDE; |
| 99 virtual void OnVideoDeviceFailed( |
| 100 const std::string& label, |
| 101 int index) OVERRIDE; |
| 102 virtual void OnAudioDeviceFailed( |
| 103 const std::string& label, |
| 104 int index) OVERRIDE; |
| 105 |
28 private: | 106 private: |
| 107 FRIEND_TEST_ALL_PREFIXES(MediaStreamImplTest, Basic); |
| 108 |
| 109 void InitializeWorkerThread( |
| 110 talk_base::Thread** thread, |
| 111 base::WaitableEvent* event); |
| 112 void DeleteIpcNetworkManager(); |
| 113 |
| 114 scoped_ptr<MediaStreamDependencyFactory> dependency_factory_; |
| 115 |
| 116 // media_stream_dispatcher_ is a weak reference, owned by RenderView. It's |
| 117 // valid for the lifetime of RenderView. |
| 118 MediaStreamDispatcher* media_stream_dispatcher_; |
| 119 |
| 120 // media_engine_ is owned by PeerConnectionFactory (which is owned by |
| 121 // dependency_factory_) and is valid for the lifetime of |
| 122 // PeerConnectionFactory. |
| 123 cricket::WebRtcMediaEngine* media_engine_; |
| 124 |
| 125 // p2p_socket_dispatcher_ is a weak reference, owned by RenderView. It's valid |
| 126 // for the lifetime of RenderView. |
| 127 content::P2PSocketDispatcher* p2p_socket_dispatcher_; |
| 128 |
| 129 // We own network_manager_, must be deleted on the worker thread. |
| 130 content::IpcNetworkManager* network_manager_; |
| 131 |
| 132 scoped_ptr<content::IpcPacketSocketFactory> socket_factory_; |
| 133 |
29 scoped_refptr<VideoCaptureImplManager> vc_manager_; | 134 scoped_refptr<VideoCaptureImplManager> vc_manager_; |
30 | 135 |
| 136 // peer_connection_handler_ is a weak reference, owned by WebKit. It's valid |
| 137 // until stop is called on it (which will call ClosePeerConnection on us). |
| 138 // TODO(grunell): Support several PeerConnectionsHandlers. |
| 139 PeerConnectionHandler* peer_connection_handler_; |
| 140 |
| 141 scoped_refptr<RTCVideoDecoder> rtc_video_decoder_; |
| 142 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 143 |
| 144 // PeerConnection threads. signaling_thread_ is created from the |
| 145 // "current" chrome thread. |
| 146 talk_base::Thread* signaling_thread_; |
| 147 talk_base::Thread* worker_thread_; |
| 148 base::Thread chrome_worker_thread_; |
| 149 |
| 150 static int next_request_id_; |
| 151 typedef std::map<int, WebKit::WebUserMediaRequest> MediaRequestMap; |
| 152 MediaRequestMap user_media_requests_; |
| 153 |
| 154 std::list<std::string> stream_labels_; |
| 155 |
| 156 // Make sure we only create the video capture module once. This is also |
| 157 // temporary and will be handled differently when several PeerConnections |
| 158 // and/or streams is supported. |
| 159 // TODO(grunell): This shall be removed or changed when native PeerConnection |
| 160 // has been updated to closer follow the specification. |
| 161 bool vcm_created_; |
| 162 |
31 DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl); | 163 DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl); |
32 }; | 164 }; |
33 | 165 |
34 #endif // CONTENT_RENDERER_MEDIA_STREAM_MEDIA_STREAM_IMPL_H_ | 166 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_ |
OLD | NEW |