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_frame_id|
154 // and |device_info|; if both are valid, it reuses existing capture if any --
155 // otherwise it creates a new capturer.
156 virtual scoped_refptr
<WebRtcAudioCapturer
> CreateAudioCapturer(
158 const StreamDeviceInfo
& device_info
,
159 const blink::WebMediaConstraints
& constraints
,
160 MediaStreamAudioSource
* audio_source
);
162 // Adds the audio device as a sink to the audio track and starts the local
163 // audio track. This is virtual for test purposes since no real audio device
164 // exist in unit tests.
165 virtual void StartLocalAudioTrack(WebRtcLocalAudioTrack
* audio_track
);
168 // Implement base::MessageLoop::DestructionObserver.
169 // This makes sure the libjingle PeerConnectionFactory is released before
170 // the renderer message loop is destroyed.
171 void WillDestroyCurrentMessageLoop() override
;
173 // Creates |pc_factory_|, which in turn is used for
174 // creating PeerConnection objects.
175 void CreatePeerConnectionFactory();
177 void InitializeSignalingThread(
178 const scoped_refptr
<media::GpuVideoAcceleratorFactories
>& gpu_factories
,
179 base::WaitableEvent
* event
);
181 void InitializeWorkerThread(rtc::Thread
** thread
,
182 base::WaitableEvent
* event
);
184 void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent
* event
);
185 void DeleteIpcNetworkManager();
186 void CleanupPeerConnectionFactory();
188 // Helper method to create a WebRtcAudioDeviceImpl.
189 void EnsureWebRtcAudioDeviceImpl();
191 // We own network_manager_, must be deleted on the worker thread.
192 // The network manager uses |p2p_socket_dispatcher_|.
193 IpcNetworkManager
* network_manager_
;
194 scoped_ptr
<IpcPacketSocketFactory
> socket_factory_
;
196 scoped_refptr
<webrtc::PeerConnectionFactoryInterface
> pc_factory_
;
198 scoped_refptr
<P2PSocketDispatcher
> p2p_socket_dispatcher_
;
199 scoped_refptr
<WebRtcAudioDeviceImpl
> audio_device_
;
201 // PeerConnection threads. signaling_thread_ is created from the
202 // "current" chrome thread.
203 rtc::Thread
* signaling_thread_
;
204 rtc::Thread
* worker_thread_
;
205 base::Thread chrome_signaling_thread_
;
206 base::Thread chrome_worker_thread_
;
208 DISALLOW_COPY_AND_ASSIGN(PeerConnectionDependencyFactory
);
211 } // namespace content
213 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_