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(int request_id
);
53 // Stop a started stream. Label is the label provided in OnStreamGenerated.
54 virtual void StopStream(const std::string
& label
);
56 // Request to enumerate devices.
57 void EnumerateDevices(
59 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
61 const GURL
& security_origin
);
63 // Request to stop enumerating devices.
64 void StopEnumerateDevices(
66 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
68 // Request to open a device.
71 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
72 const std::string
& device_id
,
74 const GURL
& security_origin
);
76 // Close a started device. |label| is provided in OnDeviceOpened.
77 void CloseDevice(const std::string
& label
);
79 // Check if the label is a valid stream.
80 virtual bool IsStream(const std::string
& label
);
81 // Get the video session_id given a label. The label identifies a stream.
82 // index is the index in the video_device_array of the stream.
83 virtual int video_session_id(const std::string
& label
, int index
);
84 // Returns an audio session_id given a label and an index.
85 virtual int audio_session_id(const std::string
& label
, int index
);
88 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, BasicStream
);
89 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, BasicStreamForDevice
);
90 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, BasicVideoDevice
);
91 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, TestFailure
);
92 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, CancelGenerateStream
);
96 // Private class for keeping track of opened devices and who have
100 struct EnumerationRequest
{
102 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& handler
,
104 ~EnumerationRequest();
106 base::WeakPtr
<MediaStreamDispatcherEventHandler
> handler
;
110 // List of requests made to EnumerateDevices.
111 typedef std::list
<EnumerationRequest
> EnumerationRequestList
;
113 struct EnumerationState
{
117 struct CachedDevices
;
119 // If |ipc_id| >= 0, then we've started.
121 scoped_ptr
<CachedDevices
> cached_devices
;
122 EnumerationRequestList requests
;
125 // Messages from the browser.
126 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
127 void OnStreamGenerated(
129 const std::string
& label
,
130 const StreamDeviceInfoArray
& audio_array
,
131 const StreamDeviceInfoArray
& video_array
);
132 void OnStreamGenerationFailed(int request_id
);
133 void OnDevicesEnumerated(
135 const std::string
& label
,
136 const StreamDeviceInfoArray
& device_array
);
137 void OnDevicesEnumerationFailed(int request_id
);
140 const std::string
& label
,
141 const StreamDeviceInfo
& device_info
);
142 void OnDeviceOpenFailed(int request_id
);
144 void RemoveEnumerationRequest(
146 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
147 EnumerationState
* state
);
149 // Used for DCHECKs so methods calls won't execute in the wrong thread.
150 scoped_refptr
<base::MessageLoopProxy
> main_loop_
;
153 typedef std::map
<std::string
, Stream
> LabelStreamMap
;
154 LabelStreamMap label_stream_map_
;
156 EnumerationState audio_enumeration_state_
;
157 EnumerationState video_enumeration_state_
;
159 // List of calls made to GenerateStream that have not yet completed.
160 typedef std::list
<Request
> RequestList
;
161 RequestList requests_
;
163 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher
);
166 } // namespace content
168 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_