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.
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_
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"
22 class MessageLoopProxy;
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> {
38 explicit MediaStreamDispatcher(RenderViewImpl* render_view);
39 virtual ~MediaStreamDispatcher();
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(
46 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
47 const StreamOptions& components,
48 const GURL& security_origin);
50 // Cancel the request for a new media stream to be created.
51 virtual void CancelGenerateStream(
53 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
55 // Stop a started device that has been requested by calling GenerateStream.
56 virtual void StopStreamDevice(const StreamDeviceInfo& device_info);
58 // Request to enumerate devices.
59 void EnumerateDevices(
61 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
63 const GURL& security_origin);
65 // Request to stop enumerating devices.
66 void StopEnumerateDevices(
68 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
70 // Request to open a device.
73 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
74 const std::string& device_id,
76 const GURL& security_origin);
78 // Cancel the request to open a device.
79 virtual void CancelOpenDevice(
81 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler);
83 // Close a started device. |label| is provided in OnDeviceOpened.
84 void CloseDevice(const std::string& label);
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);
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);
103 // Private class for keeping track of opened devices and who have
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 {
112 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler,
114 ~EnumerationRequest();
117 const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler);
119 base::WeakPtr<MediaStreamDispatcherEventHandler> handler;
123 // List of requests made to EnumerateDevices.
124 typedef std::list<EnumerationRequest> EnumerationRequestList;
126 struct EnumerationState {
130 struct CachedDevices;
132 // If |ipc_id| >= 0, then we've started.
134 scoped_ptr<CachedDevices> cached_devices;
135 EnumerationRequestList requests;
138 // RenderViewObserver OVERRIDE.
139 virtual bool Send(IPC::Message* message) OVERRIDE;
141 // Messages from the browser.
142 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
143 void OnStreamGenerated(
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(
152 const std::string& label,
153 const StreamDeviceInfoArray& device_array);
154 void OnDevicesEnumerationFailed(int request_id);
157 const std::string& label,
158 const StreamDeviceInfo& device_info);
159 void OnDeviceOpenFailed(int request_id);
161 void RemoveEnumerationRequest(
163 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
164 EnumerationState* state);
166 // Used for DCHECKs so methods calls won't execute in the wrong thread.
167 scoped_refptr<base::MessageLoopProxy> main_loop_;
170 typedef std::map<std::string, Stream> LabelStreamMap;
171 LabelStreamMap label_stream_map_;
173 EnumerationState audio_enumeration_state_;
174 EnumerationState video_enumeration_state_;
176 // List of calls made to GenerateStream that have not yet completed.
177 typedef std::list<Request> RequestList;
178 RequestList requests_;
180 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher);
183 } // namespace content
185 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_