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/message_loop/message_loop.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/p2p/socket_dispatcher.h"
18 #include "ipc/ipc_platform_file.h"
19 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
20 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h"
28 class PacketSocketFactory
;
34 class WebMediaConstraints
;
36 class WebMediaStreamSource
;
37 class WebMediaStreamTrack
;
38 class WebRTCPeerConnectionHandler
;
39 class WebRTCPeerConnectionHandlerClient
;
44 class IpcNetworkManager
;
45 class IpcPacketSocketFactory
;
46 class MediaStreamAudioSource
;
47 class RTCMediaConstraints
;
48 class WebAudioCapturerSource
;
49 class WebRtcAudioCapturer
;
50 class WebRtcAudioDeviceImpl
;
51 class WebRtcLocalAudioTrack
;
52 class WebRtcLoggingHandlerImpl
;
53 class WebRtcLoggingMessageFilter
;
54 class WebRtcVideoCapturerAdapter
;
55 struct StreamDeviceInfo
;
57 // Object factory for RTC PeerConnections.
58 class CONTENT_EXPORT PeerConnectionDependencyFactory
59 : NON_EXPORTED_BASE(base::MessageLoop::DestructionObserver
),
60 NON_EXPORTED_BASE(public base::NonThreadSafe
) {
62 PeerConnectionDependencyFactory(
63 P2PSocketDispatcher
* p2p_socket_dispatcher
);
64 ~PeerConnectionDependencyFactory() override
;
66 // Create a RTCPeerConnectionHandler object that implements the
67 // WebKit WebRTCPeerConnectionHandler interface.
68 blink::WebRTCPeerConnectionHandler
* CreateRTCPeerConnectionHandler(
69 blink::WebRTCPeerConnectionHandlerClient
* client
);
71 // Asks the PeerConnection factory to create a Local MediaStream object.
72 virtual scoped_refptr
<webrtc::MediaStreamInterface
>
73 CreateLocalMediaStream(const std::string
& label
);
75 // InitializeMediaStreamAudioSource initialize a MediaStream source object
77 bool InitializeMediaStreamAudioSource(
79 const blink::WebMediaConstraints
& audio_constraints
,
80 MediaStreamAudioSource
* source_data
);
82 // Creates an implementation of a cricket::VideoCapturer object that can be
83 // used when creating a libjingle webrtc::VideoSourceInterface object.
84 virtual WebRtcVideoCapturerAdapter
* CreateVideoCapturer(
85 bool is_screen_capture
);
87 // Create an instance of WebRtcLocalAudioTrack and store it
88 // in the extraData field of |track|.
89 void CreateLocalAudioTrack(const blink::WebMediaStreamTrack
& track
);
91 // Asks the PeerConnection factory to create a Local VideoTrack object.
92 virtual scoped_refptr
<webrtc::VideoTrackInterface
>
93 CreateLocalVideoTrack(const std::string
& id
,
94 webrtc::VideoSourceInterface
* source
);
96 // Asks the PeerConnection factory to create a Video Source.
97 // The video source takes ownership of |capturer|.
98 virtual scoped_refptr
<webrtc::VideoSourceInterface
>
99 CreateVideoSource(cricket::VideoCapturer
* capturer
,
100 const blink::WebMediaConstraints
& constraints
);
102 // Asks the libjingle PeerConnection factory to create a libjingle
103 // PeerConnection object.
104 // The PeerConnection object is owned by PeerConnectionHandler.
105 virtual scoped_refptr
<webrtc::PeerConnectionInterface
>
106 CreatePeerConnection(
107 const webrtc::PeerConnectionInterface::RTCConfiguration
& config
,
108 const webrtc::MediaConstraintsInterface
* constraints
,
109 blink::WebFrame
* web_frame
,
110 webrtc::PeerConnectionObserver
* observer
);
112 // Creates a libjingle representation of a Session description. Used by a
113 // RTCPeerConnectionHandler instance.
114 virtual webrtc::SessionDescriptionInterface
* CreateSessionDescription(
115 const std::string
& type
,
116 const std::string
& sdp
,
117 webrtc::SdpParseError
* error
);
119 // Creates a libjingle representation of an ice candidate.
120 virtual webrtc::IceCandidateInterface
* CreateIceCandidate(
121 const std::string
& sdp_mid
,
123 const std::string
& sdp
);
125 WebRtcAudioDeviceImpl
* GetWebRtcAudioDevice();
127 scoped_refptr
<base::MessageLoopProxy
> GetWebRtcWorkerThread() const;
128 scoped_refptr
<base::MessageLoopProxy
> GetWebRtcSignalingThread() const;
131 // Asks the PeerConnection factory to create a Local Audio Source.
132 virtual scoped_refptr
<webrtc::AudioSourceInterface
>
133 CreateLocalAudioSource(
134 const webrtc::MediaConstraintsInterface
* constraints
);
136 // Creates a media::AudioCapturerSource with an implementation that is
137 // specific for a WebAudio source. The created WebAudioCapturerSource
138 // instance will function as audio source instead of the default
139 // WebRtcAudioCapturer.
140 virtual scoped_refptr
<WebAudioCapturerSource
> CreateWebAudioSource(
141 blink::WebMediaStreamSource
* source
);
143 // Asks the PeerConnection factory to create a Local VideoTrack object with
144 // the video source using |capturer|.
145 virtual scoped_refptr
<webrtc::VideoTrackInterface
>
146 CreateLocalVideoTrack(const std::string
& id
,
147 cricket::VideoCapturer
* capturer
);
149 virtual const scoped_refptr
<webrtc::PeerConnectionFactoryInterface
>&
151 virtual bool PeerConnectionFactoryCreated();
153 // Returns a new capturer or existing capturer based on the |render_view_id|
154 // and |device_info|. When the |render_view_id| and |device_info| are valid,
155 // it reuses existing capture if any; otherwise it creates a new capturer.
156 virtual scoped_refptr
<WebRtcAudioCapturer
> CreateAudioCapturer(
157 int render_view_id
, const StreamDeviceInfo
& device_info
,
158 const blink::WebMediaConstraints
& constraints
,
159 MediaStreamAudioSource
* audio_source
);
161 // Adds the audio device as a sink to the audio track and starts the local
162 // audio track. This is virtual for test purposes since no real audio device
163 // exist in unit tests.
164 virtual void StartLocalAudioTrack(WebRtcLocalAudioTrack
* audio_track
);
167 // Implement base::MessageLoop::DestructionObserver.
168 // This makes sure the libjingle PeerConnectionFactory is released before
169 // the renderer message loop is destroyed.
170 void WillDestroyCurrentMessageLoop() override
;
172 // Creates |pc_factory_|, which in turn is used for
173 // creating PeerConnection objects.
174 void CreatePeerConnectionFactory();
176 void InitializeSignalingThread(
177 const scoped_refptr
<media::GpuVideoAcceleratorFactories
>& gpu_factories
,
178 base::WaitableEvent
* event
);
180 void InitializeWorkerThread(rtc::Thread
** thread
,
181 base::WaitableEvent
* event
);
183 void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent
* event
);
184 void DeleteIpcNetworkManager();
185 void CleanupPeerConnectionFactory();
187 // Helper method to create a WebRtcAudioDeviceImpl.
188 void EnsureWebRtcAudioDeviceImpl();
190 // We own network_manager_, must be deleted on the worker thread.
191 // The network manager uses |p2p_socket_dispatcher_|.
192 IpcNetworkManager
* network_manager_
;
193 scoped_ptr
<IpcPacketSocketFactory
> socket_factory_
;
195 scoped_refptr
<webrtc::PeerConnectionFactoryInterface
> pc_factory_
;
197 scoped_refptr
<P2PSocketDispatcher
> p2p_socket_dispatcher_
;
198 scoped_refptr
<WebRtcAudioDeviceImpl
> audio_device_
;
200 // PeerConnection threads. signaling_thread_ is created from the
201 // "current" chrome thread.
202 rtc::Thread
* signaling_thread_
;
203 rtc::Thread
* worker_thread_
;
204 base::Thread chrome_signaling_thread_
;
205 base::Thread chrome_worker_thread_
;
207 DISALLOW_COPY_AND_ASSIGN(PeerConnectionDependencyFactory
);
210 } // namespace content
212 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_