media_stream_request.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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.
4
5#ifndef CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
6#define CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
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"
17#include "url/gurl.h"
18
19namespace content {
20
21// Types of media streams.
22enum MediaStreamType {
23  MEDIA_NO_SERVICE = 0,
24
25  // A device provided by the operating system (e.g., webcam input).
26  MEDIA_DEVICE_AUDIO_CAPTURE,
27  MEDIA_DEVICE_VIDEO_CAPTURE,
28
29  // Mirroring of a browser tab.
30  MEDIA_TAB_AUDIO_CAPTURE,
31  MEDIA_TAB_VIDEO_CAPTURE,
32
33  // Desktop media sources.
34  MEDIA_DESKTOP_VIDEO_CAPTURE,
35
36  // Capture system audio (post-mix loopback stream).
37  //
38  // TODO(sergeyu): Replace with MEDIA_DESKTOP_AUDIO_CAPTURE.
39  MEDIA_LOOPBACK_AUDIO_CAPTURE,
40
41  NUM_MEDIA_TYPES
42};
43
44// Types of media stream requests that can be made to the media controller.
45enum MediaStreamRequestType {
46  MEDIA_DEVICE_ACCESS = 0,
47  MEDIA_GENERATE_STREAM,
48  MEDIA_ENUMERATE_DEVICES,
49  MEDIA_OPEN_DEVICE
50};
51
52// Facing mode for video capture.
53enum 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,
59
60  NUM_MEDIA_VIDEO_FACING_MODE
61};
62
63enum MediaStreamRequestResult {
64  MEDIA_DEVICE_OK = 0,
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,
74
75  NUM_MEDIA_REQUEST_RESULTS
76};
77
78// Convenience predicates to determine whether the given type represents some
79// audio or some video device.
80CONTENT_EXPORT bool IsAudioMediaType(MediaStreamType type);
81CONTENT_EXPORT bool IsVideoMediaType(MediaStreamType type);
82
83// TODO(xians): Change the structs to classes.
84// Represents one device in a request for media stream(s).
85struct CONTENT_EXPORT MediaStreamDevice {
86  MediaStreamDevice();
87
88  MediaStreamDevice(
89      MediaStreamType type,
90      const std::string& id,
91      const std::string& name);
92
93  MediaStreamDevice(
94      MediaStreamType type,
95      const std::string& id,
96      const std::string& name,
97      int sample_rate,
98      int channel_layout,
99      int frames_per_buffer);
100
101  ~MediaStreamDevice();
102
103  bool IsEqual(const MediaStreamDevice& second) const;
104
105  // The device's type.
106  MediaStreamType type;
107
108  // The device's unique ID.
109  std::string id;
110
111  // The facing mode for video capture device.
112  VideoFacingMode video_facing;
113
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;
117
118  // The device's "friendly" name. Not guaranteed to be unique.
119  std::string name;
120
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() {
126    }
127
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),
133          effects() {
134    }
135
136    // Preferred sample rate in samples per second for the device.
137    int sample_rate;
138
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.
142    int channel_layout;
143
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;
149
150    // See media::AudioParameters::PlatformEffectsMask.
151    int effects;
152  };
153
154  // These below two member variables are valid only when the type of device is
155  // audio (i.e. IsAudioMediaType returns true).
156
157  // Contains the device properties of the capture device.
158  AudioDeviceParameters input;
159
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
163  // zeros.
164  AudioDeviceParameters matched_output;
165};
166
167typedef std::vector<MediaStreamDevice> MediaStreamDevices;
168
169typedef std::map<MediaStreamType, MediaStreamDevices> MediaStreamDeviceMap;
170
171// Represents a request for media streams (audio/video).
172// TODO(vrk): Decouple MediaStreamDevice from this header file so that
173// media_stream_options.h no longer depends on this file.
174// TODO(vrk,justinlin,wjia): Figure out a way to share this code cleanly between
175// vanilla WebRTC, Tab Capture, and Pepper Video Capture. Right now there is
176// Tab-only stuff and Pepper-only stuff being passed around to all clients,
177// which is icky.
178struct CONTENT_EXPORT MediaStreamRequest {
179  MediaStreamRequest(
180      int render_process_id,
181      int render_view_id,
182      int page_request_id,
183      const GURL& security_origin,
184      bool user_gesture,
185      MediaStreamRequestType request_type,
186      const std::string& requested_audio_device_id,
187      const std::string& requested_video_device_id,
188      MediaStreamType audio_type,
189      MediaStreamType video_type);
190
191  ~MediaStreamRequest();
192
193  // This is the render process id for the renderer associated with generating
194  // frames for a MediaStream. Any indicators associated with a capture will be
195  // displayed for this renderer.
196  int render_process_id;
197
198  // This is the render view id for the renderer associated with generating
199  // frames for a MediaStream. Any indicators associated with a capture will be
200  // displayed for this renderer.
201  int render_view_id;
202
203  // The unique id combined with render_process_id and render_view_id for
204  // identifying this request. This is used for cancelling request.
205  int page_request_id;
206
207  // Used by tab capture.
208  std::string tab_capture_device_id;
209
210  // The WebKit security origin for the current request (e.g. "html5rocks.com").
211  GURL security_origin;
212
213  // Set to true if the call was made in the context of a user gesture.
214  bool user_gesture;
215
216  // Stores the type of request that was made to the media controller. Right now
217  // this is only used to distinguish between WebRTC and Pepper requests, as the
218  // latter should not be subject to user approval but only to policy check.
219  // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
220  MediaStreamRequestType request_type;
221
222  // Stores the requested raw device id for physical audio or video devices.
223  std::string requested_audio_device_id;
224  std::string requested_video_device_id;
225
226  // Flag to indicate if the request contains audio.
227  MediaStreamType audio_type;
228
229  // Flag to indicate if the request contains video.
230  MediaStreamType video_type;
231};
232
233// Interface used by the content layer to notify chrome about changes in the
234// state of a media stream. Instances of this class are passed to content layer
235// when MediaStream access is approved using MediaResponseCallback.
236class MediaStreamUI {
237 public:
238  virtual ~MediaStreamUI() {}
239
240  // Called when MediaStream capturing is started. Chrome layer can call |stop|
241  // to stop the stream. Returns the platform-dependent window ID for the UI, or
242  // 0 if not applicable.
243  virtual gfx::NativeViewId OnStarted(const base::Closure& stop) = 0;
244};
245
246// Callback used return results of media access requests.
247typedef base::Callback<void(
248    const MediaStreamDevices& devices,
249    content::MediaStreamRequestResult result,
250    scoped_ptr<MediaStreamUI> ui)> MediaResponseCallback;
251
252}  // namespace content
253
254#endif  // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
255