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
);
89 // Returns true if an audio input stream is currently active that was opened
90 // with audio ducking enabled. This is information is used when playing out
91 // audio so that rendered audio can be excluded from the ducking operation.
92 bool IsAudioDuckingActive() const;
95 int GetNextIpcIdForTest() { return next_ipc_id_
; }
98 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, BasicVideoDevice
);
99 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, TestFailure
);
100 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, CancelGenerateStream
);
101 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, CheckDuckingState
);
105 // Private class for keeping track of opened devices and who have
109 // RenderFrameObserver override.
110 void OnDestruct() override
;
111 bool Send(IPC::Message
* message
) override
;
112 bool OnMessageReceived(const IPC::Message
& message
) override
;
114 // Messages from the browser.
115 void OnStreamGenerated(
117 const std::string
& label
,
118 const StreamDeviceInfoArray
& audio_array
,
119 const StreamDeviceInfoArray
& video_array
);
120 void OnStreamGenerationFailed(
122 content::MediaStreamRequestResult result
);
123 void OnDeviceStopped(const std::string
& label
,
124 const StreamDeviceInfo
& device_info
);
125 void OnDevicesEnumerated(
127 const StreamDeviceInfoArray
& device_array
);
130 const std::string
& label
,
131 const StreamDeviceInfo
& device_info
);
132 void OnDeviceOpenFailed(int request_id
);
134 // Used for DCHECKs so methods calls won't execute in the wrong thread.
135 base::ThreadChecker thread_checker_
;
138 typedef std::map
<std::string
, Stream
> LabelStreamMap
;
139 LabelStreamMap label_stream_map_
;
141 // List of calls made to the browser process that have not yet completed or
143 typedef std::list
<Request
> RequestList
;
144 RequestList requests_
;
146 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher
);
149 } // namespace content
151 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_