blob: 58c9b10c56249c929448c73c4b08dd17a87e69a0 [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"
18
Gong Ke2a0ebbe2021-05-25 15:22:50 +080019#define DVR_PB_DG(_level, _fmt...) \
20 DVR_DEBUG_FL(_level, "playback", _fmt)
hualing chena540a7e2020-03-27 16:44:05 +080021
hualing chenb31a6c62020-01-13 17:27:00 +080022#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080023
hualing chend241c7a2021-06-22 13:34:27 +080024#define CONTROL_SPEED_ENABLE 0
hualing chena540a7e2020-03-27 16:44:05 +080025
26#define FF_SPEED (2.0f)
27#define FB_SPEED (-1.0f)
28#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080029#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080030
31#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))
32#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
33
hualing chenb31a6c62020-01-13 17:27:00 +080034
hualing chenb5cd42e2020-04-15 17:03:34 +080035#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080036#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080037//if tsplayer delay time < 200 and no data can read, we will pause
38#define MIN_TSPLAYER_DELAY_TIME (200)
39
hualing chen041c4092020-04-05 15:11:50 +080040#define MAX_CACHE_TIME (30000)
41
hualing chena540a7e2020-03-27 16:44:05 +080042static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080043//
44static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chen275379e2021-06-15 17:57:21 +080045static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_PlaybackPids_t pids, int type);
hualing chencc91e1c2020-02-28 13:26:17 +080046static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
47static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080048static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080049static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080050static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
51 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080052static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen03fd4942021-07-15 15:56:41 +080053static int dvr_playback_calculate_last_valid_segment(
54 DVR_PlaybackHandle_t handle, uint64_t *segmentid, int *pos);
hualing chen87072a82020-03-12 16:20:12 +080055
hualing chenbcada022020-04-22 14:27:01 +080056
57static char* _cmd_toString(int cmd)
58{
59
60 char *string[DVR_PLAYBACK_CMD_NONE+1]={
61 "start",
62 "stop",
63 "vstart",
64 "astart",
65 "vstop",
66 "astop",
67 "vrestart",
68 "arestart",
69 "avrestart",
70 "vstopastart",
71 "astopvstart",
72 "vstoparestart",
73 "astopvrestart",
74 "vstartarestart",
75 "astartvrestart",
76 "pause",
77 "resume",
78 "seek",
79 "ff",
80 "fb",
81 "NONE"
82 };
83
84 if (cmd > DVR_PLAYBACK_CMD_NONE) {
85 return "unkown";
86 } else {
87 return string[cmd];
88 }
89}
90
91
hualing chen6d24aa92020-03-23 18:43:47 +080092static char* _dvr_playback_state_toString(int stat)
93{
94 char *string[DVR_PLAYBACK_STATE_FB+1]={
95 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080096 "stop",
hualing chen31140872020-03-25 12:29:26 +080097 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +080098 "ff",
99 "fb"
100 };
101
102 if (stat > DVR_PLAYBACK_STATE_FB) {
103 return "unkown";
104 } else {
105 return string[stat];
106 }
107}
hualing chena540a7e2020-03-27 16:44:05 +0800108
109static DVR_Bool_t _dvr_support_speed(int speed) {
110
111 DVR_Bool_t ret = DVR_FALSE;
112
113 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800114 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800115 case PLAYBACK_SPEED_FBX2:
116 case PLAYBACK_SPEED_FBX4:
117 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800118 case PLAYBACK_SPEED_FBX16:
119 case PLAYBACK_SPEED_FBX12:
120 case PLAYBACK_SPEED_FBX32:
121 case PLAYBACK_SPEED_FBX48:
122 case PLAYBACK_SPEED_FBX64:
123 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800124 case PLAYBACK_SPEED_S2:
125 case PLAYBACK_SPEED_S4:
126 case PLAYBACK_SPEED_S8:
127 case PLAYBACK_SPEED_X1:
128 case PLAYBACK_SPEED_X2:
129 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800130 case PLAYBACK_SPEED_X3:
131 case PLAYBACK_SPEED_X5:
132 case PLAYBACK_SPEED_X6:
133 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800134 case PLAYBACK_SPEED_X8:
135 case PLAYBACK_SPEED_X12:
136 case PLAYBACK_SPEED_X16:
137 case PLAYBACK_SPEED_X32:
138 case PLAYBACK_SPEED_X48:
139 case PLAYBACK_SPEED_X64:
140 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800141 ret = DVR_TRUE;
142 break;
143 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800144 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800145 break;
146 }
147 return ret;
148}
hualing chen6e4bfa52020-03-13 14:37:11 +0800149void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
150{
hualing chen4b7c15d2020-04-07 16:13:48 +0800151 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800152 DVR_Playback_t *player = NULL;
153 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800154 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800155 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800156 }
157 switch (event->type) {
158 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
159 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800160 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800161 event->event.video_format.frame_width,
162 event->event.video_format.frame_height,
163 event->event.video_format.frame_rate);
164 break;
165 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800166 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
167 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800168 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800169 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800170 break;
171 }
172 default:
173 break;
174 }
175}
hualing chen2aba4022020-03-02 13:49:55 +0800176void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
177{
hualing chen6e4bfa52020-03-13 14:37:11 +0800178 DVR_Playback_t *player = NULL;
179 if (user_data != NULL) {
180 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800181 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800182 }
hualing chen2aba4022020-03-02 13:49:55 +0800183 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800184 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
185 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800186 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800187 event->event.video_format.frame_width,
188 event->event.video_format.frame_height,
189 event->event.video_format.frame_rate);
190 break;
191 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800192 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
193 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800194 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800195 if (player->first_trans_ok == DVR_FALSE) {
196 player->first_trans_ok = DVR_TRUE;
197 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
198 }
hualing chen30423862021-04-16 14:39:12 +0800199 if (player != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800200 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800201 player->seek_pause = DVR_FALSE;
202 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800203 break;
204 }
hualing chen487ae6d2020-07-22 10:34:11 +0800205 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
206 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
207 player->first_trans_ok = DVR_TRUE;
208 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
209 }
210 if (player != NULL && player->has_video == DVR_FALSE) {
211 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
212 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800213 player->seek_pause = DVR_FALSE;
hualing chen487ae6d2020-07-22 10:34:11 +0800214 }
215 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800216 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800217 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800218 break;
219 }
220 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800221 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800222 player->player_callback_func(player->player_callback_userdata, event);
223 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800224 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800225 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800226 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800227 }
228}
hualing chencc91e1c2020-02-28 13:26:17 +0800229
hualing chen5cbe1a62020-02-10 16:36:36 +0800230//convert video and audio fmt
231static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
232 int format = 0;
233 if (is_audio == DVR_FALSE) {
234 //for video fmt
235 switch (fmt)
236 {
237 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800238 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800239 break;
240 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800241 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800242 break;
243 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800244 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800245 break;
246 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800247 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800248 break;
hualing chena540a7e2020-03-27 16:44:05 +0800249 case DVR_VIDEO_FORMAT_VP9:
250 format = AV_VIDEO_CODEC_VP9;
251 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800252 }
253 } else {
254 //for audio fmt
255 switch (fmt)
256 {
257 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800258 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800259 break;
260 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800261 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800262 break;
263 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800264 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800265 break;
266 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800267 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800268 break;
hualing chena540a7e2020-03-27 16:44:05 +0800269 case DVR_AUDIO_FORMAT_AAC:
270 format = AV_AUDIO_CODEC_AAC;
271 break;
272 case DVR_AUDIO_FORMAT_LATM:
273 format = AV_AUDIO_CODEC_LATM;
274 break;
275 case DVR_AUDIO_FORMAT_PCM:
276 format = AV_AUDIO_CODEC_PCM;
277 break;
hualing chenee0e52b2021-04-09 16:58:44 +0800278 case DVR_AUDIO_FORMAT_AC4:
279 format = AV_AUDIO_CODEC_AC4;
280 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800281 }
282 }
283 return format;
284}
hualing chen040df222020-01-17 13:35:02 +0800285static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800286{
hualing chen040df222020-01-17 13:35:02 +0800287 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800288
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800289 if (player == NULL || player->handle == (am_tsplayer_handle)NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800290 return -1;
291
hualing chena540a7e2020-03-27 16:44:05 +0800292 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800293}
hualing chena540a7e2020-03-27 16:44:05 +0800294
hualing chen5cbe1a62020-02-10 16:36:36 +0800295//get sys time ms
hualing chen03fd4942021-07-15 15:56:41 +0800296static uint64_t _dvr_time_getClock(void)
hualing chen5cbe1a62020-02-10 16:36:36 +0800297{
298 struct timespec ts;
hualing chen03fd4942021-07-15 15:56:41 +0800299 uint64_t ms;
hualing chen5cbe1a62020-02-10 16:36:36 +0800300
hualing chen03fd4942021-07-15 15:56:41 +0800301 clock_gettime(CLOCK_REALTIME, &ts);
hualing chen5cbe1a62020-02-10 16:36:36 +0800302 ms = ts.tv_sec*1000+ts.tv_nsec/1000000;
303
304 return ms;
305}
hualing chen86e7d482020-01-16 15:13:33 +0800306
hualing chenb31a6c62020-01-13 17:27:00 +0800307
308//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800309static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800310{
hualing chen040df222020-01-17 13:35:02 +0800311 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800312
hualing chena540a7e2020-03-27 16:44:05 +0800313
314 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800315 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800316 return DVR_FAILURE;
317 }
318
hualing chen86e7d482020-01-16 15:13:33 +0800319 struct timespec ts;
320 clock_gettime(CLOCK_MONOTONIC, &ts);
321 //ms为毫秒,换算成秒
322 ts.tv_sec += ms/1000;
323 //在outtime的基础上,增加ms毫秒
324 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
325 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
326 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
327 //us的值有可能超过1秒,
328 ts.tv_sec += us / 1000000;
329 us = us % 1000000;
330 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800331 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
332 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800333}
hualing chen31140872020-03-25 12:29:26 +0800334//get tsplay delay time ms
335static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
336 DVR_Playback_t *player = (DVR_Playback_t *) handle;
337 int64_t cache = 0;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800338 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800339 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800340 return 0;
341 }
342 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800343 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800344 return cache;
345}
hualing chenb31a6c62020-01-13 17:27:00 +0800346//send signal
hualing chen040df222020-01-17 13:35:02 +0800347static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800348{
hualing chen87072a82020-03-12 16:20:12 +0800349 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800350
351 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800352 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800353 return DVR_FAILURE;
354 }
355
hualing chen87072a82020-03-12 16:20:12 +0800356 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800357 pthread_cond_signal(&player->cond);
hualing chen87072a82020-03-12 16:20:12 +0800358 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800359 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800360}
361
hualing chen2932d372020-04-29 13:44:00 +0800362//send playback event, need check is need lock first
363static 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 +0800364
365 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800366
367 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800368 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800369 return DVR_FAILURE;
370 }
371
hualing chencc91e1c2020-02-28 13:26:17 +0800372 switch (evt) {
373 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800374 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800375 break;
376 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
377 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800378 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800379 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800380 break;
381 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
382 break;
383 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
384 break;
385 case DVR_PLAYBACK_EVENT_NO_KEY:
386 break;
387 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800388 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800389 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800390 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800391 break;
392 case DVR_PLAYBACK_EVENT_REACHED_END:
393 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800394 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800395 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800396 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800397 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800398 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800399 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800400 default:
401 break;
402 }
403 if (player->openParams.event_fn != NULL)
404 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800405 return DVR_SUCCESS;
406}
hualing chen2932d372020-04-29 13:44:00 +0800407static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800408{
409 DVR_Play_Notify_t notify;
410 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
411 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
412 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800413 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800414 return DVR_SUCCESS;
415}
416
hualing chen2932d372020-04-29 13:44:00 +0800417static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800418{
419 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800420
hualing chene3797f02021-01-13 14:53:28 +0800421 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chend241c7a2021-06-22 13:34:27 +0800422 if (CONTROL_SPEED_ENABLE == 0)
423 return DVR_SUCCESS;
hualing chen4b7c15d2020-04-07 16:13:48 +0800424 }
hualing chena540a7e2020-03-27 16:44:05 +0800425 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800426 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800427 return DVR_FAILURE;
428 }
429
hualing chen03fd4942021-07-15 15:56:41 +0800430 if (player->send_time == 0) {
hualing chend241c7a2021-06-22 13:34:27 +0800431 if (CONTROL_SPEED_ENABLE == 0)
432 player->send_time = _dvr_time_getClock() + 500;
433 else
434 player->send_time = _dvr_time_getClock() + 20;
hualing chen0888c032020-12-18 17:54:57 +0800435 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800436 return DVR_SUCCESS;
437 }
hualing chend241c7a2021-06-22 13:34:27 +0800438 if (CONTROL_SPEED_ENABLE == 0)
439 player->send_time = _dvr_time_getClock() + 500;
440 else
441 player->send_time = _dvr_time_getClock() + 20;
442
hualing chen6e4bfa52020-03-13 14:37:11 +0800443 DVR_Play_Notify_t notify;
444 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
445 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
446 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800447 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800448 return DVR_SUCCESS;
449}
450
hualing chencc91e1c2020-02-28 13:26:17 +0800451//check is ongoing segment
452static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
453
454 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800455 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800456
457 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800458 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800459 return DVR_FAILURE;
460 }
hualing chen87072a82020-03-12 16:20:12 +0800461 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800462 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800463 return DVR_FALSE;
464 }
hualing chencc91e1c2020-02-28 13:26:17 +0800465 return DVR_TRUE;
466}
hualing chen4b7c15d2020-04-07 16:13:48 +0800467
468
469static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
470 DVR_Playback_t *player = (DVR_Playback_t *) handle;
471 player->fffb_start = _dvr_time_getClock();
hualing chen03fd4942021-07-15 15:56:41 +0800472 DVR_PB_DG(1, " player->fffb_start:%lld", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800473 player->fffb_current = player->fffb_start;
474 //get segment current time pos
475 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +0800476 player->next_fffb_time = _dvr_time_getClock();
477
478 return DVR_SUCCESS;
479}
480
hualing chen2aba4022020-03-02 13:49:55 +0800481static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
482 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800483 player->fffb_start = _dvr_time_getClock();
hualing chen03fd4942021-07-15 15:56:41 +0800484 DVR_PB_DG(1, " player->fffb_start:%lld", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800485 player->fffb_current = player->fffb_start;
486 //get segment current time pos
487 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +0800488
hualing chen2aba4022020-03-02 13:49:55 +0800489 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800490 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800491 return DVR_SUCCESS;
492}
hualing chencc91e1c2020-02-28 13:26:17 +0800493//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800494static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
495
496 DVR_Playback_t *player = (DVR_Playback_t *) handle;
497 DVR_PlaybackSegmentInfo_t *segment;
498 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
499
hualing chena540a7e2020-03-27 16:44:05 +0800500 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800501 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800502 return DVR_FAILURE;
503 }
504
hualing chen87072a82020-03-12 16:20:12 +0800505 int found = 0;
506 int found_eq_id = 0;
507 list_for_each_entry(segment, &player->segment_list, head)
508 {
509 if (player->segment_is_open == DVR_FALSE) {
510 //get first segment from list, case segment is not open
511 if (!IS_FB(player->speed))
512 found = 1;
513 } else if (segment->segment_id == segmentid) {
514 //find cur segment, we need get next one
515 found_eq_id = 1;
516 if (!IS_FB(player->speed)) {
517 found = 1;
518 continue;
519 } else {
520 //if is fb mode.we need used pre segment
521 if (pre_segment != NULL) {
522 found = 1;
523 } else {
524 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800525 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800526 return DVR_FAILURE;
527 }
528 }
529 }
530 if (found == 1) {
531 found = 2;
532 break;
533 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800534 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800535 }
536 if (found != 2) {
537 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800538 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800539 return DVR_FAILURE;
540 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800541 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800542 return DVR_SUCCESS;
543}
544
545//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800546static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800547
hualing chen040df222020-01-17 13:35:02 +0800548 DVR_Playback_t *player = (DVR_Playback_t *) handle;
549 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800550 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen03fd4942021-07-15 15:56:41 +0800551 uint64_t segmentid;
552 int pos;
hualing chena540a7e2020-03-27 16:44:05 +0800553 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800554 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800555 return DVR_FAILURE;
556 }
557
hualing chen03fd4942021-07-15 15:56:41 +0800558 if (IS_FB(player->speed)
559 && dvr_playback_check_limit(handle)) {
560 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
561 //case cur id < segment id
562 if (player->cur_segment_id <= segmentid) {
563 //expired ts data is player,return error
564 DVR_PB_DG(1, "reach start segment ,return error");
565 return DVR_FAILURE;
566 }
567 DVR_PB_DG(1, "has segment to fb play [%lld][%d]", segmentid, pos);
568 }
569
hualing chen86e7d482020-01-16 15:13:33 +0800570 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800571 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800572
hualing chen040df222020-01-17 13:35:02 +0800573 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800574 {
hualing chencc91e1c2020-02-28 13:26:17 +0800575 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800576 //get first segment from list, case segment is not open
577 if (!IS_FB(player->speed))
578 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800579 } else if (segment->segment_id == player->cur_segment_id) {
580 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800581 found_eq_id = 1;
582 if (!IS_FB(player->speed)) {
583 found = 1;
584 continue;
585 } else {
586 //if is fb mode.we need used pre segment
587 if (pre_segment != NULL) {
588 found = 1;
589 } else {
590 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800591 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800592 return DVR_FAILURE;
593 }
594 }
hualing chen86e7d482020-01-16 15:13:33 +0800595 }
596 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800597 if (IS_FB(player->speed)) {
598 //used pre segment
599 segment = pre_segment;
600 }
hualing chencc91e1c2020-02-28 13:26:17 +0800601 //save segment info
602 player->last_segment_id = player->cur_segment_id;
hualing chen969fe7b2021-05-26 15:13:17 +0800603 if (player->r_handle)
604 player->last_segment_tatol = segment_tell_total_time(player->r_handle);
hualing chen87072a82020-03-12 16:20:12 +0800605 player->last_segment.segment_id = player->cur_segment.segment_id;
606 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800607 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
608 //pids
609 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
610
hualing chen5cbe1a62020-02-10 16:36:36 +0800611 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800612 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800613 player->cur_segment_id = segment->segment_id;
614 player->cur_segment.segment_id = segment->segment_id;
615 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800616 DVR_PB_DG(1, "set cur id cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, segment->flags, segment->segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800617 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800618 //pids
hualing chen040df222020-01-17 13:35:02 +0800619 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800620 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800621 break;
hualing chen86e7d482020-01-16 15:13:33 +0800622 }
hualing chen2aba4022020-03-02 13:49:55 +0800623 pre_segment = segment;
624 }
625 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
626 //used the last one segment to open
627 //get segment info
628 player->segment_is_open = DVR_TRUE;
629 player->cur_segment_id = pre_segment->segment_id;
630 player->cur_segment.segment_id = pre_segment->segment_id;
631 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800632 DVR_PB_DG(1, "set cur id fb last one cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, pre_segment->flags, pre_segment->segment_id);
hualing chen2aba4022020-03-02 13:49:55 +0800633 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
634 //pids
635 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
636 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800637 }
638 if (found != 2) {
639 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800640 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800641 }
642 return DVR_SUCCESS;
643}
hualing chen040df222020-01-17 13:35:02 +0800644//open next segment to play,if reach list end return errro.
645static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800646{
hualing chen040df222020-01-17 13:35:02 +0800647 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800648 Segment_OpenParams_t params;
649 int ret = DVR_SUCCESS;
650
hualing chena540a7e2020-03-27 16:44:05 +0800651 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800652 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800653 return DVR_FAILURE;
654 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800655 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800656
657 ret = _dvr_get_next_segmentId(handle);
658 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800659 DVR_PB_DG(1, "not found segment info");
660 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800661 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800662 }
663
664 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800665 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800666 segment_close(player->r_handle);
667 player->r_handle = NULL;
668 }
669
670 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800671 //cp chur segment path to location
672 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800673 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800674 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800675 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
676
hualing chen86e7d482020-01-16 15:13:33 +0800677 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800678 if (ret == DVR_FAILURE) {
679 DVR_PB_DG(1, "open segment error");
680 }
hualing chen87072a82020-03-12 16:20:12 +0800681 pthread_mutex_unlock(&player->segment_lock);
682 int total = _dvr_get_end_time( handle);
683 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800684 if (IS_FB(player->speed)) {
685 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800686 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800687 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800688 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800689 }
hualing chen87072a82020-03-12 16:20:12 +0800690 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800691 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800692 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800693 return ret;
694}
695
hualing chen5cbe1a62020-02-10 16:36:36 +0800696//open next segment to play,if reach list end return errro.
697static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
698{
699 DVR_Playback_t *player = (DVR_Playback_t *) handle;
700 Segment_OpenParams_t params;
701 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800702 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800703 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800704 return DVR_FAILURE;
705 }
hualing chencc91e1c2020-02-28 13:26:17 +0800706 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800707 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800708 }
hualing chencc91e1c2020-02-28 13:26:17 +0800709 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800710 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800711 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800712 return DVR_FAILURE;
713 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800714 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800715 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800716
717 DVR_PlaybackSegmentInfo_t *segment;
718
719 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800720
hualing chen5cbe1a62020-02-10 16:36:36 +0800721 list_for_each_entry(segment, &player->segment_list, head)
722 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800723 DVR_PB_DG(1, "see 1 location [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800724 if (segment->segment_id == segment_id) {
725 found = 1;
726 }
727 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800728 DVR_PB_DG(1, "found [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800729 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800730 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800731 player->cur_segment_id = segment->segment_id;
732 player->cur_segment.segment_id = segment->segment_id;
733 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800734 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800735 //pids
736 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800737 DVR_PB_DG(1, "cur found location [%s]id[%lld]flag[%x]", player->cur_segment.location, player->cur_segment.segment_id,player->cur_segment.flags);
hualing chencc91e1c2020-02-28 13:26:17 +0800738 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800739 }
740 }
hualing chencc91e1c2020-02-28 13:26:17 +0800741 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800742 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800743 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800744 return DVR_FAILURE;
745 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800746 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800747 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800748 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800749 params.segment_id = (uint64_t)player->cur_segment.segment_id;
750 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800751 DVR_PB_DG(1, "open segment location[%s][%lld]cur flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
hualing chen2aba4022020-03-02 13:49:55 +0800752 if (player->r_handle != NULL) {
753 segment_close(player->r_handle);
754 player->r_handle = NULL;
755 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800756 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800757 if (ret == DVR_FAILURE) {
758 DVR_PB_DG(1, "segment opne error");
759 }
hualing chen2aba4022020-03-02 13:49:55 +0800760 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800761 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800762
hualing chen4b7c15d2020-04-07 16:13:48 +0800763 DVR_PB_DG(1, "player->dur [%d]cur id [%lld]cur flag [0x%x]\r\n", player->dur,player->cur_segment.segment_id, player->cur_segment.flags);
hualing chen5cbe1a62020-02-10 16:36:36 +0800764 return ret;
765}
766
767
768//get play info by segment id
769static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
770 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800771 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800772 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800773
774 DVR_Playback_t *player = (DVR_Playback_t *) handle;
775 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800776 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800777 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800778 return DVR_FAILURE;
779 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800780
781 int found = 0;
782
783 list_for_each_entry(segment, &player->segment_list, head)
784 {
hualing chen87072a82020-03-12 16:20:12 +0800785 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800786 //get first segment from list
787 found = 1;
788 }
789 if (segment->segment_id == segment_id) {
790 found = 1;
791 }
792 if (found == 1) {
793 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800794 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800795 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800796 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800797 player->cur_segment.segment_id = segment->segment_id;
798 player->cur_segment.flags = segment->flags;
799 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800800 player->cur_segment.pids.video.pid = segment->pids.video.pid;
801 player->cur_segment.pids.video.format = segment->pids.video.format;
802 player->cur_segment.pids.video.type = segment->pids.video.type;
803 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
804 player->cur_segment.pids.audio.format = segment->pids.audio.format;
805 player->cur_segment.pids.audio.type = segment->pids.audio.type;
806 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
807 player->cur_segment.pids.ad.format = segment->pids.ad.format;
808 player->cur_segment.pids.ad.type = segment->pids.ad.type;
809 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800810 //
hualing chen2aba4022020-03-02 13:49:55 +0800811 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800812 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800813 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800814 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800815 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
816 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800817 DVR_PB_DG(1, "get play info sucess[0x%x]apid[0x%x]vfmt[%d]afmt[%d]", vparam->pid, aparam->pid, vparam->codectype, aparam->codectype);
hualing chen5cbe1a62020-02-10 16:36:36 +0800818 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800819 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800820 }
821 }
hualing chencc91e1c2020-02-28 13:26:17 +0800822 if (found != 2) {
823 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800824 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800825 return DVR_FAILURE;
826 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800827
828 return DVR_SUCCESS;
829}
hualing chencc91e1c2020-02-28 13:26:17 +0800830static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
831 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800832 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800833 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800834 return DVR_FAILURE;
835 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800836
hualing chencc91e1c2020-02-28 13:26:17 +0800837 //compare cur segment
838 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
839 {
840 //check video pids, stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800841 _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800842 //check sub audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800843 _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800844 //check audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800845 _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids, 1);
hualing chene3797f02021-01-13 14:53:28 +0800846 DVR_PB_DG(1, ":last apid: %d set apid: %d", player->last_segment.pids.audio.pid,player->cur_segment.pids.audio.pid);
hualing chencc91e1c2020-02-28 13:26:17 +0800847 //check pcr pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800848 _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800849 }
hualing chena540a7e2020-03-27 16:44:05 +0800850 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800851}
hualing chen5cbe1a62020-02-10 16:36:36 +0800852
hualing chend241c7a2021-06-22 13:34:27 +0800853static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
854{
855 DVR_Playback_t *player = (DVR_Playback_t *) handle;
856 if (player == NULL) {
857 DVR_PB_DG(1, "player is NULL");
858 return DVR_TRUE;
859 }
860 char buf[10];
861 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
862 DVR_PB_DG(1, "player get prop[%d][%s]", atoi(buf), buf);
863
864 if (atoi(buf) != 1) {
865 //return DVR_TRUE;
866 }
867
hualing chen03fd4942021-07-15 15:56:41 +0800868 DVR_PB_DG(1, ":play speed: %f ply dur: %lld sys_dur: %lld",
869 player->speed,
870 player->con_spe.ply_dur,
871 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800872
873 if (player->speed != 1.0f)
874 return DVR_TRUE;
875
876 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800877 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800878 return DVR_FALSE;
879
880 return DVR_TRUE;
881}
882
hualing chencc91e1c2020-02-28 13:26:17 +0800883static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
884{
885 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800886 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800887 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800888 return DVR_FAILURE;
889 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800890 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
891 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
892 return DVR_SUCCESS;
893 }
hualing chen03fd4942021-07-15 15:56:41 +0800894 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]",
895 player->cur_segment.flags,
896 player->cur_segment.segment_id,
897 player->last_segment.flags,
898 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800899 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
900 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800901 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800902 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800903 AmTsPlayer_showVideo(player->handle);
904 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800905 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
906 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800907 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800908 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800909 AmTsPlayer_hideVideo(player->handle);
910 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800911 }
912 return DVR_SUCCESS;
913}
hualing chene3797f02021-01-13 14:53:28 +0800914/*
915if decodec sucess first time.
916sucess: return true
917fail: return false
918*/
hualing chena540a7e2020-03-27 16:44:05 +0800919static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
920 DVR_Playback_t *player = (DVR_Playback_t *) handle;
921 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800922 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800923 return DVR_TRUE;
924 }
hualing chene3797f02021-01-13 14:53:28 +0800925 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800926 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800927 } else {
928 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800929 }
930}
hualing chen86e7d482020-01-16 15:13:33 +0800931static void* _dvr_playback_thread(void *arg)
932{
hualing chen040df222020-01-17 13:35:02 +0800933 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800934 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800935 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800936 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800937 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800938
hualing chen39628212020-05-14 10:35:13 +0800939 #define MAX_REACHEND_TIMEOUT (3000)
940 int reach_end_timeout = 0;//ms
941 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800942 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800943 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800944 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800945 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800946 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
947
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800948 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800949 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800950 DVR_Bool_t goto_rewrite = DVR_FALSE;
hualing chen03fd4942021-07-15 15:56:41 +0800951
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800952 if (player->is_secure_mode) {
953 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800954 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800955 return NULL;
956 }
957 }
hualing chen86e7d482020-01-16 15:13:33 +0800958 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800959 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800960 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800961 return NULL;
962 }
hualing chen2aba4022020-03-02 13:49:55 +0800963 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
964 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800965
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800966 dec_bufs.buf_data = malloc(dec_buf_size);
967 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800968 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800969 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800970 return NULL;
971 }
972 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
973 dec_bufs.buf_size = dec_buf_size;
974
hualing chencc91e1c2020-02-28 13:26:17 +0800975 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800976 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
977 }
hualing chen86e7d482020-01-16 15:13:33 +0800978
hualing chen86e7d482020-01-16 15:13:33 +0800979 if (ret != DVR_SUCCESS) {
980 if (buf != NULL) {
981 free(buf);
982 buf = NULL;
983 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800984 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +0800985 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +0800986 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800987 }
hualing chen4fe3bee2020-10-23 13:58:52 +0800988 DVR_PB_DG(1, "player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +0800989 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +0800990 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
991 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
992 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
993 player->first_trans_ok = DVR_TRUE;
994 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
995 }
hualing chencc91e1c2020-02-28 13:26:17 +0800996 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +0800997 //set video show
998 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +0800999
hualing chen86e7d482020-01-16 15:13:33 +08001000 int trick_stat = 0;
1001 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001002
hualing chen86e7d482020-01-16 15:13:33 +08001003 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +08001004 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001005
hualing chen2aba4022020-03-02 13:49:55 +08001006 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1007 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001008 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001009 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001010 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001011 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001012 {
hualing chen2aba4022020-03-02 13:49:55 +08001013 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1014 if (trick_stat > 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001015 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
1016 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001017 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 +08001018 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001019 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001020 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001021 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
1022 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +08001023 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
1024 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chen03fd4942021-07-15 15:56:41 +08001025 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
1026 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001027 //need change to pause state
1028 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1029 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +08001030 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08001031 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001032 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001033 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001034 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001035 AmTsPlayer_pauseVideoDecoding(player->handle);
1036 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001037 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001038 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001039 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001040 }
1041 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1042 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001043 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001044 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001045 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen03fd4942021-07-15 15:56:41 +08001046 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%lld] cur sys[%lld] [%s] [%lld]",
1047 player->speed,
1048 player->fffb_current,
1049 _dvr_time_getClock(),
1050 _dvr_playback_state_toString(player->state),
1051 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001052 //used timeout wait need lock first,so we unlock and lock
1053 //pthread_mutex_unlock(&player->lock);
1054 //pthread_mutex_lock(&player->lock);
1055 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1056 pthread_mutex_unlock(&player->lock);
1057 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001058 } else if (_dvr_time_getClock() < player->next_fffb_time) {
hualing chen03fd4942021-07-15 15:56:41 +08001059 DVR_PB_DG(1, "fffb timeout-to pause video---speed[%f] fffb cur[%lld] cur sys[%lld] [%s] [%lld]",
1060 player->speed,
1061 player->fffb_current,
1062 _dvr_time_getClock(),
1063 _dvr_playback_state_toString(player->state),
1064 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001065 //used timeout wait need lock first,so we unlock and lock
1066 //pthread_mutex_unlock(&player->lock);
1067 //pthread_mutex_lock(&player->lock);
1068 AmTsPlayer_pauseVideoDecoding(player->handle);
1069 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1070 pthread_mutex_unlock(&player->lock);
1071 continue;
1072
hualing chen2aba4022020-03-02 13:49:55 +08001073 }
hualing chen03fd4942021-07-15 15:56:41 +08001074 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]",
1075 player->speed,
1076 goto_rewrite,
1077 real_read,
1078 _dvr_playback_state_toString(player->state),
1079 player->cmd.cur_cmd);
1080
hualing chen2aba4022020-03-02 13:49:55 +08001081 pthread_mutex_unlock(&player->lock);
1082 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001083 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001084 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1085 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001086 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001087 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001088 pthread_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001089 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001090 }else if (player->fffb_play == DVR_TRUE){
1091 //for first into fffb when reset speed
1092 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1093 _dvr_time_getClock() < player->next_fffb_time) {
hualing chen03fd4942021-07-15 15:56:41 +08001094 DVR_PB_DG(1, "fffb timeout-fffb play---speed[%f] fffb cur[%lld] cur sys[%lld] [%s] [%lld]",
1095 player->speed,
1096 player->fffb_current,
1097 _dvr_time_getClock(),
1098 _dvr_playback_state_toString(player->state),
1099 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001100 //used timeout wait need lock first,so we unlock and lock
1101 //pthread_mutex_unlock(&player->lock);
1102 //pthread_mutex_lock(&player->lock);
1103 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1104 pthread_mutex_unlock(&player->lock);
1105 continue;
1106 }
hualing chen03fd4942021-07-15 15:56:41 +08001107 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
1108 player->speed,
1109 goto_rewrite,
1110 real_read,
1111 _dvr_playback_state_toString(player->state),
1112 player->cmd.cur_cmd,
1113 player->fffb_play);
hualing chen4b7c15d2020-04-07 16:13:48 +08001114 pthread_mutex_unlock(&player->lock);
1115 goto_rewrite = DVR_FALSE;
1116 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001117 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001118 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1119 player->first_frame = 0;
1120 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1121 pthread_mutex_lock(&player->lock);
1122 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001123 }
hualing chenb31a6c62020-01-13 17:27:00 +08001124 }
hualing chen86e7d482020-01-16 15:13:33 +08001125
hualing chen30423862021-04-16 14:39:12 +08001126 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1127 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001128 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001129 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001130 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001131 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1132 pthread_mutex_unlock(&player->lock);
1133 continue;
1134 }
hualing chen266b9502020-04-04 17:39:39 +08001135 //when seek action is done. we need drop write timeout data.
1136 if (player->drop_ts == DVR_TRUE) {
1137 goto_rewrite = DVR_FALSE;
1138 real_read = 0;
1139 player->drop_ts = DVR_FALSE;
1140 }
hualing chen2aba4022020-03-02 13:49:55 +08001141 if (goto_rewrite == DVR_TRUE) {
1142 goto_rewrite = DVR_FALSE;
1143 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001144 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001145 goto rewrite;
1146 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001147 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001148 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001149 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001150 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001151 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chenfbf8e022020-06-15 13:43:11 +08001152 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001153 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001154 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001155 if (read < 0 && errno == EIO) {
1156 //EIO ERROR, EXIT THRAD
1157 DVR_PB_DG(1, "read error.EIO error, exit thread");
1158 DVR_Play_Notify_t notify;
1159 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1160 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001161 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001162 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001163 goto end;
1164 } else if (read < 0) {
1165 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1166 }
hualing chen87072a82020-03-12 16:20:12 +08001167 //if on fb mode and read file end , we need calculate pos to retry read.
1168 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001169 DVR_PB_DG(1, "recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]",
1170 read,
1171 real_read,
1172 buf_len,
1173 player->speed,
1174 player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001175 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1176 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001177 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1178 pthread_mutex_unlock(&player->lock);
1179 continue;
1180 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001181 //DVR_PB_DG(1, "read ts [%d]buf_len[%d]speed[%f]real_read:%d", read, buf_len, player->speed, real_read);
hualing chen86e7d482020-01-16 15:13:33 +08001182 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001183 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001184 #define MIN_CACHE_TIME (3000)
1185 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001186 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001187 if (_cache_time > MIN_CACHE_TIME) {
hualing chene41f4372020-06-06 16:29:17 +08001188 pthread_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001189 /*if cache time > 20s , we think get time is error,*/
1190 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
1191 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1192 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1193 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1194 }
1195 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, ((_cache_time - MIN_CACHE_TIME) > MIN_CACHE_TIME ? MIN_CACHE_TIME : (_cache_time - MIN_CACHE_TIME)));
hualing chene41f4372020-06-06 16:29:17 +08001196 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001197 DVR_PB_DG(1, "read end but cache time is %d > %d, to sleep end and continue", _cache_time, MIN_CACHE_TIME);
hualing chene41f4372020-06-06 16:29:17 +08001198 //continue;
1199 }
hualing chen969fe7b2021-05-26 15:13:17 +08001200
hualing chen040df222020-01-17 13:35:02 +08001201 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001202 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001203 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001204
1205 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chene3797f02021-01-13 14:53:28 +08001206 if (ret != DVR_SUCCESS) {
1207 player->noData++;
1208 DVR_PB_DG(1, "playback is sleep:[%d]ms nodata[%d]", timeout, player->noData);
1209 if (player->noData == 4) {
1210 DVR_PB_DG(1, "playback send nodata event nodata[%d]", player->noData);
1211 //send event here and pause
1212 DVR_Play_Notify_t notify;
1213 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1214 notify.event = DVR_PLAYBACK_EVENT_NODATA;
1215 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_NODATA");
1216 //get play statue not here
1217 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1218 }
1219 }
1220 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001221 if ((ret != DVR_SUCCESS &&
hualing chen03fd4942021-07-15 15:56:41 +08001222 (player->vendor != DVR_PLAYBACK_VENDOR_AMAZON) &&
hualing chen041c4092020-04-05 15:11:50 +08001223 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001224 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001225 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1226 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001227 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001228 DVR_Play_Notify_t notify;
1229 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1230 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1231 //get play statue not here
1232 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001233 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001234 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001235 DVR_PB_DG(1, "playback is send end delay:[%d]reach_end_timeout[%d]ms", delay, reach_end_timeout);
hualing chen31140872020-03-25 12:29:26 +08001236 pthread_mutex_lock(&player->lock);
1237 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1238 pthread_mutex_unlock(&player->lock);
1239 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001240 } else if (ret != DVR_SUCCESS) {
1241 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001242 if (delay < cache_time) {
1243 //delay time is changed and then has data to play, so not start timeout
1244 } else {
1245 reach_end_timeout = reach_end_timeout + timeout;
1246 }
1247 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001248 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001249 pthread_mutex_lock(&player->lock);
1250 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1251 pthread_mutex_unlock(&player->lock);
1252 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001253 }
hualing chen39628212020-05-14 10:35:13 +08001254 reach_end_timeout = 0;
1255 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001256 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001257 //change next segment success case
1258 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001259 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001260 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1261 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001262 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001263 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001264 }//read len 0 check end
1265 if (player->noData > 0) {
1266 player->noData = 0;
1267 DVR_PB_DG(1, "playback send data event resume[%d]", player->noData);
1268 //send event here and pause
1269 DVR_Play_Notify_t notify;
1270 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1271 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
1272 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_DATARESUME");
1273 //get play statue not here
1274 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001275 }
hualing chen39628212020-05-14 10:35:13 +08001276 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001277 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001278 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001279 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001280
hualing chena540a7e2020-03-27 16:44:05 +08001281 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001282 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001283 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001284 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001285 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001286 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001287 }
hualing chen266b9502020-04-04 17:39:39 +08001288 //if need write whole block size, we need check read buf len is eq block size.
1289 if (b_writed_whole_block == DVR_TRUE) {
1290 //buf_len is block size value.
1291 if (real_read < buf_len) {
1292 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001293 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001294 pthread_mutex_lock(&player->lock);
1295 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1296 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001297 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001298 continue;
1299 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001300 DVR_PB_DG(1, "read buf len[%d] is > block size [%d],this error occur", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001301 }
1302 }
1303
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001304 if (player->dec_func) {
1305 DVR_CryptoParams_t crypto_params;
1306
1307 memset(&crypto_params, 0, sizeof(crypto_params));
1308 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1309 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1310 crypto_params.segment_id = player->cur_segment.segment_id;
hualing chen1f26ffa2020-11-03 10:39:20 +08001311 crypto_params.offset = segment_tell_position(player->r_handle) - wbufs.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001312 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1313 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001314 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1315 crypto_params.input_buffer.addr = (size_t)buf;
1316 crypto_params.input_buffer.size = real_read;
1317
1318 if (player->is_secure_mode) {
1319 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1320 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1321 crypto_params.output_buffer.size = dec_buf_size;
1322 ret = player->dec_func(&crypto_params, player->dec_userdata);
1323 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001324 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001325 } else {
1326 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1327 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1328 crypto_params.output_buffer.size = dec_buf_size;
1329 ret = player->dec_func(&crypto_params, player->dec_userdata);
1330 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001331 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001332 }
1333 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001334 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001335 }
pengfei.liufda2a972020-04-09 14:47:15 +08001336 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001337 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001338rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001339 if (player->drop_ts == DVR_TRUE) {
1340 //need drop ts data when seek occur.we need read next loop,drop this ts data
1341 goto_rewrite = DVR_FALSE;
1342 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001343 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001344 player->drop_ts = DVR_FALSE;
1345 continue;
1346 }
hualing chen5605eed2020-05-26 18:18:06 +08001347 player->ts_cache_len = real_read;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001348 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1349 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001350 player->ts_cache_len = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001351 real_read = 0;
1352 write_success++;
hualing chend241c7a2021-06-22 13:34:27 +08001353 if (CONTROL_SPEED_ENABLE == 1) {
1354check0:
1355 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
1356 pthread_mutex_lock(&player->lock);
1357 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
1358 pthread_mutex_unlock(&player->lock);
1359 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1360 goto check0;
1361 }
1362 }
hualing chen5605eed2020-05-26 18:18:06 +08001363 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001364 continue;
hualing chen87072a82020-03-12 16:20:12 +08001365 } else {
hualing chen03fd4942021-07-15 15:56:41 +08001366 DVR_PB_DG(1, "write time out write_success:%d wbufs.buf_size:%d systime:%lld",
1367 write_success,
1368 wbufs.buf_size,
1369 _dvr_time_getClock());
1370
hualing chena540a7e2020-03-27 16:44:05 +08001371 write_success = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001372 if (CONTROL_SPEED_ENABLE == 1) {
1373check1:
1374 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
1375 pthread_mutex_lock(&player->lock);
1376 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
1377 pthread_mutex_unlock(&player->lock);
1378 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1379 goto check1;
1380 }
1381 }
hualing chencc91e1c2020-02-28 13:26:17 +08001382 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001383 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001384 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001385 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001386 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001387 break;
1388 }
hualing chen2aba4022020-03-02 13:49:55 +08001389 goto_rewrite = DVR_TRUE;
1390 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001391 }
1392 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001393end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001394 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001395 free(buf);
1396 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001397 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001398}
1399
1400
hualing chen040df222020-01-17 13:35:02 +08001401static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001402{
hualing chen040df222020-01-17 13:35:02 +08001403 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001404
1405 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001406 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001407 return DVR_FAILURE;
1408 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001409 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001410 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001411 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001412 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001413 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001414 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001415 if (rc < 0)
1416 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001417 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001418}
1419
1420
hualing chen040df222020-01-17 13:35:02 +08001421static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001422{
hualing chen040df222020-01-17 13:35:02 +08001423 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001424
1425 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001426 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001427 return DVR_FAILURE;
1428 }
1429
hualing chen4b7c15d2020-04-07 16:13:48 +08001430 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001431 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001432 {
1433 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001434 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001435 pthread_join(player->playback_thread, NULL);
1436 }
1437 if (player->r_handle) {
1438 segment_close(player->r_handle);
1439 player->r_handle = NULL;
1440 }
hualing chen7a56cba2020-04-14 14:09:27 +08001441 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001442 return 0;
1443}
1444
hualing chenb31a6c62020-01-13 17:27:00 +08001445/**\brief Open an dvr palyback
1446 * \param[out] p_handle dvr playback addr
1447 * \param[in] params dvr playback open parameters
1448 * \retval DVR_SUCCESS On success
1449 * \return Error code
1450 */
hualing chen040df222020-01-17 13:35:02 +08001451int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001452
hualing chen040df222020-01-17 13:35:02 +08001453 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001454 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001455
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001456 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001457
hualing chen86e7d482020-01-16 15:13:33 +08001458 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001459 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001460 pthread_condattr_init(&cattr);
1461 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1462 pthread_cond_init(&player->cond, &cattr);
1463 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001464
hualing chen5cbe1a62020-02-10 16:36:36 +08001465 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001466 INIT_LIST_HEAD(&player->segment_list);
1467 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1468 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001469 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001470 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001471 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001472 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001473 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001474 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001475
hualing chen86e7d482020-01-16 15:13:33 +08001476 //store open params
hualing chen040df222020-01-17 13:35:02 +08001477 player->openParams.dmx_dev_id = params->dmx_dev_id;
1478 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001479 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001480 player->openParams.event_fn = params->event_fn;
1481 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001482 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001483 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001484
hualing chen5cbe1a62020-02-10 16:36:36 +08001485 player->has_pids = params->has_pids;
1486
hualing chen2aba4022020-03-02 13:49:55 +08001487 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001488
1489 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1490 //for test get callback
1491 if (0 && player->player_callback_func == NULL) {
1492 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1493 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen03fd4942021-07-15 15:56:41 +08001494 DVR_PB_DG(1, "playback open get callback[%p][%p][%p][%p]",
1495 player->player_callback_func,
1496 player->player_callback_userdata,
1497 _dvr_tsplayer_callback_test,
1498 player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001499 }
1500 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001501
hualing chen86e7d482020-01-16 15:13:33 +08001502 //init has audio and video
1503 player->has_video = DVR_FALSE;
1504 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001505 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001506 player->last_segment_id = 0LL;
1507 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001508
hualing chen5cbe1a62020-02-10 16:36:36 +08001509 //init ff fb time
hualing chen03fd4942021-07-15 15:56:41 +08001510 player->fffb_current = UINT64_MAX;
1511 player->fffb_start = UINT64_MAX;
1512 player->fffb_start_pcr = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001513 //seek time
1514 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001515 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001516
1517 //init secure stuff
1518 player->dec_func = NULL;
1519 player->dec_userdata = NULL;
1520 player->is_secure_mode = 0;
1521 player->secure_buffer = NULL;
1522 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001523 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001524
hualing chen4b7c15d2020-04-07 16:13:48 +08001525 player->fffb_play = DVR_FALSE;
1526
1527 player->last_send_time_id = UINT64_MAX;
1528 player->last_cur_time = 0;
hualing chen30423862021-04-16 14:39:12 +08001529 player->seek_pause = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001530
hualing chend241c7a2021-06-22 13:34:27 +08001531 //speed con init
1532 if (CONTROL_SPEED_ENABLE == 1) {
1533 player->con_spe.ply_dur = 0;
1534 player->con_spe.ply_sta = -1;
1535 player->con_spe.sys_dur = 0;
1536 player->con_spe.sys_sta = 0;
1537 }
1538
hualing chen03fd4942021-07-15 15:56:41 +08001539 //limit info
1540 player->rec_start = 0;
1541 player->limit = -1;
hualing chen86e7d482020-01-16 15:13:33 +08001542 *p_handle = player;
1543 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001544}
1545
1546/**\brief Close an dvr palyback
1547 * \param[in] handle playback handle
1548 * \retval DVR_SUCCESS On success
1549 * \return Error code
1550 */
hualing chen040df222020-01-17 13:35:02 +08001551int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001552
hualing chen86e7d482020-01-16 15:13:33 +08001553 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001554 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001555 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001556 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001557 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001558 return DVR_FAILURE;
1559 }
1560
hualing chencc91e1c2020-02-28 13:26:17 +08001561 if (player->state != DVR_PLAYBACK_STATE_STOP)
1562 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001563 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001564 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001565 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1566 } else {
1567 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001568 }
hualing chen7a56cba2020-04-14 14:09:27 +08001569 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001570 pthread_mutex_destroy(&player->lock);
1571 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001572
1573 if (player) {
1574 free(player);
1575 player = NULL;
1576 }
hualing chen7a56cba2020-04-14 14:09:27 +08001577 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001578 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001579}
1580
hualing chenb31a6c62020-01-13 17:27:00 +08001581/**\brief Start play audio and video, used start auido api and start video api
1582 * \param[in] handle playback handle
1583 * \param[in] params audio playback params,contains fmt and pid...
1584 * \retval DVR_SUCCESS On success
1585 * \return Error code
1586 */
hualing chen040df222020-01-17 13:35:02 +08001587int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1588 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001589 am_tsplayer_video_params vparams;
1590 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001591 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001592
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001593 memset(&vparams, 0, sizeof(vparams));
1594 memset(&aparams, 0, sizeof(aparams));
1595
hualing chena540a7e2020-03-27 16:44:05 +08001596 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001597 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001598 return DVR_FAILURE;
1599 }
hualing chencc91e1c2020-02-28 13:26:17 +08001600 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001601 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001602
hualing chena540a7e2020-03-27 16:44:05 +08001603 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001604 //can used start api to resume playback
1605 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1606 return dvr_playback_resume(handle);
1607 }
hualing chen87072a82020-03-12 16:20:12 +08001608 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001609 //if flag is puased and not decodec first frame. if user resume, we need
1610 //clear flag and set trickmode none
1611 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1612 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1613 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1614 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1615 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001616 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001617 return DVR_SUCCESS;
1618 }
hualing chen86e7d482020-01-16 15:13:33 +08001619 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001620 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001621 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001622 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001623 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001624 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001625 //start audio and video
Zhiqiang Hana9d261b2020-11-11 18:38:10 +08001626 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001627 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001628 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001629 pthread_mutex_unlock(&player->lock);
1630 DVR_Play_Notify_t notify;
1631 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1632 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1633 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1634 notify.info.transition_failed_data.segment_id = segment_id;
1635 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001636 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001637 return -1;
1638 }
hualing chen31140872020-03-25 12:29:26 +08001639
hualing chencc91e1c2020-02-28 13:26:17 +08001640 {
hualing chen86e7d482020-01-16 15:13:33 +08001641 if (VALID_PID(vparams.pid)) {
1642 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001643 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001644 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001645 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001646 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1647 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001648 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001649 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001650 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001651 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001652 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001653 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001654 }
hualing chena93bbbc2020-12-22 17:23:42 +08001655 AmTsPlayer_showVideo(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001656 AmTsPlayer_setVideoParams(player->handle, &vparams);
1657 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001658 }
hualing chena540a7e2020-03-27 16:44:05 +08001659
hualing chen4b7c15d2020-04-07 16:13:48 +08001660 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1661 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001662 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1663 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1664 player->cmd.state = DVR_PLAYBACK_STATE_START;
1665 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001666 } else {
1667 player->cmd.last_cmd = player->cmd.cur_cmd;
1668 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001669 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001670 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001671 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001672 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001673 } else {
hualing chendf118dd2020-05-21 15:49:11 +08001674 if (VALID_PID(adparams.pid)) {
1675 player->has_ad_audio = DVR_TRUE;
1676 DVR_PB_DG(1, "start ad audio");
1677 AmTsPlayer_setADParams(player->handle, &adparams);
1678 AmTsPlayer_enableADMix(player->handle);
1679 }
hualing chen969fe7b2021-05-26 15:13:17 +08001680 if (VALID_PID(aparams.pid)) {
1681 DVR_PB_DG(1, "start audio");
1682 player->has_audio = DVR_TRUE;
1683 AmTsPlayer_setAudioParams(player->handle, &aparams);
1684 AmTsPlayer_startAudioDecoding(player->handle);
1685 }
hualing chen31140872020-03-25 12:29:26 +08001686 }
hualing chencc91e1c2020-02-28 13:26:17 +08001687 player->cmd.state = DVR_PLAYBACK_STATE_START;
1688 player->state = DVR_PLAYBACK_STATE_START;
1689 }
hualing chen86e7d482020-01-16 15:13:33 +08001690 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001691 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001692 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001693 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001694 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001695}
hualing chen040df222020-01-17 13:35:02 +08001696/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001697 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001698 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001699 * \retval DVR_SUCCESS On success
1700 * \return Error code
1701 */
hualing chen040df222020-01-17 13:35:02 +08001702int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1703 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001704
hualing chena540a7e2020-03-27 16:44:05 +08001705 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001706 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001707 return DVR_FAILURE;
1708 }
1709
hualing chen4b7c15d2020-04-07 16:13:48 +08001710 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001711 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001712
hualing chen040df222020-01-17 13:35:02 +08001713 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1714 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001715
hualing chen86e7d482020-01-16 15:13:33 +08001716 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001717 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001718 //cp location
hualing chen040df222020-01-17 13:35:02 +08001719 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001720
hualing chen4b7c15d2020-04-07 16:13:48 +08001721 DVR_PB_DG(1, "add location [%s]id[%lld]flag[%x]", segment->location, segment->segment_id, info->flags);
hualing chen040df222020-01-17 13:35:02 +08001722 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001723
1724 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001725 segment->pids.video.pid = info->pids.video.pid;
1726 segment->pids.video.format = info->pids.video.format;
1727 segment->pids.video.type = info->pids.video.type;
1728
hualing chen2aba4022020-03-02 13:49:55 +08001729 segment->pids.audio.pid = info->pids.audio.pid;
1730 segment->pids.audio.format = info->pids.audio.format;
1731 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001732
hualing chen2aba4022020-03-02 13:49:55 +08001733 segment->pids.ad.pid = info->pids.ad.pid;
1734 segment->pids.ad.format = info->pids.ad.format;
1735 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001736
1737 segment->pids.pcr.pid = info->pids.pcr.pid;
1738
hualing chen4b7c15d2020-04-07 16:13:48 +08001739 DVR_PB_DG(1, "lock pid [0x%x][0x%x][0x%x][0x%x]", segment->pids.video.pid,segment->pids.audio.pid, info->pids.video.pid,info->pids.audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001740 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001741 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001742 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001743 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001744
hualing chen5cbe1a62020-02-10 16:36:36 +08001745 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001746}
hualing chen040df222020-01-17 13:35:02 +08001747/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001748 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001749 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001750 * \retval DVR_SUCCESS On success
1751 * \return Error code
1752 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001753int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001754 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001755 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001756 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001757 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001758 return DVR_FAILURE;
1759 }
1760
hualing chencc91e1c2020-02-28 13:26:17 +08001761 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001762 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001763 return DVR_FAILURE;
1764 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001765 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001766 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001767 DVR_PlaybackSegmentInfo_t *segment = NULL;
1768 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1769 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001770 {
hualing chen040df222020-01-17 13:35:02 +08001771 if (segment->segment_id == segment_id) {
1772 list_del(&segment->head);
1773 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001774 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001775 }
hualing chen86e7d482020-01-16 15:13:33 +08001776 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001777 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001778 pthread_mutex_unlock(&player->lock);
1779
1780 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001781}
hualing chen040df222020-01-17 13:35:02 +08001782/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001783 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001784 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001785 * \retval DVR_SUCCESS On success
1786 * \return Error code
1787 */
hualing chen040df222020-01-17 13:35:02 +08001788int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001789 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001790 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001791 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001792 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001793 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001794 return DVR_FAILURE;
1795 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001796 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1797 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1798 return DVR_SUCCESS;
1799 }
hualing chena540a7e2020-03-27 16:44:05 +08001800
hualing chen040df222020-01-17 13:35:02 +08001801 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001802 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001803 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001804 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001805 {
hualing chen040df222020-01-17 13:35:02 +08001806 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08001807 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001808 }
hualing chen86e7d482020-01-16 15:13:33 +08001809 // if encramble to free, only set flag and return;
1810
1811 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001812 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001813 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1814 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001815 //disable display, mute
hualing chen703f3572021-01-06 12:51:34 +08001816 DVR_PB_DG(1, "mute av");
hualing chen2aba4022020-03-02 13:49:55 +08001817 AmTsPlayer_hideVideo(player->handle);
1818 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001819 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1820 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001821 //enable display, unmute
hualing chen703f3572021-01-06 12:51:34 +08001822 DVR_PB_DG(1, "unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08001823 AmTsPlayer_showVideo(player->handle);
1824 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001825 } else {
1826 //do nothing
1827 }
1828 } else {
1829 //do nothing
1830 }
1831 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001832 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001833 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001834 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001835 pthread_mutex_unlock(&player->lock);
1836 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001837}
1838
1839
hualing chen275379e2021-06-15 17:57:21 +08001840static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_PlaybackPids_t set_pids, int type) {
hualing chen040df222020-01-17 13:35:02 +08001841 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen275379e2021-06-15 17:57:21 +08001842 DVR_StreamInfo_t set_pid;
1843
1844 if (type == 0) {
1845 set_pid = set_pids.video;
1846 } else if (type == 1) {
1847 set_pid = set_pids.audio;
1848 } else if (type == 2) {
1849 set_pid = set_pids.ad;
1850 } else {
1851 set_pid = set_pids.pcr;
1852 }
1853
hualing chena540a7e2020-03-27 16:44:05 +08001854 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001855 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001856 return DVR_FAILURE;
1857 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001858 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001859 if (now_pid.pid == set_pid.pid) {
1860 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001861 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001862 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001863 if (VALID_PID(now_pid.pid)) {
1864 //stop now stream
1865 if (type == 0) {
1866 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001867 if (player->has_video == DVR_TRUE) {
1868 DVR_PB_DG(1, "stop video");
1869 AmTsPlayer_stopVideoDecoding(player->handle);
1870 player->has_video = DVR_FALSE;
1871 }
hualing chen86e7d482020-01-16 15:13:33 +08001872 } else if (type == 1) {
1873 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001874 if (player->has_audio == DVR_TRUE) {
1875 DVR_PB_DG(1, "stop audio");
1876 AmTsPlayer_stopAudioDecoding(player->handle);
1877 player->has_audio = DVR_FALSE;
1878 }
hualing chen86e7d482020-01-16 15:13:33 +08001879 } else if (type == 2) {
1880 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001881 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001882 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001883 } else if (type == 3) {
1884 //pcr
1885 }
1886 }
1887 if (VALID_PID(set_pid.pid)) {
1888 //start
1889 if (type == 0) {
1890 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001891 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001892 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001893 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001894 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001895 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001896 AmTsPlayer_setVideoParams(player->handle, &vparams);
1897 AmTsPlayer_startVideoDecoding(player->handle);
1898 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001899 } else if (type == 1) {
1900 //start audio
Gong Ke2a0ebbe2021-05-25 15:22:50 +08001901 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen275379e2021-06-15 17:57:21 +08001902 if (VALID_PID(set_pids.ad.pid)) {
1903 am_tsplayer_audio_params adparams;
1904 adparams.pid = set_pids.ad.pid;
1905 adparams.codectype= _dvr_convert_stream_fmt(set_pids.ad.format, DVR_TRUE);
1906 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",adparams.pid, adparams.codectype);
1907 AmTsPlayer_setADParams(player->handle, &adparams);
1908 AmTsPlayer_enableADMix(player->handle);
1909 }
1910
hualing chenc70a8df2020-05-12 19:23:11 +08001911 am_tsplayer_audio_params aparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001912
1913 memset(&aparams, 0, sizeof(aparams));
1914
hualing chenc70a8df2020-05-12 19:23:11 +08001915 aparams.pid = set_pid.pid;
1916 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1917 player->has_audio = DVR_TRUE;
1918 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1919 AmTsPlayer_setAudioParams(player->handle, &aparams);
1920 AmTsPlayer_startAudioDecoding(player->handle);
1921 //playback_device_audio_start(player->handle,&aparams);
1922 }
hualing chen86e7d482020-01-16 15:13:33 +08001923 } else if (type == 2) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +08001924 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chenc70a8df2020-05-12 19:23:11 +08001925 am_tsplayer_audio_params aparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001926
1927 memset(&aparams, 0, sizeof(aparams));
hualing chenc70a8df2020-05-12 19:23:11 +08001928 aparams.pid = set_pid.pid;
1929 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1930 player->has_audio = DVR_TRUE;
1931 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1932 AmTsPlayer_setADParams(player->handle, &aparams);
1933 AmTsPlayer_enableADMix(player->handle);
1934 //playback_device_audio_start(player->handle,&aparams);
1935 }
hualing chen86e7d482020-01-16 15:13:33 +08001936 } else if (type == 3) {
1937 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001938 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001939 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001940 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001941 //audio and video all close
1942 if (!player->has_audio && !player->has_video) {
1943 player->state = DVR_PLAYBACK_STATE_STOP;
1944 }
hualing chen86e7d482020-01-16 15:13:33 +08001945 }
1946 }
1947 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001948}
hualing chen5cbe1a62020-02-10 16:36:36 +08001949/**\brief dvr play back update segment pids
1950 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001951 * add pid stream and stop remove pid stream.
1952 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001953 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001954 * \retval DVR_SUCCESS On success
1955 * \return Error code
1956 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001957int 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 +08001958 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001959 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001960 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001961 return DVR_FAILURE;
1962 }
1963
hualing chen040df222020-01-17 13:35:02 +08001964 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001965 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001966 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001967 DVR_PB_DG(1, "get lock update segment id: %lld cur id %lld", segment_id, player->cur_segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001968
hualing chen040df222020-01-17 13:35:02 +08001969 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001970 {
hualing chen040df222020-01-17 13:35:02 +08001971 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001972
1973 if (player->cur_segment_id == segment_id) {
1974 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
1975 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
1976 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08001977 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08001978 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001979 return 0;
1980 }
1981
1982 //if segment is on going segment,we need stop start stream
1983 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08001984 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001985 //check video pids, stop or restart
hualing chen275379e2021-06-15 17:57:21 +08001986 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, *p_pids, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08001987 //check sub audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +08001988 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, *p_pids, 2);
jianchuan.pinge5f8c5a2021-02-03 19:11:05 +08001989 //check audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +08001990 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, *p_pids, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001991 //check pcr pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +08001992 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, *p_pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +08001993 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001994 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1995 //if state is pause, we need process at resume api. we only record change info
1996 int v_cmd = DVR_PLAYBACK_CMD_NONE;
1997 int a_cmd = DVR_PLAYBACK_CMD_NONE;
1998 if (VALID_PID(segment->pids.video.pid)
1999 && VALID_PID(p_pids->video.pid)
2000 && segment->pids.video.pid != p_pids->video.pid) {
2001 //restart video
2002 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
2003 }
2004 if (!VALID_PID(segment->pids.video.pid)
2005 && VALID_PID(p_pids->video.pid)
2006 && segment->pids.video.pid != p_pids->video.pid) {
2007 //start video
2008 v_cmd = DVR_PLAYBACK_CMD_VSTART;
2009 }
2010 if (VALID_PID(segment->pids.video.pid)
2011 && !VALID_PID(p_pids->video.pid)
2012 && segment->pids.video.pid != p_pids->video.pid) {
2013 //stop video
2014 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
2015 }
2016 if (VALID_PID(segment->pids.audio.pid)
2017 && VALID_PID(p_pids->audio.pid)
2018 && segment->pids.audio.pid != p_pids->audio.pid) {
2019 //restart audio
2020 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
2021 }
2022 if (!VALID_PID(segment->pids.audio.pid)
2023 && VALID_PID(p_pids->audio.pid)
2024 && segment->pids.audio.pid != p_pids->audio.pid) {
2025 //start audio
2026 a_cmd = DVR_PLAYBACK_CMD_ASTART;
2027 }
2028 if (VALID_PID(segment->pids.audio.pid)
2029 && !VALID_PID(p_pids->audio.pid)
2030 && segment->pids.audio.pid != p_pids->audio.pid) {
2031 //stop audio
2032 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
2033 }
2034 if (a_cmd == DVR_PLAYBACK_CMD_NONE
2035 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
2036 //do nothing
2037 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
2038 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
2039 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2040 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
2041 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
2042 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
2043 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
2044 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
2045 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2046 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
2047 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
2048 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2049 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2050 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
2051 } else {
2052 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2053 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
2054 }
2055
2056 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
2057 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
2058 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2059 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
2060 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
2061 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2062 //not occur this case
2063 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2064 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
2065 } else {
2066 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2067 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
2068 }
2069
2070 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
2071 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2072 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2073 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
2074 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
2075 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
2076 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2077 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
2078 } else {
2079 //not occur this case
2080 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2081 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2082 }
2083 }
2084 }
hualing chene10666f2020-04-14 13:58:37 +08002085 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08002086 }
hualing chen86e7d482020-01-16 15:13:33 +08002087 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08002088 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08002089 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08002090 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002091 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002092 }
hualing chen86e7d482020-01-16 15:13:33 +08002093 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002094 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002095 pthread_mutex_unlock(&player->lock);
2096 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002097}
2098/**\brief Stop play, will stop video and audio
2099 * \param[in] handle playback handle
2100 * \param[in] clear is clear last frame
2101 * \retval DVR_SUCCESS On success
2102 * \return Error code
2103 */
hualing chen040df222020-01-17 13:35:02 +08002104int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
2105 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002106 UNDVRUSED(clear);
hualing chena540a7e2020-03-27 16:44:05 +08002107 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002108 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002109 return DVR_FAILURE;
2110 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002111 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2112 DVR_PB_DG(1, ":playback is stoped");
2113 return DVR_SUCCESS;
2114 }
Ke Gong3c0caba2020-04-21 22:58:18 -07002115 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2116 DVR_PB_DG(1, ":playback is stoped");
2117 return DVR_SUCCESS;
2118 }
hualing chen87072a82020-03-12 16:20:12 +08002119 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08002120 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002121 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002122 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08002123 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002124 if (player->has_video) {
2125 AmTsPlayer_resumeVideoDecoding(player->handle);
2126 }
2127 if (player->has_audio) {
2128 AmTsPlayer_resumeAudioDecoding(player->handle);
2129 }
2130 if (player->has_video) {
2131 player->has_video = DVR_FALSE;
hualing chen10cdb162021-02-05 10:44:41 +08002132 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002133 AmTsPlayer_stopVideoDecoding(player->handle);
2134 }
2135 if (player->has_audio) {
2136 player->has_audio = DVR_FALSE;
2137 AmTsPlayer_stopAudioDecoding(player->handle);
2138 }
hualing chendf118dd2020-05-21 15:49:11 +08002139 if (player->has_ad_audio) {
2140 player->has_ad_audio =DVR_FALSE;
2141 AmTsPlayer_disableADMix(player->handle);
2142 }
hualing chen266b9502020-04-04 17:39:39 +08002143
hualing chen86e7d482020-01-16 15:13:33 +08002144 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002145 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2146 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2147 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002148 player->cur_segment_id = UINT64_MAX;
2149 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002150 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08002151 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08002152 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002153 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002154}
2155/**\brief Start play audio
2156 * \param[in] handle playback handle
2157 * \param[in] params audio playback params,contains fmt and pid...
2158 * \retval DVR_SUCCESS On success
2159 * \return Error code
2160 */
hualing chen2aba4022020-03-02 13:49:55 +08002161
hualing chendf118dd2020-05-21 15:49:11 +08002162int dvr_playback_audio_start(DVR_PlaybackHandle_t handle, am_tsplayer_audio_params *param, am_tsplayer_audio_params *adparam) {
hualing chen040df222020-01-17 13:35:02 +08002163 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002164
2165 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002166 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002167 return DVR_FAILURE;
2168 }
hualing chen86e7d482020-01-16 15:13:33 +08002169 _start_playback_thread(handle);
2170 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002171 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002172 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002173
hualing chendf118dd2020-05-21 15:49:11 +08002174 if (VALID_PID(adparam->pid)) {
2175 player->has_ad_audio = DVR_TRUE;
2176 DVR_PB_DG(1, "start ad audio");
2177 AmTsPlayer_setADParams(player->handle, adparam);
2178 AmTsPlayer_enableADMix(player->handle);
2179 }
hualing chen969fe7b2021-05-26 15:13:17 +08002180 if (VALID_PID(param->pid)) {
2181 DVR_PB_DG(1, "start audio");
2182 player->has_audio = DVR_TRUE;
2183 AmTsPlayer_setAudioParams(player->handle, param);
2184 AmTsPlayer_startAudioDecoding(player->handle);
2185 }
hualing chendf118dd2020-05-21 15:49:11 +08002186
hualing chen86e7d482020-01-16 15:13:33 +08002187 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002188 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
2189 player->cmd.state = DVR_PLAYBACK_STATE_START;
2190 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002191 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002192 pthread_mutex_unlock(&player->lock);
2193 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002194}
2195/**\brief Stop play audio
2196 * \param[in] handle playback handle
2197 * \retval DVR_SUCCESS On success
2198 * \return Error code
2199 */
hualing chen040df222020-01-17 13:35:02 +08002200int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2201 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002202
2203 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002204 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002205 return DVR_FAILURE;
2206 }
2207
hualing chen2aba4022020-03-02 13:49:55 +08002208 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002209 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002210 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2211 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002212 //destory thread
2213 _stop_playback_thread(handle);
2214 } else {
2215 //do nothing.video is playing
2216 }
hualing chen7a56cba2020-04-14 14:09:27 +08002217 DVR_PB_DG(1, "lock");
2218 pthread_mutex_lock(&player->lock);
2219
hualing chenf00cdc82020-06-10 14:23:35 +08002220 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002221 player->has_audio = DVR_FALSE;
2222 AmTsPlayer_stopAudioDecoding(player->handle);
2223 }
hualing chen87072a82020-03-12 16:20:12 +08002224
hualing chendf118dd2020-05-21 15:49:11 +08002225 if (player->has_ad_audio) {
2226 player->has_ad_audio =DVR_FALSE;
2227 AmTsPlayer_disableADMix(player->handle);
2228 }
2229
hualing chen87072a82020-03-12 16:20:12 +08002230 player->cmd.last_cmd = player->cmd.cur_cmd;
2231 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2232
hualing chen4b7c15d2020-04-07 16:13:48 +08002233 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002234 pthread_mutex_unlock(&player->lock);
2235 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002236}
2237/**\brief Start play video
2238 * \param[in] handle playback handle
2239 * \param[in] params video playback params,contains fmt and pid...
2240 * \retval DVR_SUCCESS On success
2241 * \return Error code
2242 */
hualing chen2aba4022020-03-02 13:49:55 +08002243int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002244 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002245
2246 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002247 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002248 return DVR_FAILURE;
2249 }
2250
hualing chen86e7d482020-01-16 15:13:33 +08002251 _start_playback_thread(handle);
2252 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002253 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002254 pthread_mutex_lock(&player->lock);
2255 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002256 AmTsPlayer_setVideoParams(player->handle, param);
2257 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002258
2259 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002260 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002261 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002262 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002263 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2264 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002265 }
2266 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002267 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2268 player->cmd.state = DVR_PLAYBACK_STATE_START;
2269 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002270 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002271 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002272 return DVR_SUCCESS;
2273}
2274/**\brief Stop play video
2275 * \param[in] handle playback handle
2276 * \retval DVR_SUCCESS On success
2277 * \return Error code
2278 */
hualing chen040df222020-01-17 13:35:02 +08002279int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2280 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002281
2282 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002283 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002284 return DVR_FAILURE;
2285 }
2286
hualing chen86e7d482020-01-16 15:13:33 +08002287 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002288 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2289 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002290 //destory thread
2291 _stop_playback_thread(handle);
2292 } else {
2293 //do nothing.audio is playing
2294 }
hualing chen7a56cba2020-04-14 14:09:27 +08002295
2296 DVR_PB_DG(1, "lock");
2297 pthread_mutex_lock(&player->lock);
2298
hualing chen87072a82020-03-12 16:20:12 +08002299 player->has_video = DVR_FALSE;
2300
2301 AmTsPlayer_stopVideoDecoding(player->handle);
2302 //playback_device_video_stop(player->handle);
2303
2304 player->cmd.last_cmd = player->cmd.cur_cmd;
2305 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2306
hualing chen4b7c15d2020-04-07 16:13:48 +08002307 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002308 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002309 return DVR_SUCCESS;
2310}
2311/**\brief Pause play
2312 * \param[in] handle playback handle
2313 * \param[in] flush whether its internal buffers should be flushed
2314 * \retval DVR_SUCCESS On success
2315 * \return Error code
2316 */
hualing chen040df222020-01-17 13:35:02 +08002317int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2318 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002319 UNDVRUSED(flush);
hualing chena540a7e2020-03-27 16:44:05 +08002320 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002321 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002322 return DVR_FAILURE;
2323 }
hualing chenf00cdc82020-06-10 14:23:35 +08002324 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2325 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002326 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002327 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002328 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002329 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002330 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002331 if (player->has_video)
2332 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002333 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002334 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002335
2336 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002337 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2338 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2339 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2340 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002341 } else {
2342 player->cmd.last_cmd = player->cmd.cur_cmd;
2343 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2344 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2345 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002346 }
hualing chen86e7d482020-01-16 15:13:33 +08002347 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002348 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002349
hualing chen86e7d482020-01-16 15:13:33 +08002350 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002351}
2352
hualing chen5cbe1a62020-02-10 16:36:36 +08002353//not add lock
2354static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2355{
2356 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2357
hualing chena540a7e2020-03-27 16:44:05 +08002358 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002359 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002360 return DVR_FAILURE;
2361 }
2362
hualing chen5cbe1a62020-02-10 16:36:36 +08002363 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002364 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002365 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002366 am_tsplayer_video_params vparams;
2367 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002368 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002369 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002370
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002371 memset(&vparams, 0, sizeof(vparams));
2372 memset(&aparams, 0, sizeof(aparams));
2373
hualing chendf118dd2020-05-21 15:49:11 +08002374 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002375 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002376 pthread_mutex_unlock(&player->lock);
2377
2378 switch (cmd) {
2379 case DVR_PLAYBACK_CMD_AVRESTART:
2380 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002381 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002382 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002383 break;
2384 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002385 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2386 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002387 break;
2388 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002389 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002390 break;
2391 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002392 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002393 break;
2394 case DVR_PLAYBACK_CMD_ARESTART:
2395 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002396 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002397 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002398 break;
2399 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002400 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002401 break;
2402 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002403 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002404 break;
2405 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002406 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2407 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2408 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002409 break;
2410 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002411 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2412 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002413 break;
2414 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002415 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2416 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002417 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002418 break;
2419 case DVR_PLAYBACK_CMD_STOP:
2420 break;
2421 case DVR_PLAYBACK_CMD_START:
2422 break;
2423 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002424 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2425 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002426 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002427 break;
2428 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002429 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2430 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002431 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002432 break;
2433 case DVR_PLAYBACK_CMD_FF:
2434 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002435 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002436 break;
2437 default:
2438 break;
2439 }
2440 return DVR_SUCCESS;
2441}
2442
2443/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002444 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002445 * \retval DVR_SUCCESS On success
2446 * \return Error code
2447 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002448int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2449 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002450 int pos = 0;
2451 uint64_t segmentid = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002452 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002453 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002454 return DVR_FAILURE;
2455 }
2456
hualing chen03fd4942021-07-15 15:56:41 +08002457 //get id and pos to check if we can seek to this pos
2458 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2459 if (segmentid != player->cur_segment_id ||
2460 (segmentid == player->cur_segment_id &&
2461 pos > _dvr_get_cur_time(handle))) {
2462 //first to seek new pos and to resume
2463 DVR_PB_DG(1, "seek new pos and to resume");
2464 dvr_playback_seek(handle, segmentid, pos);
2465 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002466 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002467 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002468 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002469 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002470 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002471 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2472 AmTsPlayer_resumeVideoDecoding(player->handle);
2473 }
2474 if (player->has_audio) {
2475 AmTsPlayer_resumeAudioDecoding(player->handle);
2476 }
2477 //check is has audio param,if has audio .we need start audio,
2478 //we will stop audio when ff fb, if reach end, we will pause.so we need
2479 //start audio when resume play
2480
2481 am_tsplayer_video_params vparams;
2482 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002483 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002484 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002485
2486 memset(&vparams, 0, sizeof(vparams));
2487 memset(&aparams, 0, sizeof(aparams));
hualing chendf118dd2020-05-21 15:49:11 +08002488 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002489 //valid audio pid, start audio
hualing chen969fe7b2021-05-26 15:13:17 +08002490 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2491 player->has_ad_audio = DVR_TRUE;
2492 DVR_PB_DG(1, "start ad audio");
2493 AmTsPlayer_setADParams(player->handle, &adparams);
2494 AmTsPlayer_enableADMix(player->handle);
2495 }
2496
hualing chenc70a8df2020-05-12 19:23:11 +08002497 if (player->has_audio == DVR_FALSE && VALID_PID(aparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chen266b9502020-04-04 17:39:39 +08002498 player->has_audio = DVR_TRUE;
2499 AmTsPlayer_setAudioParams(player->handle, &aparams);
2500 AmTsPlayer_startAudioDecoding(player->handle);
2501 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002502 DVR_PB_DG(1, "aparams.pid:%d player->has_audio:%d speed:%d", aparams.pid, player->has_audio, player->cmd.speed.speed.speed);
hualing chen266b9502020-04-04 17:39:39 +08002503 }
hualing chendf118dd2020-05-21 15:49:11 +08002504
hualing chen87072a82020-03-12 16:20:12 +08002505 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2506 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2507 player->cmd.state = DVR_PLAYBACK_STATE_START;
2508 player->state = DVR_PLAYBACK_STATE_START;
2509 } else {
2510 player->cmd.last_cmd = player->cmd.cur_cmd;
2511 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2512 player->cmd.state = DVR_PLAYBACK_STATE_START;
2513 player->state = DVR_PLAYBACK_STATE_START;
2514 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002515 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002516 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002517 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chene41f4372020-06-06 16:29:17 +08002518 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002519 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002520 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002521 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002522 }
hualing chen041c4092020-04-05 15:11:50 +08002523 if (player->has_audio)
2524 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002525 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002526 player->cmd.state = DVR_PLAYBACK_STATE_START;
2527 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002528 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2529 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002530 } else {
2531 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2532 {
2533 pthread_mutex_lock(&player->lock);
2534 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002535 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002536 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2537 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002538 if (player->has_video) {
2539 AmTsPlayer_resumeVideoDecoding(player->handle);
2540 }
2541 if (player->has_audio)
2542 AmTsPlayer_resumeAudioDecoding(player->handle);
2543
hualing chen041c4092020-04-05 15:11:50 +08002544 pthread_mutex_unlock(&player->lock);
2545 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002546 }
2547 return DVR_SUCCESS;
2548}
2549
hualing chena540a7e2020-03-27 16:44:05 +08002550static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2551
2552 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2553 DVR_PlaybackSegmentInfo_t *segment = NULL;
2554 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2555 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2556
2557 list_for_each_entry(segment, &player->segment_list, head)
2558 {
2559 if (segment->segment_id == segment_id) {
2560 cur_segment = segment;
2561 }
2562 if (segment->segment_id == set_seg_id) {
2563 set_segment = segment;
2564 }
2565 if (cur_segment != NULL && set_segment != NULL) {
2566 break;
2567 }
2568 }
2569 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002570 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002571 return DVR_TRUE;
2572 }
2573 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2574 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2575 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2576 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002577 DVR_PB_DG(1, "cur v[%d]a[%d] set v[%d]a[%d]",cur_segment->pids.video.pid,cur_segment->pids.audio.pid,set_segment->pids.video.pid,set_segment->pids.audio.pid);
hualing chena540a7e2020-03-27 16:44:05 +08002578 return DVR_TRUE;
2579 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002580 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002581 return DVR_FALSE;
2582}
2583
hualing chen03fd4942021-07-15 15:56:41 +08002584/**\brief set limit
2585 * \param[in] handle playback handle
2586 * \param[in] rec start time ms
2587 * \param[in] rec limit time ms
2588 * \retval DVR_SUCCESS On success
2589 * \return Error code
2590 */
2591int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint64_t time, int32_t limit)
2592{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2593
2594 if (player == NULL) {
2595 DVR_PB_DG(1, "player is NULL");
2596 return DVR_FAILURE;
2597 }
2598
2599 DVR_PB_DG(1, "time %lld limit: %d player->state:%d", time, limit, player->state);
2600 pthread_mutex_lock(&player->lock);
2601 player->rec_start = time;
2602 player->limit = limit;
2603 pthread_mutex_unlock(&player->lock);
2604 return DVR_SUCCESS;
2605}
2606
hualing chen5cbe1a62020-02-10 16:36:36 +08002607/**\brief seek
2608 * \param[in] handle playback handle
2609 * \param[in] time_offset time offset base cur segment
2610 * \retval DVR_SUCCESS On success
2611 * \return Error code
2612 */
hualing chencc91e1c2020-02-28 13:26:17 +08002613int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002614 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002615 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08002616 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002617 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002618 return DVR_FAILURE;
2619 }
2620
hualing chen4b7c15d2020-04-07 16:13:48 +08002621 DVR_PB_DG(1, "lock segment_id %llu cur id %llu time_offset %u cur end: %d player->state:%d", segment_id,player->cur_segment_id, (uint32_t)time_offset, _dvr_get_end_time(handle), player->state);
hualing chen86e7d482020-01-16 15:13:33 +08002622 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002623
hualing chen86e7d482020-01-16 15:13:33 +08002624 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002625 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002626 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002627
hualing chen5cbe1a62020-02-10 16:36:36 +08002628 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08002629 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08002630 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002631 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002632 pthread_mutex_unlock(&player->lock);
2633 return DVR_FAILURE;
2634 }
2635 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2636 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002637 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002638 //pthread_mutex_unlock(&player->lock);
2639 //return DVR_SUCCESS;
2640 time_offset = _dvr_get_end_time(handle);
2641 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002642 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002643 time_offset = _dvr_get_end_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08002644 //pthread_mutex_unlock(&player->lock);
2645 //return DVR_FAILURE;
2646 ret = DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08002647 }
2648 }
2649
hualing chen03fd4942021-07-15 15:56:41 +08002650 DVR_PB_DG(1, "seek open id[%lld]flag[0x%x] time_offset %u",
2651 player->cur_segment.segment_id,
2652 player->cur_segment.flags,
2653 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08002654 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002655 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2656 //forward playback.not seek end of file
2657 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2658 //default -2000ms
2659 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2660 }
hualing chen86e7d482020-01-16 15:13:33 +08002661 }
hualing chen2aba4022020-03-02 13:49:55 +08002662 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002663 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002664 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002665 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002666 DVR_PB_DG(0, "seek get offset by time offset, offset=%d time_offset %u",offset, time_offset);
hualing chen2aba4022020-03-02 13:49:55 +08002667 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002668 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002669
hualing chen2aba4022020-03-02 13:49:55 +08002670 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002671
2672 player->last_send_time_id = UINT64_MAX;
hualing chen03fd4942021-07-15 15:56:41 +08002673 player->last_segment_tatol = 0LL;
2674 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08002675 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002676 player->fffb_current = _dvr_time_getClock();
2677 player->fffb_start = player->fffb_current;
2678 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2679 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002680 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002681 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002682 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002683 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002684 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002685 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002686 }
hualing chen86e7d482020-01-16 15:13:33 +08002687 //stop play
hualing chen03fd4942021-07-15 15:56:41 +08002688 DVR_PB_DG(0, "seek stop play, not inject data has video[%d]audio[%d]",
2689 player->has_video, player->has_audio);
hualing chen266b9502020-04-04 17:39:39 +08002690 if (player->has_video) {
2691 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002692 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002693 }
2694
2695 if (player->has_audio) {
2696 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002697 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002698 }
hualing chendf118dd2020-05-21 15:49:11 +08002699 if (player->has_ad_audio) {
2700 player->has_ad_audio =DVR_FALSE;
2701 AmTsPlayer_disableADMix(player->handle);
2702 }
2703
hualing chen86e7d482020-01-16 15:13:33 +08002704 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002705 am_tsplayer_video_params vparams;
2706 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002707 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002708
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002709 memset(&vparams, 0, sizeof(vparams));
2710 memset(&aparams, 0, sizeof(aparams));
2711
hualing chen040df222020-01-17 13:35:02 +08002712 player->cur_segment_id = segment_id;
2713
2714 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002715 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002716 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002717 //start audio and video
Zhiqiang Han9adc9722020-11-11 18:38:10 +08002718 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08002719 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002720 DVR_PB_DG(0, "unlock seek start dvr play back start error, not found audio and video info");
hualing chen86e7d482020-01-16 15:13:33 +08002721 pthread_mutex_unlock(&player->lock);
2722 return -1;
2723 }
2724 //add
hualing chen040df222020-01-17 13:35:02 +08002725 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002726 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002727 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002728 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002729 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002730 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002731 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002732 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002733 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002734 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002735 }
hualing chen2aba4022020-03-02 13:49:55 +08002736 AmTsPlayer_setVideoParams(player->handle, &vparams);
2737 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002738 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2739 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2740 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2741 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2742 AmTsPlayer_stopFast(player->handle);
2743 }
hualing chen266b9502020-04-04 17:39:39 +08002744 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002745 }
hualing chene41f4372020-06-06 16:29:17 +08002746 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002747 player->has_ad_audio = DVR_TRUE;
2748 DVR_PB_DG(1, "start ad audio");
2749 AmTsPlayer_setADParams(player->handle, &adparams);
2750 AmTsPlayer_enableADMix(player->handle);
2751 }
hualing chen969fe7b2021-05-26 15:13:17 +08002752 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
2753 DVR_PB_DG(1, "start audio seek");
2754 AmTsPlayer_setAudioParams(player->handle, &aparams);
2755 AmTsPlayer_startAudioDecoding(player->handle);
2756 player->has_audio = DVR_TRUE;
2757 }
hualing chen86e7d482020-01-16 15:13:33 +08002758 }
hualing chene41f4372020-06-06 16:29:17 +08002759 if (player->state == DVR_PLAYBACK_STATE_PAUSE /*&&
hualing chen2aba4022020-03-02 13:49:55 +08002760 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2761 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2762 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chene41f4372020-06-06 16:29:17 +08002763 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))*/) {
hualing chen2aba4022020-03-02 13:49:55 +08002764 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2765 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen30423862021-04-16 14:39:12 +08002766 player->seek_pause = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002767 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002768 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2769 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002770 player->speed > 1.0f||
2771 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002772 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002773 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002774 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002775 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002776 player->cmd.last_cmd = player->cmd.cur_cmd;
2777 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2778 player->cmd.state = DVR_PLAYBACK_STATE_START;
2779 player->state = DVR_PLAYBACK_STATE_START;
2780 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002781 player->last_send_time_id = UINT64_MAX;
2782 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002783 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002784
2785 return DVR_SUCCESS;
2786}
hualing chen5cbe1a62020-02-10 16:36:36 +08002787
hualing chen5cbe1a62020-02-10 16:36:36 +08002788static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2789 //get cur time of segment
2790 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002791
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002792 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002793 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002794 return DVR_FAILURE;
2795 }
2796
hualing chen31140872020-03-25 12:29:26 +08002797 int64_t cache = 0;//defalut es buf cache 500ms
2798 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002799 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002800 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2801 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002802 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002803 DVR_PB_DG(1, "get cur time [%lld] cache:%lld cur id [%lld]last id [%lld] pb cache len [%d] [%lld]", cur, cache, player->cur_segment_id,player->last_send_time_id, player->ts_cache_len, pos);
hualing chen87072a82020-03-12 16:20:12 +08002804 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2805 cache = 0;
2806 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002807 int cur_time = (int)(cur > cache ? cur - cache : 0);
2808 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002809}
2810
hualing chen969fe7b2021-05-26 15:13:17 +08002811static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
2812 //get cur time of segment
2813 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2814
hualing chen03fd4942021-07-15 15:56:41 +08002815 if (player == NULL || player->handle == 0) {
hualing chen969fe7b2021-05-26 15:13:17 +08002816 DVR_PB_DG(1, "player is NULL");
2817 return DVR_FAILURE;
2818 }
2819
2820 int64_t cache = 0;//defalut es buf cache 500ms
2821 int cur_time = 0;
2822 AmTsPlayer_getDelayTime(player->handle, &cache);
2823 pthread_mutex_lock(&player->segment_lock);
2824 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2825 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
2826 pthread_mutex_unlock(&player->segment_lock);
2827 DVR_PB_DG(1, "get play 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);
2828 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2829 cache = 0;
2830 }
2831 if (cur > cache) {
2832 cur_time = (int)(cur - cache);
2833 *id = player->cur_segment_id;
2834 } else if (player->last_segment_tatol > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +08002835 cur_time = (int)(player->last_segment_tatol - (cache - cur));
2836 *id = player->last_segment_id;
2837 DVR_PB_DG(1, "get play cur time[%lld][%lld][%d]", player->last_segment_id, player->cur_segment_id, player->last_segment_tatol);
2838 } else {
2839 cur_time = 0;
2840 *id = player->cur_segment_id;
2841 }
2842
2843 return cur_time;
2844}
2845
hualing chencc91e1c2020-02-28 13:26:17 +08002846//get current segment current pcr time of read pos
2847static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2848 //get cur time of segment
2849 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002850
2851 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002852 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002853 return DVR_FAILURE;
2854 }
2855
hualing chen2aba4022020-03-02 13:49:55 +08002856 pthread_mutex_lock(&player->segment_lock);
2857 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002858 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002859 pthread_mutex_unlock(&player->segment_lock);
2860 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002861}
2862
hualing chen03fd4942021-07-15 15:56:41 +08002863DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
2864{
2865 //check is set limit info
2866 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2867
2868 if (player == NULL) {
2869 DVR_PB_DG(1, "player is NULL");
2870 return DVR_FALSE;
2871 }
2872 if (player->rec_start > 0 || player->limit > 0) {
2873 return DVR_TRUE;
2874 }
2875 return DVR_FALSE;
2876}
2877
2878/**\brief set DVR playback calculate expired time len
2879 * \param[in] handle, DVR playback session handle
2880 * \return DVR_SUCCESS on success
2881 * \return error code on failure
2882 */
2883int dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
2884{
2885 //calculate expired time to play
2886 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2887 uint64_t cur_time;
2888 int expired = 0;
2889 if (player == NULL) {
2890 DVR_PB_DG(1, "player is NULL");
2891 return expired;
2892 }
2893 if (player->rec_start < 0 || player->limit < 0) {
2894 return expired;
2895 }
2896 //get system time
2897 cur_time = _dvr_time_getClock();
2898 if ((cur_time - player->rec_start) > player->limit)
2899 expired = cur_time - player->rec_start - player->limit;
2900 DVR_PB_DG(1, "cur_time:%lld, rec start:%lld limit:%d c_r_diff:%lld",
2901 cur_time,
2902 player->rec_start,
2903 player->limit,
2904 cur_time - player->rec_start);
2905 return expired;
2906}
2907
2908/**\brief set DVR playback obsolete time
2909 * \param[in] handle, DVR playback session handle
2910 * \param[in] obsolete, obsolete len
2911 * \return DVR_SUCCESS on success
2912 * \return error code on failure
2913 */
2914int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
2915{
2916 int expired = 0;
2917 //calculate expired time to play
2918 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2919
2920 if (player == NULL) {
2921 DVR_PB_DG(1, "player is NULL");
2922 return DVR_FALSE;
2923 }
2924 //get system time
2925 pthread_mutex_lock(&player->lock);
2926 player->obsolete = obsolete;
2927 pthread_mutex_unlock(&player->lock);
2928 return expired;
2929}
2930
2931/**\brief update DVR playback newest segment duration
2932 * \param[in] handle, DVR playback session handle
2933 * \param[in] segmentid, newest segment id
2934 * \param[in] dur dur time ms
2935 * \return DVR_SUCCESS on success
2936 * \return error code on failure
2937 */
2938int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
2939uint64_t segmentid, int dur)
2940{
2941 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2942 DVR_PlaybackSegmentInfo_t *segment;
2943 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
2944
2945 if (player == NULL) {
2946 DVR_PB_DG(1, " player is NULL");
2947 return DVR_FAILURE;
2948 }
2949 //update the newest segment duration on timeshift mode
2950 list_for_each_entry(segment, &player->segment_list, head)
2951 {
2952 if (segment->segment_id == segmentid) {
2953 segment->duration = dur;
2954 break;
2955 }
2956 pre_segment = segment;
2957 }
2958
2959 return DVR_SUCCESS;
2960}
2961
2962static int dvr_playback_calculate_last_valid_segment(
2963 DVR_PlaybackHandle_t handle, uint64_t *segmentid, int *pos)
2964{
2965 int off = 0;
2966 uint64_t segment_id = 0;
2967 int pre_off = 0;
2968 uint64_t last_segment_id = 0;
2969 int expired = 0;
2970 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2971
2972 if (player == NULL) {
2973 DVR_PB_DG(1, "player is NULL");
2974 return DVR_FAILURE;
2975 }
2976 expired = dvr_playback_calculate_expiredlen(handle);
2977 DVR_PlaybackSegmentInfo_t *pseg;
2978 list_for_each_entry_reverse(pseg, &player->segment_list, head) {
2979 segment_id = pseg->segment_id;
2980
2981 if ((player->obsolete + pre_off + pseg->duration) > expired)
2982 break;
2983
2984 last_segment_id = pseg->segment_id;
2985 pre_off += pseg->duration;
2986 }
2987
2988 if (last_segment_id == segment_id) {
2989 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2990 off = expired;
2991 } else if (player->obsolete >= expired) {
2992 off = 0;
2993 } else {
2994 off = expired - pre_off - player->obsolete;
2995 }
2996 *segmentid = segment_id;
2997 *pos = off;
2998 return DVR_SUCCESS;
2999}
3000
hualing chen4b7c15d2020-04-07 16:13:48 +08003001#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003002//start replay
3003static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3004
3005 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3006 //calculate pcr seek time
3007 int t_diff = 0;
3008 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003009 uint64_t segmentid = 0;
3010 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003011 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003012 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003013 return DVR_FAILURE;
3014 }
3015
hualing chen5cbe1a62020-02-10 16:36:36 +08003016 if (player->fffb_start == -1) {
3017 //set fffb start time ms
3018 player->fffb_start = _dvr_time_getClock();
3019 player->fffb_current = player->fffb_start;
3020 //get segment current time pos
3021 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08003022 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
3023 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003024 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003025 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003026 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003027 } else {
3028 player->fffb_current = _dvr_time_getClock();
3029 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003030 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003031 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003032 if (seek_time <= 0) {
3033 //need seek to pre one segment
3034 seek_time = 0;
3035 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003036 //seek segment pos
3037 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003038 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003039 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003040 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3041 //set seek time to 0;
hualing chen03fd4942021-07-15 15:56:41 +08003042 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]",
3043 seek_time,
3044 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003045 seek_time = 0;
3046 }
hualing chen03fd4942021-07-15 15:56:41 +08003047 if (IS_FB(player->speed)
3048 && dvr_playback_check_limit(handle)) {
3049 //fb case.check expired time
3050 //get id and pos to check if we can seek to this pos
3051 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3052 //case cur id < segment id
3053 if (player->cur_segment_id < segmentid) {
3054 //expired ts data is player,return error
3055 //
3056 pthread_mutex_unlock(&player->segment_lock);
3057 return 0;
3058 } else if (player->cur_segment_id == segmentid) {
3059 //id is same,compare seek pos
3060 if (seek_time < pos) {
3061 //expired ts data is player,return error
3062 //
3063 pthread_mutex_unlock(&player->segment_lock);
3064 return 0;
3065 }
3066 }
3067 //case can play
3068 }
hualing chen041c4092020-04-05 15:11:50 +08003069 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3070 seek_time = 0;
3071 }
hualing chen2aba4022020-03-02 13:49:55 +08003072 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003073 } else {
3074 //
hualing chen4b7c15d2020-04-07 16:13:48 +08003075 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003076 }
hualing chen03fd4942021-07-15 15:56:41 +08003077 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
3078 seek_time,
3079 player->speed,
3080 player->cur_segment_id,
3081 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003082 }
hualing chen2aba4022020-03-02 13:49:55 +08003083 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003084}
3085
3086
3087//start replay
3088static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3089 //
3090 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003091
3092 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003093 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003094 return DVR_FAILURE;
3095 }
3096
hualing chen5cbe1a62020-02-10 16:36:36 +08003097 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003098 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003099 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08003100 AmTsPlayer_stopVideoDecoding(player->handle);
3101 }
3102 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003103 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003104 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003105 AmTsPlayer_stopAudioDecoding(player->handle);
3106 }
hualing chendf118dd2020-05-21 15:49:11 +08003107 if (player->has_ad_audio) {
3108 DVR_PB_DG(1, "fffb stop audio");
3109 player->has_ad_audio =DVR_FALSE;
3110 AmTsPlayer_disableADMix(player->handle);
3111 }
hualing chen2aba4022020-03-02 13:49:55 +08003112
hualing chen5cbe1a62020-02-10 16:36:36 +08003113 //start video and audio
3114
hualing chen2aba4022020-03-02 13:49:55 +08003115 am_tsplayer_video_params vparams;
3116 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003117 am_tsplayer_audio_params adparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003118
3119 memset(&vparams, 0, sizeof(vparams));
3120 memset(&aparams, 0, sizeof(aparams));
3121
hualing chen87072a82020-03-12 16:20:12 +08003122 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003123
3124 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08003125 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08003126 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003127 //start audio and video
3128 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
3129 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003130 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08003131 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003132 return -1;
3133 }
3134
3135 if (VALID_PID(vparams.pid)) {
3136 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08003137 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08003138 //DVR_PB_DG(1, "fffb start video and save last frame");
3139 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003140 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003141 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
3142 AmTsPlayer_setVideoParams(player->handle, &vparams);
3143 AmTsPlayer_startVideoDecoding(player->handle);
3144 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003145 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003146 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003147 }
hualing chen31140872020-03-25 12:29:26 +08003148 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08003149 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003150 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08003151 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003152 return 0;
3153}
3154
3155static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3156 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003157 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003158 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003159 return DVR_FAILURE;
3160 }
3161
3162 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08003163 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003164 pthread_mutex_lock(&player->lock);
3165
hualing chen2aba4022020-03-02 13:49:55 +08003166 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003167 DVR_PB_DG(1, "get lock speed [%f]id [%lld]seek_time[%d]", player->speed, player->cur_segment_id, seek_time);
hualing chen041c4092020-04-05 15:11:50 +08003168
hualing chen87072a82020-03-12 16:20:12 +08003169 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3170 //seek time set 0
3171 seek_time = 0;
3172 }
hualing chen041c4092020-04-05 15:11:50 +08003173 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003174 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3175 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003176 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003177 pthread_mutex_unlock(&player->lock);
3178 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003179 //send event here and pause
3180 DVR_Play_Notify_t notify;
3181 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003182 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003183 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003184 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08003185 DVR_PB_DG(1, "*******************send begin event speed [%f] cur [%d]", player->speed, _dvr_get_cur_time(handle));
hualing chen2aba4022020-03-02 13:49:55 +08003186 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003187 return DVR_SUCCESS;
3188 }
hualing chen2932d372020-04-29 13:44:00 +08003189 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003190 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003191 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003192 }
3193 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003194 _dvr_playback_fffb_replay(handle);
3195
3196 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003197 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003198
hualing chen5cbe1a62020-02-10 16:36:36 +08003199 return DVR_SUCCESS;
3200}
3201
hualing chen87072a82020-03-12 16:20:12 +08003202//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003203static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003204 //
3205 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003206
3207 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003208 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003209 return DVR_FAILURE;
3210 }
3211
hualing chen5cbe1a62020-02-10 16:36:36 +08003212 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003213 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003214 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003215 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003216 }
3217
3218 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003219 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003220 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003221 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003222 //start video and audio
3223
hualing chen2aba4022020-03-02 13:49:55 +08003224 am_tsplayer_video_params vparams;
3225 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003226 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08003227 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003228
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003229 memset(&vparams, 0, sizeof(vparams));
3230 memset(&aparams, 0, sizeof(aparams));
3231
hualing chen5cbe1a62020-02-10 16:36:36 +08003232 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08003233 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08003234 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003235 //start audio and video
3236 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003237 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003238 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003239 return -1;
3240 }
3241
3242 if (VALID_PID(vparams.pid)) {
3243 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003244 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003245 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003246 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003247 }
hualing chen266b9502020-04-04 17:39:39 +08003248 else {
hualing chen2aba4022020-03-02 13:49:55 +08003249 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003250 }
hualing chen2aba4022020-03-02 13:49:55 +08003251 AmTsPlayer_setVideoParams(player->handle, &vparams);
3252 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003253 }
hualing chena540a7e2020-03-27 16:44:05 +08003254
3255 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08003256 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08003257 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003258 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003259 } else {
hualing chendf118dd2020-05-21 15:49:11 +08003260 if (VALID_PID(adparams.pid)) {
3261 player->has_ad_audio = DVR_TRUE;
3262 DVR_PB_DG(1, "start ad audio");
3263 AmTsPlayer_setADParams(player->handle, &adparams);
3264 AmTsPlayer_enableADMix(player->handle);
3265 }
hualing chen969fe7b2021-05-26 15:13:17 +08003266 if (VALID_PID(aparams.pid)) {
3267 player->has_audio = DVR_TRUE;
3268 DVR_PB_DG(1, "start audio");
3269 AmTsPlayer_setAudioParams(player->handle, &aparams);
3270 AmTsPlayer_startAudioDecoding(player->handle);
3271 }
hualing chendf118dd2020-05-21 15:49:11 +08003272
hualing chen7a56cba2020-04-14 14:09:27 +08003273 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003274 AmTsPlayer_stopFast(player->handle);
3275 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3276 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3277 }
hualing chen2aba4022020-03-02 13:49:55 +08003278 player->cmd.last_cmd = player->cmd.cur_cmd;
3279 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003280 player->cmd.state = DVR_PLAYBACK_STATE_START;
3281 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08003282 return 0;
3283}
3284
3285
hualing chenb31a6c62020-01-13 17:27:00 +08003286/**\brief Set play speed
3287 * \param[in] handle playback handle
3288 * \param[in] speed playback speed
3289 * \retval DVR_SUCCESS On success
3290 * \return Error code
3291 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003292int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3293
3294 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003295
3296 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003297 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003298 return DVR_FAILURE;
3299 }
3300
hualing chen4b7c15d2020-04-07 16:13:48 +08003301 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003302 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003303 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003304 return DVR_FAILURE;
3305 }
hualing chenf00cdc82020-06-10 14:23:35 +08003306 if (speed.speed.speed == player->cmd.speed.speed.speed) {
3307 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
3308 return DVR_SUCCESS;
3309 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003310 pthread_mutex_lock(&player->lock);
3311 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3312 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3313 player->cmd.last_cmd = player->cmd.cur_cmd;
3314 }
hualing chene41f4372020-06-06 16:29:17 +08003315
hualing chen31140872020-03-25 12:29:26 +08003316 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003317 IS_KERNEL_SPEED(speed.speed.speed) ) {
3318 //case 1. not start play.only set speed
3319 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3320 //only set speed.and return;
3321 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3322 player->cmd.speed.speed = speed.speed;
3323 player->speed = (float)speed.speed.speed/(float)100;
3324 player->fffb_play = DVR_FALSE;
3325 pthread_mutex_unlock(&player->lock);
3326 return DVR_SUCCESS;
3327 }
3328 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003329 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3330 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003331 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003332 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3333 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003334 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003335 AmTsPlayer_stopFast(player->handle);
3336 pthread_mutex_unlock(&player->lock);
3337 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
3338 pthread_mutex_lock(&player->lock);
3339 } else {
3340 //set play speed and if audio is start, stop audio.
3341 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003342 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003343 AmTsPlayer_stopAudioDecoding(player->handle);
3344 player->has_audio = DVR_FALSE;
3345 }
hualing chenb96aa2c2020-04-15 14:13:53 +08003346 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003347 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003348 }
hualing chenbcada022020-04-22 14:27:01 +08003349 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003350 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003351 player->cmd.speed.speed = speed.speed;
3352 player->speed = (float)speed.speed.speed/(float)100;
3353 pthread_mutex_unlock(&player->lock);
3354 return DVR_SUCCESS;
3355 }
hualing chen31140872020-03-25 12:29:26 +08003356 //case 3 fffb mode
3357 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3358 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3359 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003360 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003361 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003362 player->cmd.speed.speed = speed.speed;
3363 player->speed = (float)speed.speed.speed/(float)100;
3364 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003365 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003366 pthread_mutex_unlock(&player->lock);
3367 return DVR_SUCCESS;
3368 }
3369 }
3370 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003371 IS_KERNEL_SPEED(speed.speed.speed)) {
3372 //case 1. cur speed is kernel support speed,set kernel speed.
3373 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003374 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003375 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3376 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003377 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003378 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08003379 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003380 } else {
3381 //set play speed and if audio is start, stop audio.
3382 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003383 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003384 AmTsPlayer_stopAudioDecoding(player->handle);
3385 player->has_audio = DVR_FALSE;
3386 }
hualing chenf00cdc82020-06-10 14:23:35 +08003387 DVR_PB_DG(1, "start fast");
3388 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003389 }
hualing chena540a7e2020-03-27 16:44:05 +08003390 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003391 player->cmd.speed.speed = speed.speed;
3392 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003393 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003394 pthread_mutex_unlock(&player->lock);
3395 return DVR_SUCCESS;
3396 }
3397 //case 2 fffb mode
3398 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3399 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3400 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003401 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003402 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003403 player->cmd.speed.speed = speed.speed;
3404 player->speed = (float)speed.speed.speed/(float)100;
3405 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08003406 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003407 pthread_mutex_unlock(&player->lock);
3408 return DVR_SUCCESS;
3409 }
hualing chen31140872020-03-25 12:29:26 +08003410 }
hualing chena540a7e2020-03-27 16:44:05 +08003411 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3412 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003413 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003414 } else {
hualing chen31140872020-03-25 12:29:26 +08003415 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003416 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3417 else
3418 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003419 player->fffb_play = DVR_TRUE;
3420 }
3421 DVR_Bool_t init_last_time = DVR_FALSE;
3422 if (player->speed > 0.0f && speed.speed.speed < 0) {
3423 init_last_time = DVR_TRUE;
3424 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3425 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003426 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003427 player->cmd.speed.mode = speed.mode;
3428 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003429 player->speed = (float)speed.speed.speed/(float)100;
3430 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003431 _dvr_init_fffb_t(handle);
3432 if (init_last_time == DVR_TRUE)
3433 player->last_send_time_id = UINT64_MAX;
3434
hualing chen87072a82020-03-12 16:20:12 +08003435 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003436 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3437 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003438 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003439 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003440 _dvr_playback_replay(handle, DVR_FALSE);
3441 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3442 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3443 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003444 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003445 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003446 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003447 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003448 return DVR_SUCCESS;
3449}
hualing chen2932d372020-04-29 13:44:00 +08003450
hualing chenb31a6c62020-01-13 17:27:00 +08003451/**\brief Get playback status
3452 * \param[in] handle playback handle
3453 * \param[out] p_status playback status
3454 * \retval DVR_SUCCESS On success
3455 * \return Error code
3456 */
hualing chen2932d372020-04-29 13:44:00 +08003457static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3458 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003459//
3460 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003461 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003462 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003463 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003464 return DVR_FAILURE;
3465 }
hualing chen2932d372020-04-29 13:44:00 +08003466 if (is_lock ==DVR_TRUE)
3467 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003468 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003469 //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 +08003470 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3471 player->state == DVR_PLAYBACK_STATE_START) {
3472 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3473 }
hualing chen041c4092020-04-05 15:11:50 +08003474
hualing chencc91e1c2020-02-28 13:26:17 +08003475 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003476 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003477
3478 if (CONTROL_SPEED_ENABLE == 1) {
3479 if (player->con_spe.ply_sta < 0) {
hualing chen03fd4942021-07-15 15:56:41 +08003480 DVR_PB_DG(1, "player dur[%lld] sta[%lld] cur[%d] -----reinit",
3481 player->con_spe.ply_dur,
3482 player->con_spe.ply_sta,
3483 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003484 player->con_spe.ply_sta = p_status->time_cur;
3485 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3486 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
hualing chen03fd4942021-07-15 15:56:41 +08003487 DVR_PB_DG(1, "player dur[%lld] sta[%lld] cur[%d]",
3488 player->con_spe.ply_dur,
3489 player->con_spe.ply_sta,
3490 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003491 player->con_spe.ply_sta = p_status->time_cur;
3492 }
3493
3494 if (player->con_spe.sys_sta == 0) {
3495 player->con_spe.sys_sta = _dvr_time_getClock();
3496 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
3497 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
3498 player->con_spe.sys_sta = _dvr_time_getClock();
3499 }
3500 }
3501
hualing chen4b7c15d2020-04-07 16:13:48 +08003502 if (player->last_send_time_id == UINT64_MAX) {
3503 player->last_send_time_id = player->cur_segment_id;
3504 player->last_cur_time = p_status->time_cur;
3505 }
3506 if (player->last_send_time_id == player->cur_segment_id) {
3507 if (player->speed > 0.0f ) {
3508 //ff
3509 if (p_status->time_cur < player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003510 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]",
3511 player->last_cur_time,
3512 p_status->time_cur,
3513 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08003514 p_status->time_cur = player->last_cur_time;
3515 } else {
3516 player->last_cur_time = p_status->time_cur;
3517 }
hualing chene41f4372020-06-06 16:29:17 +08003518 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003519 //fb
3520 if (p_status->time_cur > player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003521 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]",
3522 player->last_cur_time,
3523 p_status->time_cur,
3524 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08003525 p_status->time_cur = player->last_cur_time;
3526 } else {
3527 player->last_cur_time = p_status->time_cur;
3528 }
3529 }
hualing chend241c7a2021-06-22 13:34:27 +08003530 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08003531 player->last_cur_time = p_status->time_cur;
3532 }
hualing chen969fe7b2021-05-26 15:13:17 +08003533 player->last_send_time_id = segment_id;
3534 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003535
hualing chen5cbe1a62020-02-10 16:36:36 +08003536 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003537 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003538 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003539 DVR_PB_DG(1, "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 +08003540 _dvr_playback_state_toString(player->state),
3541 _dvr_playback_state_toString(p_status->state),
3542 p_status->time_cur, p_status->time_end,
3543 p_status->segment_id,player->play_flag,
3544 player->speed,
3545 is_lock);
hualing chen2932d372020-04-29 13:44:00 +08003546 if (is_lock ==DVR_TRUE)
3547 pthread_mutex_unlock(&player->lock);
3548 return DVR_SUCCESS;
3549}
3550
3551
3552/**\brief Get playback status
3553 * \param[in] handle playback handle
3554 * \param[out] p_status playback status
3555 * \retval DVR_SUCCESS On success
3556 * \return Error code
3557 */
3558int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3559 DVR_PlaybackStatus_t *p_status) {
3560//
3561 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3562
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003563 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3564
hualing chen2932d372020-04-29 13:44:00 +08003565 if (player == NULL) {
3566 DVR_PB_DG(1, "player is NULL");
3567 return DVR_FAILURE;
3568 }
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003569 pthread_mutex_lock(&player->lock);
3570 if (!player->has_video && !player->has_audio)
3571 p_status->time_cur = 0;
3572 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003573
hualing chenb31a6c62020-01-13 17:27:00 +08003574 return DVR_SUCCESS;
3575}
3576
hualing chen040df222020-01-17 13:35:02 +08003577void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3578 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003579 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3580 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3581 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3582 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3583 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3584 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3585 DVR_PB_DG(1, "segment sub apid: 0x%x sub afmt:0x%x", segment->pids.ad.pid,segment->pids.ad.format);
hualing chen86e7d482020-01-16 15:13:33 +08003586 }
hualing chenb31a6c62020-01-13 17:27:00 +08003587}
3588
hualing chen5cbe1a62020-02-10 16:36:36 +08003589int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003590 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003591
hualing chena540a7e2020-03-27 16:44:05 +08003592 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003593 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003594 return DVR_FAILURE;
3595 }
3596
hualing chen040df222020-01-17 13:35:02 +08003597 DVR_PlaybackSegmentInfo_t *segment;
3598 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003599 {
hualing chen040df222020-01-17 13:35:02 +08003600 if (segment_id >= 0) {
3601 if (segment->segment_id == segment_id) {
3602 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003603 break;
3604 }
3605 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003606 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003607 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003608 }
3609 }
3610 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003611}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003612
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003613int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003614{
3615 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3616 DVR_RETURN_IF_FALSE(player);
3617 DVR_RETURN_IF_FALSE(func);
3618
hualing chen4b7c15d2020-04-07 16:13:48 +08003619 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003620 pthread_mutex_lock(&player->lock);
3621
3622 player->dec_func = func;
3623 player->dec_userdata = userdata;
3624
3625 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003626 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003627 return DVR_SUCCESS;
3628}
3629
3630int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3631{
3632 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3633 DVR_RETURN_IF_FALSE(player);
3634 DVR_RETURN_IF_FALSE(p_secure_buf);
3635 DVR_RETURN_IF_FALSE(len);
3636
hualing chen4b7c15d2020-04-07 16:13:48 +08003637 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003638 pthread_mutex_lock(&player->lock);
3639
3640 player->is_secure_mode = 1;
3641 player->secure_buffer = p_secure_buf;
3642 player->secure_buffer_size = len;
3643
3644 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003645 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003646 return DVR_SUCCESS;
3647}