blob: 6128206fe2f9d040525a8987aba0c913c207284d [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001#include <stdio.h>
2#include <stdlib.h>
3
hualing chen5cbe1a62020-02-10 16:36:36 +08004#include <string.h>
hualing chenb31a6c62020-01-13 17:27:00 +08005#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <poll.h>
11#include <errno.h>
12#include <signal.h>
13#include <pthread.h>
hualing chenb5cd42e2020-04-15 17:03:34 +080014#include <errno.h>
hualing chenb31a6c62020-01-13 17:27:00 +080015
hualing chenb31a6c62020-01-13 17:27:00 +080016#include "dvr_playback.h"
17
hualing chen4b7c15d2020-04-07 16:13:48 +080018#define DVR_PB_DG(_level, _fmt, ...) \
19 DVR_DEBUG(_level, "playback %-30.30s:%d " _fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
20
hualing chena540a7e2020-03-27 16:44:05 +080021
hualing chenb31a6c62020-01-13 17:27:00 +080022#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080023
24
25#define FF_SPEED (2.0f)
26#define FB_SPEED (-1.0f)
27#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080028#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080029
30#define IS_KERNEL_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_X1) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
31#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
32
hualing chenb31a6c62020-01-13 17:27:00 +080033
hualing chenb5cd42e2020-04-15 17:03:34 +080034#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080035#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080036//if tsplayer delay time < 200 and no data can read, we will pause
37#define MIN_TSPLAYER_DELAY_TIME (200)
38
hualing chen041c4092020-04-05 15:11:50 +080039#define MAX_CACHE_TIME (30000)
40
hualing chena540a7e2020-03-27 16:44:05 +080041static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080042//
43static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chencc91e1c2020-02-28 13:26:17 +080044static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type);
45static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
46static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080047static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080048static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080049static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
50 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080051static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen87072a82020-03-12 16:20:12 +080052
hualing chenbcada022020-04-22 14:27:01 +080053
54static char* _cmd_toString(int cmd)
55{
56
57 char *string[DVR_PLAYBACK_CMD_NONE+1]={
58 "start",
59 "stop",
60 "vstart",
61 "astart",
62 "vstop",
63 "astop",
64 "vrestart",
65 "arestart",
66 "avrestart",
67 "vstopastart",
68 "astopvstart",
69 "vstoparestart",
70 "astopvrestart",
71 "vstartarestart",
72 "astartvrestart",
73 "pause",
74 "resume",
75 "seek",
76 "ff",
77 "fb",
78 "NONE"
79 };
80
81 if (cmd > DVR_PLAYBACK_CMD_NONE) {
82 return "unkown";
83 } else {
84 return string[cmd];
85 }
86}
87
88
hualing chen6d24aa92020-03-23 18:43:47 +080089static char* _dvr_playback_state_toString(int stat)
90{
91 char *string[DVR_PLAYBACK_STATE_FB+1]={
92 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080093 "stop",
hualing chen31140872020-03-25 12:29:26 +080094 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +080095 "ff",
96 "fb"
97 };
98
99 if (stat > DVR_PLAYBACK_STATE_FB) {
100 return "unkown";
101 } else {
102 return string[stat];
103 }
104}
hualing chena540a7e2020-03-27 16:44:05 +0800105
106static DVR_Bool_t _dvr_support_speed(int speed) {
107
108 DVR_Bool_t ret = DVR_FALSE;
109
110 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800111 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800112 case PLAYBACK_SPEED_FBX2:
113 case PLAYBACK_SPEED_FBX4:
114 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800115 case PLAYBACK_SPEED_FBX16:
116 case PLAYBACK_SPEED_FBX12:
117 case PLAYBACK_SPEED_FBX32:
118 case PLAYBACK_SPEED_FBX48:
119 case PLAYBACK_SPEED_FBX64:
120 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800121 case PLAYBACK_SPEED_S2:
122 case PLAYBACK_SPEED_S4:
123 case PLAYBACK_SPEED_S8:
124 case PLAYBACK_SPEED_X1:
125 case PLAYBACK_SPEED_X2:
126 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800127 case PLAYBACK_SPEED_X3:
128 case PLAYBACK_SPEED_X5:
129 case PLAYBACK_SPEED_X6:
130 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800131 case PLAYBACK_SPEED_X8:
132 case PLAYBACK_SPEED_X12:
133 case PLAYBACK_SPEED_X16:
134 case PLAYBACK_SPEED_X32:
135 case PLAYBACK_SPEED_X48:
136 case PLAYBACK_SPEED_X64:
137 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800138 ret = DVR_TRUE;
139 break;
140 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800141 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800142 break;
143 }
144 return ret;
145}
hualing chen6e4bfa52020-03-13 14:37:11 +0800146void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
147{
hualing chen4b7c15d2020-04-07 16:13:48 +0800148 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800149 DVR_Playback_t *player = NULL;
150 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800151 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800152 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800153 }
154 switch (event->type) {
155 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
156 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800157 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800158 event->event.video_format.frame_width,
159 event->event.video_format.frame_height,
160 event->event.video_format.frame_rate);
161 break;
162 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800163 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
164 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800165 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800166 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800167 break;
168 }
169 default:
170 break;
171 }
172}
hualing chen2aba4022020-03-02 13:49:55 +0800173void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
174{
hualing chen6e4bfa52020-03-13 14:37:11 +0800175 DVR_Playback_t *player = NULL;
176 if (user_data != NULL) {
177 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800178 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800179 }
hualing chen2aba4022020-03-02 13:49:55 +0800180 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800181 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
182 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800183 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800184 event->event.video_format.frame_width,
185 event->event.video_format.frame_height,
186 event->event.video_format.frame_rate);
187 break;
188 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800189 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
190 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800191 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800192 if (player->first_trans_ok == DVR_FALSE) {
193 player->first_trans_ok = DVR_TRUE;
194 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
195 }
hualing chena540a7e2020-03-27 16:44:05 +0800196 if (player != NULL)
197 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800198 break;
199 }
hualing chen487ae6d2020-07-22 10:34:11 +0800200 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
201 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
202 player->first_trans_ok = DVR_TRUE;
203 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
204 }
205 if (player != NULL && player->has_video == DVR_FALSE) {
206 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
207 player->first_frame = 1;
208 }
209 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800210 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800211 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800212 break;
213 }
214 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800215 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800216 player->player_callback_func(player->player_callback_userdata, event);
217 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800218 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800219 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800220 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800221 }
222}
hualing chencc91e1c2020-02-28 13:26:17 +0800223
hualing chen5cbe1a62020-02-10 16:36:36 +0800224//convert video and audio fmt
225static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
226 int format = 0;
227 if (is_audio == DVR_FALSE) {
228 //for video fmt
229 switch (fmt)
230 {
231 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800232 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800233 break;
234 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800235 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800236 break;
237 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800238 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800239 break;
240 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800241 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800242 break;
hualing chena540a7e2020-03-27 16:44:05 +0800243 case DVR_VIDEO_FORMAT_VP9:
244 format = AV_VIDEO_CODEC_VP9;
245 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800246 }
247 } else {
248 //for audio fmt
249 switch (fmt)
250 {
251 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800252 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800253 break;
254 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800255 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800256 break;
257 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800258 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800259 break;
260 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800261 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800262 break;
hualing chena540a7e2020-03-27 16:44:05 +0800263 case DVR_AUDIO_FORMAT_AAC:
264 format = AV_AUDIO_CODEC_AAC;
265 break;
266 case DVR_AUDIO_FORMAT_LATM:
267 format = AV_AUDIO_CODEC_LATM;
268 break;
269 case DVR_AUDIO_FORMAT_PCM:
270 format = AV_AUDIO_CODEC_PCM;
271 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800272 }
273 }
274 return format;
275}
hualing chen040df222020-01-17 13:35:02 +0800276static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800277{
hualing chen040df222020-01-17 13:35:02 +0800278 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800279
hualing chena540a7e2020-03-27 16:44:05 +0800280 if (player == NULL || player->handle == NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800281 return -1;
282
hualing chena540a7e2020-03-27 16:44:05 +0800283 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800284}
hualing chena540a7e2020-03-27 16:44:05 +0800285
hualing chen5cbe1a62020-02-10 16:36:36 +0800286//get sys time ms
287static int _dvr_time_getClock(void)
288{
289 struct timespec ts;
290 int ms;
291
292 clock_gettime(CLOCK_MONOTONIC, &ts);
293 ms = ts.tv_sec*1000+ts.tv_nsec/1000000;
294
295 return ms;
296}
hualing chen86e7d482020-01-16 15:13:33 +0800297
hualing chenb31a6c62020-01-13 17:27:00 +0800298
299//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800300static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800301{
hualing chen040df222020-01-17 13:35:02 +0800302 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800303
hualing chena540a7e2020-03-27 16:44:05 +0800304
305 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800306 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800307 return DVR_FAILURE;
308 }
309
hualing chen86e7d482020-01-16 15:13:33 +0800310 struct timespec ts;
311 clock_gettime(CLOCK_MONOTONIC, &ts);
312 //ms为毫秒,换算成秒
313 ts.tv_sec += ms/1000;
314 //在outtime的基础上,增加ms毫秒
315 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
316 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
317 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
318 //us的值有可能超过1秒,
319 ts.tv_sec += us / 1000000;
320 us = us % 1000000;
321 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800322 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
323 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800324}
hualing chen31140872020-03-25 12:29:26 +0800325//get tsplay delay time ms
326static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
327 DVR_Playback_t *player = (DVR_Playback_t *) handle;
328 int64_t cache = 0;
329 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800330 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800331 return 0;
332 }
333 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800334 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800335 return cache;
336}
hualing chenb31a6c62020-01-13 17:27:00 +0800337//send signal
hualing chen040df222020-01-17 13:35:02 +0800338static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800339{
hualing chen87072a82020-03-12 16:20:12 +0800340 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800341
342 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800343 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800344 return DVR_FAILURE;
345 }
346
hualing chen87072a82020-03-12 16:20:12 +0800347 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800348 pthread_cond_signal(&player->cond);
hualing chen87072a82020-03-12 16:20:12 +0800349 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800350 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800351}
352
hualing chen2932d372020-04-29 13:44:00 +0800353//send playback event, need check is need lock first
354static int _dvr_playback_sent_event(DVR_PlaybackHandle_t handle, DVR_PlaybackEvent_t evt, DVR_Play_Notify_t *notify, DVR_Bool_t is_lock) {
hualing chencc91e1c2020-02-28 13:26:17 +0800355
356 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800357
358 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800359 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800360 return DVR_FAILURE;
361 }
362
hualing chencc91e1c2020-02-28 13:26:17 +0800363 switch (evt) {
364 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800365 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800366 break;
367 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
368 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800369 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800370 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800371 break;
372 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
373 break;
374 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
375 break;
376 case DVR_PLAYBACK_EVENT_NO_KEY:
377 break;
378 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800379 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800380 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800381 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800382 break;
383 case DVR_PLAYBACK_EVENT_REACHED_END:
384 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800385 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800386 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800387 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800388 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800389 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800390 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800391 default:
392 break;
393 }
394 if (player->openParams.event_fn != NULL)
395 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800396 return DVR_SUCCESS;
397}
hualing chen2932d372020-04-29 13:44:00 +0800398static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800399{
400 DVR_Play_Notify_t notify;
401 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
402 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
403 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800404 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800405 return DVR_SUCCESS;
406}
407
hualing chen2932d372020-04-29 13:44:00 +0800408static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800409{
410 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800411
hualing chene3797f02021-01-13 14:53:28 +0800412 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800413 return DVR_SUCCESS;
414 }
hualing chena540a7e2020-03-27 16:44:05 +0800415 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800416 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800417 return DVR_FAILURE;
418 }
419
hualing chen6e4bfa52020-03-13 14:37:11 +0800420 if (player->send_time ==0) {
hualing chen0888c032020-12-18 17:54:57 +0800421 player->send_time = _dvr_time_getClock() + 500;
422 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800423 return DVR_SUCCESS;
424 }
hualing chen0888c032020-12-18 17:54:57 +0800425 player->send_time = _dvr_time_getClock() + 500;
hualing chen6e4bfa52020-03-13 14:37:11 +0800426 DVR_Play_Notify_t notify;
427 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
428 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
429 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800430 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800431 return DVR_SUCCESS;
432}
433
hualing chencc91e1c2020-02-28 13:26:17 +0800434//check is ongoing segment
435static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
436
437 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800438 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800439
440 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800441 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800442 return DVR_FAILURE;
443 }
hualing chen87072a82020-03-12 16:20:12 +0800444 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800445 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800446 return DVR_FALSE;
447 }
hualing chencc91e1c2020-02-28 13:26:17 +0800448 return DVR_TRUE;
449}
hualing chen4b7c15d2020-04-07 16:13:48 +0800450
451
452static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
453 DVR_Playback_t *player = (DVR_Playback_t *) handle;
454 player->fffb_start = _dvr_time_getClock();
455 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
456 player->fffb_current = player->fffb_start;
457 //get segment current time pos
458 player->fffb_start_pcr = _dvr_get_cur_time(handle);
459 //player->fffb_current = -1;
460 //player->fffb_start = -1;
461 //player->fffb_start_pcr = -1;
462 player->next_fffb_time = _dvr_time_getClock();
463
464 return DVR_SUCCESS;
465}
466
hualing chen2aba4022020-03-02 13:49:55 +0800467static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
468 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800469 player->fffb_start = _dvr_time_getClock();
470 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
471 player->fffb_current = player->fffb_start;
472 //get segment current time pos
473 player->fffb_start_pcr = _dvr_get_cur_time(handle);
474 //player->fffb_current = -1;
475 //player->fffb_start = -1;
476 //player->fffb_start_pcr = -1;
hualing chen2aba4022020-03-02 13:49:55 +0800477 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800478 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800479 return DVR_SUCCESS;
480}
hualing chencc91e1c2020-02-28 13:26:17 +0800481//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800482static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
483
484 DVR_Playback_t *player = (DVR_Playback_t *) handle;
485 DVR_PlaybackSegmentInfo_t *segment;
486 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
487
hualing chena540a7e2020-03-27 16:44:05 +0800488 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800489 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800490 return DVR_FAILURE;
491 }
492
hualing chen87072a82020-03-12 16:20:12 +0800493 int found = 0;
494 int found_eq_id = 0;
495 list_for_each_entry(segment, &player->segment_list, head)
496 {
497 if (player->segment_is_open == DVR_FALSE) {
498 //get first segment from list, case segment is not open
499 if (!IS_FB(player->speed))
500 found = 1;
501 } else if (segment->segment_id == segmentid) {
502 //find cur segment, we need get next one
503 found_eq_id = 1;
504 if (!IS_FB(player->speed)) {
505 found = 1;
506 continue;
507 } else {
508 //if is fb mode.we need used pre segment
509 if (pre_segment != NULL) {
510 found = 1;
511 } else {
512 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800513 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800514 return DVR_FAILURE;
515 }
516 }
517 }
518 if (found == 1) {
519 found = 2;
520 break;
521 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800522 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800523 }
524 if (found != 2) {
525 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800526 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800527 return DVR_FAILURE;
528 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800529 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800530 return DVR_SUCCESS;
531}
532
533//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800534static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800535
hualing chen040df222020-01-17 13:35:02 +0800536 DVR_Playback_t *player = (DVR_Playback_t *) handle;
537 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800538 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800539
hualing chena540a7e2020-03-27 16:44:05 +0800540 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800541 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800542 return DVR_FAILURE;
543 }
544
hualing chen86e7d482020-01-16 15:13:33 +0800545 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800546 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800547
hualing chen040df222020-01-17 13:35:02 +0800548 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800549 {
hualing chencc91e1c2020-02-28 13:26:17 +0800550 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800551 //get first segment from list, case segment is not open
552 if (!IS_FB(player->speed))
553 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800554 } else if (segment->segment_id == player->cur_segment_id) {
555 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800556 found_eq_id = 1;
557 if (!IS_FB(player->speed)) {
558 found = 1;
559 continue;
560 } else {
561 //if is fb mode.we need used pre segment
562 if (pre_segment != NULL) {
563 found = 1;
564 } else {
565 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800566 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800567 return DVR_FAILURE;
568 }
569 }
hualing chen86e7d482020-01-16 15:13:33 +0800570 }
571 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800572 if (IS_FB(player->speed)) {
573 //used pre segment
574 segment = pre_segment;
575 }
hualing chencc91e1c2020-02-28 13:26:17 +0800576 //save segment info
577 player->last_segment_id = player->cur_segment_id;
hualing chen87072a82020-03-12 16:20:12 +0800578 player->last_segment.segment_id = player->cur_segment.segment_id;
579 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800580 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
581 //pids
582 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
583
hualing chen5cbe1a62020-02-10 16:36:36 +0800584 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800585 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800586 player->cur_segment_id = segment->segment_id;
587 player->cur_segment.segment_id = segment->segment_id;
588 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800589 DVR_PB_DG(1, "set cur id cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, segment->flags, segment->segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800590 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800591 //pids
hualing chen040df222020-01-17 13:35:02 +0800592 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800593 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800594 break;
hualing chen86e7d482020-01-16 15:13:33 +0800595 }
hualing chen2aba4022020-03-02 13:49:55 +0800596 pre_segment = segment;
597 }
598 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
599 //used the last one segment to open
600 //get segment info
601 player->segment_is_open = DVR_TRUE;
602 player->cur_segment_id = pre_segment->segment_id;
603 player->cur_segment.segment_id = pre_segment->segment_id;
604 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800605 DVR_PB_DG(1, "set cur id fb last one cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, pre_segment->flags, pre_segment->segment_id);
hualing chen2aba4022020-03-02 13:49:55 +0800606 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
607 //pids
608 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
609 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800610 }
611 if (found != 2) {
612 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800613 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800614 }
615 return DVR_SUCCESS;
616}
hualing chen040df222020-01-17 13:35:02 +0800617//open next segment to play,if reach list end return errro.
618static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800619{
hualing chen040df222020-01-17 13:35:02 +0800620 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800621 Segment_OpenParams_t params;
622 int ret = DVR_SUCCESS;
623
hualing chena540a7e2020-03-27 16:44:05 +0800624 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800625 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800626 return DVR_FAILURE;
627 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800628 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800629
630 ret = _dvr_get_next_segmentId(handle);
631 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800632 DVR_PB_DG(1, "not found segment info");
633 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800634 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800635 }
636
637 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800638 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800639 segment_close(player->r_handle);
640 player->r_handle = NULL;
641 }
642
643 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800644 //cp chur segment path to location
645 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800646 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800647 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800648 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
649
hualing chen86e7d482020-01-16 15:13:33 +0800650 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800651 if (ret == DVR_FAILURE) {
652 DVR_PB_DG(1, "open segment error");
653 }
hualing chen87072a82020-03-12 16:20:12 +0800654 pthread_mutex_unlock(&player->segment_lock);
655 int total = _dvr_get_end_time( handle);
656 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800657 if (IS_FB(player->speed)) {
658 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800659 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800660 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800661 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800662 }
hualing chen87072a82020-03-12 16:20:12 +0800663 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800664 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800665 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800666 return ret;
667}
668
hualing chen5cbe1a62020-02-10 16:36:36 +0800669//open next segment to play,if reach list end return errro.
670static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
671{
672 DVR_Playback_t *player = (DVR_Playback_t *) handle;
673 Segment_OpenParams_t params;
674 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800675 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800676 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800677 return DVR_FAILURE;
678 }
hualing chencc91e1c2020-02-28 13:26:17 +0800679 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800680 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800681 }
hualing chencc91e1c2020-02-28 13:26:17 +0800682 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800683 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800684 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800685 return DVR_FAILURE;
686 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800687 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800688 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800689
690 DVR_PlaybackSegmentInfo_t *segment;
691
692 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800693
hualing chen5cbe1a62020-02-10 16:36:36 +0800694 list_for_each_entry(segment, &player->segment_list, head)
695 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800696 DVR_PB_DG(1, "see 1 location [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800697 if (segment->segment_id == segment_id) {
698 found = 1;
699 }
700 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800701 DVR_PB_DG(1, "found [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800702 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800703 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800704 player->cur_segment_id = segment->segment_id;
705 player->cur_segment.segment_id = segment->segment_id;
706 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800707 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800708 //pids
709 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800710 DVR_PB_DG(1, "cur found location [%s]id[%lld]flag[%x]", player->cur_segment.location, player->cur_segment.segment_id,player->cur_segment.flags);
hualing chencc91e1c2020-02-28 13:26:17 +0800711 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800712 }
713 }
hualing chencc91e1c2020-02-28 13:26:17 +0800714 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800715 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800716 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800717 return DVR_FAILURE;
718 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800719 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800720 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800721 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800722 params.segment_id = (uint64_t)player->cur_segment.segment_id;
723 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800724 DVR_PB_DG(1, "open segment location[%s][%lld]cur flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
hualing chen2aba4022020-03-02 13:49:55 +0800725 if (player->r_handle != NULL) {
726 segment_close(player->r_handle);
727 player->r_handle = NULL;
728 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800729 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800730 if (ret == DVR_FAILURE) {
731 DVR_PB_DG(1, "segment opne error");
732 }
hualing chen2aba4022020-03-02 13:49:55 +0800733 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800734 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800735
hualing chen4b7c15d2020-04-07 16:13:48 +0800736 DVR_PB_DG(1, "player->dur [%d]cur id [%lld]cur flag [0x%x]\r\n", player->dur,player->cur_segment.segment_id, player->cur_segment.flags);
hualing chen5cbe1a62020-02-10 16:36:36 +0800737 return ret;
738}
739
740
741//get play info by segment id
742static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
743 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800744 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800745 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800746
747 DVR_Playback_t *player = (DVR_Playback_t *) handle;
748 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800749 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800750 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800751 return DVR_FAILURE;
752 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800753
754 int found = 0;
755
756 list_for_each_entry(segment, &player->segment_list, head)
757 {
hualing chen87072a82020-03-12 16:20:12 +0800758 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800759 //get first segment from list
760 found = 1;
761 }
762 if (segment->segment_id == segment_id) {
763 found = 1;
764 }
765 if (found == 1) {
766 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800767 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800768 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800769 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800770 player->cur_segment.segment_id = segment->segment_id;
771 player->cur_segment.flags = segment->flags;
772 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800773 player->cur_segment.pids.video.pid = segment->pids.video.pid;
774 player->cur_segment.pids.video.format = segment->pids.video.format;
775 player->cur_segment.pids.video.type = segment->pids.video.type;
776 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
777 player->cur_segment.pids.audio.format = segment->pids.audio.format;
778 player->cur_segment.pids.audio.type = segment->pids.audio.type;
779 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
780 player->cur_segment.pids.ad.format = segment->pids.ad.format;
781 player->cur_segment.pids.ad.type = segment->pids.ad.type;
782 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800783 //
hualing chen2aba4022020-03-02 13:49:55 +0800784 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800785 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800786 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800787 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800788 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
789 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800790 DVR_PB_DG(1, "get play info sucess[0x%x]apid[0x%x]vfmt[%d]afmt[%d]", vparam->pid, aparam->pid, vparam->codectype, aparam->codectype);
hualing chen5cbe1a62020-02-10 16:36:36 +0800791 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800792 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800793 }
794 }
hualing chencc91e1c2020-02-28 13:26:17 +0800795 if (found != 2) {
796 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800797 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800798 return DVR_FAILURE;
799 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800800
801 return DVR_SUCCESS;
802}
hualing chencc91e1c2020-02-28 13:26:17 +0800803static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
804 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800805 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800806 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800807 return DVR_FAILURE;
808 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800809
hualing chencc91e1c2020-02-28 13:26:17 +0800810 //compare cur segment
811 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
812 {
813 //check video pids, stop or restart
814 _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids.video, 0);
815 //check audio pids stop or restart
816 _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids.audio, 1);
817 //check sub audio pids stop or restart
818 _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids.ad, 2);
hualing chene3797f02021-01-13 14:53:28 +0800819 DVR_PB_DG(1, ":last apid: %d set apid: %d", player->last_segment.pids.audio.pid,player->cur_segment.pids.audio.pid);
hualing chencc91e1c2020-02-28 13:26:17 +0800820 //check pcr pids stop or restart
821 _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids.pcr, 3);
822 }
hualing chena540a7e2020-03-27 16:44:05 +0800823 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800824}
hualing chen5cbe1a62020-02-10 16:36:36 +0800825
hualing chencc91e1c2020-02-28 13:26:17 +0800826static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
827{
828 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800829 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800830 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800831 return DVR_FAILURE;
832 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800833 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
834 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
835 return DVR_SUCCESS;
836 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800837 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]", player->cur_segment.flags, player->cur_segment.segment_id, player->last_segment.flags, player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800838 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
839 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800840 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800841 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800842 AmTsPlayer_showVideo(player->handle);
843 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800844 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
845 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800846 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800847 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800848 AmTsPlayer_hideVideo(player->handle);
849 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800850 }
851 return DVR_SUCCESS;
852}
hualing chene3797f02021-01-13 14:53:28 +0800853/*
854if decodec sucess first time.
855sucess: return true
856fail: return false
857*/
hualing chena540a7e2020-03-27 16:44:05 +0800858static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
859 DVR_Playback_t *player = (DVR_Playback_t *) handle;
860 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800861 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800862 return DVR_TRUE;
863 }
hualing chene3797f02021-01-13 14:53:28 +0800864 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800865 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800866 } else {
867 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800868 }
869}
hualing chen86e7d482020-01-16 15:13:33 +0800870static void* _dvr_playback_thread(void *arg)
871{
hualing chen040df222020-01-17 13:35:02 +0800872 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800873 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800874 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800875 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800876 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800877
hualing chen39628212020-05-14 10:35:13 +0800878 #define MAX_REACHEND_TIMEOUT (3000)
879 int reach_end_timeout = 0;//ms
880 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800881 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800882 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800883 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800884 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800885 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
886
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800887 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800888 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800889 DVR_Bool_t goto_rewrite = DVR_FALSE;
hualing chene41f4372020-06-06 16:29:17 +0800890 int retry_open_seg = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800891 if (player->is_secure_mode) {
892 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800893 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800894 return NULL;
895 }
896 }
hualing chen86e7d482020-01-16 15:13:33 +0800897 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800898 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800899 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800900 return NULL;
901 }
hualing chen2aba4022020-03-02 13:49:55 +0800902 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
903 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800904
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800905 dec_bufs.buf_data = malloc(dec_buf_size);
906 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800907 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800908 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800909 return NULL;
910 }
911 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
912 dec_bufs.buf_size = dec_buf_size;
913
hualing chencc91e1c2020-02-28 13:26:17 +0800914 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800915 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
916 }
hualing chen86e7d482020-01-16 15:13:33 +0800917
hualing chen86e7d482020-01-16 15:13:33 +0800918 if (ret != DVR_SUCCESS) {
919 if (buf != NULL) {
920 free(buf);
921 buf = NULL;
922 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800923 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +0800924 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +0800925 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800926 }
hualing chen4fe3bee2020-10-23 13:58:52 +0800927 DVR_PB_DG(1, "player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
928 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +0800929 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
930 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
931 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
932 player->first_trans_ok = DVR_TRUE;
933 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
934 }
hualing chencc91e1c2020-02-28 13:26:17 +0800935 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +0800936 //set video show
937 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +0800938
hualing chen86e7d482020-01-16 15:13:33 +0800939 int trick_stat = 0;
940 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +0800941
hualing chen86e7d482020-01-16 15:13:33 +0800942 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +0800943 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800944
hualing chen2aba4022020-03-02 13:49:55 +0800945 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
946 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +0800947 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +0800948 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +0800949 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +0800950 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +0800951 {
hualing chen2aba4022020-03-02 13:49:55 +0800952 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
953 if (trick_stat > 0) {
hualing chenbcada022020-04-22 14:27:01 +0800954 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]", player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +0800955 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK || (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen2aba4022020-03-02 13:49:55 +0800956 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +0800957 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +0800958 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +0800959 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
960 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +0800961 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
962 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chenbcada022020-04-22 14:27:01 +0800963 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]", player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +0800964 //need change to pause state
965 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
966 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +0800967 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +0800968 //clear flag
hualing chen31140872020-03-25 12:29:26 +0800969 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +0800970 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800971 AmTsPlayer_pauseVideoDecoding(player->handle);
972 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +0800973 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800974 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +0800975 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800976 }
977 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
978 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +0800979 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +0800980 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +0800981 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
982 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +0800983 //used timeout wait need lock first,so we unlock and lock
984 //pthread_mutex_unlock(&player->lock);
985 //pthread_mutex_lock(&player->lock);
986 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
987 pthread_mutex_unlock(&player->lock);
988 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +0800989 } else if (_dvr_time_getClock() < player->next_fffb_time) {
990 DVR_PB_DG(1, "fffb timeout-to pause video---speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
991 //used timeout wait need lock first,so we unlock and lock
992 //pthread_mutex_unlock(&player->lock);
993 //pthread_mutex_lock(&player->lock);
994 AmTsPlayer_pauseVideoDecoding(player->handle);
995 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
996 pthread_mutex_unlock(&player->lock);
997 continue;
998
hualing chen2aba4022020-03-02 13:49:55 +0800999 }
hualing chen2932d372020-04-29 13:44:00 +08001000 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]", player->speed, goto_rewrite, real_read, _dvr_playback_state_toString(player->state), player->cmd);
hualing chen2aba4022020-03-02 13:49:55 +08001001 pthread_mutex_unlock(&player->lock);
1002 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001003 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001004 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1005 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001006 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001007 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001008 pthread_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001009 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001010 }else if (player->fffb_play == DVR_TRUE){
1011 //for first into fffb when reset speed
1012 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1013 _dvr_time_getClock() < player->next_fffb_time) {
1014 DVR_PB_DG(1, "fffb timeout-fffb play---speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
1015 //used timeout wait need lock first,so we unlock and lock
1016 //pthread_mutex_unlock(&player->lock);
1017 //pthread_mutex_lock(&player->lock);
1018 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1019 pthread_mutex_unlock(&player->lock);
1020 continue;
1021 }
hualing chen2932d372020-04-29 13:44:00 +08001022 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]", player->speed, goto_rewrite, real_read, _dvr_playback_state_toString(player->state), player->cmd, player->fffb_play);
hualing chen4b7c15d2020-04-07 16:13:48 +08001023 pthread_mutex_unlock(&player->lock);
1024 goto_rewrite = DVR_FALSE;
1025 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001026 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001027 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1028 player->first_frame = 0;
1029 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1030 pthread_mutex_lock(&player->lock);
1031 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001032 }
hualing chenb31a6c62020-01-13 17:27:00 +08001033 }
hualing chen86e7d482020-01-16 15:13:33 +08001034
hualing chen87072a82020-03-12 16:20:12 +08001035 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001036 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001037 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001038 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001039 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1040 pthread_mutex_unlock(&player->lock);
1041 continue;
1042 }
hualing chen266b9502020-04-04 17:39:39 +08001043 //when seek action is done. we need drop write timeout data.
1044 if (player->drop_ts == DVR_TRUE) {
1045 goto_rewrite = DVR_FALSE;
1046 real_read = 0;
1047 player->drop_ts = DVR_FALSE;
1048 }
hualing chen2aba4022020-03-02 13:49:55 +08001049 if (goto_rewrite == DVR_TRUE) {
1050 goto_rewrite = DVR_FALSE;
1051 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001052 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001053 goto rewrite;
1054 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001055 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001056 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001057 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001058 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001059 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chenfbf8e022020-06-15 13:43:11 +08001060 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001061 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001062 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001063 if (read < 0 && errno == EIO) {
1064 //EIO ERROR, EXIT THRAD
1065 DVR_PB_DG(1, "read error.EIO error, exit thread");
1066 DVR_Play_Notify_t notify;
1067 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1068 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001069 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001070 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001071 goto end;
1072 } else if (read < 0) {
1073 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1074 }
hualing chen87072a82020-03-12 16:20:12 +08001075 //if on fb mode and read file end , we need calculate pos to retry read.
1076 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001077 DVR_PB_DG(1, "recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]", read,real_read, buf_len, player->speed,player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001078 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1079 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001080 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1081 pthread_mutex_unlock(&player->lock);
1082 continue;
1083 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001084 //DVR_PB_DG(1, "read ts [%d]buf_len[%d]speed[%f]real_read:%d", read, buf_len, player->speed, real_read);
hualing chen86e7d482020-01-16 15:13:33 +08001085 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001086 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001087 #define MIN_CACHE_TIME (3000)
1088 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001089 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001090 if (_cache_time > MIN_CACHE_TIME) {
hualing chene41f4372020-06-06 16:29:17 +08001091 pthread_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001092 /*if cache time > 20s , we think get time is error,*/
1093 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
1094 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1095 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1096 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1097 }
1098 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, ((_cache_time - MIN_CACHE_TIME) > MIN_CACHE_TIME ? MIN_CACHE_TIME : (_cache_time - MIN_CACHE_TIME)));
hualing chene41f4372020-06-06 16:29:17 +08001099 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001100 DVR_PB_DG(1, "read end but cache time is %d > %d, to sleep end and continue", _cache_time, MIN_CACHE_TIME);
hualing chene41f4372020-06-06 16:29:17 +08001101 //continue;
1102 }
hualing chen040df222020-01-17 13:35:02 +08001103 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001104 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001105 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001106
1107 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chene3797f02021-01-13 14:53:28 +08001108 if (ret != DVR_SUCCESS) {
1109 player->noData++;
1110 DVR_PB_DG(1, "playback is sleep:[%d]ms nodata[%d]", timeout, player->noData);
1111 if (player->noData == 4) {
1112 DVR_PB_DG(1, "playback send nodata event nodata[%d]", player->noData);
1113 //send event here and pause
1114 DVR_Play_Notify_t notify;
1115 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1116 notify.event = DVR_PLAYBACK_EVENT_NODATA;
1117 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_NODATA");
1118 //get play statue not here
1119 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1120 }
1121 }
1122 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001123 if ((ret != DVR_SUCCESS &&
hualing chen041c4092020-04-05 15:11:50 +08001124 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001125 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001126 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1127 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001128 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001129 DVR_Play_Notify_t notify;
1130 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1131 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1132 //get play statue not here
1133 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001134 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001135 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001136 DVR_PB_DG(1, "playback is send end delay:[%d]reach_end_timeout[%d]ms", delay, reach_end_timeout);
hualing chen31140872020-03-25 12:29:26 +08001137 pthread_mutex_lock(&player->lock);
1138 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1139 pthread_mutex_unlock(&player->lock);
1140 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001141 } else if (ret != DVR_SUCCESS) {
1142 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001143 if (delay < cache_time) {
1144 //delay time is changed and then has data to play, so not start timeout
1145 } else {
1146 reach_end_timeout = reach_end_timeout + timeout;
1147 }
1148 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001149 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001150 pthread_mutex_lock(&player->lock);
1151 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1152 pthread_mutex_unlock(&player->lock);
1153 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001154 }
hualing chen39628212020-05-14 10:35:13 +08001155 reach_end_timeout = 0;
1156 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001157 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001158 //change next segment success case
1159 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001160 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001161 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1162 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001163 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001164 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001165 }//read len 0 check end
1166 if (player->noData > 0) {
1167 player->noData = 0;
1168 DVR_PB_DG(1, "playback send data event resume[%d]", player->noData);
1169 //send event here and pause
1170 DVR_Play_Notify_t notify;
1171 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1172 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
1173 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_DATARESUME");
1174 //get play statue not here
1175 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001176 }
hualing chen39628212020-05-14 10:35:13 +08001177 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001178 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001179 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001180 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001181
hualing chena540a7e2020-03-27 16:44:05 +08001182 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001183 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001184 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001185 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001186 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001187 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001188 }
hualing chen266b9502020-04-04 17:39:39 +08001189 //if need write whole block size, we need check read buf len is eq block size.
1190 if (b_writed_whole_block == DVR_TRUE) {
1191 //buf_len is block size value.
1192 if (real_read < buf_len) {
1193 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001194 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001195 pthread_mutex_lock(&player->lock);
1196 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1197 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001198 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001199 continue;
1200 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001201 DVR_PB_DG(1, "read buf len[%d] is > block size [%d],this error occur", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001202 }
1203 }
1204
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001205 if (player->dec_func) {
1206 DVR_CryptoParams_t crypto_params;
1207
1208 memset(&crypto_params, 0, sizeof(crypto_params));
1209 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1210 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1211 crypto_params.segment_id = player->cur_segment.segment_id;
hualing chen1f26ffa2020-11-03 10:39:20 +08001212 crypto_params.offset = segment_tell_position(player->r_handle) - wbufs.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001213 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1214 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001215 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1216 crypto_params.input_buffer.addr = (size_t)buf;
1217 crypto_params.input_buffer.size = real_read;
1218
1219 if (player->is_secure_mode) {
1220 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1221 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1222 crypto_params.output_buffer.size = dec_buf_size;
1223 ret = player->dec_func(&crypto_params, player->dec_userdata);
1224 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001225 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001226 } else {
1227 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1228 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1229 crypto_params.output_buffer.size = dec_buf_size;
1230 ret = player->dec_func(&crypto_params, player->dec_userdata);
1231 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001232 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001233 }
1234 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001235 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001236 }
pengfei.liufda2a972020-04-09 14:47:15 +08001237 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001238 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001239rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001240 if (player->drop_ts == DVR_TRUE) {
1241 //need drop ts data when seek occur.we need read next loop,drop this ts data
1242 goto_rewrite = DVR_FALSE;
1243 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001244 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001245 player->drop_ts = DVR_FALSE;
1246 continue;
1247 }
hualing chen5605eed2020-05-26 18:18:06 +08001248 player->ts_cache_len = real_read;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001249 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1250 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001251 player->ts_cache_len = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001252 real_read = 0;
1253 write_success++;
hualing chen5605eed2020-05-26 18:18:06 +08001254 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001255 continue;
hualing chen87072a82020-03-12 16:20:12 +08001256 } else {
hualing chen2932d372020-04-29 13:44:00 +08001257 DVR_PB_DG(1, "write time out write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001258 write_success = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001259 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001260 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001261 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001262 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001263 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001264 break;
1265 }
hualing chen2aba4022020-03-02 13:49:55 +08001266 goto_rewrite = DVR_TRUE;
1267 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001268 }
1269 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001270end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001271 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001272 free(buf);
1273 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001274 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001275}
1276
1277
hualing chen040df222020-01-17 13:35:02 +08001278static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001279{
hualing chen040df222020-01-17 13:35:02 +08001280 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001281
1282 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001283 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001284 return DVR_FAILURE;
1285 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001286 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001287 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001288 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001289 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001290 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001291 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001292 if (rc < 0)
1293 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001294 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001295}
1296
1297
hualing chen040df222020-01-17 13:35:02 +08001298static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001299{
hualing chen040df222020-01-17 13:35:02 +08001300 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001301
1302 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001303 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001304 return DVR_FAILURE;
1305 }
1306
hualing chen4b7c15d2020-04-07 16:13:48 +08001307 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001308 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001309 {
1310 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001311 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001312 pthread_join(player->playback_thread, NULL);
1313 }
1314 if (player->r_handle) {
1315 segment_close(player->r_handle);
1316 player->r_handle = NULL;
1317 }
hualing chen7a56cba2020-04-14 14:09:27 +08001318 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001319 return 0;
1320}
1321
hualing chenb31a6c62020-01-13 17:27:00 +08001322/**\brief Open an dvr palyback
1323 * \param[out] p_handle dvr playback addr
1324 * \param[in] params dvr playback open parameters
1325 * \retval DVR_SUCCESS On success
1326 * \return Error code
1327 */
hualing chen040df222020-01-17 13:35:02 +08001328int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001329
hualing chen040df222020-01-17 13:35:02 +08001330 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001331 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001332
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001333 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001334
hualing chen86e7d482020-01-16 15:13:33 +08001335 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001336 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001337 pthread_condattr_init(&cattr);
1338 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1339 pthread_cond_init(&player->cond, &cattr);
1340 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001341
hualing chen5cbe1a62020-02-10 16:36:36 +08001342 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001343 INIT_LIST_HEAD(&player->segment_list);
1344 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1345 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001346 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001347 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001348 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001349 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001350 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001351 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001352
hualing chen86e7d482020-01-16 15:13:33 +08001353 //store open params
hualing chen040df222020-01-17 13:35:02 +08001354 player->openParams.dmx_dev_id = params->dmx_dev_id;
1355 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001356 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001357 player->openParams.event_fn = params->event_fn;
1358 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001359 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001360 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001361
hualing chen5cbe1a62020-02-10 16:36:36 +08001362 player->has_pids = params->has_pids;
1363
hualing chen2aba4022020-03-02 13:49:55 +08001364 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001365
1366 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1367 //for test get callback
1368 if (0 && player->player_callback_func == NULL) {
1369 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1370 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +08001371 DVR_PB_DG(1, "playback open get callback[%p][%p][%p][%p]",player->player_callback_func, player->player_callback_userdata, _dvr_tsplayer_callback_test,player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001372 }
1373 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001374
hualing chen86e7d482020-01-16 15:13:33 +08001375 //init has audio and video
1376 player->has_video = DVR_FALSE;
1377 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001378 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001379 player->last_segment_id = 0LL;
1380 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001381
hualing chen5cbe1a62020-02-10 16:36:36 +08001382 //init ff fb time
1383 player->fffb_current = -1;
1384 player->fffb_start =-1;
1385 player->fffb_start_pcr = -1;
1386 //seek time
1387 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001388 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001389
1390 //init secure stuff
1391 player->dec_func = NULL;
1392 player->dec_userdata = NULL;
1393 player->is_secure_mode = 0;
1394 player->secure_buffer = NULL;
1395 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001396 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001397
hualing chen4b7c15d2020-04-07 16:13:48 +08001398 player->fffb_play = DVR_FALSE;
1399
1400 player->last_send_time_id = UINT64_MAX;
1401 player->last_cur_time = 0;
1402
hualing chen86e7d482020-01-16 15:13:33 +08001403 *p_handle = player;
1404 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001405}
1406
1407/**\brief Close an dvr palyback
1408 * \param[in] handle playback handle
1409 * \retval DVR_SUCCESS On success
1410 * \return Error code
1411 */
hualing chen040df222020-01-17 13:35:02 +08001412int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001413
hualing chen86e7d482020-01-16 15:13:33 +08001414 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001415 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001416 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001417 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001418 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001419 return DVR_FAILURE;
1420 }
1421
hualing chencc91e1c2020-02-28 13:26:17 +08001422 if (player->state != DVR_PLAYBACK_STATE_STOP)
1423 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001424 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001425 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001426 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1427 } else {
1428 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001429 }
hualing chen7a56cba2020-04-14 14:09:27 +08001430 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001431 pthread_mutex_destroy(&player->lock);
1432 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001433
1434 if (player) {
1435 free(player);
1436 player = NULL;
1437 }
hualing chen7a56cba2020-04-14 14:09:27 +08001438 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001439 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001440}
1441
hualing chenb31a6c62020-01-13 17:27:00 +08001442/**\brief Start play audio and video, used start auido api and start video api
1443 * \param[in] handle playback handle
1444 * \param[in] params audio playback params,contains fmt and pid...
1445 * \retval DVR_SUCCESS On success
1446 * \return Error code
1447 */
hualing chen040df222020-01-17 13:35:02 +08001448int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1449 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001450 am_tsplayer_video_params vparams;
1451 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001452 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001453
1454 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001455 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001456 return DVR_FAILURE;
1457 }
hualing chencc91e1c2020-02-28 13:26:17 +08001458 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001459 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001460
hualing chena540a7e2020-03-27 16:44:05 +08001461 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001462 //can used start api to resume playback
1463 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1464 return dvr_playback_resume(handle);
1465 }
hualing chen87072a82020-03-12 16:20:12 +08001466 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001467 //if flag is puased and not decodec first frame. if user resume, we need
1468 //clear flag and set trickmode none
1469 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1470 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1471 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1472 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1473 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001474 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001475 return DVR_SUCCESS;
1476 }
hualing chen86e7d482020-01-16 15:13:33 +08001477 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001478 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001479 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001480 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001481 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001482 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001483 //start audio and video
Zhiqiang Hana9d261b2020-11-11 18:38:10 +08001484 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001485 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001486 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001487 pthread_mutex_unlock(&player->lock);
1488 DVR_Play_Notify_t notify;
1489 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1490 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1491 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1492 notify.info.transition_failed_data.segment_id = segment_id;
1493 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001494 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001495 return -1;
1496 }
hualing chen31140872020-03-25 12:29:26 +08001497
hualing chencc91e1c2020-02-28 13:26:17 +08001498 {
hualing chen86e7d482020-01-16 15:13:33 +08001499 if (VALID_PID(vparams.pid)) {
1500 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001501 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001502 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001503 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001504 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1505 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001506 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001507 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001508 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001509 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001510 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001511 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001512 }
hualing chena93bbbc2020-12-22 17:23:42 +08001513 AmTsPlayer_showVideo(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001514 AmTsPlayer_setVideoParams(player->handle, &vparams);
1515 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001516 }
hualing chena540a7e2020-03-27 16:44:05 +08001517
hualing chen4b7c15d2020-04-07 16:13:48 +08001518 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1519 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001520 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1521 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1522 player->cmd.state = DVR_PLAYBACK_STATE_START;
1523 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001524 } else {
1525 player->cmd.last_cmd = player->cmd.cur_cmd;
1526 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001527 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001528 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001529 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001530 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001531 } else {
1532 if (VALID_PID(aparams.pid)) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001533 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08001534 player->has_audio = DVR_TRUE;
1535 AmTsPlayer_setAudioParams(player->handle, &aparams);
1536 AmTsPlayer_startAudioDecoding(player->handle);
1537 }
hualing chendf118dd2020-05-21 15:49:11 +08001538 if (VALID_PID(adparams.pid)) {
1539 player->has_ad_audio = DVR_TRUE;
1540 DVR_PB_DG(1, "start ad audio");
1541 AmTsPlayer_setADParams(player->handle, &adparams);
1542 AmTsPlayer_enableADMix(player->handle);
1543 }
hualing chen31140872020-03-25 12:29:26 +08001544 }
hualing chencc91e1c2020-02-28 13:26:17 +08001545 player->cmd.state = DVR_PLAYBACK_STATE_START;
1546 player->state = DVR_PLAYBACK_STATE_START;
1547 }
hualing chen86e7d482020-01-16 15:13:33 +08001548 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001549 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001550 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001551 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001552 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001553}
hualing chen040df222020-01-17 13:35:02 +08001554/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001555 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001556 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001557 * \retval DVR_SUCCESS On success
1558 * \return Error code
1559 */
hualing chen040df222020-01-17 13:35:02 +08001560int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1561 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001562
hualing chena540a7e2020-03-27 16:44:05 +08001563 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001564 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001565 return DVR_FAILURE;
1566 }
1567
hualing chen4b7c15d2020-04-07 16:13:48 +08001568 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001569 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001570
hualing chen040df222020-01-17 13:35:02 +08001571 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1572 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001573
hualing chen86e7d482020-01-16 15:13:33 +08001574 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001575 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001576 //cp location
hualing chen040df222020-01-17 13:35:02 +08001577 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001578
hualing chen4b7c15d2020-04-07 16:13:48 +08001579 DVR_PB_DG(1, "add location [%s]id[%lld]flag[%x]", segment->location, segment->segment_id, info->flags);
hualing chen040df222020-01-17 13:35:02 +08001580 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001581
1582 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001583 segment->pids.video.pid = info->pids.video.pid;
1584 segment->pids.video.format = info->pids.video.format;
1585 segment->pids.video.type = info->pids.video.type;
1586
hualing chen2aba4022020-03-02 13:49:55 +08001587 segment->pids.audio.pid = info->pids.audio.pid;
1588 segment->pids.audio.format = info->pids.audio.format;
1589 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001590
hualing chen2aba4022020-03-02 13:49:55 +08001591 segment->pids.ad.pid = info->pids.ad.pid;
1592 segment->pids.ad.format = info->pids.ad.format;
1593 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001594
1595 segment->pids.pcr.pid = info->pids.pcr.pid;
1596
hualing chen4b7c15d2020-04-07 16:13:48 +08001597 DVR_PB_DG(1, "lock pid [0x%x][0x%x][0x%x][0x%x]", segment->pids.video.pid,segment->pids.audio.pid, info->pids.video.pid,info->pids.audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001598 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001599 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001600 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001601 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001602
hualing chen5cbe1a62020-02-10 16:36:36 +08001603 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001604}
hualing chen040df222020-01-17 13:35:02 +08001605/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001606 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001607 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001608 * \retval DVR_SUCCESS On success
1609 * \return Error code
1610 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001611int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001612 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001613 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001614 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001615 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001616 return DVR_FAILURE;
1617 }
1618
hualing chencc91e1c2020-02-28 13:26:17 +08001619 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001620 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001621 return DVR_FAILURE;
1622 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001623 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001624 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001625 DVR_PlaybackSegmentInfo_t *segment = NULL;
1626 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1627 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001628 {
hualing chen040df222020-01-17 13:35:02 +08001629 if (segment->segment_id == segment_id) {
1630 list_del(&segment->head);
1631 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001632 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001633 }
hualing chen86e7d482020-01-16 15:13:33 +08001634 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001635 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001636 pthread_mutex_unlock(&player->lock);
1637
1638 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001639}
hualing chen040df222020-01-17 13:35:02 +08001640/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001641 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001642 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001643 * \retval DVR_SUCCESS On success
1644 * \return Error code
1645 */
hualing chen040df222020-01-17 13:35:02 +08001646int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001647 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001648 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001649 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001650 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001651 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001652 return DVR_FAILURE;
1653 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001654 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1655 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1656 return DVR_SUCCESS;
1657 }
hualing chena540a7e2020-03-27 16:44:05 +08001658
hualing chen040df222020-01-17 13:35:02 +08001659 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001660 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001661 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001662 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001663 {
hualing chen040df222020-01-17 13:35:02 +08001664 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08001665 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001666 }
hualing chen86e7d482020-01-16 15:13:33 +08001667 // if encramble to free, only set flag and return;
1668
1669 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001670 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001671 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1672 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001673 //disable display, mute
hualing chen703f3572021-01-06 12:51:34 +08001674 DVR_PB_DG(1, "mute av");
hualing chen2aba4022020-03-02 13:49:55 +08001675 AmTsPlayer_hideVideo(player->handle);
1676 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001677 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1678 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001679 //enable display, unmute
hualing chen703f3572021-01-06 12:51:34 +08001680 DVR_PB_DG(1, "unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08001681 AmTsPlayer_showVideo(player->handle);
1682 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001683 } else {
1684 //do nothing
1685 }
1686 } else {
1687 //do nothing
1688 }
1689 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001690 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001691 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001692 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001693 pthread_mutex_unlock(&player->lock);
1694 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001695}
1696
1697
hualing chen5cbe1a62020-02-10 16:36:36 +08001698static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type) {
hualing chen040df222020-01-17 13:35:02 +08001699 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001700 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001701 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001702 return DVR_FAILURE;
1703 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001704 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001705 if (now_pid.pid == set_pid.pid) {
1706 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001707 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001708 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001709 if (VALID_PID(now_pid.pid)) {
1710 //stop now stream
1711 if (type == 0) {
1712 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001713 if (player->has_video == DVR_TRUE) {
1714 DVR_PB_DG(1, "stop video");
1715 AmTsPlayer_stopVideoDecoding(player->handle);
1716 player->has_video = DVR_FALSE;
1717 }
hualing chen86e7d482020-01-16 15:13:33 +08001718 } else if (type == 1) {
1719 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001720 if (player->has_audio == DVR_TRUE) {
1721 DVR_PB_DG(1, "stop audio");
1722 AmTsPlayer_stopAudioDecoding(player->handle);
1723 player->has_audio = DVR_FALSE;
1724 }
hualing chen86e7d482020-01-16 15:13:33 +08001725 } else if (type == 2) {
1726 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001727 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001728 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001729 } else if (type == 3) {
1730 //pcr
1731 }
1732 }
1733 if (VALID_PID(set_pid.pid)) {
1734 //start
1735 if (type == 0) {
1736 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001737 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001738 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001739 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001740 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001741 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001742 AmTsPlayer_setVideoParams(player->handle, &vparams);
1743 AmTsPlayer_startVideoDecoding(player->handle);
1744 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001745 } else if (type == 1) {
1746 //start audio
hualing chenc70a8df2020-05-12 19:23:11 +08001747 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
1748 am_tsplayer_audio_params aparams;
1749 aparams.pid = set_pid.pid;
1750 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1751 player->has_audio = DVR_TRUE;
1752 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1753 AmTsPlayer_setAudioParams(player->handle, &aparams);
1754 AmTsPlayer_startAudioDecoding(player->handle);
1755 //playback_device_audio_start(player->handle,&aparams);
1756 }
hualing chen86e7d482020-01-16 15:13:33 +08001757 } else if (type == 2) {
hualing chen39628212020-05-14 10:35:13 +08001758 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chenc70a8df2020-05-12 19:23:11 +08001759 am_tsplayer_audio_params aparams;
1760 aparams.pid = set_pid.pid;
1761 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1762 player->has_audio = DVR_TRUE;
1763 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1764 AmTsPlayer_setADParams(player->handle, &aparams);
1765 AmTsPlayer_enableADMix(player->handle);
1766 //playback_device_audio_start(player->handle,&aparams);
1767 }
hualing chen86e7d482020-01-16 15:13:33 +08001768 } else if (type == 3) {
1769 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001770 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001771 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001772 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001773 //audio and video all close
1774 if (!player->has_audio && !player->has_video) {
1775 player->state = DVR_PLAYBACK_STATE_STOP;
1776 }
hualing chen86e7d482020-01-16 15:13:33 +08001777 }
1778 }
1779 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001780}
hualing chen5cbe1a62020-02-10 16:36:36 +08001781/**\brief dvr play back update segment pids
1782 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001783 * add pid stream and stop remove pid stream.
1784 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001785 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001786 * \retval DVR_SUCCESS On success
1787 * \return Error code
1788 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001789int dvr_playback_update_segment_pids(DVR_PlaybackHandle_t handle, uint64_t segment_id, DVR_PlaybackPids_t *p_pids) {
hualing chen040df222020-01-17 13:35:02 +08001790 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001791 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001792 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001793 return DVR_FAILURE;
1794 }
1795
hualing chen040df222020-01-17 13:35:02 +08001796 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001797 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001798 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001799 DVR_PB_DG(1, "get lock update segment id: %lld cur id %lld", segment_id, player->cur_segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001800
hualing chen040df222020-01-17 13:35:02 +08001801 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001802 {
hualing chen040df222020-01-17 13:35:02 +08001803 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001804
1805 if (player->cur_segment_id == segment_id) {
1806 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
1807 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
1808 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08001809 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08001810 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001811 return 0;
1812 }
1813
1814 //if segment is on going segment,we need stop start stream
1815 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08001816 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001817 //check video pids, stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001818 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, p_pids->video, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08001819 //check audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001820 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, p_pids->audio, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001821 //check sub audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001822 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, p_pids->ad, 2);
hualing chen5cbe1a62020-02-10 16:36:36 +08001823 //check pcr pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001824 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, p_pids->pcr, 3);
1825 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001826 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1827 //if state is pause, we need process at resume api. we only record change info
1828 int v_cmd = DVR_PLAYBACK_CMD_NONE;
1829 int a_cmd = DVR_PLAYBACK_CMD_NONE;
1830 if (VALID_PID(segment->pids.video.pid)
1831 && VALID_PID(p_pids->video.pid)
1832 && segment->pids.video.pid != p_pids->video.pid) {
1833 //restart video
1834 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
1835 }
1836 if (!VALID_PID(segment->pids.video.pid)
1837 && VALID_PID(p_pids->video.pid)
1838 && segment->pids.video.pid != p_pids->video.pid) {
1839 //start video
1840 v_cmd = DVR_PLAYBACK_CMD_VSTART;
1841 }
1842 if (VALID_PID(segment->pids.video.pid)
1843 && !VALID_PID(p_pids->video.pid)
1844 && segment->pids.video.pid != p_pids->video.pid) {
1845 //stop video
1846 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
1847 }
1848 if (VALID_PID(segment->pids.audio.pid)
1849 && VALID_PID(p_pids->audio.pid)
1850 && segment->pids.audio.pid != p_pids->audio.pid) {
1851 //restart audio
1852 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
1853 }
1854 if (!VALID_PID(segment->pids.audio.pid)
1855 && VALID_PID(p_pids->audio.pid)
1856 && segment->pids.audio.pid != p_pids->audio.pid) {
1857 //start audio
1858 a_cmd = DVR_PLAYBACK_CMD_ASTART;
1859 }
1860 if (VALID_PID(segment->pids.audio.pid)
1861 && !VALID_PID(p_pids->audio.pid)
1862 && segment->pids.audio.pid != p_pids->audio.pid) {
1863 //stop audio
1864 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
1865 }
1866 if (a_cmd == DVR_PLAYBACK_CMD_NONE
1867 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
1868 //do nothing
1869 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
1870 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
1871 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1872 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
1873 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
1874 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
1875 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1876 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1877 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1878 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
1879 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1880 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1881 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1882 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
1883 } else {
1884 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1885 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
1886 }
1887
1888 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1889 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1890 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1891 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
1892 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1893 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1894 //not occur this case
1895 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1896 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
1897 } else {
1898 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1899 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
1900 }
1901
1902 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1903 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1904 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1905 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
1906 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1907 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
1908 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1909 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
1910 } else {
1911 //not occur this case
1912 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1913 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1914 }
1915 }
1916 }
hualing chene10666f2020-04-14 13:58:37 +08001917 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08001918 }
hualing chen86e7d482020-01-16 15:13:33 +08001919 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08001920 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08001921 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08001922 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001923 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001924 }
hualing chen86e7d482020-01-16 15:13:33 +08001925 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001926 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001927 pthread_mutex_unlock(&player->lock);
1928 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001929}
1930/**\brief Stop play, will stop video and audio
1931 * \param[in] handle playback handle
1932 * \param[in] clear is clear last frame
1933 * \retval DVR_SUCCESS On success
1934 * \return Error code
1935 */
hualing chen040df222020-01-17 13:35:02 +08001936int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
1937 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001938
1939 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001940 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001941 return DVR_FAILURE;
1942 }
hualing chenb96aa2c2020-04-15 14:13:53 +08001943 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1944 DVR_PB_DG(1, ":playback is stoped");
1945 return DVR_SUCCESS;
1946 }
Ke Gong3c0caba2020-04-21 22:58:18 -07001947 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1948 DVR_PB_DG(1, ":playback is stoped");
1949 return DVR_SUCCESS;
1950 }
hualing chen87072a82020-03-12 16:20:12 +08001951 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001952 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001953 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08001954 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08001955 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001956 if (player->has_video) {
1957 AmTsPlayer_resumeVideoDecoding(player->handle);
1958 }
1959 if (player->has_audio) {
1960 AmTsPlayer_resumeAudioDecoding(player->handle);
1961 }
1962 if (player->has_video) {
1963 player->has_video = DVR_FALSE;
hualing chen703f3572021-01-06 12:51:34 +08001964 //AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001965 AmTsPlayer_stopVideoDecoding(player->handle);
1966 }
1967 if (player->has_audio) {
1968 player->has_audio = DVR_FALSE;
1969 AmTsPlayer_stopAudioDecoding(player->handle);
1970 }
hualing chendf118dd2020-05-21 15:49:11 +08001971 if (player->has_ad_audio) {
1972 player->has_ad_audio =DVR_FALSE;
1973 AmTsPlayer_disableADMix(player->handle);
1974 }
hualing chen266b9502020-04-04 17:39:39 +08001975
hualing chen86e7d482020-01-16 15:13:33 +08001976 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001977 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1978 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1979 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08001980 player->cur_segment_id = UINT64_MAX;
1981 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001982 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08001983 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08001984 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001985 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001986}
1987/**\brief Start play audio
1988 * \param[in] handle playback handle
1989 * \param[in] params audio playback params,contains fmt and pid...
1990 * \retval DVR_SUCCESS On success
1991 * \return Error code
1992 */
hualing chen2aba4022020-03-02 13:49:55 +08001993
hualing chendf118dd2020-05-21 15:49:11 +08001994int dvr_playback_audio_start(DVR_PlaybackHandle_t handle, am_tsplayer_audio_params *param, am_tsplayer_audio_params *adparam) {
hualing chen040df222020-01-17 13:35:02 +08001995 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001996
1997 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001998 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001999 return DVR_FAILURE;
2000 }
hualing chen86e7d482020-01-16 15:13:33 +08002001 _start_playback_thread(handle);
2002 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002003 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002004 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002005
2006 if (VALID_PID(param->pid)) {
2007 DVR_PB_DG(1, "start audio");
2008 player->has_audio = DVR_TRUE;
2009 AmTsPlayer_setAudioParams(player->handle, param);
2010 AmTsPlayer_startAudioDecoding(player->handle);
2011 }
2012 if (VALID_PID(adparam->pid)) {
2013 player->has_ad_audio = DVR_TRUE;
2014 DVR_PB_DG(1, "start ad audio");
2015 AmTsPlayer_setADParams(player->handle, adparam);
2016 AmTsPlayer_enableADMix(player->handle);
2017 }
2018
hualing chen86e7d482020-01-16 15:13:33 +08002019 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002020 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
2021 player->cmd.state = DVR_PLAYBACK_STATE_START;
2022 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002023 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002024 pthread_mutex_unlock(&player->lock);
2025 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002026}
2027/**\brief Stop play audio
2028 * \param[in] handle playback handle
2029 * \retval DVR_SUCCESS On success
2030 * \return Error code
2031 */
hualing chen040df222020-01-17 13:35:02 +08002032int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2033 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002034
2035 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002036 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002037 return DVR_FAILURE;
2038 }
2039
hualing chen2aba4022020-03-02 13:49:55 +08002040 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002041 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002042 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2043 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002044 //destory thread
2045 _stop_playback_thread(handle);
2046 } else {
2047 //do nothing.video is playing
2048 }
hualing chen7a56cba2020-04-14 14:09:27 +08002049 DVR_PB_DG(1, "lock");
2050 pthread_mutex_lock(&player->lock);
2051
hualing chenf00cdc82020-06-10 14:23:35 +08002052 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002053 player->has_audio = DVR_FALSE;
2054 AmTsPlayer_stopAudioDecoding(player->handle);
2055 }
hualing chen87072a82020-03-12 16:20:12 +08002056
hualing chendf118dd2020-05-21 15:49:11 +08002057 if (player->has_ad_audio) {
2058 player->has_ad_audio =DVR_FALSE;
2059 AmTsPlayer_disableADMix(player->handle);
2060 }
2061
hualing chen87072a82020-03-12 16:20:12 +08002062 player->cmd.last_cmd = player->cmd.cur_cmd;
2063 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2064
hualing chen4b7c15d2020-04-07 16:13:48 +08002065 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002066 pthread_mutex_unlock(&player->lock);
2067 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002068}
2069/**\brief Start play video
2070 * \param[in] handle playback handle
2071 * \param[in] params video playback params,contains fmt and pid...
2072 * \retval DVR_SUCCESS On success
2073 * \return Error code
2074 */
hualing chen2aba4022020-03-02 13:49:55 +08002075int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002076 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002077
2078 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002079 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002080 return DVR_FAILURE;
2081 }
2082
hualing chen86e7d482020-01-16 15:13:33 +08002083 _start_playback_thread(handle);
2084 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002085 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002086 pthread_mutex_lock(&player->lock);
2087 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002088 AmTsPlayer_setVideoParams(player->handle, param);
2089 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002090
2091 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002092 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002093 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002094 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002095 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2096 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002097 }
2098 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002099 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2100 player->cmd.state = DVR_PLAYBACK_STATE_START;
2101 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002102 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002103 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002104 return DVR_SUCCESS;
2105}
2106/**\brief Stop play video
2107 * \param[in] handle playback handle
2108 * \retval DVR_SUCCESS On success
2109 * \return Error code
2110 */
hualing chen040df222020-01-17 13:35:02 +08002111int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2112 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002113
2114 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002115 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002116 return DVR_FAILURE;
2117 }
2118
hualing chen86e7d482020-01-16 15:13:33 +08002119 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002120 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2121 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002122 //destory thread
2123 _stop_playback_thread(handle);
2124 } else {
2125 //do nothing.audio is playing
2126 }
hualing chen7a56cba2020-04-14 14:09:27 +08002127
2128 DVR_PB_DG(1, "lock");
2129 pthread_mutex_lock(&player->lock);
2130
hualing chen87072a82020-03-12 16:20:12 +08002131 player->has_video = DVR_FALSE;
2132
2133 AmTsPlayer_stopVideoDecoding(player->handle);
2134 //playback_device_video_stop(player->handle);
2135
2136 player->cmd.last_cmd = player->cmd.cur_cmd;
2137 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2138
hualing chen4b7c15d2020-04-07 16:13:48 +08002139 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002140 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002141 return DVR_SUCCESS;
2142}
2143/**\brief Pause play
2144 * \param[in] handle playback handle
2145 * \param[in] flush whether its internal buffers should be flushed
2146 * \retval DVR_SUCCESS On success
2147 * \return Error code
2148 */
hualing chen040df222020-01-17 13:35:02 +08002149int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2150 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002151
2152 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002153 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002154 return DVR_FAILURE;
2155 }
hualing chenf00cdc82020-06-10 14:23:35 +08002156 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2157 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002158 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002159 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002160 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002161 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002162 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002163 if (player->has_video)
2164 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002165 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002166 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002167
2168 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002169 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2170 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2171 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2172 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002173 } else {
2174 player->cmd.last_cmd = player->cmd.cur_cmd;
2175 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2176 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2177 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002178 }
hualing chen86e7d482020-01-16 15:13:33 +08002179 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002180 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002181
hualing chen86e7d482020-01-16 15:13:33 +08002182 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002183}
2184
hualing chen5cbe1a62020-02-10 16:36:36 +08002185//not add lock
2186static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2187{
2188 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2189
hualing chena540a7e2020-03-27 16:44:05 +08002190 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002191 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002192 return DVR_FAILURE;
2193 }
2194
hualing chen5cbe1a62020-02-10 16:36:36 +08002195 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002196 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002197 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002198 am_tsplayer_video_params vparams;
2199 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002200 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002201 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002202
hualing chendf118dd2020-05-21 15:49:11 +08002203 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002204 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002205 pthread_mutex_unlock(&player->lock);
2206
2207 switch (cmd) {
2208 case DVR_PLAYBACK_CMD_AVRESTART:
2209 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002210 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002211 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002212 break;
2213 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002214 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2215 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002216 break;
2217 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002218 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002219 break;
2220 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002221 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002222 break;
2223 case DVR_PLAYBACK_CMD_ARESTART:
2224 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002225 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002226 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002227 break;
2228 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002229 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002230 break;
2231 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002232 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002233 break;
2234 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002235 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2236 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2237 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002238 break;
2239 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002240 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2241 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002242 break;
2243 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002244 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2245 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002246 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002247 break;
2248 case DVR_PLAYBACK_CMD_STOP:
2249 break;
2250 case DVR_PLAYBACK_CMD_START:
2251 break;
2252 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002253 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2254 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002255 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002256 break;
2257 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002258 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2259 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002260 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002261 break;
2262 case DVR_PLAYBACK_CMD_FF:
2263 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002264 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002265 break;
2266 default:
2267 break;
2268 }
2269 return DVR_SUCCESS;
2270}
2271
2272/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002273 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002274 * \retval DVR_SUCCESS On success
2275 * \return Error code
2276 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002277int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2278 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2279
hualing chena540a7e2020-03-27 16:44:05 +08002280 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002281 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002282 return DVR_FAILURE;
2283 }
2284
hualing chen5cbe1a62020-02-10 16:36:36 +08002285 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002286 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002287 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002288 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002289 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002290 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2291 AmTsPlayer_resumeVideoDecoding(player->handle);
2292 }
2293 if (player->has_audio) {
2294 AmTsPlayer_resumeAudioDecoding(player->handle);
2295 }
2296 //check is has audio param,if has audio .we need start audio,
2297 //we will stop audio when ff fb, if reach end, we will pause.so we need
2298 //start audio when resume play
2299
2300 am_tsplayer_video_params vparams;
2301 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002302 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002303 uint64_t segmentid = player->cur_segment_id;
hualing chendf118dd2020-05-21 15:49:11 +08002304 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002305 //valid audio pid, start audio
hualing chenc70a8df2020-05-12 19:23:11 +08002306 if (player->has_audio == DVR_FALSE && VALID_PID(aparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chen266b9502020-04-04 17:39:39 +08002307 player->has_audio = DVR_TRUE;
2308 AmTsPlayer_setAudioParams(player->handle, &aparams);
2309 AmTsPlayer_startAudioDecoding(player->handle);
2310 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002311 DVR_PB_DG(1, "aparams.pid:%d player->has_audio:%d speed:%d", aparams.pid, player->has_audio, player->cmd.speed.speed.speed);
hualing chen266b9502020-04-04 17:39:39 +08002312 }
hualing chendf118dd2020-05-21 15:49:11 +08002313
2314 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2315 player->has_ad_audio = DVR_TRUE;
2316 DVR_PB_DG(1, "start ad audio");
2317 AmTsPlayer_setADParams(player->handle, &adparams);
2318 AmTsPlayer_enableADMix(player->handle);
2319 }
2320
hualing chen87072a82020-03-12 16:20:12 +08002321 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2322 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2323 player->cmd.state = DVR_PLAYBACK_STATE_START;
2324 player->state = DVR_PLAYBACK_STATE_START;
2325 } else {
2326 player->cmd.last_cmd = player->cmd.cur_cmd;
2327 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2328 player->cmd.state = DVR_PLAYBACK_STATE_START;
2329 player->state = DVR_PLAYBACK_STATE_START;
2330 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002331 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002332 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002333 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chene41f4372020-06-06 16:29:17 +08002334 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002335 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002336 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002337 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002338 }
hualing chen041c4092020-04-05 15:11:50 +08002339 if (player->has_audio)
2340 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002341 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002342 player->cmd.state = DVR_PLAYBACK_STATE_START;
2343 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002344 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2345 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002346 } else {
2347 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2348 {
2349 pthread_mutex_lock(&player->lock);
2350 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002351 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002352 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2353 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002354 if (player->has_video) {
2355 AmTsPlayer_resumeVideoDecoding(player->handle);
2356 }
2357 if (player->has_audio)
2358 AmTsPlayer_resumeAudioDecoding(player->handle);
2359
hualing chen041c4092020-04-05 15:11:50 +08002360 pthread_mutex_unlock(&player->lock);
2361 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002362 }
2363 return DVR_SUCCESS;
2364}
2365
hualing chena540a7e2020-03-27 16:44:05 +08002366static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2367
2368 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2369 DVR_PlaybackSegmentInfo_t *segment = NULL;
2370 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2371 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2372
2373 list_for_each_entry(segment, &player->segment_list, head)
2374 {
2375 if (segment->segment_id == segment_id) {
2376 cur_segment = segment;
2377 }
2378 if (segment->segment_id == set_seg_id) {
2379 set_segment = segment;
2380 }
2381 if (cur_segment != NULL && set_segment != NULL) {
2382 break;
2383 }
2384 }
2385 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002386 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002387 return DVR_TRUE;
2388 }
2389 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2390 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2391 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2392 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002393 DVR_PB_DG(1, "cur v[%d]a[%d] set v[%d]a[%d]",cur_segment->pids.video.pid,cur_segment->pids.audio.pid,set_segment->pids.video.pid,set_segment->pids.audio.pid);
hualing chena540a7e2020-03-27 16:44:05 +08002394 return DVR_TRUE;
2395 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002396 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002397 return DVR_FALSE;
2398}
2399
hualing chen5cbe1a62020-02-10 16:36:36 +08002400/**\brief seek
2401 * \param[in] handle playback handle
2402 * \param[in] time_offset time offset base cur segment
2403 * \retval DVR_SUCCESS On success
2404 * \return Error code
2405 */
hualing chencc91e1c2020-02-28 13:26:17 +08002406int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002407 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002408
2409 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002410 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002411 return DVR_FAILURE;
2412 }
2413
hualing chen4b7c15d2020-04-07 16:13:48 +08002414 DVR_PB_DG(1, "lock segment_id %llu cur id %llu time_offset %u cur end: %d player->state:%d", segment_id,player->cur_segment_id, (uint32_t)time_offset, _dvr_get_end_time(handle), player->state);
hualing chen86e7d482020-01-16 15:13:33 +08002415 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002416
hualing chen86e7d482020-01-16 15:13:33 +08002417 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002418 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002419 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002420
hualing chen5cbe1a62020-02-10 16:36:36 +08002421 //open segment if id is not current segment
hualing chen87072a82020-03-12 16:20:12 +08002422 int ret = _dvr_open_segment(handle, segment_id);
2423 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002424 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002425 pthread_mutex_unlock(&player->lock);
2426 return DVR_FAILURE;
2427 }
2428 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2429 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002430 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002431 //pthread_mutex_unlock(&player->lock);
2432 //return DVR_SUCCESS;
2433 time_offset = _dvr_get_end_time(handle);
2434 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002435 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002436 time_offset = _dvr_get_end_time(handle);
hualing chen87072a82020-03-12 16:20:12 +08002437 pthread_mutex_unlock(&player->lock);
2438 return DVR_FAILURE;
2439 }
2440 }
2441
hualing chen4b7c15d2020-04-07 16:13:48 +08002442 DVR_PB_DG(1, "seek open id[%lld]flag[0x%x] time_offset %u", player->cur_segment.segment_id, player->cur_segment.flags, time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08002443 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002444 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2445 //forward playback.not seek end of file
2446 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2447 //default -2000ms
2448 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2449 }
hualing chen86e7d482020-01-16 15:13:33 +08002450 }
hualing chen2aba4022020-03-02 13:49:55 +08002451 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002452 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002453 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002454 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002455 DVR_PB_DG(0, "seek get offset by time offset, offset=%d time_offset %u",offset, time_offset);
hualing chen2aba4022020-03-02 13:49:55 +08002456 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002457 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002458
hualing chen2aba4022020-03-02 13:49:55 +08002459 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002460
2461 player->last_send_time_id = UINT64_MAX;
2462
hualing chen2aba4022020-03-02 13:49:55 +08002463 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002464 player->fffb_current = _dvr_time_getClock();
2465 player->fffb_start = player->fffb_current;
2466 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2467 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002468 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002469 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002470 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002471 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002472 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002473 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002474 }
hualing chen86e7d482020-01-16 15:13:33 +08002475 //stop play
hualing chen4b7c15d2020-04-07 16:13:48 +08002476 DVR_PB_DG(0, "seek stop play, not inject data has video[%d]audio[%d]", player->has_video, player->has_audio);
hualing chen266b9502020-04-04 17:39:39 +08002477 if (player->has_video) {
2478 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002479 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002480 }
2481
2482 if (player->has_audio) {
2483 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002484 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002485 }
hualing chendf118dd2020-05-21 15:49:11 +08002486 if (player->has_ad_audio) {
2487 player->has_ad_audio =DVR_FALSE;
2488 AmTsPlayer_disableADMix(player->handle);
2489 }
2490
hualing chen86e7d482020-01-16 15:13:33 +08002491 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002492 am_tsplayer_video_params vparams;
2493 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002494 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002495
hualing chen040df222020-01-17 13:35:02 +08002496 player->cur_segment_id = segment_id;
2497
2498 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002499 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002500 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002501 //start audio and video
Zhiqiang Han9adc9722020-11-11 18:38:10 +08002502 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08002503 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002504 DVR_PB_DG(0, "unlock seek start dvr play back start error, not found audio and video info");
hualing chen86e7d482020-01-16 15:13:33 +08002505 pthread_mutex_unlock(&player->lock);
2506 return -1;
2507 }
2508 //add
hualing chen040df222020-01-17 13:35:02 +08002509 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002510 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002511 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002512 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002513 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002514 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002515 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002516 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002517 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002518 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002519 }
hualing chen2aba4022020-03-02 13:49:55 +08002520 AmTsPlayer_setVideoParams(player->handle, &vparams);
2521 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002522 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2523 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2524 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2525 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2526 AmTsPlayer_stopFast(player->handle);
2527 }
hualing chen266b9502020-04-04 17:39:39 +08002528 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002529 }
hualing chene41f4372020-06-06 16:29:17 +08002530 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002531 DVR_PB_DG(1, "start audio seek");
hualing chen2aba4022020-03-02 13:49:55 +08002532 AmTsPlayer_setAudioParams(player->handle, &aparams);
2533 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002534 player->has_audio = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002535 }
hualing chene41f4372020-06-06 16:29:17 +08002536 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002537 player->has_ad_audio = DVR_TRUE;
2538 DVR_PB_DG(1, "start ad audio");
2539 AmTsPlayer_setADParams(player->handle, &adparams);
2540 AmTsPlayer_enableADMix(player->handle);
2541 }
hualing chen86e7d482020-01-16 15:13:33 +08002542 }
hualing chene41f4372020-06-06 16:29:17 +08002543 if (player->state == DVR_PLAYBACK_STATE_PAUSE /*&&
hualing chen2aba4022020-03-02 13:49:55 +08002544 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2545 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2546 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chene41f4372020-06-06 16:29:17 +08002547 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))*/) {
hualing chen2aba4022020-03-02 13:49:55 +08002548 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2549 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002550 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002551 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2552 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002553 player->speed > 1.0f||
2554 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002555 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002556 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002557 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002558 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002559 player->cmd.last_cmd = player->cmd.cur_cmd;
2560 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2561 player->cmd.state = DVR_PLAYBACK_STATE_START;
2562 player->state = DVR_PLAYBACK_STATE_START;
2563 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002564 player->last_send_time_id = UINT64_MAX;
2565 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002566 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002567
2568 return DVR_SUCCESS;
2569}
hualing chen5cbe1a62020-02-10 16:36:36 +08002570
hualing chen5cbe1a62020-02-10 16:36:36 +08002571static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2572 //get cur time of segment
2573 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002574
2575 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002576 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002577 return DVR_FAILURE;
2578 }
2579
hualing chen31140872020-03-25 12:29:26 +08002580 int64_t cache = 0;//defalut es buf cache 500ms
2581 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002582 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002583 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2584 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002585 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002586 DVR_PB_DG(1, "get cur time [%lld] cache:%lld cur id [%lld]last id [%lld] pb cache len [%d] [%lld]", cur, cache, player->cur_segment_id,player->last_send_time_id, player->ts_cache_len, pos);
hualing chen87072a82020-03-12 16:20:12 +08002587 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2588 cache = 0;
2589 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002590 int cur_time = (int)(cur > cache ? cur - cache : 0);
2591 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002592}
2593
2594//get current segment current pcr time of read pos
2595static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2596 //get cur time of segment
2597 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002598
2599 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002600 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002601 return DVR_FAILURE;
2602 }
2603
hualing chen2aba4022020-03-02 13:49:55 +08002604 pthread_mutex_lock(&player->segment_lock);
2605 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002606 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002607 pthread_mutex_unlock(&player->segment_lock);
2608 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002609}
2610
hualing chen4b7c15d2020-04-07 16:13:48 +08002611#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08002612//start replay
2613static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
2614
2615 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2616 //calculate pcr seek time
2617 int t_diff = 0;
2618 int seek_time = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002619
2620 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002621 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002622 return DVR_FAILURE;
2623 }
2624
hualing chen5cbe1a62020-02-10 16:36:36 +08002625 if (player->fffb_start == -1) {
2626 //set fffb start time ms
2627 player->fffb_start = _dvr_time_getClock();
2628 player->fffb_current = player->fffb_start;
2629 //get segment current time pos
2630 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002631 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]", player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002632 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08002633 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08002634 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002635 } else {
2636 player->fffb_current = _dvr_time_getClock();
2637 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08002638 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08002639 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08002640 if (seek_time <= 0) {
2641 //need seek to pre one segment
2642 seek_time = 0;
2643 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002644 //seek segment pos
2645 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08002646 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002647 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08002648 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2649 //set seek time to 0;
2650 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]", seek_time, player->cur_segment_id);
2651 seek_time = 0;
2652 }
hualing chen041c4092020-04-05 15:11:50 +08002653 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
2654 seek_time = 0;
2655 }
hualing chen2aba4022020-03-02 13:49:55 +08002656 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002657 } else {
2658 //
hualing chen4b7c15d2020-04-07 16:13:48 +08002659 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08002660 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002661 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]", seek_time, player->speed,player->cur_segment_id, _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08002662 }
hualing chen2aba4022020-03-02 13:49:55 +08002663 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08002664}
2665
2666
2667//start replay
2668static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
2669 //
2670 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002671
2672 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002673 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002674 return DVR_FAILURE;
2675 }
2676
hualing chen5cbe1a62020-02-10 16:36:36 +08002677 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002678 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002679 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08002680 AmTsPlayer_stopVideoDecoding(player->handle);
2681 }
2682 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002683 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08002684 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002685 AmTsPlayer_stopAudioDecoding(player->handle);
2686 }
hualing chendf118dd2020-05-21 15:49:11 +08002687 if (player->has_ad_audio) {
2688 DVR_PB_DG(1, "fffb stop audio");
2689 player->has_ad_audio =DVR_FALSE;
2690 AmTsPlayer_disableADMix(player->handle);
2691 }
hualing chen2aba4022020-03-02 13:49:55 +08002692
hualing chen5cbe1a62020-02-10 16:36:36 +08002693 //start video and audio
2694
hualing chen2aba4022020-03-02 13:49:55 +08002695 am_tsplayer_video_params vparams;
2696 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002697 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002698 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002699
2700 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08002701 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002702 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002703 //start audio and video
2704 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2705 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002706 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08002707 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002708 return -1;
2709 }
2710
2711 if (VALID_PID(vparams.pid)) {
2712 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002713 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08002714 //DVR_PB_DG(1, "fffb start video and save last frame");
2715 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08002716 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08002717 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2718 AmTsPlayer_setVideoParams(player->handle, &vparams);
2719 AmTsPlayer_startVideoDecoding(player->handle);
2720 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002721 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08002722 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002723 }
hualing chen31140872020-03-25 12:29:26 +08002724 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08002725 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002726 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08002727 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002728 return 0;
2729}
2730
2731static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
2732 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002733 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002734 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002735 return DVR_FAILURE;
2736 }
2737
2738 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08002739 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002740 pthread_mutex_lock(&player->lock);
2741
hualing chen2aba4022020-03-02 13:49:55 +08002742 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002743 DVR_PB_DG(1, "get lock speed [%f]id [%lld]seek_time[%d]", player->speed, player->cur_segment_id, seek_time);
hualing chen041c4092020-04-05 15:11:50 +08002744
hualing chen87072a82020-03-12 16:20:12 +08002745 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2746 //seek time set 0
2747 seek_time = 0;
2748 }
hualing chen041c4092020-04-05 15:11:50 +08002749 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08002750 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
2751 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08002752 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002753 pthread_mutex_unlock(&player->lock);
2754 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002755 //send event here and pause
2756 DVR_Play_Notify_t notify;
2757 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08002758 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08002759 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08002760 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08002761 DVR_PB_DG(1, "*******************send begin event speed [%f] cur [%d]", player->speed, _dvr_get_cur_time(handle));
hualing chen2aba4022020-03-02 13:49:55 +08002762 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08002763 return DVR_SUCCESS;
2764 }
hualing chen2932d372020-04-29 13:44:00 +08002765 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002766 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002767 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002768 }
2769 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002770 _dvr_playback_fffb_replay(handle);
2771
2772 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002773 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002774
hualing chen5cbe1a62020-02-10 16:36:36 +08002775 return DVR_SUCCESS;
2776}
2777
hualing chen87072a82020-03-12 16:20:12 +08002778//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08002779static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002780 //
2781 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002782
2783 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002784 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002785 return DVR_FAILURE;
2786 }
2787
hualing chen5cbe1a62020-02-10 16:36:36 +08002788 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002789 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08002790 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002791 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002792 }
2793
2794 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08002795 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002796 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002797 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002798 //start video and audio
2799
hualing chen2aba4022020-03-02 13:49:55 +08002800 am_tsplayer_video_params vparams;
2801 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002802 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002803 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002804
2805 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08002806 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08002807 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002808 //start audio and video
2809 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08002810 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002811 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08002812 return -1;
2813 }
2814
2815 if (VALID_PID(vparams.pid)) {
2816 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002817 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002818 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08002819 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08002820 }
hualing chen266b9502020-04-04 17:39:39 +08002821 else {
hualing chen2aba4022020-03-02 13:49:55 +08002822 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08002823 }
hualing chen2aba4022020-03-02 13:49:55 +08002824 AmTsPlayer_setVideoParams(player->handle, &vparams);
2825 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08002826 }
hualing chena540a7e2020-03-27 16:44:05 +08002827
2828 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08002829 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08002830 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002831 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08002832 } else {
hualing chena540a7e2020-03-27 16:44:05 +08002833 if (VALID_PID(aparams.pid)) {
2834 player->has_audio = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002835 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08002836 AmTsPlayer_setAudioParams(player->handle, &aparams);
Zhiqiang Hane013e3e2020-12-03 13:34:56 +08002837 AmTsPlayer_startAudioDecoding(player->handle);
hualing chena540a7e2020-03-27 16:44:05 +08002838 }
hualing chendf118dd2020-05-21 15:49:11 +08002839 if (VALID_PID(adparams.pid)) {
2840 player->has_ad_audio = DVR_TRUE;
2841 DVR_PB_DG(1, "start ad audio");
2842 AmTsPlayer_setADParams(player->handle, &adparams);
2843 AmTsPlayer_enableADMix(player->handle);
2844 }
2845
hualing chen7a56cba2020-04-14 14:09:27 +08002846 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002847 AmTsPlayer_stopFast(player->handle);
2848 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
2849 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
2850 }
hualing chen2aba4022020-03-02 13:49:55 +08002851 player->cmd.last_cmd = player->cmd.cur_cmd;
2852 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08002853 player->cmd.state = DVR_PLAYBACK_STATE_START;
2854 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002855 return 0;
2856}
2857
2858
hualing chenb31a6c62020-01-13 17:27:00 +08002859/**\brief Set play speed
2860 * \param[in] handle playback handle
2861 * \param[in] speed playback speed
2862 * \retval DVR_SUCCESS On success
2863 * \return Error code
2864 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002865int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
2866
2867 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002868
2869 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002870 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002871 return DVR_FAILURE;
2872 }
2873
hualing chen4b7c15d2020-04-07 16:13:48 +08002874 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002875 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002876 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002877 return DVR_FAILURE;
2878 }
hualing chenf00cdc82020-06-10 14:23:35 +08002879 if (speed.speed.speed == player->cmd.speed.speed.speed) {
2880 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
2881 return DVR_SUCCESS;
2882 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002883 pthread_mutex_lock(&player->lock);
2884 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
2885 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
2886 player->cmd.last_cmd = player->cmd.cur_cmd;
2887 }
hualing chene41f4372020-06-06 16:29:17 +08002888
hualing chen31140872020-03-25 12:29:26 +08002889 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08002890 IS_KERNEL_SPEED(speed.speed.speed) ) {
2891 //case 1. not start play.only set speed
2892 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2893 //only set speed.and return;
2894 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
2895 player->cmd.speed.speed = speed.speed;
2896 player->speed = (float)speed.speed.speed/(float)100;
2897 player->fffb_play = DVR_FALSE;
2898 pthread_mutex_unlock(&player->lock);
2899 return DVR_SUCCESS;
2900 }
2901 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08002902 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
2903 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002904 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002905 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2906 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002907 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002908 AmTsPlayer_stopFast(player->handle);
2909 pthread_mutex_unlock(&player->lock);
2910 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
2911 pthread_mutex_lock(&player->lock);
2912 } else {
2913 //set play speed and if audio is start, stop audio.
2914 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002915 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002916 AmTsPlayer_stopAudioDecoding(player->handle);
2917 player->has_audio = DVR_FALSE;
2918 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002919 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002920 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002921 }
hualing chenbcada022020-04-22 14:27:01 +08002922 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08002923 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002924 player->cmd.speed.speed = speed.speed;
2925 player->speed = (float)speed.speed.speed/(float)100;
2926 pthread_mutex_unlock(&player->lock);
2927 return DVR_SUCCESS;
2928 }
hualing chen31140872020-03-25 12:29:26 +08002929 //case 3 fffb mode
2930 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2931 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2932 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002933 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002934 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002935 player->cmd.speed.speed = speed.speed;
2936 player->speed = (float)speed.speed.speed/(float)100;
2937 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08002938 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002939 pthread_mutex_unlock(&player->lock);
2940 return DVR_SUCCESS;
2941 }
2942 }
2943 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08002944 IS_KERNEL_SPEED(speed.speed.speed)) {
2945 //case 1. cur speed is kernel support speed,set kernel speed.
2946 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08002947 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002948 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2949 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002950 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002951 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08002952 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08002953 } else {
2954 //set play speed and if audio is start, stop audio.
2955 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002956 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002957 AmTsPlayer_stopAudioDecoding(player->handle);
2958 player->has_audio = DVR_FALSE;
2959 }
hualing chenf00cdc82020-06-10 14:23:35 +08002960 DVR_PB_DG(1, "start fast");
2961 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08002962 }
hualing chena540a7e2020-03-27 16:44:05 +08002963 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002964 player->cmd.speed.speed = speed.speed;
2965 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08002966 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002967 pthread_mutex_unlock(&player->lock);
2968 return DVR_SUCCESS;
2969 }
2970 //case 2 fffb mode
2971 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2972 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2973 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002974 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002975 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002976 player->cmd.speed.speed = speed.speed;
2977 player->speed = (float)speed.speed.speed/(float)100;
2978 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08002979 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002980 pthread_mutex_unlock(&player->lock);
2981 return DVR_SUCCESS;
2982 }
hualing chen31140872020-03-25 12:29:26 +08002983 }
hualing chena540a7e2020-03-27 16:44:05 +08002984 if (IS_KERNEL_SPEED(speed.speed.speed)) {
2985 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08002986 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08002987 } else {
hualing chen31140872020-03-25 12:29:26 +08002988 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08002989 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
2990 else
2991 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08002992 player->fffb_play = DVR_TRUE;
2993 }
2994 DVR_Bool_t init_last_time = DVR_FALSE;
2995 if (player->speed > 0.0f && speed.speed.speed < 0) {
2996 init_last_time = DVR_TRUE;
2997 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
2998 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002999 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003000 player->cmd.speed.mode = speed.mode;
3001 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003002 player->speed = (float)speed.speed.speed/(float)100;
3003 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003004 _dvr_init_fffb_t(handle);
3005 if (init_last_time == DVR_TRUE)
3006 player->last_send_time_id = UINT64_MAX;
3007
hualing chen87072a82020-03-12 16:20:12 +08003008 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003009 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3010 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003011 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003012 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003013 _dvr_playback_replay(handle, DVR_FALSE);
3014 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3015 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3016 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003017 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003018 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003019 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003020 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003021 return DVR_SUCCESS;
3022}
hualing chen2932d372020-04-29 13:44:00 +08003023
hualing chenb31a6c62020-01-13 17:27:00 +08003024/**\brief Get playback status
3025 * \param[in] handle playback handle
3026 * \param[out] p_status playback status
3027 * \retval DVR_SUCCESS On success
3028 * \return Error code
3029 */
hualing chen2932d372020-04-29 13:44:00 +08003030static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3031 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003032//
3033 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003034
3035 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003036 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003037 return DVR_FAILURE;
3038 }
hualing chen2932d372020-04-29 13:44:00 +08003039 if (is_lock ==DVR_TRUE)
3040 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003041 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003042 //when got first frame we will change to pause state.this only from start play to got first frame
hualing chen87072a82020-03-12 16:20:12 +08003043 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3044 player->state == DVR_PLAYBACK_STATE_START) {
3045 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3046 }
hualing chen041c4092020-04-05 15:11:50 +08003047
hualing chencc91e1c2020-02-28 13:26:17 +08003048 p_status->time_end = _dvr_get_end_time(handle);
hualing chen2aba4022020-03-02 13:49:55 +08003049 p_status->time_cur = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003050 if (player->last_send_time_id == UINT64_MAX) {
3051 player->last_send_time_id = player->cur_segment_id;
3052 player->last_cur_time = p_status->time_cur;
3053 }
3054 if (player->last_send_time_id == player->cur_segment_id) {
3055 if (player->speed > 0.0f ) {
3056 //ff
3057 if (p_status->time_cur < player->last_cur_time ) {
3058 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]", player->last_cur_time, p_status->time_cur, player->last_cur_time - p_status->time_cur);
3059 p_status->time_cur = player->last_cur_time;
3060 } else {
3061 player->last_cur_time = p_status->time_cur;
3062 }
hualing chene41f4372020-06-06 16:29:17 +08003063 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003064 //fb
3065 if (p_status->time_cur > player->last_cur_time ) {
3066 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]", player->last_cur_time, p_status->time_cur, p_status->time_cur - player->last_cur_time );
3067 p_status->time_cur = player->last_cur_time;
3068 } else {
3069 player->last_cur_time = p_status->time_cur;
3070 }
3071 }
3072 } else {
3073 player->last_cur_time = p_status->time_cur;
3074 }
3075 player->last_send_time_id = player->cur_segment_id;
hualing chen041c4092020-04-05 15:11:50 +08003076 p_status->segment_id = player->cur_segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003077
hualing chen5cbe1a62020-02-10 16:36:36 +08003078 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003079 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003080 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003081 DVR_PB_DG(1, "player real state[%s]state[%s]cur[%d]end[%d] id[%lld]playflag[%d]speed[%f]is_lock[%d]",
hualing chen6d24aa92020-03-23 18:43:47 +08003082 _dvr_playback_state_toString(player->state),
3083 _dvr_playback_state_toString(p_status->state),
hualing chena540a7e2020-03-27 16:44:05 +08003084 p_status->time_cur, p_status->time_end,
3085 p_status->segment_id,player->play_flag,
hualing chen2932d372020-04-29 13:44:00 +08003086 player->speed,
3087 is_lock);
3088 if (is_lock ==DVR_TRUE)
3089 pthread_mutex_unlock(&player->lock);
3090 return DVR_SUCCESS;
3091}
3092
3093
3094/**\brief Get playback status
3095 * \param[in] handle playback handle
3096 * \param[out] p_status playback status
3097 * \retval DVR_SUCCESS On success
3098 * \return Error code
3099 */
3100int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3101 DVR_PlaybackStatus_t *p_status) {
3102//
3103 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3104
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003105 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3106
hualing chen2932d372020-04-29 13:44:00 +08003107 if (player == NULL) {
3108 DVR_PB_DG(1, "player is NULL");
3109 return DVR_FAILURE;
3110 }
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003111 pthread_mutex_lock(&player->lock);
3112 if (!player->has_video && !player->has_audio)
3113 p_status->time_cur = 0;
3114 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003115
hualing chenb31a6c62020-01-13 17:27:00 +08003116 return DVR_SUCCESS;
3117}
3118
hualing chen040df222020-01-17 13:35:02 +08003119void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3120 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003121 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3122 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3123 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3124 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3125 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3126 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3127 DVR_PB_DG(1, "segment sub apid: 0x%x sub afmt:0x%x", segment->pids.ad.pid,segment->pids.ad.format);
hualing chen86e7d482020-01-16 15:13:33 +08003128 }
hualing chenb31a6c62020-01-13 17:27:00 +08003129}
3130
hualing chen5cbe1a62020-02-10 16:36:36 +08003131int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003132 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003133
hualing chena540a7e2020-03-27 16:44:05 +08003134 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003135 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003136 return DVR_FAILURE;
3137 }
3138
hualing chen040df222020-01-17 13:35:02 +08003139 DVR_PlaybackSegmentInfo_t *segment;
3140 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003141 {
hualing chen040df222020-01-17 13:35:02 +08003142 if (segment_id >= 0) {
3143 if (segment->segment_id == segment_id) {
3144 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003145 break;
3146 }
3147 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003148 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003149 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003150 }
3151 }
3152 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003153}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003154
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003155int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003156{
3157 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3158 DVR_RETURN_IF_FALSE(player);
3159 DVR_RETURN_IF_FALSE(func);
3160
hualing chen4b7c15d2020-04-07 16:13:48 +08003161 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003162 pthread_mutex_lock(&player->lock);
3163
3164 player->dec_func = func;
3165 player->dec_userdata = userdata;
3166
3167 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003168 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003169 return DVR_SUCCESS;
3170}
3171
3172int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3173{
3174 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3175 DVR_RETURN_IF_FALSE(player);
3176 DVR_RETURN_IF_FALSE(p_secure_buf);
3177 DVR_RETURN_IF_FALSE(len);
3178
hualing chen4b7c15d2020-04-07 16:13:48 +08003179 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003180 pthread_mutex_lock(&player->lock);
3181
3182 player->is_secure_mode = 1;
3183 player->secure_buffer = p_secure_buf;
3184 player->secure_buffer_size = len;
3185
3186 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003187 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003188 return DVR_SUCCESS;
3189}