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 #include "media_data_source.h"
27 
28 namespace OHOS {
29 class Surface;
30 
31 namespace Media {
32 /**
33  * Dummy source id, used to configure recorder parameter for source-independent parameter. For
34  * example, when specify the maximum duration of the output file, a "sourceId" parameter is still
35  * required by the "Configure" interface of RecorderEngine. Then, internally use the DUMMY_SOURCE_ID
36  * to represent the parameter is source-independent.
37  */
38 constexpr int32_t DUMMY_SOURCE_ID = 0;
39 
40 /**
41  * Recorder Engine Observer. This is a abstract class, engine's user need to implement it and register
42  * its instance into engine. The  recorder engine will report itself's information or error asynchronously
43  * to observer.
44  */
45 class IRecorderEngineObs : public std::enable_shared_from_this<IRecorderEngineObs> {
46 public:
47     enum InfoType : int32_t {
48         MAX_DURATION_APPROACHING = 0,
49         MAX_FILESIZE_APPROACHING,
50         MAX_DURATION_REACHED,
51         MAX_FILESIZE_REACHED,
52         NEXT_OUTPUT_FILE_STARTED,
53         FILE_SPLIT_FINISHED,  // reserved
54         FILE_START_TIME_MS,   // reserved
55         NEXT_FILE_FD_NOT_SET,
56         INTERNEL_WARNING,
57         INFO_EXTEND_START = 0x10000,
58     };
59 
60     enum ErrorType : int32_t {
61         ERROR_CREATE_FILE_FAIL = 0,
62         ERROR_WRITE_FILE_FAIL,
63         ERROR_INTERNAL,
64         ERROR_EXTEND_START = 0X10000,
65     };
66 
67     virtual ~IRecorderEngineObs() = default;
68     virtual void OnError(ErrorType errorType, int32_t errorCode) = 0;
69     virtual void OnInfo(InfoType type, int32_t extra) = 0;
70     virtual void OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo) = 0;
71 };
72 
73 /**
74  * Recorder Engine Interface.
75  */
76 class IRecorderEngine {
77 public:
78     virtual ~IRecorderEngine() = default;
79 
80     /**
81      * Sets the video source for recording. The sourceId can be used to identify the video source when configure
82      * the video track's any properties. When the setting is failed, the sourceId is -1.
83      * This interface must be called before SetOutputFormat.
84      * Return MSERR_OK indicates success, or others indicate failed.
85      */
86     virtual int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId) = 0;
87 
88     /**
89      * Sets the audio source for recording. The sourceId can be used to identify the audio source when configure
90      * the audio track's any properties. When the setting is failed, the sourceId is -1.
91      * This interface must be called before SetOutputFormat.
92      * Return MSERR_OK indicates success, or others indicate failed.
93      */
94     virtual int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId) = 0;
95 
96     /**
97      * Sets the meta source for recording. The sourceId can be used to identify the meta source when configure
98      * the meta track's any properties. When the setting is failed, the sourceId is -1.
99      * This interface must be called before SetOutputFormat.
100      * Return MSERR_OK indicates success, or others indicate failed.
101      */
102     virtual int32_t SetMetaSource(MetaSourceType source, int32_t &sourceId) = 0;
103 
104     /**
105      * Sets the audio data source for recording. The sourceId can be used to identify the audio data source
106      * when configure the audio track's any properties. When the setting is failed, the sourceId is -1.
107      * This interface must be called before SetOutputFormat.
108      * Return MSERR_OK indicates success, or others indicate failed.
109      */
SetAudioDataSource(const std::shared_ptr<IAudioDataSource> & audioSource,int32_t & sourceId)110     virtual int32_t SetAudioDataSource(const std::shared_ptr<IAudioDataSource>& audioSource, int32_t& sourceId)
111     {
112         (void)audioSource;
113         (void)sourceId;
114         return 0;
115     };
116 
117     /**
118      * Sets the output file format. The function must be called after SetVideoSource or SetAudioSource, and before
119      * Prepare. After this interface called, the engine will not accept any source setting interface call.
120      * Return MSERR_OK indicates success, or others indicate failed.
121      */
122     virtual int32_t SetOutputFormat(OutputFormatType format) = 0;
123 
124     /**
125      * Register a recording observer.
126      * Return MSERR_OK indicates success, or others indicate failed.
127      */
128     virtual int32_t SetObs(const std::weak_ptr<IRecorderEngineObs> &obs) = 0;
129 
130     /**
131      * Configure static record parameters before calling the Prepare interface. The interface must be called after
132      * SetOutputFormat. The sourceId indicates the source ID, which can be obtained from SetVideoSource
133      * or SetAudioSource. Use the DUMMY_SOURCE_ID  to configure the source-independent parameters.
134      * Return MSERR_OK indicates success, or others indicate failed.
135      */
136     virtual int32_t Configure(int32_t sourceId, const RecorderParam &recParam) = 0;
137 
138     /**
139      * Obtains the surface of the video source. The sourceId indicates the video source ID, which can be obtained
140      * from SetVideoSource.
141      */
142     virtual sptr<Surface> GetSurface(int32_t sourceId) = 0;
143 
144     /**
145      * Obtains the surface of the meta source. The sourceId indicates the meta source ID, which can be obtained
146      * from SetMetaSource.
147      */
148     virtual sptr<Surface> GetMetaSurface(int32_t sourceId) = 0;
149 
150     /**
151      * Prepares for recording. This function must be called before Start. Ensure all required recorder parameter
152      * have already been set, or this call will be failed.
153      * Return MSERR_OK indicates success, or others indicate failed.
154      */
155     virtual int32_t Prepare() = 0;
156 
157     /**
158      * Starts recording. This function must be called after Prepare.
159      * Return MSERR_OK indicates success, or others indicate failed.
160      */
161     virtual int32_t Start() = 0;
162 
163     /**
164      * Pause recording. This function must be called during recording.
165      * Return MSERR_OK indicates success, or others indicate failed.
166      */
167     virtual int32_t Pause() = 0;
168 
169     /**
170      * Resume recording. This function must be called during recording. After called, the paused recording will
171      * be resumed.
172      * Return MSERR_OK indicates success, or others indicate failed.
173      */
174     virtual int32_t Resume() = 0;
175 
176     /**
177      * Stop recording. This function must be called during recording. The isDrainAll indicates whether all caches
178      * need to be discarded. If true, wait all caches to be processed, or discard all caches.
179      * Currently, this interface behaves like the Reset, so after it called, anything need to be reconfigured.
180      * Return MSERR_OK indicates success, or others indicate failed.
181      */
182     virtual int32_t Stop(bool isDrainAll = false) = 0;
183 
184     /**
185      * Resets the recording. After this interface called, anything need to be reconfigured.
186      * Return MSERR_OK indicates success, or others indicate failed.
187      */
188     virtual int32_t Reset() = 0;
189 
190     /**
191      * Sets an extended parameter for recording. It must be called after Prepare. The sourceId indicates the
192      * source ID, which can be obtained from SetVideoSource or SetAudioSource. Use the DUMMY_SOURCE_ID  to
193      * configure the source-independent parameters.
194      * Return MSERR_OK indicates success, or others indicate failed.
195      */
196     virtual int32_t SetParameter(int32_t sourceId, const RecorderParam &recParam) = 0;
197     /**
198      * Get current audio capturer change info.
199      * Return MSERR_OK indicates success, or others indicate failed.
200      */
201     virtual int32_t GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo &changeInfo) = 0;
202     /**
203      * Get available encoder capability info.
204      * Return MSERR_OK indicates success, or others indicate failed.
205      */
206     virtual int32_t GetAvailableEncoder(std::vector<EncoderCapabilityData> &encoderInfo) = 0;
207     /**
208      * Get current audio max amplitude.
209      * Return max amplitude.
210      */
211     virtual int32_t GetMaxAmplitude() = 0;
212     /**
213      * Set App calling info for recording.
214      */
215     virtual void SetCallingInfo(const std::string &bundleName, uint64_t instanceId) = 0;
216     /**
217      * check if the avrecorder has watermark capability.
218     */
219     virtual int32_t IsWatermarkSupported(bool &isWatermarkSupported) = 0;
220     /**
221      * Set watermark config
222     */
223     virtual int32_t SetWatermark(std::shared_ptr<AVBuffer> &waterMarkBuffer) = 0;
224 };
225 } // namespace Media
226 } // namespace OHOS
227 #endif
228