• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 "recorder_client.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdio>
21 #include <fcntl.h>
22 #include <memory>
23 #include <stdexcept>
24 #include <string>
25 #include <vector>
26 #include "ipc_skeleton.h"
27 #include "media_log.h"
28 #include "pms_interface.h"
29 #include "recorder_common.h"
30 #include "samgr_lite.h"
31 #include "securec.h"
32 #include "surface_impl.h"
33 
34 using namespace std;
35 
OHOS_SystemInit(void)36 extern "C" void __attribute__((weak)) OHOS_SystemInit(void)
37 {
38     SAMGR_Bootstrap();
39 }
40 namespace OHOS {
41 namespace Media {
42 struct CallBackPara {
43     RecFunc funcId;
44     int32_t ret;
45     int32_t data;
46 };
47 
SimpleCallback(void * owner,int code,IpcIo * reply)48 static int32_t SimpleCallback(void *owner, int code, IpcIo *reply)
49 {
50     if (code != 0) {
51         MEDIA_ERR_LOG("callback error, code = %d", code);
52         return -1;
53     }
54 
55     if (owner == nullptr) {
56         return -1;
57     }
58     CallBackPara *para = (CallBackPara *)owner;
59     MEDIA_INFO_LOG("Callback, funcId = %d", para->funcId);
60     ReadInt32(reply, &(para->ret));
61     return 0;
62 }
63 
RecorderCallbackSvc(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)64 static int32_t RecorderCallbackSvc(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
65 {
66     if (option.args == nullptr) {
67         MEDIA_ERR_LOG("call back error, option.args is null\n");
68         return MEDIA_ERR;
69     }
70     /* Not need to check if callback is valid because message will not arrive before callback ready */
71     RecorderCallback *callback = static_cast<RecorderCallback *>(option.args);
72 
73     MEDIA_INFO_LOG("DeviceCallback, code=%d", code);
74     switch (code) {
75         case REC_ANONYMOUS_FUNC_ON_ERROR: {
76             int32_t type;
77             ReadInt32(data, &type);
78             int32_t tempCode;
79             ReadInt32(data, &tempCode);
80             callback->OnError(type, tempCode);
81             break;
82         }
83         case REC_ANONYMOUS_FUNC_ON_INFO: {
84             int32_t type;
85             ReadInt32(data, &type);
86             int32_t tempCode;
87             ReadInt32(data, &tempCode);
88             callback->OnInfo(type, tempCode);
89             break;
90         }
91         default: {
92             MEDIA_ERR_LOG("Unsupport callback service.(code=%d)", code);
93             break;
94         }
95     }
96     return MEDIA_OK;
97 }
98 
RecorderClient()99 Recorder::RecorderClient::RecorderClient()
100 {
101     OHOS_SystemInit();
102 
103     IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(RECORDER_SERVICE_NAME);
104     if (iUnknown == nullptr) {
105         MEDIA_ERR_LOG("iUnknown is NULL");
106         throw runtime_error("Ipc proxy GetDefaultFeatureApi failed.");
107     }
108 
109     (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&proxy_);
110     if (proxy_ == nullptr) {
111         MEDIA_ERR_LOG("QueryInterface failed");
112         throw runtime_error("Ipc proxy init failed.");
113     }
114     IpcIo io;
115     uint8_t tmpData[DEFAULT_IPC_SIZE];
116     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
117     CallBackPara para = {.funcId = REC_FUNC_CONNECT, .ret = MEDIA_IPC_FAILED};
118     int32_t ret = proxy_->Invoke(proxy_, REC_FUNC_CONNECT, &io, &para, SimpleCallback);
119     if (ret != 0) {
120         MEDIA_ERR_LOG("Connect recorder server failed, ret=%d", ret);
121         throw runtime_error("Ipc proxy Invoke failed.");
122     }
123     MEDIA_ERR_LOG("Create recorder client succeed.");
124 }
125 
DisConnectCallback(void * owner,int code,IpcIo * reply)126 static int32_t DisConnectCallback(void *owner, int code, IpcIo *reply)
127 {
128     if (code != 0) {
129         MEDIA_ERR_LOG("callback error, code = %d", code);
130         return -1;
131     }
132 
133     if (owner == nullptr) {
134         return -1;
135     }
136     CallBackPara *para = (CallBackPara *)owner;
137     MEDIA_INFO_LOG("Callback, funcId = %d", para->funcId);
138     return 0;
139 }
140 
141 
~RecorderClient()142 Recorder::RecorderClient::~RecorderClient()
143 {
144     IpcIo io;
145     uint8_t tmpData[DEFAULT_IPC_SIZE];
146     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
147     CallBackPara para = {.funcId = REC_FUNC_DISCONNECT, .ret = MEDIA_IPC_FAILED};
148     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_DISCONNECT, &io, &para, DisConnectCallback);
149     if (ret != 0) {
150         MEDIA_ERR_LOG("Disconnect recorder server failed, ret=%u", ret);
151     }
152 }
153 
SetSourceCallback(void * owner,int code,IpcIo * reply)154 static int32_t SetSourceCallback(void *owner, int code, IpcIo *reply)
155 {
156     if (code != 0) {
157         MEDIA_ERR_LOG("callback error, code = %d", code);
158         return -1;
159     }
160 
161     if (owner == nullptr) {
162         return -1;
163     }
164     CallBackPara *para = (CallBackPara *)owner;
165     MEDIA_INFO_LOG("Callback, funcId = %d", para->funcId);
166     ReadInt32(reply, &(para->ret));
167     ReadInt32(reply, &(para->data));
168     return 0;
169 }
170 
SetVideoSource(VideoSourceType source,int32_t & sourceId)171 int32_t Recorder::RecorderClient::SetVideoSource(VideoSourceType source, int32_t &sourceId)
172 {
173     IpcIo io;
174     uint8_t tmpData[DEFAULT_IPC_SIZE];
175     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
176 
177     WriteRawData(&io, &source, sizeof(source));
178     CallBackPara para = {.funcId = REC_FUNC_SET_VIDEOSOURCE, .ret = MEDIA_IPC_FAILED};
179     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_VIDEOSOURCE, &io, &para, SetSourceCallback);
180     if (ret != 0) {
181         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
182     }
183     if (para.ret == 0) {
184         sourceId = para.data;
185     };
186     return para.ret;
187 }
188 
SetVideoEncoder(int32_t sourceId,VideoCodecFormat encoder)189 int32_t Recorder::RecorderClient::SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder)
190 {
191     IpcIo io;
192     uint8_t tmpData[DEFAULT_IPC_SIZE];
193     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
194     WriteInt32(&io, sourceId);
195     WriteRawData(&io, &encoder, sizeof(encoder));
196 
197     CallBackPara para = {.funcId = REC_FUNC_SET_VIDEOENCODER, .ret = MEDIA_IPC_FAILED};
198     int32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_VIDEOENCODER, &io, &para, SimpleCallback);
199     if (ret != 0) {
200         MEDIA_ERR_LOG("SetSource failed, ret=%d", ret);
201     }
202     return para.ret;
203 }
204 
SetVideoSize(int32_t sourceId,int32_t width,int32_t height)205 int32_t Recorder::RecorderClient::SetVideoSize(int32_t sourceId, int32_t width, int32_t height)
206 {
207     IpcIo io;
208     uint8_t tmpData[DEFAULT_IPC_SIZE];
209     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
210     MEDIA_DEBUG_LOG("RecorderClient SetVideoSize. (sourceId=%d, width=%d, height=%d)", sourceId, width, height);
211     WriteInt32(&io, sourceId);
212     WriteInt32(&io, width);
213     WriteInt32(&io, height);
214     CallBackPara para = {.funcId = REC_FUNC_SET_VIDEOSIZE, .ret = MEDIA_IPC_FAILED};
215     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_VIDEOSIZE, &io, &para, SimpleCallback);
216     if (ret != 0) {
217         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
218     }
219     return para.ret;
220 }
221 
SetVideoFrameRate(int32_t sourceId,int32_t frameRate)222 int32_t Recorder::RecorderClient::SetVideoFrameRate(int32_t sourceId, int32_t frameRate)
223 {
224     IpcIo io;
225     uint8_t tmpData[DEFAULT_IPC_SIZE];
226     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
227     WriteInt32(&io, sourceId);
228     WriteInt32(&io, frameRate);
229     CallBackPara para = {.funcId = REC_FUNC_SET_VIDEOFRAMERATE, .ret = MEDIA_IPC_FAILED};
230     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_VIDEOFRAMERATE, &io, &para, SimpleCallback);
231     if (ret != 0) {
232         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
233     }
234     return para.ret;
235 }
236 
SetVideoEncodingBitRate(int32_t sourceId,int32_t rate)237 int32_t Recorder::RecorderClient::SetVideoEncodingBitRate(int32_t sourceId, int32_t rate)
238 {
239     IpcIo io;
240     uint8_t tmpData[DEFAULT_IPC_SIZE];
241     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
242     WriteInt32(&io, sourceId);
243     WriteInt32(&io, rate);
244     CallBackPara para = {.funcId = REC_FUNC_SET_VIDEOENCODINGBITRATE, .ret = MEDIA_IPC_FAILED};
245     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_VIDEOENCODINGBITRATE, &io, &para, SimpleCallback);
246     if (ret != 0) {
247         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
248     }
249     return para.ret;
250 }
251 
SetCaptureRate(int32_t sourceId,double fps)252 int32_t Recorder::RecorderClient::SetCaptureRate(int32_t sourceId, double fps)
253 {
254     IpcIo io;
255     uint8_t tmpData[DEFAULT_IPC_SIZE];
256     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
257     WriteInt32(&io, sourceId);
258     WriteRawData(&io, &fps, sizeof(fps));
259     CallBackPara para = {.funcId = REC_FUNC_SET_CAPTURERATE, .ret = MEDIA_IPC_FAILED};
260     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_CAPTURERATE, &io, &para, SimpleCallback);
261     if (ret != 0) {
262         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
263     }
264     return para.ret;
265 }
266 
GetSurfaceCallback(void * owner,int code,IpcIo * io)267 static int32_t GetSurfaceCallback(void *owner, int code, IpcIo *io)
268 {
269     if (code != 0) {
270         MEDIA_ERR_LOG("callback error, code = %d", code);
271         return -1;
272     }
273 
274     if (owner == nullptr) {
275         return -1;
276     }
277     Surface **surface = (Surface **)owner;
278     *surface = OHOS::SurfaceImpl::GenericSurfaceByIpcIo(*io);
279     return 0;
280 }
281 
GetSurface(int32_t sourceId)282 std::shared_ptr<OHOS::Surface> Recorder::RecorderClient::GetSurface(int32_t sourceId)
283 {
284     IpcIo io;
285     uint8_t tmpData[DEFAULT_IPC_SIZE];
286     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
287     WriteInt32(&io, sourceId);
288     Surface *surface = nullptr;
289     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_GET_SURFACE, &io, &surface, GetSurfaceCallback);
290     if (ret != 0) {
291         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
292     }
293 
294     return std::shared_ptr<OHOS::Surface>(surface);
295 }
296 
SetAudioSource(AudioSourceType source,int32_t & sourceId)297 int32_t Recorder::RecorderClient::SetAudioSource(AudioSourceType source, int32_t &sourceId)
298 {
299     IpcIo io;
300     uint8_t tmpData[DEFAULT_IPC_SIZE];
301     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
302     WriteRawData(&io, &source, sizeof(source));
303     CallBackPara para = {.funcId = REC_FUNC_SET_AUDIOSOURCE, .ret = MEDIA_IPC_FAILED};
304     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_AUDIOSOURCE, &io, &para, SetSourceCallback);
305     if (ret != 0) {
306         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
307     }
308     if (para.ret == 0) {
309         sourceId = para.data;
310     };
311     return para.ret;
312 }
313 
SetAudioEncoder(int32_t sourceId,AudioCodecFormat encoder)314 int32_t Recorder::RecorderClient::SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder)
315 {
316     IpcIo io;
317     uint8_t tmpData[DEFAULT_IPC_SIZE];
318     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
319     WriteInt32(&io, sourceId);
320     WriteRawData(&io, &encoder, sizeof(encoder));
321     CallBackPara para = {.funcId = REC_FUNC_SET_AUDIOENCODER, .ret = MEDIA_IPC_FAILED};
322     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_AUDIOENCODER, &io, &para, SimpleCallback);
323     if (ret != 0) {
324         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
325     }
326     return para.ret;
327 }
328 
SetAudioSampleRate(int32_t sourceId,int32_t rate)329 int32_t Recorder::RecorderClient::SetAudioSampleRate(int32_t sourceId, int32_t rate)
330 {
331     IpcIo io;
332     uint8_t tmpData[DEFAULT_IPC_SIZE];
333     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
334     WriteInt32(&io, sourceId);
335     WriteInt32(&io, rate);
336     CallBackPara para = {.funcId = REC_FUNC_SET_AUDIOSAMPLERATE, .ret = MEDIA_IPC_FAILED};
337     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_AUDIOSAMPLERATE, &io, &para, SimpleCallback);
338     if (ret != 0) {
339         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
340     }
341     return para.ret;
342 }
343 
SetAudioChannels(int32_t sourceId,int32_t num)344 int32_t Recorder::RecorderClient::SetAudioChannels(int32_t sourceId, int32_t num)
345 {
346     IpcIo io;
347     uint8_t tmpData[DEFAULT_IPC_SIZE];
348     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
349     WriteInt32(&io, sourceId);
350     WriteInt32(&io, num);
351     CallBackPara para = {.funcId = REC_FUNC_SET_AUDIOCHANNELS, .ret = MEDIA_IPC_FAILED};
352     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_AUDIOCHANNELS, &io, &para, SimpleCallback);
353     if (ret != 0) {
354         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
355     }
356     return para.ret;
357 }
358 
SetAudioEncodingBitRate(int32_t sourceId,int32_t bitRate)359 int32_t Recorder::RecorderClient::SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate)
360 {
361     IpcIo io;
362     uint8_t tmpData[DEFAULT_IPC_SIZE];
363     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
364     WriteInt32(&io, sourceId);
365     WriteInt32(&io, bitRate);
366     CallBackPara para = {.funcId = REC_FUNC_SET_AUDIOENCODINGBITRATE, .ret = MEDIA_IPC_FAILED};
367     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_AUDIOENCODINGBITRATE, &io, &para, SimpleCallback);
368     if (ret != 0) {
369         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
370     }
371     return para.ret;
372 }
373 
SetMaxDuration(int32_t duration)374 int32_t Recorder::RecorderClient::SetMaxDuration(int32_t duration)
375 {
376     IpcIo io;
377     uint8_t tmpData[DEFAULT_IPC_SIZE];
378     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
379     WriteInt32(&io, duration);
380     CallBackPara para = {.funcId = REC_FUNC_SET_MAXDURATION, .ret = MEDIA_IPC_FAILED};
381     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_MAXDURATION, &io, &para, SimpleCallback);
382     if (ret != 0) {
383         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
384     }
385     return para.ret;
386 }
387 
SetOutputFormat(OutputFormatType format)388 int32_t Recorder::RecorderClient::SetOutputFormat(OutputFormatType format)
389 {
390     IpcIo io;
391     uint8_t tmpData[DEFAULT_IPC_SIZE];
392     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
393     WriteRawData(&io, &format, sizeof(format));
394 
395     CallBackPara para = {.funcId = REC_FUNC_SET_OUTPUTFORMAT, .ret = MEDIA_IPC_FAILED};
396     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_OUTPUTFORMAT, &io, &para, SimpleCallback);
397     if (ret != 0) {
398         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
399     }
400     return para.ret;
401 }
402 
SetOutputPath(const string & path)403 int32_t Recorder::RecorderClient::SetOutputPath(const string &path)
404 {
405     IpcIo io;
406     uint8_t tmpData[DEFAULT_IPC_SIZE];
407     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
408     WriteString(&io, path.c_str());
409     CallBackPara para = {.funcId = REC_FUNC_SET_OUTPUTPATH, .ret = MEDIA_IPC_FAILED};
410     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_OUTPUTPATH, &io, &para, SimpleCallback);
411     if (ret != 0) {
412         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
413     }
414     return para.ret;
415 }
416 
IsValidFileFd(int32_t fd)417 static int32_t IsValidFileFd(int32_t fd)
418 {
419     int flags = fcntl(fd, F_GETFL);
420     if (flags == -1) {
421         MEDIA_ERR_LOG("Fail to get File Status Flags err: %d", errno);
422         return ERR_INVALID_OPERATION;
423     }
424     // fd must be in read-write mode or write-only mode.
425     uint32_t flagsCheck = static_cast<uint32_t>(flags);
426     if ((flagsCheck & (O_RDWR | O_WRONLY)) == 0) {
427         MEDIA_ERR_LOG("File descriptor is not in read-write mode or write-only mode fd:%d flag:%x", fd, flagsCheck);
428         return ERR_INVALID_OPERATION;
429     }
430     return SUCCESS;
431 }
432 
SetOutputFile(int32_t fd)433 int32_t Recorder::RecorderClient::SetOutputFile(int32_t fd)
434 {
435     if (fd < 0 || IsValidFileFd(fd) != SUCCESS) {
436         MEDIA_ERR_LOG("Fail to get File Status Flags from fd: %d", fd);
437         return ERR_INVALID_PARAM;
438     }
439 
440     IpcIo io;
441     uint8_t tmpData[DEFAULT_IPC_SIZE];
442     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1);
443     WriteFileDescriptor(&io, fd);
444     CallBackPara para = {.funcId = REC_FUNC_SET_OUTPUTFILE, .ret = MEDIA_IPC_FAILED};
445     int32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_OUTPUTFILE, &io, &para, SimpleCallback);
446     if (ret != 0) {
447         MEDIA_ERR_LOG("SetOutputFile failed, ret=%d", ret);
448     }
449 
450     return para.ret;
451 }
452 
SetNextOutputFile(int32_t fd)453 int32_t Recorder::RecorderClient::SetNextOutputFile(int32_t fd)
454 {
455     if (fd < 0 || IsValidFileFd(fd) != SUCCESS) {
456         MEDIA_ERR_LOG("Fail to get File Status Flags from fd: %d", fd);
457         return ERR_INVALID_PARAM;
458     }
459 
460     IpcIo io;
461     uint8_t tmpData[DEFAULT_IPC_SIZE];
462     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1);
463     WriteFileDescriptor(&io, fd);
464     CallBackPara para = {.funcId = REC_FUNC_SET_NEXTOUTPUTFILE, .ret = MEDIA_IPC_FAILED};
465     int32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_NEXTOUTPUTFILE, &io, &para, SimpleCallback);
466     if (ret != 0) {
467         MEDIA_ERR_LOG("SetNextOutputFile failed, ret=%d", ret);
468     }
469 
470     return para.ret;
471 }
472 
SetMaxFileSize(int64_t size)473 int32_t Recorder::RecorderClient::SetMaxFileSize(int64_t size)
474 {
475     IpcIo io;
476     uint8_t tmpData[DEFAULT_IPC_SIZE];
477     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
478     WriteInt64(&io, size);
479     CallBackPara para = {.funcId = REC_FUNC_SET_MAXFILESIZE, .ret = MEDIA_IPC_FAILED};
480     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_MAXFILESIZE, &io, &para, SimpleCallback);
481     if (ret != 0) {
482         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
483     }
484     return para.ret;
485 }
486 
SetRecorderCallback(const std::shared_ptr<RecorderCallback> & callback)487 int32_t Recorder::RecorderClient::SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback)
488 {
489     if (callback == nullptr || callback.get() == nullptr) {
490         MEDIA_ERR_LOG("SetRecorderCallback callback is nullptr");
491         return ERR_INVALID_PARAM;
492     }
493 
494     callback_ = callback;
495     objectStub_.func = RecorderCallbackSvc;
496     objectStub_.args = callback_.get();
497     objectStub_.isRemote = false;
498     sid_.handle = IPC_INVALID_HANDLE;
499     sid_.token = SERVICE_TYPE_ANONYMOUS;
500     sid_.cookie = reinterpret_cast<uintptr_t>(&objectStub_);
501     IpcIo io;
502     uint8_t tmpData[DEFAULT_IPC_SIZE];
503     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 1);
504     bool writeRemote = WriteRemoteObject(&io, &sid_);
505     if (!writeRemote) {
506         return -1;
507     }
508     CallBackPara para = {.funcId = REC_FUNC_SET_RECORDERCALLBACK, .ret = MEDIA_IPC_FAILED};
509     int32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_RECORDERCALLBACK, &io, &para, SimpleCallback);
510     if (ret != 0) {
511         MEDIA_ERR_LOG("SetRecorderCallback failed, ret=%d", ret);
512         return -1;
513     }
514     if (para.ret != 0) {
515         callback_ = NULL;
516     }
517 
518     return para.ret;
519 }
520 
Prepare()521 int32_t Recorder::RecorderClient::Prepare()
522 {
523     IpcIo io;
524     uint8_t tmpData[DEFAULT_IPC_SIZE];
525     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
526     CallBackPara para = {.funcId = REC_FUNC_PREPARE, .ret = MEDIA_IPC_FAILED};
527     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_PREPARE, &io, &para, SimpleCallback);
528     if (ret != 0) {
529         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
530     }
531     return para.ret;
532 }
533 
Start()534 int32_t Recorder::RecorderClient::Start()
535 {
536     IpcIo io;
537     uint8_t tmpData[DEFAULT_IPC_SIZE];
538     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
539     CallBackPara para = {.funcId = REC_FUNC_START, .ret = MEDIA_IPC_FAILED};
540     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_START, &io, &para, SimpleCallback);
541     if (ret != 0) {
542         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
543     }
544     return para.ret;
545 }
546 
Pause()547 int32_t Recorder::RecorderClient::Pause()
548 {
549     IpcIo io;
550     uint8_t tmpData[DEFAULT_IPC_SIZE];
551     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
552     CallBackPara para = {.funcId = REC_FUNC_PAUSE, .ret = MEDIA_IPC_FAILED};
553     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_PAUSE, &io, &para, SimpleCallback);
554     if (ret != 0) {
555         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
556     }
557     return para.ret;
558 }
559 
Resume()560 int32_t Recorder::RecorderClient::Resume()
561 {
562     IpcIo io;
563     uint8_t tmpData[DEFAULT_IPC_SIZE];
564     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
565     CallBackPara para = {.funcId = REC_FUNC_RESUME, .ret = MEDIA_IPC_FAILED};
566     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_RESUME, &io, &para, SimpleCallback);
567     if (ret != 0) {
568         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
569     }
570     return para.ret;
571 }
572 
Stop(bool block)573 int32_t Recorder::RecorderClient::Stop(bool block)
574 {
575     IpcIo io;
576     uint8_t tmpData[DEFAULT_IPC_SIZE];
577     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
578     WriteBool(&io, block);
579     CallBackPara para = {.funcId = REC_FUNC_STOP, .ret = MEDIA_IPC_FAILED};
580     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_STOP, &io, &para, SimpleCallback);
581     if (ret != 0) {
582         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
583     }
584     return para.ret;
585 }
586 
Reset()587 int32_t Recorder::RecorderClient::Reset()
588 {
589     IpcIo io;
590     uint8_t tmpData[DEFAULT_IPC_SIZE];
591     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
592     CallBackPara para = {.funcId = REC_FUNC_RESET, .ret = MEDIA_IPC_FAILED};
593     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_RESET, &io, &para, SimpleCallback);
594     if (ret != 0) {
595         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
596     }
597     return para.ret;
598 }
599 
Release()600 int32_t Recorder::RecorderClient::Release()
601 {
602     IpcIo io;
603     uint8_t tmpData[DEFAULT_IPC_SIZE];
604     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
605     CallBackPara para = {.funcId = REC_FUNC_RELEASE, .ret = MEDIA_IPC_FAILED};
606     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_RELEASE, &io, &para, SimpleCallback);
607     if (ret != 0) {
608         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
609     }
610     return para.ret;
611 }
612 
SetFileSplitDuration(FileSplitType type,int64_t timestamp,uint32_t duration)613 int32_t Recorder::RecorderClient::SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration)
614 {
615     IpcIo io;
616     uint8_t tmpData[DEFAULT_IPC_SIZE];
617     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
618     WriteRawData(&io, &type, sizeof(type));
619     WriteInt64(&io, timestamp);
620     WriteUint32(&io, duration);
621     CallBackPara para = {.funcId = REC_FUNC_SET_FILESPLITDURATION, .ret = MEDIA_IPC_FAILED};
622     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_FILESPLITDURATION, &io, &para, SimpleCallback);
623     if (ret != 0) {
624         MEDIA_ERR_LOG("SetSource failed, ret=%u", ret);
625     }
626     return para.ret;
627 }
628 
SetParameter(int32_t sourceId,const Format & format)629 int32_t Recorder::RecorderClient::SetParameter(int32_t sourceId, const Format &format)
630 {
631     return -1;
632 }
633 
SetDataSource(DataSourceType source,int32_t & sourceId)634 int32_t Recorder::RecorderClient::SetDataSource(DataSourceType source, int32_t &sourceId)
635 {
636     IpcIo io;
637     uint8_t tmpData[DEFAULT_IPC_SIZE];
638     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
639     WriteRawData(&io, &source, sizeof(source));
640     CallBackPara para = {.funcId = REC_FUNC_SET_DATASOURCE, .ret = MEDIA_IPC_FAILED};
641     uint32_t ret = proxy_->Invoke(proxy_, REC_FUNC_SET_DATASOURCE, &io, &para, SetSourceCallback);
642     if (ret != 0) {
643         MEDIA_ERR_LOG("SetDataSource failed, ret=%u", ret);
644     }
645     if (para.ret == 0) {
646         sourceId = para.data;
647     };
648     return para.ret;
649 }
650 } /* namespace Media */
651 } /* namespace OHOS */