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_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
6 #define CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
12 #include "base/basictypes.h"
13 #include "base/callback_forward.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "content/common/content_export.h"
16 #include "ui/gfx/native_widget_types.h"
21 // Types of media streams.
22 enum MediaStreamType
{
25 // A device provided by the operating system (e.g., webcam input).
26 MEDIA_DEVICE_AUDIO_CAPTURE
,
27 MEDIA_DEVICE_VIDEO_CAPTURE
,
29 // Mirroring of a browser tab.
30 MEDIA_TAB_AUDIO_CAPTURE
,
31 MEDIA_TAB_VIDEO_CAPTURE
,
33 // Desktop media sources.
34 MEDIA_DESKTOP_VIDEO_CAPTURE
,
36 // Capture system audio (post-mix loopback stream).
38 // TODO(sergeyu): Replace with MEDIA_DESKTOP_AUDIO_CAPTURE.
39 MEDIA_LOOPBACK_AUDIO_CAPTURE
,
44 // Types of media stream requests that can be made to the media controller.
45 enum MediaStreamRequestType
{
46 MEDIA_DEVICE_ACCESS
= 0,
47 MEDIA_GENERATE_STREAM
,
48 MEDIA_ENUMERATE_DEVICES
,
49 MEDIA_OPEN_DEVICE
// Only used in requests made by Pepper.
52 // Facing mode for video capture.
53 enum VideoFacingMode
{
54 MEDIA_VIDEO_FACING_NONE
= 0,
55 MEDIA_VIDEO_FACING_USER
,
56 MEDIA_VIDEO_FACING_ENVIRONMENT
,
57 MEDIA_VIDEO_FACING_LEFT
,
58 MEDIA_VIDEO_FACING_RIGHT
,
60 NUM_MEDIA_VIDEO_FACING_MODE
63 enum MediaStreamRequestResult
{
65 MEDIA_DEVICE_PERMISSION_DENIED
,
66 MEDIA_DEVICE_PERMISSION_DISMISSED
,
67 MEDIA_DEVICE_INVALID_STATE
,
68 MEDIA_DEVICE_NO_HARDWARE
,
69 MEDIA_DEVICE_INVALID_SECURITY_ORIGIN
,
70 MEDIA_DEVICE_TAB_CAPTURE_FAILURE
,
71 MEDIA_DEVICE_SCREEN_CAPTURE_FAILURE
,
72 MEDIA_DEVICE_CAPTURE_FAILURE
,
73 MEDIA_DEVICE_TRACK_START_FAILURE
,
75 NUM_MEDIA_REQUEST_RESULTS
78 // Convenience predicates to determine whether the given type represents some
79 // audio or some video device.
80 CONTENT_EXPORT
bool IsAudioMediaType(MediaStreamType type
);
81 CONTENT_EXPORT
bool IsVideoMediaType(MediaStreamType type
);
83 // TODO(xians): Change the structs to classes.
84 // Represents one device in a request for media stream(s).
85 struct CONTENT_EXPORT MediaStreamDevice
{
90 const std::string
& id
,
91 const std::string
& name
);
95 const std::string
& id
,
96 const std::string
& name
,
99 int frames_per_buffer
);
101 ~MediaStreamDevice();
103 bool IsEqual(const MediaStreamDevice
& second
) const;
105 // The device's type.
106 MediaStreamType type
;
108 // The device's unique ID.
111 // The facing mode for video capture device.
112 VideoFacingMode video_facing
;
114 // The device id of a matched output device if any (otherwise empty).
115 // Only applicable to audio devices.
116 std::string matched_output_device_id
;
118 // The device's "friendly" name. Not guaranteed to be unique.
121 // Contains properties that match directly with those with the same name
122 // in media::AudioParameters.
123 struct AudioDeviceParameters
{
124 AudioDeviceParameters()
125 : sample_rate(), channel_layout(), frames_per_buffer(), effects() {
128 AudioDeviceParameters(int sample_rate
, int channel_layout
,
129 int frames_per_buffer
)
130 : sample_rate(sample_rate
),
131 channel_layout(channel_layout
),
132 frames_per_buffer(frames_per_buffer
),
136 // Preferred sample rate in samples per second for the device.
139 // Preferred channel configuration for the device.
140 // TODO(henrika): ideally, we would like to use media::ChannelLayout here
141 // but including media/base/channel_layout.h violates checkdeps rules.
144 // Preferred number of frames per buffer for the device. This is filled
145 // in on the browser side and can be used by the renderer to match the
146 // expected browser side settings and avoid unnecessary buffering.
147 // See media::AudioParameters for more.
148 int frames_per_buffer
;
150 // See media::AudioParameters::PlatformEffectsMask.
154 // These below two member variables are valid only when the type of device is
155 // audio (i.e. IsAudioMediaType returns true).
157 // Contains the device properties of the capture device.
158 AudioDeviceParameters input
;
160 // If the capture device has an associated output device (e.g. headphones),
161 // this will contain the properties for the output device. If no such device
162 // exists (e.g. webcam w/mic), then the value of this member will be all
164 AudioDeviceParameters matched_output
;
167 class CONTENT_EXPORT MediaStreamDevices
168 : public std::vector
<MediaStreamDevice
> {
170 MediaStreamDevices();
171 MediaStreamDevices(size_t count
, const MediaStreamDevice
& value
);
173 // Looks for a MediaStreamDevice based on its ID.
174 // Returns NULL if not found.
175 const MediaStreamDevice
* FindById(const std::string
& device_id
) const;
178 typedef std::map
<MediaStreamType
, MediaStreamDevices
> MediaStreamDeviceMap
;
180 // Represents a request for media streams (audio/video).
181 // TODO(vrk): Decouple MediaStreamDevice from this header file so that
182 // media_stream_options.h no longer depends on this file.
183 // TODO(vrk,justinlin,wjia): Figure out a way to share this code cleanly between
184 // vanilla WebRTC, Tab Capture, and Pepper Video Capture. Right now there is
185 // Tab-only stuff and Pepper-only stuff being passed around to all clients,
187 struct CONTENT_EXPORT MediaStreamRequest
{
189 int render_process_id
,
192 const GURL
& security_origin
,
194 MediaStreamRequestType request_type
,
195 const std::string
& requested_audio_device_id
,
196 const std::string
& requested_video_device_id
,
197 MediaStreamType audio_type
,
198 MediaStreamType video_type
);
200 ~MediaStreamRequest();
202 // This is the render process id for the renderer associated with generating
203 // frames for a MediaStream. Any indicators associated with a capture will be
204 // displayed for this renderer.
205 int render_process_id
;
207 // This is the render view id for the renderer associated with generating
208 // frames for a MediaStream. Any indicators associated with a capture will be
209 // displayed for this renderer.
212 // The unique id combined with render_process_id and render_view_id for
213 // identifying this request. This is used for cancelling request.
216 // Used by tab capture.
217 std::string tab_capture_device_id
;
219 // The WebKit security origin for the current request (e.g. "html5rocks.com").
220 GURL security_origin
;
222 // Set to true if the call was made in the context of a user gesture.
225 // Stores the type of request that was made to the media controller. Right now
226 // this is only used to distinguish between WebRTC and Pepper requests, as the
227 // latter should not be subject to user approval but only to policy check.
228 // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
229 MediaStreamRequestType request_type
;
231 // Stores the requested raw device id for physical audio or video devices.
232 std::string requested_audio_device_id
;
233 std::string requested_video_device_id
;
235 // Flag to indicate if the request contains audio.
236 MediaStreamType audio_type
;
238 // Flag to indicate if the request contains video.
239 MediaStreamType video_type
;
242 // Interface used by the content layer to notify chrome about changes in the
243 // state of a media stream. Instances of this class are passed to content layer
244 // when MediaStream access is approved using MediaResponseCallback.
245 class MediaStreamUI
{
247 virtual ~MediaStreamUI() {}
249 // Called when MediaStream capturing is started. Chrome layer can call |stop|
250 // to stop the stream. Returns the platform-dependent window ID for the UI, or
251 // 0 if not applicable.
252 virtual gfx::NativeViewId
OnStarted(const base::Closure
& stop
) = 0;
255 // Callback used return results of media access requests.
256 typedef base::Callback
<void(
257 const MediaStreamDevices
& devices
,
258 content::MediaStreamRequestResult result
,
259 scoped_ptr
<MediaStreamUI
> ui
)> MediaResponseCallback
;
261 } // namespace content
263 #endif // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_