Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_ |
| 6 | #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/threading/thread.h" |
| 13 | #include "content/common/content_export.h" |
| 14 | #include "content/renderer/media/media_stream_extra_data.h" |
| 15 | #include "content/renderer/p2p/socket_dispatcher.h" |
| 16 | #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h" |
| 17 | #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h" |
| 18 | |
| 19 | namespace base { |
| 20 | class WaitableEvent; |
| 21 | } |
| 22 | |
| 23 | namespace talk_base { |
| 24 | class NetworkManager; |
| 25 | class PacketSocketFactory; |
| 26 | class Thread; |
| 27 | } |
| 28 | |
| 29 | namespace webrtc { |
| 30 | class PeerConnection; |
| 31 | } |
| 32 | |
| 33 | namespace WebKit { |
| 34 | class WebFrame; |
| 35 | class WebMediaConstraints; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 36 | class WebMediaStream; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 37 | class WebRTCPeerConnectionHandler; |
| 38 | class WebRTCPeerConnectionHandlerClient; |
| 39 | } |
| 40 | |
| 41 | namespace content { |
| 42 | |
| 43 | class IpcNetworkManager; |
| 44 | class IpcPacketSocketFactory; |
| 45 | class VideoCaptureImplManager; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 46 | class WebRtcAudioCapturer; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 47 | class WebRtcAudioDeviceImpl; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 48 | class WebRtcLoggingHandlerImpl; |
| 49 | class WebRtcLoggingMessageFilter; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 50 | struct StreamDeviceInfo; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 51 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 52 | #if defined(GOOGLE_TV) |
| 53 | class RTCVideoDecoderFactoryTv; |
| 54 | #endif |
| 55 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 56 | // Object factory for RTC MediaStreams and RTC PeerConnections. |
| 57 | class CONTENT_EXPORT MediaStreamDependencyFactory |
| 58 | : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 59 | public: |
| 60 | // MediaSourcesCreatedCallback is used in CreateNativeMediaSources. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 61 | typedef base::Callback<void(WebKit::WebMediaStream* web_stream, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 62 | bool live)> MediaSourcesCreatedCallback; |
| 63 | MediaStreamDependencyFactory( |
| 64 | VideoCaptureImplManager* vc_manager, |
| 65 | P2PSocketDispatcher* p2p_socket_dispatcher); |
| 66 | virtual ~MediaStreamDependencyFactory(); |
| 67 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | // Create a RTCPeerConnectionHandler object that implements the |
| 69 | // WebKit WebRTCPeerConnectionHandler interface. |
| 70 | WebKit::WebRTCPeerConnectionHandler* CreateRTCPeerConnectionHandler( |
| 71 | WebKit::WebRTCPeerConnectionHandlerClient* client); |
| 72 | |
| 73 | // CreateNativeMediaSources creates libjingle representations of |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 74 | // the underlying sources to the tracks in |web_stream|. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 75 | // |sources_created| is invoked when the sources have either been created and |
| 76 | // transitioned to a live state or failed. |
| 77 | // The libjingle sources is stored in the extra data field of |
| 78 | // WebMediaStreamSource. |
| 79 | // |audio_constraints| and |video_constraints| set parameters for the sources. |
| 80 | void CreateNativeMediaSources( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 81 | int render_view_id, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 82 | const WebKit::WebMediaConstraints& audio_constraints, |
| 83 | const WebKit::WebMediaConstraints& video_constraints, |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 84 | WebKit::WebMediaStream* web_stream, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | const MediaSourcesCreatedCallback& sources_created); |
| 86 | |
| 87 | // Creates a libjingle representation of a MediaStream and stores |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 88 | // it in the extra data field of |web_stream|. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 89 | void CreateNativeLocalMediaStream( |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 90 | WebKit::WebMediaStream* web_stream); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 91 | |
| 92 | // Creates a libjingle representation of a MediaStream and stores |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 93 | // it in the extra data field of |web_stream|. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 94 | // |stream_stopped| is a callback that is run when a MediaStream have been |
| 95 | // stopped. |
| 96 | void CreateNativeLocalMediaStream( |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 97 | WebKit::WebMediaStream* web_stream, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 98 | const MediaStreamExtraData::StreamStopCallback& stream_stop); |
| 99 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 100 | // Adds a libjingle representation of a MediaStreamTrack to |stream| based |
| 101 | // on the source of |track|. |
| 102 | bool AddNativeMediaStreamTrack(const WebKit::WebMediaStream& stream, |
| 103 | const WebKit::WebMediaStreamTrack& track); |
| 104 | |
| 105 | // Creates and adds libjingle representation of a MediaStreamTrack to |stream| |
| 106 | // based on the desired |track_id| and |capturer|. |
| 107 | bool AddNativeVideoMediaTrack(const std::string& track_id, |
| 108 | WebKit::WebMediaStream* stream, |
| 109 | cricket::VideoCapturer* capturer); |
| 110 | |
| 111 | bool RemoveNativeMediaStreamTrack(const WebKit::WebMediaStream& stream, |
| 112 | const WebKit::WebMediaStreamTrack& track); |
| 113 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 114 | // Asks the libjingle PeerConnection factory to create a libjingle |
| 115 | // PeerConnection object. |
| 116 | // The PeerConnection object is owned by PeerConnectionHandler. |
| 117 | virtual scoped_refptr<webrtc::PeerConnectionInterface> |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | CreatePeerConnection( |
| 119 | const webrtc::PeerConnectionInterface::IceServers& ice_servers, |
| 120 | const webrtc::MediaConstraintsInterface* constraints, |
| 121 | WebKit::WebFrame* web_frame, |
| 122 | webrtc::PeerConnectionObserver* observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 123 | |
| 124 | // Creates a libjingle representation of a Session description. Used by a |
| 125 | // RTCPeerConnectionHandler instance. |
| 126 | virtual webrtc::SessionDescriptionInterface* CreateSessionDescription( |
| 127 | const std::string& type, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 128 | const std::string& sdp, |
| 129 | webrtc::SdpParseError* error); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 130 | |
| 131 | // Creates a libjingle representation of an ice candidate. |
| 132 | virtual webrtc::IceCandidateInterface* CreateIceCandidate( |
| 133 | const std::string& sdp_mid, |
| 134 | int sdp_mline_index, |
| 135 | const std::string& sdp); |
| 136 | |
| 137 | WebRtcAudioDeviceImpl* GetWebRtcAudioDevice(); |
| 138 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 139 | #if defined(GOOGLE_TV) |
| 140 | RTCVideoDecoderFactoryTv* decoder_factory_tv() { return decoder_factory_tv_; } |
| 141 | #endif |
| 142 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 143 | protected: |
| 144 | // Asks the PeerConnection factory to create a Local MediaStream object. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 145 | virtual scoped_refptr<webrtc::MediaStreamInterface> |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 146 | CreateLocalMediaStream(const std::string& label); |
| 147 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 148 | // Asks the PeerConnection factory to create a Local Audio Source. |
| 149 | virtual scoped_refptr<webrtc::AudioSourceInterface> |
| 150 | CreateLocalAudioSource( |
| 151 | const webrtc::MediaConstraintsInterface* constraints); |
| 152 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 153 | // Asks the PeerConnection factory to create a Local Video Source. |
| 154 | virtual scoped_refptr<webrtc::VideoSourceInterface> |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 155 | CreateLocalVideoSource( |
| 156 | int video_session_id, |
| 157 | bool is_screen_cast, |
| 158 | const webrtc::MediaConstraintsInterface* constraints); |
| 159 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 160 | // Creates a media::AudioCapturerSource with an implementation that is |
| 161 | // specific for a WebAudio source. The created WebAudioCapturerSource |
| 162 | // instance will function as audio source instead of the default |
| 163 | // WebRtcAudioCapturer. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 164 | virtual scoped_refptr<WebRtcAudioCapturer> CreateWebAudioSource( |
| 165 | WebKit::WebMediaStreamSource* source); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 166 | |
| 167 | // Asks the PeerConnection factory to create a Local AudioTrack object. |
| 168 | virtual scoped_refptr<webrtc::AudioTrackInterface> |
| 169 | CreateLocalAudioTrack(const std::string& id, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 170 | const scoped_refptr<WebRtcAudioCapturer>& capturer, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 171 | webrtc::AudioSourceInterface* source); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 172 | |
| 173 | // Asks the PeerConnection factory to create a Local VideoTrack object. |
| 174 | virtual scoped_refptr<webrtc::VideoTrackInterface> |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 175 | CreateLocalVideoTrack(const std::string& id, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 176 | webrtc::VideoSourceInterface* source); |
| 177 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 178 | // Asks the PeerConnection factory to create a Local VideoTrack object with |
| 179 | // the video source using |capturer|. |
| 180 | virtual scoped_refptr<webrtc::VideoTrackInterface> |
| 181 | CreateLocalVideoTrack(const std::string& id, |
| 182 | cricket::VideoCapturer* capturer); |
| 183 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 184 | virtual bool EnsurePeerConnectionFactory(); |
| 185 | virtual bool PeerConnectionFactoryCreated(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 186 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 187 | // Returns a new capturer or existing capturer based on the |render_view_id| |
| 188 | // and |device_info|. When the |render_view_id| and |device_info| are valid, |
| 189 | // it reuses existing capture if any; otherwise it creates a new capturer. |
| 190 | virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer( |
| 191 | int render_view_id, const StreamDeviceInfo& device_info); |
| 192 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 193 | private: |
| 194 | // Creates and deletes |pc_factory_|, which in turn is used for |
| 195 | // creating PeerConnection objects. |
| 196 | bool CreatePeerConnectionFactory(); |
| 197 | |
| 198 | void InitializeWorkerThread(talk_base::Thread** thread, |
| 199 | base::WaitableEvent* event); |
| 200 | |
| 201 | void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent* event); |
| 202 | void DeleteIpcNetworkManager(); |
| 203 | void CleanupPeerConnectionFactory(); |
| 204 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 205 | void CreateWebRtcLoggingHandler(WebRtcLoggingMessageFilter* filter, |
| 206 | const std::string& app_session_id, |
| 207 | const std::string& app_url); |
| 208 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 209 | // We own network_manager_, must be deleted on the worker thread. |
| 210 | // The network manager uses |p2p_socket_dispatcher_|. |
| 211 | IpcNetworkManager* network_manager_; |
| 212 | scoped_ptr<IpcPacketSocketFactory> socket_factory_; |
| 213 | |
| 214 | scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_; |
| 215 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 216 | #if defined(GOOGLE_TV) |
| 217 | // |pc_factory_| will hold the ownership of this object, and |pc_factory_| |
| 218 | // outlives this object. Thus weak pointer is sufficient. |
| 219 | RTCVideoDecoderFactoryTv* decoder_factory_tv_; |
| 220 | #endif |
| 221 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 222 | scoped_refptr<VideoCaptureImplManager> vc_manager_; |
| 223 | scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_; |
| 224 | scoped_refptr<WebRtcAudioDeviceImpl> audio_device_; |
| 225 | |
| 226 | // PeerConnection threads. signaling_thread_ is created from the |
| 227 | // "current" chrome thread. |
| 228 | talk_base::Thread* signaling_thread_; |
| 229 | talk_base::Thread* worker_thread_; |
| 230 | base::Thread chrome_worker_thread_; |
| 231 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 232 | DISALLOW_COPY_AND_ASSIGN(MediaStreamDependencyFactory); |
| 233 | }; |
| 234 | |
| 235 | } // namespace content |
| 236 | |
| 237 | #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_ |