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 #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
SetColorSpace(TranscoderColorSpace colorSpaceFormat)88 int32_t TransCoderImpl::SetColorSpace(TranscoderColorSpace colorSpaceFormat)
89 {
90 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetColorSpace in, colorSpace is %{public}d",
91 FAKE_POINTER(this), static_cast<int32_t>(colorSpaceFormat));
92 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
93 "transcoder service does not exist..");
94 return transCoderService_->SetColorSpace(colorSpaceFormat);
95 }
96
SetEnableBFrame(bool enableBFrame)97 int32_t TransCoderImpl::SetEnableBFrame(bool enableBFrame)
98 {
99 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetEnableBFrame in, enableBFrame is %{public}d",
100 FAKE_POINTER(this), static_cast<int32_t>(enableBFrame));
101 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
102 "transcoder service does not exist..");
103 return transCoderService_->SetEnableBFrame(enableBFrame);
104 }
105
SetAudioEncoder(AudioCodecFormat encoder)106 int32_t TransCoderImpl::SetAudioEncoder(AudioCodecFormat encoder)
107 {
108 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetAudioEncoder in, encoder is %{public}d",
109 FAKE_POINTER(this), encoder);
110 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
111 "transcoder service does not exist..");
112 return transCoderService_->SetAudioEncoder(encoder);
113 }
114
SetAudioEncodingBitRate(int32_t bitRate)115 int32_t TransCoderImpl::SetAudioEncodingBitRate(int32_t bitRate)
116 {
117 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetAudioEncodingBitRate in, bitRate is %{public}d",
118 FAKE_POINTER(this), bitRate);
119 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
120 "transcoder service does not exist..");
121 return transCoderService_->SetAudioEncodingBitRate(bitRate);
122 }
123
SetOutputFormat(OutputFormatType format)124 int32_t TransCoderImpl::SetOutputFormat(OutputFormatType format)
125 {
126 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetOutputFormat in, format is %{public}d",
127 FAKE_POINTER(this), format);
128 CHECK_AND_RETURN_RET_LOG(format != FORMAT_DEFAULT, MSERR_INVALID_VAL, "format is invalid");
129 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
130 "transcoder service does not exist..");
131 return transCoderService_->SetOutputFormat(format);
132 }
133
SetInputFile(int32_t fd,int64_t offset,int64_t size)134 int32_t TransCoderImpl::SetInputFile(int32_t fd, int64_t offset, int64_t size)
135 {
136 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetInputFile in", FAKE_POINTER(this));
137 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
138 "transcoder service does not exist..");
139 return transCoderService_->SetInputFile(fd, offset, size);
140 }
141
SetOutputFile(int32_t fd)142 int32_t TransCoderImpl::SetOutputFile(int32_t fd)
143 {
144 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetOutputFile in", FAKE_POINTER(this));
145 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
146 "transcoder service does not exist..");
147 return transCoderService_->SetOutputFile(fd);
148 }
149
SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> & callback)150 int32_t TransCoderImpl::SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback)
151 {
152 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetTransCoderCallback in", FAKE_POINTER(this));
153 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "input callback is nullptr.");
154 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
155 "transcoder service does not exist..");
156 return transCoderService_->SetTransCoderCallback(callback);
157 }
158
Prepare()159 int32_t TransCoderImpl::Prepare()
160 {
161 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
162 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
163 "transcoder service does not exist..");
164 return transCoderService_->Prepare();
165 }
166
Start()167 int32_t TransCoderImpl::Start()
168 {
169 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Start in", FAKE_POINTER(this));
170 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
171 "transcoder service does not exist..");
172 return transCoderService_->Start();
173 }
174
Pause()175 int32_t TransCoderImpl::Pause()
176 {
177 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
178 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
179 "transcoder service does not exist..");
180 return transCoderService_->Pause();
181 }
182
Resume()183 int32_t TransCoderImpl::Resume()
184 {
185 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Resume in", FAKE_POINTER(this));
186 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
187 "transcoder service does not exist..");
188 return transCoderService_->Resume();
189 }
190
Cancel()191 int32_t TransCoderImpl::Cancel()
192 {
193 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Cancel in", FAKE_POINTER(this));
194 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
195 "transcoder service does not exist..");
196 return transCoderService_->Cancel();
197 }
198
Release()199 int32_t TransCoderImpl::Release()
200 {
201 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
202 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
203 "transcoder service does not exist..");
204 (void)transCoderService_->Release();
205 (void)MediaServiceFactory::GetInstance().DestroyTransCoderService(transCoderService_);
206 transCoderService_ = nullptr;
207 return MSERR_OK;
208 }
209
210 } // namespace Media
211 } // namespace OHOS
212