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