1// 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;
36class WebMediaStream;
37class WebRTCPeerConnectionHandler;
38class WebRTCPeerConnectionHandlerClient;
39}
40
41namespace content {
42
43class IpcNetworkManager;
44class IpcPacketSocketFactory;
45class VideoCaptureImplManager;
46class WebRtcAudioCapturer;
47class WebRtcAudioDeviceImpl;
48class WebRtcLoggingHandlerImpl;
49class WebRtcLoggingMessageFilter;
50struct StreamDeviceInfo;
51
52#if defined(GOOGLE_TV)
53class RTCVideoDecoderFactoryTv;
54#endif
55
56// 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.
61  typedef base::Callback<void(WebKit::WebMediaStream* web_stream,
62                              bool live)> MediaSourcesCreatedCallback;
63  MediaStreamDependencyFactory(
64      VideoCaptureImplManager* vc_manager,
65      P2PSocketDispatcher* p2p_socket_dispatcher);
66  virtual ~MediaStreamDependencyFactory();
67
68  // 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
74  // the underlying sources to the tracks in |web_stream|.
75  // |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(
81      int render_view_id,
82      const WebKit::WebMediaConstraints& audio_constraints,
83      const WebKit::WebMediaConstraints& video_constraints,
84      WebKit::WebMediaStream* web_stream,
85      const MediaSourcesCreatedCallback& sources_created);
86
87  // Creates a libjingle representation of a MediaStream and stores
88  // it in the extra data field of |web_stream|.
89  void CreateNativeLocalMediaStream(
90      WebKit::WebMediaStream* web_stream);
91
92  // Creates a libjingle representation of a MediaStream and stores
93  // it in the extra data field of |web_stream|.
94  // |stream_stopped| is a callback that is run when a MediaStream have been
95  // stopped.
96  void CreateNativeLocalMediaStream(
97      WebKit::WebMediaStream* web_stream,
98      const MediaStreamExtraData::StreamStopCallback& stream_stop);
99
100  // 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
114  // 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>
118      CreatePeerConnection(
119          const webrtc::PeerConnectionInterface::IceServers& ice_servers,
120          const webrtc::MediaConstraintsInterface* constraints,
121          WebKit::WebFrame* web_frame,
122          webrtc::PeerConnectionObserver* observer);
123
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,
128      const std::string& sdp,
129      webrtc::SdpParseError* error);
130
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
139#if defined(GOOGLE_TV)
140  RTCVideoDecoderFactoryTv* decoder_factory_tv() { return decoder_factory_tv_; }
141#endif
142
143 protected:
144  // Asks the PeerConnection factory to create a Local MediaStream object.
145  virtual scoped_refptr<webrtc::MediaStreamInterface>
146      CreateLocalMediaStream(const std::string& label);
147
148  // Asks the PeerConnection factory to create a Local Audio Source.
149  virtual scoped_refptr<webrtc::AudioSourceInterface>
150      CreateLocalAudioSource(
151          const webrtc::MediaConstraintsInterface* constraints);
152
153  // Asks the PeerConnection factory to create a Local Video Source.
154  virtual scoped_refptr<webrtc::VideoSourceInterface>
155      CreateLocalVideoSource(
156          int video_session_id,
157          bool is_screen_cast,
158          const webrtc::MediaConstraintsInterface* constraints);
159
160  // 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.
164  virtual scoped_refptr<WebRtcAudioCapturer> CreateWebAudioSource(
165      WebKit::WebMediaStreamSource* source);
166
167  // Asks the PeerConnection factory to create a Local AudioTrack object.
168  virtual scoped_refptr<webrtc::AudioTrackInterface>
169      CreateLocalAudioTrack(const std::string& id,
170                            const scoped_refptr<WebRtcAudioCapturer>& capturer,
171                            webrtc::AudioSourceInterface* source);
172
173  // Asks the PeerConnection factory to create a Local VideoTrack object.
174  virtual scoped_refptr<webrtc::VideoTrackInterface>
175      CreateLocalVideoTrack(const std::string& id,
176                            webrtc::VideoSourceInterface* source);
177
178  // 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
184  virtual bool EnsurePeerConnectionFactory();
185  virtual bool PeerConnectionFactoryCreated();
186
187  // 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
193 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
205  void CreateWebRtcLoggingHandler(WebRtcLoggingMessageFilter* filter,
206                                  const std::string& app_session_id,
207                                  const std::string& app_url);
208
209  // 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
216#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
222  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
232  DISALLOW_COPY_AND_ASSIGN(MediaStreamDependencyFactory);
233};
234
235}  // namespace content
236
237#endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
238