media_stream_request.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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 "url/gurl.h"
17
18namespace content {
19
20// Types of media streams.
21enum MediaStreamType {
22  MEDIA_NO_SERVICE = 0,
23
24  // A device provided by the operating system (e.g., webcam input).
25  MEDIA_DEVICE_AUDIO_CAPTURE,
26  MEDIA_DEVICE_VIDEO_CAPTURE,
27
28  // Mirroring of a browser tab.
29  //
30  // TODO(serygeu): Remove these values and use MEDIA_DESKTOP_VIDEO_CAPTURE and
31  // MEDIA_DESKTOP_AUDIO_CAPTURE.
32  MEDIA_TAB_AUDIO_CAPTURE,
33  MEDIA_TAB_VIDEO_CAPTURE,
34
35  // Desktop media sources.
36  MEDIA_DESKTOP_VIDEO_CAPTURE,
37
38  // Capture system audio (post-mix loopback stream).
39  //
40  // TODO(sergeyu): Replace with MEDIA_DESKTOP_AUDIO_CAPTURE.
41  MEDIA_LOOPBACK_AUDIO_CAPTURE,
42
43  NUM_MEDIA_TYPES
44};
45
46// Types of media stream requests that can be made to the media controller.
47enum MediaStreamRequestType {
48  MEDIA_DEVICE_ACCESS = 0,
49  MEDIA_GENERATE_STREAM,
50  MEDIA_ENUMERATE_DEVICES,
51  MEDIA_OPEN_DEVICE
52};
53
54// Facing mode for video capture.
55enum VideoFacingMode {
56  MEDIA_VIDEO_FACING_NONE = 0,
57  MEDIA_VIDEO_FACING_USER,
58  MEDIA_VIDEO_FACING_ENVIRONMENT,
59  MEDIA_VIDEO_FACING_LEFT,
60  MEDIA_VIDEO_FACING_RIGHT,
61
62  NUM_MEDIA_VIDEO_FACING_MODE
63};
64
65// Convenience predicates to determine whether the given type represents some
66// audio or some video device.
67CONTENT_EXPORT bool IsAudioMediaType(MediaStreamType type);
68CONTENT_EXPORT bool IsVideoMediaType(MediaStreamType type);
69
70// TODO(xians): Change the structs to classes.
71// Represents one device in a request for media stream(s).
72struct CONTENT_EXPORT MediaStreamDevice {
73  MediaStreamDevice();
74
75  MediaStreamDevice(
76      MediaStreamType type,
77      const std::string& id,
78      const std::string& name);
79
80  MediaStreamDevice(
81      MediaStreamType type,
82      const std::string& id,
83      const std::string& name,
84      int sample_rate,
85      int channel_layout,
86      int frames_per_buffer);
87
88  ~MediaStreamDevice();
89
90  // The device's type.
91  MediaStreamType type;
92
93  // The device's unique ID.
94  std::string id;
95
96  // The facing mode for video capture device.
97  VideoFacingMode video_facing;
98
99  // The device id of a matched output device if any (otherwise empty).
100  // Only applicable to audio devices.
101  std::string matched_output_device_id;
102
103  // The device's "friendly" name. Not guaranteed to be unique.
104  std::string name;
105
106  // Contains properties that match directly with those with the same name
107  // in media::AudioParameters.
108  struct AudioDeviceParameters {
109    AudioDeviceParameters()
110        : sample_rate(), channel_layout(), frames_per_buffer() {
111    }
112
113    AudioDeviceParameters(int sample_rate, int channel_layout,
114                          int frames_per_buffer)
115        : sample_rate(sample_rate),
116          channel_layout(channel_layout),
117          frames_per_buffer(frames_per_buffer) {
118    }
119
120    // Preferred sample rate in samples per second for the device.
121    int sample_rate;
122
123    // Preferred channel configuration for the device.
124    // TODO(henrika): ideally, we would like to use media::ChannelLayout here
125    // but including media/base/channel_layout.h violates checkdeps rules.
126    int channel_layout;
127
128    // Preferred number of frames per buffer for the device.  This is filled
129    // in on the browser side and can be used by the renderer to match the
130    // expected browser side settings and avoid unnecessary buffering.
131    // See media::AudioParameters for more.
132    int frames_per_buffer;
133  };
134
135  // These below two member variables are valid only when the type of device is
136  // audio (i.e. IsAudioMediaType returns true).
137
138  // Contains the device properties of the capture device.
139  AudioDeviceParameters input;
140
141  // If the capture device has an associated output device (e.g. headphones),
142  // this will contain the properties for the output device.  If no such device
143  // exists (e.g. webcam w/mic), then the value of this member will be all
144  // zeros.
145  AudioDeviceParameters matched_output;
146};
147
148typedef std::vector<MediaStreamDevice> MediaStreamDevices;
149
150typedef std::map<MediaStreamType, MediaStreamDevices> MediaStreamDeviceMap;
151
152// Represents a request for media streams (audio/video).
153// It looks like the last 4 parameters should use StreamOptions instead, but
154// StreamOption depends on media_stream_request.h because it needs
155// MediaStreamDevice.
156// TODO(vrk): Decouple MediaStreamDevice from this header file so that
157// media_stream_options.h no longer depends on this file.
158// TODO(vrk,justinlin,wjia): Figure out a way to share this code cleanly between
159// vanilla WebRTC, Tab Capture, and Pepper Video Capture. Right now there is
160// Tab-only stuff and Pepper-only stuff being passed around to all clients,
161// which is icky.
162struct CONTENT_EXPORT MediaStreamRequest {
163  MediaStreamRequest(
164      int render_process_id,
165      int render_view_id,
166      int page_request_id,
167      const std::string& tab_capture_device_id,
168      const GURL& security_origin,
169      MediaStreamRequestType request_type,
170      const std::string& requested_audio_device_id,
171      const std::string& requested_video_device_id,
172      MediaStreamType audio_type,
173      MediaStreamType video_type);
174
175  ~MediaStreamRequest();
176
177  // The render process id generating this request.
178  int render_process_id;
179
180  // The render view id generating this request.
181  int render_view_id;
182
183  // The unique id combined with render_process_id and render_view_id for
184  // identifying this request. This is used for cancelling request.
185  int page_request_id;
186
187  // Used by tab capture.
188  std::string tab_capture_device_id;
189
190  // The WebKit security origin for the current request (e.g. "html5rocks.com").
191  GURL security_origin;
192
193  // Stores the type of request that was made to the media controller. Right now
194  // this is only used to distinguish between WebRTC and Pepper requests, as the
195  // latter should not be subject to user approval but only to policy check.
196  // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
197  MediaStreamRequestType request_type;
198
199  // Stores the requested raw device id for physical audio or video devices.
200  std::string requested_audio_device_id;
201  std::string requested_video_device_id;
202
203  // Flag to indicate if the request contains audio.
204  MediaStreamType audio_type;
205
206  // Flag to indicate if the request contains video.
207  MediaStreamType video_type;
208};
209
210// Interface used by the content layer to notify chrome about changes in the
211// state of a media stream. Instances of this class are passed to content layer
212// when MediaStream access is approved using MediaResponseCallback.
213class MediaStreamUI {
214 public:
215  virtual ~MediaStreamUI() {}
216
217  // Called when MediaStream capturing is started. Chrome layer can call |stop|
218  // to stop the stream.
219  virtual void OnStarted(const base::Closure& stop) = 0;
220};
221
222// Callback used return results of media access requests.
223typedef base::Callback<void(
224    const MediaStreamDevices& devices,
225    scoped_ptr<MediaStreamUI> ui)> MediaResponseCallback;
226
227}  // namespace content
228
229#endif  // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
230