Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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_BROWSER_RENDERER_HOST_MEDIA_BUILDABLE_VIDEO_CAPTURE_DEVICE_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_BUILDABLE_VIDEO_CAPTURE_DEVICE_H_ | |
| 7 | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "content/public/common/media_stream_request.h" | |
| 11 #include "media/capture/video/video_capture_device.h" | |
| 12 #include "media/capture/video_capture_types.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 class VideoCaptureController; | |
| 17 | |
| 18 class BuildableDeviceCallbacks { | |
| 19 public: | |
| 20 virtual ~BuildableDeviceCallbacks() {} | |
| 21 // Returns false if no descriptor was found. | |
| 22 virtual const media::VideoCaptureDeviceDescriptor* LookupDeviceDescriptor( | |
| 23 const std::string& id) = 0; | |
| 24 virtual void WillStartDevice(media::VideoFacingMode facing_mode) = 0; | |
| 25 virtual void DidStartDevice(VideoCaptureController* controller) = 0; | |
| 26 virtual void OnDeviceStartFailed(VideoCaptureController* controller) = 0; | |
| 27 }; | |
| 28 | |
| 29 #define FAIL_COMPILE_REQUIRE_NON_CONST_RVALUE \ | |
| 30 static_assert(!FailCompileOnWrongUsage, \ | |
| 31 "This method may only be invoked on a non-const rvalue" \ | |
| 32 ", i.e. std::move(object).Method().") | |
| 33 | |
| 34 // Encapsulates ownership of |context_reference| and enforces that the instance | |
| 35 // is released when certain methods are invoked. | |
| 36 template <bool FailCompileOnWrongUsage = true> | |
| 37 class DeviceBuildContextT { | |
| 38 public: | |
| 39 DeviceBuildContextT(base::OnceClosure context_reference, | |
| 40 BuildableDeviceCallbacks* callbacks) | |
| 41 : context_reference_(std::move(context_reference)), | |
| 42 callbacks_(callbacks) {} | |
| 43 | |
| 44 const media::VideoCaptureDeviceDescriptor* LookupDeviceDescriptor( | |
| 45 const std::string& id) { | |
| 46 return callbacks_->LookupDeviceDescriptor(id); | |
| 47 } | |
| 48 | |
| 49 void WillStartDevice(media::VideoFacingMode facing_mode) { | |
| 50 callbacks_->WillStartDevice(facing_mode); | |
| 51 } | |
| 52 | |
| 53 void DidStartDevice(VideoCaptureController* controller) const & { | |
| 54 FAIL_COMPILE_REQUIRE_NON_CONST_RVALUE; | |
| 55 } | |
| 56 | |
| 57 void DidStartDevice(VideoCaptureController* controller) && { | |
| 58 callbacks_->DidStartDevice(controller); | |
| 59 } | |
| 60 | |
| 61 void OnDeviceStartFailed(VideoCaptureController* controller) const & { | |
| 62 FAIL_COMPILE_REQUIRE_NON_CONST_RVALUE; | |
| 63 } | |
| 64 | |
| 65 void OnDeviceStartFailed(VideoCaptureController* controller) && { | |
| 66 callbacks_->OnDeviceStartFailed(controller); | |
|
miu
2017/03/20 23:02:15
This is an interesting way ensure the methods are
chfremer
2017/03/20 23:58:26
Agreed. I had no idea this was possible until I ch
| |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 // For pure move-only ownership. Not meant to be called. | |
| 71 base::OnceClosure context_reference_; | |
| 72 | |
| 73 BuildableDeviceCallbacks* callbacks_; | |
| 74 }; | |
| 75 using DeviceBuildContext = DeviceBuildContextT<>; | |
|
miu
2017/03/20 23:02:15
Suggestion: Taking your idea, but re-stating it wi
chfremer
2017/03/20 23:58:26
That is a very nice idea. I tried something simila
| |
| 76 | |
| 77 // Abstraction for a video capture device that must be "built" before it can be | |
| 78 // operated and must be "stopped", before it can be released. Typical operation | |
| 79 // is that a newly created instance initially reports IsDeviceAlive() == false. | |
| 80 // Clients call CreateAndStartDeviceAsync(), which kicks off the asynchronous | |
| 81 // building of the device. The outcome of the device building is typically | |
| 82 // reported to an instance of BuildableDeviceCallbacks (see above). Once the | |
| 83 // device has been built successfully, the "Device operation methods", are | |
| 84 // allowed to be called. ReleaseDeviceAsync() must be called in order to | |
| 85 // release the device if it has before been built successfully. After calling | |
| 86 // ReleaseDeviceAsync(), it is legal to call CreateAndStartDeviceAsync() to | |
| 87 // rebuild and start the device again. | |
| 88 class BuildableVideoCaptureDevice { | |
| 89 public: | |
| 90 virtual ~BuildableVideoCaptureDevice() {} | |
| 91 | |
| 92 // Device management methods. | |
| 93 // The passed-in |context_reference| must guarantee that the context relevant | |
| 94 // during the asynchronous processing stays alive. | |
| 95 virtual void CreateAndStartDeviceAsync( | |
| 96 VideoCaptureController* controller, | |
| 97 const media::VideoCaptureParams& params, | |
| 98 DeviceBuildContext context) = 0; | |
| 99 // The passed-in |context_reference| must guarantee that the context relevant | |
| 100 // during the asynchronous processing stays alive. | |
| 101 virtual void ReleaseDeviceAsync(VideoCaptureController* controller, | |
| 102 base::OnceClosure done_cb) = 0; | |
| 103 virtual bool IsDeviceAlive() const = 0; | |
| 104 | |
| 105 // Device operation methods. | |
| 106 virtual void GetPhotoCapabilities( | |
| 107 media::VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) | |
| 108 const = 0; | |
| 109 virtual void SetPhotoOptions( | |
| 110 media::mojom::PhotoSettingsPtr settings, | |
| 111 media::VideoCaptureDevice::SetPhotoOptionsCallback callback) = 0; | |
| 112 virtual void TakePhoto( | |
| 113 media::VideoCaptureDevice::TakePhotoCallback callback) = 0; | |
| 114 virtual void MaybeSuspendDevice() = 0; | |
| 115 virtual void ResumeDevice() = 0; | |
| 116 virtual void RequestRefreshFrame() = 0; | |
| 117 | |
| 118 // Methods for specific types of devices. | |
| 119 // The passed-in |context_reference| must guarantee that the context relevant | |
| 120 // during the asynchronous processing stays alive. | |
| 121 virtual void SetDesktopCaptureWindowIdAsync(gfx::NativeViewId window_id, | |
| 122 base::OnceClosure done_cb) = 0; | |
| 123 }; | |
| 124 | |
| 125 } // namespace content | |
| 126 | |
| 127 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_BUILDABLE_VIDEO_CAPTURE_DEVICE_H_ | |
| OLD | NEW |