1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Action, UiStateMode } from '../redux/actions/Action'; 17import { Log } from '../utils/Log'; 18import { CameraStatus } from '../utils/Constants'; 19import { BaseFunction } from './BaseFunction'; 20import type { VideoCallBack } from '../camera/CameraService'; 21import { GlobalContext } from '../utils/GlobalContext'; 22 23const TAG = '[RecordFunction]:'; 24 25export class RecordFunction extends BaseFunction { 26 load(): void { 27 this.mEventBus.on(Action.ACTION_RECORD_START, this.startRecording.bind(this)); 28 this.mEventBus.on(Action.ACTION_RECORD_PAUSE, this.pauseRecording.bind(this)); 29 this.mEventBus.on(Action.ACTION_RECORD_RESUME, this.resumeRecording.bind(this)); 30 this.mEventBus.on(Action.ACTION_RECORD_STOP, this.stopRecording.bind(this)); 31 } 32 33 unload(): void { 34 this.mEventBus.off(Action.ACTION_RECORD_START, this.startRecording.bind(this)); 35 this.mEventBus.off(Action.ACTION_RECORD_PAUSE, this.pauseRecording.bind(this)); 36 this.mEventBus.off(Action.ACTION_RECORD_RESUME, this.resumeRecording.bind(this)); 37 this.mEventBus.off(Action.ACTION_RECORD_STOP, this.stopRecording.bind(this)); 38 } 39 40 private functionBackImpl: VideoCallBack = { 41 videoUri: (videoUri: any): void => { 42 Log.info(`${TAG} functionBackImpl videoUri ${videoUri}`); 43 this.mWorkerManager.postMessage(Action.updateVideoUri(videoUri)); 44 }, 45 onRecodeError: (data: any): void => { 46 this.mWorkerManager.postMessage(Action.recordError()); 47 this.mWorkerManager.postMessage(Action.reStartPreview(1)); 48 } 49 } 50 51 private async startRecording() { 52 Log.info(`${TAG} startRecording E`); 53 GlobalContext.get().setObject('startRecordingFlag', true); 54 GlobalContext.get().setObject('cameraStatus', CameraStatus.CAMERA_BEGIN_TAKE_VIDEO); 55 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 56 await this.mCameraService.StartRecording(this.functionBackImpl); 57 // TODO update video status in State by sending action 58 // temp code 59 this.mWorkerManager.postMessage(Action.updateRecordingPaused(false)); 60 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 61 62 GlobalContext.get().setObject('startRecordingFlag', false); 63 Log.info(`${TAG} globalThis.stopRecording : ` + GlobalContext.get().getT<boolean>('stopRecordingFlag')); 64 if (GlobalContext.get().getT<boolean>('stopRecordingFlag')) { 65 this.stopRecording(); 66 GlobalContext.get().setObject('stopRecordingFlag', false); 67 } 68 Log.info(`${TAG} startRecording X`); 69 } 70 71 private async pauseRecording() { 72 Log.info(`${TAG} pauseRecording E`); 73 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 74 await this.mCameraService.pauseRecording(); 75 // TODO update video status in State by sending action 76 // temp code 77 this.mWorkerManager.postMessage(Action.updateRecordingPaused(true)); 78 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 79 Log.info(`${TAG} pauseRecording X`); 80 } 81 82 private async resumeRecording() { 83 Log.info(`${TAG} resumeRecording E`); 84 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 85 await this.mCameraService.resumeRecording(); 86 // TODO update video status in State by sending action 87 // temp code 88 this.mWorkerManager.postMessage(Action.updateRecordingPaused(false)); 89 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 90 Log.info(`${TAG} resumeRecording X`); 91 } 92 93 private async stopRecording() { 94 Log.info(`${TAG} stopRecording E`); 95 96 Log.info(`${TAG} globalThis.startRecording : ${JSON.stringify(GlobalContext.get() 97 .getT<boolean>('startRecordingFlag'))}`); 98 if (GlobalContext.get().getT<boolean>('startRecordingFlag')) { 99 return; 100 } 101 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 102 const thumbnailPixelMap = await this.mCameraService.stopRecording(); 103 // TODO update video status in State by sending action 104 // temp code 105 this.mWorkerManager.postMessage(Action.updateRecordingTime(0)); 106 this.mWorkerManager.postMessage(Action.updateRecordingTimeDisplay('00:00')); 107 this.mWorkerManager.postMessage(Action.updateRecordingSpotVisible(false)); 108 this.mWorkerManager.postMessage(Action.updateRecordingPaused(false)); 109 this.mWorkerManager.postMessage(Action.updateThumbnail(thumbnailPixelMap, '')); 110 GlobalContext.get().setObject('cameraStatus', CameraStatus.CAMERA_TAKE_VIDEO_FINISHED); 111 this.mWorkerManager.postMessage(Action.updateCameraStatus()); 112 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 113 Log.info(`${TAG} stopRecording X`); 114 } 115}