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