| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/shell/browser/media_capture_util.h" | 5 #include "extensions/shell/browser/media_capture_util.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/callback.h" | 9 #include "base/callback.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "content/public/browser/media_capture_devices.h" | 11 #include "content/public/browser/media_capture_devices.h" |
| 10 #include "extensions/common/permissions/permissions_data.h" | 12 #include "extensions/common/permissions/permissions_data.h" |
| 11 | 13 |
| 12 using content::MediaCaptureDevices; | 14 using content::MediaCaptureDevices; |
| 15 using content::MediaStreamDevice; |
| 13 using content::MediaStreamDevices; | 16 using content::MediaStreamDevices; |
| 14 using content::MediaStreamUI; | 17 using content::MediaStreamUI; |
| 15 | 18 |
| 16 namespace extensions { | 19 namespace extensions { |
| 20 |
| 21 const MediaStreamDevice* GetRequestedDeviceOrDefault( |
| 22 const MediaStreamDevices& devices, |
| 23 const std::string& requested_device_id) { |
| 24 if (!requested_device_id.empty()) |
| 25 return devices.FindById(requested_device_id); |
| 26 |
| 27 if (!devices.empty()) |
| 28 return &devices[0]; |
| 29 |
| 30 return NULL; |
| 31 } |
| 32 |
| 17 namespace media_capture_util { | 33 namespace media_capture_util { |
| 18 | 34 |
| 19 // See also Chrome's MediaCaptureDevicesDispatcher. | 35 // See also Chrome's MediaCaptureDevicesDispatcher. |
| 20 void GrantMediaStreamRequestWithFirstDevice( | 36 void GrantMediaStreamRequest(content::WebContents* web_contents, |
| 21 content::WebContents* web_contents, | 37 const content::MediaStreamRequest& request, |
| 22 const content::MediaStreamRequest& request, | 38 const content::MediaResponseCallback& callback, |
| 23 const content::MediaResponseCallback& callback, | 39 const Extension* extension) { |
| 24 const Extension* extension) { | |
| 25 // app_shell only supports audio and video capture, not tab or screen capture. | 40 // app_shell only supports audio and video capture, not tab or screen capture. |
| 26 DCHECK(request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE || | 41 DCHECK(request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE || |
| 27 request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE); | 42 request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE); |
| 28 | 43 |
| 29 // app_shell does not support requesting a specific device ID. | |
| 30 DCHECK(request.requested_audio_device_id.empty() && | |
| 31 request.requested_video_device_id.empty()); | |
| 32 | |
| 33 MediaStreamDevices devices; | 44 MediaStreamDevices devices; |
| 34 const PermissionsData* permissions_data = extension->permissions_data(); | 45 const PermissionsData* permissions_data = extension->permissions_data(); |
| 35 | 46 |
| 36 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) { | 47 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) { |
| 37 // app_shell has no UI surface to show an error, and on an embedded device | 48 // app_shell has no UI surface to show an error, and on an embedded device |
| 38 // it's better to crash than to have a feature not work. | 49 // it's better to crash than to have a feature not work. |
| 39 CHECK(permissions_data->HasAPIPermission(APIPermission::kAudioCapture)) | 50 CHECK(permissions_data->HasAPIPermission(APIPermission::kAudioCapture)) |
| 40 << "Audio capture request but no audioCapture permission in manifest."; | 51 << "Audio capture request but no audioCapture permission in manifest."; |
| 41 | 52 |
| 42 // Use first available audio capture device. | 53 const MediaStreamDevice* device = GetRequestedDeviceOrDefault( |
| 43 const MediaStreamDevices& audio_devices = | 54 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(), |
| 44 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); | 55 request.requested_audio_device_id); |
| 45 if (!audio_devices.empty()) | 56 if (device) |
| 46 devices.push_back(audio_devices[0]); | 57 devices.push_back(*device); |
| 47 } | 58 } |
| 48 | 59 |
| 49 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) { | 60 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) { |
| 50 // See APIPermission::kAudioCapture check above. | 61 // See APIPermission::kAudioCapture check above. |
| 51 CHECK(permissions_data->HasAPIPermission(APIPermission::kVideoCapture)) | 62 CHECK(permissions_data->HasAPIPermission(APIPermission::kVideoCapture)) |
| 52 << "Video capture request but no videoCapture permission in manifest."; | 63 << "Video capture request but no videoCapture permission in manifest."; |
| 53 | 64 |
| 54 // Use first available video capture device. | 65 const MediaStreamDevice* device = GetRequestedDeviceOrDefault( |
| 55 const MediaStreamDevices& video_devices = | 66 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(), |
| 56 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(); | 67 request.requested_video_device_id); |
| 57 if (!video_devices.empty()) | 68 if (device) |
| 58 devices.push_back(video_devices[0]); | 69 devices.push_back(*device); |
| 59 } | 70 } |
| 60 | 71 |
| 61 // TODO(jamescook): Should we show a recording icon somewhere? If so, where? | 72 // TODO(jamescook): Should we show a recording icon somewhere? If so, where? |
| 62 scoped_ptr<MediaStreamUI> ui; | 73 scoped_ptr<MediaStreamUI> ui; |
| 63 callback.Run(devices, | 74 callback.Run(devices, |
| 64 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE | 75 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE |
| 65 : content::MEDIA_DEVICE_OK, | 76 : content::MEDIA_DEVICE_OK, |
| 66 ui.Pass()); | 77 ui.Pass()); |
| 67 } | 78 } |
| 68 | 79 |
| 69 } // namespace media_capture_util | 80 } // namespace media_capture_util |
| 70 } // namespace extensions | 81 } // namespace extensions |
| OLD | NEW |