Home | History | Annotate | Download | only in media
      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 blink {
     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(blink::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   blink::WebRTCPeerConnectionHandler* CreateRTCPeerConnectionHandler(
     73       blink::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 blink::WebMediaConstraints& audio_constraints,
     85       const blink::WebMediaConstraints& video_constraints,
     86       blink::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       blink::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       blink::WebMediaStream* web_stream,
    100       const MediaStreamExtraData::StreamStopCallback& stream_stop);
    101 
    102   // Creates a libjingle representation of a MediaStreamTrack and stores
    103   // it in the extra data field of |track|.
    104   void CreateNativeMediaStreamTrack(const blink::WebMediaStreamTrack& track);
    105 
    106   // Adds a libjingle representation of a MediaStreamTrack to |stream| based
    107   // on the source of |track|.
    108   bool AddNativeMediaStreamTrack(const blink::WebMediaStream& stream,
    109                                  const blink::WebMediaStreamTrack& track);
    110 
    111   // Creates and adds libjingle representation of a MediaStreamTrack to |stream|
    112   // based on the desired |track_id| and |capturer|.
    113   bool AddNativeVideoMediaTrack(const std::string& track_id,
    114                                 blink::WebMediaStream* stream,
    115                                 cricket::VideoCapturer* capturer);
    116 
    117   bool RemoveNativeMediaStreamTrack(const blink::WebMediaStream& stream,
    118                                     const blink::WebMediaStreamTrack& track);
    119 
    120   // Asks the libjingle PeerConnection factory to create a libjingle
    121   // PeerConnection object.
    122   // The PeerConnection object is owned by PeerConnectionHandler.
    123   virtual scoped_refptr<webrtc::PeerConnectionInterface>
    124       CreatePeerConnection(
    125           const webrtc::PeerConnectionInterface::IceServers& ice_servers,
    126           const webrtc::MediaConstraintsInterface* constraints,
    127           blink::WebFrame* web_frame,
    128           webrtc::PeerConnectionObserver* observer);
    129 
    130   // Creates a libjingle representation of a Session description. Used by a
    131   // RTCPeerConnectionHandler instance.
    132   virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
    133       const std::string& type,
    134       const std::string& sdp,
    135       webrtc::SdpParseError* error);
    136 
    137   // Creates a libjingle representation of an ice candidate.
    138   virtual webrtc::IceCandidateInterface* CreateIceCandidate(
    139       const std::string& sdp_mid,
    140       int sdp_mline_index,
    141       const std::string& sdp);
    142 
    143   WebRtcAudioDeviceImpl* GetWebRtcAudioDevice();
    144 
    145 #if defined(GOOGLE_TV)
    146   RTCVideoDecoderFactoryTv* decoder_factory_tv() { return decoder_factory_tv_; }
    147 #endif
    148 
    149   static void AddNativeTrackToBlinkTrack(
    150       webrtc::MediaStreamTrackInterface* native_track,
    151       const blink::WebMediaStreamTrack& webkit_track,
    152       bool is_local_track);
    153 
    154   static webrtc::MediaStreamInterface* GetNativeMediaStream(
    155       const blink::WebMediaStream& stream);
    156 
    157   static webrtc::MediaStreamTrackInterface* GetNativeMediaStreamTrack(
    158       const blink::WebMediaStreamTrack& track);
    159 
    160  protected:
    161   // Asks the PeerConnection factory to create a Local MediaStream object.
    162   virtual scoped_refptr<webrtc::MediaStreamInterface>
    163       CreateLocalMediaStream(const std::string& label);
    164 
    165   // Asks the PeerConnection factory to create a Local Audio Source.
    166   virtual scoped_refptr<webrtc::AudioSourceInterface>
    167       CreateLocalAudioSource(
    168           const webrtc::MediaConstraintsInterface* constraints);
    169 
    170   // Asks the PeerConnection factory to create a Local Video Source.
    171   virtual scoped_refptr<webrtc::VideoSourceInterface>
    172       CreateLocalVideoSource(
    173           int video_session_id,
    174           bool is_screen_cast,
    175           const webrtc::MediaConstraintsInterface* constraints);
    176 
    177   // Creates a media::AudioCapturerSource with an implementation that is
    178   // specific for a WebAudio source. The created WebAudioCapturerSource
    179   // instance will function as audio source instead of the default
    180   // WebRtcAudioCapturer.
    181   // The |constraints| will be modified to include the default, mandatory
    182   // WebAudio constraints.
    183   virtual scoped_refptr<WebAudioCapturerSource> CreateWebAudioSource(
    184       blink::WebMediaStreamSource* source, RTCMediaConstraints* constraints);
    185 
    186   // Asks the PeerConnection factory to create a Local AudioTrack object.
    187   virtual scoped_refptr<webrtc::AudioTrackInterface>
    188       CreateLocalAudioTrack(
    189           const std::string& id,
    190           const scoped_refptr<WebRtcAudioCapturer>& capturer,
    191           WebAudioCapturerSource* webaudio_source,
    192           webrtc::AudioSourceInterface* source,
    193           const webrtc::MediaConstraintsInterface* constraints);
    194 
    195   // Asks the PeerConnection factory to create a Local VideoTrack object.
    196   virtual scoped_refptr<webrtc::VideoTrackInterface>
    197       CreateLocalVideoTrack(const std::string& id,
    198                             webrtc::VideoSourceInterface* source);
    199 
    200   // Asks the PeerConnection factory to create a Local VideoTrack object with
    201   // the video source using |capturer|.
    202   virtual scoped_refptr<webrtc::VideoTrackInterface>
    203       CreateLocalVideoTrack(const std::string& id,
    204                             cricket::VideoCapturer* capturer);
    205 
    206   virtual bool EnsurePeerConnectionFactory();
    207   virtual bool PeerConnectionFactoryCreated();
    208 
    209   // Returns a new capturer or existing capturer based on the |render_view_id|
    210   // and |device_info|. When the |render_view_id| and |device_info| are valid,
    211   // it reuses existing capture if any; otherwise it creates a new capturer.
    212   virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer(
    213       int render_view_id, const StreamDeviceInfo& device_info);
    214 
    215  private:
    216   // Creates and deletes |pc_factory_|, which in turn is used for
    217   // creating PeerConnection objects.
    218   bool CreatePeerConnectionFactory();
    219 
    220   void InitializeWorkerThread(talk_base::Thread** thread,
    221                               base::WaitableEvent* event);
    222 
    223   void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent* event);
    224   void DeleteIpcNetworkManager();
    225   void CleanupPeerConnectionFactory();
    226 
    227   scoped_refptr<webrtc::AudioTrackInterface>
    228   CreateNativeAudioMediaStreamTrack(const blink::WebMediaStreamTrack& track);
    229 
    230   scoped_refptr<webrtc::VideoTrackInterface>
    231   CreateNativeVideoMediaStreamTrack(const blink::WebMediaStreamTrack& track);
    232 
    233   // We own network_manager_, must be deleted on the worker thread.
    234   // The network manager uses |p2p_socket_dispatcher_|.
    235   IpcNetworkManager* network_manager_;
    236   scoped_ptr<IpcPacketSocketFactory> socket_factory_;
    237 
    238   scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
    239 
    240 #if defined(GOOGLE_TV)
    241   // |pc_factory_| will hold the ownership of this object, and |pc_factory_|
    242   // outlives this object. Thus weak pointer is sufficient.
    243   RTCVideoDecoderFactoryTv* decoder_factory_tv_;
    244 #endif
    245 
    246   scoped_refptr<VideoCaptureImplManager> vc_manager_;
    247   scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
    248   scoped_refptr<WebRtcAudioDeviceImpl> audio_device_;
    249 
    250   // PeerConnection threads. signaling_thread_ is created from the
    251   // "current" chrome thread.
    252   talk_base::Thread* signaling_thread_;
    253   talk_base::Thread* worker_thread_;
    254   base::Thread chrome_worker_thread_;
    255 
    256   DISALLOW_COPY_AND_ASSIGN(MediaStreamDependencyFactory);
    257 };
    258 
    259 }  // namespace content
    260 
    261 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
    262