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_DISPATCHER_H_
      6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
      7 
      8 #include <list>
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/gtest_prod_util.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "content/common/content_export.h"
     17 #include "content/common/media/media_stream_options.h"
     18 #include "content/public/renderer/render_view_observer.h"
     19 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
     20 
     21 namespace base {
     22 class MessageLoopProxy;
     23 }
     24 
     25 namespace content {
     26 
     27 class RenderViewImpl;
     28 
     29 // MediaStreamDispatcher is a delegate for the Media Stream API messages.
     30 // MediaStreams are used by WebKit to open media devices such as Video Capture
     31 // and Audio input devices.
     32 // It's the complement of MediaStreamDispatcherHost (owned by
     33 // BrowserRenderProcessHost).
     34 class CONTENT_EXPORT MediaStreamDispatcher
     35     : public RenderViewObserver,
     36       public base::SupportsWeakPtr<MediaStreamDispatcher> {
     37  public:
     38   explicit MediaStreamDispatcher(RenderViewImpl* render_view);
     39   virtual ~MediaStreamDispatcher();
     40 
     41   // Request a new media stream to be created.
     42   // This can be used either by WebKit or a plugin.
     43   // Note: The event_handler must be valid for as long as the stream exists.
     44   virtual void GenerateStream(
     45       int request_id,
     46       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
     47       const StreamOptions& components,
     48       const GURL& security_origin);
     49 
     50   // Cancel the request for a new media stream to be created.
     51   virtual void CancelGenerateStream(
     52       int request_id,
     53       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
     54 
     55   // Stop a started stream. Label is the label provided in OnStreamGenerated.
     56   virtual void StopStream(const std::string& label);
     57 
     58   // Request to enumerate devices.
     59   void EnumerateDevices(
     60       int request_id,
     61       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
     62       MediaStreamType type,
     63       const GURL& security_origin);
     64 
     65   // Request to stop enumerating devices.
     66   void StopEnumerateDevices(
     67       int request_id,
     68       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
     69 
     70   // Request to open a device.
     71   void OpenDevice(
     72       int request_id,
     73       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
     74       const std::string& device_id,
     75       MediaStreamType type,
     76       const GURL& security_origin);
     77 
     78   // Cancel the request to open a device.
     79   virtual void CancelOpenDevice(
     80       int request_id,
     81       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
     82 
     83   // Close a started device. |label| is provided in OnDeviceOpened.
     84   void CloseDevice(const std::string& label);
     85 
     86   // Check if the label is a valid stream.
     87   virtual bool IsStream(const std::string& label);
     88   // Get the video session_id given a label. The label identifies a stream.
     89   // index is the index in the video_device_array of the stream.
     90   virtual int video_session_id(const std::string& label, int index);
     91   // Returns an audio session_id given a label and an index.
     92   virtual int audio_session_id(const std::string& label, int index);
     93 
     94  private:
     95   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicStream);
     96   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicStreamForDevice);
     97   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicVideoDevice);
     98   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, TestFailure);
     99   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, CancelGenerateStream);
    100 
    101   struct Request;
    102 
    103   // Private class for keeping track of opened devices and who have
    104   // opened it.
    105   struct Stream;
    106 
    107   // An enumeration request is identified by pair (request_id, handler).
    108   // It allows multiple clients to make requests and each client could have
    109   // its own request_id sequence.
    110   struct EnumerationRequest {
    111     EnumerationRequest(
    112         const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler,
    113         int request_id);
    114     ~EnumerationRequest();
    115     bool IsThisRequest(
    116         int request_id,
    117         const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler);
    118 
    119     base::WeakPtr<MediaStreamDispatcherEventHandler> handler;
    120     int request_id;
    121   };
    122 
    123   // List of requests made to EnumerateDevices.
    124   typedef std::list<EnumerationRequest> EnumerationRequestList;
    125 
    126   struct EnumerationState {
    127     EnumerationState();
    128     ~EnumerationState();
    129 
    130     struct CachedDevices;
    131 
    132     // If |ipc_id| >= 0, then we've started.
    133     int ipc_id;
    134     scoped_ptr<CachedDevices> cached_devices;
    135     EnumerationRequestList requests;
    136   };
    137 
    138   // RenderViewObserver OVERRIDE.
    139   virtual bool Send(IPC::Message* message) OVERRIDE;
    140 
    141   // Messages from the browser.
    142   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
    143   void OnStreamGenerated(
    144       int request_id,
    145       const std::string& label,
    146       const StreamDeviceInfoArray& audio_array,
    147       const StreamDeviceInfoArray& video_array);
    148   void OnStreamGenerationFailed(int request_id);
    149   void OnStopGeneratedStream(const std::string& label);
    150   void OnDevicesEnumerated(
    151       int request_id,
    152       const std::string& label,
    153       const StreamDeviceInfoArray& device_array);
    154   void OnDevicesEnumerationFailed(int request_id);
    155   void OnDeviceOpened(
    156       int request_id,
    157       const std::string& label,
    158       const StreamDeviceInfo& device_info);
    159   void OnDeviceOpenFailed(int request_id);
    160 
    161   void RemoveEnumerationRequest(
    162       int request_id,
    163       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
    164       EnumerationState* state);
    165 
    166   // Used for DCHECKs so methods calls won't execute in the wrong thread.
    167   scoped_refptr<base::MessageLoopProxy> main_loop_;
    168 
    169   int next_ipc_id_;
    170   typedef std::map<std::string, Stream> LabelStreamMap;
    171   LabelStreamMap label_stream_map_;
    172 
    173   EnumerationState audio_enumeration_state_;
    174   EnumerationState video_enumeration_state_;
    175 
    176   // List of calls made to GenerateStream that have not yet completed.
    177   typedef std::list<Request> RequestList;
    178   RequestList requests_;
    179 
    180   DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher);
    181 };
    182 
    183 }  // namespace content
    184 
    185 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
    186