blob: efabb6fde487ef2596da85b9584c9896a3fd8cbc [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 chena991aa82021-08-16 10:21:15 +08002457 if (dvr_playback_check_limit(handle)) {
2458 //get id and pos to check if we can seek to this pos
2459 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2460 if (segmentid != player->cur_segment_id ||
2461 (segmentid == player->cur_segment_id &&
2462 pos > _dvr_get_cur_time(handle))) {
2463 //first to seek new pos and to resume
2464 DVR_PB_DG(1, "seek new pos and to resume");
2465 dvr_playback_seek(handle, segmentid, pos);
2466 }
hualing chen03fd4942021-07-15 15:56:41 +08002467 }
hualing chena991aa82021-08-16 10:21:15 +08002468
hualing chen5cbe1a62020-02-10 16:36:36 +08002469 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002470 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002471 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002472 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002473 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002474 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2475 AmTsPlayer_resumeVideoDecoding(player->handle);
2476 }
2477 if (player->has_audio) {
2478 AmTsPlayer_resumeAudioDecoding(player->handle);
2479 }
2480 //check is has audio param,if has audio .we need start audio,
2481 //we will stop audio when ff fb, if reach end, we will pause.so we need
2482 //start audio when resume play
2483
2484 am_tsplayer_video_params vparams;
2485 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002486 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002487 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002488
2489 memset(&vparams, 0, sizeof(vparams));
2490 memset(&aparams, 0, sizeof(aparams));
hualing chendf118dd2020-05-21 15:49:11 +08002491 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002492 //valid audio pid, start audio
hualing chen969fe7b2021-05-26 15:13:17 +08002493 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2494 player->has_ad_audio = DVR_TRUE;
2495 DVR_PB_DG(1, "start ad audio");
2496 AmTsPlayer_setADParams(player->handle, &adparams);
2497 AmTsPlayer_enableADMix(player->handle);
2498 }
2499
hualing chenc70a8df2020-05-12 19:23:11 +08002500 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 +08002501 player->has_audio = DVR_TRUE;
2502 AmTsPlayer_setAudioParams(player->handle, &aparams);
2503 AmTsPlayer_startAudioDecoding(player->handle);
2504 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002505 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 +08002506 }
hualing chendf118dd2020-05-21 15:49:11 +08002507
hualing chen87072a82020-03-12 16:20:12 +08002508 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2509 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2510 player->cmd.state = DVR_PLAYBACK_STATE_START;
2511 player->state = DVR_PLAYBACK_STATE_START;
2512 } else {
2513 player->cmd.last_cmd = player->cmd.cur_cmd;
2514 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2515 player->cmd.state = DVR_PLAYBACK_STATE_START;
2516 player->state = DVR_PLAYBACK_STATE_START;
2517 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002518 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002519 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002520 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chene41f4372020-06-06 16:29:17 +08002521 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002522 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002523 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002524 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002525 }
hualing chen041c4092020-04-05 15:11:50 +08002526 if (player->has_audio)
2527 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002528 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002529 player->cmd.state = DVR_PLAYBACK_STATE_START;
2530 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002531 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2532 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002533 } else {
2534 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2535 {
2536 pthread_mutex_lock(&player->lock);
2537 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002538 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002539 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2540 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002541 if (player->has_video) {
2542 AmTsPlayer_resumeVideoDecoding(player->handle);
2543 }
2544 if (player->has_audio)
2545 AmTsPlayer_resumeAudioDecoding(player->handle);
2546
hualing chen041c4092020-04-05 15:11:50 +08002547 pthread_mutex_unlock(&player->lock);
2548 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002549 }
2550 return DVR_SUCCESS;
2551}
2552
hualing chena540a7e2020-03-27 16:44:05 +08002553static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2554
2555 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2556 DVR_PlaybackSegmentInfo_t *segment = NULL;
2557 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2558 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2559
2560 list_for_each_entry(segment, &player->segment_list, head)
2561 {
2562 if (segment->segment_id == segment_id) {
2563 cur_segment = segment;
2564 }
2565 if (segment->segment_id == set_seg_id) {
2566 set_segment = segment;
2567 }
2568 if (cur_segment != NULL && set_segment != NULL) {
2569 break;
2570 }
2571 }
2572 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002573 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002574 return DVR_TRUE;
2575 }
2576 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2577 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2578 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2579 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002580 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 +08002581 return DVR_TRUE;
2582 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002583 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002584 return DVR_FALSE;
2585}
2586
hualing chen03fd4942021-07-15 15:56:41 +08002587/**\brief set limit
2588 * \param[in] handle playback handle
2589 * \param[in] rec start time ms
2590 * \param[in] rec limit time ms
2591 * \retval DVR_SUCCESS On success
2592 * \return Error code
2593 */
2594int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint64_t time, int32_t limit)
2595{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2596
2597 if (player == NULL) {
2598 DVR_PB_DG(1, "player is NULL");
2599 return DVR_FAILURE;
2600 }
2601
2602 DVR_PB_DG(1, "time %lld limit: %d player->state:%d", time, limit, player->state);
2603 pthread_mutex_lock(&player->lock);
2604 player->rec_start = time;
2605 player->limit = limit;
2606 pthread_mutex_unlock(&player->lock);
2607 return DVR_SUCCESS;
2608}
2609
hualing chen5cbe1a62020-02-10 16:36:36 +08002610/**\brief seek
2611 * \param[in] handle playback handle
2612 * \param[in] time_offset time offset base cur segment
2613 * \retval DVR_SUCCESS On success
2614 * \return Error code
2615 */
hualing chencc91e1c2020-02-28 13:26:17 +08002616int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002617 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002618 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08002619 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002620 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002621 return DVR_FAILURE;
2622 }
2623
hualing chen4b7c15d2020-04-07 16:13:48 +08002624 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 +08002625 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002626
hualing chen86e7d482020-01-16 15:13:33 +08002627 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002628 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002629 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002630
hualing chen5cbe1a62020-02-10 16:36:36 +08002631 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08002632 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08002633 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002634 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002635 pthread_mutex_unlock(&player->lock);
2636 return DVR_FAILURE;
2637 }
2638 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2639 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002640 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002641 //pthread_mutex_unlock(&player->lock);
2642 //return DVR_SUCCESS;
2643 time_offset = _dvr_get_end_time(handle);
2644 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002645 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002646 time_offset = _dvr_get_end_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08002647 //pthread_mutex_unlock(&player->lock);
2648 //return DVR_FAILURE;
2649 ret = DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08002650 }
2651 }
2652
hualing chen03fd4942021-07-15 15:56:41 +08002653 DVR_PB_DG(1, "seek open id[%lld]flag[0x%x] time_offset %u",
2654 player->cur_segment.segment_id,
2655 player->cur_segment.flags,
2656 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08002657 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002658 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2659 //forward playback.not seek end of file
2660 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2661 //default -2000ms
2662 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2663 }
hualing chen86e7d482020-01-16 15:13:33 +08002664 }
hualing chen2aba4022020-03-02 13:49:55 +08002665 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002666 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002667 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002668 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002669 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 +08002670 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002671 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002672
hualing chen2aba4022020-03-02 13:49:55 +08002673 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002674
2675 player->last_send_time_id = UINT64_MAX;
hualing chen03fd4942021-07-15 15:56:41 +08002676 player->last_segment_tatol = 0LL;
2677 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08002678 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002679 player->fffb_current = _dvr_time_getClock();
2680 player->fffb_start = player->fffb_current;
2681 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2682 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002683 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002684 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002685 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002686 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002687 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002688 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002689 }
hualing chen86e7d482020-01-16 15:13:33 +08002690 //stop play
hualing chen03fd4942021-07-15 15:56:41 +08002691 DVR_PB_DG(0, "seek stop play, not inject data has video[%d]audio[%d]",
2692 player->has_video, player->has_audio);
hualing chen266b9502020-04-04 17:39:39 +08002693 if (player->has_video) {
2694 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002695 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002696 }
2697
2698 if (player->has_audio) {
2699 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002700 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002701 }
hualing chendf118dd2020-05-21 15:49:11 +08002702 if (player->has_ad_audio) {
2703 player->has_ad_audio =DVR_FALSE;
2704 AmTsPlayer_disableADMix(player->handle);
2705 }
2706
hualing chen86e7d482020-01-16 15:13:33 +08002707 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002708 am_tsplayer_video_params vparams;
2709 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002710 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002711
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002712 memset(&vparams, 0, sizeof(vparams));
2713 memset(&aparams, 0, sizeof(aparams));
2714
hualing chen040df222020-01-17 13:35:02 +08002715 player->cur_segment_id = segment_id;
2716
2717 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002718 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002719 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002720 //start audio and video
Zhiqiang Han9adc9722020-11-11 18:38:10 +08002721 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08002722 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002723 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 +08002724 pthread_mutex_unlock(&player->lock);
2725 return -1;
2726 }
2727 //add
hualing chen040df222020-01-17 13:35:02 +08002728 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002729 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002730 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002731 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002732 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002733 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002734 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002735 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002736 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002737 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002738 }
hualing chen2aba4022020-03-02 13:49:55 +08002739 AmTsPlayer_setVideoParams(player->handle, &vparams);
2740 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002741 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2742 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2743 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2744 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2745 AmTsPlayer_stopFast(player->handle);
2746 }
hualing chen266b9502020-04-04 17:39:39 +08002747 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002748 }
hualing chene41f4372020-06-06 16:29:17 +08002749 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002750 player->has_ad_audio = DVR_TRUE;
2751 DVR_PB_DG(1, "start ad audio");
2752 AmTsPlayer_setADParams(player->handle, &adparams);
2753 AmTsPlayer_enableADMix(player->handle);
2754 }
hualing chen969fe7b2021-05-26 15:13:17 +08002755 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
2756 DVR_PB_DG(1, "start audio seek");
2757 AmTsPlayer_setAudioParams(player->handle, &aparams);
2758 AmTsPlayer_startAudioDecoding(player->handle);
2759 player->has_audio = DVR_TRUE;
2760 }
hualing chen86e7d482020-01-16 15:13:33 +08002761 }
hualing chene41f4372020-06-06 16:29:17 +08002762 if (player->state == DVR_PLAYBACK_STATE_PAUSE /*&&
hualing chen2aba4022020-03-02 13:49:55 +08002763 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2764 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2765 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chene41f4372020-06-06 16:29:17 +08002766 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))*/) {
hualing chen2aba4022020-03-02 13:49:55 +08002767 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2768 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen30423862021-04-16 14:39:12 +08002769 player->seek_pause = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002770 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002771 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2772 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002773 player->speed > 1.0f||
2774 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002775 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002776 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002777 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002778 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002779 player->cmd.last_cmd = player->cmd.cur_cmd;
2780 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2781 player->cmd.state = DVR_PLAYBACK_STATE_START;
2782 player->state = DVR_PLAYBACK_STATE_START;
2783 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002784 player->last_send_time_id = UINT64_MAX;
2785 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002786 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002787
2788 return DVR_SUCCESS;
2789}
hualing chen5cbe1a62020-02-10 16:36:36 +08002790
hualing chen5cbe1a62020-02-10 16:36:36 +08002791static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2792 //get cur time of segment
2793 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002794
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002795 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002796 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002797 return DVR_FAILURE;
2798 }
2799
hualing chen31140872020-03-25 12:29:26 +08002800 int64_t cache = 0;//defalut es buf cache 500ms
2801 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002802 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002803 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2804 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002805 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002806 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 +08002807 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2808 cache = 0;
2809 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002810 int cur_time = (int)(cur > cache ? cur - cache : 0);
2811 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002812}
2813
hualing chen969fe7b2021-05-26 15:13:17 +08002814static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
2815 //get cur time of segment
2816 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2817
hualing chen03fd4942021-07-15 15:56:41 +08002818 if (player == NULL || player->handle == 0) {
hualing chen969fe7b2021-05-26 15:13:17 +08002819 DVR_PB_DG(1, "player is NULL");
2820 return DVR_FAILURE;
2821 }
2822
2823 int64_t cache = 0;//defalut es buf cache 500ms
2824 int cur_time = 0;
2825 AmTsPlayer_getDelayTime(player->handle, &cache);
2826 pthread_mutex_lock(&player->segment_lock);
2827 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2828 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
2829 pthread_mutex_unlock(&player->segment_lock);
2830 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);
2831 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2832 cache = 0;
2833 }
2834 if (cur > cache) {
2835 cur_time = (int)(cur - cache);
2836 *id = player->cur_segment_id;
2837 } else if (player->last_segment_tatol > 0) {
hualing chen969fe7b2021-05-26 15:13:17 +08002838 cur_time = (int)(player->last_segment_tatol - (cache - cur));
2839 *id = player->last_segment_id;
2840 DVR_PB_DG(1, "get play cur time[%lld][%lld][%d]", player->last_segment_id, player->cur_segment_id, player->last_segment_tatol);
2841 } else {
2842 cur_time = 0;
2843 *id = player->cur_segment_id;
2844 }
2845
2846 return cur_time;
2847}
2848
hualing chencc91e1c2020-02-28 13:26:17 +08002849//get current segment current pcr time of read pos
2850static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2851 //get cur time of segment
2852 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002853
2854 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002855 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002856 return DVR_FAILURE;
2857 }
2858
hualing chen2aba4022020-03-02 13:49:55 +08002859 pthread_mutex_lock(&player->segment_lock);
2860 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002861 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002862 pthread_mutex_unlock(&player->segment_lock);
2863 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002864}
2865
hualing chen03fd4942021-07-15 15:56:41 +08002866DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
2867{
2868 //check is set limit info
2869 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2870
2871 if (player == NULL) {
2872 DVR_PB_DG(1, "player is NULL");
2873 return DVR_FALSE;
2874 }
2875 if (player->rec_start > 0 || player->limit > 0) {
2876 return DVR_TRUE;
2877 }
2878 return DVR_FALSE;
2879}
2880
2881/**\brief set DVR playback calculate expired time len
2882 * \param[in] handle, DVR playback session handle
2883 * \return DVR_SUCCESS on success
2884 * \return error code on failure
2885 */
2886int dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
2887{
2888 //calculate expired time to play
2889 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2890 uint64_t cur_time;
2891 int expired = 0;
2892 if (player == NULL) {
2893 DVR_PB_DG(1, "player is NULL");
2894 return expired;
2895 }
2896 if (player->rec_start < 0 || player->limit < 0) {
2897 return expired;
2898 }
2899 //get system time
2900 cur_time = _dvr_time_getClock();
2901 if ((cur_time - player->rec_start) > player->limit)
2902 expired = cur_time - player->rec_start - player->limit;
2903 DVR_PB_DG(1, "cur_time:%lld, rec start:%lld limit:%d c_r_diff:%lld",
2904 cur_time,
2905 player->rec_start,
2906 player->limit,
2907 cur_time - player->rec_start);
2908 return expired;
2909}
2910
2911/**\brief set DVR playback obsolete time
2912 * \param[in] handle, DVR playback session handle
2913 * \param[in] obsolete, obsolete len
2914 * \return DVR_SUCCESS on success
2915 * \return error code on failure
2916 */
2917int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
2918{
2919 int expired = 0;
2920 //calculate expired time to play
2921 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2922
2923 if (player == NULL) {
2924 DVR_PB_DG(1, "player is NULL");
2925 return DVR_FALSE;
2926 }
2927 //get system time
2928 pthread_mutex_lock(&player->lock);
2929 player->obsolete = obsolete;
2930 pthread_mutex_unlock(&player->lock);
2931 return expired;
2932}
2933
2934/**\brief update DVR playback newest segment duration
2935 * \param[in] handle, DVR playback session handle
2936 * \param[in] segmentid, newest segment id
2937 * \param[in] dur dur time ms
2938 * \return DVR_SUCCESS on success
2939 * \return error code on failure
2940 */
2941int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
2942uint64_t segmentid, int dur)
2943{
2944 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2945 DVR_PlaybackSegmentInfo_t *segment;
2946 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
2947
2948 if (player == NULL) {
2949 DVR_PB_DG(1, " player is NULL");
2950 return DVR_FAILURE;
2951 }
2952 //update the newest segment duration on timeshift mode
2953 list_for_each_entry(segment, &player->segment_list, head)
2954 {
2955 if (segment->segment_id == segmentid) {
2956 segment->duration = dur;
2957 break;
2958 }
2959 pre_segment = segment;
2960 }
2961
2962 return DVR_SUCCESS;
2963}
2964
2965static int dvr_playback_calculate_last_valid_segment(
2966 DVR_PlaybackHandle_t handle, uint64_t *segmentid, int *pos)
2967{
2968 int off = 0;
2969 uint64_t segment_id = 0;
2970 int pre_off = 0;
2971 uint64_t last_segment_id = 0;
2972 int expired = 0;
2973 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2974
2975 if (player == NULL) {
2976 DVR_PB_DG(1, "player is NULL");
2977 return DVR_FAILURE;
2978 }
2979 expired = dvr_playback_calculate_expiredlen(handle);
2980 DVR_PlaybackSegmentInfo_t *pseg;
2981 list_for_each_entry_reverse(pseg, &player->segment_list, head) {
2982 segment_id = pseg->segment_id;
2983
2984 if ((player->obsolete + pre_off + pseg->duration) > expired)
2985 break;
2986
2987 last_segment_id = pseg->segment_id;
2988 pre_off += pseg->duration;
2989 }
2990
2991 if (last_segment_id == segment_id) {
2992 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2993 off = expired;
2994 } else if (player->obsolete >= expired) {
2995 off = 0;
2996 } else {
2997 off = expired - pre_off - player->obsolete;
2998 }
2999 *segmentid = segment_id;
3000 *pos = off;
3001 return DVR_SUCCESS;
3002}
3003
hualing chen4b7c15d2020-04-07 16:13:48 +08003004#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003005//start replay
3006static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3007
3008 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3009 //calculate pcr seek time
3010 int t_diff = 0;
3011 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003012 uint64_t segmentid = 0;
3013 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003014 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003015 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003016 return DVR_FAILURE;
3017 }
3018
hualing chen5cbe1a62020-02-10 16:36:36 +08003019 if (player->fffb_start == -1) {
3020 //set fffb start time ms
3021 player->fffb_start = _dvr_time_getClock();
3022 player->fffb_current = player->fffb_start;
3023 //get segment current time pos
3024 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08003025 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
3026 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003027 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003028 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003029 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003030 } else {
3031 player->fffb_current = _dvr_time_getClock();
3032 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003033 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003034 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003035 if (seek_time <= 0) {
3036 //need seek to pre one segment
3037 seek_time = 0;
3038 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003039 //seek segment pos
3040 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003041 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003042 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003043 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3044 //set seek time to 0;
hualing chen03fd4942021-07-15 15:56:41 +08003045 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]",
3046 seek_time,
3047 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003048 seek_time = 0;
3049 }
hualing chen03fd4942021-07-15 15:56:41 +08003050 if (IS_FB(player->speed)
3051 && dvr_playback_check_limit(handle)) {
3052 //fb case.check expired time
3053 //get id and pos to check if we can seek to this pos
3054 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3055 //case cur id < segment id
3056 if (player->cur_segment_id < segmentid) {
3057 //expired ts data is player,return error
3058 //
3059 pthread_mutex_unlock(&player->segment_lock);
3060 return 0;
3061 } else if (player->cur_segment_id == segmentid) {
3062 //id is same,compare seek pos
3063 if (seek_time < pos) {
3064 //expired ts data is player,return error
3065 //
3066 pthread_mutex_unlock(&player->segment_lock);
3067 return 0;
3068 }
3069 }
3070 //case can play
3071 }
hualing chen041c4092020-04-05 15:11:50 +08003072 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3073 seek_time = 0;
3074 }
hualing chen2aba4022020-03-02 13:49:55 +08003075 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003076 } else {
3077 //
hualing chen4b7c15d2020-04-07 16:13:48 +08003078 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003079 }
hualing chen03fd4942021-07-15 15:56:41 +08003080 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
3081 seek_time,
3082 player->speed,
3083 player->cur_segment_id,
3084 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003085 }
hualing chen2aba4022020-03-02 13:49:55 +08003086 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003087}
3088
3089
3090//start replay
3091static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3092 //
3093 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003094
3095 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003096 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003097 return DVR_FAILURE;
3098 }
3099
hualing chen5cbe1a62020-02-10 16:36:36 +08003100 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003101 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003102 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08003103 AmTsPlayer_stopVideoDecoding(player->handle);
3104 }
3105 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003106 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003107 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003108 AmTsPlayer_stopAudioDecoding(player->handle);
3109 }
hualing chendf118dd2020-05-21 15:49:11 +08003110 if (player->has_ad_audio) {
3111 DVR_PB_DG(1, "fffb stop audio");
3112 player->has_ad_audio =DVR_FALSE;
3113 AmTsPlayer_disableADMix(player->handle);
3114 }
hualing chen2aba4022020-03-02 13:49:55 +08003115
hualing chen5cbe1a62020-02-10 16:36:36 +08003116 //start video and audio
3117
hualing chen2aba4022020-03-02 13:49:55 +08003118 am_tsplayer_video_params vparams;
3119 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003120 am_tsplayer_audio_params adparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003121
3122 memset(&vparams, 0, sizeof(vparams));
3123 memset(&aparams, 0, sizeof(aparams));
3124
hualing chen87072a82020-03-12 16:20:12 +08003125 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003126
3127 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08003128 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08003129 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003130 //start audio and video
3131 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
3132 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003133 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08003134 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003135 return -1;
3136 }
3137
3138 if (VALID_PID(vparams.pid)) {
3139 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08003140 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08003141 //DVR_PB_DG(1, "fffb start video and save last frame");
3142 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003143 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003144 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
3145 AmTsPlayer_setVideoParams(player->handle, &vparams);
3146 AmTsPlayer_startVideoDecoding(player->handle);
3147 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003148 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003149 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003150 }
hualing chen31140872020-03-25 12:29:26 +08003151 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08003152 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003153 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08003154 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003155 return 0;
3156}
3157
3158static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3159 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003160 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003161 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003162 return DVR_FAILURE;
3163 }
3164
3165 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08003166 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003167 pthread_mutex_lock(&player->lock);
3168
hualing chen2aba4022020-03-02 13:49:55 +08003169 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003170 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 +08003171
hualing chen87072a82020-03-12 16:20:12 +08003172 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3173 //seek time set 0
3174 seek_time = 0;
3175 }
hualing chen041c4092020-04-05 15:11:50 +08003176 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003177 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3178 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003179 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003180 pthread_mutex_unlock(&player->lock);
3181 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003182 //send event here and pause
3183 DVR_Play_Notify_t notify;
3184 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003185 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003186 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003187 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08003188 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 +08003189 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003190 return DVR_SUCCESS;
3191 }
hualing chen2932d372020-04-29 13:44:00 +08003192 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003193 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003194 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003195 }
3196 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003197 _dvr_playback_fffb_replay(handle);
3198
3199 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003200 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003201
hualing chen5cbe1a62020-02-10 16:36:36 +08003202 return DVR_SUCCESS;
3203}
3204
hualing chen87072a82020-03-12 16:20:12 +08003205//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003206static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003207 //
3208 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003209
3210 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003211 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003212 return DVR_FAILURE;
3213 }
3214
hualing chen5cbe1a62020-02-10 16:36:36 +08003215 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003216 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003217 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003218 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003219 }
3220
3221 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003222 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003223 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003224 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003225 //start video and audio
3226
hualing chen2aba4022020-03-02 13:49:55 +08003227 am_tsplayer_video_params vparams;
3228 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003229 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08003230 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003231
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003232 memset(&vparams, 0, sizeof(vparams));
3233 memset(&aparams, 0, sizeof(aparams));
3234
hualing chen5cbe1a62020-02-10 16:36:36 +08003235 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08003236 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08003237 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003238 //start audio and video
3239 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003240 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003241 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003242 return -1;
3243 }
3244
3245 if (VALID_PID(vparams.pid)) {
3246 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003247 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003248 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003249 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003250 }
hualing chen266b9502020-04-04 17:39:39 +08003251 else {
hualing chen2aba4022020-03-02 13:49:55 +08003252 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003253 }
hualing chen2aba4022020-03-02 13:49:55 +08003254 AmTsPlayer_setVideoParams(player->handle, &vparams);
3255 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003256 }
hualing chena540a7e2020-03-27 16:44:05 +08003257
3258 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08003259 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08003260 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003261 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003262 } else {
hualing chendf118dd2020-05-21 15:49:11 +08003263 if (VALID_PID(adparams.pid)) {
3264 player->has_ad_audio = DVR_TRUE;
3265 DVR_PB_DG(1, "start ad audio");
3266 AmTsPlayer_setADParams(player->handle, &adparams);
3267 AmTsPlayer_enableADMix(player->handle);
3268 }
hualing chen969fe7b2021-05-26 15:13:17 +08003269 if (VALID_PID(aparams.pid)) {
3270 player->has_audio = DVR_TRUE;
3271 DVR_PB_DG(1, "start audio");
3272 AmTsPlayer_setAudioParams(player->handle, &aparams);
3273 AmTsPlayer_startAudioDecoding(player->handle);
3274 }
hualing chendf118dd2020-05-21 15:49:11 +08003275
hualing chen7a56cba2020-04-14 14:09:27 +08003276 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003277 AmTsPlayer_stopFast(player->handle);
3278 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3279 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3280 }
hualing chen2aba4022020-03-02 13:49:55 +08003281 player->cmd.last_cmd = player->cmd.cur_cmd;
3282 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003283 player->cmd.state = DVR_PLAYBACK_STATE_START;
3284 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08003285 return 0;
3286}
3287
3288
hualing chenb31a6c62020-01-13 17:27:00 +08003289/**\brief Set play speed
3290 * \param[in] handle playback handle
3291 * \param[in] speed playback speed
3292 * \retval DVR_SUCCESS On success
3293 * \return Error code
3294 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003295int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3296
3297 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003298
3299 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003300 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003301 return DVR_FAILURE;
3302 }
3303
hualing chen4b7c15d2020-04-07 16:13:48 +08003304 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003305 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003306 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003307 return DVR_FAILURE;
3308 }
hualing chenf00cdc82020-06-10 14:23:35 +08003309 if (speed.speed.speed == player->cmd.speed.speed.speed) {
3310 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
3311 return DVR_SUCCESS;
3312 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003313 pthread_mutex_lock(&player->lock);
3314 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3315 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3316 player->cmd.last_cmd = player->cmd.cur_cmd;
3317 }
hualing chene41f4372020-06-06 16:29:17 +08003318
hualing chen31140872020-03-25 12:29:26 +08003319 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003320 IS_KERNEL_SPEED(speed.speed.speed) ) {
3321 //case 1. not start play.only set speed
3322 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3323 //only set speed.and return;
3324 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3325 player->cmd.speed.speed = speed.speed;
3326 player->speed = (float)speed.speed.speed/(float)100;
3327 player->fffb_play = DVR_FALSE;
3328 pthread_mutex_unlock(&player->lock);
3329 return DVR_SUCCESS;
3330 }
3331 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003332 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3333 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003334 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003335 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3336 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003337 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003338 AmTsPlayer_stopFast(player->handle);
3339 pthread_mutex_unlock(&player->lock);
3340 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
3341 pthread_mutex_lock(&player->lock);
3342 } else {
3343 //set play speed and if audio is start, stop audio.
3344 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003345 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003346 AmTsPlayer_stopAudioDecoding(player->handle);
3347 player->has_audio = DVR_FALSE;
3348 }
hualing chenb96aa2c2020-04-15 14:13:53 +08003349 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003350 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003351 }
hualing chenbcada022020-04-22 14:27:01 +08003352 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003353 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003354 player->cmd.speed.speed = speed.speed;
3355 player->speed = (float)speed.speed.speed/(float)100;
3356 pthread_mutex_unlock(&player->lock);
3357 return DVR_SUCCESS;
3358 }
hualing chen31140872020-03-25 12:29:26 +08003359 //case 3 fffb mode
3360 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3361 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3362 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003363 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003364 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003365 player->cmd.speed.speed = speed.speed;
3366 player->speed = (float)speed.speed.speed/(float)100;
3367 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003368 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003369 pthread_mutex_unlock(&player->lock);
3370 return DVR_SUCCESS;
3371 }
3372 }
3373 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003374 IS_KERNEL_SPEED(speed.speed.speed)) {
3375 //case 1. cur speed is kernel support speed,set kernel speed.
3376 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003377 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003378 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3379 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003380 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003381 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08003382 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003383 } else {
3384 //set play speed and if audio is start, stop audio.
3385 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003386 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003387 AmTsPlayer_stopAudioDecoding(player->handle);
3388 player->has_audio = DVR_FALSE;
3389 }
hualing chenf00cdc82020-06-10 14:23:35 +08003390 DVR_PB_DG(1, "start fast");
3391 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003392 }
hualing chena540a7e2020-03-27 16:44:05 +08003393 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003394 player->cmd.speed.speed = speed.speed;
3395 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003396 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003397 pthread_mutex_unlock(&player->lock);
3398 return DVR_SUCCESS;
3399 }
3400 //case 2 fffb mode
3401 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3402 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3403 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003404 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003405 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003406 player->cmd.speed.speed = speed.speed;
3407 player->speed = (float)speed.speed.speed/(float)100;
3408 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08003409 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003410 pthread_mutex_unlock(&player->lock);
3411 return DVR_SUCCESS;
3412 }
hualing chen31140872020-03-25 12:29:26 +08003413 }
hualing chena540a7e2020-03-27 16:44:05 +08003414 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3415 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003416 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003417 } else {
hualing chen31140872020-03-25 12:29:26 +08003418 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003419 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3420 else
3421 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003422 player->fffb_play = DVR_TRUE;
3423 }
3424 DVR_Bool_t init_last_time = DVR_FALSE;
3425 if (player->speed > 0.0f && speed.speed.speed < 0) {
3426 init_last_time = DVR_TRUE;
3427 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3428 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003429 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003430 player->cmd.speed.mode = speed.mode;
3431 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003432 player->speed = (float)speed.speed.speed/(float)100;
3433 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003434 _dvr_init_fffb_t(handle);
3435 if (init_last_time == DVR_TRUE)
3436 player->last_send_time_id = UINT64_MAX;
3437
hualing chen87072a82020-03-12 16:20:12 +08003438 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003439 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3440 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003441 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003442 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003443 _dvr_playback_replay(handle, DVR_FALSE);
3444 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3445 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3446 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003447 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003448 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003449 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003450 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003451 return DVR_SUCCESS;
3452}
hualing chen2932d372020-04-29 13:44:00 +08003453
hualing chenb31a6c62020-01-13 17:27:00 +08003454/**\brief Get playback status
3455 * \param[in] handle playback handle
3456 * \param[out] p_status playback status
3457 * \retval DVR_SUCCESS On success
3458 * \return Error code
3459 */
hualing chen2932d372020-04-29 13:44:00 +08003460static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3461 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003462//
3463 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003464 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003465 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003466 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003467 return DVR_FAILURE;
3468 }
hualing chen2932d372020-04-29 13:44:00 +08003469 if (is_lock ==DVR_TRUE)
3470 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003471 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003472 //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 +08003473 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3474 player->state == DVR_PLAYBACK_STATE_START) {
3475 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3476 }
hualing chen041c4092020-04-05 15:11:50 +08003477
hualing chencc91e1c2020-02-28 13:26:17 +08003478 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003479 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003480
3481 if (CONTROL_SPEED_ENABLE == 1) {
3482 if (player->con_spe.ply_sta < 0) {
hualing chen03fd4942021-07-15 15:56:41 +08003483 DVR_PB_DG(1, "player dur[%lld] sta[%lld] cur[%d] -----reinit",
3484 player->con_spe.ply_dur,
3485 player->con_spe.ply_sta,
3486 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003487 player->con_spe.ply_sta = p_status->time_cur;
3488 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3489 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
hualing chen03fd4942021-07-15 15:56:41 +08003490 DVR_PB_DG(1, "player dur[%lld] sta[%lld] cur[%d]",
3491 player->con_spe.ply_dur,
3492 player->con_spe.ply_sta,
3493 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003494 player->con_spe.ply_sta = p_status->time_cur;
3495 }
3496
3497 if (player->con_spe.sys_sta == 0) {
3498 player->con_spe.sys_sta = _dvr_time_getClock();
3499 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
3500 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
3501 player->con_spe.sys_sta = _dvr_time_getClock();
3502 }
3503 }
3504
hualing chen4b7c15d2020-04-07 16:13:48 +08003505 if (player->last_send_time_id == UINT64_MAX) {
3506 player->last_send_time_id = player->cur_segment_id;
3507 player->last_cur_time = p_status->time_cur;
3508 }
3509 if (player->last_send_time_id == player->cur_segment_id) {
3510 if (player->speed > 0.0f ) {
3511 //ff
3512 if (p_status->time_cur < player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003513 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]",
3514 player->last_cur_time,
3515 p_status->time_cur,
3516 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08003517 p_status->time_cur = player->last_cur_time;
3518 } else {
3519 player->last_cur_time = p_status->time_cur;
3520 }
hualing chene41f4372020-06-06 16:29:17 +08003521 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003522 //fb
3523 if (p_status->time_cur > player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003524 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]",
3525 player->last_cur_time,
3526 p_status->time_cur,
3527 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08003528 p_status->time_cur = player->last_cur_time;
3529 } else {
3530 player->last_cur_time = p_status->time_cur;
3531 }
3532 }
hualing chend241c7a2021-06-22 13:34:27 +08003533 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08003534 player->last_cur_time = p_status->time_cur;
3535 }
hualing chen969fe7b2021-05-26 15:13:17 +08003536 player->last_send_time_id = segment_id;
3537 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003538
hualing chen5cbe1a62020-02-10 16:36:36 +08003539 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003540 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003541 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003542 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 +08003543 _dvr_playback_state_toString(player->state),
3544 _dvr_playback_state_toString(p_status->state),
3545 p_status->time_cur, p_status->time_end,
3546 p_status->segment_id,player->play_flag,
3547 player->speed,
3548 is_lock);
hualing chen2932d372020-04-29 13:44:00 +08003549 if (is_lock ==DVR_TRUE)
3550 pthread_mutex_unlock(&player->lock);
3551 return DVR_SUCCESS;
3552}
3553
3554
3555/**\brief Get playback status
3556 * \param[in] handle playback handle
3557 * \param[out] p_status playback status
3558 * \retval DVR_SUCCESS On success
3559 * \return Error code
3560 */
3561int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3562 DVR_PlaybackStatus_t *p_status) {
3563//
3564 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3565
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003566 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3567
hualing chen2932d372020-04-29 13:44:00 +08003568 if (player == NULL) {
3569 DVR_PB_DG(1, "player is NULL");
3570 return DVR_FAILURE;
3571 }
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003572 pthread_mutex_lock(&player->lock);
3573 if (!player->has_video && !player->has_audio)
3574 p_status->time_cur = 0;
3575 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003576
hualing chenb31a6c62020-01-13 17:27:00 +08003577 return DVR_SUCCESS;
3578}
3579
hualing chen040df222020-01-17 13:35:02 +08003580void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3581 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003582 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3583 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3584 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3585 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3586 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3587 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3588 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 +08003589 }
hualing chenb31a6c62020-01-13 17:27:00 +08003590}
3591
hualing chen5cbe1a62020-02-10 16:36:36 +08003592int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003593 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003594
hualing chena540a7e2020-03-27 16:44:05 +08003595 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003596 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003597 return DVR_FAILURE;
3598 }
3599
hualing chen040df222020-01-17 13:35:02 +08003600 DVR_PlaybackSegmentInfo_t *segment;
3601 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003602 {
hualing chen040df222020-01-17 13:35:02 +08003603 if (segment_id >= 0) {
3604 if (segment->segment_id == segment_id) {
3605 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003606 break;
3607 }
3608 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003609 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003610 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003611 }
3612 }
3613 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003614}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003615
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003616int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003617{
3618 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3619 DVR_RETURN_IF_FALSE(player);
3620 DVR_RETURN_IF_FALSE(func);
3621
hualing chen4b7c15d2020-04-07 16:13:48 +08003622 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003623 pthread_mutex_lock(&player->lock);
3624
3625 player->dec_func = func;
3626 player->dec_userdata = userdata;
3627
3628 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003629 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003630 return DVR_SUCCESS;
3631}
3632
3633int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3634{
3635 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3636 DVR_RETURN_IF_FALSE(player);
3637 DVR_RETURN_IF_FALSE(p_secure_buf);
3638 DVR_RETURN_IF_FALSE(len);
3639
hualing chen4b7c15d2020-04-07 16:13:48 +08003640 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003641 pthread_mutex_lock(&player->lock);
3642
3643 player->is_secure_mode = 1;
3644 player->secure_buffer = p_secure_buf;
3645 player->secure_buffer_size = len;
3646
3647 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003648 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003649 return DVR_SUCCESS;
3650}