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 "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"
24 // MediaStreamDispatcher is a delegate for the Media Stream API messages.
25 // MediaStreams are used by WebKit to open media devices such as Video Capture
26 // and Audio input devices.
27 // It's the complement of MediaStreamDispatcherHost (owned by
28 // BrowserRenderProcessHost).
29 class CONTENT_EXPORT MediaStreamDispatcher
30 : public RenderFrameObserver
,
31 public base::SupportsWeakPtr
<MediaStreamDispatcher
> {
33 explicit MediaStreamDispatcher(RenderFrame
* render_frame
);
34 ~MediaStreamDispatcher() override
;
36 // Request a new media stream to be created.
37 // This can be used either by WebKit or a plugin.
38 // Note: The event_handler must be valid for as long as the stream exists.
39 virtual void GenerateStream(
41 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
42 const StreamOptions
& components
,
43 const GURL
& security_origin
);
45 // Cancel the request for a new media stream to be created.
46 virtual void CancelGenerateStream(
48 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
50 // Stop a started device that has been requested by calling GenerateStream.
51 virtual void StopStreamDevice(const StreamDeviceInfo
& device_info
);
53 // Request to enumerate devices.
54 virtual void EnumerateDevices(
56 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
58 const GURL
& security_origin
);
60 // Request to stop enumerating devices.
61 void StopEnumerateDevices(
63 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
65 // Request to open a device.
68 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
69 const std::string
& device_id
,
71 const GURL
& security_origin
);
73 // Cancel the request to open a device.
74 virtual void CancelOpenDevice(
76 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
78 // Close a started device. |label| is provided in OnDeviceOpened.
79 void CloseDevice(const std::string
& label
);
81 // Check if the label is a valid stream.
82 virtual bool IsStream(const std::string
& label
);
83 // Get the video session_id given a label. The label identifies a stream.
84 // index is the index in the video_device_array of the stream.
85 virtual int video_session_id(const std::string
& label
, int index
);
86 // Returns an audio session_id given a label and an index.
87 virtual int audio_session_id(const std::string
& label
, int index
);
90 int GetNextIpcIdForTest() { return next_ipc_id_
; }
93 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, BasicVideoDevice
);
94 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, TestFailure
);
95 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, CancelGenerateStream
);
99 // Private class for keeping track of opened devices and who have
103 // RenderFrameObserver override.
104 void OnDestruct() override
;
105 bool Send(IPC::Message
* message
) override
;
106 bool OnMessageReceived(const IPC::Message
& message
) override
;
108 // Messages from the browser.
109 void OnStreamGenerated(
111 const std::string
& label
,
112 const StreamDeviceInfoArray
& audio_array
,
113 const StreamDeviceInfoArray
& video_array
);
114 void OnStreamGenerationFailed(
116 content::MediaStreamRequestResult result
);
117 void OnDeviceStopped(const std::string
& label
,
118 const StreamDeviceInfo
& device_info
);
119 void OnDevicesEnumerated(
121 const StreamDeviceInfoArray
& device_array
);
124 const std::string
& label
,
125 const StreamDeviceInfo
& device_info
);
126 void OnDeviceOpenFailed(int request_id
);
128 // Used for DCHECKs so methods calls won't execute in the wrong thread.
129 base::ThreadChecker thread_checker_
;
132 typedef std::map
<std::string
, Stream
> LabelStreamMap
;
133 LabelStreamMap label_stream_map_
;
135 // List of calls made to the browser process that have not yet completed or
137 typedef std::list
<Request
> RequestList
;
138 RequestList requests_
;
140 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher
);
143 } // namespace content
145 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_