blob: 13b5b0b1cac6250be55e1062d47ce2ab62967997 [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001#include <stdio.h>
2#include <stdlib.h>
3
hualing chen5cbe1a62020-02-10 16:36:36 +08004#include <string.h>
hualing chenb31a6c62020-01-13 17:27:00 +08005#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <poll.h>
11#include <errno.h>
12#include <signal.h>
13#include <pthread.h>
hualing chenb5cd42e2020-04-15 17:03:34 +080014#include <errno.h>
hualing chenb31a6c62020-01-13 17:27:00 +080015
hualing chenb31a6c62020-01-13 17:27:00 +080016#include "dvr_playback.h"
17
hualing chen4b7c15d2020-04-07 16:13:48 +080018#define DVR_PB_DG(_level, _fmt, ...) \
19 DVR_DEBUG(_level, "playback %-30.30s:%d " _fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
20
hualing chena540a7e2020-03-27 16:44:05 +080021
hualing chenb31a6c62020-01-13 17:27:00 +080022#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080023
24
25#define FF_SPEED (2.0f)
26#define FB_SPEED (-1.0f)
27#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080028#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080029
30#define IS_KERNEL_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_X1) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
31#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
32
hualing chenb31a6c62020-01-13 17:27:00 +080033
hualing chenb5cd42e2020-04-15 17:03:34 +080034#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080035#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080036//if tsplayer delay time < 200 and no data can read, we will pause
37#define MIN_TSPLAYER_DELAY_TIME (200)
38
hualing chen041c4092020-04-05 15:11:50 +080039#define MAX_CACHE_TIME (30000)
40
hualing chena540a7e2020-03-27 16:44:05 +080041static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080042//
43static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chencc91e1c2020-02-28 13:26:17 +080044static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type);
45static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
46static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080047static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080048static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080049static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
50 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080051static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen87072a82020-03-12 16:20:12 +080052
hualing chenbcada022020-04-22 14:27:01 +080053
54static char* _cmd_toString(int cmd)
55{
56
57 char *string[DVR_PLAYBACK_CMD_NONE+1]={
58 "start",
59 "stop",
60 "vstart",
61 "astart",
62 "vstop",
63 "astop",
64 "vrestart",
65 "arestart",
66 "avrestart",
67 "vstopastart",
68 "astopvstart",
69 "vstoparestart",
70 "astopvrestart",
71 "vstartarestart",
72 "astartvrestart",
73 "pause",
74 "resume",
75 "seek",
76 "ff",
77 "fb",
78 "NONE"
79 };
80
81 if (cmd > DVR_PLAYBACK_CMD_NONE) {
82 return "unkown";
83 } else {
84 return string[cmd];
85 }
86}
87
88
hualing chen6d24aa92020-03-23 18:43:47 +080089static char* _dvr_playback_state_toString(int stat)
90{
91 char *string[DVR_PLAYBACK_STATE_FB+1]={
92 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080093 "stop",
hualing chen31140872020-03-25 12:29:26 +080094 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +080095 "ff",
96 "fb"
97 };
98
99 if (stat > DVR_PLAYBACK_STATE_FB) {
100 return "unkown";
101 } else {
102 return string[stat];
103 }
104}
hualing chena540a7e2020-03-27 16:44:05 +0800105
106static DVR_Bool_t _dvr_support_speed(int speed) {
107
108 DVR_Bool_t ret = DVR_FALSE;
109
110 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800111 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800112 case PLAYBACK_SPEED_FBX2:
113 case PLAYBACK_SPEED_FBX4:
114 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800115 case PLAYBACK_SPEED_FBX16:
116 case PLAYBACK_SPEED_FBX12:
117 case PLAYBACK_SPEED_FBX32:
118 case PLAYBACK_SPEED_FBX48:
119 case PLAYBACK_SPEED_FBX64:
120 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800121 case PLAYBACK_SPEED_S2:
122 case PLAYBACK_SPEED_S4:
123 case PLAYBACK_SPEED_S8:
124 case PLAYBACK_SPEED_X1:
125 case PLAYBACK_SPEED_X2:
126 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800127 case PLAYBACK_SPEED_X3:
128 case PLAYBACK_SPEED_X5:
129 case PLAYBACK_SPEED_X6:
130 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800131 case PLAYBACK_SPEED_X8:
132 case PLAYBACK_SPEED_X12:
133 case PLAYBACK_SPEED_X16:
134 case PLAYBACK_SPEED_X32:
135 case PLAYBACK_SPEED_X48:
136 case PLAYBACK_SPEED_X64:
137 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800138 ret = DVR_TRUE;
139 break;
140 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800141 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800142 break;
143 }
144 return ret;
145}
hualing chen6e4bfa52020-03-13 14:37:11 +0800146void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
147{
hualing chen4b7c15d2020-04-07 16:13:48 +0800148 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800149 DVR_Playback_t *player = NULL;
150 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800151 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800152 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800153 }
154 switch (event->type) {
155 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
156 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800157 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800158 event->event.video_format.frame_width,
159 event->event.video_format.frame_height,
160 event->event.video_format.frame_rate);
161 break;
162 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800163 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
164 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800165 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800166 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800167 break;
168 }
169 default:
170 break;
171 }
172}
hualing chen2aba4022020-03-02 13:49:55 +0800173void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
174{
hualing chen6e4bfa52020-03-13 14:37:11 +0800175 DVR_Playback_t *player = NULL;
176 if (user_data != NULL) {
177 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800178 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800179 }
hualing chen2aba4022020-03-02 13:49:55 +0800180 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800181 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
182 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800183 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800184 event->event.video_format.frame_width,
185 event->event.video_format.frame_height,
186 event->event.video_format.frame_rate);
187 break;
188 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800189 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
190 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800191 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800192 if (player->first_trans_ok == DVR_FALSE) {
193 player->first_trans_ok = DVR_TRUE;
194 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
195 }
hualing chena540a7e2020-03-27 16:44:05 +0800196 if (player != NULL)
197 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800198 break;
199 }
hualing chen487ae6d2020-07-22 10:34:11 +0800200 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
201 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
202 player->first_trans_ok = DVR_TRUE;
203 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
204 }
205 if (player != NULL && player->has_video == DVR_FALSE) {
206 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
207 player->first_frame = 1;
208 }
209 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800210 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800211 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800212 break;
213 }
214 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800215 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800216 player->player_callback_func(player->player_callback_userdata, event);
217 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800218 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800219 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800220 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800221 }
222}
hualing chencc91e1c2020-02-28 13:26:17 +0800223
hualing chen5cbe1a62020-02-10 16:36:36 +0800224//convert video and audio fmt
225static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
226 int format = 0;
227 if (is_audio == DVR_FALSE) {
228 //for video fmt
229 switch (fmt)
230 {
231 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800232 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800233 break;
234 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800235 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800236 break;
237 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800238 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800239 break;
240 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800241 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800242 break;
hualing chena540a7e2020-03-27 16:44:05 +0800243 case DVR_VIDEO_FORMAT_VP9:
244 format = AV_VIDEO_CODEC_VP9;
245 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800246 }
247 } else {
248 //for audio fmt
249 switch (fmt)
250 {
251 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800252 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800253 break;
254 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800255 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800256 break;
257 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800258 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800259 break;
260 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800261 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800262 break;
hualing chena540a7e2020-03-27 16:44:05 +0800263 case DVR_AUDIO_FORMAT_AAC:
264 format = AV_AUDIO_CODEC_AAC;
265 break;
266 case DVR_AUDIO_FORMAT_LATM:
267 format = AV_AUDIO_CODEC_LATM;
268 break;
269 case DVR_AUDIO_FORMAT_PCM:
270 format = AV_AUDIO_CODEC_PCM;
271 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800272 }
273 }
274 return format;
275}
hualing chen040df222020-01-17 13:35:02 +0800276static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800277{
hualing chen040df222020-01-17 13:35:02 +0800278 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800279
hualing chena540a7e2020-03-27 16:44:05 +0800280 if (player == NULL || player->handle == NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800281 return -1;
282
hualing chena540a7e2020-03-27 16:44:05 +0800283 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800284}
hualing chena540a7e2020-03-27 16:44:05 +0800285
hualing chen5cbe1a62020-02-10 16:36:36 +0800286//get sys time ms
287static int _dvr_time_getClock(void)
288{
289 struct timespec ts;
290 int ms;
291
292 clock_gettime(CLOCK_MONOTONIC, &ts);
293 ms = ts.tv_sec*1000+ts.tv_nsec/1000000;
294
295 return ms;
296}
hualing chen86e7d482020-01-16 15:13:33 +0800297
hualing chenb31a6c62020-01-13 17:27:00 +0800298
299//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800300static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800301{
hualing chen040df222020-01-17 13:35:02 +0800302 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800303
hualing chena540a7e2020-03-27 16:44:05 +0800304
305 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800306 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800307 return DVR_FAILURE;
308 }
309
hualing chen86e7d482020-01-16 15:13:33 +0800310 struct timespec ts;
311 clock_gettime(CLOCK_MONOTONIC, &ts);
312 //ms为毫秒,换算成秒
313 ts.tv_sec += ms/1000;
314 //在outtime的基础上,增加ms毫秒
315 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
316 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
317 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
318 //us的值有可能超过1秒,
319 ts.tv_sec += us / 1000000;
320 us = us % 1000000;
321 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800322 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
323 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800324}
hualing chen31140872020-03-25 12:29:26 +0800325//get tsplay delay time ms
326static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
327 DVR_Playback_t *player = (DVR_Playback_t *) handle;
328 int64_t cache = 0;
329 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800330 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800331 return 0;
332 }
333 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800334 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800335 return cache;
336}
hualing chenb31a6c62020-01-13 17:27:00 +0800337//send signal
hualing chen040df222020-01-17 13:35:02 +0800338static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800339{
hualing chen87072a82020-03-12 16:20:12 +0800340 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800341
342 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800343 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800344 return DVR_FAILURE;
345 }
346
hualing chen87072a82020-03-12 16:20:12 +0800347 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800348 pthread_cond_signal(&player->cond);
hualing chen87072a82020-03-12 16:20:12 +0800349 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800350 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800351}
352
hualing chen2932d372020-04-29 13:44:00 +0800353//send playback event, need check is need lock first
354static int _dvr_playback_sent_event(DVR_PlaybackHandle_t handle, DVR_PlaybackEvent_t evt, DVR_Play_Notify_t *notify, DVR_Bool_t is_lock) {
hualing chencc91e1c2020-02-28 13:26:17 +0800355
356 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800357
358 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800359 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800360 return DVR_FAILURE;
361 }
362
hualing chencc91e1c2020-02-28 13:26:17 +0800363 switch (evt) {
364 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800365 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800366 break;
367 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
368 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800369 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800370 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800371 break;
372 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
373 break;
374 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
375 break;
376 case DVR_PLAYBACK_EVENT_NO_KEY:
377 break;
378 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800379 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800380 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800381 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800382 break;
383 case DVR_PLAYBACK_EVENT_REACHED_END:
384 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800385 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800386 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800387 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800388 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800389 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800390 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800391 default:
392 break;
393 }
394 if (player->openParams.event_fn != NULL)
395 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800396 return DVR_SUCCESS;
397}
hualing chen2932d372020-04-29 13:44:00 +0800398static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800399{
400 DVR_Play_Notify_t notify;
401 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
402 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
403 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800404 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800405 return DVR_SUCCESS;
406}
407
hualing chen2932d372020-04-29 13:44:00 +0800408static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800409{
410 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800411
hualing chene3797f02021-01-13 14:53:28 +0800412 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800413 return DVR_SUCCESS;
414 }
hualing chena540a7e2020-03-27 16:44:05 +0800415 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800416 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800417 return DVR_FAILURE;
418 }
419
hualing chen6e4bfa52020-03-13 14:37:11 +0800420 if (player->send_time ==0) {
hualing chen0888c032020-12-18 17:54:57 +0800421 player->send_time = _dvr_time_getClock() + 500;
422 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800423 return DVR_SUCCESS;
424 }
hualing chen0888c032020-12-18 17:54:57 +0800425 player->send_time = _dvr_time_getClock() + 500;
hualing chen6e4bfa52020-03-13 14:37:11 +0800426 DVR_Play_Notify_t notify;
427 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
428 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
429 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800430 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800431 return DVR_SUCCESS;
432}
433
hualing chencc91e1c2020-02-28 13:26:17 +0800434//check is ongoing segment
435static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
436
437 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800438 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800439
440 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800441 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800442 return DVR_FAILURE;
443 }
hualing chen87072a82020-03-12 16:20:12 +0800444 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800445 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800446 return DVR_FALSE;
447 }
hualing chencc91e1c2020-02-28 13:26:17 +0800448 return DVR_TRUE;
449}
hualing chen4b7c15d2020-04-07 16:13:48 +0800450
451
452static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
453 DVR_Playback_t *player = (DVR_Playback_t *) handle;
454 player->fffb_start = _dvr_time_getClock();
455 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
456 player->fffb_current = player->fffb_start;
457 //get segment current time pos
458 player->fffb_start_pcr = _dvr_get_cur_time(handle);
459 //player->fffb_current = -1;
460 //player->fffb_start = -1;
461 //player->fffb_start_pcr = -1;
462 player->next_fffb_time = _dvr_time_getClock();
463
464 return DVR_SUCCESS;
465}
466
hualing chen2aba4022020-03-02 13:49:55 +0800467static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
468 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800469 player->fffb_start = _dvr_time_getClock();
470 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
471 player->fffb_current = player->fffb_start;
472 //get segment current time pos
473 player->fffb_start_pcr = _dvr_get_cur_time(handle);
474 //player->fffb_current = -1;
475 //player->fffb_start = -1;
476 //player->fffb_start_pcr = -1;
hualing chen2aba4022020-03-02 13:49:55 +0800477 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800478 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800479 return DVR_SUCCESS;
480}
hualing chencc91e1c2020-02-28 13:26:17 +0800481//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800482static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
483
484 DVR_Playback_t *player = (DVR_Playback_t *) handle;
485 DVR_PlaybackSegmentInfo_t *segment;
486 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
487
hualing chena540a7e2020-03-27 16:44:05 +0800488 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800489 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800490 return DVR_FAILURE;
491 }
492
hualing chen87072a82020-03-12 16:20:12 +0800493 int found = 0;
494 int found_eq_id = 0;
495 list_for_each_entry(segment, &player->segment_list, head)
496 {
497 if (player->segment_is_open == DVR_FALSE) {
498 //get first segment from list, case segment is not open
499 if (!IS_FB(player->speed))
500 found = 1;
501 } else if (segment->segment_id == segmentid) {
502 //find cur segment, we need get next one
503 found_eq_id = 1;
504 if (!IS_FB(player->speed)) {
505 found = 1;
506 continue;
507 } else {
508 //if is fb mode.we need used pre segment
509 if (pre_segment != NULL) {
510 found = 1;
511 } else {
512 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800513 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800514 return DVR_FAILURE;
515 }
516 }
517 }
518 if (found == 1) {
519 found = 2;
520 break;
521 }
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
815 _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids.audio, 1);
816 //check sub audio pids stop or restart
817 _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids.ad, 2);
hualing chene3797f02021-01-13 14:53:28 +0800818 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 +0800819 //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 chene3797f02021-01-13 14:53:28 +0800852/*
853if decodec sucess first time.
854sucess: return true
855fail: return false
856*/
hualing chena540a7e2020-03-27 16:44:05 +0800857static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
858 DVR_Playback_t *player = (DVR_Playback_t *) handle;
859 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800860 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800861 return DVR_TRUE;
862 }
hualing chene3797f02021-01-13 14:53:28 +0800863 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800864 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800865 } else {
866 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800867 }
868}
hualing chen86e7d482020-01-16 15:13:33 +0800869static void* _dvr_playback_thread(void *arg)
870{
hualing chen040df222020-01-17 13:35:02 +0800871 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800872 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800873 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800874 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800875 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800876
hualing chen39628212020-05-14 10:35:13 +0800877 #define MAX_REACHEND_TIMEOUT (3000)
878 int reach_end_timeout = 0;//ms
879 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800880 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800881 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800882 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800883 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800884 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
885
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800886 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800887 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800888 DVR_Bool_t goto_rewrite = DVR_FALSE;
hualing chene41f4372020-06-06 16:29:17 +0800889 int retry_open_seg = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800890 if (player->is_secure_mode) {
891 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800892 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800893 return NULL;
894 }
895 }
hualing chen86e7d482020-01-16 15:13:33 +0800896 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800897 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800898 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800899 return NULL;
900 }
hualing chen2aba4022020-03-02 13:49:55 +0800901 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
902 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800903
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800904 dec_bufs.buf_data = malloc(dec_buf_size);
905 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800906 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800907 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800908 return NULL;
909 }
910 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
911 dec_bufs.buf_size = dec_buf_size;
912
hualing chencc91e1c2020-02-28 13:26:17 +0800913 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800914 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
915 }
hualing chen86e7d482020-01-16 15:13:33 +0800916
hualing chen86e7d482020-01-16 15:13:33 +0800917 if (ret != DVR_SUCCESS) {
918 if (buf != NULL) {
919 free(buf);
920 buf = NULL;
921 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800922 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +0800923 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +0800924 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800925 }
hualing chen4fe3bee2020-10-23 13:58:52 +0800926 DVR_PB_DG(1, "player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
927 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +0800928 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
929 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
930 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
931 player->first_trans_ok = DVR_TRUE;
932 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
933 }
hualing chencc91e1c2020-02-28 13:26:17 +0800934 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +0800935 //set video show
936 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +0800937
hualing chen86e7d482020-01-16 15:13:33 +0800938 int trick_stat = 0;
939 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +0800940
hualing chen86e7d482020-01-16 15:13:33 +0800941 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +0800942 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800943
hualing chen2aba4022020-03-02 13:49:55 +0800944 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
945 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +0800946 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +0800947 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +0800948 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +0800949 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +0800950 {
hualing chen2aba4022020-03-02 13:49:55 +0800951 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
952 if (trick_stat > 0) {
hualing chenbcada022020-04-22 14:27:01 +0800953 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 +0800954 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 +0800955 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +0800956 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +0800957 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +0800958 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
959 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +0800960 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
961 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chenbcada022020-04-22 14:27:01 +0800962 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 +0800963 //need change to pause state
964 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
965 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +0800966 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +0800967 //clear flag
hualing chen31140872020-03-25 12:29:26 +0800968 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +0800969 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800970 AmTsPlayer_pauseVideoDecoding(player->handle);
971 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +0800972 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800973 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +0800974 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800975 }
976 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
977 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +0800978 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +0800979 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +0800980 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
981 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 +0800982 //used timeout wait need lock first,so we unlock and lock
983 //pthread_mutex_unlock(&player->lock);
984 //pthread_mutex_lock(&player->lock);
985 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
986 pthread_mutex_unlock(&player->lock);
987 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +0800988 } else if (_dvr_time_getClock() < player->next_fffb_time) {
989 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);
990 //used timeout wait need lock first,so we unlock and lock
991 //pthread_mutex_unlock(&player->lock);
992 //pthread_mutex_lock(&player->lock);
993 AmTsPlayer_pauseVideoDecoding(player->handle);
994 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
995 pthread_mutex_unlock(&player->lock);
996 continue;
997
hualing chen2aba4022020-03-02 13:49:55 +0800998 }
hualing chen2932d372020-04-29 13:44:00 +0800999 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 +08001000 pthread_mutex_unlock(&player->lock);
1001 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001002 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001003 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1004 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001005 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001006 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001007 pthread_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001008 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001009 }else if (player->fffb_play == DVR_TRUE){
1010 //for first into fffb when reset speed
1011 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1012 _dvr_time_getClock() < player->next_fffb_time) {
1013 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);
1014 //used timeout wait need lock first,so we unlock and lock
1015 //pthread_mutex_unlock(&player->lock);
1016 //pthread_mutex_lock(&player->lock);
1017 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1018 pthread_mutex_unlock(&player->lock);
1019 continue;
1020 }
hualing chen2932d372020-04-29 13:44:00 +08001021 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 +08001022 pthread_mutex_unlock(&player->lock);
1023 goto_rewrite = DVR_FALSE;
1024 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001025 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001026 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1027 player->first_frame = 0;
1028 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1029 pthread_mutex_lock(&player->lock);
1030 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001031 }
hualing chenb31a6c62020-01-13 17:27:00 +08001032 }
hualing chen86e7d482020-01-16 15:13:33 +08001033
hualing chen87072a82020-03-12 16:20:12 +08001034 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001035 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001036 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001037 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001038 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1039 pthread_mutex_unlock(&player->lock);
1040 continue;
1041 }
hualing chen266b9502020-04-04 17:39:39 +08001042 //when seek action is done. we need drop write timeout data.
1043 if (player->drop_ts == DVR_TRUE) {
1044 goto_rewrite = DVR_FALSE;
1045 real_read = 0;
1046 player->drop_ts = DVR_FALSE;
1047 }
hualing chen2aba4022020-03-02 13:49:55 +08001048 if (goto_rewrite == DVR_TRUE) {
1049 goto_rewrite = DVR_FALSE;
1050 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001051 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001052 goto rewrite;
1053 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001054 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001055 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001056 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001057 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001058 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chenfbf8e022020-06-15 13:43:11 +08001059 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001060 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001061 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001062 if (read < 0 && errno == EIO) {
1063 //EIO ERROR, EXIT THRAD
1064 DVR_PB_DG(1, "read error.EIO error, exit thread");
1065 DVR_Play_Notify_t notify;
1066 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1067 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001068 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001069 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001070 goto end;
1071 } else if (read < 0) {
1072 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1073 }
hualing chen87072a82020-03-12 16:20:12 +08001074 //if on fb mode and read file end , we need calculate pos to retry read.
1075 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001076 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 +08001077 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1078 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001079 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1080 pthread_mutex_unlock(&player->lock);
1081 continue;
1082 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001083 //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 +08001084 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001085 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001086 #define MIN_CACHE_TIME (3000)
1087 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001088 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001089 if (_cache_time > MIN_CACHE_TIME) {
hualing chene41f4372020-06-06 16:29:17 +08001090 pthread_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001091 /*if cache time > 20s , we think get time is error,*/
1092 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
1093 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1094 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1095 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1096 }
1097 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, ((_cache_time - MIN_CACHE_TIME) > MIN_CACHE_TIME ? MIN_CACHE_TIME : (_cache_time - MIN_CACHE_TIME)));
hualing chene41f4372020-06-06 16:29:17 +08001098 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001099 DVR_PB_DG(1, "read end but cache time is %d > %d, to sleep end and continue", _cache_time, MIN_CACHE_TIME);
hualing chene41f4372020-06-06 16:29:17 +08001100 //continue;
1101 }
hualing chen040df222020-01-17 13:35:02 +08001102 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001103 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001104 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001105
1106 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chene3797f02021-01-13 14:53:28 +08001107 if (ret != DVR_SUCCESS) {
1108 player->noData++;
1109 DVR_PB_DG(1, "playback is sleep:[%d]ms nodata[%d]", timeout, player->noData);
1110 if (player->noData == 4) {
1111 DVR_PB_DG(1, "playback send nodata event nodata[%d]", player->noData);
1112 //send event here and pause
1113 DVR_Play_Notify_t notify;
1114 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1115 notify.event = DVR_PLAYBACK_EVENT_NODATA;
1116 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_NODATA");
1117 //get play statue not here
1118 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1119 }
1120 }
1121 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001122 if ((ret != DVR_SUCCESS &&
hualing chen041c4092020-04-05 15:11:50 +08001123 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001124 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001125 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1126 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001127 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001128 DVR_Play_Notify_t notify;
1129 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1130 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1131 //get play statue not here
1132 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001133 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001134 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001135 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 +08001136 pthread_mutex_lock(&player->lock);
1137 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1138 pthread_mutex_unlock(&player->lock);
1139 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001140 } else if (ret != DVR_SUCCESS) {
1141 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001142 if (delay < cache_time) {
1143 //delay time is changed and then has data to play, so not start timeout
1144 } else {
1145 reach_end_timeout = reach_end_timeout + timeout;
1146 }
1147 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001148 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001149 pthread_mutex_lock(&player->lock);
1150 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1151 pthread_mutex_unlock(&player->lock);
1152 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001153 }
hualing chen39628212020-05-14 10:35:13 +08001154 reach_end_timeout = 0;
1155 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001156 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001157 //change next segment success case
1158 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001159 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001160 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1161 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001162 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001163 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001164 }//read len 0 check end
1165 if (player->noData > 0) {
1166 player->noData = 0;
1167 DVR_PB_DG(1, "playback send data event resume[%d]", player->noData);
1168 //send event here and pause
1169 DVR_Play_Notify_t notify;
1170 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1171 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
1172 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_DATARESUME");
1173 //get play statue not here
1174 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001175 }
hualing chen39628212020-05-14 10:35:13 +08001176 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001177 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001178 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001179 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001180
hualing chena540a7e2020-03-27 16:44:05 +08001181 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001182 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001183 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001184 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001185 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001186 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001187 }
hualing chen266b9502020-04-04 17:39:39 +08001188 //if need write whole block size, we need check read buf len is eq block size.
1189 if (b_writed_whole_block == DVR_TRUE) {
1190 //buf_len is block size value.
1191 if (real_read < buf_len) {
1192 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001193 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001194 pthread_mutex_lock(&player->lock);
1195 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1196 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001197 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001198 continue;
1199 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001200 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 +08001201 }
1202 }
1203
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001204 if (player->dec_func) {
1205 DVR_CryptoParams_t crypto_params;
1206
1207 memset(&crypto_params, 0, sizeof(crypto_params));
1208 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1209 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1210 crypto_params.segment_id = player->cur_segment.segment_id;
hualing chen1f26ffa2020-11-03 10:39:20 +08001211 crypto_params.offset = segment_tell_position(player->r_handle) - wbufs.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001212 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1213 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001214 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1215 crypto_params.input_buffer.addr = (size_t)buf;
1216 crypto_params.input_buffer.size = real_read;
1217
1218 if (player->is_secure_mode) {
1219 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1220 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1221 crypto_params.output_buffer.size = dec_buf_size;
1222 ret = player->dec_func(&crypto_params, player->dec_userdata);
1223 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001224 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001225 } else {
1226 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1227 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1228 crypto_params.output_buffer.size = dec_buf_size;
1229 ret = player->dec_func(&crypto_params, player->dec_userdata);
1230 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001231 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001232 }
1233 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001234 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001235 }
pengfei.liufda2a972020-04-09 14:47:15 +08001236 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001237 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001238rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001239 if (player->drop_ts == DVR_TRUE) {
1240 //need drop ts data when seek occur.we need read next loop,drop this ts data
1241 goto_rewrite = DVR_FALSE;
1242 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001243 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001244 player->drop_ts = DVR_FALSE;
1245 continue;
1246 }
hualing chen5605eed2020-05-26 18:18:06 +08001247 player->ts_cache_len = real_read;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001248 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1249 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001250 player->ts_cache_len = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001251 real_read = 0;
1252 write_success++;
hualing chen5605eed2020-05-26 18:18:06 +08001253 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001254 continue;
hualing chen87072a82020-03-12 16:20:12 +08001255 } else {
hualing chen2932d372020-04-29 13:44:00 +08001256 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 +08001257 write_success = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001258 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001259 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001260 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001261 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001262 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001263 break;
1264 }
hualing chen2aba4022020-03-02 13:49:55 +08001265 goto_rewrite = DVR_TRUE;
1266 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001267 }
1268 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001269end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001270 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001271 free(buf);
1272 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001273 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001274}
1275
1276
hualing chen040df222020-01-17 13:35:02 +08001277static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001278{
hualing chen040df222020-01-17 13:35:02 +08001279 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001280
1281 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001282 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001283 return DVR_FAILURE;
1284 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001285 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001286 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001287 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001288 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001289 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001290 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001291 if (rc < 0)
1292 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001293 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001294}
1295
1296
hualing chen040df222020-01-17 13:35:02 +08001297static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001298{
hualing chen040df222020-01-17 13:35:02 +08001299 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001300
1301 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001302 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001303 return DVR_FAILURE;
1304 }
1305
hualing chen4b7c15d2020-04-07 16:13:48 +08001306 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001307 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001308 {
1309 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001310 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001311 pthread_join(player->playback_thread, NULL);
1312 }
1313 if (player->r_handle) {
1314 segment_close(player->r_handle);
1315 player->r_handle = NULL;
1316 }
hualing chen7a56cba2020-04-14 14:09:27 +08001317 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001318 return 0;
1319}
1320
hualing chenb31a6c62020-01-13 17:27:00 +08001321/**\brief Open an dvr palyback
1322 * \param[out] p_handle dvr playback addr
1323 * \param[in] params dvr playback open parameters
1324 * \retval DVR_SUCCESS On success
1325 * \return Error code
1326 */
hualing chen040df222020-01-17 13:35:02 +08001327int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001328
hualing chen040df222020-01-17 13:35:02 +08001329 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001330 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001331
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001332 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001333
hualing chen86e7d482020-01-16 15:13:33 +08001334 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001335 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001336 pthread_condattr_init(&cattr);
1337 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1338 pthread_cond_init(&player->cond, &cattr);
1339 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001340
hualing chen5cbe1a62020-02-10 16:36:36 +08001341 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001342 INIT_LIST_HEAD(&player->segment_list);
1343 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1344 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001345 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001346 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001347 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001348 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001349 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001350 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001351
hualing chen86e7d482020-01-16 15:13:33 +08001352 //store open params
hualing chen040df222020-01-17 13:35:02 +08001353 player->openParams.dmx_dev_id = params->dmx_dev_id;
1354 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001355 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001356 player->openParams.event_fn = params->event_fn;
1357 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001358 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001359 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001360
hualing chen5cbe1a62020-02-10 16:36:36 +08001361 player->has_pids = params->has_pids;
1362
hualing chen2aba4022020-03-02 13:49:55 +08001363 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001364
1365 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1366 //for test get callback
1367 if (0 && player->player_callback_func == NULL) {
1368 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1369 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +08001370 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 +08001371 }
1372 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001373
hualing chen86e7d482020-01-16 15:13:33 +08001374 //init has audio and video
1375 player->has_video = DVR_FALSE;
1376 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001377 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001378 player->last_segment_id = 0LL;
1379 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001380
hualing chen5cbe1a62020-02-10 16:36:36 +08001381 //init ff fb time
1382 player->fffb_current = -1;
1383 player->fffb_start =-1;
1384 player->fffb_start_pcr = -1;
1385 //seek time
1386 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001387 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001388
1389 //init secure stuff
1390 player->dec_func = NULL;
1391 player->dec_userdata = NULL;
1392 player->is_secure_mode = 0;
1393 player->secure_buffer = NULL;
1394 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001395 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001396
hualing chen4b7c15d2020-04-07 16:13:48 +08001397 player->fffb_play = DVR_FALSE;
1398
1399 player->last_send_time_id = UINT64_MAX;
1400 player->last_cur_time = 0;
1401
hualing chen86e7d482020-01-16 15:13:33 +08001402 *p_handle = player;
1403 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001404}
1405
1406/**\brief Close an dvr palyback
1407 * \param[in] handle playback handle
1408 * \retval DVR_SUCCESS On success
1409 * \return Error code
1410 */
hualing chen040df222020-01-17 13:35:02 +08001411int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001412
hualing chen86e7d482020-01-16 15:13:33 +08001413 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001414 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001415 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001416 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001417 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001418 return DVR_FAILURE;
1419 }
1420
hualing chencc91e1c2020-02-28 13:26:17 +08001421 if (player->state != DVR_PLAYBACK_STATE_STOP)
1422 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001423 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001424 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001425 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1426 } else {
1427 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001428 }
hualing chen7a56cba2020-04-14 14:09:27 +08001429 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001430 pthread_mutex_destroy(&player->lock);
1431 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001432
1433 if (player) {
1434 free(player);
1435 player = NULL;
1436 }
hualing chen7a56cba2020-04-14 14:09:27 +08001437 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001438 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001439}
1440
hualing chenb31a6c62020-01-13 17:27:00 +08001441/**\brief Start play audio and video, used start auido api and start video api
1442 * \param[in] handle playback handle
1443 * \param[in] params audio playback params,contains fmt and pid...
1444 * \retval DVR_SUCCESS On success
1445 * \return Error code
1446 */
hualing chen040df222020-01-17 13:35:02 +08001447int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1448 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001449 am_tsplayer_video_params vparams;
1450 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001451 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001452
1453 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001454 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001455 return DVR_FAILURE;
1456 }
hualing chencc91e1c2020-02-28 13:26:17 +08001457 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001458 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001459
hualing chena540a7e2020-03-27 16:44:05 +08001460 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001461 //can used start api to resume playback
1462 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1463 return dvr_playback_resume(handle);
1464 }
hualing chen87072a82020-03-12 16:20:12 +08001465 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001466 //if flag is puased and not decodec first frame. if user resume, we need
1467 //clear flag and set trickmode none
1468 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1469 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1470 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1471 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1472 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001473 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001474 return DVR_SUCCESS;
1475 }
hualing chen86e7d482020-01-16 15:13:33 +08001476 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001477 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001478 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001479 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001480 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001481 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001482 //start audio and video
Zhiqiang Hana9d261b2020-11-11 18:38:10 +08001483 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001484 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001485 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001486 pthread_mutex_unlock(&player->lock);
1487 DVR_Play_Notify_t notify;
1488 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1489 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1490 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1491 notify.info.transition_failed_data.segment_id = segment_id;
1492 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001493 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001494 return -1;
1495 }
hualing chen31140872020-03-25 12:29:26 +08001496
hualing chencc91e1c2020-02-28 13:26:17 +08001497 {
hualing chen86e7d482020-01-16 15:13:33 +08001498 if (VALID_PID(vparams.pid)) {
1499 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001500 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001501 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001502 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001503 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1504 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001505 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001506 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001507 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001508 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001509 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001510 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001511 }
hualing chena93bbbc2020-12-22 17:23:42 +08001512 AmTsPlayer_showVideo(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001513 AmTsPlayer_setVideoParams(player->handle, &vparams);
1514 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001515 }
hualing chena540a7e2020-03-27 16:44:05 +08001516
hualing chen4b7c15d2020-04-07 16:13:48 +08001517 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1518 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001519 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1520 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1521 player->cmd.state = DVR_PLAYBACK_STATE_START;
1522 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001523 } else {
1524 player->cmd.last_cmd = player->cmd.cur_cmd;
1525 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001526 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001527 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001528 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001529 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001530 } else {
1531 if (VALID_PID(aparams.pid)) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001532 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08001533 player->has_audio = DVR_TRUE;
1534 AmTsPlayer_setAudioParams(player->handle, &aparams);
1535 AmTsPlayer_startAudioDecoding(player->handle);
1536 }
hualing chendf118dd2020-05-21 15:49:11 +08001537 if (VALID_PID(adparams.pid)) {
1538 player->has_ad_audio = DVR_TRUE;
1539 DVR_PB_DG(1, "start ad audio");
1540 AmTsPlayer_setADParams(player->handle, &adparams);
1541 AmTsPlayer_enableADMix(player->handle);
1542 }
hualing chen31140872020-03-25 12:29:26 +08001543 }
hualing chencc91e1c2020-02-28 13:26:17 +08001544 player->cmd.state = DVR_PLAYBACK_STATE_START;
1545 player->state = DVR_PLAYBACK_STATE_START;
1546 }
hualing chen86e7d482020-01-16 15:13:33 +08001547 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001548 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001549 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001550 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001551 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001552}
hualing chen040df222020-01-17 13:35:02 +08001553/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001554 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001555 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001556 * \retval DVR_SUCCESS On success
1557 * \return Error code
1558 */
hualing chen040df222020-01-17 13:35:02 +08001559int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1560 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001561
hualing chena540a7e2020-03-27 16:44:05 +08001562 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001563 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001564 return DVR_FAILURE;
1565 }
1566
hualing chen4b7c15d2020-04-07 16:13:48 +08001567 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001568 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001569
hualing chen040df222020-01-17 13:35:02 +08001570 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1571 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001572
hualing chen86e7d482020-01-16 15:13:33 +08001573 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001574 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001575 //cp location
hualing chen040df222020-01-17 13:35:02 +08001576 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001577
hualing chen4b7c15d2020-04-07 16:13:48 +08001578 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 +08001579 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001580
1581 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001582 segment->pids.video.pid = info->pids.video.pid;
1583 segment->pids.video.format = info->pids.video.format;
1584 segment->pids.video.type = info->pids.video.type;
1585
hualing chen2aba4022020-03-02 13:49:55 +08001586 segment->pids.audio.pid = info->pids.audio.pid;
1587 segment->pids.audio.format = info->pids.audio.format;
1588 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001589
hualing chen2aba4022020-03-02 13:49:55 +08001590 segment->pids.ad.pid = info->pids.ad.pid;
1591 segment->pids.ad.format = info->pids.ad.format;
1592 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001593
1594 segment->pids.pcr.pid = info->pids.pcr.pid;
1595
hualing chen4b7c15d2020-04-07 16:13:48 +08001596 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 +08001597 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001598 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001599 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001600 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001601
hualing chen5cbe1a62020-02-10 16:36:36 +08001602 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001603}
hualing chen040df222020-01-17 13:35:02 +08001604/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001605 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001606 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001607 * \retval DVR_SUCCESS On success
1608 * \return Error code
1609 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001610int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001611 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001612 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001613 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001614 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001615 return DVR_FAILURE;
1616 }
1617
hualing chencc91e1c2020-02-28 13:26:17 +08001618 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001619 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001620 return DVR_FAILURE;
1621 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001622 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001623 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001624 DVR_PlaybackSegmentInfo_t *segment = NULL;
1625 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1626 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001627 {
hualing chen040df222020-01-17 13:35:02 +08001628 if (segment->segment_id == segment_id) {
1629 list_del(&segment->head);
1630 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001631 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001632 }
hualing chen86e7d482020-01-16 15:13:33 +08001633 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001634 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001635 pthread_mutex_unlock(&player->lock);
1636
1637 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001638}
hualing chen040df222020-01-17 13:35:02 +08001639/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001640 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001641 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001642 * \retval DVR_SUCCESS On success
1643 * \return Error code
1644 */
hualing chen040df222020-01-17 13:35:02 +08001645int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001646 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001647 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001648 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001649 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001650 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001651 return DVR_FAILURE;
1652 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001653 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1654 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1655 return DVR_SUCCESS;
1656 }
hualing chena540a7e2020-03-27 16:44:05 +08001657
hualing chen040df222020-01-17 13:35:02 +08001658 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001659 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001660 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001661 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001662 {
hualing chen040df222020-01-17 13:35:02 +08001663 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08001664 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001665 }
hualing chen86e7d482020-01-16 15:13:33 +08001666 // if encramble to free, only set flag and return;
1667
1668 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001669 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001670 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1671 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001672 //disable display, mute
hualing chen703f3572021-01-06 12:51:34 +08001673 DVR_PB_DG(1, "mute av");
hualing chen2aba4022020-03-02 13:49:55 +08001674 AmTsPlayer_hideVideo(player->handle);
1675 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001676 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1677 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001678 //enable display, unmute
hualing chen703f3572021-01-06 12:51:34 +08001679 DVR_PB_DG(1, "unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08001680 AmTsPlayer_showVideo(player->handle);
1681 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001682 } else {
1683 //do nothing
1684 }
1685 } else {
1686 //do nothing
1687 }
1688 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001689 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001690 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001691 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001692 pthread_mutex_unlock(&player->lock);
1693 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001694}
1695
1696
hualing chen5cbe1a62020-02-10 16:36:36 +08001697static 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 +08001698 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001699 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001700 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001701 return DVR_FAILURE;
1702 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001703 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001704 if (now_pid.pid == set_pid.pid) {
1705 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001706 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001707 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001708 if (VALID_PID(now_pid.pid)) {
1709 //stop now stream
1710 if (type == 0) {
1711 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001712 if (player->has_video == DVR_TRUE) {
1713 DVR_PB_DG(1, "stop video");
1714 AmTsPlayer_stopVideoDecoding(player->handle);
1715 player->has_video = DVR_FALSE;
1716 }
hualing chen86e7d482020-01-16 15:13:33 +08001717 } else if (type == 1) {
1718 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001719 if (player->has_audio == DVR_TRUE) {
1720 DVR_PB_DG(1, "stop audio");
1721 AmTsPlayer_stopAudioDecoding(player->handle);
1722 player->has_audio = DVR_FALSE;
1723 }
hualing chen86e7d482020-01-16 15:13:33 +08001724 } else if (type == 2) {
1725 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001726 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001727 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001728 } else if (type == 3) {
1729 //pcr
1730 }
1731 }
1732 if (VALID_PID(set_pid.pid)) {
1733 //start
1734 if (type == 0) {
1735 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001736 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001737 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001738 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001739 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001740 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001741 AmTsPlayer_setVideoParams(player->handle, &vparams);
1742 AmTsPlayer_startVideoDecoding(player->handle);
1743 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001744 } else if (type == 1) {
1745 //start audio
hualing chenc70a8df2020-05-12 19:23:11 +08001746 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
1747 am_tsplayer_audio_params aparams;
1748 aparams.pid = set_pid.pid;
1749 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1750 player->has_audio = DVR_TRUE;
1751 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1752 AmTsPlayer_setAudioParams(player->handle, &aparams);
1753 AmTsPlayer_startAudioDecoding(player->handle);
1754 //playback_device_audio_start(player->handle,&aparams);
1755 }
hualing chen86e7d482020-01-16 15:13:33 +08001756 } else if (type == 2) {
hualing chen39628212020-05-14 10:35:13 +08001757 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chenc70a8df2020-05-12 19:23:11 +08001758 am_tsplayer_audio_params aparams;
1759 aparams.pid = set_pid.pid;
1760 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1761 player->has_audio = DVR_TRUE;
1762 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1763 AmTsPlayer_setADParams(player->handle, &aparams);
1764 AmTsPlayer_enableADMix(player->handle);
1765 //playback_device_audio_start(player->handle,&aparams);
1766 }
hualing chen86e7d482020-01-16 15:13:33 +08001767 } else if (type == 3) {
1768 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001769 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001770 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001771 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001772 //audio and video all close
1773 if (!player->has_audio && !player->has_video) {
1774 player->state = DVR_PLAYBACK_STATE_STOP;
1775 }
hualing chen86e7d482020-01-16 15:13:33 +08001776 }
1777 }
1778 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001779}
hualing chen5cbe1a62020-02-10 16:36:36 +08001780/**\brief dvr play back update segment pids
1781 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001782 * add pid stream and stop remove pid stream.
1783 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001784 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001785 * \retval DVR_SUCCESS On success
1786 * \return Error code
1787 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001788int 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 +08001789 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001790 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001791 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001792 return DVR_FAILURE;
1793 }
1794
hualing chen040df222020-01-17 13:35:02 +08001795 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001796 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001797 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001798 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 +08001799
hualing chen040df222020-01-17 13:35:02 +08001800 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001801 {
hualing chen040df222020-01-17 13:35:02 +08001802 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001803
1804 if (player->cur_segment_id == segment_id) {
1805 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
1806 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
1807 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08001808 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08001809 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001810 return 0;
1811 }
1812
1813 //if segment is on going segment,we need stop start stream
1814 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08001815 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001816 //check video pids, stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001817 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, p_pids->video, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08001818 //check audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001819 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, p_pids->audio, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001820 //check sub audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001821 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, p_pids->ad, 2);
hualing chen5cbe1a62020-02-10 16:36:36 +08001822 //check pcr pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001823 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, p_pids->pcr, 3);
1824 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001825 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1826 //if state is pause, we need process at resume api. we only record change info
1827 int v_cmd = DVR_PLAYBACK_CMD_NONE;
1828 int a_cmd = DVR_PLAYBACK_CMD_NONE;
1829 if (VALID_PID(segment->pids.video.pid)
1830 && VALID_PID(p_pids->video.pid)
1831 && segment->pids.video.pid != p_pids->video.pid) {
1832 //restart video
1833 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
1834 }
1835 if (!VALID_PID(segment->pids.video.pid)
1836 && VALID_PID(p_pids->video.pid)
1837 && segment->pids.video.pid != p_pids->video.pid) {
1838 //start video
1839 v_cmd = DVR_PLAYBACK_CMD_VSTART;
1840 }
1841 if (VALID_PID(segment->pids.video.pid)
1842 && !VALID_PID(p_pids->video.pid)
1843 && segment->pids.video.pid != p_pids->video.pid) {
1844 //stop video
1845 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
1846 }
1847 if (VALID_PID(segment->pids.audio.pid)
1848 && VALID_PID(p_pids->audio.pid)
1849 && segment->pids.audio.pid != p_pids->audio.pid) {
1850 //restart audio
1851 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
1852 }
1853 if (!VALID_PID(segment->pids.audio.pid)
1854 && VALID_PID(p_pids->audio.pid)
1855 && segment->pids.audio.pid != p_pids->audio.pid) {
1856 //start audio
1857 a_cmd = DVR_PLAYBACK_CMD_ASTART;
1858 }
1859 if (VALID_PID(segment->pids.audio.pid)
1860 && !VALID_PID(p_pids->audio.pid)
1861 && segment->pids.audio.pid != p_pids->audio.pid) {
1862 //stop audio
1863 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
1864 }
1865 if (a_cmd == DVR_PLAYBACK_CMD_NONE
1866 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
1867 //do nothing
1868 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
1869 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
1870 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1871 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
1872 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
1873 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
1874 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1875 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1876 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1877 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
1878 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1879 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1880 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1881 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
1882 } else {
1883 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1884 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
1885 }
1886
1887 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1888 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1889 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1890 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
1891 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1892 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1893 //not occur this case
1894 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1895 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
1896 } else {
1897 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1898 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
1899 }
1900
1901 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1902 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1903 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1904 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
1905 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1906 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
1907 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1908 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
1909 } else {
1910 //not occur this case
1911 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1912 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1913 }
1914 }
1915 }
hualing chene10666f2020-04-14 13:58:37 +08001916 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08001917 }
hualing chen86e7d482020-01-16 15:13:33 +08001918 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08001919 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08001920 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08001921 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001922 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001923 }
hualing chen86e7d482020-01-16 15:13:33 +08001924 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001925 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001926 pthread_mutex_unlock(&player->lock);
1927 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001928}
1929/**\brief Stop play, will stop video and audio
1930 * \param[in] handle playback handle
1931 * \param[in] clear is clear last frame
1932 * \retval DVR_SUCCESS On success
1933 * \return Error code
1934 */
hualing chen040df222020-01-17 13:35:02 +08001935int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
1936 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001937
1938 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001939 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001940 return DVR_FAILURE;
1941 }
hualing chenb96aa2c2020-04-15 14:13:53 +08001942 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1943 DVR_PB_DG(1, ":playback is stoped");
1944 return DVR_SUCCESS;
1945 }
Ke Gong3c0caba2020-04-21 22:58:18 -07001946 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1947 DVR_PB_DG(1, ":playback is stoped");
1948 return DVR_SUCCESS;
1949 }
hualing chen87072a82020-03-12 16:20:12 +08001950 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001951 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001952 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08001953 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08001954 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001955 if (player->has_video) {
1956 AmTsPlayer_resumeVideoDecoding(player->handle);
1957 }
1958 if (player->has_audio) {
1959 AmTsPlayer_resumeAudioDecoding(player->handle);
1960 }
1961 if (player->has_video) {
1962 player->has_video = DVR_FALSE;
hualing chen703f3572021-01-06 12:51:34 +08001963 //AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001964 AmTsPlayer_stopVideoDecoding(player->handle);
1965 }
1966 if (player->has_audio) {
1967 player->has_audio = DVR_FALSE;
1968 AmTsPlayer_stopAudioDecoding(player->handle);
1969 }
hualing chendf118dd2020-05-21 15:49:11 +08001970 if (player->has_ad_audio) {
1971 player->has_ad_audio =DVR_FALSE;
1972 AmTsPlayer_disableADMix(player->handle);
1973 }
hualing chen266b9502020-04-04 17:39:39 +08001974
hualing chen86e7d482020-01-16 15:13:33 +08001975 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001976 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1977 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1978 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08001979 player->cur_segment_id = UINT64_MAX;
1980 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001981 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08001982 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08001983 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001984 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001985}
1986/**\brief Start play audio
1987 * \param[in] handle playback handle
1988 * \param[in] params audio playback params,contains fmt and pid...
1989 * \retval DVR_SUCCESS On success
1990 * \return Error code
1991 */
hualing chen2aba4022020-03-02 13:49:55 +08001992
hualing chendf118dd2020-05-21 15:49:11 +08001993int 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 +08001994 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001995
1996 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001997 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001998 return DVR_FAILURE;
1999 }
hualing chen86e7d482020-01-16 15:13:33 +08002000 _start_playback_thread(handle);
2001 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002002 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002003 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002004
2005 if (VALID_PID(param->pid)) {
2006 DVR_PB_DG(1, "start audio");
2007 player->has_audio = DVR_TRUE;
2008 AmTsPlayer_setAudioParams(player->handle, param);
2009 AmTsPlayer_startAudioDecoding(player->handle);
2010 }
2011 if (VALID_PID(adparam->pid)) {
2012 player->has_ad_audio = DVR_TRUE;
2013 DVR_PB_DG(1, "start ad audio");
2014 AmTsPlayer_setADParams(player->handle, adparam);
2015 AmTsPlayer_enableADMix(player->handle);
2016 }
2017
hualing chen86e7d482020-01-16 15:13:33 +08002018 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002019 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
2020 player->cmd.state = DVR_PLAYBACK_STATE_START;
2021 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002022 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002023 pthread_mutex_unlock(&player->lock);
2024 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002025}
2026/**\brief Stop play audio
2027 * \param[in] handle playback handle
2028 * \retval DVR_SUCCESS On success
2029 * \return Error code
2030 */
hualing chen040df222020-01-17 13:35:02 +08002031int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2032 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002033
2034 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002035 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002036 return DVR_FAILURE;
2037 }
2038
hualing chen2aba4022020-03-02 13:49:55 +08002039 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002040 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002041 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2042 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002043 //destory thread
2044 _stop_playback_thread(handle);
2045 } else {
2046 //do nothing.video is playing
2047 }
hualing chen7a56cba2020-04-14 14:09:27 +08002048 DVR_PB_DG(1, "lock");
2049 pthread_mutex_lock(&player->lock);
2050
hualing chenf00cdc82020-06-10 14:23:35 +08002051 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002052 player->has_audio = DVR_FALSE;
2053 AmTsPlayer_stopAudioDecoding(player->handle);
2054 }
hualing chen87072a82020-03-12 16:20:12 +08002055
hualing chendf118dd2020-05-21 15:49:11 +08002056 if (player->has_ad_audio) {
2057 player->has_ad_audio =DVR_FALSE;
2058 AmTsPlayer_disableADMix(player->handle);
2059 }
2060
hualing chen87072a82020-03-12 16:20:12 +08002061 player->cmd.last_cmd = player->cmd.cur_cmd;
2062 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2063
hualing chen4b7c15d2020-04-07 16:13:48 +08002064 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002065 pthread_mutex_unlock(&player->lock);
2066 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002067}
2068/**\brief Start play video
2069 * \param[in] handle playback handle
2070 * \param[in] params video playback params,contains fmt and pid...
2071 * \retval DVR_SUCCESS On success
2072 * \return Error code
2073 */
hualing chen2aba4022020-03-02 13:49:55 +08002074int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002075 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 _start_playback_thread(handle);
2083 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002084 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002085 pthread_mutex_lock(&player->lock);
2086 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002087 AmTsPlayer_setVideoParams(player->handle, param);
2088 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002089
2090 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002091 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002092 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002093 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002094 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2095 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002096 }
2097 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002098 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2099 player->cmd.state = DVR_PLAYBACK_STATE_START;
2100 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002101 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002102 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002103 return DVR_SUCCESS;
2104}
2105/**\brief Stop play video
2106 * \param[in] handle playback handle
2107 * \retval DVR_SUCCESS On success
2108 * \return Error code
2109 */
hualing chen040df222020-01-17 13:35:02 +08002110int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2111 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002112
2113 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002114 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002115 return DVR_FAILURE;
2116 }
2117
hualing chen86e7d482020-01-16 15:13:33 +08002118 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002119 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2120 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002121 //destory thread
2122 _stop_playback_thread(handle);
2123 } else {
2124 //do nothing.audio is playing
2125 }
hualing chen7a56cba2020-04-14 14:09:27 +08002126
2127 DVR_PB_DG(1, "lock");
2128 pthread_mutex_lock(&player->lock);
2129
hualing chen87072a82020-03-12 16:20:12 +08002130 player->has_video = DVR_FALSE;
2131
2132 AmTsPlayer_stopVideoDecoding(player->handle);
2133 //playback_device_video_stop(player->handle);
2134
2135 player->cmd.last_cmd = player->cmd.cur_cmd;
2136 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2137
hualing chen4b7c15d2020-04-07 16:13:48 +08002138 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002139 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002140 return DVR_SUCCESS;
2141}
2142/**\brief Pause play
2143 * \param[in] handle playback handle
2144 * \param[in] flush whether its internal buffers should be flushed
2145 * \retval DVR_SUCCESS On success
2146 * \return Error code
2147 */
hualing chen040df222020-01-17 13:35:02 +08002148int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2149 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002150
2151 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002152 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002153 return DVR_FAILURE;
2154 }
hualing chenf00cdc82020-06-10 14:23:35 +08002155 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2156 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002157 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002158 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002159 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002160 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002161 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002162 if (player->has_video)
2163 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002164 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002165 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002166
2167 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002168 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2169 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2170 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2171 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002172 } else {
2173 player->cmd.last_cmd = player->cmd.cur_cmd;
2174 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2175 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2176 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002177 }
hualing chen86e7d482020-01-16 15:13:33 +08002178 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002179 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002180
hualing chen86e7d482020-01-16 15:13:33 +08002181 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002182}
2183
hualing chen5cbe1a62020-02-10 16:36:36 +08002184//not add lock
2185static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2186{
2187 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2188
hualing chena540a7e2020-03-27 16:44:05 +08002189 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002190 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002191 return DVR_FAILURE;
2192 }
2193
hualing chen5cbe1a62020-02-10 16:36:36 +08002194 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002195 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002196 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002197 am_tsplayer_video_params vparams;
2198 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002199 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002200 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002201
hualing chendf118dd2020-05-21 15:49:11 +08002202 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002203 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002204 pthread_mutex_unlock(&player->lock);
2205
2206 switch (cmd) {
2207 case DVR_PLAYBACK_CMD_AVRESTART:
2208 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002209 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002210 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002211 break;
2212 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002213 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2214 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002215 break;
2216 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002217 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002218 break;
2219 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002220 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002221 break;
2222 case DVR_PLAYBACK_CMD_ARESTART:
2223 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002224 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002225 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002226 break;
2227 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002228 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002229 break;
2230 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002231 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002232 break;
2233 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002234 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2235 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2236 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002237 break;
2238 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002239 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2240 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002241 break;
2242 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002243 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2244 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002245 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002246 break;
2247 case DVR_PLAYBACK_CMD_STOP:
2248 break;
2249 case DVR_PLAYBACK_CMD_START:
2250 break;
2251 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002252 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2253 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002254 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002255 break;
2256 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002257 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2258 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002259 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002260 break;
2261 case DVR_PLAYBACK_CMD_FF:
2262 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002263 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002264 break;
2265 default:
2266 break;
2267 }
2268 return DVR_SUCCESS;
2269}
2270
2271/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002272 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002273 * \retval DVR_SUCCESS On success
2274 * \return Error code
2275 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002276int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2277 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2278
hualing chena540a7e2020-03-27 16:44:05 +08002279 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002280 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002281 return DVR_FAILURE;
2282 }
2283
hualing chen5cbe1a62020-02-10 16:36:36 +08002284 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002285 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002286 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002287 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002288 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002289 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2290 AmTsPlayer_resumeVideoDecoding(player->handle);
2291 }
2292 if (player->has_audio) {
2293 AmTsPlayer_resumeAudioDecoding(player->handle);
2294 }
2295 //check is has audio param,if has audio .we need start audio,
2296 //we will stop audio when ff fb, if reach end, we will pause.so we need
2297 //start audio when resume play
2298
2299 am_tsplayer_video_params vparams;
2300 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002301 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002302 uint64_t segmentid = player->cur_segment_id;
hualing chendf118dd2020-05-21 15:49:11 +08002303 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002304 //valid audio pid, start audio
hualing chenc70a8df2020-05-12 19:23:11 +08002305 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 +08002306 player->has_audio = DVR_TRUE;
2307 AmTsPlayer_setAudioParams(player->handle, &aparams);
2308 AmTsPlayer_startAudioDecoding(player->handle);
2309 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002310 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 +08002311 }
hualing chendf118dd2020-05-21 15:49:11 +08002312
2313 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2314 player->has_ad_audio = DVR_TRUE;
2315 DVR_PB_DG(1, "start ad audio");
2316 AmTsPlayer_setADParams(player->handle, &adparams);
2317 AmTsPlayer_enableADMix(player->handle);
2318 }
2319
hualing chen87072a82020-03-12 16:20:12 +08002320 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2321 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2322 player->cmd.state = DVR_PLAYBACK_STATE_START;
2323 player->state = DVR_PLAYBACK_STATE_START;
2324 } else {
2325 player->cmd.last_cmd = player->cmd.cur_cmd;
2326 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2327 player->cmd.state = DVR_PLAYBACK_STATE_START;
2328 player->state = DVR_PLAYBACK_STATE_START;
2329 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002330 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002331 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002332 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chene41f4372020-06-06 16:29:17 +08002333 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002334 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002335 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002336 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002337 }
hualing chen041c4092020-04-05 15:11:50 +08002338 if (player->has_audio)
2339 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002340 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002341 player->cmd.state = DVR_PLAYBACK_STATE_START;
2342 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002343 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2344 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002345 } else {
2346 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2347 {
2348 pthread_mutex_lock(&player->lock);
2349 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002350 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002351 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2352 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002353 if (player->has_video) {
2354 AmTsPlayer_resumeVideoDecoding(player->handle);
2355 }
2356 if (player->has_audio)
2357 AmTsPlayer_resumeAudioDecoding(player->handle);
2358
hualing chen041c4092020-04-05 15:11:50 +08002359 pthread_mutex_unlock(&player->lock);
2360 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002361 }
2362 return DVR_SUCCESS;
2363}
2364
hualing chena540a7e2020-03-27 16:44:05 +08002365static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2366
2367 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2368 DVR_PlaybackSegmentInfo_t *segment = NULL;
2369 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2370 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2371
2372 list_for_each_entry(segment, &player->segment_list, head)
2373 {
2374 if (segment->segment_id == segment_id) {
2375 cur_segment = segment;
2376 }
2377 if (segment->segment_id == set_seg_id) {
2378 set_segment = segment;
2379 }
2380 if (cur_segment != NULL && set_segment != NULL) {
2381 break;
2382 }
2383 }
2384 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002385 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002386 return DVR_TRUE;
2387 }
2388 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2389 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2390 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2391 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002392 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 +08002393 return DVR_TRUE;
2394 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002395 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002396 return DVR_FALSE;
2397}
2398
hualing chen5cbe1a62020-02-10 16:36:36 +08002399/**\brief seek
2400 * \param[in] handle playback handle
2401 * \param[in] time_offset time offset base cur segment
2402 * \retval DVR_SUCCESS On success
2403 * \return Error code
2404 */
hualing chencc91e1c2020-02-28 13:26:17 +08002405int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002406 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002407
2408 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002409 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002410 return DVR_FAILURE;
2411 }
2412
hualing chen4b7c15d2020-04-07 16:13:48 +08002413 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 +08002414 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002415
hualing chen86e7d482020-01-16 15:13:33 +08002416 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002417 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002418 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002419
hualing chen5cbe1a62020-02-10 16:36:36 +08002420 //open segment if id is not current segment
hualing chen87072a82020-03-12 16:20:12 +08002421 int ret = _dvr_open_segment(handle, segment_id);
2422 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002423 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002424 pthread_mutex_unlock(&player->lock);
2425 return DVR_FAILURE;
2426 }
2427 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2428 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002429 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002430 //pthread_mutex_unlock(&player->lock);
2431 //return DVR_SUCCESS;
2432 time_offset = _dvr_get_end_time(handle);
2433 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002434 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002435 time_offset = _dvr_get_end_time(handle);
hualing chen87072a82020-03-12 16:20:12 +08002436 pthread_mutex_unlock(&player->lock);
2437 return DVR_FAILURE;
2438 }
2439 }
2440
hualing chen4b7c15d2020-04-07 16:13:48 +08002441 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 +08002442 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002443 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2444 //forward playback.not seek end of file
2445 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2446 //default -2000ms
2447 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2448 }
hualing chen86e7d482020-01-16 15:13:33 +08002449 }
hualing chen2aba4022020-03-02 13:49:55 +08002450 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002451 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002452 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002453 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002454 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 +08002455 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002456 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002457
hualing chen2aba4022020-03-02 13:49:55 +08002458 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002459
2460 player->last_send_time_id = UINT64_MAX;
2461
hualing chen2aba4022020-03-02 13:49:55 +08002462 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002463 player->fffb_current = _dvr_time_getClock();
2464 player->fffb_start = player->fffb_current;
2465 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2466 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002467 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002468 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002469 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002470 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002471 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002472 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002473 }
hualing chen86e7d482020-01-16 15:13:33 +08002474 //stop play
hualing chen4b7c15d2020-04-07 16:13:48 +08002475 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 +08002476 if (player->has_video) {
2477 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002478 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002479 }
2480
2481 if (player->has_audio) {
2482 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002483 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002484 }
hualing chendf118dd2020-05-21 15:49:11 +08002485 if (player->has_ad_audio) {
2486 player->has_ad_audio =DVR_FALSE;
2487 AmTsPlayer_disableADMix(player->handle);
2488 }
2489
hualing chen86e7d482020-01-16 15:13:33 +08002490 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002491 am_tsplayer_video_params vparams;
2492 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002493 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002494
hualing chen040df222020-01-17 13:35:02 +08002495 player->cur_segment_id = segment_id;
2496
2497 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002498 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002499 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002500 //start audio and video
Zhiqiang Han9adc9722020-11-11 18:38:10 +08002501 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08002502 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002503 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 +08002504 pthread_mutex_unlock(&player->lock);
2505 return -1;
2506 }
2507 //add
hualing chen040df222020-01-17 13:35:02 +08002508 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002509 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002510 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002511 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002512 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002513 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002514 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002515 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002516 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002517 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002518 }
hualing chen2aba4022020-03-02 13:49:55 +08002519 AmTsPlayer_setVideoParams(player->handle, &vparams);
2520 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002521 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2522 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2523 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2524 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2525 AmTsPlayer_stopFast(player->handle);
2526 }
hualing chen266b9502020-04-04 17:39:39 +08002527 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002528 }
hualing chene41f4372020-06-06 16:29:17 +08002529 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002530 DVR_PB_DG(1, "start audio seek");
hualing chen2aba4022020-03-02 13:49:55 +08002531 AmTsPlayer_setAudioParams(player->handle, &aparams);
2532 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002533 player->has_audio = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002534 }
hualing chene41f4372020-06-06 16:29:17 +08002535 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002536 player->has_ad_audio = DVR_TRUE;
2537 DVR_PB_DG(1, "start ad audio");
2538 AmTsPlayer_setADParams(player->handle, &adparams);
2539 AmTsPlayer_enableADMix(player->handle);
2540 }
hualing chen86e7d482020-01-16 15:13:33 +08002541 }
hualing chene41f4372020-06-06 16:29:17 +08002542 if (player->state == DVR_PLAYBACK_STATE_PAUSE /*&&
hualing chen2aba4022020-03-02 13:49:55 +08002543 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2544 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2545 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chene41f4372020-06-06 16:29:17 +08002546 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))*/) {
hualing chen2aba4022020-03-02 13:49:55 +08002547 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2548 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002549 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002550 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2551 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002552 player->speed > 1.0f||
2553 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002554 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002555 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002556 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002557 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002558 player->cmd.last_cmd = player->cmd.cur_cmd;
2559 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2560 player->cmd.state = DVR_PLAYBACK_STATE_START;
2561 player->state = DVR_PLAYBACK_STATE_START;
2562 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002563 player->last_send_time_id = UINT64_MAX;
2564 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002565 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002566
2567 return DVR_SUCCESS;
2568}
hualing chen5cbe1a62020-02-10 16:36:36 +08002569
hualing chen5cbe1a62020-02-10 16:36:36 +08002570static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2571 //get cur time of segment
2572 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002573
2574 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002575 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002576 return DVR_FAILURE;
2577 }
2578
hualing chen31140872020-03-25 12:29:26 +08002579 int64_t cache = 0;//defalut es buf cache 500ms
2580 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002581 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002582 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2583 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002584 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002585 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 +08002586 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2587 cache = 0;
2588 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002589 int cur_time = (int)(cur > cache ? cur - cache : 0);
2590 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002591}
2592
2593//get current segment current pcr time of read pos
2594static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2595 //get cur time of segment
2596 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002597
2598 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002599 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002600 return DVR_FAILURE;
2601 }
2602
hualing chen2aba4022020-03-02 13:49:55 +08002603 pthread_mutex_lock(&player->segment_lock);
2604 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002605 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002606 pthread_mutex_unlock(&player->segment_lock);
2607 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002608}
2609
hualing chen4b7c15d2020-04-07 16:13:48 +08002610#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08002611//start replay
2612static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
2613
2614 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2615 //calculate pcr seek time
2616 int t_diff = 0;
2617 int seek_time = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002618
2619 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002620 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002621 return DVR_FAILURE;
2622 }
2623
hualing chen5cbe1a62020-02-10 16:36:36 +08002624 if (player->fffb_start == -1) {
2625 //set fffb start time ms
2626 player->fffb_start = _dvr_time_getClock();
2627 player->fffb_current = player->fffb_start;
2628 //get segment current time pos
2629 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002630 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 +08002631 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08002632 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08002633 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002634 } else {
2635 player->fffb_current = _dvr_time_getClock();
2636 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08002637 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08002638 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08002639 if (seek_time <= 0) {
2640 //need seek to pre one segment
2641 seek_time = 0;
2642 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002643 //seek segment pos
2644 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08002645 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002646 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08002647 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2648 //set seek time to 0;
2649 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]", seek_time, player->cur_segment_id);
2650 seek_time = 0;
2651 }
hualing chen041c4092020-04-05 15:11:50 +08002652 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
2653 seek_time = 0;
2654 }
hualing chen2aba4022020-03-02 13:49:55 +08002655 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002656 } else {
2657 //
hualing chen4b7c15d2020-04-07 16:13:48 +08002658 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08002659 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002660 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 +08002661 }
hualing chen2aba4022020-03-02 13:49:55 +08002662 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08002663}
2664
2665
2666//start replay
2667static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
2668 //
2669 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002670
2671 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002672 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002673 return DVR_FAILURE;
2674 }
2675
hualing chen5cbe1a62020-02-10 16:36:36 +08002676 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002677 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002678 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08002679 AmTsPlayer_stopVideoDecoding(player->handle);
2680 }
2681 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002682 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08002683 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002684 AmTsPlayer_stopAudioDecoding(player->handle);
2685 }
hualing chendf118dd2020-05-21 15:49:11 +08002686 if (player->has_ad_audio) {
2687 DVR_PB_DG(1, "fffb stop audio");
2688 player->has_ad_audio =DVR_FALSE;
2689 AmTsPlayer_disableADMix(player->handle);
2690 }
hualing chen2aba4022020-03-02 13:49:55 +08002691
hualing chen5cbe1a62020-02-10 16:36:36 +08002692 //start video and audio
2693
hualing chen2aba4022020-03-02 13:49:55 +08002694 am_tsplayer_video_params vparams;
2695 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002696 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002697 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002698
2699 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08002700 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002701 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002702 //start audio and video
2703 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2704 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002705 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08002706 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002707 return -1;
2708 }
2709
2710 if (VALID_PID(vparams.pid)) {
2711 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002712 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08002713 //DVR_PB_DG(1, "fffb start video and save last frame");
2714 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08002715 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08002716 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2717 AmTsPlayer_setVideoParams(player->handle, &vparams);
2718 AmTsPlayer_startVideoDecoding(player->handle);
2719 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002720 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08002721 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002722 }
hualing chen31140872020-03-25 12:29:26 +08002723 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08002724 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002725 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08002726 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002727 return 0;
2728}
2729
2730static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
2731 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002732 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002733 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002734 return DVR_FAILURE;
2735 }
2736
2737 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08002738 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002739 pthread_mutex_lock(&player->lock);
2740
hualing chen2aba4022020-03-02 13:49:55 +08002741 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002742 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 +08002743
hualing chen87072a82020-03-12 16:20:12 +08002744 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2745 //seek time set 0
2746 seek_time = 0;
2747 }
hualing chen041c4092020-04-05 15:11:50 +08002748 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08002749 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
2750 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08002751 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002752 pthread_mutex_unlock(&player->lock);
2753 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002754 //send event here and pause
2755 DVR_Play_Notify_t notify;
2756 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08002757 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08002758 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08002759 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08002760 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 +08002761 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08002762 return DVR_SUCCESS;
2763 }
hualing chen2932d372020-04-29 13:44:00 +08002764 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002765 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002766 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002767 }
2768 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002769 _dvr_playback_fffb_replay(handle);
2770
2771 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002772 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002773
hualing chen5cbe1a62020-02-10 16:36:36 +08002774 return DVR_SUCCESS;
2775}
2776
hualing chen87072a82020-03-12 16:20:12 +08002777//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08002778static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002779 //
2780 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002781
2782 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002783 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002784 return DVR_FAILURE;
2785 }
2786
hualing chen5cbe1a62020-02-10 16:36:36 +08002787 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002788 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08002789 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002790 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002791 }
2792
2793 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08002794 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002795 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002796 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002797 //start video and audio
2798
hualing chen2aba4022020-03-02 13:49:55 +08002799 am_tsplayer_video_params vparams;
2800 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002801 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002802 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002803
2804 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08002805 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08002806 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002807 //start audio and video
2808 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08002809 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002810 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08002811 return -1;
2812 }
2813
2814 if (VALID_PID(vparams.pid)) {
2815 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002816 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002817 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08002818 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08002819 }
hualing chen266b9502020-04-04 17:39:39 +08002820 else {
hualing chen2aba4022020-03-02 13:49:55 +08002821 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08002822 }
hualing chen2aba4022020-03-02 13:49:55 +08002823 AmTsPlayer_setVideoParams(player->handle, &vparams);
2824 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08002825 }
hualing chena540a7e2020-03-27 16:44:05 +08002826
2827 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08002828 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08002829 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002830 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08002831 } else {
hualing chena540a7e2020-03-27 16:44:05 +08002832 if (VALID_PID(aparams.pid)) {
2833 player->has_audio = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002834 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08002835 AmTsPlayer_setAudioParams(player->handle, &aparams);
Zhiqiang Hane013e3e2020-12-03 13:34:56 +08002836 AmTsPlayer_startAudioDecoding(player->handle);
hualing chena540a7e2020-03-27 16:44:05 +08002837 }
hualing chendf118dd2020-05-21 15:49:11 +08002838 if (VALID_PID(adparams.pid)) {
2839 player->has_ad_audio = DVR_TRUE;
2840 DVR_PB_DG(1, "start ad audio");
2841 AmTsPlayer_setADParams(player->handle, &adparams);
2842 AmTsPlayer_enableADMix(player->handle);
2843 }
2844
hualing chen7a56cba2020-04-14 14:09:27 +08002845 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002846 AmTsPlayer_stopFast(player->handle);
2847 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
2848 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
2849 }
hualing chen2aba4022020-03-02 13:49:55 +08002850 player->cmd.last_cmd = player->cmd.cur_cmd;
2851 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08002852 player->cmd.state = DVR_PLAYBACK_STATE_START;
2853 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002854 return 0;
2855}
2856
2857
hualing chenb31a6c62020-01-13 17:27:00 +08002858/**\brief Set play speed
2859 * \param[in] handle playback handle
2860 * \param[in] speed playback speed
2861 * \retval DVR_SUCCESS On success
2862 * \return Error code
2863 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002864int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
2865
2866 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002867
2868 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002869 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002870 return DVR_FAILURE;
2871 }
2872
hualing chen4b7c15d2020-04-07 16:13:48 +08002873 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002874 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002875 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002876 return DVR_FAILURE;
2877 }
hualing chenf00cdc82020-06-10 14:23:35 +08002878 if (speed.speed.speed == player->cmd.speed.speed.speed) {
2879 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
2880 return DVR_SUCCESS;
2881 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002882 pthread_mutex_lock(&player->lock);
2883 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
2884 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
2885 player->cmd.last_cmd = player->cmd.cur_cmd;
2886 }
hualing chene41f4372020-06-06 16:29:17 +08002887
hualing chen31140872020-03-25 12:29:26 +08002888 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08002889 IS_KERNEL_SPEED(speed.speed.speed) ) {
2890 //case 1. not start play.only set speed
2891 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2892 //only set speed.and return;
2893 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
2894 player->cmd.speed.speed = speed.speed;
2895 player->speed = (float)speed.speed.speed/(float)100;
2896 player->fffb_play = DVR_FALSE;
2897 pthread_mutex_unlock(&player->lock);
2898 return DVR_SUCCESS;
2899 }
2900 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08002901 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
2902 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002903 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002904 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2905 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002906 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002907 AmTsPlayer_stopFast(player->handle);
2908 pthread_mutex_unlock(&player->lock);
2909 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
2910 pthread_mutex_lock(&player->lock);
2911 } else {
2912 //set play speed and if audio is start, stop audio.
2913 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002914 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002915 AmTsPlayer_stopAudioDecoding(player->handle);
2916 player->has_audio = DVR_FALSE;
2917 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002918 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002919 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002920 }
hualing chenbcada022020-04-22 14:27:01 +08002921 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08002922 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002923 player->cmd.speed.speed = speed.speed;
2924 player->speed = (float)speed.speed.speed/(float)100;
2925 pthread_mutex_unlock(&player->lock);
2926 return DVR_SUCCESS;
2927 }
hualing chen31140872020-03-25 12:29:26 +08002928 //case 3 fffb mode
2929 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2930 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2931 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002932 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002933 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002934 player->cmd.speed.speed = speed.speed;
2935 player->speed = (float)speed.speed.speed/(float)100;
2936 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08002937 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002938 pthread_mutex_unlock(&player->lock);
2939 return DVR_SUCCESS;
2940 }
2941 }
2942 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08002943 IS_KERNEL_SPEED(speed.speed.speed)) {
2944 //case 1. cur speed is kernel support speed,set kernel speed.
2945 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08002946 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002947 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2948 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002949 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002950 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08002951 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08002952 } else {
2953 //set play speed and if audio is start, stop audio.
2954 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002955 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002956 AmTsPlayer_stopAudioDecoding(player->handle);
2957 player->has_audio = DVR_FALSE;
2958 }
hualing chenf00cdc82020-06-10 14:23:35 +08002959 DVR_PB_DG(1, "start fast");
2960 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08002961 }
hualing chena540a7e2020-03-27 16:44:05 +08002962 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002963 player->cmd.speed.speed = speed.speed;
2964 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08002965 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002966 pthread_mutex_unlock(&player->lock);
2967 return DVR_SUCCESS;
2968 }
2969 //case 2 fffb mode
2970 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2971 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2972 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002973 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002974 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002975 player->cmd.speed.speed = speed.speed;
2976 player->speed = (float)speed.speed.speed/(float)100;
2977 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08002978 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002979 pthread_mutex_unlock(&player->lock);
2980 return DVR_SUCCESS;
2981 }
hualing chen31140872020-03-25 12:29:26 +08002982 }
hualing chena540a7e2020-03-27 16:44:05 +08002983 if (IS_KERNEL_SPEED(speed.speed.speed)) {
2984 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08002985 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08002986 } else {
hualing chen31140872020-03-25 12:29:26 +08002987 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08002988 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
2989 else
2990 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08002991 player->fffb_play = DVR_TRUE;
2992 }
2993 DVR_Bool_t init_last_time = DVR_FALSE;
2994 if (player->speed > 0.0f && speed.speed.speed < 0) {
2995 init_last_time = DVR_TRUE;
2996 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
2997 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002998 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002999 player->cmd.speed.mode = speed.mode;
3000 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003001 player->speed = (float)speed.speed.speed/(float)100;
3002 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003003 _dvr_init_fffb_t(handle);
3004 if (init_last_time == DVR_TRUE)
3005 player->last_send_time_id = UINT64_MAX;
3006
hualing chen87072a82020-03-12 16:20:12 +08003007 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003008 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3009 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003010 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003011 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003012 _dvr_playback_replay(handle, DVR_FALSE);
3013 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3014 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3015 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003016 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003017 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003018 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003019 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003020 return DVR_SUCCESS;
3021}
hualing chen2932d372020-04-29 13:44:00 +08003022
hualing chenb31a6c62020-01-13 17:27:00 +08003023/**\brief Get playback status
3024 * \param[in] handle playback handle
3025 * \param[out] p_status playback status
3026 * \retval DVR_SUCCESS On success
3027 * \return Error code
3028 */
hualing chen2932d372020-04-29 13:44:00 +08003029static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3030 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003031//
3032 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003033
3034 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003035 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003036 return DVR_FAILURE;
3037 }
hualing chen2932d372020-04-29 13:44:00 +08003038 if (is_lock ==DVR_TRUE)
3039 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003040 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003041 //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 +08003042 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3043 player->state == DVR_PLAYBACK_STATE_START) {
3044 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3045 }
hualing chen041c4092020-04-05 15:11:50 +08003046
hualing chencc91e1c2020-02-28 13:26:17 +08003047 p_status->time_end = _dvr_get_end_time(handle);
hualing chen2aba4022020-03-02 13:49:55 +08003048 p_status->time_cur = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003049 if (player->last_send_time_id == UINT64_MAX) {
3050 player->last_send_time_id = player->cur_segment_id;
3051 player->last_cur_time = p_status->time_cur;
3052 }
3053 if (player->last_send_time_id == player->cur_segment_id) {
3054 if (player->speed > 0.0f ) {
3055 //ff
3056 if (p_status->time_cur < player->last_cur_time ) {
3057 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);
3058 p_status->time_cur = player->last_cur_time;
3059 } else {
3060 player->last_cur_time = p_status->time_cur;
3061 }
hualing chene41f4372020-06-06 16:29:17 +08003062 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003063 //fb
3064 if (p_status->time_cur > player->last_cur_time ) {
3065 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 );
3066 p_status->time_cur = player->last_cur_time;
3067 } else {
3068 player->last_cur_time = p_status->time_cur;
3069 }
3070 }
3071 } else {
3072 player->last_cur_time = p_status->time_cur;
3073 }
3074 player->last_send_time_id = player->cur_segment_id;
hualing chen041c4092020-04-05 15:11:50 +08003075 p_status->segment_id = player->cur_segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003076
hualing chen5cbe1a62020-02-10 16:36:36 +08003077 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003078 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003079 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003080 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 +08003081 _dvr_playback_state_toString(player->state),
3082 _dvr_playback_state_toString(p_status->state),
hualing chena540a7e2020-03-27 16:44:05 +08003083 p_status->time_cur, p_status->time_end,
3084 p_status->segment_id,player->play_flag,
hualing chen2932d372020-04-29 13:44:00 +08003085 player->speed,
3086 is_lock);
3087 if (is_lock ==DVR_TRUE)
3088 pthread_mutex_unlock(&player->lock);
3089 return DVR_SUCCESS;
3090}
3091
3092
3093/**\brief Get playback status
3094 * \param[in] handle playback handle
3095 * \param[out] p_status playback status
3096 * \retval DVR_SUCCESS On success
3097 * \return Error code
3098 */
3099int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3100 DVR_PlaybackStatus_t *p_status) {
3101//
3102 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3103
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003104 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3105
hualing chen2932d372020-04-29 13:44:00 +08003106 if (player == NULL) {
3107 DVR_PB_DG(1, "player is NULL");
3108 return DVR_FAILURE;
3109 }
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003110 pthread_mutex_lock(&player->lock);
3111 if (!player->has_video && !player->has_audio)
3112 p_status->time_cur = 0;
3113 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003114
hualing chenb31a6c62020-01-13 17:27:00 +08003115 return DVR_SUCCESS;
3116}
3117
hualing chen040df222020-01-17 13:35:02 +08003118void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3119 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003120 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3121 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3122 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3123 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3124 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3125 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3126 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 +08003127 }
hualing chenb31a6c62020-01-13 17:27:00 +08003128}
3129
hualing chen5cbe1a62020-02-10 16:36:36 +08003130int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003131 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003132
hualing chena540a7e2020-03-27 16:44:05 +08003133 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003134 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003135 return DVR_FAILURE;
3136 }
3137
hualing chen040df222020-01-17 13:35:02 +08003138 DVR_PlaybackSegmentInfo_t *segment;
3139 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003140 {
hualing chen040df222020-01-17 13:35:02 +08003141 if (segment_id >= 0) {
3142 if (segment->segment_id == segment_id) {
3143 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003144 break;
3145 }
3146 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003147 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003148 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003149 }
3150 }
3151 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003152}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003153
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003154int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003155{
3156 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3157 DVR_RETURN_IF_FALSE(player);
3158 DVR_RETURN_IF_FALSE(func);
3159
hualing chen4b7c15d2020-04-07 16:13:48 +08003160 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003161 pthread_mutex_lock(&player->lock);
3162
3163 player->dec_func = func;
3164 player->dec_userdata = userdata;
3165
3166 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003167 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003168 return DVR_SUCCESS;
3169}
3170
3171int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3172{
3173 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3174 DVR_RETURN_IF_FALSE(player);
3175 DVR_RETURN_IF_FALSE(p_secure_buf);
3176 DVR_RETURN_IF_FALSE(len);
3177
hualing chen4b7c15d2020-04-07 16:13:48 +08003178 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003179 pthread_mutex_lock(&player->lock);
3180
3181 player->is_secure_mode = 1;
3182 player->secure_buffer = p_secure_buf;
3183 player->secure_buffer_size = len;
3184
3185 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003186 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003187 return DVR_SUCCESS;
3188}