- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / common / media_stream_request.h
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
18 namespace content {
19
20 // Types of media streams.
21 enum 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.
47 enum 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.
55 enum 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.
67 CONTENT_EXPORT bool IsAudioMediaType(MediaStreamType type);
68 CONTENT_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).
72 struct 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
148 typedef std::vector<MediaStreamDevice> MediaStreamDevices;
149
150 typedef 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.
162 struct 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   // This is the render process id for the renderer associated with generating
178   // frames for a MediaStream. Any indicators associated with a capture will be
179   // displayed for this renderer.
180   int render_process_id;
181
182   // This is the render view id for the renderer associated with generating
183   // frames for a MediaStream. Any indicators associated with a capture will be
184   // displayed for this renderer.
185   int render_view_id;
186
187   // The unique id combined with render_process_id and render_view_id for
188   // identifying this request. This is used for cancelling request.
189   int page_request_id;
190
191   // Used by tab capture.
192   std::string tab_capture_device_id;
193
194   // The WebKit security origin for the current request (e.g. "html5rocks.com").
195   GURL security_origin;
196
197   // Stores the type of request that was made to the media controller. Right now
198   // this is only used to distinguish between WebRTC and Pepper requests, as the
199   // latter should not be subject to user approval but only to policy check.
200   // Pepper requests are signified by the |MEDIA_OPEN_DEVICE| value.
201   MediaStreamRequestType request_type;
202
203   // Stores the requested raw device id for physical audio or video devices.
204   std::string requested_audio_device_id;
205   std::string requested_video_device_id;
206
207   // Flag to indicate if the request contains audio.
208   MediaStreamType audio_type;
209
210   // Flag to indicate if the request contains video.
211   MediaStreamType video_type;
212 };
213
214 // Interface used by the content layer to notify chrome about changes in the
215 // state of a media stream. Instances of this class are passed to content layer
216 // when MediaStream access is approved using MediaResponseCallback.
217 class MediaStreamUI {
218  public:
219   virtual ~MediaStreamUI() {}
220
221   // Called when MediaStream capturing is started. Chrome layer can call |stop|
222   // to stop the stream.
223   virtual void OnStarted(const base::Closure& stop) = 0;
224 };
225
226 // Callback used return results of media access requests.
227 typedef base::Callback<void(
228     const MediaStreamDevices& devices,
229     scoped_ptr<MediaStreamUI> ui)> MediaResponseCallback;
230
231 }  // namespace content
232
233 #endif  // CONTENT_PUBLIC_COMMON_MEDIA_STREAM_REQUEST_H_