1 /*
2  * Copyright (C) 2024 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 SetAudioEncoder(AudioCodecFormat encoder) override;
54     int32_t SetAudioEncodingBitRate(int32_t bitRate) override;
55     int32_t SetOutputFormat(OutputFormatType format) override;
56     int32_t SetInputFile(int32_t fd, int64_t offset, int64_t size) override;
57     int32_t SetOutputFile(int32_t fd) override;
58     int32_t SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback) override;
59     int32_t Prepare() override;
60     int32_t Start() override;
61     int32_t Pause() override;
62     int32_t Resume() override;
63     int32_t Cancel() override;
64     int32_t Release() override;
65 
66     // ITransCoderEngineObs override
67     void OnError(TransCoderErrorType errorType, int32_t errorCode) override;
68     void OnInfo(TransCoderOnInfoType type, int32_t extra) override;
69     void ReleaseInner();
70     int32_t DumpInfo(int32_t fd);
71 
72 private:
73     int32_t Init();
74     const std::string &GetStatusDescription(OHOS::Media::TransCoderServer::RecStatus status);
75 
76     std::unique_ptr<ITransCoderEngine> transCoderEngine_ = nullptr;
77     std::shared_ptr<TransCoderCallback> transCoderCb_ = nullptr;
78     RecStatus status_ = REC_INITIALIZED;
79     std::mutex mutex_;
80     std::mutex cbMutex_;
81     TaskQueue taskQue_;
82     struct ConfigInfo {
83         VideoCodecFormat videoCodec = VIDEO_CODEC_FORMAT_BUTT;
84         AudioCodecFormat audioCodec = AUDIO_CODEC_FORMAT_BUTT;
85         int32_t width = -1;
86         int32_t height = -1;
87         int32_t videoBitRate = -1;
88         int32_t audioBitRate = -1;
89         OutputFormatType format = FORMAT_BUTT;
90         std::string srcUrl;
91         int32_t srcFd = -1;
92         int64_t srcFdOffset = -1;
93         int64_t srcFdSize = -1;
94         int32_t dstUrl = -1;
95     } config_;
96     std::string lastErrMsg_;
97 
98     std::shared_ptr<UriHelper> uriHelper_;
99 
100     std::atomic<bool> watchdogPause_ = false;
101 
102     uint64_t instanceId_ = 0;
103 };
104 } // namespace Media
105 } // namespace OHOS
106 #endif // TRANSCODER_SERVICE_SERVER_H
107