blob: 8c4d9194c4c7e4acebe34c7999821113215daac5 [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 chen4b7c15d2020-04-07 16:13:48 +0800412 if (1) {
413 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) {
421 player->send_time = _dvr_time_getClock() + 1000;
422 } else if (player->send_time > _dvr_time_getClock()) {
423 return DVR_SUCCESS;
424 }
425 player->send_time = _dvr_time_getClock() + 1000;
426 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 }
522 }
523 if (found != 2) {
524 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800525 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800526 return DVR_FAILURE;
527 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800528 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800529 return DVR_SUCCESS;
530}
531
532//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800533static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800534
hualing chen040df222020-01-17 13:35:02 +0800535 DVR_Playback_t *player = (DVR_Playback_t *) handle;
536 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800537 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800538
hualing chena540a7e2020-03-27 16:44:05 +0800539 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800540 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800541 return DVR_FAILURE;
542 }
543
hualing chen86e7d482020-01-16 15:13:33 +0800544 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800545 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800546
hualing chen040df222020-01-17 13:35:02 +0800547 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800548 {
hualing chencc91e1c2020-02-28 13:26:17 +0800549 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800550 //get first segment from list, case segment is not open
551 if (!IS_FB(player->speed))
552 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800553 } else if (segment->segment_id == player->cur_segment_id) {
554 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800555 found_eq_id = 1;
556 if (!IS_FB(player->speed)) {
557 found = 1;
558 continue;
559 } else {
560 //if is fb mode.we need used pre segment
561 if (pre_segment != NULL) {
562 found = 1;
563 } else {
564 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800565 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800566 return DVR_FAILURE;
567 }
568 }
hualing chen86e7d482020-01-16 15:13:33 +0800569 }
570 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800571 if (IS_FB(player->speed)) {
572 //used pre segment
573 segment = pre_segment;
574 }
hualing chencc91e1c2020-02-28 13:26:17 +0800575 //save segment info
576 player->last_segment_id = player->cur_segment_id;
hualing chen87072a82020-03-12 16:20:12 +0800577 player->last_segment.segment_id = player->cur_segment.segment_id;
578 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800579 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
580 //pids
581 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
582
hualing chen5cbe1a62020-02-10 16:36:36 +0800583 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800584 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800585 player->cur_segment_id = segment->segment_id;
586 player->cur_segment.segment_id = segment->segment_id;
587 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800588 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 +0800589 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800590 //pids
hualing chen040df222020-01-17 13:35:02 +0800591 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800592 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800593 break;
hualing chen86e7d482020-01-16 15:13:33 +0800594 }
hualing chen2aba4022020-03-02 13:49:55 +0800595 pre_segment = segment;
596 }
597 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
598 //used the last one segment to open
599 //get segment info
600 player->segment_is_open = DVR_TRUE;
601 player->cur_segment_id = pre_segment->segment_id;
602 player->cur_segment.segment_id = pre_segment->segment_id;
603 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800604 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 +0800605 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
606 //pids
607 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
608 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800609 }
610 if (found != 2) {
611 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800612 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800613 }
614 return DVR_SUCCESS;
615}
hualing chen040df222020-01-17 13:35:02 +0800616//open next segment to play,if reach list end return errro.
617static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800618{
hualing chen040df222020-01-17 13:35:02 +0800619 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800620 Segment_OpenParams_t params;
621 int ret = DVR_SUCCESS;
622
hualing chena540a7e2020-03-27 16:44:05 +0800623 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800624 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800625 return DVR_FAILURE;
626 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800627 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800628
629 ret = _dvr_get_next_segmentId(handle);
630 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800631 DVR_PB_DG(1, "not found segment info");
632 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800633 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800634 }
635
636 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800637 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800638 segment_close(player->r_handle);
639 player->r_handle = NULL;
640 }
641
642 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800643 //cp chur segment path to location
644 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800645 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800646 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800647 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
648
hualing chen86e7d482020-01-16 15:13:33 +0800649 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800650 if (ret == DVR_FAILURE) {
651 DVR_PB_DG(1, "open segment error");
652 }
hualing chen87072a82020-03-12 16:20:12 +0800653 pthread_mutex_unlock(&player->segment_lock);
654 int total = _dvr_get_end_time( handle);
655 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800656 if (IS_FB(player->speed)) {
657 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800658 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800659 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800660 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800661 }
hualing chen87072a82020-03-12 16:20:12 +0800662 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800663 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800664 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800665 return ret;
666}
667
hualing chen5cbe1a62020-02-10 16:36:36 +0800668//open next segment to play,if reach list end return errro.
669static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
670{
671 DVR_Playback_t *player = (DVR_Playback_t *) handle;
672 Segment_OpenParams_t params;
673 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800674 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800675 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800676 return DVR_FAILURE;
677 }
hualing chencc91e1c2020-02-28 13:26:17 +0800678 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800679 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800680 }
hualing chencc91e1c2020-02-28 13:26:17 +0800681 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800682 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800683 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800684 return DVR_FAILURE;
685 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800686 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800687 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800688
689 DVR_PlaybackSegmentInfo_t *segment;
690
691 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800692
hualing chen5cbe1a62020-02-10 16:36:36 +0800693 list_for_each_entry(segment, &player->segment_list, head)
694 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800695 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 +0800696 if (segment->segment_id == segment_id) {
697 found = 1;
698 }
699 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800700 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 +0800701 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800702 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800703 player->cur_segment_id = segment->segment_id;
704 player->cur_segment.segment_id = segment->segment_id;
705 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800706 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800707 //pids
708 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800709 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 +0800710 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800711 }
712 }
hualing chencc91e1c2020-02-28 13:26:17 +0800713 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800714 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800715 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800716 return DVR_FAILURE;
717 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800718 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800719 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800720 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800721 params.segment_id = (uint64_t)player->cur_segment.segment_id;
722 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800723 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 +0800724 if (player->r_handle != NULL) {
725 segment_close(player->r_handle);
726 player->r_handle = NULL;
727 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800728 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800729 if (ret == DVR_FAILURE) {
730 DVR_PB_DG(1, "segment opne error");
731 }
hualing chen2aba4022020-03-02 13:49:55 +0800732 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800733 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800734
hualing chen4b7c15d2020-04-07 16:13:48 +0800735 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 +0800736 return ret;
737}
738
739
740//get play info by segment id
741static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
742 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800743 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800744 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800745
746 DVR_Playback_t *player = (DVR_Playback_t *) handle;
747 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800748 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800749 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800750 return DVR_FAILURE;
751 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800752
753 int found = 0;
754
755 list_for_each_entry(segment, &player->segment_list, head)
756 {
hualing chen87072a82020-03-12 16:20:12 +0800757 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800758 //get first segment from list
759 found = 1;
760 }
761 if (segment->segment_id == segment_id) {
762 found = 1;
763 }
764 if (found == 1) {
765 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800766 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800767 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800768 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800769 player->cur_segment.segment_id = segment->segment_id;
770 player->cur_segment.flags = segment->flags;
771 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800772 player->cur_segment.pids.video.pid = segment->pids.video.pid;
773 player->cur_segment.pids.video.format = segment->pids.video.format;
774 player->cur_segment.pids.video.type = segment->pids.video.type;
775 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
776 player->cur_segment.pids.audio.format = segment->pids.audio.format;
777 player->cur_segment.pids.audio.type = segment->pids.audio.type;
778 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
779 player->cur_segment.pids.ad.format = segment->pids.ad.format;
780 player->cur_segment.pids.ad.type = segment->pids.ad.type;
781 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800782 //
hualing chen2aba4022020-03-02 13:49:55 +0800783 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800784 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800785 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800786 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800787 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
788 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800789 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 +0800790 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800791 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800792 }
793 }
hualing chencc91e1c2020-02-28 13:26:17 +0800794 if (found != 2) {
795 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800796 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800797 return DVR_FAILURE;
798 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800799
800 return DVR_SUCCESS;
801}
hualing chencc91e1c2020-02-28 13:26:17 +0800802static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
803 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800804 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800805 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800806 return DVR_FAILURE;
807 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800808
hualing chencc91e1c2020-02-28 13:26:17 +0800809 //compare cur segment
810 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
811 {
812 //check video pids, stop or restart
813 _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids.video, 0);
814 //check audio pids stop or restart
hualing chen7a56cba2020-04-14 14:09:27 +0800815 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 +0800816 _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);
819 //check pcr pids stop or restart
820 _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids.pcr, 3);
821 }
hualing chena540a7e2020-03-27 16:44:05 +0800822 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800823}
hualing chen5cbe1a62020-02-10 16:36:36 +0800824
hualing chencc91e1c2020-02-28 13:26:17 +0800825static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
826{
827 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800828 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800829 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800830 return DVR_FAILURE;
831 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800832 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
833 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
834 return DVR_SUCCESS;
835 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800836 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 +0800837 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
838 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800839 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800840 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800841 AmTsPlayer_showVideo(player->handle);
842 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800843 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
844 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800845 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800846 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800847 AmTsPlayer_hideVideo(player->handle);
848 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800849 }
850 return DVR_SUCCESS;
851}
hualing chena540a7e2020-03-27 16:44:05 +0800852static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
853 DVR_Playback_t *player = (DVR_Playback_t *) handle;
854 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800855 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800856 return DVR_TRUE;
857 }
hualing chene41f4372020-06-06 16:29:17 +0800858 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE || 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800859 if (player->first_frame == 1) {
860 return DVR_TRUE;
861 } else {
862 return DVR_FALSE;
863 }
864 } else {
865 return DVR_TRUE;
866 }
867}
hualing chen86e7d482020-01-16 15:13:33 +0800868static void* _dvr_playback_thread(void *arg)
869{
hualing chen040df222020-01-17 13:35:02 +0800870 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800871 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800872 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800873 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800874 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800875
hualing chen39628212020-05-14 10:35:13 +0800876 #define MAX_REACHEND_TIMEOUT (3000)
877 int reach_end_timeout = 0;//ms
878 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800879 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800880 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800881 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800882 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800883 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
884
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800885 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800886 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800887 DVR_Bool_t goto_rewrite = DVR_FALSE;
hualing chene41f4372020-06-06 16:29:17 +0800888 int retry_open_seg = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800889 if (player->is_secure_mode) {
890 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800891 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800892 return NULL;
893 }
894 }
hualing chen86e7d482020-01-16 15:13:33 +0800895 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800896 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800897 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800898 return NULL;
899 }
hualing chen2aba4022020-03-02 13:49:55 +0800900 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
901 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800902
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800903 dec_bufs.buf_data = malloc(dec_buf_size);
904 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800905 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800906 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800907 return NULL;
908 }
909 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
910 dec_bufs.buf_size = dec_buf_size;
911
hualing chencc91e1c2020-02-28 13:26:17 +0800912 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800913 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
914 }
hualing chen86e7d482020-01-16 15:13:33 +0800915
hualing chen86e7d482020-01-16 15:13:33 +0800916 if (ret != DVR_SUCCESS) {
917 if (buf != NULL) {
918 free(buf);
919 buf = NULL;
920 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800921 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +0800922 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +0800923 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800924 }
hualing chen4fe3bee2020-10-23 13:58:52 +0800925 DVR_PB_DG(1, "player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
926 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +0800927 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
928 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
929 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
930 player->first_trans_ok = DVR_TRUE;
931 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
932 }
hualing chencc91e1c2020-02-28 13:26:17 +0800933 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +0800934 //set video show
935 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +0800936
hualing chen86e7d482020-01-16 15:13:33 +0800937 int trick_stat = 0;
938 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +0800939
hualing chen86e7d482020-01-16 15:13:33 +0800940 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +0800941 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800942
hualing chen2aba4022020-03-02 13:49:55 +0800943 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
944 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +0800945 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +0800946 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +0800947 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +0800948 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +0800949 {
hualing chen2aba4022020-03-02 13:49:55 +0800950 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
951 if (trick_stat > 0) {
hualing chenbcada022020-04-22 14:27:01 +0800952 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 +0800953 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 +0800954 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +0800955 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +0800956 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +0800957 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
958 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +0800959 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
960 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chenbcada022020-04-22 14:27:01 +0800961 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 +0800962 //need change to pause state
963 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
964 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +0800965 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +0800966 //clear flag
hualing chen31140872020-03-25 12:29:26 +0800967 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +0800968 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800969 AmTsPlayer_pauseVideoDecoding(player->handle);
970 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +0800971 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800972 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +0800973 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800974 }
975 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
976 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +0800977 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +0800978 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +0800979 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
980 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 +0800981 //used timeout wait need lock first,so we unlock and lock
982 //pthread_mutex_unlock(&player->lock);
983 //pthread_mutex_lock(&player->lock);
984 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
985 pthread_mutex_unlock(&player->lock);
986 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +0800987 } else if (_dvr_time_getClock() < player->next_fffb_time) {
988 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);
989 //used timeout wait need lock first,so we unlock and lock
990 //pthread_mutex_unlock(&player->lock);
991 //pthread_mutex_lock(&player->lock);
992 AmTsPlayer_pauseVideoDecoding(player->handle);
993 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
994 pthread_mutex_unlock(&player->lock);
995 continue;
996
hualing chen2aba4022020-03-02 13:49:55 +0800997 }
hualing chen2932d372020-04-29 13:44:00 +0800998 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 +0800999 pthread_mutex_unlock(&player->lock);
1000 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001001 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001002 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1003 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001004 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001005 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001006 pthread_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001007 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001008 }else if (player->fffb_play == DVR_TRUE){
1009 //for first into fffb when reset speed
1010 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1011 _dvr_time_getClock() < player->next_fffb_time) {
1012 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);
1013 //used timeout wait need lock first,so we unlock and lock
1014 //pthread_mutex_unlock(&player->lock);
1015 //pthread_mutex_lock(&player->lock);
1016 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1017 pthread_mutex_unlock(&player->lock);
1018 continue;
1019 }
hualing chen2932d372020-04-29 13:44:00 +08001020 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 +08001021 pthread_mutex_unlock(&player->lock);
1022 goto_rewrite = DVR_FALSE;
1023 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001024 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001025 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1026 player->first_frame = 0;
1027 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1028 pthread_mutex_lock(&player->lock);
1029 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001030 }
hualing chenb31a6c62020-01-13 17:27:00 +08001031 }
hualing chen86e7d482020-01-16 15:13:33 +08001032
hualing chen87072a82020-03-12 16:20:12 +08001033 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001034 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001035 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001036 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001037 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1038 pthread_mutex_unlock(&player->lock);
1039 continue;
1040 }
hualing chen266b9502020-04-04 17:39:39 +08001041 //when seek action is done. we need drop write timeout data.
1042 if (player->drop_ts == DVR_TRUE) {
1043 goto_rewrite = DVR_FALSE;
1044 real_read = 0;
1045 player->drop_ts = DVR_FALSE;
1046 }
hualing chen2aba4022020-03-02 13:49:55 +08001047 if (goto_rewrite == DVR_TRUE) {
1048 goto_rewrite = DVR_FALSE;
1049 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001050 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001051 goto rewrite;
1052 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001053 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001054 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001055 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001056 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001057 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chenfbf8e022020-06-15 13:43:11 +08001058 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001059 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001060 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001061 if (read < 0 && errno == EIO) {
1062 //EIO ERROR, EXIT THRAD
1063 DVR_PB_DG(1, "read error.EIO error, exit thread");
1064 DVR_Play_Notify_t notify;
1065 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1066 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001067 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001068 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001069 goto end;
1070 } else if (read < 0) {
1071 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1072 }
hualing chen87072a82020-03-12 16:20:12 +08001073 //if on fb mode and read file end , we need calculate pos to retry read.
1074 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001075 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 +08001076 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1077 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001078 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1079 pthread_mutex_unlock(&player->lock);
1080 continue;
1081 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001082 //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 +08001083 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001084 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001085 #define MIN_CACHE_TIME (3000)
1086 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
1087 if (_cache_time > MIN_CACHE_TIME) {
1088 DVR_PB_DG(1, "read end but cache time is %d > %d, to sleep and continue", _cache_time, MIN_CACHE_TIME);
1089 pthread_mutex_lock(&player->lock);
1090 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, (_cache_time -MIN_CACHE_TIME));
1091 pthread_mutex_unlock(&player->lock);
1092 //continue;
1093 }
hualing chen040df222020-01-17 13:35:02 +08001094 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001095 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001096 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001097
1098 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001099 //del delay time max check.
hualing chen39628212020-05-14 10:35:13 +08001100 if ((ret != DVR_SUCCESS &&
hualing chen041c4092020-04-05 15:11:50 +08001101 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001102 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001103 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1104 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001105 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001106 DVR_Play_Notify_t notify;
1107 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1108 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1109 //get play statue not here
1110 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001111 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001112 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001113 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 +08001114 pthread_mutex_lock(&player->lock);
1115 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1116 pthread_mutex_unlock(&player->lock);
1117 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001118 } else if (ret != DVR_SUCCESS) {
1119 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001120 if (delay < cache_time) {
1121 //delay time is changed and then has data to play, so not start timeout
1122 } else {
1123 reach_end_timeout = reach_end_timeout + timeout;
1124 }
1125 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001126 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001127 pthread_mutex_lock(&player->lock);
1128 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1129 pthread_mutex_unlock(&player->lock);
1130 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001131 }
hualing chen39628212020-05-14 10:35:13 +08001132 reach_end_timeout = 0;
1133 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001134 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001135 //change next segment success case
1136 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001137 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001138 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1139 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001140 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001141 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001142 }
hualing chen39628212020-05-14 10:35:13 +08001143 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001144 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001145 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001146 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001147
hualing chena540a7e2020-03-27 16:44:05 +08001148 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001149 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001150 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001151 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001152 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001153 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001154 }
hualing chen266b9502020-04-04 17:39:39 +08001155 //if need write whole block size, we need check read buf len is eq block size.
1156 if (b_writed_whole_block == DVR_TRUE) {
1157 //buf_len is block size value.
1158 if (real_read < buf_len) {
1159 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001160 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001161 pthread_mutex_lock(&player->lock);
1162 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1163 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001164 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001165 continue;
1166 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001167 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 +08001168 }
1169 }
1170
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001171 if (player->dec_func) {
1172 DVR_CryptoParams_t crypto_params;
1173
1174 memset(&crypto_params, 0, sizeof(crypto_params));
1175 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1176 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1177 crypto_params.segment_id = player->cur_segment.segment_id;
hualing chen1f26ffa2020-11-03 10:39:20 +08001178 crypto_params.offset = segment_tell_position(player->r_handle) - wbufs.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001179 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1180 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001181 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1182 crypto_params.input_buffer.addr = (size_t)buf;
1183 crypto_params.input_buffer.size = real_read;
1184
1185 if (player->is_secure_mode) {
1186 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1187 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1188 crypto_params.output_buffer.size = dec_buf_size;
1189 ret = player->dec_func(&crypto_params, player->dec_userdata);
1190 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001191 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001192 } else {
1193 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1194 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1195 crypto_params.output_buffer.size = dec_buf_size;
1196 ret = player->dec_func(&crypto_params, player->dec_userdata);
1197 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001198 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001199 }
1200 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001201 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001202 }
pengfei.liufda2a972020-04-09 14:47:15 +08001203 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001204 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001205rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001206 if (player->drop_ts == DVR_TRUE) {
1207 //need drop ts data when seek occur.we need read next loop,drop this ts data
1208 goto_rewrite = DVR_FALSE;
1209 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001210 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001211 player->drop_ts = DVR_FALSE;
1212 continue;
1213 }
hualing chen5605eed2020-05-26 18:18:06 +08001214 player->ts_cache_len = real_read;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001215 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1216 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001217 player->ts_cache_len = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001218 real_read = 0;
1219 write_success++;
hualing chen5605eed2020-05-26 18:18:06 +08001220 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001221 continue;
hualing chen87072a82020-03-12 16:20:12 +08001222 } else {
hualing chen2932d372020-04-29 13:44:00 +08001223 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 +08001224 write_success = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001225 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001226 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001227 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001228 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001229 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001230 break;
1231 }
hualing chen2aba4022020-03-02 13:49:55 +08001232 goto_rewrite = DVR_TRUE;
1233 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001234 }
1235 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001236end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001237 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001238 free(buf);
1239 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001240 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001241}
1242
1243
hualing chen040df222020-01-17 13:35:02 +08001244static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001245{
hualing chen040df222020-01-17 13:35:02 +08001246 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001247
1248 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001249 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001250 return DVR_FAILURE;
1251 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001252 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001253 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001254 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001255 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001256 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001257 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001258 if (rc < 0)
1259 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001260 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001261}
1262
1263
hualing chen040df222020-01-17 13:35:02 +08001264static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001265{
hualing chen040df222020-01-17 13:35:02 +08001266 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001267
1268 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001269 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001270 return DVR_FAILURE;
1271 }
1272
hualing chen4b7c15d2020-04-07 16:13:48 +08001273 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001274 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001275 {
1276 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001277 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001278 pthread_join(player->playback_thread, NULL);
1279 }
1280 if (player->r_handle) {
1281 segment_close(player->r_handle);
1282 player->r_handle = NULL;
1283 }
hualing chen7a56cba2020-04-14 14:09:27 +08001284 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001285 return 0;
1286}
1287
hualing chenb31a6c62020-01-13 17:27:00 +08001288/**\brief Open an dvr palyback
1289 * \param[out] p_handle dvr playback addr
1290 * \param[in] params dvr playback open parameters
1291 * \retval DVR_SUCCESS On success
1292 * \return Error code
1293 */
hualing chen040df222020-01-17 13:35:02 +08001294int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001295
hualing chen040df222020-01-17 13:35:02 +08001296 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001297 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001298
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001299 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001300
hualing chen86e7d482020-01-16 15:13:33 +08001301 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001302 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001303 pthread_condattr_init(&cattr);
1304 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1305 pthread_cond_init(&player->cond, &cattr);
1306 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001307
hualing chen5cbe1a62020-02-10 16:36:36 +08001308 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001309 INIT_LIST_HEAD(&player->segment_list);
1310 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1311 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001312 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001313 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001314 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001315 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001316 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001317 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001318
hualing chen86e7d482020-01-16 15:13:33 +08001319 //store open params
hualing chen040df222020-01-17 13:35:02 +08001320 player->openParams.dmx_dev_id = params->dmx_dev_id;
1321 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001322 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001323 player->openParams.event_fn = params->event_fn;
1324 player->openParams.event_userdata = params->event_userdata;
hualing chenfbf8e022020-06-15 13:43:11 +08001325 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001326
hualing chen5cbe1a62020-02-10 16:36:36 +08001327 player->has_pids = params->has_pids;
1328
hualing chen2aba4022020-03-02 13:49:55 +08001329 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001330
1331 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1332 //for test get callback
1333 if (0 && player->player_callback_func == NULL) {
1334 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1335 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +08001336 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 +08001337 }
1338 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001339
hualing chen86e7d482020-01-16 15:13:33 +08001340 //init has audio and video
1341 player->has_video = DVR_FALSE;
1342 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001343 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001344 player->last_segment_id = 0LL;
1345 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001346
hualing chen5cbe1a62020-02-10 16:36:36 +08001347 //init ff fb time
1348 player->fffb_current = -1;
1349 player->fffb_start =-1;
1350 player->fffb_start_pcr = -1;
1351 //seek time
1352 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001353 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001354
1355 //init secure stuff
1356 player->dec_func = NULL;
1357 player->dec_userdata = NULL;
1358 player->is_secure_mode = 0;
1359 player->secure_buffer = NULL;
1360 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001361 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001362
hualing chen4b7c15d2020-04-07 16:13:48 +08001363 player->fffb_play = DVR_FALSE;
1364
1365 player->last_send_time_id = UINT64_MAX;
1366 player->last_cur_time = 0;
1367
hualing chen86e7d482020-01-16 15:13:33 +08001368 *p_handle = player;
1369 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001370}
1371
1372/**\brief Close an dvr palyback
1373 * \param[in] handle playback handle
1374 * \retval DVR_SUCCESS On success
1375 * \return Error code
1376 */
hualing chen040df222020-01-17 13:35:02 +08001377int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001378
hualing chen86e7d482020-01-16 15:13:33 +08001379 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001380 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001381 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001382 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001383 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001384 return DVR_FAILURE;
1385 }
1386
hualing chencc91e1c2020-02-28 13:26:17 +08001387 if (player->state != DVR_PLAYBACK_STATE_STOP)
1388 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001389 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001390 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001391 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1392 } else {
1393 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001394 }
hualing chen7a56cba2020-04-14 14:09:27 +08001395 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001396 pthread_mutex_destroy(&player->lock);
1397 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001398
1399 if (player) {
1400 free(player);
1401 player = NULL;
1402 }
hualing chen7a56cba2020-04-14 14:09:27 +08001403 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001404 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001405}
1406
hualing chenb31a6c62020-01-13 17:27:00 +08001407/**\brief Start play audio and video, used start auido api and start video api
1408 * \param[in] handle playback handle
1409 * \param[in] params audio playback params,contains fmt and pid...
1410 * \retval DVR_SUCCESS On success
1411 * \return Error code
1412 */
hualing chen040df222020-01-17 13:35:02 +08001413int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1414 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001415 am_tsplayer_video_params vparams;
1416 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001417 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001418
1419 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001420 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001421 return DVR_FAILURE;
1422 }
hualing chencc91e1c2020-02-28 13:26:17 +08001423 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001424 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001425
hualing chena540a7e2020-03-27 16:44:05 +08001426 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001427 //can used start api to resume playback
1428 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1429 return dvr_playback_resume(handle);
1430 }
hualing chen87072a82020-03-12 16:20:12 +08001431 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001432 //if flag is puased and not decodec first frame. if user resume, we need
1433 //clear flag and set trickmode none
1434 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1435 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1436 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1437 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1438 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001439 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001440 return DVR_SUCCESS;
1441 }
hualing chen86e7d482020-01-16 15:13:33 +08001442 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001443 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001444 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001445 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001446 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001447 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001448 //start audio and video
Zhiqiang Hana9d261b2020-11-11 18:38:10 +08001449 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001450 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001451 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001452 pthread_mutex_unlock(&player->lock);
1453 DVR_Play_Notify_t notify;
1454 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1455 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1456 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1457 notify.info.transition_failed_data.segment_id = segment_id;
1458 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001459 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001460 return -1;
1461 }
hualing chen31140872020-03-25 12:29:26 +08001462
hualing chencc91e1c2020-02-28 13:26:17 +08001463 {
hualing chen86e7d482020-01-16 15:13:33 +08001464 if (VALID_PID(vparams.pid)) {
1465 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001466 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001467 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001468 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001469 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1470 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001471 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001472 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001473 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001474 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001475 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001476 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001477 }
hualing chena93bbbc2020-12-22 17:23:42 +08001478 AmTsPlayer_showVideo(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001479 AmTsPlayer_setVideoParams(player->handle, &vparams);
1480 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001481 }
hualing chena540a7e2020-03-27 16:44:05 +08001482
hualing chen4b7c15d2020-04-07 16:13:48 +08001483 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1484 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001485 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1486 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1487 player->cmd.state = DVR_PLAYBACK_STATE_START;
1488 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001489 } else {
1490 player->cmd.last_cmd = player->cmd.cur_cmd;
1491 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001492 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001493 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001494 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001495 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001496 } else {
1497 if (VALID_PID(aparams.pid)) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001498 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08001499 player->has_audio = DVR_TRUE;
1500 AmTsPlayer_setAudioParams(player->handle, &aparams);
1501 AmTsPlayer_startAudioDecoding(player->handle);
1502 }
hualing chendf118dd2020-05-21 15:49:11 +08001503 if (VALID_PID(adparams.pid)) {
1504 player->has_ad_audio = DVR_TRUE;
1505 DVR_PB_DG(1, "start ad audio");
1506 AmTsPlayer_setADParams(player->handle, &adparams);
1507 AmTsPlayer_enableADMix(player->handle);
1508 }
hualing chen31140872020-03-25 12:29:26 +08001509 }
hualing chencc91e1c2020-02-28 13:26:17 +08001510 player->cmd.state = DVR_PLAYBACK_STATE_START;
1511 player->state = DVR_PLAYBACK_STATE_START;
1512 }
hualing chen86e7d482020-01-16 15:13:33 +08001513 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001514 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001515 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001516 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001517 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001518}
hualing chen040df222020-01-17 13:35:02 +08001519/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001520 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001521 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001522 * \retval DVR_SUCCESS On success
1523 * \return Error code
1524 */
hualing chen040df222020-01-17 13:35:02 +08001525int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1526 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001527
hualing chena540a7e2020-03-27 16:44:05 +08001528 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001529 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001530 return DVR_FAILURE;
1531 }
1532
hualing chen4b7c15d2020-04-07 16:13:48 +08001533 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001534 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001535
hualing chen040df222020-01-17 13:35:02 +08001536 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1537 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001538
hualing chen86e7d482020-01-16 15:13:33 +08001539 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001540 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001541 //cp location
hualing chen040df222020-01-17 13:35:02 +08001542 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001543
hualing chen4b7c15d2020-04-07 16:13:48 +08001544 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 +08001545 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001546
1547 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001548 segment->pids.video.pid = info->pids.video.pid;
1549 segment->pids.video.format = info->pids.video.format;
1550 segment->pids.video.type = info->pids.video.type;
1551
hualing chen2aba4022020-03-02 13:49:55 +08001552 segment->pids.audio.pid = info->pids.audio.pid;
1553 segment->pids.audio.format = info->pids.audio.format;
1554 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001555
hualing chen2aba4022020-03-02 13:49:55 +08001556 segment->pids.ad.pid = info->pids.ad.pid;
1557 segment->pids.ad.format = info->pids.ad.format;
1558 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001559
1560 segment->pids.pcr.pid = info->pids.pcr.pid;
1561
hualing chen4b7c15d2020-04-07 16:13:48 +08001562 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 +08001563 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001564 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001565 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001566 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001567
hualing chen5cbe1a62020-02-10 16:36:36 +08001568 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001569}
hualing chen040df222020-01-17 13:35:02 +08001570/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001571 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001572 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001573 * \retval DVR_SUCCESS On success
1574 * \return Error code
1575 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001576int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001577 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001578 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001579 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001580 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001581 return DVR_FAILURE;
1582 }
1583
hualing chencc91e1c2020-02-28 13:26:17 +08001584 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001585 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001586 return DVR_FAILURE;
1587 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001588 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001589 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001590 DVR_PlaybackSegmentInfo_t *segment = NULL;
1591 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1592 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001593 {
hualing chen040df222020-01-17 13:35:02 +08001594 if (segment->segment_id == segment_id) {
1595 list_del(&segment->head);
1596 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001597 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001598 }
hualing chen86e7d482020-01-16 15:13:33 +08001599 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001600 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001601 pthread_mutex_unlock(&player->lock);
1602
1603 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001604}
hualing chen040df222020-01-17 13:35:02 +08001605/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001606 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001607 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001608 * \retval DVR_SUCCESS On success
1609 * \return Error code
1610 */
hualing chen040df222020-01-17 13:35:02 +08001611int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001612 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001613 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001614 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001615 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001616 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001617 return DVR_FAILURE;
1618 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001619 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1620 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1621 return DVR_SUCCESS;
1622 }
hualing chena540a7e2020-03-27 16:44:05 +08001623
hualing chen040df222020-01-17 13:35:02 +08001624 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001625 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001626 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001627 list_for_each_entry(segment, &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) {
hualing chen86e7d482020-01-16 15:13:33 +08001630 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001631 }
hualing chen86e7d482020-01-16 15:13:33 +08001632 // if encramble to free, only set flag and return;
1633
1634 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001635 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001636 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1637 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001638 //disable display, mute
hualing chen2aba4022020-03-02 13:49:55 +08001639 AmTsPlayer_hideVideo(player->handle);
1640 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001641 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1642 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001643 //enable display, unmute
hualing chen2aba4022020-03-02 13:49:55 +08001644 AmTsPlayer_showVideo(player->handle);
1645 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001646 } else {
1647 //do nothing
1648 }
1649 } else {
1650 //do nothing
1651 }
1652 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001653 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001654 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001655 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001656 pthread_mutex_unlock(&player->lock);
1657 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001658}
1659
1660
hualing chen5cbe1a62020-02-10 16:36:36 +08001661static 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 +08001662 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001663 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001664 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001665 return DVR_FAILURE;
1666 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001667 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001668 if (now_pid.pid == set_pid.pid) {
1669 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001670 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001671 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001672 if (VALID_PID(now_pid.pid)) {
1673 //stop now stream
1674 if (type == 0) {
1675 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001676 if (player->has_video == DVR_TRUE) {
1677 DVR_PB_DG(1, "stop video");
1678 AmTsPlayer_stopVideoDecoding(player->handle);
1679 player->has_video = DVR_FALSE;
1680 }
hualing chen86e7d482020-01-16 15:13:33 +08001681 } else if (type == 1) {
1682 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001683 if (player->has_audio == DVR_TRUE) {
1684 DVR_PB_DG(1, "stop audio");
1685 AmTsPlayer_stopAudioDecoding(player->handle);
1686 player->has_audio = DVR_FALSE;
1687 }
hualing chen86e7d482020-01-16 15:13:33 +08001688 } else if (type == 2) {
1689 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001690 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001691 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001692 } else if (type == 3) {
1693 //pcr
1694 }
1695 }
1696 if (VALID_PID(set_pid.pid)) {
1697 //start
1698 if (type == 0) {
1699 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001700 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001701 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001702 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001703 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001704 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001705 AmTsPlayer_setVideoParams(player->handle, &vparams);
1706 AmTsPlayer_startVideoDecoding(player->handle);
1707 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001708 } else if (type == 1) {
1709 //start audio
hualing chenc70a8df2020-05-12 19:23:11 +08001710 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
1711 am_tsplayer_audio_params aparams;
1712 aparams.pid = set_pid.pid;
1713 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1714 player->has_audio = DVR_TRUE;
1715 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1716 AmTsPlayer_setAudioParams(player->handle, &aparams);
1717 AmTsPlayer_startAudioDecoding(player->handle);
1718 //playback_device_audio_start(player->handle,&aparams);
1719 }
hualing chen86e7d482020-01-16 15:13:33 +08001720 } else if (type == 2) {
hualing chen39628212020-05-14 10:35:13 +08001721 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chenc70a8df2020-05-12 19:23:11 +08001722 am_tsplayer_audio_params aparams;
1723 aparams.pid = set_pid.pid;
1724 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1725 player->has_audio = DVR_TRUE;
1726 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1727 AmTsPlayer_setADParams(player->handle, &aparams);
1728 AmTsPlayer_enableADMix(player->handle);
1729 //playback_device_audio_start(player->handle,&aparams);
1730 }
hualing chen86e7d482020-01-16 15:13:33 +08001731 } else if (type == 3) {
1732 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001733 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001734 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001735 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001736 //audio and video all close
1737 if (!player->has_audio && !player->has_video) {
1738 player->state = DVR_PLAYBACK_STATE_STOP;
1739 }
hualing chen86e7d482020-01-16 15:13:33 +08001740 }
1741 }
1742 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001743}
hualing chen5cbe1a62020-02-10 16:36:36 +08001744/**\brief dvr play back update segment pids
1745 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001746 * add pid stream and stop remove pid stream.
1747 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001748 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001749 * \retval DVR_SUCCESS On success
1750 * \return Error code
1751 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001752int 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 +08001753 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001754 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001755 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001756 return DVR_FAILURE;
1757 }
1758
hualing chen040df222020-01-17 13:35:02 +08001759 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001760 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001761 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001762 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 +08001763
hualing chen040df222020-01-17 13:35:02 +08001764 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001765 {
hualing chen040df222020-01-17 13:35:02 +08001766 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001767
1768 if (player->cur_segment_id == segment_id) {
1769 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
1770 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
1771 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08001772 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08001773 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001774 return 0;
1775 }
1776
1777 //if segment is on going segment,we need stop start stream
1778 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08001779 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001780 //check video pids, stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001781 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, p_pids->video, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08001782 //check audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001783 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, p_pids->audio, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001784 //check sub audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001785 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, p_pids->ad, 2);
hualing chen5cbe1a62020-02-10 16:36:36 +08001786 //check pcr pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001787 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, p_pids->pcr, 3);
1788 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001789 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1790 //if state is pause, we need process at resume api. we only record change info
1791 int v_cmd = DVR_PLAYBACK_CMD_NONE;
1792 int a_cmd = DVR_PLAYBACK_CMD_NONE;
1793 if (VALID_PID(segment->pids.video.pid)
1794 && VALID_PID(p_pids->video.pid)
1795 && segment->pids.video.pid != p_pids->video.pid) {
1796 //restart video
1797 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
1798 }
1799 if (!VALID_PID(segment->pids.video.pid)
1800 && VALID_PID(p_pids->video.pid)
1801 && segment->pids.video.pid != p_pids->video.pid) {
1802 //start video
1803 v_cmd = DVR_PLAYBACK_CMD_VSTART;
1804 }
1805 if (VALID_PID(segment->pids.video.pid)
1806 && !VALID_PID(p_pids->video.pid)
1807 && segment->pids.video.pid != p_pids->video.pid) {
1808 //stop video
1809 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
1810 }
1811 if (VALID_PID(segment->pids.audio.pid)
1812 && VALID_PID(p_pids->audio.pid)
1813 && segment->pids.audio.pid != p_pids->audio.pid) {
1814 //restart audio
1815 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
1816 }
1817 if (!VALID_PID(segment->pids.audio.pid)
1818 && VALID_PID(p_pids->audio.pid)
1819 && segment->pids.audio.pid != p_pids->audio.pid) {
1820 //start audio
1821 a_cmd = DVR_PLAYBACK_CMD_ASTART;
1822 }
1823 if (VALID_PID(segment->pids.audio.pid)
1824 && !VALID_PID(p_pids->audio.pid)
1825 && segment->pids.audio.pid != p_pids->audio.pid) {
1826 //stop audio
1827 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
1828 }
1829 if (a_cmd == DVR_PLAYBACK_CMD_NONE
1830 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
1831 //do nothing
1832 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
1833 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
1834 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1835 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
1836 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
1837 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
1838 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1839 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1840 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1841 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
1842 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1843 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1844 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1845 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
1846 } else {
1847 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1848 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
1849 }
1850
1851 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1852 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1853 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1854 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
1855 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1856 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1857 //not occur this case
1858 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1859 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
1860 } else {
1861 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1862 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
1863 }
1864
1865 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1866 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1867 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1868 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
1869 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1870 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
1871 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1872 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
1873 } else {
1874 //not occur this case
1875 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1876 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1877 }
1878 }
1879 }
hualing chene10666f2020-04-14 13:58:37 +08001880 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08001881 }
hualing chen86e7d482020-01-16 15:13:33 +08001882 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08001883 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08001884 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08001885 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001886 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001887 }
hualing chen86e7d482020-01-16 15:13:33 +08001888 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001889 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001890 pthread_mutex_unlock(&player->lock);
1891 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001892}
1893/**\brief Stop play, will stop video and audio
1894 * \param[in] handle playback handle
1895 * \param[in] clear is clear last frame
1896 * \retval DVR_SUCCESS On success
1897 * \return Error code
1898 */
hualing chen040df222020-01-17 13:35:02 +08001899int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
1900 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001901
1902 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001903 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001904 return DVR_FAILURE;
1905 }
hualing chenb96aa2c2020-04-15 14:13:53 +08001906 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1907 DVR_PB_DG(1, ":playback is stoped");
1908 return DVR_SUCCESS;
1909 }
Ke Gong3c0caba2020-04-21 22:58:18 -07001910 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1911 DVR_PB_DG(1, ":playback is stoped");
1912 return DVR_SUCCESS;
1913 }
hualing chen87072a82020-03-12 16:20:12 +08001914 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001915 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001916 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08001917 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08001918 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001919 if (player->has_video) {
1920 AmTsPlayer_resumeVideoDecoding(player->handle);
1921 }
1922 if (player->has_audio) {
1923 AmTsPlayer_resumeAudioDecoding(player->handle);
1924 }
1925 if (player->has_video) {
1926 player->has_video = DVR_FALSE;
hualing chena93bbbc2020-12-22 17:23:42 +08001927 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001928 AmTsPlayer_stopVideoDecoding(player->handle);
1929 }
1930 if (player->has_audio) {
1931 player->has_audio = DVR_FALSE;
1932 AmTsPlayer_stopAudioDecoding(player->handle);
1933 }
hualing chendf118dd2020-05-21 15:49:11 +08001934 if (player->has_ad_audio) {
1935 player->has_ad_audio =DVR_FALSE;
1936 AmTsPlayer_disableADMix(player->handle);
1937 }
hualing chen266b9502020-04-04 17:39:39 +08001938
hualing chen86e7d482020-01-16 15:13:33 +08001939 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001940 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1941 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1942 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08001943 player->cur_segment_id = UINT64_MAX;
1944 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001945 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08001946 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08001947 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001948 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001949}
1950/**\brief Start play audio
1951 * \param[in] handle playback handle
1952 * \param[in] params audio playback params,contains fmt and pid...
1953 * \retval DVR_SUCCESS On success
1954 * \return Error code
1955 */
hualing chen2aba4022020-03-02 13:49:55 +08001956
hualing chendf118dd2020-05-21 15:49:11 +08001957int 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 +08001958 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001959
1960 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001961 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001962 return DVR_FAILURE;
1963 }
hualing chen86e7d482020-01-16 15:13:33 +08001964 _start_playback_thread(handle);
1965 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08001966 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001967 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001968
1969 if (VALID_PID(param->pid)) {
1970 DVR_PB_DG(1, "start audio");
1971 player->has_audio = DVR_TRUE;
1972 AmTsPlayer_setAudioParams(player->handle, param);
1973 AmTsPlayer_startAudioDecoding(player->handle);
1974 }
1975 if (VALID_PID(adparam->pid)) {
1976 player->has_ad_audio = DVR_TRUE;
1977 DVR_PB_DG(1, "start ad audio");
1978 AmTsPlayer_setADParams(player->handle, adparam);
1979 AmTsPlayer_enableADMix(player->handle);
1980 }
1981
hualing chen86e7d482020-01-16 15:13:33 +08001982 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001983 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
1984 player->cmd.state = DVR_PLAYBACK_STATE_START;
1985 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08001986 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001987 pthread_mutex_unlock(&player->lock);
1988 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001989}
1990/**\brief Stop play audio
1991 * \param[in] handle playback handle
1992 * \retval DVR_SUCCESS On success
1993 * \return Error code
1994 */
hualing chen040df222020-01-17 13:35:02 +08001995int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
1996 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001997
1998 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001999 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002000 return DVR_FAILURE;
2001 }
2002
hualing chen2aba4022020-03-02 13:49:55 +08002003 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002004 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002005 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2006 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002007 //destory thread
2008 _stop_playback_thread(handle);
2009 } else {
2010 //do nothing.video is playing
2011 }
hualing chen7a56cba2020-04-14 14:09:27 +08002012 DVR_PB_DG(1, "lock");
2013 pthread_mutex_lock(&player->lock);
2014
hualing chenf00cdc82020-06-10 14:23:35 +08002015 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002016 player->has_audio = DVR_FALSE;
2017 AmTsPlayer_stopAudioDecoding(player->handle);
2018 }
hualing chen87072a82020-03-12 16:20:12 +08002019
hualing chendf118dd2020-05-21 15:49:11 +08002020 if (player->has_ad_audio) {
2021 player->has_ad_audio =DVR_FALSE;
2022 AmTsPlayer_disableADMix(player->handle);
2023 }
2024
hualing chen87072a82020-03-12 16:20:12 +08002025 player->cmd.last_cmd = player->cmd.cur_cmd;
2026 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2027
hualing chen4b7c15d2020-04-07 16:13:48 +08002028 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002029 pthread_mutex_unlock(&player->lock);
2030 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002031}
2032/**\brief Start play video
2033 * \param[in] handle playback handle
2034 * \param[in] params video playback params,contains fmt and pid...
2035 * \retval DVR_SUCCESS On success
2036 * \return Error code
2037 */
hualing chen2aba4022020-03-02 13:49:55 +08002038int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002039 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002040
2041 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002042 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002043 return DVR_FAILURE;
2044 }
2045
hualing chen86e7d482020-01-16 15:13:33 +08002046 _start_playback_thread(handle);
2047 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002048 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002049 pthread_mutex_lock(&player->lock);
2050 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002051 AmTsPlayer_setVideoParams(player->handle, param);
2052 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002053
2054 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002055 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002056 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002057 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002058 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2059 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002060 }
2061 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002062 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2063 player->cmd.state = DVR_PLAYBACK_STATE_START;
2064 player->state = DVR_PLAYBACK_STATE_START;
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);
hualing chenb31a6c62020-01-13 17:27:00 +08002067 return DVR_SUCCESS;
2068}
2069/**\brief Stop play video
2070 * \param[in] handle playback handle
2071 * \retval DVR_SUCCESS On success
2072 * \return Error code
2073 */
hualing chen040df222020-01-17 13:35:02 +08002074int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2075 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002076
2077 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002078 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002079 return DVR_FAILURE;
2080 }
2081
hualing chen86e7d482020-01-16 15:13:33 +08002082 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002083 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2084 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002085 //destory thread
2086 _stop_playback_thread(handle);
2087 } else {
2088 //do nothing.audio is playing
2089 }
hualing chen7a56cba2020-04-14 14:09:27 +08002090
2091 DVR_PB_DG(1, "lock");
2092 pthread_mutex_lock(&player->lock);
2093
hualing chen87072a82020-03-12 16:20:12 +08002094 player->has_video = DVR_FALSE;
2095
2096 AmTsPlayer_stopVideoDecoding(player->handle);
2097 //playback_device_video_stop(player->handle);
2098
2099 player->cmd.last_cmd = player->cmd.cur_cmd;
2100 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2101
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 Pause play
2107 * \param[in] handle playback handle
2108 * \param[in] flush whether its internal buffers should be flushed
2109 * \retval DVR_SUCCESS On success
2110 * \return Error code
2111 */
hualing chen040df222020-01-17 13:35:02 +08002112int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2113 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002114
2115 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002116 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002117 return DVR_FAILURE;
2118 }
hualing chenf00cdc82020-06-10 14:23:35 +08002119 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2120 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002121 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002122 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002123 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002124 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002125 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002126 if (player->has_video)
2127 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002128 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002129 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002130
2131 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002132 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2133 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2134 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2135 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002136 } else {
2137 player->cmd.last_cmd = player->cmd.cur_cmd;
2138 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2139 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2140 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002141 }
hualing chen86e7d482020-01-16 15:13:33 +08002142 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002143 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002144
hualing chen86e7d482020-01-16 15:13:33 +08002145 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002146}
2147
hualing chen5cbe1a62020-02-10 16:36:36 +08002148//not add lock
2149static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2150{
2151 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2152
hualing chena540a7e2020-03-27 16:44:05 +08002153 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002154 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002155 return DVR_FAILURE;
2156 }
2157
hualing chen5cbe1a62020-02-10 16:36:36 +08002158 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002159 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002160 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002161 am_tsplayer_video_params vparams;
2162 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002163 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002164 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002165
hualing chendf118dd2020-05-21 15:49:11 +08002166 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002167 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002168 pthread_mutex_unlock(&player->lock);
2169
2170 switch (cmd) {
2171 case DVR_PLAYBACK_CMD_AVRESTART:
2172 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002173 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002174 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002175 break;
2176 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002177 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2178 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002179 break;
2180 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002181 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002182 break;
2183 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002184 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002185 break;
2186 case DVR_PLAYBACK_CMD_ARESTART:
2187 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002188 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002189 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002190 break;
2191 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002192 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002193 break;
2194 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002195 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002196 break;
2197 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002198 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2199 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2200 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002201 break;
2202 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002203 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2204 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002205 break;
2206 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002207 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2208 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002209 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002210 break;
2211 case DVR_PLAYBACK_CMD_STOP:
2212 break;
2213 case DVR_PLAYBACK_CMD_START:
2214 break;
2215 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002216 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2217 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002218 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002219 break;
2220 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002221 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2222 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002223 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002224 break;
2225 case DVR_PLAYBACK_CMD_FF:
2226 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002227 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002228 break;
2229 default:
2230 break;
2231 }
2232 return DVR_SUCCESS;
2233}
2234
2235/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002236 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002237 * \retval DVR_SUCCESS On success
2238 * \return Error code
2239 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002240int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2241 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2242
hualing chena540a7e2020-03-27 16:44:05 +08002243 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002244 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002245 return DVR_FAILURE;
2246 }
2247
hualing chen5cbe1a62020-02-10 16:36:36 +08002248 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002249 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002250 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002251 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002252 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002253 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2254 AmTsPlayer_resumeVideoDecoding(player->handle);
2255 }
2256 if (player->has_audio) {
2257 AmTsPlayer_resumeAudioDecoding(player->handle);
2258 }
2259 //check is has audio param,if has audio .we need start audio,
2260 //we will stop audio when ff fb, if reach end, we will pause.so we need
2261 //start audio when resume play
2262
2263 am_tsplayer_video_params vparams;
2264 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002265 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002266 uint64_t segmentid = player->cur_segment_id;
hualing chendf118dd2020-05-21 15:49:11 +08002267 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002268 //valid audio pid, start audio
hualing chenc70a8df2020-05-12 19:23:11 +08002269 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 +08002270 player->has_audio = DVR_TRUE;
2271 AmTsPlayer_setAudioParams(player->handle, &aparams);
2272 AmTsPlayer_startAudioDecoding(player->handle);
2273 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002274 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 +08002275 }
hualing chendf118dd2020-05-21 15:49:11 +08002276
2277 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2278 player->has_ad_audio = DVR_TRUE;
2279 DVR_PB_DG(1, "start ad audio");
2280 AmTsPlayer_setADParams(player->handle, &adparams);
2281 AmTsPlayer_enableADMix(player->handle);
2282 }
2283
hualing chen87072a82020-03-12 16:20:12 +08002284 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2285 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2286 player->cmd.state = DVR_PLAYBACK_STATE_START;
2287 player->state = DVR_PLAYBACK_STATE_START;
2288 } else {
2289 player->cmd.last_cmd = player->cmd.cur_cmd;
2290 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2291 player->cmd.state = DVR_PLAYBACK_STATE_START;
2292 player->state = DVR_PLAYBACK_STATE_START;
2293 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002294 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002295 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002296 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chene41f4372020-06-06 16:29:17 +08002297 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002298 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002299 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002300 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002301 }
hualing chen041c4092020-04-05 15:11:50 +08002302 if (player->has_audio)
2303 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002304 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002305 player->cmd.state = DVR_PLAYBACK_STATE_START;
2306 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002307 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2308 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002309 } else {
2310 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2311 {
2312 pthread_mutex_lock(&player->lock);
2313 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002314 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002315 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2316 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2317 pthread_mutex_unlock(&player->lock);
2318 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002319 }
2320 return DVR_SUCCESS;
2321}
2322
hualing chena540a7e2020-03-27 16:44:05 +08002323static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2324
2325 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2326 DVR_PlaybackSegmentInfo_t *segment = NULL;
2327 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2328 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2329
2330 list_for_each_entry(segment, &player->segment_list, head)
2331 {
2332 if (segment->segment_id == segment_id) {
2333 cur_segment = segment;
2334 }
2335 if (segment->segment_id == set_seg_id) {
2336 set_segment = segment;
2337 }
2338 if (cur_segment != NULL && set_segment != NULL) {
2339 break;
2340 }
2341 }
2342 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002343 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002344 return DVR_TRUE;
2345 }
2346 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2347 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2348 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2349 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002350 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 +08002351 return DVR_TRUE;
2352 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002353 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002354 return DVR_FALSE;
2355}
2356
hualing chen5cbe1a62020-02-10 16:36:36 +08002357/**\brief seek
2358 * \param[in] handle playback handle
2359 * \param[in] time_offset time offset base cur segment
2360 * \retval DVR_SUCCESS On success
2361 * \return Error code
2362 */
hualing chencc91e1c2020-02-28 13:26:17 +08002363int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002364 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002365
2366 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002367 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002368 return DVR_FAILURE;
2369 }
2370
hualing chen4b7c15d2020-04-07 16:13:48 +08002371 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 +08002372 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002373
hualing chen86e7d482020-01-16 15:13:33 +08002374 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002375 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002376 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002377
hualing chen5cbe1a62020-02-10 16:36:36 +08002378 //open segment if id is not current segment
hualing chen87072a82020-03-12 16:20:12 +08002379 int ret = _dvr_open_segment(handle, segment_id);
2380 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002381 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002382 pthread_mutex_unlock(&player->lock);
2383 return DVR_FAILURE;
2384 }
2385 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2386 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002387 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002388 //pthread_mutex_unlock(&player->lock);
2389 //return DVR_SUCCESS;
2390 time_offset = _dvr_get_end_time(handle);
2391 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002392 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002393 time_offset = _dvr_get_end_time(handle);
hualing chen87072a82020-03-12 16:20:12 +08002394 pthread_mutex_unlock(&player->lock);
2395 return DVR_FAILURE;
2396 }
2397 }
2398
hualing chen4b7c15d2020-04-07 16:13:48 +08002399 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 +08002400 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002401 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2402 //forward playback.not seek end of file
2403 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2404 //default -2000ms
2405 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2406 }
hualing chen86e7d482020-01-16 15:13:33 +08002407 }
hualing chen2aba4022020-03-02 13:49:55 +08002408 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002409 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002410 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002411 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002412 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 +08002413 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002414 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002415
hualing chen2aba4022020-03-02 13:49:55 +08002416 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002417
2418 player->last_send_time_id = UINT64_MAX;
2419
hualing chen2aba4022020-03-02 13:49:55 +08002420 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002421 player->fffb_current = _dvr_time_getClock();
2422 player->fffb_start = player->fffb_current;
2423 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2424 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002425 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002426 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002427 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002428 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002429 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002430 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002431 }
hualing chen86e7d482020-01-16 15:13:33 +08002432 //stop play
hualing chen4b7c15d2020-04-07 16:13:48 +08002433 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 +08002434 if (player->has_video) {
2435 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002436 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002437 }
2438
2439 if (player->has_audio) {
2440 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002441 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002442 }
hualing chendf118dd2020-05-21 15:49:11 +08002443 if (player->has_ad_audio) {
2444 player->has_ad_audio =DVR_FALSE;
2445 AmTsPlayer_disableADMix(player->handle);
2446 }
2447
hualing chen86e7d482020-01-16 15:13:33 +08002448 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002449 am_tsplayer_video_params vparams;
2450 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002451 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002452
hualing chen040df222020-01-17 13:35:02 +08002453 player->cur_segment_id = segment_id;
2454
2455 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002456 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002457 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002458 //start audio and video
Zhiqiang Han9adc9722020-11-11 18:38:10 +08002459 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08002460 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002461 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 +08002462 pthread_mutex_unlock(&player->lock);
2463 return -1;
2464 }
2465 //add
hualing chen040df222020-01-17 13:35:02 +08002466 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002467 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002468 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002469 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002470 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002471 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002472 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002473 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002474 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002475 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002476 }
hualing chen2aba4022020-03-02 13:49:55 +08002477 AmTsPlayer_setVideoParams(player->handle, &vparams);
2478 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002479 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2480 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2481 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2482 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2483 AmTsPlayer_stopFast(player->handle);
2484 }
hualing chen266b9502020-04-04 17:39:39 +08002485 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002486 }
hualing chene41f4372020-06-06 16:29:17 +08002487 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002488 DVR_PB_DG(1, "start audio seek");
hualing chen2aba4022020-03-02 13:49:55 +08002489 AmTsPlayer_setAudioParams(player->handle, &aparams);
2490 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002491 player->has_audio = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002492 }
hualing chene41f4372020-06-06 16:29:17 +08002493 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002494 player->has_ad_audio = DVR_TRUE;
2495 DVR_PB_DG(1, "start ad audio");
2496 AmTsPlayer_setADParams(player->handle, &adparams);
2497 AmTsPlayer_enableADMix(player->handle);
2498 }
hualing chen86e7d482020-01-16 15:13:33 +08002499 }
hualing chene41f4372020-06-06 16:29:17 +08002500 if (player->state == DVR_PLAYBACK_STATE_PAUSE /*&&
hualing chen2aba4022020-03-02 13:49:55 +08002501 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2502 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2503 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chene41f4372020-06-06 16:29:17 +08002504 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))*/) {
hualing chen2aba4022020-03-02 13:49:55 +08002505 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2506 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002507 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002508 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2509 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002510 player->speed > 1.0f||
2511 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002512 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002513 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002514 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002515 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002516 player->cmd.last_cmd = player->cmd.cur_cmd;
2517 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2518 player->cmd.state = DVR_PLAYBACK_STATE_START;
2519 player->state = DVR_PLAYBACK_STATE_START;
2520 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002521 player->last_send_time_id = UINT64_MAX;
2522 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002523 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002524
2525 return DVR_SUCCESS;
2526}
hualing chen5cbe1a62020-02-10 16:36:36 +08002527
hualing chen5cbe1a62020-02-10 16:36:36 +08002528static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2529 //get cur time of segment
2530 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002531
2532 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002533 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002534 return DVR_FAILURE;
2535 }
2536
hualing chen31140872020-03-25 12:29:26 +08002537 int64_t cache = 0;//defalut es buf cache 500ms
2538 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002539 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002540 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2541 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002542 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002543 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 +08002544 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2545 cache = 0;
2546 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002547 int cur_time = (int)(cur > cache ? cur - cache : 0);
2548 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002549}
2550
2551//get current segment current pcr time of read pos
2552static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2553 //get cur time of segment
2554 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002555
2556 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002557 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002558 return DVR_FAILURE;
2559 }
2560
hualing chen2aba4022020-03-02 13:49:55 +08002561 pthread_mutex_lock(&player->segment_lock);
2562 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002563 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002564 pthread_mutex_unlock(&player->segment_lock);
2565 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002566}
2567
hualing chen4b7c15d2020-04-07 16:13:48 +08002568#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08002569//start replay
2570static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
2571
2572 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2573 //calculate pcr seek time
2574 int t_diff = 0;
2575 int seek_time = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002576
2577 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002578 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002579 return DVR_FAILURE;
2580 }
2581
hualing chen5cbe1a62020-02-10 16:36:36 +08002582 if (player->fffb_start == -1) {
2583 //set fffb start time ms
2584 player->fffb_start = _dvr_time_getClock();
2585 player->fffb_current = player->fffb_start;
2586 //get segment current time pos
2587 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002588 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 +08002589 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08002590 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08002591 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002592 } else {
2593 player->fffb_current = _dvr_time_getClock();
2594 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08002595 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08002596 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08002597 if (seek_time <= 0) {
2598 //need seek to pre one segment
2599 seek_time = 0;
2600 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002601 //seek segment pos
2602 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08002603 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002604 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08002605 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2606 //set seek time to 0;
2607 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]", seek_time, player->cur_segment_id);
2608 seek_time = 0;
2609 }
hualing chen041c4092020-04-05 15:11:50 +08002610 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
2611 seek_time = 0;
2612 }
hualing chen2aba4022020-03-02 13:49:55 +08002613 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002614 } else {
2615 //
hualing chen4b7c15d2020-04-07 16:13:48 +08002616 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08002617 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002618 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 +08002619 }
hualing chen2aba4022020-03-02 13:49:55 +08002620 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08002621}
2622
2623
2624//start replay
2625static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
2626 //
2627 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002628
2629 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002630 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002631 return DVR_FAILURE;
2632 }
2633
hualing chen5cbe1a62020-02-10 16:36:36 +08002634 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002635 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002636 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08002637 AmTsPlayer_stopVideoDecoding(player->handle);
2638 }
2639 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002640 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08002641 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002642 AmTsPlayer_stopAudioDecoding(player->handle);
2643 }
hualing chendf118dd2020-05-21 15:49:11 +08002644 if (player->has_ad_audio) {
2645 DVR_PB_DG(1, "fffb stop audio");
2646 player->has_ad_audio =DVR_FALSE;
2647 AmTsPlayer_disableADMix(player->handle);
2648 }
hualing chen2aba4022020-03-02 13:49:55 +08002649
hualing chen5cbe1a62020-02-10 16:36:36 +08002650 //start video and audio
2651
hualing chen2aba4022020-03-02 13:49:55 +08002652 am_tsplayer_video_params vparams;
2653 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002654 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002655 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002656
2657 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08002658 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002659 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002660 //start audio and video
2661 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2662 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002663 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08002664 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002665 return -1;
2666 }
2667
2668 if (VALID_PID(vparams.pid)) {
2669 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002670 DVR_PB_DG(1, "fffb start video");
hualing chen31140872020-03-25 12:29:26 +08002671 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08002672 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2673 AmTsPlayer_setVideoParams(player->handle, &vparams);
2674 AmTsPlayer_startVideoDecoding(player->handle);
2675 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002676 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08002677 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002678 }
hualing chen31140872020-03-25 12:29:26 +08002679 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08002680 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002681 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08002682 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002683 return 0;
2684}
2685
2686static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
2687 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002688 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002689 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002690 return DVR_FAILURE;
2691 }
2692
2693 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08002694 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002695 pthread_mutex_lock(&player->lock);
2696
hualing chen2aba4022020-03-02 13:49:55 +08002697 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002698 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 +08002699
hualing chen87072a82020-03-12 16:20:12 +08002700 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2701 //seek time set 0
2702 seek_time = 0;
2703 }
hualing chen041c4092020-04-05 15:11:50 +08002704 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08002705 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
2706 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08002707 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002708 pthread_mutex_unlock(&player->lock);
2709 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002710 //send event here and pause
2711 DVR_Play_Notify_t notify;
2712 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08002713 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08002714 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08002715 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08002716 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 +08002717 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08002718 return DVR_SUCCESS;
2719 }
hualing chen2932d372020-04-29 13:44:00 +08002720 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002721 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002722 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002723 }
2724 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002725 _dvr_playback_fffb_replay(handle);
2726
2727 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002728 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002729
hualing chen5cbe1a62020-02-10 16:36:36 +08002730 return DVR_SUCCESS;
2731}
2732
hualing chen87072a82020-03-12 16:20:12 +08002733//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08002734static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002735 //
2736 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002737
2738 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002739 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002740 return DVR_FAILURE;
2741 }
2742
hualing chen5cbe1a62020-02-10 16:36:36 +08002743 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002744 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08002745 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002746 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002747 }
2748
2749 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08002750 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002751 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002752 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002753 //start video and audio
2754
hualing chen2aba4022020-03-02 13:49:55 +08002755 am_tsplayer_video_params vparams;
2756 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002757 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002758 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002759
2760 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08002761 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08002762 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002763 //start audio and video
2764 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08002765 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002766 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08002767 return -1;
2768 }
2769
2770 if (VALID_PID(vparams.pid)) {
2771 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002772 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002773 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08002774 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08002775 }
hualing chen266b9502020-04-04 17:39:39 +08002776 else {
hualing chen2aba4022020-03-02 13:49:55 +08002777 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08002778 }
hualing chen2aba4022020-03-02 13:49:55 +08002779 AmTsPlayer_setVideoParams(player->handle, &vparams);
2780 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08002781 }
hualing chena540a7e2020-03-27 16:44:05 +08002782
2783 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08002784 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08002785 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002786 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08002787 } else {
hualing chena540a7e2020-03-27 16:44:05 +08002788 if (VALID_PID(aparams.pid)) {
2789 player->has_audio = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002790 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08002791 AmTsPlayer_setAudioParams(player->handle, &aparams);
Zhiqiang Hane013e3e2020-12-03 13:34:56 +08002792 AmTsPlayer_startAudioDecoding(player->handle);
hualing chena540a7e2020-03-27 16:44:05 +08002793 }
hualing chendf118dd2020-05-21 15:49:11 +08002794 if (VALID_PID(adparams.pid)) {
2795 player->has_ad_audio = DVR_TRUE;
2796 DVR_PB_DG(1, "start ad audio");
2797 AmTsPlayer_setADParams(player->handle, &adparams);
2798 AmTsPlayer_enableADMix(player->handle);
2799 }
2800
hualing chen7a56cba2020-04-14 14:09:27 +08002801 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002802 AmTsPlayer_stopFast(player->handle);
2803 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
2804 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
2805 }
hualing chen2aba4022020-03-02 13:49:55 +08002806 player->cmd.last_cmd = player->cmd.cur_cmd;
2807 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08002808 player->cmd.state = DVR_PLAYBACK_STATE_START;
2809 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002810 return 0;
2811}
2812
2813
hualing chenb31a6c62020-01-13 17:27:00 +08002814/**\brief Set play speed
2815 * \param[in] handle playback handle
2816 * \param[in] speed playback speed
2817 * \retval DVR_SUCCESS On success
2818 * \return Error code
2819 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002820int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
2821
2822 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002823
2824 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002825 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002826 return DVR_FAILURE;
2827 }
2828
hualing chen4b7c15d2020-04-07 16:13:48 +08002829 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002830 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002831 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002832 return DVR_FAILURE;
2833 }
hualing chenf00cdc82020-06-10 14:23:35 +08002834 if (speed.speed.speed == player->cmd.speed.speed.speed) {
2835 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
2836 return DVR_SUCCESS;
2837 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002838 pthread_mutex_lock(&player->lock);
2839 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
2840 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
2841 player->cmd.last_cmd = player->cmd.cur_cmd;
2842 }
hualing chene41f4372020-06-06 16:29:17 +08002843
hualing chen31140872020-03-25 12:29:26 +08002844 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08002845 IS_KERNEL_SPEED(speed.speed.speed) ) {
2846 //case 1. not start play.only set speed
2847 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2848 //only set speed.and return;
2849 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
2850 player->cmd.speed.speed = speed.speed;
2851 player->speed = (float)speed.speed.speed/(float)100;
2852 player->fffb_play = DVR_FALSE;
2853 pthread_mutex_unlock(&player->lock);
2854 return DVR_SUCCESS;
2855 }
2856 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08002857 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
2858 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002859 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002860 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2861 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002862 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002863 AmTsPlayer_stopFast(player->handle);
2864 pthread_mutex_unlock(&player->lock);
2865 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
2866 pthread_mutex_lock(&player->lock);
2867 } else {
2868 //set play speed and if audio is start, stop audio.
2869 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002870 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002871 AmTsPlayer_stopAudioDecoding(player->handle);
2872 player->has_audio = DVR_FALSE;
2873 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002874 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002875 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002876 }
hualing chenbcada022020-04-22 14:27:01 +08002877 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08002878 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002879 player->cmd.speed.speed = speed.speed;
2880 player->speed = (float)speed.speed.speed/(float)100;
2881 pthread_mutex_unlock(&player->lock);
2882 return DVR_SUCCESS;
2883 }
hualing chen31140872020-03-25 12:29:26 +08002884 //case 3 fffb mode
2885 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2886 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2887 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002888 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002889 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002890 player->cmd.speed.speed = speed.speed;
2891 player->speed = (float)speed.speed.speed/(float)100;
2892 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08002893 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002894 pthread_mutex_unlock(&player->lock);
2895 return DVR_SUCCESS;
2896 }
2897 }
2898 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08002899 IS_KERNEL_SPEED(speed.speed.speed)) {
2900 //case 1. cur speed is kernel support speed,set kernel speed.
2901 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08002902 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002903 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2904 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002905 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002906 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08002907 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08002908 } else {
2909 //set play speed and if audio is start, stop audio.
2910 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002911 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002912 AmTsPlayer_stopAudioDecoding(player->handle);
2913 player->has_audio = DVR_FALSE;
2914 }
hualing chenf00cdc82020-06-10 14:23:35 +08002915 DVR_PB_DG(1, "start fast");
2916 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08002917 }
hualing chena540a7e2020-03-27 16:44:05 +08002918 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002919 player->cmd.speed.speed = speed.speed;
2920 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08002921 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002922 pthread_mutex_unlock(&player->lock);
2923 return DVR_SUCCESS;
2924 }
2925 //case 2 fffb mode
2926 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2927 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2928 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002929 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002930 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002931 player->cmd.speed.speed = speed.speed;
2932 player->speed = (float)speed.speed.speed/(float)100;
2933 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08002934 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002935 pthread_mutex_unlock(&player->lock);
2936 return DVR_SUCCESS;
2937 }
hualing chen31140872020-03-25 12:29:26 +08002938 }
hualing chena540a7e2020-03-27 16:44:05 +08002939 if (IS_KERNEL_SPEED(speed.speed.speed)) {
2940 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08002941 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08002942 } else {
hualing chen31140872020-03-25 12:29:26 +08002943 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08002944 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
2945 else
2946 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08002947 player->fffb_play = DVR_TRUE;
2948 }
2949 DVR_Bool_t init_last_time = DVR_FALSE;
2950 if (player->speed > 0.0f && speed.speed.speed < 0) {
2951 init_last_time = DVR_TRUE;
2952 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
2953 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002954 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002955 player->cmd.speed.mode = speed.mode;
2956 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08002957 player->speed = (float)speed.speed.speed/(float)100;
2958 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08002959 _dvr_init_fffb_t(handle);
2960 if (init_last_time == DVR_TRUE)
2961 player->last_send_time_id = UINT64_MAX;
2962
hualing chen87072a82020-03-12 16:20:12 +08002963 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08002964 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2965 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08002966 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002967 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08002968 _dvr_playback_replay(handle, DVR_FALSE);
2969 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
2970 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
2971 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08002972 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08002973 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002974 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002975 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002976 return DVR_SUCCESS;
2977}
hualing chen2932d372020-04-29 13:44:00 +08002978
hualing chenb31a6c62020-01-13 17:27:00 +08002979/**\brief Get playback status
2980 * \param[in] handle playback handle
2981 * \param[out] p_status playback status
2982 * \retval DVR_SUCCESS On success
2983 * \return Error code
2984 */
hualing chen2932d372020-04-29 13:44:00 +08002985static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
2986 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002987//
2988 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002989
2990 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002991 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002992 return DVR_FAILURE;
2993 }
hualing chen2932d372020-04-29 13:44:00 +08002994 if (is_lock ==DVR_TRUE)
2995 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002996 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08002997 //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 +08002998 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
2999 player->state == DVR_PLAYBACK_STATE_START) {
3000 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3001 }
hualing chen041c4092020-04-05 15:11:50 +08003002
hualing chencc91e1c2020-02-28 13:26:17 +08003003 p_status->time_end = _dvr_get_end_time(handle);
hualing chen2aba4022020-03-02 13:49:55 +08003004 p_status->time_cur = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003005 if (player->last_send_time_id == UINT64_MAX) {
3006 player->last_send_time_id = player->cur_segment_id;
3007 player->last_cur_time = p_status->time_cur;
3008 }
3009 if (player->last_send_time_id == player->cur_segment_id) {
3010 if (player->speed > 0.0f ) {
3011 //ff
3012 if (p_status->time_cur < player->last_cur_time ) {
3013 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);
3014 p_status->time_cur = player->last_cur_time;
3015 } else {
3016 player->last_cur_time = p_status->time_cur;
3017 }
hualing chene41f4372020-06-06 16:29:17 +08003018 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003019 //fb
3020 if (p_status->time_cur > player->last_cur_time ) {
3021 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 );
3022 p_status->time_cur = player->last_cur_time;
3023 } else {
3024 player->last_cur_time = p_status->time_cur;
3025 }
3026 }
3027 } else {
3028 player->last_cur_time = p_status->time_cur;
3029 }
3030 player->last_send_time_id = player->cur_segment_id;
hualing chen041c4092020-04-05 15:11:50 +08003031 p_status->segment_id = player->cur_segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003032
hualing chen5cbe1a62020-02-10 16:36:36 +08003033 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003034 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003035 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003036 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 +08003037 _dvr_playback_state_toString(player->state),
3038 _dvr_playback_state_toString(p_status->state),
hualing chena540a7e2020-03-27 16:44:05 +08003039 p_status->time_cur, p_status->time_end,
3040 p_status->segment_id,player->play_flag,
hualing chen2932d372020-04-29 13:44:00 +08003041 player->speed,
3042 is_lock);
3043 if (is_lock ==DVR_TRUE)
3044 pthread_mutex_unlock(&player->lock);
3045 return DVR_SUCCESS;
3046}
3047
3048
3049/**\brief Get playback status
3050 * \param[in] handle playback handle
3051 * \param[out] p_status playback status
3052 * \retval DVR_SUCCESS On success
3053 * \return Error code
3054 */
3055int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3056 DVR_PlaybackStatus_t *p_status) {
3057//
3058 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3059
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003060 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3061
hualing chen2932d372020-04-29 13:44:00 +08003062 if (player == NULL) {
3063 DVR_PB_DG(1, "player is NULL");
3064 return DVR_FAILURE;
3065 }
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003066 pthread_mutex_lock(&player->lock);
3067 if (!player->has_video && !player->has_audio)
3068 p_status->time_cur = 0;
3069 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003070
hualing chenb31a6c62020-01-13 17:27:00 +08003071 return DVR_SUCCESS;
3072}
3073
hualing chen040df222020-01-17 13:35:02 +08003074void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3075 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003076 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3077 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3078 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3079 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3080 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3081 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3082 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 +08003083 }
hualing chenb31a6c62020-01-13 17:27:00 +08003084}
3085
hualing chen5cbe1a62020-02-10 16:36:36 +08003086int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003087 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003088
hualing chena540a7e2020-03-27 16:44:05 +08003089 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003090 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003091 return DVR_FAILURE;
3092 }
3093
hualing chen040df222020-01-17 13:35:02 +08003094 DVR_PlaybackSegmentInfo_t *segment;
3095 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003096 {
hualing chen040df222020-01-17 13:35:02 +08003097 if (segment_id >= 0) {
3098 if (segment->segment_id == segment_id) {
3099 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003100 break;
3101 }
3102 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003103 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003104 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003105 }
3106 }
3107 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003108}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003109
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003110int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003111{
3112 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3113 DVR_RETURN_IF_FALSE(player);
3114 DVR_RETURN_IF_FALSE(func);
3115
hualing chen4b7c15d2020-04-07 16:13:48 +08003116 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003117 pthread_mutex_lock(&player->lock);
3118
3119 player->dec_func = func;
3120 player->dec_userdata = userdata;
3121
3122 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003123 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003124 return DVR_SUCCESS;
3125}
3126
3127int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3128{
3129 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3130 DVR_RETURN_IF_FALSE(player);
3131 DVR_RETURN_IF_FALSE(p_secure_buf);
3132 DVR_RETURN_IF_FALSE(len);
3133
hualing chen4b7c15d2020-04-07 16:13:48 +08003134 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003135 pthread_mutex_lock(&player->lock);
3136
3137 player->is_secure_mode = 1;
3138 player->secure_buffer = p_secure_buf;
3139 player->secure_buffer_size = len;
3140
3141 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003142 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003143 return DVR_SUCCESS;
3144}