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 #include "transcoder_impl.h"
17 #include <map>
18 #include "i_media_service.h"
19 #include "media_log.h"
20 #include "media_errors.h"
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "TransCoderImpl"};
24 }
25 
26 namespace OHOS {
27 namespace Media {
CreateTransCoder()28 std::shared_ptr<TransCoder> TransCoderFactory::CreateTransCoder()
29 {
30     std::shared_ptr<TransCoderImpl> impl = std::make_shared<TransCoderImpl>();
31     CHECK_AND_RETURN_RET_LOG(impl != nullptr, nullptr, "failed to new TransCoderImpl");
32 
33     int32_t ret = impl->Init();
34     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to init TransCoderImpl");
35     return impl;
36 }
37 
Init()38 int32_t TransCoderImpl::Init()
39 {
40     HiviewDFX::HiTraceChain::SetId(traceId_);
41     transCoderService_ = MediaServiceFactory::GetInstance().CreateTransCoderService();
42     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_NO_MEMORY, "failed to create transcoder service");
43     return MSERR_OK;
44 }
45 
TransCoderImpl()46 TransCoderImpl::TransCoderImpl()
47 {
48     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
49     traceId_ = HiviewDFX::HiTraceChain::Begin("TransCoderImpl", HITRACE_FLAG_DEFAULT);
50 }
51 
~TransCoderImpl()52 TransCoderImpl::~TransCoderImpl()
53 {
54     CHECK_AND_RETURN_LOG(transCoderService_ != nullptr, "0x%{public}06" PRIXPTR " Inst destroy", FAKE_POINTER(this));
55     (void)MediaServiceFactory::GetInstance().DestroyTransCoderService(transCoderService_);
56     transCoderService_ = nullptr;
57     HiviewDFX::HiTraceChain::End(traceId_);
58     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
59 }
60 
SetVideoEncoder(VideoCodecFormat encoder)61 int32_t TransCoderImpl::SetVideoEncoder(VideoCodecFormat encoder)
62 {
63     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetVideoEncoder in, encoder is %{public}d",
64         FAKE_POINTER(this), encoder);
65     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
66         "transcoder service does not exist..");
67     return transCoderService_->SetVideoEncoder(encoder);
68 }
69 
SetVideoSize(int32_t width,int32_t height)70 int32_t TransCoderImpl::SetVideoSize(int32_t width, int32_t height)
71 {
72     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetVideoSize in, width is %{public}d, height is %{public}d",
73         FAKE_POINTER(this), width, height);
74     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
75         "transcoder service does not exist..");
76     return transCoderService_->SetVideoSize(width, height);
77 }
78 
SetVideoEncodingBitRate(int32_t bitRate)79 int32_t TransCoderImpl::SetVideoEncodingBitRate(int32_t bitRate)
80 {
81     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetVideoEncodingBitRate in, bitRate is %{public}d",
82         FAKE_POINTER(this), bitRate);
83     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
84         "transcoder service does not exist..");
85     return transCoderService_->SetVideoEncodingBitRate(bitRate);
86 }
87 
SetAudioEncoder(AudioCodecFormat encoder)88 int32_t TransCoderImpl::SetAudioEncoder(AudioCodecFormat encoder)
89 {
90     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetAudioEncoder in, encoder is %{public}d",
91         FAKE_POINTER(this), encoder);
92     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
93         "transcoder service does not exist..");
94     return transCoderService_->SetAudioEncoder(encoder);
95 }
96 
SetAudioEncodingBitRate(int32_t bitRate)97 int32_t TransCoderImpl::SetAudioEncodingBitRate(int32_t bitRate)
98 {
99     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetAudioEncodingBitRate in, bitRate is %{public}d",
100         FAKE_POINTER(this), bitRate);
101     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
102         "transcoder service does not exist..");
103     return transCoderService_->SetAudioEncodingBitRate(bitRate);
104 }
105 
SetOutputFormat(OutputFormatType format)106 int32_t TransCoderImpl::SetOutputFormat(OutputFormatType format)
107 {
108     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetOutputFormat in, format is %{public}d",
109         FAKE_POINTER(this), format);
110     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
111         "transcoder service does not exist..");
112     return transCoderService_->SetOutputFormat(format);
113 }
114 
SetInputFile(int32_t fd,int64_t offset,int64_t size)115 int32_t TransCoderImpl::SetInputFile(int32_t fd, int64_t offset, int64_t size)
116 {
117     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetInputFile in", FAKE_POINTER(this));
118     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
119         "transcoder service does not exist..");
120     return transCoderService_->SetInputFile(fd, offset, size);
121 }
122 
SetOutputFile(int32_t fd)123 int32_t TransCoderImpl::SetOutputFile(int32_t fd)
124 {
125     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetOutputFile in", FAKE_POINTER(this));
126     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
127         "transcoder service does not exist..");
128     return transCoderService_->SetOutputFile(fd);
129 }
130 
SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> & callback)131 int32_t TransCoderImpl::SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback)
132 {
133     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetTransCoderCallback in", FAKE_POINTER(this));
134     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "input callback is nullptr.");
135     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
136         "transcoder service does not exist..");
137     return transCoderService_->SetTransCoderCallback(callback);
138 }
139 
Prepare()140 int32_t TransCoderImpl::Prepare()
141 {
142     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
143     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
144         "transcoder service does not exist..");
145     return transCoderService_->Prepare();
146 }
147 
Start()148 int32_t TransCoderImpl::Start()
149 {
150     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Start in", FAKE_POINTER(this));
151     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
152         "transcoder service does not exist..");
153     return transCoderService_->Start();
154 }
155 
Pause()156 int32_t TransCoderImpl::Pause()
157 {
158     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
159     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
160         "transcoder service does not exist..");
161     return transCoderService_->Pause();
162 }
163 
Resume()164 int32_t TransCoderImpl::Resume()
165 {
166     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Resume in", FAKE_POINTER(this));
167     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
168         "transcoder service does not exist..");
169     return transCoderService_->Resume();
170 }
171 
Cancel()172 int32_t TransCoderImpl::Cancel()
173 {
174     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Cancel in", FAKE_POINTER(this));
175     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
176         "transcoder service does not exist..");
177     return transCoderService_->Cancel();
178 }
179 
Release()180 int32_t TransCoderImpl::Release()
181 {
182     MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
183     CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
184         "transcoder service does not exist..");
185     (void)transCoderService_->Release();
186     (void)MediaServiceFactory::GetInstance().DestroyTransCoderService(transCoderService_);
187     transCoderService_ = nullptr;
188     return MSERR_OK;
189 }
190 
191 } // namespace Media
192 } // namespace OHOS
193