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_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, pls create a new transcodercallback 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
SetAudioEncoder(AudioCodecFormat encoder)111 int32_t TransCoderClient::SetAudioEncoder(AudioCodecFormat encoder)
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("SetAudioEncoder encoder(%{public}d)", encoder);
117 return transCoderProxy_->SetAudioEncoder(encoder);
118 }
119
SetAudioEncodingBitRate(int32_t bitRate)120 int32_t TransCoderClient::SetAudioEncodingBitRate(int32_t bitRate)
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("SetAudioEncodingBitRate bitRate(%{public}d)", bitRate);
126 return transCoderProxy_->SetAudioEncodingBitRate(bitRate);
127 }
128
SetOutputFormat(OutputFormatType format)129 int32_t TransCoderClient::SetOutputFormat(OutputFormatType format)
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("SetOutputFormat format(%{public}d)", format);
135 return transCoderProxy_->SetOutputFormat(format);
136 }
137
SetInputFile(int32_t fd,int64_t offset,int64_t size)138 int32_t TransCoderClient::SetInputFile(int32_t fd, int64_t offset, int64_t size)
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 return transCoderProxy_->SetInputFile(fd, offset, size);
144 }
145
SetOutputFile(int32_t fd)146 int32_t TransCoderClient::SetOutputFile(int32_t fd)
147 {
148 std::lock_guard<std::mutex> lock(mutex_);
149 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
150
151 MEDIA_LOGD("SetOutputFile fd(%{public}d)", fd);
152 return transCoderProxy_->SetOutputFile(fd);
153 }
154
SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> & callback)155 int32_t TransCoderClient::SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback)
156 {
157 CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "input param callback is nullptr.");
158 CHECK_AND_RETURN_RET_LOG(listenerStub_ != nullptr, MSERR_NO_MEMORY, "listenerStub_ is nullptr.");
159
160 callback_ = callback;
161 MEDIA_LOGD("SetTransCoderCallback");
162 listenerStub_->SetTransCoderCallback(callback);
163 return MSERR_OK;
164 }
165
Prepare()166 int32_t TransCoderClient::Prepare()
167 {
168 std::lock_guard<std::mutex> lock(mutex_);
169 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
170
171 MEDIA_LOGD("Prepare");
172 return transCoderProxy_->Prepare();
173 }
174
ExecuteWhen(int32_t ret,bool ok)175 int32_t TransCoderClient::ExecuteWhen(int32_t ret, bool ok)
176 {
177 if ((ok && (ret == MSERR_OK)) || ((!ok) && (ret != MSERR_OK))) {
178 (void)DisableMonitor();
179 }
180 return ret;
181 }
182
Start()183 int32_t TransCoderClient::Start()
184 {
185 std::lock_guard<std::mutex> lock(mutex_);
186 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
187
188 MEDIA_LOGD("TransCoderClient::Start");
189 (void)EnableMonitor();
190 return ExecuteWhen(transCoderProxy_->Start(), false);
191 }
192
Pause()193 int32_t TransCoderClient::Pause()
194 {
195 std::lock_guard<std::mutex> lock(mutex_);
196 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
197
198 MEDIA_LOGD("Pause");
199 return ExecuteWhen(transCoderProxy_->Pause(), true);
200 }
201
Resume()202 int32_t TransCoderClient::Resume()
203 {
204 std::lock_guard<std::mutex> lock(mutex_);
205 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
206
207 MEDIA_LOGD("Resume");
208 (void)EnableMonitor();
209 return ExecuteWhen(transCoderProxy_->Resume(), false);
210 }
211
Cancel()212 int32_t TransCoderClient::Cancel()
213 {
214 std::lock_guard<std::mutex> lock(mutex_);
215 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
216
217 MEDIA_LOGD("Cancel");
218 return ExecuteWhen(transCoderProxy_->Cancel(), true);
219 }
220
Release()221 int32_t TransCoderClient::Release()
222 {
223 std::lock_guard<std::mutex> lock(mutex_);
224 CHECK_AND_RETURN_RET_LOG(transCoderProxy_ != nullptr, MSERR_NO_MEMORY, "transcoder service does not exist.");
225
226 MEDIA_LOGD("Release");
227 return ExecuteWhen(transCoderProxy_->Release(), true);
228 }
229 } // namespace Media
230 } // namespace OHOS
231