• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service_stub.h"
17 #include <unistd.h>
18 #include "transcoder_listener_proxy.h"
19 #include "media_server_manager.h"
20 #include "media_log.h"
21 #include "media_errors.h"
22 #include "ipc_skeleton.h"
23 #include "media_permission.h"
24 #include "accesstoken_kit.h"
25 #include "media_dfx.h"
26 
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "TransCoderServiceStub"};
29 }
30 
31 namespace OHOS {
32 namespace Media {
Create()33 sptr<TransCoderServiceStub> TransCoderServiceStub::Create()
34 {
35     sptr<TransCoderServiceStub> transCoderStub = new(std::nothrow) TransCoderServiceStub();
36     CHECK_AND_RETURN_RET_LOG(transCoderStub != nullptr, nullptr, "failed to new TransCoderServiceStub");
37 
38     int32_t ret = transCoderStub->Init();
39     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "failed to transcoder stub init");
40     StatisticEventWriteBundleName("create", "TransCoderServiceStub");
41     return transCoderStub;
42 }
43 
TransCoderServiceStub()44 TransCoderServiceStub::TransCoderServiceStub()
45 {
46     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
47 }
48 
~TransCoderServiceStub()49 TransCoderServiceStub::~TransCoderServiceStub()
50 {
51     (void)CancellationMonitor(pid_);
52     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
53 }
54 
Init()55 int32_t TransCoderServiceStub::Init()
56 {
57     transCoderServer_ = TransCoderServer::Create();
58     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "failed to create TransCoderServer");
59 
60     recFuncs_[SET_LISTENER_OBJ] = &TransCoderServiceStub::SetListenerObject;
61     recFuncs_[SET_VIDEO_ENCODER] = &TransCoderServiceStub::SetVideoEncoder;
62     recFuncs_[SET_VIDEO_SIZE] = &TransCoderServiceStub::SetVideoSize;
63     recFuncs_[SET_VIDEO_ENCODING_BIT_RATE] = &TransCoderServiceStub::SetVideoEncodingBitRate;
64     recFuncs_[SET_AUDIO_ENCODER] = &TransCoderServiceStub::SetAudioEncoder;
65     recFuncs_[SET_AUDIO_ENCODING_BIT_RATE] = &TransCoderServiceStub::SetAudioEncodingBitRate;
66     recFuncs_[SET_OUTPUT_FORMAT] = &TransCoderServiceStub::SetOutputFormat;
67     recFuncs_[SET_INPUT_FILE_FD] = &TransCoderServiceStub::SetInputFileFd;
68     recFuncs_[SET_OUTPUT_FILE] = &TransCoderServiceStub::SetOutputFile;
69     recFuncs_[PREPARE] = &TransCoderServiceStub::Prepare;
70     recFuncs_[START] = &TransCoderServiceStub::Start;
71     recFuncs_[PAUSE] = &TransCoderServiceStub::Pause;
72     recFuncs_[RESUME] = &TransCoderServiceStub::Resume;
73     recFuncs_[CANCEL] = &TransCoderServiceStub::Cancel;
74     recFuncs_[RELEASE] = &TransCoderServiceStub::Release;
75     recFuncs_[DESTROY] = &TransCoderServiceStub::DestroyStub;
76 
77     pid_ = IPCSkeleton::GetCallingPid();
78     (void)RegisterMonitor(pid_);
79     return MSERR_OK;
80 }
81 
DumpInfo(int32_t fd)82 int32_t TransCoderServiceStub::DumpInfo(int32_t fd)
83 {
84     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transCoder server is nullptr");
85     return std::static_pointer_cast<TransCoderServer>(transCoderServer_)->DumpInfo(fd);
86 }
87 
DestroyStub()88 int32_t TransCoderServiceStub::DestroyStub()
89 {
90     transCoderServer_ = nullptr;
91     MediaServerManager::GetInstance().DestroyStubObject(MediaServerManager::TRANSCODER, AsObject());
92     return MSERR_OK;
93 }
94 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)95 int TransCoderServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
96     MessageOption &option)
97 {
98     MEDIA_LOGI("Stub: OnRemoteRequest of code: %{public}d is received", code);
99 
100     auto remoteDescriptor = data.ReadInterfaceToken();
101     CHECK_AND_RETURN_RET_LOG(TransCoderServiceStub::GetDescriptor() == remoteDescriptor,
102         MSERR_INVALID_OPERATION, "Invalid descriptor");
103 
104     auto itFunc = recFuncs_.find(code);
105     if (itFunc != recFuncs_.end()) {
106         auto memberFunc = itFunc->second;
107         if (memberFunc != nullptr) {
108             std::lock_guard<std::mutex> lock(mutex_);
109             (void)IpcRecovery(false);
110             int32_t ret = (this->*memberFunc)(data, reply);
111             CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, MSERR_OK, "calling memberFunc is failed.");
112             return MSERR_OK;
113         }
114     }
115     MEDIA_LOGW("TransCoderServiceStub: no member func supporting, applying default process");
116 
117     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
118 }
119 
SetListenerObject(const sptr<IRemoteObject> & object)120 int32_t TransCoderServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
121 {
122     CHECK_AND_RETURN_RET_LOG(object != nullptr, MSERR_NO_MEMORY, "set listener object is nullptr");
123 
124     sptr<IStandardTransCoderListener> listener = iface_cast<IStandardTransCoderListener>(object);
125     CHECK_AND_RETURN_RET_LOG(listener != nullptr, MSERR_NO_MEMORY, "failed to convert IStandardTransCoderListener");
126 
127     std::shared_ptr<TransCoderCallback> callback = std::make_shared<TransCoderListenerCallback>(listener);
128     CHECK_AND_RETURN_RET_LOG(callback != nullptr, MSERR_NO_MEMORY, "failed to new TransCoderListenerCallback");
129 
130     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transCoder server is nullptr");
131     (void)transCoderServer_->SetTransCoderCallback(callback);
132     return MSERR_OK;
133 }
134 
SetVideoEncoder(VideoCodecFormat encoder)135 int32_t TransCoderServiceStub::SetVideoEncoder(VideoCodecFormat encoder)
136 {
137     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
138     return transCoderServer_->SetVideoEncoder(encoder);
139 }
140 
SetVideoSize(int32_t width,int32_t height)141 int32_t TransCoderServiceStub::SetVideoSize(int32_t width, int32_t height)
142 {
143     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
144     return transCoderServer_->SetVideoSize(width, height);
145 }
146 
SetVideoEncodingBitRate(int32_t rate)147 int32_t TransCoderServiceStub::SetVideoEncodingBitRate(int32_t rate)
148 {
149     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
150     return transCoderServer_->SetVideoEncodingBitRate(rate);
151 }
152 
SetAudioEncoder(AudioCodecFormat encoder)153 int32_t TransCoderServiceStub::SetAudioEncoder(AudioCodecFormat encoder)
154 {
155     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
156     return transCoderServer_->SetAudioEncoder(encoder);
157 }
158 
SetAudioEncodingBitRate(int32_t bitRate)159 int32_t TransCoderServiceStub::SetAudioEncodingBitRate(int32_t bitRate)
160 {
161     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
162     return transCoderServer_->SetAudioEncodingBitRate(bitRate);
163 }
164 
SetOutputFormat(OutputFormatType format)165 int32_t TransCoderServiceStub::SetOutputFormat(OutputFormatType format)
166 {
167     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
168     return transCoderServer_->SetOutputFormat(format);
169 }
170 
SetInputFile(int32_t fd,int64_t offset,int64_t size)171 int32_t TransCoderServiceStub::SetInputFile(int32_t fd, int64_t offset, int64_t size)
172 {
173     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
174     return transCoderServer_->SetInputFile(fd, offset, size);
175 }
176 
SetOutputFile(int32_t fd)177 int32_t TransCoderServiceStub::SetOutputFile(int32_t fd)
178 {
179     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
180     return transCoderServer_->SetOutputFile(fd);
181 }
182 
Prepare()183 int32_t TransCoderServiceStub::Prepare()
184 {
185     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
186     return transCoderServer_->Prepare();
187 }
188 
Start()189 int32_t TransCoderServiceStub::Start()
190 {
191     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
192     return transCoderServer_->Start();
193 }
194 
Pause()195 int32_t TransCoderServiceStub::Pause()
196 {
197     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
198     return transCoderServer_->Pause();
199 }
200 
Resume()201 int32_t TransCoderServiceStub::Resume()
202 {
203     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
204     return transCoderServer_->Resume();
205 }
206 
Cancel()207 int32_t TransCoderServiceStub::Cancel()
208 {
209     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
210     return transCoderServer_->Cancel();
211 }
212 
Release()213 int32_t TransCoderServiceStub::Release()
214 {
215     CHECK_AND_RETURN_RET_LOG(transCoderServer_ != nullptr, MSERR_NO_MEMORY, "transcoder server is nullptr");
216     return transCoderServer_->Release();
217 }
218 
DoIpcAbnormality()219 int32_t TransCoderServiceStub::DoIpcAbnormality()
220 {
221     MEDIA_LOGI("Enter DoIpcAbnormality.");
222     SetIpcAlarmedFlag();
223     return MSERR_OK;
224 }
225 
DoIpcRecovery(bool fromMonitor)226 int32_t TransCoderServiceStub::DoIpcRecovery(bool fromMonitor)
227 {
228     MEDIA_LOGI("Enter DoIpcRecovery %{public}d.", fromMonitor);
229     UnSetIpcAlarmedFlag();
230     return MSERR_OK;
231 }
232 
SetListenerObject(MessageParcel & data,MessageParcel & reply)233 int32_t TransCoderServiceStub::SetListenerObject(MessageParcel &data, MessageParcel &reply)
234 {
235     sptr<IRemoteObject> object = data.ReadRemoteObject();
236     reply.WriteInt32(SetListenerObject(object));
237     return MSERR_OK;
238 }
239 
SetVideoEncoder(MessageParcel & data,MessageParcel & reply)240 int32_t TransCoderServiceStub::SetVideoEncoder(MessageParcel &data, MessageParcel &reply)
241 {
242     int32_t encoder = data.ReadInt32();
243     VideoCodecFormat codecFormat = static_cast<VideoCodecFormat>(encoder);
244     reply.WriteInt32(SetVideoEncoder(codecFormat));
245     return MSERR_OK;
246 }
247 
SetVideoSize(MessageParcel & data,MessageParcel & reply)248 int32_t TransCoderServiceStub::SetVideoSize(MessageParcel &data, MessageParcel &reply)
249 {
250     int32_t width = data.ReadInt32();
251     int32_t height = data.ReadInt32();
252     reply.WriteInt32(SetVideoSize(width, height));
253     return MSERR_OK;
254 }
255 
SetVideoEncodingBitRate(MessageParcel & data,MessageParcel & reply)256 int32_t TransCoderServiceStub::SetVideoEncodingBitRate(MessageParcel &data, MessageParcel &reply)
257 {
258     int32_t rate = data.ReadInt32();
259     reply.WriteInt32(SetVideoEncodingBitRate(rate));
260     return MSERR_OK;
261 }
262 
SetAudioEncoder(MessageParcel & data,MessageParcel & reply)263 int32_t TransCoderServiceStub::SetAudioEncoder(MessageParcel &data, MessageParcel &reply)
264 {
265     int32_t format = data.ReadInt32();
266     AudioCodecFormat encoderFormat = static_cast<AudioCodecFormat>(format);
267     reply.WriteInt32(SetAudioEncoder(encoderFormat));
268     return MSERR_OK;
269 }
270 
SetAudioEncodingBitRate(MessageParcel & data,MessageParcel & reply)271 int32_t TransCoderServiceStub::SetAudioEncodingBitRate(MessageParcel &data, MessageParcel &reply)
272 {
273     int32_t bitRate = data.ReadInt32();
274     reply.WriteInt32(SetAudioEncodingBitRate(bitRate));
275     return MSERR_OK;
276 }
277 
SetOutputFormat(MessageParcel & data,MessageParcel & reply)278 int32_t TransCoderServiceStub::SetOutputFormat(MessageParcel &data, MessageParcel &reply)
279 {
280     int32_t type = data.ReadInt32();
281     OutputFormatType formatType = static_cast<OutputFormatType>(type);
282     reply.WriteInt32(SetOutputFormat(formatType));
283     return MSERR_OK;
284 }
285 
SetInputFileFd(MessageParcel & data,MessageParcel & reply)286 int32_t TransCoderServiceStub::SetInputFileFd(MessageParcel &data, MessageParcel &reply)
287 {
288     int32_t fd = data.ReadFileDescriptor();
289     int64_t offset = data.ReadInt64();
290     int64_t size = data.ReadInt64();
291     reply.WriteInt32(SetInputFile(fd, offset, size));
292     (void)::close(fd);
293     return MSERR_OK;
294 }
295 
SetOutputFile(MessageParcel & data,MessageParcel & reply)296 int32_t TransCoderServiceStub::SetOutputFile(MessageParcel &data, MessageParcel &reply)
297 {
298     int32_t fd = data.ReadFileDescriptor();
299     reply.WriteInt32(SetOutputFile(fd));
300     (void)::close(fd);
301     return MSERR_OK;
302 }
303 
Prepare(MessageParcel & data,MessageParcel & reply)304 int32_t TransCoderServiceStub::Prepare(MessageParcel &data, MessageParcel &reply)
305 {
306     (void)data;
307     reply.WriteInt32(Prepare());
308     return MSERR_OK;
309 }
310 
Start(MessageParcel & data,MessageParcel & reply)311 int32_t TransCoderServiceStub::Start(MessageParcel &data, MessageParcel &reply)
312 {
313     (void)data;
314     reply.WriteInt32(Start());
315     return MSERR_OK;
316 }
317 
Pause(MessageParcel & data,MessageParcel & reply)318 int32_t TransCoderServiceStub::Pause(MessageParcel &data, MessageParcel &reply)
319 {
320     (void)data;
321     reply.WriteInt32(Pause());
322     return MSERR_OK;
323 }
324 
Resume(MessageParcel & data,MessageParcel & reply)325 int32_t TransCoderServiceStub::Resume(MessageParcel &data, MessageParcel &reply)
326 {
327     (void)data;
328     reply.WriteInt32(Resume());
329     return MSERR_OK;
330 }
331 
Cancel(MessageParcel & data,MessageParcel & reply)332 int32_t TransCoderServiceStub::Cancel(MessageParcel &data, MessageParcel &reply)
333 {
334     (void)data;
335     reply.WriteInt32(Cancel());
336     return MSERR_OK;
337 }
338 
Release(MessageParcel & data,MessageParcel & reply)339 int32_t TransCoderServiceStub::Release(MessageParcel &data, MessageParcel &reply)
340 {
341     (void)data;
342     reply.WriteInt32(Release());
343     return MSERR_OK;
344 }
345 
DestroyStub(MessageParcel & data,MessageParcel & reply)346 int32_t TransCoderServiceStub::DestroyStub(MessageParcel &data, MessageParcel &reply)
347 {
348     (void)data;
349     reply.WriteInt32(DestroyStub());
350     return MSERR_OK;
351 }
352 } // namespace Media
353 } // namespace OHOS
354