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 #ifndef RECORDER_IMPL_H
17 #define RECORDER_IMPL_H
18 
19 #include <vector>
20 #include "recorder.h"
21 #include "recorder_audio_source.h"
22 #include "recorder_data_source.h"
23 #include "recorder_sink.h"
24 #include "recorder_video_source.h"
25 
26 namespace OHOS {
27 namespace Media {
28 constexpr uint32_t RECORDER_SOURCE_MAX_CNT = 4;
29 
30 enum RecState {INITIALIZED = 0, PREPARED, RECORDING, PAUSED, STOPPED, RELEASED};
31 
32 struct SourceManager {
33     RecorderVideoSource *videoSource;
34     bool videoSourceStarted;
35     bool videoSourcePaused;
36     int32_t videoTrackId;
37     std::thread videoProcessThread;
38     RecorderAudioSource *audioSource;
39     bool audioSourceStarted;
40     bool audioSourcePaused;
41     int32_t audioTrackId;
42     std::thread audioProcessThread;
43     RecorderDataSource *dataSource;
44     bool dataSourceStarted;
45     bool dataSourcePaused;
46     int32_t dataTrackId;
47     std::thread dataProcessThread;
48     RecorderVideoSourceConfig videoSourceConfig;
49     RecorderAudioSourceConfig audioSourceConfig;
50     RecorderDataSourceConfig dataSourceConfig;
51 };
52 
53 class RecorderImpl {
54 public:
55     RecorderImpl();
56     virtual ~RecorderImpl();
57     int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId);
58     int32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder);
59     int32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height);
60     int32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate);
61     int32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate);
62     int32_t SetCaptureRate(int32_t sourceId, double fps);
63     int32_t SetOrientationHint(int32_t sourceId, int32_t degree);
64     std::shared_ptr<OHOS::Surface> GetSurface(int32_t sourceId);
65 
66     int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId);
67     int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder);
68     int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate);
69     int32_t SetAudioChannels(int32_t sourceId, int32_t num);
70     int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate);
71 
72     int32_t SetDataSource(DataSourceType source, int32_t &sourceId);
73 
74     int32_t SetLocation(int32_t latitude, int32_t longitude);
75     int32_t SetMaxDuration(int32_t duration);
76     int32_t SetOutputFormat(OutputFormatType format);
77     int32_t SetOutputPath(const string &path);
78     int32_t SetOutputFile(int32_t fd);
79     int32_t SetNextOutputFile(int32_t fd);
80     int32_t SetMaxFileSize(int64_t size);
81 
82     int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback);
83     int32_t Prepare();
84     int32_t Start();
85     int32_t Pause();
86     int32_t Resume();
87     int32_t Stop(bool block);
88     int32_t Reset();
89     int32_t Release();
90     int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration);
91     int32_t SetParameter(int32_t sourceId, const Format &format);
92 
93 private:
94     int32_t ResetConfig();
95     int32_t GetFreeVideoSourceID(int32_t &sourceId, uint32_t &freeIndex);
96     int32_t GetFreeAudioSourceID(int32_t &sourceId, uint32_t &freeIndex);
97     int32_t GetFreeDataSourceID(int32_t &sourceId, uint32_t &freeIndex);
98     bool GetIndexBySourceID(int32_t sourceId, uint32_t &validIndex);
99     bool IsValidAudioSource(AudioSourceType source);
100     bool IsPrepared();
101     int32_t IsValidFileFd(int32_t fd);
102     int32_t GetVideoTrackSource(const RecorderVideoSourceConfig &videoSourceConfig, TrackSource &trackSource);
103     int32_t GetAudioTrackSource(const RecorderAudioSourceConfig &audioSourceConfig, TrackSource &trackSource);
104     int32_t GetDataTrackSource(TrackSource &trackSource);
105     int32_t PrepareAudioSource();
106     int32_t PrepareVideoSource();
107     int32_t PrepareDataSource();
108     int32_t StartAudioSource();
109     int32_t StartVideoSource();
110     int32_t StartDataSource();
111     int32_t PauseAudioSource();
112     int32_t PauseVideoSource();
113     int32_t PauseDataSource();
114     int32_t ResumeAudioSource();
115     int32_t ResumeVideoSource();
116     int32_t ResumeDataSource();
117     int32_t StopAudioSource();
118     int32_t StopVideoSource();
119     int32_t StopDataSource();
120     int32_t PrepareRecorderSink();
121     int32_t StopInternal(bool block);
122 
123 private:
124     SourceManager sourceManager_[RECORDER_SOURCE_MAX_CNT];
125     RecorderSink *recorderSink_;
126     RecState status_ = RELEASED;
127     std::mutex mutex_;
128 };
129 } // namespace Media
130 } // namespace OHOS
131 
132 #endif // RECORDER_IMPL_H
133