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
21namespace base {
22class MessageLoopProxy;
23}
24
25namespace content {
26
27class 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).
34class 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  // If |hide_labels_if_no_access| is true, labels will be empty in the
60  // response if permission has not been granted for the device type. This
61  // should normally be true.
62  virtual void EnumerateDevices(
63      int request_id,
64      const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
65      MediaStreamType type,
66      const GURL& security_origin,
67      bool hide_labels_if_no_access);
68
69  // Request to stop enumerating devices.
70  void StopEnumerateDevices(
71      int request_id,
72      const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
73
74  // Request to open a device.
75  void OpenDevice(
76      int request_id,
77      const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
78      const std::string& device_id,
79      MediaStreamType type,
80      const GURL& security_origin);
81
82  // Cancel the request to open a device.
83  virtual void CancelOpenDevice(
84      int request_id,
85      const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
86
87  // Close a started device. |label| is provided in OnDeviceOpened.
88  void CloseDevice(const std::string& label);
89
90  // Check if the label is a valid stream.
91  virtual bool IsStream(const std::string& label);
92  // Get the video session_id given a label. The label identifies a stream.
93  // index is the index in the video_device_array of the stream.
94  virtual int video_session_id(const std::string& label, int index);
95  // Returns an audio session_id given a label and an index.
96  virtual int audio_session_id(const std::string& label, int index);
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
106  struct Request;
107
108  // Private class for keeping track of opened devices and who have
109  // opened it.
110  struct Stream;
111
112  // RenderViewObserver OVERRIDE.
113  virtual bool Send(IPC::Message* message) OVERRIDE;
114
115  // Messages from the browser.
116  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
117  void OnStreamGenerated(
118      int request_id,
119      const std::string& label,
120      const StreamDeviceInfoArray& audio_array,
121      const StreamDeviceInfoArray& video_array);
122  void OnStreamGenerationFailed(
123      int request_id,
124      content::MediaStreamRequestResult result);
125  void OnDeviceStopped(const std::string& label,
126                       const StreamDeviceInfo& device_info);
127  void OnDevicesEnumerated(
128      int request_id,
129      const StreamDeviceInfoArray& device_array);
130  void OnDeviceOpened(
131      int request_id,
132      const std::string& label,
133      const StreamDeviceInfo& device_info);
134  void OnDeviceOpenFailed(int request_id);
135
136  // Used for DCHECKs so methods calls won't execute in the wrong thread.
137  scoped_refptr<base::MessageLoopProxy> main_loop_;
138
139  int next_ipc_id_;
140  typedef std::map<std::string, Stream> LabelStreamMap;
141  LabelStreamMap label_stream_map_;
142
143  // List of calls made to the browser process that have not yet completed or
144  // been canceled.
145  typedef std::list<Request> RequestList;
146  RequestList requests_;
147
148  DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher);
149};
150
151}  // namespace content
152
153#endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
154