Commit c8b6f7d4 authored by Kuo Jen Wei's avatar Kuo Jen Wei Committed by Commit Bot

[CCA] Migrate cca.mojo.ImageCapture to ES6 class

Bug: b/141518780
Test: Pass closure compiler check, tast run <DUT> 'camera.CCAUI*' and
validate all function of CCA on HALv1/v3 device works correctly.

Change-Id: I29c0113218f1214fd438f04c1c45d3945601b8b3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1955000
Auto-Submit: Kuo Jen Wei <inker@chromium.org>
Reviewed-by: default avatarShik Chen <shik@chromium.org>
Commit-Queue: Kuo Jen Wei <inker@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723304}
parent de19be1f
......@@ -26,100 +26,100 @@ cca.mojo.PhotoCapabilities.prototype.supportedEffects;
/**
* Creates the wrapper of JS image-capture and Mojo image-capture.
* @param {!MediaStreamTrack} videoTrack A video track whose still images will
* be taken.
* @constructor
*/
cca.mojo.ImageCapture = function(videoTrack) {
cca.mojo.ImageCapture = class {
/**
* The id of target media device.
* @type {string}
* @private
* @param {!MediaStreamTrack} videoTrack A video track whose still images will
* be taken.
*/
this.deviceId_ = videoTrack.getSettings().deviceId;
constructor(videoTrack) {
/**
* The id of target media device.
* @type {string}
* @private
*/
this.deviceId_ = videoTrack.getSettings().deviceId;
/**
* The standard ImageCapture object.
* @type {ImageCapture}
* @private
*/
this.capture_ = new ImageCapture(videoTrack);
}
/**
* The standard ImageCapture object.
* @type {ImageCapture}
* @private
* Gets the photo capabilities with the available options/effects.
* @return {!Promise<!PhotoCapabilities|cca.mojo.PhotoCapabilities>} Promise
* for the result.
*/
this.capture_ = new ImageCapture(videoTrack);
// End of properties, seal the object.
Object.seal(this);
};
async getPhotoCapabilities() {
const deviceOperator = await cca.mojo.DeviceOperator.getInstance();
if (!deviceOperator) {
return this.capture_.getPhotoCapabilities();
}
/**
* Gets the photo capabilities with the available options/effects.
* @return {!Promise<!PhotoCapabilities|cca.mojo.PhotoCapabilities>} Promise for
* the result.
*/
cca.mojo.ImageCapture.prototype.getPhotoCapabilities = async function() {
const deviceOperator = await cca.mojo.DeviceOperator.getInstance();
if (!deviceOperator) {
return this.capture_.getPhotoCapabilities();
}
const supportedEffects = [cros.mojom.Effect.NO_EFFECT];
const isPortraitModeSupported =
await deviceOperator.isPortraitModeSupported(this.deviceId_);
if (isPortraitModeSupported) {
supportedEffects.push(cros.mojom.Effect.PORTRAIT_MODE);
}
const baseCapabilities = await this.capture_.getPhotoCapabilities();
const supportedEffects = [cros.mojom.Effect.NO_EFFECT];
const isPortraitModeSupported =
await deviceOperator.isPortraitModeSupported(this.deviceId_);
if (isPortraitModeSupported) {
supportedEffects.push(cros.mojom.Effect.PORTRAIT_MODE);
let /** !cca.mojo.PhotoCapabilities */ extendedCapabilities;
Object.assign(extendedCapabilities, baseCapabilities, {supportedEffects});
return extendedCapabilities;
}
const baseCapabilities = await this.capture_.getPhotoCapabilities();
let /** !cca.mojo.PhotoCapabilities */ extendedCapabilities;
Object.assign(extendedCapabilities, baseCapabilities, {supportedEffects});
return extendedCapabilities;
};
/**
* Takes single or multiple photo(s) with the specified settings and effects.
* The amount of result photo(s) depends on the specified settings and effects,
* and the first promise in the returned array will always resolve with the
* unreprocessed photo. The returned array will be resolved once it received
* the shutter event.
* @param {!PhotoSettings} photoSettings Photo settings for ImageCapture's
* takePhoto().
* @param {!Array<cros.mojom.Effect>=} photoEffects Photo effects to be applied.
* @return {!Promise<!Array<!Promise<!Blob>>>} A promise of the array containing
* promise of each blob result.
*/
cca.mojo.ImageCapture.prototype.takePhoto =
async function(photoSettings, photoEffects = []) {
/** @type {Array<!Promise<!Blob>>} */
const takes = [];
const deviceOperator = await cca.mojo.DeviceOperator.getInstance();
if (deviceOperator === null && photoEffects.length > 0) {
throw new Error('Applying effects is not supported on this device');
}
/**
* Takes single or multiple photo(s) with the specified settings and effects.
* The amount of result photo(s) depends on the specified settings and
* effects, and the first promise in the returned array will always resolve
* with the unreprocessed photo. The returned array will be resolved once it
* received the shutter event.
* @param {!PhotoSettings} photoSettings Photo settings for ImageCapture's
* takePhoto().
* @param {!Array<cros.mojom.Effect>=} photoEffects Photo effects to be
* applied.
* @return {!Promise<!Array<!Promise<!Blob>>>} A promise of the array
* containing promise of each blob result.
*/
async takePhoto(photoSettings, photoEffects = []) {
/** @type {Array<!Promise<!Blob>>} */
const takes = [];
const deviceOperator = await cca.mojo.DeviceOperator.getInstance();
if (deviceOperator === null && photoEffects.length > 0) {
throw new Error('Applying effects is not supported on this device');
}
for (const effect of photoEffects) {
const take = (async () => {
const {data, mimeType} =
await deviceOperator.setReprocessOption(this.deviceId_, effect);
return new Blob([new Uint8Array(data)], {type: mimeType});
})();
takes.push(take);
}
for (const effect of photoEffects) {
const take = (async () => {
const {data, mimeType} =
await deviceOperator.setReprocessOption(this.deviceId_, effect);
return new Blob([new Uint8Array(data)], {type: mimeType});
})();
takes.push(take);
}
if (deviceOperator !== null) {
let onShutterDone;
const isShutterDone = new Promise((resolve) => {
onShutterDone = resolve;
});
const observerId =
await deviceOperator.addShutterObserver(this.deviceId_, onShutterDone);
takes.unshift(this.capture_.takePhoto(photoSettings));
await isShutterDone;
const isSuccess =
await deviceOperator.removeShutterObserver(this.deviceId_, observerId);
if (!isSuccess) {
console.error('Failed to remove shutter observer');
if (deviceOperator !== null) {
let onShutterDone;
const isShutterDone = new Promise((resolve) => {
onShutterDone = resolve;
});
const observerId = await deviceOperator.addShutterObserver(
this.deviceId_, onShutterDone);
takes.unshift(this.capture_.takePhoto(photoSettings));
await isShutterDone;
const isSuccess = await deviceOperator.removeShutterObserver(
this.deviceId_, observerId);
if (!isSuccess) {
console.error('Failed to remove shutter observer');
}
return takes;
} else {
takes.unshift(this.capture_.takePhoto(photoSettings));
return takes;
}
return takes;
} else {
takes.unshift(this.capture_.takePhoto(photoSettings));
return takes;
}
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment