| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/media/capture/image_capture_impl.h" | 5 #include "content/browser/media/capture/image_capture_impl.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "content/browser/browser_main_loop.h" | 8 #include "content/browser/browser_main_loop.h" |
| 9 #include "content/browser/renderer_host/media/media_stream_manager.h" | 9 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 10 #include "content/browser/renderer_host/media/video_capture_manager.h" | 10 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 11 #include "content/common/media/media_stream_options.h" | 11 #include "content/common/media/media_stream_options.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "media/capture/video/video_capture_device.h" | 13 #include "media/capture/video/video_capture_device.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 void RunMojoCallback(const ImageCaptureImpl::TakePhotoCallback& callback, | 19 void RunFailedGetCapabilitiesCallback( |
| 20 const std::string& mime_type, | 20 const ImageCaptureImpl::GetCapabilitiesCallback& callback) { |
| 21 mojo::Array<uint8_t> data) { | 21 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 22 callback.Run(mime_type, std::move(data)); | 22 BrowserThread::PostTask( |
| 23 BrowserThread::UI, FROM_HERE, |
| 24 base::Bind(&RunFailedGetCapabilitiesCallback, std::move(callback))); |
| 25 return; |
| 26 } |
| 27 |
| 28 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 29 blink::mojom::PhotoCapabilitiesPtr empty_capabilities = |
| 30 blink::mojom::PhotoCapabilities::New(); |
| 31 empty_capabilities->zoom = blink::mojom::Range::New(); |
| 32 callback.Run(std::move(empty_capabilities)); |
| 23 } | 33 } |
| 24 | 34 |
| 25 void RunTakePhotoCallback(const ImageCaptureImpl::TakePhotoCallback& callback, | 35 void RunFailedTakePhotoCallback( |
| 26 const std::string& mime_type, | 36 const ImageCaptureImpl::TakePhotoCallback& callback) { |
| 27 std::unique_ptr<std::vector<uint8_t>> data) { | 37 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 28 DCHECK(data.get()); | 38 BrowserThread::PostTask( |
| 29 BrowserThread::PostTask( | 39 BrowserThread::UI, FROM_HERE, |
| 30 BrowserThread::UI, FROM_HERE, | 40 base::Bind(&RunFailedTakePhotoCallback, std::move(callback))); |
| 31 base::Bind(&RunMojoCallback, callback, mime_type, | 41 return; |
| 32 base::Passed(mojo::Array<uint8_t>::From(*data)))); | 42 } |
| 43 |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 callback.Run("", mojo::Array<uint8_t>()); |
| 33 } | 46 } |
| 34 | 47 |
| 35 void TakePhotoOnIOThread(const mojo::String& source_id, | 48 void TakePhotoOnIOThread( |
| 36 const ImageCaptureImpl::TakePhotoCallback& callback, | 49 const mojo::String& source_id, |
| 37 MediaStreamManager* media_stream_manager) { | 50 MediaStreamManager* media_stream_manager, |
| 51 media::ScopedCallback<ImageCaptureImpl::TakePhotoCallback> callback) { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 39 | 53 |
| 40 const int session_id = | 54 const int session_id = |
| 41 media_stream_manager->VideoDeviceIdToSessionId(source_id); | 55 media_stream_manager->VideoDeviceIdToSessionId(source_id); |
| 42 | 56 |
| 43 if (session_id == StreamDeviceInfo::kNoId || | 57 if (session_id == StreamDeviceInfo::kNoId) |
| 44 !media_stream_manager->video_capture_manager()->TakePhoto( | 58 return; |
| 45 session_id, base::Bind(&RunTakePhotoCallback, callback))) { | 59 media_stream_manager->video_capture_manager()->TakePhoto(session_id, |
| 46 std::unique_ptr<std::vector<uint8_t>> empty_vector( | 60 std::move(callback)); |
| 47 new std::vector<uint8_t>()); | |
| 48 RunTakePhotoCallback(callback, "", std::move(empty_vector)); | |
| 49 } | |
| 50 } | 61 } |
| 51 | 62 |
| 52 } // anonymous namespace | 63 } // anonymous namespace |
| 53 | 64 |
| 54 // static | 65 // static |
| 55 void ImageCaptureImpl::Create( | 66 void ImageCaptureImpl::Create( |
| 56 mojo::InterfaceRequest<blink::mojom::ImageCapture> request) { | 67 mojo::InterfaceRequest<blink::mojom::ImageCapture> request) { |
| 57 // |binding_| will take ownership of ImageCaptureImpl. | 68 // |binding_| will take ownership of ImageCaptureImpl. |
| 58 new ImageCaptureImpl(std::move(request)); | 69 new ImageCaptureImpl(std::move(request)); |
| 59 } | 70 } |
| 60 | 71 |
| 61 ImageCaptureImpl::~ImageCaptureImpl() {} | 72 ImageCaptureImpl::~ImageCaptureImpl() {} |
| 62 | 73 |
| 63 void ImageCaptureImpl::GetCapabilities( | 74 void ImageCaptureImpl::GetCapabilities( |
| 64 const mojo::String& source_id, | 75 const mojo::String& source_id, |
| 65 const GetCapabilitiesCallback& callback) { | 76 const GetCapabilitiesCallback& callback) { |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 | 78 |
| 68 blink::mojom::PhotoCapabilitiesPtr empty_capabilities = | 79 media::ScopedCallback<GetCapabilitiesCallback> scoped_callback( |
| 69 blink::mojom::PhotoCapabilities::New(); | 80 std::move(callback), base::Bind(&RunFailedGetCapabilitiesCallback)); |
| 70 empty_capabilities->zoom = blink::mojom::Range::New(); | |
| 71 callback.Run(std::move(empty_capabilities)); | |
| 72 } | 81 } |
| 73 | 82 |
| 74 | |
| 75 void ImageCaptureImpl::TakePhoto(const mojo::String& source_id, | 83 void ImageCaptureImpl::TakePhoto(const mojo::String& source_id, |
| 76 const TakePhotoCallback& callback) { | 84 const TakePhotoCallback& callback) { |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 86 |
| 87 media::ScopedCallback<TakePhotoCallback> scoped_callback( |
| 88 std::move(callback), base::Bind(&RunFailedTakePhotoCallback)); |
| 89 |
| 78 // media_stream_manager() can only be called on UI thread. | 90 // media_stream_manager() can only be called on UI thread. |
| 79 BrowserThread::PostTask( | 91 BrowserThread::PostTask( |
| 80 BrowserThread::IO, FROM_HERE, | 92 BrowserThread::IO, FROM_HERE, |
| 81 base::Bind(&TakePhotoOnIOThread, source_id, callback, | 93 base::Bind(&TakePhotoOnIOThread, source_id, |
| 82 BrowserMainLoop::GetInstance()->media_stream_manager())); | 94 BrowserMainLoop::GetInstance()->media_stream_manager(), |
| 95 base::Passed(std::move(scoped_callback)))); |
| 83 } | 96 } |
| 84 | 97 |
| 85 ImageCaptureImpl::ImageCaptureImpl( | 98 ImageCaptureImpl::ImageCaptureImpl( |
| 86 mojo::InterfaceRequest<blink::mojom::ImageCapture> request) | 99 mojo::InterfaceRequest<blink::mojom::ImageCapture> request) |
| 87 : binding_(this, std::move(request)) {} | 100 : binding_(this, std::move(request)) {} |
| 88 | 101 |
| 89 } // namespace content | 102 } // namespace content |
| OLD | NEW |