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/single_thread_task_runner.h"
13 #include "base/threading/thread.h"
14 #include "content/common/content_export.h"
15 #include "content/public/renderer/render_process_observer.h"
16 #include "content/renderer/media/aec_dump_message_filter.h"
17 #include "content/renderer/media/webrtc/stun_field_trial.h"
18 #include "content/renderer/p2p/socket_dispatcher.h"
19 #include "ipc/ipc_platform_file.h"
20 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
21 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h"
22 #include "third_party/webrtc/p2p/stunprober/stunprober.h"
30 class PacketSocketFactory
;
36 class WebMediaConstraints
;
38 class WebMediaStreamSource
;
39 class WebMediaStreamTrack
;
40 class WebRTCPeerConnectionHandler
;
41 class WebRTCPeerConnectionHandlerClient
;
46 class IpcNetworkManager
;
47 class IpcPacketSocketFactory
;
48 class MediaStreamAudioSource
;
49 class RTCMediaConstraints
;
50 class WebAudioCapturerSource
;
51 class WebRtcAudioCapturer
;
52 class WebRtcAudioDeviceImpl
;
53 class WebRtcLocalAudioTrack
;
54 class WebRtcLoggingHandlerImpl
;
55 class WebRtcLoggingMessageFilter
;
56 class WebRtcVideoCapturerAdapter
;
57 struct StreamDeviceInfo
;
59 // Object factory for RTC PeerConnections.
60 class CONTENT_EXPORT PeerConnectionDependencyFactory
61 : NON_EXPORTED_BASE(base::MessageLoop::DestructionObserver
),
62 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
64 PeerConnectionDependencyFactory(
65 P2PSocketDispatcher
* p2p_socket_dispatcher
);
66 ~PeerConnectionDependencyFactory() override
;
68 // Create a RTCPeerConnectionHandler object that implements the
69 // WebKit WebRTCPeerConnectionHandler interface.
70 blink::WebRTCPeerConnectionHandler
* CreateRTCPeerConnectionHandler(
71 blink::WebRTCPeerConnectionHandlerClient
* client
);
73 // Asks the PeerConnection factory to create a Local MediaStream object.
74 virtual scoped_refptr
<webrtc::MediaStreamInterface
>
75 CreateLocalMediaStream(const std::string
& label
);
77 // InitializeMediaStreamAudioSource initialize a MediaStream source object
79 bool InitializeMediaStreamAudioSource(
81 const blink::WebMediaConstraints
& audio_constraints
,
82 MediaStreamAudioSource
* source_data
);
84 // Creates an implementation of a cricket::VideoCapturer object that can be
85 // used when creating a libjingle webrtc::VideoSourceInterface object.
86 virtual WebRtcVideoCapturerAdapter
* CreateVideoCapturer(
87 bool is_screen_capture
);
89 // Create an instance of WebRtcLocalAudioTrack and store it
90 // in the extraData field of |track|.
91 void CreateLocalAudioTrack(const blink::WebMediaStreamTrack
& track
);
93 // Asks the PeerConnection factory to create a Local VideoTrack object.
94 virtual scoped_refptr
<webrtc::VideoTrackInterface
>
95 CreateLocalVideoTrack(const std::string
& id
,
96 webrtc::VideoSourceInterface
* source
);
98 // Asks the PeerConnection factory to create a Video Source.
99 // The video source takes ownership of |capturer|.
100 virtual scoped_refptr
<webrtc::VideoSourceInterface
>
101 CreateVideoSource(cricket::VideoCapturer
* capturer
,
102 const blink::WebMediaConstraints
& constraints
);
104 // Asks the libjingle PeerConnection factory to create a libjingle
105 // PeerConnection object.
106 // The PeerConnection object is owned by PeerConnectionHandler.
107 virtual scoped_refptr
<webrtc::PeerConnectionInterface
>
108 CreatePeerConnection(
109 const webrtc::PeerConnectionInterface::RTCConfiguration
& config
,
110 const webrtc::MediaConstraintsInterface
* constraints
,
111 blink::WebFrame
* web_frame
,
112 webrtc::PeerConnectionObserver
* observer
);
114 // Creates a libjingle representation of a Session description. Used by a
115 // RTCPeerConnectionHandler instance.
116 virtual webrtc::SessionDescriptionInterface
* CreateSessionDescription(
117 const std::string
& type
,
118 const std::string
& sdp
,
119 webrtc::SdpParseError
* error
);
121 // Creates a libjingle representation of an ice candidate.
122 virtual webrtc::IceCandidateInterface
* CreateIceCandidate(
123 const std::string
& sdp_mid
,
125 const std::string
& sdp
);
127 WebRtcAudioDeviceImpl
* GetWebRtcAudioDevice();
129 scoped_refptr
<base::SingleThreadTaskRunner
> GetWebRtcWorkerThread() const;
130 scoped_refptr
<base::SingleThreadTaskRunner
> GetWebRtcSignalingThread() const;
133 // Asks the PeerConnection factory to create a Local Audio Source.
134 virtual scoped_refptr
<webrtc::AudioSourceInterface
>
135 CreateLocalAudioSource(
136 const webrtc::MediaConstraintsInterface
* constraints
);
138 // Creates a media::AudioCapturerSource with an implementation that is
139 // specific for a WebAudio source. The created WebAudioCapturerSource
140 // instance will function as audio source instead of the default
141 // WebRtcAudioCapturer.
142 virtual scoped_refptr
<WebAudioCapturerSource
> CreateWebAudioSource(
143 blink::WebMediaStreamSource
* source
);
145 // Asks the PeerConnection factory to create a Local VideoTrack object with
146 // the video source using |capturer|.
147 virtual scoped_refptr
<webrtc::VideoTrackInterface
>
148 CreateLocalVideoTrack(const std::string
& id
,
149 cricket::VideoCapturer
* capturer
);
151 virtual const scoped_refptr
<webrtc::PeerConnectionFactoryInterface
>&
153 virtual bool PeerConnectionFactoryCreated();
155 // Returns a new capturer or existing capturer based on the |render_frame_id|
156 // and |device_info|; if both are valid, it reuses existing capture if any --
157 // otherwise it creates a new capturer.
158 virtual scoped_refptr
<WebRtcAudioCapturer
> CreateAudioCapturer(
160 const StreamDeviceInfo
& device_info
,
161 const blink::WebMediaConstraints
& constraints
,
162 MediaStreamAudioSource
* audio_source
);
164 // Adds the audio device as a sink to the audio track and starts the local
165 // audio track. This is virtual for test purposes since no real audio device
166 // exist in unit tests.
167 virtual void StartLocalAudioTrack(WebRtcLocalAudioTrack
* audio_track
);
170 // Implement base::MessageLoop::DestructionObserver.
171 // This makes sure the libjingle PeerConnectionFactory is released before
172 // the renderer message loop is destroyed.
173 void WillDestroyCurrentMessageLoop() override
;
175 // Functions related to Stun probing trial to determine how fast we could send
176 // Stun request without being dropped by NAT.
177 void TryScheduleStunProbeTrial();
178 void StartStunProbeTrialOnWorkerThread(const std::string
& params
);
180 // Creates |pc_factory_|, which in turn is used for
181 // creating PeerConnection objects.
182 void CreatePeerConnectionFactory();
184 void InitializeSignalingThread(
185 const scoped_refptr
<media::GpuVideoAcceleratorFactories
>& gpu_factories
,
186 base::WaitableEvent
* event
);
188 void InitializeWorkerThread(rtc::Thread
** thread
,
189 base::WaitableEvent
* event
);
191 void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent
* event
);
192 void DeleteIpcNetworkManager();
193 void CleanupPeerConnectionFactory();
195 // Helper method to create a WebRtcAudioDeviceImpl.
196 void EnsureWebRtcAudioDeviceImpl();
198 // We own network_manager_, must be deleted on the worker thread.
199 // The network manager uses |p2p_socket_dispatcher_|.
200 IpcNetworkManager
* network_manager_
;
201 scoped_ptr
<IpcPacketSocketFactory
> socket_factory_
;
203 scoped_refptr
<webrtc::PeerConnectionFactoryInterface
> pc_factory_
;
205 scoped_refptr
<P2PSocketDispatcher
> p2p_socket_dispatcher_
;
206 scoped_refptr
<WebRtcAudioDeviceImpl
> audio_device_
;
208 scoped_ptr
<stunprober::StunProber
> stun_prober_
;
210 // PeerConnection threads. signaling_thread_ is created from the
211 // "current" chrome thread.
212 rtc::Thread
* signaling_thread_
;
213 rtc::Thread
* worker_thread_
;
214 base::Thread chrome_signaling_thread_
;
215 base::Thread chrome_worker_thread_
;
217 DISALLOW_COPY_AND_ASSIGN(PeerConnectionDependencyFactory
);
220 } // namespace content
222 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_