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 "liteplayer.h"
17
18 #include "hi_liteplayer_err.h"
19 #include "liteplayer_comm.h"
20 #include <sys/prctl.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 #include "decoder.h"
24
25 using OHOS::Media::PlayerSource;
26 using OHOS::Media::SinkManager;
27 using OHOS::Media::Decoder;
28
29 namespace OHOS {
30 namespace Media {
31 /* DEFAULT_VID_BUFSIZE: 2 * 1024 * 1024 */
32 const uint32_t DEFAULT_VID_BUFSIZE = 2097152;
33 /* DEFAULT_AUD_BUFSIZE: 256 * 1024 */
34 const uint32_t DEFAULT_AUD_BUFSIZE = 262144;
35
36 const uint32_t GET_BUFFER_TIMEOUT_MS = 0u;
37 const uint32_t RENDER_FULL_SLEEP_TIME_US = 5000u;
38 const uint32_t RENDER_EOS_SLEEP_TIME_US = 3000000u;
39 const uint32_t PAUSE_WAIT_TIME_US = 10000u;
40 const uint32_t DECODER_DEFAULT_WIDTH = 1920;
41 const uint32_t DECODER_DEFAULT_HEIGHT = 1080;
42 const uint32_t QUEUE_BUFFER_FULL_SLEEP_TIME_US = 5000u;
43 const uint32_t NO_DATA_READ_SLEEP_TIME_US = 5000u;
44 const uint32_t MAX_EVENT_MESSAGE_NUM = 128;
45
46 struct CodecFormatAndMimePair {
47 CodecFormat format;
48 AvCodecMime mime;
49 };
50
51 static CodecFormatAndMimePair g_avCodecFormatInfo[CODEC_BUT + 1] = {
52 {CODEC_H264, MEDIA_MIMETYPE_VIDEO_AVC},
53 {CODEC_H265, MEDIA_MIMETYPE_VIDEO_HEVC},
54 {CODEC_JPEG, MEDIA_MIMETYPE_IMAGE_JPEG},
55 {CODEC_AAC, MEDIA_MIMETYPE_AUDIO_AAC},
56 {CODEC_G711A, MEDIA_MIMETYPE_AUDIO_G711A},
57 {CODEC_G711U, MEDIA_MIMETYPE_AUDIO_G711U},
58 {CODEC_PCM, MEDIA_MIMETYPE_AUDIO_PCM},
59 {CODEC_MP3, MEDIA_MIMETYPE_AUDIO_MP3},
60 {CODEC_G726, MEDIA_MIMETYPE_AUDIO_G726},
61 {CODEC_BUT, MEDIA_MIMETYPE_INVALID},
62 };
63
64 namespace {
65 /* playing position notify interval in ms */
66 const uint32_t DEFAULT_POS_NOFITY_INTERVAL = 300;
67 }
68
69 #define CHECK_NULL_RETURN(value, ret, printfString) \
70 do { \
71 if ((value) == nullptr) { \
72 MEDIA_ERR_LOG("%s", printfString ? printfString : " "); \
73 return (ret); \
74 } \
75 } while (0)
76
77 #define CHECK_NULL_RETURN_VOID(value, printfString) \
78 do { \
79 if ((value) == nullptr) { \
80 MEDIA_ERR_LOG("%s", printfString ? printfString : " "); \
81 return; \
82 } \
83 } while (0)
84
85 #define CHECK_FAILED_RETURN(value, target, ret, printfString) \
86 do { \
87 if ((value) != (target)) { \
88 MEDIA_ERR_LOG("%s",printfString ? printfString : " "); \
89 return ret; \
90 } \
91 } while (0)
92
93 #define CHECK_FAILED_PRINT(value, target, printfString) \
94 do { \
95 if ((value) != (target)) { \
96 MEDIA_ERR_LOG("%s",printfString ? printfString : " "); \
97 } \
98 } while (0)
99
100
101 #define CHECK_STATE_SAME(srcState, dstState) \
102 do { \
103 if ((dstState) == (srcState)) { \
104 MEDIA_INFO_LOG("current play state already be %d", (dstState)); \
105 return HI_SUCCESS; \
106 } \
107 } while (0)
108
109 const int32_t SS2US = 1000000;
110
CondTimeWait(pthread_cond_t & cond,pthread_mutex_t & mutex,uint32_t delayUs)111 static void CondTimeWait(pthread_cond_t &cond, pthread_mutex_t &mutex, uint32_t delayUs)
112 {
113 uint32_t tmpUs;
114 struct timeval ts;
115 struct timespec outtime;
116
117 ts.tv_sec = 0;
118 ts.tv_usec = 0;
119 gettimeofday(&ts , nullptr);
120 ts.tv_sec += (delayUs / SS2US);
121 tmpUs = delayUs % SS2US;
122
123 if (ts.tv_usec + tmpUs > SS2US) {
124 outtime.tv_sec = ts.tv_sec + 1;
125 outtime.tv_nsec = ((ts.tv_usec + tmpUs) - SS2US) * 1000;
126 } else {
127 outtime.tv_sec = ts.tv_sec;
128 outtime.tv_nsec = (ts.tv_usec + tmpUs) * 1000;
129 }
130 pthread_cond_timedwait(&cond, &mutex, &outtime);
131 }
132
GetCurVideoSolution(FormatFileInfo & info,uint32_t & width,uint32_t & height)133 static void GetCurVideoSolution(FormatFileInfo &info, uint32_t &width, uint32_t &height)
134 {
135 for (int i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) {
136 if (info.stSteamResolution[i].s32VideoStreamIndex == info.s32UsedVideoStreamIndex) {
137 width = info.stSteamResolution[i].u32Width;
138 height = info.stSteamResolution[i].u32Height;
139 break;
140 }
141 }
142 }
143
PlayerControl()144 PlayerControl::PlayerControl() : stateMachine_(nullptr), observer_(nullptr), isInited_(false), isNeedPause_(false),
145 pauseMode_(false), isPlayEnd_(false), isVidPlayEos_(false),
146 isAudPlayEos_(false), cachedPacket_(false), lastRendPos_(0), lastNotifyTime_(0),
147 tplayMode_(PLAYER_TPLAY_ONLY_I_FRAME), lastReadPktStrmIdx_(-1), lastReadPktPts_(0), lastSendPktPts_(0),
148 curSeekOffset_(0), isVideoStarted_(false), isAudioStarted_(false), isVidContinueLost_(false),
149 strmReadEnd_(false), isPlayErr_(false), isTplayLastFrame_(false), isTplayStartRead_(false),
150 lastSendVdecPts_(AV_INVALID_PTS), lastSendAdecPts_(AV_INVALID_PTS), currentPosition_(0), paused_(false),
151 schThreadExit_(false), loop_(false), hasRenderAudioEos_(false), hasRenderVideoEos_(false), renderSleepTime_(0),
152 leftVolume_(-1.0f), rightVolume_(-1.0f), schProcess_(0), seekToTimeMs_(-1), firstAudioFrameAfterSeek_(false),
153 firstVideoFrameAfterSeek_(false), sourceType_(SOURCE_TYPE_BUT), fd_(-1), playerSource_(nullptr),
154 sinkManager_(nullptr), audioDecoder_(nullptr), videoDecoder_(nullptr), audioStreamType_(0)
155 {
156 eventCallback_.player = nullptr;
157 eventCallback_.callbackFun = nullptr;
158 (void)memset_s(&fmtFileInfo_, sizeof(fmtFileInfo_), 0, sizeof(FormatFileInfo));
159 fmtFileInfo_.s32UsedAudioStreamIndex = HI_DEMUXER_NO_MEDIA_STREAM;
160 fmtFileInfo_.s32UsedVideoStreamIndex = HI_DEMUXER_NO_MEDIA_STREAM;
161 playerParam_.u32PlayPosNotifyIntervalMs = DEFAULT_POS_NOFITY_INTERVAL;
162 playerParam_.u32VideoEsBufSize = DEFAULT_POS_NOFITY_INTERVAL;
163 playerParam_.u32AudioEsBufSize = DEFAULT_POS_NOFITY_INTERVAL;
164 playerParam_.u32VdecFrameBufCnt = DEFAULT_POS_NOFITY_INTERVAL;
165 playerParam_.bPauseMode = DEFAULT_POS_NOFITY_INTERVAL;
166 (void)memset_s(&formatPacket_, sizeof(formatPacket_), 0, sizeof(FormatFrame));
167 tplayAttr_.direction = TPLAY_DIRECT_BUTT;
168 tplayAttr_.speed = 1.0;
169 streamAttr_.playMode = RENDER_MODE_NORMAL;
170 streamAttr_.videoBufSize = 0;
171 streamAttr_.audioBufSize = 0;
172 vdecAttr_.type = 0;
173 vdecAttr_.maxWidth = 0;
174 vdecAttr_.maxHeight = 0;
175 vdecAttr_.frameBufCnt = 0;
176 vdecAttr_.errRecover = 0;
177 adecAttr_.type = 0;
178 adecAttr_.sampleRate = 0;
179 adecAttr_.channel = 0;
180 adecAttr_.bitWidth = 0;
181 stream_.ReadData = nullptr;
182 stream_.GetReadableSize = nullptr;
183 stream_.handle = nullptr;
184 surface_ = nullptr;
185 eventQueue.clear();
186 }
187
~PlayerControl()188 PlayerControl::~PlayerControl()
189 {
190 (void)Deinit();
191 if (stateMachine_ != nullptr) {
192 delete stateMachine_;
193 stateMachine_ = nullptr;
194 }
195 if (observer_ != nullptr) {
196 delete observer_;
197 observer_ = nullptr;
198 }
199 eventQueue.clear();
200 }
201
GetState(void)202 PlayerStatus PlayerControl::GetState(void)
203 {
204 CHECK_NULL_RETURN(stateMachine_, PLAY_STATUS_BUTT, "stateMachine_ nullptr");
205 return stateMachine_->GetCurState();
206 }
207
InitAttr(const PlayerControlParam & createParam)208 int32_t PlayerControl::InitAttr(const PlayerControlParam &createParam)
209 {
210 if (memset_s(&fmtFileInfo_, sizeof(fmtFileInfo_), 0x00, sizeof(FormatFileInfo)) != EOK) {
211 MEDIA_ERR_LOG("set fmtFileInfo_ fail\n");
212 return HI_ERR_PLAYERCONTROL_MEM_SET;
213 }
214 fmtFileInfo_.s32UsedAudioStreamIndex = HI_DEMUXER_NO_MEDIA_STREAM;
215 fmtFileInfo_.s32UsedVideoStreamIndex = HI_DEMUXER_NO_MEDIA_STREAM;
216 streamAttr_.playMode = RENDER_MODE_NORMAL;
217 streamAttr_.videoBufSize = DEFAULT_VID_BUFSIZE;
218 streamAttr_.audioBufSize = DEFAULT_AUD_BUFSIZE;
219
220 lastReadPktPts_ = 0;
221 lastSendPktPts_ = 0;
222 pauseMode_ = createParam.bPauseMode;
223 CHECK_FAILED_PRINT(memcpy_s(&playerParam_, sizeof(playerParam_), &createParam, sizeof(PlayerControlParam)), EOK,
224 "copy playerParam_ fail");
225 playerParam_.u32PlayPosNotifyIntervalMs = (createParam.u32PlayPosNotifyIntervalMs == 0) ?
226 DEFAULT_POS_NOFITY_INTERVAL : createParam.u32PlayPosNotifyIntervalMs;
227 return HI_SUCCESS;
228 }
229
Init(const PlayerControlParam & createParam)230 int32_t PlayerControl::Init(const PlayerControlParam &createParam)
231 {
232 int32_t ret;
233 MEDIA_INFO_LOG("Init in");
234 CHECK_FAILED_RETURN(isInited_, false, HI_SUCCESS, "already be inited");
235 CHECK_FAILED_RETURN(InitAttr(createParam), HI_SUCCESS, HI_FAILURE, "InitAttr failed");
236 stateMachine_ = new(std::nothrow) PlayerControlStateMachine(*this);
237 CHECK_NULL_RETURN(stateMachine_, HI_FAILURE, "new PlayerControlStateMachine failed");
238 observer_ = new(std::nothrow) PlayerControlSMObserver(*this);
239 if (observer_ == nullptr) {
240 MEDIA_ERR_LOG("new PlayerControlSMObserver failed\n");
241 goto DEL_STATE;
242 }
243 (void)stateMachine_->RegisterObserver(*observer_);
244 ret = stateMachine_->Init(MAX_MSG_QUEUE_SIZE, MAX_MSG_QUEUE_PAYLOAD_SIZE);
245 if (ret != HI_SUCCESS) {
246 MEDIA_ERR_LOG("init PlayerControlStateMachine failed\n");
247 goto DEL_OBSERVER;
248 }
249 ret = stateMachine_->Start();
250 if (ret != HI_SUCCESS) {
251 MEDIA_ERR_LOG("start PlayerControlStateMachine failed\n");
252 goto DEINIT_STATE;
253 }
254 pthread_mutex_init(&schMutex_, nullptr);
255 pthread_cond_init(&schCond_, nullptr);
256
257 isInited_ = true;
258 return HI_SUCCESS;
259 DEINIT_STATE:
260 (void)stateMachine_->Deinit();
261 DEL_OBSERVER:
262 delete observer_;
263 observer_ = nullptr;
264 DEL_STATE:
265 delete stateMachine_;
266 stateMachine_ = nullptr;
267 return HI_FAILURE;
268 }
269
Deinit()270 int32_t PlayerControl::Deinit()
271 {
272 if (isInited_ == false) {
273 return HI_SUCCESS;
274 }
275 DestroyDecoder();
276 if (stateMachine_ != nullptr) {
277 MsgInfo msgInfo;
278 CHECK_FAILED_PRINT(memset_s(&msgInfo, sizeof(msgInfo), 0, sizeof(MsgInfo)), EOK, "memset_s failed");
279 msgInfo.what = PLAYERCONTROL_MSG_HANDLEDATA;
280 CHECK_FAILED_PRINT(stateMachine_->RemoveEvent(msgInfo), HI_SUCCESS, "RemoveEvent failed");
281 CHECK_FAILED_PRINT(stateMachine_->Stop(), HI_SUCCESS, "Stop failed");
282 CHECK_FAILED_PRINT(stateMachine_->Deinit(), HI_SUCCESS, "Deinit failed");
283 if (observer_ != nullptr) {
284 delete observer_;
285 observer_ = nullptr;
286 }
287 delete stateMachine_;
288 stateMachine_ = nullptr;
289 }
290 pthread_mutex_destroy(&schMutex_);
291 pthread_cond_destroy(&schCond_);
292 isInited_ = false;
293 return HI_SUCCESS;
294 }
295
RegCallback(PlayerCtrlCallbackParam & eventObserver)296 int32_t PlayerControl::RegCallback(PlayerCtrlCallbackParam &eventObserver)
297 {
298 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
299 MsgInfo msg;
300 msg.what = PLAYERCONTROL_MSG_REGCALLBACK;
301 msg.msgData = reinterpret_cast<void *>(&eventObserver);
302 msg.msgDataLen = 0;
303 return stateMachine_->Send(msg);
304 }
305
SetDataSource(const std::string filePath)306 int32_t PlayerControl::SetDataSource(const std::string filePath)
307 {
308 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
309 MsgInfo msg;
310 msg.what = PLAYERCONTROL_MSG_SET_DATASOURCE_URI;
311 msg.msgData = const_cast<void *>(reinterpret_cast<const void *>(filePath.c_str()));
312 msg.msgDataLen = 0;
313 return stateMachine_->Send(msg);
314 }
315
SetDataSource(const int fd)316 int32_t PlayerControl::SetDataSource(const int fd)
317 {
318 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
319 int mediaFd = fd;
320 MsgInfo msg;
321 msg.what = PLAYERCONTROL_MSG_SET_DATASOURCE_FD;
322 msg.msgData = reinterpret_cast<void *>(&mediaFd);
323 msg.msgDataLen = 0;
324 return stateMachine_->Send(msg);
325 }
326
SetDataSource(BufferStream & stream)327 int32_t PlayerControl::SetDataSource(BufferStream &stream)
328 {
329 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
330 MsgInfo msg;
331 msg.what = PLAYERCONTROL_MSG_SET_DATASOURCE_STREAM;
332 msg.msgData = reinterpret_cast<void *>(&stream);
333 msg.msgDataLen = 0;
334 return stateMachine_->Send(msg);
335 }
336
Prepare()337 int32_t PlayerControl::Prepare()
338 {
339 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
340 return stateMachine_->Send(PLAYERCONTROL_MSG_PREPARE);
341 }
342
Play()343 int32_t PlayerControl::Play()
344 {
345 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
346 int32_t ret = stateMachine_->Send(PLAYERCONTROL_MSG_PLAY);
347 CHECK_FAILED_PRINT(ret, HI_SUCCESS, "play failed");
348 return ret;
349 }
350
SetVolume(VolumeAttr & volumeAttr)351 int32_t PlayerControl::SetVolume(VolumeAttr &volumeAttr)
352 {
353 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
354 VolumeAttr attr = volumeAttr;
355 MsgInfo msg;
356 msg.what = PLAYERCONTROL_MSG_SET_VOLUME;
357 msg.msgData = reinterpret_cast<void *>(&attr);
358 msg.msgDataLen = 0;
359 return stateMachine_->Send(msg);
360 }
361
Stop()362 int32_t PlayerControl::Stop()
363 {
364 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
365 return stateMachine_->Send(PLAYERCONTROL_MSG_STOP);
366 }
367
Pause()368 int32_t PlayerControl::Pause()
369 {
370 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
371 return stateMachine_->Send(PLAYERCONTROL_MSG_PAUSE);
372 }
373
Seek(int64_t timeInMs)374 int32_t PlayerControl::Seek(int64_t timeInMs)
375 {
376 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
377 if (fmtFileInfo_.enVideoType == CODEC_JPEG) {
378 MEDIA_ERR_LOG("seek action not support for play picture");
379 return HI_ERR_PLAYERCONTROL_NOT_SUPPORT;
380 }
381 if ((fmtFileInfo_.s64Duration > 0) && (fmtFileInfo_.s64Duration < timeInMs)) {
382 return HI_ERR_PLAYERCONTROL_ILLEGAL_PARAM;
383 }
384 if (!stateMachine_->EventValidAtCurState(PLAYERCONTROL_MSG_SEEK)) {
385 MEDIA_ERR_LOG("invalid event seek at current state");
386 return HI_ERR_PLAYERCONTROL_ILLEGAL_STATE_ACTION;
387 }
388
389 if (seekTabel_[0] == -1) {
390 seekTabel_[0] = timeInMs;
391 } else if (seekTabel_[1] == -1) {
392 seekTabel_[1] = timeInMs;
393 }
394 MEDIA_INFO_LOG("seek out %lld", timeInMs);
395 return HI_SUCCESS;
396 }
397
GetFileInfo(FormatFileInfo & formatInfo)398 int32_t PlayerControl::GetFileInfo(FormatFileInfo &formatInfo)
399 {
400 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
401 MsgInfo msg;
402 msg.what = PLAYERCONTROL_MSG_GETFILEINFO;
403 msg.msgData = &formatInfo;
404 msg.msgDataLen = sizeof(FormatFileInfo);
405 return stateMachine_->Send(msg);
406 }
407
SetMedia(PlayerControlStreamAttr & mediaAttr)408 int32_t PlayerControl::SetMedia(PlayerControlStreamAttr &mediaAttr)
409 {
410 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
411 MsgInfo msg;
412 msg.what = PLAYERCONTROL_MSG_SETATTR;
413 msg.msgData = &mediaAttr;
414 msg.msgDataLen = sizeof(PlayerControlStreamAttr);
415 return stateMachine_->Send(msg);
416 }
417
SetSurface(Surface * surface)418 int32_t PlayerControl::SetSurface(Surface *surface)
419 {
420 surface_ = surface;
421 return 0;
422 }
423
TPlay(TplayAttr tplayAttr)424 int32_t PlayerControl::TPlay(TplayAttr tplayAttr)
425 {
426 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
427 MsgInfo msg;
428 msg.what = PLAYERCONTROL_MSG_TPLAY;
429 msg.msgData = &tplayAttr;
430 msg.msgDataLen = sizeof(TplayAttr);
431 return stateMachine_->Send(msg);
432 }
433
ClearCachePacket()434 void PlayerControl::ClearCachePacket()
435 {
436 if (cachedPacket_) {
437 if (formatPacket_.data != nullptr) {
438 if (playerSource_!= nullptr && playerSource_->FreeFrame(formatPacket_) != 0) {
439 MEDIA_ERR_LOG("FreeFrame failed");
440 }
441 formatPacket_.data = nullptr;
442 formatPacket_.len = 0;
443 formatPacket_.trackId = -1;
444 }
445 /* end packet is null do not need to demux free */
446 cachedPacket_ = false;
447 }
448 }
449
OnVideoEndOfStream()450 void PlayerControl::OnVideoEndOfStream()
451 {
452 CHECK_NULL_RETURN_VOID(stateMachine_, "stateMachine_ nullptr");
453 if ((fmtFileInfo_.s32UsedAudioStreamIndex == HI_DEMUXER_NO_MEDIA_STREAM ||
454 stateMachine_->GetCurState() == PLAY_STATUS_TPLAY) &&
455 lastSendVdecPts_ > AV_INVALID_PTS) {
456 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, &lastSendVdecPts_);
457 }
458 isVidPlayEos_ = true;
459 isPlayEnd_ = IsPlayEos() ? true : isPlayEnd_;
460 }
461
EventProcess(EventCbType event)462 void PlayerControl::EventProcess(EventCbType event)
463 {
464 PlayerControlError playerError;
465 MEDIA_DEBUG_LOG("handleEvent %d", event);
466
467 switch (event) {
468 case EVNET_VIDEO_PLAY_EOS:
469 OnVideoEndOfStream();
470 break;
471 case EVNET_VIDEO_PLAY_SOS:
472 MEDIA_INFO_LOG("video sos recv");
473 break;
474 case EVNET_AUDIO_PLAY_EOS:
475 CHECK_NULL_RETURN_VOID(stateMachine_, "stateMachine_ nullptr");
476 if (stateMachine_->GetCurState() != PLAY_STATUS_TPLAY) {
477 isAudPlayEos_ = true;
478 isPlayEnd_ = IsPlayEos() ? true : isPlayEnd_;
479 if (lastSendAdecPts_ > AV_INVALID_PTS) {
480 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, &lastSendAdecPts_);
481 }
482 }
483 break;
484 case EVNET_VIDEO_RUNNING_ERR:
485 case EVNET_AUDIO_RUNNING_ERR:
486 playerError = PLAYERCONTROL_ERROR_VID_PLAY_FAIL;
487 isPlayErr_ = true;
488 EventCallback(PLAYERCONTROL_EVENT_ERROR, &playerError);
489 break;
490 case EVNET_FRAME_CONTINUE_LOST:
491 isVidContinueLost_ = true;
492 MEDIA_INFO_LOG("receive frame continue lost");
493 break;
494 case EVNET_ON_JPEG_FRAME_RENDED:
495 break;
496 case EVNET_FIRST_VIDEO_REND:
497 if (pauseMode_) {
498 MEDIA_INFO_LOG("first video rended");
499 isNeedPause_ = true;
500 EventCallback(PLAYERCONTROL_FIRST_VIDEO_FRAME, NULL);
501 }
502 break;
503 case EVNET_FIRST_AUDIO_REND:
504 if (pauseMode_ && fmtFileInfo_.s32UsedVideoStreamIndex == -1) {
505 MEDIA_INFO_LOG("first audio rended");
506 isNeedPause_ = true;
507 EventCallback(PLAYERCONTROL_FIRST_AUDIO_FRAME, NULL);
508 }
509 break;
510 default:
511 break;
512 }
513 }
514
EventQueueProcess(void)515 void PlayerControl::EventQueueProcess(void)
516 {
517 size_t queSize;
518 PalayControlEventItem *item = nullptr;
519
520 queSize = eventQueue.size();
521 if (queSize > MAX_EVENT_MESSAGE_NUM) {
522 MEDIA_WARNING_LOG("mesaage except, num:%u", queSize);
523 }
524 for (size_t i = 0; i < queSize && i < MAX_EVENT_MESSAGE_NUM; i++) {
525 item = &eventQueue[i];
526 EventProcess(item->event);
527 }
528 eventQueue.clear();
529 }
530
OnPlayControlEvent(void * priv,const EventCbType event)531 int32_t PlayerControl::OnPlayControlEvent(void *priv, const EventCbType event)
532 {
533 PalayControlEventItem item;
534 PlayerControl *player = reinterpret_cast<PlayerControl *>(priv);
535
536 item.event = event;
537 player->eventQueue.push_back(item);
538 return HI_SUCCESS;
539 }
540
PauseResume(void)541 int32_t PlayerControl::PauseResume(void)
542 {
543 CHECK_NULL_RETURN(sinkManager_, HI_ERR_PLAYERCONTROL_NULL_PTR, "sinkManager_ nullptr");
544 int32_t ret = sinkManager_->Resume();
545 if (ret != HI_SUCCESS) {
546 MEDIA_ERR_LOG("resume failed");
547 }
548 pthread_mutex_lock(&schMutex_);
549 paused_ = false;
550 pthread_cond_signal(&schCond_);
551 pthread_mutex_unlock(&schMutex_);
552 return HI_SUCCESS;
553 }
554
TPlayResume(void)555 int32_t PlayerControl::TPlayResume(void)
556 {
557 CHECK_NULL_RETURN(sinkManager_, HI_ERR_PLAYERCONTROL_NULL_PTR, "sinkManager_ nullptr");
558 int32_t ret = sinkManager_->SetSpeed(1.0, tplayAttr_.direction);
559 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SetSpeed failed");
560 return HI_SUCCESS;
561 }
562
OnSwitchTPlay2Play()563 int32_t PlayerControl::OnSwitchTPlay2Play()
564 {
565 int32_t ret;
566 CHECK_NULL_RETURN(playerSource_, HI_FAILURE, "playerSource_ nullptr");
567
568 pthread_mutex_lock(&schMutex_);
569 if (tplayMode_ != PLAYER_TPLAY_FULL_PLAY) {
570 ret = TPlayResetBuffer();
571 if (ret != HI_SUCCESS) {
572 MEDIA_ERR_LOG("TPlayResetBuffer failed , ret:%d", ret);
573 pthread_mutex_unlock(&schMutex_);
574 return ret;
575 }
576 ret = playerSource_->Seek(fmtFileInfo_.s32UsedVideoStreamIndex, lastReadPktPts_, FORMAT_SEEK_MODE_FORWARD_KEY);
577 if (ret != HI_SUCCESS) {
578 MEDIA_ERR_LOG("playerSource_ seek failed , ret:%d", ret);
579 pthread_mutex_unlock(&schMutex_);
580 return ret;
581 }
582 strmReadEnd_ = false;
583 isVidPlayEos_ = false;
584 isAudPlayEos_ = false;
585 hasRenderVideoEos_ = false;
586 hasRenderAudioEos_ = false;
587 }
588 firstAudioFrameAfterSeek_ = true;
589 firstVideoFrameAfterSeek_ = true;
590
591 ret = TPlayResume();
592 pthread_mutex_unlock(&schMutex_);
593 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "TPlayResume failed");
594 return HI_SUCCESS;
595 }
596
CheckMediaInfo(void)597 int32_t PlayerControl::CheckMediaInfo(void)
598 {
599 if (fmtFileInfo_.s32UsedAudioStreamIndex >= HI_DEMUXER_RESOLUTION_CNT
600 || fmtFileInfo_.s32UsedVideoStreamIndex >= HI_DEMUXER_RESOLUTION_CNT) {
601 MEDIA_ERR_LOG("input file error video track: %d audio %d",
602 fmtFileInfo_.s32UsedVideoStreamIndex, fmtFileInfo_.s32UsedAudioStreamIndex);
603 return HI_ERR_PLAYERCONTROL_DEMUX_ERROR;
604 }
605 if (CheckMediaType(fmtFileInfo_) != HI_SUCCESS) {
606 return HI_ERR_PLAYERCONTROL_NOT_SUPPORT;
607 }
608 return HI_SUCCESS;
609 }
610
SetVDecAttr(void)611 int32_t PlayerControl::SetVDecAttr(void)
612 {
613 StreamResolution resolution = { 0 };
614 if (GetVideoResolution(fmtFileInfo_.s32UsedVideoStreamIndex, resolution) != HI_SUCCESS) {
615 MEDIA_ERR_LOG("GetVideoResolution failed");
616 return HI_FAILURE;
617 }
618 vdecAttr_.maxWidth = resolution.u32Width;
619 vdecAttr_.maxHeight = resolution.u32Height;
620 vdecAttr_.type = resolution.enVideoType;
621 vdecAttr_.frameBufCnt = playerParam_.u32VdecFrameBufCnt;
622 return HI_SUCCESS;
623 }
624
SetADecAttr(void)625 int32_t PlayerControl::SetADecAttr(void)
626 {
627 adecAttr_.bitWidth = AUDIO_BITWIDTH;
628 adecAttr_.channel = fmtFileInfo_.u32AudioChannelCnt;
629 adecAttr_.sampleRate = fmtFileInfo_.u32SampleRate;
630 adecAttr_.type = fmtFileInfo_.enAudioType;
631 return HI_SUCCESS;
632 }
633
SetStreamAttr()634 int32_t PlayerControl::SetStreamAttr()
635 {
636 if (pauseMode_) {
637 streamAttr_.playMode = RENDER_MODE_PAUSE_AFTER_PLAY;
638 }
639 if (fmtFileInfo_.s32UsedVideoStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM) {
640 StreamResolution resolution = { 0 };
641 if (GetVideoResolution(fmtFileInfo_.s32UsedVideoStreamIndex, resolution) != HI_SUCCESS) {
642 MEDIA_ERR_LOG("GetVideoResolution failed");
643 return HI_ERR_PLAYERCONTROL_ILLEGAL_PARAM;
644 }
645 streamAttr_.videoBufSize = playerParam_.u32VideoEsBufSize;
646 }
647 if (fmtFileInfo_.s32UsedAudioStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM) {
648 streamAttr_.audioBufSize = (playerParam_.u32AudioEsBufSize) ? (playerParam_.u32AudioEsBufSize) :
649 streamAttr_.audioBufSize;
650 }
651
652 if (sinkManager_ != nullptr) {
653 sinkManager_->SetRenderMode(streamAttr_.playMode);
654 }
655 return HI_SUCCESS;
656 }
657
SetDecoderAndStreamAttr(void)658 int32_t PlayerControl::SetDecoderAndStreamAttr(void)
659 {
660 int ret;
661
662 if (fmtFileInfo_.s32UsedAudioStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM) {
663 ret = SetADecAttr();
664 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SetADecAttr failed");
665 }
666 if (fmtFileInfo_.s32UsedVideoStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM) {
667 ret = SetVDecAttr();
668 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SetVDecAttr failed");
669 }
670 ret = SetStreamAttr();
671 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SetStreamAttr failed");
672 return HI_SUCCESS;
673 }
674
TransformCodecFormatToAvCodecMime(CodecFormat format)675 static AvCodecMime TransformCodecFormatToAvCodecMime(CodecFormat format)
676 {
677 AvCodecMime mime = MEDIA_MIMETYPE_INVALID;
678 uint32_t size = sizeof(g_avCodecFormatInfo) / sizeof(CodecFormatAndMimePair);
679
680 for (uint32_t i = 0; i < size; i++) {
681 if (g_avCodecFormatInfo[i].format == format) {
682 mime = g_avCodecFormatInfo[i].mime;
683 break;
684 }
685 }
686
687 return mime;
688 }
689
GetAudioNameByAvCodecMime(AvCodecMime mime)690 static std::string GetAudioNameByAvCodecMime(AvCodecMime mime)
691 {
692 std::string audioName = "codec.unknow.soft.decoder";
693 switch (mime) {
694 case MEDIA_MIMETYPE_AUDIO_AAC:
695 audioName = "codec.aac.soft.decoder";
696 break;
697 case MEDIA_MIMETYPE_AUDIO_MP3:
698 audioName = "codec.mp3.soft.decoder";
699 break;
700 case MEDIA_MIMETYPE_AUDIO_PCM:
701 audioName = "codec.pcm16s.soft.decoder";
702 break;
703 default:
704 MEDIA_ERR_LOG("not support codec type:%d", mime);
705 break;
706 }
707 return audioName;
708 }
709
AudioDecoderStart(void)710 int32_t PlayerControl::AudioDecoderStart(void)
711 {
712 AvCodecMime mime = TransformCodecFormatToAvCodecMime(fmtFileInfo_.enAudioType);
713 if (mime == MEDIA_MIMETYPE_INVALID) {
714 MEDIA_ERR_LOG("DecoderStart not support codec:%d", fmtFileInfo_.enAudioType);
715 return -1;
716 }
717 audioDecoder_ = std::make_shared<Decoder>();
718 CHECK_NULL_RETURN(audioDecoder_, -1, "new decoder failed");
719 AvAttribute attr;
720 attr.type = AUDIO_DECODER;
721 attr.adecAttr.mime = mime;
722 attr.adecAttr.priv = nullptr;
723 attr.adecAttr.bufSize = 0x400; /* 1024 */
724 attr.adecAttr.channelCnt = (mime == MEDIA_MIMETYPE_AUDIO_PCM) ? fmtFileInfo_.u32AudioChannelCnt : 0;
725 const std::string audioName = GetAudioNameByAvCodecMime(mime);
726 int32_t ret = audioDecoder_->CreateHandle(audioName, attr);
727 CHECK_FAILED_RETURN(ret, 0, -1, "create audio decoder failed");
728 ret = audioDecoder_->StartDec();
729 CHECK_FAILED_RETURN(ret, 0, -1, "start audio decoder failed");
730 MEDIA_INFO_LOG("audio decoder started");
731
732 return 0;
733 }
734
VideoDecoderStart(void)735 int32_t PlayerControl::VideoDecoderStart(void)
736 {
737 AvAttribute attr;
738 uint32_t width = DECODER_DEFAULT_WIDTH;
739 uint32_t height = DECODER_DEFAULT_HEIGHT;
740
741 AvCodecMime mime = TransformCodecFormatToAvCodecMime(fmtFileInfo_.enVideoType);
742 if (mime == MEDIA_MIMETYPE_INVALID) {
743 MEDIA_ERR_LOG("DecoderStart not support codec:%d", fmtFileInfo_.enVideoType);
744 return -1;
745 }
746 GetCurVideoSolution(fmtFileInfo_, width, height);
747
748 videoDecoder_ = std::make_shared<Decoder>();
749 CHECK_NULL_RETURN(videoDecoder_, -1, "new decoder failed");
750 attr.type = VIDEO_DECODER;
751 attr.vdecAttr.mime = mime;
752 attr.vdecAttr.priv = nullptr;
753 attr.vdecAttr.bufSize = 0;
754 attr.vdecAttr.maxWidth = width;
755 attr.vdecAttr.maxHeight = height;
756 const std::string videoName = "codec.avc.soft.decoder";
757 int32_t ret = videoDecoder_->CreateHandle(videoName, attr);
758 CHECK_FAILED_RETURN(ret, 0, -1, "create video decoder failed");
759 ret = videoDecoder_->StartDec();
760 CHECK_FAILED_RETURN(ret, 0, -1, "start video decoder failed");
761 MEDIA_INFO_LOG("video decoder started");
762
763 return 0;
764 }
765
DecoderStart(void)766 int32_t PlayerControl::DecoderStart(void)
767 {
768 if (fmtFileInfo_.s32UsedAudioStreamIndex == -1 && fmtFileInfo_.s32UsedVideoStreamIndex == -1) {
769 MEDIA_ERR_LOG("not decoder need start");
770 return -1;
771 }
772 MEDIA_INFO_LOG("PlayerControl::DecoderStart streamid:%d-%d",
773 fmtFileInfo_.s32UsedAudioStreamIndex, fmtFileInfo_.s32UsedVideoStreamIndex);
774 if (fmtFileInfo_.s32UsedAudioStreamIndex != -1) {
775 if (AudioDecoderStart() != 0) {
776 return -1;
777 }
778 }
779 if (fmtFileInfo_.s32UsedVideoStreamIndex != -1) {
780 if (VideoDecoderStart() != 0) {
781 return -1;
782 }
783 }
784 return 0;
785 }
786
DestroyDecoder()787 void PlayerControl::DestroyDecoder()
788 {
789 if (audioDecoder_ != nullptr) {
790 audioDecoder_->DestroyHandle();
791 }
792 if (videoDecoder_ != nullptr) {
793 videoDecoder_->DestroyHandle();
794 }
795 }
796
StopSinkAndDecoder()797 void PlayerControl::StopSinkAndDecoder()
798 {
799 OutputInfo outInfo;
800
801 if (sinkManager_ != nullptr) {
802 sinkManager_->Stop();
803 }
804 if (audioDecoder_ != nullptr && sinkManager_ != nullptr) {
805 while (sinkManager_->DequeReleaseFrame(true, outInfo) == 0) {
806 audioDecoder_->QueueOutputBuffer(outInfo, GET_BUFFER_TIMEOUT_MS);
807 }
808 audioDecoder_->StopDec();
809 }
810 if (videoDecoder_ != nullptr && sinkManager_ != nullptr) {
811 while (sinkManager_->DequeReleaseFrame(false, outInfo) == 0) {
812 videoDecoder_->QueueOutputBuffer(outInfo, GET_BUFFER_TIMEOUT_MS);
813 }
814 videoDecoder_->StopDec();
815 }
816 isVideoStarted_ = false;
817 isAudioStarted_ = false;
818 }
819
820
AddAudioSink(void)821 int32_t PlayerControl::AddAudioSink(void)
822 {
823 CHECK_FAILED_RETURN(isAudioStarted_, false, HI_SUCCESS, "already started");
824 SinkAttr attr;
825 attr.sinkType = SINK_TYPE_AUDIO;
826 attr.trackId = fmtFileInfo_.s32UsedAudioStreamIndex;
827 attr.audAttr.format = fmtFileInfo_.enAudioType;
828 attr.audAttr.sampleFmt = DATA_TYPE_S16;
829 attr.audAttr.sampleRate = adecAttr_.sampleRate;
830 attr.audAttr.channel = fmtFileInfo_.u32AudioChannelCnt;
831 attr.audAttr.volume = 0x6;
832
833 if (sinkManager_->AddNewSink(attr) != 0) {
834 MEDIA_ERR_LOG("AddNewSink failed");
835 return -1;
836 }
837 isAudioStarted_ = true;
838 return HI_SUCCESS;
839 }
840
AddVideoSink()841 int32_t PlayerControl::AddVideoSink()
842 {
843 CHECK_FAILED_RETURN(isVideoStarted_, false, HI_SUCCESS, "already started");
844 SinkAttr attr;
845 attr.sinkType = SINK_TYPE_VIDEO;
846 attr.trackId = fmtFileInfo_.s32UsedVideoStreamIndex;
847 attr.vidAttr.width = fmtFileInfo_.u32Width;
848 attr.vidAttr.height = fmtFileInfo_.u32Height;
849 attr.vidAttr.frameRate = fmtFileInfo_.fFrameRate;
850 attr.vidAttr.format = fmtFileInfo_.enVideoType;
851 attr.vidAttr.surface = surface_;
852 if (sinkManager_->AddNewSink(attr) != 0) {
853 MEDIA_ERR_LOG("AddNewSink failed");
854 return -1;
855 }
856 isVideoStarted_ = true;
857 return HI_SUCCESS;
858 }
859
860
SinkStart(void)861 int32_t PlayerControl::SinkStart(void)
862 {
863 int32_t ret = HI_SUCCESS;
864 sinkManager_ = std::make_shared<SinkManager>();
865 CHECK_NULL_RETURN(sinkManager_, HI_FAILURE, "new sinkManager_ nullptr");
866 if (fmtFileInfo_.s32UsedVideoStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM && !isVideoStarted_) {
867 ret = AddVideoSink();
868 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "AddVideoSink failed");
869 isVideoStarted_ = true;
870 }
871 if (fmtFileInfo_.s32UsedAudioStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM && !isAudioStarted_) {
872 ret = AddAudioSink();
873 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "AddAudioSink failed");
874 isAudioStarted_ = true;
875 sinkManager_->SetAudioStreamType(audioStreamType_);
876 }
877
878 PlayEventCallback callback;
879 GetPlayElementEventCallBack(callback);
880 ret = sinkManager_->RegisterCallBack(callback);
881 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "RegisterCallBack failed");
882 sinkManager_->SetRenderMode((pauseMode_ == true) ? RENDER_MODE_PAUSE_AFTER_PLAY : RENDER_MODE_NORMAL);
883 ret = sinkManager_->Start();
884 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "sinkManager_ Start failed");
885
886 if (leftVolume_ >= 0.0f || rightVolume_ >= 0.0f) {
887 sinkManager_->SetVolume(leftVolume_, rightVolume_);
888 }
889 return HI_SUCCESS;
890 }
891
ReadFrameFromSource(FormatFrame & fmtFrame)892 int32_t PlayerControl::ReadFrameFromSource(FormatFrame &fmtFrame)
893 {
894 CHECK_NULL_RETURN(playerSource_, HI_ERR_PLAYERCONTROL_NULL_PTR, "playerSource_ nullptr");
895 int ret = playerSource_->ReadFrame(fmtFrame);
896 // Check consistency of fmtFrame
897 if ((ret == HI_SUCCESS) && (fmtFrame.data != nullptr) && (fmtFrame.len == 0)) {
898 MEDIA_ERR_LOG("ReadFrame ERROR : u32Size is invalid");
899 ret = HI_FAILURE;
900 }
901 return ret;
902 }
903
GetVideoResolution(int32_t streamIdx,StreamResolution & resolution)904 int32_t PlayerControl::GetVideoResolution(int32_t streamIdx, StreamResolution &resolution)
905 {
906 if (streamIdx == HI_DEMUXER_NO_MEDIA_STREAM) {
907 MEDIA_ERR_LOG("stream id [%d] in invalid", streamIdx);
908 return HI_FAILURE;
909 }
910
911 for (uint32_t i = 0; i < HI_DEMUXER_RESOLUTION_CNT; i++) {
912 if (fmtFileInfo_.stSteamResolution[i].s32VideoStreamIndex == streamIdx) {
913 resolution.enVideoType = fmtFileInfo_.stSteamResolution[i].enVideoType;
914 resolution.s32VideoStreamIndex = fmtFileInfo_.stSteamResolution[i].s32VideoStreamIndex;
915 resolution.u32Width = fmtFileInfo_.stSteamResolution[i].u32Width;
916 resolution.u32Height = fmtFileInfo_.stSteamResolution[i].u32Height;
917 return HI_SUCCESS;
918 }
919 }
920
921 MEDIA_ERR_LOG("not found stream[%d] in file", streamIdx);
922 return HI_FAILURE;
923 }
924
925 // message begin
DoRegCallback(PlayerCtrlCallbackParam & eventCallback)926 int32_t PlayerControl::DoRegCallback(PlayerCtrlCallbackParam &eventCallback)
927 {
928 if (eventCallback_.callbackFun != nullptr) {
929 MEDIA_WARNING_LOG("you have reg callback,now the before callback fun pointer will be overwrite");
930 }
931 eventCallback_ = eventCallback;
932 return HI_SUCCESS;
933 }
934
DoSetDataSource(const std::string filepath)935 int32_t PlayerControl::DoSetDataSource(const std::string filepath)
936 {
937 filePath_ = filepath;
938 sourceType_ = SOURCE_TYPE_URI;
939 return HI_SUCCESS;
940 }
941
DoSetDataSource(const int32_t fd)942 int32_t PlayerControl::DoSetDataSource(const int32_t fd)
943 {
944 fd_ = fd;
945 sourceType_ = SOURCE_TYPE_FD;
946 return HI_SUCCESS;
947 }
948
DoSetDataSource(BufferStream & stream)949 int32_t PlayerControl::DoSetDataSource(BufferStream &stream)
950 {
951 stream_ = stream;
952 sourceType_ = SOURCE_TYPE_STREAM;
953 return HI_SUCCESS;
954 }
955
DoPrepare(void)956 int32_t PlayerControl::DoPrepare(void)
957 {
958 MEDIA_DEBUG_LOG("Process in");
959 CHECK_NULL_RETURN(stateMachine_, HI_FAILURE, "stateMachine_ nullptr");
960 PlayerStatus playerState = stateMachine_->GetCurState();
961 CHECK_STATE_SAME(playerState, PLAY_STATUS_PREPARED);
962 CHECK_NULL_RETURN(eventCallback_.callbackFun, HI_ERR_PLAYERCONTROL_CALLBACK_ERROR, "callbackFun nullptr");
963 isPlayEnd_ = false;
964 isTplayStartRead_ = false;
965 int32_t ret = SyncPrepare();
966 CHECK_FAILED_RETURN(ret, HI_SUCCESS, HI_ERR_PLAYERCONTROL_OTHER, "SyncPrepare failed");
967 if (playerParam_.u32PlayPosNotifyIntervalMs >= fmtFileInfo_.s64Duration) {
968 MEDIA_WARNING_LOG(
969 "play postion notify interval %d ms oversize file duration, user will never receive notify",
970 playerParam_.u32PlayPosNotifyIntervalMs);
971 }
972 MEDIA_DEBUG_LOG("Process out");
973 return HI_SUCCESS;
974 }
975
ReleaseADecoderOutputFrame(void)976 void PlayerControl::ReleaseADecoderOutputFrame(void)
977 {
978 if (audioDecoder_ == nullptr || sinkManager_ == nullptr) {
979 return;
980 }
981 while (true) {
982 OutputInfo outInfo;
983 if (sinkManager_->DequeReleaseFrame(true, outInfo) != 0) {
984 break;
985 }
986 audioDecoder_->QueueOutputBuffer(outInfo, GET_BUFFER_TIMEOUT_MS);
987 }
988 }
989
InitOutputBuffer(OutputInfo & outInfo,CodecType type)990 static void InitOutputBuffer(OutputInfo &outInfo, CodecType type)
991 {
992 outInfo.bufferCnt = 0;
993 outInfo.buffers = nullptr;
994 outInfo.timeStamp = -1;
995 outInfo.sequence = 0;
996 outInfo.flag = 0;
997 outInfo.type = type;
998 outInfo.vendorPrivate = nullptr;
999 }
1000
RenderAudioFrame(void)1001 void PlayerControl::RenderAudioFrame(void)
1002 {
1003 if (audioDecoder_ == nullptr || sinkManager_ == nullptr || hasRenderAudioEos_) {
1004 return;
1005 }
1006
1007 OutputInfo outInfo;
1008 int ret = audioDecoder_->DequeueOutputBuffer(outInfo, GET_BUFFER_TIMEOUT_MS);
1009 if (ret != 0) {
1010 InitOutputBuffer(outInfo, AUDIO_DECODER);
1011 if (ret == CODEC_RECEIVE_EOS && strmReadEnd_) {
1012 sinkManager_->RenderEos(true); /* all frame have been send to audio sink */
1013 }
1014 }
1015 outInfo.type = AUDIO_DECODER;
1016 ret = sinkManager_->RenderFrame(outInfo);
1017 if (ret == SINK_RENDER_FULL || ret == SINK_RENDER_DELAY) {
1018 renderSleepTime_ = RENDER_FULL_SLEEP_TIME_US;
1019 } else if (ret == SINK_QUE_EMPTY) {
1020 renderSleepTime_ = 0;
1021 }
1022 ReleaseADecoderOutputFrame();
1023 /* fuction RenderFrame will return SINK_RENDER_EOS when all frame in queue that have been processed */
1024 if (ret == SINK_RENDER_EOS) {
1025 hasRenderAudioEos_ = true;
1026 MEDIA_INFO_LOG("have render audio eos");
1027 if (IsPlayEos() == true) {
1028 isPlayEnd_ = true;
1029 }
1030 }
1031 }
1032
ReleaseVDecoderOutputFrame(void)1033 void PlayerControl::ReleaseVDecoderOutputFrame(void)
1034 {
1035 if (videoDecoder_ == nullptr || sinkManager_ == nullptr) {
1036 return;
1037 }
1038 while (true) {
1039 OutputInfo outInfo;
1040 if (sinkManager_->DequeReleaseFrame(false, outInfo) != 0) {
1041 break;
1042 }
1043 videoDecoder_->QueueOutputBuffer(outInfo, GET_BUFFER_TIMEOUT_MS);
1044 }
1045 }
1046
RenderVideoFrame(void)1047 void PlayerControl::RenderVideoFrame(void)
1048 {
1049 if (videoDecoder_ == nullptr || sinkManager_ == nullptr || hasRenderVideoEos_) {
1050 return;
1051 }
1052
1053 OutputInfo outInfo;
1054 int ret = videoDecoder_->DequeueOutputBuffer(outInfo, GET_BUFFER_TIMEOUT_MS);
1055 if (ret != 0) {
1056 InitOutputBuffer(outInfo, VIDEO_DECODER);
1057 if (ret == CODEC_RECEIVE_EOS) {
1058 sinkManager_->RenderEos(false); /* all frame have been send to video sink */
1059 }
1060 }
1061
1062 outInfo.type = VIDEO_DECODER;
1063 ret = sinkManager_->RenderFrame(outInfo);
1064 if (ret == SINK_RENDER_FULL || ret == SINK_RENDER_DELAY) {
1065 renderSleepTime_ = RENDER_FULL_SLEEP_TIME_US;
1066 } else if (ret == SINK_QUE_EMPTY) {
1067 renderSleepTime_ = 0;
1068 }
1069 ReleaseVDecoderOutputFrame();
1070 /* fuction RenderFrame will return SINK_RENDER_EOS when all frame in queue that have been processed */
1071 if (ret == SINK_RENDER_EOS) {
1072 hasRenderVideoEos_ = true;
1073 MEDIA_INFO_LOG("have rendered video eos");
1074 if (IsPlayEos()) {
1075 isPlayEnd_ = true;
1076 }
1077 }
1078 }
1079
ReortRenderPosition(void)1080 void PlayerControl::ReortRenderPosition(void)
1081 {
1082 int64_t position = -1;
1083 CHECK_NULL_RETURN_VOID(sinkManager_, "sinkManager_ nullptr");
1084 sinkManager_->GetRenderPosition(position);
1085 if (position >= 0 && currentPosition_ != position) {
1086 currentPosition_ = position;
1087 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, ¤tPosition_);
1088 }
1089 }
1090
DataSchProcess(void * priv)1091 void *PlayerControl::DataSchProcess(void *priv)
1092 {
1093 PlayerControl *play = (PlayerControl*)priv;
1094 CHECK_NULL_RETURN(play, nullptr, "play nullptr");
1095
1096 prctl(PR_SET_NAME, "PlaySch", 0, 0, 0);
1097 MEDIA_INFO_LOG("start work");
1098 while (true) {
1099 pthread_mutex_lock(&play->schMutex_);
1100 if (play->schThreadExit_ == true) {
1101 pthread_mutex_unlock(&play->schMutex_);
1102 break;
1103 }
1104 play->DoSeekIfNeed();
1105 if (play->paused_) {
1106 CondTimeWait(play->schCond_, play->schMutex_, PAUSE_WAIT_TIME_US);
1107 pthread_mutex_unlock(&play->schMutex_);
1108 continue;
1109 }
1110 play->renderSleepTime_ = 0;
1111 play->ReadPacketAndPushToDecoder();
1112
1113 /* get and render frame */
1114 play->RenderAudioFrame();
1115 play->RenderVideoFrame();
1116 pthread_mutex_unlock(&play->schMutex_);
1117 play->EventQueueProcess();
1118 play->ReortRenderPosition();
1119 if (play->isPlayEnd_) {
1120 play->DealPlayEnd();
1121 play->isPlayEnd_ = false;
1122 }
1123 if (play->IsPlayEos()) {
1124 play->renderSleepTime_ = RENDER_EOS_SLEEP_TIME_US;
1125 }
1126 if (play->renderSleepTime_ > 0) {
1127 pthread_mutex_lock(&play->schMutex_);
1128 CondTimeWait(play->schCond_, play->schMutex_, play->renderSleepTime_);
1129 pthread_mutex_unlock(&play->schMutex_);
1130 }
1131 }
1132 pthread_mutex_lock(&play->schMutex_);
1133 play->schThreadExit_ = true;
1134 pthread_mutex_unlock(&play->schMutex_);
1135 MEDIA_INFO_LOG("end work");
1136 return nullptr;
1137 }
1138
DoPlay()1139 int32_t PlayerControl::DoPlay()
1140 {
1141 MEDIA_DEBUG_LOG("Process in");
1142 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
1143 CHECK_NULL_RETURN(playerSource_, HI_ERR_PLAYERCONTROL_NULL_PTR, "playerSource_ nullptr");
1144
1145 int32_t ret = HI_SUCCESS;
1146 PlayerStatus playerState = stateMachine_->GetCurState();
1147 CHECK_STATE_SAME(playerState, PLAY_STATUS_PLAY);
1148 if (playerState == PLAY_STATUS_TPLAY) {
1149 ret = OnSwitchTPlay2Play();
1150 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "OnSwitchTPlay2Play failed");
1151 } else if (playerState == PLAY_STATUS_PREPARED) {
1152 ret = CheckMediaInfo();
1153 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "CheckMediaInfo failed");
1154 ret = SetDecoderAndStreamAttr();
1155 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SetDecoderAndStreamAttr failed");
1156 ret = DecoderStart();
1157 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "DecoderStart failed");
1158 ret = SinkStart();
1159 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SinkStart failed");
1160 ret = playerSource_->Start();
1161 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "playerSource_ Start failed");
1162 pthread_mutex_lock(&schMutex_);
1163 paused_ = true;
1164 pthread_mutex_unlock(&schMutex_);
1165 int32_t ret = pthread_create(&schProcess_, nullptr, DataSchProcess, this);
1166 if (ret != 0) {
1167 MEDIA_ERR_LOG("pthread_create failed %d", ret);
1168 pthread_mutex_lock(&schMutex_);
1169 schThreadExit_ = false;
1170 pthread_mutex_unlock(&schMutex_);
1171 return -1;
1172 }
1173 pthread_mutex_lock(&schMutex_);
1174 paused_ = false;
1175 pthread_cond_signal(&schCond_);
1176 pthread_mutex_unlock(&schMutex_);
1177 } else if (playerState == PLAY_STATUS_PAUSE) {
1178 ret = PauseResume();
1179 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "PauseResume failed");
1180 } else {
1181 return HI_ERR_PLAYERCONTROL_ILLEGAL_STATE_ACTION;
1182 }
1183
1184 MEDIA_DEBUG_LOG("Process out");
1185 return ret;
1186 }
1187
DoSetVolume(VolumeAttr & volumeAttr)1188 int32_t PlayerControl::DoSetVolume(VolumeAttr &volumeAttr)
1189 {
1190 leftVolume_ = volumeAttr.leftVolume;
1191 rightVolume_ = volumeAttr.rightVolume;
1192 CHECK_NULL_RETURN(sinkManager_, HI_SUCCESS, "sinkManager_ nullptr, it will be setted after play");
1193 return sinkManager_->SetVolume(volumeAttr.leftVolume, volumeAttr.rightVolume);
1194 }
1195
ReadPacket()1196 int32_t PlayerControl::ReadPacket()
1197 {
1198 int32_t ret = HI_SUCCESS;
1199 if (cachedPacket_) {
1200 return ret;
1201 }
1202 ret = ReadFrameFromSource(formatPacket_);
1203 if (ret != HI_RET_FILE_EOF && (formatPacket_.data == nullptr || formatPacket_.len == 0)) {
1204 return HI_RET_NODATA;
1205 }
1206 if (ret == HI_RET_FILE_EOF) {
1207 CHECK_FAILED_RETURN(memset_s(&formatPacket_, sizeof(formatPacket_), 0, sizeof(FormatFrame)), EOK, -1,
1208 "memset_s failed");
1209 ret = HI_SUCCESS;
1210 } else if (ret == HI_RET_NODATA) {
1211 CHECK_FAILED_RETURN(memset_s(&formatPacket_, sizeof(formatPacket_), 0, sizeof(FormatFrame)), EOK, -1,
1212 "memset_s failed");
1213 strmReadEnd_ = false;
1214 return ret;
1215 } else if (ret != HI_SUCCESS) {
1216 MEDIA_ERR_LOG("ReadFrameFromSource failed , ret:%d", ret);
1217 NotifyError(PLAYERCONTROL_ERROR_DEMUX_FAIL);
1218 return HI_FAILURE;
1219 }
1220 strmReadEnd_ = false;
1221 cachedPacket_ = true;
1222
1223 return ret;
1224 }
1225
IsValidPacket(FormatFrame & packet)1226 inline static bool IsValidPacket(FormatFrame &packet)
1227 {
1228 return (packet.data != nullptr && packet.len != 0) ? true : false;
1229 }
1230
PushPacketToADecoder(void)1231 void PlayerControl::PushPacketToADecoder(void)
1232 {
1233 InputInfo inputData;
1234 CodecBufferInfo inBufInfo;
1235 if (audioDecoder_ == nullptr) {
1236 return;
1237 }
1238 inputData.bufferCnt = 0;
1239 inputData.buffers = nullptr;
1240 inputData.pts = 0;
1241 inputData.flag = 0;
1242 if (memset_s(&inBufInfo, sizeof(inBufInfo), 0, sizeof(CodecBufferInfo)) != EOK) {
1243 return;
1244 }
1245 int32_t ret = audioDecoder_->DequeInputBuffer(inputData, GET_BUFFER_TIMEOUT_MS);
1246 if (ret != 0) {
1247 return;
1248 }
1249 inBufInfo.addr = formatPacket_.data;
1250 inBufInfo.length = formatPacket_.len;
1251 inputData.bufferCnt = 1;
1252 inputData.buffers = &inBufInfo;
1253 inputData.pts = formatPacket_.timestampUs;
1254 inputData.flag = 0;
1255 ret = audioDecoder_->QueueInputBuffer(inputData, GET_BUFFER_TIMEOUT_MS);
1256 if (ret == CODEC_ERR_STREAM_BUF_FULL) {
1257 renderSleepTime_ = QUEUE_BUFFER_FULL_SLEEP_TIME_US;
1258 return;
1259 }
1260 if (firstAudioFrameAfterSeek_ && IsValidPacket(formatPacket_)) {
1261 firstAudioFrameAfterSeek_ = false;
1262 MEDIA_INFO_LOG("push firstAudioFrameAfterSeek_ success, pts:%lld", inputData.pts);
1263 }
1264 if (formatPacket_.data != nullptr || formatPacket_.len != 0) {
1265 lastSendAdecPts_ = formatPacket_.timestampUs;
1266 }
1267 ClearCachePacket();
1268 }
1269
PushPacketToVDecoder(void)1270 void PlayerControl::PushPacketToVDecoder(void)
1271 {
1272 InputInfo inputData;
1273 CodecBufferInfo inBufInfo;
1274 if (videoDecoder_ == nullptr) {
1275 return;
1276 }
1277 inputData.bufferCnt = 0;
1278 inputData.buffers = nullptr;
1279 inputData.pts = 0;
1280 inputData.flag = 0;
1281 if (memset_s(&inBufInfo, sizeof(inBufInfo), 0, sizeof(CodecBufferInfo)) != EOK) {
1282 return;
1283 }
1284 int32_t ret = videoDecoder_->DequeInputBuffer(inputData, GET_BUFFER_TIMEOUT_MS);
1285 if (ret != 0) {
1286 return;
1287 }
1288 inBufInfo.addr = formatPacket_.data;
1289 inBufInfo.length = formatPacket_.len;
1290 inputData.bufferCnt = 1;
1291 inputData.buffers = &inBufInfo;
1292 inputData.pts = formatPacket_.timestampUs;
1293 inputData.flag = 0;
1294 ret = videoDecoder_->QueueInputBuffer(inputData, GET_BUFFER_TIMEOUT_MS);
1295 if (ret == CODEC_ERR_STREAM_BUF_FULL) {
1296 renderSleepTime_ = QUEUE_BUFFER_FULL_SLEEP_TIME_US;
1297 return;
1298 }
1299 if (firstVideoFrameAfterSeek_ && IsValidPacket(formatPacket_)) {
1300 firstVideoFrameAfterSeek_ = false;
1301 MEDIA_INFO_LOG("push firstVideoFrameAfterSeek_ success, pts:%lld", inputData.pts);
1302 }
1303 if (formatPacket_.data != nullptr && formatPacket_.len != 0) {
1304 lastSendVdecPts_ = formatPacket_.timestampUs;
1305 }
1306 ClearCachePacket();
1307 }
1308
ReadPacketAndPushToDecoder()1309 int32_t PlayerControl::ReadPacketAndPushToDecoder()
1310 {
1311 int32_t ret = HI_SUCCESS;
1312 PlayerStatus playerState;
1313 if (stateMachine_ == nullptr) {
1314 return HI_FAILURE;
1315 }
1316 playerState = stateMachine_->GetCurState();
1317 if ((playerState != PLAY_STATUS_PLAY) && (playerState != PLAY_STATUS_TPLAY)) {
1318 return HI_SUCCESS;
1319 }
1320 if (isNeedPause_ == true) {
1321 isNeedPause_ = false;
1322 return HI_SUCCESS;
1323 }
1324 if (strmReadEnd_ == true) {
1325 return HI_SUCCESS;
1326 }
1327 if (playerState == PLAY_STATUS_PLAY) {
1328 ret = ReadPacket();
1329 } else {
1330 ret = ReadTplayData();
1331 }
1332 if (ret == HI_RET_NODATA) {
1333 renderSleepTime_ = NO_DATA_READ_SLEEP_TIME_US;
1334 ClearCachePacket();
1335 return ret;
1336 } else if (ret != HI_SUCCESS) {
1337 ClearCachePacket();
1338 return ret;
1339 }
1340 if (formatPacket_.data == nullptr && formatPacket_.len == 0) {
1341 PushPacketToADecoder();
1342 PushPacketToVDecoder();
1343 strmReadEnd_ = true;
1344 ClearCachePacket();
1345 return HI_SUCCESS;
1346 }
1347 if (formatPacket_.frameType == FRAME_TYPE_AUDIO && audioDecoder_ != nullptr) {
1348 PushPacketToADecoder();
1349 } else if (formatPacket_.frameType == FRAME_TYPE_VIDEO && videoDecoder_ != nullptr) {
1350 PushPacketToVDecoder();
1351 }
1352 return HI_SUCCESS;
1353 }
1354
ReadTplayData()1355 int32_t PlayerControl::ReadTplayData()
1356 {
1357 int32_t ret = HI_SUCCESS;
1358 if (cachedPacket_) {
1359 return ret;
1360 }
1361
1362 ret = TPlayBeforeFrameRead();
1363 if (ret == HI_RET_FILE_EOF) {
1364 if (memset_s(&formatPacket_, sizeof(formatPacket_), 0, sizeof(FormatFrame)) != EOK) {
1365 MEDIA_ERR_LOG("SET formatPacket_ failed");
1366 return HI_FAILURE;
1367 }
1368 cachedPacket_ = true; // send eos
1369 return HI_SUCCESS;
1370 } else if (ret != HI_SUCCESS) {
1371 NotifyError(PLAYERCONTROL_ERROR_DEMUX_FAIL);
1372 return HI_FAILURE;
1373 }
1374 ret = ReadFrameFromSource(formatPacket_);
1375 if (ret == HI_RET_FILE_EOF) {
1376 if (memset_s(&formatPacket_, sizeof(formatPacket_), 0, sizeof(FormatFrame)) != EOK) {
1377 MEDIA_ERR_LOG("SET formatPacket_ failed");
1378 return HI_FAILURE;
1379 }
1380 } else if (ret != HI_SUCCESS) {
1381 MEDIA_ERR_LOG("ReadFrameFromSource failed , ret:%d", ret);
1382 NotifyError(PLAYERCONTROL_ERROR_DEMUX_FAIL);
1383 return HI_FAILURE;
1384 }
1385 cachedPacket_ = true;
1386 lastReadPktStrmIdx_ = formatPacket_.trackId;
1387 ret = TPlayAfterFrameRead(formatPacket_);
1388 if (ret == HI_RET_SKIP_PACKET) {
1389 ClearCachePacket();
1390 return ret;
1391 }
1392 isTplayStartRead_ = true;
1393 strmReadEnd_ = false;
1394 return ret;
1395 }
1396
DoStop()1397 int32_t PlayerControl::DoStop()
1398 {
1399 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
1400 int32_t ret = HI_SUCCESS;
1401 PlayerStatus playerState = stateMachine_->GetCurState();
1402 CHECK_STATE_SAME(playerState, PLAY_STATUS_IDLE);
1403
1404 MsgInfo msgInfo;
1405 if (memset_s(&msgInfo, sizeof(MsgInfo), 0x00, sizeof(msgInfo)) != EOK) {
1406 MEDIA_ERR_LOG("memset_s msgInfo failed");
1407 return HI_ERR_PLAYERCONTROL_MEM_SET;
1408 }
1409 msgInfo.what = PLAYERCONTROL_MSG_SEEK;
1410 (void)stateMachine_->RemoveEvent(msgInfo);
1411 if (schProcess_ != 0) {
1412 pthread_mutex_lock(&schMutex_);
1413 schThreadExit_ = true;
1414 pthread_cond_signal(&schCond_);
1415 pthread_mutex_unlock(&schMutex_);
1416 pthread_join(schProcess_, nullptr);
1417 }
1418 StopSinkAndDecoder();
1419 ClearCachePacket();
1420 if (playerSource_ != nullptr) {
1421 ret = playerSource_->Stop();
1422 CHECK_FAILED_PRINT(ret, HI_SUCCESS, "playerSource_ stop failed");
1423 }
1424 isPlayEnd_ = false;
1425 strmReadEnd_ = false;
1426 isPlayErr_ = false;
1427 lastRendPos_ = 0;
1428 lastReadPktPts_ = 0;
1429 lastSendPktPts_ = 0;
1430 isAudPlayEos_ = false;
1431 isVidPlayEos_ = false;
1432 hasRenderVideoEos_ = false;
1433 hasRenderAudioEos_ = false;
1434 lastNotifyTime_ = 0;
1435 seekToTimeMs_ = -1;
1436 if (memset_s(&(fmtFileInfo_), sizeof(fmtFileInfo_), 0, sizeof(FormatFrame)) != EOK) {
1437 MEDIA_ERR_LOG("SET fmtFileInfo_ failed ");
1438 return HI_ERR_PLAYERCONTROL_MEM_SET;
1439 }
1440 return ret;
1441 }
1442
DoPause(void)1443 int32_t PlayerControl::DoPause(void)
1444 {
1445 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
1446 PlayerStatus playerState = stateMachine_->GetCurState();
1447 CHECK_STATE_SAME(playerState, PLAY_STATUS_PAUSE);
1448 CHECK_NULL_RETURN(sinkManager_, HI_FAILURE, "sinkManager_ nullptr");
1449
1450 if (playerState == PLAY_STATUS_PLAY || (playerState == PLAY_STATUS_TPLAY)) {
1451 pthread_mutex_lock(&schMutex_);
1452 paused_ = true;
1453 pthread_mutex_unlock(&schMutex_);
1454 int32_t ret = sinkManager_->Pause();
1455 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "Pause failed");
1456 } else {
1457 return HI_ERR_PLAYERCONTROL_ILLEGAL_STATE_ACTION;
1458 }
1459 return HI_SUCCESS;
1460 }
1461
DoSeekIfNeed(void)1462 int32_t PlayerControl::DoSeekIfNeed(void)
1463 {
1464 uint32_t index = 0;
1465 uint32_t seekCnt = 0;
1466 if (seekTabel_[0] == -1 && seekTabel_[1] == -1) {
1467 return HI_SUCCESS;
1468 }
1469
1470 while (seekCnt < 0x2) {
1471 if (seekTabel_[index] != -1) {
1472 int32_t ret = AyncSeek(seekTabel_[index]);
1473 if (ret != HI_SUCCESS) {
1474 MEDIA_ERR_LOG(" AyncSeek failed , ret:%d", ret);
1475 NotifyError(PLAYERCONTROL_ERROR_VID_PLAY_FAIL);
1476 return ret;
1477 }
1478 seekCnt++;
1479 }
1480 seekTabel_[index] = -1;
1481 if (seekTabel_[0] == -1 && seekTabel_[1] == -1) {
1482 break;
1483 }
1484 index = (index + 1) & 0x1;
1485 }
1486 strmReadEnd_ = false;
1487 isAudPlayEos_ = false;
1488 isVidPlayEos_ = false;
1489 hasRenderVideoEos_ = false;
1490 hasRenderAudioEos_ = false;
1491 isPlayEnd_ = false;
1492
1493 firstAudioFrameAfterSeek_ = true;
1494 firstVideoFrameAfterSeek_ = true;
1495 MEDIA_INFO_LOG("seek end");
1496 return HI_SUCCESS;
1497 }
1498
DoSeek(int64_t timeInMs)1499 int32_t PlayerControl::DoSeek(int64_t timeInMs)
1500 {
1501 seekToTimeMs_ = timeInMs;
1502 return DoSeekIfNeed();
1503 }
1504
DoGetFileInfo(FormatFileInfo & fileInfo)1505 int32_t PlayerControl::DoGetFileInfo(FormatFileInfo &fileInfo)
1506 {
1507 return memcpy_s(&fileInfo, sizeof(FormatFileInfo), &fmtFileInfo_, sizeof(fmtFileInfo_));
1508 }
1509
DoSetMedia(PlayerControlStreamAttr & mediaAttr)1510 int32_t PlayerControl::DoSetMedia(PlayerControlStreamAttr &mediaAttr)
1511 {
1512 CHECK_NULL_RETURN(playerSource_, HI_ERR_PLAYERCONTROL_NULL_PTR, "playerSource_ nullptr");
1513 int32_t ret = playerSource_->SelectTrack(0, mediaAttr.s32VidStreamId);
1514 CHECK_FAILED_RETURN(ret, 0, HI_ERR_PLAYERCONTROL_DEMUX_ERROR, "SelectTrack failed");
1515 ret = playerSource_->SelectTrack(0, mediaAttr.s32AudStreamId);
1516 CHECK_FAILED_RETURN(ret, 0, HI_ERR_PLAYERCONTROL_DEMUX_ERROR, "SelectTrack failed");
1517 fmtFileInfo_.s32UsedVideoStreamIndex = mediaAttr.s32VidStreamId;
1518 fmtFileInfo_.s32UsedAudioStreamIndex = mediaAttr.s32AudStreamId;
1519 return HI_SUCCESS;
1520 }
1521
GetStreamInfo(PlayerStreamInfo & streamInfo)1522 int32_t PlayerControl::GetStreamInfo(PlayerStreamInfo &streamInfo)
1523 {
1524 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
1525 streamInfo.avStatus.syncStatus.lastAudPts = AV_INVALID_PTS;
1526 streamInfo.avStatus.syncStatus.lastVidPts = AV_INVALID_PTS;
1527 if (sinkManager_->GetStatus(streamInfo) != HI_SUCCESS) {
1528 return HI_FAILURE;
1529 }
1530 PlayerStatus status = stateMachine_->GetCurState();
1531 if (status == PLAY_STATUS_PLAY) {
1532 streamInfo.avStatus.vidStatus = (isVideoStarted_) ? (DecoderStatus)status : DECODER_STATUS_IDLE;
1533 streamInfo.avStatus.audStatus = (isAudioStarted_) ? (DecoderStatus)status : DECODER_STATUS_IDLE;
1534 } else {
1535 streamInfo.avStatus.audStatus = (DecoderStatus)status;
1536 }
1537
1538 streamInfo.audioInfo.channel = adecAttr_.channel;
1539 streamInfo.audioInfo.bitwith = adecAttr_.bitWidth;
1540 streamInfo.audioInfo.sampleRate = adecAttr_.sampleRate;
1541 streamInfo.audioInfo.esBufSize = streamAttr_.audioBufSize;
1542 streamInfo.audioInfo.freeEsBufSize = streamAttr_.audioBufSize >> 1;
1543 streamInfo.videoInfo.esBufSize = streamAttr_.videoBufSize;
1544 streamInfo.videoInfo.freeEsBufSize = streamAttr_.videoBufSize >> 1;
1545 return HI_SUCCESS;
1546 }
1547
1548
IsRepeatTplayReq(TplayAttr & tplayAttr,bool & isRepeat)1549 int32_t PlayerControl::IsRepeatTplayReq(TplayAttr &tplayAttr, bool &isRepeat)
1550 {
1551 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
1552 isRepeat = false;
1553 if (stateMachine_->GetCurState() == PLAY_STATUS_TPLAY &&
1554 tplayAttr_.direction == tplayAttr.direction &&
1555 tplayAttr_.speed == tplayAttr.speed) {
1556 MEDIA_ERR_LOG("TPlay input Attr same with current");
1557 isRepeat = true;
1558 return HI_SUCCESS;
1559 }
1560 PlayerStreamInfo streamInfo;
1561 int64_t lastVidRendPts;
1562 int32_t ret = GetStreamInfo(streamInfo);
1563 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "GetStreamInfo failed");
1564 lastVidRendPts = streamInfo.avStatus.syncStatus.lastVidPts;
1565 if (tplayAttr_.direction == TPLAY_DIRECT_BACKWARD &&
1566 tplayAttr.direction == TPLAY_DIRECT_BACKWARD &&
1567 lastVidRendPts == 0) {
1568 MEDIA_DEBUG_LOG("TPlay already played to start");
1569 isRepeat = true;
1570 return HI_SUCCESS;
1571 }
1572 return HI_SUCCESS;
1573 }
1574
DoTPlay(TplayAttr & trickPlayAttr)1575 int32_t PlayerControl::DoTPlay(TplayAttr &trickPlayAttr)
1576 {
1577 isTplayLastFrame_ = false;
1578 isTplayStartRead_ = false;
1579 if (fmtFileInfo_.enVideoType == CODEC_JPEG) {
1580 MEDIA_ERR_LOG("jpeg file do not support tplay");
1581 return HI_ERR_PLAYERCONTROL_NOT_SUPPORT;
1582 }
1583 if (fmtFileInfo_.s32UsedVideoStreamIndex == HI_DEMUXER_NO_MEDIA_STREAM) {
1584 MEDIA_ERR_LOG("only audio file do not support tplay");
1585 return HI_ERR_PLAYERCONTROL_NOT_SUPPORT;
1586 }
1587 bool isRepeat = false;
1588 int32_t ret = IsRepeatTplayReq(trickPlayAttr, isRepeat);
1589 if (ret != HI_SUCCESS) {
1590 return ret;
1591 }
1592 if (isRepeat) {
1593 return HI_SUCCESS;
1594 }
1595 if (memcpy_s(&tplayAttr_, sizeof(tplayAttr_), &trickPlayAttr, sizeof(trickPlayAttr)) != EOK) {
1596 MEDIA_ERR_LOG("copy tplayAttr_ fail");
1597 return HI_FAILURE;
1598 }
1599 pthread_mutex_lock(&schMutex_);
1600 ret = TPlayResetBuffer();
1601 if (ret != HI_SUCCESS) {
1602 MEDIA_ERR_LOG("TPlayResetBuffer failed, ret:%d", ret);
1603 pthread_mutex_unlock(&schMutex_);
1604 return ret;
1605 }
1606 isVidContinueLost_ = false;
1607 ret = sinkManager_->SetSpeed(tplayAttr_.speed, tplayAttr_.direction);
1608 if (ret != HI_SUCCESS) {
1609 MEDIA_ERR_LOG("TPlayResetBuffer failed, ret:%d", ret);
1610 }
1611 curSeekOffset_ = TPlayGetSeekOffset(tplayAttr_.speed, tplayAttr_.direction);
1612 /* for ts file, double step */
1613 if (fmtFileInfo_.formatName != nullptr && strstr(fmtFileInfo_.formatName, "mpegts") != NULL) {
1614 curSeekOffset_ = curSeekOffset_ * 0x2;
1615 }
1616
1617 tplayMode_ = TPlayGetPlayMode();
1618
1619 lastReadPktStrmIdx_ = (uint32_t)fmtFileInfo_.s32UsedVideoStreamIndex;
1620 strmReadEnd_ = false;
1621 isVidPlayEos_ = false;
1622 isAudPlayEos_ = false;
1623 hasRenderVideoEos_ = false;
1624 hasRenderAudioEos_ = false;
1625 lastSendAdecPts_ = AV_INVALID_PTS;
1626 pthread_mutex_unlock(&schMutex_);
1627 return HI_SUCCESS;
1628 }
1629
1630 // util bigein
EventCallback(PlayerControlEvent event,const void * data)1631 void PlayerControl::EventCallback(PlayerControlEvent event, const void *data)
1632 {
1633 CHECK_NULL_RETURN_VOID(eventCallback_.callbackFun, "callbackFun nullptr");
1634 eventCallback_.callbackFun(eventCallback_.player, event, data);
1635 }
1636
NotifyError(PlayerControlError playerError)1637 void PlayerControl::NotifyError(PlayerControlError playerError)
1638 {
1639 EventCallback(PLAYERCONTROL_EVENT_ERROR, reinterpret_cast<void *>(&playerError));
1640 }
1641
StateChangeCallback(PlayerStatus state)1642 void PlayerControl::StateChangeCallback(PlayerStatus state)
1643 {
1644 EventCallback(PLAYERCONTROL_EVENT_STATE_CHANGED, reinterpret_cast<void *>(&state));
1645 }
1646
UpdateProgressNotify()1647 void PlayerControl::UpdateProgressNotify()
1648 {
1649 CHECK_NULL_RETURN_VOID(stateMachine_, "stateMachine nullptr");
1650 int64_t lastRendPts;
1651 PlayerStreamInfo streamInfo;
1652 uint64_t curTime = PlayerControlGetCurRelativeTime();
1653
1654 if (!isPlayEnd_) {
1655 // First progress is not sended to reduce cpu
1656 lastNotifyTime_ = (!lastNotifyTime_) ? curTime : lastNotifyTime_;
1657 if (lastNotifyTime_ &&
1658 (curTime - lastNotifyTime_) < playerParam_.u32PlayPosNotifyIntervalMs) {
1659 return;
1660 }
1661 }
1662 int32_t ret = GetStreamInfo(streamInfo);
1663 if (ret != HI_SUCCESS) {
1664 MEDIA_ERR_LOG("GetStreamInfo failed , ret:%x", ret);
1665 return;
1666 }
1667 if (fmtFileInfo_.s32UsedAudioStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM &&
1668 stateMachine_->GetCurState() != PLAY_STATUS_TPLAY) {
1669 lastRendPts = streamInfo.avStatus.syncStatus.lastAudPts;
1670 if (lastRendPts < fmtFileInfo_.s64StartTime) {
1671 return;
1672 }
1673 lastRendPts -= fmtFileInfo_.s64StartTime;
1674 if (!isAudPlayEos_) {
1675 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, &lastRendPts);
1676 }
1677 lastNotifyTime_ = curTime;
1678 lastRendPos_ = lastRendPts;
1679 } else if (!isVidPlayEos_) {
1680 lastRendPts = streamInfo.avStatus.syncStatus.lastVidPts;
1681 if (lastRendPts < fmtFileInfo_.s64StartTime) {
1682 return;
1683 }
1684 lastRendPts -= fmtFileInfo_.s64StartTime;
1685 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, &lastRendPts);
1686 lastNotifyTime_ = curTime;
1687 lastRendPos_ = lastRendPts;
1688 }
1689 }
1690
DealPlayEnd()1691 void PlayerControl::DealPlayEnd()
1692 {
1693 CHECK_NULL_RETURN_VOID(stateMachine_, "stateMachine nullptr");
1694 PlayerStatus playState = stateMachine_->GetCurState();
1695 if (tplayAttr_.direction == TPLAY_DIRECT_BACKWARD && playState == PLAY_STATUS_TPLAY) {
1696 EventCallback(PLAYERCONTROL_EVENT_SOF, nullptr);
1697 } else {
1698 if (fmtFileInfo_.s64Duration != -1) {
1699 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, &fmtFileInfo_.s64Duration);
1700 }
1701 EventCallback(PLAYERCONTROL_EVENT_EOF, nullptr);
1702 }
1703 }
1704
TPlayGetPlayMode()1705 PlayerTplayMode PlayerControl::TPlayGetPlayMode()
1706 {
1707 PlayerTplayMode tplayMode = PLAYER_TPLAY_ONLY_I_FRAME;
1708 StreamResolution resolution = { 0 };
1709 if (GetVideoResolution(fmtFileInfo_.s32UsedVideoStreamIndex, resolution) != HI_SUCCESS) {
1710 MEDIA_ERR_LOG("GetVideoResolution failed");
1711 return PLAYER_TPLAY_ONLY_I_FRAME;
1712 }
1713
1714 if (tplayAttr_.direction == TPLAY_DIRECT_FORWARD && tplayAttr_.speed == PLAY_SPEED_2X_FAST) {
1715 if ((resolution.u32Width * resolution.u32Height) <= FULL_TPLAY_RESULITON_LIMIT &&
1716 fmtFileInfo_.fFrameRate <= FULL_TPLAY_FRAMERATE_LIMIT &&
1717 fmtFileInfo_.u32Bitrate <= FULL_TPLAY_BITRATE_LIMIT) {
1718 tplayMode = PLAYER_TPLAY_FULL_PLAY;
1719 }
1720 }
1721 return tplayMode;
1722 }
1723
TPlayGetSeekOffset(float playSpeed,TplayDirect direction)1724 int32_t PlayerControl::TPlayGetSeekOffset(float playSpeed, TplayDirect direction)
1725 {
1726 int32_t seekOffset = 0;
1727 switch ((int)playSpeed) {
1728 case PLAY_SPEED_2X_FAST:
1729 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_2X;
1730 break;
1731 case PLAY_SPEED_4X_FAST:
1732 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_4X;
1733 break;
1734 case PLAY_SPEED_8X_FAST:
1735 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_8X;
1736 break;
1737 case PLAY_SPEED_16X_FAST:
1738 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_16X;
1739 break;
1740 case PLAY_SPEED_32X_FAST:
1741 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_32X;
1742 break;
1743 case PLAY_SPEED_64X_FAST:
1744 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_64X;
1745 break;
1746 case PLAY_SPEED_128X_FAST:
1747 seekOffset = (int32_t)TPLAY_SEEK_OFFSET_128X;
1748 break;
1749 default:
1750 MEDIA_ERR_LOG("unsupported play speed: %f", playSpeed);
1751 break;
1752 }
1753 seekOffset = (direction == TPLAY_DIRECT_BACKWARD) ? (-seekOffset) : seekOffset;
1754 return seekOffset;
1755 }
1756
TPlayResetBuffer()1757 int32_t PlayerControl::TPlayResetBuffer()
1758 {
1759 int32_t ret;
1760 PlayerStreamInfo streamInfo;
1761
1762 if (memset_s(&streamInfo, sizeof(streamInfo), 0x00, sizeof(PlayerStreamInfo)) != EOK) {
1763 return HI_FAILURE;
1764 }
1765 ret = GetStreamInfo(streamInfo);
1766 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "GetStreamInfo failed");
1767 lastReadPktPts_ = currentPosition_;
1768 isTplayLastFrame_ = false;
1769 ClearCachePacket();
1770 ret = DecoderAndSinkReset();
1771 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "DecoderAndSinkReset failed");
1772 return HI_SUCCESS;
1773 }
1774
TPlayCheckContinueLost()1775 int32_t PlayerControl::TPlayCheckContinueLost()
1776 {
1777 int32_t ret = HI_SUCCESS;
1778 if (isVidContinueLost_) {
1779 if (curSeekOffset_ < INT32_MAX / OFFSET_INCREASE_FOR_FRAME_LOST) {
1780 curSeekOffset_ *= OFFSET_INCREASE_FOR_FRAME_LOST;
1781 }
1782 MEDIA_ERR_LOG("vid dec frame slow, increase seekoffset to %d", curSeekOffset_);
1783 ret = TPlayResetBuffer();
1784 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "TPlayResetBuffer failed");
1785 isVidContinueLost_ = false;
1786 }
1787 return HI_SUCCESS;
1788 }
1789
TPlayIsFileReadEnd()1790 bool PlayerControl::TPlayIsFileReadEnd()
1791 {
1792 if (lastReadPktPts_ == 0 && tplayAttr_.direction == TPLAY_DIRECT_BACKWARD) {
1793 MEDIA_DEBUG_LOG("backward last seek pts %lld", lastReadPktPts_);
1794 return true;
1795 } else if (isTplayLastFrame_ == true && (tplayAttr_.direction == TPLAY_DIRECT_FORWARD)) {
1796 MEDIA_DEBUG_LOG("forward last seek pts %lld fmtFileInfo_.s64Duration:%lld", lastReadPktPts_,
1797 fmtFileInfo_.s64Duration);
1798 return true;
1799 }
1800 return false;
1801 }
1802
SeekInTplayMode(int64_t seekTimeInMs,FormatSeekMode seekFlag)1803 int32_t PlayerControl::SeekInTplayMode(int64_t seekTimeInMs, FormatSeekMode seekFlag)
1804 {
1805 CHECK_NULL_RETURN(playerSource_, HI_FAILURE, "playerSource_ nullptr");
1806 int32_t ret = playerSource_->Seek(fmtFileInfo_.s32UsedVideoStreamIndex, seekTimeInMs, seekFlag);
1807 if (ret != HI_SUCCESS) {
1808 // if forward tplay not find the last i frame, then seek again for backword
1809 if (tplayAttr_.direction == TPLAY_DIRECT_FORWARD) {
1810 seekFlag = FORMAT_SEEK_MODE_FORWARD_KEY;
1811 isTplayLastFrame_ = true;
1812 ret = playerSource_->Seek(fmtFileInfo_.s32UsedVideoStreamIndex, seekTimeInMs, seekFlag);
1813 }
1814 if (ret != HI_SUCCESS) {
1815 MEDIA_DEBUG_LOG("playerSource_ seek failed maybe seek to file end, ret:%d", ret);
1816 /* read end */
1817 return HI_RET_FILE_EOF;
1818 }
1819 }
1820 return ret;
1821 }
TPlayBeforeFrameRead()1822 int32_t PlayerControl::TPlayBeforeFrameRead()
1823 {
1824 int32_t ret = HI_SUCCESS;
1825 if (tplayMode_ != PLAYER_TPLAY_ONLY_I_FRAME) {
1826 return ret;
1827 }
1828
1829 ret = TPlayCheckContinueLost();
1830 if (ret != HI_SUCCESS) {
1831 return ret;
1832 }
1833 /* last packet should be skip if streamidx is not playing video stream */
1834 if (lastReadPktStrmIdx_ == (uint32_t)fmtFileInfo_.s32UsedVideoStreamIndex) {
1835 int64_t seekTimeInMs = lastReadPktPts_ + curSeekOffset_;
1836 if (TPlayIsFileReadEnd()) {
1837 return HI_RET_FILE_EOF;
1838 }
1839 FormatSeekMode seekFlag = (tplayAttr_.direction == TPLAY_DIRECT_BACKWARD) ? FORMAT_SEEK_MODE_BACKWARD_KEY :
1840 FORMAT_SEEK_MODE_FORWARD_KEY;
1841 if (seekTimeInMs < 0 && tplayAttr_.direction == TPLAY_DIRECT_BACKWARD) {
1842 seekTimeInMs = 0;
1843 isTplayLastFrame_ = true;
1844 } else if (seekTimeInMs > fmtFileInfo_.s64Duration && tplayAttr_.direction == TPLAY_DIRECT_FORWARD) {
1845 seekTimeInMs = fmtFileInfo_.s64Duration;
1846 seekFlag = FORMAT_SEEK_MODE_BACKWARD_KEY;
1847 isTplayLastFrame_ = true;
1848 } else if (lastReadPktPts_ == 0 && isTplayStartRead_ == false) {
1849 seekTimeInMs = 0;
1850 seekFlag = FORMAT_SEEK_MODE_BACKWARD_KEY;
1851 isTplayLastFrame_ = false;
1852 }
1853 ret = SeekInTplayMode(seekTimeInMs, seekFlag);
1854 if (ret != HI_SUCCESS) {
1855 return ret;
1856 }
1857 }
1858 return HI_SUCCESS;
1859 }
1860
TPlayAfterFrameRead(FormatFrame & packet)1861 int32_t PlayerControl::TPlayAfterFrameRead(FormatFrame &packet)
1862 {
1863 int32_t ret = HI_SUCCESS;
1864 bool isSkipPkt = false;
1865
1866 if ((int)packet.trackId == fmtFileInfo_.s32UsedVideoStreamIndex) {
1867 if ((packet.timestampUs == lastSendPktPts_) && (isTplayStartRead_ == true)) {
1868 lastReadPktPts_ += curSeekOffset_;
1869 isSkipPkt = true;
1870 } else {
1871 lastReadPktPts_ = packet.timestampUs;
1872 }
1873 } else {
1874 lastReadPktPts_ = packet.timestampUs;
1875 isSkipPkt = true;
1876 }
1877 if (isSkipPkt) {
1878 ret = HI_RET_SKIP_PACKET;
1879 }
1880 return ret;
1881 }
1882
FlushDecoder(void)1883 void PlayerControl::FlushDecoder(void)
1884 {
1885 ReleaseADecoderOutputFrame();
1886 ReleaseVDecoderOutputFrame();
1887 if (audioDecoder_ != nullptr) {
1888 audioDecoder_->FlushDec();
1889 }
1890 if (videoDecoder_ != nullptr) {
1891 videoDecoder_->FlushDec();
1892 }
1893 }
1894
DecoderAndSinkReset(void)1895 int32_t PlayerControl::DecoderAndSinkReset(void)
1896 {
1897 int32_t ret;
1898 bool isNeedResume = false;
1899 ret = sinkManager_->Pause();
1900 if ((ret == HI_SUCCESS) && (paused_ == false)) {
1901 isNeedResume = true;
1902 }
1903 ret = sinkManager_->Reset();
1904 if (ret != HI_SUCCESS) {
1905 MEDIA_ERR_LOG("m_render reset failed");
1906 (void)sinkManager_->Resume();
1907 return ret;
1908 }
1909 FlushDecoder();
1910 if (isNeedResume) {
1911 ret = sinkManager_->Resume();
1912 CHECK_FAILED_RETURN(ret, HI_SUCCESS, HI_FAILURE, "sinkManager_ Resume failed");
1913 }
1914 return HI_SUCCESS;
1915 }
1916
AyncSeek(int64_t seekTime)1917 int32_t PlayerControl::AyncSeek(int64_t seekTime)
1918 {
1919 CHECK_NULL_RETURN(playerSource_, HI_FAILURE, "playerSource_ nullptr");
1920 int64_t seekTimeInMs = seekTime;
1921 int32_t ret = DecoderAndSinkReset();
1922 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "DecoderAndSinkReset failed");
1923 ClearCachePacket();
1924 if (fmtFileInfo_.s32UsedVideoStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM) {
1925 ret = playerSource_->Seek(fmtFileInfo_.s32UsedVideoStreamIndex, seekTimeInMs, FORMAT_SEEK_MODE_BACKWARD_KEY);
1926 if (ret != HI_SUCCESS) {
1927 MEDIA_INFO_LOG("exec fmt_seek video stream failed, ret:%d", ret);
1928 seekTimeInMs = currentPosition_;
1929 }
1930 } else if (fmtFileInfo_.s32UsedAudioStreamIndex != HI_DEMUXER_NO_MEDIA_STREAM) {
1931 ret = playerSource_->Seek(fmtFileInfo_.s32UsedAudioStreamIndex, seekTimeInMs, FORMAT_SEEK_MODE_BACKWARD_KEY);
1932 if (ret != HI_SUCCESS) {
1933 MEDIA_INFO_LOG("exec fmt_seek audio stream failed, ret:%d", ret);
1934 seekTimeInMs = currentPosition_;
1935 }
1936 }
1937 currentPosition_ = seekTimeInMs;
1938
1939 if (tplayAttr_.speed != 1.0f) {
1940 lastReadPktPts_ = currentPosition_;
1941 isTplayStartRead_ = (currentPosition_ == 0) ? false : true;
1942 isTplayLastFrame_ = false;
1943 }
1944
1945 EventCallback(PLAYERCONTROL_EVENT_PROGRESS, ¤tPosition_);
1946 EventCallback(PLAYERCONTROL_EVENT_SEEK_END, reinterpret_cast<void *>(&seekTimeInMs));
1947 return HI_SUCCESS;
1948 }
1949
GetPlayElementEventCallBack(PlayEventCallback & callback)1950 void PlayerControl::GetPlayElementEventCallBack(PlayEventCallback &callback)
1951 {
1952 callback.onEventCallback = PlayerControlOnEvent;
1953 callback.priv = reinterpret_cast<void *>(this);
1954 }
SyncPrepare()1955 int32_t PlayerControl::SyncPrepare()
1956 {
1957 int ret;
1958
1959 playerSource_ = std::make_shared<PlayerSource>();
1960 CHECK_NULL_RETURN(playerSource_, HI_FAILURE, "new playerSource_ nullptr");
1961 playerSource_->Init();
1962
1963 if (sourceType_ == SOURCE_TYPE_FD) {
1964 playerSource_->SetSource(fd_);
1965 } else if (sourceType_ == SOURCE_TYPE_STREAM) {
1966 playerSource_->SetSource(stream_);
1967 } else {
1968 playerSource_->SetSource(filePath_.c_str());
1969 }
1970
1971 PlayEventCallback callback;
1972 GetPlayElementEventCallBack(callback);
1973 ret = playerSource_->SetCallBack(callback);
1974 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "SetCallBack failed");
1975
1976 ret = playerSource_->Prepare();
1977 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "Prepare failed");
1978 ret = playerSource_->GetFileInfo(fmtFileInfo_);
1979 CHECK_FAILED_RETURN(ret, HI_SUCCESS, ret, "GetFileInfo failed");
1980 MEDIA_INFO_LOG("used audiostream index %d", fmtFileInfo_.s32UsedAudioStreamIndex);
1981 MEDIA_INFO_LOG("used videostream index %d", fmtFileInfo_.s32UsedVideoStreamIndex);
1982 return HI_SUCCESS;
1983 }
1984
IsPlayEos()1985 bool PlayerControl::IsPlayEos()
1986 {
1987 CHECK_NULL_RETURN(stateMachine_, false, "stateMachine_ nullptr");
1988 PlayerStatus playerState = stateMachine_->GetCurState();
1989 if (playerState == PLAY_STATUS_TPLAY && hasRenderVideoEos_) {
1990 return true;
1991 }
1992 if ((!isAudioStarted_ || hasRenderAudioEos_) && (!isVideoStarted_ || hasRenderVideoEos_)) {
1993 return true;
1994 }
1995 return false;
1996 }
1997
CheckMediaType(FormatFileInfo & fmtFileInfo)1998 int32_t PlayerControl::CheckMediaType(FormatFileInfo &fmtFileInfo)
1999 {
2000 if (fmtFileInfo.s32UsedVideoStreamIndex == HI_DEMUXER_NO_MEDIA_STREAM) {
2001 return HI_SUCCESS;
2002 }
2003 if ((fmtFileInfo.enVideoType == CODEC_H264)
2004 || (fmtFileInfo.enVideoType == CODEC_H265)
2005 || (fmtFileInfo.enVideoType == CODEC_JPEG)) {
2006 return HI_SUCCESS;
2007 }
2008 MEDIA_ERR_LOG("video type: %d not supported", fmtFileInfo.enVideoType);
2009 return HI_ERR_PLAYERCONTROL_NOT_SUPPORT;
2010 }
2011
Invoke(PlayerInvoke invokeId,void * param)2012 int32_t PlayerControl::Invoke(PlayerInvoke invokeId, void *param)
2013 {
2014 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
2015 MsgInfo msg;
2016 InvokeParameter invokeParam;
2017 invokeParam.id = invokeId;
2018 invokeParam.param = param;
2019
2020 msg.what = PLAYERCONTROL_MSG_INVOKE;
2021 msg.msgData = &invokeParam;
2022 msg.msgDataLen = sizeof(InvokeParameter);
2023 return stateMachine_->Send(msg);
2024 }
2025
EnablePauseAfterPlay(bool pauseAfterPlay)2026 int32_t PlayerControl::EnablePauseAfterPlay(bool pauseAfterPlay)
2027 {
2028 PlayerStatus playerState = stateMachine_->GetCurState();
2029 if (playerState != PLAY_STATUS_IDLE && playerState != PLAY_STATUS_INIT) {
2030 MEDIA_ERR_LOG("unsupport set play mode, state:%d\n", playerState);
2031 return -1;
2032 }
2033 pauseMode_ = pauseAfterPlay;
2034 return 0;
2035 }
2036
DoInvoke(InvokeParameter & invokeParam)2037 int32_t PlayerControl::DoInvoke(InvokeParameter& invokeParam)
2038 {
2039 int32_t ret = -1;
2040
2041 switch (invokeParam.id) {
2042 case INVOKE_ENABLE_PAUSE_AFTER_PLAYER:
2043 if (invokeParam.param == nullptr) {
2044 break;
2045 }
2046 ret = EnablePauseAfterPlay((*((uint32_t *)invokeParam.param)) > 0 ? true : false);
2047 break;
2048 default:
2049 MEDIA_ERR_LOG("unsupport invoke:0x%x\n", invokeParam.id);
2050 break;
2051 }
2052 return ret;
2053 }
2054
SetAudioStreamType(int32_t type)2055 int32_t PlayerControl::SetAudioStreamType(int32_t type)
2056 {
2057 CHECK_NULL_RETURN(stateMachine_, HI_ERR_PLAYERCONTROL_NULL_PTR, "stateMachine_ nullptr");
2058 MsgInfo msg;
2059 int32_t audioStreamType = type;
2060
2061 msg.what = PLAYERCONTROL_MSG_SET_AUDIOSTREAM_TYPE;
2062 msg.msgData = &audioStreamType;
2063 msg.msgDataLen = sizeof(int32_t);
2064 return stateMachine_->Send(msg);
2065 }
2066
DoSetAudioStreamType(int32_t type)2067 int32_t PlayerControl::DoSetAudioStreamType(int32_t type)
2068 {
2069 audioStreamType_ = type;
2070 if (sinkManager_ != nullptr) {
2071 sinkManager_->SetAudioStreamType(type);
2072 }
2073 return 0;
2074 }
2075 }
2076 }
2077