• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SetVideoEnableStableQualityMode(int32_t sourceId, bool enableStableQualityMode) override;
79     int32_t SetVideoEnableBFrame(int32_t sourceId, bool enableBFrame) override;
80     int32_t SetMetaSource(MetaSourceType source, int32_t &sourceId) override;
81     int32_t SetMetaConfigs(int32_t sourceId) override;
82     int32_t SetMetaMimeType(int32_t sourceId, const std::string_view &type) override;
83     int32_t SetMetaTimedKey(int32_t sourceId, const std::string_view &timedKey) override;
84     int32_t SetMetaSourceTrackMime(int32_t sourceId, const std::string_view &srcTrackMime) override;
85     int32_t SetCaptureRate(int32_t sourceId, double fps) override;
86     sptr<OHOS::Surface> GetSurface(int32_t sourceId) override;
87     sptr<OHOS::Surface> GetMetaSurface(int32_t sourceId) override;
88     int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId) override;
89     int32_t SetAudioDataSource(const std::shared_ptr<IAudioDataSource>& audioSource, int32_t& sourceId) override;
90     int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder) override;
91     int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate) override;
92     int32_t SetAudioChannels(int32_t sourceId, int32_t num) override;
93     int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate) override;
94     int32_t SetDataSource(DataSourceType dataType, int32_t &sourceId) override;
95     int32_t SetUserCustomInfo(Meta &userCustomInfo) override;
96     int32_t SetGenre(std::string &genre) override;
97     int32_t SetMaxDuration(int32_t duration) override;
98     int32_t SetOutputFormat(OutputFormatType format) override;
99     int32_t SetOutputFile(int32_t fd) override;
100     int32_t SetFileGenerationMode(FileGenerationMode mode) override;
101     int32_t SetNextOutputFile(int32_t fd) override;
102     int32_t SetMaxFileSize(int64_t size) override;
103     void SetLocation(float latitude, float longitude) override;
104     void SetOrientationHint(int32_t rotation) override;
105     int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback) override;
106     int32_t Prepare() override;
107     int32_t Start() override;
108     int32_t Pause() override;
109     int32_t Resume() override;
110     int32_t Stop(bool block) override;
111     int32_t Reset() override;
112     int32_t Release() override;
113     int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration) override;
114     int32_t SetParameter(int32_t sourceId, const Format &format) override;
115     int32_t DumpInfo(int32_t fd);
116     int32_t GetAVRecorderConfig(ConfigMap &configMap) override;
117     int32_t GetLocation(Location &location) override;
118     int32_t GetCurrentCapturerChangeInfo(AudioRecorderChangeInfo &changeInfo) override;
119     int32_t GetAvailableEncoder(std::vector<EncoderCapabilityData> &encoderInfo) override;
120     int32_t GetMaxAmplitude() override;
121     int32_t IsWatermarkSupported(bool &isWatermarkSupported) override;
122     int32_t SetWatermark(std::shared_ptr<AVBuffer> &waterMarkBuffer) override;
123     int32_t SetUserMeta(const std::shared_ptr<Meta> &userMeta) override;
124     int32_t SetWillMuteWhenInterrupted(bool muteWhenInterrupted) override;
125     int32_t TransmitQos(QOS::QosLevel level) override;
126 
127     // IRecorderEngineObs override
128     void OnError(ErrorType errorType, int32_t errorCode) override;
129     void OnInfo(InfoType type, int32_t extra) override;
130     void OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo) override;
131 
132     void SetMetaDataReport();
133     int64_t GetCurrentMillisecond();
134     void SetErrorInfo(int32_t errCode, std::string &errMsg);
135     std::string GetVideoMime(VideoCodecFormat encoder);
136     std::string GetAudioMime(AudioCodecFormat encoder);
137 
138     /* used for DFX events */
139     uint64_t instanceId_ = 0;
140     std::string bundleName_;
141 private:
142     int32_t Init();
143     const std::string &GetStatusDescription(OHOS::Media::RecorderServer::RecStatus status);
144     bool CheckCameraOutputState();
145 
146     std::unique_ptr<IRecorderEngine> recorderEngine_ = nullptr;
147     std::shared_ptr<RecorderCallback> recorderCb_ = nullptr;
148     RecStatus status_ = REC_INITIALIZED;
149     std::mutex mutex_;
150     std::mutex cbMutex_;
151     TaskQueue taskQue_;
152     struct ConfigInfo {
153         VideoSourceType videoSource = VIDEO_SOURCE_BUTT;
154         AudioSourceType audioSource = AUDIO_SOURCE_INVALID;
155         MetaSourceType metaSource = VIDEO_META_SOURCE_INVALID;
156         VideoCodecFormat videoCodec = VIDEO_CODEC_FORMAT_BUTT;
157         AudioCodecFormat audioCodec = AUDIO_CODEC_FORMAT_BUTT;
158         int32_t width = 0;
159         int32_t height = 0;
160         int32_t frameRate = 0;
161         int32_t bitRate = 0;
162         bool isHdr = false;
163         bool enableTemporalScale = false;
164         bool enableStableQualityMode = false;
165         bool enableBFrame = false;
166         double captureRate = 0.0;
167         int32_t audioSampleRate = 0;
168         int32_t audioChannel = 0;
169         int32_t audioBitRate = 0;
170         int32_t maxDuration = 0;
171         OutputFormatType format = FORMAT_BUTT;
172         int64_t maxFileSize = 0;
173         float latitude = 0.0;
174         float longitude = 0.0;
175         int32_t rotation = 0;
176         int32_t url = -1;
177         std::string uri = "";
178         FileGenerationMode fileGenerationMode = APP_CREATE;
179         std::string metaSrcTrackMime;
180         Meta customInfo;
181         std::string genre;
182         std::string metaMimeType;
183         std::string metaTimedKey;
184         bool withVideo = false;
185         bool withAudio = false;
186         bool withLocation = false;
187     } config_;
188     std::string lastErrMsg_;
189 
190     std::atomic<bool> watchdogPause_ = false;
191     struct StatisticalEventInfo {
192         int32_t errCode = -1;
193         std::string errMsg;
194         int32_t recordDuration = -1;
195         std::string containerMime;
196         std::string videoResolution;
197         int8_t hdrType = HdrType::HDR_TYPE_NONE;
198         int32_t startLatency = -1;
199     } statisticalEventInfo_;
200     int64_t startTime_ = 0;
201     QOS::QosLevel clientQos_ = QOS::QosLevel::QOS_BACKGROUND;
202 #ifdef SUPPORT_POWER_MANAGER
203     sptr<SaveDocumentSyncCallback> syncCallback_ = nullptr;
204     PowerMgr::ShutdownClient &shutdownClient_ = PowerMgr::ShutdownClient::GetInstance();
205 #endif
206 };
207 } // namespace Media
208 } // namespace OHOS
209 #endif // RECORDER_SERVICE_SERVER_H
210