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 device that has been requested by calling GenerateStream.
     56   virtual void StopStreamDevice(const StreamDeviceInfo& device_info);
     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  protected:
     95   int GetNextIpcIdForTest() { return next_ipc_id_; }
     96 
     97  private:
     98   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicVideoDevice);
     99   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, TestFailure);
    100   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, CancelGenerateStream);
    101 
    102   struct Request;
    103 
    104   // Private class for keeping track of opened devices and who have
    105   // opened it.
    106   struct Stream;
    107 
    108   // RenderViewObserver OVERRIDE.
    109   virtual bool Send(IPC::Message* message) OVERRIDE;
    110 
    111   // Messages from the browser.
    112   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
    113   void OnStreamGenerated(
    114       int request_id,
    115       const std::string& label,
    116       const StreamDeviceInfoArray& audio_array,
    117       const StreamDeviceInfoArray& video_array);
    118   void OnStreamGenerationFailed(int request_id);
    119   void OnDeviceStopped(const std::string& label,
    120                        const StreamDeviceInfo& device_info);
    121   void OnDevicesEnumerated(
    122       int request_id,
    123       const StreamDeviceInfoArray& device_array);
    124   void OnDevicesEnumerationFailed(int request_id);
    125   void OnDeviceOpened(
    126       int request_id,
    127       const std::string& label,
    128       const StreamDeviceInfo& device_info);
    129   void OnDeviceOpenFailed(int request_id);
    130 
    131   // Used for DCHECKs so methods calls won't execute in the wrong thread.
    132   scoped_refptr<base::MessageLoopProxy> main_loop_;
    133 
    134   int next_ipc_id_;
    135   typedef std::map<std::string, Stream> LabelStreamMap;
    136   LabelStreamMap label_stream_map_;
    137 
    138   // List of calls made to the browser process that have not yet completed or
    139   // been canceled.
    140   typedef std::list<Request> RequestList;
    141   RequestList requests_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher);
    144 };
    145 
    146 }  // namespace content
    147 
    148 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
    149