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 RECORDER_SERVICE_SERVER_H
17 #define RECORDER_SERVICE_SERVER_H
18 
19 #include <chrono>
20 
21 #include "i_recorder_service.h"
22 #include "i_recorder_engine.h"
23 #include "nocopyable.h"
24 #include "task_queue.h"
25 #include "watchdog.h"
26 #include "meta/meta.h"
27 #ifdef SUPPORT_POWER_MANAGER
28 #include "shutdown/sync_shutdown_callback_stub.h"
29 #include "shutdown/shutdown_client.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Media {
34 enum class RecorderWatchDogStatus : int32_t {
35     WATCHDOG_WATCHING = 0,
36     WATCHDOG_PAUSE,
37 };
38 #ifdef SUPPORT_POWER_MANAGER
39 class SaveDocumentSyncCallback : public PowerMgr::SyncShutdownCallbackStub {
40 public:
SaveDocumentSyncCallback()41     SaveDocumentSyncCallback() {};
~SaveDocumentSyncCallback()42     virtual ~SaveDocumentSyncCallback() {};
43     void OnSyncShutdown() override;
44     bool isShutdown = false;
45 
46 private:
47     const int32_t intervalTime = 500000; // 500 ms
48 };
49 #endif
50 class RecorderServer : public IRecorderService, public IRecorderEngineObs, public NoCopyable {
51 public:
52     static std::shared_ptr<IRecorderService> Create();
53     RecorderServer();
54     ~RecorderServer();
55 
56     enum RecStatus {
57         REC_INITIALIZED = 0,
58         REC_CONFIGURED,
59         REC_PREPARED,
60         REC_RECORDING,
61         REC_PAUSED,
62         REC_ERROR,
63     };
64 
65     enum HdrType : int8_t {
66         HDR_TYPE_NONE,
67         HDR_TYPE_VIVID,
68     };
69 
70     // IRecorderService override
71     int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId) override;
72     int32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder) override;
73     int32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height) override;
74     int32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate) override;
75     int32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate) override;
76     int32_t SetVideoIsHdr(int32_t sourceId, bool isHdr) override;
77     int32_t SetVideoEnableTemporalScale(int32_t sourceId, bool enableTemporalScale) override;
78     int32_t SetMetaSource(MetaSourceType source, int32_t &sourceId) override;
79     int32_t SetMetaConfigs(int32_t sourceId) override;
80     int32_t SetMetaMimeType(int32_t sourceId, const std::string_view &type) override;
81     int32_t SetMetaTimedKey(int32_t sourceId, const std::string_view &timedKey) override;
82     int32_t SetMetaSourceTrackMime(int32_t sourceId, const std::string_view &srcTrackMime) override;
83     int32_t SetCaptureRate(int32_t sourceId, double fps) override;
84     sptr<OHOS::Surface> GetSurface(int32_t sourceId) override;
85     sptr<OHOS::Surface> GetMetaSurface(int32_t sourceId) override;
86     int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId) override;
87     int32_t SetAudioDataSource(const std::shared_ptr<IAudioDataSource>& audioSource, int32_t& sourceId) override;
88     int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder) override;
89     int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate) override;
90     int32_t SetAudioChannels(int32_t sourceId, int32_t num) override;
91     int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate) override;
92     int32_t SetDataSource(DataSourceType dataType, int32_t &sourceId) override;
93     int32_t SetUserCustomInfo(Meta &userCustomInfo) override;
94     int32_t SetGenre(std::string &genre) override;
95     int32_t SetMaxDuration(int32_t duration) override;
96     int32_t SetOutputFormat(OutputFormatType format) override;
97     int32_t SetOutputFile(int32_t fd) override;
98     int32_t SetFileGenerationMode(FileGenerationMode mode) override;
99     int32_t SetNextOutputFile(int32_t fd) override;
100     int32_t SetMaxFileSize(int64_t size) override;
101     void SetLocation(float latitude, float longitude) override;
102     void SetOrientationHint(int32_t rotation) override;
103     int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback) override;
104     int32_t Prepare() override;
105     int32_t Start() override;
106     int32_t Pause() override;
107     int32_t Resume() override;
108     int32_t Stop(bool block) override;
109     int32_t Reset() override;
110     int32_t Release() override;
111     int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration) override;
112     int32_t SetParameter(int32_t sourceId, const Format &format) override;
113     int32_t DumpInfo(int32_t fd);
114     int32_t GetAVRecorderConfig(ConfigMap &configMap) override;
115     int32_t GetLocation(Location &location) override;
116     int32_t GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo &changeInfo) override;
117     int32_t GetAvailableEncoder(std::vector<EncoderCapabilityData> &encoderInfo) override;
118     int32_t GetMaxAmplitude() override;
119     int32_t IsWatermarkSupported(bool &isWatermarkSupported) override;
120     int32_t SetWatermark(std::shared_ptr<AVBuffer> &waterMarkBuffer) override;
121 
122     // IRecorderEngineObs override
123     void OnError(ErrorType errorType, int32_t errorCode) override;
124     void OnInfo(InfoType type, int32_t extra) override;
125     void OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo) override;
126 
127     void SetMetaDataReport();
128     int64_t GetCurrentMillisecond();
129     void SetErrorInfo(int32_t errCode, std::string &errMsg);
130     std::string GetVideoMime(VideoCodecFormat encoder);
131     std::string GetAudioMime(AudioCodecFormat encoder);
132 
133     /* used for DFX events */
134     uint64_t instanceId_ = 0;
135     std::string bundleName_;
136 private:
137     int32_t Init();
138     const std::string &GetStatusDescription(OHOS::Media::RecorderServer::RecStatus status);
139     bool CheckCameraOutputState();
140 
141     std::unique_ptr<IRecorderEngine> recorderEngine_ = nullptr;
142     std::shared_ptr<RecorderCallback> recorderCb_ = nullptr;
143     RecStatus status_ = REC_INITIALIZED;
144     std::mutex mutex_;
145     std::mutex cbMutex_;
146     TaskQueue taskQue_;
147     struct ConfigInfo {
148         VideoSourceType videoSource = VIDEO_SOURCE_BUTT;
149         AudioSourceType audioSource = AUDIO_SOURCE_INVALID;
150         MetaSourceType metaSource = VIDEO_META_SOURCE_INVALID;
151         VideoCodecFormat videoCodec = VIDEO_CODEC_FORMAT_BUTT;
152         AudioCodecFormat audioCodec = AUDIO_CODEC_FORMAT_BUTT;
153         int32_t width = 0;
154         int32_t height = 0;
155         int32_t frameRate = 0;
156         int32_t bitRate = 0;
157         bool isHdr = false;
158         bool enableTemporalScale = false;
159         double captureRate = 0.0;
160         int32_t audioSampleRate = 0;
161         int32_t audioChannel = 0;
162         int32_t audioBitRate = 0;
163         int32_t maxDuration = 0;
164         OutputFormatType format = FORMAT_BUTT;
165         int64_t maxFileSize = 0;
166         float latitude = 0.0;
167         float longitude = 0.0;
168         int32_t rotation = 0;
169         int32_t url = -1;
170         std::string uri = "";
171         FileGenerationMode fileGenerationMode = APP_CREATE;
172         std::string metaSrcTrackMime;
173         Meta customInfo;
174         std::string genre;
175         std::string metaMimeType;
176         std::string metaTimedKey;
177         bool withVideo = false;
178         bool withAudio = false;
179         bool withLocation = false;
180     } config_;
181     std::string lastErrMsg_;
182 
183     std::atomic<bool> watchdogPause_ = false;
184     struct StatisticalEventInfo {
185         int32_t errCode = -1;
186         std::string errMsg;
187         int32_t recordDuration = -1;
188         std::string containerMime;
189         std::string videoResolution;
190         int8_t hdrType = HdrType::HDR_TYPE_NONE;
191         int32_t startLatency = -1;
192     } statisticalEventInfo_;
193     int64_t startTime_ = 0;
194 #ifdef SUPPORT_POWER_MANAGER
195     sptr<SaveDocumentSyncCallback> syncCallback_ = nullptr;
196     PowerMgr::ShutdownClient &shutdownClient_ = PowerMgr::ShutdownClient::GetInstance();
197 #endif
198 };
199 } // namespace Media
200 } // namespace OHOS
201 #endif // RECORDER_SERVICE_SERVER_H
202