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