Home | History | Annotate | Download | only in webrtc
      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.
      4 
      5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
      6 #define CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/files/file.h"
     12 #include "base/threading/thread.h"
     13 #include "content/common/content_export.h"
     14 #include "content/public/renderer/render_process_observer.h"
     15 #include "content/renderer/media/aec_dump_message_filter.h"
     16 #include "content/renderer/p2p/socket_dispatcher.h"
     17 #include "ipc/ipc_platform_file.h"
     18 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
     19 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h"
     20 
     21 namespace base {
     22 class WaitableEvent;
     23 }
     24 
     25 namespace talk_base {
     26 class NetworkManager;
     27 class PacketSocketFactory;
     28 class Thread;
     29 }
     30 
     31 namespace blink {
     32 class WebFrame;
     33 class WebMediaConstraints;
     34 class WebMediaStream;
     35 class WebMediaStreamSource;
     36 class WebMediaStreamTrack;
     37 class WebRTCPeerConnectionHandler;
     38 class WebRTCPeerConnectionHandlerClient;
     39 }
     40 
     41 namespace content {
     42 
     43 class IpcNetworkManager;
     44 class IpcPacketSocketFactory;
     45 class MediaStreamAudioSource;
     46 class RTCMediaConstraints;
     47 class WebAudioCapturerSource;
     48 class WebRtcAudioCapturer;
     49 class WebRtcAudioDeviceImpl;
     50 class WebRtcLocalAudioTrack;
     51 class WebRtcLoggingHandlerImpl;
     52 class WebRtcLoggingMessageFilter;
     53 class WebRtcVideoCapturerAdapter;
     54 struct StreamDeviceInfo;
     55 
     56 // Object factory for RTC PeerConnections.
     57 class CONTENT_EXPORT PeerConnectionDependencyFactory
     58     : NON_EXPORTED_BASE(public base::NonThreadSafe),
     59       NON_EXPORTED_BASE(public AecDumpMessageFilter::AecDumpDelegate) {
     60  public:
     61   PeerConnectionDependencyFactory(
     62       P2PSocketDispatcher* p2p_socket_dispatcher);
     63   virtual ~PeerConnectionDependencyFactory();
     64 
     65   // Create a RTCPeerConnectionHandler object that implements the
     66   // WebKit WebRTCPeerConnectionHandler interface.
     67   blink::WebRTCPeerConnectionHandler* CreateRTCPeerConnectionHandler(
     68       blink::WebRTCPeerConnectionHandlerClient* client);
     69 
     70   // Asks the PeerConnection factory to create a Local MediaStream object.
     71   virtual scoped_refptr<webrtc::MediaStreamInterface>
     72       CreateLocalMediaStream(const std::string& label);
     73 
     74   // InitializeMediaStreamAudioSource initialize a MediaStream source object
     75   // for audio input.
     76   bool InitializeMediaStreamAudioSource(
     77       int render_view_id,
     78       const blink::WebMediaConstraints& audio_constraints,
     79       MediaStreamAudioSource* source_data);
     80 
     81   // Creates an implementation of a cricket::VideoCapturer object that can be
     82   // used when creating a libjingle webrtc::VideoSourceInterface object.
     83   virtual WebRtcVideoCapturerAdapter* CreateVideoCapturer(
     84       bool is_screen_capture);
     85 
     86   // Create an instance of WebRtcLocalAudioTrack and store it
     87   // in the extraData field of |track|.
     88   void CreateLocalAudioTrack(const blink::WebMediaStreamTrack& track);
     89 
     90   // Asks the PeerConnection factory to create a Local VideoTrack object.
     91   virtual scoped_refptr<webrtc::VideoTrackInterface>
     92       CreateLocalVideoTrack(const std::string& id,
     93                             webrtc::VideoSourceInterface* source);
     94 
     95   // Asks the PeerConnection factory to create a Video Source.
     96   // The video source takes ownership of |capturer|.
     97   virtual scoped_refptr<webrtc::VideoSourceInterface>
     98       CreateVideoSource(cricket::VideoCapturer* capturer,
     99                         const blink::WebMediaConstraints& constraints);
    100 
    101   // Asks the libjingle PeerConnection factory to create a libjingle
    102   // PeerConnection object.
    103   // The PeerConnection object is owned by PeerConnectionHandler.
    104   virtual scoped_refptr<webrtc::PeerConnectionInterface>
    105       CreatePeerConnection(
    106           const webrtc::PeerConnectionInterface::IceServers& ice_servers,
    107           const webrtc::MediaConstraintsInterface* constraints,
    108           blink::WebFrame* web_frame,
    109           webrtc::PeerConnectionObserver* observer);
    110 
    111   // Creates a libjingle representation of a Session description. Used by a
    112   // RTCPeerConnectionHandler instance.
    113   virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
    114       const std::string& type,
    115       const std::string& sdp,
    116       webrtc::SdpParseError* error);
    117 
    118   // Creates a libjingle representation of an ice candidate.
    119   virtual webrtc::IceCandidateInterface* CreateIceCandidate(
    120       const std::string& sdp_mid,
    121       int sdp_mline_index,
    122       const std::string& sdp);
    123 
    124   WebRtcAudioDeviceImpl* GetWebRtcAudioDevice();
    125 
    126   static void AddNativeAudioTrackToBlinkTrack(
    127       webrtc::MediaStreamTrackInterface* native_track,
    128       const blink::WebMediaStreamTrack& webkit_track,
    129       bool is_local_track);
    130 
    131   scoped_refptr<base::MessageLoopProxy> GetWebRtcWorkerThread() const;
    132 
    133   // AecDumpMessageFilter::AecDumpDelegate implementation.
    134   // TODO(xians): Remove when option to disable audio track processing is
    135   // removed.
    136   virtual void OnAecDumpFile(
    137       const IPC::PlatformFileForTransit& file_handle) OVERRIDE;
    138   virtual void OnDisableAecDump() OVERRIDE;
    139   virtual void OnIpcClosing() OVERRIDE;
    140 
    141  protected:
    142   // Asks the PeerConnection factory to create a Local Audio Source.
    143   virtual scoped_refptr<webrtc::AudioSourceInterface>
    144       CreateLocalAudioSource(
    145           const webrtc::MediaConstraintsInterface* constraints);
    146 
    147   // Creates a media::AudioCapturerSource with an implementation that is
    148   // specific for a WebAudio source. The created WebAudioCapturerSource
    149   // instance will function as audio source instead of the default
    150   // WebRtcAudioCapturer.
    151   virtual scoped_refptr<WebAudioCapturerSource> CreateWebAudioSource(
    152       blink::WebMediaStreamSource* source);
    153 
    154   // Asks the PeerConnection factory to create a Local VideoTrack object with
    155   // the video source using |capturer|.
    156   virtual scoped_refptr<webrtc::VideoTrackInterface>
    157       CreateLocalVideoTrack(const std::string& id,
    158                             cricket::VideoCapturer* capturer);
    159 
    160   virtual const scoped_refptr<webrtc::PeerConnectionFactoryInterface>&
    161       GetPcFactory();
    162   virtual bool PeerConnectionFactoryCreated();
    163 
    164   // Returns a new capturer or existing capturer based on the |render_view_id|
    165   // and |device_info|. When the |render_view_id| and |device_info| are valid,
    166   // it reuses existing capture if any; otherwise it creates a new capturer.
    167   virtual scoped_refptr<WebRtcAudioCapturer> CreateAudioCapturer(
    168       int render_view_id, const StreamDeviceInfo& device_info,
    169       const blink::WebMediaConstraints& constraints,
    170       MediaStreamAudioSource* audio_source);
    171 
    172   // Adds the audio device as a sink to the audio track and starts the local
    173   // audio track. This is virtual for test purposes since no real audio device
    174   // exist in unit tests.
    175   virtual void StartLocalAudioTrack(WebRtcLocalAudioTrack* audio_track);
    176 
    177  private:
    178   // Creates |pc_factory_|, which in turn is used for
    179   // creating PeerConnection objects.
    180   void CreatePeerConnectionFactory();
    181 
    182   void InitializeWorkerThread(talk_base::Thread** thread,
    183                               base::WaitableEvent* event);
    184 
    185   void CreateIpcNetworkManagerOnWorkerThread(base::WaitableEvent* event);
    186   void DeleteIpcNetworkManager();
    187   void CleanupPeerConnectionFactory();
    188 
    189   // Helper method to create a WebRtcAudioDeviceImpl.
    190   void EnsureWebRtcAudioDeviceImpl();
    191 
    192   // We own network_manager_, must be deleted on the worker thread.
    193   // The network manager uses |p2p_socket_dispatcher_|.
    194   IpcNetworkManager* network_manager_;
    195   scoped_ptr<IpcPacketSocketFactory> socket_factory_;
    196 
    197   scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
    198 
    199   scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
    200   scoped_refptr<WebRtcAudioDeviceImpl> audio_device_;
    201 
    202   // This is only used if audio track processing is disabled.
    203   // TODO(xians): Remove when option to disable audio track processing is
    204   // removed.
    205   scoped_refptr<AecDumpMessageFilter> aec_dump_message_filter_;
    206 
    207   // PeerConnection threads. signaling_thread_ is created from the
    208   // "current" chrome thread.
    209   talk_base::Thread* signaling_thread_;
    210   talk_base::Thread* worker_thread_;
    211   base::Thread chrome_worker_thread_;
    212 
    213   DISALLOW_COPY_AND_ASSIGN(PeerConnectionDependencyFactory);
    214 };
    215 
    216 }  // namespace content
    217 
    218 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
    219