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_IMPL_H_
      6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/scoped_vector.h"
     16 #include "base/memory/weak_ptr.h"
     17 #include "base/threading/non_thread_safe.h"
     18 #include "content/common/content_export.h"
     19 #include "content/public/renderer/render_view_observer.h"
     20 #include "content/renderer/media/media_stream_client.h"
     21 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
     22 #include "third_party/WebKit/public/platform/WebMediaStream.h"
     23 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
     24 #include "third_party/WebKit/public/platform/WebVector.h"
     25 #include "third_party/WebKit/public/web/WebUserMediaClient.h"
     26 #include "third_party/WebKit/public/web/WebUserMediaRequest.h"
     27 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
     28 
     29 namespace content {
     30 class MediaStreamAudioRenderer;
     31 class MediaStreamDependencyFactory;
     32 class MediaStreamDispatcher;
     33 class MediaStreamSourceExtraData;
     34 class WebRtcAudioRenderer;
     35 class WebRtcLocalAudioRenderer;
     36 
     37 // MediaStreamImpl is a delegate for the Media Stream API messages used by
     38 // WebKit. It ties together WebKit, native PeerConnection in libjingle and
     39 // MediaStreamManager (via MediaStreamDispatcher and MediaStreamDispatcherHost)
     40 // in the browser process. It must be created, called and destroyed on the
     41 // render thread.
     42 // MediaStreamImpl have weak pointers to a MediaStreamDispatcher.
     43 class CONTENT_EXPORT MediaStreamImpl
     44     : public RenderViewObserver,
     45       NON_EXPORTED_BASE(public blink::WebUserMediaClient),
     46       NON_EXPORTED_BASE(public MediaStreamClient),
     47       public MediaStreamDispatcherEventHandler,
     48       public base::SupportsWeakPtr<MediaStreamImpl>,
     49       NON_EXPORTED_BASE(public base::NonThreadSafe) {
     50  public:
     51   MediaStreamImpl(
     52       RenderView* render_view,
     53       MediaStreamDispatcher* media_stream_dispatcher,
     54       MediaStreamDependencyFactory* dependency_factory);
     55   virtual ~MediaStreamImpl();
     56 
     57   // blink::WebUserMediaClient implementation
     58   virtual void requestUserMedia(
     59       const blink::WebUserMediaRequest& user_media_request) OVERRIDE;
     60   virtual void cancelUserMediaRequest(
     61       const blink::WebUserMediaRequest& user_media_request) OVERRIDE;
     62 
     63   // MediaStreamClient implementation.
     64   virtual bool IsMediaStream(const GURL& url) OVERRIDE;
     65   virtual scoped_refptr<VideoFrameProvider> GetVideoFrameProvider(
     66       const GURL& url,
     67       const base::Closure& error_cb,
     68       const VideoFrameProvider::RepaintCB& repaint_cb) OVERRIDE;
     69   virtual scoped_refptr<MediaStreamAudioRenderer>
     70       GetAudioRenderer(const GURL& url) OVERRIDE;
     71 
     72   // MediaStreamDispatcherEventHandler implementation.
     73   virtual void OnStreamGenerated(
     74       int request_id,
     75       const std::string& label,
     76       const StreamDeviceInfoArray& audio_array,
     77       const StreamDeviceInfoArray& video_array) OVERRIDE;
     78   virtual void OnStreamGenerationFailed(int request_id) OVERRIDE;
     79   virtual void OnDeviceStopped(const std::string& label,
     80                                const StreamDeviceInfo& device_info) OVERRIDE;
     81   virtual void OnDevicesEnumerated(
     82       int request_id,
     83       const StreamDeviceInfoArray& device_array) OVERRIDE;
     84   virtual void OnDeviceOpened(
     85       int request_id,
     86       const std::string& label,
     87       const StreamDeviceInfo& device_info) OVERRIDE;
     88   virtual void OnDeviceOpenFailed(int request_id) OVERRIDE;
     89 
     90   // RenderViewObserver OVERRIDE
     91   virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE;
     92   virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE;
     93 
     94  protected:
     95   void OnLocalSourceStop(const blink::WebMediaStreamSource& source);
     96 
     97   void OnLocalMediaStreamStop(const std::string& label);
     98 
     99   // Callback function triggered when all native (libjingle) versions of the
    100   // underlying media sources have been created and started.
    101   // |web_stream| is a raw pointer to the web_stream in
    102   // UserMediaRequests::web_stream for which the underlying sources have been
    103   // created.
    104   void OnCreateNativeSourcesComplete(
    105       blink::WebMediaStream* web_stream,
    106       bool request_succeeded);
    107 
    108   // This function is virtual for test purposes. A test can override this to
    109   // test requesting local media streams. The function notifies WebKit that the
    110   // |request| have completed and generated the MediaStream |stream|.
    111   virtual void CompleteGetUserMediaRequest(
    112       const blink::WebMediaStream& stream,
    113       blink::WebUserMediaRequest* request_info,
    114       bool request_succeeded);
    115 
    116   // Returns the WebKit representation of a MediaStream given an URL.
    117   // This is virtual for test purposes.
    118   virtual blink::WebMediaStream GetMediaStream(const GURL& url);
    119 
    120  private:
    121   // Structure for storing information about a WebKit request to create a
    122   // MediaStream.
    123   struct UserMediaRequestInfo {
    124     UserMediaRequestInfo(int request_id,
    125                          blink::WebFrame* frame,
    126                          const blink::WebUserMediaRequest& request,
    127                          bool enable_automatic_output_device_selection);
    128     ~UserMediaRequestInfo();
    129     int request_id;
    130     // True if MediaStreamDispatcher has generated the stream, see
    131     // OnStreamGenerated.
    132     bool generated;
    133     const bool enable_automatic_output_device_selection;
    134     blink::WebFrame* frame;  // WebFrame that requested the MediaStream.
    135     blink::WebMediaStream web_stream;
    136     blink::WebUserMediaRequest request;
    137     std::vector<blink::WebMediaStreamSource> sources;
    138   };
    139   typedef ScopedVector<UserMediaRequestInfo> UserMediaRequests;
    140 
    141   struct LocalStreamSource {
    142     LocalStreamSource(blink::WebFrame* frame,
    143                       const blink::WebMediaStreamSource& source)
    144         : frame(frame), source(source) {
    145     }
    146     // |frame| is the WebFrame that requested |source|. NULL in unit tests.
    147     // TODO(perkj): Change so that |frame| is not NULL in unit tests.
    148     blink::WebFrame* frame;
    149     blink::WebMediaStreamSource source;
    150   };
    151   typedef std::vector<LocalStreamSource> LocalStreamSources;
    152 
    153   // Creates a WebKit representation of stream sources based on
    154   // |devices| from the MediaStreamDispatcher.
    155   void CreateWebKitSourceVector(
    156       const std::string& label,
    157       const StreamDeviceInfoArray& devices,
    158       blink::WebMediaStreamSource::Type type,
    159       blink::WebFrame* frame,
    160       blink::WebVector<blink::WebMediaStreamSource>& webkit_sources);
    161 
    162   UserMediaRequestInfo* FindUserMediaRequestInfo(int request_id);
    163   UserMediaRequestInfo* FindUserMediaRequestInfo(
    164       blink::WebMediaStream* web_stream);
    165   UserMediaRequestInfo* FindUserMediaRequestInfo(
    166       const blink::WebUserMediaRequest& request);
    167   UserMediaRequestInfo* FindUserMediaRequestInfo(const std::string& label);
    168   void DeleteUserMediaRequestInfo(UserMediaRequestInfo* request);
    169 
    170   // Returns the source that use a device with |device.session_id|
    171   // and |device.device.id|. NULL if such source doesn't exist.
    172   const blink::WebMediaStreamSource* FindLocalSource(
    173       const StreamDeviceInfo& device) const;
    174 
    175   // Returns true if |source| exists in |user_media_requests_|
    176   bool FindSourceInRequests(const blink::WebMediaStreamSource& source) const;
    177 
    178   void StopLocalSource(const blink::WebMediaStreamSource& source,
    179                        bool notify_dispatcher);
    180   // Stops all local sources that don't exist in exist in
    181   // |user_media_requests_|.
    182   void StopUnreferencedSources(bool notify_dispatcher);
    183 
    184   scoped_refptr<WebRtcAudioRenderer> CreateRemoteAudioRenderer(
    185       webrtc::MediaStreamInterface* stream);
    186   scoped_refptr<WebRtcLocalAudioRenderer> CreateLocalAudioRenderer(
    187       const blink::WebMediaStreamTrack& audio_track);
    188 
    189   // Returns a valid session id if a single capture device is currently open
    190   // (and then the matching session_id), otherwise -1.
    191   // This is used to pass on a session id to a webrtc audio renderer (either
    192   // local or remote), so that audio will be rendered to a matching output
    193   // device, should one exist.
    194   // Note that if there are more than one open capture devices the function
    195   // will not be able to pick an appropriate device and return false.
    196   bool GetAuthorizedDeviceInfoForAudioRenderer(
    197       int* session_id, int* output_sample_rate, int* output_buffer_size);
    198 
    199   // Weak ref to a MediaStreamDependencyFactory, owned by the RenderThread.
    200   // It's valid for the lifetime of RenderThread.
    201   MediaStreamDependencyFactory* dependency_factory_;
    202 
    203   // media_stream_dispatcher_ is a weak reference, owned by RenderView. It's
    204   // valid for the lifetime of RenderView.
    205   MediaStreamDispatcher* media_stream_dispatcher_;
    206 
    207   UserMediaRequests user_media_requests_;
    208 
    209   LocalStreamSources local_sources_;
    210 
    211   DISALLOW_COPY_AND_ASSIGN(MediaStreamImpl);
    212 };
    213 
    214 }  // namespace content
    215 
    216 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_IMPL_H_
    217