blob: eba885b8deefeae52ec6a6c5789adae02ae7430a [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 MA16f870e2022-09-09 11:00:22 +0800180 if (player == NULL) {
181 DVR_PB_WARN("player is null at line %d",__LINE__);
182 break;
183 }
Wentao MA96f68962022-06-15 19:45:35 +0800184 DVR_PB_INFO("[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800185 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800186 break;
187 }
188 default:
189 break;
190 }
191}
hualing chen2aba4022020-03-02 13:49:55 +0800192void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
193{
hualing chen6e4bfa52020-03-13 14:37:11 +0800194 DVR_Playback_t *player = NULL;
195 if (user_data != NULL) {
196 player = (DVR_Playback_t *) user_data;
Wentao MA16f870e2022-09-09 11:00:22 +0800197 DVR_PB_INFO("playing speed is [%f] in callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800198 }
hualing chen2aba4022020-03-02 13:49:55 +0800199 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800200 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
201 {
Wentao MA96f68962022-06-15 19:45:35 +0800202 DVR_PB_INFO("[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800203 event->event.video_format.frame_width,
204 event->event.video_format.frame_height,
205 event->event.video_format.frame_rate);
206 break;
207 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800208 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
209 {
Wentao MA96f68962022-06-15 19:45:35 +0800210 DVR_PB_INFO("[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
Wentao MA16f870e2022-09-09 11:00:22 +0800211 if (player == NULL) {
212 DVR_PB_WARN("player is null at line %d",__LINE__);
213 break;
214 }
hualing chene41f4372020-06-06 16:29:17 +0800215 if (player->first_trans_ok == DVR_FALSE) {
216 player->first_trans_ok = DVR_TRUE;
217 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
218 }
hualing chen30423862021-04-16 14:39:12 +0800219 if (player != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800220 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800221 player->seek_pause = DVR_FALSE;
222 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800223 break;
224 }
hualing chen487ae6d2020-07-22 10:34:11 +0800225 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
Wentao MA96f68962022-06-15 19:45:35 +0800226 DVR_PB_INFO("[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
Wentao MA16f870e2022-09-09 11:00:22 +0800227 if (player == NULL) {
228 DVR_PB_WARN("player is null at line %d",__LINE__);
229 break;
230 }
hualing chen487ae6d2020-07-22 10:34:11 +0800231 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
232 player->first_trans_ok = DVR_TRUE;
233 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
234 }
235 if (player != NULL && player->has_video == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +0800236 DVR_PB_INFO("[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
hualing chen487ae6d2020-07-22 10:34:11 +0800237 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800238 player->seek_pause = DVR_FALSE;
hualing chen487ae6d2020-07-22 10:34:11 +0800239 }
240 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800241 default:
Wentao MA270dc0f2022-08-23 13:17:26 +0800242 DVR_PB_INFO("[evt]unknown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800243 break;
244 }
245 if (player&&player->player_callback_func) {
Wentao MA16f870e2022-09-09 11:00:22 +0800246 DVR_PB_INFO("calling callback");
hualing chen6e4bfa52020-03-13 14:37:11 +0800247 player->player_callback_func(player->player_callback_userdata, event);
248 } else if (player == NULL){
Wentao MA16f870e2022-09-09 11:00:22 +0800249 DVR_PB_WARN("player pointer %p is invalid",player);
hualing chen6e4bfa52020-03-13 14:37:11 +0800250 } else {
Wentao MA16f870e2022-09-09 11:00:22 +0800251 DVR_PB_WARN("player callback function %p is invalid",player->player_callback_func);
hualing chen2aba4022020-03-02 13:49:55 +0800252 }
253}
hualing chencc91e1c2020-02-28 13:26:17 +0800254
hualing chen5cbe1a62020-02-10 16:36:36 +0800255//convert video and audio fmt
256static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
257 int format = 0;
258 if (is_audio == DVR_FALSE) {
259 //for video fmt
260 switch (fmt)
261 {
262 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800263 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800264 break;
265 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800266 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800267 break;
268 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800269 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800270 break;
271 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800272 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800273 break;
hualing chena540a7e2020-03-27 16:44:05 +0800274 case DVR_VIDEO_FORMAT_VP9:
275 format = AV_VIDEO_CODEC_VP9;
276 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800277 }
278 } else {
279 //for audio fmt
280 switch (fmt)
281 {
282 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800283 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800284 break;
285 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800286 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800287 break;
288 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800289 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800290 break;
291 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800292 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800293 break;
hualing chena540a7e2020-03-27 16:44:05 +0800294 case DVR_AUDIO_FORMAT_AAC:
295 format = AV_AUDIO_CODEC_AAC;
296 break;
297 case DVR_AUDIO_FORMAT_LATM:
298 format = AV_AUDIO_CODEC_LATM;
299 break;
300 case DVR_AUDIO_FORMAT_PCM:
301 format = AV_AUDIO_CODEC_PCM;
302 break;
hualing chenee0e52b2021-04-09 16:58:44 +0800303 case DVR_AUDIO_FORMAT_AC4:
304 format = AV_AUDIO_CODEC_AC4;
305 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800306 }
307 }
308 return format;
309}
hualing chen040df222020-01-17 13:35:02 +0800310static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800311{
hualing chen040df222020-01-17 13:35:02 +0800312 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800313
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800314 if (player == NULL || player->handle == (am_tsplayer_handle)NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800315 return -1;
316
hualing chena540a7e2020-03-27 16:44:05 +0800317 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800318}
hualing chena540a7e2020-03-27 16:44:05 +0800319
hualing chen7ea70a72021-09-09 11:25:13 +0800320
321//get sys time sec
322static uint32_t _dvr_getClock_sec(void)
hualing chen5cbe1a62020-02-10 16:36:36 +0800323{
324 struct timespec ts;
hualing chen7ea70a72021-09-09 11:25:13 +0800325 uint32_t s;
hualing chen03fd4942021-07-15 15:56:41 +0800326 clock_gettime(CLOCK_REALTIME, &ts);
hualing chen7ea70a72021-09-09 11:25:13 +0800327 s = (uint32_t)(ts.tv_sec);
Wentao MA96f68962022-06-15 19:45:35 +0800328 DVR_PB_INFO("n:%u", s);
hualing chen7ea70a72021-09-09 11:25:13 +0800329 return s;
hualing chen5cbe1a62020-02-10 16:36:36 +0800330}
hualing chen86e7d482020-01-16 15:13:33 +0800331
hualing chen7ea70a72021-09-09 11:25:13 +0800332//get sys time ms
333static uint32_t _dvr_time_getClock(void)
334{
335 struct timespec ts;
336 uint32_t ms;
337 clock_gettime(CLOCK_REALTIME, &ts);
338 ms = (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
339 return ms;
340}
hualing chenb31a6c62020-01-13 17:27:00 +0800341
Wentao MA270dc0f2022-08-23 13:17:26 +0800342//timeout wait signal
hualing chen040df222020-01-17 13:35:02 +0800343static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800344{
hualing chen040df222020-01-17 13:35:02 +0800345 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800346
hualing chena540a7e2020-03-27 16:44:05 +0800347
348 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800349 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800350 return DVR_FAILURE;
351 }
352
hualing chen86e7d482020-01-16 15:13:33 +0800353 struct timespec ts;
354 clock_gettime(CLOCK_MONOTONIC, &ts);
355 //ms为毫秒,换算成秒
356 ts.tv_sec += ms/1000;
357 //在outtime的基础上,增加ms毫秒
358 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
359 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
360 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
361 //us的值有可能超过1秒,
362 ts.tv_sec += us / 1000000;
363 us = us % 1000000;
364 ts.tv_nsec = us * 1000;//换算成纳秒
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800365
366 int val = dvr_mutex_save(&player->lock);
367 pthread_cond_timedwait(&player->cond, &player->lock.lock, &ts);
368 dvr_mutex_restore(&player->lock, val);
hualing chen86e7d482020-01-16 15:13:33 +0800369 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800370}
hualing chen31140872020-03-25 12:29:26 +0800371//get tsplay delay time ms
372static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
373 DVR_Playback_t *player = (DVR_Playback_t *) handle;
374 int64_t cache = 0;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800375 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800376 DVR_PB_INFO("tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800377 return 0;
378 }
379 AmTsPlayer_getDelayTime(player->handle, &cache);
Wentao MA96f68962022-06-15 19:45:35 +0800380 DVR_PB_INFO("tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800381 return cache;
382}
hualing chenb31a6c62020-01-13 17:27:00 +0800383//send signal
hualing chen040df222020-01-17 13:35:02 +0800384static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800385{
hualing chen87072a82020-03-12 16:20:12 +0800386 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800387
388 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800389 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800390 return DVR_FAILURE;
391 }
Wentao MA96f68962022-06-15 19:45:35 +0800392 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800393 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800394 pthread_cond_signal(&player->cond);
Wentao MA96f68962022-06-15 19:45:35 +0800395 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800396 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800397 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800398}
399
hualing chen2932d372020-04-29 13:44:00 +0800400//send playback event, need check is need lock first
401static 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 +0800402
403 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800404
405 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800406 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800407 return DVR_FAILURE;
408 }
409
hualing chencc91e1c2020-02-28 13:26:17 +0800410 switch (evt) {
411 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800412 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800413 break;
414 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
415 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800416 DVR_PB_INFO("trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800417 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800418 break;
419 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
420 break;
421 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
422 break;
423 case DVR_PLAYBACK_EVENT_NO_KEY:
424 break;
425 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800426 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800427 DVR_PB_INFO("reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800428 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800429 break;
430 case DVR_PLAYBACK_EVENT_REACHED_END:
431 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800432 DVR_PB_INFO("reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800433 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800434 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800435 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800436 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800437 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800438 default:
439 break;
440 }
441 if (player->openParams.event_fn != NULL)
442 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800443 return DVR_SUCCESS;
444}
hualing chen2932d372020-04-29 13:44:00 +0800445static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800446{
447 DVR_Play_Notify_t notify;
448 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
449 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
450 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800451 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800452 return DVR_SUCCESS;
453}
454
hualing chen2932d372020-04-29 13:44:00 +0800455static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800456{
457 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800458
Wentao MA16f870e2022-09-09 11:00:22 +0800459 if (player == NULL) {
460 DVR_PB_ERROR("player is NULL");
461 return DVR_FAILURE;
462 }
hualing chene3797f02021-01-13 14:53:28 +0800463 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chend241c7a2021-06-22 13:34:27 +0800464 if (CONTROL_SPEED_ENABLE == 0)
465 return DVR_SUCCESS;
hualing chen4b7c15d2020-04-07 16:13:48 +0800466 }
hualing chena540a7e2020-03-27 16:44:05 +0800467
hualing chen03fd4942021-07-15 15:56:41 +0800468 if (player->send_time == 0) {
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;
hualing chen0888c032020-12-18 17:54:57 +0800473 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen56c0a162022-01-27 17:01:50 +0800474 if ((player->send_time - _dvr_time_getClock()) > 1000) {
475 player->send_time = _dvr_time_getClock() + 500;
Wentao MA96f68962022-06-15 19:45:35 +0800476 DVR_PB_INFO("player send time occur system time changed!!!!!");
hualing chen56c0a162022-01-27 17:01:50 +0800477 } else {
478 return DVR_SUCCESS;
479 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800480 }
hualing chend241c7a2021-06-22 13:34:27 +0800481 if (CONTROL_SPEED_ENABLE == 0)
482 player->send_time = _dvr_time_getClock() + 500;
483 else
484 player->send_time = _dvr_time_getClock() + 20;
485
hualing chen6e4bfa52020-03-13 14:37:11 +0800486 DVR_Play_Notify_t notify;
487 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
488 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
489 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800490 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800491 return DVR_SUCCESS;
492}
493
hualing chencc91e1c2020-02-28 13:26:17 +0800494//check is ongoing segment
495static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
496
497 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800498
499 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800500 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800501 return DVR_FAILURE;
502 }
wentao.maa210e5e2022-10-12 16:10:03 +0800503 int ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800504 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800505 return DVR_FALSE;
506 }
hualing chencc91e1c2020-02-28 13:26:17 +0800507 return DVR_TRUE;
508}
hualing chen4b7c15d2020-04-07 16:13:48 +0800509
510
511static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
512 DVR_Playback_t *player = (DVR_Playback_t *) handle;
513 player->fffb_start = _dvr_time_getClock();
Wentao MA96f68962022-06-15 19:45:35 +0800514 DVR_PB_INFO(" player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800515 player->fffb_current = player->fffb_start;
516 //get segment current time pos
517 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +0800518 player->next_fffb_time = _dvr_time_getClock();
519
520 return DVR_SUCCESS;
521}
522
hualing chen2aba4022020-03-02 13:49:55 +0800523static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
524 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800525 player->fffb_start = _dvr_time_getClock();
Wentao MA96f68962022-06-15 19:45:35 +0800526 DVR_PB_INFO(" player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800527 player->fffb_current = player->fffb_start;
528 //get segment current time pos
529 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +0800530
hualing chen2aba4022020-03-02 13:49:55 +0800531 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800532 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800533 return DVR_SUCCESS;
534}
hualing chencc91e1c2020-02-28 13:26:17 +0800535//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800536static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
537
538 DVR_Playback_t *player = (DVR_Playback_t *) handle;
539 DVR_PlaybackSegmentInfo_t *segment;
540 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
541
hualing chena540a7e2020-03-27 16:44:05 +0800542 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800543 DVR_PB_INFO(" player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800544 return DVR_FAILURE;
545 }
546
hualing chen87072a82020-03-12 16:20:12 +0800547 int found = 0;
548 int found_eq_id = 0;
549 list_for_each_entry(segment, &player->segment_list, head)
550 {
551 if (player->segment_is_open == DVR_FALSE) {
552 //get first segment from list, case segment is not open
553 if (!IS_FB(player->speed))
554 found = 1;
555 } else if (segment->segment_id == segmentid) {
556 //find cur segment, we need get next one
557 found_eq_id = 1;
558 if (!IS_FB(player->speed)) {
559 found = 1;
560 continue;
561 } else {
562 //if is fb mode.we need used pre segment
563 if (pre_segment != NULL) {
564 found = 1;
565 } else {
566 //not find next id.
Wentao MA96f68962022-06-15 19:45:35 +0800567 DVR_PB_INFO("not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800568 return DVR_FAILURE;
569 }
570 }
571 }
572 if (found == 1) {
573 found = 2;
574 break;
575 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800576 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800577 }
578 if (found != 2) {
579 //list is null or reache list end
Wentao MA96f68962022-06-15 19:45:35 +0800580 DVR_PB_INFO("not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800581 return DVR_FAILURE;
582 }
Wentao MA96f68962022-06-15 19:45:35 +0800583 DVR_PB_INFO("found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800584 return DVR_SUCCESS;
585}
586
587//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800588static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800589
hualing chen040df222020-01-17 13:35:02 +0800590 DVR_Playback_t *player = (DVR_Playback_t *) handle;
591 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800592 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen03fd4942021-07-15 15:56:41 +0800593 uint64_t segmentid;
hualing chen7ea70a72021-09-09 11:25:13 +0800594 uint32_t pos;
hualing chena540a7e2020-03-27 16:44:05 +0800595 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800596 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800597 return DVR_FAILURE;
598 }
599
hualing chen03fd4942021-07-15 15:56:41 +0800600 if (IS_FB(player->speed)
601 && dvr_playback_check_limit(handle)) {
602 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
603 //case cur id < segment id
604 if (player->cur_segment_id <= segmentid) {
605 //expired ts data is player,return error
Wentao MA96f68962022-06-15 19:45:35 +0800606 DVR_PB_INFO("reach start segment ,return error");
hualing chen03fd4942021-07-15 15:56:41 +0800607 return DVR_FAILURE;
608 }
Wentao MA96f68962022-06-15 19:45:35 +0800609 DVR_PB_INFO("has segment to fb play [%lld][%u]", segmentid, pos);
hualing chen03fd4942021-07-15 15:56:41 +0800610 }
611
hualing chen86e7d482020-01-16 15:13:33 +0800612 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800613 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800614
hualing chen040df222020-01-17 13:35:02 +0800615 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800616 {
hualing chencc91e1c2020-02-28 13:26:17 +0800617 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800618 //get first segment from list, case segment is not open
619 if (!IS_FB(player->speed))
620 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800621 } else if (segment->segment_id == player->cur_segment_id) {
622 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800623 found_eq_id = 1;
624 if (!IS_FB(player->speed)) {
625 found = 1;
626 continue;
627 } else {
628 //if is fb mode.we need used pre segment
629 if (pre_segment != NULL) {
630 found = 1;
631 } else {
632 //not find next id.
Wentao MA96f68962022-06-15 19:45:35 +0800633 DVR_PB_INFO("not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800634 return DVR_FAILURE;
635 }
636 }
hualing chen86e7d482020-01-16 15:13:33 +0800637 }
638 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800639 if (IS_FB(player->speed)) {
640 //used pre segment
641 segment = pre_segment;
642 }
hualing chencc91e1c2020-02-28 13:26:17 +0800643 //save segment info
644 player->last_segment_id = player->cur_segment_id;
hualing chen969fe7b2021-05-26 15:13:17 +0800645 if (player->r_handle)
Wentao MA270dc0f2022-08-23 13:17:26 +0800646 player->last_segment_total = segment_tell_total_time(player->r_handle);
hualing chen87072a82020-03-12 16:20:12 +0800647 player->last_segment.segment_id = player->cur_segment.segment_id;
648 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800649 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
650 //pids
651 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
652
hualing chen5cbe1a62020-02-10 16:36:36 +0800653 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800654 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800655 player->cur_segment_id = segment->segment_id;
656 player->cur_segment.segment_id = segment->segment_id;
657 player->cur_segment.flags = segment->flags;
Wentao MA96f68962022-06-15 19:45:35 +0800658 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 +0800659 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800660 //pids
hualing chen040df222020-01-17 13:35:02 +0800661 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800662 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800663 break;
hualing chen86e7d482020-01-16 15:13:33 +0800664 }
hualing chen2aba4022020-03-02 13:49:55 +0800665 pre_segment = segment;
666 }
667 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
668 //used the last one segment to open
669 //get segment info
670 player->segment_is_open = DVR_TRUE;
671 player->cur_segment_id = pre_segment->segment_id;
672 player->cur_segment.segment_id = pre_segment->segment_id;
673 player->cur_segment.flags = pre_segment->flags;
Wentao MA96f68962022-06-15 19:45:35 +0800674 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 +0800675 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
676 //pids
677 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
678 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800679 }
680 if (found != 2) {
681 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800682 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800683 }
684 return DVR_SUCCESS;
685}
hualing chen040df222020-01-17 13:35:02 +0800686//open next segment to play,if reach list end return errro.
687static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800688{
hualing chen040df222020-01-17 13:35:02 +0800689 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800690 Segment_OpenParams_t params;
691 int ret = DVR_SUCCESS;
692
hualing chena540a7e2020-03-27 16:44:05 +0800693 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800694 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800695 return DVR_FAILURE;
696 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800697 pthread_mutex_lock(&player->segment_lock);
hualing chen926a8ec2021-12-20 20:38:24 +0800698retry:
hualing chena540a7e2020-03-27 16:44:05 +0800699 ret = _dvr_get_next_segmentId(handle);
700 if (ret == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800701 DVR_PB_INFO("not found segment info");
hualing chen4b7c15d2020-04-07 16:13:48 +0800702 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800703 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800704 }
705
706 if (player->r_handle != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800707 DVR_PB_INFO("close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800708 segment_close(player->r_handle);
709 player->r_handle = NULL;
710 }
711
Wentao MA4d85ff32022-09-23 11:36:18 +0800712 memset((void*)&params,0,sizeof(params));
Wentao MA270dc0f2022-08-23 13:17:26 +0800713 //cp current segment path to location
hualing chen5cbe1a62020-02-10 16:36:36 +0800714 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800715 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800716 params.mode = SEGMENT_MODE_READ;
Wentao MA96f68962022-06-15 19:45:35 +0800717 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 +0800718
hualing chen86e7d482020-01-16 15:13:33 +0800719 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800720 if (ret == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800721 DVR_PB_INFO("open segment error");
hualing chen926a8ec2021-12-20 20:38:24 +0800722 goto retry;
hualing chen4b7c15d2020-04-07 16:13:48 +0800723 }
Wentao MA01de0e62022-01-10 18:48:23 +0800724 // Keep the start segment_id when the first segment_open is called during a playback
725 if (player->first_start_id == UINT64_MAX) {
726 player->first_start_id = player->cur_segment.segment_id;
727 }
hualing chen87072a82020-03-12 16:20:12 +0800728 pthread_mutex_unlock(&player->segment_lock);
729 int total = _dvr_get_end_time( handle);
730 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800731 if (IS_FB(player->speed)) {
732 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800733 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800734 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +0800735 DVR_PB_INFO("seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800736 }
hualing chen87072a82020-03-12 16:20:12 +0800737 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800738 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +0800739 DVR_PB_INFO("next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800740 return ret;
741}
742
hualing chen5cbe1a62020-02-10 16:36:36 +0800743//open next segment to play,if reach list end return errro.
744static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
745{
746 DVR_Playback_t *player = (DVR_Playback_t *) handle;
747 Segment_OpenParams_t params;
748 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800749 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800750 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800751 return DVR_FAILURE;
752 }
hualing chencc91e1c2020-02-28 13:26:17 +0800753 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800754 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800755 }
hualing chencc91e1c2020-02-28 13:26:17 +0800756 uint64_t id = segment_id;
Wentao MA07d3d742022-09-06 09:58:05 +0800757 DVR_PB_INFO("start finding segment[%lld] info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800758 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800759
760 DVR_PlaybackSegmentInfo_t *segment;
761
762 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800763
hualing chen5cbe1a62020-02-10 16:36:36 +0800764 list_for_each_entry(segment, &player->segment_list, head)
765 {
Wentao MA96f68962022-06-15 19:45:35 +0800766 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 +0800767 if (segment->segment_id == segment_id) {
768 found = 1;
769 }
770 if (found == 1) {
Wentao MA96f68962022-06-15 19:45:35 +0800771 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 +0800772 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800773 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800774 player->cur_segment_id = segment->segment_id;
775 player->cur_segment.segment_id = segment->segment_id;
776 player->cur_segment.flags = segment->flags;
Wentao MAe88ad702022-09-02 10:35:00 +0800777 const int len = strlen(segment->location);
778 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
779 DVR_PB_ERROR("Invalid segment.location length %d",len);
780 pthread_mutex_unlock(&player->segment_lock);
781 return DVR_FAILURE;
782 }
783 strncpy(player->cur_segment.location, segment->location, len+1);
hualing chen5cbe1a62020-02-10 16:36:36 +0800784 //pids
785 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +0800786 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 +0800787 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800788 }
789 }
hualing chencc91e1c2020-02-28 13:26:17 +0800790 if (found == 0) {
Wentao MA96f68962022-06-15 19:45:35 +0800791 DVR_PB_INFO("not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800792 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800793 return DVR_FAILURE;
794 }
Wentao MA4d85ff32022-09-23 11:36:18 +0800795 memset((void*)&params,0,sizeof(params));
Wentao MAe88ad702022-09-02 10:35:00 +0800796
797 const int len2 = strlen(player->cur_segment.location);
798 if (len2 >= DVR_MAX_LOCATION_SIZE || len2 <= 0) {
799 DVR_PB_ERROR("Invalid cur_segment.location length %d",len2);
800 pthread_mutex_unlock(&player->segment_lock);
801 return DVR_FAILURE;
802 }
803 strncpy(params.location, player->cur_segment.location, len2+1);
hualing chen5cbe1a62020-02-10 16:36:36 +0800804 params.segment_id = (uint64_t)player->cur_segment.segment_id;
805 params.mode = SEGMENT_MODE_READ;
Wentao MA96f68962022-06-15 19:45:35 +0800806 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 +0800807 if (player->r_handle != NULL) {
808 segment_close(player->r_handle);
809 player->r_handle = NULL;
810 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800811 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800812 if (ret == DVR_FAILURE) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800813 DVR_PB_INFO("segment open error");
hualing chen4b7c15d2020-04-07 16:13:48 +0800814 }
Wentao MA01de0e62022-01-10 18:48:23 +0800815 // Keep the start segment_id when the first segment_open is called during a playback
816 if (player->first_start_id == UINT64_MAX) {
817 player->first_start_id = player->cur_segment.segment_id;
818 }
hualing chen2aba4022020-03-02 13:49:55 +0800819 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800820 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800821
Wentao MA96f68962022-06-15 19:45:35 +0800822 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 +0800823 return ret;
824}
825
826
827//get play info by segment id
828static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
829 uint64_t segment_id,
Wentao MA270dc0f2022-08-23 13:17:26 +0800830 am_tsplayer_video_params *video_param,
831 am_tsplayer_audio_params *audio_param, am_tsplayer_audio_params *ad_param) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800832
833 DVR_Playback_t *player = (DVR_Playback_t *) handle;
834 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800835 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800836 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800837 return DVR_FAILURE;
838 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800839
840 int found = 0;
841
842 list_for_each_entry(segment, &player->segment_list, head)
843 {
hualing chen87072a82020-03-12 16:20:12 +0800844 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800845 //get first segment from list
846 found = 1;
847 }
848 if (segment->segment_id == segment_id) {
849 found = 1;
850 }
851 if (found == 1) {
852 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800853 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800854 player->cur_segment_id = segment->segment_id;
Wentao MA96f68962022-06-15 19:45:35 +0800855 DVR_PB_INFO("get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800856 player->cur_segment.segment_id = segment->segment_id;
857 player->cur_segment.flags = segment->flags;
858 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800859 player->cur_segment.pids.video.pid = segment->pids.video.pid;
860 player->cur_segment.pids.video.format = segment->pids.video.format;
861 player->cur_segment.pids.video.type = segment->pids.video.type;
862 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
863 player->cur_segment.pids.audio.format = segment->pids.audio.format;
864 player->cur_segment.pids.audio.type = segment->pids.audio.type;
865 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
866 player->cur_segment.pids.ad.format = segment->pids.ad.format;
867 player->cur_segment.pids.ad.type = segment->pids.ad.type;
868 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800869 //
Wentao MA270dc0f2022-08-23 13:17:26 +0800870 video_param->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
871 video_param->pid = segment->pids.video.pid;
872 audio_param->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
873 audio_param->pid = segment->pids.audio.pid;
874 ad_param->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
875 ad_param->pid =segment->pids.ad.pid;
876 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 +0800877 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800878 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800879 }
880 }
hualing chencc91e1c2020-02-28 13:26:17 +0800881 if (found != 2) {
882 //list is null or reache list end
Wentao MA96f68962022-06-15 19:45:35 +0800883 DVR_PB_INFO("get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800884 return DVR_FAILURE;
885 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800886
887 return DVR_SUCCESS;
888}
hualing chencc91e1c2020-02-28 13:26:17 +0800889static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
890 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800891 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800892 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800893 return DVR_FAILURE;
894 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800895
hualing chencc91e1c2020-02-28 13:26:17 +0800896 //compare cur segment
897 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
898 {
899 //check video pids, stop or restart
hualing chen99508642021-10-18 15:41:17 +0800900 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800901 //check sub audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800902 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800903 //check audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800904 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 1);
Wentao MA96f68962022-06-15 19:45:35 +0800905 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 +0800906 //check pcr pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800907 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800908 }
hualing chena540a7e2020-03-27 16:44:05 +0800909 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800910}
hualing chen5cbe1a62020-02-10 16:36:36 +0800911
hualing chend241c7a2021-06-22 13:34:27 +0800912static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
913{
914 DVR_Playback_t *player = (DVR_Playback_t *) handle;
915 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800916 DVR_PB_INFO("player is NULL");
hualing chend241c7a2021-06-22 13:34:27 +0800917 return DVR_TRUE;
918 }
wentao.maa210e5e2022-10-12 16:10:03 +0800919 char buf[10] = {0};
hualing chend241c7a2021-06-22 13:34:27 +0800920 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
Wentao MA96f68962022-06-15 19:45:35 +0800921 DVR_PB_INFO("player get prop[%d][%s]", atoi(buf), buf);
hualing chend241c7a2021-06-22 13:34:27 +0800922
923 if (atoi(buf) != 1) {
924 //return DVR_TRUE;
925 }
926
Wentao MA96f68962022-06-15 19:45:35 +0800927 DVR_PB_INFO(":play speed: %f ply dur: %u sys_dur: %u",
hualing chen03fd4942021-07-15 15:56:41 +0800928 player->speed,
929 player->con_spe.ply_dur,
930 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800931
932 if (player->speed != 1.0f)
933 return DVR_TRUE;
934
935 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800936 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800937 return DVR_FALSE;
938
939 return DVR_TRUE;
940}
941
hualing chencc91e1c2020-02-28 13:26:17 +0800942static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
943{
944 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800945 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800946 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800947 return DVR_FAILURE;
948 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800949 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
Wentao MA96f68962022-06-15 19:45:35 +0800950 DVR_PB_INFO("vendor is amlogic. no used segment flag to hide or show av");
hualing chenf43b8ba2020-07-28 13:11:42 +0800951 return DVR_SUCCESS;
952 }
Wentao MA96f68962022-06-15 19:45:35 +0800953 DVR_PB_INFO("flag[0x%x]id[%lld]last[0x%x][%llu]",
hualing chen03fd4942021-07-15 15:56:41 +0800954 player->cur_segment.flags,
955 player->cur_segment.segment_id,
956 player->last_segment.flags,
957 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800958 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
959 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800960 //enable display
Wentao MA96f68962022-06-15 19:45:35 +0800961 DVR_PB_INFO("unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800962 AmTsPlayer_showVideo(player->handle);
963 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800964 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
965 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800966 //disable display
Wentao MA96f68962022-06-15 19:45:35 +0800967 DVR_PB_INFO("mute");
hualing chen2aba4022020-03-02 13:49:55 +0800968 AmTsPlayer_hideVideo(player->handle);
969 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800970 }
971 return DVR_SUCCESS;
972}
hualing chene3797f02021-01-13 14:53:28 +0800973/*
Wentao MA270dc0f2022-08-23 13:17:26 +0800974if decode success first time.
975success: return true
hualing chene3797f02021-01-13 14:53:28 +0800976fail: return false
977*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800978static DVR_Bool_t _dvr_pauselive_decode_success(DVR_PlaybackHandle_t handle) {
hualing chena540a7e2020-03-27 16:44:05 +0800979 DVR_Playback_t *player = (DVR_Playback_t *) handle;
980 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800981 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800982 return DVR_TRUE;
983 }
hualing chene3797f02021-01-13 14:53:28 +0800984 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800985 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800986 } else {
987 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800988 }
989}
hualing chen86e7d482020-01-16 15:13:33 +0800990static void* _dvr_playback_thread(void *arg)
991{
hualing chen040df222020-01-17 13:35:02 +0800992 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800993 //int need_open_segment = 1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800994 am_tsplayer_input_buffer input_buffer;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800995 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800996 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800997
hualing chen39628212020-05-14 10:35:13 +0800998 #define MAX_REACHEND_TIMEOUT (3000)
999 int reach_end_timeout = 0;//ms
1000 int cache_time = 0;
hualing chenb9a1a2c2021-12-31 11:27:59 +08001001 int timeout = 200;//ms
hualing chen40dd5462021-11-26 19:56:20 +08001002 int check_no_data_time = 4;
hualing chen2aba4022020-03-02 13:49:55 +08001003 uint64_t write_timeout_ms = 50;
hualing chen040df222020-01-17 13:35:02 +08001004 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +08001005 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
hualing chen40dd5462021-11-26 19:56:20 +08001006 int first_write = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001007 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +08001008 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001009 DVR_Bool_t goto_rewrite = DVR_FALSE;
yinming ding0ce94922021-09-08 15:09:15 +08001010 char prop_buf[10];
Wentao MA9628de82022-09-13 14:19:37 +08001011 int read = 0;
yinming ding0ce94922021-09-08 15:09:15 +08001012
1013 memset(prop_buf, 0 ,sizeof(prop_buf));
1014 dvr_prop_read("vendor.tv.libdvr.writetm", prop_buf, sizeof(prop_buf));
Wentao MA96f68962022-06-15 19:45:35 +08001015 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 +08001016 if (atoi(prop_buf) > 0)
1017 write_timeout_ms = atoi(prop_buf);
hualing chen03fd4942021-07-15 15:56:41 +08001018
hualing chen56c0a162022-01-27 17:01:50 +08001019
1020 memset(prop_buf, 0 ,sizeof(prop_buf));
1021 dvr_prop_read("vendor.tv.libdvr.waittm", prop_buf, sizeof(prop_buf));
Wentao MA96f68962022-06-15 19:45:35 +08001022 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 +08001023 if (atoi(prop_buf) > 0)
1024 timeout = atoi(prop_buf);
1025
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001026 if (player->is_secure_mode) {
1027 if (dec_buf_size > player->secure_buffer_size) {
Wentao MA96f68962022-06-15 19:45:35 +08001028 DVR_PB_INFO("playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001029 return NULL;
1030 }
1031 }
wentao.maa210e5e2022-10-12 16:10:03 +08001032
1033 uint8_t *buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001034 if (!buf) {
Wentao MA96f68962022-06-15 19:45:35 +08001035 DVR_PB_INFO("Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001036 return NULL;
1037 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001038 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1039 input_buffer.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001040
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001041 dec_bufs.buf_data = malloc(dec_buf_size);
1042 if (!dec_bufs.buf_data) {
Wentao MA96f68962022-06-15 19:45:35 +08001043 DVR_PB_INFO("Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +08001044 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001045 return NULL;
1046 }
1047 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1048 dec_bufs.buf_size = dec_buf_size;
1049
hualing chencc91e1c2020-02-28 13:26:17 +08001050 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001051 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
1052 }
hualing chen86e7d482020-01-16 15:13:33 +08001053
hualing chen86e7d482020-01-16 15:13:33 +08001054 if (ret != DVR_SUCCESS) {
1055 if (buf != NULL) {
1056 free(buf);
hualing chen86e7d482020-01-16 15:13:33 +08001057 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001058 free(dec_bufs.buf_data);
Wentao MA96f68962022-06-15 19:45:35 +08001059 DVR_PB_INFO("get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +08001060 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +08001061 }
Wentao MA96f68962022-06-15 19:45:35 +08001062 DVR_PB_INFO("--player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001063 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +08001064 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
1065 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
1066 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
1067 player->first_trans_ok = DVR_TRUE;
1068 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
1069 }
hualing chencc91e1c2020-02-28 13:26:17 +08001070 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +08001071 //set video show
1072 AmTsPlayer_showVideo(player->handle);
hualing chen40dd5462021-11-26 19:56:20 +08001073 if (player->vendor == DVR_PLAYBACK_VENDOR_AMAZON)
1074 check_no_data_time = 8;
hualing chen86e7d482020-01-16 15:13:33 +08001075 int trick_stat = 0;
1076 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001077
hualing chen86e7d482020-01-16 15:13:33 +08001078 //check trick stat
Wentao MA96f68962022-06-15 19:45:35 +08001079 //DVR_PB_INFO("lock check_no_data_time:%d", check_no_data_time);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001080 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001081
Wentao MA907b6432022-08-01 06:23:08 +00001082 {
Wentao MA9a164002022-08-29 11:20:24 +08001083 static struct timespec _prev_ts={0,0};
Wentao MA907b6432022-08-01 06:23:08 +00001084 struct timespec _nowts,_diffts;
1085 clock_gettime(CLOCK_MONOTONIC, &_nowts);
Wentao MA9a164002022-08-29 11:20:24 +08001086 clock_timespec_subtract(&_nowts,&_prev_ts,&_diffts);
Wentao MA907b6432022-08-01 06:23:08 +00001087 if (_diffts.tv_sec>0) {
1088 char _logbuf[512]={0};
1089 char* _pbuf=_logbuf;
1090 int _nchar=0;
1091 DVR_PlaybackSegmentInfo_t* _segment;
1092 list_for_each_entry(_segment, &player->segment_list, head) {
1093 if (player->cur_segment_id == _segment->segment_id) {
Wentao MA9a164002022-08-29 11:20:24 +08001094 int seg_size = segment_get_cur_segment_size(player->r_handle);
1095 int read_ptr = segment_tell_position(player->r_handle);
Wentao MA907b6432022-08-01 06:23:08 +00001096 float progress = -1.0f;
Wentao MA9a164002022-08-29 11:20:24 +08001097 if (seg_size>0) {
1098 progress = (float)read_ptr*100/seg_size;
Wentao MA907b6432022-08-01 06:23:08 +00001099 }
Wentao MA4d85ff32022-09-23 11:36:18 +08001100 _nchar=sprintf(_pbuf,"%lld(%.1f%%), ",_segment->segment_id,progress);
Wentao MA907b6432022-08-01 06:23:08 +00001101 } else {
1102 _nchar=sprintf(_pbuf,"%lld, ",_segment->segment_id);
1103 }
1104 if (_nchar<0) {
1105 break;
1106 }
1107 _pbuf+=_nchar;
1108 if (_pbuf-_logbuf+10 >= sizeof(_logbuf)) {
1109 sprintf(_pbuf,"...");
1110 break;
1111 }
1112 }
Wentao MA9a164002022-08-29 11:20:24 +08001113 DVR_PB_INFO("clk: %08u, seg_list: %s",_nowts.tv_sec,_logbuf);
1114 _prev_ts=_nowts;
Wentao MA907b6432022-08-01 06:23:08 +00001115 }
1116 }
1117
hualing chen2aba4022020-03-02 13:49:55 +08001118 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1119 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001120 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001121 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001122 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001123 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001124 {
hualing chen2aba4022020-03-02 13:49:55 +08001125 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1126 if (trick_stat > 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001127 DVR_PB_INFO("trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
hualing chen03fd4942021-07-15 15:56:41 +08001128 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001129 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 +08001130 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001131 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001132 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001133 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
Wentao MA270dc0f2022-08-23 13:17:26 +08001134 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_V_START
1135 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_A_START
hualing chen2aba4022020-03-02 13:49:55 +08001136 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
Wentao MA96f68962022-06-15 19:45:35 +08001137 DVR_PB_INFO("pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
hualing chen03fd4942021-07-15 15:56:41 +08001138 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001139 //need change to pause state
1140 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1141 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00001142 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08001143 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001144 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001145 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001146 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001147 AmTsPlayer_pauseVideoDecoding(player->handle);
1148 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001149 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001150 DVR_PB_INFO("clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001151 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001152 }
1153 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1154 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001155 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001156 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001157 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001158 DVR_PB_INFO("fffb pause state----speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001159 player->speed,
1160 player->fffb_current,
1161 _dvr_time_getClock(),
1162 _dvr_playback_state_toString(player->state),
1163 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001164 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001165 //dvr_mutex_unlock(&player->lock);
1166 //dvr_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001167 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001168 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001169 dvr_mutex_unlock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001170 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001171 } else if (_dvr_time_getClock() < player->next_fffb_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001172 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 +08001173 player->speed,
1174 player->fffb_current,
1175 _dvr_time_getClock(),
1176 _dvr_playback_state_toString(player->state),
1177 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001178 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001179 //dvr_mutex_unlock(&player->lock);
1180 //dvr_mutex_lock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001181 AmTsPlayer_pauseVideoDecoding(player->handle);
1182 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001183 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001184 dvr_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001185 continue;
hualing chen2aba4022020-03-02 13:49:55 +08001186 }
Wentao MA96f68962022-06-15 19:45:35 +08001187 DVR_PB_INFO("fffb play-------speed[%f][%d][%d][%s][%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001188 player->speed,
1189 goto_rewrite,
1190 real_read,
1191 _dvr_playback_state_toString(player->state),
1192 player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08001193 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001194 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001195 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1196 player->first_frame = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001197 DVR_PB_INFO("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001198 dvr_mutex_unlock(&player->lock);
1199
hualing chen2aba4022020-03-02 13:49:55 +08001200 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
Wentao MA96f68962022-06-15 19:45:35 +08001201 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001202 dvr_mutex_lock(&player->lock);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001203 player->fffb_play = DVR_FALSE;
hualing chen1ffd85b2021-08-16 15:18:43 +08001204 } else if(player->state == DVR_PLAYBACK_STATE_PAUSE) {
1205 //on pause state,user seek to new pos,we need pause and wait
1206 //user to resume
Wentao MA96f68962022-06-15 19:45:35 +08001207 DVR_PB_INFO("pause, when got first frame event when user seek end");
hualing chen1ffd85b2021-08-16 15:18:43 +08001208 player->first_frame = 0;
1209 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1210 AmTsPlayer_pauseVideoDecoding(player->handle);
1211 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001212 }
hualing chen1ffd85b2021-08-16 15:18:43 +08001213 } else if (player->fffb_play == DVR_TRUE){
hualing chen4b7c15d2020-04-07 16:13:48 +08001214 //for first into fffb when reset speed
1215 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1216 _dvr_time_getClock() < player->next_fffb_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001217 DVR_PB_INFO("fffb timeout-fffb play---speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001218 player->speed,
1219 player->fffb_current,
1220 _dvr_time_getClock(),
1221 _dvr_playback_state_toString(player->state),
1222 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001223 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001224 //dvr_mutex_unlock(&player->lock);
1225 //dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001226 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001227 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001228 dvr_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001229 continue;
1230 }
Wentao MA96f68962022-06-15 19:45:35 +08001231 DVR_PB_INFO("fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001232 player->speed,
1233 goto_rewrite,
1234 real_read,
1235 _dvr_playback_state_toString(player->state),
1236 player->cmd.cur_cmd,
1237 player->fffb_play);
Wentao MA96f68962022-06-15 19:45:35 +08001238 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001239 dvr_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001240 goto_rewrite = DVR_FALSE;
1241 real_read = 0;
Wentao MA63c6cba2022-09-20 10:14:29 +08001242 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001243 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001244 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1245 player->first_frame = 0;
Wentao MA63c6cba2022-09-20 10:14:29 +08001246 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001247 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001248 dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001249 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001250 }
hualing chenb31a6c62020-01-13 17:27:00 +08001251 }
hualing chen86e7d482020-01-16 15:13:33 +08001252
hualing chen30423862021-04-16 14:39:12 +08001253 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1254 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001255 //check is need send time send end
Wentao MA96f68962022-06-15 19:45:35 +08001256 DVR_PB_INFO("pause, continue");
1257 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001258 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001259 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001260 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001261 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001262 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001263 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001264 continue;
1265 }
hualing chen266b9502020-04-04 17:39:39 +08001266 //when seek action is done. we need drop write timeout data.
1267 if (player->drop_ts == DVR_TRUE) {
1268 goto_rewrite = DVR_FALSE;
1269 real_read = 0;
1270 player->drop_ts = DVR_FALSE;
1271 }
hualing chen2aba4022020-03-02 13:49:55 +08001272 if (goto_rewrite == DVR_TRUE) {
1273 goto_rewrite = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08001274 //DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001275 dvr_mutex_unlock(&player->lock);
hualing chen3bcf3be2021-12-22 20:15:01 +08001276 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +08001277 //DVR_PB_INFO("rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001278 goto rewrite;
1279 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001280 //.check is need send time send end
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001281 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001282 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001283 dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001284 pthread_mutex_lock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001285 //DVR_PB_INFO("start read");
Wentao MA9628de82022-09-13 14:19:37 +08001286 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001287 real_read = real_read + read;
1288 player->ts_cache_len = real_read;
Wentao MA96f68962022-06-15 19:45:35 +08001289 //DVR_PB_INFO("start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001290 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001291 //DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001292 dvr_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001293 if (read < 0 && errno == EIO) {
1294 //EIO ERROR, EXIT THRAD
Wentao MA96f68962022-06-15 19:45:35 +08001295 DVR_PB_INFO("read error.EIO error, exit thread");
hualing chenb5cd42e2020-04-15 17:03:34 +08001296 DVR_Play_Notify_t notify;
1297 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1298 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001299 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001300 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001301 goto end;
1302 } else if (read < 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001303 DVR_PB_INFO("read error.:%d EIO:%d", errno, EIO);
hualing chenb5cd42e2020-04-15 17:03:34 +08001304 }
hualing chen87072a82020-03-12 16:20:12 +08001305 //if on fb mode and read file end , we need calculate pos to retry read.
1306 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001307 DVR_PB_INFO("recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]",
hualing chen03fd4942021-07-15 15:56:41 +08001308 read,
1309 real_read,
1310 buf_len,
1311 player->speed,
1312 player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001313 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
Wentao MA96f68962022-06-15 19:45:35 +08001314 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001315 dvr_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001316 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001317 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001318 dvr_mutex_unlock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001319 continue;
1320 }
Wentao MA96f68962022-06-15 19:45:35 +08001321 //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 +08001322 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001323 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001324 #define MIN_CACHE_TIME (3000)
1325 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001326 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001327 if (_cache_time > MIN_CACHE_TIME) {
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001328 //dvr_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001329 /*if cache time > 20s , we think get time is error,*/
1330 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
Wentao MA907b6432022-08-01 06:23:08 +00001331 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 +08001332 }
hualing chen1679f812021-11-08 15:17:46 +08001333 //_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 +08001334 //dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001335 // 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 +08001336 //continue;
1337 }
hualing chen969fe7b2021-05-26 15:13:17 +08001338
hualing chen040df222020-01-17 13:35:02 +08001339 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001340 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001341 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001342
1343 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chen1679f812021-11-08 15:17:46 +08001344
1345 if (ret != DVR_SUCCESS && delay < MIN_TSPLAYER_DELAY_TIME) {
1346 player->noData++;
Wentao MA96f68962022-06-15 19:45:35 +08001347 DVR_PB_INFO("playback nodata[%d]", player->noData);
hualing chen40dd5462021-11-26 19:56:20 +08001348 if (player->noData == check_no_data_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001349 DVR_PB_INFO("playback send nodata event nodata[%d]", player->noData);
hualing chene3797f02021-01-13 14:53:28 +08001350 //send event here and pause
1351 DVR_Play_Notify_t notify;
1352 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1353 notify.event = DVR_PLAYBACK_EVENT_NODATA;
Wentao MA96f68962022-06-15 19:45:35 +08001354 DVR_PB_INFO("send event DVR_PLAYBACK_EVENT_NODATA--");
hualing chene3797f02021-01-13 14:53:28 +08001355 //get play statue not here
1356 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1357 }
1358 }
1359 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001360 if ((ret != DVR_SUCCESS &&
hualing chen03fd4942021-07-15 15:56:41 +08001361 (player->vendor != DVR_PLAYBACK_VENDOR_AMAZON) &&
hualing chen041c4092020-04-05 15:11:50 +08001362 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001363 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
Wentao MA270dc0f2022-08-23 13:17:26 +08001364 _dvr_pauselive_decode_success((DVR_PlaybackHandle_t)player)) ||
hualing chen39628212020-05-14 10:35:13 +08001365 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001366 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001367 DVR_Play_Notify_t notify;
1368 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1369 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1370 //get play statue not here
1371 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001372 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001373 //continue,timeshift mode, when read end,need wait cur recording segment
Wentao MA96f68962022-06-15 19:45:35 +08001374 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 +08001375 dvr_mutex_lock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08001376 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001377 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08001378 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001379 } else if (ret != DVR_SUCCESS) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001380 DVR_PB_INFO("delay:%d pauselive:%d", delay, _dvr_pauselive_decode_success((DVR_PlaybackHandle_t)player));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001381 dvr_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08001382 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001383 dvr_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08001384 delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chena540a7e2020-03-27 16:44:05 +08001385 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001386 if (delay < cache_time) {
1387 //delay time is changed and then has data to play, so not start timeout
hualing chen1679f812021-11-08 15:17:46 +08001388 reach_end_timeout = 0;
hualing chen39628212020-05-14 10:35:13 +08001389 } else {
1390 reach_end_timeout = reach_end_timeout + timeout;
1391 }
1392 cache_time = delay;
hualing chen31140872020-03-25 12:29:26 +08001393 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001394 }
hualing chen39628212020-05-14 10:35:13 +08001395 reach_end_timeout = 0;
1396 cache_time = 0;
hualing chen2932d372020-04-29 13:44:00 +08001397 //change next segment success case
1398 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001399 dvr_mutex_lock(&player->lock);
hualing chen40dd5462021-11-26 19:56:20 +08001400 player->noData = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001401 DVR_PB_INFO("_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001402 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1403 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen21a40372021-10-29 11:07:26 +08001404 pthread_mutex_lock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001405 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001406 real_read = real_read + read;
1407 player->ts_cache_len = real_read;
1408 pthread_mutex_unlock(&player->segment_lock);
1409
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001410 dvr_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001411 }//read len 0 check end
hualing chen40dd5462021-11-26 19:56:20 +08001412 if (player->noData >= check_no_data_time) {
hualing chene3797f02021-01-13 14:53:28 +08001413 player->noData = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001414 DVR_PB_INFO("playback send data event resume[%d]", player->noData);
hualing chene3797f02021-01-13 14:53:28 +08001415 //send event here and pause
1416 DVR_Play_Notify_t notify;
1417 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1418 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
Wentao MA96f68962022-06-15 19:45:35 +08001419 DVR_PB_INFO("----send event DVR_PLAYBACK_EVENT_DATARESUME");
hualing chene3797f02021-01-13 14:53:28 +08001420 //get play statue not here
1421 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001422 }
hualing chen39628212020-05-14 10:35:13 +08001423 reach_end_timeout = 0;
hualing chen21a40372021-10-29 11:07:26 +08001424 //real_read = real_read + read;
Wentao MA270dc0f2022-08-23 13:17:26 +08001425 input_buffer.buf_size = real_read;
1426 input_buffer.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001427
Wentao MA270dc0f2022-08-23 13:17:26 +08001428 //check read data len,if len < 0, we need continue
1429 if (input_buffer.buf_size <= 0 || input_buffer.buf_data == NULL) {
1430 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 +08001431 real_read = 0;
Wentao MA63c6cba2022-09-20 10:14:29 +08001432 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001433 player->ts_cache_len = 0;
Wentao MA63c6cba2022-09-20 10:14:29 +08001434 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001435 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001436 }
hualing chen266b9502020-04-04 17:39:39 +08001437 //if need write whole block size, we need check read buf len is eq block size.
1438 if (b_writed_whole_block == DVR_TRUE) {
1439 //buf_len is block size value.
1440 if (real_read < buf_len) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001441 //continue to read data from file
Wentao MA96f68962022-06-15 19:45:35 +08001442 DVR_PB_INFO("read buf len[%d] is < block size [%d]", real_read, buf_len);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001443 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001444 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1445 dvr_mutex_unlock(&player->lock);
1446 DVR_PB_INFO("read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001447 continue;
1448 } else if (real_read > buf_len) {
Wentao MA96f68962022-06-15 19:45:35 +08001449 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 +08001450 }
1451 }
1452
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001453 if (player->dec_func) {
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001454 DVR_CryptoParams_t crypto_params;
1455
1456 memset(&crypto_params, 0, sizeof(crypto_params));
1457 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1458 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1459 crypto_params.segment_id = player->cur_segment.segment_id;
Wentao MA270dc0f2022-08-23 13:17:26 +08001460 crypto_params.offset = segment_tell_position(player->r_handle) - input_buffer.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001461 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
Wentao MA96f68962022-06-15 19:45:35 +08001462 DVR_PB_INFO("offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001463 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1464 crypto_params.input_buffer.addr = (size_t)buf;
1465 crypto_params.input_buffer.size = real_read;
1466
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001467 if (player->is_secure_mode) {
1468 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1469 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1470 crypto_params.output_buffer.size = dec_buf_size;
1471 ret = player->dec_func(&crypto_params, player->dec_userdata);
Wentao MA270dc0f2022-08-23 13:17:26 +08001472 input_buffer.buf_data = player->secure_buffer;
1473 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001474 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08001475 DVR_PB_INFO("decrypt failed");
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001476 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001477 input_buffer.buf_size = crypto_params.output_size;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001478 } else { // only for NAGRA
1479 crypto_params.output_buffer.type = crypto_params.input_buffer.type;
1480 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1481 crypto_params.output_buffer.size = crypto_params.input_buffer.size;
1482 ret = player->dec_func(&crypto_params, player->dec_userdata);
Wentao MA270dc0f2022-08-23 13:17:26 +08001483 input_buffer.buf_data = (uint8_t*)crypto_params.output_buffer.addr;
1484 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001485 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08001486 DVR_PB_INFO("decrypt failed");
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001487 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001488 input_buffer.buf_size = crypto_params.output_buffer.size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001489 }
Yahui Han1fbf3292021-11-08 18:17:19 +08001490 } else if (player->cryptor) {
Yahui Han63b23b42021-12-07 15:37:46 +08001491 int len = real_read;
Yahui Han1fbf3292021-11-08 18:17:19 +08001492 am_crypt_des_crypt(player->cryptor, dec_bufs.buf_data, buf, &len, 1);
Wentao MA270dc0f2022-08-23 13:17:26 +08001493 input_buffer.buf_data = dec_bufs.buf_data;
1494 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1495 input_buffer.buf_size = len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001496 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001497rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001498 if (player->drop_ts == DVR_TRUE) {
1499 //need drop ts data when seek occur.we need read next loop,drop this ts data
1500 goto_rewrite = DVR_FALSE;
1501 real_read = 0;
Wentao MA63c6cba2022-09-20 10:14:29 +08001502 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001503 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001504 player->drop_ts = DVR_FALSE;
Wentao MA63c6cba2022-09-20 10:14:29 +08001505 pthread_mutex_unlock(&player->segment_lock);
1506 DVR_PB_INFO("----drop ts");
hualing chenbcada022020-04-22 14:27:01 +08001507 continue;
1508 }
hualing chen21a40372021-10-29 11:07:26 +08001509
1510 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001511 player->ts_cache_len = real_read;
hualing chenb9a02922021-12-14 11:29:47 +08001512 //used for printf first write data time.
1513 //to check change channel kpi.
1514 if (first_write == 0) {
1515 first_write++;
Wentao MA270dc0f2022-08-23 13:17:26 +08001516 DVR_PB_INFO("----first write ts data");
hualing chenb9a02922021-12-14 11:29:47 +08001517 }
1518
Wentao MA270dc0f2022-08-23 13:17:26 +08001519 ret = AmTsPlayer_writeData(player->handle, &input_buffer, write_timeout_ms);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001520 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001521 player->ts_cache_len = 0;
hualing chen21a40372021-10-29 11:07:26 +08001522 pthread_mutex_unlock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +08001523 real_read = 0;
1524 write_success++;
hualing chend241c7a2021-06-22 13:34:27 +08001525 if (CONTROL_SPEED_ENABLE == 1) {
1526check0:
Yahui Hanc7ab63d2022-08-29 15:59:08 +08001527 if (!player->is_running) {
Yahui Han28c66ed2022-09-08 10:32:33 +08001528 //DVR_PB_DEBUG(1, "playback thread exit");
Yahui Hanc7ab63d2022-08-29 15:59:08 +08001529 break;
1530 }
hualing chend241c7a2021-06-22 13:34:27 +08001531 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001532 dvr_mutex_lock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001533 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001534 dvr_mutex_unlock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001535 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1536 goto check0;
1537 }
1538 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001539 //DVR_PB_INFO("write write_success:%d input_buffer.buf_size:%d", write_success, input_buffer.buf_size);
hualing chen87072a82020-03-12 16:20:12 +08001540 } else {
hualing chen21a40372021-10-29 11:07:26 +08001541 pthread_mutex_unlock(&player->segment_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08001542 DVR_PB_INFO("write time out write_success:%d input_buffer.buf_size:%d systime:%u",
hualing chen03fd4942021-07-15 15:56:41 +08001543 write_success,
Wentao MA270dc0f2022-08-23 13:17:26 +08001544 input_buffer.buf_size,
hualing chen03fd4942021-07-15 15:56:41 +08001545 _dvr_time_getClock());
1546
hualing chena540a7e2020-03-27 16:44:05 +08001547 write_success = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001548 if (CONTROL_SPEED_ENABLE == 1) {
1549check1:
Yahui Hanc7ab63d2022-08-29 15:59:08 +08001550 if (!player->is_running) {
Yahui Han28c66ed2022-09-08 10:32:33 +08001551 //DVR_PB_DEBUG(1, "playback thread exit");
Yahui Hanc7ab63d2022-08-29 15:59:08 +08001552 break;
1553 }
hualing chend241c7a2021-06-22 13:34:27 +08001554 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001555 dvr_mutex_lock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001556 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001557 dvr_mutex_unlock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001558 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1559 goto check1;
1560 }
1561 }
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001562 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001563 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001564 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001565 if (!player->is_running) {
Wentao MA96f68962022-06-15 19:45:35 +08001566 DVR_PB_INFO("playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001567 break;
1568 }
hualing chen2aba4022020-03-02 13:49:55 +08001569 goto_rewrite = DVR_TRUE;
1570 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001571 }
1572 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001573end:
Wentao MA96f68962022-06-15 19:45:35 +08001574 DVR_PB_INFO("playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001575 free(buf);
1576 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001577 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001578}
1579
1580
hualing chen040df222020-01-17 13:35:02 +08001581static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001582{
hualing chen040df222020-01-17 13:35:02 +08001583 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001584
1585 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001586 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001587 return DVR_FAILURE;
1588 }
Wentao MA96f68962022-06-15 19:45:35 +08001589 DVR_PB_INFO("start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001590 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001591 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001592 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001593 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001594 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001595 if (rc < 0)
1596 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001597 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001598}
1599
1600
hualing chen040df222020-01-17 13:35:02 +08001601static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001602{
hualing chen040df222020-01-17 13:35:02 +08001603 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001604
1605 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001606 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001607 return DVR_FAILURE;
1608 }
1609
Wentao MA96f68962022-06-15 19:45:35 +08001610 DVR_PB_INFO("stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001611 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001612 {
1613 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001614 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001615 pthread_join(player->playback_thread, NULL);
1616 }
1617 if (player->r_handle) {
1618 segment_close(player->r_handle);
1619 player->r_handle = NULL;
1620 }
Wentao MA96f68962022-06-15 19:45:35 +08001621 DVR_PB_INFO(":end");
hualing chen86e7d482020-01-16 15:13:33 +08001622 return 0;
1623}
1624
hualing chen1679f812021-11-08 15:17:46 +08001625static int getFakePid()
1626{
1627 char fake_pid_prop[] = "vendor.tv.dtv.fake_pid";
wentao.maa210e5e2022-10-12 16:10:03 +08001628 char buf[32] = {0};
hualing chen1679f812021-11-08 15:17:46 +08001629 int pid = 0xffff;
1630
1631 dvr_prop_read(fake_pid_prop, buf, sizeof(buf));
1632
1633 if (sscanf(buf, "%i", &pid) != 1)
1634 {
Wentao MA96f68962022-06-15 19:45:35 +08001635 DVR_PB_INFO("get fake pid error");
hualing chen1679f812021-11-08 15:17:46 +08001636 pid = 0xffff;
1637 }
1638 return pid;
1639}
1640
1641void dvr_playback_change_seek_state(DVR_PlaybackHandle_t handle,int pid) {
1642
1643 DVR_ASSERT(handle);
1644 DVR_Playback_t *player = (DVR_Playback_t *) handle;
1645 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001646 DVR_PB_INFO("player is NULL");
hualing chen1679f812021-11-08 15:17:46 +08001647 return ;
1648 }
1649 if (player->need_seek_start == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001650 DVR_PB_INFO("player need_seek_start is false");
hualing chen1679f812021-11-08 15:17:46 +08001651 return ;
1652 }
1653
hualing chena5f03222021-12-02 11:22:35 +08001654 if (pid != player->fake_pid) {
hualing chen1679f812021-11-08 15:17:46 +08001655 player->need_seek_start = DVR_FALSE;
1656 }
Wentao MA96f68962022-06-15 19:45:35 +08001657 DVR_PB_INFO("player player->need_seek_start=%d", player->need_seek_start);
hualing chen1679f812021-11-08 15:17:46 +08001658}
1659
hualing chenb31a6c62020-01-13 17:27:00 +08001660/**\brief Open an dvr palyback
1661 * \param[out] p_handle dvr playback addr
1662 * \param[in] params dvr playback open parameters
1663 * \retval DVR_SUCCESS On success
1664 * \return Error code
1665 */
hualing chen040df222020-01-17 13:35:02 +08001666int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001667
hualing chen040df222020-01-17 13:35:02 +08001668 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001669 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001670
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001671 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001672
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001673 dvr_mutex_init(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001674 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001675 pthread_condattr_init(&cattr);
1676 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1677 pthread_cond_init(&player->cond, &cattr);
1678 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001679
hualing chen5cbe1a62020-02-10 16:36:36 +08001680 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001681 INIT_LIST_HEAD(&player->segment_list);
1682 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1683 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001684 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001685 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00001686 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen86e7d482020-01-16 15:13:33 +08001687 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001688 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001689 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001690
hualing chen86e7d482020-01-16 15:13:33 +08001691 //store open params
hualing chen040df222020-01-17 13:35:02 +08001692 player->openParams.dmx_dev_id = params->dmx_dev_id;
1693 player->openParams.block_size = params->block_size;
Wentao MA96f68962022-06-15 19:45:35 +08001694 DVR_PB_INFO("playback open block_size:[%d]",params->block_size);
hualing chen86e7d482020-01-16 15:13:33 +08001695 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001696 player->openParams.event_fn = params->event_fn;
1697 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001698 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001699 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001700
hualing chen5cbe1a62020-02-10 16:36:36 +08001701 player->has_pids = params->has_pids;
1702
hualing chen2aba4022020-03-02 13:49:55 +08001703 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001704
1705 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1706 //for test get callback
1707 if (0 && player->player_callback_func == NULL) {
1708 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1709 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
Wentao MA96f68962022-06-15 19:45:35 +08001710 DVR_PB_INFO("playback open get callback[%p][%p][%p][%p]",
hualing chen03fd4942021-07-15 15:56:41 +08001711 player->player_callback_func,
1712 player->player_callback_userdata,
1713 _dvr_tsplayer_callback_test,
1714 player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001715 }
1716 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001717
hualing chen86e7d482020-01-16 15:13:33 +08001718 //init has audio and video
1719 player->has_video = DVR_FALSE;
1720 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001721 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001722 player->last_segment_id = 0LL;
1723 player->segment_is_open = DVR_FALSE;
Wentao MA5629ad82022-08-24 10:03:02 +08001724 player->audio_presentation_id = -1;
hualing chenb31a6c62020-01-13 17:27:00 +08001725
hualing chen5cbe1a62020-02-10 16:36:36 +08001726 //init ff fb time
hualing chen7ea70a72021-09-09 11:25:13 +08001727 player->fffb_current = 0;
1728 player->fffb_start = 0;
hualing chen03fd4942021-07-15 15:56:41 +08001729 player->fffb_start_pcr = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001730 //seek time
1731 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001732 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001733
Yahui Han1fbf3292021-11-08 18:17:19 +08001734 //allocate cryptor if have clearkey
1735 if (params->keylen > 0) {
1736 player->cryptor = am_crypt_des_open((uint8_t *)params->clearkey,
hualing chen002e5b92022-02-23 17:51:21 +08001737 (uint8_t *)params->cleariv,
1738 params->keylen * 8);
Yahui Han1fbf3292021-11-08 18:17:19 +08001739 if (!player->cryptor) {
Wentao MA96f68962022-06-15 19:45:35 +08001740 DVR_INFO("%s , open des cryptor failed!!!\n", __func__);
Yahui Han1fbf3292021-11-08 18:17:19 +08001741 }
1742 } else {
1743 player->cryptor = NULL;
1744 }
1745
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001746 //init secure stuff
1747 player->dec_func = NULL;
1748 player->dec_userdata = NULL;
1749 player->is_secure_mode = 0;
1750 player->secure_buffer = NULL;
1751 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001752 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001753
hualing chen4b7c15d2020-04-07 16:13:48 +08001754 player->fffb_play = DVR_FALSE;
1755
1756 player->last_send_time_id = UINT64_MAX;
1757 player->last_cur_time = 0;
hualing chen30423862021-04-16 14:39:12 +08001758 player->seek_pause = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001759
hualing chend241c7a2021-06-22 13:34:27 +08001760 //speed con init
1761 if (CONTROL_SPEED_ENABLE == 1) {
1762 player->con_spe.ply_dur = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001763 player->con_spe.ply_sta = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001764 player->con_spe.sys_dur = 0;
1765 player->con_spe.sys_sta = 0;
1766 }
1767
hualing chen03fd4942021-07-15 15:56:41 +08001768 //limit info
1769 player->rec_start = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001770 player->limit = 0;
hualing chen8a657f32021-08-30 13:12:49 +08001771 //need seek to start pos
1772 player->first_start_time = 0;
Wentao MA01de0e62022-01-10 18:48:23 +08001773 player->first_start_id = UINT64_MAX;
1774 player->check_cache_flag = DVR_TRUE;
hualing chen8a657f32021-08-30 13:12:49 +08001775 player->need_seek_start = DVR_TRUE;
hualing chena5f03222021-12-02 11:22:35 +08001776 //fake_pid init
1777 player->fake_pid = getFakePid();
hualing chen86e7d482020-01-16 15:13:33 +08001778 *p_handle = player;
1779 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001780}
1781
1782/**\brief Close an dvr palyback
1783 * \param[in] handle playback handle
1784 * \retval DVR_SUCCESS On success
1785 * \return Error code
1786 */
hualing chen040df222020-01-17 13:35:02 +08001787int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001788
hualing chen86e7d482020-01-16 15:13:33 +08001789 DVR_ASSERT(handle);
Wentao MA96f68962022-06-15 19:45:35 +08001790 DVR_PB_INFO(":into");
hualing chen040df222020-01-17 13:35:02 +08001791 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001792 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001793 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001794 return DVR_FAILURE;
1795 }
1796
hualing chencc91e1c2020-02-28 13:26:17 +08001797 if (player->state != DVR_PLAYBACK_STATE_STOP)
1798 {
Wentao MA96f68962022-06-15 19:45:35 +08001799 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
Yahui Han1fbf3292021-11-08 18:17:19 +08001800 if (player->cryptor) {
1801 am_crypt_des_close(player->cryptor);
1802 player->cryptor = NULL;
1803 }
hualing chencc91e1c2020-02-28 13:26:17 +08001804 dvr_playback_stop(handle, DVR_TRUE);
Wentao MA96f68962022-06-15 19:45:35 +08001805 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
hualing chenb96aa2c2020-04-15 14:13:53 +08001806 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001807 DVR_PB_INFO(":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001808 }
Wentao MA96f68962022-06-15 19:45:35 +08001809 DVR_PB_INFO(":into");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001810 dvr_mutex_destroy(&player->lock);
Zhiqiang Hanc9513462022-06-21 09:55:23 +08001811 pthread_mutex_destroy(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001812 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001813
1814 if (player) {
1815 free(player);
hualing chen040df222020-01-17 13:35:02 +08001816 }
Wentao MA96f68962022-06-15 19:45:35 +08001817 DVR_PB_INFO(":end");
hualing chen86e7d482020-01-16 15:13:33 +08001818 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001819}
1820
Wentao MA270dc0f2022-08-23 13:17:26 +08001821/**\brief Start play audio and video, used start audio api and start video api
hualing chenb31a6c62020-01-13 17:27:00 +08001822 * \param[in] handle playback handle
1823 * \param[in] params audio playback params,contains fmt and pid...
1824 * \retval DVR_SUCCESS On success
1825 * \return Error code
1826 */
hualing chen040df222020-01-17 13:35:02 +08001827int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1828 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA270dc0f2022-08-23 13:17:26 +08001829 am_tsplayer_video_params video_params;
1830 am_tsplayer_audio_params audio_params;
1831 am_tsplayer_audio_params ad_params;
hualing chena540a7e2020-03-27 16:44:05 +08001832
Wentao MA270dc0f2022-08-23 13:17:26 +08001833 memset(&video_params, 0, sizeof(video_params));
1834 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001835
hualing chena540a7e2020-03-27 16:44:05 +08001836 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001837 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001838 return DVR_FAILURE;
1839 }
hualing chencc91e1c2020-02-28 13:26:17 +08001840 uint64_t segment_id = player->cur_segment_id;
Wentao MA96f68962022-06-15 19:45:35 +08001841 DVR_PB_INFO("[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001842
hualing chena540a7e2020-03-27 16:44:05 +08001843 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001844 //can used start api to resume playback
1845 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1846 return dvr_playback_resume(handle);
1847 }
hualing chen87072a82020-03-12 16:20:12 +08001848 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001849 //if flag is paused and not decode first frame. if user resume, we need
hualing chen9b434f02020-06-10 15:06:54 +08001850 //clear flag and set trickmode none
1851 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08001852 DVR_PB_INFO("[%p]clear pause live flag and clear trick mode", handle);
hualing chen9b434f02020-06-10 15:06:54 +08001853 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1854 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1855 }
Wentao MA96f68962022-06-15 19:45:35 +08001856 DVR_PB_INFO("stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001857 return DVR_SUCCESS;
1858 }
hualing chen86e7d482020-01-16 15:13:33 +08001859 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001860 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001861 //get segment info and audio video pid fmt ;
Wentao MA96f68962022-06-15 19:45:35 +08001862 DVR_PB_INFO("lock flag:0x%x", flag);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001863 dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08001864 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen86e7d482020-01-16 15:13:33 +08001865 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08001866 if (video_params.pid != player->fake_pid && !VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
1867 //audio and video pids are all invalid, return error.
1868 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 +08001869 dvr_mutex_unlock(&player->lock);
hualing chencc91e1c2020-02-28 13:26:17 +08001870 DVR_Play_Notify_t notify;
1871 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1872 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1873 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1874 notify.info.transition_failed_data.segment_id = segment_id;
1875 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001876 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001877 return -1;
1878 }
hualing chen31140872020-03-25 12:29:26 +08001879
hualing chencc91e1c2020-02-28 13:26:17 +08001880 {
Wentao MA270dc0f2022-08-23 13:17:26 +08001881 if (VALID_PID(video_params.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001882 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001883 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001884 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08001885 DVR_PB_INFO("set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001886 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1887 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001888 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
Wentao MA96f68962022-06-15 19:45:35 +08001889 DVR_PB_INFO("set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001890 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001891 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001892 DVR_PB_INFO("set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001893 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001894 }
hualing chena93bbbc2020-12-22 17:23:42 +08001895 AmTsPlayer_showVideo(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08001896 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08001897 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08001898 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001899 }
hualing chena540a7e2020-03-27 16:44:05 +08001900
Wentao MA270dc0f2022-08-23 13:17:26 +08001901 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 +08001902 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001903 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1904 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1905 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00001906 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chencc91e1c2020-02-28 13:26:17 +08001907 } else {
1908 player->cmd.last_cmd = player->cmd.cur_cmd;
1909 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001910 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001911 //set fast play
Wentao MA96f68962022-06-15 19:45:35 +08001912 DVR_PB_INFO("start fast");
hualing chen31140872020-03-25 12:29:26 +08001913 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001914 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08001915 if (VALID_PID(ad_params.pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08001916 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08001917 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08001918 dvr_playback_change_seek_state(handle, ad_params.pid);
1919 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08001920 AmTsPlayer_enableADMix(player->handle);
1921 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001922 if (VALID_PID(audio_params.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08001923 DVR_PB_INFO("start audio");
hualing chen969fe7b2021-05-26 15:13:17 +08001924 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08001925 dvr_playback_change_seek_state(handle, audio_params.pid);
1926 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08001927 if (player->audio_presentation_id > -1) {
1928 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
1929 }
hualing chen969fe7b2021-05-26 15:13:17 +08001930 AmTsPlayer_startAudioDecoding(player->handle);
1931 }
hualing chen31140872020-03-25 12:29:26 +08001932 }
hualing chencc91e1c2020-02-28 13:26:17 +08001933 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00001934 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chencc91e1c2020-02-28 13:26:17 +08001935 }
hualing chen86e7d482020-01-16 15:13:33 +08001936 }
hualing chen43a89bc2022-01-19 14:31:20 +08001937#ifdef AVSYNC_USED_PCR
1938 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08001939 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08001940 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
1941 }
1942#endif
Wentao MA96f68962022-06-15 19:45:35 +08001943 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001944 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001945 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001946 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001947}
hualing chen040df222020-01-17 13:35:02 +08001948/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001949 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001950 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001951 * \retval DVR_SUCCESS On success
1952 * \return Error code
1953 */
hualing chen040df222020-01-17 13:35:02 +08001954int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1955 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001956
hualing chena540a7e2020-03-27 16:44:05 +08001957 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001958 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001959 return DVR_FAILURE;
1960 }
1961
Wentao MA96f68962022-06-15 19:45:35 +08001962 DVR_PB_INFO("add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001963 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001964
hualing chen040df222020-01-17 13:35:02 +08001965 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1966 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001967
Wentao MA270dc0f2022-08-23 13:17:26 +08001968 //not memcpy chunk info.
hualing chen040df222020-01-17 13:35:02 +08001969 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001970 //cp location
hualing chen040df222020-01-17 13:35:02 +08001971 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001972
Wentao MA96f68962022-06-15 19:45:35 +08001973 DVR_PB_INFO("add location [%s]id[%lld]flag[%x]", segment->location, segment->segment_id, info->flags);
hualing chen040df222020-01-17 13:35:02 +08001974 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001975
1976 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001977 segment->pids.video.pid = info->pids.video.pid;
1978 segment->pids.video.format = info->pids.video.format;
1979 segment->pids.video.type = info->pids.video.type;
1980
hualing chen2aba4022020-03-02 13:49:55 +08001981 segment->pids.audio.pid = info->pids.audio.pid;
1982 segment->pids.audio.format = info->pids.audio.format;
1983 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001984
hualing chen2aba4022020-03-02 13:49:55 +08001985 segment->pids.ad.pid = info->pids.ad.pid;
1986 segment->pids.ad.format = info->pids.ad.format;
1987 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001988
1989 segment->pids.pcr.pid = info->pids.pcr.pid;
1990
Wentao MA96f68962022-06-15 19:45:35 +08001991 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 +08001992 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001993 list_add_tail(&segment->head, &player->segment_list);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001994 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001995 DVR_PB_DEBUG("unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001996
hualing chen5cbe1a62020-02-10 16:36:36 +08001997 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001998}
hualing chen040df222020-01-17 13:35:02 +08001999/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08002000 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08002001 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08002002 * \retval DVR_SUCCESS On success
2003 * \return Error code
2004 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002005int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08002006 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA96f68962022-06-15 19:45:35 +08002007 DVR_PB_INFO("remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08002008 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002009 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002010 return DVR_FAILURE;
2011 }
2012
hualing chencc91e1c2020-02-28 13:26:17 +08002013 if (segment_id == player->cur_segment_id) {
Wentao MA9a164002022-08-29 11:20:24 +08002014 DVR_PB_INFO("not support remove current segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08002015 return DVR_FAILURE;
2016 }
Wentao MA96f68962022-06-15 19:45:35 +08002017 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002018 dvr_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08002019 DVR_PlaybackSegmentInfo_t *segment = NULL;
2020 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
2021 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002022 {
hualing chen040df222020-01-17 13:35:02 +08002023 if (segment->segment_id == segment_id) {
2024 list_del(&segment->head);
2025 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08002026 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002027 }
hualing chen86e7d482020-01-16 15:13:33 +08002028 }
Wentao MA96f68962022-06-15 19:45:35 +08002029 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002030 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002031
2032 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002033}
hualing chen040df222020-01-17 13:35:02 +08002034/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08002035 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08002036 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08002037 * \retval DVR_SUCCESS On success
2038 * \return Error code
2039 */
hualing chen040df222020-01-17 13:35:02 +08002040int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08002041 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08002042 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA96f68962022-06-15 19:45:35 +08002043 DVR_PB_INFO("update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08002044 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002045 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002046 return DVR_FAILURE;
2047 }
hualing chenf43b8ba2020-07-28 13:11:42 +08002048 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
Wentao MA96f68962022-06-15 19:45:35 +08002049 DVR_PB_INFO("vendor is amlogic. not hide or show av and update segment");
hualing chenf43b8ba2020-07-28 13:11:42 +08002050 return DVR_SUCCESS;
2051 }
hualing chena540a7e2020-03-27 16:44:05 +08002052
hualing chen040df222020-01-17 13:35:02 +08002053 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002054 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002055 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08002056 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002057 {
hualing chen040df222020-01-17 13:35:02 +08002058 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08002059 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08002060 }
hualing chen86e7d482020-01-16 15:13:33 +08002061 // if encramble to free, only set flag and return;
2062
2063 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08002064 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002065 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
2066 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08002067 //disable display, mute
Wentao MA96f68962022-06-15 19:45:35 +08002068 DVR_PB_INFO("mute av");
hualing chen2aba4022020-03-02 13:49:55 +08002069 AmTsPlayer_hideVideo(player->handle);
2070 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002071 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
2072 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08002073 //enable display, unmute
Wentao MA96f68962022-06-15 19:45:35 +08002074 DVR_PB_INFO("unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08002075 AmTsPlayer_showVideo(player->handle);
2076 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08002077 } else {
2078 //do nothing
2079 }
2080 } else {
2081 //do nothing
2082 }
2083 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08002084 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08002085 }
Wentao MA96f68962022-06-15 19:45:35 +08002086 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002087 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002088 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002089}
2090
2091
hualing chen99508642021-10-18 15:41:17 +08002092static 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 +08002093 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen275379e2021-06-15 17:57:21 +08002094 DVR_StreamInfo_t set_pid;
hualing chen99508642021-10-18 15:41:17 +08002095 DVR_StreamInfo_t now_pid;
hualing chen275379e2021-06-15 17:57:21 +08002096
2097 if (type == 0) {
2098 set_pid = set_pids.video;
hualing chen99508642021-10-18 15:41:17 +08002099 now_pid = now_pids.video;
hualing chen275379e2021-06-15 17:57:21 +08002100 } else if (type == 1) {
2101 set_pid = set_pids.audio;
hualing chen99508642021-10-18 15:41:17 +08002102 now_pid = now_pids.audio;
hualing chen275379e2021-06-15 17:57:21 +08002103 } else if (type == 2) {
2104 set_pid = set_pids.ad;
hualing chen99508642021-10-18 15:41:17 +08002105 now_pid = now_pids.ad;
hualing chen275379e2021-06-15 17:57:21 +08002106 } else {
2107 set_pid = set_pids.pcr;
hualing chen99508642021-10-18 15:41:17 +08002108 now_pid = now_pids.pcr;
hualing chen275379e2021-06-15 17:57:21 +08002109 }
2110
hualing chena540a7e2020-03-27 16:44:05 +08002111 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002112 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002113 return DVR_FAILURE;
2114 }
Wentao MA96f68962022-06-15 19:45:35 +08002115 DVR_PB_INFO(" do check");
hualing chen86e7d482020-01-16 15:13:33 +08002116 if (now_pid.pid == set_pid.pid) {
2117 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08002118 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08002119 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08002120 if (VALID_PID(now_pid.pid)) {
2121 //stop now stream
2122 if (type == 0) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002123 //stop video
hualing chenc70a8df2020-05-12 19:23:11 +08002124 if (player->has_video == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08002125 DVR_PB_INFO("stop video");
hualing chenc70a8df2020-05-12 19:23:11 +08002126 AmTsPlayer_stopVideoDecoding(player->handle);
2127 player->has_video = DVR_FALSE;
2128 }
hualing chen86e7d482020-01-16 15:13:33 +08002129 } else if (type == 1) {
2130 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08002131 if (player->has_audio == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08002132 DVR_PB_INFO("stop audio");
hualing chenc70a8df2020-05-12 19:23:11 +08002133 AmTsPlayer_stopAudioDecoding(player->handle);
2134 player->has_audio = DVR_FALSE;
2135 }
hualing chen86e7d482020-01-16 15:13:33 +08002136 } else if (type == 2) {
2137 //stop sub audio
Wentao MA96f68962022-06-15 19:45:35 +08002138 DVR_PB_INFO("stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08002139 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002140 } else if (type == 3) {
2141 //pcr
2142 }
2143 }
2144 if (VALID_PID(set_pid.pid)) {
2145 //start
2146 if (type == 0) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002147 //start video
2148 am_tsplayer_video_params video_params;
2149 video_params.pid = set_pid.pid;
2150 video_params.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002151 player->has_video = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002152 DVR_PB_INFO("start video pid[%d]fmt[%d]",video_params.pid, video_params.codectype);
2153 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen2aba4022020-03-02 13:49:55 +08002154 AmTsPlayer_startVideoDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002155 //playback_device_video_start(player->handle,&video_params);
hualing chen86e7d482020-01-16 15:13:33 +08002156 } else if (type == 1) {
2157 //start audio
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002158 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen275379e2021-06-15 17:57:21 +08002159 if (VALID_PID(set_pids.ad.pid)) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002160 am_tsplayer_audio_params ad_params;
2161 ad_params.pid = set_pids.ad.pid;
2162 ad_params.codectype= _dvr_convert_stream_fmt(set_pids.ad.format, DVR_TRUE);
2163 DVR_PB_INFO("start ad audio pid[%d]fmt[%d]",ad_params.pid, ad_params.codectype);
2164 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chen275379e2021-06-15 17:57:21 +08002165 AmTsPlayer_enableADMix(player->handle);
2166 }
2167
Wentao MA270dc0f2022-08-23 13:17:26 +08002168 am_tsplayer_audio_params audio_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002169
Wentao MA270dc0f2022-08-23 13:17:26 +08002170 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002171
Wentao MA270dc0f2022-08-23 13:17:26 +08002172 audio_params.pid = set_pid.pid;
2173 audio_params.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
hualing chenc70a8df2020-05-12 19:23:11 +08002174 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002175 DVR_PB_INFO("start audio pid[%d]fmt[%d]",audio_params.pid, audio_params.codectype);
2176 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002177 if (player->audio_presentation_id > -1) {
2178 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2179 }
hualing chenc70a8df2020-05-12 19:23:11 +08002180 AmTsPlayer_startAudioDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002181 //playback_device_audio_start(player->handle,&audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002182 }
hualing chen86e7d482020-01-16 15:13:33 +08002183 } else if (type == 2) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002184 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen99508642021-10-18 15:41:17 +08002185 if (set_pids.audio.pid == now_pids.audio.pid) {
2186 //stop audio if audio pid not change
Wentao MA96f68962022-06-15 19:45:35 +08002187 DVR_PB_INFO("stop audio when start ad");
hualing chen99508642021-10-18 15:41:17 +08002188 AmTsPlayer_stopAudioDecoding(player->handle);
2189 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002190 am_tsplayer_audio_params audio_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002191
Wentao MA270dc0f2022-08-23 13:17:26 +08002192 memset(&audio_params, 0, sizeof(audio_params));
2193 audio_params.pid = set_pid.pid;
2194 audio_params.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
hualing chenc70a8df2020-05-12 19:23:11 +08002195 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002196 DVR_PB_INFO("start ad audio pid[%d]fmt[%d]",audio_params.pid, audio_params.codectype);
2197 AmTsPlayer_setADParams(player->handle, &audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002198 AmTsPlayer_enableADMix(player->handle);
hualing chen99508642021-10-18 15:41:17 +08002199
2200 if (set_pids.audio.pid == now_pids.audio.pid) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002201 am_tsplayer_audio_params audio_params;
hualing chen99508642021-10-18 15:41:17 +08002202
Wentao MA270dc0f2022-08-23 13:17:26 +08002203 memset(&audio_params, 0, sizeof(audio_params));
hualing chen99508642021-10-18 15:41:17 +08002204
Wentao MA270dc0f2022-08-23 13:17:26 +08002205 audio_params.pid = set_pids.audio.pid;
2206 audio_params.codectype= _dvr_convert_stream_fmt(set_pids.audio.format, DVR_TRUE);
hualing chen99508642021-10-18 15:41:17 +08002207 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002208 DVR_PB_INFO("restart audio when start ad");
Wentao MA270dc0f2022-08-23 13:17:26 +08002209 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002210 if (player->audio_presentation_id > -1) {
2211 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2212 }
hualing chen99508642021-10-18 15:41:17 +08002213 AmTsPlayer_startAudioDecoding(player->handle);
2214 }
hualing chenc70a8df2020-05-12 19:23:11 +08002215 }
hualing chen86e7d482020-01-16 15:13:33 +08002216 } else if (type == 3) {
2217 //pcr
Wentao MA96f68962022-06-15 19:45:35 +08002218 DVR_PB_INFO("start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08002219 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002220 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002221 //audio and video all close
2222 if (!player->has_audio && !player->has_video) {
Wentao MA907b6432022-08-01 06:23:08 +00002223 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen5cbe1a62020-02-10 16:36:36 +08002224 }
hualing chen43a89bc2022-01-19 14:31:20 +08002225 } else if (type == 2) {
2226 //case disable ad
Wentao MA96f68962022-06-15 19:45:35 +08002227 DVR_PB_INFO("restart audio when stop ad");
hualing chen43a89bc2022-01-19 14:31:20 +08002228 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
Wentao MA6d045b32022-02-18 18:47:25 +08002229 if (VALID_PID(now_pids.audio.pid)) {
2230 //stop audio if audio pid not change
Wentao MA96f68962022-06-15 19:45:35 +08002231 DVR_PB_INFO("stop audio when stop ad pid [0x%x]", now_pids.audio.pid);
Wentao MA6d045b32022-02-18 18:47:25 +08002232 AmTsPlayer_stopAudioDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002233 am_tsplayer_audio_params audio_params;
hualing chen43a89bc2022-01-19 14:31:20 +08002234
Wentao MA270dc0f2022-08-23 13:17:26 +08002235 memset(&audio_params, 0, sizeof(audio_params));
hualing chen43a89bc2022-01-19 14:31:20 +08002236
Wentao MA270dc0f2022-08-23 13:17:26 +08002237 audio_params.pid = now_pids.audio.pid;
2238 audio_params.codectype= _dvr_convert_stream_fmt(now_pids.audio.format, DVR_TRUE);
Wentao MA6d045b32022-02-18 18:47:25 +08002239 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002240 DVR_PB_INFO("restart audio when stop ad");
Wentao MA270dc0f2022-08-23 13:17:26 +08002241 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002242 if (player->audio_presentation_id > -1) {
2243 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2244 }
Wentao MA6d045b32022-02-18 18:47:25 +08002245 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen43a89bc2022-01-19 14:31:20 +08002246 }
Wentao MA6d045b32022-02-18 18:47:25 +08002247 }
hualing chen86e7d482020-01-16 15:13:33 +08002248 }
2249 }
2250 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08002251}
hualing chena5f03222021-12-02 11:22:35 +08002252/**\brief dvr play back only update segment pids info
2253 * only update pid info not to start stop codec.
2254 * \param[in] handle playback handle
2255 * \param[in] segment_id need updated pids segment id
2256 * \param[in] p_pids need updated pids
2257 * \retval DVR_SUCCESS On success
2258 * \return Error code
2259 */
2260int dvr_playback_only_update_segment_pids(DVR_PlaybackHandle_t handle, uint64_t segment_id, DVR_PlaybackPids_t *p_pids) {
2261 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2262 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002263 DVR_PB_INFO("player is NULL");
hualing chena5f03222021-12-02 11:22:35 +08002264 return DVR_FAILURE;
2265 }
2266
2267 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002268 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002269 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002270 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 +08002271 list_for_each_entry(segment, &player->segment_list, head)
2272 {
2273 if (segment->segment_id == segment_id) {
2274 if (player->cur_segment_id == segment_id) {
2275 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
Wentao MA16f870e2022-09-09 11:00:22 +08002276 || player->cmd.state == DVR_PLAYBACK_STATE_FB) {
hualing chena5f03222021-12-02 11:22:35 +08002277 //do nothing when ff fb
Wentao MA96f68962022-06-15 19:45:35 +08002278 DVR_PB_INFO("unlock now is ff fb, not to update cur segment info\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002279 dvr_mutex_unlock(&player->lock);
hualing chena5f03222021-12-02 11:22:35 +08002280 return 0;
2281 }
2282 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
2283 }
2284 //save pids info
Wentao MA96f68962022-06-15 19:45:35 +08002285 DVR_PB_INFO(":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chena5f03222021-12-02 11:22:35 +08002286 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +08002287 DVR_PB_INFO(":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chena5f03222021-12-02 11:22:35 +08002288 break;
2289 }
2290 }
Wentao MA96f68962022-06-15 19:45:35 +08002291 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002292 dvr_mutex_unlock(&player->lock);
hualing chena5f03222021-12-02 11:22:35 +08002293 return DVR_SUCCESS;
2294}
2295
hualing chen5cbe1a62020-02-10 16:36:36 +08002296/**\brief dvr play back update segment pids
2297 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08002298 * add pid stream and stop remove pid stream.
2299 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08002300 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08002301 * \retval DVR_SUCCESS On success
2302 * \return Error code
2303 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002304int 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 +08002305 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002306 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002307 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002308 return DVR_FAILURE;
2309 }
2310
hualing chen040df222020-01-17 13:35:02 +08002311 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002312 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002313 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002314 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 +08002315
hualing chen040df222020-01-17 13:35:02 +08002316 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002317 {
hualing chen040df222020-01-17 13:35:02 +08002318 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002319
2320 if (player->cur_segment_id == segment_id) {
2321 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
Wentao MA16f870e2022-09-09 11:00:22 +08002322 || player->cmd.state == DVR_PLAYBACK_STATE_FB) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002323 //do nothing when ff fb
Wentao MA96f68962022-06-15 19:45:35 +08002324 DVR_PB_INFO("unlock now is ff fb, not to update cur segment info\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002325 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002326 return 0;
2327 }
2328
2329 //if segment is on going segment,we need stop start stream
2330 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
Wentao MA96f68962022-06-15 19:45:35 +08002331 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002332 dvr_mutex_unlock(&player->lock);
hualing chen8a657f32021-08-30 13:12:49 +08002333 if (segment->pids.audio.pid != p_pids->audio.pid &&
2334 segment->pids.audio.pid == 0x1fff) {
hualing chena5f03222021-12-02 11:22:35 +08002335 //not used this to seek to start pos.we will
Wentao MA9a164002022-08-29 11:20:24 +08002336 //add update only api. if need seek to start
hualing chena5f03222021-12-02 11:22:35 +08002337 //pos, we will call only update api and used seek api
2338 //to start and stop av codec
2339 if (0 && player->need_seek_start == DVR_TRUE) {
hualing chen8a657f32021-08-30 13:12:49 +08002340 player->need_seek_start = DVR_FALSE;
2341 pthread_mutex_lock(&player->segment_lock);
2342 player->drop_ts = DVR_TRUE;
2343 player->ts_cache_len = 0;
2344 if (player->first_start_time > 0)
2345 player->first_start_time = player->first_start_time - 1;
2346 segment_seek(player->r_handle, (uint64_t)(player->first_start_time), player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +08002347 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 +08002348 pthread_mutex_unlock(&player->segment_lock);
2349 }
2350 }
hualing chen1679f812021-11-08 15:17:46 +08002351 //check video pids, stop or restart
2352 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 0);
2353 //check sub audio pids stop or restart
2354 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 2);
2355 //check audio pids stop or restart
2356 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 1);
2357 //check pcr pids stop or restart
2358 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 3);
2359
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002360 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002361 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
2362 //if state is pause, we need process at resume api. we only record change info
2363 int v_cmd = DVR_PLAYBACK_CMD_NONE;
2364 int a_cmd = DVR_PLAYBACK_CMD_NONE;
2365 if (VALID_PID(segment->pids.video.pid)
2366 && VALID_PID(p_pids->video.pid)
2367 && segment->pids.video.pid != p_pids->video.pid) {
2368 //restart video
Wentao MA270dc0f2022-08-23 13:17:26 +08002369 v_cmd = DVR_PLAYBACK_CMD_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002370 }
2371 if (!VALID_PID(segment->pids.video.pid)
2372 && VALID_PID(p_pids->video.pid)
2373 && segment->pids.video.pid != p_pids->video.pid) {
2374 //start video
Wentao MA270dc0f2022-08-23 13:17:26 +08002375 v_cmd = DVR_PLAYBACK_CMD_V_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002376 }
2377 if (VALID_PID(segment->pids.video.pid)
2378 && !VALID_PID(p_pids->video.pid)
2379 && segment->pids.video.pid != p_pids->video.pid) {
2380 //stop video
Wentao MA270dc0f2022-08-23 13:17:26 +08002381 v_cmd = DVR_PLAYBACK_CMD_V_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08002382 }
2383 if (VALID_PID(segment->pids.audio.pid)
2384 && VALID_PID(p_pids->audio.pid)
2385 && segment->pids.audio.pid != p_pids->audio.pid) {
2386 //restart audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002387 a_cmd = DVR_PLAYBACK_CMD_A_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002388 }
2389 if (!VALID_PID(segment->pids.audio.pid)
2390 && VALID_PID(p_pids->audio.pid)
2391 && segment->pids.audio.pid != p_pids->audio.pid) {
2392 //start audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002393 a_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002394 }
2395 if (VALID_PID(segment->pids.audio.pid)
2396 && !VALID_PID(p_pids->audio.pid)
2397 && segment->pids.audio.pid != p_pids->audio.pid) {
2398 //stop audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002399 a_cmd = DVR_PLAYBACK_CMD_A_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08002400 }
2401 if (a_cmd == DVR_PLAYBACK_CMD_NONE
2402 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
2403 //do nothing
2404 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
2405 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
2406 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2407 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
2408 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
2409 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002410 if (v_cmd == DVR_PLAYBACK_CMD_V_RESTART
2411 && (a_cmd == DVR_PLAYBACK_CMD_A_RESTART)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002412 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002413 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
2414 }else if (v_cmd == DVR_PLAYBACK_CMD_V_RESTART
2415 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002416 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002417 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002418 } else {
2419 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002420 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002421 }
2422
Wentao MA270dc0f2022-08-23 13:17:26 +08002423 if (v_cmd == DVR_PLAYBACK_CMD_V_START
2424 && (a_cmd == DVR_PLAYBACK_CMD_A_RESTART)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002425 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002426 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_START_A_RESTART;
2427 } else if (v_cmd == DVR_PLAYBACK_CMD_V_START
2428 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002429 //not occur this case
2430 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2431 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
2432 } else {
2433 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002434 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP_V_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002435 }
2436
Wentao MA270dc0f2022-08-23 13:17:26 +08002437 if (v_cmd == DVR_PLAYBACK_CMD_V_STOP
2438 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002439 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002440 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP_A_START;
2441 } else if (v_cmd == DVR_PLAYBACK_CMD_V_STOP
2442 && a_cmd == DVR_PLAYBACK_CMD_A_RESTART) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002443 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002444 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP_A_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002445 } else {
2446 //not occur this case
2447 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2448 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2449 }
2450 }
2451 }
hualing chene10666f2020-04-14 13:58:37 +08002452 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08002453 }
hualing chen86e7d482020-01-16 15:13:33 +08002454 //save pids info
Wentao MA96f68962022-06-15 19:45:35 +08002455 DVR_PB_INFO(":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08002456 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +08002457 DVR_PB_INFO(":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002458 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002459 }
hualing chen86e7d482020-01-16 15:13:33 +08002460 }
Wentao MA96f68962022-06-15 19:45:35 +08002461 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002462 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002463 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002464}
2465/**\brief Stop play, will stop video and audio
2466 * \param[in] handle playback handle
2467 * \param[in] clear is clear last frame
2468 * \retval DVR_SUCCESS On success
2469 * \return Error code
2470 */
hualing chen040df222020-01-17 13:35:02 +08002471int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
2472 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MAe8ba5172022-08-09 11:18:17 +08002473 DVR_UNUSED(clear);
hualing chena540a7e2020-03-27 16:44:05 +08002474 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002475 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002476 return DVR_FAILURE;
2477 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002478 if (player->state == DVR_PLAYBACK_STATE_STOP) {
Wentao MA96f68962022-06-15 19:45:35 +08002479 DVR_PB_INFO(":playback is stoped");
hualing chenb96aa2c2020-04-15 14:13:53 +08002480 return DVR_SUCCESS;
2481 }
Ke Gong3c0caba2020-04-21 22:58:18 -07002482 if (player->state == DVR_PLAYBACK_STATE_STOP) {
Wentao MA96f68962022-06-15 19:45:35 +08002483 DVR_PB_INFO(":playback is stoped");
Ke Gong3c0caba2020-04-21 22:58:18 -07002484 return DVR_SUCCESS;
2485 }
hualing chen87072a82020-03-12 16:20:12 +08002486 _stop_playback_thread(handle);
Wentao MA96f68962022-06-15 19:45:35 +08002487 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002488 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002489 DVR_PB_INFO(":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08002490 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002491 if (player->has_video) {
2492 AmTsPlayer_resumeVideoDecoding(player->handle);
2493 }
2494 if (player->has_audio) {
2495 AmTsPlayer_resumeAudioDecoding(player->handle);
2496 }
2497 if (player->has_video) {
2498 player->has_video = DVR_FALSE;
hualing chen10cdb162021-02-05 10:44:41 +08002499 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002500 AmTsPlayer_stopVideoDecoding(player->handle);
2501 }
2502 if (player->has_audio) {
2503 player->has_audio = DVR_FALSE;
2504 AmTsPlayer_stopAudioDecoding(player->handle);
2505 }
hualing chendf118dd2020-05-21 15:49:11 +08002506 if (player->has_ad_audio) {
2507 player->has_ad_audio =DVR_FALSE;
2508 AmTsPlayer_disableADMix(player->handle);
2509 }
hualing chen266b9502020-04-04 17:39:39 +08002510
hualing chen86e7d482020-01-16 15:13:33 +08002511 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002512 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2513 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002514 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen87072a82020-03-12 16:20:12 +08002515 player->cur_segment_id = UINT64_MAX;
2516 player->segment_is_open = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08002517 DVR_PB_DEBUG("unlock");
2518 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002519 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002520 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002521}
2522/**\brief Start play audio
2523 * \param[in] handle playback handle
2524 * \param[in] params audio playback params,contains fmt and pid...
2525 * \retval DVR_SUCCESS On success
2526 * \return Error code
2527 */
hualing chen2aba4022020-03-02 13:49:55 +08002528
Wentao MA270dc0f2022-08-23 13:17:26 +08002529int 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 +08002530 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002531
2532 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002533 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002534 return DVR_FAILURE;
2535 }
hualing chen86e7d482020-01-16 15:13:33 +08002536 _start_playback_thread(handle);
2537 //start audio and video
Wentao MA96f68962022-06-15 19:45:35 +08002538 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002539 dvr_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002540
Wentao MA270dc0f2022-08-23 13:17:26 +08002541 if (VALID_PID(ad_param->pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08002542 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002543 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08002544 AmTsPlayer_setADParams(player->handle, ad_param);
hualing chendf118dd2020-05-21 15:49:11 +08002545 AmTsPlayer_enableADMix(player->handle);
2546 }
hualing chen969fe7b2021-05-26 15:13:17 +08002547 if (VALID_PID(param->pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08002548 DVR_PB_INFO("start audio");
hualing chen969fe7b2021-05-26 15:13:17 +08002549 player->has_audio = DVR_TRUE;
2550 AmTsPlayer_setAudioParams(player->handle, param);
Wentao MA5629ad82022-08-24 10:03:02 +08002551 if (player->audio_presentation_id > -1) {
2552 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2553 }
hualing chen969fe7b2021-05-26 15:13:17 +08002554 AmTsPlayer_startAudioDecoding(player->handle);
2555 }
hualing chendf118dd2020-05-21 15:49:11 +08002556
hualing chen86e7d482020-01-16 15:13:33 +08002557 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002558 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen040df222020-01-17 13:35:02 +08002559 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002560 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002561 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002562 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002563 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002564}
2565/**\brief Stop play audio
2566 * \param[in] handle playback handle
2567 * \retval DVR_SUCCESS On success
2568 * \return Error code
2569 */
hualing chen040df222020-01-17 13:35:02 +08002570int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2571 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002572
2573 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002574 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002575 return DVR_FAILURE;
2576 }
2577
hualing chen2aba4022020-03-02 13:49:55 +08002578 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002579 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002580 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002581 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
Wentao MA9a164002022-08-29 11:20:24 +08002582 //destroy thread
hualing chen86e7d482020-01-16 15:13:33 +08002583 _stop_playback_thread(handle);
2584 } else {
2585 //do nothing.video is playing
2586 }
Wentao MA96f68962022-06-15 19:45:35 +08002587 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002588 dvr_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002589
hualing chenf00cdc82020-06-10 14:23:35 +08002590 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002591 player->has_audio = DVR_FALSE;
2592 AmTsPlayer_stopAudioDecoding(player->handle);
2593 }
hualing chen87072a82020-03-12 16:20:12 +08002594
hualing chendf118dd2020-05-21 15:49:11 +08002595 if (player->has_ad_audio) {
2596 player->has_ad_audio =DVR_FALSE;
2597 AmTsPlayer_disableADMix(player->handle);
2598 }
2599
hualing chen87072a82020-03-12 16:20:12 +08002600 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002601 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002602
Wentao MA96f68962022-06-15 19:45:35 +08002603 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002604 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002605 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002606}
2607/**\brief Start play video
2608 * \param[in] handle playback handle
2609 * \param[in] params video playback params,contains fmt and pid...
2610 * \retval DVR_SUCCESS On success
2611 * \return Error code
2612 */
hualing chen2aba4022020-03-02 13:49:55 +08002613int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002614 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002615
2616 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002617 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002618 return DVR_FAILURE;
2619 }
2620
hualing chen86e7d482020-01-16 15:13:33 +08002621 _start_playback_thread(handle);
2622 //start audio and video
Wentao MA96f68962022-06-15 19:45:35 +08002623 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002624 dvr_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002625 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002626 AmTsPlayer_setVideoParams(player->handle, param);
hualing chen21a40372021-10-29 11:07:26 +08002627 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chena540a7e2020-03-27 16:44:05 +08002628 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002629
2630 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002631 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002632 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08002633 DVR_PB_INFO("settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002634 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2635 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002636 }
2637 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002638 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_START;
hualing chen040df222020-01-17 13:35:02 +08002639 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002640 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002641 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002642 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002643 return DVR_SUCCESS;
2644}
2645/**\brief Stop play video
2646 * \param[in] handle playback handle
2647 * \retval DVR_SUCCESS On success
2648 * \return Error code
2649 */
hualing chen040df222020-01-17 13:35:02 +08002650int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2651 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002652
2653 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002654 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002655 return DVR_FAILURE;
2656 }
2657
hualing chen86e7d482020-01-16 15:13:33 +08002658 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002659 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002660 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
Wentao MA9a164002022-08-29 11:20:24 +08002661 //destroy thread
hualing chen86e7d482020-01-16 15:13:33 +08002662 _stop_playback_thread(handle);
2663 } else {
2664 //do nothing.audio is playing
2665 }
hualing chen7a56cba2020-04-14 14:09:27 +08002666
Wentao MA96f68962022-06-15 19:45:35 +08002667 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002668 dvr_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002669
hualing chen87072a82020-03-12 16:20:12 +08002670 player->has_video = DVR_FALSE;
2671
2672 AmTsPlayer_stopVideoDecoding(player->handle);
2673 //playback_device_video_stop(player->handle);
2674
2675 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002676 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002677
Wentao MA96f68962022-06-15 19:45:35 +08002678 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002679 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002680 return DVR_SUCCESS;
2681}
2682/**\brief Pause play
2683 * \param[in] handle playback handle
2684 * \param[in] flush whether its internal buffers should be flushed
2685 * \retval DVR_SUCCESS On success
2686 * \return Error code
2687 */
hualing chen040df222020-01-17 13:35:02 +08002688int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2689 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MAe8ba5172022-08-09 11:18:17 +08002690 DVR_UNUSED(flush);
hualing chena540a7e2020-03-27 16:44:05 +08002691 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002692 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002693 return DVR_FAILURE;
2694 }
hualing chenf00cdc82020-06-10 14:23:35 +08002695 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
Wentao MA96f68962022-06-15 19:45:35 +08002696 DVR_PB_INFO("player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002697 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002698 }
Wentao MA96f68962022-06-15 19:45:35 +08002699 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002700 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002701 DVR_PB_DEBUG("get lock");
hualing chen266b9502020-04-04 17:39:39 +08002702 if (player->has_video)
2703 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002704 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002705 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002706
2707 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002708 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2709 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2710 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00002711 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08002712 } else {
2713 player->cmd.last_cmd = player->cmd.cur_cmd;
2714 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2715 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00002716 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08002717 }
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002718 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002719 DVR_PB_DEBUG("unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002720
hualing chen86e7d482020-01-16 15:13:33 +08002721 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002722}
2723
hualing chen5cbe1a62020-02-10 16:36:36 +08002724//not add lock
2725static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2726{
2727 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2728
hualing chena540a7e2020-03-27 16:44:05 +08002729 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002730 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002731 return DVR_FAILURE;
2732 }
2733
hualing chen5cbe1a62020-02-10 16:36:36 +08002734 //get video params and audio params
Wentao MA96f68962022-06-15 19:45:35 +08002735 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002736 dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08002737 am_tsplayer_video_params video_params;
2738 am_tsplayer_audio_params audio_params;
2739 am_tsplayer_audio_params ad_params;
hualing chencc91e1c2020-02-28 13:26:17 +08002740 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002741
Wentao MA270dc0f2022-08-23 13:17:26 +08002742 memset(&video_params, 0, sizeof(video_params));
2743 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002744
Wentao MA270dc0f2022-08-23 13:17:26 +08002745 _dvr_playback_get_playinfo(handle, segmentid, &video_params, &audio_params, &ad_params);
Wentao MA96f68962022-06-15 19:45:35 +08002746 DVR_PB_INFO("unlock cmd: %d", cmd);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002747 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002748
2749 switch (cmd) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002750 case DVR_PLAYBACK_CMD_AV_RESTART:
hualing chen5cbe1a62020-02-10 16:36:36 +08002751 //av restart
Wentao MA270dc0f2022-08-23 13:17:26 +08002752 DVR_PB_INFO("do_cmd av_restart");
hualing chen87072a82020-03-12 16:20:12 +08002753 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002754 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002755 case DVR_PLAYBACK_CMD_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002756 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002757 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002758 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002759 case DVR_PLAYBACK_CMD_V_START:
2760 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002761 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002762 case DVR_PLAYBACK_CMD_V_STOP:
hualing chen2aba4022020-03-02 13:49:55 +08002763 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002764 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002765 case DVR_PLAYBACK_CMD_A_RESTART:
hualing chen5cbe1a62020-02-10 16:36:36 +08002766 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002767 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002768 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002769 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002770 case DVR_PLAYBACK_CMD_A_START:
2771 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002772 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002773 case DVR_PLAYBACK_CMD_A_STOP:
hualing chen2aba4022020-03-02 13:49:55 +08002774 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002775 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002776 case DVR_PLAYBACK_CMD_A_STOP_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002777 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2778 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002779 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002780 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002781 case DVR_PLAYBACK_CMD_A_STOP_V_START:
hualing chen2aba4022020-03-02 13:49:55 +08002782 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002783 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002784 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002785 case DVR_PLAYBACK_CMD_V_STOP_A_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002786 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2787 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002788 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002789 break;
2790 case DVR_PLAYBACK_CMD_STOP:
2791 break;
2792 case DVR_PLAYBACK_CMD_START:
2793 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002794 case DVR_PLAYBACK_CMD_A_START_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002795 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002796 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
2797 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002798 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002799 case DVR_PLAYBACK_CMD_V_START_A_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002800 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002801 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
2802 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002803 break;
2804 case DVR_PLAYBACK_CMD_FF:
2805 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002806 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002807 break;
2808 default:
2809 break;
2810 }
2811 return DVR_SUCCESS;
2812}
2813
2814/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002815 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002816 * \retval DVR_SUCCESS On success
2817 * \return Error code
2818 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002819int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2820 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08002821 uint32_t pos = 0;
hualing chen03fd4942021-07-15 15:56:41 +08002822 uint64_t segmentid = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002823 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002824 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002825 return DVR_FAILURE;
2826 }
2827
hualing chena991aa82021-08-16 10:21:15 +08002828 if (dvr_playback_check_limit(handle)) {
2829 //get id and pos to check if we can seek to this pos
Wentao MA96f68962022-06-15 19:45:35 +08002830 DVR_PB_INFO("player start calculate time");
hualing chena991aa82021-08-16 10:21:15 +08002831 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2832 if (segmentid != player->cur_segment_id ||
2833 (segmentid == player->cur_segment_id &&
2834 pos > _dvr_get_cur_time(handle))) {
2835 //first to seek new pos and to resume
Wentao MA96f68962022-06-15 19:45:35 +08002836 DVR_PB_INFO("seek new pos and to resume");
hualing chena991aa82021-08-16 10:21:15 +08002837 dvr_playback_seek(handle, segmentid, pos);
2838 }
hualing chen7ea70a72021-09-09 11:25:13 +08002839 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002840 DVR_PB_INFO("player is not set limit");
hualing chen03fd4942021-07-15 15:56:41 +08002841 }
hualing chena991aa82021-08-16 10:21:15 +08002842
hualing chen5cbe1a62020-02-10 16:36:36 +08002843 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002844 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002845 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002846 player->first_frame = 0;
2847 if (player->has_video)
2848 AmTsPlayer_pauseVideoDecoding(player->handle);
2849 if (player->has_audio)
2850 AmTsPlayer_pauseAudioDecoding(player->handle);
2851
hualing chen266b9502020-04-04 17:39:39 +08002852 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08002853 DVR_PB_INFO("dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002854 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2855 AmTsPlayer_resumeVideoDecoding(player->handle);
2856 }
2857 if (player->has_audio) {
2858 AmTsPlayer_resumeAudioDecoding(player->handle);
2859 }
2860 //check is has audio param,if has audio .we need start audio,
2861 //we will stop audio when ff fb, if reach end, we will pause.so we need
2862 //start audio when resume play
2863
Wentao MA270dc0f2022-08-23 13:17:26 +08002864 am_tsplayer_video_params video_params;
2865 am_tsplayer_audio_params audio_params;
2866 am_tsplayer_audio_params ad_params;
hualing chen266b9502020-04-04 17:39:39 +08002867 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002868
Wentao MA270dc0f2022-08-23 13:17:26 +08002869 memset(&video_params, 0, sizeof(video_params));
2870 memset(&audio_params, 0, sizeof(audio_params));
2871 _dvr_playback_get_playinfo(handle, segmentid, &video_params, &audio_params, &ad_params);
hualing chen266b9502020-04-04 17:39:39 +08002872 //valid audio pid, start audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002873 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 +08002874 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002875 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08002876 dvr_playback_change_seek_state(handle, ad_params.pid);
2877 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chen969fe7b2021-05-26 15:13:17 +08002878 AmTsPlayer_enableADMix(player->handle);
2879 }
2880
Wentao MA270dc0f2022-08-23 13:17:26 +08002881 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 +08002882 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002883 dvr_playback_change_seek_state(handle, audio_params.pid);
2884 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002885 if (player->audio_presentation_id > -1) {
2886 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2887 }
hualing chen266b9502020-04-04 17:39:39 +08002888 AmTsPlayer_startAudioDecoding(player->handle);
2889 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002890 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 +08002891 }
hualing chendf118dd2020-05-21 15:49:11 +08002892
hualing chen87072a82020-03-12 16:20:12 +08002893 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2894 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2895 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002896 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen87072a82020-03-12 16:20:12 +08002897 } else {
2898 player->cmd.last_cmd = player->cmd.cur_cmd;
2899 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2900 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002901 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen87072a82020-03-12 16:20:12 +08002902 }
Wentao MA96f68962022-06-15 19:45:35 +08002903 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002904 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002905 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
Wentao MA96f68962022-06-15 19:45:35 +08002906 DVR_PB_INFO("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002907 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002908 player->first_frame = 0;
2909 if (player->has_video)
2910 AmTsPlayer_pauseVideoDecoding(player->handle);
2911 if (player->has_audio)
2912 AmTsPlayer_pauseAudioDecoding(player->handle);
2913
hualing chene41f4372020-06-06 16:29:17 +08002914 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08002915 DVR_PB_INFO("dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002916 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002917 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002918 }
hualing chen041c4092020-04-05 15:11:50 +08002919 if (player->has_audio)
2920 AmTsPlayer_resumeAudioDecoding(player->handle);
Wentao MA96f68962022-06-15 19:45:35 +08002921 DVR_PB_INFO("set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen9811b212020-10-29 11:21:44 +08002922 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2923 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chend1686e52022-01-05 17:10:42 +08002924 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002925 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002926 DVR_PB_INFO("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002927 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002928 } else {
2929 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2930 {
Wentao MA96f68962022-06-15 19:45:35 +08002931 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002932 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002933 player->first_frame = 0;
2934 if (player->has_video)
2935 AmTsPlayer_pauseVideoDecoding(player->handle);
2936 if (player->has_audio)
2937 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen041c4092020-04-05 15:11:50 +08002938 //clear flag
Wentao MA96f68962022-06-15 19:45:35 +08002939 DVR_PB_INFO("clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002940 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2941 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002942 if (player->has_video) {
2943 AmTsPlayer_resumeVideoDecoding(player->handle);
2944 }
2945 if (player->has_audio)
2946 AmTsPlayer_resumeAudioDecoding(player->handle);
Wentao MA96f68962022-06-15 19:45:35 +08002947 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002948 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002949 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002950 }
2951 return DVR_SUCCESS;
2952}
2953
hualing chena540a7e2020-03-27 16:44:05 +08002954static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2955
2956 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2957 DVR_PlaybackSegmentInfo_t *segment = NULL;
2958 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2959 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2960
2961 list_for_each_entry(segment, &player->segment_list, head)
2962 {
2963 if (segment->segment_id == segment_id) {
2964 cur_segment = segment;
2965 }
2966 if (segment->segment_id == set_seg_id) {
2967 set_segment = segment;
2968 }
2969 if (cur_segment != NULL && set_segment != NULL) {
2970 break;
2971 }
2972 }
2973 if (cur_segment == NULL || set_segment == NULL) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002974 DVR_PB_INFO("set segment or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002975 return DVR_TRUE;
2976 }
2977 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2978 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2979 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2980 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
Wentao MA96f68962022-06-15 19:45:35 +08002981 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 +08002982 return DVR_TRUE;
2983 }
Wentao MA96f68962022-06-15 19:45:35 +08002984 DVR_PB_INFO("play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002985 return DVR_FALSE;
2986}
2987
hualing chen03fd4942021-07-15 15:56:41 +08002988/**\brief set limit
2989 * \param[in] handle playback handle
2990 * \param[in] rec start time ms
2991 * \param[in] rec limit time ms
2992 * \retval DVR_SUCCESS On success
2993 * \return Error code
2994 */
hualing chen7ea70a72021-09-09 11:25:13 +08002995int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint32_t time, uint32_t limit)
hualing chen03fd4942021-07-15 15:56:41 +08002996{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2997
2998 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002999 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003000 return DVR_FAILURE;
3001 }
hualing chen7ea70a72021-09-09 11:25:13 +08003002 _dvr_getClock_sec();
Wentao MA96f68962022-06-15 19:45:35 +08003003 DVR_PB_INFO("lock time %lu limit: %u player->state:%d", time, limit, player->state);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003004 dvr_mutex_lock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003005 player->rec_start = time;
3006 player->limit = limit;
Wentao MA96f68962022-06-15 19:45:35 +08003007 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003008 dvr_mutex_unlock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003009 return DVR_SUCCESS;
3010}
3011
hualing chen5cbe1a62020-02-10 16:36:36 +08003012/**\brief seek
3013 * \param[in] handle playback handle
3014 * \param[in] time_offset time offset base cur segment
3015 * \retval DVR_SUCCESS On success
3016 * \return Error code
3017 */
hualing chencc91e1c2020-02-28 13:26:17 +08003018int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08003019 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08003020 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08003021 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003022 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003023 return DVR_FAILURE;
3024 }
3025
Wentao MA96f68962022-06-15 19:45:35 +08003026 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 +08003027 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003028
hualing chena540a7e2020-03-27 16:44:05 +08003029 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08003030 DVR_PB_INFO("player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08003031
hualing chen5cbe1a62020-02-10 16:36:36 +08003032 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08003033 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08003034 if (ret ==DVR_FAILURE) {
wentao.maa210e5e2022-10-12 16:10:03 +08003035 DVR_PB_ERROR("unlock seek error at open segment");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003036 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003037 return DVR_FAILURE;
3038 }
3039 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
3040 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08003041 DVR_PB_INFO("is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08003042 time_offset = _dvr_get_end_time(handle);
3043 } else {
wentao.maa210e5e2022-10-12 16:10:03 +08003044 DVR_PB_ERROR("is not ongoing segment when seek end, return failure");
3045 return DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08003046 }
3047 }
3048
Wentao MA96f68962022-06-15 19:45:35 +08003049 DVR_PB_INFO("seek open id[%lld]flag[0x%x] time_offset %u",
hualing chen03fd4942021-07-15 15:56:41 +08003050 player->cur_segment.segment_id,
3051 player->cur_segment.flags,
3052 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08003053 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08003054 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3055 //forward playback.not seek end of file
3056 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
3057 //default -2000ms
3058 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
3059 }
hualing chen86e7d482020-01-16 15:13:33 +08003060 }
Wentao MA01de0e62022-01-10 18:48:23 +08003061 // Seek can be regarded as a new playback, so keep the start segment_id for it also
hualing chen8a657f32021-08-30 13:12:49 +08003062 if (player->need_seek_start == DVR_TRUE) {
3063 player->first_start_time = (uint64_t)time_offset + 1;//set first start time not eq 0
Wentao MA01de0e62022-01-10 18:48:23 +08003064 player->first_start_id = player->cur_segment.segment_id;
hualing chen8a657f32021-08-30 13:12:49 +08003065 }
hualing chen2aba4022020-03-02 13:49:55 +08003066 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08003067 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08003068 player->ts_cache_len = 0;
wentao.maa210e5e2022-10-12 16:10:03 +08003069 int offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +08003070 DVR_PB_ERROR("seek get offset by time offset, offset=%d time_offset %u",offset, time_offset);
hualing chen2aba4022020-03-02 13:49:55 +08003071 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08003072 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08003073
hualing chen2aba4022020-03-02 13:49:55 +08003074 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08003075
3076 player->last_send_time_id = UINT64_MAX;
Wentao MA270dc0f2022-08-23 13:17:26 +08003077 player->last_segment_total = 0LL;
hualing chen03fd4942021-07-15 15:56:41 +08003078 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08003079 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08003080 player->fffb_current = _dvr_time_getClock();
3081 player->fffb_start = player->fffb_current;
3082 player->fffb_start_pcr = _dvr_get_cur_time(handle);
3083 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08003084 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08003085 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003086 //only seek file,not start
Wentao MA96f68962022-06-15 19:45:35 +08003087 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003088 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003089 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08003090 }
hualing chen86e7d482020-01-16 15:13:33 +08003091 //stop play
Wentao MA96f68962022-06-15 19:45:35 +08003092 DVR_PB_ERROR("seek stop play, not inject data has video[%d]audio[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003093 player->has_video, player->has_audio);
hualing chen1ffd85b2021-08-16 15:18:43 +08003094
hualing chen266b9502020-04-04 17:39:39 +08003095 if (player->has_video) {
hualing chen7e14e532021-09-23 11:23:28 +08003096 //player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003097 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003098 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08003099 }
3100
hualing chen40dd5462021-11-26 19:56:20 +08003101
hualing chen266b9502020-04-04 17:39:39 +08003102 if (player->has_audio) {
3103 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003104 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08003105 }
hualing chendf118dd2020-05-21 15:49:11 +08003106 if (player->has_ad_audio) {
3107 player->has_ad_audio =DVR_FALSE;
3108 AmTsPlayer_disableADMix(player->handle);
3109 }
3110
hualing chen86e7d482020-01-16 15:13:33 +08003111 //start play
Wentao MA270dc0f2022-08-23 13:17:26 +08003112 am_tsplayer_video_params video_params;
3113 am_tsplayer_audio_params audio_params;
3114 am_tsplayer_audio_params ad_params;
hualing chenb31a6c62020-01-13 17:27:00 +08003115
Wentao MA270dc0f2022-08-23 13:17:26 +08003116 memset(&video_params, 0, sizeof(video_params));
3117 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003118
hualing chen040df222020-01-17 13:35:02 +08003119 player->cur_segment_id = segment_id;
3120
3121 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08003122 //get segment info and audio video pid fmt ;
Wentao MA270dc0f2022-08-23 13:17:26 +08003123 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen86e7d482020-01-16 15:13:33 +08003124 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003125 if (video_params.pid != player->fake_pid && !VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
hualing chena5f03222021-12-02 11:22:35 +08003126 //audio and video pid is all invalid, return error.
Wentao MA270dc0f2022-08-23 13:17:26 +08003127 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 +08003128 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08003129 return -1;
3130 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003131 DVR_PB_ERROR("seek start[0x%x]", video_params.pid);
hualing chen86e7d482020-01-16 15:13:33 +08003132 //add
hualing chen040df222020-01-17 13:35:02 +08003133 if (sync == DVR_PLAYBACK_SYNC) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003134 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003135 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08003136 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08003137 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08003138 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08003139 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003140 //if is pause state. we need set trick mode.
Wentao MA96f68962022-06-15 19:45:35 +08003141 DVR_PB_INFO("seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003142 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08003143 }
Wentao MA96f68962022-06-15 19:45:35 +08003144 DVR_PB_INFO("start video");
Wentao MA270dc0f2022-08-23 13:17:26 +08003145 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003146 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003147 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08003148 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
3149 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
3150 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
3151 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
3152 AmTsPlayer_stopFast(player->handle);
3153 }
hualing chen266b9502020-04-04 17:39:39 +08003154 player->has_video = DVR_TRUE;
hualing chen7e14e532021-09-23 11:23:28 +08003155 } else {
3156 player->has_video = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08003157 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003158 if (VALID_PID(ad_params.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08003159 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003160 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003161 dvr_playback_change_seek_state(handle, ad_params.pid);
3162 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08003163 AmTsPlayer_enableADMix(player->handle);
3164 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003165 if (VALID_PID(audio_params.pid) && player->speed == 1.0) {
Wentao MA96f68962022-06-15 19:45:35 +08003166 DVR_PB_INFO("start audio seek");
Wentao MA270dc0f2022-08-23 13:17:26 +08003167 dvr_playback_change_seek_state(handle, audio_params.pid);
3168 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08003169 if (player->audio_presentation_id > -1) {
3170 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
3171 }
hualing chen969fe7b2021-05-26 15:13:17 +08003172 AmTsPlayer_startAudioDecoding(player->handle);
3173 player->has_audio = DVR_TRUE;
3174 }
hualing chen43a89bc2022-01-19 14:31:20 +08003175#ifdef AVSYNC_USED_PCR
3176 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08003177 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08003178 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
3179 }
3180#endif
hualing chen86e7d482020-01-16 15:13:33 +08003181 }
hualing chen1ffd85b2021-08-16 15:18:43 +08003182 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen2aba4022020-03-02 13:49:55 +08003183 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00003184 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
Wentao MA270dc0f2022-08-23 13:17:26 +08003185 if (VALID_PID(audio_params.pid) || VALID_PID(video_params.pid))
hualing chena5f03222021-12-02 11:22:35 +08003186 player->seek_pause = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08003187 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 +08003188 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
Wentao MA16f870e2022-09-09 11:00:22 +08003189 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chen31140872020-03-25 12:29:26 +08003190 player->speed > 1.0f||
3191 player->speed <= -1.0f) {
Wentao MA96f68962022-06-15 19:45:35 +08003192 DVR_PB_INFO("not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08003193 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08003194 } else {
Wentao MA96f68962022-06-15 19:45:35 +08003195 DVR_PB_INFO("set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08003196 player->cmd.last_cmd = player->cmd.cur_cmd;
3197 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
3198 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00003199 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen2aba4022020-03-02 13:49:55 +08003200 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003201 player->last_send_time_id = UINT64_MAX;
Wentao MA96f68962022-06-15 19:45:35 +08003202 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003203 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003204
3205 return DVR_SUCCESS;
3206}
hualing chen5cbe1a62020-02-10 16:36:36 +08003207
Wentao MAac5ea062022-08-11 11:44:27 +08003208// Get current playback time position of the ongoing segment.
3209// Notice the return value may be negative. This is because previous segment's
3210// data cached in demux buffer need to be considered.
hualing chen5cbe1a62020-02-10 16:36:36 +08003211static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
3212 //get cur time of segment
3213 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003214
Gong Ke2a0ebbe2021-05-25 15:22:50 +08003215 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003216 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003217 return DVR_FAILURE;
3218 }
3219
Wentao MA270dc0f2022-08-23 13:17:26 +08003220 int64_t cache = 0;//default es buf cache 500ms
hualing chen2aba4022020-03-02 13:49:55 +08003221 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003222 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
hualing chena5f03222021-12-02 11:22:35 +08003223 uint64_t cur = 0;
3224 if (player->ts_cache_len > 0 && pos < 0) {
3225 //this case is open new segment end,but cache data is last segment.
3226 //we need used last segment len to send play time.
3227 cur = 0;
3228 } else {
3229 cur = segment_tell_position_time(player->r_handle, pos);
3230 }
hualing chen21a40372021-10-29 11:07:26 +08003231 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08003232 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08003233 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 +08003234 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3235 cache = 0;
3236 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003237 int cur_time = (int)(cur > cache ? cur - cache : 0);
3238 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08003239}
3240
Wentao MAac5ea062022-08-11 11:44:27 +08003241// Get current playback time position of the ongoing segment.
3242// Notice the return value may be negative. This is because previous segment's
3243// data cached in demux buffer need to be considered.
hualing chen969fe7b2021-05-26 15:13:17 +08003244static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
3245 //get cur time of segment
3246 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3247
hualing chen03fd4942021-07-15 15:56:41 +08003248 if (player == NULL || player->handle == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003249 DVR_PB_INFO("player is NULL");
hualing chen969fe7b2021-05-26 15:13:17 +08003250 return DVR_FAILURE;
3251 }
3252
Wentao MA270dc0f2022-08-23 13:17:26 +08003253 int64_t cache = 0;//default es buf cache 500ms
hualing chen969fe7b2021-05-26 15:13:17 +08003254 int cur_time = 0;
hualing chen969fe7b2021-05-26 15:13:17 +08003255 pthread_mutex_lock(&player->segment_lock);
hualing chena5f03222021-12-02 11:22:35 +08003256 loff_t tmp_pos = segment_tell_position(player->r_handle);
3257 loff_t pos = tmp_pos - player->ts_cache_len;
3258 uint64_t cur = 0;
3259 if (player->ts_cache_len > 0 && (tmp_pos < player->ts_cache_len)) {
3260 //this case is open new segment end,but cache data is last segment.
3261 //we need used last segment len to send play time.
3262 cur = 0;
Wentao MA96f68962022-06-15 19:45:35 +08003263 DVR_PB_INFO("change segment [%lld][%lld]",
hualing chen926a8ec2021-12-20 20:38:24 +08003264 player->last_segment_id, player->cur_segment_id);
hualing chena5f03222021-12-02 11:22:35 +08003265 } else {
3266 cur = segment_tell_position_time(player->r_handle, pos);
3267 }
hualing chen21a40372021-10-29 11:07:26 +08003268 AmTsPlayer_getDelayTime(player->handle, &cache);
Wentao MAaf716972021-12-28 13:28:52 +08003269
hualing chen969fe7b2021-05-26 15:13:17 +08003270 pthread_mutex_unlock(&player->segment_lock);
Wentao MA01de0e62022-01-10 18:48:23 +08003271
3272 // The idea here is to work around a weakness of AmTsPlayer_getDelayTime at
3273 // starting phase of a playback in a short period of 20ms or less. During the
3274 // said period, getDelayTime does NOT work as expect to return real cache
3275 // length because demux isn't actually running to provide valid pts to
3276 // TsPlayer. "cache==0" implies the situation that playback is NOT actually
3277 // started. Under such conditions a '0' cache size may NOT reflect actual data
3278 // length remaining in TsPlayer cache, therefore corresponding libdvr 'cur' is
3279 // useless if data in TsPlayer cache is not considered, so it needs to be
Wentao MA270dc0f2022-08-23 13:17:26 +08003280 // reset to a previous valid state. To make the reset operation stricter, extra
Wentao MA01de0e62022-01-10 18:48:23 +08003281 // AmTsPlayer_getPts invocations on both video/audio are introduced to test if
3282 // TsPlayer can get valid pts which indicates the actual running status of
3283 // demux. (JIRA issue: SWPL-68740)
3284 if (player->first_start_id != UINT64_MAX && cache == 0
3285 && player->check_cache_flag == DVR_TRUE ) {
3286 uint64_t pts_a=0;
3287 uint64_t pts_v=0;
3288 AmTsPlayer_getPts(player->handle, TS_STREAM_AUDIO, &pts_a);
3289 AmTsPlayer_getPts(player->handle, TS_STREAM_VIDEO, &pts_v);
3290 if ((int64_t)pts_a <= 0 && (int64_t)pts_v <= 0) {
3291 // Identified the wired situation and just return previous valid state
3292 cur = player->first_start_time;
3293 *id = player->first_start_id;
3294 return cur;
3295 }
3296 }
3297 if (cache != 0) {
3298 // Do NOT permit to enter 'if' code block above any more
3299 player->check_cache_flag=DVR_FALSE;
3300 }
3301
Wentao MA96f68962022-06-15 19:45:35 +08003302 DVR_PB_INFO("***get play cur time [%lld] cache:%lld cur id [%lld]"
3303 " last id [%lld] pb cache len [%d] pos [%lld][%lld]",
hualing chen926a8ec2021-12-20 20:38:24 +08003304 cur,
3305 cache,
3306 player->cur_segment_id,
3307 player->last_send_time_id,
3308 player->ts_cache_len,
3309 pos,
3310 tmp_pos);
hualing chen969fe7b2021-05-26 15:13:17 +08003311 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3312 cache = 0;
3313 }
3314 if (cur > cache) {
3315 cur_time = (int)(cur - cache);
3316 *id = player->cur_segment_id;
Wentao MA270dc0f2022-08-23 13:17:26 +08003317 } else if (player->last_segment_total > 0) {
hualing chend1686e52022-01-05 17:10:42 +08003318 //if at fb mode,we not used last id to replace cur id if cache > cur time.
3319 //this case only used for normal speed or ff speed
3320 if (!IS_FB(player->speed) && player->last_segment_id <= player->cur_segment_id) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003321 if (player->last_segment_total > (cache - cur))
3322 cur_time = (int)(player->last_segment_total - (cache - cur));
hualing chend1686e52022-01-05 17:10:42 +08003323 else
Wentao MA270dc0f2022-08-23 13:17:26 +08003324 cur_time = (int)(player->last_segment_total - cur);
hualing chen8a657f32021-08-30 13:12:49 +08003325
hualing chend1686e52022-01-05 17:10:42 +08003326 *id = player->last_segment_id;
3327 } else {//fb mode
3328 cur_time = (int)(cur);
3329 *id = player->cur_segment_id;
3330 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003331 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 +08003332 } else {
3333 cur_time = 0;
3334 *id = player->cur_segment_id;
3335 }
hualing chen969fe7b2021-05-26 15:13:17 +08003336 return cur_time;
3337}
3338
hualing chencc91e1c2020-02-28 13:26:17 +08003339//get current segment current pcr time of read pos
3340static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
3341 //get cur time of segment
3342 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003343
3344 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003345 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003346 return DVR_FAILURE;
3347 }
3348
hualing chen2aba4022020-03-02 13:49:55 +08003349 pthread_mutex_lock(&player->segment_lock);
3350 uint64_t end = segment_tell_total_time(player->r_handle);
Wentao MA96f68962022-06-15 19:45:35 +08003351 DVR_PB_INFO("get total time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08003352 pthread_mutex_unlock(&player->segment_lock);
3353 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08003354}
3355
hualing chen03fd4942021-07-15 15:56:41 +08003356DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
3357{
3358 //check is set limit info
3359 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3360
3361 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003362 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003363 return DVR_FALSE;
3364 }
3365 if (player->rec_start > 0 || player->limit > 0) {
3366 return DVR_TRUE;
3367 }
3368 return DVR_FALSE;
3369}
3370
3371/**\brief set DVR playback calculate expired time len
3372 * \param[in] handle, DVR playback session handle
3373 * \return DVR_SUCCESS on success
3374 * \return error code on failure
3375 */
hualing chen7ea70a72021-09-09 11:25:13 +08003376uint32_t dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
hualing chen03fd4942021-07-15 15:56:41 +08003377{
3378 //calculate expired time to play
3379 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08003380 uint32_t cur_time;
3381 uint32_t tmp_time;
3382 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003383 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003384 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003385 return expired;
3386 }
hualing chen7ea70a72021-09-09 11:25:13 +08003387 if (player->rec_start == 0 || player->limit == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003388 DVR_PB_INFO("rec limit 0");
hualing chen03fd4942021-07-15 15:56:41 +08003389 return expired;
3390 }
3391 //get system time
hualing chen7ea70a72021-09-09 11:25:13 +08003392 cur_time = _dvr_getClock_sec();
3393 if ((cur_time - player->rec_start) > player->limit) {
3394 tmp_time = (uint32_t)((cur_time - player->rec_start) - player->limit) * 1000U;
3395 expired = *(int*)&tmp_time;
Wentao MA96f68962022-06-15 19:45:35 +08003396 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 +08003397 cur_time,
3398 player->rec_start,
3399 player->limit,
hualing chen7ea70a72021-09-09 11:25:13 +08003400 (uint32_t)(cur_time - player->rec_start - player->limit), expired, tmp_time);
3401 }
hualing chen03fd4942021-07-15 15:56:41 +08003402 return expired;
3403}
3404
3405/**\brief set DVR playback obsolete time
3406 * \param[in] handle, DVR playback session handle
3407 * \param[in] obsolete, obsolete len
3408 * \return DVR_SUCCESS on success
3409 * \return error code on failure
3410 */
3411int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
3412{
3413 int expired = 0;
3414 //calculate expired time to play
3415 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3416
3417 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003418 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003419 return DVR_FALSE;
3420 }
3421 //get system time
Wentao MA96f68962022-06-15 19:45:35 +08003422 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003423 dvr_mutex_lock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003424 player->obsolete = obsolete;
Wentao MA96f68962022-06-15 19:45:35 +08003425 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003426 dvr_mutex_unlock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003427 return expired;
3428}
3429
3430/**\brief update DVR playback newest segment duration
3431 * \param[in] handle, DVR playback session handle
3432 * \param[in] segmentid, newest segment id
3433 * \param[in] dur dur time ms
3434 * \return DVR_SUCCESS on success
3435 * \return error code on failure
3436 */
3437int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
3438uint64_t segmentid, int dur)
3439{
3440 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3441 DVR_PlaybackSegmentInfo_t *segment;
3442 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
3443
3444 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003445 DVR_PB_INFO(" player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003446 return DVR_FAILURE;
3447 }
3448 //update the newest segment duration on timeshift mode
3449 list_for_each_entry(segment, &player->segment_list, head)
3450 {
3451 if (segment->segment_id == segmentid) {
3452 segment->duration = dur;
3453 break;
3454 }
3455 pre_segment = segment;
3456 }
3457
3458 return DVR_SUCCESS;
3459}
3460
hualing chen7ea70a72021-09-09 11:25:13 +08003461static uint32_t dvr_playback_calculate_last_valid_segment(
3462 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos)
hualing chen03fd4942021-07-15 15:56:41 +08003463{
hualing chen7ea70a72021-09-09 11:25:13 +08003464 uint32_t off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003465 uint64_t segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003466 uint32_t pre_off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003467 uint64_t last_segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003468 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003469 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3470
3471 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003472 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003473 return DVR_FAILURE;
3474 }
3475 expired = dvr_playback_calculate_expiredlen(handle);
hualing chen7e14e532021-09-23 11:23:28 +08003476 if (expired == 0) {
3477 *segmentid = player->cur_segment_id;
3478 *pos = 0;
3479 return DVR_SUCCESS;
3480 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003481 DVR_PlaybackSegmentInfo_t *p_seg;
3482 list_for_each_entry_reverse(p_seg, &player->segment_list, head) {
3483 segment_id = p_seg->segment_id;
hualing chen03fd4942021-07-15 15:56:41 +08003484
Wentao MA270dc0f2022-08-23 13:17:26 +08003485 if ((player->obsolete + pre_off + p_seg->duration) > expired)
hualing chen03fd4942021-07-15 15:56:41 +08003486 break;
3487
Wentao MA270dc0f2022-08-23 13:17:26 +08003488 last_segment_id = p_seg->segment_id;
3489 pre_off += p_seg->duration;
hualing chen03fd4942021-07-15 15:56:41 +08003490 }
3491
3492 if (last_segment_id == segment_id) {
3493 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
3494 off = expired;
3495 } else if (player->obsolete >= expired) {
3496 off = 0;
3497 } else {
3498 off = expired - pre_off - player->obsolete;
3499 }
3500 *segmentid = segment_id;
3501 *pos = off;
3502 return DVR_SUCCESS;
3503}
3504
hualing chen4b7c15d2020-04-07 16:13:48 +08003505#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003506//start replay
3507static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3508
3509 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3510 //calculate pcr seek time
3511 int t_diff = 0;
3512 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003513 uint64_t segmentid = 0;
3514 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003515 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003516 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003517 return DVR_FAILURE;
3518 }
3519
hualing chen5cbe1a62020-02-10 16:36:36 +08003520 if (player->fffb_start == -1) {
3521 //set fffb start time ms
3522 player->fffb_start = _dvr_time_getClock();
3523 player->fffb_current = player->fffb_start;
3524 //get segment current time pos
3525 player->fffb_start_pcr = _dvr_get_cur_time(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003526 DVR_PB_INFO("calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
hualing chen03fd4942021-07-15 15:56:41 +08003527 player->fffb_start_pcr, player->speed);
hualing chene41f4372020-06-06 16:29:17 +08003528 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003529 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003530 } else {
3531 player->fffb_current = _dvr_time_getClock();
3532 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003533 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003534 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003535 if (seek_time <= 0) {
3536 //need seek to pre one segment
3537 seek_time = 0;
3538 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003539 //seek segment pos
3540 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003541 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003542 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003543 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3544 //set seek time to 0;
Wentao MA96f68962022-06-15 19:45:35 +08003545 DVR_PB_INFO("segment seek to 0 at fb mode [%d]id[%lld]",
hualing chen03fd4942021-07-15 15:56:41 +08003546 seek_time,
3547 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003548 seek_time = 0;
3549 }
hualing chen03fd4942021-07-15 15:56:41 +08003550 if (IS_FB(player->speed)
3551 && dvr_playback_check_limit(handle)) {
3552 //fb case.check expired time
3553 //get id and pos to check if we can seek to this pos
3554 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3555 //case cur id < segment id
3556 if (player->cur_segment_id < segmentid) {
3557 //expired ts data is player,return error
3558 //
3559 pthread_mutex_unlock(&player->segment_lock);
3560 return 0;
3561 } else if (player->cur_segment_id == segmentid) {
3562 //id is same,compare seek pos
3563 if (seek_time < pos) {
3564 //expired ts data is player,return error
3565 //
3566 pthread_mutex_unlock(&player->segment_lock);
3567 return 0;
3568 }
3569 }
3570 //case can play
3571 }
hualing chen041c4092020-04-05 15:11:50 +08003572 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3573 seek_time = 0;
3574 }
hualing chen2aba4022020-03-02 13:49:55 +08003575 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003576 } else {
3577 //
Wentao MA96f68962022-06-15 19:45:35 +08003578 DVR_PB_INFO("segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003579 }
Wentao MA96f68962022-06-15 19:45:35 +08003580 DVR_PB_INFO("calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003581 seek_time,
3582 player->speed,
3583 player->cur_segment_id,
3584 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003585 }
hualing chen2aba4022020-03-02 13:49:55 +08003586 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003587}
3588
3589
3590//start replay
3591static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3592 //
3593 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003594
3595 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003596 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003597 return DVR_FAILURE;
3598 }
3599
hualing chen5cbe1a62020-02-10 16:36:36 +08003600 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003601 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08003602 DVR_PB_INFO("fffb stop video");
hualing chen21a40372021-10-29 11:07:26 +08003603 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003604 AmTsPlayer_stopVideoDecoding(player->handle);
3605 }
3606 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003607 DVR_PB_INFO("fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003608 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003609 AmTsPlayer_stopAudioDecoding(player->handle);
3610 }
hualing chendf118dd2020-05-21 15:49:11 +08003611 if (player->has_ad_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003612 DVR_PB_INFO("fffb stop audio");
hualing chendf118dd2020-05-21 15:49:11 +08003613 player->has_ad_audio =DVR_FALSE;
3614 AmTsPlayer_disableADMix(player->handle);
3615 }
hualing chen2aba4022020-03-02 13:49:55 +08003616
hualing chen5cbe1a62020-02-10 16:36:36 +08003617 //start video and audio
3618
Wentao MA270dc0f2022-08-23 13:17:26 +08003619 am_tsplayer_video_params video_params;
3620 am_tsplayer_audio_params audio_params;
3621 am_tsplayer_audio_params ad_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003622
Wentao MA270dc0f2022-08-23 13:17:26 +08003623 memset(&video_params, 0, sizeof(video_params));
3624 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003625
hualing chen87072a82020-03-12 16:20:12 +08003626 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003627
3628 //get segment info and audio video pid fmt ;
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003629 //dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08003630 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003631 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003632 if (!VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
3633 //audio and video pids are all invalid, return error.
Wentao MA96f68962022-06-15 19:45:35 +08003634 DVR_PB_ERROR("dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003635 return -1;
3636 }
3637
Wentao MA270dc0f2022-08-23 13:17:26 +08003638 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003639 player->has_video = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003640 DVR_PB_INFO("fffb start video");
3641 //DVR_PB_INFO("fffb start video and save last frame");
hualing chen0888c032020-12-18 17:54:57 +08003642 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003643 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003644 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
Wentao MA270dc0f2022-08-23 13:17:26 +08003645 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003646 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003647 AmTsPlayer_startVideoDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08003648 //playback_device_video_start(player->handle , &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003649 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003650 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003651 }
hualing chen31140872020-03-25 12:29:26 +08003652 //fffb mode need stop fast;
Wentao MA96f68962022-06-15 19:45:35 +08003653 DVR_PB_INFO("stop fast");
hualing chen31140872020-03-25 12:29:26 +08003654 AmTsPlayer_stopFast(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003655 return 0;
3656}
3657
3658static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3659 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003660 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003661 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003662 return DVR_FAILURE;
3663 }
3664
3665 player->first_frame = 0;
Wentao MA96f68962022-06-15 19:45:35 +08003666 DVR_PB_INFO("lock speed [%f]", player->speed);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003667 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003668
hualing chen2aba4022020-03-02 13:49:55 +08003669 int seek_time = _dvr_playback_calculate_seekpos(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003670 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 +08003671
hualing chen87072a82020-03-12 16:20:12 +08003672 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3673 //seek time set 0
3674 seek_time = 0;
3675 }
hualing chen041c4092020-04-05 15:11:50 +08003676 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003677 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3678 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003679 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003680 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003681 DVR_PB_DEBUG("unlock");
hualing chen87072a82020-03-12 16:20:12 +08003682 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003683 //send event here and pause
3684 DVR_Play_Notify_t notify;
3685 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003686 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003687 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003688 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
Wentao MA96f68962022-06-15 19:45:35 +08003689 DVR_PB_INFO("*******************send begin event speed [%f] cur [%d]", player->speed, _dvr_get_cur_time(handle));
hualing chen2aba4022020-03-02 13:49:55 +08003690 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003691 return DVR_SUCCESS;
3692 }
hualing chen2932d372020-04-29 13:44:00 +08003693 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003694 _dvr_init_fffb_time(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003695 DVR_PB_INFO("*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003696 }
3697 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003698 _dvr_playback_fffb_replay(handle);
3699
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003700 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003701 DVR_PB_DEBUG("unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003702
hualing chen5cbe1a62020-02-10 16:36:36 +08003703 return DVR_SUCCESS;
3704}
3705
hualing chen87072a82020-03-12 16:20:12 +08003706//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003707static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003708 //
3709 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003710
3711 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003712 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003713 return DVR_FAILURE;
3714 }
3715
hualing chen5cbe1a62020-02-10 16:36:36 +08003716 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003717 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003718 player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003719 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003720 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003721 }
3722
3723 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003724 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003725 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003726 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003727 //start video and audio
3728
Wentao MA270dc0f2022-08-23 13:17:26 +08003729 am_tsplayer_video_params video_params;
3730 am_tsplayer_audio_params audio_params;
3731 am_tsplayer_audio_params ad_params;
hualing chen87072a82020-03-12 16:20:12 +08003732 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003733
Wentao MA270dc0f2022-08-23 13:17:26 +08003734 memset(&video_params, 0, sizeof(video_params));
3735 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003736
hualing chen5cbe1a62020-02-10 16:36:36 +08003737 //get segment info and audio video pid fmt ;
Wentao MA96f68962022-06-15 19:45:35 +08003738 DVR_PB_INFO("into");
Wentao MA270dc0f2022-08-23 13:17:26 +08003739 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003740 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003741 if (!VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003742 //audio and video pis is all invalid, return error.
Wentao MA96f68962022-06-15 19:45:35 +08003743 DVR_PB_ERROR("dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003744 return -1;
3745 }
3746
Wentao MA270dc0f2022-08-23 13:17:26 +08003747 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003748 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003749 if (trick == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08003750 DVR_PB_INFO("settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003751 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003752 }
hualing chen266b9502020-04-04 17:39:39 +08003753 else {
hualing chen2aba4022020-03-02 13:49:55 +08003754 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003755 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003756 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003757 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003758 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003759 }
hualing chena540a7e2020-03-27 16:44:05 +08003760
3761 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
Wentao MA96f68962022-06-15 19:45:35 +08003762 DVR_PB_INFO("start fast");
hualing chen31140872020-03-25 12:29:26 +08003763 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003764 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003765 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08003766 if (VALID_PID(ad_params.pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08003767 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003768 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003769 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08003770 AmTsPlayer_enableADMix(player->handle);
3771 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003772 if (VALID_PID(audio_params.pid)) {
hualing chen969fe7b2021-05-26 15:13:17 +08003773 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003774 DVR_PB_INFO("start audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003775 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08003776 if (player->audio_presentation_id > -1) {
3777 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
3778 }
hualing chen969fe7b2021-05-26 15:13:17 +08003779 AmTsPlayer_startAudioDecoding(player->handle);
3780 }
hualing chendf118dd2020-05-21 15:49:11 +08003781
Wentao MA96f68962022-06-15 19:45:35 +08003782 DVR_PB_INFO("stop fast");
hualing chen31140872020-03-25 12:29:26 +08003783 AmTsPlayer_stopFast(player->handle);
3784 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3785 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3786 }
hualing chen43a89bc2022-01-19 14:31:20 +08003787#ifdef AVSYNC_USED_PCR
3788 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08003789 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08003790 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
3791 }
3792#endif
hualing chen2aba4022020-03-02 13:49:55 +08003793 player->cmd.last_cmd = player->cmd.cur_cmd;
3794 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003795 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00003796 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen5cbe1a62020-02-10 16:36:36 +08003797 return 0;
3798}
3799
3800
hualing chenb31a6c62020-01-13 17:27:00 +08003801/**\brief Set play speed
3802 * \param[in] handle playback handle
3803 * \param[in] speed playback speed
3804 * \retval DVR_SUCCESS On success
3805 * \return Error code
3806 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003807int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3808
3809 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003810
3811 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003812 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003813 return DVR_FAILURE;
3814 }
3815
hualing chena540a7e2020-03-27 16:44:05 +08003816 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08003817 DVR_PB_INFO(" func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003818 return DVR_FAILURE;
3819 }
hualing chenf00cdc82020-06-10 14:23:35 +08003820 if (speed.speed.speed == player->cmd.speed.speed.speed) {
Wentao MA96f68962022-06-15 19:45:35 +08003821 DVR_PB_INFO(" func: eq speed [%d]", speed.speed.speed);
hualing chenf00cdc82020-06-10 14:23:35 +08003822 return DVR_SUCCESS;
3823 }
Wentao MA96f68962022-06-15 19:45:35 +08003824 DVR_PB_INFO("lock func: speed [%d]", speed.speed.speed);
hualing chen1679f812021-11-08 15:17:46 +08003825
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003826 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003827 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3828 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3829 player->cmd.last_cmd = player->cmd.cur_cmd;
3830 }
hualing chene41f4372020-06-06 16:29:17 +08003831
hualing chen31140872020-03-25 12:29:26 +08003832 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003833 IS_KERNEL_SPEED(speed.speed.speed) ) {
3834 //case 1. not start play.only set speed
3835 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3836 //only set speed.and return;
3837 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3838 player->cmd.speed.speed = speed.speed;
3839 player->speed = (float)speed.speed.speed/(float)100;
3840 player->fffb_play = DVR_FALSE;
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003841 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003842 DVR_PB_DEBUG("unlock");
hualing chenf00cdc82020-06-10 14:23:35 +08003843 return DVR_SUCCESS;
3844 }
3845 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003846 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3847 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003848 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003849 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3850 // resume audio and stop fast play
Wentao MA96f68962022-06-15 19:45:35 +08003851 DVR_PB_INFO("stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003852 AmTsPlayer_stopFast(player->handle);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003853 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003854 DVR_PB_DEBUG("unlock ---\r\n");
Wentao MA270dc0f2022-08-23 13:17:26 +08003855 _dvr_cmd(handle, DVR_PLAYBACK_CMD_A_START);
Wentao MA96f68962022-06-15 19:45:35 +08003856 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003857 dvr_mutex_lock(&player->lock);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003858 } else {
3859 //set play speed and if audio is start, stop audio.
3860 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003861 DVR_PB_INFO("fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003862 AmTsPlayer_stopAudioDecoding(player->handle);
3863 player->has_audio = DVR_FALSE;
3864 }
Wentao MA96f68962022-06-15 19:45:35 +08003865 DVR_PB_INFO("start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003866 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003867 }
hualing chenbcada022020-04-22 14:27:01 +08003868 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003869 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003870 player->cmd.speed.speed = speed.speed;
3871 player->speed = (float)speed.speed.speed/(float)100;
Wentao MA96f68962022-06-15 19:45:35 +08003872 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003873 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003874 return DVR_SUCCESS;
3875 }
hualing chen31140872020-03-25 12:29:26 +08003876 //case 3 fffb mode
3877 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3878 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3879 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003880 DVR_PB_INFO("set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003881 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003882 player->cmd.speed.speed = speed.speed;
3883 player->speed = (float)speed.speed.speed/(float)100;
3884 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003885 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003886 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003887 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003888 return DVR_SUCCESS;
3889 }
3890 }
3891 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003892 IS_KERNEL_SPEED(speed.speed.speed)) {
3893 //case 1. cur speed is kernel support speed,set kernel speed.
3894 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003895 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003896 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3897 // resume audio and stop fast play
Wentao MA96f68962022-06-15 19:45:35 +08003898 DVR_PB_INFO("stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003899 AmTsPlayer_stopFast(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08003900 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003901 } else {
3902 //set play speed and if audio is start, stop audio.
3903 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003904 DVR_PB_INFO("fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003905 AmTsPlayer_stopAudioDecoding(player->handle);
3906 player->has_audio = DVR_FALSE;
3907 }
Wentao MA96f68962022-06-15 19:45:35 +08003908 DVR_PB_INFO("start fast");
hualing chenf00cdc82020-06-10 14:23:35 +08003909 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003910 }
hualing chena540a7e2020-03-27 16:44:05 +08003911 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003912 player->cmd.speed.speed = speed.speed;
3913 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003914 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003915 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003916 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003917 return DVR_SUCCESS;
3918 }
3919 //case 2 fffb mode
3920 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3921 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3922 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003923 DVR_PB_INFO("set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003924 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003925 player->cmd.speed.speed = speed.speed;
3926 player->speed = (float)speed.speed.speed/(float)100;
Wentao MA270dc0f2022-08-23 13:17:26 +08003927 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
hualing chenbcada022020-04-22 14:27:01 +08003928 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003929 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003930 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003931 return DVR_SUCCESS;
3932 }
hualing chen31140872020-03-25 12:29:26 +08003933 }
hualing chena540a7e2020-03-27 16:44:05 +08003934 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3935 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003936 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003937 } else {
hualing chen31140872020-03-25 12:29:26 +08003938 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003939 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3940 else
3941 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003942 player->fffb_play = DVR_TRUE;
3943 }
3944 DVR_Bool_t init_last_time = DVR_FALSE;
3945 if (player->speed > 0.0f && speed.speed.speed < 0) {
3946 init_last_time = DVR_TRUE;
3947 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3948 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003949 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003950 player->cmd.speed.mode = speed.mode;
3951 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003952 player->speed = (float)speed.speed.speed/(float)100;
3953 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003954 _dvr_init_fffb_t(handle);
3955 if (init_last_time == DVR_TRUE)
3956 player->last_send_time_id = UINT64_MAX;
3957
hualing chen87072a82020-03-12 16:20:12 +08003958 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003959 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3960 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003961 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003962 DVR_PB_INFO("set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003963 _dvr_playback_replay(handle, DVR_FALSE);
3964 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3965 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003966 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
Wentao MA96f68962022-06-15 19:45:35 +08003967 DVR_PB_INFO("set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003968 }
Wentao MA96f68962022-06-15 19:45:35 +08003969 DVR_PB_INFO("unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003970 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003971 return DVR_SUCCESS;
3972}
hualing chen2932d372020-04-29 13:44:00 +08003973
hualing chenb31a6c62020-01-13 17:27:00 +08003974/**\brief Get playback status
3975 * \param[in] handle playback handle
3976 * \param[out] p_status playback status
3977 * \retval DVR_SUCCESS On success
3978 * \return Error code
3979 */
hualing chen2932d372020-04-29 13:44:00 +08003980static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3981 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003982//
3983 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003984 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003985 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003986 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003987 return DVR_FAILURE;
3988 }
hualing chen1679f812021-11-08 15:17:46 +08003989 if (is_lock ==DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08003990 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003991 dvr_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003992 }
3993
hualing chen5cbe1a62020-02-10 16:36:36 +08003994 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003995 //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 +08003996 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3997 player->state == DVR_PLAYBACK_STATE_START) {
3998 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3999 }
hualing chen041c4092020-04-05 15:11:50 +08004000
hualing chencc91e1c2020-02-28 13:26:17 +08004001 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08004002 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08004003
4004 if (CONTROL_SPEED_ENABLE == 1) {
hualing chen7ea70a72021-09-09 11:25:13 +08004005 if (player->con_spe.ply_sta == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08004006 DVR_PB_INFO("player dur[%u] sta[%u] cur[%d] -----reinit",
hualing chen03fd4942021-07-15 15:56:41 +08004007 player->con_spe.ply_dur,
4008 player->con_spe.ply_sta,
4009 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08004010 player->con_spe.ply_sta = p_status->time_cur;
4011 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
4012 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
Wentao MA96f68962022-06-15 19:45:35 +08004013 DVR_PB_INFO("player dur[%u] sta[%u] cur[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08004014 player->con_spe.ply_dur,
4015 player->con_spe.ply_sta,
4016 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08004017 player->con_spe.ply_sta = p_status->time_cur;
4018 }
4019
4020 if (player->con_spe.sys_sta == 0) {
4021 player->con_spe.sys_sta = _dvr_time_getClock();
4022 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
4023 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
4024 player->con_spe.sys_sta = _dvr_time_getClock();
4025 }
4026 }
4027
hualing chen4b7c15d2020-04-07 16:13:48 +08004028 if (player->last_send_time_id == UINT64_MAX) {
4029 player->last_send_time_id = player->cur_segment_id;
4030 player->last_cur_time = p_status->time_cur;
4031 }
4032 if (player->last_send_time_id == player->cur_segment_id) {
4033 if (player->speed > 0.0f ) {
4034 //ff
4035 if (p_status->time_cur < player->last_cur_time ) {
Wentao MA96f68962022-06-15 19:45:35 +08004036 DVR_PB_INFO("get ff time error last[%d]cur[%d]diff[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08004037 player->last_cur_time,
4038 p_status->time_cur,
4039 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08004040 p_status->time_cur = player->last_cur_time;
4041 } else {
4042 player->last_cur_time = p_status->time_cur;
4043 }
hualing chene41f4372020-06-06 16:29:17 +08004044 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08004045 //fb
4046 if (p_status->time_cur > player->last_cur_time ) {
Wentao MA96f68962022-06-15 19:45:35 +08004047 DVR_PB_INFO("get fb time error last[%d]cur[%d]diff[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08004048 player->last_cur_time,
4049 p_status->time_cur,
4050 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08004051 p_status->time_cur = player->last_cur_time;
4052 } else {
4053 player->last_cur_time = p_status->time_cur;
4054 }
4055 }
hualing chend241c7a2021-06-22 13:34:27 +08004056 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08004057 player->last_cur_time = p_status->time_cur;
4058 }
hualing chen969fe7b2021-05-26 15:13:17 +08004059 player->last_send_time_id = segment_id;
4060 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08004061
hualing chen5cbe1a62020-02-10 16:36:36 +08004062 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08004063 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08004064 p_status->flags = player->cur_segment.flags;
Wentao MA96f68962022-06-15 19:45:35 +08004065 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 +08004066 _dvr_playback_state_toString(player->state),
4067 _dvr_playback_state_toString(p_status->state),
4068 p_status->time_cur, p_status->time_end,
4069 p_status->segment_id,player->play_flag,
4070 player->speed,
4071 is_lock);
hualing chen1679f812021-11-08 15:17:46 +08004072 if (is_lock ==DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08004073 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004074 dvr_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08004075 }
hualing chen2932d372020-04-29 13:44:00 +08004076 return DVR_SUCCESS;
4077}
4078
4079
4080/**\brief Get playback status
4081 * \param[in] handle playback handle
4082 * \param[out] p_status playback status
4083 * \retval DVR_SUCCESS On success
4084 * \return Error code
4085 */
4086int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
4087 DVR_PlaybackStatus_t *p_status) {
4088//
4089 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4090
Zhiqiang Han9adc9722020-11-11 18:38:10 +08004091 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
4092
hualing chen2932d372020-04-29 13:44:00 +08004093 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004094 DVR_PB_INFO("player is NULL");
hualing chen2932d372020-04-29 13:44:00 +08004095 return DVR_FAILURE;
4096 }
Wentao MA96f68962022-06-15 19:45:35 +08004097 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004098 dvr_mutex_lock(&player->lock);
Zhiqiang Han9adc9722020-11-11 18:38:10 +08004099 if (!player->has_video && !player->has_audio)
4100 p_status->time_cur = 0;
Wentao MA96f68962022-06-15 19:45:35 +08004101 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004102 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08004103
hualing chenb31a6c62020-01-13 17:27:00 +08004104 return DVR_SUCCESS;
4105}
4106
hualing chen040df222020-01-17 13:35:02 +08004107void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
4108 if (segment != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004109 DVR_PB_INFO("segment id: %lld", segment->segment_id);
4110 DVR_PB_INFO("segment flag: %d", segment->flags);
4111 DVR_PB_INFO("segment location: [%s]", segment->location);
4112 DVR_PB_INFO("segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
4113 DVR_PB_INFO("segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
4114 DVR_PB_INFO("segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
4115 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 +08004116 }
hualing chenb31a6c62020-01-13 17:27:00 +08004117}
4118
hualing chen5cbe1a62020-02-10 16:36:36 +08004119int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08004120 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08004121
hualing chena540a7e2020-03-27 16:44:05 +08004122 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004123 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08004124 return DVR_FAILURE;
4125 }
4126
hualing chen040df222020-01-17 13:35:02 +08004127 DVR_PlaybackSegmentInfo_t *segment;
4128 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08004129 {
Wentao MA07d3d742022-09-06 09:58:05 +08004130 if (segment->segment_id == segment_id) {
hualing chen040df222020-01-17 13:35:02 +08004131 _dvr_dump_segment(segment);
Wentao MA07d3d742022-09-06 09:58:05 +08004132 break;
hualing chen86e7d482020-01-16 15:13:33 +08004133 }
4134 }
4135 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08004136}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004137
pengfei.liu27cc4ec2020-04-03 16:28:16 +08004138int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004139{
4140 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4141 DVR_RETURN_IF_FALSE(player);
4142 DVR_RETURN_IF_FALSE(func);
4143
Wentao MA96f68962022-06-15 19:45:35 +08004144 DVR_PB_INFO("in ");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004145 dvr_mutex_lock(&player->lock);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004146
4147 player->dec_func = func;
4148 player->dec_userdata = userdata;
4149
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004150 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08004151 DVR_PB_INFO("out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004152 return DVR_SUCCESS;
4153}
4154
4155int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
4156{
4157 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4158 DVR_RETURN_IF_FALSE(player);
4159 DVR_RETURN_IF_FALSE(p_secure_buf);
4160 DVR_RETURN_IF_FALSE(len);
4161
Wentao MA96f68962022-06-15 19:45:35 +08004162 DVR_PB_INFO("in ");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004163 dvr_mutex_lock(&player->lock);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004164
4165 player->is_secure_mode = 1;
4166 player->secure_buffer = p_secure_buf;
4167 player->secure_buffer_size = len;
4168
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004169 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08004170 DVR_PB_INFO("out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004171 return DVR_SUCCESS;
4172}
Wentao MA5629ad82022-08-24 10:03:02 +08004173
4174int dvr_playback_set_ac4_preselection_id(DVR_PlaybackHandle_t handle, int presel_id)
4175{
4176 DVR_Playback_t *player = (DVR_Playback_t *) handle;
wentao.maa210e5e2022-10-12 16:10:03 +08004177 DVR_RETURN_IF_FALSE(player != NULL);
Wentao MA5629ad82022-08-24 10:03:02 +08004178
4179 player->audio_presentation_id = presel_id;
4180 am_tsplayer_result ret = AmTsPlayer_setParams(player->handle,
4181 AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &presel_id);
4182 DVR_RETURN_IF_FALSE(ret == AM_TSPLAYER_OK);
4183
4184 return DVR_SUCCESS;
4185}