• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "player_server.h"
17 #include "media_info.h"
18 #include "media_log.h"
19 #include "format.h"
20 #include "player_type.h"
21 #include "source.h"
22 #include "surface.h"
23 #include "surface_impl.h"
24 #include "player_factory.h"
25 extern "C"
26 {
27 #include "codec_interface.h"
28 #include "pthread.h"
29 #include <sys/prctl.h>
30 #include "securec.h"
31 }
32 
33 #define READ_LEN  (1024)
34 
35 typedef struct TagIdleBuffer {
36     size_t idx;
37     size_t offset;
38     size_t size;
39 } IdleBuffer;
40 
41 namespace OHOS {
42 namespace Media {
PlayerServerRequestHandle(int funcId,void * origin,IpcIo * req,IpcIo * reply)43 void PlayerServer::PlayerServerRequestHandle(int funcId, void *origin, IpcIo *req, IpcIo *reply)
44 {
45     switch (funcId) {
46         case PLAYER_SERVER_SET_SOURCE:
47             PlayerServer::GetInstance()->SetSource(req, reply);
48             break;
49         case PLAYER_SERVER_PREPARE:
50             PlayerServer::GetInstance()->Prepare(req, reply);
51             break;
52         case PLAYER_SERVER_PLAY:
53             PlayerServer::GetInstance()->Play(req, reply);
54             break;
55         case PLAYER_SERVER_IS_PLAYING:
56             PlayerServer::GetInstance()->IsPlaying(req, reply);
57             break;
58         case PLAYER_SERVER_PAUSE:
59             PlayerServer::GetInstance()->Pause(req, reply);
60             break;
61         case PLAYER_SERVER_STOP:
62             PlayerServer::GetInstance()->Stop(req, reply);
63             break;
64         case PLAYER_SERVER_REWIND:
65             PlayerServer::GetInstance()->Rewind(req, reply);
66             break;
67         case PLAYER_SERVER_SET_VOLUME:
68             PlayerServer::GetInstance()->SetVolume(req, reply);
69             break;
70         case PLAYER_SERVER_SET_VIDEO_SURFACE:
71             PlayerServer::GetInstance()->SetSurface(req, reply);
72             break;
73         case PLAYER_SERVER_ENABLE_SINGLE_LOOPING:
74             PlayerServer::GetInstance()->SetLoop(req, reply);
75             break;
76         case PLAYER_SERVER_IS_SINGLE_LOOPING:
77             PlayerServer::GetInstance()->IsSingleLooping(req, reply);
78             break;
79         case PLAYER_SERVER_GET_CURRENT_TIME:
80             PlayerServer::GetInstance()->GetCurrentPosition(req, reply);
81             break;
82         case PLAYER_SERVER_GET_DURATION:
83             PlayerServer::GetInstance()->GetDuration(req, reply);
84             break;
85         case PLAYER_SERVER_GET_VIDEO_WIDTH:
86             PlayerServer::GetInstance()->GetVideoWidth(req, reply);
87             break;
88         case PLAYER_SERVER_GET_VIDEO_HEIGHT:
89             PlayerServer::GetInstance()->GetVideoHeight(req, reply);
90             break;
91         case PLAYER_SERVER_RESET:
92             PlayerServer::GetInstance()->Reset(req, reply);
93             break;
94         case PLAYER_SERVER_RELEASE:
95             PlayerServer::GetInstance()->Release(req, reply);
96             break;
97         case PLAYER_SERVER_SET_PLAYER_CALLBACK:
98             PlayerServer::GetInstance()->SetPlayerCallback(req, reply);
99             break;
100         case PLAYER_SERVER_GET_STATE:
101             PlayerServer::GetInstance()->GetPlayerState(req, reply);
102             break;
103         case PLAYER_SERVER_SET_SPEED:
104             PlayerServer::GetInstance()->SetPlaybackSpeed(req, reply);
105             break;
106         case PLAYER_SERVER_GET_SPEED:
107             PlayerServer::GetInstance()->GetPlaybackSpeed(req, reply);
108             break;
109         case PLAYER_SERVER_SET_PARAMETER:
110             PlayerServer::GetInstance()->SetParameter(req, reply);
111             break;
112         case PLAYER_SERVER_SET_AUDIO_STREAM_TYPE:
113             PlayerServer::GetInstance()->SetAudioStreamType(req, reply);
114             break;
115         case PLAYER_SERVER_GET_AUDIO_STREAM_TYPE:
116             PlayerServer::GetInstance()->GetAudioStreamType(req, reply);
117             break;
118         default:
119             MEDIA_ERR_LOG("code not support: %d", funcId);
120             break;
121     }
122 }
123 
PlayerServerInit()124 int32_t PlayerServer::PlayerServerInit()
125 {
126     return 0;
127 }
128 
129 class ServerStreamSource : public StreamSource {
130 public:
131     ServerStreamSource(void);
132     virtual ~ServerStreamSource(void);
133     void OnBufferAvailable(size_t index, size_t offset, size_t size);
134     void SetStreamCallback(const std::shared_ptr<StreamCallback> &callback);
135     uint8_t *GetBufferAddress(size_t idx);
136     void QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags);
137     int GetAvailableBuffer(IdleBuffer* buffer);
138     bool threadRuning;
139 
140 private:
141     std::weak_ptr<StreamCallback> m_callBack;
142     std::vector<IdleBuffer> aviableBuffer;
143     pthread_mutex_t m_mutex;
144 };
145 
146 struct StreamThreadControl {
147     pthread_t process;
148     pthread_mutex_t mutex;
149 };
150 StreamThreadControl g_streamThreadControl;
151 
ServerStreamSource(void)152 ServerStreamSource::ServerStreamSource(void)
153 {
154     aviableBuffer.clear();
155     pthread_mutex_init(&m_mutex, nullptr);
156 }
157 
~ServerStreamSource(void)158 ServerStreamSource::~ServerStreamSource(void)
159 {
160     aviableBuffer.clear();
161     pthread_mutex_destroy(&m_mutex);
162 }
163 
SetStreamCallback(const std::shared_ptr<StreamCallback> & callback)164 void ServerStreamSource::SetStreamCallback(const std::shared_ptr<StreamCallback> &callback)
165 {
166     m_callBack = callback;
167 }
168 
GetBufferAddress(size_t idx)169 uint8_t *ServerStreamSource::GetBufferAddress(size_t idx)
170 {
171     std::shared_ptr<StreamCallback> callback = m_callBack.lock();
172     if (callback == nullptr) {
173         return nullptr;
174     }
175     return callback->GetBuffer(idx);
176 }
177 
QueueBuffer(size_t index,size_t offset,size_t size,int64_t timestampUs,uint32_t flags)178 void ServerStreamSource::QueueBuffer(size_t index, size_t offset, size_t size, int64_t timestampUs, uint32_t flags)
179 {
180     std::shared_ptr<StreamCallback> callback = m_callBack.lock();
181     if (callback == nullptr) {
182         return;
183     }
184     callback->QueueBuffer(index, offset, size, timestampUs, flags);
185 }
186 
OnBufferAvailable(size_t index,size_t offset,size_t size)187 void ServerStreamSource::OnBufferAvailable(size_t index, size_t offset, size_t size)
188 {
189     IdleBuffer buffer;
190     pthread_mutex_lock(&m_mutex);
191     buffer.idx = index;
192     buffer.offset = offset;
193     buffer.size = size;
194     aviableBuffer.push_back(buffer);
195     pthread_mutex_unlock(&m_mutex);
196 }
197 
GetAvailableBuffer(IdleBuffer * buffer)198 int ServerStreamSource::GetAvailableBuffer(IdleBuffer* buffer)
199 {
200     pthread_mutex_lock(&m_mutex);
201     if (aviableBuffer.empty()) {
202         pthread_mutex_unlock(&m_mutex);
203         return -1;
204     }
205     *buffer = aviableBuffer[0];
206     aviableBuffer.erase(aviableBuffer.begin());
207     pthread_mutex_unlock(&m_mutex);
208     return 0;
209 }
210 
streamProcess(void * arg)211 static void* streamProcess(void* arg)
212 {
213     IdleBuffer buffer;
214     int ret;
215     uint8_t *data = nullptr;
216     int32_t readLen;
217     ServerStreamSource *stream = (ServerStreamSource *)arg;
218     SurfaceBuffer* acquireBuffer = nullptr;
219     prctl(PR_SET_NAME, "StreamProc_server", 0, 0, 0);
220     MEDIA_INFO_LOG("[%s %d]", __func__, __LINE__);
221     int sleepTime = 20000;
222     while (true) {
223         pthread_mutex_lock(&g_streamThreadControl.mutex);
224         if (stream->threadRuning == false) {
225             pthread_mutex_unlock(&g_streamThreadControl.mutex);
226             break;
227         }
228         pthread_mutex_unlock(&g_streamThreadControl.mutex);
229         ret = stream->GetAvailableBuffer(&buffer);
230         if (ret != 0) {
231             usleep(sleepTime);
232             continue;
233         }
234         data = stream->GetBufferAddress(buffer.idx);
235         if (data == nullptr) {
236             break;
237         }
238         while (true) {
239             pthread_mutex_lock(&g_streamThreadControl.mutex);
240             if (stream->threadRuning == false) {
241                 pthread_mutex_unlock(&g_streamThreadControl.mutex);
242                 break;
243             }
244             pthread_mutex_unlock(&g_streamThreadControl.mutex);
245             acquireBuffer = stream->GetSurface()->AcquireBuffer();
246             if (acquireBuffer != nullptr) {
247                 ret = acquireBuffer->GetInt32(0, readLen);
248                 if (ret != 0) {
249                     MEDIA_ERR_LOG("[%s,%d] acquireBuffer GetInt32 failed", __func__, __LINE__);
250                     readLen = 0;
251                 }
252                 if (readLen != 0) {
253                     void* acquireBufVirAddr = acquireBuffer->GetVirAddr();
254                     if (acquireBufVirAddr != nullptr) {
255                         if (buffer.size < readLen) {
256                             MEDIA_ERR_LOG("[%s,%d] error:buffer.size < readLen", __func__, __LINE__);
257                         }
258                         if (memcpy_s(data + buffer.offset, buffer.size, acquireBufVirAddr, readLen) != EOK) {
259                             MEDIA_ERR_LOG("memcpy_s error!");
260                         }
261                     } else {
262                         MEDIA_ERR_LOG("[%s,%d]", __func__, __LINE__);
263                     }
264                 }
265                 stream->GetSurface()->ReleaseBuffer(acquireBuffer);
266                 break;
267             } else {
268                 usleep(sleepTime);
269                 continue;
270             }
271         }
272         if (readLen > 0) {
273             int flags = 8;
274             stream->QueueBuffer(buffer.idx, buffer.offset, readLen, 0, flags);
275         } else {
276             int flags = 4;
277             stream->QueueBuffer(buffer.idx, buffer.offset, readLen, 0, flags);
278             break;
279         }
280     }
281     return NULL;
282 }
283 
SurfaceRequestHandler(const IpcContext * context,void * ipcMsg,IpcIo * io,void * arg)284 int32_t SurfaceRequestHandler(const IpcContext* context, void* ipcMsg, IpcIo* io, void* arg)
285 {
286     Surface* surface = (Surface*)arg;
287     SurfaceImpl* liteSurface = reinterpret_cast<SurfaceImpl*>(surface);
288     liteSurface->DoIpcMsg(ipcMsg, io);
289     return 0;
290 }
291 
SetStreamSource(IpcIo * reply)292 void PlayerServer::SetStreamSource(IpcIo *reply)
293 {
294     MEDIA_INFO_LOG("process in");
295     stream_ = std::make_shared<ServerStreamSource>();
296     Format formats;
297     formats.PutStringValue(CODEC_MIME, MIME_AUDIO_AAC);
298     Source streamSource(stream_, formats);
299     int32_t ret = player_->SetSource(streamSource);
300     IpcIoPushInt32(reply, ret);
301     if (ret == 0) {
302         Surface* surface = Surface::CreateSurface();
303         surface->SetUsage(BUFFER_CONSUMER_USAGE_HARDWARE);
304         surface->SetSize(READ_LEN);
305         if (sid_ == nullptr) {
306             sid_ = new SvcIdentity();
307         }
308         ret = RegisterIpcCallback(SurfaceRequestHandler, 0, IPC_WAIT_FOREVER, sid_, surface);
309         if (ret != LITEIPC_OK) {
310             MEDIA_ERR_LOG("RegisterIpcCallback failed.");
311         }
312         stream_->SetSurface(surface);
313         IpcIoPushSvc(reply, sid_);
314         pthread_attr_t attr;
315         pthread_mutex_init(&g_streamThreadControl.mutex, nullptr);
316         ServerStreamSource* serverStream = reinterpret_cast<ServerStreamSource*>(stream_.get());
317         serverStream->threadRuning = true;
318         pthread_attr_init(&attr);
319         pthread_create(&g_streamThreadControl.process, &attr, streamProcess, stream_.get());
320     }
321 }
322 
SetSource(IpcIo * req,IpcIo * reply)323 void PlayerServer::SetSource(IpcIo *req, IpcIo *reply)
324 {
325     MEDIA_INFO_LOG("process in");
326     int32_t sourceType = IpcIoPopInt32(req);
327 
328     if (player_ == nullptr) {
329         MEDIA_INFO_LOG("player nullptr");
330         player_ = PlayerFactory::CreatePlayer();
331     }
332 
333     int32_t state = 0;
334     /* only support set source at state idle */
335     if (player_ == nullptr || player_->GetPlayerState(state) != 0 || state != static_cast<int32_t> (PLAYER_IDLE)) {
336         IpcIoPushInt32(reply, -1);
337         return;
338     }
339     switch ((SourceType)sourceType) {
340         case SourceType::SOURCE_TYPE_URI: {
341             size_t size;
342             char* str = (char*)IpcIoPopString(req, &size);
343             std::string uri(str);
344             Source sourceUri(uri);
345             IpcIoPushInt32(reply, player_->SetSource(sourceUri));
346             break;
347         }
348         case SourceType::SOURCE_TYPE_FD:
349             MEDIA_ERR_LOG("unsupport now: SOURCE_TYPE_FD");
350             IpcIoPushInt32(reply, -1);
351             break;
352         case SourceType::SOURCE_TYPE_STREAM:{
353             SetStreamSource(reply);
354             break;
355         }
356         default:
357             break;
358     }
359     MEDIA_INFO_LOG("PlayerServer::SetSource out");
360 }
361 
Prepare(IpcIo * req,IpcIo * reply)362 void PlayerServer::Prepare(IpcIo *req, IpcIo *reply)
363 {
364     MEDIA_INFO_LOG("process in");
365     if (player_ != nullptr) {
366         IpcIoPushInt32(reply, player_->Prepare());
367         return;
368     }
369     IpcIoPushInt32(reply, -1);
370 }
371 
Play(IpcIo * req,IpcIo * reply)372 void PlayerServer::Play(IpcIo *req, IpcIo *reply)
373 {
374     MEDIA_INFO_LOG("process in");
375     if (player_ != nullptr) {
376         IpcIoPushInt32(reply, player_->Play());
377         return;
378     }
379     IpcIoPushInt32(reply, -1);
380 }
381 
IsPlaying(IpcIo * req,IpcIo * reply)382 void PlayerServer::IsPlaying(IpcIo *req, IpcIo *reply)
383 {
384     MEDIA_INFO_LOG("process in");
385     if (player_ != nullptr) {
386         IpcIoPushBool(reply, player_->IsPlaying());
387         return;
388     }
389     IpcIoPushBool(reply, false);
390 }
391 
Pause(IpcIo * req,IpcIo * reply)392 void PlayerServer::Pause(IpcIo *req, IpcIo *reply)
393 {
394     MEDIA_INFO_LOG("process in");
395     if (player_ != nullptr) {
396         IpcIoPushInt32(reply, player_->Pause());
397         return;
398     }
399     IpcIoPushInt32(reply, -1);
400 }
401 
Stop(IpcIo * req,IpcIo * reply)402 void PlayerServer::Stop(IpcIo *req, IpcIo *reply)
403 {
404     MEDIA_INFO_LOG("process in");
405     if (player_ != nullptr) {
406         int32_t ret = player_->Stop();
407         if (stream_.get() != nullptr) {
408             ServerStreamSource* serverStream = reinterpret_cast<ServerStreamSource*>(stream_.get());
409             pthread_mutex_lock(&g_streamThreadControl.mutex);
410             serverStream->threadRuning = false;
411             pthread_mutex_unlock(&g_streamThreadControl.mutex);
412         }
413         IpcIoPushInt32(reply, ret);
414         return;
415     }
416     IpcIoPushInt32(reply, -1);
417 }
418 
Rewind(IpcIo * req,IpcIo * reply)419 void PlayerServer::Rewind(IpcIo *req, IpcIo *reply)
420 {
421     MEDIA_INFO_LOG("process in");
422     int64_t mSecond = IpcIoPopInt64(req);
423     int32_t mode = IpcIoPopInt32(req);
424     if (player_ != nullptr) {
425         IpcIoPushInt32(reply, player_->Rewind(mSecond, mode));
426         return;
427     }
428     IpcIoPushInt32(reply, -1);
429 }
430 
SetVolume(IpcIo * req,IpcIo * reply)431 void PlayerServer::SetVolume(IpcIo *req, IpcIo *reply)
432 {
433     MEDIA_INFO_LOG("process in");
434     uint32_t size;
435     float leftVolume = *static_cast<float *>(IpcIoPopFlatObj(req, &size));
436     float rightVolume = *static_cast<float *>(IpcIoPopFlatObj(req, &size));
437     if (player_ != nullptr) {
438         IpcIoPushInt32(reply, player_->SetVolume(leftVolume, rightVolume));
439         return;
440     }
441     IpcIoPushInt32(reply, -1);
442 }
443 
SetSurface(IpcIo * req,IpcIo * reply)444 void PlayerServer::SetSurface(IpcIo *req, IpcIo *reply)
445 {
446     MEDIA_INFO_LOG("process in");
447     size_t size;
448     char* str_x = (char*)IpcIoPopString(req, &size);
449     char* str_y = (char*)IpcIoPopString(req, &size);
450     char* str_width = (char*)IpcIoPopString(req, &size);
451     char* str_height = (char*)IpcIoPopString(req, &size);
452     Surface* surface = Surface::CreateSurface();
453     if (surface != nullptr) {
454         surface->SetUserData("region_position_x", std::string(str_x));
455         surface->SetUserData("region_position_y", std::string(str_y));
456         surface->SetUserData("region_width", std::string(str_width));
457         surface->SetUserData("region_height", std::string(str_height));
458         if (player_ != nullptr) {
459             IpcIoPushInt32(reply, player_->SetSurface(surface));
460             return;
461         }
462     }
463     IpcIoPushInt32(reply, -1);
464 }
465 
SetLoop(IpcIo * req,IpcIo * reply)466 void PlayerServer::SetLoop(IpcIo *req, IpcIo *reply)
467 {
468     MEDIA_INFO_LOG("process in");
469     bool loop = IpcIoPopBool(req);
470     if (player_ != nullptr) {
471         IpcIoPushInt32(reply, player_->SetLoop(loop));
472         return;
473     }
474     IpcIoPushInt32(reply, -1);
475 }
476 
IsSingleLooping(IpcIo * req,IpcIo * reply)477 void PlayerServer::IsSingleLooping(IpcIo *req, IpcIo *reply)
478 {
479     MEDIA_INFO_LOG("process in");
480     if (player_ != nullptr) {
481         IpcIoPushBool(reply, player_->IsSingleLooping());
482         return;
483     }
484     IpcIoPushBool(reply, false);
485 }
486 
GetCurrentPosition(IpcIo * req,IpcIo * reply)487 void PlayerServer::GetCurrentPosition(IpcIo *req, IpcIo *reply)
488 {
489     int64_t time = 0;
490     if (player_ != nullptr) {
491         IpcIoPushInt32(reply, player_->GetCurrentPosition(time));
492         IpcIoPushInt64(reply, time);
493         return;
494     }
495     IpcIoPushInt32(reply, -1);
496     IpcIoPushInt64(reply, time);
497 }
498 
GetDuration(IpcIo * req,IpcIo * reply)499 void PlayerServer::GetDuration(IpcIo *req, IpcIo *reply)
500 {
501     MEDIA_INFO_LOG("process in");
502     int64_t duration = 0;
503     if (player_ != nullptr) {
504         IpcIoPushInt32(reply, player_->GetDuration(duration));
505         IpcIoPushInt64(reply, duration);
506         return;
507     }
508     IpcIoPushInt32(reply, -1);
509     IpcIoPushInt64(reply, duration);
510 }
511 
GetVideoWidth(IpcIo * req,IpcIo * reply)512 void PlayerServer::GetVideoWidth(IpcIo *req, IpcIo *reply)
513 {
514     MEDIA_INFO_LOG("process in");
515     int32_t width = 0;
516     if (player_ != nullptr) {
517         IpcIoPushInt32(reply, player_->GetVideoWidth(width));
518         IpcIoPushInt32(reply, width);
519         return;
520     }
521     IpcIoPushInt32(reply, -1);
522     IpcIoPushInt32(reply, width);
523 }
524 
GetVideoHeight(IpcIo * req,IpcIo * reply)525 void PlayerServer::GetVideoHeight(IpcIo *req, IpcIo *reply)
526 {
527     MEDIA_INFO_LOG("process in");
528     int32_t hight = 0;
529     if (player_ != nullptr) {
530         IpcIoPushInt32(reply, player_->GetVideoHeight(hight));
531         IpcIoPushInt32(reply, hight);
532         return;
533     }
534     IpcIoPushInt32(reply, -1);
535     IpcIoPushInt32(reply, hight);
536 }
537 
Reset(IpcIo * req,IpcIo * reply)538 void PlayerServer::Reset(IpcIo *req, IpcIo *reply)
539 {
540     MEDIA_INFO_LOG("process in");
541     if (player_ != nullptr) {
542         IpcIoPushInt32(reply, player_->Reset());
543         return;
544     }
545     IpcIoPushInt32(reply, 0); /* return success if there is no need to do anything */
546 }
547 
Release(IpcIo * req,IpcIo * reply)548 void PlayerServer::Release(IpcIo *req, IpcIo *reply)
549 {
550     MEDIA_INFO_LOG("process in");
551     if (player_ != nullptr) {
552         int32_t ret = player_->Release();
553         if (stream_ != nullptr) {
554             ServerStreamSource* serverStream = reinterpret_cast<ServerStreamSource*>(stream_.get());
555             pthread_mutex_lock(&g_streamThreadControl.mutex);
556             serverStream->threadRuning = false;
557             pthread_mutex_unlock(&g_streamThreadControl.mutex);
558             pthread_join(g_streamThreadControl.process, nullptr);
559             pthread_mutex_destroy(&g_streamThreadControl.mutex);
560             stream_.reset();
561             stream_ = nullptr;
562         }
563         if (sid_ != nullptr) {
564             UnregisterIpcCallback(*sid_);
565             delete sid_;
566             sid_ = nullptr;
567         }
568         playerCallback_.reset();
569         player_ = nullptr;
570         IpcIoPushInt32(reply, ret);
571         return;
572     }
573     IpcIoPushInt32(reply, 0); /* return success if there is no need to do anything */
574 }
575 
SetPlayerCallback(IpcIo * req,IpcIo * reply)576 void PlayerServer::SetPlayerCallback(IpcIo *req, IpcIo *reply)
577 {
578     MEDIA_INFO_LOG("process in");
579     int32_t ret = IpcIoPopInt32(req);
580     if (ret != LITEIPC_OK) {
581         MEDIA_ERR_LOG("SetPlayerCallback failed\n");
582         return;
583     }
584     SvcIdentity *sid = IpcIoPopSvc(req);
585 #ifdef __LINUX__
586     BinderAcquire(sid->ipcContext, sid->handle);
587 #endif
588     if (sid != nullptr) {
589         playerCallback_ = std::make_shared<PalyerCallbackImpl>(*sid);
590         if (player_ != nullptr) {
591             player_->SetPlayerCallback(playerCallback_);
592             return;
593         }
594     }
595 }
596 
GetPlayerState(IpcIo * req,IpcIo * reply)597 void PlayerServer::GetPlayerState(IpcIo *req, IpcIo *reply)
598 {
599     MEDIA_INFO_LOG("process in");
600     int32_t state = 0;
601     if (player_ != nullptr) {
602         IpcIoPushInt32(reply, player_->GetPlayerState(state));
603         IpcIoPushInt32(reply, state);
604         return;
605     }
606     IpcIoPushInt32(reply, 0);
607     IpcIoPushInt32(reply, PLAYER_IDLE); /* return inited state if not operate setsource */
608 }
609 
SetPlaybackSpeed(IpcIo * req,IpcIo * reply)610 void PlayerServer::SetPlaybackSpeed(IpcIo *req, IpcIo *reply)
611 {
612     MEDIA_INFO_LOG("process in");
613     float speed = IpcIoPopFloat(req);
614     if (player_ != nullptr) {
615         IpcIoPushInt32(reply, player_->SetPlaybackSpeed(speed));
616         return;
617     }
618     IpcIoPushInt32(reply, -1);
619 }
620 
GetPlaybackSpeed(IpcIo * req,IpcIo * reply)621 void PlayerServer::GetPlaybackSpeed(IpcIo *req, IpcIo *reply)
622 {
623     MEDIA_INFO_LOG("process in");
624     float speed = 1.0;
625     if (player_ != nullptr) {
626         IpcIoPushInt32(reply, player_->GetPlaybackSpeed(speed));
627         IpcIoPushFloat(reply, speed);
628         return;
629     }
630     IpcIoPushInt32(reply, -1);
631     IpcIoPushFloat(reply, 1.0);
632 }
633 
SetParameter(IpcIo * req,IpcIo * reply)634 void PlayerServer::SetParameter(IpcIo *req, IpcIo *reply)
635 {
636     MEDIA_INFO_LOG("process in");
637     Format formats;
638     if (player_ == nullptr) {
639         IpcIoPushInt32(reply, -1);
640         return;
641     }
642 
643     int32_t count = IpcIoPopInt32(req);
644     for (int32_t i = 0; i < count; i++) {
645         uint32_t size;
646         char *key = (char *)IpcIoPopString(req, &size);
647 
648         FormatDataType type = (FormatDataType)IpcIoPopInt32(req);
649         if (type == FORMAT_TYPE_INT32) {
650             int32_t value = IpcIoPopInt32(req);
651             formats.PutIntValue(key, value);
652         } else if (type == FORMAT_TYPE_INT64) {
653             int64_t value = IpcIoPopInt64(req);
654             formats.PutLongValue(key, value);
655         } else if (type == FORMAT_TYPE_FLOAT) {
656             float value = IpcIoPopFloat(req);
657             formats.PutFloatValue(key, value);
658         } else if (type == FORMAT_TYPE_DOUBLE) {
659             double value = IpcIoPopFloat(req);
660             formats.PutDoubleValue(key, value);
661         } else if (type == FORMAT_TYPE_STRING) {
662             char *value = (char *)IpcIoPopString(req, &size);
663             formats.PutStringValue(key, value);
664         } else {
665             MEDIA_ERR_LOG("SetParameter failed, type:%d\n", type);
666             IpcIoPushInt32(reply, -1);
667             return;
668         }
669     }
670 
671     IpcIoPushInt32(reply, player_->SetParameter(formats));
672 }
673 
SetAudioStreamType(IpcIo * req,IpcIo * reply)674 void PlayerServer::SetAudioStreamType(IpcIo *req, IpcIo *reply)
675 {
676     MEDIA_INFO_LOG("process in");
677     int32_t type = IpcIoPopInt32(req);
678     if (player_ != nullptr) {
679         IpcIoPushInt32(reply, player_->SetAudioStreamType(type));
680         return;
681     }
682     IpcIoPushInt32(reply, -1);
683 }
684 
GetAudioStreamType(IpcIo * req,IpcIo * reply)685 void PlayerServer::GetAudioStreamType(IpcIo *req, IpcIo *reply)
686 {
687     MEDIA_INFO_LOG("process in");
688     int32_t type = TYPE_MEDIA;
689     if (player_ != nullptr) {
690         player_->GetAudioStreamType(type);
691         IpcIoPushInt32(reply, 0);
692         IpcIoPushFloat(reply, type);
693         return;
694     }
695     IpcIoPushInt32(reply, -1);
696     IpcIoPushFloat(reply, type);
697 }
698 
OnPlaybackComplete()699 void PalyerCallbackImpl::OnPlaybackComplete()
700 {
701     IpcIo io;
702     uint8_t tmpData[DEFAULT_IPC_SIZE];
703     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
704     int32_t ret = Transact(nullptr, sid_, ON_PALYBACK_COMPLETE, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr);
705     if (ret != LITEIPC_OK) {
706         MEDIA_ERR_LOG("PalyerCallbackImpl::OnPlaybackComplete failed\n");
707     }
708 }
709 
OnError(int32_t errorType,int32_t errorCode)710 void PalyerCallbackImpl::OnError(int32_t errorType, int32_t errorCode)
711 {
712     IpcIo io;
713     uint8_t tmpData[DEFAULT_IPC_SIZE];
714     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
715     IpcIoPushInt32(&io, errorType);
716     IpcIoPushInt32(&io, errorCode);
717     int32_t ret = Transact(nullptr, sid_, ON_ERROR, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr);
718     if (ret != LITEIPC_OK) {
719         MEDIA_ERR_LOG("PalyerCallbackImpl::OnError failed\n");
720     }
721 }
722 
OnInfo(int type,int extra)723 void PalyerCallbackImpl::OnInfo(int type, int extra)
724 {
725     IpcIo io;
726     uint8_t tmpData[DEFAULT_IPC_SIZE];
727     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
728     IpcIoPushInt32(&io, (int32_t)type);
729     IpcIoPushInt32(&io, (int32_t)extra);
730     int32_t ret = Transact(nullptr, sid_, ON_INFO, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr);
731     if (ret != LITEIPC_OK) {
732         MEDIA_ERR_LOG("PalyerCallbackImpl::OnInfo failed\n");
733     }
734 }
735 
OnVideoSizeChanged(int width,int height)736 void PalyerCallbackImpl::OnVideoSizeChanged(int width, int height)
737 {
738     IpcIo io;
739     uint8_t tmpData[DEFAULT_IPC_SIZE];
740     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
741     IpcIoPushInt32(&io, (int32_t)width);
742     IpcIoPushInt32(&io, (int32_t)height);
743     int32_t ret = Transact(nullptr, sid_, ON_INFO, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr);
744     if (ret != LITEIPC_OK) {
745         MEDIA_ERR_LOG("PalyerCallbackImpl::OnInfo failed\n");
746     }
747 }
748 
OnRewindToComplete()749 void PalyerCallbackImpl::OnRewindToComplete()
750 {
751     IpcIo io;
752     uint8_t tmpData[DEFAULT_IPC_SIZE];
753     IpcIoInit(&io, tmpData, DEFAULT_IPC_SIZE, 0);
754     int32_t ret = Transact(nullptr, sid_, ON_REWIND_TO_COMPLETE, &io, nullptr, LITEIPC_FLAG_ONEWAY, nullptr);
755     if (ret != LITEIPC_OK) {
756         MEDIA_ERR_LOG("PalyerCallbackImpl::OnRewindToComplete failed\n");
757     }
758 }
759 }
760 }
761