1 // Copyright 2014 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.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
10 #include "base/basictypes.h"
11 #include "base/files/file.h"
12 #include "base/threading/thread.h"
13 #include "content/common/content_export.h"
14 #include "content/public/renderer/render_process_observer.h"
15 #include "content/renderer/media/aec_dump_message_filter.h"
16 #include "content/renderer/p2p/socket_dispatcher.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
19 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h"
27 class PacketSocketFactory;
33 class WebMediaConstraints;
35 class WebMediaStreamSource;
36 class WebMediaStreamTrack;
37 class WebRTCPeerConnectionHandler;
38 class WebRTCPeerConnectionHandlerClient;
43 class IpcNetworkManager;
44 class IpcPacketSocketFactory;
45 class MediaStreamAudioSource;
46 class RTCMediaConstraints;
47 class WebAudioCapturerSource;
48 class WebRtcAudioCapturer;
49 class WebRtcAudioDeviceImpl;
50 class WebRtcLocalAudioTrack;
51 class WebRtcLoggingHandlerImpl;
52 class WebRtcLoggingMessageFilter;
53 class WebRtcVideoCapturerAdapter;
54 struct StreamDeviceInfo;
56 // Object factory for RTC PeerConnections.
57 class CONTENT_EXPORT PeerConnectionDependencyFactory
58 : NON_EXPORTED_BASE(public base::NonThreadSafe),
59 NON_EXPORTED_BASE(public AecDumpMessageFilter::AecDumpDelegate) {
61 PeerConnectionDependencyFactory(
62 P2PSocketDispatcher* p2p_socket_dispatcher);
63 virtual ~PeerConnectionDependencyFactory();
65 // Create a RTCPeerConnectionHandler object that implements the
66 // WebKit WebRTCPeerConnectionHandler interface.
67 blink::WebRTCPeerConnectionHandler* CreateRTCPeerConnectionHandler(
68 blink::WebRTCPeerConnectionHandlerClient* client);
70 // Asks the PeerConnection factory to create a Local MediaStream object.
71 virtual scoped_refptr<webrtc::MediaStreamInterface>
72 CreateLocalMediaStream(const std::string& label);
74 // InitializeMediaStreamAudioSource initialize a MediaStream source object
76 bool InitializeMediaStreamAudioSource(
78 const blink::WebMediaConstraints& audio_constraints,
79 MediaStreamAudioSource* source_data);
81 // Creates an implementation of a cricket::VideoCapturer object that can be
82 // used when creating a libjingle webrtc::VideoSourceInterface object.
83 virtual WebRtcVideoCapturerAdapter* CreateVideoCapturer(
84 bool is_screen_capture);
86 // Create an instance of WebRtcLocalAudioTrack and store it
87 // in the extraData field of |track|.
88 void CreateLocalAudioTrack(const blink::WebMediaStreamTrack& track);
90 // Asks the PeerConnection factory to create a Local VideoTrack object.
91 virtual scoped_refptr<webrtc::VideoTrackInterface>
92 CreateLocalVideoTrack(const std::string& id,
93 webrtc::VideoSourceInterface* source);
95 // Asks the PeerConnection factory to create a Video Source.
96 // The video source takes ownership of |capturer|.
97 virtual scoped_refptr<webrtc::VideoSourceInterface>
98 CreateVideoSource(cricket::VideoCapturer* capturer,
99 const blink::WebMediaConstraints& constraints);
101 // Asks the libjingle PeerConnection factory to create a libjingle
102 // PeerConnection object.
103 // The PeerConnection object is owned by PeerConnectionHandler.
104 virtual scoped_refptr<webrtc::PeerConnectionInterface>
105 CreatePeerConnection(
106 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
107 const webrtc::MediaConstraintsInterface* constraints,
108 blink::WebFrame* web_frame,
109 webrtc::PeerConnectionObserver* observer);
111 // Creates a libjingle representation of a Session description. Used by a
112 // RTCPeerConnectionHandler instance.
113 virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
114 const std::string& type,
115 const std::string& sdp,
116 webrtc::SdpParseError* error);
118 // Creates a libjingle representation of an ice candidate.
119 virtual webrtc::IceCandidateInterface* CreateIceCandidate(
120 const std::string& sdp_mid,
122 const std::string& sdp);
124 WebRtcAudioDeviceImpl* GetWebRtcAudioDevice();
126 static void AddNativeAudioTrackToBlinkTrack(
127 webrtc::MediaStreamTrackInterface* native_track,
128 const blink::WebMediaStreamTrack& webkit_track,
129 bool is_local_track);
131 scoped_refptr<base::MessageLoopProxy> GetWebRtcWorkerThread() const;
133 // AecDumpMessageFilter::AecDumpDelegate implementation.
134 // TODO(xians): Remove when option to disable audio track processing is
136 virtual void OnAecDumpFile(
137 const IPC::PlatformFileForTransit& file_handle) OVERRIDE;
138 virtual void OnDisableAecDump() OVERRIDE;
139 virtual void OnIpcClosing() OVERRIDE;
142 // Asks the PeerConnection factory to create a Local Audio Source.
143 virtual scoped_refptr<webrtc::AudioSourceInterface>
144 CreateLocalAudioSource(
145 const webrtc::MediaConstraintsInterface* constraints);
147 // Creates a media::AudioCapturerSource with an implementation that is
148 // specific for a WebAudio source. The created WebAudioCapturerSource
149 // instance will function as audio source instead of the default
150 // WebRtcAudioCapturer.
151 virtual scoped_refptr<WebAudioCapturerSource> CreateWebAudioSource(
152 blink::WebMediaStreamSource* source);
154 // Asks the PeerConnection factory to create a Local VideoTrack object with
155 // the video source using |capturer|.
156 virtual scoped_refptr<webrtc::VideoTrackInterface>
157 CreateLocalVideoTrack(const std::string& id,
158 cricket::VideoCapturer* capturer);
160 virtual const scoped_refptr<webrtc::PeerConnectionFactoryInterface>&
162 virtual bool PeerConnectionFactoryCreated();
164 // Returns a new capturer or existing capturer based on the |render_view_id|
165 // and |device_info|. When the |render_view_id| and |device_info| are valid,
166 // it reuses existing capture if any; otherwise it creates a new capturer.
167 virtual scoped_refptr<WebRtcAudioCapturer> CreateAudioCapturer(
168 int render_view_id, const StreamDeviceInfo& device_info,
169 const blink::WebMediaConstraints& constraints,
170 MediaStreamAudioSource* audio_source);
172 // Adds the audio device as a sink to the audio track and starts the local
173 // audio track. This is virtual for test purposes since no real audio device
174 // exist in unit tests.
175 virtual void StartLocalAudioTrack(WebRtcLocalAudioTrack* audio_track);
178 // Creates |pc_factory_|, which in turn is used for
179 // creating PeerConnection objects.
180 void CreatePeerConnectionFactory();
182 void InitializeWorkerThread(rtc::Thread** thread,
183 base::WaitableEvent* event);
185 void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent* event);
186 void DeleteIpcNetworkManager();
187 void CleanupPeerConnectionFactory();
189 // Helper method to create a WebRtcAudioDeviceImpl.
190 void EnsureWebRtcAudioDeviceImpl();
192 // We own network_manager_, must be deleted on the worker thread.
193 // The network manager uses |p2p_socket_dispatcher_|.
194 IpcNetworkManager* network_manager_;
195 scoped_ptr<IpcPacketSocketFactory> socket_factory_;
197 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
199 scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
200 scoped_refptr<WebRtcAudioDeviceImpl> audio_device_;
202 // This is only used if audio track processing is disabled.
203 // TODO(xians): Remove when option to disable audio track processing is
205 scoped_refptr<AecDumpMessageFilter> aec_dump_message_filter_;
207 // PeerConnection threads. signaling_thread_ is created from the
208 // "current" chrome thread.
209 rtc::Thread* signaling_thread_;
210 rtc::Thread* worker_thread_;
211 base::Thread chrome_worker_thread_;
213 DISALLOW_COPY_AND_ASSIGN(PeerConnectionDependencyFactory);
216 } // namespace content
218 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_