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,
41 // This is used for enumerating audio output devices.
42 // TODO(grunell): Output isn't really a part of media streams. Device
43 // enumeration should be decoupled from media streams and related code.
44 MEDIA_DEVICE_AUDIO_OUTPUT,
49 // Types of media stream requests that can be made to the media controller.
50 enum MediaStreamRequestType {
51 MEDIA_DEVICE_ACCESS = 0,
52 MEDIA_GENERATE_STREAM,
53 MEDIA_ENUMERATE_DEVICES,
54 MEDIA_OPEN_DEVICE // Only used in requests made by Pepper.
57 // Facing mode for video capture.
58 enum VideoFacingMode {
59 MEDIA_VIDEO_FACING_NONE = 0,
60 MEDIA_VIDEO_FACING_USER,
61 MEDIA_VIDEO_FACING_ENVIRONMENT,
62 MEDIA_VIDEO_FACING_LEFT,
63 MEDIA_VIDEO_FACING_RIGHT,
65 NUM_MEDIA_VIDEO_FACING_MODE
68 // Elements in this enum should not be deleted or rearranged; the only
69 // permitted operation is to add new elements before NUM_MEDIA_REQUEST_RESULTS.
70 enum MediaStreamRequestResult {
72 MEDIA_DEVICE_PERMISSION_DENIED = 1,
73 MEDIA_DEVICE_PERMISSION_DISMISSED = 2,
74 MEDIA_DEVICE_INVALID_STATE = 3,
75 MEDIA_DEVICE_NO_HARDWARE = 4,
76 MEDIA_DEVICE_INVALID_SECURITY_ORIGIN = 5,
77 MEDIA_DEVICE_TAB_CAPTURE_FAILURE = 6,
78 MEDIA_DEVICE_SCREEN_CAPTURE_FAILURE = 7,
79 MEDIA_DEVICE_CAPTURE_FAILURE = 8,
80 MEDIA_DEVICE_CONSTRAINT_NOT_SATISFIED = 9,
81 MEDIA_DEVICE_TRACK_START_FAILURE = 10,
82 MEDIA_DEVICE_NOT_SUPPORTED = 11,
83 MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN = 12,
84 NUM_MEDIA_REQUEST_RESULTS
87 // Convenience predicates to determine whether the given type represents some
88 // audio or some video device.
89 CONTENT_EXPORT bool IsAudioInputMediaType(MediaStreamType type);
90 CONTENT_EXPORT bool IsVideoMediaType(MediaStreamType type);
92 // TODO(xians): Change the structs to classes.
93 // Represents one device in a request for media stream(s).
94 struct CONTENT_EXPORT MediaStreamDevice {
99 const std::string& id,
100 const std::string& name);
103 MediaStreamType type,
104 const std::string& id,
105 const std::string& name,
108 int frames_per_buffer);
110 ~MediaStreamDevice();
112 bool IsEqual(const MediaStreamDevice& second) const;
114 // The device's type.
115 MediaStreamType type;
117 // The device's unique ID.
120 // The facing mode for video capture device.
121 VideoFacingMode video_facing;
123 // The device id of a matched output device if any (otherwise empty).
124 // Only applicable to audio devices.
125 std::string matched_output_device_id;
127 // The device's "friendly" name. Not guaranteed to be unique.
130 // Contains properties that match directly with those with the same name
131 // in media::AudioParameters.
132 struct AudioDeviceParameters {
133 AudioDeviceParameters()
134 : sample_rate(), channel_layout(), frames_per_buffer(), effects() {
137 AudioDeviceParameters(int sample_rate, int channel_layout,
138 int frames_per_buffer)
139 : sample_rate(sample_rate),
140 channel_layout(channel_layout),
141 frames_per_buffer(frames_per_buffer),
145 // Preferred sample rate in samples per second for the device.
148 // Preferred channel configuration for the device.
149 // TODO(henrika): ideally, we would like to use media::ChannelLayout here
150 // but including media/base/channel_layout.h violates checkdeps rules.
153 // Preferred number of frames per buffer for the device. This is filled
154 // in on the browser side and can be used by the renderer to match the
155 // expected browser side settings and avoid unnecessary buffering.
156 // See media::AudioParameters for more.
157 int frames_per_buffer;
159 // See media::AudioParameters::PlatformEffectsMask.
163 // These below two member variables are valid only when the type of device is
164 // audio (i.e. IsAudioInputMediaType returns true).
166 // Contains the device properties of the capture device.
167 AudioDeviceParameters input;
169 // If the capture device has an associated output device (e.g. headphones),
170 // this will contain the properties for the output device. If no such device
171 // exists (e.g. webcam w/mic), then the value of this member will be all
173 AudioDeviceParameters matched_output;
176 class CONTENT_EXPORT MediaStreamDevices
177 : public std::vector<MediaStreamDevice> {
179 MediaStreamDevices();
180 MediaStreamDevices(size_t count, const MediaStreamDevice& value);
182 // Looks for a MediaStreamDevice based on its ID.
183 // Returns NULL if not found.
184 const MediaStreamDevice* FindById(const std::string& device_id) const;
187 typedef std::map<MediaStreamType, MediaStreamDevices> MediaStreamDeviceMap;
189 // Represents a request for media streams (audio/video).
190 // TODO(vrk): Decouple MediaStreamDevice from this header file so that
191 // media_stream_options.h no longer depends on this file.
192 // TODO(vrk,justinlin,wjia): Figure out a way to share this code cleanly between
193 // vanilla WebRTC, Tab Capture, and Pepper Video Capture. Right now there is
194 // Tab-only stuff and Pepper-only stuff being passed around to all clients,
196 struct CONTENT_EXPORT MediaStreamRequest {
198 int render_process_id,
201 const GURL& security_origin,
203 MediaStreamRequestType request_type,
204 const std::string& requested_audio_device_id,
205 const std::string& requested_video_device_id,
206 MediaStreamType audio_type,
207 MediaStreamType video_type);
209 ~MediaStreamRequest();
211 // This is the render process id for the renderer associated with generating
212 // frames for a MediaStream. Any indicators associated with a capture will be
213 // displayed for this renderer.
214 int render_process_id;
216 // This is the render frame id for the renderer associated with generating
217 // frames for a MediaStream. Any indicators associated with a capture will be
218 // displayed for this renderer.
221 // The unique id combined with render_process_id and render_frame_id for
222 // identifying this request. This is used for cancelling request.
225 // Used by tab capture.
226 std::string tab_capture_device_id;
228 // The WebKit security origin for the current request (e.g. "html5rocks.com").
229 GURL security_origin;
231 // Set to true if the call was made in the context of a user gesture.
234 // Stores the type of request that was made to the media controller. Right now
235 // this is only used to distinguish between WebRTC and Pepper requests, as the
236 // latter should not be subject to user approval but only to policy check.
237 // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
238 MediaStreamRequestType request_type;
240 // Stores the requested raw device id for physical audio or video devices.
241 std::string requested_audio_device_id;
242 std::string requested_video_device_id;
244 // Flag to indicate if the request contains audio.
245 MediaStreamType audio_type;
247 // Flag to indicate if the request contains video.
248 MediaStreamType video_type;
251 // Interface used by the content layer to notify chrome about changes in the
252 // state of a media stream. Instances of this class are passed to content layer
253 // when MediaStream access is approved using MediaResponseCallback.
254 class MediaStreamUI {
256 virtual ~MediaStreamUI() {}
258 // Called when MediaStream capturing is started. Chrome layer can call |stop|
259 // to stop the stream. Returns the platform-dependent window ID for the UI, or
260 // 0 if not applicable.
261 virtual gfx::NativeViewId OnStarted(const base::Closure& stop) = 0;
264 // Callback used return results of media access requests.
265 typedef base::Callback<void(
266 const MediaStreamDevices& devices,
267 content::MediaStreamRequestResult result,
268 scoped_ptr<MediaStreamUI> ui)> MediaResponseCallback;
270 } // namespace content
272 #endif // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_