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 #include "recorder_service.h"
16 #include "media_log.h"
17 #include "surface_impl.h"
18 #include "rpc_errno.h"
19 
20 namespace OHOS {
21 namespace Media {
GetInstance()22 RecorderClientMng *RecorderClientMng::GetInstance()
23 {
24     static RecorderClientMng mng;
25     return &mng;
26 }
27 
GetRecorder(pid_t pid)28 RecorderImpl *RecorderClientMng::GetRecorder(pid_t pid)
29 {
30     return (pid == client_) ? rec_ : nullptr;
31 }
32 
AcceptClient(pid_t pid)33 bool RecorderClientMng::AcceptClient(pid_t pid)
34 {
35     if (client_ == -1) {
36         rec_ = new RecorderImpl;
37         client_ = pid;
38         return true;
39     }
40     return false;
41 }
42 
DropClient(pid_t pid)43 void RecorderClientMng::DropClient(pid_t pid)
44 {
45     if (pid == client_) {
46         delete rec_;
47         rec_ = nullptr;
48         client_ = -1;
49     }
50 }
51 
Dispatch(int32_t funcId,pid_t pid,IpcIo * req,IpcIo * reply)52 void RecorderClientMng::Dispatch(int32_t funcId, pid_t pid, IpcIo *req, IpcIo *reply)
53 {
54     if (funcId == REC_FUNC_CONNECT) {
55         AcceptClient(pid);
56         return;
57     }
58     auto recorder = GetRecorder(pid);
59     if (recorder == nullptr) {
60         MEDIA_ERR_LOG("Cannot find client object.(pid=%d)", pid);
61         WriteInt32(reply, MEDIA_IPC_FAILED);
62         return;
63     }
64     switch (funcId) {
65         case REC_FUNC_DISCONNECT:
66             DropClient(pid);
67             break;
68         case REC_FUNC_SET_VIDEOSOURCE: {
69             VideoSourceType *src = (VideoSourceType*)ReadRawData(req, sizeof(VideoSourceType));
70             int32_t srcId;
71             int32_t ret = recorder->SetVideoSource(*src, srcId);
72             WriteInt32(reply, ret);
73             WriteInt32(reply, srcId);
74             break;
75         }
76         case REC_FUNC_SET_VIDEOENCODER: {
77             int32_t src;
78             ReadInt32(req, &src);
79             VideoCodecFormat *enc = (VideoCodecFormat*)ReadRawData(req, sizeof(VideoCodecFormat));
80             int32_t ret = recorder->SetVideoEncoder(src, *enc);
81             WriteInt32(reply, ret);
82             break;
83         }
84         case REC_FUNC_SET_VIDEOSIZE: {
85             int32_t src;
86             ReadInt32(req, &src);
87             int32_t width;
88             ReadInt32(req, &width);
89             int32_t height;
90             ReadInt32(req, &height);
91             int32_t ret = recorder->SetVideoSize(src, width, height);
92             WriteInt32(reply, ret);
93             break;
94         }
95         case REC_FUNC_SET_VIDEOFRAMERATE: {
96             int32_t sourceId;
97             ReadInt32(req, &sourceId);
98             int32_t frameRate;
99             ReadInt32(req, &frameRate);
100             int32_t ret = recorder->SetVideoFrameRate(sourceId, frameRate);
101             WriteInt32(reply, ret);
102             break;
103         }
104         case REC_FUNC_SET_VIDEOENCODINGBITRATE: {
105             int32_t sourceId;
106             ReadInt32(req, &sourceId);
107             int32_t rate;
108             ReadInt32(req, &rate);
109             int32_t ret = recorder->SetVideoEncodingBitRate(sourceId, rate);
110             WriteInt32(reply, ret);
111             break;
112         }
113         case REC_FUNC_SET_CAPTURERATE: {
114             int32_t sourceId;
115             ReadInt32(req, &sourceId);
116             double *fps = (double*)ReadRawData(req, sizeof(double));
117             int32_t ret = recorder->SetCaptureRate(sourceId, *fps);
118             WriteInt32(reply, ret);
119             break;
120         }
121         case REC_FUNC_GET_SURFACE: {
122             int32_t sourceId;
123             ReadInt32(req, &sourceId);
124             std::shared_ptr<OHOS::Surface> surface = recorder->GetSurface(sourceId);
125             if (surface != nullptr) {
126                 dynamic_cast<OHOS::SurfaceImpl *>(surface.get())->WriteIoIpcIo(*reply);
127             }
128             break;
129         }
130         case REC_FUNC_SET_AUDIOSOURCE: {
131             AudioSourceType *src = (AudioSourceType *)ReadRawData(req, sizeof(AudioSourceType));
132             int32_t srcId;
133             int32_t ret = recorder->SetAudioSource(*src, srcId);
134             WriteInt32(reply, ret);
135             WriteInt32(reply, srcId);
136             break;
137         }
138         case REC_FUNC_SET_AUDIOENCODER: {
139             int32_t sourceId;
140             ReadInt32(req, &sourceId);
141             AudioCodecFormat *enc = (AudioCodecFormat *)ReadRawData(req, sizeof(AudioCodecFormat));
142             int32_t ret = recorder->SetAudioEncoder(sourceId, *enc);
143             WriteInt32(reply, ret);
144             break;
145         }
146         case REC_FUNC_SET_AUDIOSAMPLERATE: {
147             int32_t sourceId;
148             ReadInt32(req, &sourceId);
149             int32_t rate;
150             ReadInt32(req, &rate);
151             int32_t ret = recorder->SetAudioSampleRate(sourceId, rate);
152             WriteInt32(reply, ret);
153             break;
154         }
155         case REC_FUNC_SET_AUDIOCHANNELS: {
156             int32_t sourceId;
157             ReadInt32(req, &sourceId);
158             int32_t num;
159             ReadInt32(req, &num);
160             int32_t ret = recorder->SetAudioChannels(sourceId, num);
161             WriteInt32(reply, ret);
162             break;
163         }
164         case REC_FUNC_SET_AUDIOENCODINGBITRATE: {
165             int32_t sourceId;
166             ReadInt32(req, &sourceId);
167             int32_t bitRate;
168             ReadInt32(req, &bitRate);
169             int32_t ret = recorder->SetAudioEncodingBitRate(sourceId, bitRate);
170             WriteInt32(reply, ret);
171             break;
172         }
173         case REC_FUNC_SET_MAXDURATION: {
174             int32_t duration;
175             ReadInt32(req, &duration);
176             int32_t ret = recorder->SetMaxDuration(duration);
177             WriteInt32(reply, ret);
178             break;
179         }
180         case REC_FUNC_SET_OUTPUTFORMAT: {
181             OutputFormatType *format = (OutputFormatType *)ReadRawData(req, sizeof(OutputFormatType));
182             int32_t ret = recorder->SetOutputFormat(*format);
183             WriteInt32(reply, ret);
184             break;
185         }
186         case REC_FUNC_SET_OUTPUTPATH: {
187             size_t strSize;
188             char *path = (char *)ReadString(req, &strSize);
189             int32_t ret = recorder->SetOutputPath(string(path));
190             WriteInt32(reply, ret);
191             break;
192         }
193         case REC_FUNC_SET_OUTPUTFILE: {
194             int32_t fd = ReadFileDescriptor(req);
195             int32_t ret = recorder->SetOutputFile(fd);
196             WriteInt32(reply, ret);
197             break;
198         }
199         case REC_FUNC_SET_NEXTOUTPUTFILE: {
200             int32_t fd = ReadFileDescriptor(req);
201             int32_t ret = recorder->SetNextOutputFile(fd);
202             WriteInt32(reply, ret);
203             break;
204         }
205         case REC_FUNC_SET_MAXFILESIZE: {
206             int64_t size;
207             ReadInt64(req, &size);
208             int32_t ret = recorder->SetMaxFileSize(size);
209             WriteInt32(reply, ret);
210             break;
211         }
212         case REC_FUNC_SET_RECORDERCALLBACK: {
213             SvcIdentity svc;
214             ReadRemoteObject(req, &svc);
215             std::shared_ptr<RecorderCallbackClient> client(new RecorderCallbackClient(&svc));
216             int32_t ret = recorder->SetRecorderCallback(client);
217             WriteInt32(reply, ret);
218             break;
219         }
220         case REC_FUNC_PREPARE: {
221             int32_t ret = recorder->Prepare();
222             WriteInt32(reply, ret);
223             break;
224         }
225         case REC_FUNC_START: {
226             int32_t ret = recorder->Start();
227             WriteInt32(reply, ret);
228             break;
229         }
230         case REC_FUNC_PAUSE: {
231             int32_t ret = recorder->Pause();
232             WriteInt32(reply, ret);
233             break;
234         }
235         case REC_FUNC_RESUME: {
236             int32_t ret = recorder->Resume();
237             WriteInt32(reply, ret);
238             break;
239         }
240         case REC_FUNC_STOP: {
241             bool block;
242             ReadBool(req, &block);
243             int32_t ret = recorder->Stop(block);
244             WriteInt32(reply, ret);
245             break;
246         }
247         case REC_FUNC_RESET: {
248             int32_t ret = recorder->Reset();
249             WriteInt32(reply, ret);
250             break;
251         }
252         case REC_FUNC_RELEASE: {
253             int32_t ret = recorder->Release();
254             WriteInt32(reply, ret);
255             break;
256         }
257         case REC_FUNC_SET_FILESPLITDURATION: {
258             FileSplitType *type = (FileSplitType *)ReadRawData(req, sizeof(FileSplitType));
259             int64_t timestamp;
260             ReadInt64(req, &timestamp);
261             uint32_t duration;
262             ReadUint32(req, &duration);
263             int32_t ret = recorder->SetFileSplitDuration(*type, timestamp, duration);
264             WriteInt32(reply, ret);
265             break;
266         }
267         case REC_FUNC_SET_PARAMETER: {
268             break;
269         }
270         case REC_FUNC_SET_DATASOURCE: {
271             DataSourceType *src = (DataSourceType *)ReadRawData(req, sizeof(DataSourceType));
272             int32_t srcId;
273             int32_t ret = recorder->SetDataSource(*src, srcId);
274             WriteInt32(reply, ret);
275             WriteInt32(reply, srcId);
276             break;
277         }
278         default:
279             break;
280     }
281 }
282 
OnError(int32_t errorType,int32_t errorCode)283 void RecorderCallbackClient::OnError(int32_t errorType, int32_t errorCode)
284 {
285     IpcIo io;
286     uint8_t tmpData[DEFAULT_IPC_SIZE];
287     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
288     WriteInt32(&io, errorType);
289     WriteInt32(&io, errorCode);
290     MessageOption option;
291     MessageOptionInit(&option);
292     option.flags = TF_OP_ASYNC;
293     int32_t ret = SendRequest(sid_, REC_ANONYMOUS_FUNC_ON_ERROR, &io, nullptr, option, nullptr);
294     if (ret != ERR_NONE) {
295         MEDIA_ERR_LOG("Recorder server SendRequest OnError failed.(ret=%d)", ret);
296     }
297 }
298 
OnInfo(int32_t type,int32_t extra)299 void RecorderCallbackClient::OnInfo(int32_t type, int32_t extra)
300 {
301     IpcIo io;
302     uint8_t tmpData[DEFAULT_IPC_SIZE];
303     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
304     WriteInt32(&io, type);
305     WriteInt32(&io, extra);
306     MessageOption option;
307     MessageOptionInit(&option);
308     option.flags = TF_OP_ASYNC;
309     int32_t ret = SendRequest(sid_, REC_ANONYMOUS_FUNC_ON_INFO, &io, nullptr, option, nullptr);
310     if (ret != ERR_NONE) {
311         MEDIA_ERR_LOG("Recorder server SendRequest OnInfo failed.(ret=%d)", ret);
312     }
313 }
314 } // namespace Media
315 } // namespace OHOS