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 "base/threading/thread_checker.h"
     17 #include "content/common/content_export.h"
     18 #include "content/common/media/media_stream_options.h"
     19 #include "content/public/renderer/render_frame_observer.h"
     20 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
     21 
     22 namespace base {
     23 class MessageLoopProxy;
     24 }
     25 
     26 namespace content {
     27 
     28 // MediaStreamDispatcher is a delegate for the Media Stream API messages.
     29 // MediaStreams are used by WebKit to open media devices such as Video Capture
     30 // and Audio input devices.
     31 // It's the complement of MediaStreamDispatcherHost (owned by
     32 // BrowserRenderProcessHost).
     33 class CONTENT_EXPORT MediaStreamDispatcher
     34     : public RenderFrameObserver,
     35       public base::SupportsWeakPtr<MediaStreamDispatcher> {
     36  public:
     37   explicit MediaStreamDispatcher(RenderFrame* render_frame);
     38   virtual ~MediaStreamDispatcher();
     39 
     40   // Request a new media stream to be created.
     41   // This can be used either by WebKit or a plugin.
     42   // Note: The event_handler must be valid for as long as the stream exists.
     43   virtual void GenerateStream(
     44       int request_id,
     45       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
     46       const StreamOptions& components,
     47       const GURL& security_origin);
     48 
     49   // Cancel the request for a new media stream to be created.
     50   virtual void CancelGenerateStream(
     51       int request_id,
     52       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
     53 
     54   // Stop a started device that has been requested by calling GenerateStream.
     55   virtual void StopStreamDevice(const StreamDeviceInfo& device_info);
     56 
     57   // Request to enumerate devices.
     58   virtual void EnumerateDevices(
     59       int request_id,
     60       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
     61       MediaStreamType type,
     62       const GURL& security_origin);
     63 
     64   // Request to stop enumerating devices.
     65   void StopEnumerateDevices(
     66       int request_id,
     67       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
     68 
     69   // Request to open a device.
     70   void OpenDevice(
     71       int request_id,
     72       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
     73       const std::string& device_id,
     74       MediaStreamType type,
     75       const GURL& security_origin);
     76 
     77   // Cancel the request to open a device.
     78   virtual void CancelOpenDevice(
     79       int request_id,
     80       const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
     81 
     82   // Close a started device. |label| is provided in OnDeviceOpened.
     83   void CloseDevice(const std::string& label);
     84 
     85   // Check if the label is a valid stream.
     86   virtual bool IsStream(const std::string& label);
     87   // Get the video session_id given a label. The label identifies a stream.
     88   // index is the index in the video_device_array of the stream.
     89   virtual int video_session_id(const std::string& label, int index);
     90   // Returns an audio session_id given a label and an index.
     91   virtual int audio_session_id(const std::string& label, int index);
     92 
     93   // Returns true if an audio input stream is currently active that was opened
     94   // with audio ducking enabled.  This is information is used when playing out
     95   // audio so that rendered audio can be excluded from the ducking operation.
     96   bool IsAudioDuckingActive() const;
     97 
     98  protected:
     99   int GetNextIpcIdForTest() { return next_ipc_id_; }
    100 
    101  private:
    102   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, BasicVideoDevice);
    103   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, TestFailure);
    104   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, CancelGenerateStream);
    105   FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest, CheckDuckingState);
    106 
    107   struct Request;
    108 
    109   // Private class for keeping track of opened devices and who have
    110   // opened it.
    111   struct Stream;
    112 
    113   // RenderFrameObserver OVERRIDE.
    114   virtual void OnDestruct() OVERRIDE;
    115   virtual bool Send(IPC::Message* message) OVERRIDE;
    116   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
    117 
    118   // Messages from the browser.
    119   void OnStreamGenerated(
    120       int request_id,
    121       const std::string& label,
    122       const StreamDeviceInfoArray& audio_array,
    123       const StreamDeviceInfoArray& video_array);
    124   void OnStreamGenerationFailed(
    125       int request_id,
    126       content::MediaStreamRequestResult result);
    127   void OnDeviceStopped(const std::string& label,
    128                        const StreamDeviceInfo& device_info);
    129   void OnDevicesEnumerated(
    130       int request_id,
    131       const StreamDeviceInfoArray& device_array);
    132   void OnDeviceOpened(
    133       int request_id,
    134       const std::string& label,
    135       const StreamDeviceInfo& device_info);
    136   void OnDeviceOpenFailed(int request_id);
    137 
    138   // Used for DCHECKs so methods calls won't execute in the wrong thread.
    139   base::ThreadChecker thread_checker_;
    140 
    141   int next_ipc_id_;
    142   typedef std::map<std::string, Stream> LabelStreamMap;
    143   LabelStreamMap label_stream_map_;
    144 
    145   // List of calls made to the browser process that have not yet completed or
    146   // been canceled.
    147   typedef std::list<Request> RequestList;
    148   RequestList requests_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher);
    151 };
    152 
    153 }  // namespace content
    154 
    155 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
    156