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