blob: 0d37b5f76e56bed320052d755fd0bd0c9d5fe89a [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 chen03fd4942021-07-15 15:56:41 +080015#include "dvr_utils.h"
16#include "dvr_types.h"
hualing chenb31a6c62020-01-13 17:27:00 +080017#include "dvr_playback.h"
Yahui Han1fbf3292021-11-08 18:17:19 +080018#include "am_crypt.h"
hualing chenb31a6c62020-01-13 17:27:00 +080019
Wentao MA96f68962022-06-15 19:45:35 +080020#define PB_LOG_TAG "libdvr-playback"
21#define DVR_PB_DEBUG(...) DVR_LOG_PRINT(LOG_LV_DEBUG, PB_LOG_TAG, __VA_ARGS__)
22#define DVR_PB_INFO(...) DVR_LOG_PRINT(LOG_LV_INFO, PB_LOG_TAG, __VA_ARGS__)
23#define DVR_PB_WARN(...) DVR_LOG_PRINT(LOG_LV_WARN, PB_LOG_TAG, __VA_ARGS__)
24#define DVR_PB_ERROR(...) DVR_LOG_PRINT(LOG_LV_ERROR, PB_LOG_TAG, __VA_ARGS__)
25#define DVR_PB_FATAL(...) DVR_LOG_PRINT(LOG_LV_FATAL, PB_LOG_TAG, __VA_ARGS__)
hualing chena540a7e2020-03-27 16:44:05 +080026
hualing chenb31a6c62020-01-13 17:27:00 +080027#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080028
hualing chend241c7a2021-06-22 13:34:27 +080029#define CONTROL_SPEED_ENABLE 0
hualing chena540a7e2020-03-27 16:44:05 +080030
31#define FF_SPEED (2.0f)
32#define FB_SPEED (-1.0f)
33#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080034#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080035
36#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))
37#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
38
Wentao MA907b6432022-08-01 06:23:08 +000039#define DVR_PLAYER_CHANGE_STATE(player,newstate)\
40 DVR_PB_INFO("%s:%d player %p changes state from %s to %s",__func__,__LINE__,\
41 player,_dvr_playback_state_toString(player->state),_dvr_playback_state_toString(newstate));\
42 player->state=newstate;
43
hualing chenb31a6c62020-01-13 17:27:00 +080044
hualing chenb5cd42e2020-04-15 17:03:34 +080045#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080046#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080047//if tsplayer delay time < 200 and no data can read, we will pause
48#define MIN_TSPLAYER_DELAY_TIME (200)
49
hualing chen041c4092020-04-05 15:11:50 +080050#define MAX_CACHE_TIME (30000)
hualing chen43a89bc2022-01-19 14:31:20 +080051//used pcr to control avsync,default not used
52//#define AVSYNC_USED_PCR 1
hualing chena540a7e2020-03-27 16:44:05 +080053static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080054//
55static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chen99508642021-10-18 15:41:17 +080056static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_PlaybackPids_t now_pids, DVR_PlaybackPids_t pids, int type);
hualing chencc91e1c2020-02-28 13:26:17 +080057static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
58static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080059static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080060static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080061static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
62 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080063static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen7ea70a72021-09-09 11:25:13 +080064static uint32_t dvr_playback_calculate_last_valid_segment(
65 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos);
hualing chen87072a82020-03-12 16:20:12 +080066
hualing chenbcada022020-04-22 14:27:01 +080067
hualing chena5f03222021-12-02 11:22:35 +080068
hualing chenbcada022020-04-22 14:27:01 +080069static char* _cmd_toString(int cmd)
70{
71
72 char *string[DVR_PLAYBACK_CMD_NONE+1]={
73 "start",
74 "stop",
Wentao MA270dc0f2022-08-23 13:17:26 +080075 "v_start",
76 "a_start",
77 "v_stop",
78 "a_stop",
79 "v_restart",
80 "a_restart",
81 "av_restart",
82 "v_stop_a_start",
83 "a_stop_v_start",
84 "v_stop_a_restart",
85 "a_stop_v_restart",
86 "v_start_a_restart",
87 "a_start_v_restart",
hualing chenbcada022020-04-22 14:27:01 +080088 "pause",
89 "resume",
90 "seek",
91 "ff",
92 "fb",
93 "NONE"
94 };
95
96 if (cmd > DVR_PLAYBACK_CMD_NONE) {
Wentao MA270dc0f2022-08-23 13:17:26 +080097 return "unknown";
hualing chenbcada022020-04-22 14:27:01 +080098 } else {
99 return string[cmd];
100 }
101}
102
103
Wentao MA907b6432022-08-01 06:23:08 +0000104static char* _dvr_playback_state_toString(int state)
hualing chen6d24aa92020-03-23 18:43:47 +0800105{
Wentao MA907b6432022-08-01 06:23:08 +0000106 char *strings[5]={
107 "START",
108 "STOP",
109 "PAUSE",
110 "FF",
111 "FB",
hualing chen6d24aa92020-03-23 18:43:47 +0800112 };
113
Wentao MA907b6432022-08-01 06:23:08 +0000114 if (state >= 5 || state < 0) {
115 return "UNKNOWN";
hualing chen6d24aa92020-03-23 18:43:47 +0800116 }
Wentao MA907b6432022-08-01 06:23:08 +0000117 return strings[state];
hualing chen6d24aa92020-03-23 18:43:47 +0800118}
hualing chena540a7e2020-03-27 16:44:05 +0800119
120static DVR_Bool_t _dvr_support_speed(int speed) {
121
122 DVR_Bool_t ret = DVR_FALSE;
123
124 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800125 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800126 case PLAYBACK_SPEED_FBX2:
127 case PLAYBACK_SPEED_FBX4:
128 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800129 case PLAYBACK_SPEED_FBX16:
130 case PLAYBACK_SPEED_FBX12:
131 case PLAYBACK_SPEED_FBX32:
132 case PLAYBACK_SPEED_FBX48:
133 case PLAYBACK_SPEED_FBX64:
134 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800135 case PLAYBACK_SPEED_S2:
136 case PLAYBACK_SPEED_S4:
137 case PLAYBACK_SPEED_S8:
138 case PLAYBACK_SPEED_X1:
139 case PLAYBACK_SPEED_X2:
140 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800141 case PLAYBACK_SPEED_X3:
142 case PLAYBACK_SPEED_X5:
143 case PLAYBACK_SPEED_X6:
144 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800145 case PLAYBACK_SPEED_X8:
146 case PLAYBACK_SPEED_X12:
147 case PLAYBACK_SPEED_X16:
148 case PLAYBACK_SPEED_X32:
149 case PLAYBACK_SPEED_X48:
150 case PLAYBACK_SPEED_X64:
151 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800152 ret = DVR_TRUE;
153 break;
154 default:
Wentao MA96f68962022-06-15 19:45:35 +0800155 DVR_PB_INFO("not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800156 break;
157 }
158 return ret;
159}
Wentao MA907b6432022-08-01 06:23:08 +0000160
hualing chen6e4bfa52020-03-13 14:37:11 +0800161void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
162{
Wentao MA96f68962022-06-15 19:45:35 +0800163 DVR_PB_INFO("in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800164 DVR_Playback_t *player = NULL;
165 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800166 player = (DVR_Playback_t *) user_data;
Wentao MA96f68962022-06-15 19:45:35 +0800167 DVR_PB_INFO("play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800168 }
169 switch (event->type) {
170 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
171 {
Wentao MA96f68962022-06-15 19:45:35 +0800172 DVR_PB_INFO("[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800173 event->event.video_format.frame_width,
174 event->event.video_format.frame_height,
175 event->event.video_format.frame_rate);
176 break;
177 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800178 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
179 {
Wentao MA96f68962022-06-15 19:45:35 +0800180 DVR_PB_INFO("[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800181 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800182 break;
183 }
184 default:
185 break;
186 }
187}
hualing chen2aba4022020-03-02 13:49:55 +0800188void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
189{
hualing chen6e4bfa52020-03-13 14:37:11 +0800190 DVR_Playback_t *player = NULL;
191 if (user_data != NULL) {
192 player = (DVR_Playback_t *) user_data;
Wentao MA96f68962022-06-15 19:45:35 +0800193 DVR_PB_INFO("play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800194 }
hualing chen2aba4022020-03-02 13:49:55 +0800195 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800196 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
197 {
Wentao MA96f68962022-06-15 19:45:35 +0800198 DVR_PB_INFO("[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800199 event->event.video_format.frame_width,
200 event->event.video_format.frame_height,
201 event->event.video_format.frame_rate);
202 break;
203 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800204 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
205 {
Wentao MA96f68962022-06-15 19:45:35 +0800206 DVR_PB_INFO("[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800207 if (player->first_trans_ok == DVR_FALSE) {
208 player->first_trans_ok = DVR_TRUE;
209 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
210 }
hualing chen30423862021-04-16 14:39:12 +0800211 if (player != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800212 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800213 player->seek_pause = DVR_FALSE;
214 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800215 break;
216 }
hualing chen487ae6d2020-07-22 10:34:11 +0800217 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
Wentao MA96f68962022-06-15 19:45:35 +0800218 DVR_PB_INFO("[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
hualing chen487ae6d2020-07-22 10:34:11 +0800219 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
220 player->first_trans_ok = DVR_TRUE;
221 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
222 }
223 if (player != NULL && player->has_video == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +0800224 DVR_PB_INFO("[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
hualing chen487ae6d2020-07-22 10:34:11 +0800225 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800226 player->seek_pause = DVR_FALSE;
hualing chen487ae6d2020-07-22 10:34:11 +0800227 }
228 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800229 default:
Wentao MA270dc0f2022-08-23 13:17:26 +0800230 DVR_PB_INFO("[evt]unknown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800231 break;
232 }
233 if (player&&player->player_callback_func) {
Wentao MA96f68962022-06-15 19:45:35 +0800234 DVR_PB_INFO("player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800235 player->player_callback_func(player->player_callback_userdata, event);
236 } else if (player == NULL){
Wentao MA96f68962022-06-15 19:45:35 +0800237 DVR_PB_INFO("player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800238 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800239 DVR_PB_INFO("player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800240 }
241}
hualing chencc91e1c2020-02-28 13:26:17 +0800242
hualing chen5cbe1a62020-02-10 16:36:36 +0800243//convert video and audio fmt
244static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
245 int format = 0;
246 if (is_audio == DVR_FALSE) {
247 //for video fmt
248 switch (fmt)
249 {
250 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800251 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800252 break;
253 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800254 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800255 break;
256 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800257 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800258 break;
259 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800260 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800261 break;
hualing chena540a7e2020-03-27 16:44:05 +0800262 case DVR_VIDEO_FORMAT_VP9:
263 format = AV_VIDEO_CODEC_VP9;
264 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800265 }
266 } else {
267 //for audio fmt
268 switch (fmt)
269 {
270 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800271 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800272 break;
273 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800274 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800275 break;
276 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800277 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800278 break;
279 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800280 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800281 break;
hualing chena540a7e2020-03-27 16:44:05 +0800282 case DVR_AUDIO_FORMAT_AAC:
283 format = AV_AUDIO_CODEC_AAC;
284 break;
285 case DVR_AUDIO_FORMAT_LATM:
286 format = AV_AUDIO_CODEC_LATM;
287 break;
288 case DVR_AUDIO_FORMAT_PCM:
289 format = AV_AUDIO_CODEC_PCM;
290 break;
hualing chenee0e52b2021-04-09 16:58:44 +0800291 case DVR_AUDIO_FORMAT_AC4:
292 format = AV_AUDIO_CODEC_AC4;
293 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800294 }
295 }
296 return format;
297}
hualing chen040df222020-01-17 13:35:02 +0800298static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800299{
hualing chen040df222020-01-17 13:35:02 +0800300 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800301
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800302 if (player == NULL || player->handle == (am_tsplayer_handle)NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800303 return -1;
304
hualing chena540a7e2020-03-27 16:44:05 +0800305 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800306}
hualing chena540a7e2020-03-27 16:44:05 +0800307
hualing chen7ea70a72021-09-09 11:25:13 +0800308
309//get sys time sec
310static uint32_t _dvr_getClock_sec(void)
hualing chen5cbe1a62020-02-10 16:36:36 +0800311{
312 struct timespec ts;
hualing chen7ea70a72021-09-09 11:25:13 +0800313 uint32_t s;
hualing chen03fd4942021-07-15 15:56:41 +0800314 clock_gettime(CLOCK_REALTIME, &ts);
hualing chen7ea70a72021-09-09 11:25:13 +0800315 s = (uint32_t)(ts.tv_sec);
Wentao MA96f68962022-06-15 19:45:35 +0800316 DVR_PB_INFO("n:%u", s);
hualing chen7ea70a72021-09-09 11:25:13 +0800317 return s;
hualing chen5cbe1a62020-02-10 16:36:36 +0800318}
hualing chen86e7d482020-01-16 15:13:33 +0800319
hualing chen7ea70a72021-09-09 11:25:13 +0800320//get sys time ms
321static uint32_t _dvr_time_getClock(void)
322{
323 struct timespec ts;
324 uint32_t ms;
325 clock_gettime(CLOCK_REALTIME, &ts);
326 ms = (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
327 return ms;
328}
hualing chenb31a6c62020-01-13 17:27:00 +0800329
Wentao MA270dc0f2022-08-23 13:17:26 +0800330//timeout wait signal
hualing chen040df222020-01-17 13:35:02 +0800331static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800332{
hualing chen040df222020-01-17 13:35:02 +0800333 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800334
hualing chena540a7e2020-03-27 16:44:05 +0800335
336 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800337 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800338 return DVR_FAILURE;
339 }
340
hualing chen86e7d482020-01-16 15:13:33 +0800341 struct timespec ts;
342 clock_gettime(CLOCK_MONOTONIC, &ts);
343 //ms为毫秒,换算成秒
344 ts.tv_sec += ms/1000;
345 //在outtime的基础上,增加ms毫秒
346 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
347 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
348 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
349 //us的值有可能超过1秒,
350 ts.tv_sec += us / 1000000;
351 us = us % 1000000;
352 ts.tv_nsec = us * 1000;//换算成纳秒
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800353
354 int val = dvr_mutex_save(&player->lock);
355 pthread_cond_timedwait(&player->cond, &player->lock.lock, &ts);
356 dvr_mutex_restore(&player->lock, val);
hualing chen86e7d482020-01-16 15:13:33 +0800357 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800358}
hualing chen31140872020-03-25 12:29:26 +0800359//get tsplay delay time ms
360static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
361 DVR_Playback_t *player = (DVR_Playback_t *) handle;
362 int64_t cache = 0;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800363 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800364 DVR_PB_INFO("tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800365 return 0;
366 }
367 AmTsPlayer_getDelayTime(player->handle, &cache);
Wentao MA96f68962022-06-15 19:45:35 +0800368 DVR_PB_INFO("tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800369 return cache;
370}
hualing chenb31a6c62020-01-13 17:27:00 +0800371//send signal
hualing chen040df222020-01-17 13:35:02 +0800372static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800373{
hualing chen87072a82020-03-12 16:20:12 +0800374 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800375
376 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800377 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800378 return DVR_FAILURE;
379 }
Wentao MA96f68962022-06-15 19:45:35 +0800380 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800381 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800382 pthread_cond_signal(&player->cond);
Wentao MA96f68962022-06-15 19:45:35 +0800383 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800384 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800385 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800386}
387
hualing chen2932d372020-04-29 13:44:00 +0800388//send playback event, need check is need lock first
389static 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 +0800390
391 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800392
393 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800394 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800395 return DVR_FAILURE;
396 }
397
hualing chencc91e1c2020-02-28 13:26:17 +0800398 switch (evt) {
399 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800400 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800401 break;
402 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
403 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800404 DVR_PB_INFO("trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800405 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800406 break;
407 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
408 break;
409 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
410 break;
411 case DVR_PLAYBACK_EVENT_NO_KEY:
412 break;
413 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800414 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800415 DVR_PB_INFO("reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800416 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800417 break;
418 case DVR_PLAYBACK_EVENT_REACHED_END:
419 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800420 DVR_PB_INFO("reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800421 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800422 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800423 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800424 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800425 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800426 default:
427 break;
428 }
429 if (player->openParams.event_fn != NULL)
430 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800431 return DVR_SUCCESS;
432}
hualing chen2932d372020-04-29 13:44:00 +0800433static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800434{
435 DVR_Play_Notify_t notify;
436 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
437 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
438 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800439 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800440 return DVR_SUCCESS;
441}
442
hualing chen2932d372020-04-29 13:44:00 +0800443static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800444{
445 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800446
hualing chene3797f02021-01-13 14:53:28 +0800447 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chend241c7a2021-06-22 13:34:27 +0800448 if (CONTROL_SPEED_ENABLE == 0)
449 return DVR_SUCCESS;
hualing chen4b7c15d2020-04-07 16:13:48 +0800450 }
hualing chena540a7e2020-03-27 16:44:05 +0800451 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800452 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800453 return DVR_FAILURE;
454 }
455
hualing chen03fd4942021-07-15 15:56:41 +0800456 if (player->send_time == 0) {
hualing chend241c7a2021-06-22 13:34:27 +0800457 if (CONTROL_SPEED_ENABLE == 0)
458 player->send_time = _dvr_time_getClock() + 500;
459 else
460 player->send_time = _dvr_time_getClock() + 20;
hualing chen0888c032020-12-18 17:54:57 +0800461 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen56c0a162022-01-27 17:01:50 +0800462 if ((player->send_time - _dvr_time_getClock()) > 1000) {
463 player->send_time = _dvr_time_getClock() + 500;
Wentao MA96f68962022-06-15 19:45:35 +0800464 DVR_PB_INFO("player send time occur system time changed!!!!!");
hualing chen56c0a162022-01-27 17:01:50 +0800465 } else {
466 return DVR_SUCCESS;
467 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800468 }
hualing chend241c7a2021-06-22 13:34:27 +0800469 if (CONTROL_SPEED_ENABLE == 0)
470 player->send_time = _dvr_time_getClock() + 500;
471 else
472 player->send_time = _dvr_time_getClock() + 20;
473
hualing chen6e4bfa52020-03-13 14:37:11 +0800474 DVR_Play_Notify_t notify;
475 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
476 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
477 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800478 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800479 return DVR_SUCCESS;
480}
481
hualing chencc91e1c2020-02-28 13:26:17 +0800482//check is ongoing segment
483static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
484
485 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800486 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800487
488 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800489 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800490 return DVR_FAILURE;
491 }
hualing chen87072a82020-03-12 16:20:12 +0800492 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800493 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800494 return DVR_FALSE;
495 }
hualing chencc91e1c2020-02-28 13:26:17 +0800496 return DVR_TRUE;
497}
hualing chen4b7c15d2020-04-07 16:13:48 +0800498
499
500static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
501 DVR_Playback_t *player = (DVR_Playback_t *) handle;
502 player->fffb_start = _dvr_time_getClock();
Wentao MA96f68962022-06-15 19:45:35 +0800503 DVR_PB_INFO(" player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800504 player->fffb_current = player->fffb_start;
505 //get segment current time pos
506 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +0800507 player->next_fffb_time = _dvr_time_getClock();
508
509 return DVR_SUCCESS;
510}
511
hualing chen2aba4022020-03-02 13:49:55 +0800512static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
513 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800514 player->fffb_start = _dvr_time_getClock();
Wentao MA96f68962022-06-15 19:45:35 +0800515 DVR_PB_INFO(" player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800516 player->fffb_current = player->fffb_start;
517 //get segment current time pos
518 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +0800519
hualing chen2aba4022020-03-02 13:49:55 +0800520 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800521 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800522 return DVR_SUCCESS;
523}
hualing chencc91e1c2020-02-28 13:26:17 +0800524//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800525static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
526
527 DVR_Playback_t *player = (DVR_Playback_t *) handle;
528 DVR_PlaybackSegmentInfo_t *segment;
529 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
530
hualing chena540a7e2020-03-27 16:44:05 +0800531 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800532 DVR_PB_INFO(" player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800533 return DVR_FAILURE;
534 }
535
hualing chen87072a82020-03-12 16:20:12 +0800536 int found = 0;
537 int found_eq_id = 0;
538 list_for_each_entry(segment, &player->segment_list, head)
539 {
540 if (player->segment_is_open == DVR_FALSE) {
541 //get first segment from list, case segment is not open
542 if (!IS_FB(player->speed))
543 found = 1;
544 } else if (segment->segment_id == segmentid) {
545 //find cur segment, we need get next one
546 found_eq_id = 1;
547 if (!IS_FB(player->speed)) {
548 found = 1;
549 continue;
550 } else {
551 //if is fb mode.we need used pre segment
552 if (pre_segment != NULL) {
553 found = 1;
554 } else {
555 //not find next id.
Wentao MA96f68962022-06-15 19:45:35 +0800556 DVR_PB_INFO("not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800557 return DVR_FAILURE;
558 }
559 }
560 }
561 if (found == 1) {
562 found = 2;
563 break;
564 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800565 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800566 }
567 if (found != 2) {
568 //list is null or reache list end
Wentao MA96f68962022-06-15 19:45:35 +0800569 DVR_PB_INFO("not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800570 return DVR_FAILURE;
571 }
Wentao MA96f68962022-06-15 19:45:35 +0800572 DVR_PB_INFO("found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800573 return DVR_SUCCESS;
574}
575
576//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800577static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800578
hualing chen040df222020-01-17 13:35:02 +0800579 DVR_Playback_t *player = (DVR_Playback_t *) handle;
580 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800581 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen03fd4942021-07-15 15:56:41 +0800582 uint64_t segmentid;
hualing chen7ea70a72021-09-09 11:25:13 +0800583 uint32_t pos;
hualing chena540a7e2020-03-27 16:44:05 +0800584 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800585 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800586 return DVR_FAILURE;
587 }
588
hualing chen03fd4942021-07-15 15:56:41 +0800589 if (IS_FB(player->speed)
590 && dvr_playback_check_limit(handle)) {
591 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
592 //case cur id < segment id
593 if (player->cur_segment_id <= segmentid) {
594 //expired ts data is player,return error
Wentao MA96f68962022-06-15 19:45:35 +0800595 DVR_PB_INFO("reach start segment ,return error");
hualing chen03fd4942021-07-15 15:56:41 +0800596 return DVR_FAILURE;
597 }
Wentao MA96f68962022-06-15 19:45:35 +0800598 DVR_PB_INFO("has segment to fb play [%lld][%u]", segmentid, pos);
hualing chen03fd4942021-07-15 15:56:41 +0800599 }
600
hualing chen86e7d482020-01-16 15:13:33 +0800601 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800602 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800603
hualing chen040df222020-01-17 13:35:02 +0800604 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800605 {
hualing chencc91e1c2020-02-28 13:26:17 +0800606 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800607 //get first segment from list, case segment is not open
608 if (!IS_FB(player->speed))
609 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800610 } else if (segment->segment_id == player->cur_segment_id) {
611 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800612 found_eq_id = 1;
613 if (!IS_FB(player->speed)) {
614 found = 1;
615 continue;
616 } else {
617 //if is fb mode.we need used pre segment
618 if (pre_segment != NULL) {
619 found = 1;
620 } else {
621 //not find next id.
Wentao MA96f68962022-06-15 19:45:35 +0800622 DVR_PB_INFO("not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800623 return DVR_FAILURE;
624 }
625 }
hualing chen86e7d482020-01-16 15:13:33 +0800626 }
627 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800628 if (IS_FB(player->speed)) {
629 //used pre segment
630 segment = pre_segment;
631 }
hualing chencc91e1c2020-02-28 13:26:17 +0800632 //save segment info
633 player->last_segment_id = player->cur_segment_id;
hualing chen969fe7b2021-05-26 15:13:17 +0800634 if (player->r_handle)
Wentao MA270dc0f2022-08-23 13:17:26 +0800635 player->last_segment_total = segment_tell_total_time(player->r_handle);
hualing chen87072a82020-03-12 16:20:12 +0800636 player->last_segment.segment_id = player->cur_segment.segment_id;
637 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800638 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
639 //pids
640 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
641
hualing chen5cbe1a62020-02-10 16:36:36 +0800642 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800643 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800644 player->cur_segment_id = segment->segment_id;
645 player->cur_segment.segment_id = segment->segment_id;
646 player->cur_segment.flags = segment->flags;
Wentao MA96f68962022-06-15 19:45:35 +0800647 DVR_PB_INFO("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 +0800648 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800649 //pids
hualing chen040df222020-01-17 13:35:02 +0800650 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800651 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800652 break;
hualing chen86e7d482020-01-16 15:13:33 +0800653 }
hualing chen2aba4022020-03-02 13:49:55 +0800654 pre_segment = segment;
655 }
656 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
657 //used the last one segment to open
658 //get segment info
659 player->segment_is_open = DVR_TRUE;
660 player->cur_segment_id = pre_segment->segment_id;
661 player->cur_segment.segment_id = pre_segment->segment_id;
662 player->cur_segment.flags = pre_segment->flags;
Wentao MA96f68962022-06-15 19:45:35 +0800663 DVR_PB_INFO("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 +0800664 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
665 //pids
666 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
667 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800668 }
669 if (found != 2) {
670 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800671 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800672 }
673 return DVR_SUCCESS;
674}
hualing chen040df222020-01-17 13:35:02 +0800675//open next segment to play,if reach list end return errro.
676static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800677{
hualing chen040df222020-01-17 13:35:02 +0800678 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800679 Segment_OpenParams_t params;
680 int ret = DVR_SUCCESS;
681
hualing chena540a7e2020-03-27 16:44:05 +0800682 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800683 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800684 return DVR_FAILURE;
685 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800686 pthread_mutex_lock(&player->segment_lock);
hualing chen926a8ec2021-12-20 20:38:24 +0800687retry:
hualing chena540a7e2020-03-27 16:44:05 +0800688 ret = _dvr_get_next_segmentId(handle);
689 if (ret == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800690 DVR_PB_INFO("not found segment info");
hualing chen4b7c15d2020-04-07 16:13:48 +0800691 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800692 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800693 }
694
695 if (player->r_handle != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800696 DVR_PB_INFO("close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800697 segment_close(player->r_handle);
698 player->r_handle = NULL;
699 }
700
701 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
Wentao MA270dc0f2022-08-23 13:17:26 +0800702 //cp current segment path to location
hualing chen5cbe1a62020-02-10 16:36:36 +0800703 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800704 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800705 params.mode = SEGMENT_MODE_READ;
Wentao MA96f68962022-06-15 19:45:35 +0800706 DVR_PB_INFO("open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
hualing chen4b7c15d2020-04-07 16:13:48 +0800707
hualing chen86e7d482020-01-16 15:13:33 +0800708 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800709 if (ret == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800710 DVR_PB_INFO("open segment error");
hualing chen926a8ec2021-12-20 20:38:24 +0800711 goto retry;
hualing chen4b7c15d2020-04-07 16:13:48 +0800712 }
Wentao MA01de0e62022-01-10 18:48:23 +0800713 // Keep the start segment_id when the first segment_open is called during a playback
714 if (player->first_start_id == UINT64_MAX) {
715 player->first_start_id = player->cur_segment.segment_id;
716 }
hualing chen87072a82020-03-12 16:20:12 +0800717 pthread_mutex_unlock(&player->segment_lock);
718 int total = _dvr_get_end_time( handle);
719 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800720 if (IS_FB(player->speed)) {
721 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800722 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800723 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +0800724 DVR_PB_INFO("seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800725 }
hualing chen87072a82020-03-12 16:20:12 +0800726 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800727 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +0800728 DVR_PB_INFO("next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800729 return ret;
730}
731
hualing chen5cbe1a62020-02-10 16:36:36 +0800732//open next segment to play,if reach list end return errro.
733static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
734{
735 DVR_Playback_t *player = (DVR_Playback_t *) handle;
736 Segment_OpenParams_t params;
737 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800738 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800739 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800740 return DVR_FAILURE;
741 }
hualing chencc91e1c2020-02-28 13:26:17 +0800742 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800743 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800744 }
hualing chencc91e1c2020-02-28 13:26:17 +0800745 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800746 if (id < 0) {
Wentao MA96f68962022-06-15 19:45:35 +0800747 DVR_PB_INFO("not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800748 return DVR_FAILURE;
749 }
Wentao MA96f68962022-06-15 19:45:35 +0800750 DVR_PB_INFO("start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800751 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800752
753 DVR_PlaybackSegmentInfo_t *segment;
754
755 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800756
hualing chen5cbe1a62020-02-10 16:36:36 +0800757 list_for_each_entry(segment, &player->segment_list, head)
758 {
Wentao MA96f68962022-06-15 19:45:35 +0800759 DVR_PB_INFO("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 +0800760 if (segment->segment_id == segment_id) {
761 found = 1;
762 }
763 if (found == 1) {
Wentao MA96f68962022-06-15 19:45:35 +0800764 DVR_PB_INFO("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 +0800765 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800766 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800767 player->cur_segment_id = segment->segment_id;
768 player->cur_segment.segment_id = segment->segment_id;
769 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800770 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800771 //pids
772 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +0800773 DVR_PB_INFO("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 +0800774 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800775 }
776 }
hualing chencc91e1c2020-02-28 13:26:17 +0800777 if (found == 0) {
Wentao MA96f68962022-06-15 19:45:35 +0800778 DVR_PB_INFO("not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800779 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800780 return DVR_FAILURE;
781 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800782 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800783 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800784 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800785 params.segment_id = (uint64_t)player->cur_segment.segment_id;
786 params.mode = SEGMENT_MODE_READ;
Wentao MA96f68962022-06-15 19:45:35 +0800787 DVR_PB_INFO("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 +0800788 if (player->r_handle != NULL) {
789 segment_close(player->r_handle);
790 player->r_handle = NULL;
791 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800792 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800793 if (ret == DVR_FAILURE) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800794 DVR_PB_INFO("segment open error");
hualing chen4b7c15d2020-04-07 16:13:48 +0800795 }
Wentao MA01de0e62022-01-10 18:48:23 +0800796 // Keep the start segment_id when the first segment_open is called during a playback
797 if (player->first_start_id == UINT64_MAX) {
798 player->first_start_id = player->cur_segment.segment_id;
799 }
hualing chen2aba4022020-03-02 13:49:55 +0800800 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800801 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800802
Wentao MA96f68962022-06-15 19:45:35 +0800803 DVR_PB_INFO("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 +0800804 return ret;
805}
806
807
808//get play info by segment id
809static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
810 uint64_t segment_id,
Wentao MA270dc0f2022-08-23 13:17:26 +0800811 am_tsplayer_video_params *video_param,
812 am_tsplayer_audio_params *audio_param, am_tsplayer_audio_params *ad_param) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800813
814 DVR_Playback_t *player = (DVR_Playback_t *) handle;
815 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800816 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800817 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800818 return DVR_FAILURE;
819 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800820
821 int found = 0;
822
823 list_for_each_entry(segment, &player->segment_list, head)
824 {
hualing chen87072a82020-03-12 16:20:12 +0800825 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800826 //get first segment from list
827 found = 1;
828 }
829 if (segment->segment_id == segment_id) {
830 found = 1;
831 }
832 if (found == 1) {
833 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800834 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800835 player->cur_segment_id = segment->segment_id;
Wentao MA96f68962022-06-15 19:45:35 +0800836 DVR_PB_INFO("get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800837 player->cur_segment.segment_id = segment->segment_id;
838 player->cur_segment.flags = segment->flags;
839 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800840 player->cur_segment.pids.video.pid = segment->pids.video.pid;
841 player->cur_segment.pids.video.format = segment->pids.video.format;
842 player->cur_segment.pids.video.type = segment->pids.video.type;
843 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
844 player->cur_segment.pids.audio.format = segment->pids.audio.format;
845 player->cur_segment.pids.audio.type = segment->pids.audio.type;
846 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
847 player->cur_segment.pids.ad.format = segment->pids.ad.format;
848 player->cur_segment.pids.ad.type = segment->pids.ad.type;
849 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800850 //
Wentao MA270dc0f2022-08-23 13:17:26 +0800851 video_param->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
852 video_param->pid = segment->pids.video.pid;
853 audio_param->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
854 audio_param->pid = segment->pids.audio.pid;
855 ad_param->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
856 ad_param->pid =segment->pids.ad.pid;
857 DVR_PB_INFO("get play info success[0x%x]apid[0x%x]vfmt[%d]afmt[%d]", video_param->pid, audio_param->pid, video_param->codectype, audio_param->codectype);
hualing chen5cbe1a62020-02-10 16:36:36 +0800858 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800859 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800860 }
861 }
hualing chencc91e1c2020-02-28 13:26:17 +0800862 if (found != 2) {
863 //list is null or reache list end
Wentao MA96f68962022-06-15 19:45:35 +0800864 DVR_PB_INFO("get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800865 return DVR_FAILURE;
866 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800867
868 return DVR_SUCCESS;
869}
hualing chencc91e1c2020-02-28 13:26:17 +0800870static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
871 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800872 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800873 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800874 return DVR_FAILURE;
875 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800876
hualing chencc91e1c2020-02-28 13:26:17 +0800877 //compare cur segment
878 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
879 {
880 //check video pids, stop or restart
hualing chen99508642021-10-18 15:41:17 +0800881 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800882 //check sub audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800883 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800884 //check audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800885 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 1);
Wentao MA96f68962022-06-15 19:45:35 +0800886 DVR_PB_INFO(":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 +0800887 //check pcr pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800888 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800889 }
hualing chena540a7e2020-03-27 16:44:05 +0800890 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800891}
hualing chen5cbe1a62020-02-10 16:36:36 +0800892
hualing chend241c7a2021-06-22 13:34:27 +0800893static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
894{
895 DVR_Playback_t *player = (DVR_Playback_t *) handle;
896 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800897 DVR_PB_INFO("player is NULL");
hualing chend241c7a2021-06-22 13:34:27 +0800898 return DVR_TRUE;
899 }
900 char buf[10];
901 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
Wentao MA96f68962022-06-15 19:45:35 +0800902 DVR_PB_INFO("player get prop[%d][%s]", atoi(buf), buf);
hualing chend241c7a2021-06-22 13:34:27 +0800903
904 if (atoi(buf) != 1) {
905 //return DVR_TRUE;
906 }
907
Wentao MA96f68962022-06-15 19:45:35 +0800908 DVR_PB_INFO(":play speed: %f ply dur: %u sys_dur: %u",
hualing chen03fd4942021-07-15 15:56:41 +0800909 player->speed,
910 player->con_spe.ply_dur,
911 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800912
913 if (player->speed != 1.0f)
914 return DVR_TRUE;
915
916 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800917 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800918 return DVR_FALSE;
919
920 return DVR_TRUE;
921}
922
hualing chencc91e1c2020-02-28 13:26:17 +0800923static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
924{
925 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800926 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800927 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800928 return DVR_FAILURE;
929 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800930 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
Wentao MA96f68962022-06-15 19:45:35 +0800931 DVR_PB_INFO("vendor is amlogic. no used segment flag to hide or show av");
hualing chenf43b8ba2020-07-28 13:11:42 +0800932 return DVR_SUCCESS;
933 }
Wentao MA96f68962022-06-15 19:45:35 +0800934 DVR_PB_INFO("flag[0x%x]id[%lld]last[0x%x][%llu]",
hualing chen03fd4942021-07-15 15:56:41 +0800935 player->cur_segment.flags,
936 player->cur_segment.segment_id,
937 player->last_segment.flags,
938 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800939 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
940 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800941 //enable display
Wentao MA96f68962022-06-15 19:45:35 +0800942 DVR_PB_INFO("unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800943 AmTsPlayer_showVideo(player->handle);
944 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800945 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
946 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800947 //disable display
Wentao MA96f68962022-06-15 19:45:35 +0800948 DVR_PB_INFO("mute");
hualing chen2aba4022020-03-02 13:49:55 +0800949 AmTsPlayer_hideVideo(player->handle);
950 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800951 }
952 return DVR_SUCCESS;
953}
hualing chene3797f02021-01-13 14:53:28 +0800954/*
Wentao MA270dc0f2022-08-23 13:17:26 +0800955if decode success first time.
956success: return true
hualing chene3797f02021-01-13 14:53:28 +0800957fail: return false
958*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800959static DVR_Bool_t _dvr_pauselive_decode_success(DVR_PlaybackHandle_t handle) {
hualing chena540a7e2020-03-27 16:44:05 +0800960 DVR_Playback_t *player = (DVR_Playback_t *) handle;
961 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800962 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800963 return DVR_TRUE;
964 }
hualing chene3797f02021-01-13 14:53:28 +0800965 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800966 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800967 } else {
968 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800969 }
970}
hualing chen86e7d482020-01-16 15:13:33 +0800971static void* _dvr_playback_thread(void *arg)
972{
hualing chen040df222020-01-17 13:35:02 +0800973 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800974 //int need_open_segment = 1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800975 am_tsplayer_input_buffer input_buffer;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800976 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800977 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800978
hualing chen39628212020-05-14 10:35:13 +0800979 #define MAX_REACHEND_TIMEOUT (3000)
980 int reach_end_timeout = 0;//ms
981 int cache_time = 0;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800982 int timeout = 200;//ms
hualing chen40dd5462021-11-26 19:56:20 +0800983 int check_no_data_time = 4;
hualing chen2aba4022020-03-02 13:49:55 +0800984 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800985 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800986 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800987 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
hualing chen40dd5462021-11-26 19:56:20 +0800988 int first_write = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800989 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800990 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800991 DVR_Bool_t goto_rewrite = DVR_FALSE;
yinming ding0ce94922021-09-08 15:09:15 +0800992 char prop_buf[10];
993
994 memset(prop_buf, 0 ,sizeof(prop_buf));
995 dvr_prop_read("vendor.tv.libdvr.writetm", prop_buf, sizeof(prop_buf));
Wentao MA96f68962022-06-15 19:45:35 +0800996 DVR_PB_INFO("---vendor.tv.libdvr.writetm get prop[%d][%s]block_size[%d]", atoi(prop_buf), prop_buf, player->openParams.block_size);
yinming ding0ce94922021-09-08 15:09:15 +0800997 if (atoi(prop_buf) > 0)
998 write_timeout_ms = atoi(prop_buf);
hualing chen03fd4942021-07-15 15:56:41 +0800999
hualing chen56c0a162022-01-27 17:01:50 +08001000
1001 memset(prop_buf, 0 ,sizeof(prop_buf));
1002 dvr_prop_read("vendor.tv.libdvr.waittm", prop_buf, sizeof(prop_buf));
Wentao MA96f68962022-06-15 19:45:35 +08001003 DVR_PB_INFO("---vendor.tv.libdvr.waittm get prop[%d][%s]block_size[%d]", atoi(prop_buf), prop_buf, player->openParams.block_size);
hualing chen56c0a162022-01-27 17:01:50 +08001004 if (atoi(prop_buf) > 0)
1005 timeout = atoi(prop_buf);
1006
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001007 if (player->is_secure_mode) {
1008 if (dec_buf_size > player->secure_buffer_size) {
Wentao MA96f68962022-06-15 19:45:35 +08001009 DVR_PB_INFO("playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001010 return NULL;
1011 }
1012 }
hualing chen86e7d482020-01-16 15:13:33 +08001013 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001014 if (!buf) {
Wentao MA96f68962022-06-15 19:45:35 +08001015 DVR_PB_INFO("Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001016 return NULL;
1017 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001018 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1019 input_buffer.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001020
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001021 dec_bufs.buf_data = malloc(dec_buf_size);
1022 if (!dec_bufs.buf_data) {
Wentao MA96f68962022-06-15 19:45:35 +08001023 DVR_PB_INFO("Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +08001024 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001025 return NULL;
1026 }
1027 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1028 dec_bufs.buf_size = dec_buf_size;
1029
hualing chencc91e1c2020-02-28 13:26:17 +08001030 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001031 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
1032 }
hualing chen86e7d482020-01-16 15:13:33 +08001033
hualing chen86e7d482020-01-16 15:13:33 +08001034 if (ret != DVR_SUCCESS) {
1035 if (buf != NULL) {
1036 free(buf);
1037 buf = NULL;
1038 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001039 free(dec_bufs.buf_data);
Wentao MA96f68962022-06-15 19:45:35 +08001040 DVR_PB_INFO("get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +08001041 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +08001042 }
Wentao MA96f68962022-06-15 19:45:35 +08001043 DVR_PB_INFO("--player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001044 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +08001045 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
1046 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
1047 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
1048 player->first_trans_ok = DVR_TRUE;
1049 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
1050 }
hualing chencc91e1c2020-02-28 13:26:17 +08001051 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +08001052 //set video show
1053 AmTsPlayer_showVideo(player->handle);
hualing chen40dd5462021-11-26 19:56:20 +08001054 if (player->vendor == DVR_PLAYBACK_VENDOR_AMAZON)
1055 check_no_data_time = 8;
hualing chen86e7d482020-01-16 15:13:33 +08001056 int trick_stat = 0;
1057 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001058
hualing chen86e7d482020-01-16 15:13:33 +08001059 //check trick stat
Wentao MA96f68962022-06-15 19:45:35 +08001060 //DVR_PB_INFO("lock check_no_data_time:%d", check_no_data_time);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001061 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001062
Wentao MA907b6432022-08-01 06:23:08 +00001063 {
Wentao MA9a164002022-08-29 11:20:24 +08001064 static struct timespec _prev_ts={0,0};
Wentao MA907b6432022-08-01 06:23:08 +00001065 struct timespec _nowts,_diffts;
1066 clock_gettime(CLOCK_MONOTONIC, &_nowts);
Wentao MA9a164002022-08-29 11:20:24 +08001067 clock_timespec_subtract(&_nowts,&_prev_ts,&_diffts);
Wentao MA907b6432022-08-01 06:23:08 +00001068 if (_diffts.tv_sec>0) {
1069 char _logbuf[512]={0};
1070 char* _pbuf=_logbuf;
1071 int _nchar=0;
1072 DVR_PlaybackSegmentInfo_t* _segment;
1073 list_for_each_entry(_segment, &player->segment_list, head) {
1074 if (player->cur_segment_id == _segment->segment_id) {
Wentao MA9a164002022-08-29 11:20:24 +08001075 int seg_size = segment_get_cur_segment_size(player->r_handle);
1076 int read_ptr = segment_tell_position(player->r_handle);
Wentao MA907b6432022-08-01 06:23:08 +00001077 float progress = -1.0f;
Wentao MA9a164002022-08-29 11:20:24 +08001078 if (seg_size>0) {
1079 progress = (float)read_ptr*100/seg_size;
Wentao MA907b6432022-08-01 06:23:08 +00001080 }
1081 _nchar=sprintf(_pbuf,"%lld(%.1f%), ",_segment->segment_id,progress);
1082 } else {
1083 _nchar=sprintf(_pbuf,"%lld, ",_segment->segment_id);
1084 }
1085 if (_nchar<0) {
1086 break;
1087 }
1088 _pbuf+=_nchar;
1089 if (_pbuf-_logbuf+10 >= sizeof(_logbuf)) {
1090 sprintf(_pbuf,"...");
1091 break;
1092 }
1093 }
Wentao MA9a164002022-08-29 11:20:24 +08001094 DVR_PB_INFO("clk: %08u, seg_list: %s",_nowts.tv_sec,_logbuf);
1095 _prev_ts=_nowts;
Wentao MA907b6432022-08-01 06:23:08 +00001096 }
1097 }
1098
hualing chen2aba4022020-03-02 13:49:55 +08001099 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1100 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001101 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001102 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001103 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001104 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001105 {
hualing chen2aba4022020-03-02 13:49:55 +08001106 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1107 if (trick_stat > 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001108 DVR_PB_INFO("trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
hualing chen03fd4942021-07-15 15:56:41 +08001109 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001110 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 +08001111 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001112 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001113 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001114 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
Wentao MA270dc0f2022-08-23 13:17:26 +08001115 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_V_START
1116 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_A_START
hualing chen2aba4022020-03-02 13:49:55 +08001117 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
Wentao MA96f68962022-06-15 19:45:35 +08001118 DVR_PB_INFO("pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
hualing chen03fd4942021-07-15 15:56:41 +08001119 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001120 //need change to pause state
1121 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1122 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00001123 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08001124 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001125 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001126 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001127 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001128 AmTsPlayer_pauseVideoDecoding(player->handle);
1129 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001130 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001131 DVR_PB_INFO("clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001132 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001133 }
1134 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1135 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001136 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001137 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001138 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001139 DVR_PB_INFO("fffb pause state----speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001140 player->speed,
1141 player->fffb_current,
1142 _dvr_time_getClock(),
1143 _dvr_playback_state_toString(player->state),
1144 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001145 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001146 //dvr_mutex_unlock(&player->lock);
1147 //dvr_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001148 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001149 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001150 dvr_mutex_unlock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001151 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001152 } else if (_dvr_time_getClock() < player->next_fffb_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001153 DVR_PB_INFO("fffb timeout-to pause video---speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001154 player->speed,
1155 player->fffb_current,
1156 _dvr_time_getClock(),
1157 _dvr_playback_state_toString(player->state),
1158 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001159 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001160 //dvr_mutex_unlock(&player->lock);
1161 //dvr_mutex_lock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001162 AmTsPlayer_pauseVideoDecoding(player->handle);
1163 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001164 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001165 dvr_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001166 continue;
hualing chen2aba4022020-03-02 13:49:55 +08001167 }
Wentao MA96f68962022-06-15 19:45:35 +08001168 DVR_PB_INFO("fffb play-------speed[%f][%d][%d][%s][%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001169 player->speed,
1170 goto_rewrite,
1171 real_read,
1172 _dvr_playback_state_toString(player->state),
1173 player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08001174 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001175 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001176 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1177 player->first_frame = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001178 DVR_PB_INFO("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001179 dvr_mutex_unlock(&player->lock);
1180
hualing chen2aba4022020-03-02 13:49:55 +08001181 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
Wentao MA96f68962022-06-15 19:45:35 +08001182 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001183 dvr_mutex_lock(&player->lock);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001184 player->fffb_play = DVR_FALSE;
hualing chen1ffd85b2021-08-16 15:18:43 +08001185 } else if(player->state == DVR_PLAYBACK_STATE_PAUSE) {
1186 //on pause state,user seek to new pos,we need pause and wait
1187 //user to resume
Wentao MA96f68962022-06-15 19:45:35 +08001188 DVR_PB_INFO("pause, when got first frame event when user seek end");
hualing chen1ffd85b2021-08-16 15:18:43 +08001189 player->first_frame = 0;
1190 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1191 AmTsPlayer_pauseVideoDecoding(player->handle);
1192 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001193 }
hualing chen1ffd85b2021-08-16 15:18:43 +08001194 } else if (player->fffb_play == DVR_TRUE){
hualing chen4b7c15d2020-04-07 16:13:48 +08001195 //for first into fffb when reset speed
1196 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1197 _dvr_time_getClock() < player->next_fffb_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001198 DVR_PB_INFO("fffb timeout-fffb play---speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001199 player->speed,
1200 player->fffb_current,
1201 _dvr_time_getClock(),
1202 _dvr_playback_state_toString(player->state),
1203 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001204 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001205 //dvr_mutex_unlock(&player->lock);
1206 //dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001207 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001208 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001209 dvr_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001210 continue;
1211 }
Wentao MA96f68962022-06-15 19:45:35 +08001212 DVR_PB_INFO("fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001213 player->speed,
1214 goto_rewrite,
1215 real_read,
1216 _dvr_playback_state_toString(player->state),
1217 player->cmd.cur_cmd,
1218 player->fffb_play);
Wentao MA96f68962022-06-15 19:45:35 +08001219 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001220 dvr_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001221 goto_rewrite = DVR_FALSE;
1222 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001223 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001224 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1225 player->first_frame = 0;
1226 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001227 dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001228 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001229 }
hualing chenb31a6c62020-01-13 17:27:00 +08001230 }
hualing chen86e7d482020-01-16 15:13:33 +08001231
hualing chen30423862021-04-16 14:39:12 +08001232 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1233 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001234 //check is need send time send end
Wentao MA96f68962022-06-15 19:45:35 +08001235 DVR_PB_INFO("pause, continue");
1236 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001237 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001238 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001239 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001240 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001241 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001242 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001243 continue;
1244 }
hualing chen266b9502020-04-04 17:39:39 +08001245 //when seek action is done. we need drop write timeout data.
1246 if (player->drop_ts == DVR_TRUE) {
1247 goto_rewrite = DVR_FALSE;
1248 real_read = 0;
1249 player->drop_ts = DVR_FALSE;
1250 }
hualing chen2aba4022020-03-02 13:49:55 +08001251 if (goto_rewrite == DVR_TRUE) {
1252 goto_rewrite = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08001253 //DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001254 dvr_mutex_unlock(&player->lock);
hualing chen3bcf3be2021-12-22 20:15:01 +08001255 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +08001256 //DVR_PB_INFO("rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001257 goto rewrite;
1258 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001259 //.check is need send time send end
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001260 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001261 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001262 dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001263 pthread_mutex_lock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001264 //DVR_PB_INFO("start read");
hualing chen87072a82020-03-12 16:20:12 +08001265 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001266 real_read = real_read + read;
1267 player->ts_cache_len = real_read;
Wentao MA96f68962022-06-15 19:45:35 +08001268 //DVR_PB_INFO("start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001269 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001270 //DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001271 dvr_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001272 if (read < 0 && errno == EIO) {
1273 //EIO ERROR, EXIT THRAD
Wentao MA96f68962022-06-15 19:45:35 +08001274 DVR_PB_INFO("read error.EIO error, exit thread");
hualing chenb5cd42e2020-04-15 17:03:34 +08001275 DVR_Play_Notify_t notify;
1276 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1277 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001278 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001279 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001280 goto end;
1281 } else if (read < 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001282 DVR_PB_INFO("read error.:%d EIO:%d", errno, EIO);
hualing chenb5cd42e2020-04-15 17:03:34 +08001283 }
hualing chen87072a82020-03-12 16:20:12 +08001284 //if on fb mode and read file end , we need calculate pos to retry read.
1285 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001286 DVR_PB_INFO("recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]",
hualing chen03fd4942021-07-15 15:56:41 +08001287 read,
1288 real_read,
1289 buf_len,
1290 player->speed,
1291 player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001292 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
Wentao MA96f68962022-06-15 19:45:35 +08001293 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001294 dvr_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001295 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001296 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001297 dvr_mutex_unlock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001298 continue;
1299 }
Wentao MA96f68962022-06-15 19:45:35 +08001300 //DVR_PB_INFO("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 +08001301 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001302 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001303 #define MIN_CACHE_TIME (3000)
1304 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001305 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001306 if (_cache_time > MIN_CACHE_TIME) {
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001307 //dvr_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001308 /*if cache time > 20s , we think get time is error,*/
1309 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
Wentao MA907b6432022-08-01 06:23:08 +00001310 DVR_PB_WARN("read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
hualing chene3797f02021-01-13 14:53:28 +08001311 }
hualing chen1679f812021-11-08 15:17:46 +08001312 //_dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, ((_cache_time - MIN_CACHE_TIME) > MIN_CACHE_TIME ? MIN_CACHE_TIME : (_cache_time - MIN_CACHE_TIME)));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001313 //dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001314 // DVR_PB_INFO("read end but cache time is %d > %d, to sleep end and continue", _cache_time, MIN_CACHE_TIME);
hualing chene41f4372020-06-06 16:29:17 +08001315 //continue;
1316 }
hualing chen969fe7b2021-05-26 15:13:17 +08001317
hualing chen040df222020-01-17 13:35:02 +08001318 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001319 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001320 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001321
1322 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chen1679f812021-11-08 15:17:46 +08001323
1324 if (ret != DVR_SUCCESS && delay < MIN_TSPLAYER_DELAY_TIME) {
1325 player->noData++;
Wentao MA96f68962022-06-15 19:45:35 +08001326 DVR_PB_INFO("playback nodata[%d]", player->noData);
hualing chen40dd5462021-11-26 19:56:20 +08001327 if (player->noData == check_no_data_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001328 DVR_PB_INFO("playback send nodata event nodata[%d]", player->noData);
hualing chene3797f02021-01-13 14:53:28 +08001329 //send event here and pause
1330 DVR_Play_Notify_t notify;
1331 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1332 notify.event = DVR_PLAYBACK_EVENT_NODATA;
Wentao MA96f68962022-06-15 19:45:35 +08001333 DVR_PB_INFO("send event DVR_PLAYBACK_EVENT_NODATA--");
hualing chene3797f02021-01-13 14:53:28 +08001334 //get play statue not here
1335 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1336 }
1337 }
1338 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001339 if ((ret != DVR_SUCCESS &&
hualing chen03fd4942021-07-15 15:56:41 +08001340 (player->vendor != DVR_PLAYBACK_VENDOR_AMAZON) &&
hualing chen041c4092020-04-05 15:11:50 +08001341 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001342 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
Wentao MA270dc0f2022-08-23 13:17:26 +08001343 _dvr_pauselive_decode_success((DVR_PlaybackHandle_t)player)) ||
hualing chen39628212020-05-14 10:35:13 +08001344 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001345 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001346 DVR_Play_Notify_t notify;
1347 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1348 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1349 //get play statue not here
1350 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001351 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001352 //continue,timeshift mode, when read end,need wait cur recording segment
Wentao MA96f68962022-06-15 19:45:35 +08001353 DVR_PB_INFO("playback is send end delay:[%d]reach_end_timeout[%d]ms", delay, reach_end_timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001354 dvr_mutex_lock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08001355 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001356 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08001357 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001358 } else if (ret != DVR_SUCCESS) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001359 DVR_PB_INFO("delay:%d pauselive:%d", delay, _dvr_pauselive_decode_success((DVR_PlaybackHandle_t)player));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001360 dvr_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08001361 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001362 dvr_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08001363 delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chena540a7e2020-03-27 16:44:05 +08001364 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001365 if (delay < cache_time) {
1366 //delay time is changed and then has data to play, so not start timeout
hualing chen1679f812021-11-08 15:17:46 +08001367 reach_end_timeout = 0;
hualing chen39628212020-05-14 10:35:13 +08001368 } else {
1369 reach_end_timeout = reach_end_timeout + timeout;
1370 }
1371 cache_time = delay;
hualing chen31140872020-03-25 12:29:26 +08001372 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001373 }
hualing chen39628212020-05-14 10:35:13 +08001374 reach_end_timeout = 0;
1375 cache_time = 0;
hualing chen2932d372020-04-29 13:44:00 +08001376 //change next segment success case
1377 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001378 dvr_mutex_lock(&player->lock);
hualing chen40dd5462021-11-26 19:56:20 +08001379 player->noData = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001380 DVR_PB_INFO("_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001381 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1382 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen21a40372021-10-29 11:07:26 +08001383 pthread_mutex_lock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001384 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001385 real_read = real_read + read;
1386 player->ts_cache_len = real_read;
1387 pthread_mutex_unlock(&player->segment_lock);
1388
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001389 dvr_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001390 }//read len 0 check end
hualing chen40dd5462021-11-26 19:56:20 +08001391 if (player->noData >= check_no_data_time) {
hualing chene3797f02021-01-13 14:53:28 +08001392 player->noData = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001393 DVR_PB_INFO("playback send data event resume[%d]", player->noData);
hualing chene3797f02021-01-13 14:53:28 +08001394 //send event here and pause
1395 DVR_Play_Notify_t notify;
1396 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1397 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
Wentao MA96f68962022-06-15 19:45:35 +08001398 DVR_PB_INFO("----send event DVR_PLAYBACK_EVENT_DATARESUME");
hualing chene3797f02021-01-13 14:53:28 +08001399 //get play statue not here
1400 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001401 }
hualing chen39628212020-05-14 10:35:13 +08001402 reach_end_timeout = 0;
hualing chen21a40372021-10-29 11:07:26 +08001403 //real_read = real_read + read;
Wentao MA270dc0f2022-08-23 13:17:26 +08001404 input_buffer.buf_size = real_read;
1405 input_buffer.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001406
Wentao MA270dc0f2022-08-23 13:17:26 +08001407 //check read data len,if len < 0, we need continue
1408 if (input_buffer.buf_size <= 0 || input_buffer.buf_data == NULL) {
1409 DVR_PB_INFO("error occur read_read [%d],buf=[%p]",input_buffer.buf_size, input_buffer.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001410 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001411 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001412 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001413 }
hualing chen266b9502020-04-04 17:39:39 +08001414 //if need write whole block size, we need check read buf len is eq block size.
1415 if (b_writed_whole_block == DVR_TRUE) {
1416 //buf_len is block size value.
1417 if (real_read < buf_len) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001418 //continue to read data from file
Wentao MA96f68962022-06-15 19:45:35 +08001419 DVR_PB_INFO("read buf len[%d] is < block size [%d]", real_read, buf_len);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001420 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001421 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1422 dvr_mutex_unlock(&player->lock);
1423 DVR_PB_INFO("read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001424 continue;
1425 } else if (real_read > buf_len) {
Wentao MA96f68962022-06-15 19:45:35 +08001426 DVR_PB_INFO("read buf len[%d] is > block size [%d],this error occur", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001427 }
1428 }
1429
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001430 //if (player->is_secure_mode && player->dec_func) {
1431 if (player->dec_func) {
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001432 DVR_CryptoParams_t crypto_params;
1433
1434 memset(&crypto_params, 0, sizeof(crypto_params));
1435 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1436 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1437 crypto_params.segment_id = player->cur_segment.segment_id;
Wentao MA270dc0f2022-08-23 13:17:26 +08001438 crypto_params.offset = segment_tell_position(player->r_handle) - input_buffer.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001439 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
Wentao MA96f68962022-06-15 19:45:35 +08001440 DVR_PB_INFO("offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001441 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1442 crypto_params.input_buffer.addr = (size_t)buf;
1443 crypto_params.input_buffer.size = real_read;
1444
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001445 if (player->is_secure_mode) {
1446 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1447 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1448 crypto_params.output_buffer.size = dec_buf_size;
1449 ret = player->dec_func(&crypto_params, player->dec_userdata);
Wentao MA270dc0f2022-08-23 13:17:26 +08001450 input_buffer.buf_data = player->secure_buffer;
1451 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001452 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08001453 DVR_PB_INFO("decrypt failed");
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001454 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001455 input_buffer.buf_size = crypto_params.output_size;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001456 } else { // only for NAGRA
1457 crypto_params.output_buffer.type = crypto_params.input_buffer.type;
1458 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1459 crypto_params.output_buffer.size = crypto_params.input_buffer.size;
1460 ret = player->dec_func(&crypto_params, player->dec_userdata);
Wentao MA270dc0f2022-08-23 13:17:26 +08001461 input_buffer.buf_data = (uint8_t*)crypto_params.output_buffer.addr;
1462 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001463 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08001464 DVR_PB_INFO("decrypt failed");
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001465 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001466 input_buffer.buf_size = crypto_params.output_buffer.size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001467 }
Yahui Han1fbf3292021-11-08 18:17:19 +08001468 } else if (player->cryptor) {
Yahui Han63b23b42021-12-07 15:37:46 +08001469 int len = real_read;
Yahui Han1fbf3292021-11-08 18:17:19 +08001470 am_crypt_des_crypt(player->cryptor, dec_bufs.buf_data, buf, &len, 1);
Wentao MA270dc0f2022-08-23 13:17:26 +08001471 input_buffer.buf_data = dec_bufs.buf_data;
1472 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1473 input_buffer.buf_size = len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001474 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001475rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001476 if (player->drop_ts == DVR_TRUE) {
1477 //need drop ts data when seek occur.we need read next loop,drop this ts data
1478 goto_rewrite = DVR_FALSE;
1479 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001480 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001481 player->drop_ts = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08001482 DVR_PB_INFO("----drop ts");
hualing chenbcada022020-04-22 14:27:01 +08001483 continue;
1484 }
hualing chen21a40372021-10-29 11:07:26 +08001485
1486 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001487 player->ts_cache_len = real_read;
hualing chenb9a02922021-12-14 11:29:47 +08001488 //used for printf first write data time.
1489 //to check change channel kpi.
1490 if (first_write == 0) {
1491 first_write++;
Wentao MA270dc0f2022-08-23 13:17:26 +08001492 DVR_PB_INFO("----first write ts data");
hualing chenb9a02922021-12-14 11:29:47 +08001493 }
1494
Wentao MA270dc0f2022-08-23 13:17:26 +08001495 ret = AmTsPlayer_writeData(player->handle, &input_buffer, write_timeout_ms);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001496 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001497 player->ts_cache_len = 0;
hualing chen21a40372021-10-29 11:07:26 +08001498 pthread_mutex_unlock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +08001499 real_read = 0;
1500 write_success++;
hualing chend241c7a2021-06-22 13:34:27 +08001501 if (CONTROL_SPEED_ENABLE == 1) {
1502check0:
1503 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001504 dvr_mutex_lock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001505 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001506 dvr_mutex_unlock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001507 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1508 goto check0;
1509 }
1510 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001511 //DVR_PB_INFO("write write_success:%d input_buffer.buf_size:%d", write_success, input_buffer.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001512 continue;
hualing chen87072a82020-03-12 16:20:12 +08001513 } else {
hualing chen21a40372021-10-29 11:07:26 +08001514 pthread_mutex_unlock(&player->segment_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08001515 DVR_PB_INFO("write time out write_success:%d input_buffer.buf_size:%d systime:%u",
hualing chen03fd4942021-07-15 15:56:41 +08001516 write_success,
Wentao MA270dc0f2022-08-23 13:17:26 +08001517 input_buffer.buf_size,
hualing chen03fd4942021-07-15 15:56:41 +08001518 _dvr_time_getClock());
1519
hualing chena540a7e2020-03-27 16:44:05 +08001520 write_success = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001521 if (CONTROL_SPEED_ENABLE == 1) {
1522check1:
1523 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001524 dvr_mutex_lock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001525 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001526 dvr_mutex_unlock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001527 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1528 goto check1;
1529 }
1530 }
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001531 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001532 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001533 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001534 if (!player->is_running) {
Wentao MA96f68962022-06-15 19:45:35 +08001535 DVR_PB_INFO("playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001536 break;
1537 }
hualing chen2aba4022020-03-02 13:49:55 +08001538 goto_rewrite = DVR_TRUE;
1539 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001540 }
1541 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001542end:
Wentao MA96f68962022-06-15 19:45:35 +08001543 DVR_PB_INFO("playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001544 free(buf);
1545 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001546 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001547}
1548
1549
hualing chen040df222020-01-17 13:35:02 +08001550static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001551{
hualing chen040df222020-01-17 13:35:02 +08001552 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001553
1554 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001555 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001556 return DVR_FAILURE;
1557 }
Wentao MA96f68962022-06-15 19:45:35 +08001558 DVR_PB_INFO("start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001559 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001560 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001561 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001562 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001563 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001564 if (rc < 0)
1565 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001566 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001567}
1568
1569
hualing chen040df222020-01-17 13:35:02 +08001570static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001571{
hualing chen040df222020-01-17 13:35:02 +08001572 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001573
1574 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001575 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001576 return DVR_FAILURE;
1577 }
1578
Wentao MA96f68962022-06-15 19:45:35 +08001579 DVR_PB_INFO("stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001580 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001581 {
1582 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001583 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001584 pthread_join(player->playback_thread, NULL);
1585 }
1586 if (player->r_handle) {
1587 segment_close(player->r_handle);
1588 player->r_handle = NULL;
1589 }
Wentao MA96f68962022-06-15 19:45:35 +08001590 DVR_PB_INFO(":end");
hualing chen86e7d482020-01-16 15:13:33 +08001591 return 0;
1592}
1593
hualing chen1679f812021-11-08 15:17:46 +08001594static int getFakePid()
1595{
1596 char fake_pid_prop[] = "vendor.tv.dtv.fake_pid";
1597 char buf[32];
1598 int pid = 0xffff;
1599
1600 dvr_prop_read(fake_pid_prop, buf, sizeof(buf));
1601
1602 if (sscanf(buf, "%i", &pid) != 1)
1603 {
Wentao MA96f68962022-06-15 19:45:35 +08001604 DVR_PB_INFO("get fake pid error");
hualing chen1679f812021-11-08 15:17:46 +08001605 pid = 0xffff;
1606 }
1607 return pid;
1608}
1609
1610void dvr_playback_change_seek_state(DVR_PlaybackHandle_t handle,int pid) {
1611
1612 DVR_ASSERT(handle);
1613 DVR_Playback_t *player = (DVR_Playback_t *) handle;
1614 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001615 DVR_PB_INFO("player is NULL");
hualing chen1679f812021-11-08 15:17:46 +08001616 return ;
1617 }
1618 if (player->need_seek_start == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001619 DVR_PB_INFO("player need_seek_start is false");
hualing chen1679f812021-11-08 15:17:46 +08001620 return ;
1621 }
1622
hualing chena5f03222021-12-02 11:22:35 +08001623 if (pid != player->fake_pid) {
hualing chen1679f812021-11-08 15:17:46 +08001624 player->need_seek_start = DVR_FALSE;
1625 }
Wentao MA96f68962022-06-15 19:45:35 +08001626 DVR_PB_INFO("player player->need_seek_start=%d", player->need_seek_start);
hualing chen1679f812021-11-08 15:17:46 +08001627}
1628
hualing chenb31a6c62020-01-13 17:27:00 +08001629/**\brief Open an dvr palyback
1630 * \param[out] p_handle dvr playback addr
1631 * \param[in] params dvr playback open parameters
1632 * \retval DVR_SUCCESS On success
1633 * \return Error code
1634 */
hualing chen040df222020-01-17 13:35:02 +08001635int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001636
hualing chen040df222020-01-17 13:35:02 +08001637 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001638 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001639
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001640 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001641
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001642 dvr_mutex_init(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001643 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001644 pthread_condattr_init(&cattr);
1645 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1646 pthread_cond_init(&player->cond, &cattr);
1647 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001648
hualing chen5cbe1a62020-02-10 16:36:36 +08001649 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001650 INIT_LIST_HEAD(&player->segment_list);
1651 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1652 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001653 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001654 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00001655 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen86e7d482020-01-16 15:13:33 +08001656 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001657 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001658 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001659
hualing chen86e7d482020-01-16 15:13:33 +08001660 //store open params
hualing chen040df222020-01-17 13:35:02 +08001661 player->openParams.dmx_dev_id = params->dmx_dev_id;
1662 player->openParams.block_size = params->block_size;
Wentao MA96f68962022-06-15 19:45:35 +08001663 DVR_PB_INFO("playback open block_size:[%d]",params->block_size);
hualing chen86e7d482020-01-16 15:13:33 +08001664 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001665 player->openParams.event_fn = params->event_fn;
1666 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001667 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001668 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001669
hualing chen5cbe1a62020-02-10 16:36:36 +08001670 player->has_pids = params->has_pids;
1671
hualing chen2aba4022020-03-02 13:49:55 +08001672 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001673
1674 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1675 //for test get callback
1676 if (0 && player->player_callback_func == NULL) {
1677 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1678 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
Wentao MA96f68962022-06-15 19:45:35 +08001679 DVR_PB_INFO("playback open get callback[%p][%p][%p][%p]",
hualing chen03fd4942021-07-15 15:56:41 +08001680 player->player_callback_func,
1681 player->player_callback_userdata,
1682 _dvr_tsplayer_callback_test,
1683 player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001684 }
1685 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001686
hualing chen86e7d482020-01-16 15:13:33 +08001687 //init has audio and video
1688 player->has_video = DVR_FALSE;
1689 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001690 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001691 player->last_segment_id = 0LL;
1692 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001693
hualing chen5cbe1a62020-02-10 16:36:36 +08001694 //init ff fb time
hualing chen7ea70a72021-09-09 11:25:13 +08001695 player->fffb_current = 0;
1696 player->fffb_start = 0;
hualing chen03fd4942021-07-15 15:56:41 +08001697 player->fffb_start_pcr = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001698 //seek time
1699 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001700 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001701
Yahui Han1fbf3292021-11-08 18:17:19 +08001702 //allocate cryptor if have clearkey
1703 if (params->keylen > 0) {
1704 player->cryptor = am_crypt_des_open((uint8_t *)params->clearkey,
hualing chen002e5b92022-02-23 17:51:21 +08001705 (uint8_t *)params->cleariv,
1706 params->keylen * 8);
Yahui Han1fbf3292021-11-08 18:17:19 +08001707 if (!player->cryptor) {
Wentao MA96f68962022-06-15 19:45:35 +08001708 DVR_INFO("%s , open des cryptor failed!!!\n", __func__);
Yahui Han1fbf3292021-11-08 18:17:19 +08001709 }
1710 } else {
1711 player->cryptor = NULL;
1712 }
1713
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001714 //init secure stuff
1715 player->dec_func = NULL;
1716 player->dec_userdata = NULL;
1717 player->is_secure_mode = 0;
1718 player->secure_buffer = NULL;
1719 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001720 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001721
hualing chen4b7c15d2020-04-07 16:13:48 +08001722 player->fffb_play = DVR_FALSE;
1723
1724 player->last_send_time_id = UINT64_MAX;
1725 player->last_cur_time = 0;
hualing chen30423862021-04-16 14:39:12 +08001726 player->seek_pause = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001727
hualing chend241c7a2021-06-22 13:34:27 +08001728 //speed con init
1729 if (CONTROL_SPEED_ENABLE == 1) {
1730 player->con_spe.ply_dur = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001731 player->con_spe.ply_sta = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001732 player->con_spe.sys_dur = 0;
1733 player->con_spe.sys_sta = 0;
1734 }
1735
hualing chen03fd4942021-07-15 15:56:41 +08001736 //limit info
1737 player->rec_start = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001738 player->limit = 0;
hualing chen8a657f32021-08-30 13:12:49 +08001739 //need seek to start pos
1740 player->first_start_time = 0;
Wentao MA01de0e62022-01-10 18:48:23 +08001741 player->first_start_id = UINT64_MAX;
1742 player->check_cache_flag = DVR_TRUE;
hualing chen8a657f32021-08-30 13:12:49 +08001743 player->need_seek_start = DVR_TRUE;
hualing chena5f03222021-12-02 11:22:35 +08001744 //fake_pid init
1745 player->fake_pid = getFakePid();
hualing chen86e7d482020-01-16 15:13:33 +08001746 *p_handle = player;
1747 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001748}
1749
1750/**\brief Close an dvr palyback
1751 * \param[in] handle playback handle
1752 * \retval DVR_SUCCESS On success
1753 * \return Error code
1754 */
hualing chen040df222020-01-17 13:35:02 +08001755int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001756
hualing chen86e7d482020-01-16 15:13:33 +08001757 DVR_ASSERT(handle);
Wentao MA96f68962022-06-15 19:45:35 +08001758 DVR_PB_INFO(":into");
hualing chen040df222020-01-17 13:35:02 +08001759 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001760 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001761 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001762 return DVR_FAILURE;
1763 }
1764
hualing chencc91e1c2020-02-28 13:26:17 +08001765 if (player->state != DVR_PLAYBACK_STATE_STOP)
1766 {
Wentao MA96f68962022-06-15 19:45:35 +08001767 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
Yahui Han1fbf3292021-11-08 18:17:19 +08001768 if (player->cryptor) {
1769 am_crypt_des_close(player->cryptor);
1770 player->cryptor = NULL;
1771 }
hualing chencc91e1c2020-02-28 13:26:17 +08001772 dvr_playback_stop(handle, DVR_TRUE);
Wentao MA96f68962022-06-15 19:45:35 +08001773 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
hualing chenb96aa2c2020-04-15 14:13:53 +08001774 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001775 DVR_PB_INFO(":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001776 }
Wentao MA96f68962022-06-15 19:45:35 +08001777 DVR_PB_INFO(":into");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001778 dvr_mutex_destroy(&player->lock);
Zhiqiang Hanc9513462022-06-21 09:55:23 +08001779 pthread_mutex_destroy(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001780 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001781
1782 if (player) {
1783 free(player);
1784 player = NULL;
1785 }
Wentao MA96f68962022-06-15 19:45:35 +08001786 DVR_PB_INFO(":end");
hualing chen86e7d482020-01-16 15:13:33 +08001787 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001788}
1789
Wentao MA270dc0f2022-08-23 13:17:26 +08001790/**\brief Start play audio and video, used start audio api and start video api
hualing chenb31a6c62020-01-13 17:27:00 +08001791 * \param[in] handle playback handle
1792 * \param[in] params audio playback params,contains fmt and pid...
1793 * \retval DVR_SUCCESS On success
1794 * \return Error code
1795 */
hualing chen040df222020-01-17 13:35:02 +08001796int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1797 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA270dc0f2022-08-23 13:17:26 +08001798 am_tsplayer_video_params video_params;
1799 am_tsplayer_audio_params audio_params;
1800 am_tsplayer_audio_params ad_params;
hualing chena540a7e2020-03-27 16:44:05 +08001801
Wentao MA270dc0f2022-08-23 13:17:26 +08001802 memset(&video_params, 0, sizeof(video_params));
1803 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001804
hualing chena540a7e2020-03-27 16:44:05 +08001805 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001806 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001807 return DVR_FAILURE;
1808 }
hualing chencc91e1c2020-02-28 13:26:17 +08001809 uint64_t segment_id = player->cur_segment_id;
Wentao MA96f68962022-06-15 19:45:35 +08001810 DVR_PB_INFO("[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001811
hualing chena540a7e2020-03-27 16:44:05 +08001812 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001813 //can used start api to resume playback
1814 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1815 return dvr_playback_resume(handle);
1816 }
hualing chen87072a82020-03-12 16:20:12 +08001817 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001818 //if flag is paused and not decode first frame. if user resume, we need
hualing chen9b434f02020-06-10 15:06:54 +08001819 //clear flag and set trickmode none
1820 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08001821 DVR_PB_INFO("[%p]clear pause live flag and clear trick mode", handle);
hualing chen9b434f02020-06-10 15:06:54 +08001822 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1823 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1824 }
Wentao MA96f68962022-06-15 19:45:35 +08001825 DVR_PB_INFO("stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001826 return DVR_SUCCESS;
1827 }
hualing chen86e7d482020-01-16 15:13:33 +08001828 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001829 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001830 //get segment info and audio video pid fmt ;
Wentao MA96f68962022-06-15 19:45:35 +08001831 DVR_PB_INFO("lock flag:0x%x", flag);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001832 dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08001833 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen86e7d482020-01-16 15:13:33 +08001834 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08001835 if (video_params.pid != player->fake_pid && !VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
1836 //audio and video pids are all invalid, return error.
1837 DVR_PB_ERROR("unlock dvr play back start error, not found audio and video info [0x%x]", video_params.pid);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001838 dvr_mutex_unlock(&player->lock);
hualing chencc91e1c2020-02-28 13:26:17 +08001839 DVR_Play_Notify_t notify;
1840 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1841 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1842 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1843 notify.info.transition_failed_data.segment_id = segment_id;
1844 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001845 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001846 return -1;
1847 }
hualing chen31140872020-03-25 12:29:26 +08001848
hualing chencc91e1c2020-02-28 13:26:17 +08001849 {
Wentao MA270dc0f2022-08-23 13:17:26 +08001850 if (VALID_PID(video_params.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001851 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001852 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001853 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08001854 DVR_PB_INFO("set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001855 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1856 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001857 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
Wentao MA96f68962022-06-15 19:45:35 +08001858 DVR_PB_INFO("set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001859 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001860 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001861 DVR_PB_INFO("set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001862 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001863 }
hualing chena93bbbc2020-12-22 17:23:42 +08001864 AmTsPlayer_showVideo(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08001865 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08001866 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08001867 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001868 }
hualing chena540a7e2020-03-27 16:44:05 +08001869
Wentao MA270dc0f2022-08-23 13:17:26 +08001870 DVR_PB_INFO("player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, video_params.pid, audio_params.pid);
hualing chen4b7c15d2020-04-07 16:13:48 +08001871 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001872 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1873 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1874 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00001875 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chencc91e1c2020-02-28 13:26:17 +08001876 } else {
1877 player->cmd.last_cmd = player->cmd.cur_cmd;
1878 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001879 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001880 //set fast play
Wentao MA96f68962022-06-15 19:45:35 +08001881 DVR_PB_INFO("start fast");
hualing chen31140872020-03-25 12:29:26 +08001882 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001883 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08001884 if (VALID_PID(ad_params.pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08001885 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08001886 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08001887 dvr_playback_change_seek_state(handle, ad_params.pid);
1888 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08001889 AmTsPlayer_enableADMix(player->handle);
1890 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001891 if (VALID_PID(audio_params.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08001892 DVR_PB_INFO("start audio");
hualing chen969fe7b2021-05-26 15:13:17 +08001893 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08001894 dvr_playback_change_seek_state(handle, audio_params.pid);
1895 AmTsPlayer_setAudioParams(player->handle, &audio_params);
hualing chen969fe7b2021-05-26 15:13:17 +08001896 AmTsPlayer_startAudioDecoding(player->handle);
1897 }
hualing chen31140872020-03-25 12:29:26 +08001898 }
hualing chencc91e1c2020-02-28 13:26:17 +08001899 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00001900 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chencc91e1c2020-02-28 13:26:17 +08001901 }
hualing chen86e7d482020-01-16 15:13:33 +08001902 }
hualing chen43a89bc2022-01-19 14:31:20 +08001903#ifdef AVSYNC_USED_PCR
1904 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08001905 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08001906 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
1907 }
1908#endif
Wentao MA96f68962022-06-15 19:45:35 +08001909 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001910 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001911 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001912 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001913}
hualing chen040df222020-01-17 13:35:02 +08001914/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001915 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001916 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001917 * \retval DVR_SUCCESS On success
1918 * \return Error code
1919 */
hualing chen040df222020-01-17 13:35:02 +08001920int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1921 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001922
hualing chena540a7e2020-03-27 16:44:05 +08001923 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001924 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001925 return DVR_FAILURE;
1926 }
1927
Wentao MA96f68962022-06-15 19:45:35 +08001928 DVR_PB_INFO("add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001929 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001930
hualing chen040df222020-01-17 13:35:02 +08001931 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1932 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001933
Wentao MA270dc0f2022-08-23 13:17:26 +08001934 //not memcpy chunk info.
hualing chen040df222020-01-17 13:35:02 +08001935 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001936 //cp location
hualing chen040df222020-01-17 13:35:02 +08001937 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001938
Wentao MA96f68962022-06-15 19:45:35 +08001939 DVR_PB_INFO("add location [%s]id[%lld]flag[%x]", segment->location, segment->segment_id, info->flags);
hualing chen040df222020-01-17 13:35:02 +08001940 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001941
1942 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001943 segment->pids.video.pid = info->pids.video.pid;
1944 segment->pids.video.format = info->pids.video.format;
1945 segment->pids.video.type = info->pids.video.type;
1946
hualing chen2aba4022020-03-02 13:49:55 +08001947 segment->pids.audio.pid = info->pids.audio.pid;
1948 segment->pids.audio.format = info->pids.audio.format;
1949 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001950
hualing chen2aba4022020-03-02 13:49:55 +08001951 segment->pids.ad.pid = info->pids.ad.pid;
1952 segment->pids.ad.format = info->pids.ad.format;
1953 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001954
1955 segment->pids.pcr.pid = info->pids.pcr.pid;
1956
Wentao MA96f68962022-06-15 19:45:35 +08001957 DVR_PB_INFO("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);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001958 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001959 list_add_tail(&segment->head, &player->segment_list);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001960 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001961 DVR_PB_DEBUG("unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001962
hualing chen5cbe1a62020-02-10 16:36:36 +08001963 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001964}
hualing chen040df222020-01-17 13:35:02 +08001965/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001966 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001967 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001968 * \retval DVR_SUCCESS On success
1969 * \return Error code
1970 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001971int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001972 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA96f68962022-06-15 19:45:35 +08001973 DVR_PB_INFO("remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001974 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001975 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001976 return DVR_FAILURE;
1977 }
1978
hualing chencc91e1c2020-02-28 13:26:17 +08001979 if (segment_id == player->cur_segment_id) {
Wentao MA9a164002022-08-29 11:20:24 +08001980 DVR_PB_INFO("not support remove current segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001981 return DVR_FAILURE;
1982 }
Wentao MA96f68962022-06-15 19:45:35 +08001983 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001984 dvr_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001985 DVR_PlaybackSegmentInfo_t *segment = NULL;
1986 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1987 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001988 {
hualing chen040df222020-01-17 13:35:02 +08001989 if (segment->segment_id == segment_id) {
1990 list_del(&segment->head);
1991 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001992 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001993 }
hualing chen86e7d482020-01-16 15:13:33 +08001994 }
Wentao MA96f68962022-06-15 19:45:35 +08001995 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001996 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001997
1998 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001999}
hualing chen040df222020-01-17 13:35:02 +08002000/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08002001 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08002002 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08002003 * \retval DVR_SUCCESS On success
2004 * \return Error code
2005 */
hualing chen040df222020-01-17 13:35:02 +08002006int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08002007 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08002008 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA96f68962022-06-15 19:45:35 +08002009 DVR_PB_INFO("update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08002010 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002011 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002012 return DVR_FAILURE;
2013 }
hualing chenf43b8ba2020-07-28 13:11:42 +08002014 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
Wentao MA96f68962022-06-15 19:45:35 +08002015 DVR_PB_INFO("vendor is amlogic. not hide or show av and update segment");
hualing chenf43b8ba2020-07-28 13:11:42 +08002016 return DVR_SUCCESS;
2017 }
hualing chena540a7e2020-03-27 16:44:05 +08002018
hualing chen040df222020-01-17 13:35:02 +08002019 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002020 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002021 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08002022 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002023 {
hualing chen040df222020-01-17 13:35:02 +08002024 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08002025 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08002026 }
hualing chen86e7d482020-01-16 15:13:33 +08002027 // if encramble to free, only set flag and return;
2028
2029 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08002030 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002031 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
2032 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08002033 //disable display, mute
Wentao MA96f68962022-06-15 19:45:35 +08002034 DVR_PB_INFO("mute av");
hualing chen2aba4022020-03-02 13:49:55 +08002035 AmTsPlayer_hideVideo(player->handle);
2036 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002037 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
2038 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08002039 //enable display, unmute
Wentao MA96f68962022-06-15 19:45:35 +08002040 DVR_PB_INFO("unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08002041 AmTsPlayer_showVideo(player->handle);
2042 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08002043 } else {
2044 //do nothing
2045 }
2046 } else {
2047 //do nothing
2048 }
2049 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08002050 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08002051 }
Wentao MA96f68962022-06-15 19:45:35 +08002052 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002053 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002054 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002055}
2056
2057
hualing chen99508642021-10-18 15:41:17 +08002058static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_PlaybackPids_t now_pids, DVR_PlaybackPids_t set_pids, int type) {
hualing chen040df222020-01-17 13:35:02 +08002059 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen275379e2021-06-15 17:57:21 +08002060 DVR_StreamInfo_t set_pid;
hualing chen99508642021-10-18 15:41:17 +08002061 DVR_StreamInfo_t now_pid;
hualing chen275379e2021-06-15 17:57:21 +08002062
2063 if (type == 0) {
2064 set_pid = set_pids.video;
hualing chen99508642021-10-18 15:41:17 +08002065 now_pid = now_pids.video;
hualing chen275379e2021-06-15 17:57:21 +08002066 } else if (type == 1) {
2067 set_pid = set_pids.audio;
hualing chen99508642021-10-18 15:41:17 +08002068 now_pid = now_pids.audio;
hualing chen275379e2021-06-15 17:57:21 +08002069 } else if (type == 2) {
2070 set_pid = set_pids.ad;
hualing chen99508642021-10-18 15:41:17 +08002071 now_pid = now_pids.ad;
hualing chen275379e2021-06-15 17:57:21 +08002072 } else {
2073 set_pid = set_pids.pcr;
hualing chen99508642021-10-18 15:41:17 +08002074 now_pid = now_pids.pcr;
hualing chen275379e2021-06-15 17:57:21 +08002075 }
2076
hualing chena540a7e2020-03-27 16:44:05 +08002077 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002078 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002079 return DVR_FAILURE;
2080 }
Wentao MA96f68962022-06-15 19:45:35 +08002081 DVR_PB_INFO(" do check");
hualing chen86e7d482020-01-16 15:13:33 +08002082 if (now_pid.pid == set_pid.pid) {
2083 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08002084 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08002085 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08002086 if (VALID_PID(now_pid.pid)) {
2087 //stop now stream
2088 if (type == 0) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002089 //stop video
hualing chenc70a8df2020-05-12 19:23:11 +08002090 if (player->has_video == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08002091 DVR_PB_INFO("stop video");
hualing chenc70a8df2020-05-12 19:23:11 +08002092 AmTsPlayer_stopVideoDecoding(player->handle);
2093 player->has_video = DVR_FALSE;
2094 }
hualing chen86e7d482020-01-16 15:13:33 +08002095 } else if (type == 1) {
2096 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08002097 if (player->has_audio == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08002098 DVR_PB_INFO("stop audio");
hualing chenc70a8df2020-05-12 19:23:11 +08002099 AmTsPlayer_stopAudioDecoding(player->handle);
2100 player->has_audio = DVR_FALSE;
2101 }
hualing chen86e7d482020-01-16 15:13:33 +08002102 } else if (type == 2) {
2103 //stop sub audio
Wentao MA96f68962022-06-15 19:45:35 +08002104 DVR_PB_INFO("stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08002105 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002106 } else if (type == 3) {
2107 //pcr
2108 }
2109 }
2110 if (VALID_PID(set_pid.pid)) {
2111 //start
2112 if (type == 0) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002113 //start video
2114 am_tsplayer_video_params video_params;
2115 video_params.pid = set_pid.pid;
2116 video_params.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002117 player->has_video = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002118 DVR_PB_INFO("start video pid[%d]fmt[%d]",video_params.pid, video_params.codectype);
2119 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen2aba4022020-03-02 13:49:55 +08002120 AmTsPlayer_startVideoDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002121 //playback_device_video_start(player->handle,&video_params);
hualing chen86e7d482020-01-16 15:13:33 +08002122 } else if (type == 1) {
2123 //start audio
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002124 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen275379e2021-06-15 17:57:21 +08002125 if (VALID_PID(set_pids.ad.pid)) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002126 am_tsplayer_audio_params ad_params;
2127 ad_params.pid = set_pids.ad.pid;
2128 ad_params.codectype= _dvr_convert_stream_fmt(set_pids.ad.format, DVR_TRUE);
2129 DVR_PB_INFO("start ad audio pid[%d]fmt[%d]",ad_params.pid, ad_params.codectype);
2130 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chen275379e2021-06-15 17:57:21 +08002131 AmTsPlayer_enableADMix(player->handle);
2132 }
2133
Wentao MA270dc0f2022-08-23 13:17:26 +08002134 am_tsplayer_audio_params audio_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002135
Wentao MA270dc0f2022-08-23 13:17:26 +08002136 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002137
Wentao MA270dc0f2022-08-23 13:17:26 +08002138 audio_params.pid = set_pid.pid;
2139 audio_params.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
hualing chenc70a8df2020-05-12 19:23:11 +08002140 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002141 DVR_PB_INFO("start audio pid[%d]fmt[%d]",audio_params.pid, audio_params.codectype);
2142 AmTsPlayer_setAudioParams(player->handle, &audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002143 AmTsPlayer_startAudioDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002144 //playback_device_audio_start(player->handle,&audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002145 }
hualing chen86e7d482020-01-16 15:13:33 +08002146 } else if (type == 2) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002147 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen99508642021-10-18 15:41:17 +08002148 if (set_pids.audio.pid == now_pids.audio.pid) {
2149 //stop audio if audio pid not change
Wentao MA96f68962022-06-15 19:45:35 +08002150 DVR_PB_INFO("stop audio when start ad");
hualing chen99508642021-10-18 15:41:17 +08002151 AmTsPlayer_stopAudioDecoding(player->handle);
2152 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002153 am_tsplayer_audio_params audio_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002154
Wentao MA270dc0f2022-08-23 13:17:26 +08002155 memset(&audio_params, 0, sizeof(audio_params));
2156 audio_params.pid = set_pid.pid;
2157 audio_params.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
hualing chenc70a8df2020-05-12 19:23:11 +08002158 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002159 DVR_PB_INFO("start ad audio pid[%d]fmt[%d]",audio_params.pid, audio_params.codectype);
2160 AmTsPlayer_setADParams(player->handle, &audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002161 AmTsPlayer_enableADMix(player->handle);
hualing chen99508642021-10-18 15:41:17 +08002162
2163 if (set_pids.audio.pid == now_pids.audio.pid) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002164 am_tsplayer_audio_params audio_params;
hualing chen99508642021-10-18 15:41:17 +08002165
Wentao MA270dc0f2022-08-23 13:17:26 +08002166 memset(&audio_params, 0, sizeof(audio_params));
hualing chen99508642021-10-18 15:41:17 +08002167
Wentao MA270dc0f2022-08-23 13:17:26 +08002168 audio_params.pid = set_pids.audio.pid;
2169 audio_params.codectype= _dvr_convert_stream_fmt(set_pids.audio.format, DVR_TRUE);
hualing chen99508642021-10-18 15:41:17 +08002170 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002171 DVR_PB_INFO("restart audio when start ad");
Wentao MA270dc0f2022-08-23 13:17:26 +08002172 AmTsPlayer_setAudioParams(player->handle, &audio_params);
hualing chen99508642021-10-18 15:41:17 +08002173 AmTsPlayer_startAudioDecoding(player->handle);
2174 }
hualing chenc70a8df2020-05-12 19:23:11 +08002175 }
hualing chen86e7d482020-01-16 15:13:33 +08002176 } else if (type == 3) {
2177 //pcr
Wentao MA96f68962022-06-15 19:45:35 +08002178 DVR_PB_INFO("start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08002179 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002180 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002181 //audio and video all close
2182 if (!player->has_audio && !player->has_video) {
Wentao MA907b6432022-08-01 06:23:08 +00002183 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen5cbe1a62020-02-10 16:36:36 +08002184 }
hualing chen43a89bc2022-01-19 14:31:20 +08002185 } else if (type == 2) {
2186 //case disable ad
Wentao MA96f68962022-06-15 19:45:35 +08002187 DVR_PB_INFO("restart audio when stop ad");
hualing chen43a89bc2022-01-19 14:31:20 +08002188 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
Wentao MA6d045b32022-02-18 18:47:25 +08002189 if (VALID_PID(now_pids.audio.pid)) {
2190 //stop audio if audio pid not change
Wentao MA96f68962022-06-15 19:45:35 +08002191 DVR_PB_INFO("stop audio when stop ad pid [0x%x]", now_pids.audio.pid);
Wentao MA6d045b32022-02-18 18:47:25 +08002192 AmTsPlayer_stopAudioDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002193 am_tsplayer_audio_params audio_params;
hualing chen43a89bc2022-01-19 14:31:20 +08002194
Wentao MA270dc0f2022-08-23 13:17:26 +08002195 memset(&audio_params, 0, sizeof(audio_params));
hualing chen43a89bc2022-01-19 14:31:20 +08002196
Wentao MA270dc0f2022-08-23 13:17:26 +08002197 audio_params.pid = now_pids.audio.pid;
2198 audio_params.codectype= _dvr_convert_stream_fmt(now_pids.audio.format, DVR_TRUE);
Wentao MA6d045b32022-02-18 18:47:25 +08002199 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002200 DVR_PB_INFO("restart audio when stop ad");
Wentao MA270dc0f2022-08-23 13:17:26 +08002201 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA6d045b32022-02-18 18:47:25 +08002202 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen43a89bc2022-01-19 14:31:20 +08002203 }
Wentao MA6d045b32022-02-18 18:47:25 +08002204 }
hualing chen86e7d482020-01-16 15:13:33 +08002205 }
2206 }
2207 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08002208}
hualing chena5f03222021-12-02 11:22:35 +08002209/**\brief dvr play back only update segment pids info
2210 * only update pid info not to start stop codec.
2211 * \param[in] handle playback handle
2212 * \param[in] segment_id need updated pids segment id
2213 * \param[in] p_pids need updated pids
2214 * \retval DVR_SUCCESS On success
2215 * \return Error code
2216 */
2217int dvr_playback_only_update_segment_pids(DVR_PlaybackHandle_t handle, uint64_t segment_id, DVR_PlaybackPids_t *p_pids) {
2218 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2219 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002220 DVR_PB_INFO("player is NULL");
hualing chena5f03222021-12-02 11:22:35 +08002221 return DVR_FAILURE;
2222 }
2223
2224 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002225 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002226 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002227 DVR_PB_INFO("get lock update segment id: %lld cur id %lld", segment_id, player->cur_segment_id);
hualing chena5f03222021-12-02 11:22:35 +08002228 list_for_each_entry(segment, &player->segment_list, head)
2229 {
2230 if (segment->segment_id == segment_id) {
2231 if (player->cur_segment_id == segment_id) {
2232 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2233 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2234 //do nothing when ff fb
Wentao MA96f68962022-06-15 19:45:35 +08002235 DVR_PB_INFO("unlock now is ff fb, not to update cur segment info\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002236 dvr_mutex_unlock(&player->lock);
hualing chena5f03222021-12-02 11:22:35 +08002237 return 0;
2238 }
2239 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
2240 }
2241 //save pids info
Wentao MA96f68962022-06-15 19:45:35 +08002242 DVR_PB_INFO(":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chena5f03222021-12-02 11:22:35 +08002243 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +08002244 DVR_PB_INFO(":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chena5f03222021-12-02 11:22:35 +08002245 break;
2246 }
2247 }
Wentao MA96f68962022-06-15 19:45:35 +08002248 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002249 dvr_mutex_unlock(&player->lock);
hualing chena5f03222021-12-02 11:22:35 +08002250 return DVR_SUCCESS;
2251}
2252
hualing chen5cbe1a62020-02-10 16:36:36 +08002253/**\brief dvr play back update segment pids
2254 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08002255 * add pid stream and stop remove pid stream.
2256 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08002257 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08002258 * \retval DVR_SUCCESS On success
2259 * \return Error code
2260 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002261int 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 +08002262 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002263 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002264 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002265 return DVR_FAILURE;
2266 }
2267
hualing chen040df222020-01-17 13:35:02 +08002268 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002269 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002270 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002271 DVR_PB_INFO("get lock update segment id: %lld cur id %lld", segment_id, player->cur_segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08002272
hualing chen040df222020-01-17 13:35:02 +08002273 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002274 {
hualing chen040df222020-01-17 13:35:02 +08002275 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002276
2277 if (player->cur_segment_id == segment_id) {
2278 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2279 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2280 //do nothing when ff fb
Wentao MA96f68962022-06-15 19:45:35 +08002281 DVR_PB_INFO("unlock now is ff fb, not to update cur segment info\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002282 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002283 return 0;
2284 }
2285
2286 //if segment is on going segment,we need stop start stream
2287 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
Wentao MA96f68962022-06-15 19:45:35 +08002288 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002289 dvr_mutex_unlock(&player->lock);
hualing chen8a657f32021-08-30 13:12:49 +08002290 if (segment->pids.audio.pid != p_pids->audio.pid &&
2291 segment->pids.audio.pid == 0x1fff) {
hualing chena5f03222021-12-02 11:22:35 +08002292 //not used this to seek to start pos.we will
Wentao MA9a164002022-08-29 11:20:24 +08002293 //add update only api. if need seek to start
hualing chena5f03222021-12-02 11:22:35 +08002294 //pos, we will call only update api and used seek api
2295 //to start and stop av codec
2296 if (0 && player->need_seek_start == DVR_TRUE) {
hualing chen8a657f32021-08-30 13:12:49 +08002297 player->need_seek_start = DVR_FALSE;
2298 pthread_mutex_lock(&player->segment_lock);
2299 player->drop_ts = DVR_TRUE;
2300 player->ts_cache_len = 0;
2301 if (player->first_start_time > 0)
2302 player->first_start_time = player->first_start_time - 1;
2303 segment_seek(player->r_handle, (uint64_t)(player->first_start_time), player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +08002304 DVR_PB_ERROR("unlock segment update need seek time_offset %llu [0x%x][0x%x]", player->first_start_time, segment->pids.audio.pid, segment->pids.ad.pid);
hualing chen8a657f32021-08-30 13:12:49 +08002305 pthread_mutex_unlock(&player->segment_lock);
2306 }
2307 }
hualing chen1679f812021-11-08 15:17:46 +08002308 //check video pids, stop or restart
2309 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 0);
2310 //check sub audio pids stop or restart
2311 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 2);
2312 //check audio pids stop or restart
2313 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 1);
2314 //check pcr pids stop or restart
2315 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 3);
2316
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002317 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002318 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
2319 //if state is pause, we need process at resume api. we only record change info
2320 int v_cmd = DVR_PLAYBACK_CMD_NONE;
2321 int a_cmd = DVR_PLAYBACK_CMD_NONE;
2322 if (VALID_PID(segment->pids.video.pid)
2323 && VALID_PID(p_pids->video.pid)
2324 && segment->pids.video.pid != p_pids->video.pid) {
2325 //restart video
Wentao MA270dc0f2022-08-23 13:17:26 +08002326 v_cmd = DVR_PLAYBACK_CMD_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002327 }
2328 if (!VALID_PID(segment->pids.video.pid)
2329 && VALID_PID(p_pids->video.pid)
2330 && segment->pids.video.pid != p_pids->video.pid) {
2331 //start video
Wentao MA270dc0f2022-08-23 13:17:26 +08002332 v_cmd = DVR_PLAYBACK_CMD_V_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002333 }
2334 if (VALID_PID(segment->pids.video.pid)
2335 && !VALID_PID(p_pids->video.pid)
2336 && segment->pids.video.pid != p_pids->video.pid) {
2337 //stop video
Wentao MA270dc0f2022-08-23 13:17:26 +08002338 v_cmd = DVR_PLAYBACK_CMD_V_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08002339 }
2340 if (VALID_PID(segment->pids.audio.pid)
2341 && VALID_PID(p_pids->audio.pid)
2342 && segment->pids.audio.pid != p_pids->audio.pid) {
2343 //restart audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002344 a_cmd = DVR_PLAYBACK_CMD_A_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002345 }
2346 if (!VALID_PID(segment->pids.audio.pid)
2347 && VALID_PID(p_pids->audio.pid)
2348 && segment->pids.audio.pid != p_pids->audio.pid) {
2349 //start audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002350 a_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002351 }
2352 if (VALID_PID(segment->pids.audio.pid)
2353 && !VALID_PID(p_pids->audio.pid)
2354 && segment->pids.audio.pid != p_pids->audio.pid) {
2355 //stop audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002356 a_cmd = DVR_PLAYBACK_CMD_A_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08002357 }
2358 if (a_cmd == DVR_PLAYBACK_CMD_NONE
2359 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
2360 //do nothing
2361 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
2362 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
2363 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2364 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
2365 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
2366 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002367 if (v_cmd == DVR_PLAYBACK_CMD_V_RESTART
2368 && (a_cmd == DVR_PLAYBACK_CMD_A_RESTART)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002369 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002370 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
2371 }else if (v_cmd == DVR_PLAYBACK_CMD_V_RESTART
2372 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002373 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002374 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002375 } else {
2376 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002377 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002378 }
2379
Wentao MA270dc0f2022-08-23 13:17:26 +08002380 if (v_cmd == DVR_PLAYBACK_CMD_V_START
2381 && (a_cmd == DVR_PLAYBACK_CMD_A_RESTART)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002382 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002383 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_START_A_RESTART;
2384 } else if (v_cmd == DVR_PLAYBACK_CMD_V_START
2385 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002386 //not occur this case
2387 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2388 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
2389 } else {
2390 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002391 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP_V_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002392 }
2393
Wentao MA270dc0f2022-08-23 13:17:26 +08002394 if (v_cmd == DVR_PLAYBACK_CMD_V_STOP
2395 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002396 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002397 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP_A_START;
2398 } else if (v_cmd == DVR_PLAYBACK_CMD_V_STOP
2399 && a_cmd == DVR_PLAYBACK_CMD_A_RESTART) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002400 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002401 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP_A_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002402 } else {
2403 //not occur this case
2404 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2405 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2406 }
2407 }
2408 }
hualing chene10666f2020-04-14 13:58:37 +08002409 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08002410 }
hualing chen86e7d482020-01-16 15:13:33 +08002411 //save pids info
Wentao MA96f68962022-06-15 19:45:35 +08002412 DVR_PB_INFO(":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08002413 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +08002414 DVR_PB_INFO(":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002415 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002416 }
hualing chen86e7d482020-01-16 15:13:33 +08002417 }
Wentao MA96f68962022-06-15 19:45:35 +08002418 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002419 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002420 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002421}
2422/**\brief Stop play, will stop video and audio
2423 * \param[in] handle playback handle
2424 * \param[in] clear is clear last frame
2425 * \retval DVR_SUCCESS On success
2426 * \return Error code
2427 */
hualing chen040df222020-01-17 13:35:02 +08002428int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
2429 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MAe8ba5172022-08-09 11:18:17 +08002430 DVR_UNUSED(clear);
hualing chena540a7e2020-03-27 16:44:05 +08002431 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002432 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002433 return DVR_FAILURE;
2434 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002435 if (player->state == DVR_PLAYBACK_STATE_STOP) {
Wentao MA96f68962022-06-15 19:45:35 +08002436 DVR_PB_INFO(":playback is stoped");
hualing chenb96aa2c2020-04-15 14:13:53 +08002437 return DVR_SUCCESS;
2438 }
Ke Gong3c0caba2020-04-21 22:58:18 -07002439 if (player->state == DVR_PLAYBACK_STATE_STOP) {
Wentao MA96f68962022-06-15 19:45:35 +08002440 DVR_PB_INFO(":playback is stoped");
Ke Gong3c0caba2020-04-21 22:58:18 -07002441 return DVR_SUCCESS;
2442 }
hualing chen87072a82020-03-12 16:20:12 +08002443 _stop_playback_thread(handle);
Wentao MA96f68962022-06-15 19:45:35 +08002444 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002445 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002446 DVR_PB_INFO(":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08002447 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002448 if (player->has_video) {
2449 AmTsPlayer_resumeVideoDecoding(player->handle);
2450 }
2451 if (player->has_audio) {
2452 AmTsPlayer_resumeAudioDecoding(player->handle);
2453 }
2454 if (player->has_video) {
2455 player->has_video = DVR_FALSE;
hualing chen10cdb162021-02-05 10:44:41 +08002456 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002457 AmTsPlayer_stopVideoDecoding(player->handle);
2458 }
2459 if (player->has_audio) {
2460 player->has_audio = DVR_FALSE;
2461 AmTsPlayer_stopAudioDecoding(player->handle);
2462 }
hualing chendf118dd2020-05-21 15:49:11 +08002463 if (player->has_ad_audio) {
2464 player->has_ad_audio =DVR_FALSE;
2465 AmTsPlayer_disableADMix(player->handle);
2466 }
hualing chen266b9502020-04-04 17:39:39 +08002467
hualing chen86e7d482020-01-16 15:13:33 +08002468 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002469 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2470 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002471 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen87072a82020-03-12 16:20:12 +08002472 player->cur_segment_id = UINT64_MAX;
2473 player->segment_is_open = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08002474 DVR_PB_DEBUG("unlock");
2475 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002476 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002477 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002478}
2479/**\brief Start play audio
2480 * \param[in] handle playback handle
2481 * \param[in] params audio playback params,contains fmt and pid...
2482 * \retval DVR_SUCCESS On success
2483 * \return Error code
2484 */
hualing chen2aba4022020-03-02 13:49:55 +08002485
Wentao MA270dc0f2022-08-23 13:17:26 +08002486int dvr_playback_audio_start(DVR_PlaybackHandle_t handle, am_tsplayer_audio_params *param, am_tsplayer_audio_params *ad_param) {
hualing chen040df222020-01-17 13:35:02 +08002487 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002488
2489 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002490 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002491 return DVR_FAILURE;
2492 }
hualing chen86e7d482020-01-16 15:13:33 +08002493 _start_playback_thread(handle);
2494 //start audio and video
Wentao MA96f68962022-06-15 19:45:35 +08002495 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002496 dvr_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002497
Wentao MA270dc0f2022-08-23 13:17:26 +08002498 if (VALID_PID(ad_param->pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08002499 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002500 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08002501 AmTsPlayer_setADParams(player->handle, ad_param);
hualing chendf118dd2020-05-21 15:49:11 +08002502 AmTsPlayer_enableADMix(player->handle);
2503 }
hualing chen969fe7b2021-05-26 15:13:17 +08002504 if (VALID_PID(param->pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08002505 DVR_PB_INFO("start audio");
hualing chen969fe7b2021-05-26 15:13:17 +08002506 player->has_audio = DVR_TRUE;
2507 AmTsPlayer_setAudioParams(player->handle, param);
2508 AmTsPlayer_startAudioDecoding(player->handle);
2509 }
hualing chendf118dd2020-05-21 15:49:11 +08002510
hualing chen86e7d482020-01-16 15:13:33 +08002511 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002512 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen040df222020-01-17 13:35:02 +08002513 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002514 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002515 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002516 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002517 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002518}
2519/**\brief Stop play audio
2520 * \param[in] handle playback handle
2521 * \retval DVR_SUCCESS On success
2522 * \return Error code
2523 */
hualing chen040df222020-01-17 13:35:02 +08002524int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2525 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002526
2527 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002528 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002529 return DVR_FAILURE;
2530 }
2531
hualing chen2aba4022020-03-02 13:49:55 +08002532 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002533 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002534 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002535 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
Wentao MA9a164002022-08-29 11:20:24 +08002536 //destroy thread
hualing chen86e7d482020-01-16 15:13:33 +08002537 _stop_playback_thread(handle);
2538 } else {
2539 //do nothing.video is playing
2540 }
Wentao MA96f68962022-06-15 19:45:35 +08002541 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002542 dvr_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002543
hualing chenf00cdc82020-06-10 14:23:35 +08002544 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002545 player->has_audio = DVR_FALSE;
2546 AmTsPlayer_stopAudioDecoding(player->handle);
2547 }
hualing chen87072a82020-03-12 16:20:12 +08002548
hualing chendf118dd2020-05-21 15:49:11 +08002549 if (player->has_ad_audio) {
2550 player->has_ad_audio =DVR_FALSE;
2551 AmTsPlayer_disableADMix(player->handle);
2552 }
2553
hualing chen87072a82020-03-12 16:20:12 +08002554 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002555 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002556
Wentao MA96f68962022-06-15 19:45:35 +08002557 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002558 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002559 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002560}
2561/**\brief Start play video
2562 * \param[in] handle playback handle
2563 * \param[in] params video playback params,contains fmt and pid...
2564 * \retval DVR_SUCCESS On success
2565 * \return Error code
2566 */
hualing chen2aba4022020-03-02 13:49:55 +08002567int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002568 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002569
2570 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002571 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002572 return DVR_FAILURE;
2573 }
2574
hualing chen86e7d482020-01-16 15:13:33 +08002575 _start_playback_thread(handle);
2576 //start audio and video
Wentao MA96f68962022-06-15 19:45:35 +08002577 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002578 dvr_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002579 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002580 AmTsPlayer_setVideoParams(player->handle, param);
hualing chen21a40372021-10-29 11:07:26 +08002581 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chena540a7e2020-03-27 16:44:05 +08002582 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002583
2584 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002585 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002586 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08002587 DVR_PB_INFO("settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002588 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2589 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002590 }
2591 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002592 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_START;
hualing chen040df222020-01-17 13:35:02 +08002593 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002594 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002595 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002596 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002597 return DVR_SUCCESS;
2598}
2599/**\brief Stop play video
2600 * \param[in] handle playback handle
2601 * \retval DVR_SUCCESS On success
2602 * \return Error code
2603 */
hualing chen040df222020-01-17 13:35:02 +08002604int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2605 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002606
2607 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002608 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002609 return DVR_FAILURE;
2610 }
2611
hualing chen86e7d482020-01-16 15:13:33 +08002612 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002613 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002614 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
Wentao MA9a164002022-08-29 11:20:24 +08002615 //destroy thread
hualing chen86e7d482020-01-16 15:13:33 +08002616 _stop_playback_thread(handle);
2617 } else {
2618 //do nothing.audio is playing
2619 }
hualing chen7a56cba2020-04-14 14:09:27 +08002620
Wentao MA96f68962022-06-15 19:45:35 +08002621 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002622 dvr_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002623
hualing chen87072a82020-03-12 16:20:12 +08002624 player->has_video = DVR_FALSE;
2625
2626 AmTsPlayer_stopVideoDecoding(player->handle);
2627 //playback_device_video_stop(player->handle);
2628
2629 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002630 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002631
Wentao MA96f68962022-06-15 19:45:35 +08002632 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002633 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002634 return DVR_SUCCESS;
2635}
2636/**\brief Pause play
2637 * \param[in] handle playback handle
2638 * \param[in] flush whether its internal buffers should be flushed
2639 * \retval DVR_SUCCESS On success
2640 * \return Error code
2641 */
hualing chen040df222020-01-17 13:35:02 +08002642int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2643 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MAe8ba5172022-08-09 11:18:17 +08002644 DVR_UNUSED(flush);
hualing chena540a7e2020-03-27 16:44:05 +08002645 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002646 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002647 return DVR_FAILURE;
2648 }
hualing chenf00cdc82020-06-10 14:23:35 +08002649 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
Wentao MA96f68962022-06-15 19:45:35 +08002650 DVR_PB_INFO("player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002651 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002652 }
Wentao MA96f68962022-06-15 19:45:35 +08002653 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002654 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002655 DVR_PB_DEBUG("get lock");
hualing chen266b9502020-04-04 17:39:39 +08002656 if (player->has_video)
2657 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002658 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002659 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002660
2661 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002662 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2663 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2664 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00002665 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08002666 } else {
2667 player->cmd.last_cmd = player->cmd.cur_cmd;
2668 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2669 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00002670 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08002671 }
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002672 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002673 DVR_PB_DEBUG("unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002674
hualing chen86e7d482020-01-16 15:13:33 +08002675 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002676}
2677
hualing chen5cbe1a62020-02-10 16:36:36 +08002678//not add lock
2679static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2680{
2681 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2682
hualing chena540a7e2020-03-27 16:44:05 +08002683 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002684 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002685 return DVR_FAILURE;
2686 }
2687
hualing chen5cbe1a62020-02-10 16:36:36 +08002688 //get video params and audio params
Wentao MA96f68962022-06-15 19:45:35 +08002689 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002690 dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08002691 am_tsplayer_video_params video_params;
2692 am_tsplayer_audio_params audio_params;
2693 am_tsplayer_audio_params ad_params;
hualing chencc91e1c2020-02-28 13:26:17 +08002694 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002695
Wentao MA270dc0f2022-08-23 13:17:26 +08002696 memset(&video_params, 0, sizeof(video_params));
2697 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002698
Wentao MA270dc0f2022-08-23 13:17:26 +08002699 _dvr_playback_get_playinfo(handle, segmentid, &video_params, &audio_params, &ad_params);
Wentao MA96f68962022-06-15 19:45:35 +08002700 DVR_PB_INFO("unlock cmd: %d", cmd);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002701 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002702
2703 switch (cmd) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002704 case DVR_PLAYBACK_CMD_AV_RESTART:
hualing chen5cbe1a62020-02-10 16:36:36 +08002705 //av restart
Wentao MA270dc0f2022-08-23 13:17:26 +08002706 DVR_PB_INFO("do_cmd av_restart");
hualing chen87072a82020-03-12 16:20:12 +08002707 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002708 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002709 case DVR_PLAYBACK_CMD_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002710 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002711 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002712 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002713 case DVR_PLAYBACK_CMD_V_START:
2714 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002715 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002716 case DVR_PLAYBACK_CMD_V_STOP:
hualing chen2aba4022020-03-02 13:49:55 +08002717 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002718 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002719 case DVR_PLAYBACK_CMD_A_RESTART:
hualing chen5cbe1a62020-02-10 16:36:36 +08002720 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002721 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002722 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002723 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002724 case DVR_PLAYBACK_CMD_A_START:
2725 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002726 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002727 case DVR_PLAYBACK_CMD_A_STOP:
hualing chen2aba4022020-03-02 13:49:55 +08002728 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002729 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002730 case DVR_PLAYBACK_CMD_A_STOP_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002731 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2732 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002733 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002734 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002735 case DVR_PLAYBACK_CMD_A_STOP_V_START:
hualing chen2aba4022020-03-02 13:49:55 +08002736 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002737 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002738 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002739 case DVR_PLAYBACK_CMD_V_STOP_A_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002740 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2741 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002742 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002743 break;
2744 case DVR_PLAYBACK_CMD_STOP:
2745 break;
2746 case DVR_PLAYBACK_CMD_START:
2747 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002748 case DVR_PLAYBACK_CMD_A_START_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002749 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002750 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
2751 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002752 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002753 case DVR_PLAYBACK_CMD_V_START_A_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002754 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002755 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
2756 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002757 break;
2758 case DVR_PLAYBACK_CMD_FF:
2759 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002760 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002761 break;
2762 default:
2763 break;
2764 }
2765 return DVR_SUCCESS;
2766}
2767
2768/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002769 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002770 * \retval DVR_SUCCESS On success
2771 * \return Error code
2772 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002773int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2774 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08002775 uint32_t pos = 0;
hualing chen03fd4942021-07-15 15:56:41 +08002776 uint64_t segmentid = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002777 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002778 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002779 return DVR_FAILURE;
2780 }
2781
hualing chena991aa82021-08-16 10:21:15 +08002782 if (dvr_playback_check_limit(handle)) {
2783 //get id and pos to check if we can seek to this pos
Wentao MA96f68962022-06-15 19:45:35 +08002784 DVR_PB_INFO("player start calculate time");
hualing chena991aa82021-08-16 10:21:15 +08002785 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2786 if (segmentid != player->cur_segment_id ||
2787 (segmentid == player->cur_segment_id &&
2788 pos > _dvr_get_cur_time(handle))) {
2789 //first to seek new pos and to resume
Wentao MA96f68962022-06-15 19:45:35 +08002790 DVR_PB_INFO("seek new pos and to resume");
hualing chena991aa82021-08-16 10:21:15 +08002791 dvr_playback_seek(handle, segmentid, pos);
2792 }
hualing chen7ea70a72021-09-09 11:25:13 +08002793 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002794 DVR_PB_INFO("player is not set limit");
hualing chen03fd4942021-07-15 15:56:41 +08002795 }
hualing chena991aa82021-08-16 10:21:15 +08002796
hualing chen5cbe1a62020-02-10 16:36:36 +08002797 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002798 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002799 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002800 player->first_frame = 0;
2801 if (player->has_video)
2802 AmTsPlayer_pauseVideoDecoding(player->handle);
2803 if (player->has_audio)
2804 AmTsPlayer_pauseAudioDecoding(player->handle);
2805
hualing chen266b9502020-04-04 17:39:39 +08002806 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08002807 DVR_PB_INFO("dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002808 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2809 AmTsPlayer_resumeVideoDecoding(player->handle);
2810 }
2811 if (player->has_audio) {
2812 AmTsPlayer_resumeAudioDecoding(player->handle);
2813 }
2814 //check is has audio param,if has audio .we need start audio,
2815 //we will stop audio when ff fb, if reach end, we will pause.so we need
2816 //start audio when resume play
2817
Wentao MA270dc0f2022-08-23 13:17:26 +08002818 am_tsplayer_video_params video_params;
2819 am_tsplayer_audio_params audio_params;
2820 am_tsplayer_audio_params ad_params;
hualing chen266b9502020-04-04 17:39:39 +08002821 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002822
Wentao MA270dc0f2022-08-23 13:17:26 +08002823 memset(&video_params, 0, sizeof(video_params));
2824 memset(&audio_params, 0, sizeof(audio_params));
2825 _dvr_playback_get_playinfo(handle, segmentid, &video_params, &audio_params, &ad_params);
hualing chen266b9502020-04-04 17:39:39 +08002826 //valid audio pid, start audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002827 if (player->has_ad_audio == DVR_FALSE && VALID_PID(ad_params.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chen969fe7b2021-05-26 15:13:17 +08002828 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002829 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08002830 dvr_playback_change_seek_state(handle, ad_params.pid);
2831 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chen969fe7b2021-05-26 15:13:17 +08002832 AmTsPlayer_enableADMix(player->handle);
2833 }
2834
Wentao MA270dc0f2022-08-23 13:17:26 +08002835 if (player->has_audio == DVR_FALSE && VALID_PID(audio_params.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chen266b9502020-04-04 17:39:39 +08002836 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002837 dvr_playback_change_seek_state(handle, audio_params.pid);
2838 AmTsPlayer_setAudioParams(player->handle, &audio_params);
hualing chen266b9502020-04-04 17:39:39 +08002839 AmTsPlayer_startAudioDecoding(player->handle);
2840 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002841 DVR_PB_INFO("audio_params.pid:%d player->has_audio:%d speed:%d", audio_params.pid, player->has_audio, player->cmd.speed.speed.speed);
hualing chen266b9502020-04-04 17:39:39 +08002842 }
hualing chendf118dd2020-05-21 15:49:11 +08002843
hualing chen87072a82020-03-12 16:20:12 +08002844 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2845 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2846 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002847 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen87072a82020-03-12 16:20:12 +08002848 } else {
2849 player->cmd.last_cmd = player->cmd.cur_cmd;
2850 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2851 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002852 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen87072a82020-03-12 16:20:12 +08002853 }
Wentao MA96f68962022-06-15 19:45:35 +08002854 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002855 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002856 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
Wentao MA96f68962022-06-15 19:45:35 +08002857 DVR_PB_INFO("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002858 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002859 player->first_frame = 0;
2860 if (player->has_video)
2861 AmTsPlayer_pauseVideoDecoding(player->handle);
2862 if (player->has_audio)
2863 AmTsPlayer_pauseAudioDecoding(player->handle);
2864
hualing chene41f4372020-06-06 16:29:17 +08002865 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08002866 DVR_PB_INFO("dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002867 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002868 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002869 }
hualing chen041c4092020-04-05 15:11:50 +08002870 if (player->has_audio)
2871 AmTsPlayer_resumeAudioDecoding(player->handle);
Wentao MA96f68962022-06-15 19:45:35 +08002872 DVR_PB_INFO("set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen9811b212020-10-29 11:21:44 +08002873 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2874 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chend1686e52022-01-05 17:10:42 +08002875 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002876 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002877 DVR_PB_INFO("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002878 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002879 } else {
2880 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2881 {
Wentao MA96f68962022-06-15 19:45:35 +08002882 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002883 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002884 player->first_frame = 0;
2885 if (player->has_video)
2886 AmTsPlayer_pauseVideoDecoding(player->handle);
2887 if (player->has_audio)
2888 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen041c4092020-04-05 15:11:50 +08002889 //clear flag
Wentao MA96f68962022-06-15 19:45:35 +08002890 DVR_PB_INFO("clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002891 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2892 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002893 if (player->has_video) {
2894 AmTsPlayer_resumeVideoDecoding(player->handle);
2895 }
2896 if (player->has_audio)
2897 AmTsPlayer_resumeAudioDecoding(player->handle);
Wentao MA96f68962022-06-15 19:45:35 +08002898 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002899 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002900 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002901 }
2902 return DVR_SUCCESS;
2903}
2904
hualing chena540a7e2020-03-27 16:44:05 +08002905static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2906
2907 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2908 DVR_PlaybackSegmentInfo_t *segment = NULL;
2909 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2910 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2911
2912 list_for_each_entry(segment, &player->segment_list, head)
2913 {
2914 if (segment->segment_id == segment_id) {
2915 cur_segment = segment;
2916 }
2917 if (segment->segment_id == set_seg_id) {
2918 set_segment = segment;
2919 }
2920 if (cur_segment != NULL && set_segment != NULL) {
2921 break;
2922 }
2923 }
2924 if (cur_segment == NULL || set_segment == NULL) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002925 DVR_PB_INFO("set segment or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002926 return DVR_TRUE;
2927 }
2928 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2929 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2930 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2931 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
Wentao MA96f68962022-06-15 19:45:35 +08002932 DVR_PB_INFO("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 +08002933 return DVR_TRUE;
2934 }
Wentao MA96f68962022-06-15 19:45:35 +08002935 DVR_PB_INFO("play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002936 return DVR_FALSE;
2937}
2938
hualing chen03fd4942021-07-15 15:56:41 +08002939/**\brief set limit
2940 * \param[in] handle playback handle
2941 * \param[in] rec start time ms
2942 * \param[in] rec limit time ms
2943 * \retval DVR_SUCCESS On success
2944 * \return Error code
2945 */
hualing chen7ea70a72021-09-09 11:25:13 +08002946int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint32_t time, uint32_t limit)
hualing chen03fd4942021-07-15 15:56:41 +08002947{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2948
2949 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002950 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08002951 return DVR_FAILURE;
2952 }
hualing chen7ea70a72021-09-09 11:25:13 +08002953 _dvr_getClock_sec();
Wentao MA96f68962022-06-15 19:45:35 +08002954 DVR_PB_INFO("lock time %lu limit: %u player->state:%d", time, limit, player->state);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002955 dvr_mutex_lock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08002956 player->rec_start = time;
2957 player->limit = limit;
Wentao MA96f68962022-06-15 19:45:35 +08002958 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002959 dvr_mutex_unlock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08002960 return DVR_SUCCESS;
2961}
2962
hualing chen5cbe1a62020-02-10 16:36:36 +08002963/**\brief seek
2964 * \param[in] handle playback handle
2965 * \param[in] time_offset time offset base cur segment
2966 * \retval DVR_SUCCESS On success
2967 * \return Error code
2968 */
hualing chencc91e1c2020-02-28 13:26:17 +08002969int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002970 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002971 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08002972 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002973 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002974 return DVR_FAILURE;
2975 }
2976
Wentao MA96f68962022-06-15 19:45:35 +08002977 DVR_PB_INFO("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);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002978 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002979
hualing chen86e7d482020-01-16 15:13:33 +08002980 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002981 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002982 DVR_PB_INFO("player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002983
hualing chen5cbe1a62020-02-10 16:36:36 +08002984 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08002985 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08002986 if (ret ==DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +08002987 DVR_PB_INFO("unlock seek error at open segment");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002988 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002989 return DVR_FAILURE;
2990 }
2991 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2992 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08002993 DVR_PB_INFO("is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002994 time_offset = _dvr_get_end_time(handle);
2995 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002996 DVR_PB_INFO("is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002997 time_offset = _dvr_get_end_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08002998 ret = DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08002999 }
3000 }
3001
Wentao MA96f68962022-06-15 19:45:35 +08003002 DVR_PB_INFO("seek open id[%lld]flag[0x%x] time_offset %u",
hualing chen03fd4942021-07-15 15:56:41 +08003003 player->cur_segment.segment_id,
3004 player->cur_segment.flags,
3005 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08003006 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08003007 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3008 //forward playback.not seek end of file
3009 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
3010 //default -2000ms
3011 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
3012 }
hualing chen86e7d482020-01-16 15:13:33 +08003013 }
Wentao MA01de0e62022-01-10 18:48:23 +08003014 // Seek can be regarded as a new playback, so keep the start segment_id for it also
hualing chen8a657f32021-08-30 13:12:49 +08003015 if (player->need_seek_start == DVR_TRUE) {
3016 player->first_start_time = (uint64_t)time_offset + 1;//set first start time not eq 0
Wentao MA01de0e62022-01-10 18:48:23 +08003017 player->first_start_id = player->cur_segment.segment_id;
hualing chen8a657f32021-08-30 13:12:49 +08003018 }
hualing chen2aba4022020-03-02 13:49:55 +08003019 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08003020 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08003021 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08003022 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +08003023 DVR_PB_ERROR("seek get offset by time offset, offset=%d time_offset %u",offset, time_offset);
hualing chen2aba4022020-03-02 13:49:55 +08003024 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08003025 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08003026
hualing chen2aba4022020-03-02 13:49:55 +08003027 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08003028
3029 player->last_send_time_id = UINT64_MAX;
Wentao MA270dc0f2022-08-23 13:17:26 +08003030 player->last_segment_total = 0LL;
hualing chen03fd4942021-07-15 15:56:41 +08003031 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08003032 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08003033 player->fffb_current = _dvr_time_getClock();
3034 player->fffb_start = player->fffb_current;
3035 player->fffb_start_pcr = _dvr_get_cur_time(handle);
3036 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08003037 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08003038 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003039 //only seek file,not start
Wentao MA96f68962022-06-15 19:45:35 +08003040 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003041 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003042 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08003043 }
hualing chen86e7d482020-01-16 15:13:33 +08003044 //stop play
Wentao MA96f68962022-06-15 19:45:35 +08003045 DVR_PB_ERROR("seek stop play, not inject data has video[%d]audio[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003046 player->has_video, player->has_audio);
hualing chen1ffd85b2021-08-16 15:18:43 +08003047
hualing chen266b9502020-04-04 17:39:39 +08003048 if (player->has_video) {
hualing chen7e14e532021-09-23 11:23:28 +08003049 //player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003050 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003051 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08003052 }
3053
hualing chen40dd5462021-11-26 19:56:20 +08003054
hualing chen266b9502020-04-04 17:39:39 +08003055 if (player->has_audio) {
3056 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003057 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08003058 }
hualing chendf118dd2020-05-21 15:49:11 +08003059 if (player->has_ad_audio) {
3060 player->has_ad_audio =DVR_FALSE;
3061 AmTsPlayer_disableADMix(player->handle);
3062 }
3063
hualing chen86e7d482020-01-16 15:13:33 +08003064 //start play
Wentao MA270dc0f2022-08-23 13:17:26 +08003065 am_tsplayer_video_params video_params;
3066 am_tsplayer_audio_params audio_params;
3067 am_tsplayer_audio_params ad_params;
hualing chenb31a6c62020-01-13 17:27:00 +08003068
Wentao MA270dc0f2022-08-23 13:17:26 +08003069 memset(&video_params, 0, sizeof(video_params));
3070 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003071
hualing chen040df222020-01-17 13:35:02 +08003072 player->cur_segment_id = segment_id;
3073
3074 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08003075 //get segment info and audio video pid fmt ;
Wentao MA270dc0f2022-08-23 13:17:26 +08003076 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen86e7d482020-01-16 15:13:33 +08003077 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003078 if (video_params.pid != player->fake_pid && !VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
hualing chena5f03222021-12-02 11:22:35 +08003079 //audio and video pid is all invalid, return error.
Wentao MA270dc0f2022-08-23 13:17:26 +08003080 DVR_PB_ERROR("unlock seek start dvr play back start error, not found audio and video info [0x%x]", video_params.pid);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003081 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08003082 return -1;
3083 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003084 DVR_PB_ERROR("seek start[0x%x]", video_params.pid);
hualing chen86e7d482020-01-16 15:13:33 +08003085 //add
hualing chen040df222020-01-17 13:35:02 +08003086 if (sync == DVR_PLAYBACK_SYNC) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003087 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003088 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08003089 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08003090 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08003091 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08003092 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003093 //if is pause state. we need set trick mode.
Wentao MA96f68962022-06-15 19:45:35 +08003094 DVR_PB_INFO("seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003095 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08003096 }
Wentao MA96f68962022-06-15 19:45:35 +08003097 DVR_PB_INFO("start video");
Wentao MA270dc0f2022-08-23 13:17:26 +08003098 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003099 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003100 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08003101 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
3102 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
3103 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
3104 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
3105 AmTsPlayer_stopFast(player->handle);
3106 }
hualing chen266b9502020-04-04 17:39:39 +08003107 player->has_video = DVR_TRUE;
hualing chen7e14e532021-09-23 11:23:28 +08003108 } else {
3109 player->has_video = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08003110 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003111 if (VALID_PID(ad_params.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08003112 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003113 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003114 dvr_playback_change_seek_state(handle, ad_params.pid);
3115 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08003116 AmTsPlayer_enableADMix(player->handle);
3117 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003118 if (VALID_PID(audio_params.pid) && player->speed == 1.0) {
Wentao MA96f68962022-06-15 19:45:35 +08003119 DVR_PB_INFO("start audio seek");
Wentao MA270dc0f2022-08-23 13:17:26 +08003120 dvr_playback_change_seek_state(handle, audio_params.pid);
3121 AmTsPlayer_setAudioParams(player->handle, &audio_params);
hualing chen969fe7b2021-05-26 15:13:17 +08003122 AmTsPlayer_startAudioDecoding(player->handle);
3123 player->has_audio = DVR_TRUE;
3124 }
hualing chen43a89bc2022-01-19 14:31:20 +08003125#ifdef AVSYNC_USED_PCR
3126 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08003127 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08003128 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
3129 }
3130#endif
hualing chen86e7d482020-01-16 15:13:33 +08003131 }
hualing chen1ffd85b2021-08-16 15:18:43 +08003132 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen2aba4022020-03-02 13:49:55 +08003133 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00003134 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
Wentao MA270dc0f2022-08-23 13:17:26 +08003135 if (VALID_PID(audio_params.pid) || VALID_PID(video_params.pid))
hualing chena5f03222021-12-02 11:22:35 +08003136 player->seek_pause = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08003137 DVR_PB_INFO("set state pause in seek vpid[0x%x]apid[0x%x]",video_params.pid, audio_params.pid);
hualing chen87072a82020-03-12 16:20:12 +08003138 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3139 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08003140 player->speed > 1.0f||
3141 player->speed <= -1.0f) {
Wentao MA96f68962022-06-15 19:45:35 +08003142 DVR_PB_INFO("not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08003143 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08003144 } else {
Wentao MA96f68962022-06-15 19:45:35 +08003145 DVR_PB_INFO("set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08003146 player->cmd.last_cmd = player->cmd.cur_cmd;
3147 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
3148 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00003149 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen2aba4022020-03-02 13:49:55 +08003150 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003151 player->last_send_time_id = UINT64_MAX;
Wentao MA96f68962022-06-15 19:45:35 +08003152 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003153 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003154
3155 return DVR_SUCCESS;
3156}
hualing chen5cbe1a62020-02-10 16:36:36 +08003157
Wentao MAac5ea062022-08-11 11:44:27 +08003158// Get current playback time position of the ongoing segment.
3159// Notice the return value may be negative. This is because previous segment's
3160// data cached in demux buffer need to be considered.
hualing chen5cbe1a62020-02-10 16:36:36 +08003161static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
3162 //get cur time of segment
3163 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003164
Gong Ke2a0ebbe2021-05-25 15:22:50 +08003165 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003166 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003167 return DVR_FAILURE;
3168 }
3169
Wentao MA270dc0f2022-08-23 13:17:26 +08003170 int64_t cache = 0;//default es buf cache 500ms
hualing chen2aba4022020-03-02 13:49:55 +08003171 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003172 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
hualing chena5f03222021-12-02 11:22:35 +08003173 uint64_t cur = 0;
3174 if (player->ts_cache_len > 0 && pos < 0) {
3175 //this case is open new segment end,but cache data is last segment.
3176 //we need used last segment len to send play time.
3177 cur = 0;
3178 } else {
3179 cur = segment_tell_position_time(player->r_handle, pos);
3180 }
hualing chen21a40372021-10-29 11:07:26 +08003181 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08003182 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08003183 DVR_PB_INFO("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 +08003184 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3185 cache = 0;
3186 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003187 int cur_time = (int)(cur > cache ? cur - cache : 0);
3188 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08003189}
3190
Wentao MAac5ea062022-08-11 11:44:27 +08003191// Get current playback time position of the ongoing segment.
3192// Notice the return value may be negative. This is because previous segment's
3193// data cached in demux buffer need to be considered.
hualing chen969fe7b2021-05-26 15:13:17 +08003194static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
3195 //get cur time of segment
3196 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3197
hualing chen03fd4942021-07-15 15:56:41 +08003198 if (player == NULL || player->handle == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003199 DVR_PB_INFO("player is NULL");
hualing chen969fe7b2021-05-26 15:13:17 +08003200 return DVR_FAILURE;
3201 }
3202
Wentao MA270dc0f2022-08-23 13:17:26 +08003203 int64_t cache = 0;//default es buf cache 500ms
hualing chen969fe7b2021-05-26 15:13:17 +08003204 int cur_time = 0;
hualing chen969fe7b2021-05-26 15:13:17 +08003205 pthread_mutex_lock(&player->segment_lock);
hualing chena5f03222021-12-02 11:22:35 +08003206 loff_t tmp_pos = segment_tell_position(player->r_handle);
3207 loff_t pos = tmp_pos - player->ts_cache_len;
3208 uint64_t cur = 0;
3209 if (player->ts_cache_len > 0 && (tmp_pos < player->ts_cache_len)) {
3210 //this case is open new segment end,but cache data is last segment.
3211 //we need used last segment len to send play time.
3212 cur = 0;
Wentao MA96f68962022-06-15 19:45:35 +08003213 DVR_PB_INFO("change segment [%lld][%lld]",
hualing chen926a8ec2021-12-20 20:38:24 +08003214 player->last_segment_id, player->cur_segment_id);
hualing chena5f03222021-12-02 11:22:35 +08003215 } else {
3216 cur = segment_tell_position_time(player->r_handle, pos);
3217 }
hualing chen21a40372021-10-29 11:07:26 +08003218 AmTsPlayer_getDelayTime(player->handle, &cache);
Wentao MAaf716972021-12-28 13:28:52 +08003219
hualing chen969fe7b2021-05-26 15:13:17 +08003220 pthread_mutex_unlock(&player->segment_lock);
Wentao MA01de0e62022-01-10 18:48:23 +08003221
3222 // The idea here is to work around a weakness of AmTsPlayer_getDelayTime at
3223 // starting phase of a playback in a short period of 20ms or less. During the
3224 // said period, getDelayTime does NOT work as expect to return real cache
3225 // length because demux isn't actually running to provide valid pts to
3226 // TsPlayer. "cache==0" implies the situation that playback is NOT actually
3227 // started. Under such conditions a '0' cache size may NOT reflect actual data
3228 // length remaining in TsPlayer cache, therefore corresponding libdvr 'cur' is
3229 // useless if data in TsPlayer cache is not considered, so it needs to be
Wentao MA270dc0f2022-08-23 13:17:26 +08003230 // reset to a previous valid state. To make the reset operation stricter, extra
Wentao MA01de0e62022-01-10 18:48:23 +08003231 // AmTsPlayer_getPts invocations on both video/audio are introduced to test if
3232 // TsPlayer can get valid pts which indicates the actual running status of
3233 // demux. (JIRA issue: SWPL-68740)
3234 if (player->first_start_id != UINT64_MAX && cache == 0
3235 && player->check_cache_flag == DVR_TRUE ) {
3236 uint64_t pts_a=0;
3237 uint64_t pts_v=0;
3238 AmTsPlayer_getPts(player->handle, TS_STREAM_AUDIO, &pts_a);
3239 AmTsPlayer_getPts(player->handle, TS_STREAM_VIDEO, &pts_v);
3240 if ((int64_t)pts_a <= 0 && (int64_t)pts_v <= 0) {
3241 // Identified the wired situation and just return previous valid state
3242 cur = player->first_start_time;
3243 *id = player->first_start_id;
3244 return cur;
3245 }
3246 }
3247 if (cache != 0) {
3248 // Do NOT permit to enter 'if' code block above any more
3249 player->check_cache_flag=DVR_FALSE;
3250 }
3251
Wentao MA96f68962022-06-15 19:45:35 +08003252 DVR_PB_INFO("***get play cur time [%lld] cache:%lld cur id [%lld]"
3253 " last id [%lld] pb cache len [%d] pos [%lld][%lld]",
hualing chen926a8ec2021-12-20 20:38:24 +08003254 cur,
3255 cache,
3256 player->cur_segment_id,
3257 player->last_send_time_id,
3258 player->ts_cache_len,
3259 pos,
3260 tmp_pos);
hualing chen969fe7b2021-05-26 15:13:17 +08003261 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3262 cache = 0;
3263 }
3264 if (cur > cache) {
3265 cur_time = (int)(cur - cache);
3266 *id = player->cur_segment_id;
Wentao MA270dc0f2022-08-23 13:17:26 +08003267 } else if (player->last_segment_total > 0) {
hualing chend1686e52022-01-05 17:10:42 +08003268 //if at fb mode,we not used last id to replace cur id if cache > cur time.
3269 //this case only used for normal speed or ff speed
3270 if (!IS_FB(player->speed) && player->last_segment_id <= player->cur_segment_id) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003271 if (player->last_segment_total > (cache - cur))
3272 cur_time = (int)(player->last_segment_total - (cache - cur));
hualing chend1686e52022-01-05 17:10:42 +08003273 else
Wentao MA270dc0f2022-08-23 13:17:26 +08003274 cur_time = (int)(player->last_segment_total - cur);
hualing chen8a657f32021-08-30 13:12:49 +08003275
hualing chend1686e52022-01-05 17:10:42 +08003276 *id = player->last_segment_id;
3277 } else {//fb mode
3278 cur_time = (int)(cur);
3279 *id = player->cur_segment_id;
3280 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003281 DVR_PB_INFO("get play cur time[%lld][%lld][%d]", player->last_segment_id, player->cur_segment_id, player->last_segment_total);
hualing chen969fe7b2021-05-26 15:13:17 +08003282 } else {
3283 cur_time = 0;
3284 *id = player->cur_segment_id;
3285 }
hualing chen969fe7b2021-05-26 15:13:17 +08003286 return cur_time;
3287}
3288
hualing chencc91e1c2020-02-28 13:26:17 +08003289//get current segment current pcr time of read pos
3290static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
3291 //get cur time of segment
3292 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003293
3294 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003295 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003296 return DVR_FAILURE;
3297 }
3298
hualing chen2aba4022020-03-02 13:49:55 +08003299 pthread_mutex_lock(&player->segment_lock);
3300 uint64_t end = segment_tell_total_time(player->r_handle);
Wentao MA96f68962022-06-15 19:45:35 +08003301 DVR_PB_INFO("get total time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08003302 pthread_mutex_unlock(&player->segment_lock);
3303 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08003304}
3305
hualing chen03fd4942021-07-15 15:56:41 +08003306DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
3307{
3308 //check is set limit info
3309 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3310
3311 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003312 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003313 return DVR_FALSE;
3314 }
3315 if (player->rec_start > 0 || player->limit > 0) {
3316 return DVR_TRUE;
3317 }
3318 return DVR_FALSE;
3319}
3320
3321/**\brief set DVR playback calculate expired time len
3322 * \param[in] handle, DVR playback session handle
3323 * \return DVR_SUCCESS on success
3324 * \return error code on failure
3325 */
hualing chen7ea70a72021-09-09 11:25:13 +08003326uint32_t dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
hualing chen03fd4942021-07-15 15:56:41 +08003327{
3328 //calculate expired time to play
3329 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08003330 uint32_t cur_time;
3331 uint32_t tmp_time;
3332 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003333 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003334 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003335 return expired;
3336 }
hualing chen7ea70a72021-09-09 11:25:13 +08003337 if (player->rec_start == 0 || player->limit == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003338 DVR_PB_INFO("rec limit 0");
hualing chen03fd4942021-07-15 15:56:41 +08003339 return expired;
3340 }
3341 //get system time
hualing chen7ea70a72021-09-09 11:25:13 +08003342 cur_time = _dvr_getClock_sec();
3343 if ((cur_time - player->rec_start) > player->limit) {
3344 tmp_time = (uint32_t)((cur_time - player->rec_start) - player->limit) * 1000U;
3345 expired = *(int*)&tmp_time;
Wentao MA96f68962022-06-15 19:45:35 +08003346 DVR_PB_INFO("cur_time:%u, rec start:%u limit:%d c_r_diff:%u expired:%u tmp_time:%u",
hualing chen03fd4942021-07-15 15:56:41 +08003347 cur_time,
3348 player->rec_start,
3349 player->limit,
hualing chen7ea70a72021-09-09 11:25:13 +08003350 (uint32_t)(cur_time - player->rec_start - player->limit), expired, tmp_time);
3351 }
hualing chen03fd4942021-07-15 15:56:41 +08003352 return expired;
3353}
3354
3355/**\brief set DVR playback obsolete time
3356 * \param[in] handle, DVR playback session handle
3357 * \param[in] obsolete, obsolete len
3358 * \return DVR_SUCCESS on success
3359 * \return error code on failure
3360 */
3361int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
3362{
3363 int expired = 0;
3364 //calculate expired time to play
3365 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3366
3367 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003368 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003369 return DVR_FALSE;
3370 }
3371 //get system time
Wentao MA96f68962022-06-15 19:45:35 +08003372 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003373 dvr_mutex_lock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003374 player->obsolete = obsolete;
Wentao MA96f68962022-06-15 19:45:35 +08003375 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003376 dvr_mutex_unlock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003377 return expired;
3378}
3379
3380/**\brief update DVR playback newest segment duration
3381 * \param[in] handle, DVR playback session handle
3382 * \param[in] segmentid, newest segment id
3383 * \param[in] dur dur time ms
3384 * \return DVR_SUCCESS on success
3385 * \return error code on failure
3386 */
3387int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
3388uint64_t segmentid, int dur)
3389{
3390 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3391 DVR_PlaybackSegmentInfo_t *segment;
3392 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
3393
3394 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003395 DVR_PB_INFO(" player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003396 return DVR_FAILURE;
3397 }
3398 //update the newest segment duration on timeshift mode
3399 list_for_each_entry(segment, &player->segment_list, head)
3400 {
3401 if (segment->segment_id == segmentid) {
3402 segment->duration = dur;
3403 break;
3404 }
3405 pre_segment = segment;
3406 }
3407
3408 return DVR_SUCCESS;
3409}
3410
hualing chen7ea70a72021-09-09 11:25:13 +08003411static uint32_t dvr_playback_calculate_last_valid_segment(
3412 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos)
hualing chen03fd4942021-07-15 15:56:41 +08003413{
hualing chen7ea70a72021-09-09 11:25:13 +08003414 uint32_t off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003415 uint64_t segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003416 uint32_t pre_off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003417 uint64_t last_segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003418 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003419 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3420
3421 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003422 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003423 return DVR_FAILURE;
3424 }
3425 expired = dvr_playback_calculate_expiredlen(handle);
hualing chen7e14e532021-09-23 11:23:28 +08003426 if (expired == 0) {
3427 *segmentid = player->cur_segment_id;
3428 *pos = 0;
3429 return DVR_SUCCESS;
3430 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003431 DVR_PlaybackSegmentInfo_t *p_seg;
3432 list_for_each_entry_reverse(p_seg, &player->segment_list, head) {
3433 segment_id = p_seg->segment_id;
hualing chen03fd4942021-07-15 15:56:41 +08003434
Wentao MA270dc0f2022-08-23 13:17:26 +08003435 if ((player->obsolete + pre_off + p_seg->duration) > expired)
hualing chen03fd4942021-07-15 15:56:41 +08003436 break;
3437
Wentao MA270dc0f2022-08-23 13:17:26 +08003438 last_segment_id = p_seg->segment_id;
3439 pre_off += p_seg->duration;
hualing chen03fd4942021-07-15 15:56:41 +08003440 }
3441
3442 if (last_segment_id == segment_id) {
3443 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
3444 off = expired;
3445 } else if (player->obsolete >= expired) {
3446 off = 0;
3447 } else {
3448 off = expired - pre_off - player->obsolete;
3449 }
3450 *segmentid = segment_id;
3451 *pos = off;
3452 return DVR_SUCCESS;
3453}
3454
hualing chen4b7c15d2020-04-07 16:13:48 +08003455#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003456//start replay
3457static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3458
3459 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3460 //calculate pcr seek time
3461 int t_diff = 0;
3462 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003463 uint64_t segmentid = 0;
3464 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003465 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003466 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003467 return DVR_FAILURE;
3468 }
3469
hualing chen5cbe1a62020-02-10 16:36:36 +08003470 if (player->fffb_start == -1) {
3471 //set fffb start time ms
3472 player->fffb_start = _dvr_time_getClock();
3473 player->fffb_current = player->fffb_start;
3474 //get segment current time pos
3475 player->fffb_start_pcr = _dvr_get_cur_time(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003476 DVR_PB_INFO("calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
hualing chen03fd4942021-07-15 15:56:41 +08003477 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003478 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003479 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003480 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003481 } else {
3482 player->fffb_current = _dvr_time_getClock();
3483 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003484 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003485 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003486 if (seek_time <= 0) {
3487 //need seek to pre one segment
3488 seek_time = 0;
3489 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003490 //seek segment pos
3491 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003492 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003493 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003494 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3495 //set seek time to 0;
Wentao MA96f68962022-06-15 19:45:35 +08003496 DVR_PB_INFO("segment seek to 0 at fb mode [%d]id[%lld]",
hualing chen03fd4942021-07-15 15:56:41 +08003497 seek_time,
3498 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003499 seek_time = 0;
3500 }
hualing chen03fd4942021-07-15 15:56:41 +08003501 if (IS_FB(player->speed)
3502 && dvr_playback_check_limit(handle)) {
3503 //fb case.check expired time
3504 //get id and pos to check if we can seek to this pos
3505 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3506 //case cur id < segment id
3507 if (player->cur_segment_id < segmentid) {
3508 //expired ts data is player,return error
3509 //
3510 pthread_mutex_unlock(&player->segment_lock);
3511 return 0;
3512 } else if (player->cur_segment_id == segmentid) {
3513 //id is same,compare seek pos
3514 if (seek_time < pos) {
3515 //expired ts data is player,return error
3516 //
3517 pthread_mutex_unlock(&player->segment_lock);
3518 return 0;
3519 }
3520 }
3521 //case can play
3522 }
hualing chen041c4092020-04-05 15:11:50 +08003523 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3524 seek_time = 0;
3525 }
hualing chen2aba4022020-03-02 13:49:55 +08003526 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003527 } else {
3528 //
Wentao MA96f68962022-06-15 19:45:35 +08003529 DVR_PB_INFO("segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003530 }
Wentao MA96f68962022-06-15 19:45:35 +08003531 DVR_PB_INFO("calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003532 seek_time,
3533 player->speed,
3534 player->cur_segment_id,
3535 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003536 }
hualing chen2aba4022020-03-02 13:49:55 +08003537 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003538}
3539
3540
3541//start replay
3542static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3543 //
3544 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003545
3546 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003547 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003548 return DVR_FAILURE;
3549 }
3550
hualing chen5cbe1a62020-02-10 16:36:36 +08003551 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003552 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08003553 DVR_PB_INFO("fffb stop video");
hualing chen21a40372021-10-29 11:07:26 +08003554 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003555 AmTsPlayer_stopVideoDecoding(player->handle);
3556 }
3557 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003558 DVR_PB_INFO("fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003559 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003560 AmTsPlayer_stopAudioDecoding(player->handle);
3561 }
hualing chendf118dd2020-05-21 15:49:11 +08003562 if (player->has_ad_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003563 DVR_PB_INFO("fffb stop audio");
hualing chendf118dd2020-05-21 15:49:11 +08003564 player->has_ad_audio =DVR_FALSE;
3565 AmTsPlayer_disableADMix(player->handle);
3566 }
hualing chen2aba4022020-03-02 13:49:55 +08003567
hualing chen5cbe1a62020-02-10 16:36:36 +08003568 //start video and audio
3569
Wentao MA270dc0f2022-08-23 13:17:26 +08003570 am_tsplayer_video_params video_params;
3571 am_tsplayer_audio_params audio_params;
3572 am_tsplayer_audio_params ad_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003573
Wentao MA270dc0f2022-08-23 13:17:26 +08003574 memset(&video_params, 0, sizeof(video_params));
3575 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003576
hualing chen87072a82020-03-12 16:20:12 +08003577 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003578
3579 //get segment info and audio video pid fmt ;
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003580 //dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08003581 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003582 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003583 if (!VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
3584 //audio and video pids are all invalid, return error.
Wentao MA96f68962022-06-15 19:45:35 +08003585 DVR_PB_ERROR("dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003586 return -1;
3587 }
3588
Wentao MA270dc0f2022-08-23 13:17:26 +08003589 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003590 player->has_video = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003591 DVR_PB_INFO("fffb start video");
3592 //DVR_PB_INFO("fffb start video and save last frame");
hualing chen0888c032020-12-18 17:54:57 +08003593 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003594 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003595 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
Wentao MA270dc0f2022-08-23 13:17:26 +08003596 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003597 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003598 AmTsPlayer_startVideoDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08003599 //playback_device_video_start(player->handle , &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003600 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003601 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003602 }
hualing chen31140872020-03-25 12:29:26 +08003603 //fffb mode need stop fast;
Wentao MA96f68962022-06-15 19:45:35 +08003604 DVR_PB_INFO("stop fast");
hualing chen31140872020-03-25 12:29:26 +08003605 AmTsPlayer_stopFast(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003606 return 0;
3607}
3608
3609static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3610 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003611 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003612 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003613 return DVR_FAILURE;
3614 }
3615
3616 player->first_frame = 0;
Wentao MA96f68962022-06-15 19:45:35 +08003617 DVR_PB_INFO("lock speed [%f]", player->speed);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003618 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003619
hualing chen2aba4022020-03-02 13:49:55 +08003620 int seek_time = _dvr_playback_calculate_seekpos(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003621 DVR_PB_INFO("get lock speed [%f]id [%lld]seek_time[%d]", player->speed, player->cur_segment_id, seek_time);
hualing chen041c4092020-04-05 15:11:50 +08003622
hualing chen87072a82020-03-12 16:20:12 +08003623 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3624 //seek time set 0
3625 seek_time = 0;
3626 }
hualing chen041c4092020-04-05 15:11:50 +08003627 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003628 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3629 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003630 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003631 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003632 DVR_PB_DEBUG("unlock");
hualing chen87072a82020-03-12 16:20:12 +08003633 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003634 //send event here and pause
3635 DVR_Play_Notify_t notify;
3636 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003637 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003638 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003639 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
Wentao MA96f68962022-06-15 19:45:35 +08003640 DVR_PB_INFO("*******************send begin event speed [%f] cur [%d]", player->speed, _dvr_get_cur_time(handle));
hualing chen2aba4022020-03-02 13:49:55 +08003641 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003642 return DVR_SUCCESS;
3643 }
hualing chen2932d372020-04-29 13:44:00 +08003644 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003645 _dvr_init_fffb_time(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003646 DVR_PB_INFO("*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003647 }
3648 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003649 _dvr_playback_fffb_replay(handle);
3650
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003651 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003652 DVR_PB_DEBUG("unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003653
hualing chen5cbe1a62020-02-10 16:36:36 +08003654 return DVR_SUCCESS;
3655}
3656
hualing chen87072a82020-03-12 16:20:12 +08003657//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003658static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003659 //
3660 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003661
3662 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003663 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003664 return DVR_FAILURE;
3665 }
3666
hualing chen5cbe1a62020-02-10 16:36:36 +08003667 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003668 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003669 player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003670 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003671 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003672 }
3673
3674 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003675 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003676 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003677 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003678 //start video and audio
3679
Wentao MA270dc0f2022-08-23 13:17:26 +08003680 am_tsplayer_video_params video_params;
3681 am_tsplayer_audio_params audio_params;
3682 am_tsplayer_audio_params ad_params;
hualing chen87072a82020-03-12 16:20:12 +08003683 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003684
Wentao MA270dc0f2022-08-23 13:17:26 +08003685 memset(&video_params, 0, sizeof(video_params));
3686 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003687
hualing chen5cbe1a62020-02-10 16:36:36 +08003688 //get segment info and audio video pid fmt ;
Wentao MA96f68962022-06-15 19:45:35 +08003689 DVR_PB_INFO("into");
Wentao MA270dc0f2022-08-23 13:17:26 +08003690 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003691 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003692 if (!VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003693 //audio and video pis is all invalid, return error.
Wentao MA96f68962022-06-15 19:45:35 +08003694 DVR_PB_ERROR("dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003695 return -1;
3696 }
3697
Wentao MA270dc0f2022-08-23 13:17:26 +08003698 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003699 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003700 if (trick == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08003701 DVR_PB_INFO("settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003702 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003703 }
hualing chen266b9502020-04-04 17:39:39 +08003704 else {
hualing chen2aba4022020-03-02 13:49:55 +08003705 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003706 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003707 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003708 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003709 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003710 }
hualing chena540a7e2020-03-27 16:44:05 +08003711
3712 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
Wentao MA96f68962022-06-15 19:45:35 +08003713 DVR_PB_INFO("start fast");
hualing chen31140872020-03-25 12:29:26 +08003714 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003715 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003716 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08003717 if (VALID_PID(ad_params.pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08003718 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003719 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003720 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08003721 AmTsPlayer_enableADMix(player->handle);
3722 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003723 if (VALID_PID(audio_params.pid)) {
hualing chen969fe7b2021-05-26 15:13:17 +08003724 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003725 DVR_PB_INFO("start audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003726 AmTsPlayer_setAudioParams(player->handle, &audio_params);
hualing chen969fe7b2021-05-26 15:13:17 +08003727 AmTsPlayer_startAudioDecoding(player->handle);
3728 }
hualing chendf118dd2020-05-21 15:49:11 +08003729
Wentao MA96f68962022-06-15 19:45:35 +08003730 DVR_PB_INFO("stop fast");
hualing chen31140872020-03-25 12:29:26 +08003731 AmTsPlayer_stopFast(player->handle);
3732 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3733 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3734 }
hualing chen43a89bc2022-01-19 14:31:20 +08003735#ifdef AVSYNC_USED_PCR
3736 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08003737 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08003738 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
3739 }
3740#endif
hualing chen2aba4022020-03-02 13:49:55 +08003741 player->cmd.last_cmd = player->cmd.cur_cmd;
3742 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003743 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00003744 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen5cbe1a62020-02-10 16:36:36 +08003745 return 0;
3746}
3747
3748
hualing chenb31a6c62020-01-13 17:27:00 +08003749/**\brief Set play speed
3750 * \param[in] handle playback handle
3751 * \param[in] speed playback speed
3752 * \retval DVR_SUCCESS On success
3753 * \return Error code
3754 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003755int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3756
3757 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003758
3759 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003760 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003761 return DVR_FAILURE;
3762 }
3763
hualing chena540a7e2020-03-27 16:44:05 +08003764 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08003765 DVR_PB_INFO(" func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003766 return DVR_FAILURE;
3767 }
hualing chenf00cdc82020-06-10 14:23:35 +08003768 if (speed.speed.speed == player->cmd.speed.speed.speed) {
Wentao MA96f68962022-06-15 19:45:35 +08003769 DVR_PB_INFO(" func: eq speed [%d]", speed.speed.speed);
hualing chenf00cdc82020-06-10 14:23:35 +08003770 return DVR_SUCCESS;
3771 }
Wentao MA96f68962022-06-15 19:45:35 +08003772 DVR_PB_INFO("lock func: speed [%d]", speed.speed.speed);
hualing chen1679f812021-11-08 15:17:46 +08003773
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003774 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003775 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3776 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3777 player->cmd.last_cmd = player->cmd.cur_cmd;
3778 }
hualing chene41f4372020-06-06 16:29:17 +08003779
hualing chen31140872020-03-25 12:29:26 +08003780 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003781 IS_KERNEL_SPEED(speed.speed.speed) ) {
3782 //case 1. not start play.only set speed
3783 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3784 //only set speed.and return;
3785 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3786 player->cmd.speed.speed = speed.speed;
3787 player->speed = (float)speed.speed.speed/(float)100;
3788 player->fffb_play = DVR_FALSE;
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003789 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003790 DVR_PB_DEBUG("unlock");
hualing chenf00cdc82020-06-10 14:23:35 +08003791 return DVR_SUCCESS;
3792 }
3793 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003794 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3795 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003796 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003797 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3798 // resume audio and stop fast play
Wentao MA96f68962022-06-15 19:45:35 +08003799 DVR_PB_INFO("stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003800 AmTsPlayer_stopFast(player->handle);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003801 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003802 DVR_PB_DEBUG("unlock ---\r\n");
Wentao MA270dc0f2022-08-23 13:17:26 +08003803 _dvr_cmd(handle, DVR_PLAYBACK_CMD_A_START);
Wentao MA96f68962022-06-15 19:45:35 +08003804 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003805 dvr_mutex_lock(&player->lock);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003806 } else {
3807 //set play speed and if audio is start, stop audio.
3808 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003809 DVR_PB_INFO("fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003810 AmTsPlayer_stopAudioDecoding(player->handle);
3811 player->has_audio = DVR_FALSE;
3812 }
Wentao MA96f68962022-06-15 19:45:35 +08003813 DVR_PB_INFO("start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003814 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003815 }
hualing chenbcada022020-04-22 14:27:01 +08003816 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003817 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003818 player->cmd.speed.speed = speed.speed;
3819 player->speed = (float)speed.speed.speed/(float)100;
Wentao MA96f68962022-06-15 19:45:35 +08003820 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003821 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003822 return DVR_SUCCESS;
3823 }
hualing chen31140872020-03-25 12:29:26 +08003824 //case 3 fffb mode
3825 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3826 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3827 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003828 DVR_PB_INFO("set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003829 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003830 player->cmd.speed.speed = speed.speed;
3831 player->speed = (float)speed.speed.speed/(float)100;
3832 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003833 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003834 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003835 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003836 return DVR_SUCCESS;
3837 }
3838 }
3839 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003840 IS_KERNEL_SPEED(speed.speed.speed)) {
3841 //case 1. cur speed is kernel support speed,set kernel speed.
3842 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003843 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003844 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3845 // resume audio and stop fast play
Wentao MA96f68962022-06-15 19:45:35 +08003846 DVR_PB_INFO("stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003847 AmTsPlayer_stopFast(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08003848 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003849 } else {
3850 //set play speed and if audio is start, stop audio.
3851 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003852 DVR_PB_INFO("fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003853 AmTsPlayer_stopAudioDecoding(player->handle);
3854 player->has_audio = DVR_FALSE;
3855 }
Wentao MA96f68962022-06-15 19:45:35 +08003856 DVR_PB_INFO("start fast");
hualing chenf00cdc82020-06-10 14:23:35 +08003857 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003858 }
hualing chena540a7e2020-03-27 16:44:05 +08003859 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003860 player->cmd.speed.speed = speed.speed;
3861 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003862 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003863 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003864 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003865 return DVR_SUCCESS;
3866 }
3867 //case 2 fffb mode
3868 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3869 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3870 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003871 DVR_PB_INFO("set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003872 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003873 player->cmd.speed.speed = speed.speed;
3874 player->speed = (float)speed.speed.speed/(float)100;
Wentao MA270dc0f2022-08-23 13:17:26 +08003875 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
hualing chenbcada022020-04-22 14:27:01 +08003876 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003877 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003878 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003879 return DVR_SUCCESS;
3880 }
hualing chen31140872020-03-25 12:29:26 +08003881 }
hualing chena540a7e2020-03-27 16:44:05 +08003882 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3883 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003884 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003885 } else {
hualing chen31140872020-03-25 12:29:26 +08003886 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003887 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3888 else
3889 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003890 player->fffb_play = DVR_TRUE;
3891 }
3892 DVR_Bool_t init_last_time = DVR_FALSE;
3893 if (player->speed > 0.0f && speed.speed.speed < 0) {
3894 init_last_time = DVR_TRUE;
3895 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3896 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003897 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003898 player->cmd.speed.mode = speed.mode;
3899 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003900 player->speed = (float)speed.speed.speed/(float)100;
3901 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003902 _dvr_init_fffb_t(handle);
3903 if (init_last_time == DVR_TRUE)
3904 player->last_send_time_id = UINT64_MAX;
3905
hualing chen87072a82020-03-12 16:20:12 +08003906 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003907 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3908 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003909 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003910 DVR_PB_INFO("set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003911 _dvr_playback_replay(handle, DVR_FALSE);
3912 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3913 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003914 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
Wentao MA96f68962022-06-15 19:45:35 +08003915 DVR_PB_INFO("set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003916 }
Wentao MA96f68962022-06-15 19:45:35 +08003917 DVR_PB_INFO("unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003918 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003919 return DVR_SUCCESS;
3920}
hualing chen2932d372020-04-29 13:44:00 +08003921
hualing chenb31a6c62020-01-13 17:27:00 +08003922/**\brief Get playback status
3923 * \param[in] handle playback handle
3924 * \param[out] p_status playback status
3925 * \retval DVR_SUCCESS On success
3926 * \return Error code
3927 */
hualing chen2932d372020-04-29 13:44:00 +08003928static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3929 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003930//
3931 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003932 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003933 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003934 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003935 return DVR_FAILURE;
3936 }
hualing chen1679f812021-11-08 15:17:46 +08003937 if (is_lock ==DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08003938 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003939 dvr_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003940 }
3941
hualing chen5cbe1a62020-02-10 16:36:36 +08003942 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003943 //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 +08003944 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3945 player->state == DVR_PLAYBACK_STATE_START) {
3946 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3947 }
hualing chen041c4092020-04-05 15:11:50 +08003948
hualing chencc91e1c2020-02-28 13:26:17 +08003949 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003950 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003951
3952 if (CONTROL_SPEED_ENABLE == 1) {
hualing chen7ea70a72021-09-09 11:25:13 +08003953 if (player->con_spe.ply_sta == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003954 DVR_PB_INFO("player dur[%u] sta[%u] cur[%d] -----reinit",
hualing chen03fd4942021-07-15 15:56:41 +08003955 player->con_spe.ply_dur,
3956 player->con_spe.ply_sta,
3957 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003958 player->con_spe.ply_sta = p_status->time_cur;
3959 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3960 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
Wentao MA96f68962022-06-15 19:45:35 +08003961 DVR_PB_INFO("player dur[%u] sta[%u] cur[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003962 player->con_spe.ply_dur,
3963 player->con_spe.ply_sta,
3964 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003965 player->con_spe.ply_sta = p_status->time_cur;
3966 }
3967
3968 if (player->con_spe.sys_sta == 0) {
3969 player->con_spe.sys_sta = _dvr_time_getClock();
3970 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
3971 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
3972 player->con_spe.sys_sta = _dvr_time_getClock();
3973 }
3974 }
3975
hualing chen4b7c15d2020-04-07 16:13:48 +08003976 if (player->last_send_time_id == UINT64_MAX) {
3977 player->last_send_time_id = player->cur_segment_id;
3978 player->last_cur_time = p_status->time_cur;
3979 }
3980 if (player->last_send_time_id == player->cur_segment_id) {
3981 if (player->speed > 0.0f ) {
3982 //ff
3983 if (p_status->time_cur < player->last_cur_time ) {
Wentao MA96f68962022-06-15 19:45:35 +08003984 DVR_PB_INFO("get ff time error last[%d]cur[%d]diff[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003985 player->last_cur_time,
3986 p_status->time_cur,
3987 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08003988 p_status->time_cur = player->last_cur_time;
3989 } else {
3990 player->last_cur_time = p_status->time_cur;
3991 }
hualing chene41f4372020-06-06 16:29:17 +08003992 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003993 //fb
3994 if (p_status->time_cur > player->last_cur_time ) {
Wentao MA96f68962022-06-15 19:45:35 +08003995 DVR_PB_INFO("get fb time error last[%d]cur[%d]diff[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003996 player->last_cur_time,
3997 p_status->time_cur,
3998 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08003999 p_status->time_cur = player->last_cur_time;
4000 } else {
4001 player->last_cur_time = p_status->time_cur;
4002 }
4003 }
hualing chend241c7a2021-06-22 13:34:27 +08004004 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08004005 player->last_cur_time = p_status->time_cur;
4006 }
hualing chen969fe7b2021-05-26 15:13:17 +08004007 player->last_send_time_id = segment_id;
4008 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08004009
hualing chen5cbe1a62020-02-10 16:36:36 +08004010 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08004011 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08004012 p_status->flags = player->cur_segment.flags;
Wentao MA96f68962022-06-15 19:45:35 +08004013 DVR_PB_INFO("player real state[%s]state[%s]cur[%d]end[%d] id[%lld]playflag[%d]speed[%f]is_lock[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08004014 _dvr_playback_state_toString(player->state),
4015 _dvr_playback_state_toString(p_status->state),
4016 p_status->time_cur, p_status->time_end,
4017 p_status->segment_id,player->play_flag,
4018 player->speed,
4019 is_lock);
hualing chen1679f812021-11-08 15:17:46 +08004020 if (is_lock ==DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08004021 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004022 dvr_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08004023 }
hualing chen2932d372020-04-29 13:44:00 +08004024 return DVR_SUCCESS;
4025}
4026
4027
4028/**\brief Get playback status
4029 * \param[in] handle playback handle
4030 * \param[out] p_status playback status
4031 * \retval DVR_SUCCESS On success
4032 * \return Error code
4033 */
4034int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
4035 DVR_PlaybackStatus_t *p_status) {
4036//
4037 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4038
Zhiqiang Han9adc9722020-11-11 18:38:10 +08004039 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
4040
hualing chen2932d372020-04-29 13:44:00 +08004041 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004042 DVR_PB_INFO("player is NULL");
hualing chen2932d372020-04-29 13:44:00 +08004043 return DVR_FAILURE;
4044 }
Wentao MA96f68962022-06-15 19:45:35 +08004045 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004046 dvr_mutex_lock(&player->lock);
Zhiqiang Han9adc9722020-11-11 18:38:10 +08004047 if (!player->has_video && !player->has_audio)
4048 p_status->time_cur = 0;
Wentao MA96f68962022-06-15 19:45:35 +08004049 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004050 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08004051
hualing chenb31a6c62020-01-13 17:27:00 +08004052 return DVR_SUCCESS;
4053}
4054
hualing chen040df222020-01-17 13:35:02 +08004055void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
4056 if (segment != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004057 DVR_PB_INFO("segment id: %lld", segment->segment_id);
4058 DVR_PB_INFO("segment flag: %d", segment->flags);
4059 DVR_PB_INFO("segment location: [%s]", segment->location);
4060 DVR_PB_INFO("segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
4061 DVR_PB_INFO("segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
4062 DVR_PB_INFO("segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
4063 DVR_PB_INFO("segment sub apid: 0x%x sub afmt:0x%x", segment->pids.ad.pid,segment->pids.ad.format);
hualing chen86e7d482020-01-16 15:13:33 +08004064 }
hualing chenb31a6c62020-01-13 17:27:00 +08004065}
4066
hualing chen5cbe1a62020-02-10 16:36:36 +08004067int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08004068 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08004069
hualing chena540a7e2020-03-27 16:44:05 +08004070 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004071 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08004072 return DVR_FAILURE;
4073 }
4074
hualing chen040df222020-01-17 13:35:02 +08004075 DVR_PlaybackSegmentInfo_t *segment;
4076 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08004077 {
hualing chen040df222020-01-17 13:35:02 +08004078 if (segment_id >= 0) {
4079 if (segment->segment_id == segment_id) {
4080 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08004081 break;
4082 }
4083 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08004084 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08004085 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08004086 }
4087 }
4088 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08004089}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004090
pengfei.liu27cc4ec2020-04-03 16:28:16 +08004091int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004092{
4093 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4094 DVR_RETURN_IF_FALSE(player);
4095 DVR_RETURN_IF_FALSE(func);
4096
Wentao MA96f68962022-06-15 19:45:35 +08004097 DVR_PB_INFO("in ");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004098 dvr_mutex_lock(&player->lock);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004099
4100 player->dec_func = func;
4101 player->dec_userdata = userdata;
4102
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004103 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08004104 DVR_PB_INFO("out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004105 return DVR_SUCCESS;
4106}
4107
4108int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
4109{
4110 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4111 DVR_RETURN_IF_FALSE(player);
4112 DVR_RETURN_IF_FALSE(p_secure_buf);
4113 DVR_RETURN_IF_FALSE(len);
4114
Wentao MA96f68962022-06-15 19:45:35 +08004115 DVR_PB_INFO("in ");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004116 dvr_mutex_lock(&player->lock);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004117
4118 player->is_secure_mode = 1;
4119 player->secure_buffer = p_secure_buf;
4120 player->secure_buffer_size = len;
4121
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004122 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08004123 DVR_PB_INFO("out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004124 return DVR_SUCCESS;
4125}