1 /*
2  * Copyright (c) 2020-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 #include "recorder_impl.h"
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <pthread.h>
20 #include <sys/io.h>
21 #include <sys/select.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <sys/prctl.h>
25 #include "media_log.h"
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace Media {
30 constexpr float RECORDER_DEFAULT_SPEED = 1.0;
31 constexpr uint32_t RECORDER_AUDIO_THREAD_PRIORITY = 19;
32 constexpr uint32_t RECORDER_VIDEO_THREAD_PRIORITY = 20;
33 
34 constexpr uint32_t RECORDER_VIDEO_SOURCE_ID_MASK = 0;
35 constexpr uint32_t RECORDER_AUDIO_SOURCE_ID_MASK = 0x100;
36 constexpr uint32_t RECORDER_DATA_SOURCE_ID_MASK = 0x200;
37 
CalcDiffTimeMs(struct timeval begin,struct timeval end)38 static int64_t CalcDiffTimeMs(struct timeval begin, struct timeval end)
39 {
40     const int32_t us2Ms = 1000;
41     const int32_t us2MsHalf = 500;
42     const int32_t s2Ms = 1000;
43     int64_t diffSec = static_cast<int64_t> (end.tv_sec - begin.tv_sec);
44     int64_t diffMsec = (static_cast<int64_t> (end.tv_usec - begin.tv_usec) + us2MsHalf) / us2Ms;
45     const int64_t diffSecMax = INT64_MAX / s2Ms;
46     diffSec = (diffSec > diffSecMax) ? diffSecMax : diffSec;
47     return diffSec * s2Ms + diffMsec;
48 }
49 
SetDefaultVideoConfig(RecorderVideoSourceConfig & config)50 static void SetDefaultVideoConfig(RecorderVideoSourceConfig &config)
51 {
52     const int32_t width = 1920;
53     const int32_t height = 1080;
54     const int32_t frameRate = 30;
55     const int32_t bitRate = 4 * 1024; /* kbps */
56 
57     config.videoFormat = HEVC;
58     config.width = width;
59     config.height = height;
60     config.frameRate = frameRate;
61     config.bitRate = bitRate;
62     config.captureRate = frameRate;
63     config.speed = RECORDER_DEFAULT_SPEED;
64 }
65 
SetDefaultAudioConfig(RecorderAudioSourceConfig & config)66 static void SetDefaultAudioConfig(RecorderAudioSourceConfig &config)
67 {
68     const int32_t bitRate = 64000;
69     const int32_t sampleRate = 48000;
70 
71     config.inputSource = AUDIO_MIC;
72     config.audioFormat = AAC_LC;
73     config.sampleRate = sampleRate;
74     config.channelCount = 1;
75     config.bitRate = bitRate;
76     config.streamType = TYPE_MEDIA;
77     config.bitWidth = BIT_WIDTH_16;
78 }
79 
RecorderImpl()80 RecorderImpl::RecorderImpl()
81 {
82     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
83         sourceManager_[i].videoSource = nullptr;
84         sourceManager_[i].videoSourceStarted = false;
85         sourceManager_[i].videoSourcePaused = false;
86         sourceManager_[i].videoTrackId = -1;
87         sourceManager_[i].audioSource = nullptr;
88         sourceManager_[i].audioSourceStarted = false;
89         sourceManager_[i].audioSourcePaused = false;
90         sourceManager_[i].audioTrackId = -1;
91         sourceManager_[i].dataSource = nullptr;
92         sourceManager_[i].dataSourceStarted = false;
93         sourceManager_[i].dataSourcePaused = false;
94         sourceManager_[i].dataTrackId = -1;
95 
96         SetDefaultVideoConfig(sourceManager_[i].videoSourceConfig);
97         SetDefaultAudioConfig(sourceManager_[i].audioSourceConfig);
98         sourceManager_[i].dataSourceConfig = {};
99     }
100     recorderSink_ = new(std::nothrow) RecorderSink();
101     if (recorderSink_ != nullptr) {
102         status_ = INITIALIZED;
103     }
104     MEDIA_INFO_LOG("ctor status_:%d", status_);
105 }
106 
~RecorderImpl()107 RecorderImpl::~RecorderImpl()
108 {
109     if (status_ != RELEASED) {
110         (void)Release();
111     }
112     ResetConfig();
113     if (recorderSink_ != nullptr) {
114         delete recorderSink_;
115         recorderSink_ = nullptr;
116     }
117     MEDIA_INFO_LOG("RecorderImpl dctor");
118 }
119 
ResetConfig()120 int32_t RecorderImpl::ResetConfig()
121 {
122     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
123         sourceManager_[i].videoSourceStarted = false;
124         sourceManager_[i].videoSourcePaused = false;
125         sourceManager_[i].videoTrackId = -1;
126         sourceManager_[i].audioSourceStarted = false;
127         sourceManager_[i].audioSourcePaused = false;
128         sourceManager_[i].audioTrackId = -1;
129         sourceManager_[i].dataSourceStarted = false;
130         sourceManager_[i].dataSourcePaused = false;
131         sourceManager_[i].dataTrackId = -1;
132         if (sourceManager_[i].videoSource != nullptr) {
133             delete sourceManager_[i].videoSource;
134             sourceManager_[i].videoSource = nullptr;
135         }
136         if (sourceManager_[i].audioSource != nullptr) {
137             delete sourceManager_[i].audioSource;
138             sourceManager_[i].audioSource = nullptr;
139         }
140         if (sourceManager_[i].dataSource != nullptr) {
141             delete sourceManager_[i].dataSource;
142             sourceManager_[i].dataSource = nullptr;
143         }
144         SetDefaultVideoConfig(sourceManager_[i].videoSourceConfig);
145         SetDefaultAudioConfig(sourceManager_[i].audioSourceConfig);
146 
147         if (memset_s(&sourceManager_[i].dataSourceConfig, sizeof(RecorderDataSourceConfig),
148             0x00, sizeof(RecorderDataSourceConfig)) != EOK) {
149             MEDIA_ERR_LOG("memset dataSourceConfig failed");
150             return ERR_UNKNOWN;
151         }
152     }
153     MEDIA_INFO_LOG("ResetConfig SUCCESS");
154     return SUCCESS;
155 }
156 
GetFreeVideoSourceID(int32_t & sourceId,uint32_t & freeIndex)157 int32_t RecorderImpl::GetFreeVideoSourceID(int32_t &sourceId, uint32_t &freeIndex)
158 {
159     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
160         if (sourceManager_[i].videoSource == nullptr) {
161             sourceId = static_cast<int32_t>(RECORDER_VIDEO_SOURCE_ID_MASK + i);
162             freeIndex = i;
163             return SUCCESS;
164         }
165     }
166     MEDIA_ERR_LOG("get free video sourceId failed");
167     return ERROR;
168 }
169 
GetFreeAudioSourceID(int32_t & sourceId,uint32_t & freeIndex)170 int32_t RecorderImpl::GetFreeAudioSourceID(int32_t &sourceId, uint32_t &freeIndex)
171 {
172     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
173         if (sourceManager_[i].audioSource == nullptr) {
174             sourceId = static_cast<int32_t>(RECORDER_AUDIO_SOURCE_ID_MASK + i);
175             freeIndex = i;
176             return SUCCESS;
177         }
178     }
179     MEDIA_ERR_LOG("get free Audio sourceId failed");
180     return ERROR;
181 }
182 
GetFreeDataSourceID(int32_t & sourceId,uint32_t & freeIndex)183 int32_t RecorderImpl::GetFreeDataSourceID(int32_t &sourceId, uint32_t &freeIndex)
184 {
185     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
186         if (sourceManager_[i].dataSource == nullptr) {
187             sourceId = static_cast<int32_t>(RECORDER_DATA_SOURCE_ID_MASK + i);
188             freeIndex = i;
189             return SUCCESS;
190         }
191     }
192     MEDIA_ERR_LOG("get free Data sourceId failed");
193     return ERROR;
194 }
195 
GetIndexBySourceID(int32_t sourceId,uint32_t & validIndex)196 bool RecorderImpl::GetIndexBySourceID(int32_t sourceId, uint32_t &validIndex)
197 {
198     uint32_t index;
199     if (sourceId >= static_cast<int32_t>(RECORDER_DATA_SOURCE_ID_MASK)) { /* source type is data. */
200         index = static_cast<uint32_t>(sourceId) - RECORDER_DATA_SOURCE_ID_MASK;
201     } else if (sourceId >= static_cast<int32_t>(RECORDER_AUDIO_SOURCE_ID_MASK)) { /* source type is audio. */
202         index = static_cast<uint32_t>(sourceId) - RECORDER_AUDIO_SOURCE_ID_MASK;
203     } else { /* source type is VIDEO. */
204         index = static_cast<uint32_t>(sourceId) - RECORDER_VIDEO_SOURCE_ID_MASK;
205     }
206 
207     if (index >= RECORDER_SOURCE_MAX_CNT) {
208         MEDIA_ERR_LOG("InValidSourceID sourceId:%x", sourceId);
209         return false;
210     }
211 
212     if (sourceManager_[index].videoSource != nullptr) {
213         validIndex = index;
214         return true;
215     } else if (sourceManager_[index].audioSource != nullptr) {
216         validIndex = index;
217         return true;
218     } else if (sourceManager_[index].dataSource != nullptr) {
219         validIndex = index;
220         return true;
221     } else {
222         validIndex = 0xffffffff; /* INVALID_SOURCE_INDEX */
223         MEDIA_ERR_LOG("InValid source type.");
224     }
225 
226     MEDIA_ERR_LOG("IsValidSourceID sourceId:%x", sourceId);
227     return false;
228 }
229 
SetVideoSource(VideoSourceType source,int32_t & sourceId)230 int32_t RecorderImpl::SetVideoSource(VideoSourceType source, int32_t &sourceId)
231 {
232     std::lock_guard<std::mutex> lock(mutex_);
233     if (IsPrepared()) {
234         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
235         return ERR_ILLEGAL_STATE;
236     }
237     if (source < VIDEO_SOURCE_SURFACE_YUV || source >= VIDEO_SOURCE_BUTT) {
238         MEDIA_ERR_LOG("only support VIDEO_SOURCE_SURFACE_ES source: %x is invalid", source);
239         return ERR_INVALID_PARAM;
240     }
241     uint32_t freeIndex;
242     int32_t ret = GetFreeVideoSourceID(sourceId, freeIndex);
243     if (ret != SUCCESS) {
244         MEDIA_ERR_LOG("GetFreeVideoSourceID  failed Ret: %d", ERR_NOFREE_CHANNEL);
245         return ERR_NOFREE_CHANNEL;
246     }
247 
248     sourceManager_[freeIndex].videoSource = new(std::nothrow) RecorderVideoSource();
249     if (sourceManager_[freeIndex].videoSource == nullptr) {
250         ret = ERR_UNKNOWN;
251     }
252     MEDIA_INFO_LOG("Video Source :%d Set SUCCESS sourceId:%x, ret:%d", source, sourceId, ret);
253     return ret;
254 }
255 
SetVideoEncoder(int32_t sourceId,VideoCodecFormat encoder)256 int32_t RecorderImpl::SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder)
257 {
258     std::lock_guard<std::mutex> lock(mutex_);
259     if (IsPrepared()) {
260         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
261         return ERR_ILLEGAL_STATE;
262     }
263     uint32_t validIndex;
264     if (!GetIndexBySourceID(sourceId, validIndex)) {
265         MEDIA_ERR_LOG("input sourceId:%x is invalid", sourceId);
266         return ERR_INVALID_PARAM;
267     }
268     if (encoder != VIDEO_DEFAULT && encoder != H264 && encoder != HEVC) {
269         MEDIA_ERR_LOG("input VideoCodecFormat : %d is invalid", encoder);
270         return ERR_INVALID_PARAM;
271     }
272     sourceManager_[validIndex].videoSourceConfig.videoFormat = encoder;
273     MEDIA_INFO_LOG("Video Encoder:%d Set SUCCESS", encoder);
274     return SUCCESS;
275 }
276 
SetVideoSize(int32_t sourceId,int32_t width,int32_t height)277 int32_t RecorderImpl::SetVideoSize(int32_t sourceId, int32_t width, int32_t height)
278 {
279     std::lock_guard<std::mutex> lock(mutex_);
280     if (IsPrepared()) {
281         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
282         return ERR_ILLEGAL_STATE;
283     }
284     uint32_t validIndex;
285     if (!GetIndexBySourceID(sourceId, validIndex)) {
286         MEDIA_ERR_LOG("input sourceId: %x is invalid", sourceId);
287         return ERR_INVALID_PARAM;
288     }
289     if (width <= 0 || height <= 0) {
290         MEDIA_ERR_LOG("input VideoSize width:%d height:%d is invalid", width, height);
291         return ERR_INVALID_PARAM;
292     }
293     sourceManager_[validIndex].videoSourceConfig.width = width;
294     sourceManager_[validIndex].videoSourceConfig.height = height;
295     int ret = sourceManager_[validIndex].videoSource->SetSurfaceSize(width, height);
296     MEDIA_INFO_LOG("Video Size width:%d height:%d", width, height);
297     return ret;
298 }
SetVideoFrameRate(int32_t sourceId,int32_t frameRate)299 int32_t RecorderImpl::SetVideoFrameRate(int32_t sourceId, int32_t frameRate)
300 {
301     std::lock_guard<std::mutex> lock(mutex_);
302     if (IsPrepared()) {
303         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
304         return ERR_ILLEGAL_STATE;
305     }
306     uint32_t validIndex;
307     if (!GetIndexBySourceID(sourceId, validIndex)) {
308         MEDIA_ERR_LOG("input sourceId : %x is invalid", sourceId);
309         return ERR_INVALID_PARAM;
310     }
311     if (frameRate <= 0) {
312         MEDIA_ERR_LOG("input frameRate %d is invalid", frameRate);
313         return ERR_INVALID_PARAM;
314     }
315     sourceManager_[validIndex].videoSourceConfig.frameRate = frameRate;
316     MEDIA_INFO_LOG("Video frameRate:%d ", frameRate);
317     return SUCCESS;
318 }
319 
SetVideoEncodingBitRate(int32_t sourceId,int32_t rate)320 int32_t RecorderImpl::SetVideoEncodingBitRate(int32_t sourceId, int32_t rate)
321 {
322     std::lock_guard<std::mutex> lock(mutex_);
323     if (IsPrepared()) {
324         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
325         return ERR_ILLEGAL_STATE;
326     }
327     uint32_t validIndex;
328     if (!GetIndexBySourceID(sourceId, validIndex)) {
329         MEDIA_ERR_LOG("input sourceId: %x is invalid", sourceId);
330         return ERR_INVALID_PARAM;
331     }
332     if (rate <= 0) {
333         MEDIA_ERR_LOG("input rate %d is invalid", rate);
334         return ERR_INVALID_PARAM;
335     }
336     sourceManager_[validIndex].videoSourceConfig.bitRate = rate;
337     MEDIA_INFO_LOG("Video Encoding BitRate:%d ", rate);
338     return SUCCESS;
339 }
340 
SetCaptureRate(int32_t sourceId,double fps)341 int32_t RecorderImpl::SetCaptureRate(int32_t sourceId, double fps)
342 {
343     std::lock_guard<std::mutex> lock(mutex_);
344     if (IsPrepared()) {
345         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
346         return ERR_ILLEGAL_STATE;
347     }
348     uint32_t validIndex;
349     if (!GetIndexBySourceID(sourceId, validIndex)) {
350         MEDIA_ERR_LOG("input sourceId: %x is invalid", sourceId);
351         return ERR_INVALID_PARAM;
352     }
353     if (fps <= 0.0) {
354         MEDIA_ERR_LOG("input rate %lf is invalid", fps);
355         return ERR_INVALID_PARAM;
356     }
357     sourceManager_[validIndex].videoSourceConfig.captureRate = fps;
358     MEDIA_INFO_LOG("Video Capture Rate:%lf ", fps);
359     return SUCCESS;
360 }
361 
SetOrientationHint(int32_t sourceId,int32_t degree)362 int32_t RecorderImpl::SetOrientationHint(int32_t sourceId, int32_t degree)
363 {
364     std::lock_guard<std::mutex> lock(mutex_);
365     if (IsPrepared()) {
366         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
367         return ERR_ILLEGAL_STATE;
368     }
369     uint32_t validIndex;
370     if (!GetIndexBySourceID(sourceId, validIndex)) {
371         MEDIA_ERR_LOG("input sourceId: %x is invalid", sourceId);
372         return ERR_INVALID_PARAM;
373     }
374     if (degree <= 0) {
375         MEDIA_ERR_LOG("input rate %d is invalid", degree);
376         return ERR_INVALID_PARAM;
377     }
378     sourceManager_[validIndex].videoSourceConfig.degree = degree;
379     return SUCCESS;
380 }
381 
GetSurface(int32_t sourceId)382 std::shared_ptr<OHOS::Surface> RecorderImpl::GetSurface(int32_t sourceId)
383 {
384     std::lock_guard<std::mutex> lock(mutex_);
385     uint32_t validIndex;
386     if (!GetIndexBySourceID(sourceId, validIndex)) {
387         MEDIA_ERR_LOG("input sourceId: %x is invalid", sourceId);
388         return nullptr;
389     }
390 
391     if (sourceId >= static_cast<int32_t>(RECORDER_DATA_SOURCE_ID_MASK)) { /* source type is data. */
392         return sourceManager_[validIndex].dataSource->GetSurface();
393     } else { /* source type is VIDEO. */
394         return sourceManager_[validIndex].videoSource->GetSurface();
395     }
396 }
397 
IsValidAudioSource(AudioSourceType source)398 bool RecorderImpl::IsValidAudioSource(AudioSourceType source)
399 {
400     if (source <= AUDIO_SOURCE_INVALID || source > AUDIO_VOICE_PERFORMANCE) {
401         MEDIA_ERR_LOG("input AudioSourceType : %d is invalid", source);
402         return false;
403     }
404     return true;
405 }
406 
IsPrepared()407 bool RecorderImpl::IsPrepared()
408 {
409     if (status_ != INITIALIZED) {
410         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
411         return true;
412     }
413     return false;
414 }
415 
SetAudioSource(AudioSourceType source,int32_t & sourceId)416 int32_t RecorderImpl::SetAudioSource(AudioSourceType source, int32_t &sourceId)
417 {
418     std::lock_guard<std::mutex> lock(mutex_);
419     if (IsPrepared()) {
420         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
421         return ERR_ILLEGAL_STATE;
422     }
423     uint32_t freeIndex;
424     int32_t ret = GetFreeAudioSourceID(sourceId, freeIndex);
425     if (ret != SUCCESS) {
426         MEDIA_ERR_LOG("GetFreeAudioSourceID failed Ret: %d", ERR_NOFREE_CHANNEL);
427         return ERR_NOFREE_CHANNEL;
428     }
429     if (!IsValidAudioSource(source)) {
430         return ERR_INVALID_PARAM;
431     }
432     sourceManager_[freeIndex].audioSource = new(std::nothrow) RecorderAudioSource();
433     if (sourceManager_[freeIndex].audioSource == nullptr) {
434         MEDIA_INFO_LOG("new RecorderAudioSource failed, Source :%d sourceId:%x", source, sourceId);
435         return ERR_UNKNOWN;
436     }
437     sourceManager_[freeIndex].audioSourceConfig.inputSource = source;
438 
439     MEDIA_INFO_LOG("Audio Source :%d Set sourceId:%x SUCCESS", source, sourceId);
440     return SUCCESS;
441 }
442 
SetAudioEncoder(int32_t sourceId,AudioCodecFormat encoder)443 int32_t RecorderImpl::SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder)
444 {
445     std::lock_guard<std::mutex> lock(mutex_);
446     if (IsPrepared()) {
447         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
448         return ERR_ILLEGAL_STATE;
449     }
450     uint32_t validIndex;
451     if (!GetIndexBySourceID(sourceId, validIndex)) {
452         MEDIA_ERR_LOG("input sourceId: %x is invalid", sourceId);
453         return ERR_INVALID_PARAM;
454     }
455     if (encoder >= FORMAT_BUTT || encoder <= AUDIO_DEFAULT) {
456         MEDIA_ERR_LOG("input AudioCodecFormat:%d is invalid", encoder);
457         return ERR_INVALID_PARAM;
458     }
459 
460     AudioCodecFormat audioFormat;
461     switch (encoder) {
462         case AAC_LC:
463         case AAC_HE_V1:
464         case AAC_HE_V2:
465         case AAC_LD:
466         case AAC_ELD:
467             audioFormat = encoder;
468             break;
469         case AUDIO_DEFAULT:
470             audioFormat = AAC_LC;
471             break;
472         default:
473             MEDIA_ERR_LOG("input AudioCodecFormat:%d is invalid", encoder);
474             return ERR_INVALID_PARAM;
475     }
476     sourceManager_[validIndex].audioSourceConfig.audioFormat = audioFormat;
477     MEDIA_INFO_LOG("Set audio encoder:%d success", encoder);
478     return SUCCESS;
479 }
480 
SetAudioSampleRate(int32_t sourceId,int32_t rate)481 int32_t RecorderImpl::SetAudioSampleRate(int32_t sourceId, int32_t rate)
482 {
483     std::lock_guard<std::mutex> lock(mutex_);
484     if (IsPrepared()) {
485         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
486         return ERR_ILLEGAL_STATE;
487     }
488     uint32_t validIndex;
489     if (!GetIndexBySourceID(sourceId, validIndex)) {
490         MEDIA_ERR_LOG("input sourceId : %x is invalid", sourceId);
491         return ERR_INVALID_PARAM;
492     }
493     if (rate <= 0) {
494         MEDIA_ERR_LOG("input AudioSampleRate %d is invalid", rate);
495         return ERR_INVALID_PARAM;
496     }
497     sourceManager_[validIndex].audioSourceConfig.sampleRate = rate;
498     MEDIA_INFO_LOG("Audio Sample Rate :%d Set", rate);
499     return SUCCESS;
500 }
501 
SetAudioChannels(int32_t sourceId,int32_t num)502 int32_t RecorderImpl::SetAudioChannels(int32_t sourceId, int32_t num)
503 {
504     std::lock_guard<std::mutex> lock(mutex_);
505     if (IsPrepared()) {
506         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
507         return ERR_ILLEGAL_STATE;
508     }
509     uint32_t validIndex;
510     if (!GetIndexBySourceID(sourceId, validIndex)) {
511         MEDIA_ERR_LOG("input sourceId : %x is invalid", sourceId);
512         return ERR_INVALID_PARAM;
513     }
514     if (num <= 0) {
515         MEDIA_ERR_LOG("input AudioChannels %d is invalid", num);
516         return ERR_INVALID_PARAM;
517     }
518     sourceManager_[validIndex].audioSourceConfig.channelCount = num;
519     MEDIA_INFO_LOG("Audio Channels :%d Set", num);
520     return SUCCESS;
521 }
522 
SetAudioEncodingBitRate(int32_t sourceId,int32_t bitRate)523 int32_t RecorderImpl::SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate)
524 {
525     std::lock_guard<std::mutex> lock(mutex_);
526     if (IsPrepared()) {
527         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
528         return ERR_ILLEGAL_STATE;
529     }
530     uint32_t validIndex;
531     if (!GetIndexBySourceID(sourceId, validIndex)) {
532         MEDIA_ERR_LOG("input sourceId : %x is invalid", sourceId);
533         return ERR_INVALID_PARAM;
534     }
535     if (bitRate <= 0) {
536         MEDIA_ERR_LOG("input AudioEncodingBitRate %d is invalid", bitRate);
537         return ERR_INVALID_PARAM;
538     }
539     sourceManager_[validIndex].audioSourceConfig.bitRate = bitRate;
540     MEDIA_INFO_LOG("Audio Encoding BitRate :%d Set", bitRate);
541     return SUCCESS;
542 }
543 
SetDataSource(DataSourceType source,int32_t & sourceId)544 int32_t RecorderImpl::SetDataSource(DataSourceType source, int32_t &sourceId)
545 {
546     std::lock_guard<std::mutex> lock(mutex_);
547     if (IsPrepared()) {
548         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
549         return ERR_ILLEGAL_STATE;
550     }
551 
552     if (source > METADATA) {
553         MEDIA_ERR_LOG("dataSource source = %d is invalid", source);
554         return ERR_INVALID_PARAM;
555     }
556     uint32_t freeIndex;
557     int32_t ret = GetFreeDataSourceID(sourceId, freeIndex);
558     if (ret != SUCCESS) {
559         MEDIA_ERR_LOG("GetFreeVideoSourceID failed Ret: %d", ERR_NOFREE_CHANNEL);
560         return ERR_NOFREE_CHANNEL;
561     }
562     sourceManager_[freeIndex].dataSource = new(std::nothrow) RecorderDataSource();
563     if (sourceManager_[freeIndex].dataSource == nullptr) {
564         MEDIA_INFO_LOG("new RecorderDataSource failed, Source :%d sourceId:%x", source, sourceId);
565         return ERR_UNKNOWN;
566     }
567     sourceManager_[freeIndex].dataSourceConfig.dataType = (DataType)source;
568 
569     MEDIA_INFO_LOG("Data Source Set SUCCESS sourceId:%x", sourceId);
570     return SUCCESS;
571 }
572 
SetLocation(int32_t latitude,int32_t longitude)573 int32_t RecorderImpl::SetLocation(int32_t latitude, int32_t longitude)
574 {
575     std::lock_guard<std::mutex> lock(mutex_);
576     if (IsPrepared()) {
577         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
578         return ERR_ILLEGAL_STATE;
579     }
580     if (recorderSink_ == nullptr) {
581         MEDIA_ERR_LOG("recorderSink_ is null");
582         return ERR_UNKNOWN;
583     }
584 
585     return recorderSink_->SetLocation(latitude, longitude);
586 }
587 
SetMaxDuration(int32_t duration)588 int32_t RecorderImpl::SetMaxDuration(int32_t duration)
589 {
590     std::lock_guard<std::mutex> lock(mutex_);
591     if (IsPrepared()) {
592         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
593         return ERR_ILLEGAL_STATE;
594     }
595     if (recorderSink_ == nullptr) {
596         MEDIA_ERR_LOG("recorderSink_ is null");
597         return ERR_UNKNOWN;
598     }
599 
600     MEDIA_INFO_LOG("Max Duration :%d Set", duration);
601     return recorderSink_->SetMaxDuration(duration);
602 }
603 
SetOutputFormat(OutputFormatType format)604 int32_t RecorderImpl::SetOutputFormat(OutputFormatType format)
605 {
606     std::lock_guard<std::mutex> lock(mutex_);
607     MEDIA_ERR_LOG("in");
608     if (IsPrepared()) {
609         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
610         return ERR_ILLEGAL_STATE;
611     }
612     MEDIA_INFO_LOG("Output Format :%d Set", format);
613     OutputFormat outPutFormat;
614     switch (format) {
615         case FORMAT_MPEG_4:
616             outPutFormat = OUTPUT_FORMAT_MPEG_4;
617             break;
618         case FORMAT_TS:
619             outPutFormat = OUTPUT_FORMAT_TS;
620             break;
621         case FORMAT_DEFAULT:
622             outPutFormat = OUTPUT_FORMAT_MPEG_4;
623             MEDIA_WARNING_LOG("format: %d use default OUTPUT_FORMAT_MPEG_4", format);
624             break;
625         default:
626             MEDIA_ERR_LOG("invalid OutputFormatType: %d ", format);
627             return ERR_INVALID_PARAM;
628     }
629     if (recorderSink_ == nullptr) {
630         MEDIA_ERR_LOG("recorderSink_ is null");
631         return ERR_UNKNOWN;
632     }
633 
634     MEDIA_INFO_LOG("Output Format:%d Set", format);
635     return recorderSink_->SetOutputFormat(outPutFormat);
636 }
637 
SetOutputPath(const string & path)638 int32_t RecorderImpl::SetOutputPath(const string &path)
639 {
640     std::lock_guard<std::mutex> lock(mutex_);
641     if (recorderSink_ == nullptr) {
642         MEDIA_ERR_LOG("recorderSink_ is null");
643         return ERR_UNKNOWN;
644     }
645 
646     MEDIA_ERR_LOG("in");
647     if (IsPrepared()) {
648         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
649         return ERR_ILLEGAL_STATE;
650     }
651     if (path.data() == nullptr) {
652         MEDIA_ERR_LOG("The path is nullptr");
653         return ERR_INVALID_PARAM;
654     }
655     if (access(path.c_str(), F_OK) == -1) {
656         MEDIA_ERR_LOG("The path :%s doesn't exisit", path.c_str());
657         return ERR_INVALID_PARAM;
658     }
659     struct stat fileStatus;
660     stat(path.c_str(), &fileStatus);
661     if (!S_ISDIR(fileStatus.st_mode)) {
662         MEDIA_ERR_LOG("The path :%s is a file!", path.c_str());
663         return ERR_INVALID_PARAM;
664     }
665     if (access(path.c_str(),  W_OK) == -1) {
666         MEDIA_ERR_LOG("The path :%s No write permission", path.c_str());
667         return ERR_INVALID_PARAM;
668     }
669     recorderSink_->SetOutputPath(path);
670     return SUCCESS;
671 }
672 
IsValidFileFd(int32_t fd)673 int32_t RecorderImpl::IsValidFileFd(int32_t fd)
674 {
675     int flags = fcntl(fd, F_GETFL);
676     if (flags == -1) {
677         MEDIA_ERR_LOG("Fail to get File Status Flags err: %d", errno);
678         return ERR_INVALID_OPERATION;
679     }
680     // fd must be in read-write mode or write-only mode.
681     uint32_t flagsCheck = static_cast<uint32_t>(flags);
682     if ((flagsCheck & (O_RDWR | O_WRONLY)) == 0) {
683         MEDIA_ERR_LOG("File descriptor is not in read-write mode or write-only mode fd:%d flag:%x", fd, flagsCheck);
684         return ERR_INVALID_OPERATION;
685     }
686     return SUCCESS;
687 }
688 
SetOutputFile(int32_t fd)689 int32_t RecorderImpl::SetOutputFile(int32_t fd)
690 {
691     std::lock_guard<std::mutex> lock(mutex_);
692     MEDIA_INFO_LOG("Output File :%d Set", fd);
693     if (status_ != INITIALIZED && status_ != STOPPED) {
694         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
695         return ERR_ILLEGAL_STATE;
696     }
697     if (fd < 0 || IsValidFileFd(fd) != SUCCESS) {
698         MEDIA_ERR_LOG("Fail to get File Status Flags from fd: %d", fd);
699         return ERR_INVALID_PARAM;
700     }
701     if (recorderSink_ == nullptr) {
702         MEDIA_ERR_LOG("recorderSink_ is null");
703         return ERR_UNKNOWN;
704     }
705 
706     return recorderSink_->SetOutputFile(fd);
707 }
708 
SetNextOutputFile(int32_t fd)709 int32_t RecorderImpl::SetNextOutputFile(int32_t fd)
710 {
711     std::lock_guard<std::mutex> lock(mutex_);
712     if (status_ == RELEASED) {
713         MEDIA_ERR_LOG("already RELEASED");
714         return ERR_ILLEGAL_STATE;
715     }
716 
717     MEDIA_INFO_LOG("Next Output File :%d Set", fd);
718     if (fd < 0 || IsValidFileFd(fd) != SUCCESS) {
719         MEDIA_ERR_LOG("Fail to get File Status Flags from fd: %d", fd);
720         return ERR_INVALID_PARAM;
721     }
722     if (recorderSink_ == nullptr) {
723         MEDIA_ERR_LOG("recorderSink_ is null");
724         return ERR_UNKNOWN;
725     }
726 
727     return recorderSink_->SetNextOutputFile(fd);
728 }
729 
SetMaxFileSize(int64_t size)730 int32_t RecorderImpl::SetMaxFileSize(int64_t size)
731 {
732     std::lock_guard<std::mutex> lock(mutex_);
733     if (IsPrepared()) {
734         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
735         return ERR_ILLEGAL_STATE;
736     }
737     MEDIA_INFO_LOG("Max File Size :%lld Set", size);
738     if (recorderSink_ == nullptr) {
739         MEDIA_ERR_LOG("recorderSink_ is null");
740         return ERR_UNKNOWN;
741     }
742 
743     return recorderSink_->SetMaxFileSize(size);
744 }
745 
SetRecorderCallback(const std::shared_ptr<RecorderCallback> & callback)746 int32_t RecorderImpl::SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback)
747 {
748     std::lock_guard<std::mutex> lock(mutex_);
749     if (status_ == RELEASED) {
750         MEDIA_ERR_LOG("illegal status:%u", status_);
751         return ERR_ILLEGAL_STATE;
752     }
753     if (callback == nullptr || callback.get() == nullptr) {
754         MEDIA_ERR_LOG("SetRecorderCallback callback is nullptr");
755         return ERR_INVALID_PARAM;
756     }
757     MEDIA_INFO_LOG("RecorderCallback : Set");
758     if (recorderSink_ == nullptr) {
759         MEDIA_ERR_LOG("recorderSink_ is null");
760         return ERR_UNKNOWN;
761     }
762 
763     return recorderSink_->SetRecorderCallback(callback);
764 }
765 
PrepareAudioSource()766 int32_t RecorderImpl::PrepareAudioSource()
767 {
768     int32_t ret = 0;
769     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
770         if (sourceManager_[i].audioSource != nullptr) {
771             TrackSource trackSource;
772             ret = GetAudioTrackSource(sourceManager_[i].audioSourceConfig, trackSource);
773             if (ret != SUCCESS) {
774                 MEDIA_ERR_LOG("GetAudioTrackSource  failed Ret: 0x%x", ret);
775                 return ret;
776             }
777             ret = sourceManager_[i].audioSource->Init(sourceManager_[i].audioSourceConfig);
778             if (ret != SUCCESS) {
779                 MEDIA_ERR_LOG("audioSource Init failed Ret: 0x%x", ret);
780                 return ret;
781             }
782 
783             if (recorderSink_ == nullptr) {
784                 MEDIA_ERR_LOG("recorderSink_ is null");
785                 return ERR_UNKNOWN;
786             }
787 
788             int32_t trackId;
789             ret = recorderSink_->AddTrackSource(trackSource, trackId);
790             if (ret != SUCCESS) {
791                 MEDIA_ERR_LOG("AddTrackSource failed Ret:0x%x", ret);
792                 return ret;
793             }
794             sourceManager_[i].audioTrackId = trackId;
795             MEDIA_INFO_LOG("recorderSink_ AddTrackSource .audioTrackId :0x%x", sourceManager_[i].audioTrackId);
796         }
797     }
798     MEDIA_INFO_LOG("SUCCESS");
799     return SUCCESS;
800 }
801 
PrepareDataSource()802 int32_t RecorderImpl::PrepareDataSource()
803 {
804     int32_t ret = 0;
805     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
806         if (sourceManager_[i].dataSource != nullptr) {
807             TrackSource trackSource;
808             ret = GetDataTrackSource(trackSource);
809             if (ret != SUCCESS) {
810                 MEDIA_ERR_LOG("GetDataTrackSource failed ret:0x%x", ret);
811                 return ret;
812             }
813 
814             if (recorderSink_ == nullptr) {
815                 MEDIA_ERR_LOG("recorderSink_ is null");
816                 return ERR_UNKNOWN;
817             }
818 
819             int32_t trackId;
820             ret = recorderSink_->AddTrackSource(trackSource, trackId);
821             if (ret != SUCCESS) {
822                 MEDIA_ERR_LOG("AddTrackSource failed Ret: 0x%x", ret);
823                 return ret;
824             }
825             sourceManager_[i].dataTrackId = trackId;
826             MEDIA_INFO_LOG("recorderSink_ AddTrackSource dataTrackId :0x%x", sourceManager_[i].dataTrackId);
827         }
828     }
829     MEDIA_INFO_LOG("SUCCESS");
830     return SUCCESS;
831 }
832 
PrepareVideoSource()833 int32_t RecorderImpl::PrepareVideoSource()
834 {
835     int32_t ret = 0;
836     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
837         if (sourceManager_[i].videoSource != nullptr) {
838             TrackSource trackSource;
839             ret = GetVideoTrackSource(sourceManager_[i].videoSourceConfig, trackSource);
840             if (ret != SUCCESS) {
841                 MEDIA_ERR_LOG("GetVideoTrackSource failed. index:%u ret:0x%x", i, ret);
842                 return ret;
843             }
844 
845             if (recorderSink_ == nullptr) {
846                 MEDIA_ERR_LOG("recorderSink_ is null");
847                 return ERR_UNKNOWN;
848             }
849 
850             int32_t trackId;
851             ret = recorderSink_->AddTrackSource(trackSource, trackId);
852             if (ret != SUCCESS) {
853                 MEDIA_ERR_LOG("AddTrackSource failed ret:0x%x", ret);
854                 return ret;
855             }
856             sourceManager_[i].videoTrackId = trackId;
857             MEDIA_INFO_LOG("recorderSink_ AddTrackSource videoTrackId :0x%x",
858                            sourceManager_[i].videoTrackId);
859         }
860     }
861     MEDIA_INFO_LOG("SUCCESS");
862     return SUCCESS;
863 }
864 
GetVideoTrackSource(const RecorderVideoSourceConfig & videoSourceConfig,TrackSource & trackSource)865 int32_t RecorderImpl::GetVideoTrackSource(const RecorderVideoSourceConfig &videoSourceConfig,
866                                           TrackSource &trackSource)
867 {
868     trackSource.trackSourceType = TRACK_SOURCE_TYPE_VIDEO;
869     switch (videoSourceConfig.videoFormat) {
870         case H264:
871             trackSource.trackSourceInfo.videoInfo.codecType = CODEC_H264;
872             break;
873         case HEVC:
874             trackSource.trackSourceInfo.videoInfo.codecType = CODEC_H265;
875             break;
876         default:
877             MEDIA_ERR_LOG("unsupported videoFormat:%d", videoSourceConfig.videoFormat);
878             return ERR_INVALID_PARAM;
879     }
880     if (videoSourceConfig.width <= 0 || videoSourceConfig.height <= 0 || videoSourceConfig.bitRate <= 0 ||
881         videoSourceConfig.frameRate <= 0) {
882         MEDIA_ERR_LOG("VideoTrackSource not prepared, width:%d, height:%d, bitRate:%d, frameRate:%d",
883                       videoSourceConfig.width, videoSourceConfig.height, videoSourceConfig.bitRate,
884                       videoSourceConfig.frameRate);
885         return ERR_INVALID_PARAM;
886     }
887     trackSource.trackSourceInfo.videoInfo.width = videoSourceConfig.width;
888     trackSource.trackSourceInfo.videoInfo.height = videoSourceConfig.height;
889     trackSource.trackSourceInfo.videoInfo.bitRate = videoSourceConfig.bitRate;
890     trackSource.trackSourceInfo.videoInfo.frameRate = videoSourceConfig.frameRate;
891     trackSource.trackSourceInfo.videoInfo.keyFrameInterval = videoSourceConfig.frameRate;
892     trackSource.trackSourceInfo.videoInfo.speed = videoSourceConfig.speed;
893     return SUCCESS;
894 }
895 
GetAudioTrackSource(const RecorderAudioSourceConfig & audioSourceConfig,TrackSource & trackSource)896 int32_t RecorderImpl::GetAudioTrackSource(const RecorderAudioSourceConfig &audioSourceConfig,
897                                           TrackSource &trackSource)
898 {
899     trackSource.trackSourceType = TRACK_SOURCE_TYPE_AUDIO;
900     switch (audioSourceConfig.audioFormat) {
901         case AAC_LC:
902         case AAC_HE_V1:
903         case AAC_HE_V2:
904         case AAC_LD:
905         case AAC_ELD:
906         case AUDIO_DEFAULT:
907             trackSource.trackSourceInfo.audioInfo.codecType = CODEC_AAC;
908             break;
909         default:
910             MEDIA_ERR_LOG("unsupported audiFormat: %d", audioSourceConfig.audioFormat);
911             return ERR_INVALID_PARAM;
912     }
913     if (audioSourceConfig.bitRate == 0 ||
914         audioSourceConfig.sampleRate == 0 ||
915         audioSourceConfig.channelCount == 0) {
916         MEDIA_ERR_LOG("not set bitRate:%d sampleRate:%d channelCount:%d", audioSourceConfig.bitRate,
917                       audioSourceConfig.sampleRate, audioSourceConfig.channelCount);
918         return ERR_INVALID_PARAM;
919     }
920     trackSource.trackSourceInfo.audioInfo.sampleRate = audioSourceConfig.sampleRate;
921     trackSource.trackSourceInfo.audioInfo.channelCount = audioSourceConfig.channelCount;
922     switch (audioSourceConfig.bitWidth) {
923         case BIT_WIDTH_8:
924             trackSource.trackSourceInfo.audioInfo.sampleBitWidth = FORMAT_AUDIO_SAMPLE_FMT_S8;
925             break;
926         case BIT_WIDTH_16:
927             trackSource.trackSourceInfo.audioInfo.sampleBitWidth = FORMAT_AUDIO_SAMPLE_FMT_S16;
928             break;
929         case BIT_WIDTH_24:
930             trackSource.trackSourceInfo.audioInfo.sampleBitWidth = FORMAT_AUDIO_SAMPLE_FMT_S24;
931             break;
932         default:
933             MEDIA_WARNING_LOG("default sampleBitWidth: %d", audioSourceConfig.bitWidth);
934             trackSource.trackSourceInfo.audioInfo.sampleBitWidth = FORMAT_AUDIO_SAMPLE_FMT_S16;
935             break;
936     }
937     int frame = 1024;
938     trackSource.trackSourceInfo.audioInfo.samplesPerFrame = frame;
939     trackSource.trackSourceInfo.audioInfo.avgBytesPerSec = audioSourceConfig.bitRate;
940     return SUCCESS;
941 }
942 
GetDataTrackSource(TrackSource & trackSource)943 int32_t RecorderImpl::GetDataTrackSource(TrackSource &trackSource)
944 {
945     const int32_t frameRate = 30;
946     const int32_t bitrate = 4 * 1024;
947     trackSource.trackSourceType = TRACK_SOURCE_TYPE_DATA;
948     trackSource.trackSourceInfo.dataInfo.frameRate = frameRate;
949     trackSource.trackSourceInfo.dataInfo.bitRate = bitrate;
950     return SUCCESS;
951 }
952 
PrepareRecorderSink()953 int32_t RecorderImpl::PrepareRecorderSink()
954 {
955     if (IsPrepared()) {
956         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
957         return ERR_ILLEGAL_STATE;
958     }
959     if (recorderSink_ == nullptr) {
960         MEDIA_ERR_LOG("recorderSink_ is null");
961         return ERR_UNKNOWN;
962     }
963 
964     return recorderSink_->Prepare();
965 }
966 
Prepare()967 int32_t RecorderImpl::Prepare()
968 {
969     std::lock_guard<std::mutex> lock(mutex_);
970     if (IsPrepared()) {
971         MEDIA_ERR_LOG("IsPrepared status:%u", status_);
972         return ERR_ILLEGAL_STATE;
973     }
974 
975     int32_t ret = PrepareRecorderSink();
976     if (ret != SUCCESS) {
977         MEDIA_ERR_LOG("PrepareRecorderSink  failed Ret: %d", ret);
978         return ret;
979     }
980     ret = PrepareVideoSource();
981     if (ret != SUCCESS) {
982         MEDIA_ERR_LOG("PrepareVideoSource  failed Ret: %d", ret);
983         return ret;
984     }
985     ret = PrepareAudioSource();
986     if (ret != SUCCESS) {
987         MEDIA_ERR_LOG("PrepareAudioSource  failed Ret: %d", ret);
988         return ret;
989     }
990 
991     ret = PrepareDataSource();
992     if (ret != SUCCESS) {
993         MEDIA_ERR_LOG("PrepareDataSource  failed Ret: %d", ret);
994         return ret;
995     }
996     status_ = PREPARED;
997     MEDIA_INFO_LOG("Prepare SUCCESS");
998     return SUCCESS;
999 }
1000 
DataSourceProcess(const SourceManager * dataSourceManager,const RecorderSink * recorderSink)1001 void DataSourceProcess(const SourceManager *dataSourceManager, const RecorderSink *recorderSink)
1002 {
1003     MEDIA_INFO_LOG("in");
1004     if (dataSourceManager == nullptr || recorderSink == nullptr) {
1005         MEDIA_INFO_LOG("dataSourceManager or recorderSink null");
1006         return;
1007     }
1008     if (dataSourceManager->dataSource == nullptr) {
1009         MEDIA_ERR_LOG("dataSource is nullptr");
1010         return;
1011     }
1012     prctl(PR_SET_NAME, "DataSourceProcess", 0, 0, 0);
1013     struct sched_param param = {};
1014     pthread_attr_t attr;
1015     pthread_attr_getschedparam(&attr, &param);
1016     param.sched_priority = RECORDER_AUDIO_THREAD_PRIORITY;
1017     pthread_setschedparam(pthread_self(), SCHED_RR, &param);
1018     MEDIA_INFO_LOG("sched_priority:%d", param.sched_priority);
1019     while (dataSourceManager->dataSourceStarted) {
1020         RecorderSourceBuffer buffer;
1021         int32_t ret = dataSourceManager->dataSource->AcquireBuffer(buffer, true);
1022         if (ret != SUCCESS) {
1023             MEDIA_ERR_LOG("Read failed ret:0x%x", ret);
1024             continue;
1025         }
1026         if (!dataSourceManager->dataSourcePaused) {
1027             FormatFrame frameData;
1028             frameData.frameType = FRAME_TYPE_DATA;
1029             frameData.trackId = dataSourceManager->dataTrackId;
1030             frameData.isKeyFrame = false;
1031             frameData.timestampUs = buffer.timeStamp;
1032             frameData.data = buffer.dataAddr;
1033             frameData.len = buffer.dataLen;
1034             ret = recorderSink->WriteData(dataSourceManager->dataTrackId, frameData);
1035             if (ret != SUCCESS) {
1036                 MEDIA_ERR_LOG("WriteData failed 0x%x", ret);
1037             }
1038         }
1039         dataSourceManager->dataSource->ReleaseBuffer(buffer);
1040     }
1041     MEDIA_DEBUG_LOG("over");
1042 }
1043 
StartDataSource()1044 int32_t RecorderImpl::StartDataSource()
1045 {
1046     int32_t ret = 0;
1047     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1048         if (sourceManager_[i].dataSource != nullptr) {
1049             ret = sourceManager_[i].dataSource->Start();
1050             if (ret != SUCCESS) {
1051                 MEDIA_ERR_LOG("DataSource Start failed ret:0x%x", ret);
1052                 return ret;
1053             }
1054             sourceManager_[i].dataSourceStarted = true;
1055             sourceManager_[i].dataProcessThread = std::thread(DataSourceProcess, &sourceManager_[i], recorderSink_);
1056         }
1057     }
1058     MEDIA_INFO_LOG("SUCCESS");
1059     return SUCCESS;
1060 }
1061 
StopDataSource()1062 int32_t RecorderImpl::StopDataSource()
1063 {
1064     int32_t ret = 0;
1065     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1066         if (sourceManager_[i].dataSource != nullptr) {
1067             MEDIA_DEBUG_LOG("dataSource->Stop");
1068             sourceManager_[i].dataSourceStarted = false;
1069             ret = sourceManager_[i].dataSource->Stop();
1070             MEDIA_DEBUG_LOG("dataSource->Stop out");
1071             if (ret != SUCCESS) {
1072                 MEDIA_ERR_LOG("dataSource Stop failed Ret: 0x%x", ret);
1073                 return ret;
1074             }
1075             MEDIA_DEBUG_LOG("dataProcessThread.join");
1076             sourceManager_[i].dataProcessThread.join();
1077             MEDIA_DEBUG_LOG("dataProcessThread.join out");
1078         }
1079     }
1080     MEDIA_DEBUG_LOG("SUCCESS");
1081     return SUCCESS;
1082 }
1083 
AudioSourceProcess(const SourceManager * audioSourceManager,const RecorderSink * recorderSink)1084 void AudioSourceProcess(const SourceManager *audioSourceManager, const RecorderSink *recorderSink)
1085 {
1086     MEDIA_INFO_LOG("audioSourceManager");
1087     if (audioSourceManager == nullptr || recorderSink == nullptr) {
1088         MEDIA_ERR_LOG("audioSourceManager or recorderSink null");
1089         return;
1090     }
1091     if (audioSourceManager->audioSource == nullptr) {
1092         MEDIA_ERR_LOG("audioSource is nullptr");
1093         return;
1094     }
1095     prctl(PR_SET_NAME, "AudioSourceProcess", 0, 0, 0);
1096     struct sched_param param = {};
1097     pthread_attr_t attr;
1098     pthread_attr_getschedparam(&attr, &param);
1099     param.sched_priority = RECORDER_AUDIO_THREAD_PRIORITY;
1100     pthread_setschedparam(pthread_self(), SCHED_RR, &param);
1101     MEDIA_INFO_LOG("sched_priority:%d", param.sched_priority);
1102     while (audioSourceManager->audioSourceStarted) {
1103         RecorderSourceBuffer buffer;
1104         int32_t ret = audioSourceManager->audioSource->AcquireBuffer(buffer, false);
1105         if (ret != SUCCESS) {
1106             MEDIA_ERR_LOG("audioSource Read failed ret:0x%x", ret);
1107             continue;
1108         }
1109         if (!audioSourceManager->audioSourcePaused) {
1110             FormatFrame frameData;
1111             frameData.frameType = FRAME_TYPE_AUDIO;
1112             frameData.trackId = audioSourceManager->audioTrackId;
1113             frameData.isKeyFrame = false;
1114             frameData.timestampUs = buffer.timeStamp;
1115             frameData.data = buffer.dataAddr;
1116             frameData.len = buffer.dataLen;
1117             ret = recorderSink->WriteData(audioSourceManager->audioTrackId, frameData);
1118             if (ret != SUCCESS) {
1119                 MEDIA_ERR_LOG("Audio WriteData failed 0x%x", ret);
1120             }
1121         }
1122         audioSourceManager->audioSource->ReleaseBuffer(buffer);
1123     }
1124     MEDIA_INFO_LOG("audioSourceManager:  over");
1125 }
1126 
StartAudioSource()1127 int32_t RecorderImpl::StartAudioSource()
1128 {
1129     int32_t ret = 0;
1130     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1131         if (sourceManager_[i].audioSource != nullptr) {
1132             ret = sourceManager_[i].audioSource->Start();
1133             if (ret != SUCCESS) {
1134                 MEDIA_ERR_LOG("audioSource Start  failed Ret: 0x%x", ret);
1135                 return ret;
1136             }
1137             sourceManager_[i].audioSourceStarted = true;
1138             sourceManager_[i].audioProcessThread = std::thread(AudioSourceProcess, &sourceManager_[i], recorderSink_);
1139         }
1140     }
1141     MEDIA_INFO_LOG("Start Audio Source SUCCESS");
1142     return SUCCESS;
1143 }
1144 
StopAudioSource()1145 int32_t RecorderImpl::StopAudioSource()
1146 {
1147     int32_t ret = 0;
1148     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1149         if (sourceManager_[i].audioSource != nullptr) {
1150             MEDIA_DEBUG_LOG("audioSource->Stop");
1151             sourceManager_[i].audioSourceStarted = false;
1152             ret = sourceManager_[i].audioSource->Stop();
1153             MEDIA_DEBUG_LOG("audioSource->Stop out");
1154             if (ret != SUCCESS) {
1155                 MEDIA_ERR_LOG("audioSource Stop  failed Ret: 0x%x", ret);
1156                 return ret;
1157             }
1158             MEDIA_DEBUG_LOG("audioProcessThread.join");
1159             sourceManager_[i].audioProcessThread.join();
1160             MEDIA_DEBUG_LOG("audioProcessThread.join out");
1161         }
1162     }
1163     MEDIA_INFO_LOG("Stop Audio Source SUCCESS");
1164     return SUCCESS;
1165 }
1166 
VideoSourceProcess(const SourceManager * videoSourceManager,const RecorderSink * recorderSink)1167 void VideoSourceProcess(const SourceManager *videoSourceManager, const RecorderSink *recorderSink)
1168 {
1169     MEDIA_INFO_LOG("videoSourceManager");
1170     if (videoSourceManager == nullptr) {
1171         MEDIA_ERR_LOG("videoSourceManager null");
1172         return;
1173     }
1174     if (videoSourceManager->videoSource == nullptr || recorderSink == nullptr) {
1175         MEDIA_ERR_LOG("videoSource or recorderSink is nullptr");
1176         return;
1177     }
1178     prctl(PR_SET_NAME, "VideoSourceProcess", 0, 0, 0);
1179     struct sched_param param = {};
1180     pthread_attr_t attr;
1181     pthread_attr_getschedparam(&attr, &param);
1182     param.sched_priority = RECORDER_VIDEO_THREAD_PRIORITY;
1183     pthread_setschedparam(pthread_self(), SCHED_RR, &param);
1184     MEDIA_INFO_LOG("sched_priority:%d", param.sched_priority);
1185     while (videoSourceManager->videoSourceStarted) {
1186         RecorderSourceBuffer buffer;
1187         int32_t ret = videoSourceManager->videoSource->AcquireBuffer(buffer, true);
1188         if (ret != SUCCESS) {
1189             MEDIA_ERR_LOG("videoSource AcquireBuffer failed ret:0x%x", ret);
1190             continue;
1191         }
1192         if (!videoSourceManager->videoSourcePaused) {
1193             FormatFrame frameData;
1194             frameData.frameType = FRAME_TYPE_VIDEO;
1195             frameData.trackId = videoSourceManager->videoTrackId;
1196             frameData.isKeyFrame = buffer.keyFrameFlag;
1197             frameData.timestampUs = buffer.timeStamp;
1198             frameData.data = buffer.dataAddr;
1199             frameData.len = buffer.dataLen;
1200             ret = recorderSink->WriteData(videoSourceManager->videoTrackId, frameData);
1201             if (ret != SUCCESS) {
1202                 MEDIA_ERR_LOG("video WriteData failed 0x%x", ret);
1203             }
1204         }
1205         videoSourceManager->videoSource->ReleaseBuffer(buffer);
1206     }
1207     MEDIA_INFO_LOG("videoSourceManager: over");
1208 }
1209 
StartVideoSource()1210 int32_t RecorderImpl::StartVideoSource()
1211 {
1212     int32_t ret = 0;
1213     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1214         if (sourceManager_[i].videoSource != nullptr) {
1215             ret = sourceManager_[i].videoSource->Start();
1216             if (ret != SUCCESS) {
1217                 MEDIA_ERR_LOG("videoSource->Start  failed Ret: 0x%x", ret);
1218                 return ret;
1219             }
1220             sourceManager_[i].videoSourceStarted = true;
1221             sourceManager_[i].videoProcessThread = std::thread(VideoSourceProcess, &sourceManager_[i], recorderSink_);
1222         }
1223     }
1224     MEDIA_INFO_LOG("SUCCESS");
1225     return SUCCESS;
1226 }
1227 
StopVideoSource()1228 int32_t RecorderImpl::StopVideoSource()
1229 {
1230     int32_t ret = 0;
1231     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1232         if (sourceManager_[i].videoSource != nullptr) {
1233             sourceManager_[i].videoSourceStarted = false;
1234             ret = sourceManager_[i].videoSource->Stop();
1235             if (ret != SUCCESS) {
1236                 MEDIA_ERR_LOG("videoSource->Start  failed Ret: 0x%x", ret);
1237                 return ret;
1238             }
1239             MEDIA_DEBUG_LOG("videoProcessThread.join");
1240             sourceManager_[i].videoProcessThread.join();
1241             MEDIA_DEBUG_LOG("videoProcessThread.join over");
1242         }
1243     }
1244     MEDIA_INFO_LOG("SUCCESS");
1245     return SUCCESS;
1246 }
1247 
Start()1248 int32_t RecorderImpl::Start()
1249 {
1250     std::lock_guard<std::mutex> lock(mutex_);
1251     int64_t startTime;
1252     int64_t startVideoTime;
1253     int64_t startAudioAndDataTime;
1254     struct timeval begin = { 0, 0 };
1255     struct timeval end = { 0, 0 };
1256 
1257     gettimeofday(&begin, nullptr);
1258     if (status_ != PREPARED && status_ != PAUSED && status_ != STOPPED) {
1259         MEDIA_ERR_LOG("Start ILLEGAL_STATE  status:%u", status_);
1260         return ERR_ILLEGAL_STATE;
1261     }
1262 
1263     if (recorderSink_ == nullptr) {
1264         MEDIA_ERR_LOG("recorderSink_ is null");
1265         return ERR_UNKNOWN;
1266     }
1267     int32_t ret = recorderSink_->Start();
1268     if (ret != SUCCESS) {
1269         MEDIA_ERR_LOG("recorderSink_  Start failed Ret: %d", ret);
1270         return ret;
1271     }
1272     gettimeofday(&end, nullptr);
1273     startTime = CalcDiffTimeMs(begin, end);
1274 
1275     gettimeofday(&begin, nullptr);
1276     ret = StartVideoSource();
1277     if (ret != SUCCESS) {
1278         MEDIA_ERR_LOG("StartVideoSource  Start failed Ret: %d", ret);
1279         return ret;
1280     }
1281     gettimeofday(&end, nullptr);
1282     startVideoTime = CalcDiffTimeMs(begin, end);
1283 
1284     gettimeofday(&begin, nullptr);
1285     ret = StartAudioSource();
1286     if (ret != SUCCESS) {
1287         MEDIA_ERR_LOG("StartAudioSource  Start failed Ret: %d", ret);
1288         return ret;
1289     }
1290     ret = StartDataSource();
1291     if (ret != SUCCESS) {
1292         MEDIA_ERR_LOG("StartDataSource failed:%d", ret);
1293         return ret;
1294     }
1295     gettimeofday(&end, nullptr);
1296     startAudioAndDataTime = CalcDiffTimeMs(begin, end);
1297     MEDIA_INFO_LOG("SUCCESS. cost: startTime:%lld, startVideoTime:%lld, startAudioAndDataTime:%lld", startTime,
1298         startVideoTime, startAudioAndDataTime);
1299     status_ = RECORDING;
1300     return SUCCESS;
1301 }
1302 
PauseAudioSource()1303 int32_t RecorderImpl::PauseAudioSource()
1304 {
1305     int32_t ret = 0;
1306     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1307         if (sourceManager_[i].audioSource != nullptr) {
1308             ret = sourceManager_[i].audioSource->Pause();
1309             if (ret != SUCCESS) {
1310                 MEDIA_ERR_LOG("audioSource Pause  failed Ret: 0x%x", ret);
1311                 return ret;
1312             }
1313             sourceManager_[i].audioSourcePaused = true;
1314         }
1315     }
1316     return SUCCESS;
1317 }
1318 
PauseVideoSource()1319 int32_t RecorderImpl::PauseVideoSource()
1320 {
1321     int32_t ret = 0;
1322     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1323         if (sourceManager_[i].videoSource != nullptr) {
1324             ret = sourceManager_[i].videoSource->Pause();
1325             if (ret != SUCCESS) {
1326                 MEDIA_ERR_LOG("videoSource->Pause  failed Ret: 0x%x", ret);
1327                 return ret;
1328             }
1329             sourceManager_[i].videoSourcePaused = true;
1330         }
1331     }
1332     return SUCCESS;
1333 }
1334 
PauseDataSource()1335 int32_t RecorderImpl::PauseDataSource()
1336 {
1337     int32_t ret = 0;
1338     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1339         if (sourceManager_[i].dataSource != nullptr) {
1340             ret = sourceManager_[i].dataSource->Pause();
1341             if (ret != SUCCESS) {
1342                 MEDIA_ERR_LOG("dataSource->Pause failed: 0x%x", ret);
1343                 return ret;
1344             }
1345             sourceManager_[i].dataSourcePaused = true;
1346         }
1347     }
1348     return SUCCESS;
1349 }
1350 
ResumeAudioSource()1351 int32_t RecorderImpl::ResumeAudioSource()
1352 {
1353     int32_t ret = 0;
1354     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1355         if (sourceManager_[i].audioSource != nullptr) {
1356             ret = sourceManager_[i].audioSource->Resume();
1357             if (ret != SUCCESS) {
1358                 MEDIA_ERR_LOG("audioSource Resume failed ret:0x%x", ret);
1359                 return ret;
1360             }
1361             sourceManager_[i].audioSourcePaused = false;
1362         }
1363     }
1364     return SUCCESS;
1365 }
1366 
ResumeVideoSource()1367 int32_t RecorderImpl::ResumeVideoSource()
1368 {
1369     int32_t ret = 0;
1370     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1371         if (sourceManager_[i].videoSource != nullptr) {
1372             ret = sourceManager_[i].videoSource->Resume();
1373             if (ret != SUCCESS) {
1374                 MEDIA_ERR_LOG("videoSource->Resume failed ret:0x%x", ret);
1375                 return ret;
1376             }
1377             sourceManager_[i].videoSourcePaused = false;
1378         }
1379     }
1380     return SUCCESS;
1381 }
1382 
ResumeDataSource()1383 int32_t RecorderImpl::ResumeDataSource()
1384 {
1385     int32_t ret = 0;
1386     for (uint32_t i = 0; i < RECORDER_SOURCE_MAX_CNT; i++) {
1387         if (sourceManager_[i].dataSource != nullptr) {
1388             ret = sourceManager_[i].dataSource->Resume();
1389             if (ret != SUCCESS) {
1390                 MEDIA_ERR_LOG("dataSource->Resume failed ret:0x%x", ret);
1391                 return ret;
1392             }
1393             sourceManager_[i].dataSourcePaused = false;
1394         }
1395     }
1396     return SUCCESS;
1397 }
1398 
Pause()1399 int32_t RecorderImpl::Pause()
1400 {
1401     std::lock_guard<std::mutex> lock(mutex_);
1402     if (status_ != RECORDING) {
1403         MEDIA_ERR_LOG("Pause ILLEGAL_STATE status:%u", status_);
1404         return ERR_ILLEGAL_STATE;
1405     }
1406     int32_t ret = PauseVideoSource();
1407     if (ret != SUCCESS) {
1408         MEDIA_ERR_LOG("StopVideoSource Pause failed ret:%d", ret);
1409         return ret;
1410     }
1411     ret = PauseAudioSource();
1412     if (ret != SUCCESS) {
1413         MEDIA_ERR_LOG("StopAudioSource Pause failed ret:%d", ret);
1414         return ret;
1415     }
1416     ret = PauseDataSource();
1417     if (ret != SUCCESS) {
1418         MEDIA_ERR_LOG("StopDataSource Pause failed ret:%d", ret);
1419         return ret;
1420     }
1421 
1422     if (recorderSink_ == nullptr) {
1423         MEDIA_ERR_LOG("recorderSink_ is null");
1424         return ERR_UNKNOWN;
1425     }
1426     ret = recorderSink_->Stop(false);
1427     if (ret != SUCCESS) {
1428         MEDIA_ERR_LOG("recorderSink_ Stop! ret:0x%x", ret);
1429         return ret;
1430     }
1431     status_ = PAUSED;
1432     MEDIA_INFO_LOG("Pause Recorder SUCCESS");
1433     return SUCCESS;
1434 }
1435 
Resume()1436 int32_t RecorderImpl::Resume()
1437 {
1438     std::lock_guard<std::mutex> lock(mutex_);
1439     if (status_ != PAUSED) {
1440         MEDIA_ERR_LOG("Resume ILLEGAL_STATE status:%u", status_);
1441         return ERR_ILLEGAL_STATE;
1442     }
1443     if (recorderSink_ == nullptr) {
1444         MEDIA_ERR_LOG("recorderSink_ is null");
1445         return ERR_UNKNOWN;
1446     }
1447 
1448     int32_t ret = recorderSink_->Start();
1449     if (ret != SUCCESS) {
1450         MEDIA_ERR_LOG("recorderSink_ Start! ret: 0x%x", ret);
1451         return ret;
1452     }
1453     ret = ResumeVideoSource();
1454     if (ret != SUCCESS) {
1455         MEDIA_ERR_LOG("VideoSource Pause failed ret:%d", ret);
1456         return ret;
1457     }
1458     ret = ResumeAudioSource();
1459     if (ret != SUCCESS) {
1460         MEDIA_ERR_LOG("pAudioSource Resume failed ret:%d", ret);
1461         return ret;
1462     }
1463     ret = ResumeDataSource();
1464     if (ret != SUCCESS) {
1465         MEDIA_ERR_LOG("pAudioSource Resume failed Ret: %d", ret);
1466         return ret;
1467     }
1468     status_ = RECORDING;
1469     MEDIA_INFO_LOG("Resume Recorder SUCCESS");
1470     return SUCCESS;
1471 }
1472 
StopInternal(bool block)1473 int32_t RecorderImpl::StopInternal(bool block)
1474 {
1475     MEDIA_DEBUG_LOG("StopInternal");
1476 
1477     int64_t time;
1478     struct timeval begin = { 0, 0 };
1479     struct timeval end = { 0, 0 };
1480 
1481     gettimeofday(&begin, nullptr);
1482 
1483     int32_t ret = StopVideoSource();
1484     MEDIA_DEBUG_LOG("StopVideoSource");
1485     if (ret != SUCCESS) {
1486         MEDIA_ERR_LOG("StopVideoSource  Start failed ret:%d", ret);
1487         return ret;
1488     }
1489     gettimeofday(&end, nullptr);
1490     time = CalcDiffTimeMs(begin, end);
1491     MEDIA_INFO_LOG("StopVideoSource cost:%lld", time);
1492     gettimeofday(&begin, nullptr);
1493     MEDIA_DEBUG_LOG("StopAudioSource");
1494     ret = StopAudioSource();
1495     if (ret != SUCCESS) {
1496         MEDIA_ERR_LOG("StopAudioSource Start failed ret:%d", ret);
1497         return ret;
1498     }
1499     gettimeofday(&end, nullptr);
1500     time = CalcDiffTimeMs(begin, end);
1501     MEDIA_INFO_LOG("StopAudioSource cost:%lld", time);
1502 
1503     ret = StopDataSource();
1504     if (ret != SUCCESS) {
1505         MEDIA_ERR_LOG("StopDataSource failed ret:%d", ret);
1506         return ret;
1507     }
1508 
1509     if (recorderSink_ == nullptr) {
1510         MEDIA_ERR_LOG("recorderSink_ is null");
1511         return ERR_UNKNOWN;
1512     }
1513     ret = recorderSink_->Stop(block);
1514     if (ret != SUCCESS) {
1515         MEDIA_ERR_LOG("recorderSink_ Stop! ret:0x%x", ret);
1516         return ret;
1517     }
1518     gettimeofday(&end, nullptr);
1519     time = CalcDiffTimeMs(begin, end);
1520     MEDIA_INFO_LOG("recorderSink Stop cost:%lld", time);
1521     status_ = STOPPED;
1522     MEDIA_INFO_LOG("Stop Recorder SUCCESS");
1523     return SUCCESS;
1524 }
1525 
Stop(bool block)1526 int32_t RecorderImpl::Stop(bool block)
1527 {
1528     std::lock_guard<std::mutex> lock(mutex_);
1529     if (status_ != RECORDING &&
1530         status_ != PAUSED) {
1531         MEDIA_ERR_LOG("Stop ILLEGAL_STATE status:%u", status_);
1532         return ERR_ILLEGAL_STATE;
1533     }
1534     return StopInternal(block);
1535 }
1536 
Reset()1537 int32_t RecorderImpl::Reset()
1538 {
1539     std::lock_guard<std::mutex> lock(mutex_);
1540     int32_t ret;
1541     if (status_ == RELEASED) {
1542         MEDIA_ERR_LOG("already RELEASED");
1543         return ERR_ILLEGAL_STATE;
1544     }
1545     if (status_ == RECORDING ||
1546         status_ == PAUSED) {
1547         if ((ret = StopInternal(false)) != SUCCESS) {
1548             MEDIA_ERR_LOG("Reset StopInternal err");
1549             return ret;
1550         }
1551     }
1552 
1553     if (recorderSink_ == nullptr) {
1554         MEDIA_ERR_LOG("recorderSink_ is null");
1555         return ERR_UNKNOWN;
1556     }
1557     ret = recorderSink_->Reset();
1558     if (ret != SUCCESS) {
1559         MEDIA_ERR_LOG("recorderSink Reset err:0x%x", ret);
1560         return ret;
1561     }
1562     ret = ResetConfig();
1563     if (ret != SUCCESS) {
1564         MEDIA_ERR_LOG("ResetConfig err:0x%x", ret);
1565         return ret;
1566     }
1567     status_ = INITIALIZED;
1568     MEDIA_INFO_LOG("Reset Recorder SUCCESS");
1569     return SUCCESS;
1570 }
1571 
Release()1572 int32_t RecorderImpl::Release()
1573 {
1574     std::lock_guard<std::mutex> lock(mutex_);
1575     int32_t ret;
1576     if (status_ == RELEASED) {
1577         MEDIA_ERR_LOG("already Released");
1578         return ERR_ILLEGAL_STATE;
1579     }
1580     if (status_ == RECORDING ||
1581         status_ == PAUSED) {
1582         if ((ret = StopInternal(false)) != SUCCESS) {
1583             MEDIA_ERR_LOG("Release StopInternal err");
1584             return ret;
1585         }
1586     }
1587 
1588     if (recorderSink_ == nullptr) {
1589         MEDIA_ERR_LOG("recorderSink_ is null");
1590         return ERR_UNKNOWN;
1591     }
1592     ret = recorderSink_->Release();
1593     if (ret != SUCCESS) {
1594         MEDIA_ERR_LOG("recorderSink_ Release failed:%d", ret);
1595         return ret;
1596     }
1597     status_ = RELEASED;
1598     MEDIA_INFO_LOG("Recorder Released");
1599     return SUCCESS;
1600 }
1601 
SetFileSplitDuration(FileSplitType type,int64_t timestamp,uint32_t duration)1602 int32_t RecorderImpl::SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration)
1603 {
1604     std::lock_guard<std::mutex> lock(mutex_);
1605     if (status_ != RECORDING) {
1606         MEDIA_ERR_LOG("ILLEGAL_STATE status:%u", status_);
1607         return ERR_ILLEGAL_STATE;
1608     }
1609     MEDIA_INFO_LOG("Set File Split Duration type:%d", type);
1610     ManualSplitType manualSplitType;
1611     switch (type) {
1612         case FILE_SPLIT_POST:
1613             manualSplitType = MANUAL_SPLIT_POST;
1614             break;
1615         case FILE_SPLIT_PRE:
1616             manualSplitType = MANUAL_SPLIT_PRE;
1617             break;
1618         case FILE_SPLIT_NORMAL:
1619             manualSplitType = MANUAL_SPLIT_NORMAL;
1620             break;
1621         default:
1622             MEDIA_ERR_LOG("unsupported FileSplitType type: %d", type);
1623             return ERR_INVALID_PARAM;
1624     }
1625     if (recorderSink_ == nullptr) {
1626         MEDIA_ERR_LOG("recorderSink_ is null");
1627         return ERR_UNKNOWN;
1628     }
1629 
1630     return recorderSink_->SetFileSplitDuration(manualSplitType, timestamp, duration);
1631 }
1632 
SetParameter(int32_t sourceId,const Format & format)1633 int32_t RecorderImpl::SetParameter(int32_t sourceId, const Format &format)
1634 {
1635     std::lock_guard<std::mutex> lock(mutex_);
1636     if (status_ == RELEASED) {
1637         MEDIA_ERR_LOG("when RELEASED can not Set Parameter");
1638         return ERR_ILLEGAL_STATE;
1639     }
1640     if (recorderSink_ == nullptr) {
1641         MEDIA_ERR_LOG("recorderSink_ is null");
1642         return ERR_UNKNOWN;
1643     }
1644 
1645     float value;
1646     if (format.GetFloatValue(RECORDER_RECORD_SPEED, value)) {
1647         MEDIA_INFO_LOG("SetParameter RCORDER_RECORD_SPEED value = %f\n", value);
1648         sourceManager_[sourceId].videoSourceConfig.speed = value;
1649         return SUCCESS;
1650     }
1651     return recorderSink_->SetParameter(sourceId, format);
1652 }
1653 }  // namespace Media
1654 }  // namespace OHOS