blob: 720ecb063d737ddcbdc072dc699a4b93dca468e5 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000036class WebMediaStream;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000037class WebRTCPeerConnectionHandler;
38class WebRTCPeerConnectionHandlerClient;
39}
40
41namespace content {
42
43class IpcNetworkManager;
44class IpcPacketSocketFactory;
45class VideoCaptureImplManager;
Ben Murdochbb1529c2013-08-08 10:24:53 +010046class WebRtcAudioCapturer;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000047class WebRtcAudioDeviceImpl;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010048class WebRtcLoggingHandlerImpl;
49class WebRtcLoggingMessageFilter;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000050struct StreamDeviceInfo;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000051
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010052#if defined(GOOGLE_TV)
53class RTCVideoDecoderFactoryTv;
54#endif
55
Torne (Richard Coles)58218062012-11-14 11:43:16 +000056// Object factory for RTC MediaStreams and RTC PeerConnections.
57class CONTENT_EXPORT MediaStreamDependencyFactory
58 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
59 public:
60 // MediaSourcesCreatedCallback is used in CreateNativeMediaSources.
Ben Murdochbbcdd452013-07-25 10:06:34 +010061 typedef base::Callback<void(WebKit::WebMediaStream* web_stream,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062 bool live)> MediaSourcesCreatedCallback;
63 MediaStreamDependencyFactory(
64 VideoCaptureImplManager* vc_manager,
65 P2PSocketDispatcher* p2p_socket_dispatcher);
66 virtual ~MediaStreamDependencyFactory();
67
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068 // Create a RTCPeerConnectionHandler object that implements the
69 // WebKit WebRTCPeerConnectionHandler interface.
70 WebKit::WebRTCPeerConnectionHandler* CreateRTCPeerConnectionHandler(
71 WebKit::WebRTCPeerConnectionHandlerClient* client);
72
73 // CreateNativeMediaSources creates libjingle representations of
Ben Murdochbbcdd452013-07-25 10:06:34 +010074 // the underlying sources to the tracks in |web_stream|.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000075 // |sources_created| is invoked when the sources have either been created and
76 // transitioned to a live state or failed.
77 // The libjingle sources is stored in the extra data field of
78 // WebMediaStreamSource.
79 // |audio_constraints| and |video_constraints| set parameters for the sources.
80 void CreateNativeMediaSources(
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010081 int render_view_id,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082 const WebKit::WebMediaConstraints& audio_constraints,
83 const WebKit::WebMediaConstraints& video_constraints,
Ben Murdochbbcdd452013-07-25 10:06:34 +010084 WebKit::WebMediaStream* web_stream,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000085 const MediaSourcesCreatedCallback& sources_created);
86
87 // Creates a libjingle representation of a MediaStream and stores
Ben Murdochbbcdd452013-07-25 10:06:34 +010088 // it in the extra data field of |web_stream|.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089 void CreateNativeLocalMediaStream(
Ben Murdochbbcdd452013-07-25 10:06:34 +010090 WebKit::WebMediaStream* web_stream);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091
92 // Creates a libjingle representation of a MediaStream and stores
Ben Murdochbbcdd452013-07-25 10:06:34 +010093 // it in the extra data field of |web_stream|.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000094 // |stream_stopped| is a callback that is run when a MediaStream have been
95 // stopped.
96 void CreateNativeLocalMediaStream(
Ben Murdochbbcdd452013-07-25 10:06:34 +010097 WebKit::WebMediaStream* web_stream,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000098 const MediaStreamExtraData::StreamStopCallback& stream_stop);
99
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100100 // Adds a libjingle representation of a MediaStreamTrack to |stream| based
101 // on the source of |track|.
102 bool AddNativeMediaStreamTrack(const WebKit::WebMediaStream& stream,
103 const WebKit::WebMediaStreamTrack& track);
104
105 // Creates and adds libjingle representation of a MediaStreamTrack to |stream|
106 // based on the desired |track_id| and |capturer|.
107 bool AddNativeVideoMediaTrack(const std::string& track_id,
108 WebKit::WebMediaStream* stream,
109 cricket::VideoCapturer* capturer);
110
111 bool RemoveNativeMediaStreamTrack(const WebKit::WebMediaStream& stream,
112 const WebKit::WebMediaStreamTrack& track);
113
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000114 // Asks the libjingle PeerConnection factory to create a libjingle
115 // PeerConnection object.
116 // The PeerConnection object is owned by PeerConnectionHandler.
117 virtual scoped_refptr<webrtc::PeerConnectionInterface>
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000118 CreatePeerConnection(
119 const webrtc::PeerConnectionInterface::IceServers& ice_servers,
120 const webrtc::MediaConstraintsInterface* constraints,
121 WebKit::WebFrame* web_frame,
122 webrtc::PeerConnectionObserver* observer);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000123
124 // Creates a libjingle representation of a Session description. Used by a
125 // RTCPeerConnectionHandler instance.
126 virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
127 const std::string& type,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000128 const std::string& sdp,
129 webrtc::SdpParseError* error);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000130
131 // Creates a libjingle representation of an ice candidate.
132 virtual webrtc::IceCandidateInterface* CreateIceCandidate(
133 const std::string& sdp_mid,
134 int sdp_mline_index,
135 const std::string& sdp);
136
137 WebRtcAudioDeviceImpl* GetWebRtcAudioDevice();
138
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100139#if defined(GOOGLE_TV)
140 RTCVideoDecoderFactoryTv* decoder_factory_tv() { return decoder_factory_tv_; }
141#endif
142
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000143 protected:
144 // Asks the PeerConnection factory to create a Local MediaStream object.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000145 virtual scoped_refptr<webrtc::MediaStreamInterface>
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000146 CreateLocalMediaStream(const std::string& label);
147
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000148 // Asks the PeerConnection factory to create a Local Audio Source.
149 virtual scoped_refptr<webrtc::AudioSourceInterface>
150 CreateLocalAudioSource(
151 const webrtc::MediaConstraintsInterface* constraints);
152
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000153 // Asks the PeerConnection factory to create a Local Video Source.
154 virtual scoped_refptr<webrtc::VideoSourceInterface>
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000155 CreateLocalVideoSource(
156 int video_session_id,
157 bool is_screen_cast,
158 const webrtc::MediaConstraintsInterface* constraints);
159
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000160 // Creates a media::AudioCapturerSource with an implementation that is
161 // specific for a WebAudio source. The created WebAudioCapturerSource
162 // instance will function as audio source instead of the default
163 // WebRtcAudioCapturer.
Ben Murdochbb1529c2013-08-08 10:24:53 +0100164 virtual scoped_refptr<WebRtcAudioCapturer> CreateWebAudioSource(
165 WebKit::WebMediaStreamSource* source);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000166
167 // Asks the PeerConnection factory to create a Local AudioTrack object.
168 virtual scoped_refptr<webrtc::AudioTrackInterface>
169 CreateLocalAudioTrack(const std::string& id,
Ben Murdochbb1529c2013-08-08 10:24:53 +0100170 const scoped_refptr<WebRtcAudioCapturer>& capturer,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000171 webrtc::AudioSourceInterface* source);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000172
173 // Asks the PeerConnection factory to create a Local VideoTrack object.
174 virtual scoped_refptr<webrtc::VideoTrackInterface>
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000175 CreateLocalVideoTrack(const std::string& id,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000176 webrtc::VideoSourceInterface* source);
177
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100178 // Asks the PeerConnection factory to create a Local VideoTrack object with
179 // the video source using |capturer|.
180 virtual scoped_refptr<webrtc::VideoTrackInterface>
181 CreateLocalVideoTrack(const std::string& id,
182 cricket::VideoCapturer* capturer);
183
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000184 virtual bool EnsurePeerConnectionFactory();
185 virtual bool PeerConnectionFactoryCreated();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000186
Ben Murdochbb1529c2013-08-08 10:24:53 +0100187 // Returns a new capturer or existing capturer based on the |render_view_id|
188 // and |device_info|. When the |render_view_id| and |device_info| are valid,
189 // it reuses existing capture if any; otherwise it creates a new capturer.
190 virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer(
191 int render_view_id, const StreamDeviceInfo& device_info);
192
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000193 private:
194 // Creates and deletes |pc_factory_|, which in turn is used for
195 // creating PeerConnection objects.
196 bool CreatePeerConnectionFactory();
197
198 void InitializeWorkerThread(talk_base::Thread** thread,
199 base::WaitableEvent* event);
200
201 void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent* event);
202 void DeleteIpcNetworkManager();
203 void CleanupPeerConnectionFactory();
204
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100205 void CreateWebRtcLoggingHandler(WebRtcLoggingMessageFilter* filter,
206 const std::string& app_session_id,
207 const std::string& app_url);
208
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000209 // We own network_manager_, must be deleted on the worker thread.
210 // The network manager uses |p2p_socket_dispatcher_|.
211 IpcNetworkManager* network_manager_;
212 scoped_ptr<IpcPacketSocketFactory> socket_factory_;
213
214 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
215
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100216#if defined(GOOGLE_TV)
217 // |pc_factory_| will hold the ownership of this object, and |pc_factory_|
218 // outlives this object. Thus weak pointer is sufficient.
219 RTCVideoDecoderFactoryTv* decoder_factory_tv_;
220#endif
221
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000222 scoped_refptr<VideoCaptureImplManager> vc_manager_;
223 scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
224 scoped_refptr<WebRtcAudioDeviceImpl> audio_device_;
225
226 // PeerConnection threads. signaling_thread_ is created from the
227 // "current" chrome thread.
228 talk_base::Thread* signaling_thread_;
229 talk_base::Thread* worker_thread_;
230 base::Thread chrome_worker_thread_;
231
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000232 DISALLOW_COPY_AND_ASSIGN(MediaStreamDependencyFactory);
233};
234
235} // namespace content
236
237#endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_