media_stream_request.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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  MEDIA_TAB_AUDIO_CAPTURE,
30  MEDIA_TAB_VIDEO_CAPTURE,
31
32  // Desktop media sources.
33  MEDIA_DESKTOP_VIDEO_CAPTURE,
34
35  // Capture system audio (post-mix loopback stream).
36  //
37  // TODO(sergeyu): Replace with MEDIA_DESKTOP_AUDIO_CAPTURE.
38  MEDIA_LOOPBACK_AUDIO_CAPTURE,
39
40  NUM_MEDIA_TYPES
41};
42
43// Types of media stream requests that can be made to the media controller.
44enum MediaStreamRequestType {
45  MEDIA_DEVICE_ACCESS = 0,
46  MEDIA_GENERATE_STREAM,
47  MEDIA_ENUMERATE_DEVICES,
48  MEDIA_OPEN_DEVICE
49};
50
51// Facing mode for video capture.
52enum VideoFacingMode {
53  MEDIA_VIDEO_FACING_NONE = 0,
54  MEDIA_VIDEO_FACING_USER,
55  MEDIA_VIDEO_FACING_ENVIRONMENT,
56  MEDIA_VIDEO_FACING_LEFT,
57  MEDIA_VIDEO_FACING_RIGHT,
58
59  NUM_MEDIA_VIDEO_FACING_MODE
60};
61
62// Convenience predicates to determine whether the given type represents some
63// audio or some video device.
64CONTENT_EXPORT bool IsAudioMediaType(MediaStreamType type);
65CONTENT_EXPORT bool IsVideoMediaType(MediaStreamType type);
66
67// TODO(xians): Change the structs to classes.
68// Represents one device in a request for media stream(s).
69struct CONTENT_EXPORT MediaStreamDevice {
70  MediaStreamDevice();
71
72  MediaStreamDevice(
73      MediaStreamType type,
74      const std::string& id,
75      const std::string& name);
76
77  MediaStreamDevice(
78      MediaStreamType type,
79      const std::string& id,
80      const std::string& name,
81      int sample_rate,
82      int channel_layout,
83      int frames_per_buffer);
84
85  ~MediaStreamDevice();
86
87  bool IsEqual(const MediaStreamDevice& second) const;
88
89  // The device's type.
90  MediaStreamType type;
91
92  // The device's unique ID.
93  std::string id;
94
95  // The facing mode for video capture device.
96  VideoFacingMode video_facing;
97
98  // The device id of a matched output device if any (otherwise empty).
99  // Only applicable to audio devices.
100  std::string matched_output_device_id;
101
102  // The device's "friendly" name. Not guaranteed to be unique.
103  std::string name;
104
105  // Contains properties that match directly with those with the same name
106  // in media::AudioParameters.
107  struct AudioDeviceParameters {
108    AudioDeviceParameters()
109        : sample_rate(), channel_layout(), frames_per_buffer(), effects() {
110    }
111
112    AudioDeviceParameters(int sample_rate, int channel_layout,
113        int frames_per_buffer)
114        : sample_rate(sample_rate),
115          channel_layout(channel_layout),
116          frames_per_buffer(frames_per_buffer),
117          effects() {
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    // See media::AudioParameters::PlatformEffectsMask.
135    int effects;
136  };
137
138  // These below two member variables are valid only when the type of device is
139  // audio (i.e. IsAudioMediaType returns true).
140
141  // Contains the device properties of the capture device.
142  AudioDeviceParameters input;
143
144  // If the capture device has an associated output device (e.g. headphones),
145  // this will contain the properties for the output device.  If no such device
146  // exists (e.g. webcam w/mic), then the value of this member will be all
147  // zeros.
148  AudioDeviceParameters matched_output;
149};
150
151typedef std::vector<MediaStreamDevice> MediaStreamDevices;
152
153typedef std::map<MediaStreamType, MediaStreamDevices> MediaStreamDeviceMap;
154
155// Represents a request for media streams (audio/video).
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 GURL& security_origin,
168      MediaStreamRequestType request_type,
169      const std::string& requested_audio_device_id,
170      const std::string& requested_video_device_id,
171      MediaStreamType audio_type,
172      MediaStreamType video_type);
173
174  ~MediaStreamRequest();
175
176  // This is the render process id for the renderer associated with generating
177  // frames for a MediaStream. Any indicators associated with a capture will be
178  // displayed for this renderer.
179  int render_process_id;
180
181  // This is the render view id for the renderer associated with generating
182  // frames for a MediaStream. Any indicators associated with a capture will be
183  // displayed for this renderer.
184  int render_view_id;
185
186  // The unique id combined with render_process_id and render_view_id for
187  // identifying this request. This is used for cancelling request.
188  int page_request_id;
189
190  // Used by tab capture.
191  std::string tab_capture_device_id;
192
193  // The WebKit security origin for the current request (e.g. "html5rocks.com").
194  GURL security_origin;
195
196  // Stores the type of request that was made to the media controller. Right now
197  // this is only used to distinguish between WebRTC and Pepper requests, as the
198  // latter should not be subject to user approval but only to policy check.
199  // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
200  MediaStreamRequestType request_type;
201
202  // Stores the requested raw device id for physical audio or video devices.
203  std::string requested_audio_device_id;
204  std::string requested_video_device_id;
205
206  // Flag to indicate if the request contains audio.
207  MediaStreamType audio_type;
208
209  // Flag to indicate if the request contains video.
210  MediaStreamType video_type;
211};
212
213// Interface used by the content layer to notify chrome about changes in the
214// state of a media stream. Instances of this class are passed to content layer
215// when MediaStream access is approved using MediaResponseCallback.
216class MediaStreamUI {
217 public:
218  virtual ~MediaStreamUI() {}
219
220  // Called when MediaStream capturing is started. Chrome layer can call |stop|
221  // to stop the stream.
222  virtual void OnStarted(const base::Closure& stop) = 0;
223};
224
225// Callback used return results of media access requests.
226typedef base::Callback<void(
227    const MediaStreamDevices& devices,
228    scoped_ptr<MediaStreamUI> ui)> MediaResponseCallback;
229
230}  // namespace content
231
232#endif  // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_
233