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(format != FORMAT_DEFAULT, MSERR_INVALID_VAL, "format is invalid");
111 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
112 "transcoder service does not exist..");
113 return transCoderService_->SetOutputFormat(format);
114 }
115
SetInputFile(int32_t fd,int64_t offset,int64_t size)116 int32_t TransCoderImpl::SetInputFile(int32_t fd, int64_t offset, int64_t size)
117 {
118 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetInputFile in", FAKE_POINTER(this));
119 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
120 "transcoder service does not exist..");
121 return transCoderService_->SetInputFile(fd, offset, size);
122 }
123
SetOutputFile(int32_t fd)124 int32_t TransCoderImpl::SetOutputFile(int32_t fd)
125 {
126 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetOutputFile in", FAKE_POINTER(this));
127 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
128 "transcoder service does not exist..");
129 return transCoderService_->SetOutputFile(fd);
130 }
131
SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> & callback)132 int32_t TransCoderImpl::SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback)
133 {
134 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " SetTransCoderCallback in", FAKE_POINTER(this));
135 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_INVALID_VAL, "input callback is nullptr.");
136 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
137 "transcoder service does not exist..");
138 return transCoderService_->SetTransCoderCallback(callback);
139 }
140
Prepare()141 int32_t TransCoderImpl::Prepare()
142 {
143 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Prepare in", FAKE_POINTER(this));
144 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
145 "transcoder service does not exist..");
146 return transCoderService_->Prepare();
147 }
148
Start()149 int32_t TransCoderImpl::Start()
150 {
151 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Start in", FAKE_POINTER(this));
152 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
153 "transcoder service does not exist..");
154 return transCoderService_->Start();
155 }
156
Pause()157 int32_t TransCoderImpl::Pause()
158 {
159 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Pause in", FAKE_POINTER(this));
160 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
161 "transcoder service does not exist..");
162 return transCoderService_->Pause();
163 }
164
Resume()165 int32_t TransCoderImpl::Resume()
166 {
167 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Resume in", FAKE_POINTER(this));
168 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
169 "transcoder service does not exist..");
170 return transCoderService_->Resume();
171 }
172
Cancel()173 int32_t TransCoderImpl::Cancel()
174 {
175 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Cancel in", FAKE_POINTER(this));
176 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
177 "transcoder service does not exist..");
178 return transCoderService_->Cancel();
179 }
180
Release()181 int32_t TransCoderImpl::Release()
182 {
183 MEDIA_LOGI("TransCoderImpl:0x%{public}06" PRIXPTR " Release in", FAKE_POINTER(this));
184 CHECK_AND_RETURN_RET_LOG(transCoderService_ != nullptr, MSERR_INVALID_OPERATION,
185 "transcoder service does not exist..");
186 (void)transCoderService_->Release();
187 (void)MediaServiceFactory::GetInstance().DestroyTransCoderService(transCoderService_);
188 transCoderService_ = nullptr;
189 return MSERR_OK;
190 }
191
192 } // namespace Media
193 } // namespace OHOS
194