1 /* 2 * Copyright (C) 2021 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 16 #ifndef IRECORDER_ENGINE_H 17 #define IRECORDER_ENGINE_H 18 19 #include <cstdint> 20 #include <string> 21 #include <memory> 22 #include <refbase.h> 23 #include "nocopyable.h" 24 #include "recorder.h" 25 #include "recorder_param.h" 26 27 namespace OHOS { 28 class Surface; 29 30 namespace Media { 31 /** 32 * Dummy source id, used to configure recorder parameter for source-independent parameter. For 33 * example, when specify the maximum duration of the output file, a "sourceId" parameter is still 34 * required by the "Configure" interface of RecorderEngine. Then, internally use the DUMMY_SOURCE_ID 35 * to represent the parameter is source-independent. 36 */ 37 constexpr int32_t DUMMY_SOURCE_ID = 0; 38 39 /** 40 * Recorder Engine Observer. This is a abstract class, engine's user need to implement it and register 41 * its instance into engine. The recorder engine will report itself's information or error asynchronously 42 * to observer. 43 */ 44 class IRecorderEngineObs : public std::enable_shared_from_this<IRecorderEngineObs> { 45 public: 46 enum InfoType : int32_t { 47 MAX_DURATION_APPROACHING = 0, 48 MAX_FILESIZE_APPROACHING, 49 MAX_DURATION_REACHED, 50 MAX_FILESIZE_REACHED, 51 NEXT_OUTPUT_FILE_STARTED, 52 FILE_SPLIT_FINISHED, // reserved 53 FILE_START_TIME_MS, // reserved 54 NEXT_FILE_FD_NOT_SET, 55 INTERNEL_WARNING, 56 INFO_EXTEND_START = 0x10000, 57 }; 58 59 enum ErrorType : int32_t { 60 ERROR_CREATE_FILE_FAIL = 0, 61 ERROR_WRITE_FILE_FAIL, 62 ERROR_INTERNAL, 63 ERROR_EXTEND_START = 0X10000, 64 }; 65 66 virtual ~IRecorderEngineObs() = default; 67 virtual void OnError(ErrorType errorType, int32_t errorCode) = 0; 68 virtual void OnInfo(InfoType type, int32_t extra) = 0; 69 }; 70 71 /** 72 * Recorder Engine Interface. 73 */ 74 class IRecorderEngine { 75 public: 76 virtual ~IRecorderEngine() = default; 77 78 /** 79 * Sets the video source for recording. The sourceId can be used to identify the video source when configure 80 * the video track's any properties. When the setting is failed, the sourceId is -1. 81 * This interface must be called before SetOutputFormat. 82 * Return MSERR_OK indicates success, or others indicate failed. 83 */ 84 virtual int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId) = 0; 85 86 /** 87 * Sets the audio source for recording. The sourceId can be used to identify the audio source when configure 88 * the audio track's any properties. When the setting is failed, the sourceId is -1. 89 * This interface must be called before SetOutputFormat. 90 * Return MSERR_OK indicates success, or others indicate failed. 91 */ 92 virtual int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId) = 0; 93 94 /** 95 * Sets the output file format. The function must be called after SetVideoSource or SetAudioSource, and before 96 * Prepare. After this interface called, the engine will not accept any source setting interface call. 97 * Return MSERR_OK indicates success, or others indicate failed. 98 */ 99 virtual int32_t SetOutputFormat(OutputFormatType format) = 0; 100 101 /** 102 * Register a recording observer. 103 * Return MSERR_OK indicates success, or others indicate failed. 104 */ 105 virtual int32_t SetObs(const std::weak_ptr<IRecorderEngineObs> &obs) = 0; 106 107 /** 108 * Configure static record parameters before calling the Prepare interface. The interface must be called after 109 * SetOutputFormat. The sourceId indicates the source ID, which can be obtained from SetVideoSource 110 * or SetAudioSource. Use the DUMMY_SOURCE_ID to configure the source-independent parameters. 111 * Return MSERR_OK indicates success, or others indicate failed. 112 */ 113 virtual int32_t Configure(int32_t sourceId, const RecorderParam &recParam) = 0; 114 115 /** 116 * Obtains the surface of the video source. The sourceId indicates the video source ID, which can be obtained 117 * from SetVideoSource. 118 */ 119 virtual sptr<Surface> GetSurface(int32_t sourceId) = 0; 120 121 /** 122 * Prepares for recording. This function must be called before Start. Ensure all required recorder parameter 123 * have already been set, or this call will be failed. 124 * Return MSERR_OK indicates success, or others indicate failed. 125 */ 126 virtual int32_t Prepare() = 0; 127 128 /** 129 * Starts recording. This function must be called after Prepare. 130 * Return MSERR_OK indicates success, or others indicate failed. 131 */ 132 virtual int32_t Start() = 0; 133 134 /** 135 * Pause recording. This function must be called during recording. 136 * Return MSERR_OK indicates success, or others indicate failed. 137 */ 138 virtual int32_t Pause() = 0; 139 140 /** 141 * Resume recording. This function must be called during recording. After called, the paused recording will 142 * be resumed. 143 * Return MSERR_OK indicates success, or others indicate failed. 144 */ 145 virtual int32_t Resume() = 0; 146 147 /** 148 * Stop recording. This function must be called during recording. The isDrainAll indicates whether all caches 149 * need to be discarded. If true, wait all caches to be processed, or discard all caches. 150 * Currently, this interface behaves like the Reset, so after it called, anything need to be reconfigured. 151 * Return MSERR_OK indicates success, or others indicate failed. 152 */ 153 virtual int32_t Stop(bool isDrainAll = false) = 0; 154 155 /** 156 * Resets the recording. After this interface called, anything need to be reconfigured. 157 * Return MSERR_OK indicates success, or others indicate failed. 158 */ 159 virtual int32_t Reset() = 0; 160 161 /** 162 * Sets an extended parameter for recording. It must be called after Prepare. The sourceId indicates the 163 * source ID, which can be obtained from SetVideoSource or SetAudioSource. Use the DUMMY_SOURCE_ID to 164 * configure the source-independent parameters. 165 * Return MSERR_OK indicates success, or others indicate failed. 166 */ 167 virtual int32_t SetParameter(int32_t sourceId, const RecorderParam &recParam) = 0; 168 }; 169 } // namespace Media 170 } // namespace OHOS 171 #endif 172