media_stream_request.h revision f2477e01787aa58f445919b809d89e252beef54f
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  bool IsEqual(const MediaStreamDevice& second) const;
91
92  // The device's type.
93  MediaStreamType type;
94
95  // The device's unique ID.
96  std::string id;
97
98  // The facing mode for video capture device.
99  VideoFacingMode video_facing;
100
101  // The device id of a matched output device if any (otherwise empty).
102  // Only applicable to audio devices.
103  std::string matched_output_device_id;
104
105  // The device's "friendly" name. Not guaranteed to be unique.
106  std::string name;
107
108  // Contains properties that match directly with those with the same name
109  // in media::AudioParameters.
110  struct AudioDeviceParameters {
111    AudioDeviceParameters()
112        : sample_rate(), channel_layout(), frames_per_buffer() {
113    }
114
115    AudioDeviceParameters(int sample_rate, int channel_layout,
116                          int frames_per_buffer)
117        : sample_rate(sample_rate),
118          channel_layout(channel_layout),
119          frames_per_buffer(frames_per_buffer) {
120    }
121
122    // Preferred sample rate in samples per second for the device.
123    int sample_rate;
124
125    // Preferred channel configuration for the device.
126    // TODO(henrika): ideally, we would like to use media::ChannelLayout here
127    // but including media/base/channel_layout.h violates checkdeps rules.
128    int channel_layout;
129
130    // Preferred number of frames per buffer for the device.  This is filled
131    // in on the browser side and can be used by the renderer to match the
132    // expected browser side settings and avoid unnecessary buffering.
133    // See media::AudioParameters for more.
134    int frames_per_buffer;
135  };
136
137  // These below two member variables are valid only when the type of device is
138  // audio (i.e. IsAudioMediaType returns true).
139
140  // Contains the device properties of the capture device.
141  AudioDeviceParameters input;
142
143  // If the capture device has an associated output device (e.g. headphones),
144  // this will contain the properties for the output device.  If no such device
145  // exists (e.g. webcam w/mic), then the value of this member will be all
146  // zeros.
147  AudioDeviceParameters matched_output;
148};
149
150typedef std::vector<MediaStreamDevice> MediaStreamDevices;
151
152typedef std::map<MediaStreamType, MediaStreamDevices> MediaStreamDeviceMap;
153
154// Represents a request for media streams (audio/video).
155// It looks like the last 4 parameters should use StreamOptions instead, but
156// StreamOption depends on media_stream_request.h because it needs
157// MediaStreamDevice.
158// TODO(vrk): Decouple MediaStreamDevice from this header file so that
159// media_stream_options.h no longer depends on this file.
160// TODO(vrk,justinlin,wjia): Figure out a way to share this code cleanly between
161// vanilla WebRTC, Tab Capture, and Pepper Video Capture. Right now there is
162// Tab-only stuff and Pepper-only stuff being passed around to all clients,
163// which is icky.
164struct CONTENT_EXPORT MediaStreamRequest {
165  MediaStreamRequest(
166      int render_process_id,
167      int render_view_id,
168      int page_request_id,
169      const std::string& tab_capture_device_id,
170      const GURL& security_origin,
171      MediaStreamRequestType request_type,
172      const std::string& requested_audio_device_id,
173      const std::string& requested_video_device_id,
174      MediaStreamType audio_type,
175      MediaStreamType video_type);
176
177  ~MediaStreamRequest();
178
179  // This is the render process id for the renderer associated with generating
180  // frames for a MediaStream. Any indicators associated with a capture will be
181  // displayed for this renderer.
182  int render_process_id;
183
184  // This is the render view id for the renderer associated with generating
185  // frames for a MediaStream. Any indicators associated with a capture will be
186  // displayed for this renderer.
187  int render_view_id;
188
189  // The unique id combined with render_process_id and render_view_id for
190  // identifying this request. This is used for cancelling request.
191  int page_request_id;
192
193  // Used by tab capture.
194  std::string tab_capture_device_id;
195
196  // The WebKit security origin for the current request (e.g. "html5rocks.com").
197  GURL security_origin;
198
199  // Stores the type of request that was made to the media controller. Right now
200  // this is only used to distinguish between WebRTC and Pepper requests, as the
201  // latter should not be subject to user approval but only to policy check.
202  // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
203  MediaStreamRequestType request_type;
204
205  // Stores the requested raw device id for physical audio or video devices.
206  std::string requested_audio_device_id;
207  std::string requested_video_device_id;
208
209  // Flag to indicate if the request contains audio.
210  MediaStreamType audio_type;
211
212  // Flag to indicate if the request contains video.
213  MediaStreamType video_type;
214};
215
216// Interface used by the content layer to notify chrome about changes in the
217// state of a media stream. Instances of this class are passed to content layer
218// when MediaStream access is approved using MediaResponseCallback.
219class MediaStreamUI {
220 public:
221  virtual ~MediaStreamUI() {}
222
223  // Called when MediaStream capturing is started. Chrome layer can call |stop|
224  // to stop the stream.
225  virtual void OnStarted(const base::Closure& stop) = 0;
226};
227
228// Callback used return results of media access requests.
229typedef base::Callback<void(
230    const MediaStreamDevices& devices,
231    scoped_ptr<MediaStreamUI> ui)> MediaResponseCallback;
232
233}  // namespace content
234
235#endif  // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
236