blob: a1d88cf970cc073a9d0ea6e773d120fa66fc2c16 [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001#include <stdio.h>
2#include <stdlib.h>
3
hualing chen5cbe1a62020-02-10 16:36:36 +08004#include <string.h>
hualing chenb31a6c62020-01-13 17:27:00 +08005#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <poll.h>
11#include <errno.h>
12#include <signal.h>
13#include <pthread.h>
hualing chenb5cd42e2020-04-15 17:03:34 +080014#include <errno.h>
hualing chenb31a6c62020-01-13 17:27:00 +080015
hualing chenb31a6c62020-01-13 17:27:00 +080016#include "dvr_playback.h"
17
hualing chen4b7c15d2020-04-07 16:13:48 +080018#define DVR_PB_DG(_level, _fmt, ...) \
19 DVR_DEBUG(_level, "playback %-30.30s:%d " _fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
20
hualing chena540a7e2020-03-27 16:44:05 +080021
hualing chenb31a6c62020-01-13 17:27:00 +080022#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080023
24
25#define FF_SPEED (2.0f)
26#define FB_SPEED (-1.0f)
27#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080028#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080029
30#define IS_KERNEL_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_X1) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
31#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
32
hualing chenb31a6c62020-01-13 17:27:00 +080033
hualing chenb5cd42e2020-04-15 17:03:34 +080034#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080035#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080036//if tsplayer delay time < 200 and no data can read, we will pause
37#define MIN_TSPLAYER_DELAY_TIME (200)
38
hualing chen041c4092020-04-05 15:11:50 +080039#define MAX_CACHE_TIME (30000)
40
hualing chena540a7e2020-03-27 16:44:05 +080041static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080042//
43static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chencc91e1c2020-02-28 13:26:17 +080044static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type);
45static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
46static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080047static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080048static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080049static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
50 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080051static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen87072a82020-03-12 16:20:12 +080052
hualing chenbcada022020-04-22 14:27:01 +080053
54static char* _cmd_toString(int cmd)
55{
56
57 char *string[DVR_PLAYBACK_CMD_NONE+1]={
58 "start",
59 "stop",
60 "vstart",
61 "astart",
62 "vstop",
63 "astop",
64 "vrestart",
65 "arestart",
66 "avrestart",
67 "vstopastart",
68 "astopvstart",
69 "vstoparestart",
70 "astopvrestart",
71 "vstartarestart",
72 "astartvrestart",
73 "pause",
74 "resume",
75 "seek",
76 "ff",
77 "fb",
78 "NONE"
79 };
80
81 if (cmd > DVR_PLAYBACK_CMD_NONE) {
82 return "unkown";
83 } else {
84 return string[cmd];
85 }
86}
87
88
hualing chen6d24aa92020-03-23 18:43:47 +080089static char* _dvr_playback_state_toString(int stat)
90{
91 char *string[DVR_PLAYBACK_STATE_FB+1]={
92 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080093 "stop",
hualing chen31140872020-03-25 12:29:26 +080094 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +080095 "ff",
96 "fb"
97 };
98
99 if (stat > DVR_PLAYBACK_STATE_FB) {
100 return "unkown";
101 } else {
102 return string[stat];
103 }
104}
hualing chena540a7e2020-03-27 16:44:05 +0800105
106static DVR_Bool_t _dvr_support_speed(int speed) {
107
108 DVR_Bool_t ret = DVR_FALSE;
109
110 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800111 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800112 case PLAYBACK_SPEED_FBX2:
113 case PLAYBACK_SPEED_FBX4:
114 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800115 case PLAYBACK_SPEED_FBX16:
116 case PLAYBACK_SPEED_FBX12:
117 case PLAYBACK_SPEED_FBX32:
118 case PLAYBACK_SPEED_FBX48:
119 case PLAYBACK_SPEED_FBX64:
120 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800121 case PLAYBACK_SPEED_S2:
122 case PLAYBACK_SPEED_S4:
123 case PLAYBACK_SPEED_S8:
124 case PLAYBACK_SPEED_X1:
125 case PLAYBACK_SPEED_X2:
126 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800127 case PLAYBACK_SPEED_X3:
128 case PLAYBACK_SPEED_X5:
129 case PLAYBACK_SPEED_X6:
130 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800131 case PLAYBACK_SPEED_X8:
132 case PLAYBACK_SPEED_X12:
133 case PLAYBACK_SPEED_X16:
134 case PLAYBACK_SPEED_X32:
135 case PLAYBACK_SPEED_X48:
136 case PLAYBACK_SPEED_X64:
137 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800138 ret = DVR_TRUE;
139 break;
140 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800141 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800142 break;
143 }
144 return ret;
145}
hualing chen6e4bfa52020-03-13 14:37:11 +0800146void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
147{
hualing chen4b7c15d2020-04-07 16:13:48 +0800148 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800149 DVR_Playback_t *player = NULL;
150 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800151 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800152 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800153 }
154 switch (event->type) {
155 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
156 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800157 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800158 event->event.video_format.frame_width,
159 event->event.video_format.frame_height,
160 event->event.video_format.frame_rate);
161 break;
162 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800163 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
164 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800165 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800166 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800167 break;
168 }
169 default:
170 break;
171 }
172}
hualing chen2aba4022020-03-02 13:49:55 +0800173void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
174{
hualing chen6e4bfa52020-03-13 14:37:11 +0800175 DVR_Playback_t *player = NULL;
176 if (user_data != NULL) {
177 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800178 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800179 }
hualing chen2aba4022020-03-02 13:49:55 +0800180 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800181 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
182 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800183 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800184 event->event.video_format.frame_width,
185 event->event.video_format.frame_height,
186 event->event.video_format.frame_rate);
187 break;
188 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800189 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
190 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800191 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800192 if (player->first_trans_ok == DVR_FALSE) {
193 player->first_trans_ok = DVR_TRUE;
194 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
195 }
hualing chena540a7e2020-03-27 16:44:05 +0800196 if (player != NULL)
197 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800198 break;
199 }
hualing chen487ae6d2020-07-22 10:34:11 +0800200 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
201 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
202 player->first_trans_ok = DVR_TRUE;
203 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
204 }
205 if (player != NULL && player->has_video == DVR_FALSE) {
206 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
207 player->first_frame = 1;
208 }
209 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800210 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800211 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800212 break;
213 }
214 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800215 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800216 player->player_callback_func(player->player_callback_userdata, event);
217 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800218 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800219 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800220 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800221 }
222}
hualing chencc91e1c2020-02-28 13:26:17 +0800223
hualing chen5cbe1a62020-02-10 16:36:36 +0800224//convert video and audio fmt
225static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
226 int format = 0;
227 if (is_audio == DVR_FALSE) {
228 //for video fmt
229 switch (fmt)
230 {
231 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800232 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800233 break;
234 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800235 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800236 break;
237 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800238 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800239 break;
240 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800241 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800242 break;
hualing chena540a7e2020-03-27 16:44:05 +0800243 case DVR_VIDEO_FORMAT_VP9:
244 format = AV_VIDEO_CODEC_VP9;
245 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800246 }
247 } else {
248 //for audio fmt
249 switch (fmt)
250 {
251 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800252 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800253 break;
254 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800255 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800256 break;
257 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800258 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800259 break;
260 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800261 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800262 break;
hualing chena540a7e2020-03-27 16:44:05 +0800263 case DVR_AUDIO_FORMAT_AAC:
264 format = AV_AUDIO_CODEC_AAC;
265 break;
266 case DVR_AUDIO_FORMAT_LATM:
267 format = AV_AUDIO_CODEC_LATM;
268 break;
269 case DVR_AUDIO_FORMAT_PCM:
270 format = AV_AUDIO_CODEC_PCM;
271 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800272 }
273 }
274 return format;
275}
hualing chen040df222020-01-17 13:35:02 +0800276static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800277{
hualing chen040df222020-01-17 13:35:02 +0800278 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800279
hualing chena540a7e2020-03-27 16:44:05 +0800280 if (player == NULL || player->handle == NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800281 return -1;
282
hualing chena540a7e2020-03-27 16:44:05 +0800283 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800284}
hualing chena540a7e2020-03-27 16:44:05 +0800285
hualing chen5cbe1a62020-02-10 16:36:36 +0800286//get sys time ms
287static int _dvr_time_getClock(void)
288{
289 struct timespec ts;
290 int ms;
291
292 clock_gettime(CLOCK_MONOTONIC, &ts);
293 ms = ts.tv_sec*1000+ts.tv_nsec/1000000;
294
295 return ms;
296}
hualing chen86e7d482020-01-16 15:13:33 +0800297
hualing chenb31a6c62020-01-13 17:27:00 +0800298
299//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800300static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800301{
hualing chen040df222020-01-17 13:35:02 +0800302 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800303
hualing chena540a7e2020-03-27 16:44:05 +0800304
305 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800306 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800307 return DVR_FAILURE;
308 }
309
hualing chen86e7d482020-01-16 15:13:33 +0800310 struct timespec ts;
311 clock_gettime(CLOCK_MONOTONIC, &ts);
312 //ms为毫秒,换算成秒
313 ts.tv_sec += ms/1000;
314 //在outtime的基础上,增加ms毫秒
315 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
316 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
317 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
318 //us的值有可能超过1秒,
319 ts.tv_sec += us / 1000000;
320 us = us % 1000000;
321 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800322 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
323 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800324}
hualing chen31140872020-03-25 12:29:26 +0800325//get tsplay delay time ms
326static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
327 DVR_Playback_t *player = (DVR_Playback_t *) handle;
328 int64_t cache = 0;
329 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800330 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800331 return 0;
332 }
333 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800334 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800335 return cache;
336}
hualing chenb31a6c62020-01-13 17:27:00 +0800337//send signal
hualing chen040df222020-01-17 13:35:02 +0800338static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800339{
hualing chen87072a82020-03-12 16:20:12 +0800340 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800341
342 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800343 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800344 return DVR_FAILURE;
345 }
346
hualing chen87072a82020-03-12 16:20:12 +0800347 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800348 pthread_cond_signal(&player->cond);
hualing chen87072a82020-03-12 16:20:12 +0800349 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800350 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800351}
352
hualing chen2932d372020-04-29 13:44:00 +0800353//send playback event, need check is need lock first
354static int _dvr_playback_sent_event(DVR_PlaybackHandle_t handle, DVR_PlaybackEvent_t evt, DVR_Play_Notify_t *notify, DVR_Bool_t is_lock) {
hualing chencc91e1c2020-02-28 13:26:17 +0800355
356 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800357
358 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800359 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800360 return DVR_FAILURE;
361 }
362
hualing chencc91e1c2020-02-28 13:26:17 +0800363 switch (evt) {
364 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800365 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800366 break;
367 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
368 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800369 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800370 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800371 break;
372 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
373 break;
374 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
375 break;
376 case DVR_PLAYBACK_EVENT_NO_KEY:
377 break;
378 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800379 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800380 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800381 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800382 break;
383 case DVR_PLAYBACK_EVENT_REACHED_END:
384 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800385 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800386 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800387 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800388 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800389 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800390 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800391 default:
392 break;
393 }
394 if (player->openParams.event_fn != NULL)
395 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800396 return DVR_SUCCESS;
397}
hualing chen2932d372020-04-29 13:44:00 +0800398static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800399{
400 DVR_Play_Notify_t notify;
401 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
402 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
403 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800404 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800405 return DVR_SUCCESS;
406}
407
hualing chen2932d372020-04-29 13:44:00 +0800408static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800409{
410 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800411
hualing chen4b7c15d2020-04-07 16:13:48 +0800412 if (1) {
413 return DVR_SUCCESS;
414 }
hualing chena540a7e2020-03-27 16:44:05 +0800415 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800416 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800417 return DVR_FAILURE;
418 }
419
hualing chen6e4bfa52020-03-13 14:37:11 +0800420 if (player->send_time ==0) {
421 player->send_time = _dvr_time_getClock() + 1000;
422 } else if (player->send_time > _dvr_time_getClock()) {
423 return DVR_SUCCESS;
424 }
425 player->send_time = _dvr_time_getClock() + 1000;
426 DVR_Play_Notify_t notify;
427 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
428 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
429 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800430 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800431 return DVR_SUCCESS;
432}
433
hualing chencc91e1c2020-02-28 13:26:17 +0800434//check is ongoing segment
435static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
436
437 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800438 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800439
440 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800441 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800442 return DVR_FAILURE;
443 }
hualing chen87072a82020-03-12 16:20:12 +0800444 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800445 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800446 return DVR_FALSE;
447 }
hualing chencc91e1c2020-02-28 13:26:17 +0800448 return DVR_TRUE;
449}
hualing chen4b7c15d2020-04-07 16:13:48 +0800450
451
452static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
453 DVR_Playback_t *player = (DVR_Playback_t *) handle;
454 player->fffb_start = _dvr_time_getClock();
455 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
456 player->fffb_current = player->fffb_start;
457 //get segment current time pos
458 player->fffb_start_pcr = _dvr_get_cur_time(handle);
459 //player->fffb_current = -1;
460 //player->fffb_start = -1;
461 //player->fffb_start_pcr = -1;
462 player->next_fffb_time = _dvr_time_getClock();
463
464 return DVR_SUCCESS;
465}
466
hualing chen2aba4022020-03-02 13:49:55 +0800467static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
468 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800469 player->fffb_start = _dvr_time_getClock();
470 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
471 player->fffb_current = player->fffb_start;
472 //get segment current time pos
473 player->fffb_start_pcr = _dvr_get_cur_time(handle);
474 //player->fffb_current = -1;
475 //player->fffb_start = -1;
476 //player->fffb_start_pcr = -1;
hualing chen2aba4022020-03-02 13:49:55 +0800477 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800478 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800479 return DVR_SUCCESS;
480}
hualing chencc91e1c2020-02-28 13:26:17 +0800481//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800482static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
483
484 DVR_Playback_t *player = (DVR_Playback_t *) handle;
485 DVR_PlaybackSegmentInfo_t *segment;
486 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
487
hualing chena540a7e2020-03-27 16:44:05 +0800488 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800489 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800490 return DVR_FAILURE;
491 }
492
hualing chen87072a82020-03-12 16:20:12 +0800493 int found = 0;
494 int found_eq_id = 0;
495 list_for_each_entry(segment, &player->segment_list, head)
496 {
497 if (player->segment_is_open == DVR_FALSE) {
498 //get first segment from list, case segment is not open
499 if (!IS_FB(player->speed))
500 found = 1;
501 } else if (segment->segment_id == segmentid) {
502 //find cur segment, we need get next one
503 found_eq_id = 1;
504 if (!IS_FB(player->speed)) {
505 found = 1;
506 continue;
507 } else {
508 //if is fb mode.we need used pre segment
509 if (pre_segment != NULL) {
510 found = 1;
511 } else {
512 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800513 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800514 return DVR_FAILURE;
515 }
516 }
517 }
518 if (found == 1) {
519 found = 2;
520 break;
521 }
522 }
523 if (found != 2) {
524 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800525 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800526 return DVR_FAILURE;
527 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800528 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800529 return DVR_SUCCESS;
530}
531
532//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800533static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800534
hualing chen040df222020-01-17 13:35:02 +0800535 DVR_Playback_t *player = (DVR_Playback_t *) handle;
536 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800537 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800538
hualing chena540a7e2020-03-27 16:44:05 +0800539 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800540 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800541 return DVR_FAILURE;
542 }
543
hualing chen86e7d482020-01-16 15:13:33 +0800544 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800545 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800546
hualing chen040df222020-01-17 13:35:02 +0800547 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800548 {
hualing chencc91e1c2020-02-28 13:26:17 +0800549 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800550 //get first segment from list, case segment is not open
551 if (!IS_FB(player->speed))
552 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800553 } else if (segment->segment_id == player->cur_segment_id) {
554 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800555 found_eq_id = 1;
556 if (!IS_FB(player->speed)) {
557 found = 1;
558 continue;
559 } else {
560 //if is fb mode.we need used pre segment
561 if (pre_segment != NULL) {
562 found = 1;
563 } else {
564 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800565 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800566 return DVR_FAILURE;
567 }
568 }
hualing chen86e7d482020-01-16 15:13:33 +0800569 }
570 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800571 if (IS_FB(player->speed)) {
572 //used pre segment
573 segment = pre_segment;
574 }
hualing chencc91e1c2020-02-28 13:26:17 +0800575 //save segment info
576 player->last_segment_id = player->cur_segment_id;
hualing chen87072a82020-03-12 16:20:12 +0800577 player->last_segment.segment_id = player->cur_segment.segment_id;
578 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800579 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
580 //pids
581 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
582
hualing chen5cbe1a62020-02-10 16:36:36 +0800583 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800584 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800585 player->cur_segment_id = segment->segment_id;
586 player->cur_segment.segment_id = segment->segment_id;
587 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800588 DVR_PB_DG(1, "set cur id cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, segment->flags, segment->segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800589 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800590 //pids
hualing chen040df222020-01-17 13:35:02 +0800591 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800592 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800593 break;
hualing chen86e7d482020-01-16 15:13:33 +0800594 }
hualing chen2aba4022020-03-02 13:49:55 +0800595 pre_segment = segment;
596 }
597 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
598 //used the last one segment to open
599 //get segment info
600 player->segment_is_open = DVR_TRUE;
601 player->cur_segment_id = pre_segment->segment_id;
602 player->cur_segment.segment_id = pre_segment->segment_id;
603 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800604 DVR_PB_DG(1, "set cur id fb last one cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, pre_segment->flags, pre_segment->segment_id);
hualing chen2aba4022020-03-02 13:49:55 +0800605 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
606 //pids
607 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
608 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800609 }
610 if (found != 2) {
611 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800612 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800613 }
614 return DVR_SUCCESS;
615}
hualing chen040df222020-01-17 13:35:02 +0800616//open next segment to play,if reach list end return errro.
617static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800618{
hualing chen040df222020-01-17 13:35:02 +0800619 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800620 Segment_OpenParams_t params;
621 int ret = DVR_SUCCESS;
622
hualing chena540a7e2020-03-27 16:44:05 +0800623 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800624 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800625 return DVR_FAILURE;
626 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800627 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800628
629 ret = _dvr_get_next_segmentId(handle);
630 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800631 DVR_PB_DG(1, "not found segment info");
632 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800633 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800634 }
635
636 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800637 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800638 segment_close(player->r_handle);
639 player->r_handle = NULL;
640 }
641
642 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800643 //cp chur segment path to location
644 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800645 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800646 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800647 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
648
hualing chen86e7d482020-01-16 15:13:33 +0800649 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800650 if (ret == DVR_FAILURE) {
651 DVR_PB_DG(1, "open segment error");
652 }
hualing chen87072a82020-03-12 16:20:12 +0800653 pthread_mutex_unlock(&player->segment_lock);
654 int total = _dvr_get_end_time( handle);
655 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800656 if (IS_FB(player->speed)) {
657 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800658 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800659 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800660 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800661 }
hualing chen87072a82020-03-12 16:20:12 +0800662 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800663 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800664 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800665 return ret;
666}
667
hualing chen5cbe1a62020-02-10 16:36:36 +0800668//open next segment to play,if reach list end return errro.
669static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
670{
671 DVR_Playback_t *player = (DVR_Playback_t *) handle;
672 Segment_OpenParams_t params;
673 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800674 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800675 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800676 return DVR_FAILURE;
677 }
hualing chencc91e1c2020-02-28 13:26:17 +0800678 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800679 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800680 }
hualing chencc91e1c2020-02-28 13:26:17 +0800681 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800682 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800683 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800684 return DVR_FAILURE;
685 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800686 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800687 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800688
689 DVR_PlaybackSegmentInfo_t *segment;
690
691 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800692
hualing chen5cbe1a62020-02-10 16:36:36 +0800693 list_for_each_entry(segment, &player->segment_list, head)
694 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800695 DVR_PB_DG(1, "see 1 location [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800696 if (segment->segment_id == segment_id) {
697 found = 1;
698 }
699 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800700 DVR_PB_DG(1, "found [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800701 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800702 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800703 player->cur_segment_id = segment->segment_id;
704 player->cur_segment.segment_id = segment->segment_id;
705 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800706 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800707 //pids
708 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800709 DVR_PB_DG(1, "cur found location [%s]id[%lld]flag[%x]", player->cur_segment.location, player->cur_segment.segment_id,player->cur_segment.flags);
hualing chencc91e1c2020-02-28 13:26:17 +0800710 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800711 }
712 }
hualing chencc91e1c2020-02-28 13:26:17 +0800713 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800714 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800715 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800716 return DVR_FAILURE;
717 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800718 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800719 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800720 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800721 params.segment_id = (uint64_t)player->cur_segment.segment_id;
722 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800723 DVR_PB_DG(1, "open segment location[%s][%lld]cur flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
hualing chen2aba4022020-03-02 13:49:55 +0800724 if (player->r_handle != NULL) {
725 segment_close(player->r_handle);
726 player->r_handle = NULL;
727 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800728 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800729 if (ret == DVR_FAILURE) {
730 DVR_PB_DG(1, "segment opne error");
731 }
hualing chen2aba4022020-03-02 13:49:55 +0800732 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800733 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800734
hualing chen4b7c15d2020-04-07 16:13:48 +0800735 DVR_PB_DG(1, "player->dur [%d]cur id [%lld]cur flag [0x%x]\r\n", player->dur,player->cur_segment.segment_id, player->cur_segment.flags);
hualing chen5cbe1a62020-02-10 16:36:36 +0800736 return ret;
737}
738
739
740//get play info by segment id
741static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
742 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800743 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800744 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800745
746 DVR_Playback_t *player = (DVR_Playback_t *) handle;
747 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800748 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800749 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800750 return DVR_FAILURE;
751 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800752
753 int found = 0;
754
755 list_for_each_entry(segment, &player->segment_list, head)
756 {
hualing chen87072a82020-03-12 16:20:12 +0800757 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800758 //get first segment from list
759 found = 1;
760 }
761 if (segment->segment_id == segment_id) {
762 found = 1;
763 }
764 if (found == 1) {
765 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800766 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800767 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800768 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800769 player->cur_segment.segment_id = segment->segment_id;
770 player->cur_segment.flags = segment->flags;
771 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800772 player->cur_segment.pids.video.pid = segment->pids.video.pid;
773 player->cur_segment.pids.video.format = segment->pids.video.format;
774 player->cur_segment.pids.video.type = segment->pids.video.type;
775 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
776 player->cur_segment.pids.audio.format = segment->pids.audio.format;
777 player->cur_segment.pids.audio.type = segment->pids.audio.type;
778 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
779 player->cur_segment.pids.ad.format = segment->pids.ad.format;
780 player->cur_segment.pids.ad.type = segment->pids.ad.type;
781 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800782 //
hualing chen2aba4022020-03-02 13:49:55 +0800783 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800784 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800785 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800786 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800787 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
788 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800789 DVR_PB_DG(1, "get play info sucess[0x%x]apid[0x%x]vfmt[%d]afmt[%d]", vparam->pid, aparam->pid, vparam->codectype, aparam->codectype);
hualing chen5cbe1a62020-02-10 16:36:36 +0800790 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800791 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800792 }
793 }
hualing chencc91e1c2020-02-28 13:26:17 +0800794 if (found != 2) {
795 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800796 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800797 return DVR_FAILURE;
798 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800799
800 return DVR_SUCCESS;
801}
hualing chencc91e1c2020-02-28 13:26:17 +0800802static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
803 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800804 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800805 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800806 return DVR_FAILURE;
807 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800808
hualing chencc91e1c2020-02-28 13:26:17 +0800809 //compare cur segment
810 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
811 {
812 //check video pids, stop or restart
813 _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids.video, 0);
814 //check audio pids stop or restart
hualing chen7a56cba2020-04-14 14:09:27 +0800815 DVR_PB_DG(1, ":last apid: %d set apid: %d", player->last_segment.pids.audio.pid,player->cur_segment.pids.audio.pid);
hualing chencc91e1c2020-02-28 13:26:17 +0800816 _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids.audio, 1);
817 //check sub audio pids stop or restart
818 _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids.ad, 2);
819 //check pcr pids stop or restart
820 _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids.pcr, 3);
821 }
hualing chena540a7e2020-03-27 16:44:05 +0800822 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800823}
hualing chen5cbe1a62020-02-10 16:36:36 +0800824
hualing chencc91e1c2020-02-28 13:26:17 +0800825static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
826{
827 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800828 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800829 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800830 return DVR_FAILURE;
831 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800832 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
833 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
834 return DVR_SUCCESS;
835 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800836 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]", player->cur_segment.flags, player->cur_segment.segment_id, player->last_segment.flags, player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800837 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
838 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800839 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800840 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800841 AmTsPlayer_showVideo(player->handle);
842 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800843 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
844 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800845 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800846 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800847 AmTsPlayer_hideVideo(player->handle);
848 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800849 }
850 return DVR_SUCCESS;
851}
hualing chena540a7e2020-03-27 16:44:05 +0800852static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
853 DVR_Playback_t *player = (DVR_Playback_t *) handle;
854 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800855 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800856 return DVR_TRUE;
857 }
hualing chene41f4372020-06-06 16:29:17 +0800858 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE || 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800859 if (player->first_frame == 1) {
860 return DVR_TRUE;
861 } else {
862 return DVR_FALSE;
863 }
864 } else {
865 return DVR_TRUE;
866 }
867}
hualing chen86e7d482020-01-16 15:13:33 +0800868static void* _dvr_playback_thread(void *arg)
869{
hualing chen040df222020-01-17 13:35:02 +0800870 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800871 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800872 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800873 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800874 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800875
hualing chen39628212020-05-14 10:35:13 +0800876 #define MAX_REACHEND_TIMEOUT (3000)
877 int reach_end_timeout = 0;//ms
878 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800879 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800880 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800881 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800882 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800883 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
884
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800885 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800886 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800887 DVR_Bool_t goto_rewrite = DVR_FALSE;
hualing chene41f4372020-06-06 16:29:17 +0800888 int retry_open_seg = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800889 if (player->is_secure_mode) {
890 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800891 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800892 return NULL;
893 }
894 }
hualing chen86e7d482020-01-16 15:13:33 +0800895 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800896 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800897 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800898 return NULL;
899 }
hualing chen2aba4022020-03-02 13:49:55 +0800900 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
901 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800902
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800903 dec_bufs.buf_data = malloc(dec_buf_size);
904 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800905 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800906 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800907 return NULL;
908 }
909 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
910 dec_bufs.buf_size = dec_buf_size;
911
hualing chencc91e1c2020-02-28 13:26:17 +0800912 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800913 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
914 }
hualing chen86e7d482020-01-16 15:13:33 +0800915
hualing chen86e7d482020-01-16 15:13:33 +0800916 if (ret != DVR_SUCCESS) {
917 if (buf != NULL) {
918 free(buf);
919 buf = NULL;
920 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800921 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +0800922 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +0800923 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800924 }
hualing chen4fe3bee2020-10-23 13:58:52 +0800925 DVR_PB_DG(1, "player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
926 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +0800927 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
928 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
929 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
930 player->first_trans_ok = DVR_TRUE;
931 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
932 }
hualing chencc91e1c2020-02-28 13:26:17 +0800933 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +0800934 //set video show
935 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +0800936
hualing chen86e7d482020-01-16 15:13:33 +0800937 int trick_stat = 0;
938 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +0800939
hualing chen86e7d482020-01-16 15:13:33 +0800940 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +0800941 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800942
hualing chen2aba4022020-03-02 13:49:55 +0800943 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
944 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +0800945 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +0800946 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +0800947 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +0800948 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +0800949 {
hualing chen2aba4022020-03-02 13:49:55 +0800950 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
951 if (trick_stat > 0) {
hualing chenbcada022020-04-22 14:27:01 +0800952 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]", player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +0800953 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK || (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen2aba4022020-03-02 13:49:55 +0800954 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +0800955 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +0800956 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +0800957 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
958 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +0800959 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
960 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chenbcada022020-04-22 14:27:01 +0800961 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]", player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +0800962 //need change to pause state
963 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
964 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +0800965 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +0800966 //clear flag
hualing chen31140872020-03-25 12:29:26 +0800967 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +0800968 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800969 AmTsPlayer_pauseVideoDecoding(player->handle);
970 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +0800971 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800972 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +0800973 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800974 }
975 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
976 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +0800977 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +0800978 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +0800979 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
980 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +0800981 //used timeout wait need lock first,so we unlock and lock
982 //pthread_mutex_unlock(&player->lock);
983 //pthread_mutex_lock(&player->lock);
984 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
985 pthread_mutex_unlock(&player->lock);
986 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +0800987 } else if (_dvr_time_getClock() < player->next_fffb_time) {
988 DVR_PB_DG(1, "fffb timeout-to pause video---speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
989 //used timeout wait need lock first,so we unlock and lock
990 //pthread_mutex_unlock(&player->lock);
991 //pthread_mutex_lock(&player->lock);
992 AmTsPlayer_pauseVideoDecoding(player->handle);
993 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
994 pthread_mutex_unlock(&player->lock);
995 continue;
996
hualing chen2aba4022020-03-02 13:49:55 +0800997 }
hualing chen2932d372020-04-29 13:44:00 +0800998 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]", player->speed, goto_rewrite, real_read, _dvr_playback_state_toString(player->state), player->cmd);
hualing chen2aba4022020-03-02 13:49:55 +0800999 pthread_mutex_unlock(&player->lock);
1000 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001001 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001002 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1003 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001004 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001005 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001006 pthread_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001007 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001008 }else if (player->fffb_play == DVR_TRUE){
1009 //for first into fffb when reset speed
1010 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1011 _dvr_time_getClock() < player->next_fffb_time) {
1012 DVR_PB_DG(1, "fffb timeout-fffb play---speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
1013 //used timeout wait need lock first,so we unlock and lock
1014 //pthread_mutex_unlock(&player->lock);
1015 //pthread_mutex_lock(&player->lock);
1016 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1017 pthread_mutex_unlock(&player->lock);
1018 continue;
1019 }
hualing chen2932d372020-04-29 13:44:00 +08001020 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]", player->speed, goto_rewrite, real_read, _dvr_playback_state_toString(player->state), player->cmd, player->fffb_play);
hualing chen4b7c15d2020-04-07 16:13:48 +08001021 pthread_mutex_unlock(&player->lock);
1022 goto_rewrite = DVR_FALSE;
1023 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001024 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001025 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1026 player->first_frame = 0;
1027 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1028 pthread_mutex_lock(&player->lock);
1029 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001030 }
hualing chenb31a6c62020-01-13 17:27:00 +08001031 }
hualing chen86e7d482020-01-16 15:13:33 +08001032
hualing chen87072a82020-03-12 16:20:12 +08001033 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001034 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001035 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001036 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001037 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1038 pthread_mutex_unlock(&player->lock);
1039 continue;
1040 }
hualing chen266b9502020-04-04 17:39:39 +08001041 //when seek action is done. we need drop write timeout data.
1042 if (player->drop_ts == DVR_TRUE) {
1043 goto_rewrite = DVR_FALSE;
1044 real_read = 0;
1045 player->drop_ts = DVR_FALSE;
1046 }
hualing chen2aba4022020-03-02 13:49:55 +08001047 if (goto_rewrite == DVR_TRUE) {
1048 goto_rewrite = DVR_FALSE;
1049 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001050 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001051 goto rewrite;
1052 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001053 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001054 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001055 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001056 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001057 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chenfbf8e022020-06-15 13:43:11 +08001058 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001059 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001060 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001061 if (read < 0 && errno == EIO) {
1062 //EIO ERROR, EXIT THRAD
1063 DVR_PB_DG(1, "read error.EIO error, exit thread");
1064 DVR_Play_Notify_t notify;
1065 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1066 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001067 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001068 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001069 goto end;
1070 } else if (read < 0) {
1071 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1072 }
hualing chen87072a82020-03-12 16:20:12 +08001073 //if on fb mode and read file end , we need calculate pos to retry read.
1074 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001075 DVR_PB_DG(1, "recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]", read,real_read, buf_len, player->speed,player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001076 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1077 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001078 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1079 pthread_mutex_unlock(&player->lock);
1080 continue;
1081 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001082 //DVR_PB_DG(1, "read ts [%d]buf_len[%d]speed[%f]real_read:%d", read, buf_len, player->speed, real_read);
hualing chen86e7d482020-01-16 15:13:33 +08001083 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001084 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001085 #define MIN_CACHE_TIME (3000)
1086 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
1087 if (_cache_time > MIN_CACHE_TIME) {
1088 DVR_PB_DG(1, "read end but cache time is %d > %d, to sleep and continue", _cache_time, MIN_CACHE_TIME);
1089 pthread_mutex_lock(&player->lock);
1090 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, (_cache_time -MIN_CACHE_TIME));
1091 pthread_mutex_unlock(&player->lock);
1092 //continue;
1093 }
hualing chen040df222020-01-17 13:35:02 +08001094 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001095 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001096 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001097
1098 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001099 //del delay time max check.
hualing chen39628212020-05-14 10:35:13 +08001100 if ((ret != DVR_SUCCESS &&
hualing chen041c4092020-04-05 15:11:50 +08001101 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001102 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001103 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1104 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001105 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001106 DVR_Play_Notify_t notify;
1107 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1108 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1109 //get play statue not here
1110 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001111 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001112 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001113 DVR_PB_DG(1, "playback is send end delay:[%d]reach_end_timeout[%d]ms", delay, reach_end_timeout);
hualing chen31140872020-03-25 12:29:26 +08001114 pthread_mutex_lock(&player->lock);
1115 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1116 pthread_mutex_unlock(&player->lock);
1117 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001118 } else if (ret != DVR_SUCCESS) {
1119 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001120 if (delay < cache_time) {
1121 //delay time is changed and then has data to play, so not start timeout
1122 } else {
1123 reach_end_timeout = reach_end_timeout + timeout;
1124 }
1125 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001126 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001127 pthread_mutex_lock(&player->lock);
1128 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1129 pthread_mutex_unlock(&player->lock);
1130 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001131 }
hualing chen39628212020-05-14 10:35:13 +08001132 reach_end_timeout = 0;
1133 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001134 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001135 //change next segment success case
1136 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001137 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001138 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1139 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001140 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001141 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001142 }
hualing chen39628212020-05-14 10:35:13 +08001143 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001144 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001145 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001146 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001147
hualing chena540a7e2020-03-27 16:44:05 +08001148 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001149 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001150 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001151 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001152 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001153 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001154 }
hualing chen266b9502020-04-04 17:39:39 +08001155 //if need write whole block size, we need check read buf len is eq block size.
1156 if (b_writed_whole_block == DVR_TRUE) {
1157 //buf_len is block size value.
1158 if (real_read < buf_len) {
1159 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001160 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001161 pthread_mutex_lock(&player->lock);
1162 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1163 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001164 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001165 continue;
1166 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001167 DVR_PB_DG(1, "read buf len[%d] is > block size [%d],this error occur", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001168 }
1169 }
1170
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001171 if (player->dec_func) {
1172 DVR_CryptoParams_t crypto_params;
1173
1174 memset(&crypto_params, 0, sizeof(crypto_params));
1175 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1176 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1177 crypto_params.segment_id = player->cur_segment.segment_id;
1178 crypto_params.offset = segment_tell_position(player->r_handle);
hualing chenbafc62d2020-11-02 15:44:05 +08001179 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1180 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001181 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1182 crypto_params.input_buffer.addr = (size_t)buf;
1183 crypto_params.input_buffer.size = real_read;
1184
1185 if (player->is_secure_mode) {
1186 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1187 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1188 crypto_params.output_buffer.size = dec_buf_size;
1189 ret = player->dec_func(&crypto_params, player->dec_userdata);
1190 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001191 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001192 } else {
1193 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1194 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1195 crypto_params.output_buffer.size = dec_buf_size;
1196 ret = player->dec_func(&crypto_params, player->dec_userdata);
1197 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001198 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001199 }
1200 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001201 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001202 }
pengfei.liufda2a972020-04-09 14:47:15 +08001203 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001204 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001205rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001206 if (player->drop_ts == DVR_TRUE) {
1207 //need drop ts data when seek occur.we need read next loop,drop this ts data
1208 goto_rewrite = DVR_FALSE;
1209 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001210 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001211 player->drop_ts = DVR_FALSE;
1212 continue;
1213 }
hualing chen5605eed2020-05-26 18:18:06 +08001214 player->ts_cache_len = real_read;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001215 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1216 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001217 player->ts_cache_len = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001218 real_read = 0;
1219 write_success++;
hualing chen5605eed2020-05-26 18:18:06 +08001220 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001221 continue;
hualing chen87072a82020-03-12 16:20:12 +08001222 } else {
hualing chen2932d372020-04-29 13:44:00 +08001223 DVR_PB_DG(1, "write time out write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001224 write_success = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001225 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001226 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001227 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001228 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001229 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001230 break;
1231 }
hualing chen2aba4022020-03-02 13:49:55 +08001232 goto_rewrite = DVR_TRUE;
1233 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001234 }
1235 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001236end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001237 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001238 free(buf);
1239 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001240 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001241}
1242
1243
hualing chen040df222020-01-17 13:35:02 +08001244static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001245{
hualing chen040df222020-01-17 13:35:02 +08001246 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001247
1248 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001249 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001250 return DVR_FAILURE;
1251 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001252 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001253 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001254 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001255 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001256 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001257 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001258 if (rc < 0)
1259 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001260 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001261}
1262
1263
hualing chen040df222020-01-17 13:35:02 +08001264static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001265{
hualing chen040df222020-01-17 13:35:02 +08001266 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001267
1268 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001269 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001270 return DVR_FAILURE;
1271 }
1272
hualing chen4b7c15d2020-04-07 16:13:48 +08001273 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001274 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001275 {
1276 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001277 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001278 pthread_join(player->playback_thread, NULL);
1279 }
1280 if (player->r_handle) {
1281 segment_close(player->r_handle);
1282 player->r_handle = NULL;
1283 }
hualing chen7a56cba2020-04-14 14:09:27 +08001284 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001285 return 0;
1286}
1287
hualing chenb31a6c62020-01-13 17:27:00 +08001288/**\brief Open an dvr palyback
1289 * \param[out] p_handle dvr playback addr
1290 * \param[in] params dvr playback open parameters
1291 * \retval DVR_SUCCESS On success
1292 * \return Error code
1293 */
hualing chen040df222020-01-17 13:35:02 +08001294int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001295
hualing chen040df222020-01-17 13:35:02 +08001296 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001297 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001298
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001299 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001300
hualing chen86e7d482020-01-16 15:13:33 +08001301 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001302 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001303 pthread_condattr_init(&cattr);
1304 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1305 pthread_cond_init(&player->cond, &cattr);
1306 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001307
hualing chen5cbe1a62020-02-10 16:36:36 +08001308 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001309 INIT_LIST_HEAD(&player->segment_list);
1310 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1311 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001312 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001313 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001314 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001315 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001316 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001317 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001318
hualing chen86e7d482020-01-16 15:13:33 +08001319 //store open params
hualing chen040df222020-01-17 13:35:02 +08001320 player->openParams.dmx_dev_id = params->dmx_dev_id;
1321 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001322 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001323 player->openParams.event_fn = params->event_fn;
1324 player->openParams.event_userdata = params->event_userdata;
hualing chenfbf8e022020-06-15 13:43:11 +08001325 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001326
hualing chen5cbe1a62020-02-10 16:36:36 +08001327 player->has_pids = params->has_pids;
1328
hualing chen2aba4022020-03-02 13:49:55 +08001329 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001330
1331 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1332 //for test get callback
1333 if (0 && player->player_callback_func == NULL) {
1334 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1335 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +08001336 DVR_PB_DG(1, "playback open get callback[%p][%p][%p][%p]",player->player_callback_func, player->player_callback_userdata, _dvr_tsplayer_callback_test,player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001337 }
1338 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001339
hualing chen86e7d482020-01-16 15:13:33 +08001340 //init has audio and video
1341 player->has_video = DVR_FALSE;
1342 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001343 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001344 player->last_segment_id = 0LL;
1345 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001346
hualing chen5cbe1a62020-02-10 16:36:36 +08001347 //init ff fb time
1348 player->fffb_current = -1;
1349 player->fffb_start =-1;
1350 player->fffb_start_pcr = -1;
1351 //seek time
1352 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001353 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001354
1355 //init secure stuff
1356 player->dec_func = NULL;
1357 player->dec_userdata = NULL;
1358 player->is_secure_mode = 0;
1359 player->secure_buffer = NULL;
1360 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001361 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001362
hualing chen4b7c15d2020-04-07 16:13:48 +08001363 player->fffb_play = DVR_FALSE;
1364
1365 player->last_send_time_id = UINT64_MAX;
1366 player->last_cur_time = 0;
1367
hualing chen86e7d482020-01-16 15:13:33 +08001368 *p_handle = player;
1369 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001370}
1371
1372/**\brief Close an dvr palyback
1373 * \param[in] handle playback handle
1374 * \retval DVR_SUCCESS On success
1375 * \return Error code
1376 */
hualing chen040df222020-01-17 13:35:02 +08001377int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001378
hualing chen86e7d482020-01-16 15:13:33 +08001379 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001380 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001381 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001382 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001383 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001384 return DVR_FAILURE;
1385 }
1386
hualing chencc91e1c2020-02-28 13:26:17 +08001387 if (player->state != DVR_PLAYBACK_STATE_STOP)
1388 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001389 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001390 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001391 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1392 } else {
1393 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001394 }
hualing chen7a56cba2020-04-14 14:09:27 +08001395 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001396 pthread_mutex_destroy(&player->lock);
1397 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001398
1399 if (player) {
1400 free(player);
1401 player = NULL;
1402 }
hualing chen7a56cba2020-04-14 14:09:27 +08001403 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001404 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001405}
1406
hualing chenb31a6c62020-01-13 17:27:00 +08001407/**\brief Start play audio and video, used start auido api and start video api
1408 * \param[in] handle playback handle
1409 * \param[in] params audio playback params,contains fmt and pid...
1410 * \retval DVR_SUCCESS On success
1411 * \return Error code
1412 */
hualing chen040df222020-01-17 13:35:02 +08001413int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1414 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001415 am_tsplayer_video_params vparams;
1416 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001417 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001418
1419 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001420 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001421 return DVR_FAILURE;
1422 }
hualing chencc91e1c2020-02-28 13:26:17 +08001423 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001424 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001425
hualing chena540a7e2020-03-27 16:44:05 +08001426 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001427 //can used start api to resume playback
1428 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1429 return dvr_playback_resume(handle);
1430 }
hualing chen87072a82020-03-12 16:20:12 +08001431 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001432 //if flag is puased and not decodec first frame. if user resume, we need
1433 //clear flag and set trickmode none
1434 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1435 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1436 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1437 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1438 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001439 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001440 return DVR_SUCCESS;
1441 }
hualing chen86e7d482020-01-16 15:13:33 +08001442 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001443 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001444 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001445 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001446 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001447 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001448 //start audio and video
1449 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
1450 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001451 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001452 pthread_mutex_unlock(&player->lock);
1453 DVR_Play_Notify_t notify;
1454 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1455 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1456 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1457 notify.info.transition_failed_data.segment_id = segment_id;
1458 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001459 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001460 return -1;
1461 }
hualing chen31140872020-03-25 12:29:26 +08001462
hualing chencc91e1c2020-02-28 13:26:17 +08001463 {
hualing chen86e7d482020-01-16 15:13:33 +08001464 if (VALID_PID(vparams.pid)) {
1465 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001466 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001467 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001468 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001469 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1470 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001471 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001472 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001473 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001474 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001475 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001476 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001477 }
1478 AmTsPlayer_setVideoParams(player->handle, &vparams);
1479 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001480 }
hualing chena540a7e2020-03-27 16:44:05 +08001481
hualing chen4b7c15d2020-04-07 16:13:48 +08001482 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1483 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001484 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1485 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1486 player->cmd.state = DVR_PLAYBACK_STATE_START;
1487 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001488 } else {
1489 player->cmd.last_cmd = player->cmd.cur_cmd;
1490 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001491 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001492 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001493 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001494 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001495 } else {
1496 if (VALID_PID(aparams.pid)) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001497 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08001498 player->has_audio = DVR_TRUE;
1499 AmTsPlayer_setAudioParams(player->handle, &aparams);
1500 AmTsPlayer_startAudioDecoding(player->handle);
1501 }
hualing chendf118dd2020-05-21 15:49:11 +08001502 if (VALID_PID(adparams.pid)) {
1503 player->has_ad_audio = DVR_TRUE;
1504 DVR_PB_DG(1, "start ad audio");
1505 AmTsPlayer_setADParams(player->handle, &adparams);
1506 AmTsPlayer_enableADMix(player->handle);
1507 }
hualing chen31140872020-03-25 12:29:26 +08001508 }
hualing chencc91e1c2020-02-28 13:26:17 +08001509 player->cmd.state = DVR_PLAYBACK_STATE_START;
1510 player->state = DVR_PLAYBACK_STATE_START;
1511 }
hualing chen86e7d482020-01-16 15:13:33 +08001512 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001513 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001514 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001515 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001516 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001517}
hualing chen040df222020-01-17 13:35:02 +08001518/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001519 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001520 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001521 * \retval DVR_SUCCESS On success
1522 * \return Error code
1523 */
hualing chen040df222020-01-17 13:35:02 +08001524int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1525 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001526
hualing chena540a7e2020-03-27 16:44:05 +08001527 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001528 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001529 return DVR_FAILURE;
1530 }
1531
hualing chen4b7c15d2020-04-07 16:13:48 +08001532 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001533 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001534
hualing chen040df222020-01-17 13:35:02 +08001535 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1536 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001537
hualing chen86e7d482020-01-16 15:13:33 +08001538 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001539 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001540 //cp location
hualing chen040df222020-01-17 13:35:02 +08001541 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001542
hualing chen4b7c15d2020-04-07 16:13:48 +08001543 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 +08001544 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001545
1546 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001547 segment->pids.video.pid = info->pids.video.pid;
1548 segment->pids.video.format = info->pids.video.format;
1549 segment->pids.video.type = info->pids.video.type;
1550
hualing chen2aba4022020-03-02 13:49:55 +08001551 segment->pids.audio.pid = info->pids.audio.pid;
1552 segment->pids.audio.format = info->pids.audio.format;
1553 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001554
hualing chen2aba4022020-03-02 13:49:55 +08001555 segment->pids.ad.pid = info->pids.ad.pid;
1556 segment->pids.ad.format = info->pids.ad.format;
1557 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001558
1559 segment->pids.pcr.pid = info->pids.pcr.pid;
1560
hualing chen4b7c15d2020-04-07 16:13:48 +08001561 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 +08001562 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001563 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001564 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001565 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001566
hualing chen5cbe1a62020-02-10 16:36:36 +08001567 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001568}
hualing chen040df222020-01-17 13:35:02 +08001569/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001570 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001571 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001572 * \retval DVR_SUCCESS On success
1573 * \return Error code
1574 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001575int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001576 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001577 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001578 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001579 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001580 return DVR_FAILURE;
1581 }
1582
hualing chencc91e1c2020-02-28 13:26:17 +08001583 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001584 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001585 return DVR_FAILURE;
1586 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001587 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001588 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001589 DVR_PlaybackSegmentInfo_t *segment = NULL;
1590 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1591 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001592 {
hualing chen040df222020-01-17 13:35:02 +08001593 if (segment->segment_id == segment_id) {
1594 list_del(&segment->head);
1595 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001596 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001597 }
hualing chen86e7d482020-01-16 15:13:33 +08001598 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001599 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001600 pthread_mutex_unlock(&player->lock);
1601
1602 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001603}
hualing chen040df222020-01-17 13:35:02 +08001604/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001605 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001606 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001607 * \retval DVR_SUCCESS On success
1608 * \return Error code
1609 */
hualing chen040df222020-01-17 13:35:02 +08001610int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001611 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001612 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001613 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001614 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001615 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001616 return DVR_FAILURE;
1617 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001618 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1619 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1620 return DVR_SUCCESS;
1621 }
hualing chena540a7e2020-03-27 16:44:05 +08001622
hualing chen040df222020-01-17 13:35:02 +08001623 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001624 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001625 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001626 list_for_each_entry(segment, &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) {
hualing chen86e7d482020-01-16 15:13:33 +08001629 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001630 }
hualing chen86e7d482020-01-16 15:13:33 +08001631 // if encramble to free, only set flag and return;
1632
1633 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001634 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001635 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1636 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001637 //disable display, mute
hualing chen2aba4022020-03-02 13:49:55 +08001638 AmTsPlayer_hideVideo(player->handle);
1639 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001640 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1641 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001642 //enable display, unmute
hualing chen2aba4022020-03-02 13:49:55 +08001643 AmTsPlayer_showVideo(player->handle);
1644 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001645 } else {
1646 //do nothing
1647 }
1648 } else {
1649 //do nothing
1650 }
1651 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001652 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001653 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001654 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001655 pthread_mutex_unlock(&player->lock);
1656 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001657}
1658
1659
hualing chen5cbe1a62020-02-10 16:36:36 +08001660static 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 +08001661 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001662 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001663 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001664 return DVR_FAILURE;
1665 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001666 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001667 if (now_pid.pid == set_pid.pid) {
1668 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001669 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001670 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001671 if (VALID_PID(now_pid.pid)) {
1672 //stop now stream
1673 if (type == 0) {
1674 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001675 if (player->has_video == DVR_TRUE) {
1676 DVR_PB_DG(1, "stop video");
1677 AmTsPlayer_stopVideoDecoding(player->handle);
1678 player->has_video = DVR_FALSE;
1679 }
hualing chen86e7d482020-01-16 15:13:33 +08001680 } else if (type == 1) {
1681 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001682 if (player->has_audio == DVR_TRUE) {
1683 DVR_PB_DG(1, "stop audio");
1684 AmTsPlayer_stopAudioDecoding(player->handle);
1685 player->has_audio = DVR_FALSE;
1686 }
hualing chen86e7d482020-01-16 15:13:33 +08001687 } else if (type == 2) {
1688 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001689 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001690 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001691 } else if (type == 3) {
1692 //pcr
1693 }
1694 }
1695 if (VALID_PID(set_pid.pid)) {
1696 //start
1697 if (type == 0) {
1698 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001699 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001700 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001701 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001702 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001703 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001704 AmTsPlayer_setVideoParams(player->handle, &vparams);
1705 AmTsPlayer_startVideoDecoding(player->handle);
1706 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001707 } else if (type == 1) {
1708 //start audio
hualing chenc70a8df2020-05-12 19:23:11 +08001709 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
1710 am_tsplayer_audio_params aparams;
1711 aparams.pid = set_pid.pid;
1712 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1713 player->has_audio = DVR_TRUE;
1714 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1715 AmTsPlayer_setAudioParams(player->handle, &aparams);
1716 AmTsPlayer_startAudioDecoding(player->handle);
1717 //playback_device_audio_start(player->handle,&aparams);
1718 }
hualing chen86e7d482020-01-16 15:13:33 +08001719 } else if (type == 2) {
hualing chen39628212020-05-14 10:35:13 +08001720 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chenc70a8df2020-05-12 19:23:11 +08001721 am_tsplayer_audio_params aparams;
1722 aparams.pid = set_pid.pid;
1723 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1724 player->has_audio = DVR_TRUE;
1725 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1726 AmTsPlayer_setADParams(player->handle, &aparams);
1727 AmTsPlayer_enableADMix(player->handle);
1728 //playback_device_audio_start(player->handle,&aparams);
1729 }
hualing chen86e7d482020-01-16 15:13:33 +08001730 } else if (type == 3) {
1731 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001732 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001733 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001734 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001735 //audio and video all close
1736 if (!player->has_audio && !player->has_video) {
1737 player->state = DVR_PLAYBACK_STATE_STOP;
1738 }
hualing chen86e7d482020-01-16 15:13:33 +08001739 }
1740 }
1741 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001742}
hualing chen5cbe1a62020-02-10 16:36:36 +08001743/**\brief dvr play back update segment pids
1744 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001745 * add pid stream and stop remove pid stream.
1746 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001747 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001748 * \retval DVR_SUCCESS On success
1749 * \return Error code
1750 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001751int 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 +08001752 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001753 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001754 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001755 return DVR_FAILURE;
1756 }
1757
hualing chen040df222020-01-17 13:35:02 +08001758 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001759 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001760 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001761 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 +08001762
hualing chen040df222020-01-17 13:35:02 +08001763 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001764 {
hualing chen040df222020-01-17 13:35:02 +08001765 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001766
1767 if (player->cur_segment_id == segment_id) {
1768 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
1769 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
1770 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08001771 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08001772 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001773 return 0;
1774 }
1775
1776 //if segment is on going segment,we need stop start stream
1777 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08001778 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001779 //check video pids, stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001780 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, p_pids->video, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08001781 //check audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001782 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, p_pids->audio, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001783 //check sub audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001784 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, p_pids->ad, 2);
hualing chen5cbe1a62020-02-10 16:36:36 +08001785 //check pcr pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001786 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, p_pids->pcr, 3);
1787 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001788 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1789 //if state is pause, we need process at resume api. we only record change info
1790 int v_cmd = DVR_PLAYBACK_CMD_NONE;
1791 int a_cmd = DVR_PLAYBACK_CMD_NONE;
1792 if (VALID_PID(segment->pids.video.pid)
1793 && VALID_PID(p_pids->video.pid)
1794 && segment->pids.video.pid != p_pids->video.pid) {
1795 //restart video
1796 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
1797 }
1798 if (!VALID_PID(segment->pids.video.pid)
1799 && VALID_PID(p_pids->video.pid)
1800 && segment->pids.video.pid != p_pids->video.pid) {
1801 //start video
1802 v_cmd = DVR_PLAYBACK_CMD_VSTART;
1803 }
1804 if (VALID_PID(segment->pids.video.pid)
1805 && !VALID_PID(p_pids->video.pid)
1806 && segment->pids.video.pid != p_pids->video.pid) {
1807 //stop video
1808 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
1809 }
1810 if (VALID_PID(segment->pids.audio.pid)
1811 && VALID_PID(p_pids->audio.pid)
1812 && segment->pids.audio.pid != p_pids->audio.pid) {
1813 //restart audio
1814 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
1815 }
1816 if (!VALID_PID(segment->pids.audio.pid)
1817 && VALID_PID(p_pids->audio.pid)
1818 && segment->pids.audio.pid != p_pids->audio.pid) {
1819 //start audio
1820 a_cmd = DVR_PLAYBACK_CMD_ASTART;
1821 }
1822 if (VALID_PID(segment->pids.audio.pid)
1823 && !VALID_PID(p_pids->audio.pid)
1824 && segment->pids.audio.pid != p_pids->audio.pid) {
1825 //stop audio
1826 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
1827 }
1828 if (a_cmd == DVR_PLAYBACK_CMD_NONE
1829 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
1830 //do nothing
1831 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
1832 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
1833 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1834 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
1835 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
1836 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
1837 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1838 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1839 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1840 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
1841 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1842 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1843 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1844 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
1845 } else {
1846 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1847 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
1848 }
1849
1850 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1851 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1852 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1853 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
1854 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1855 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1856 //not occur this case
1857 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1858 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
1859 } else {
1860 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1861 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
1862 }
1863
1864 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1865 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1866 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1867 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
1868 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1869 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
1870 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1871 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
1872 } else {
1873 //not occur this case
1874 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1875 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1876 }
1877 }
1878 }
hualing chene10666f2020-04-14 13:58:37 +08001879 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08001880 }
hualing chen86e7d482020-01-16 15:13:33 +08001881 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08001882 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08001883 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08001884 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001885 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001886 }
hualing chen86e7d482020-01-16 15:13:33 +08001887 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001888 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001889 pthread_mutex_unlock(&player->lock);
1890 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001891}
1892/**\brief Stop play, will stop video and audio
1893 * \param[in] handle playback handle
1894 * \param[in] clear is clear last frame
1895 * \retval DVR_SUCCESS On success
1896 * \return Error code
1897 */
hualing chen040df222020-01-17 13:35:02 +08001898int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
1899 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001900
1901 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001902 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001903 return DVR_FAILURE;
1904 }
hualing chenb96aa2c2020-04-15 14:13:53 +08001905 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1906 DVR_PB_DG(1, ":playback is stoped");
1907 return DVR_SUCCESS;
1908 }
Ke Gong3c0caba2020-04-21 22:58:18 -07001909 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1910 DVR_PB_DG(1, ":playback is stoped");
1911 return DVR_SUCCESS;
1912 }
hualing chen87072a82020-03-12 16:20:12 +08001913 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001914 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001915 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08001916 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08001917 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001918 if (player->has_video) {
1919 AmTsPlayer_resumeVideoDecoding(player->handle);
1920 }
1921 if (player->has_audio) {
1922 AmTsPlayer_resumeAudioDecoding(player->handle);
1923 }
1924 if (player->has_video) {
1925 player->has_video = DVR_FALSE;
1926 AmTsPlayer_showVideo(player->handle);
1927 AmTsPlayer_stopVideoDecoding(player->handle);
1928 }
1929 if (player->has_audio) {
1930 player->has_audio = DVR_FALSE;
1931 AmTsPlayer_stopAudioDecoding(player->handle);
1932 }
hualing chendf118dd2020-05-21 15:49:11 +08001933 if (player->has_ad_audio) {
1934 player->has_ad_audio =DVR_FALSE;
1935 AmTsPlayer_disableADMix(player->handle);
1936 }
hualing chen266b9502020-04-04 17:39:39 +08001937
hualing chen86e7d482020-01-16 15:13:33 +08001938 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001939 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1940 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1941 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08001942 player->cur_segment_id = UINT64_MAX;
1943 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001944 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08001945 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08001946 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001947 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001948}
1949/**\brief Start play audio
1950 * \param[in] handle playback handle
1951 * \param[in] params audio playback params,contains fmt and pid...
1952 * \retval DVR_SUCCESS On success
1953 * \return Error code
1954 */
hualing chen2aba4022020-03-02 13:49:55 +08001955
hualing chendf118dd2020-05-21 15:49:11 +08001956int 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 +08001957 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001958
1959 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001960 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001961 return DVR_FAILURE;
1962 }
hualing chen86e7d482020-01-16 15:13:33 +08001963 _start_playback_thread(handle);
1964 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08001965 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001966 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001967
1968 if (VALID_PID(param->pid)) {
1969 DVR_PB_DG(1, "start audio");
1970 player->has_audio = DVR_TRUE;
1971 AmTsPlayer_setAudioParams(player->handle, param);
1972 AmTsPlayer_startAudioDecoding(player->handle);
1973 }
1974 if (VALID_PID(adparam->pid)) {
1975 player->has_ad_audio = DVR_TRUE;
1976 DVR_PB_DG(1, "start ad audio");
1977 AmTsPlayer_setADParams(player->handle, adparam);
1978 AmTsPlayer_enableADMix(player->handle);
1979 }
1980
hualing chen86e7d482020-01-16 15:13:33 +08001981 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001982 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
1983 player->cmd.state = DVR_PLAYBACK_STATE_START;
1984 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08001985 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001986 pthread_mutex_unlock(&player->lock);
1987 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001988}
1989/**\brief Stop play audio
1990 * \param[in] handle playback handle
1991 * \retval DVR_SUCCESS On success
1992 * \return Error code
1993 */
hualing chen040df222020-01-17 13:35:02 +08001994int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
1995 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001996
1997 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001998 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001999 return DVR_FAILURE;
2000 }
2001
hualing chen2aba4022020-03-02 13:49:55 +08002002 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002003 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002004 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2005 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002006 //destory thread
2007 _stop_playback_thread(handle);
2008 } else {
2009 //do nothing.video is playing
2010 }
hualing chen7a56cba2020-04-14 14:09:27 +08002011 DVR_PB_DG(1, "lock");
2012 pthread_mutex_lock(&player->lock);
2013
hualing chenf00cdc82020-06-10 14:23:35 +08002014 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002015 player->has_audio = DVR_FALSE;
2016 AmTsPlayer_stopAudioDecoding(player->handle);
2017 }
hualing chen87072a82020-03-12 16:20:12 +08002018
hualing chendf118dd2020-05-21 15:49:11 +08002019 if (player->has_ad_audio) {
2020 player->has_ad_audio =DVR_FALSE;
2021 AmTsPlayer_disableADMix(player->handle);
2022 }
2023
hualing chen87072a82020-03-12 16:20:12 +08002024 player->cmd.last_cmd = player->cmd.cur_cmd;
2025 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2026
hualing chen4b7c15d2020-04-07 16:13:48 +08002027 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002028 pthread_mutex_unlock(&player->lock);
2029 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002030}
2031/**\brief Start play video
2032 * \param[in] handle playback handle
2033 * \param[in] params video playback params,contains fmt and pid...
2034 * \retval DVR_SUCCESS On success
2035 * \return Error code
2036 */
hualing chen2aba4022020-03-02 13:49:55 +08002037int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002038 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002039
2040 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002041 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002042 return DVR_FAILURE;
2043 }
2044
hualing chen86e7d482020-01-16 15:13:33 +08002045 _start_playback_thread(handle);
2046 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002047 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002048 pthread_mutex_lock(&player->lock);
2049 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002050 AmTsPlayer_setVideoParams(player->handle, param);
2051 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002052
2053 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002054 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002055 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002056 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002057 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2058 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002059 }
2060 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002061 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2062 player->cmd.state = DVR_PLAYBACK_STATE_START;
2063 player->state = DVR_PLAYBACK_STATE_START;
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);
hualing chenb31a6c62020-01-13 17:27:00 +08002066 return DVR_SUCCESS;
2067}
2068/**\brief Stop play video
2069 * \param[in] handle playback handle
2070 * \retval DVR_SUCCESS On success
2071 * \return Error code
2072 */
hualing chen040df222020-01-17 13:35:02 +08002073int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2074 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002075
2076 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002077 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002078 return DVR_FAILURE;
2079 }
2080
hualing chen86e7d482020-01-16 15:13:33 +08002081 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002082 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2083 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002084 //destory thread
2085 _stop_playback_thread(handle);
2086 } else {
2087 //do nothing.audio is playing
2088 }
hualing chen7a56cba2020-04-14 14:09:27 +08002089
2090 DVR_PB_DG(1, "lock");
2091 pthread_mutex_lock(&player->lock);
2092
hualing chen87072a82020-03-12 16:20:12 +08002093 player->has_video = DVR_FALSE;
2094
2095 AmTsPlayer_stopVideoDecoding(player->handle);
2096 //playback_device_video_stop(player->handle);
2097
2098 player->cmd.last_cmd = player->cmd.cur_cmd;
2099 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2100
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 Pause play
2106 * \param[in] handle playback handle
2107 * \param[in] flush whether its internal buffers should be flushed
2108 * \retval DVR_SUCCESS On success
2109 * \return Error code
2110 */
hualing chen040df222020-01-17 13:35:02 +08002111int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2112 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002113
2114 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002115 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002116 return DVR_FAILURE;
2117 }
hualing chenf00cdc82020-06-10 14:23:35 +08002118 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2119 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002120 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002121 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002122 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002123 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002124 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002125 if (player->has_video)
2126 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002127 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002128 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002129
2130 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002131 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2132 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2133 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2134 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002135 } else {
2136 player->cmd.last_cmd = player->cmd.cur_cmd;
2137 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2138 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2139 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002140 }
hualing chen86e7d482020-01-16 15:13:33 +08002141 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002142 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002143
hualing chen86e7d482020-01-16 15:13:33 +08002144 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002145}
2146
hualing chen5cbe1a62020-02-10 16:36:36 +08002147//not add lock
2148static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2149{
2150 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2151
hualing chena540a7e2020-03-27 16:44:05 +08002152 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002153 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002154 return DVR_FAILURE;
2155 }
2156
hualing chen5cbe1a62020-02-10 16:36:36 +08002157 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002158 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002159 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002160 am_tsplayer_video_params vparams;
2161 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002162 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002163 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002164
hualing chendf118dd2020-05-21 15:49:11 +08002165 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002166 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002167 pthread_mutex_unlock(&player->lock);
2168
2169 switch (cmd) {
2170 case DVR_PLAYBACK_CMD_AVRESTART:
2171 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002172 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002173 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002174 break;
2175 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002176 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2177 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002178 break;
2179 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002180 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002181 break;
2182 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002183 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002184 break;
2185 case DVR_PLAYBACK_CMD_ARESTART:
2186 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002187 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002188 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002189 break;
2190 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002191 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002192 break;
2193 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002194 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002195 break;
2196 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002197 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2198 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2199 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002200 break;
2201 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002202 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2203 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002204 break;
2205 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002206 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2207 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002208 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002209 break;
2210 case DVR_PLAYBACK_CMD_STOP:
2211 break;
2212 case DVR_PLAYBACK_CMD_START:
2213 break;
2214 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002215 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2216 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002217 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002218 break;
2219 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002220 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2221 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002222 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002223 break;
2224 case DVR_PLAYBACK_CMD_FF:
2225 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002226 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002227 break;
2228 default:
2229 break;
2230 }
2231 return DVR_SUCCESS;
2232}
2233
2234/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002235 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002236 * \retval DVR_SUCCESS On success
2237 * \return Error code
2238 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002239int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2240 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2241
hualing chena540a7e2020-03-27 16:44:05 +08002242 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002243 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002244 return DVR_FAILURE;
2245 }
2246
hualing chen5cbe1a62020-02-10 16:36:36 +08002247 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002248 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002249 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002250 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002251 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002252 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2253 AmTsPlayer_resumeVideoDecoding(player->handle);
2254 }
2255 if (player->has_audio) {
2256 AmTsPlayer_resumeAudioDecoding(player->handle);
2257 }
2258 //check is has audio param,if has audio .we need start audio,
2259 //we will stop audio when ff fb, if reach end, we will pause.so we need
2260 //start audio when resume play
2261
2262 am_tsplayer_video_params vparams;
2263 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002264 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002265 uint64_t segmentid = player->cur_segment_id;
hualing chendf118dd2020-05-21 15:49:11 +08002266 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002267 //valid audio pid, start audio
hualing chenc70a8df2020-05-12 19:23:11 +08002268 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 +08002269 player->has_audio = DVR_TRUE;
2270 AmTsPlayer_setAudioParams(player->handle, &aparams);
2271 AmTsPlayer_startAudioDecoding(player->handle);
2272 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002273 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 +08002274 }
hualing chendf118dd2020-05-21 15:49:11 +08002275
2276 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2277 player->has_ad_audio = DVR_TRUE;
2278 DVR_PB_DG(1, "start ad audio");
2279 AmTsPlayer_setADParams(player->handle, &adparams);
2280 AmTsPlayer_enableADMix(player->handle);
2281 }
2282
hualing chen87072a82020-03-12 16:20:12 +08002283 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2284 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2285 player->cmd.state = DVR_PLAYBACK_STATE_START;
2286 player->state = DVR_PLAYBACK_STATE_START;
2287 } else {
2288 player->cmd.last_cmd = player->cmd.cur_cmd;
2289 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2290 player->cmd.state = DVR_PLAYBACK_STATE_START;
2291 player->state = DVR_PLAYBACK_STATE_START;
2292 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002293 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002294 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002295 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chene41f4372020-06-06 16:29:17 +08002296 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002297 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002298 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002299 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002300 }
hualing chen041c4092020-04-05 15:11:50 +08002301 if (player->has_audio)
2302 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002303 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002304 player->cmd.state = DVR_PLAYBACK_STATE_START;
2305 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002306 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2307 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002308 } else {
2309 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2310 {
2311 pthread_mutex_lock(&player->lock);
2312 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002313 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002314 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2315 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2316 pthread_mutex_unlock(&player->lock);
2317 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002318 }
2319 return DVR_SUCCESS;
2320}
2321
hualing chena540a7e2020-03-27 16:44:05 +08002322static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2323
2324 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2325 DVR_PlaybackSegmentInfo_t *segment = NULL;
2326 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2327 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2328
2329 list_for_each_entry(segment, &player->segment_list, head)
2330 {
2331 if (segment->segment_id == segment_id) {
2332 cur_segment = segment;
2333 }
2334 if (segment->segment_id == set_seg_id) {
2335 set_segment = segment;
2336 }
2337 if (cur_segment != NULL && set_segment != NULL) {
2338 break;
2339 }
2340 }
2341 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002342 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002343 return DVR_TRUE;
2344 }
2345 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2346 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2347 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2348 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002349 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 +08002350 return DVR_TRUE;
2351 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002352 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002353 return DVR_FALSE;
2354}
2355
hualing chen5cbe1a62020-02-10 16:36:36 +08002356/**\brief seek
2357 * \param[in] handle playback handle
2358 * \param[in] time_offset time offset base cur segment
2359 * \retval DVR_SUCCESS On success
2360 * \return Error code
2361 */
hualing chencc91e1c2020-02-28 13:26:17 +08002362int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002363 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002364
2365 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002366 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002367 return DVR_FAILURE;
2368 }
2369
hualing chen4b7c15d2020-04-07 16:13:48 +08002370 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 +08002371 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002372
hualing chen86e7d482020-01-16 15:13:33 +08002373 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002374 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002375 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002376
hualing chen5cbe1a62020-02-10 16:36:36 +08002377 //open segment if id is not current segment
hualing chen87072a82020-03-12 16:20:12 +08002378 int ret = _dvr_open_segment(handle, segment_id);
2379 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002380 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002381 pthread_mutex_unlock(&player->lock);
2382 return DVR_FAILURE;
2383 }
2384 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2385 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002386 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002387 //pthread_mutex_unlock(&player->lock);
2388 //return DVR_SUCCESS;
2389 time_offset = _dvr_get_end_time(handle);
2390 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002391 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002392 time_offset = _dvr_get_end_time(handle);
hualing chen87072a82020-03-12 16:20:12 +08002393 pthread_mutex_unlock(&player->lock);
2394 return DVR_FAILURE;
2395 }
2396 }
2397
hualing chen4b7c15d2020-04-07 16:13:48 +08002398 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 +08002399 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002400 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2401 //forward playback.not seek end of file
2402 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2403 //default -2000ms
2404 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2405 }
hualing chen86e7d482020-01-16 15:13:33 +08002406 }
hualing chen2aba4022020-03-02 13:49:55 +08002407 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002408 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002409 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002410 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002411 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 +08002412 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002413 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002414
hualing chen2aba4022020-03-02 13:49:55 +08002415 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002416
2417 player->last_send_time_id = UINT64_MAX;
2418
hualing chen2aba4022020-03-02 13:49:55 +08002419 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002420 player->fffb_current = _dvr_time_getClock();
2421 player->fffb_start = player->fffb_current;
2422 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2423 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002424 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002425 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002426 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002427 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002428 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002429 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002430 }
hualing chen86e7d482020-01-16 15:13:33 +08002431 //stop play
hualing chen4b7c15d2020-04-07 16:13:48 +08002432 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 +08002433 if (player->has_video) {
2434 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002435 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002436 }
2437
2438 if (player->has_audio) {
2439 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002440 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002441 }
hualing chendf118dd2020-05-21 15:49:11 +08002442 if (player->has_ad_audio) {
2443 player->has_ad_audio =DVR_FALSE;
2444 AmTsPlayer_disableADMix(player->handle);
2445 }
2446
hualing chen86e7d482020-01-16 15:13:33 +08002447 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002448 am_tsplayer_video_params vparams;
2449 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002450 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002451
hualing chen040df222020-01-17 13:35:02 +08002452 player->cur_segment_id = segment_id;
2453
2454 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002455 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002456 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002457 //start audio and video
2458 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2459 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002460 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 +08002461 pthread_mutex_unlock(&player->lock);
2462 return -1;
2463 }
2464 //add
hualing chen040df222020-01-17 13:35:02 +08002465 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002466 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002467 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002468 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002469 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002470 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002471 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002472 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002473 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002474 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002475 }
hualing chen2aba4022020-03-02 13:49:55 +08002476 AmTsPlayer_setVideoParams(player->handle, &vparams);
2477 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002478 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2479 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2480 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2481 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2482 AmTsPlayer_stopFast(player->handle);
2483 }
hualing chen266b9502020-04-04 17:39:39 +08002484 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002485 }
hualing chene41f4372020-06-06 16:29:17 +08002486 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002487 DVR_PB_DG(1, "start audio seek");
hualing chen2aba4022020-03-02 13:49:55 +08002488 AmTsPlayer_setAudioParams(player->handle, &aparams);
2489 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002490 player->has_audio = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002491 }
hualing chene41f4372020-06-06 16:29:17 +08002492 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002493 player->has_ad_audio = DVR_TRUE;
2494 DVR_PB_DG(1, "start ad audio");
2495 AmTsPlayer_setADParams(player->handle, &adparams);
2496 AmTsPlayer_enableADMix(player->handle);
2497 }
hualing chen86e7d482020-01-16 15:13:33 +08002498 }
hualing chene41f4372020-06-06 16:29:17 +08002499 if (player->state == DVR_PLAYBACK_STATE_PAUSE /*&&
hualing chen2aba4022020-03-02 13:49:55 +08002500 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2501 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2502 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chene41f4372020-06-06 16:29:17 +08002503 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))*/) {
hualing chen2aba4022020-03-02 13:49:55 +08002504 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2505 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002506 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002507 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2508 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002509 player->speed > 1.0f||
2510 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002511 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002512 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002513 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002514 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002515 player->cmd.last_cmd = player->cmd.cur_cmd;
2516 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2517 player->cmd.state = DVR_PLAYBACK_STATE_START;
2518 player->state = DVR_PLAYBACK_STATE_START;
2519 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002520 player->last_send_time_id = UINT64_MAX;
2521 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002522 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002523
2524 return DVR_SUCCESS;
2525}
hualing chen5cbe1a62020-02-10 16:36:36 +08002526
hualing chen5cbe1a62020-02-10 16:36:36 +08002527static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2528 //get cur time of segment
2529 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002530
2531 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002532 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002533 return DVR_FAILURE;
2534 }
2535
hualing chen31140872020-03-25 12:29:26 +08002536 int64_t cache = 0;//defalut es buf cache 500ms
2537 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002538 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002539 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2540 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002541 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002542 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 +08002543 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2544 cache = 0;
2545 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002546 int cur_time = (int)(cur > cache ? cur - cache : 0);
2547 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002548}
2549
2550//get current segment current pcr time of read pos
2551static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2552 //get cur time of segment
2553 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002554
2555 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002556 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002557 return DVR_FAILURE;
2558 }
2559
hualing chen2aba4022020-03-02 13:49:55 +08002560 pthread_mutex_lock(&player->segment_lock);
2561 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002562 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002563 pthread_mutex_unlock(&player->segment_lock);
2564 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002565}
2566
hualing chen4b7c15d2020-04-07 16:13:48 +08002567#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08002568//start replay
2569static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
2570
2571 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2572 //calculate pcr seek time
2573 int t_diff = 0;
2574 int seek_time = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002575
2576 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002577 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002578 return DVR_FAILURE;
2579 }
2580
hualing chen5cbe1a62020-02-10 16:36:36 +08002581 if (player->fffb_start == -1) {
2582 //set fffb start time ms
2583 player->fffb_start = _dvr_time_getClock();
2584 player->fffb_current = player->fffb_start;
2585 //get segment current time pos
2586 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002587 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 +08002588 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08002589 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08002590 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002591 } else {
2592 player->fffb_current = _dvr_time_getClock();
2593 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08002594 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08002595 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08002596 if (seek_time <= 0) {
2597 //need seek to pre one segment
2598 seek_time = 0;
2599 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002600 //seek segment pos
2601 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08002602 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002603 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08002604 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2605 //set seek time to 0;
2606 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]", seek_time, player->cur_segment_id);
2607 seek_time = 0;
2608 }
hualing chen041c4092020-04-05 15:11:50 +08002609 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
2610 seek_time = 0;
2611 }
hualing chen2aba4022020-03-02 13:49:55 +08002612 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002613 } else {
2614 //
hualing chen4b7c15d2020-04-07 16:13:48 +08002615 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08002616 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002617 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 +08002618 }
hualing chen2aba4022020-03-02 13:49:55 +08002619 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08002620}
2621
2622
2623//start replay
2624static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
2625 //
2626 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002627
2628 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002629 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002630 return DVR_FAILURE;
2631 }
2632
hualing chen5cbe1a62020-02-10 16:36:36 +08002633 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002634 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002635 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08002636 AmTsPlayer_stopVideoDecoding(player->handle);
2637 }
2638 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002639 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08002640 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002641 AmTsPlayer_stopAudioDecoding(player->handle);
2642 }
hualing chendf118dd2020-05-21 15:49:11 +08002643 if (player->has_ad_audio) {
2644 DVR_PB_DG(1, "fffb stop audio");
2645 player->has_ad_audio =DVR_FALSE;
2646 AmTsPlayer_disableADMix(player->handle);
2647 }
hualing chen2aba4022020-03-02 13:49:55 +08002648
hualing chen5cbe1a62020-02-10 16:36:36 +08002649 //start video and audio
2650
hualing chen2aba4022020-03-02 13:49:55 +08002651 am_tsplayer_video_params vparams;
2652 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002653 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002654 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002655
2656 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08002657 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002658 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002659 //start audio and video
2660 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2661 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002662 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08002663 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002664 return -1;
2665 }
2666
2667 if (VALID_PID(vparams.pid)) {
2668 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002669 DVR_PB_DG(1, "fffb start video");
hualing chen31140872020-03-25 12:29:26 +08002670 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08002671 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2672 AmTsPlayer_setVideoParams(player->handle, &vparams);
2673 AmTsPlayer_startVideoDecoding(player->handle);
2674 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002675 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08002676 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002677 }
hualing chen31140872020-03-25 12:29:26 +08002678 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08002679 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002680 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08002681 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002682 return 0;
2683}
2684
2685static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
2686 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002687 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002688 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002689 return DVR_FAILURE;
2690 }
2691
2692 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08002693 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002694 pthread_mutex_lock(&player->lock);
2695
hualing chen2aba4022020-03-02 13:49:55 +08002696 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002697 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 +08002698
hualing chen87072a82020-03-12 16:20:12 +08002699 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2700 //seek time set 0
2701 seek_time = 0;
2702 }
hualing chen041c4092020-04-05 15:11:50 +08002703 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08002704 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
2705 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08002706 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002707 pthread_mutex_unlock(&player->lock);
2708 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002709 //send event here and pause
2710 DVR_Play_Notify_t notify;
2711 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08002712 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08002713 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08002714 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08002715 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 +08002716 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08002717 return DVR_SUCCESS;
2718 }
hualing chen2932d372020-04-29 13:44:00 +08002719 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002720 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002721 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002722 }
2723 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002724 _dvr_playback_fffb_replay(handle);
2725
2726 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002727 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002728
hualing chen5cbe1a62020-02-10 16:36:36 +08002729 return DVR_SUCCESS;
2730}
2731
hualing chen87072a82020-03-12 16:20:12 +08002732//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08002733static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002734 //
2735 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002736
2737 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002738 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002739 return DVR_FAILURE;
2740 }
2741
hualing chen5cbe1a62020-02-10 16:36:36 +08002742 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002743 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08002744 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002745 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002746 }
2747
2748 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08002749 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002750 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002751 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002752 //start video and audio
2753
hualing chen2aba4022020-03-02 13:49:55 +08002754 am_tsplayer_video_params vparams;
2755 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002756 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08002757 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002758
2759 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08002760 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08002761 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002762 //start audio and video
2763 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08002764 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002765 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08002766 return -1;
2767 }
2768
2769 if (VALID_PID(vparams.pid)) {
2770 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002771 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002772 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08002773 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08002774 }
hualing chen266b9502020-04-04 17:39:39 +08002775 else {
hualing chen2aba4022020-03-02 13:49:55 +08002776 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08002777 }
hualing chen2aba4022020-03-02 13:49:55 +08002778 AmTsPlayer_setVideoParams(player->handle, &vparams);
2779 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08002780 }
hualing chena540a7e2020-03-27 16:44:05 +08002781
2782 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08002783 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08002784 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002785 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08002786 } else {
hualing chena540a7e2020-03-27 16:44:05 +08002787 if (VALID_PID(aparams.pid)) {
2788 player->has_audio = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002789 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08002790 AmTsPlayer_startAudioDecoding(player->handle);
2791 AmTsPlayer_setAudioParams(player->handle, &aparams);
hualing chena540a7e2020-03-27 16:44:05 +08002792 }
hualing chendf118dd2020-05-21 15:49:11 +08002793 if (VALID_PID(adparams.pid)) {
2794 player->has_ad_audio = DVR_TRUE;
2795 DVR_PB_DG(1, "start ad audio");
2796 AmTsPlayer_setADParams(player->handle, &adparams);
2797 AmTsPlayer_enableADMix(player->handle);
2798 }
2799
hualing chen7a56cba2020-04-14 14:09:27 +08002800 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002801 AmTsPlayer_stopFast(player->handle);
2802 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
2803 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
2804 }
hualing chen2aba4022020-03-02 13:49:55 +08002805 player->cmd.last_cmd = player->cmd.cur_cmd;
2806 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08002807 player->cmd.state = DVR_PLAYBACK_STATE_START;
2808 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002809 return 0;
2810}
2811
2812
hualing chenb31a6c62020-01-13 17:27:00 +08002813/**\brief Set play speed
2814 * \param[in] handle playback handle
2815 * \param[in] speed playback speed
2816 * \retval DVR_SUCCESS On success
2817 * \return Error code
2818 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002819int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
2820
2821 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002822
2823 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002824 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002825 return DVR_FAILURE;
2826 }
2827
hualing chen4b7c15d2020-04-07 16:13:48 +08002828 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002829 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002830 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002831 return DVR_FAILURE;
2832 }
hualing chenf00cdc82020-06-10 14:23:35 +08002833 if (speed.speed.speed == player->cmd.speed.speed.speed) {
2834 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
2835 return DVR_SUCCESS;
2836 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002837 pthread_mutex_lock(&player->lock);
2838 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
2839 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
2840 player->cmd.last_cmd = player->cmd.cur_cmd;
2841 }
hualing chene41f4372020-06-06 16:29:17 +08002842
hualing chen31140872020-03-25 12:29:26 +08002843 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08002844 IS_KERNEL_SPEED(speed.speed.speed) ) {
2845 //case 1. not start play.only set speed
2846 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2847 //only set speed.and return;
2848 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
2849 player->cmd.speed.speed = speed.speed;
2850 player->speed = (float)speed.speed.speed/(float)100;
2851 player->fffb_play = DVR_FALSE;
2852 pthread_mutex_unlock(&player->lock);
2853 return DVR_SUCCESS;
2854 }
2855 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08002856 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
2857 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002858 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002859 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2860 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002861 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002862 AmTsPlayer_stopFast(player->handle);
2863 pthread_mutex_unlock(&player->lock);
2864 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
2865 pthread_mutex_lock(&player->lock);
2866 } else {
2867 //set play speed and if audio is start, stop audio.
2868 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002869 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002870 AmTsPlayer_stopAudioDecoding(player->handle);
2871 player->has_audio = DVR_FALSE;
2872 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002873 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002874 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002875 }
hualing chenbcada022020-04-22 14:27:01 +08002876 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08002877 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002878 player->cmd.speed.speed = speed.speed;
2879 player->speed = (float)speed.speed.speed/(float)100;
2880 pthread_mutex_unlock(&player->lock);
2881 return DVR_SUCCESS;
2882 }
hualing chen31140872020-03-25 12:29:26 +08002883 //case 3 fffb mode
2884 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2885 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2886 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002887 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002888 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002889 player->cmd.speed.speed = speed.speed;
2890 player->speed = (float)speed.speed.speed/(float)100;
2891 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08002892 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002893 pthread_mutex_unlock(&player->lock);
2894 return DVR_SUCCESS;
2895 }
2896 }
2897 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08002898 IS_KERNEL_SPEED(speed.speed.speed)) {
2899 //case 1. cur speed is kernel support speed,set kernel speed.
2900 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08002901 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002902 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2903 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002904 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002905 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08002906 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08002907 } else {
2908 //set play speed and if audio is start, stop audio.
2909 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002910 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002911 AmTsPlayer_stopAudioDecoding(player->handle);
2912 player->has_audio = DVR_FALSE;
2913 }
hualing chenf00cdc82020-06-10 14:23:35 +08002914 DVR_PB_DG(1, "start fast");
2915 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08002916 }
hualing chena540a7e2020-03-27 16:44:05 +08002917 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002918 player->cmd.speed.speed = speed.speed;
2919 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08002920 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002921 pthread_mutex_unlock(&player->lock);
2922 return DVR_SUCCESS;
2923 }
2924 //case 2 fffb mode
2925 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2926 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2927 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002928 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002929 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002930 player->cmd.speed.speed = speed.speed;
2931 player->speed = (float)speed.speed.speed/(float)100;
2932 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08002933 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002934 pthread_mutex_unlock(&player->lock);
2935 return DVR_SUCCESS;
2936 }
hualing chen31140872020-03-25 12:29:26 +08002937 }
hualing chena540a7e2020-03-27 16:44:05 +08002938 if (IS_KERNEL_SPEED(speed.speed.speed)) {
2939 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08002940 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08002941 } else {
hualing chen31140872020-03-25 12:29:26 +08002942 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08002943 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
2944 else
2945 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08002946 player->fffb_play = DVR_TRUE;
2947 }
2948 DVR_Bool_t init_last_time = DVR_FALSE;
2949 if (player->speed > 0.0f && speed.speed.speed < 0) {
2950 init_last_time = DVR_TRUE;
2951 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
2952 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002953 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002954 player->cmd.speed.mode = speed.mode;
2955 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08002956 player->speed = (float)speed.speed.speed/(float)100;
2957 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08002958 _dvr_init_fffb_t(handle);
2959 if (init_last_time == DVR_TRUE)
2960 player->last_send_time_id = UINT64_MAX;
2961
hualing chen87072a82020-03-12 16:20:12 +08002962 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08002963 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2964 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08002965 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002966 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08002967 _dvr_playback_replay(handle, DVR_FALSE);
2968 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
2969 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
2970 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08002971 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08002972 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002973 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002974 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002975 return DVR_SUCCESS;
2976}
hualing chen2932d372020-04-29 13:44:00 +08002977
hualing chenb31a6c62020-01-13 17:27:00 +08002978/**\brief Get playback status
2979 * \param[in] handle playback handle
2980 * \param[out] p_status playback status
2981 * \retval DVR_SUCCESS On success
2982 * \return Error code
2983 */
hualing chen2932d372020-04-29 13:44:00 +08002984static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
2985 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002986//
2987 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002988
2989 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002990 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002991 return DVR_FAILURE;
2992 }
hualing chen2932d372020-04-29 13:44:00 +08002993 if (is_lock ==DVR_TRUE)
2994 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002995 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08002996 //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 +08002997 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
2998 player->state == DVR_PLAYBACK_STATE_START) {
2999 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3000 }
hualing chen041c4092020-04-05 15:11:50 +08003001
hualing chencc91e1c2020-02-28 13:26:17 +08003002 p_status->time_end = _dvr_get_end_time(handle);
hualing chen2aba4022020-03-02 13:49:55 +08003003 p_status->time_cur = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003004 if (player->last_send_time_id == UINT64_MAX) {
3005 player->last_send_time_id = player->cur_segment_id;
3006 player->last_cur_time = p_status->time_cur;
3007 }
3008 if (player->last_send_time_id == player->cur_segment_id) {
3009 if (player->speed > 0.0f ) {
3010 //ff
3011 if (p_status->time_cur < player->last_cur_time ) {
3012 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);
3013 p_status->time_cur = player->last_cur_time;
3014 } else {
3015 player->last_cur_time = p_status->time_cur;
3016 }
hualing chene41f4372020-06-06 16:29:17 +08003017 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003018 //fb
3019 if (p_status->time_cur > player->last_cur_time ) {
3020 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 );
3021 p_status->time_cur = player->last_cur_time;
3022 } else {
3023 player->last_cur_time = p_status->time_cur;
3024 }
3025 }
3026 } else {
3027 player->last_cur_time = p_status->time_cur;
3028 }
3029 player->last_send_time_id = player->cur_segment_id;
hualing chen041c4092020-04-05 15:11:50 +08003030 p_status->segment_id = player->cur_segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003031
hualing chen5cbe1a62020-02-10 16:36:36 +08003032 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003033 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003034 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003035 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 +08003036 _dvr_playback_state_toString(player->state),
3037 _dvr_playback_state_toString(p_status->state),
hualing chena540a7e2020-03-27 16:44:05 +08003038 p_status->time_cur, p_status->time_end,
3039 p_status->segment_id,player->play_flag,
hualing chen2932d372020-04-29 13:44:00 +08003040 player->speed,
3041 is_lock);
3042 if (is_lock ==DVR_TRUE)
3043 pthread_mutex_unlock(&player->lock);
3044 return DVR_SUCCESS;
3045}
3046
3047
3048/**\brief Get playback status
3049 * \param[in] handle playback handle
3050 * \param[out] p_status playback status
3051 * \retval DVR_SUCCESS On success
3052 * \return Error code
3053 */
3054int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3055 DVR_PlaybackStatus_t *p_status) {
3056//
3057 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3058
3059 if (player == NULL) {
3060 DVR_PB_DG(1, "player is NULL");
3061 return DVR_FAILURE;
3062 }
3063 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3064
hualing chenb31a6c62020-01-13 17:27:00 +08003065 return DVR_SUCCESS;
3066}
3067
hualing chen040df222020-01-17 13:35:02 +08003068void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3069 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003070 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3071 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3072 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3073 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3074 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3075 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3076 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 +08003077 }
hualing chenb31a6c62020-01-13 17:27:00 +08003078}
3079
hualing chen5cbe1a62020-02-10 16:36:36 +08003080int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003081 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003082
hualing chena540a7e2020-03-27 16:44:05 +08003083 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003084 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003085 return DVR_FAILURE;
3086 }
3087
hualing chen040df222020-01-17 13:35:02 +08003088 DVR_PlaybackSegmentInfo_t *segment;
3089 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003090 {
hualing chen040df222020-01-17 13:35:02 +08003091 if (segment_id >= 0) {
3092 if (segment->segment_id == segment_id) {
3093 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003094 break;
3095 }
3096 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003097 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003098 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003099 }
3100 }
3101 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003102}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003103
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003104int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003105{
3106 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3107 DVR_RETURN_IF_FALSE(player);
3108 DVR_RETURN_IF_FALSE(func);
3109
hualing chen4b7c15d2020-04-07 16:13:48 +08003110 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003111 pthread_mutex_lock(&player->lock);
3112
3113 player->dec_func = func;
3114 player->dec_userdata = userdata;
3115
3116 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003117 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003118 return DVR_SUCCESS;
3119}
3120
3121int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3122{
3123 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3124 DVR_RETURN_IF_FALSE(player);
3125 DVR_RETURN_IF_FALSE(p_secure_buf);
3126 DVR_RETURN_IF_FALSE(len);
3127
hualing chen4b7c15d2020-04-07 16:13:48 +08003128 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003129 pthread_mutex_lock(&player->lock);
3130
3131 player->is_secure_mode = 1;
3132 player->secure_buffer = p_secure_buf;
3133 player->secure_buffer_size = len;
3134
3135 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003136 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003137 return DVR_SUCCESS;
3138}