1 /* 2 * Copyright (C) 2024-2025 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 TRANSCODER_SERVICE_SERVER_H 17 #define TRANSCODER_SERVICE_SERVER_H 18 19 #include "i_transcoder_service.h" 20 #include "i_transcoder_engine.h" 21 #include "nocopyable.h" 22 #include "task_queue.h" 23 #include "watchdog.h" 24 #include "uri_helper.h" 25 #include "hitrace/tracechain.h" 26 27 namespace OHOS { 28 namespace Media { 29 enum class TransCoderWatchDogStatus : int32_t { 30 WATCHDOG_WATCHING = 0, 31 WATCHDOG_PAUSE, 32 }; 33 34 class TransCoderServer : public ITransCoderService, public ITransCoderEngineObs, public NoCopyable { 35 public: 36 static std::shared_ptr<ITransCoderService> Create(); 37 TransCoderServer(); 38 ~TransCoderServer(); 39 40 enum RecStatus { 41 REC_INITIALIZED = 0, 42 REC_CONFIGURED, 43 REC_PREPARED, 44 REC_TRANSCODERING, 45 REC_PAUSED, 46 REC_ERROR, 47 }; 48 49 // ITransCoderService override 50 int32_t SetVideoEncoder(VideoCodecFormat encoder) override; 51 int32_t SetVideoSize(int32_t width, int32_t height) override; 52 int32_t SetVideoEncodingBitRate(int32_t rate) override; 53 int32_t SetColorSpace(TranscoderColorSpace colorSpaceFormat) override; 54 int32_t SetEnableBFrame(bool enableBFrame) override; 55 int32_t SetAudioEncoder(AudioCodecFormat encoder) override; 56 int32_t SetAudioEncodingBitRate(int32_t bitRate) override; 57 int32_t SetOutputFormat(OutputFormatType format) override; 58 int32_t SetInputFile(int32_t fd, int64_t offset, int64_t size) override; 59 int32_t SetOutputFile(int32_t fd) override; 60 int32_t SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback) override; 61 int32_t Prepare() override; 62 int32_t Start() override; 63 int32_t Pause() override; 64 int32_t Resume() override; 65 int32_t Cancel() override; 66 int32_t Release() override; 67 68 // ITransCoderEngineObs override 69 void OnError(TransCoderErrorType errorType, int32_t errorCode) override; 70 void OnInfo(TransCoderOnInfoType type, int32_t extra) override; 71 void ReleaseInner(); 72 int32_t DumpInfo(int32_t fd); 73 74 private: 75 int32_t Init(); 76 void ChangeStatus(RecStatus status); 77 const std::string &GetStatusDescription(OHOS::Media::TransCoderServer::RecStatus status); 78 79 std::unique_ptr<ITransCoderEngine> transCoderEngine_ = nullptr; 80 std::shared_ptr<TransCoderCallback> transCoderCb_ = nullptr; 81 RecStatus status_ = REC_INITIALIZED; 82 std::mutex mutex_; 83 std::mutex cbMutex_; 84 TaskQueue taskQue_; 85 struct ConfigInfo { 86 VideoCodecFormat videoCodec = VIDEO_CODEC_FORMAT_BUTT; 87 AudioCodecFormat audioCodec = AUDIO_CODEC_FORMAT_BUTT; 88 TranscoderColorSpace colorSpaceFormat = TRANSCODER_COLORSPACE_NONE; 89 int32_t width = -1; 90 int32_t height = -1; 91 int32_t videoBitRate = -1; 92 int32_t audioBitRate = -1; 93 OutputFormatType format = FORMAT_BUTT; 94 std::string srcUrl; 95 int32_t srcFd = -1; 96 int64_t srcFdOffset = -1; 97 int64_t srcFdSize = -1; 98 int32_t dstUrl = -1; 99 bool enableBFrame = false; 100 } config_; 101 std::string lastErrMsg_; 102 103 std::shared_ptr<UriHelper> uriHelper_; 104 105 std::atomic<bool> watchdogPause_ = false; 106 107 uint64_t instanceId_ = 0; 108 bool isReleased_ = false; 109 }; 110 } // namespace Media 111 } // namespace OHOS 112 #endif // TRANSCODER_SERVICE_SERVER_H 113