- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / media / media_stream_dependency_factory.h
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
19 namespace base {
20 class WaitableEvent;
21 }
22
23 namespace talk_base {
24 class NetworkManager;
25 class PacketSocketFactory;
26 class Thread;
27 }
28
29 namespace webrtc {
30 class PeerConnection;
31 }
32
33 namespace WebKit {
34 class WebFrame;
35 class WebMediaConstraints;
36 class WebMediaStream;
37 class WebRTCPeerConnectionHandler;
38 class WebRTCPeerConnectionHandlerClient;
39 }
40
41 namespace content {
42
43 class IpcNetworkManager;
44 class IpcPacketSocketFactory;
45 class RTCMediaConstraints;
46 class VideoCaptureImplManager;
47 class WebAudioCapturerSource;
48 class WebRtcAudioCapturer;
49 class WebRtcAudioDeviceImpl;
50 class WebRtcLoggingHandlerImpl;
51 class WebRtcLoggingMessageFilter;
52 struct StreamDeviceInfo;
53
54 #if defined(GOOGLE_TV)
55 class RTCVideoDecoderFactoryTv;
56 #endif
57
58 // Object factory for RTC MediaStreams and RTC PeerConnections.
59 class CONTENT_EXPORT MediaStreamDependencyFactory
60     : NON_EXPORTED_BASE(public base::NonThreadSafe) {
61  public:
62   // MediaSourcesCreatedCallback is used in CreateNativeMediaSources.
63   typedef base::Callback<void(WebKit::WebMediaStream* web_stream,
64                               bool live)> MediaSourcesCreatedCallback;
65   MediaStreamDependencyFactory(
66       VideoCaptureImplManager* vc_manager,
67       P2PSocketDispatcher* p2p_socket_dispatcher);
68   virtual ~MediaStreamDependencyFactory();
69
70   // Create a RTCPeerConnectionHandler object that implements the
71   // WebKit WebRTCPeerConnectionHandler interface.
72   WebKit::WebRTCPeerConnectionHandler* CreateRTCPeerConnectionHandler(
73       WebKit::WebRTCPeerConnectionHandlerClient* client);
74
75   // CreateNativeMediaSources creates libjingle representations of
76   // the underlying sources to the tracks in |web_stream|.
77   // |sources_created| is invoked when the sources have either been created and
78   // transitioned to a live state or failed.
79   // The libjingle sources is stored in the extra data field of
80   // WebMediaStreamSource.
81   // |audio_constraints| and |video_constraints| set parameters for the sources.
82   void CreateNativeMediaSources(
83       int render_view_id,
84       const WebKit::WebMediaConstraints& audio_constraints,
85       const WebKit::WebMediaConstraints& video_constraints,
86       WebKit::WebMediaStream* web_stream,
87       const MediaSourcesCreatedCallback& sources_created);
88
89   // Creates a libjingle representation of a MediaStream and stores
90   // it in the extra data field of |web_stream|.
91   void CreateNativeLocalMediaStream(
92       WebKit::WebMediaStream* web_stream);
93
94   // Creates a libjingle representation of a MediaStream and stores
95   // it in the extra data field of |web_stream|.
96   // |stream_stopped| is a callback that is run when a MediaStream have been
97   // stopped.
98   void CreateNativeLocalMediaStream(
99       WebKit::WebMediaStream* web_stream,
100       const MediaStreamExtraData::StreamStopCallback& stream_stop);
101
102   // Adds a libjingle representation of a MediaStreamTrack to |stream| based
103   // on the source of |track|.
104   bool AddNativeMediaStreamTrack(const WebKit::WebMediaStream& stream,
105                                  const WebKit::WebMediaStreamTrack& track);
106
107   // Creates and adds libjingle representation of a MediaStreamTrack to |stream|
108   // based on the desired |track_id| and |capturer|.
109   bool AddNativeVideoMediaTrack(const std::string& track_id,
110                                 WebKit::WebMediaStream* stream,
111                                 cricket::VideoCapturer* capturer);
112
113   bool RemoveNativeMediaStreamTrack(const WebKit::WebMediaStream& stream,
114                                     const WebKit::WebMediaStreamTrack& track);
115
116   // Asks the libjingle PeerConnection factory to create a libjingle
117   // PeerConnection object.
118   // The PeerConnection object is owned by PeerConnectionHandler.
119   virtual scoped_refptr<webrtc::PeerConnectionInterface>
120       CreatePeerConnection(
121           const webrtc::PeerConnectionInterface::IceServers& ice_servers,
122           const webrtc::MediaConstraintsInterface* constraints,
123           WebKit::WebFrame* web_frame,
124           webrtc::PeerConnectionObserver* observer);
125
126   // Creates a libjingle representation of a Session description. Used by a
127   // RTCPeerConnectionHandler instance.
128   virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
129       const std::string& type,
130       const std::string& sdp,
131       webrtc::SdpParseError* error);
132
133   // Creates a libjingle representation of an ice candidate.
134   virtual webrtc::IceCandidateInterface* CreateIceCandidate(
135       const std::string& sdp_mid,
136       int sdp_mline_index,
137       const std::string& sdp);
138
139   WebRtcAudioDeviceImpl* GetWebRtcAudioDevice();
140
141 #if defined(GOOGLE_TV)
142   RTCVideoDecoderFactoryTv* decoder_factory_tv() { return decoder_factory_tv_; }
143 #endif
144
145   static void AddNativeTrackToBlinkTrack(
146       webrtc::MediaStreamTrackInterface* native_track,
147       const WebKit::WebMediaStreamTrack& webkit_track);
148
149   static webrtc::MediaStreamInterface* GetNativeMediaStream(
150       const WebKit::WebMediaStream& stream);
151
152   static webrtc::MediaStreamTrackInterface* GetNativeMediaStreamTrack(
153       const WebKit::WebMediaStreamTrack& track);
154
155  protected:
156   // Asks the PeerConnection factory to create a Local MediaStream object.
157   virtual scoped_refptr<webrtc::MediaStreamInterface>
158       CreateLocalMediaStream(const std::string& label);
159
160   // Asks the PeerConnection factory to create a Local Audio Source.
161   virtual scoped_refptr<webrtc::AudioSourceInterface>
162       CreateLocalAudioSource(
163           const webrtc::MediaConstraintsInterface* constraints);
164
165   // Asks the PeerConnection factory to create a Local Video Source.
166   virtual scoped_refptr<webrtc::VideoSourceInterface>
167       CreateLocalVideoSource(
168           int video_session_id,
169           bool is_screen_cast,
170           const webrtc::MediaConstraintsInterface* constraints);
171
172   // Creates a media::AudioCapturerSource with an implementation that is
173   // specific for a WebAudio source. The created WebAudioCapturerSource
174   // instance will function as audio source instead of the default
175   // WebRtcAudioCapturer.
176   // The |constraints| will be modified to include the default, mandatory
177   // WebAudio constraints.
178   virtual scoped_refptr<WebAudioCapturerSource> CreateWebAudioSource(
179       WebKit::WebMediaStreamSource* source, RTCMediaConstraints* constraints);
180
181   // Asks the PeerConnection factory to create a Local AudioTrack object.
182   virtual scoped_refptr<webrtc::AudioTrackInterface>
183       CreateLocalAudioTrack(
184           const std::string& id,
185           const scoped_refptr<WebRtcAudioCapturer>& capturer,
186           WebAudioCapturerSource* webaudio_source,
187           webrtc::AudioSourceInterface* source,
188           const webrtc::MediaConstraintsInterface* constraints);
189
190   // Asks the PeerConnection factory to create a Local VideoTrack object.
191   virtual scoped_refptr<webrtc::VideoTrackInterface>
192       CreateLocalVideoTrack(const std::string& id,
193                             webrtc::VideoSourceInterface* source);
194
195   // Asks the PeerConnection factory to create a Local VideoTrack object with
196   // the video source using |capturer|.
197   virtual scoped_refptr<webrtc::VideoTrackInterface>
198       CreateLocalVideoTrack(const std::string& id,
199                             cricket::VideoCapturer* capturer);
200
201   virtual bool EnsurePeerConnectionFactory();
202   virtual bool PeerConnectionFactoryCreated();
203
204   // Returns a new capturer or existing capturer based on the |render_view_id|
205   // and |device_info|. When the |render_view_id| and |device_info| are valid,
206   // it reuses existing capture if any; otherwise it creates a new capturer.
207   virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer(
208       int render_view_id, const StreamDeviceInfo& device_info);
209
210  private:
211   // Creates and deletes |pc_factory_|, which in turn is used for
212   // creating PeerConnection objects.
213   bool CreatePeerConnectionFactory();
214
215   void InitializeWorkerThread(talk_base::Thread** thread,
216                               base::WaitableEvent* event);
217
218   void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent* event);
219   void DeleteIpcNetworkManager();
220   void CleanupPeerConnectionFactory();
221
222   // We own network_manager_, must be deleted on the worker thread.
223   // The network manager uses |p2p_socket_dispatcher_|.
224   IpcNetworkManager* network_manager_;
225   scoped_ptr<IpcPacketSocketFactory> socket_factory_;
226
227   scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
228
229 #if defined(GOOGLE_TV)
230   // |pc_factory_| will hold the ownership of this object, and |pc_factory_|
231   // outlives this object. Thus weak pointer is sufficient.
232   RTCVideoDecoderFactoryTv* decoder_factory_tv_;
233 #endif
234
235   scoped_refptr<VideoCaptureImplManager> vc_manager_;
236   scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
237   scoped_refptr<WebRtcAudioDeviceImpl> audio_device_;
238
239   // PeerConnection threads. signaling_thread_ is created from the
240   // "current" chrome thread.
241   talk_base::Thread* signaling_thread_;
242   talk_base::Thread* worker_thread_;
243   base::Thread chrome_worker_thread_;
244
245   DISALLOW_COPY_AND_ASSIGN(MediaStreamDependencyFactory);
246 };
247
248 }  // namespace content
249
250 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_