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_client.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "TransCoderClient"};
22 }
23
24 namespace OHOS {
25 namespace Media {
Create(const sptr<IStandardTransCoderService> & ipcProxy)26 std::shared_ptr<TransCoderClient> TransCoderClient::Create(const sptr<IStandardTransCoderService> &ipcProxy)
27 {
28 CHECK_AND_RETURN_RET_LOG(ipcProxy != nullptr, nullptr, "ipcProxy is nullptr..");
29
30 std::shared_ptr<TransCoderClient> transCoder = std::make_shared<TransCoderClient>(ipcProxy);
31 CHECK_AND_RETURN_RET_LOG(transCoder != nullptr, nullptr, "failed to new TransCoderClient..");
32
33 int32_t ret = transCoder->CreateListenerObject();
34 CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to create listener object..");
35
36 return transCoder;
37 }
38
TransCoderClient(const sptr<IStandardTransCoderService> & ipcProxy)39 TransCoderClient::TransCoderClient(const sptr<IStandardTransCoderService> &ipcProxy)
40 : transCoderProxy_(ipcProxy)
41 {
42 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
43 }
44
~TransCoderClient()45 TransCoderClient::~TransCoderClient()
46 {
47 {
48 std::lock_guard<std::mutex> lock(mutex_);
49 (void)DisableMonitor();
50 CHECK_AND_RETURN_LOG(transCoderProxy_ != nullptr,
51 "0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
52 (void)transCoderProxy_->DestroyStub();
53 transCoderProxy_ = nullptr;
54 }
55 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
56 }
57
MediaServerDied()58 void TransCoderClient::MediaServerDied()
59 {
60 {
61 std::lock_guard<std::mutex> lock(mutex_);
62 transCoderProxy_ = nullptr;
63 listenerStub_ = nullptr;
64 CHECK_AND_RETURN(callback_ != nullptr);
65 callback_->OnError(MSERR_SERVICE_DIED, "mediaserver died, please create a new avtranscoder instance again");
66 }
67 }
68
CreateListenerObject()69 int32_t TransCoderClient::CreateListenerObject()
70 {
71 std::lock_guard<std::mutex> lock(mutex_);
72 listenerStub_ = new(std::nothrow) TransCoderListenerStub();
73 CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY, "failed to new TransCoderListenerStub object");
74 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
75
76 (void)listenerStub_->SetMonitor(weak_from_this());
77 sptr<IRemoteObject> object = listenerStub_->AsObject();
78 CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "listener object is nullptr..");
79
80 MEDIA_LOGD("SetListenerObject");
81 return transCoderProxy_->SetListenerObject(object);
82 }
83
SetVideoEncoder(VideoCodecFormat encoder)84 int32_t TransCoderClient::SetVideoEncoder(VideoCodecFormat encoder)
85 {
86 std::lock_guard<std::mutex> lock(mutex_);
87 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
88
89 MEDIA_LOGD("SetVideoSource encoder(%{public}d)", encoder);
90 return transCoderProxy_->SetVideoEncoder(encoder);
91 }
92
SetVideoSize(int32_t width,int32_t height)93 int32_t TransCoderClient::SetVideoSize(int32_t width, int32_t height)
94 {
95 std::lock_guard<std::mutex> lock(mutex_);
96 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
97
98 MEDIA_LOGD("SetVideoSize width(%{public}d), height(%{public}d)", width, height);
99 return transCoderProxy_->SetVideoSize(width, height);
100 }
101
SetVideoEncodingBitRate(int32_t rate)102 int32_t TransCoderClient::SetVideoEncodingBitRate(int32_t rate)
103 {
104 std::lock_guard<std::mutex> lock(mutex_);
105 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
106
107 MEDIA_LOGD("SetVideoEncodingBitRate rate(%{public}d)", rate);
108 return transCoderProxy_->SetVideoEncodingBitRate(rate);
109 }
110
SetColorSpace(TranscoderColorSpace colorSpaceFormat)111 int32_t TransCoderClient::SetColorSpace(TranscoderColorSpace colorSpaceFormat)
112 {
113 std::lock_guard<std::mutex> lock(mutex_);
114 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
115
116 MEDIA_LOGD("SetColorSpace, format(%{public}d)", static_cast<int32_t>(colorSpaceFormat));
117 return transCoderProxy_->SetColorSpace(colorSpaceFormat);
118 }
119
SetEnableBFrame(bool enableBFrame)120 int32_t TransCoderClient::SetEnableBFrame(bool enableBFrame)
121 {
122 std::lock_guard<std::mutex> lock(mutex_);
123 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
124
125 MEDIA_LOGD("SetEnableBFrame, format(%{public}d)", static_cast<int32_t>(enableBFrame));
126 return transCoderProxy_->SetEnableBFrame(enableBFrame);
127 }
128
SetAudioEncoder(AudioCodecFormat encoder)129 int32_t TransCoderClient::SetAudioEncoder(AudioCodecFormat encoder)
130 {
131 std::lock_guard<std::mutex> lock(mutex_);
132 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
133
134 MEDIA_LOGD("SetAudioEncoder encoder(%{public}d)", encoder);
135 return transCoderProxy_->SetAudioEncoder(encoder);
136 }
137
SetAudioEncodingBitRate(int32_t bitRate)138 int32_t TransCoderClient::SetAudioEncodingBitRate(int32_t bitRate)
139 {
140 std::lock_guard<std::mutex> lock(mutex_);
141 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
142
143 MEDIA_LOGD("SetAudioEncodingBitRate bitRate(%{public}d)", bitRate);
144 return transCoderProxy_->SetAudioEncodingBitRate(bitRate);
145 }
146
SetOutputFormat(OutputFormatType format)147 int32_t TransCoderClient::SetOutputFormat(OutputFormatType format)
148 {
149 std::lock_guard<std::mutex> lock(mutex_);
150 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
151
152 MEDIA_LOGD("SetOutputFormat format(%{public}d)", format);
153 return transCoderProxy_->SetOutputFormat(format);
154 }
155
SetInputFile(int32_t fd,int64_t offset,int64_t size)156 int32_t TransCoderClient::SetInputFile(int32_t fd, int64_t offset, int64_t size)
157 {
158 std::lock_guard<std::mutex> lock(mutex_);
159 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
160
161 return transCoderProxy_->SetInputFile(fd, offset, size);
162 }
163
SetOutputFile(int32_t fd)164 int32_t TransCoderClient::SetOutputFile(int32_t fd)
165 {
166 std::lock_guard<std::mutex> lock(mutex_);
167 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
168
169 MEDIA_LOGD("SetOutputFile fd(%{public}d)", fd);
170 return transCoderProxy_->SetOutputFile(fd);
171 }
172
SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> & callback)173 int32_t TransCoderClient::SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback)
174 {
175 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "input param callback is nullptr.");
176 CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY, "listenerStub_ is nullptr.");
177
178 callback_ = callback;
179 MEDIA_LOGD("SetTransCoderCallback");
180 listenerStub_->SetTransCoderCallback(callback);
181 return MSERR_OK;
182 }
183
Prepare()184 int32_t TransCoderClient::Prepare()
185 {
186 std::lock_guard<std::mutex> lock(mutex_);
187 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
188
189 MEDIA_LOGD("Prepare");
190 return transCoderProxy_->Prepare();
191 }
192
ExecuteWhen(int32_t ret,bool ok)193 int32_t TransCoderClient::ExecuteWhen(int32_t ret, bool ok)
194 {
195 if ((ok && (ret == MSERR_OK)) || ((!ok) && (ret != MSERR_OK))) {
196 (void)DisableMonitor();
197 }
198 return ret;
199 }
200
Start()201 int32_t TransCoderClient::Start()
202 {
203 std::lock_guard<std::mutex> lock(mutex_);
204 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
205
206 MEDIA_LOGD("TransCoderClient::Start");
207 (void)EnableMonitor();
208 return ExecuteWhen(transCoderProxy_->Start(), false);
209 }
210
Pause()211 int32_t TransCoderClient::Pause()
212 {
213 std::lock_guard<std::mutex> lock(mutex_);
214 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
215
216 MEDIA_LOGD("Pause");
217 return ExecuteWhen(transCoderProxy_->Pause(), true);
218 }
219
Resume()220 int32_t TransCoderClient::Resume()
221 {
222 std::lock_guard<std::mutex> lock(mutex_);
223 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
224
225 MEDIA_LOGD("Resume");
226 (void)EnableMonitor();
227 return ExecuteWhen(transCoderProxy_->Resume(), false);
228 }
229
Cancel()230 int32_t TransCoderClient::Cancel()
231 {
232 std::lock_guard<std::mutex> lock(mutex_);
233 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
234
235 MEDIA_LOGD("Cancel");
236 return ExecuteWhen(transCoderProxy_->Cancel(), true);
237 }
238
Release()239 int32_t TransCoderClient::Release()
240 {
241 std::lock_guard<std::mutex> lock(mutex_);
242 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
243
244 MEDIA_LOGD("Release");
245 return ExecuteWhen(transCoderProxy_->Release(), true);
246 }
247 } // namespace Media
248 } // namespace OHOS
249