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"
23 class MessageLoopProxy
;
28 // MediaStreamDispatcher is a delegate for the Media Stream API messages.
29 // MediaStreams are used by WebKit to open media devices such as Video Capture
30 // and Audio input devices.
31 // It's the complement of MediaStreamDispatcherHost (owned by
32 // BrowserRenderProcessHost).
33 class CONTENT_EXPORT MediaStreamDispatcher
34 : public RenderFrameObserver
,
35 public base::SupportsWeakPtr
<MediaStreamDispatcher
> {
37 explicit MediaStreamDispatcher(RenderFrame
* render_frame
);
38 ~MediaStreamDispatcher() override
;
40 // Request a new media stream to be created.
41 // This can be used either by WebKit or a plugin.
42 // Note: The event_handler must be valid for as long as the stream exists.
43 virtual void GenerateStream(
45 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
46 const StreamOptions
& components
,
47 const GURL
& security_origin
);
49 // Cancel the request for a new media stream to be created.
50 virtual void CancelGenerateStream(
52 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
54 // Stop a started device that has been requested by calling GenerateStream.
55 virtual void StopStreamDevice(const StreamDeviceInfo
& device_info
);
57 // Request to enumerate devices.
58 virtual void EnumerateDevices(
60 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
62 const GURL
& security_origin
);
64 // Request to stop enumerating devices.
65 void StopEnumerateDevices(
67 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
69 // Request to open a device.
72 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
,
73 const std::string
& device_id
,
75 const GURL
& security_origin
);
77 // Cancel the request to open a device.
78 virtual void CancelOpenDevice(
80 const base::WeakPtr
<MediaStreamDispatcherEventHandler
>& event_handler
);
82 // Close a started device. |label| is provided in OnDeviceOpened.
83 void CloseDevice(const std::string
& label
);
85 // Check if the label is a valid stream.
86 virtual bool IsStream(const std::string
& label
);
87 // Get the video session_id given a label. The label identifies a stream.
88 // index is the index in the video_device_array of the stream.
89 virtual int video_session_id(const std::string
& label
, int index
);
90 // Returns an audio session_id given a label and an index.
91 virtual int audio_session_id(const std::string
& label
, int index
);
93 // Returns true if an audio input stream is currently active that was opened
94 // with audio ducking enabled. This is information is used when playing out
95 // audio so that rendered audio can be excluded from the ducking operation.
96 bool IsAudioDuckingActive() const;
99 int GetNextIpcIdForTest() { return next_ipc_id_
; }
102 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, BasicVideoDevice
);
103 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, TestFailure
);
104 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, CancelGenerateStream
);
105 FRIEND_TEST_ALL_PREFIXES(MediaStreamDispatcherTest
, CheckDuckingState
);
109 // Private class for keeping track of opened devices and who have
113 // RenderFrameObserver override.
114 void OnDestruct() override
;
115 bool Send(IPC::Message
* message
) override
;
116 bool OnMessageReceived(const IPC::Message
& message
) override
;
118 // Messages from the browser.
119 void OnStreamGenerated(
121 const std::string
& label
,
122 const StreamDeviceInfoArray
& audio_array
,
123 const StreamDeviceInfoArray
& video_array
);
124 void OnStreamGenerationFailed(
126 content::MediaStreamRequestResult result
);
127 void OnDeviceStopped(const std::string
& label
,
128 const StreamDeviceInfo
& device_info
);
129 void OnDevicesEnumerated(
131 const StreamDeviceInfoArray
& device_array
);
134 const std::string
& label
,
135 const StreamDeviceInfo
& device_info
);
136 void OnDeviceOpenFailed(int request_id
);
138 // Used for DCHECKs so methods calls won't execute in the wrong thread.
139 base::ThreadChecker thread_checker_
;
142 typedef std::map
<std::string
, Stream
> LabelStreamMap
;
143 LabelStreamMap label_stream_map_
;
145 // List of calls made to the browser process that have not yet completed or
147 typedef std::list
<Request
> RequestList
;
148 RequestList requests_
;
150 DISALLOW_COPY_AND_ASSIGN(MediaStreamDispatcher
);
153 } // namespace content
155 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_DISPATCHER_H_