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 private functionBackImpl: VideoCallBack = { 27 videoUri: (videoUri: any): void => { 28 Log.info(`${TAG} functionBackImpl videoUri ${videoUri}`); 29 this.mWorkerManager.postMessage(Action.updateVideoUri(videoUri)); 30 }, 31 onRecodeError:(data: any): void => { 32 this.mWorkerManager.postMessage(Action.recordError()); 33 this.mWorkerManager.postMessage(Action.reStartPreview(1)); 34 } 35 } 36 37 private async startRecording() { 38 Log.info(`${TAG} startRecording E`); 39 GlobalContext.get().setObject('startRecordingFlag', true); 40 GlobalContext.get().setObject('cameraStatus', CameraStatus.CAMERA_BEGIN_TAKE_VIDEO); 41 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 42 await this.mCameraService.StartRecording(this.functionBackImpl); 43 // TODO update video status in State by sending action 44 // temp code 45 this.mWorkerManager.postMessage(Action.updateRecordingPaused(false)); 46 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 47 48 GlobalContext.get().setObject('startRecordingFlag', false); 49 Log.info(`${TAG} globalThis.stopRecording : ` + GlobalContext.get().getT<boolean>('stopRecordingFlag')); 50 if (GlobalContext.get().getT<boolean>('stopRecordingFlag')) { 51 this.stopRecording(); 52 GlobalContext.get().setObject('stopRecordingFlag', false); 53 } 54 Log.info(`${TAG} startRecording X`); 55 } 56 57 private async pauseRecording() { 58 Log.info(`${TAG} pauseRecording E`); 59 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 60 await this.mCameraService.pauseRecording(); 61 // TODO update video status in State by sending action 62 // temp code 63 this.mWorkerManager.postMessage(Action.updateRecordingPaused(true)); 64 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 65 Log.info(`${TAG} pauseRecording X`); 66 } 67 68 private async resumeRecording() { 69 Log.info(`${TAG} resumeRecording E`); 70 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 71 await this.mCameraService.resumeRecording(); 72 // TODO update video status in State by sending action 73 // temp code 74 this.mWorkerManager.postMessage(Action.updateRecordingPaused(false)); 75 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 76 Log.info(`${TAG} resumeRecording X`); 77 } 78 79 private async stopRecording() { 80 Log.info(`${TAG} stopRecording E`); 81 82 Log.info(`${TAG} globalThis.startRecording : ${JSON.stringify(GlobalContext.get().getT<boolean>('startRecordingFlag'))}`); 83 if (GlobalContext.get().getT<boolean>('startRecordingFlag')) { 84 return; 85 } 86 this.disableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 87 const thumbnailPixelMap = await this.mCameraService.stopRecording(); 88 // TODO update video status in State by sending action 89 // temp code 90 this.mWorkerManager.postMessage(Action.updateRecordingTime(0)); 91 this.mWorkerManager.postMessage(Action.updateRecordingTimeDisplay('00:00')); 92 this.mWorkerManager.postMessage(Action.updateRecordingSpotVisible(false)); 93 this.mWorkerManager.postMessage(Action.updateRecordingPaused(false)); 94 this.mWorkerManager.postMessage(Action.updateThumbnail(thumbnailPixelMap, '')); 95 GlobalContext.get().setObject('cameraStatus', CameraStatus.CAMERA_TAKE_VIDEO_FINISHED); 96 this.mWorkerManager.postMessage(Action.updateCameraStatus()); 97 this.enableUiWithMode(UiStateMode.EXCLUDE_PREVIEW); 98 Log.info(`${TAG} stopRecording X`); 99 } 100 101 load(): void{ 102 this.mEventBus.on(Action.ACTION_RECORD_START, this.startRecording.bind(this)); 103 this.mEventBus.on(Action.ACTION_RECORD_PAUSE, this.pauseRecording.bind(this)); 104 this.mEventBus.on(Action.ACTION_RECORD_RESUME, this.resumeRecording.bind(this)); 105 this.mEventBus.on(Action.ACTION_RECORD_STOP, this.stopRecording.bind(this)); 106 } 107 108 unload(): void { 109 this.mEventBus.off(Action.ACTION_RECORD_START, this.startRecording.bind(this)); 110 this.mEventBus.off(Action.ACTION_RECORD_PAUSE, this.pauseRecording.bind(this)); 111 this.mEventBus.off(Action.ACTION_RECORD_RESUME, this.resumeRecording.bind(this)); 112 this.mEventBus.off(Action.ACTION_RECORD_STOP, this.stopRecording.bind(this)); 113 } 114}