blob: 17780aba651cfe38f3d1473551402d6913c19725 [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001#include <stdio.h>
2#include <stdlib.h>
3
hualing chen5cbe1a62020-02-10 16:36:36 +08004#include <string.h>
hualing chenb31a6c62020-01-13 17:27:00 +08005#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <poll.h>
11#include <errno.h>
12#include <signal.h>
13#include <pthread.h>
hualing chenb5cd42e2020-04-15 17:03:34 +080014#include <errno.h>
hualing chen03fd4942021-07-15 15:56:41 +080015#include "dvr_utils.h"
16#include "dvr_types.h"
hualing chenb31a6c62020-01-13 17:27:00 +080017#include "dvr_playback.h"
Yahui Han1fbf3292021-11-08 18:17:19 +080018#include "am_crypt.h"
hualing chenb31a6c62020-01-13 17:27:00 +080019
Wentao MA96f68962022-06-15 19:45:35 +080020#define PB_LOG_TAG "libdvr-playback"
21#define DVR_PB_DEBUG(...) DVR_LOG_PRINT(LOG_LV_DEBUG, PB_LOG_TAG, __VA_ARGS__)
22#define DVR_PB_INFO(...) DVR_LOG_PRINT(LOG_LV_INFO, PB_LOG_TAG, __VA_ARGS__)
23#define DVR_PB_WARN(...) DVR_LOG_PRINT(LOG_LV_WARN, PB_LOG_TAG, __VA_ARGS__)
24#define DVR_PB_ERROR(...) DVR_LOG_PRINT(LOG_LV_ERROR, PB_LOG_TAG, __VA_ARGS__)
25#define DVR_PB_FATAL(...) DVR_LOG_PRINT(LOG_LV_FATAL, PB_LOG_TAG, __VA_ARGS__)
hualing chena540a7e2020-03-27 16:44:05 +080026
hualing chenb31a6c62020-01-13 17:27:00 +080027#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080028
hualing chend241c7a2021-06-22 13:34:27 +080029#define CONTROL_SPEED_ENABLE 0
hualing chena540a7e2020-03-27 16:44:05 +080030
31#define FF_SPEED (2.0f)
32#define FB_SPEED (-1.0f)
33#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080034#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080035
36#define IS_KERNEL_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_X1) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
37#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
38
Wentao MA907b6432022-08-01 06:23:08 +000039#define DVR_PLAYER_CHANGE_STATE(player,newstate)\
40 DVR_PB_INFO("%s:%d player %p changes state from %s to %s",__func__,__LINE__,\
41 player,_dvr_playback_state_toString(player->state),_dvr_playback_state_toString(newstate));\
42 player->state=newstate;
43
hualing chenb31a6c62020-01-13 17:27:00 +080044
hualing chenb5cd42e2020-04-15 17:03:34 +080045#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080046#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080047//if tsplayer delay time < 200 and no data can read, we will pause
48#define MIN_TSPLAYER_DELAY_TIME (200)
49
hualing chen041c4092020-04-05 15:11:50 +080050#define MAX_CACHE_TIME (30000)
hualing chen43a89bc2022-01-19 14:31:20 +080051//used pcr to control avsync,default not used
52//#define AVSYNC_USED_PCR 1
hualing chena540a7e2020-03-27 16:44:05 +080053static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080054//
55static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chen99508642021-10-18 15:41:17 +080056static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_PlaybackPids_t now_pids, DVR_PlaybackPids_t pids, int type);
hualing chencc91e1c2020-02-28 13:26:17 +080057static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
58static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080059static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080060static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080061static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
62 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080063static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen7ea70a72021-09-09 11:25:13 +080064static uint32_t dvr_playback_calculate_last_valid_segment(
65 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos);
hualing chen87072a82020-03-12 16:20:12 +080066
hualing chenbcada022020-04-22 14:27:01 +080067
hualing chena5f03222021-12-02 11:22:35 +080068
hualing chenbcada022020-04-22 14:27:01 +080069static char* _cmd_toString(int cmd)
70{
71
72 char *string[DVR_PLAYBACK_CMD_NONE+1]={
73 "start",
74 "stop",
Wentao MA270dc0f2022-08-23 13:17:26 +080075 "v_start",
76 "a_start",
77 "v_stop",
78 "a_stop",
79 "v_restart",
80 "a_restart",
81 "av_restart",
82 "v_stop_a_start",
83 "a_stop_v_start",
84 "v_stop_a_restart",
85 "a_stop_v_restart",
86 "v_start_a_restart",
87 "a_start_v_restart",
hualing chenbcada022020-04-22 14:27:01 +080088 "pause",
89 "resume",
90 "seek",
91 "ff",
92 "fb",
93 "NONE"
94 };
95
96 if (cmd > DVR_PLAYBACK_CMD_NONE) {
Wentao MA270dc0f2022-08-23 13:17:26 +080097 return "unknown";
hualing chenbcada022020-04-22 14:27:01 +080098 } else {
99 return string[cmd];
100 }
101}
102
103
Wentao MA907b6432022-08-01 06:23:08 +0000104static char* _dvr_playback_state_toString(int state)
hualing chen6d24aa92020-03-23 18:43:47 +0800105{
Wentao MA907b6432022-08-01 06:23:08 +0000106 char *strings[5]={
107 "START",
108 "STOP",
109 "PAUSE",
110 "FF",
111 "FB",
hualing chen6d24aa92020-03-23 18:43:47 +0800112 };
113
Wentao MA907b6432022-08-01 06:23:08 +0000114 if (state >= 5 || state < 0) {
115 return "UNKNOWN";
hualing chen6d24aa92020-03-23 18:43:47 +0800116 }
Wentao MA907b6432022-08-01 06:23:08 +0000117 return strings[state];
hualing chen6d24aa92020-03-23 18:43:47 +0800118}
hualing chena540a7e2020-03-27 16:44:05 +0800119
120static DVR_Bool_t _dvr_support_speed(int speed) {
121
122 DVR_Bool_t ret = DVR_FALSE;
123
124 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800125 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800126 case PLAYBACK_SPEED_FBX2:
127 case PLAYBACK_SPEED_FBX4:
128 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800129 case PLAYBACK_SPEED_FBX16:
130 case PLAYBACK_SPEED_FBX12:
131 case PLAYBACK_SPEED_FBX32:
132 case PLAYBACK_SPEED_FBX48:
133 case PLAYBACK_SPEED_FBX64:
134 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800135 case PLAYBACK_SPEED_S2:
136 case PLAYBACK_SPEED_S4:
137 case PLAYBACK_SPEED_S8:
138 case PLAYBACK_SPEED_X1:
139 case PLAYBACK_SPEED_X2:
140 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800141 case PLAYBACK_SPEED_X3:
142 case PLAYBACK_SPEED_X5:
143 case PLAYBACK_SPEED_X6:
144 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800145 case PLAYBACK_SPEED_X8:
146 case PLAYBACK_SPEED_X12:
147 case PLAYBACK_SPEED_X16:
148 case PLAYBACK_SPEED_X32:
149 case PLAYBACK_SPEED_X48:
150 case PLAYBACK_SPEED_X64:
151 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800152 ret = DVR_TRUE;
153 break;
154 default:
Wentao MA96f68962022-06-15 19:45:35 +0800155 DVR_PB_INFO("not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800156 break;
157 }
158 return ret;
159}
Wentao MA907b6432022-08-01 06:23:08 +0000160
hualing chen6e4bfa52020-03-13 14:37:11 +0800161void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
162{
Wentao MA96f68962022-06-15 19:45:35 +0800163 DVR_PB_INFO("in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800164 DVR_Playback_t *player = NULL;
165 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800166 player = (DVR_Playback_t *) user_data;
Wentao MA96f68962022-06-15 19:45:35 +0800167 DVR_PB_INFO("play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800168 }
169 switch (event->type) {
170 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
171 {
Wentao MA96f68962022-06-15 19:45:35 +0800172 DVR_PB_INFO("[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800173 event->event.video_format.frame_width,
174 event->event.video_format.frame_height,
175 event->event.video_format.frame_rate);
176 break;
177 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800178 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
179 {
Wentao MA96f68962022-06-15 19:45:35 +0800180 DVR_PB_INFO("[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800181 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800182 break;
183 }
184 default:
185 break;
186 }
187}
hualing chen2aba4022020-03-02 13:49:55 +0800188void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
189{
hualing chen6e4bfa52020-03-13 14:37:11 +0800190 DVR_Playback_t *player = NULL;
191 if (user_data != NULL) {
192 player = (DVR_Playback_t *) user_data;
Wentao MA96f68962022-06-15 19:45:35 +0800193 DVR_PB_INFO("play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800194 }
hualing chen2aba4022020-03-02 13:49:55 +0800195 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800196 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
197 {
Wentao MA96f68962022-06-15 19:45:35 +0800198 DVR_PB_INFO("[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800199 event->event.video_format.frame_width,
200 event->event.video_format.frame_height,
201 event->event.video_format.frame_rate);
202 break;
203 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800204 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
205 {
Wentao MA96f68962022-06-15 19:45:35 +0800206 DVR_PB_INFO("[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800207 if (player->first_trans_ok == DVR_FALSE) {
208 player->first_trans_ok = DVR_TRUE;
209 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
210 }
hualing chen30423862021-04-16 14:39:12 +0800211 if (player != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800212 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800213 player->seek_pause = DVR_FALSE;
214 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800215 break;
216 }
hualing chen487ae6d2020-07-22 10:34:11 +0800217 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
Wentao MA96f68962022-06-15 19:45:35 +0800218 DVR_PB_INFO("[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
hualing chen487ae6d2020-07-22 10:34:11 +0800219 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
220 player->first_trans_ok = DVR_TRUE;
221 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
222 }
223 if (player != NULL && player->has_video == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +0800224 DVR_PB_INFO("[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
hualing chen487ae6d2020-07-22 10:34:11 +0800225 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800226 player->seek_pause = DVR_FALSE;
hualing chen487ae6d2020-07-22 10:34:11 +0800227 }
228 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800229 default:
Wentao MA270dc0f2022-08-23 13:17:26 +0800230 DVR_PB_INFO("[evt]unknown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800231 break;
232 }
233 if (player&&player->player_callback_func) {
Wentao MA96f68962022-06-15 19:45:35 +0800234 DVR_PB_INFO("player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800235 player->player_callback_func(player->player_callback_userdata, event);
236 } else if (player == NULL){
Wentao MA96f68962022-06-15 19:45:35 +0800237 DVR_PB_INFO("player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800238 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800239 DVR_PB_INFO("player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800240 }
241}
hualing chencc91e1c2020-02-28 13:26:17 +0800242
hualing chen5cbe1a62020-02-10 16:36:36 +0800243//convert video and audio fmt
244static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
245 int format = 0;
246 if (is_audio == DVR_FALSE) {
247 //for video fmt
248 switch (fmt)
249 {
250 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800251 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800252 break;
253 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800254 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800255 break;
256 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800257 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800258 break;
259 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800260 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800261 break;
hualing chena540a7e2020-03-27 16:44:05 +0800262 case DVR_VIDEO_FORMAT_VP9:
263 format = AV_VIDEO_CODEC_VP9;
264 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800265 }
266 } else {
267 //for audio fmt
268 switch (fmt)
269 {
270 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800271 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800272 break;
273 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800274 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800275 break;
276 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800277 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800278 break;
279 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800280 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800281 break;
hualing chena540a7e2020-03-27 16:44:05 +0800282 case DVR_AUDIO_FORMAT_AAC:
283 format = AV_AUDIO_CODEC_AAC;
284 break;
285 case DVR_AUDIO_FORMAT_LATM:
286 format = AV_AUDIO_CODEC_LATM;
287 break;
288 case DVR_AUDIO_FORMAT_PCM:
289 format = AV_AUDIO_CODEC_PCM;
290 break;
hualing chenee0e52b2021-04-09 16:58:44 +0800291 case DVR_AUDIO_FORMAT_AC4:
292 format = AV_AUDIO_CODEC_AC4;
293 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800294 }
295 }
296 return format;
297}
hualing chen040df222020-01-17 13:35:02 +0800298static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800299{
hualing chen040df222020-01-17 13:35:02 +0800300 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800301
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800302 if (player == NULL || player->handle == (am_tsplayer_handle)NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800303 return -1;
304
hualing chena540a7e2020-03-27 16:44:05 +0800305 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800306}
hualing chena540a7e2020-03-27 16:44:05 +0800307
hualing chen7ea70a72021-09-09 11:25:13 +0800308
309//get sys time sec
310static uint32_t _dvr_getClock_sec(void)
hualing chen5cbe1a62020-02-10 16:36:36 +0800311{
312 struct timespec ts;
hualing chen7ea70a72021-09-09 11:25:13 +0800313 uint32_t s;
hualing chen03fd4942021-07-15 15:56:41 +0800314 clock_gettime(CLOCK_REALTIME, &ts);
hualing chen7ea70a72021-09-09 11:25:13 +0800315 s = (uint32_t)(ts.tv_sec);
Wentao MA96f68962022-06-15 19:45:35 +0800316 DVR_PB_INFO("n:%u", s);
hualing chen7ea70a72021-09-09 11:25:13 +0800317 return s;
hualing chen5cbe1a62020-02-10 16:36:36 +0800318}
hualing chen86e7d482020-01-16 15:13:33 +0800319
hualing chen7ea70a72021-09-09 11:25:13 +0800320//get sys time ms
321static uint32_t _dvr_time_getClock(void)
322{
323 struct timespec ts;
324 uint32_t ms;
325 clock_gettime(CLOCK_REALTIME, &ts);
326 ms = (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
327 return ms;
328}
hualing chenb31a6c62020-01-13 17:27:00 +0800329
Wentao MA270dc0f2022-08-23 13:17:26 +0800330//timeout wait signal
hualing chen040df222020-01-17 13:35:02 +0800331static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800332{
hualing chen040df222020-01-17 13:35:02 +0800333 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800334
hualing chena540a7e2020-03-27 16:44:05 +0800335
336 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800337 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800338 return DVR_FAILURE;
339 }
340
hualing chen86e7d482020-01-16 15:13:33 +0800341 struct timespec ts;
342 clock_gettime(CLOCK_MONOTONIC, &ts);
343 //ms为毫秒,换算成秒
344 ts.tv_sec += ms/1000;
345 //在outtime的基础上,增加ms毫秒
346 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
347 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
348 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
349 //us的值有可能超过1秒,
350 ts.tv_sec += us / 1000000;
351 us = us % 1000000;
352 ts.tv_nsec = us * 1000;//换算成纳秒
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800353
354 int val = dvr_mutex_save(&player->lock);
355 pthread_cond_timedwait(&player->cond, &player->lock.lock, &ts);
356 dvr_mutex_restore(&player->lock, val);
hualing chen86e7d482020-01-16 15:13:33 +0800357 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800358}
hualing chen31140872020-03-25 12:29:26 +0800359//get tsplay delay time ms
360static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
361 DVR_Playback_t *player = (DVR_Playback_t *) handle;
362 int64_t cache = 0;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800363 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800364 DVR_PB_INFO("tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800365 return 0;
366 }
367 AmTsPlayer_getDelayTime(player->handle, &cache);
Wentao MA96f68962022-06-15 19:45:35 +0800368 DVR_PB_INFO("tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800369 return cache;
370}
hualing chenb31a6c62020-01-13 17:27:00 +0800371//send signal
hualing chen040df222020-01-17 13:35:02 +0800372static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800373{
hualing chen87072a82020-03-12 16:20:12 +0800374 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800375
376 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800377 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800378 return DVR_FAILURE;
379 }
Wentao MA96f68962022-06-15 19:45:35 +0800380 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800381 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800382 pthread_cond_signal(&player->cond);
Wentao MA96f68962022-06-15 19:45:35 +0800383 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +0800384 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800385 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800386}
387
hualing chen2932d372020-04-29 13:44:00 +0800388//send playback event, need check is need lock first
389static int _dvr_playback_sent_event(DVR_PlaybackHandle_t handle, DVR_PlaybackEvent_t evt, DVR_Play_Notify_t *notify, DVR_Bool_t is_lock) {
hualing chencc91e1c2020-02-28 13:26:17 +0800390
391 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800392
393 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800394 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800395 return DVR_FAILURE;
396 }
397
hualing chencc91e1c2020-02-28 13:26:17 +0800398 switch (evt) {
399 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800400 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800401 break;
402 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
403 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800404 DVR_PB_INFO("trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800405 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800406 break;
407 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
408 break;
409 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
410 break;
411 case DVR_PLAYBACK_EVENT_NO_KEY:
412 break;
413 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800414 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800415 DVR_PB_INFO("reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800416 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800417 break;
418 case DVR_PLAYBACK_EVENT_REACHED_END:
419 //GET STATE
Wentao MA96f68962022-06-15 19:45:35 +0800420 DVR_PB_INFO("reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800421 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800422 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800423 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800424 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800425 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800426 default:
427 break;
428 }
429 if (player->openParams.event_fn != NULL)
430 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800431 return DVR_SUCCESS;
432}
hualing chen2932d372020-04-29 13:44:00 +0800433static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800434{
435 DVR_Play_Notify_t notify;
436 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
437 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
438 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800439 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800440 return DVR_SUCCESS;
441}
442
hualing chen2932d372020-04-29 13:44:00 +0800443static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800444{
445 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800446
hualing chene3797f02021-01-13 14:53:28 +0800447 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chend241c7a2021-06-22 13:34:27 +0800448 if (CONTROL_SPEED_ENABLE == 0)
449 return DVR_SUCCESS;
hualing chen4b7c15d2020-04-07 16:13:48 +0800450 }
hualing chena540a7e2020-03-27 16:44:05 +0800451 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800452 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800453 return DVR_FAILURE;
454 }
455
hualing chen03fd4942021-07-15 15:56:41 +0800456 if (player->send_time == 0) {
hualing chend241c7a2021-06-22 13:34:27 +0800457 if (CONTROL_SPEED_ENABLE == 0)
458 player->send_time = _dvr_time_getClock() + 500;
459 else
460 player->send_time = _dvr_time_getClock() + 20;
hualing chen0888c032020-12-18 17:54:57 +0800461 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen56c0a162022-01-27 17:01:50 +0800462 if ((player->send_time - _dvr_time_getClock()) > 1000) {
463 player->send_time = _dvr_time_getClock() + 500;
Wentao MA96f68962022-06-15 19:45:35 +0800464 DVR_PB_INFO("player send time occur system time changed!!!!!");
hualing chen56c0a162022-01-27 17:01:50 +0800465 } else {
466 return DVR_SUCCESS;
467 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800468 }
hualing chend241c7a2021-06-22 13:34:27 +0800469 if (CONTROL_SPEED_ENABLE == 0)
470 player->send_time = _dvr_time_getClock() + 500;
471 else
472 player->send_time = _dvr_time_getClock() + 20;
473
hualing chen6e4bfa52020-03-13 14:37:11 +0800474 DVR_Play_Notify_t notify;
475 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
476 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
477 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800478 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800479 return DVR_SUCCESS;
480}
481
hualing chencc91e1c2020-02-28 13:26:17 +0800482//check is ongoing segment
483static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
484
485 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800486 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800487
488 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800489 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800490 return DVR_FAILURE;
491 }
hualing chen87072a82020-03-12 16:20:12 +0800492 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800493 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800494 return DVR_FALSE;
495 }
hualing chencc91e1c2020-02-28 13:26:17 +0800496 return DVR_TRUE;
497}
hualing chen4b7c15d2020-04-07 16:13:48 +0800498
499
500static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
501 DVR_Playback_t *player = (DVR_Playback_t *) handle;
502 player->fffb_start = _dvr_time_getClock();
Wentao MA96f68962022-06-15 19:45:35 +0800503 DVR_PB_INFO(" player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800504 player->fffb_current = player->fffb_start;
505 //get segment current time pos
506 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +0800507 player->next_fffb_time = _dvr_time_getClock();
508
509 return DVR_SUCCESS;
510}
511
hualing chen2aba4022020-03-02 13:49:55 +0800512static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
513 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800514 player->fffb_start = _dvr_time_getClock();
Wentao MA96f68962022-06-15 19:45:35 +0800515 DVR_PB_INFO(" player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800516 player->fffb_current = player->fffb_start;
517 //get segment current time pos
518 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +0800519
hualing chen2aba4022020-03-02 13:49:55 +0800520 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800521 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800522 return DVR_SUCCESS;
523}
hualing chencc91e1c2020-02-28 13:26:17 +0800524//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800525static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
526
527 DVR_Playback_t *player = (DVR_Playback_t *) handle;
528 DVR_PlaybackSegmentInfo_t *segment;
529 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
530
hualing chena540a7e2020-03-27 16:44:05 +0800531 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800532 DVR_PB_INFO(" player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800533 return DVR_FAILURE;
534 }
535
hualing chen87072a82020-03-12 16:20:12 +0800536 int found = 0;
537 int found_eq_id = 0;
538 list_for_each_entry(segment, &player->segment_list, head)
539 {
540 if (player->segment_is_open == DVR_FALSE) {
541 //get first segment from list, case segment is not open
542 if (!IS_FB(player->speed))
543 found = 1;
544 } else if (segment->segment_id == segmentid) {
545 //find cur segment, we need get next one
546 found_eq_id = 1;
547 if (!IS_FB(player->speed)) {
548 found = 1;
549 continue;
550 } else {
551 //if is fb mode.we need used pre segment
552 if (pre_segment != NULL) {
553 found = 1;
554 } else {
555 //not find next id.
Wentao MA96f68962022-06-15 19:45:35 +0800556 DVR_PB_INFO("not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800557 return DVR_FAILURE;
558 }
559 }
560 }
561 if (found == 1) {
562 found = 2;
563 break;
564 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800565 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800566 }
567 if (found != 2) {
568 //list is null or reache list end
Wentao MA96f68962022-06-15 19:45:35 +0800569 DVR_PB_INFO("not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800570 return DVR_FAILURE;
571 }
Wentao MA96f68962022-06-15 19:45:35 +0800572 DVR_PB_INFO("found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800573 return DVR_SUCCESS;
574}
575
576//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800577static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800578
hualing chen040df222020-01-17 13:35:02 +0800579 DVR_Playback_t *player = (DVR_Playback_t *) handle;
580 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800581 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen03fd4942021-07-15 15:56:41 +0800582 uint64_t segmentid;
hualing chen7ea70a72021-09-09 11:25:13 +0800583 uint32_t pos;
hualing chena540a7e2020-03-27 16:44:05 +0800584 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800585 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800586 return DVR_FAILURE;
587 }
588
hualing chen03fd4942021-07-15 15:56:41 +0800589 if (IS_FB(player->speed)
590 && dvr_playback_check_limit(handle)) {
591 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
592 //case cur id < segment id
593 if (player->cur_segment_id <= segmentid) {
594 //expired ts data is player,return error
Wentao MA96f68962022-06-15 19:45:35 +0800595 DVR_PB_INFO("reach start segment ,return error");
hualing chen03fd4942021-07-15 15:56:41 +0800596 return DVR_FAILURE;
597 }
Wentao MA96f68962022-06-15 19:45:35 +0800598 DVR_PB_INFO("has segment to fb play [%lld][%u]", segmentid, pos);
hualing chen03fd4942021-07-15 15:56:41 +0800599 }
600
hualing chen86e7d482020-01-16 15:13:33 +0800601 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800602 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800603
hualing chen040df222020-01-17 13:35:02 +0800604 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800605 {
hualing chencc91e1c2020-02-28 13:26:17 +0800606 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800607 //get first segment from list, case segment is not open
608 if (!IS_FB(player->speed))
609 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800610 } else if (segment->segment_id == player->cur_segment_id) {
611 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800612 found_eq_id = 1;
613 if (!IS_FB(player->speed)) {
614 found = 1;
615 continue;
616 } else {
617 //if is fb mode.we need used pre segment
618 if (pre_segment != NULL) {
619 found = 1;
620 } else {
621 //not find next id.
Wentao MA96f68962022-06-15 19:45:35 +0800622 DVR_PB_INFO("not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800623 return DVR_FAILURE;
624 }
625 }
hualing chen86e7d482020-01-16 15:13:33 +0800626 }
627 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800628 if (IS_FB(player->speed)) {
629 //used pre segment
630 segment = pre_segment;
631 }
hualing chencc91e1c2020-02-28 13:26:17 +0800632 //save segment info
633 player->last_segment_id = player->cur_segment_id;
hualing chen969fe7b2021-05-26 15:13:17 +0800634 if (player->r_handle)
Wentao MA270dc0f2022-08-23 13:17:26 +0800635 player->last_segment_total = segment_tell_total_time(player->r_handle);
hualing chen87072a82020-03-12 16:20:12 +0800636 player->last_segment.segment_id = player->cur_segment.segment_id;
637 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800638 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
639 //pids
640 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
641
hualing chen5cbe1a62020-02-10 16:36:36 +0800642 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800643 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800644 player->cur_segment_id = segment->segment_id;
645 player->cur_segment.segment_id = segment->segment_id;
646 player->cur_segment.flags = segment->flags;
Wentao MA96f68962022-06-15 19:45:35 +0800647 DVR_PB_INFO("set cur id cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, segment->flags, segment->segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800648 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800649 //pids
hualing chen040df222020-01-17 13:35:02 +0800650 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800651 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800652 break;
hualing chen86e7d482020-01-16 15:13:33 +0800653 }
hualing chen2aba4022020-03-02 13:49:55 +0800654 pre_segment = segment;
655 }
656 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
657 //used the last one segment to open
658 //get segment info
659 player->segment_is_open = DVR_TRUE;
660 player->cur_segment_id = pre_segment->segment_id;
661 player->cur_segment.segment_id = pre_segment->segment_id;
662 player->cur_segment.flags = pre_segment->flags;
Wentao MA96f68962022-06-15 19:45:35 +0800663 DVR_PB_INFO("set cur id fb last one cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, pre_segment->flags, pre_segment->segment_id);
hualing chen2aba4022020-03-02 13:49:55 +0800664 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
665 //pids
666 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
667 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800668 }
669 if (found != 2) {
670 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800671 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800672 }
673 return DVR_SUCCESS;
674}
hualing chen040df222020-01-17 13:35:02 +0800675//open next segment to play,if reach list end return errro.
676static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800677{
hualing chen040df222020-01-17 13:35:02 +0800678 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800679 Segment_OpenParams_t params;
680 int ret = DVR_SUCCESS;
681
hualing chena540a7e2020-03-27 16:44:05 +0800682 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800683 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800684 return DVR_FAILURE;
685 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800686 pthread_mutex_lock(&player->segment_lock);
hualing chen926a8ec2021-12-20 20:38:24 +0800687retry:
hualing chena540a7e2020-03-27 16:44:05 +0800688 ret = _dvr_get_next_segmentId(handle);
689 if (ret == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800690 DVR_PB_INFO("not found segment info");
hualing chen4b7c15d2020-04-07 16:13:48 +0800691 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800692 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800693 }
694
695 if (player->r_handle != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800696 DVR_PB_INFO("close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800697 segment_close(player->r_handle);
698 player->r_handle = NULL;
699 }
700
701 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
Wentao MA270dc0f2022-08-23 13:17:26 +0800702 //cp current segment path to location
hualing chen5cbe1a62020-02-10 16:36:36 +0800703 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800704 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800705 params.mode = SEGMENT_MODE_READ;
Wentao MA96f68962022-06-15 19:45:35 +0800706 DVR_PB_INFO("open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
hualing chen4b7c15d2020-04-07 16:13:48 +0800707
hualing chen86e7d482020-01-16 15:13:33 +0800708 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800709 if (ret == DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +0800710 DVR_PB_INFO("open segment error");
hualing chen926a8ec2021-12-20 20:38:24 +0800711 goto retry;
hualing chen4b7c15d2020-04-07 16:13:48 +0800712 }
Wentao MA01de0e62022-01-10 18:48:23 +0800713 // Keep the start segment_id when the first segment_open is called during a playback
714 if (player->first_start_id == UINT64_MAX) {
715 player->first_start_id = player->cur_segment.segment_id;
716 }
hualing chen87072a82020-03-12 16:20:12 +0800717 pthread_mutex_unlock(&player->segment_lock);
718 int total = _dvr_get_end_time( handle);
719 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800720 if (IS_FB(player->speed)) {
721 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800722 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800723 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +0800724 DVR_PB_INFO("seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800725 }
hualing chen87072a82020-03-12 16:20:12 +0800726 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800727 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +0800728 DVR_PB_INFO("next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800729 return ret;
730}
731
hualing chen5cbe1a62020-02-10 16:36:36 +0800732//open next segment to play,if reach list end return errro.
733static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
734{
735 DVR_Playback_t *player = (DVR_Playback_t *) handle;
736 Segment_OpenParams_t params;
737 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800738 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800739 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800740 return DVR_FAILURE;
741 }
hualing chencc91e1c2020-02-28 13:26:17 +0800742 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800743 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800744 }
hualing chencc91e1c2020-02-28 13:26:17 +0800745 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800746 if (id < 0) {
Wentao MA96f68962022-06-15 19:45:35 +0800747 DVR_PB_INFO("not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800748 return DVR_FAILURE;
749 }
Wentao MA96f68962022-06-15 19:45:35 +0800750 DVR_PB_INFO("start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800751 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800752
753 DVR_PlaybackSegmentInfo_t *segment;
754
755 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800756
hualing chen5cbe1a62020-02-10 16:36:36 +0800757 list_for_each_entry(segment, &player->segment_list, head)
758 {
Wentao MA96f68962022-06-15 19:45:35 +0800759 DVR_PB_INFO("see 1 location [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800760 if (segment->segment_id == segment_id) {
761 found = 1;
762 }
763 if (found == 1) {
Wentao MA96f68962022-06-15 19:45:35 +0800764 DVR_PB_INFO("found [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800765 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800766 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800767 player->cur_segment_id = segment->segment_id;
768 player->cur_segment.segment_id = segment->segment_id;
769 player->cur_segment.flags = segment->flags;
Wentao MAe88ad702022-09-02 10:35:00 +0800770 const int len = strlen(segment->location);
771 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
772 DVR_PB_ERROR("Invalid segment.location length %d",len);
773 pthread_mutex_unlock(&player->segment_lock);
774 return DVR_FAILURE;
775 }
776 strncpy(player->cur_segment.location, segment->location, len+1);
hualing chen5cbe1a62020-02-10 16:36:36 +0800777 //pids
778 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +0800779 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 +0800780 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800781 }
782 }
hualing chencc91e1c2020-02-28 13:26:17 +0800783 if (found == 0) {
Wentao MA96f68962022-06-15 19:45:35 +0800784 DVR_PB_INFO("not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800785 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800786 return DVR_FAILURE;
787 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800788 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
Wentao MAe88ad702022-09-02 10:35:00 +0800789
790 const int len2 = strlen(player->cur_segment.location);
791 if (len2 >= DVR_MAX_LOCATION_SIZE || len2 <= 0) {
792 DVR_PB_ERROR("Invalid cur_segment.location length %d",len2);
793 pthread_mutex_unlock(&player->segment_lock);
794 return DVR_FAILURE;
795 }
796 strncpy(params.location, player->cur_segment.location, len2+1);
hualing chen5cbe1a62020-02-10 16:36:36 +0800797 params.segment_id = (uint64_t)player->cur_segment.segment_id;
798 params.mode = SEGMENT_MODE_READ;
Wentao MA96f68962022-06-15 19:45:35 +0800799 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 +0800800 if (player->r_handle != NULL) {
801 segment_close(player->r_handle);
802 player->r_handle = NULL;
803 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800804 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800805 if (ret == DVR_FAILURE) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800806 DVR_PB_INFO("segment open error");
hualing chen4b7c15d2020-04-07 16:13:48 +0800807 }
Wentao MA01de0e62022-01-10 18:48:23 +0800808 // Keep the start segment_id when the first segment_open is called during a playback
809 if (player->first_start_id == UINT64_MAX) {
810 player->first_start_id = player->cur_segment.segment_id;
811 }
hualing chen2aba4022020-03-02 13:49:55 +0800812 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800813 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800814
Wentao MA96f68962022-06-15 19:45:35 +0800815 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 +0800816 return ret;
817}
818
819
820//get play info by segment id
821static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
822 uint64_t segment_id,
Wentao MA270dc0f2022-08-23 13:17:26 +0800823 am_tsplayer_video_params *video_param,
824 am_tsplayer_audio_params *audio_param, am_tsplayer_audio_params *ad_param) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800825
826 DVR_Playback_t *player = (DVR_Playback_t *) handle;
827 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800828 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800829 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800830 return DVR_FAILURE;
831 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800832
833 int found = 0;
834
835 list_for_each_entry(segment, &player->segment_list, head)
836 {
hualing chen87072a82020-03-12 16:20:12 +0800837 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800838 //get first segment from list
839 found = 1;
840 }
841 if (segment->segment_id == segment_id) {
842 found = 1;
843 }
844 if (found == 1) {
845 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800846 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800847 player->cur_segment_id = segment->segment_id;
Wentao MA96f68962022-06-15 19:45:35 +0800848 DVR_PB_INFO("get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800849 player->cur_segment.segment_id = segment->segment_id;
850 player->cur_segment.flags = segment->flags;
851 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800852 player->cur_segment.pids.video.pid = segment->pids.video.pid;
853 player->cur_segment.pids.video.format = segment->pids.video.format;
854 player->cur_segment.pids.video.type = segment->pids.video.type;
855 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
856 player->cur_segment.pids.audio.format = segment->pids.audio.format;
857 player->cur_segment.pids.audio.type = segment->pids.audio.type;
858 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
859 player->cur_segment.pids.ad.format = segment->pids.ad.format;
860 player->cur_segment.pids.ad.type = segment->pids.ad.type;
861 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800862 //
Wentao MA270dc0f2022-08-23 13:17:26 +0800863 video_param->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
864 video_param->pid = segment->pids.video.pid;
865 audio_param->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
866 audio_param->pid = segment->pids.audio.pid;
867 ad_param->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
868 ad_param->pid =segment->pids.ad.pid;
869 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 +0800870 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800871 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800872 }
873 }
hualing chencc91e1c2020-02-28 13:26:17 +0800874 if (found != 2) {
875 //list is null or reache list end
Wentao MA96f68962022-06-15 19:45:35 +0800876 DVR_PB_INFO("get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800877 return DVR_FAILURE;
878 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800879
880 return DVR_SUCCESS;
881}
hualing chencc91e1c2020-02-28 13:26:17 +0800882static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
883 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800884 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800885 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800886 return DVR_FAILURE;
887 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800888
hualing chencc91e1c2020-02-28 13:26:17 +0800889 //compare cur segment
890 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
891 {
892 //check video pids, stop or restart
hualing chen99508642021-10-18 15:41:17 +0800893 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800894 //check sub audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800895 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800896 //check audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800897 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 1);
Wentao MA96f68962022-06-15 19:45:35 +0800898 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 +0800899 //check pcr 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, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800901 }
hualing chena540a7e2020-03-27 16:44:05 +0800902 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800903}
hualing chen5cbe1a62020-02-10 16:36:36 +0800904
hualing chend241c7a2021-06-22 13:34:27 +0800905static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
906{
907 DVR_Playback_t *player = (DVR_Playback_t *) handle;
908 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800909 DVR_PB_INFO("player is NULL");
hualing chend241c7a2021-06-22 13:34:27 +0800910 return DVR_TRUE;
911 }
912 char buf[10];
913 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
Wentao MA96f68962022-06-15 19:45:35 +0800914 DVR_PB_INFO("player get prop[%d][%s]", atoi(buf), buf);
hualing chend241c7a2021-06-22 13:34:27 +0800915
916 if (atoi(buf) != 1) {
917 //return DVR_TRUE;
918 }
919
Wentao MA96f68962022-06-15 19:45:35 +0800920 DVR_PB_INFO(":play speed: %f ply dur: %u sys_dur: %u",
hualing chen03fd4942021-07-15 15:56:41 +0800921 player->speed,
922 player->con_spe.ply_dur,
923 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800924
925 if (player->speed != 1.0f)
926 return DVR_TRUE;
927
928 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800929 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800930 return DVR_FALSE;
931
932 return DVR_TRUE;
933}
934
hualing chencc91e1c2020-02-28 13:26:17 +0800935static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
936{
937 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800938 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800939 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800940 return DVR_FAILURE;
941 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800942 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
Wentao MA96f68962022-06-15 19:45:35 +0800943 DVR_PB_INFO("vendor is amlogic. no used segment flag to hide or show av");
hualing chenf43b8ba2020-07-28 13:11:42 +0800944 return DVR_SUCCESS;
945 }
Wentao MA96f68962022-06-15 19:45:35 +0800946 DVR_PB_INFO("flag[0x%x]id[%lld]last[0x%x][%llu]",
hualing chen03fd4942021-07-15 15:56:41 +0800947 player->cur_segment.flags,
948 player->cur_segment.segment_id,
949 player->last_segment.flags,
950 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800951 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
952 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800953 //enable display
Wentao MA96f68962022-06-15 19:45:35 +0800954 DVR_PB_INFO("unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800955 AmTsPlayer_showVideo(player->handle);
956 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800957 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
958 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800959 //disable display
Wentao MA96f68962022-06-15 19:45:35 +0800960 DVR_PB_INFO("mute");
hualing chen2aba4022020-03-02 13:49:55 +0800961 AmTsPlayer_hideVideo(player->handle);
962 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800963 }
964 return DVR_SUCCESS;
965}
hualing chene3797f02021-01-13 14:53:28 +0800966/*
Wentao MA270dc0f2022-08-23 13:17:26 +0800967if decode success first time.
968success: return true
hualing chene3797f02021-01-13 14:53:28 +0800969fail: return false
970*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800971static DVR_Bool_t _dvr_pauselive_decode_success(DVR_PlaybackHandle_t handle) {
hualing chena540a7e2020-03-27 16:44:05 +0800972 DVR_Playback_t *player = (DVR_Playback_t *) handle;
973 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800974 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800975 return DVR_TRUE;
976 }
hualing chene3797f02021-01-13 14:53:28 +0800977 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800978 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800979 } else {
980 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800981 }
982}
hualing chen86e7d482020-01-16 15:13:33 +0800983static void* _dvr_playback_thread(void *arg)
984{
hualing chen040df222020-01-17 13:35:02 +0800985 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800986 //int need_open_segment = 1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800987 am_tsplayer_input_buffer input_buffer;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800988 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800989 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800990
hualing chen39628212020-05-14 10:35:13 +0800991 #define MAX_REACHEND_TIMEOUT (3000)
992 int reach_end_timeout = 0;//ms
993 int cache_time = 0;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800994 int timeout = 200;//ms
hualing chen40dd5462021-11-26 19:56:20 +0800995 int check_no_data_time = 4;
hualing chen2aba4022020-03-02 13:49:55 +0800996 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800997 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800998 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800999 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
hualing chen40dd5462021-11-26 19:56:20 +08001000 int first_write = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001001 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +08001002 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001003 DVR_Bool_t goto_rewrite = DVR_FALSE;
yinming ding0ce94922021-09-08 15:09:15 +08001004 char prop_buf[10];
1005
1006 memset(prop_buf, 0 ,sizeof(prop_buf));
1007 dvr_prop_read("vendor.tv.libdvr.writetm", prop_buf, sizeof(prop_buf));
Wentao MA96f68962022-06-15 19:45:35 +08001008 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 +08001009 if (atoi(prop_buf) > 0)
1010 write_timeout_ms = atoi(prop_buf);
hualing chen03fd4942021-07-15 15:56:41 +08001011
hualing chen56c0a162022-01-27 17:01:50 +08001012
1013 memset(prop_buf, 0 ,sizeof(prop_buf));
1014 dvr_prop_read("vendor.tv.libdvr.waittm", prop_buf, sizeof(prop_buf));
Wentao MA96f68962022-06-15 19:45:35 +08001015 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 +08001016 if (atoi(prop_buf) > 0)
1017 timeout = atoi(prop_buf);
1018
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001019 if (player->is_secure_mode) {
1020 if (dec_buf_size > player->secure_buffer_size) {
Wentao MA96f68962022-06-15 19:45:35 +08001021 DVR_PB_INFO("playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001022 return NULL;
1023 }
1024 }
hualing chen86e7d482020-01-16 15:13:33 +08001025 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001026 if (!buf) {
Wentao MA96f68962022-06-15 19:45:35 +08001027 DVR_PB_INFO("Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001028 return NULL;
1029 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001030 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1031 input_buffer.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001032
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001033 dec_bufs.buf_data = malloc(dec_buf_size);
1034 if (!dec_bufs.buf_data) {
Wentao MA96f68962022-06-15 19:45:35 +08001035 DVR_PB_INFO("Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +08001036 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001037 return NULL;
1038 }
1039 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1040 dec_bufs.buf_size = dec_buf_size;
1041
hualing chencc91e1c2020-02-28 13:26:17 +08001042 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001043 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
1044 }
hualing chen86e7d482020-01-16 15:13:33 +08001045
hualing chen86e7d482020-01-16 15:13:33 +08001046 if (ret != DVR_SUCCESS) {
1047 if (buf != NULL) {
1048 free(buf);
1049 buf = NULL;
1050 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001051 free(dec_bufs.buf_data);
Wentao MA96f68962022-06-15 19:45:35 +08001052 DVR_PB_INFO("get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +08001053 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +08001054 }
Wentao MA96f68962022-06-15 19:45:35 +08001055 DVR_PB_INFO("--player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001056 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +08001057 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
1058 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
1059 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
1060 player->first_trans_ok = DVR_TRUE;
1061 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
1062 }
hualing chencc91e1c2020-02-28 13:26:17 +08001063 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +08001064 //set video show
1065 AmTsPlayer_showVideo(player->handle);
hualing chen40dd5462021-11-26 19:56:20 +08001066 if (player->vendor == DVR_PLAYBACK_VENDOR_AMAZON)
1067 check_no_data_time = 8;
hualing chen86e7d482020-01-16 15:13:33 +08001068 int trick_stat = 0;
1069 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001070
hualing chen86e7d482020-01-16 15:13:33 +08001071 //check trick stat
Wentao MA96f68962022-06-15 19:45:35 +08001072 //DVR_PB_INFO("lock check_no_data_time:%d", check_no_data_time);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001073 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001074
Wentao MA907b6432022-08-01 06:23:08 +00001075 {
Wentao MA9a164002022-08-29 11:20:24 +08001076 static struct timespec _prev_ts={0,0};
Wentao MA907b6432022-08-01 06:23:08 +00001077 struct timespec _nowts,_diffts;
1078 clock_gettime(CLOCK_MONOTONIC, &_nowts);
Wentao MA9a164002022-08-29 11:20:24 +08001079 clock_timespec_subtract(&_nowts,&_prev_ts,&_diffts);
Wentao MA907b6432022-08-01 06:23:08 +00001080 if (_diffts.tv_sec>0) {
1081 char _logbuf[512]={0};
1082 char* _pbuf=_logbuf;
1083 int _nchar=0;
1084 DVR_PlaybackSegmentInfo_t* _segment;
1085 list_for_each_entry(_segment, &player->segment_list, head) {
1086 if (player->cur_segment_id == _segment->segment_id) {
Wentao MA9a164002022-08-29 11:20:24 +08001087 int seg_size = segment_get_cur_segment_size(player->r_handle);
1088 int read_ptr = segment_tell_position(player->r_handle);
Wentao MA907b6432022-08-01 06:23:08 +00001089 float progress = -1.0f;
Wentao MA9a164002022-08-29 11:20:24 +08001090 if (seg_size>0) {
1091 progress = (float)read_ptr*100/seg_size;
Wentao MA907b6432022-08-01 06:23:08 +00001092 }
1093 _nchar=sprintf(_pbuf,"%lld(%.1f%), ",_segment->segment_id,progress);
1094 } else {
1095 _nchar=sprintf(_pbuf,"%lld, ",_segment->segment_id);
1096 }
1097 if (_nchar<0) {
1098 break;
1099 }
1100 _pbuf+=_nchar;
1101 if (_pbuf-_logbuf+10 >= sizeof(_logbuf)) {
1102 sprintf(_pbuf,"...");
1103 break;
1104 }
1105 }
Wentao MA9a164002022-08-29 11:20:24 +08001106 DVR_PB_INFO("clk: %08u, seg_list: %s",_nowts.tv_sec,_logbuf);
1107 _prev_ts=_nowts;
Wentao MA907b6432022-08-01 06:23:08 +00001108 }
1109 }
1110
hualing chen2aba4022020-03-02 13:49:55 +08001111 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1112 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001113 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001114 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001115 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001116 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001117 {
hualing chen2aba4022020-03-02 13:49:55 +08001118 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1119 if (trick_stat > 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001120 DVR_PB_INFO("trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
hualing chen03fd4942021-07-15 15:56:41 +08001121 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001122 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 +08001123 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001124 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001125 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001126 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
Wentao MA270dc0f2022-08-23 13:17:26 +08001127 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_V_START
1128 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_A_START
hualing chen2aba4022020-03-02 13:49:55 +08001129 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
Wentao MA96f68962022-06-15 19:45:35 +08001130 DVR_PB_INFO("pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
hualing chen03fd4942021-07-15 15:56:41 +08001131 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001132 //need change to pause state
1133 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1134 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00001135 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08001136 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001137 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001138 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001139 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001140 AmTsPlayer_pauseVideoDecoding(player->handle);
1141 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001142 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001143 DVR_PB_INFO("clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001144 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001145 }
1146 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1147 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001148 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001149 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001150 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001151 DVR_PB_INFO("fffb pause state----speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001152 player->speed,
1153 player->fffb_current,
1154 _dvr_time_getClock(),
1155 _dvr_playback_state_toString(player->state),
1156 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001157 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001158 //dvr_mutex_unlock(&player->lock);
1159 //dvr_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001160 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001161 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001162 dvr_mutex_unlock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001163 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001164 } else if (_dvr_time_getClock() < player->next_fffb_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001165 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 +08001166 player->speed,
1167 player->fffb_current,
1168 _dvr_time_getClock(),
1169 _dvr_playback_state_toString(player->state),
1170 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001171 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001172 //dvr_mutex_unlock(&player->lock);
1173 //dvr_mutex_lock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001174 AmTsPlayer_pauseVideoDecoding(player->handle);
1175 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001176 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001177 dvr_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001178 continue;
hualing chen2aba4022020-03-02 13:49:55 +08001179 }
Wentao MA96f68962022-06-15 19:45:35 +08001180 DVR_PB_INFO("fffb play-------speed[%f][%d][%d][%s][%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001181 player->speed,
1182 goto_rewrite,
1183 real_read,
1184 _dvr_playback_state_toString(player->state),
1185 player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08001186 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001187 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001188 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1189 player->first_frame = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001190 DVR_PB_INFO("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001191 dvr_mutex_unlock(&player->lock);
1192
hualing chen2aba4022020-03-02 13:49:55 +08001193 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
Wentao MA96f68962022-06-15 19:45:35 +08001194 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001195 dvr_mutex_lock(&player->lock);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001196 player->fffb_play = DVR_FALSE;
hualing chen1ffd85b2021-08-16 15:18:43 +08001197 } else if(player->state == DVR_PLAYBACK_STATE_PAUSE) {
1198 //on pause state,user seek to new pos,we need pause and wait
1199 //user to resume
Wentao MA96f68962022-06-15 19:45:35 +08001200 DVR_PB_INFO("pause, when got first frame event when user seek end");
hualing chen1ffd85b2021-08-16 15:18:43 +08001201 player->first_frame = 0;
1202 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1203 AmTsPlayer_pauseVideoDecoding(player->handle);
1204 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001205 }
hualing chen1ffd85b2021-08-16 15:18:43 +08001206 } else if (player->fffb_play == DVR_TRUE){
hualing chen4b7c15d2020-04-07 16:13:48 +08001207 //for first into fffb when reset speed
1208 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1209 _dvr_time_getClock() < player->next_fffb_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001210 DVR_PB_INFO("fffb timeout-fffb play---speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001211 player->speed,
1212 player->fffb_current,
1213 _dvr_time_getClock(),
1214 _dvr_playback_state_toString(player->state),
1215 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001216 //used timeout wait need lock first,so we unlock and lock
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001217 //dvr_mutex_unlock(&player->lock);
1218 //dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001219 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001220 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001221 dvr_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001222 continue;
1223 }
Wentao MA96f68962022-06-15 19:45:35 +08001224 DVR_PB_INFO("fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001225 player->speed,
1226 goto_rewrite,
1227 real_read,
1228 _dvr_playback_state_toString(player->state),
1229 player->cmd.cur_cmd,
1230 player->fffb_play);
Wentao MA96f68962022-06-15 19:45:35 +08001231 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001232 dvr_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001233 goto_rewrite = DVR_FALSE;
1234 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001235 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001236 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1237 player->first_frame = 0;
1238 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001239 dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001240 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001241 }
hualing chenb31a6c62020-01-13 17:27:00 +08001242 }
hualing chen86e7d482020-01-16 15:13:33 +08001243
hualing chen30423862021-04-16 14:39:12 +08001244 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1245 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001246 //check is need send time send end
Wentao MA96f68962022-06-15 19:45:35 +08001247 DVR_PB_INFO("pause, continue");
1248 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001249 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001250 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001251 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001252 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001253 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001254 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001255 continue;
1256 }
hualing chen266b9502020-04-04 17:39:39 +08001257 //when seek action is done. we need drop write timeout data.
1258 if (player->drop_ts == DVR_TRUE) {
1259 goto_rewrite = DVR_FALSE;
1260 real_read = 0;
1261 player->drop_ts = DVR_FALSE;
1262 }
hualing chen2aba4022020-03-02 13:49:55 +08001263 if (goto_rewrite == DVR_TRUE) {
1264 goto_rewrite = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08001265 //DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001266 dvr_mutex_unlock(&player->lock);
hualing chen3bcf3be2021-12-22 20:15:01 +08001267 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +08001268 //DVR_PB_INFO("rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001269 goto rewrite;
1270 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001271 //.check is need send time send end
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001272 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001273 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001274 dvr_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001275 pthread_mutex_lock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001276 //DVR_PB_INFO("start read");
hualing chen87072a82020-03-12 16:20:12 +08001277 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001278 real_read = real_read + read;
1279 player->ts_cache_len = real_read;
Wentao MA96f68962022-06-15 19:45:35 +08001280 //DVR_PB_INFO("start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001281 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001282 //DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001283 dvr_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001284 if (read < 0 && errno == EIO) {
1285 //EIO ERROR, EXIT THRAD
Wentao MA96f68962022-06-15 19:45:35 +08001286 DVR_PB_INFO("read error.EIO error, exit thread");
hualing chenb5cd42e2020-04-15 17:03:34 +08001287 DVR_Play_Notify_t notify;
1288 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1289 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001290 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001291 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001292 goto end;
1293 } else if (read < 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001294 DVR_PB_INFO("read error.:%d EIO:%d", errno, EIO);
hualing chenb5cd42e2020-04-15 17:03:34 +08001295 }
hualing chen87072a82020-03-12 16:20:12 +08001296 //if on fb mode and read file end , we need calculate pos to retry read.
1297 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001298 DVR_PB_INFO("recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]",
hualing chen03fd4942021-07-15 15:56:41 +08001299 read,
1300 real_read,
1301 buf_len,
1302 player->speed,
1303 player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001304 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
Wentao MA96f68962022-06-15 19:45:35 +08001305 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001306 dvr_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001307 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Wentao MA96f68962022-06-15 19:45:35 +08001308 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001309 dvr_mutex_unlock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001310 continue;
1311 }
Wentao MA96f68962022-06-15 19:45:35 +08001312 //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 +08001313 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001314 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001315 #define MIN_CACHE_TIME (3000)
1316 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001317 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001318 if (_cache_time > MIN_CACHE_TIME) {
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001319 //dvr_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001320 /*if cache time > 20s , we think get time is error,*/
1321 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
Wentao MA907b6432022-08-01 06:23:08 +00001322 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 +08001323 }
hualing chen1679f812021-11-08 15:17:46 +08001324 //_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 +08001325 //dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001326 // 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 +08001327 //continue;
1328 }
hualing chen969fe7b2021-05-26 15:13:17 +08001329
hualing chen040df222020-01-17 13:35:02 +08001330 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001331 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001332 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001333
1334 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chen1679f812021-11-08 15:17:46 +08001335
1336 if (ret != DVR_SUCCESS && delay < MIN_TSPLAYER_DELAY_TIME) {
1337 player->noData++;
Wentao MA96f68962022-06-15 19:45:35 +08001338 DVR_PB_INFO("playback nodata[%d]", player->noData);
hualing chen40dd5462021-11-26 19:56:20 +08001339 if (player->noData == check_no_data_time) {
Wentao MA96f68962022-06-15 19:45:35 +08001340 DVR_PB_INFO("playback send nodata event nodata[%d]", player->noData);
hualing chene3797f02021-01-13 14:53:28 +08001341 //send event here and pause
1342 DVR_Play_Notify_t notify;
1343 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1344 notify.event = DVR_PLAYBACK_EVENT_NODATA;
Wentao MA96f68962022-06-15 19:45:35 +08001345 DVR_PB_INFO("send event DVR_PLAYBACK_EVENT_NODATA--");
hualing chene3797f02021-01-13 14:53:28 +08001346 //get play statue not here
1347 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1348 }
1349 }
1350 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001351 if ((ret != DVR_SUCCESS &&
hualing chen03fd4942021-07-15 15:56:41 +08001352 (player->vendor != DVR_PLAYBACK_VENDOR_AMAZON) &&
hualing chen041c4092020-04-05 15:11:50 +08001353 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001354 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
Wentao MA270dc0f2022-08-23 13:17:26 +08001355 _dvr_pauselive_decode_success((DVR_PlaybackHandle_t)player)) ||
hualing chen39628212020-05-14 10:35:13 +08001356 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001357 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001358 DVR_Play_Notify_t notify;
1359 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1360 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1361 //get play statue not here
1362 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001363 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001364 //continue,timeshift mode, when read end,need wait cur recording segment
Wentao MA96f68962022-06-15 19:45:35 +08001365 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 +08001366 dvr_mutex_lock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08001367 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001368 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08001369 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001370 } else if (ret != DVR_SUCCESS) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001371 DVR_PB_INFO("delay:%d pauselive:%d", delay, _dvr_pauselive_decode_success((DVR_PlaybackHandle_t)player));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001372 dvr_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08001373 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001374 dvr_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08001375 delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chena540a7e2020-03-27 16:44:05 +08001376 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001377 if (delay < cache_time) {
1378 //delay time is changed and then has data to play, so not start timeout
hualing chen1679f812021-11-08 15:17:46 +08001379 reach_end_timeout = 0;
hualing chen39628212020-05-14 10:35:13 +08001380 } else {
1381 reach_end_timeout = reach_end_timeout + timeout;
1382 }
1383 cache_time = delay;
hualing chen31140872020-03-25 12:29:26 +08001384 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001385 }
hualing chen39628212020-05-14 10:35:13 +08001386 reach_end_timeout = 0;
1387 cache_time = 0;
hualing chen2932d372020-04-29 13:44:00 +08001388 //change next segment success case
1389 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001390 dvr_mutex_lock(&player->lock);
hualing chen40dd5462021-11-26 19:56:20 +08001391 player->noData = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001392 DVR_PB_INFO("_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001393 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1394 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen21a40372021-10-29 11:07:26 +08001395 pthread_mutex_lock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001396 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001397 real_read = real_read + read;
1398 player->ts_cache_len = real_read;
1399 pthread_mutex_unlock(&player->segment_lock);
1400
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001401 dvr_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001402 }//read len 0 check end
hualing chen40dd5462021-11-26 19:56:20 +08001403 if (player->noData >= check_no_data_time) {
hualing chene3797f02021-01-13 14:53:28 +08001404 player->noData = 0;
Wentao MA96f68962022-06-15 19:45:35 +08001405 DVR_PB_INFO("playback send data event resume[%d]", player->noData);
hualing chene3797f02021-01-13 14:53:28 +08001406 //send event here and pause
1407 DVR_Play_Notify_t notify;
1408 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1409 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
Wentao MA96f68962022-06-15 19:45:35 +08001410 DVR_PB_INFO("----send event DVR_PLAYBACK_EVENT_DATARESUME");
hualing chene3797f02021-01-13 14:53:28 +08001411 //get play statue not here
1412 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001413 }
hualing chen39628212020-05-14 10:35:13 +08001414 reach_end_timeout = 0;
hualing chen21a40372021-10-29 11:07:26 +08001415 //real_read = real_read + read;
Wentao MA270dc0f2022-08-23 13:17:26 +08001416 input_buffer.buf_size = real_read;
1417 input_buffer.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001418
Wentao MA270dc0f2022-08-23 13:17:26 +08001419 //check read data len,if len < 0, we need continue
1420 if (input_buffer.buf_size <= 0 || input_buffer.buf_data == NULL) {
1421 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 +08001422 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001423 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001424 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001425 }
hualing chen266b9502020-04-04 17:39:39 +08001426 //if need write whole block size, we need check read buf len is eq block size.
1427 if (b_writed_whole_block == DVR_TRUE) {
1428 //buf_len is block size value.
1429 if (real_read < buf_len) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001430 //continue to read data from file
Wentao MA96f68962022-06-15 19:45:35 +08001431 DVR_PB_INFO("read buf len[%d] is < block size [%d]", real_read, buf_len);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001432 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001433 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1434 dvr_mutex_unlock(&player->lock);
1435 DVR_PB_INFO("read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001436 continue;
1437 } else if (real_read > buf_len) {
Wentao MA96f68962022-06-15 19:45:35 +08001438 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 +08001439 }
1440 }
1441
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001442 //if (player->is_secure_mode && player->dec_func) {
1443 if (player->dec_func) {
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001444 DVR_CryptoParams_t crypto_params;
1445
1446 memset(&crypto_params, 0, sizeof(crypto_params));
1447 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1448 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1449 crypto_params.segment_id = player->cur_segment.segment_id;
Wentao MA270dc0f2022-08-23 13:17:26 +08001450 crypto_params.offset = segment_tell_position(player->r_handle) - input_buffer.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001451 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
Wentao MA96f68962022-06-15 19:45:35 +08001452 DVR_PB_INFO("offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001453 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1454 crypto_params.input_buffer.addr = (size_t)buf;
1455 crypto_params.input_buffer.size = real_read;
1456
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001457 if (player->is_secure_mode) {
1458 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1459 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1460 crypto_params.output_buffer.size = dec_buf_size;
1461 ret = player->dec_func(&crypto_params, player->dec_userdata);
Wentao MA270dc0f2022-08-23 13:17:26 +08001462 input_buffer.buf_data = player->secure_buffer;
1463 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001464 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08001465 DVR_PB_INFO("decrypt failed");
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001466 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001467 input_buffer.buf_size = crypto_params.output_size;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001468 } else { // only for NAGRA
1469 crypto_params.output_buffer.type = crypto_params.input_buffer.type;
1470 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1471 crypto_params.output_buffer.size = crypto_params.input_buffer.size;
1472 ret = player->dec_func(&crypto_params, player->dec_userdata);
Wentao MA270dc0f2022-08-23 13:17:26 +08001473 input_buffer.buf_data = (uint8_t*)crypto_params.output_buffer.addr;
1474 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001475 if (ret != DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08001476 DVR_PB_INFO("decrypt failed");
weishi.zhang0a6d5c82021-12-13 14:05:31 +08001477 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001478 input_buffer.buf_size = crypto_params.output_buffer.size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001479 }
Yahui Han1fbf3292021-11-08 18:17:19 +08001480 } else if (player->cryptor) {
Yahui Han63b23b42021-12-07 15:37:46 +08001481 int len = real_read;
Yahui Han1fbf3292021-11-08 18:17:19 +08001482 am_crypt_des_crypt(player->cryptor, dec_bufs.buf_data, buf, &len, 1);
Wentao MA270dc0f2022-08-23 13:17:26 +08001483 input_buffer.buf_data = dec_bufs.buf_data;
1484 input_buffer.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1485 input_buffer.buf_size = len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001486 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001487rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001488 if (player->drop_ts == DVR_TRUE) {
1489 //need drop ts data when seek occur.we need read next loop,drop this ts data
1490 goto_rewrite = DVR_FALSE;
1491 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001492 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001493 player->drop_ts = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08001494 DVR_PB_INFO("----drop ts");
hualing chenbcada022020-04-22 14:27:01 +08001495 continue;
1496 }
hualing chen21a40372021-10-29 11:07:26 +08001497
1498 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001499 player->ts_cache_len = real_read;
hualing chenb9a02922021-12-14 11:29:47 +08001500 //used for printf first write data time.
1501 //to check change channel kpi.
1502 if (first_write == 0) {
1503 first_write++;
Wentao MA270dc0f2022-08-23 13:17:26 +08001504 DVR_PB_INFO("----first write ts data");
hualing chenb9a02922021-12-14 11:29:47 +08001505 }
1506
Wentao MA270dc0f2022-08-23 13:17:26 +08001507 ret = AmTsPlayer_writeData(player->handle, &input_buffer, write_timeout_ms);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001508 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001509 player->ts_cache_len = 0;
hualing chen21a40372021-10-29 11:07:26 +08001510 pthread_mutex_unlock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +08001511 real_read = 0;
1512 write_success++;
hualing chend241c7a2021-06-22 13:34:27 +08001513 if (CONTROL_SPEED_ENABLE == 1) {
1514check0:
1515 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001516 dvr_mutex_lock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001517 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001518 dvr_mutex_unlock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001519 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1520 goto check0;
1521 }
1522 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001523 //DVR_PB_INFO("write write_success:%d input_buffer.buf_size:%d", write_success, input_buffer.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001524 continue;
hualing chen87072a82020-03-12 16:20:12 +08001525 } else {
hualing chen21a40372021-10-29 11:07:26 +08001526 pthread_mutex_unlock(&player->segment_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08001527 DVR_PB_INFO("write time out write_success:%d input_buffer.buf_size:%d systime:%u",
hualing chen03fd4942021-07-15 15:56:41 +08001528 write_success,
Wentao MA270dc0f2022-08-23 13:17:26 +08001529 input_buffer.buf_size,
hualing chen03fd4942021-07-15 15:56:41 +08001530 _dvr_time_getClock());
1531
hualing chena540a7e2020-03-27 16:44:05 +08001532 write_success = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001533 if (CONTROL_SPEED_ENABLE == 1) {
1534check1:
1535 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001536 dvr_mutex_lock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001537 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001538 dvr_mutex_unlock(&player->lock);
hualing chend241c7a2021-06-22 13:34:27 +08001539 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1540 goto check1;
1541 }
1542 }
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001543 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001544 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001545 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001546 if (!player->is_running) {
Wentao MA96f68962022-06-15 19:45:35 +08001547 DVR_PB_INFO("playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001548 break;
1549 }
hualing chen2aba4022020-03-02 13:49:55 +08001550 goto_rewrite = DVR_TRUE;
1551 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001552 }
1553 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001554end:
Wentao MA96f68962022-06-15 19:45:35 +08001555 DVR_PB_INFO("playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001556 free(buf);
1557 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001558 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001559}
1560
1561
hualing chen040df222020-01-17 13:35:02 +08001562static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001563{
hualing chen040df222020-01-17 13:35:02 +08001564 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001565
1566 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001567 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001568 return DVR_FAILURE;
1569 }
Wentao MA96f68962022-06-15 19:45:35 +08001570 DVR_PB_INFO("start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001571 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001572 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001573 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001574 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001575 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001576 if (rc < 0)
1577 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001578 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001579}
1580
1581
hualing chen040df222020-01-17 13:35:02 +08001582static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001583{
hualing chen040df222020-01-17 13:35:02 +08001584 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001585
1586 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001587 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001588 return DVR_FAILURE;
1589 }
1590
Wentao MA96f68962022-06-15 19:45:35 +08001591 DVR_PB_INFO("stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001592 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001593 {
1594 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001595 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001596 pthread_join(player->playback_thread, NULL);
1597 }
1598 if (player->r_handle) {
1599 segment_close(player->r_handle);
1600 player->r_handle = NULL;
1601 }
Wentao MA96f68962022-06-15 19:45:35 +08001602 DVR_PB_INFO(":end");
hualing chen86e7d482020-01-16 15:13:33 +08001603 return 0;
1604}
1605
hualing chen1679f812021-11-08 15:17:46 +08001606static int getFakePid()
1607{
1608 char fake_pid_prop[] = "vendor.tv.dtv.fake_pid";
1609 char buf[32];
1610 int pid = 0xffff;
1611
1612 dvr_prop_read(fake_pid_prop, buf, sizeof(buf));
1613
1614 if (sscanf(buf, "%i", &pid) != 1)
1615 {
Wentao MA96f68962022-06-15 19:45:35 +08001616 DVR_PB_INFO("get fake pid error");
hualing chen1679f812021-11-08 15:17:46 +08001617 pid = 0xffff;
1618 }
1619 return pid;
1620}
1621
1622void dvr_playback_change_seek_state(DVR_PlaybackHandle_t handle,int pid) {
1623
1624 DVR_ASSERT(handle);
1625 DVR_Playback_t *player = (DVR_Playback_t *) handle;
1626 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001627 DVR_PB_INFO("player is NULL");
hualing chen1679f812021-11-08 15:17:46 +08001628 return ;
1629 }
1630 if (player->need_seek_start == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001631 DVR_PB_INFO("player need_seek_start is false");
hualing chen1679f812021-11-08 15:17:46 +08001632 return ;
1633 }
1634
hualing chena5f03222021-12-02 11:22:35 +08001635 if (pid != player->fake_pid) {
hualing chen1679f812021-11-08 15:17:46 +08001636 player->need_seek_start = DVR_FALSE;
1637 }
Wentao MA96f68962022-06-15 19:45:35 +08001638 DVR_PB_INFO("player player->need_seek_start=%d", player->need_seek_start);
hualing chen1679f812021-11-08 15:17:46 +08001639}
1640
hualing chenb31a6c62020-01-13 17:27:00 +08001641/**\brief Open an dvr palyback
1642 * \param[out] p_handle dvr playback addr
1643 * \param[in] params dvr playback open parameters
1644 * \retval DVR_SUCCESS On success
1645 * \return Error code
1646 */
hualing chen040df222020-01-17 13:35:02 +08001647int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001648
hualing chen040df222020-01-17 13:35:02 +08001649 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001650 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001651
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001652 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001653
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001654 dvr_mutex_init(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001655 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001656 pthread_condattr_init(&cattr);
1657 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1658 pthread_cond_init(&player->cond, &cattr);
1659 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001660
hualing chen5cbe1a62020-02-10 16:36:36 +08001661 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001662 INIT_LIST_HEAD(&player->segment_list);
1663 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1664 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001665 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001666 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00001667 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen86e7d482020-01-16 15:13:33 +08001668 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001669 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001670 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001671
hualing chen86e7d482020-01-16 15:13:33 +08001672 //store open params
hualing chen040df222020-01-17 13:35:02 +08001673 player->openParams.dmx_dev_id = params->dmx_dev_id;
1674 player->openParams.block_size = params->block_size;
Wentao MA96f68962022-06-15 19:45:35 +08001675 DVR_PB_INFO("playback open block_size:[%d]",params->block_size);
hualing chen86e7d482020-01-16 15:13:33 +08001676 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001677 player->openParams.event_fn = params->event_fn;
1678 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001679 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001680 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001681
hualing chen5cbe1a62020-02-10 16:36:36 +08001682 player->has_pids = params->has_pids;
1683
hualing chen2aba4022020-03-02 13:49:55 +08001684 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001685
1686 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1687 //for test get callback
1688 if (0 && player->player_callback_func == NULL) {
1689 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1690 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
Wentao MA96f68962022-06-15 19:45:35 +08001691 DVR_PB_INFO("playback open get callback[%p][%p][%p][%p]",
hualing chen03fd4942021-07-15 15:56:41 +08001692 player->player_callback_func,
1693 player->player_callback_userdata,
1694 _dvr_tsplayer_callback_test,
1695 player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001696 }
1697 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001698
hualing chen86e7d482020-01-16 15:13:33 +08001699 //init has audio and video
1700 player->has_video = DVR_FALSE;
1701 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001702 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001703 player->last_segment_id = 0LL;
1704 player->segment_is_open = DVR_FALSE;
Wentao MA5629ad82022-08-24 10:03:02 +08001705 player->audio_presentation_id = -1;
hualing chenb31a6c62020-01-13 17:27:00 +08001706
hualing chen5cbe1a62020-02-10 16:36:36 +08001707 //init ff fb time
hualing chen7ea70a72021-09-09 11:25:13 +08001708 player->fffb_current = 0;
1709 player->fffb_start = 0;
hualing chen03fd4942021-07-15 15:56:41 +08001710 player->fffb_start_pcr = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001711 //seek time
1712 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001713 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001714
Yahui Han1fbf3292021-11-08 18:17:19 +08001715 //allocate cryptor if have clearkey
1716 if (params->keylen > 0) {
1717 player->cryptor = am_crypt_des_open((uint8_t *)params->clearkey,
hualing chen002e5b92022-02-23 17:51:21 +08001718 (uint8_t *)params->cleariv,
1719 params->keylen * 8);
Yahui Han1fbf3292021-11-08 18:17:19 +08001720 if (!player->cryptor) {
Wentao MA96f68962022-06-15 19:45:35 +08001721 DVR_INFO("%s , open des cryptor failed!!!\n", __func__);
Yahui Han1fbf3292021-11-08 18:17:19 +08001722 }
1723 } else {
1724 player->cryptor = NULL;
1725 }
1726
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001727 //init secure stuff
1728 player->dec_func = NULL;
1729 player->dec_userdata = NULL;
1730 player->is_secure_mode = 0;
1731 player->secure_buffer = NULL;
1732 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001733 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001734
hualing chen4b7c15d2020-04-07 16:13:48 +08001735 player->fffb_play = DVR_FALSE;
1736
1737 player->last_send_time_id = UINT64_MAX;
1738 player->last_cur_time = 0;
hualing chen30423862021-04-16 14:39:12 +08001739 player->seek_pause = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001740
hualing chend241c7a2021-06-22 13:34:27 +08001741 //speed con init
1742 if (CONTROL_SPEED_ENABLE == 1) {
1743 player->con_spe.ply_dur = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001744 player->con_spe.ply_sta = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001745 player->con_spe.sys_dur = 0;
1746 player->con_spe.sys_sta = 0;
1747 }
1748
hualing chen03fd4942021-07-15 15:56:41 +08001749 //limit info
1750 player->rec_start = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001751 player->limit = 0;
hualing chen8a657f32021-08-30 13:12:49 +08001752 //need seek to start pos
1753 player->first_start_time = 0;
Wentao MA01de0e62022-01-10 18:48:23 +08001754 player->first_start_id = UINT64_MAX;
1755 player->check_cache_flag = DVR_TRUE;
hualing chen8a657f32021-08-30 13:12:49 +08001756 player->need_seek_start = DVR_TRUE;
hualing chena5f03222021-12-02 11:22:35 +08001757 //fake_pid init
1758 player->fake_pid = getFakePid();
hualing chen86e7d482020-01-16 15:13:33 +08001759 *p_handle = player;
1760 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001761}
1762
1763/**\brief Close an dvr palyback
1764 * \param[in] handle playback handle
1765 * \retval DVR_SUCCESS On success
1766 * \return Error code
1767 */
hualing chen040df222020-01-17 13:35:02 +08001768int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001769
hualing chen86e7d482020-01-16 15:13:33 +08001770 DVR_ASSERT(handle);
Wentao MA96f68962022-06-15 19:45:35 +08001771 DVR_PB_INFO(":into");
hualing chen040df222020-01-17 13:35:02 +08001772 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001773 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001774 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001775 return DVR_FAILURE;
1776 }
1777
hualing chencc91e1c2020-02-28 13:26:17 +08001778 if (player->state != DVR_PLAYBACK_STATE_STOP)
1779 {
Wentao MA96f68962022-06-15 19:45:35 +08001780 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
Yahui Han1fbf3292021-11-08 18:17:19 +08001781 if (player->cryptor) {
1782 am_crypt_des_close(player->cryptor);
1783 player->cryptor = NULL;
1784 }
hualing chencc91e1c2020-02-28 13:26:17 +08001785 dvr_playback_stop(handle, DVR_TRUE);
Wentao MA96f68962022-06-15 19:45:35 +08001786 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
hualing chenb96aa2c2020-04-15 14:13:53 +08001787 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001788 DVR_PB_INFO(":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001789 }
Wentao MA96f68962022-06-15 19:45:35 +08001790 DVR_PB_INFO(":into");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001791 dvr_mutex_destroy(&player->lock);
Zhiqiang Hanc9513462022-06-21 09:55:23 +08001792 pthread_mutex_destroy(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001793 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001794
1795 if (player) {
1796 free(player);
1797 player = NULL;
1798 }
Wentao MA96f68962022-06-15 19:45:35 +08001799 DVR_PB_INFO(":end");
hualing chen86e7d482020-01-16 15:13:33 +08001800 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001801}
1802
Wentao MA270dc0f2022-08-23 13:17:26 +08001803/**\brief Start play audio and video, used start audio api and start video api
hualing chenb31a6c62020-01-13 17:27:00 +08001804 * \param[in] handle playback handle
1805 * \param[in] params audio playback params,contains fmt and pid...
1806 * \retval DVR_SUCCESS On success
1807 * \return Error code
1808 */
hualing chen040df222020-01-17 13:35:02 +08001809int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1810 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA270dc0f2022-08-23 13:17:26 +08001811 am_tsplayer_video_params video_params;
1812 am_tsplayer_audio_params audio_params;
1813 am_tsplayer_audio_params ad_params;
hualing chena540a7e2020-03-27 16:44:05 +08001814
Wentao MA270dc0f2022-08-23 13:17:26 +08001815 memset(&video_params, 0, sizeof(video_params));
1816 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001817
hualing chena540a7e2020-03-27 16:44:05 +08001818 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001819 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001820 return DVR_FAILURE;
1821 }
hualing chencc91e1c2020-02-28 13:26:17 +08001822 uint64_t segment_id = player->cur_segment_id;
Wentao MA96f68962022-06-15 19:45:35 +08001823 DVR_PB_INFO("[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001824
hualing chena540a7e2020-03-27 16:44:05 +08001825 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001826 //can used start api to resume playback
1827 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1828 return dvr_playback_resume(handle);
1829 }
hualing chen87072a82020-03-12 16:20:12 +08001830 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001831 //if flag is paused and not decode first frame. if user resume, we need
hualing chen9b434f02020-06-10 15:06:54 +08001832 //clear flag and set trickmode none
1833 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08001834 DVR_PB_INFO("[%p]clear pause live flag and clear trick mode", handle);
hualing chen9b434f02020-06-10 15:06:54 +08001835 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1836 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1837 }
Wentao MA96f68962022-06-15 19:45:35 +08001838 DVR_PB_INFO("stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001839 return DVR_SUCCESS;
1840 }
hualing chen86e7d482020-01-16 15:13:33 +08001841 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001842 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001843 //get segment info and audio video pid fmt ;
Wentao MA96f68962022-06-15 19:45:35 +08001844 DVR_PB_INFO("lock flag:0x%x", flag);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001845 dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08001846 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen86e7d482020-01-16 15:13:33 +08001847 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08001848 if (video_params.pid != player->fake_pid && !VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
1849 //audio and video pids are all invalid, return error.
1850 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 +08001851 dvr_mutex_unlock(&player->lock);
hualing chencc91e1c2020-02-28 13:26:17 +08001852 DVR_Play_Notify_t notify;
1853 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1854 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1855 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1856 notify.info.transition_failed_data.segment_id = segment_id;
1857 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001858 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001859 return -1;
1860 }
hualing chen31140872020-03-25 12:29:26 +08001861
hualing chencc91e1c2020-02-28 13:26:17 +08001862 {
Wentao MA270dc0f2022-08-23 13:17:26 +08001863 if (VALID_PID(video_params.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001864 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001865 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001866 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08001867 DVR_PB_INFO("set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001868 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1869 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001870 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
Wentao MA96f68962022-06-15 19:45:35 +08001871 DVR_PB_INFO("set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001872 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001873 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001874 DVR_PB_INFO("set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001875 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001876 }
hualing chena93bbbc2020-12-22 17:23:42 +08001877 AmTsPlayer_showVideo(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08001878 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08001879 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08001880 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001881 }
hualing chena540a7e2020-03-27 16:44:05 +08001882
Wentao MA270dc0f2022-08-23 13:17:26 +08001883 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 +08001884 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001885 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1886 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1887 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00001888 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chencc91e1c2020-02-28 13:26:17 +08001889 } else {
1890 player->cmd.last_cmd = player->cmd.cur_cmd;
1891 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001892 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001893 //set fast play
Wentao MA96f68962022-06-15 19:45:35 +08001894 DVR_PB_INFO("start fast");
hualing chen31140872020-03-25 12:29:26 +08001895 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001896 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08001897 if (VALID_PID(ad_params.pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08001898 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08001899 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08001900 dvr_playback_change_seek_state(handle, ad_params.pid);
1901 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08001902 AmTsPlayer_enableADMix(player->handle);
1903 }
Wentao MA270dc0f2022-08-23 13:17:26 +08001904 if (VALID_PID(audio_params.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08001905 DVR_PB_INFO("start audio");
hualing chen969fe7b2021-05-26 15:13:17 +08001906 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08001907 dvr_playback_change_seek_state(handle, audio_params.pid);
1908 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08001909 if (player->audio_presentation_id > -1) {
1910 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
1911 }
hualing chen969fe7b2021-05-26 15:13:17 +08001912 AmTsPlayer_startAudioDecoding(player->handle);
1913 }
hualing chen31140872020-03-25 12:29:26 +08001914 }
hualing chencc91e1c2020-02-28 13:26:17 +08001915 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00001916 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chencc91e1c2020-02-28 13:26:17 +08001917 }
hualing chen86e7d482020-01-16 15:13:33 +08001918 }
hualing chen43a89bc2022-01-19 14:31:20 +08001919#ifdef AVSYNC_USED_PCR
1920 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08001921 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08001922 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
1923 }
1924#endif
Wentao MA96f68962022-06-15 19:45:35 +08001925 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001926 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001927 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001928 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001929}
hualing chen040df222020-01-17 13:35:02 +08001930/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001931 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001932 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001933 * \retval DVR_SUCCESS On success
1934 * \return Error code
1935 */
hualing chen040df222020-01-17 13:35:02 +08001936int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1937 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001938
hualing chena540a7e2020-03-27 16:44:05 +08001939 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001940 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001941 return DVR_FAILURE;
1942 }
1943
Wentao MA96f68962022-06-15 19:45:35 +08001944 DVR_PB_INFO("add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001945 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001946
hualing chen040df222020-01-17 13:35:02 +08001947 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1948 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001949
Wentao MA270dc0f2022-08-23 13:17:26 +08001950 //not memcpy chunk info.
hualing chen040df222020-01-17 13:35:02 +08001951 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001952 //cp location
hualing chen040df222020-01-17 13:35:02 +08001953 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001954
Wentao MA96f68962022-06-15 19:45:35 +08001955 DVR_PB_INFO("add location [%s]id[%lld]flag[%x]", segment->location, segment->segment_id, info->flags);
hualing chen040df222020-01-17 13:35:02 +08001956 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001957
1958 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001959 segment->pids.video.pid = info->pids.video.pid;
1960 segment->pids.video.format = info->pids.video.format;
1961 segment->pids.video.type = info->pids.video.type;
1962
hualing chen2aba4022020-03-02 13:49:55 +08001963 segment->pids.audio.pid = info->pids.audio.pid;
1964 segment->pids.audio.format = info->pids.audio.format;
1965 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001966
hualing chen2aba4022020-03-02 13:49:55 +08001967 segment->pids.ad.pid = info->pids.ad.pid;
1968 segment->pids.ad.format = info->pids.ad.format;
1969 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001970
1971 segment->pids.pcr.pid = info->pids.pcr.pid;
1972
Wentao MA96f68962022-06-15 19:45:35 +08001973 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 +08001974 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001975 list_add_tail(&segment->head, &player->segment_list);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08001976 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08001977 DVR_PB_DEBUG("unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001978
hualing chen5cbe1a62020-02-10 16:36:36 +08001979 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001980}
hualing chen040df222020-01-17 13:35:02 +08001981/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001982 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001983 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001984 * \retval DVR_SUCCESS On success
1985 * \return Error code
1986 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001987int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001988 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA96f68962022-06-15 19:45:35 +08001989 DVR_PB_INFO("remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001990 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001991 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001992 return DVR_FAILURE;
1993 }
1994
hualing chencc91e1c2020-02-28 13:26:17 +08001995 if (segment_id == player->cur_segment_id) {
Wentao MA9a164002022-08-29 11:20:24 +08001996 DVR_PB_INFO("not support remove current segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001997 return DVR_FAILURE;
1998 }
Wentao MA96f68962022-06-15 19:45:35 +08001999 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002000 dvr_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08002001 DVR_PlaybackSegmentInfo_t *segment = NULL;
2002 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
2003 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002004 {
hualing chen040df222020-01-17 13:35:02 +08002005 if (segment->segment_id == segment_id) {
2006 list_del(&segment->head);
2007 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08002008 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002009 }
hualing chen86e7d482020-01-16 15:13:33 +08002010 }
Wentao MA96f68962022-06-15 19:45:35 +08002011 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002012 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002013
2014 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002015}
hualing chen040df222020-01-17 13:35:02 +08002016/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08002017 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08002018 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08002019 * \retval DVR_SUCCESS On success
2020 * \return Error code
2021 */
hualing chen040df222020-01-17 13:35:02 +08002022int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08002023 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08002024 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MA96f68962022-06-15 19:45:35 +08002025 DVR_PB_INFO("update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08002026 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002027 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002028 return DVR_FAILURE;
2029 }
hualing chenf43b8ba2020-07-28 13:11:42 +08002030 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
Wentao MA96f68962022-06-15 19:45:35 +08002031 DVR_PB_INFO("vendor is amlogic. not hide or show av and update segment");
hualing chenf43b8ba2020-07-28 13:11:42 +08002032 return DVR_SUCCESS;
2033 }
hualing chena540a7e2020-03-27 16:44:05 +08002034
hualing chen040df222020-01-17 13:35:02 +08002035 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002036 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002037 dvr_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08002038 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002039 {
hualing chen040df222020-01-17 13:35:02 +08002040 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08002041 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08002042 }
hualing chen86e7d482020-01-16 15:13:33 +08002043 // if encramble to free, only set flag and return;
2044
2045 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08002046 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002047 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
2048 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08002049 //disable display, mute
Wentao MA96f68962022-06-15 19:45:35 +08002050 DVR_PB_INFO("mute av");
hualing chen2aba4022020-03-02 13:49:55 +08002051 AmTsPlayer_hideVideo(player->handle);
2052 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002053 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
2054 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08002055 //enable display, unmute
Wentao MA96f68962022-06-15 19:45:35 +08002056 DVR_PB_INFO("unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08002057 AmTsPlayer_showVideo(player->handle);
2058 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08002059 } else {
2060 //do nothing
2061 }
2062 } else {
2063 //do nothing
2064 }
2065 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08002066 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08002067 }
Wentao MA96f68962022-06-15 19:45:35 +08002068 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002069 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002070 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002071}
2072
2073
hualing chen99508642021-10-18 15:41:17 +08002074static 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 +08002075 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen275379e2021-06-15 17:57:21 +08002076 DVR_StreamInfo_t set_pid;
hualing chen99508642021-10-18 15:41:17 +08002077 DVR_StreamInfo_t now_pid;
hualing chen275379e2021-06-15 17:57:21 +08002078
2079 if (type == 0) {
2080 set_pid = set_pids.video;
hualing chen99508642021-10-18 15:41:17 +08002081 now_pid = now_pids.video;
hualing chen275379e2021-06-15 17:57:21 +08002082 } else if (type == 1) {
2083 set_pid = set_pids.audio;
hualing chen99508642021-10-18 15:41:17 +08002084 now_pid = now_pids.audio;
hualing chen275379e2021-06-15 17:57:21 +08002085 } else if (type == 2) {
2086 set_pid = set_pids.ad;
hualing chen99508642021-10-18 15:41:17 +08002087 now_pid = now_pids.ad;
hualing chen275379e2021-06-15 17:57:21 +08002088 } else {
2089 set_pid = set_pids.pcr;
hualing chen99508642021-10-18 15:41:17 +08002090 now_pid = now_pids.pcr;
hualing chen275379e2021-06-15 17:57:21 +08002091 }
2092
hualing chena540a7e2020-03-27 16:44:05 +08002093 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002094 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002095 return DVR_FAILURE;
2096 }
Wentao MA96f68962022-06-15 19:45:35 +08002097 DVR_PB_INFO(" do check");
hualing chen86e7d482020-01-16 15:13:33 +08002098 if (now_pid.pid == set_pid.pid) {
2099 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08002100 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08002101 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08002102 if (VALID_PID(now_pid.pid)) {
2103 //stop now stream
2104 if (type == 0) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002105 //stop video
hualing chenc70a8df2020-05-12 19:23:11 +08002106 if (player->has_video == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08002107 DVR_PB_INFO("stop video");
hualing chenc70a8df2020-05-12 19:23:11 +08002108 AmTsPlayer_stopVideoDecoding(player->handle);
2109 player->has_video = DVR_FALSE;
2110 }
hualing chen86e7d482020-01-16 15:13:33 +08002111 } else if (type == 1) {
2112 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08002113 if (player->has_audio == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08002114 DVR_PB_INFO("stop audio");
hualing chenc70a8df2020-05-12 19:23:11 +08002115 AmTsPlayer_stopAudioDecoding(player->handle);
2116 player->has_audio = DVR_FALSE;
2117 }
hualing chen86e7d482020-01-16 15:13:33 +08002118 } else if (type == 2) {
2119 //stop sub audio
Wentao MA96f68962022-06-15 19:45:35 +08002120 DVR_PB_INFO("stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08002121 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002122 } else if (type == 3) {
2123 //pcr
2124 }
2125 }
2126 if (VALID_PID(set_pid.pid)) {
2127 //start
2128 if (type == 0) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002129 //start video
2130 am_tsplayer_video_params video_params;
2131 video_params.pid = set_pid.pid;
2132 video_params.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002133 player->has_video = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002134 DVR_PB_INFO("start video pid[%d]fmt[%d]",video_params.pid, video_params.codectype);
2135 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen2aba4022020-03-02 13:49:55 +08002136 AmTsPlayer_startVideoDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002137 //playback_device_video_start(player->handle,&video_params);
hualing chen86e7d482020-01-16 15:13:33 +08002138 } else if (type == 1) {
2139 //start audio
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002140 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen275379e2021-06-15 17:57:21 +08002141 if (VALID_PID(set_pids.ad.pid)) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002142 am_tsplayer_audio_params ad_params;
2143 ad_params.pid = set_pids.ad.pid;
2144 ad_params.codectype= _dvr_convert_stream_fmt(set_pids.ad.format, DVR_TRUE);
2145 DVR_PB_INFO("start ad audio pid[%d]fmt[%d]",ad_params.pid, ad_params.codectype);
2146 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chen275379e2021-06-15 17:57:21 +08002147 AmTsPlayer_enableADMix(player->handle);
2148 }
2149
Wentao MA270dc0f2022-08-23 13:17:26 +08002150 am_tsplayer_audio_params audio_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002151
Wentao MA270dc0f2022-08-23 13:17:26 +08002152 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002153
Wentao MA270dc0f2022-08-23 13:17:26 +08002154 audio_params.pid = set_pid.pid;
2155 audio_params.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
hualing chenc70a8df2020-05-12 19:23:11 +08002156 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002157 DVR_PB_INFO("start audio pid[%d]fmt[%d]",audio_params.pid, audio_params.codectype);
2158 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002159 if (player->audio_presentation_id > -1) {
2160 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2161 }
hualing chenc70a8df2020-05-12 19:23:11 +08002162 AmTsPlayer_startAudioDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002163 //playback_device_audio_start(player->handle,&audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002164 }
hualing chen86e7d482020-01-16 15:13:33 +08002165 } else if (type == 2) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002166 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen99508642021-10-18 15:41:17 +08002167 if (set_pids.audio.pid == now_pids.audio.pid) {
2168 //stop audio if audio pid not change
Wentao MA96f68962022-06-15 19:45:35 +08002169 DVR_PB_INFO("stop audio when start ad");
hualing chen99508642021-10-18 15:41:17 +08002170 AmTsPlayer_stopAudioDecoding(player->handle);
2171 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002172 am_tsplayer_audio_params audio_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002173
Wentao MA270dc0f2022-08-23 13:17:26 +08002174 memset(&audio_params, 0, sizeof(audio_params));
2175 audio_params.pid = set_pid.pid;
2176 audio_params.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
hualing chenc70a8df2020-05-12 19:23:11 +08002177 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002178 DVR_PB_INFO("start ad audio pid[%d]fmt[%d]",audio_params.pid, audio_params.codectype);
2179 AmTsPlayer_setADParams(player->handle, &audio_params);
hualing chenc70a8df2020-05-12 19:23:11 +08002180 AmTsPlayer_enableADMix(player->handle);
hualing chen99508642021-10-18 15:41:17 +08002181
2182 if (set_pids.audio.pid == now_pids.audio.pid) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002183 am_tsplayer_audio_params audio_params;
hualing chen99508642021-10-18 15:41:17 +08002184
Wentao MA270dc0f2022-08-23 13:17:26 +08002185 memset(&audio_params, 0, sizeof(audio_params));
hualing chen99508642021-10-18 15:41:17 +08002186
Wentao MA270dc0f2022-08-23 13:17:26 +08002187 audio_params.pid = set_pids.audio.pid;
2188 audio_params.codectype= _dvr_convert_stream_fmt(set_pids.audio.format, DVR_TRUE);
hualing chen99508642021-10-18 15:41:17 +08002189 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002190 DVR_PB_INFO("restart audio when start ad");
Wentao MA270dc0f2022-08-23 13:17:26 +08002191 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002192 if (player->audio_presentation_id > -1) {
2193 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2194 }
hualing chen99508642021-10-18 15:41:17 +08002195 AmTsPlayer_startAudioDecoding(player->handle);
2196 }
hualing chenc70a8df2020-05-12 19:23:11 +08002197 }
hualing chen86e7d482020-01-16 15:13:33 +08002198 } else if (type == 3) {
2199 //pcr
Wentao MA96f68962022-06-15 19:45:35 +08002200 DVR_PB_INFO("start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08002201 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002202 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002203 //audio and video all close
2204 if (!player->has_audio && !player->has_video) {
Wentao MA907b6432022-08-01 06:23:08 +00002205 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen5cbe1a62020-02-10 16:36:36 +08002206 }
hualing chen43a89bc2022-01-19 14:31:20 +08002207 } else if (type == 2) {
2208 //case disable ad
Wentao MA96f68962022-06-15 19:45:35 +08002209 DVR_PB_INFO("restart audio when stop ad");
hualing chen43a89bc2022-01-19 14:31:20 +08002210 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
Wentao MA6d045b32022-02-18 18:47:25 +08002211 if (VALID_PID(now_pids.audio.pid)) {
2212 //stop audio if audio pid not change
Wentao MA96f68962022-06-15 19:45:35 +08002213 DVR_PB_INFO("stop audio when stop ad pid [0x%x]", now_pids.audio.pid);
Wentao MA6d045b32022-02-18 18:47:25 +08002214 AmTsPlayer_stopAudioDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08002215 am_tsplayer_audio_params audio_params;
hualing chen43a89bc2022-01-19 14:31:20 +08002216
Wentao MA270dc0f2022-08-23 13:17:26 +08002217 memset(&audio_params, 0, sizeof(audio_params));
hualing chen43a89bc2022-01-19 14:31:20 +08002218
Wentao MA270dc0f2022-08-23 13:17:26 +08002219 audio_params.pid = now_pids.audio.pid;
2220 audio_params.codectype= _dvr_convert_stream_fmt(now_pids.audio.format, DVR_TRUE);
Wentao MA6d045b32022-02-18 18:47:25 +08002221 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002222 DVR_PB_INFO("restart audio when stop ad");
Wentao MA270dc0f2022-08-23 13:17:26 +08002223 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002224 if (player->audio_presentation_id > -1) {
2225 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2226 }
Wentao MA6d045b32022-02-18 18:47:25 +08002227 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen43a89bc2022-01-19 14:31:20 +08002228 }
Wentao MA6d045b32022-02-18 18:47:25 +08002229 }
hualing chen86e7d482020-01-16 15:13:33 +08002230 }
2231 }
2232 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08002233}
hualing chena5f03222021-12-02 11:22:35 +08002234/**\brief dvr play back only update segment pids info
2235 * only update pid info not to start stop codec.
2236 * \param[in] handle playback handle
2237 * \param[in] segment_id need updated pids segment id
2238 * \param[in] p_pids need updated pids
2239 * \retval DVR_SUCCESS On success
2240 * \return Error code
2241 */
2242int dvr_playback_only_update_segment_pids(DVR_PlaybackHandle_t handle, uint64_t segment_id, DVR_PlaybackPids_t *p_pids) {
2243 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2244 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002245 DVR_PB_INFO("player is NULL");
hualing chena5f03222021-12-02 11:22:35 +08002246 return DVR_FAILURE;
2247 }
2248
2249 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002250 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002251 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002252 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 +08002253 list_for_each_entry(segment, &player->segment_list, head)
2254 {
2255 if (segment->segment_id == segment_id) {
2256 if (player->cur_segment_id == segment_id) {
2257 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2258 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2259 //do nothing when ff fb
Wentao MA96f68962022-06-15 19:45:35 +08002260 DVR_PB_INFO("unlock now is ff fb, not to update cur segment info\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002261 dvr_mutex_unlock(&player->lock);
hualing chena5f03222021-12-02 11:22:35 +08002262 return 0;
2263 }
2264 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
2265 }
2266 //save pids info
Wentao MA96f68962022-06-15 19:45:35 +08002267 DVR_PB_INFO(":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chena5f03222021-12-02 11:22:35 +08002268 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +08002269 DVR_PB_INFO(":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chena5f03222021-12-02 11:22:35 +08002270 break;
2271 }
2272 }
Wentao MA96f68962022-06-15 19:45:35 +08002273 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002274 dvr_mutex_unlock(&player->lock);
hualing chena5f03222021-12-02 11:22:35 +08002275 return DVR_SUCCESS;
2276}
2277
hualing chen5cbe1a62020-02-10 16:36:36 +08002278/**\brief dvr play back update segment pids
2279 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08002280 * add pid stream and stop remove pid stream.
2281 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08002282 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08002283 * \retval DVR_SUCCESS On success
2284 * \return Error code
2285 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002286int 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 +08002287 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002288 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002289 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002290 return DVR_FAILURE;
2291 }
2292
hualing chen040df222020-01-17 13:35:02 +08002293 DVR_PlaybackSegmentInfo_t *segment;
Wentao MA96f68962022-06-15 19:45:35 +08002294 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002295 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002296 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 +08002297
hualing chen040df222020-01-17 13:35:02 +08002298 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002299 {
hualing chen040df222020-01-17 13:35:02 +08002300 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002301
2302 if (player->cur_segment_id == segment_id) {
2303 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2304 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2305 //do nothing when ff fb
Wentao MA96f68962022-06-15 19:45:35 +08002306 DVR_PB_INFO("unlock now is ff fb, not to update cur segment info\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002307 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002308 return 0;
2309 }
2310
2311 //if segment is on going segment,we need stop start stream
2312 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
Wentao MA96f68962022-06-15 19:45:35 +08002313 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002314 dvr_mutex_unlock(&player->lock);
hualing chen8a657f32021-08-30 13:12:49 +08002315 if (segment->pids.audio.pid != p_pids->audio.pid &&
2316 segment->pids.audio.pid == 0x1fff) {
hualing chena5f03222021-12-02 11:22:35 +08002317 //not used this to seek to start pos.we will
Wentao MA9a164002022-08-29 11:20:24 +08002318 //add update only api. if need seek to start
hualing chena5f03222021-12-02 11:22:35 +08002319 //pos, we will call only update api and used seek api
2320 //to start and stop av codec
2321 if (0 && player->need_seek_start == DVR_TRUE) {
hualing chen8a657f32021-08-30 13:12:49 +08002322 player->need_seek_start = DVR_FALSE;
2323 pthread_mutex_lock(&player->segment_lock);
2324 player->drop_ts = DVR_TRUE;
2325 player->ts_cache_len = 0;
2326 if (player->first_start_time > 0)
2327 player->first_start_time = player->first_start_time - 1;
2328 segment_seek(player->r_handle, (uint64_t)(player->first_start_time), player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +08002329 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 +08002330 pthread_mutex_unlock(&player->segment_lock);
2331 }
2332 }
hualing chen1679f812021-11-08 15:17:46 +08002333 //check video pids, stop or restart
2334 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 0);
2335 //check sub audio pids stop or restart
2336 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 2);
2337 //check audio pids stop or restart
2338 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 1);
2339 //check pcr pids stop or restart
2340 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 3);
2341
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002342 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002343 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
2344 //if state is pause, we need process at resume api. we only record change info
2345 int v_cmd = DVR_PLAYBACK_CMD_NONE;
2346 int a_cmd = DVR_PLAYBACK_CMD_NONE;
2347 if (VALID_PID(segment->pids.video.pid)
2348 && VALID_PID(p_pids->video.pid)
2349 && segment->pids.video.pid != p_pids->video.pid) {
2350 //restart video
Wentao MA270dc0f2022-08-23 13:17:26 +08002351 v_cmd = DVR_PLAYBACK_CMD_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002352 }
2353 if (!VALID_PID(segment->pids.video.pid)
2354 && VALID_PID(p_pids->video.pid)
2355 && segment->pids.video.pid != p_pids->video.pid) {
2356 //start video
Wentao MA270dc0f2022-08-23 13:17:26 +08002357 v_cmd = DVR_PLAYBACK_CMD_V_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002358 }
2359 if (VALID_PID(segment->pids.video.pid)
2360 && !VALID_PID(p_pids->video.pid)
2361 && segment->pids.video.pid != p_pids->video.pid) {
2362 //stop video
Wentao MA270dc0f2022-08-23 13:17:26 +08002363 v_cmd = DVR_PLAYBACK_CMD_V_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08002364 }
2365 if (VALID_PID(segment->pids.audio.pid)
2366 && VALID_PID(p_pids->audio.pid)
2367 && segment->pids.audio.pid != p_pids->audio.pid) {
2368 //restart audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002369 a_cmd = DVR_PLAYBACK_CMD_A_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002370 }
2371 if (!VALID_PID(segment->pids.audio.pid)
2372 && VALID_PID(p_pids->audio.pid)
2373 && segment->pids.audio.pid != p_pids->audio.pid) {
2374 //start audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002375 a_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002376 }
2377 if (VALID_PID(segment->pids.audio.pid)
2378 && !VALID_PID(p_pids->audio.pid)
2379 && segment->pids.audio.pid != p_pids->audio.pid) {
2380 //stop audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002381 a_cmd = DVR_PLAYBACK_CMD_A_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08002382 }
2383 if (a_cmd == DVR_PLAYBACK_CMD_NONE
2384 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
2385 //do nothing
2386 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
2387 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
2388 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2389 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
2390 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
2391 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002392 if (v_cmd == DVR_PLAYBACK_CMD_V_RESTART
2393 && (a_cmd == DVR_PLAYBACK_CMD_A_RESTART)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002394 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002395 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
2396 }else if (v_cmd == DVR_PLAYBACK_CMD_V_RESTART
2397 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002398 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002399 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002400 } else {
2401 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002402 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP_V_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002403 }
2404
Wentao MA270dc0f2022-08-23 13:17:26 +08002405 if (v_cmd == DVR_PLAYBACK_CMD_V_START
2406 && (a_cmd == DVR_PLAYBACK_CMD_A_RESTART)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002407 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002408 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_START_A_RESTART;
2409 } else if (v_cmd == DVR_PLAYBACK_CMD_V_START
2410 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002411 //not occur this case
2412 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2413 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
2414 } else {
2415 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002416 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP_V_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002417 }
2418
Wentao MA270dc0f2022-08-23 13:17:26 +08002419 if (v_cmd == DVR_PLAYBACK_CMD_V_STOP
2420 && a_cmd == DVR_PLAYBACK_CMD_A_START) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002421 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002422 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP_A_START;
2423 } else if (v_cmd == DVR_PLAYBACK_CMD_V_STOP
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_STOP_A_RESTART;
hualing chen5cbe1a62020-02-10 16:36:36 +08002427 } else {
2428 //not occur this case
2429 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2430 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2431 }
2432 }
2433 }
hualing chene10666f2020-04-14 13:58:37 +08002434 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08002435 }
hualing chen86e7d482020-01-16 15:13:33 +08002436 //save pids info
Wentao MA96f68962022-06-15 19:45:35 +08002437 DVR_PB_INFO(":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08002438 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
Wentao MA96f68962022-06-15 19:45:35 +08002439 DVR_PB_INFO(":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002440 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002441 }
hualing chen86e7d482020-01-16 15:13:33 +08002442 }
Wentao MA96f68962022-06-15 19:45:35 +08002443 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002444 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002445 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002446}
2447/**\brief Stop play, will stop video and audio
2448 * \param[in] handle playback handle
2449 * \param[in] clear is clear last frame
2450 * \retval DVR_SUCCESS On success
2451 * \return Error code
2452 */
hualing chen040df222020-01-17 13:35:02 +08002453int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
2454 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MAe8ba5172022-08-09 11:18:17 +08002455 DVR_UNUSED(clear);
hualing chena540a7e2020-03-27 16:44:05 +08002456 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002457 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002458 return DVR_FAILURE;
2459 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002460 if (player->state == DVR_PLAYBACK_STATE_STOP) {
Wentao MA96f68962022-06-15 19:45:35 +08002461 DVR_PB_INFO(":playback is stoped");
hualing chenb96aa2c2020-04-15 14:13:53 +08002462 return DVR_SUCCESS;
2463 }
Ke Gong3c0caba2020-04-21 22:58:18 -07002464 if (player->state == DVR_PLAYBACK_STATE_STOP) {
Wentao MA96f68962022-06-15 19:45:35 +08002465 DVR_PB_INFO(":playback is stoped");
Ke Gong3c0caba2020-04-21 22:58:18 -07002466 return DVR_SUCCESS;
2467 }
hualing chen87072a82020-03-12 16:20:12 +08002468 _stop_playback_thread(handle);
Wentao MA96f68962022-06-15 19:45:35 +08002469 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002470 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002471 DVR_PB_INFO(":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08002472 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002473 if (player->has_video) {
2474 AmTsPlayer_resumeVideoDecoding(player->handle);
2475 }
2476 if (player->has_audio) {
2477 AmTsPlayer_resumeAudioDecoding(player->handle);
2478 }
2479 if (player->has_video) {
2480 player->has_video = DVR_FALSE;
hualing chen10cdb162021-02-05 10:44:41 +08002481 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002482 AmTsPlayer_stopVideoDecoding(player->handle);
2483 }
2484 if (player->has_audio) {
2485 player->has_audio = DVR_FALSE;
2486 AmTsPlayer_stopAudioDecoding(player->handle);
2487 }
hualing chendf118dd2020-05-21 15:49:11 +08002488 if (player->has_ad_audio) {
2489 player->has_ad_audio =DVR_FALSE;
2490 AmTsPlayer_disableADMix(player->handle);
2491 }
hualing chen266b9502020-04-04 17:39:39 +08002492
hualing chen86e7d482020-01-16 15:13:33 +08002493 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002494 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2495 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002496 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
hualing chen87072a82020-03-12 16:20:12 +08002497 player->cur_segment_id = UINT64_MAX;
2498 player->segment_is_open = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08002499 DVR_PB_DEBUG("unlock");
2500 DVR_PB_INFO("player->state %s", _dvr_playback_state_toString(player->state));
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002501 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002502 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002503}
2504/**\brief Start play audio
2505 * \param[in] handle playback handle
2506 * \param[in] params audio playback params,contains fmt and pid...
2507 * \retval DVR_SUCCESS On success
2508 * \return Error code
2509 */
hualing chen2aba4022020-03-02 13:49:55 +08002510
Wentao MA270dc0f2022-08-23 13:17:26 +08002511int 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 +08002512 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002513
2514 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002515 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002516 return DVR_FAILURE;
2517 }
hualing chen86e7d482020-01-16 15:13:33 +08002518 _start_playback_thread(handle);
2519 //start audio and video
Wentao MA96f68962022-06-15 19:45:35 +08002520 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002521 dvr_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002522
Wentao MA270dc0f2022-08-23 13:17:26 +08002523 if (VALID_PID(ad_param->pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08002524 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002525 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08002526 AmTsPlayer_setADParams(player->handle, ad_param);
hualing chendf118dd2020-05-21 15:49:11 +08002527 AmTsPlayer_enableADMix(player->handle);
2528 }
hualing chen969fe7b2021-05-26 15:13:17 +08002529 if (VALID_PID(param->pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08002530 DVR_PB_INFO("start audio");
hualing chen969fe7b2021-05-26 15:13:17 +08002531 player->has_audio = DVR_TRUE;
2532 AmTsPlayer_setAudioParams(player->handle, param);
Wentao MA5629ad82022-08-24 10:03:02 +08002533 if (player->audio_presentation_id > -1) {
2534 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2535 }
hualing chen969fe7b2021-05-26 15:13:17 +08002536 AmTsPlayer_startAudioDecoding(player->handle);
2537 }
hualing chendf118dd2020-05-21 15:49:11 +08002538
hualing chen86e7d482020-01-16 15:13:33 +08002539 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002540 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen040df222020-01-17 13:35:02 +08002541 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002542 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002543 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002544 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002545 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002546}
2547/**\brief Stop play audio
2548 * \param[in] handle playback handle
2549 * \retval DVR_SUCCESS On success
2550 * \return Error code
2551 */
hualing chen040df222020-01-17 13:35:02 +08002552int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2553 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002554
2555 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002556 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002557 return DVR_FAILURE;
2558 }
2559
hualing chen2aba4022020-03-02 13:49:55 +08002560 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002561 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002562 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002563 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
Wentao MA9a164002022-08-29 11:20:24 +08002564 //destroy thread
hualing chen86e7d482020-01-16 15:13:33 +08002565 _stop_playback_thread(handle);
2566 } else {
2567 //do nothing.video is playing
2568 }
Wentao MA96f68962022-06-15 19:45:35 +08002569 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002570 dvr_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002571
hualing chenf00cdc82020-06-10 14:23:35 +08002572 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002573 player->has_audio = DVR_FALSE;
2574 AmTsPlayer_stopAudioDecoding(player->handle);
2575 }
hualing chen87072a82020-03-12 16:20:12 +08002576
hualing chendf118dd2020-05-21 15:49:11 +08002577 if (player->has_ad_audio) {
2578 player->has_ad_audio =DVR_FALSE;
2579 AmTsPlayer_disableADMix(player->handle);
2580 }
2581
hualing chen87072a82020-03-12 16:20:12 +08002582 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002583 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002584
Wentao MA96f68962022-06-15 19:45:35 +08002585 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002586 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002587 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002588}
2589/**\brief Start play video
2590 * \param[in] handle playback handle
2591 * \param[in] params video playback params,contains fmt and pid...
2592 * \retval DVR_SUCCESS On success
2593 * \return Error code
2594 */
hualing chen2aba4022020-03-02 13:49:55 +08002595int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002596 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002597
2598 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002599 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002600 return DVR_FAILURE;
2601 }
2602
hualing chen86e7d482020-01-16 15:13:33 +08002603 _start_playback_thread(handle);
2604 //start audio and video
Wentao MA96f68962022-06-15 19:45:35 +08002605 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002606 dvr_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002607 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002608 AmTsPlayer_setVideoParams(player->handle, param);
hualing chen21a40372021-10-29 11:07:26 +08002609 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chena540a7e2020-03-27 16:44:05 +08002610 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002611
2612 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002613 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002614 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
Wentao MA96f68962022-06-15 19:45:35 +08002615 DVR_PB_INFO("settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002616 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2617 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002618 }
2619 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002620 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_START;
hualing chen040df222020-01-17 13:35:02 +08002621 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002622 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002623 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002624 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002625 return DVR_SUCCESS;
2626}
2627/**\brief Stop play video
2628 * \param[in] handle playback handle
2629 * \retval DVR_SUCCESS On success
2630 * \return Error code
2631 */
hualing chen040df222020-01-17 13:35:02 +08002632int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2633 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002634
2635 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002636 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002637 return DVR_FAILURE;
2638 }
2639
hualing chen86e7d482020-01-16 15:13:33 +08002640 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002641 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
Wentao MA907b6432022-08-01 06:23:08 +00002642 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_STOP);
Wentao MA9a164002022-08-29 11:20:24 +08002643 //destroy thread
hualing chen86e7d482020-01-16 15:13:33 +08002644 _stop_playback_thread(handle);
2645 } else {
2646 //do nothing.audio is playing
2647 }
hualing chen7a56cba2020-04-14 14:09:27 +08002648
Wentao MA96f68962022-06-15 19:45:35 +08002649 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002650 dvr_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002651
hualing chen87072a82020-03-12 16:20:12 +08002652 player->has_video = DVR_FALSE;
2653
2654 AmTsPlayer_stopVideoDecoding(player->handle);
2655 //playback_device_video_stop(player->handle);
2656
2657 player->cmd.last_cmd = player->cmd.cur_cmd;
Wentao MA270dc0f2022-08-23 13:17:26 +08002658 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_V_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002659
Wentao MA96f68962022-06-15 19:45:35 +08002660 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002661 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002662 return DVR_SUCCESS;
2663}
2664/**\brief Pause play
2665 * \param[in] handle playback handle
2666 * \param[in] flush whether its internal buffers should be flushed
2667 * \retval DVR_SUCCESS On success
2668 * \return Error code
2669 */
hualing chen040df222020-01-17 13:35:02 +08002670int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2671 DVR_Playback_t *player = (DVR_Playback_t *) handle;
Wentao MAe8ba5172022-08-09 11:18:17 +08002672 DVR_UNUSED(flush);
hualing chena540a7e2020-03-27 16:44:05 +08002673 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002674 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002675 return DVR_FAILURE;
2676 }
hualing chenf00cdc82020-06-10 14:23:35 +08002677 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
Wentao MA96f68962022-06-15 19:45:35 +08002678 DVR_PB_INFO("player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002679 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002680 }
Wentao MA96f68962022-06-15 19:45:35 +08002681 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002682 dvr_mutex_lock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002683 DVR_PB_DEBUG("get lock");
hualing chen266b9502020-04-04 17:39:39 +08002684 if (player->has_video)
2685 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002686 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002687 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002688
2689 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002690 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2691 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2692 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00002693 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08002694 } else {
2695 player->cmd.last_cmd = player->cmd.cur_cmd;
2696 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2697 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00002698 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
hualing chen87072a82020-03-12 16:20:12 +08002699 }
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002700 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08002701 DVR_PB_DEBUG("unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002702
hualing chen86e7d482020-01-16 15:13:33 +08002703 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002704}
2705
hualing chen5cbe1a62020-02-10 16:36:36 +08002706//not add lock
2707static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2708{
2709 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2710
hualing chena540a7e2020-03-27 16:44:05 +08002711 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002712 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002713 return DVR_FAILURE;
2714 }
2715
hualing chen5cbe1a62020-02-10 16:36:36 +08002716 //get video params and audio params
Wentao MA96f68962022-06-15 19:45:35 +08002717 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002718 dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08002719 am_tsplayer_video_params video_params;
2720 am_tsplayer_audio_params audio_params;
2721 am_tsplayer_audio_params ad_params;
hualing chencc91e1c2020-02-28 13:26:17 +08002722 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002723
Wentao MA270dc0f2022-08-23 13:17:26 +08002724 memset(&video_params, 0, sizeof(video_params));
2725 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002726
Wentao MA270dc0f2022-08-23 13:17:26 +08002727 _dvr_playback_get_playinfo(handle, segmentid, &video_params, &audio_params, &ad_params);
Wentao MA96f68962022-06-15 19:45:35 +08002728 DVR_PB_INFO("unlock cmd: %d", cmd);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002729 dvr_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002730
2731 switch (cmd) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002732 case DVR_PLAYBACK_CMD_AV_RESTART:
hualing chen5cbe1a62020-02-10 16:36:36 +08002733 //av restart
Wentao MA270dc0f2022-08-23 13:17:26 +08002734 DVR_PB_INFO("do_cmd av_restart");
hualing chen87072a82020-03-12 16:20:12 +08002735 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002736 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002737 case DVR_PLAYBACK_CMD_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002738 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002739 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002740 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002741 case DVR_PLAYBACK_CMD_V_START:
2742 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002743 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002744 case DVR_PLAYBACK_CMD_V_STOP:
hualing chen2aba4022020-03-02 13:49:55 +08002745 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002746 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002747 case DVR_PLAYBACK_CMD_A_RESTART:
hualing chen5cbe1a62020-02-10 16:36:36 +08002748 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002749 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002750 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002751 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002752 case DVR_PLAYBACK_CMD_A_START:
2753 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002754 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002755 case DVR_PLAYBACK_CMD_A_STOP:
hualing chen2aba4022020-03-02 13:49:55 +08002756 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002757 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002758 case DVR_PLAYBACK_CMD_A_STOP_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002759 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2760 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002761 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002762 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002763 case DVR_PLAYBACK_CMD_A_STOP_V_START:
hualing chen2aba4022020-03-02 13:49:55 +08002764 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002765 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002766 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002767 case DVR_PLAYBACK_CMD_V_STOP_A_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002768 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2769 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002770 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002771 break;
2772 case DVR_PLAYBACK_CMD_STOP:
2773 break;
2774 case DVR_PLAYBACK_CMD_START:
2775 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002776 case DVR_PLAYBACK_CMD_A_START_V_RESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002777 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
Wentao MA270dc0f2022-08-23 13:17:26 +08002778 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &video_params);
2779 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002780 break;
Wentao MA270dc0f2022-08-23 13:17:26 +08002781 case DVR_PLAYBACK_CMD_V_START_A_RESTART:
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);
2784 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08002785 break;
2786 case DVR_PLAYBACK_CMD_FF:
2787 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002788 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002789 break;
2790 default:
2791 break;
2792 }
2793 return DVR_SUCCESS;
2794}
2795
2796/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002797 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002798 * \retval DVR_SUCCESS On success
2799 * \return Error code
2800 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002801int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2802 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08002803 uint32_t pos = 0;
hualing chen03fd4942021-07-15 15:56:41 +08002804 uint64_t segmentid = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002805 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002806 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002807 return DVR_FAILURE;
2808 }
2809
hualing chena991aa82021-08-16 10:21:15 +08002810 if (dvr_playback_check_limit(handle)) {
2811 //get id and pos to check if we can seek to this pos
Wentao MA96f68962022-06-15 19:45:35 +08002812 DVR_PB_INFO("player start calculate time");
hualing chena991aa82021-08-16 10:21:15 +08002813 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2814 if (segmentid != player->cur_segment_id ||
2815 (segmentid == player->cur_segment_id &&
2816 pos > _dvr_get_cur_time(handle))) {
2817 //first to seek new pos and to resume
Wentao MA96f68962022-06-15 19:45:35 +08002818 DVR_PB_INFO("seek new pos and to resume");
hualing chena991aa82021-08-16 10:21:15 +08002819 dvr_playback_seek(handle, segmentid, pos);
2820 }
hualing chen7ea70a72021-09-09 11:25:13 +08002821 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002822 DVR_PB_INFO("player is not set limit");
hualing chen03fd4942021-07-15 15:56:41 +08002823 }
hualing chena991aa82021-08-16 10:21:15 +08002824
hualing chen5cbe1a62020-02-10 16:36:36 +08002825 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002826 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002827 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002828 player->first_frame = 0;
2829 if (player->has_video)
2830 AmTsPlayer_pauseVideoDecoding(player->handle);
2831 if (player->has_audio)
2832 AmTsPlayer_pauseAudioDecoding(player->handle);
2833
hualing chen266b9502020-04-04 17:39:39 +08002834 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08002835 DVR_PB_INFO("dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002836 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2837 AmTsPlayer_resumeVideoDecoding(player->handle);
2838 }
2839 if (player->has_audio) {
2840 AmTsPlayer_resumeAudioDecoding(player->handle);
2841 }
2842 //check is has audio param,if has audio .we need start audio,
2843 //we will stop audio when ff fb, if reach end, we will pause.so we need
2844 //start audio when resume play
2845
Wentao MA270dc0f2022-08-23 13:17:26 +08002846 am_tsplayer_video_params video_params;
2847 am_tsplayer_audio_params audio_params;
2848 am_tsplayer_audio_params ad_params;
hualing chen266b9502020-04-04 17:39:39 +08002849 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002850
Wentao MA270dc0f2022-08-23 13:17:26 +08002851 memset(&video_params, 0, sizeof(video_params));
2852 memset(&audio_params, 0, sizeof(audio_params));
2853 _dvr_playback_get_playinfo(handle, segmentid, &video_params, &audio_params, &ad_params);
hualing chen266b9502020-04-04 17:39:39 +08002854 //valid audio pid, start audio
Wentao MA270dc0f2022-08-23 13:17:26 +08002855 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 +08002856 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08002857 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08002858 dvr_playback_change_seek_state(handle, ad_params.pid);
2859 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chen969fe7b2021-05-26 15:13:17 +08002860 AmTsPlayer_enableADMix(player->handle);
2861 }
2862
Wentao MA270dc0f2022-08-23 13:17:26 +08002863 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 +08002864 player->has_audio = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08002865 dvr_playback_change_seek_state(handle, audio_params.pid);
2866 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08002867 if (player->audio_presentation_id > -1) {
2868 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
2869 }
hualing chen266b9502020-04-04 17:39:39 +08002870 AmTsPlayer_startAudioDecoding(player->handle);
2871 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002872 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 +08002873 }
hualing chendf118dd2020-05-21 15:49:11 +08002874
hualing chen87072a82020-03-12 16:20:12 +08002875 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2876 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2877 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002878 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen87072a82020-03-12 16:20:12 +08002879 } else {
2880 player->cmd.last_cmd = player->cmd.cur_cmd;
2881 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2882 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002883 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen87072a82020-03-12 16:20:12 +08002884 }
Wentao MA96f68962022-06-15 19:45:35 +08002885 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002886 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002887 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
Wentao MA96f68962022-06-15 19:45:35 +08002888 DVR_PB_INFO("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002889 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002890 player->first_frame = 0;
2891 if (player->has_video)
2892 AmTsPlayer_pauseVideoDecoding(player->handle);
2893 if (player->has_audio)
2894 AmTsPlayer_pauseAudioDecoding(player->handle);
2895
hualing chene41f4372020-06-06 16:29:17 +08002896 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08002897 DVR_PB_INFO("dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002898 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002899 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002900 }
hualing chen041c4092020-04-05 15:11:50 +08002901 if (player->has_audio)
2902 AmTsPlayer_resumeAudioDecoding(player->handle);
Wentao MA96f68962022-06-15 19:45:35 +08002903 DVR_PB_INFO("set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen9811b212020-10-29 11:21:44 +08002904 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2905 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chend1686e52022-01-05 17:10:42 +08002906 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00002907 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
Wentao MA96f68962022-06-15 19:45:35 +08002908 DVR_PB_INFO("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002909 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002910 } else {
2911 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2912 {
Wentao MA96f68962022-06-15 19:45:35 +08002913 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002914 dvr_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002915 player->first_frame = 0;
2916 if (player->has_video)
2917 AmTsPlayer_pauseVideoDecoding(player->handle);
2918 if (player->has_audio)
2919 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen041c4092020-04-05 15:11:50 +08002920 //clear flag
Wentao MA96f68962022-06-15 19:45:35 +08002921 DVR_PB_INFO("clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002922 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2923 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002924 if (player->has_video) {
2925 AmTsPlayer_resumeVideoDecoding(player->handle);
2926 }
2927 if (player->has_audio)
2928 AmTsPlayer_resumeAudioDecoding(player->handle);
Wentao MA96f68962022-06-15 19:45:35 +08002929 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002930 dvr_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002931 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002932 }
2933 return DVR_SUCCESS;
2934}
2935
hualing chena540a7e2020-03-27 16:44:05 +08002936static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2937
2938 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2939 DVR_PlaybackSegmentInfo_t *segment = NULL;
2940 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2941 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2942
2943 list_for_each_entry(segment, &player->segment_list, head)
2944 {
2945 if (segment->segment_id == segment_id) {
2946 cur_segment = segment;
2947 }
2948 if (segment->segment_id == set_seg_id) {
2949 set_segment = segment;
2950 }
2951 if (cur_segment != NULL && set_segment != NULL) {
2952 break;
2953 }
2954 }
2955 if (cur_segment == NULL || set_segment == NULL) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002956 DVR_PB_INFO("set segment or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002957 return DVR_TRUE;
2958 }
2959 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2960 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2961 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2962 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
Wentao MA96f68962022-06-15 19:45:35 +08002963 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 +08002964 return DVR_TRUE;
2965 }
Wentao MA96f68962022-06-15 19:45:35 +08002966 DVR_PB_INFO("play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002967 return DVR_FALSE;
2968}
2969
hualing chen03fd4942021-07-15 15:56:41 +08002970/**\brief set limit
2971 * \param[in] handle playback handle
2972 * \param[in] rec start time ms
2973 * \param[in] rec limit time ms
2974 * \retval DVR_SUCCESS On success
2975 * \return Error code
2976 */
hualing chen7ea70a72021-09-09 11:25:13 +08002977int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint32_t time, uint32_t limit)
hualing chen03fd4942021-07-15 15:56:41 +08002978{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2979
2980 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002981 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08002982 return DVR_FAILURE;
2983 }
hualing chen7ea70a72021-09-09 11:25:13 +08002984 _dvr_getClock_sec();
Wentao MA96f68962022-06-15 19:45:35 +08002985 DVR_PB_INFO("lock time %lu limit: %u player->state:%d", time, limit, player->state);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002986 dvr_mutex_lock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08002987 player->rec_start = time;
2988 player->limit = limit;
Wentao MA96f68962022-06-15 19:45:35 +08002989 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08002990 dvr_mutex_unlock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08002991 return DVR_SUCCESS;
2992}
2993
hualing chen5cbe1a62020-02-10 16:36:36 +08002994/**\brief seek
2995 * \param[in] handle playback handle
2996 * \param[in] time_offset time offset base cur segment
2997 * \retval DVR_SUCCESS On success
2998 * \return Error code
2999 */
hualing chencc91e1c2020-02-28 13:26:17 +08003000int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08003001 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08003002 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08003003 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003004 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003005 return DVR_FAILURE;
3006 }
3007
Wentao MA96f68962022-06-15 19:45:35 +08003008 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 +08003009 dvr_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003010
hualing chen86e7d482020-01-16 15:13:33 +08003011 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08003012 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08003013 DVR_PB_INFO("player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08003014
hualing chen5cbe1a62020-02-10 16:36:36 +08003015 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08003016 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08003017 if (ret ==DVR_FAILURE) {
Wentao MA96f68962022-06-15 19:45:35 +08003018 DVR_PB_INFO("unlock seek error at open segment");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003019 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003020 return DVR_FAILURE;
3021 }
3022 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
3023 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
Wentao MA96f68962022-06-15 19:45:35 +08003024 DVR_PB_INFO("is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08003025 time_offset = _dvr_get_end_time(handle);
3026 } else {
Wentao MA96f68962022-06-15 19:45:35 +08003027 DVR_PB_INFO("is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08003028 time_offset = _dvr_get_end_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08003029 ret = DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08003030 }
3031 }
3032
Wentao MA96f68962022-06-15 19:45:35 +08003033 DVR_PB_INFO("seek open id[%lld]flag[0x%x] time_offset %u",
hualing chen03fd4942021-07-15 15:56:41 +08003034 player->cur_segment.segment_id,
3035 player->cur_segment.flags,
3036 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08003037 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08003038 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3039 //forward playback.not seek end of file
3040 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
3041 //default -2000ms
3042 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
3043 }
hualing chen86e7d482020-01-16 15:13:33 +08003044 }
Wentao MA01de0e62022-01-10 18:48:23 +08003045 // Seek can be regarded as a new playback, so keep the start segment_id for it also
hualing chen8a657f32021-08-30 13:12:49 +08003046 if (player->need_seek_start == DVR_TRUE) {
3047 player->first_start_time = (uint64_t)time_offset + 1;//set first start time not eq 0
Wentao MA01de0e62022-01-10 18:48:23 +08003048 player->first_start_id = player->cur_segment.segment_id;
hualing chen8a657f32021-08-30 13:12:49 +08003049 }
hualing chen2aba4022020-03-02 13:49:55 +08003050 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08003051 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08003052 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08003053 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
Wentao MA96f68962022-06-15 19:45:35 +08003054 DVR_PB_ERROR("seek get offset by time offset, offset=%d time_offset %u",offset, time_offset);
hualing chen2aba4022020-03-02 13:49:55 +08003055 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08003056 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08003057
hualing chen2aba4022020-03-02 13:49:55 +08003058 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08003059
3060 player->last_send_time_id = UINT64_MAX;
Wentao MA270dc0f2022-08-23 13:17:26 +08003061 player->last_segment_total = 0LL;
hualing chen03fd4942021-07-15 15:56:41 +08003062 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08003063 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08003064 player->fffb_current = _dvr_time_getClock();
3065 player->fffb_start = player->fffb_current;
3066 player->fffb_start_pcr = _dvr_get_cur_time(handle);
3067 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08003068 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08003069 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003070 //only seek file,not start
Wentao MA96f68962022-06-15 19:45:35 +08003071 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003072 dvr_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08003073 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08003074 }
hualing chen86e7d482020-01-16 15:13:33 +08003075 //stop play
Wentao MA96f68962022-06-15 19:45:35 +08003076 DVR_PB_ERROR("seek stop play, not inject data has video[%d]audio[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003077 player->has_video, player->has_audio);
hualing chen1ffd85b2021-08-16 15:18:43 +08003078
hualing chen266b9502020-04-04 17:39:39 +08003079 if (player->has_video) {
hualing chen7e14e532021-09-23 11:23:28 +08003080 //player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003081 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003082 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08003083 }
3084
hualing chen40dd5462021-11-26 19:56:20 +08003085
hualing chen266b9502020-04-04 17:39:39 +08003086 if (player->has_audio) {
3087 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003088 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08003089 }
hualing chendf118dd2020-05-21 15:49:11 +08003090 if (player->has_ad_audio) {
3091 player->has_ad_audio =DVR_FALSE;
3092 AmTsPlayer_disableADMix(player->handle);
3093 }
3094
hualing chen86e7d482020-01-16 15:13:33 +08003095 //start play
Wentao MA270dc0f2022-08-23 13:17:26 +08003096 am_tsplayer_video_params video_params;
3097 am_tsplayer_audio_params audio_params;
3098 am_tsplayer_audio_params ad_params;
hualing chenb31a6c62020-01-13 17:27:00 +08003099
Wentao MA270dc0f2022-08-23 13:17:26 +08003100 memset(&video_params, 0, sizeof(video_params));
3101 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003102
hualing chen040df222020-01-17 13:35:02 +08003103 player->cur_segment_id = segment_id;
3104
3105 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08003106 //get segment info and audio video pid fmt ;
Wentao MA270dc0f2022-08-23 13:17:26 +08003107 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen86e7d482020-01-16 15:13:33 +08003108 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003109 if (video_params.pid != player->fake_pid && !VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
hualing chena5f03222021-12-02 11:22:35 +08003110 //audio and video pid is all invalid, return error.
Wentao MA270dc0f2022-08-23 13:17:26 +08003111 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 +08003112 dvr_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08003113 return -1;
3114 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003115 DVR_PB_ERROR("seek start[0x%x]", video_params.pid);
hualing chen86e7d482020-01-16 15:13:33 +08003116 //add
hualing chen040df222020-01-17 13:35:02 +08003117 if (sync == DVR_PLAYBACK_SYNC) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003118 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003119 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08003120 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08003121 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08003122 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08003123 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003124 //if is pause state. we need set trick mode.
Wentao MA96f68962022-06-15 19:45:35 +08003125 DVR_PB_INFO("seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003126 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08003127 }
Wentao MA96f68962022-06-15 19:45:35 +08003128 DVR_PB_INFO("start video");
Wentao MA270dc0f2022-08-23 13:17:26 +08003129 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003130 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003131 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08003132 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
3133 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
3134 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
3135 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
3136 AmTsPlayer_stopFast(player->handle);
3137 }
hualing chen266b9502020-04-04 17:39:39 +08003138 player->has_video = DVR_TRUE;
hualing chen7e14e532021-09-23 11:23:28 +08003139 } else {
3140 player->has_video = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08003141 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003142 if (VALID_PID(ad_params.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08003143 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003144 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003145 dvr_playback_change_seek_state(handle, ad_params.pid);
3146 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08003147 AmTsPlayer_enableADMix(player->handle);
3148 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003149 if (VALID_PID(audio_params.pid) && player->speed == 1.0) {
Wentao MA96f68962022-06-15 19:45:35 +08003150 DVR_PB_INFO("start audio seek");
Wentao MA270dc0f2022-08-23 13:17:26 +08003151 dvr_playback_change_seek_state(handle, audio_params.pid);
3152 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08003153 if (player->audio_presentation_id > -1) {
3154 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
3155 }
hualing chen969fe7b2021-05-26 15:13:17 +08003156 AmTsPlayer_startAudioDecoding(player->handle);
3157 player->has_audio = DVR_TRUE;
3158 }
hualing chen43a89bc2022-01-19 14:31:20 +08003159#ifdef AVSYNC_USED_PCR
3160 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08003161 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08003162 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
3163 }
3164#endif
hualing chen86e7d482020-01-16 15:13:33 +08003165 }
hualing chen1ffd85b2021-08-16 15:18:43 +08003166 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen2aba4022020-03-02 13:49:55 +08003167 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
Wentao MA907b6432022-08-01 06:23:08 +00003168 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_PAUSE);
Wentao MA270dc0f2022-08-23 13:17:26 +08003169 if (VALID_PID(audio_params.pid) || VALID_PID(video_params.pid))
hualing chena5f03222021-12-02 11:22:35 +08003170 player->seek_pause = DVR_TRUE;
Wentao MA270dc0f2022-08-23 13:17:26 +08003171 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 +08003172 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3173 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08003174 player->speed > 1.0f||
3175 player->speed <= -1.0f) {
Wentao MA96f68962022-06-15 19:45:35 +08003176 DVR_PB_INFO("not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08003177 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08003178 } else {
Wentao MA96f68962022-06-15 19:45:35 +08003179 DVR_PB_INFO("set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08003180 player->cmd.last_cmd = player->cmd.cur_cmd;
3181 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
3182 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00003183 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen2aba4022020-03-02 13:49:55 +08003184 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003185 player->last_send_time_id = UINT64_MAX;
Wentao MA96f68962022-06-15 19:45:35 +08003186 DVR_PB_DEBUG("unlock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003187 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003188
3189 return DVR_SUCCESS;
3190}
hualing chen5cbe1a62020-02-10 16:36:36 +08003191
Wentao MAac5ea062022-08-11 11:44:27 +08003192// Get current playback time position of the ongoing segment.
3193// Notice the return value may be negative. This is because previous segment's
3194// data cached in demux buffer need to be considered.
hualing chen5cbe1a62020-02-10 16:36:36 +08003195static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
3196 //get cur time of segment
3197 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003198
Gong Ke2a0ebbe2021-05-25 15:22:50 +08003199 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003200 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003201 return DVR_FAILURE;
3202 }
3203
Wentao MA270dc0f2022-08-23 13:17:26 +08003204 int64_t cache = 0;//default es buf cache 500ms
hualing chen2aba4022020-03-02 13:49:55 +08003205 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003206 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
hualing chena5f03222021-12-02 11:22:35 +08003207 uint64_t cur = 0;
3208 if (player->ts_cache_len > 0 && pos < 0) {
3209 //this case is open new segment end,but cache data is last segment.
3210 //we need used last segment len to send play time.
3211 cur = 0;
3212 } else {
3213 cur = segment_tell_position_time(player->r_handle, pos);
3214 }
hualing chen21a40372021-10-29 11:07:26 +08003215 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08003216 pthread_mutex_unlock(&player->segment_lock);
Wentao MA96f68962022-06-15 19:45:35 +08003217 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 +08003218 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3219 cache = 0;
3220 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003221 int cur_time = (int)(cur > cache ? cur - cache : 0);
3222 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08003223}
3224
Wentao MAac5ea062022-08-11 11:44:27 +08003225// Get current playback time position of the ongoing segment.
3226// Notice the return value may be negative. This is because previous segment's
3227// data cached in demux buffer need to be considered.
hualing chen969fe7b2021-05-26 15:13:17 +08003228static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
3229 //get cur time of segment
3230 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3231
hualing chen03fd4942021-07-15 15:56:41 +08003232 if (player == NULL || player->handle == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003233 DVR_PB_INFO("player is NULL");
hualing chen969fe7b2021-05-26 15:13:17 +08003234 return DVR_FAILURE;
3235 }
3236
Wentao MA270dc0f2022-08-23 13:17:26 +08003237 int64_t cache = 0;//default es buf cache 500ms
hualing chen969fe7b2021-05-26 15:13:17 +08003238 int cur_time = 0;
hualing chen969fe7b2021-05-26 15:13:17 +08003239 pthread_mutex_lock(&player->segment_lock);
hualing chena5f03222021-12-02 11:22:35 +08003240 loff_t tmp_pos = segment_tell_position(player->r_handle);
3241 loff_t pos = tmp_pos - player->ts_cache_len;
3242 uint64_t cur = 0;
3243 if (player->ts_cache_len > 0 && (tmp_pos < player->ts_cache_len)) {
3244 //this case is open new segment end,but cache data is last segment.
3245 //we need used last segment len to send play time.
3246 cur = 0;
Wentao MA96f68962022-06-15 19:45:35 +08003247 DVR_PB_INFO("change segment [%lld][%lld]",
hualing chen926a8ec2021-12-20 20:38:24 +08003248 player->last_segment_id, player->cur_segment_id);
hualing chena5f03222021-12-02 11:22:35 +08003249 } else {
3250 cur = segment_tell_position_time(player->r_handle, pos);
3251 }
hualing chen21a40372021-10-29 11:07:26 +08003252 AmTsPlayer_getDelayTime(player->handle, &cache);
Wentao MAaf716972021-12-28 13:28:52 +08003253
hualing chen969fe7b2021-05-26 15:13:17 +08003254 pthread_mutex_unlock(&player->segment_lock);
Wentao MA01de0e62022-01-10 18:48:23 +08003255
3256 // The idea here is to work around a weakness of AmTsPlayer_getDelayTime at
3257 // starting phase of a playback in a short period of 20ms or less. During the
3258 // said period, getDelayTime does NOT work as expect to return real cache
3259 // length because demux isn't actually running to provide valid pts to
3260 // TsPlayer. "cache==0" implies the situation that playback is NOT actually
3261 // started. Under such conditions a '0' cache size may NOT reflect actual data
3262 // length remaining in TsPlayer cache, therefore corresponding libdvr 'cur' is
3263 // useless if data in TsPlayer cache is not considered, so it needs to be
Wentao MA270dc0f2022-08-23 13:17:26 +08003264 // reset to a previous valid state. To make the reset operation stricter, extra
Wentao MA01de0e62022-01-10 18:48:23 +08003265 // AmTsPlayer_getPts invocations on both video/audio are introduced to test if
3266 // TsPlayer can get valid pts which indicates the actual running status of
3267 // demux. (JIRA issue: SWPL-68740)
3268 if (player->first_start_id != UINT64_MAX && cache == 0
3269 && player->check_cache_flag == DVR_TRUE ) {
3270 uint64_t pts_a=0;
3271 uint64_t pts_v=0;
3272 AmTsPlayer_getPts(player->handle, TS_STREAM_AUDIO, &pts_a);
3273 AmTsPlayer_getPts(player->handle, TS_STREAM_VIDEO, &pts_v);
3274 if ((int64_t)pts_a <= 0 && (int64_t)pts_v <= 0) {
3275 // Identified the wired situation and just return previous valid state
3276 cur = player->first_start_time;
3277 *id = player->first_start_id;
3278 return cur;
3279 }
3280 }
3281 if (cache != 0) {
3282 // Do NOT permit to enter 'if' code block above any more
3283 player->check_cache_flag=DVR_FALSE;
3284 }
3285
Wentao MA96f68962022-06-15 19:45:35 +08003286 DVR_PB_INFO("***get play cur time [%lld] cache:%lld cur id [%lld]"
3287 " last id [%lld] pb cache len [%d] pos [%lld][%lld]",
hualing chen926a8ec2021-12-20 20:38:24 +08003288 cur,
3289 cache,
3290 player->cur_segment_id,
3291 player->last_send_time_id,
3292 player->ts_cache_len,
3293 pos,
3294 tmp_pos);
hualing chen969fe7b2021-05-26 15:13:17 +08003295 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3296 cache = 0;
3297 }
3298 if (cur > cache) {
3299 cur_time = (int)(cur - cache);
3300 *id = player->cur_segment_id;
Wentao MA270dc0f2022-08-23 13:17:26 +08003301 } else if (player->last_segment_total > 0) {
hualing chend1686e52022-01-05 17:10:42 +08003302 //if at fb mode,we not used last id to replace cur id if cache > cur time.
3303 //this case only used for normal speed or ff speed
3304 if (!IS_FB(player->speed) && player->last_segment_id <= player->cur_segment_id) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003305 if (player->last_segment_total > (cache - cur))
3306 cur_time = (int)(player->last_segment_total - (cache - cur));
hualing chend1686e52022-01-05 17:10:42 +08003307 else
Wentao MA270dc0f2022-08-23 13:17:26 +08003308 cur_time = (int)(player->last_segment_total - cur);
hualing chen8a657f32021-08-30 13:12:49 +08003309
hualing chend1686e52022-01-05 17:10:42 +08003310 *id = player->last_segment_id;
3311 } else {//fb mode
3312 cur_time = (int)(cur);
3313 *id = player->cur_segment_id;
3314 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003315 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 +08003316 } else {
3317 cur_time = 0;
3318 *id = player->cur_segment_id;
3319 }
hualing chen969fe7b2021-05-26 15:13:17 +08003320 return cur_time;
3321}
3322
hualing chencc91e1c2020-02-28 13:26:17 +08003323//get current segment current pcr time of read pos
3324static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
3325 //get cur time of segment
3326 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003327
3328 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003329 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003330 return DVR_FAILURE;
3331 }
3332
hualing chen2aba4022020-03-02 13:49:55 +08003333 pthread_mutex_lock(&player->segment_lock);
3334 uint64_t end = segment_tell_total_time(player->r_handle);
Wentao MA96f68962022-06-15 19:45:35 +08003335 DVR_PB_INFO("get total time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08003336 pthread_mutex_unlock(&player->segment_lock);
3337 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08003338}
3339
hualing chen03fd4942021-07-15 15:56:41 +08003340DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
3341{
3342 //check is set limit info
3343 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3344
3345 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003346 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003347 return DVR_FALSE;
3348 }
3349 if (player->rec_start > 0 || player->limit > 0) {
3350 return DVR_TRUE;
3351 }
3352 return DVR_FALSE;
3353}
3354
3355/**\brief set DVR playback calculate expired time len
3356 * \param[in] handle, DVR playback session handle
3357 * \return DVR_SUCCESS on success
3358 * \return error code on failure
3359 */
hualing chen7ea70a72021-09-09 11:25:13 +08003360uint32_t dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
hualing chen03fd4942021-07-15 15:56:41 +08003361{
3362 //calculate expired time to play
3363 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08003364 uint32_t cur_time;
3365 uint32_t tmp_time;
3366 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003367 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003368 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003369 return expired;
3370 }
hualing chen7ea70a72021-09-09 11:25:13 +08003371 if (player->rec_start == 0 || player->limit == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003372 DVR_PB_INFO("rec limit 0");
hualing chen03fd4942021-07-15 15:56:41 +08003373 return expired;
3374 }
3375 //get system time
hualing chen7ea70a72021-09-09 11:25:13 +08003376 cur_time = _dvr_getClock_sec();
3377 if ((cur_time - player->rec_start) > player->limit) {
3378 tmp_time = (uint32_t)((cur_time - player->rec_start) - player->limit) * 1000U;
3379 expired = *(int*)&tmp_time;
Wentao MA96f68962022-06-15 19:45:35 +08003380 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 +08003381 cur_time,
3382 player->rec_start,
3383 player->limit,
hualing chen7ea70a72021-09-09 11:25:13 +08003384 (uint32_t)(cur_time - player->rec_start - player->limit), expired, tmp_time);
3385 }
hualing chen03fd4942021-07-15 15:56:41 +08003386 return expired;
3387}
3388
3389/**\brief set DVR playback obsolete time
3390 * \param[in] handle, DVR playback session handle
3391 * \param[in] obsolete, obsolete len
3392 * \return DVR_SUCCESS on success
3393 * \return error code on failure
3394 */
3395int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
3396{
3397 int expired = 0;
3398 //calculate expired time to play
3399 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3400
3401 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003402 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003403 return DVR_FALSE;
3404 }
3405 //get system time
Wentao MA96f68962022-06-15 19:45:35 +08003406 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003407 dvr_mutex_lock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003408 player->obsolete = obsolete;
Wentao MA96f68962022-06-15 19:45:35 +08003409 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003410 dvr_mutex_unlock(&player->lock);
hualing chen03fd4942021-07-15 15:56:41 +08003411 return expired;
3412}
3413
3414/**\brief update DVR playback newest segment duration
3415 * \param[in] handle, DVR playback session handle
3416 * \param[in] segmentid, newest segment id
3417 * \param[in] dur dur time ms
3418 * \return DVR_SUCCESS on success
3419 * \return error code on failure
3420 */
3421int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
3422uint64_t segmentid, int dur)
3423{
3424 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3425 DVR_PlaybackSegmentInfo_t *segment;
3426 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
3427
3428 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003429 DVR_PB_INFO(" player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003430 return DVR_FAILURE;
3431 }
3432 //update the newest segment duration on timeshift mode
3433 list_for_each_entry(segment, &player->segment_list, head)
3434 {
3435 if (segment->segment_id == segmentid) {
3436 segment->duration = dur;
3437 break;
3438 }
3439 pre_segment = segment;
3440 }
3441
3442 return DVR_SUCCESS;
3443}
3444
hualing chen7ea70a72021-09-09 11:25:13 +08003445static uint32_t dvr_playback_calculate_last_valid_segment(
3446 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos)
hualing chen03fd4942021-07-15 15:56:41 +08003447{
hualing chen7ea70a72021-09-09 11:25:13 +08003448 uint32_t off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003449 uint64_t segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003450 uint32_t pre_off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003451 uint64_t last_segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003452 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003453 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3454
3455 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003456 DVR_PB_INFO("player is NULL");
hualing chen03fd4942021-07-15 15:56:41 +08003457 return DVR_FAILURE;
3458 }
3459 expired = dvr_playback_calculate_expiredlen(handle);
hualing chen7e14e532021-09-23 11:23:28 +08003460 if (expired == 0) {
3461 *segmentid = player->cur_segment_id;
3462 *pos = 0;
3463 return DVR_SUCCESS;
3464 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003465 DVR_PlaybackSegmentInfo_t *p_seg;
3466 list_for_each_entry_reverse(p_seg, &player->segment_list, head) {
3467 segment_id = p_seg->segment_id;
hualing chen03fd4942021-07-15 15:56:41 +08003468
Wentao MA270dc0f2022-08-23 13:17:26 +08003469 if ((player->obsolete + pre_off + p_seg->duration) > expired)
hualing chen03fd4942021-07-15 15:56:41 +08003470 break;
3471
Wentao MA270dc0f2022-08-23 13:17:26 +08003472 last_segment_id = p_seg->segment_id;
3473 pre_off += p_seg->duration;
hualing chen03fd4942021-07-15 15:56:41 +08003474 }
3475
3476 if (last_segment_id == segment_id) {
3477 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
3478 off = expired;
3479 } else if (player->obsolete >= expired) {
3480 off = 0;
3481 } else {
3482 off = expired - pre_off - player->obsolete;
3483 }
3484 *segmentid = segment_id;
3485 *pos = off;
3486 return DVR_SUCCESS;
3487}
3488
hualing chen4b7c15d2020-04-07 16:13:48 +08003489#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003490//start replay
3491static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3492
3493 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3494 //calculate pcr seek time
3495 int t_diff = 0;
3496 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003497 uint64_t segmentid = 0;
3498 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003499 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003500 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003501 return DVR_FAILURE;
3502 }
3503
hualing chen5cbe1a62020-02-10 16:36:36 +08003504 if (player->fffb_start == -1) {
3505 //set fffb start time ms
3506 player->fffb_start = _dvr_time_getClock();
3507 player->fffb_current = player->fffb_start;
3508 //get segment current time pos
3509 player->fffb_start_pcr = _dvr_get_cur_time(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003510 DVR_PB_INFO("calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
hualing chen03fd4942021-07-15 15:56:41 +08003511 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003512 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003513 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003514 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003515 } else {
3516 player->fffb_current = _dvr_time_getClock();
3517 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003518 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003519 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003520 if (seek_time <= 0) {
3521 //need seek to pre one segment
3522 seek_time = 0;
3523 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003524 //seek segment pos
3525 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003526 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003527 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003528 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3529 //set seek time to 0;
Wentao MA96f68962022-06-15 19:45:35 +08003530 DVR_PB_INFO("segment seek to 0 at fb mode [%d]id[%lld]",
hualing chen03fd4942021-07-15 15:56:41 +08003531 seek_time,
3532 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003533 seek_time = 0;
3534 }
hualing chen03fd4942021-07-15 15:56:41 +08003535 if (IS_FB(player->speed)
3536 && dvr_playback_check_limit(handle)) {
3537 //fb case.check expired time
3538 //get id and pos to check if we can seek to this pos
3539 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3540 //case cur id < segment id
3541 if (player->cur_segment_id < segmentid) {
3542 //expired ts data is player,return error
3543 //
3544 pthread_mutex_unlock(&player->segment_lock);
3545 return 0;
3546 } else if (player->cur_segment_id == segmentid) {
3547 //id is same,compare seek pos
3548 if (seek_time < pos) {
3549 //expired ts data is player,return error
3550 //
3551 pthread_mutex_unlock(&player->segment_lock);
3552 return 0;
3553 }
3554 }
3555 //case can play
3556 }
hualing chen041c4092020-04-05 15:11:50 +08003557 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3558 seek_time = 0;
3559 }
hualing chen2aba4022020-03-02 13:49:55 +08003560 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003561 } else {
3562 //
Wentao MA96f68962022-06-15 19:45:35 +08003563 DVR_PB_INFO("segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003564 }
Wentao MA96f68962022-06-15 19:45:35 +08003565 DVR_PB_INFO("calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003566 seek_time,
3567 player->speed,
3568 player->cur_segment_id,
3569 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003570 }
hualing chen2aba4022020-03-02 13:49:55 +08003571 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003572}
3573
3574
3575//start replay
3576static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3577 //
3578 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003579
3580 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003581 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003582 return DVR_FAILURE;
3583 }
3584
hualing chen5cbe1a62020-02-10 16:36:36 +08003585 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003586 if (player->has_video) {
Wentao MA96f68962022-06-15 19:45:35 +08003587 DVR_PB_INFO("fffb stop video");
hualing chen21a40372021-10-29 11:07:26 +08003588 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003589 AmTsPlayer_stopVideoDecoding(player->handle);
3590 }
3591 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003592 DVR_PB_INFO("fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003593 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003594 AmTsPlayer_stopAudioDecoding(player->handle);
3595 }
hualing chendf118dd2020-05-21 15:49:11 +08003596 if (player->has_ad_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003597 DVR_PB_INFO("fffb stop audio");
hualing chendf118dd2020-05-21 15:49:11 +08003598 player->has_ad_audio =DVR_FALSE;
3599 AmTsPlayer_disableADMix(player->handle);
3600 }
hualing chen2aba4022020-03-02 13:49:55 +08003601
hualing chen5cbe1a62020-02-10 16:36:36 +08003602 //start video and audio
3603
Wentao MA270dc0f2022-08-23 13:17:26 +08003604 am_tsplayer_video_params video_params;
3605 am_tsplayer_audio_params audio_params;
3606 am_tsplayer_audio_params ad_params;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003607
Wentao MA270dc0f2022-08-23 13:17:26 +08003608 memset(&video_params, 0, sizeof(video_params));
3609 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003610
hualing chen87072a82020-03-12 16:20:12 +08003611 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003612
3613 //get segment info and audio video pid fmt ;
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003614 //dvr_mutex_lock(&player->lock);
Wentao MA270dc0f2022-08-23 13:17:26 +08003615 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003616 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003617 if (!VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
3618 //audio and video pids are all invalid, return error.
Wentao MA96f68962022-06-15 19:45:35 +08003619 DVR_PB_ERROR("dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003620 return -1;
3621 }
3622
Wentao MA270dc0f2022-08-23 13:17:26 +08003623 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003624 player->has_video = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003625 DVR_PB_INFO("fffb start video");
3626 //DVR_PB_INFO("fffb start video and save last frame");
hualing chen0888c032020-12-18 17:54:57 +08003627 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003628 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003629 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
Wentao MA270dc0f2022-08-23 13:17:26 +08003630 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003631 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003632 AmTsPlayer_startVideoDecoding(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08003633 //playback_device_video_start(player->handle , &video_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003634 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003635 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003636 }
hualing chen31140872020-03-25 12:29:26 +08003637 //fffb mode need stop fast;
Wentao MA96f68962022-06-15 19:45:35 +08003638 DVR_PB_INFO("stop fast");
hualing chen31140872020-03-25 12:29:26 +08003639 AmTsPlayer_stopFast(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003640 return 0;
3641}
3642
3643static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3644 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003645 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003646 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003647 return DVR_FAILURE;
3648 }
3649
3650 player->first_frame = 0;
Wentao MA96f68962022-06-15 19:45:35 +08003651 DVR_PB_INFO("lock speed [%f]", player->speed);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003652 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003653
hualing chen2aba4022020-03-02 13:49:55 +08003654 int seek_time = _dvr_playback_calculate_seekpos(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003655 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 +08003656
hualing chen87072a82020-03-12 16:20:12 +08003657 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3658 //seek time set 0
3659 seek_time = 0;
3660 }
hualing chen041c4092020-04-05 15:11:50 +08003661 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003662 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3663 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003664 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003665 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003666 DVR_PB_DEBUG("unlock");
hualing chen87072a82020-03-12 16:20:12 +08003667 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003668 //send event here and pause
3669 DVR_Play_Notify_t notify;
3670 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003671 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003672 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003673 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
Wentao MA96f68962022-06-15 19:45:35 +08003674 DVR_PB_INFO("*******************send begin event speed [%f] cur [%d]", player->speed, _dvr_get_cur_time(handle));
hualing chen2aba4022020-03-02 13:49:55 +08003675 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003676 return DVR_SUCCESS;
3677 }
hualing chen2932d372020-04-29 13:44:00 +08003678 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003679 _dvr_init_fffb_time(handle);
Wentao MA96f68962022-06-15 19:45:35 +08003680 DVR_PB_INFO("*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003681 }
3682 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003683 _dvr_playback_fffb_replay(handle);
3684
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003685 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003686 DVR_PB_DEBUG("unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003687
hualing chen5cbe1a62020-02-10 16:36:36 +08003688 return DVR_SUCCESS;
3689}
3690
hualing chen87072a82020-03-12 16:20:12 +08003691//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003692static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003693 //
3694 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003695
3696 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003697 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003698 return DVR_FAILURE;
3699 }
3700
hualing chen5cbe1a62020-02-10 16:36:36 +08003701 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003702 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003703 player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003704 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003705 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003706 }
3707
3708 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003709 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003710 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003711 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003712 //start video and audio
3713
Wentao MA270dc0f2022-08-23 13:17:26 +08003714 am_tsplayer_video_params video_params;
3715 am_tsplayer_audio_params audio_params;
3716 am_tsplayer_audio_params ad_params;
hualing chen87072a82020-03-12 16:20:12 +08003717 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003718
Wentao MA270dc0f2022-08-23 13:17:26 +08003719 memset(&video_params, 0, sizeof(video_params));
3720 memset(&audio_params, 0, sizeof(audio_params));
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003721
hualing chen5cbe1a62020-02-10 16:36:36 +08003722 //get segment info and audio video pid fmt ;
Wentao MA96f68962022-06-15 19:45:35 +08003723 DVR_PB_INFO("into");
Wentao MA270dc0f2022-08-23 13:17:26 +08003724 _dvr_playback_get_playinfo(handle, segment_id, &video_params, &audio_params, &ad_params);
hualing chen5cbe1a62020-02-10 16:36:36 +08003725 //start audio and video
Wentao MA270dc0f2022-08-23 13:17:26 +08003726 if (!VALID_PID(video_params.pid) && !VALID_PID(audio_params.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003727 //audio and video pis is all invalid, return error.
Wentao MA96f68962022-06-15 19:45:35 +08003728 DVR_PB_ERROR("dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003729 return -1;
3730 }
3731
Wentao MA270dc0f2022-08-23 13:17:26 +08003732 if (VALID_PID(video_params.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003733 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003734 if (trick == DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08003735 DVR_PB_INFO("settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003736 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003737 }
hualing chen266b9502020-04-04 17:39:39 +08003738 else {
hualing chen2aba4022020-03-02 13:49:55 +08003739 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003740 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003741 AmTsPlayer_setVideoParams(player->handle, &video_params);
hualing chen21a40372021-10-29 11:07:26 +08003742 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003743 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003744 }
hualing chena540a7e2020-03-27 16:44:05 +08003745
3746 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
Wentao MA96f68962022-06-15 19:45:35 +08003747 DVR_PB_INFO("start fast");
hualing chen31140872020-03-25 12:29:26 +08003748 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003749 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003750 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08003751 if (VALID_PID(ad_params.pid)) {
hualing chendf118dd2020-05-21 15:49:11 +08003752 player->has_ad_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003753 DVR_PB_INFO("start ad audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003754 AmTsPlayer_setADParams(player->handle, &ad_params);
hualing chendf118dd2020-05-21 15:49:11 +08003755 AmTsPlayer_enableADMix(player->handle);
3756 }
Wentao MA270dc0f2022-08-23 13:17:26 +08003757 if (VALID_PID(audio_params.pid)) {
hualing chen969fe7b2021-05-26 15:13:17 +08003758 player->has_audio = DVR_TRUE;
Wentao MA96f68962022-06-15 19:45:35 +08003759 DVR_PB_INFO("start audio");
Wentao MA270dc0f2022-08-23 13:17:26 +08003760 AmTsPlayer_setAudioParams(player->handle, &audio_params);
Wentao MA5629ad82022-08-24 10:03:02 +08003761 if (player->audio_presentation_id > -1) {
3762 AmTsPlayer_setParams(player->handle, AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &player->audio_presentation_id);
3763 }
hualing chen969fe7b2021-05-26 15:13:17 +08003764 AmTsPlayer_startAudioDecoding(player->handle);
3765 }
hualing chendf118dd2020-05-21 15:49:11 +08003766
Wentao MA96f68962022-06-15 19:45:35 +08003767 DVR_PB_INFO("stop fast");
hualing chen31140872020-03-25 12:29:26 +08003768 AmTsPlayer_stopFast(player->handle);
3769 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3770 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3771 }
hualing chen43a89bc2022-01-19 14:31:20 +08003772#ifdef AVSYNC_USED_PCR
3773 if (player && VALID_PID(player->cur_segment.pids.pcr.pid)) {
Wentao MA96f68962022-06-15 19:45:35 +08003774 DVR_PB_INFO("start set pcr [%d]", player->cur_segment.pids.pcr.pid);
hualing chen43a89bc2022-01-19 14:31:20 +08003775 AmTsPlayer_setPcrPid(player->handle, player->cur_segment.pids.pcr.pid);
3776 }
3777#endif
hualing chen2aba4022020-03-02 13:49:55 +08003778 player->cmd.last_cmd = player->cmd.cur_cmd;
3779 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003780 player->cmd.state = DVR_PLAYBACK_STATE_START;
Wentao MA907b6432022-08-01 06:23:08 +00003781 DVR_PLAYER_CHANGE_STATE(player,DVR_PLAYBACK_STATE_START);
hualing chen5cbe1a62020-02-10 16:36:36 +08003782 return 0;
3783}
3784
3785
hualing chenb31a6c62020-01-13 17:27:00 +08003786/**\brief Set play speed
3787 * \param[in] handle playback handle
3788 * \param[in] speed playback speed
3789 * \retval DVR_SUCCESS On success
3790 * \return Error code
3791 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003792int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3793
3794 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003795
3796 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003797 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003798 return DVR_FAILURE;
3799 }
3800
hualing chena540a7e2020-03-27 16:44:05 +08003801 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08003802 DVR_PB_INFO(" func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003803 return DVR_FAILURE;
3804 }
hualing chenf00cdc82020-06-10 14:23:35 +08003805 if (speed.speed.speed == player->cmd.speed.speed.speed) {
Wentao MA96f68962022-06-15 19:45:35 +08003806 DVR_PB_INFO(" func: eq speed [%d]", speed.speed.speed);
hualing chenf00cdc82020-06-10 14:23:35 +08003807 return DVR_SUCCESS;
3808 }
Wentao MA96f68962022-06-15 19:45:35 +08003809 DVR_PB_INFO("lock func: speed [%d]", speed.speed.speed);
hualing chen1679f812021-11-08 15:17:46 +08003810
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003811 dvr_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003812 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3813 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3814 player->cmd.last_cmd = player->cmd.cur_cmd;
3815 }
hualing chene41f4372020-06-06 16:29:17 +08003816
hualing chen31140872020-03-25 12:29:26 +08003817 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003818 IS_KERNEL_SPEED(speed.speed.speed) ) {
3819 //case 1. not start play.only set speed
3820 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3821 //only set speed.and return;
3822 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3823 player->cmd.speed.speed = speed.speed;
3824 player->speed = (float)speed.speed.speed/(float)100;
3825 player->fffb_play = DVR_FALSE;
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003826 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003827 DVR_PB_DEBUG("unlock");
hualing chenf00cdc82020-06-10 14:23:35 +08003828 return DVR_SUCCESS;
3829 }
3830 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003831 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3832 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003833 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003834 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3835 // resume audio and stop fast play
Wentao MA96f68962022-06-15 19:45:35 +08003836 DVR_PB_INFO("stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003837 AmTsPlayer_stopFast(player->handle);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003838 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08003839 DVR_PB_DEBUG("unlock ---\r\n");
Wentao MA270dc0f2022-08-23 13:17:26 +08003840 _dvr_cmd(handle, DVR_PLAYBACK_CMD_A_START);
Wentao MA96f68962022-06-15 19:45:35 +08003841 DVR_PB_DEBUG("lock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003842 dvr_mutex_lock(&player->lock);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003843 } else {
3844 //set play speed and if audio is start, stop audio.
3845 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003846 DVR_PB_INFO("fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003847 AmTsPlayer_stopAudioDecoding(player->handle);
3848 player->has_audio = DVR_FALSE;
3849 }
Wentao MA96f68962022-06-15 19:45:35 +08003850 DVR_PB_INFO("start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003851 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003852 }
hualing chenbcada022020-04-22 14:27:01 +08003853 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003854 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003855 player->cmd.speed.speed = speed.speed;
3856 player->speed = (float)speed.speed.speed/(float)100;
Wentao MA96f68962022-06-15 19:45:35 +08003857 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003858 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003859 return DVR_SUCCESS;
3860 }
hualing chen31140872020-03-25 12:29:26 +08003861 //case 3 fffb mode
3862 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3863 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3864 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003865 DVR_PB_INFO("set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003866 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003867 player->cmd.speed.speed = speed.speed;
3868 player->speed = (float)speed.speed.speed/(float)100;
3869 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003870 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003871 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003872 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003873 return DVR_SUCCESS;
3874 }
3875 }
3876 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003877 IS_KERNEL_SPEED(speed.speed.speed)) {
3878 //case 1. cur speed is kernel support speed,set kernel speed.
3879 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003880 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003881 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3882 // resume audio and stop fast play
Wentao MA96f68962022-06-15 19:45:35 +08003883 DVR_PB_INFO("stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003884 AmTsPlayer_stopFast(player->handle);
Wentao MA270dc0f2022-08-23 13:17:26 +08003885 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_A_START;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003886 } else {
3887 //set play speed and if audio is start, stop audio.
3888 if (player->has_audio) {
Wentao MA96f68962022-06-15 19:45:35 +08003889 DVR_PB_INFO("fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003890 AmTsPlayer_stopAudioDecoding(player->handle);
3891 player->has_audio = DVR_FALSE;
3892 }
Wentao MA96f68962022-06-15 19:45:35 +08003893 DVR_PB_INFO("start fast");
hualing chenf00cdc82020-06-10 14:23:35 +08003894 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003895 }
hualing chena540a7e2020-03-27 16:44:05 +08003896 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003897 player->cmd.speed.speed = speed.speed;
3898 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003899 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003900 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003901 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003902 return DVR_SUCCESS;
3903 }
3904 //case 2 fffb mode
3905 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3906 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3907 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003908 DVR_PB_INFO("set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003909 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003910 player->cmd.speed.speed = speed.speed;
3911 player->speed = (float)speed.speed.speed/(float)100;
Wentao MA270dc0f2022-08-23 13:17:26 +08003912 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
hualing chenbcada022020-04-22 14:27:01 +08003913 player->fffb_play = DVR_FALSE;
Wentao MA96f68962022-06-15 19:45:35 +08003914 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003915 dvr_mutex_unlock(&player->lock);
hualing chen31140872020-03-25 12:29:26 +08003916 return DVR_SUCCESS;
3917 }
hualing chen31140872020-03-25 12:29:26 +08003918 }
hualing chena540a7e2020-03-27 16:44:05 +08003919 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3920 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003921 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003922 } else {
hualing chen31140872020-03-25 12:29:26 +08003923 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003924 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3925 else
3926 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003927 player->fffb_play = DVR_TRUE;
3928 }
3929 DVR_Bool_t init_last_time = DVR_FALSE;
3930 if (player->speed > 0.0f && speed.speed.speed < 0) {
3931 init_last_time = DVR_TRUE;
3932 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3933 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003934 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003935 player->cmd.speed.mode = speed.mode;
3936 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003937 player->speed = (float)speed.speed.speed/(float)100;
3938 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003939 _dvr_init_fffb_t(handle);
3940 if (init_last_time == DVR_TRUE)
3941 player->last_send_time_id = UINT64_MAX;
3942
hualing chen87072a82020-03-12 16:20:12 +08003943 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003944 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3945 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003946 //restart play at normal speed exit ff fb
Wentao MA96f68962022-06-15 19:45:35 +08003947 DVR_PB_INFO("set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003948 _dvr_playback_replay(handle, DVR_FALSE);
3949 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3950 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
Wentao MA270dc0f2022-08-23 13:17:26 +08003951 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AV_RESTART;
Wentao MA96f68962022-06-15 19:45:35 +08003952 DVR_PB_INFO("set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003953 }
Wentao MA96f68962022-06-15 19:45:35 +08003954 DVR_PB_INFO("unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003955 dvr_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003956 return DVR_SUCCESS;
3957}
hualing chen2932d372020-04-29 13:44:00 +08003958
hualing chenb31a6c62020-01-13 17:27:00 +08003959/**\brief Get playback status
3960 * \param[in] handle playback handle
3961 * \param[out] p_status playback status
3962 * \retval DVR_SUCCESS On success
3963 * \return Error code
3964 */
hualing chen2932d372020-04-29 13:44:00 +08003965static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3966 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003967//
3968 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003969 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003970 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08003971 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003972 return DVR_FAILURE;
3973 }
hualing chen1679f812021-11-08 15:17:46 +08003974 if (is_lock ==DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08003975 DVR_PB_DEBUG("lock");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08003976 dvr_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003977 }
3978
hualing chen5cbe1a62020-02-10 16:36:36 +08003979 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003980 //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 +08003981 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3982 player->state == DVR_PLAYBACK_STATE_START) {
3983 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3984 }
hualing chen041c4092020-04-05 15:11:50 +08003985
hualing chencc91e1c2020-02-28 13:26:17 +08003986 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003987 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003988
3989 if (CONTROL_SPEED_ENABLE == 1) {
hualing chen7ea70a72021-09-09 11:25:13 +08003990 if (player->con_spe.ply_sta == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08003991 DVR_PB_INFO("player dur[%u] sta[%u] cur[%d] -----reinit",
hualing chen03fd4942021-07-15 15:56:41 +08003992 player->con_spe.ply_dur,
3993 player->con_spe.ply_sta,
3994 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003995 player->con_spe.ply_sta = p_status->time_cur;
3996 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3997 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
Wentao MA96f68962022-06-15 19:45:35 +08003998 DVR_PB_INFO("player dur[%u] sta[%u] cur[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003999 player->con_spe.ply_dur,
4000 player->con_spe.ply_sta,
4001 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08004002 player->con_spe.ply_sta = p_status->time_cur;
4003 }
4004
4005 if (player->con_spe.sys_sta == 0) {
4006 player->con_spe.sys_sta = _dvr_time_getClock();
4007 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
4008 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
4009 player->con_spe.sys_sta = _dvr_time_getClock();
4010 }
4011 }
4012
hualing chen4b7c15d2020-04-07 16:13:48 +08004013 if (player->last_send_time_id == UINT64_MAX) {
4014 player->last_send_time_id = player->cur_segment_id;
4015 player->last_cur_time = p_status->time_cur;
4016 }
4017 if (player->last_send_time_id == player->cur_segment_id) {
4018 if (player->speed > 0.0f ) {
4019 //ff
4020 if (p_status->time_cur < player->last_cur_time ) {
Wentao MA96f68962022-06-15 19:45:35 +08004021 DVR_PB_INFO("get ff time error last[%d]cur[%d]diff[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08004022 player->last_cur_time,
4023 p_status->time_cur,
4024 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08004025 p_status->time_cur = player->last_cur_time;
4026 } else {
4027 player->last_cur_time = p_status->time_cur;
4028 }
hualing chene41f4372020-06-06 16:29:17 +08004029 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08004030 //fb
4031 if (p_status->time_cur > player->last_cur_time ) {
Wentao MA96f68962022-06-15 19:45:35 +08004032 DVR_PB_INFO("get fb time error last[%d]cur[%d]diff[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08004033 player->last_cur_time,
4034 p_status->time_cur,
4035 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08004036 p_status->time_cur = player->last_cur_time;
4037 } else {
4038 player->last_cur_time = p_status->time_cur;
4039 }
4040 }
hualing chend241c7a2021-06-22 13:34:27 +08004041 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08004042 player->last_cur_time = p_status->time_cur;
4043 }
hualing chen969fe7b2021-05-26 15:13:17 +08004044 player->last_send_time_id = segment_id;
4045 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08004046
hualing chen5cbe1a62020-02-10 16:36:36 +08004047 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08004048 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08004049 p_status->flags = player->cur_segment.flags;
Wentao MA96f68962022-06-15 19:45:35 +08004050 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 +08004051 _dvr_playback_state_toString(player->state),
4052 _dvr_playback_state_toString(p_status->state),
4053 p_status->time_cur, p_status->time_end,
4054 p_status->segment_id,player->play_flag,
4055 player->speed,
4056 is_lock);
hualing chen1679f812021-11-08 15:17:46 +08004057 if (is_lock ==DVR_TRUE) {
Wentao MA96f68962022-06-15 19:45:35 +08004058 DVR_PB_DEBUG("unlock ---\r\n");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004059 dvr_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08004060 }
hualing chen2932d372020-04-29 13:44:00 +08004061 return DVR_SUCCESS;
4062}
4063
4064
4065/**\brief Get playback status
4066 * \param[in] handle playback handle
4067 * \param[out] p_status playback status
4068 * \retval DVR_SUCCESS On success
4069 * \return Error code
4070 */
4071int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
4072 DVR_PlaybackStatus_t *p_status) {
4073//
4074 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4075
Zhiqiang Han9adc9722020-11-11 18:38:10 +08004076 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
4077
hualing chen2932d372020-04-29 13:44:00 +08004078 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004079 DVR_PB_INFO("player is NULL");
hualing chen2932d372020-04-29 13:44:00 +08004080 return DVR_FAILURE;
4081 }
Wentao MA96f68962022-06-15 19:45:35 +08004082 DVR_PB_DEBUG("lock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004083 dvr_mutex_lock(&player->lock);
Zhiqiang Han9adc9722020-11-11 18:38:10 +08004084 if (!player->has_video && !player->has_audio)
4085 p_status->time_cur = 0;
Wentao MA96f68962022-06-15 19:45:35 +08004086 DVR_PB_DEBUG("unlock---");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004087 dvr_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08004088
hualing chenb31a6c62020-01-13 17:27:00 +08004089 return DVR_SUCCESS;
4090}
4091
hualing chen040df222020-01-17 13:35:02 +08004092void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
4093 if (segment != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004094 DVR_PB_INFO("segment id: %lld", segment->segment_id);
4095 DVR_PB_INFO("segment flag: %d", segment->flags);
4096 DVR_PB_INFO("segment location: [%s]", segment->location);
4097 DVR_PB_INFO("segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
4098 DVR_PB_INFO("segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
4099 DVR_PB_INFO("segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
4100 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 +08004101 }
hualing chenb31a6c62020-01-13 17:27:00 +08004102}
4103
hualing chen5cbe1a62020-02-10 16:36:36 +08004104int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08004105 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08004106
hualing chena540a7e2020-03-27 16:44:05 +08004107 if (player == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08004108 DVR_PB_INFO("player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08004109 return DVR_FAILURE;
4110 }
4111
hualing chen040df222020-01-17 13:35:02 +08004112 DVR_PlaybackSegmentInfo_t *segment;
4113 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08004114 {
hualing chen040df222020-01-17 13:35:02 +08004115 if (segment_id >= 0) {
4116 if (segment->segment_id == segment_id) {
4117 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08004118 break;
4119 }
4120 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08004121 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08004122 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08004123 }
4124 }
4125 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08004126}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004127
pengfei.liu27cc4ec2020-04-03 16:28:16 +08004128int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004129{
4130 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4131 DVR_RETURN_IF_FALSE(player);
4132 DVR_RETURN_IF_FALSE(func);
4133
Wentao MA96f68962022-06-15 19:45:35 +08004134 DVR_PB_INFO("in ");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004135 dvr_mutex_lock(&player->lock);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004136
4137 player->dec_func = func;
4138 player->dec_userdata = userdata;
4139
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004140 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08004141 DVR_PB_INFO("out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004142 return DVR_SUCCESS;
4143}
4144
4145int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
4146{
4147 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4148 DVR_RETURN_IF_FALSE(player);
4149 DVR_RETURN_IF_FALSE(p_secure_buf);
4150 DVR_RETURN_IF_FALSE(len);
4151
Wentao MA96f68962022-06-15 19:45:35 +08004152 DVR_PB_INFO("in ");
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004153 dvr_mutex_lock(&player->lock);
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004154
4155 player->is_secure_mode = 1;
4156 player->secure_buffer = p_secure_buf;
4157 player->secure_buffer_size = len;
4158
Zhiqiang Hanf9c0e272022-06-14 13:54:03 +08004159 dvr_mutex_unlock(&player->lock);
Wentao MA96f68962022-06-15 19:45:35 +08004160 DVR_PB_INFO("out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08004161 return DVR_SUCCESS;
4162}
Wentao MA5629ad82022-08-24 10:03:02 +08004163
4164int dvr_playback_set_ac4_preselection_id(DVR_PlaybackHandle_t handle, int presel_id)
4165{
4166 DVR_Playback_t *player = (DVR_Playback_t *) handle;
4167 DVR_RETURN_IF_FALSE(player == NULL);
4168
4169 player->audio_presentation_id = presel_id;
4170 am_tsplayer_result ret = AmTsPlayer_setParams(player->handle,
4171 AM_TSPLAYER_KEY_AUDIO_PRESENTATION_ID, &presel_id);
4172 DVR_RETURN_IF_FALSE(ret == AM_TSPLAYER_OK);
4173
4174 return DVR_SUCCESS;
4175}