blob: debc9bd83642cc2b907684b904a6fb1128de988d [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001#include <stdio.h>
2#include <stdlib.h>
3
hualing chen5cbe1a62020-02-10 16:36:36 +08004#include <string.h>
hualing chenb31a6c62020-01-13 17:27:00 +08005#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <poll.h>
11#include <errno.h>
12#include <signal.h>
13#include <pthread.h>
hualing chenb5cd42e2020-04-15 17:03:34 +080014#include <errno.h>
hualing chen03fd4942021-07-15 15:56:41 +080015#include "dvr_utils.h"
16#include "dvr_types.h"
hualing chenb31a6c62020-01-13 17:27:00 +080017#include "dvr_playback.h"
Yahui Han1fbf3292021-11-08 18:17:19 +080018#include "am_crypt.h"
hualing chenb31a6c62020-01-13 17:27:00 +080019
Gong Ke2a0ebbe2021-05-25 15:22:50 +080020#define DVR_PB_DG(_level, _fmt...) \
21 DVR_DEBUG_FL(_level, "playback", _fmt)
hualing chena540a7e2020-03-27 16:44:05 +080022
hualing chenb31a6c62020-01-13 17:27:00 +080023#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080024
hualing chend241c7a2021-06-22 13:34:27 +080025#define CONTROL_SPEED_ENABLE 0
hualing chena540a7e2020-03-27 16:44:05 +080026
27#define FF_SPEED (2.0f)
28#define FB_SPEED (-1.0f)
29#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080030#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080031
32#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))
33#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
34
hualing chenb31a6c62020-01-13 17:27:00 +080035
hualing chenb5cd42e2020-04-15 17:03:34 +080036#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080037#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080038//if tsplayer delay time < 200 and no data can read, we will pause
39#define MIN_TSPLAYER_DELAY_TIME (200)
40
hualing chen041c4092020-04-05 15:11:50 +080041#define MAX_CACHE_TIME (30000)
42
hualing chena540a7e2020-03-27 16:44:05 +080043static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080044//
45static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chen99508642021-10-18 15:41:17 +080046static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_PlaybackPids_t now_pids, DVR_PlaybackPids_t pids, int type);
hualing chencc91e1c2020-02-28 13:26:17 +080047static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
48static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080049static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080050static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080051static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
52 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080053static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen7ea70a72021-09-09 11:25:13 +080054static uint32_t dvr_playback_calculate_last_valid_segment(
55 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos);
hualing chen87072a82020-03-12 16:20:12 +080056
hualing chenbcada022020-04-22 14:27:01 +080057
hualing chena5f03222021-12-02 11:22:35 +080058
hualing chenbcada022020-04-22 14:27:01 +080059static char* _cmd_toString(int cmd)
60{
61
62 char *string[DVR_PLAYBACK_CMD_NONE+1]={
63 "start",
64 "stop",
65 "vstart",
66 "astart",
67 "vstop",
68 "astop",
69 "vrestart",
70 "arestart",
71 "avrestart",
72 "vstopastart",
73 "astopvstart",
74 "vstoparestart",
75 "astopvrestart",
76 "vstartarestart",
77 "astartvrestart",
78 "pause",
79 "resume",
80 "seek",
81 "ff",
82 "fb",
83 "NONE"
84 };
85
86 if (cmd > DVR_PLAYBACK_CMD_NONE) {
87 return "unkown";
88 } else {
89 return string[cmd];
90 }
91}
92
93
hualing chen6d24aa92020-03-23 18:43:47 +080094static char* _dvr_playback_state_toString(int stat)
95{
96 char *string[DVR_PLAYBACK_STATE_FB+1]={
97 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080098 "stop",
hualing chen31140872020-03-25 12:29:26 +080099 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +0800100 "ff",
101 "fb"
102 };
103
104 if (stat > DVR_PLAYBACK_STATE_FB) {
105 return "unkown";
106 } else {
107 return string[stat];
108 }
109}
hualing chena540a7e2020-03-27 16:44:05 +0800110
111static DVR_Bool_t _dvr_support_speed(int speed) {
112
113 DVR_Bool_t ret = DVR_FALSE;
114
115 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800116 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800117 case PLAYBACK_SPEED_FBX2:
118 case PLAYBACK_SPEED_FBX4:
119 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800120 case PLAYBACK_SPEED_FBX16:
121 case PLAYBACK_SPEED_FBX12:
122 case PLAYBACK_SPEED_FBX32:
123 case PLAYBACK_SPEED_FBX48:
124 case PLAYBACK_SPEED_FBX64:
125 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800126 case PLAYBACK_SPEED_S2:
127 case PLAYBACK_SPEED_S4:
128 case PLAYBACK_SPEED_S8:
129 case PLAYBACK_SPEED_X1:
130 case PLAYBACK_SPEED_X2:
131 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800132 case PLAYBACK_SPEED_X3:
133 case PLAYBACK_SPEED_X5:
134 case PLAYBACK_SPEED_X6:
135 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800136 case PLAYBACK_SPEED_X8:
137 case PLAYBACK_SPEED_X12:
138 case PLAYBACK_SPEED_X16:
139 case PLAYBACK_SPEED_X32:
140 case PLAYBACK_SPEED_X48:
141 case PLAYBACK_SPEED_X64:
142 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800143 ret = DVR_TRUE;
144 break;
145 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800146 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800147 break;
148 }
149 return ret;
150}
hualing chen6e4bfa52020-03-13 14:37:11 +0800151void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
152{
hualing chen4b7c15d2020-04-07 16:13:48 +0800153 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800154 DVR_Playback_t *player = NULL;
155 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800156 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800157 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800158 }
159 switch (event->type) {
160 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
161 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800162 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800163 event->event.video_format.frame_width,
164 event->event.video_format.frame_height,
165 event->event.video_format.frame_rate);
166 break;
167 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800168 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
169 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800170 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800171 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800172 break;
173 }
174 default:
175 break;
176 }
177}
hualing chen2aba4022020-03-02 13:49:55 +0800178void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
179{
hualing chen6e4bfa52020-03-13 14:37:11 +0800180 DVR_Playback_t *player = NULL;
181 if (user_data != NULL) {
182 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800183 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800184 }
hualing chen2aba4022020-03-02 13:49:55 +0800185 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800186 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
187 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800188 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800189 event->event.video_format.frame_width,
190 event->event.video_format.frame_height,
191 event->event.video_format.frame_rate);
192 break;
193 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800194 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
195 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800196 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800197 if (player->first_trans_ok == DVR_FALSE) {
198 player->first_trans_ok = DVR_TRUE;
199 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
200 }
hualing chen30423862021-04-16 14:39:12 +0800201 if (player != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800202 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800203 player->seek_pause = DVR_FALSE;
204 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800205 break;
206 }
hualing chen487ae6d2020-07-22 10:34:11 +0800207 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
hualing chen1679f812021-11-08 15:17:46 +0800208 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
hualing chen487ae6d2020-07-22 10:34:11 +0800209 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
210 player->first_trans_ok = DVR_TRUE;
211 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
212 }
213 if (player != NULL && player->has_video == DVR_FALSE) {
214 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
215 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800216 player->seek_pause = DVR_FALSE;
hualing chen487ae6d2020-07-22 10:34:11 +0800217 }
218 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800219 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800220 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800221 break;
222 }
223 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800224 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800225 player->player_callback_func(player->player_callback_userdata, event);
226 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800227 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800228 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800229 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800230 }
231}
hualing chencc91e1c2020-02-28 13:26:17 +0800232
hualing chen5cbe1a62020-02-10 16:36:36 +0800233//convert video and audio fmt
234static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
235 int format = 0;
236 if (is_audio == DVR_FALSE) {
237 //for video fmt
238 switch (fmt)
239 {
240 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800241 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800242 break;
243 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800244 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800245 break;
246 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800247 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800248 break;
249 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800250 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800251 break;
hualing chena540a7e2020-03-27 16:44:05 +0800252 case DVR_VIDEO_FORMAT_VP9:
253 format = AV_VIDEO_CODEC_VP9;
254 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800255 }
256 } else {
257 //for audio fmt
258 switch (fmt)
259 {
260 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800261 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800262 break;
263 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800264 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800265 break;
266 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800267 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800268 break;
269 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800270 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800271 break;
hualing chena540a7e2020-03-27 16:44:05 +0800272 case DVR_AUDIO_FORMAT_AAC:
273 format = AV_AUDIO_CODEC_AAC;
274 break;
275 case DVR_AUDIO_FORMAT_LATM:
276 format = AV_AUDIO_CODEC_LATM;
277 break;
278 case DVR_AUDIO_FORMAT_PCM:
279 format = AV_AUDIO_CODEC_PCM;
280 break;
hualing chenee0e52b2021-04-09 16:58:44 +0800281 case DVR_AUDIO_FORMAT_AC4:
282 format = AV_AUDIO_CODEC_AC4;
283 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800284 }
285 }
286 return format;
287}
hualing chen040df222020-01-17 13:35:02 +0800288static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800289{
hualing chen040df222020-01-17 13:35:02 +0800290 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800291
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800292 if (player == NULL || player->handle == (am_tsplayer_handle)NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800293 return -1;
294
hualing chena540a7e2020-03-27 16:44:05 +0800295 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800296}
hualing chena540a7e2020-03-27 16:44:05 +0800297
hualing chen7ea70a72021-09-09 11:25:13 +0800298
299//get sys time sec
300static uint32_t _dvr_getClock_sec(void)
hualing chen5cbe1a62020-02-10 16:36:36 +0800301{
302 struct timespec ts;
hualing chen7ea70a72021-09-09 11:25:13 +0800303 uint32_t s;
hualing chen03fd4942021-07-15 15:56:41 +0800304 clock_gettime(CLOCK_REALTIME, &ts);
hualing chen7ea70a72021-09-09 11:25:13 +0800305 s = (uint32_t)(ts.tv_sec);
306 DVR_PB_DG(1, "n:%u", s);
307 return s;
hualing chen5cbe1a62020-02-10 16:36:36 +0800308}
hualing chen86e7d482020-01-16 15:13:33 +0800309
hualing chen7ea70a72021-09-09 11:25:13 +0800310//get sys time ms
311static uint32_t _dvr_time_getClock(void)
312{
313 struct timespec ts;
314 uint32_t ms;
315 clock_gettime(CLOCK_REALTIME, &ts);
316 ms = (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
317 return ms;
318}
hualing chenb31a6c62020-01-13 17:27:00 +0800319
320//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800321static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800322{
hualing chen040df222020-01-17 13:35:02 +0800323 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800324
hualing chena540a7e2020-03-27 16:44:05 +0800325
326 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800327 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800328 return DVR_FAILURE;
329 }
330
hualing chen86e7d482020-01-16 15:13:33 +0800331 struct timespec ts;
332 clock_gettime(CLOCK_MONOTONIC, &ts);
333 //ms为毫秒,换算成秒
334 ts.tv_sec += ms/1000;
335 //在outtime的基础上,增加ms毫秒
336 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
337 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
338 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
339 //us的值有可能超过1秒,
340 ts.tv_sec += us / 1000000;
341 us = us % 1000000;
342 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800343 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
344 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800345}
hualing chen31140872020-03-25 12:29:26 +0800346//get tsplay delay time ms
347static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
348 DVR_Playback_t *player = (DVR_Playback_t *) handle;
349 int64_t cache = 0;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800350 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800351 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800352 return 0;
353 }
354 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800355 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800356 return cache;
357}
hualing chenb31a6c62020-01-13 17:27:00 +0800358//send signal
hualing chen040df222020-01-17 13:35:02 +0800359static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800360{
hualing chen87072a82020-03-12 16:20:12 +0800361 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800362
363 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800364 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800365 return DVR_FAILURE;
366 }
hualing chen1679f812021-11-08 15:17:46 +0800367 DVR_PB_DG(1, "lock---");
hualing chen87072a82020-03-12 16:20:12 +0800368 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800369 pthread_cond_signal(&player->cond);
hualing chen1679f812021-11-08 15:17:46 +0800370 DVR_PB_DG(1, "unlock---");
hualing chen87072a82020-03-12 16:20:12 +0800371 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800372 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800373}
374
hualing chen2932d372020-04-29 13:44:00 +0800375//send playback event, need check is need lock first
376static 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 +0800377
378 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800379
380 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800381 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800382 return DVR_FAILURE;
383 }
384
hualing chencc91e1c2020-02-28 13:26:17 +0800385 switch (evt) {
386 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800387 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800388 break;
389 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
390 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800391 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800392 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800393 break;
394 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
395 break;
396 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
397 break;
398 case DVR_PLAYBACK_EVENT_NO_KEY:
399 break;
400 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800401 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800402 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800403 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800404 break;
405 case DVR_PLAYBACK_EVENT_REACHED_END:
406 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800407 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800408 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800409 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800410 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800411 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800412 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800413 default:
414 break;
415 }
416 if (player->openParams.event_fn != NULL)
417 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800418 return DVR_SUCCESS;
419}
hualing chen2932d372020-04-29 13:44:00 +0800420static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800421{
422 DVR_Play_Notify_t notify;
423 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
424 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
425 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800426 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800427 return DVR_SUCCESS;
428}
429
hualing chen2932d372020-04-29 13:44:00 +0800430static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800431{
432 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800433
hualing chene3797f02021-01-13 14:53:28 +0800434 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chend241c7a2021-06-22 13:34:27 +0800435 if (CONTROL_SPEED_ENABLE == 0)
436 return DVR_SUCCESS;
hualing chen4b7c15d2020-04-07 16:13:48 +0800437 }
hualing chena540a7e2020-03-27 16:44:05 +0800438 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800439 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800440 return DVR_FAILURE;
441 }
442
hualing chen03fd4942021-07-15 15:56:41 +0800443 if (player->send_time == 0) {
hualing chend241c7a2021-06-22 13:34:27 +0800444 if (CONTROL_SPEED_ENABLE == 0)
445 player->send_time = _dvr_time_getClock() + 500;
446 else
447 player->send_time = _dvr_time_getClock() + 20;
hualing chen0888c032020-12-18 17:54:57 +0800448 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800449 return DVR_SUCCESS;
450 }
hualing chend241c7a2021-06-22 13:34:27 +0800451 if (CONTROL_SPEED_ENABLE == 0)
452 player->send_time = _dvr_time_getClock() + 500;
453 else
454 player->send_time = _dvr_time_getClock() + 20;
455
hualing chen6e4bfa52020-03-13 14:37:11 +0800456 DVR_Play_Notify_t notify;
457 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
458 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
459 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800460 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800461 return DVR_SUCCESS;
462}
463
hualing chencc91e1c2020-02-28 13:26:17 +0800464//check is ongoing segment
465static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
466
467 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800468 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800469
470 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800471 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800472 return DVR_FAILURE;
473 }
hualing chen87072a82020-03-12 16:20:12 +0800474 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800475 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800476 return DVR_FALSE;
477 }
hualing chencc91e1c2020-02-28 13:26:17 +0800478 return DVR_TRUE;
479}
hualing chen4b7c15d2020-04-07 16:13:48 +0800480
481
482static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
483 DVR_Playback_t *player = (DVR_Playback_t *) handle;
484 player->fffb_start = _dvr_time_getClock();
hualing chen7ea70a72021-09-09 11:25:13 +0800485 DVR_PB_DG(1, " player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800486 player->fffb_current = player->fffb_start;
487 //get segment current time pos
488 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +0800489 player->next_fffb_time = _dvr_time_getClock();
490
491 return DVR_SUCCESS;
492}
493
hualing chen2aba4022020-03-02 13:49:55 +0800494static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
495 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800496 player->fffb_start = _dvr_time_getClock();
hualing chen7ea70a72021-09-09 11:25:13 +0800497 DVR_PB_DG(1, " player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800498 player->fffb_current = player->fffb_start;
499 //get segment current time pos
500 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +0800501
hualing chen2aba4022020-03-02 13:49:55 +0800502 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800503 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800504 return DVR_SUCCESS;
505}
hualing chencc91e1c2020-02-28 13:26:17 +0800506//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800507static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
508
509 DVR_Playback_t *player = (DVR_Playback_t *) handle;
510 DVR_PlaybackSegmentInfo_t *segment;
511 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
512
hualing chena540a7e2020-03-27 16:44:05 +0800513 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800514 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800515 return DVR_FAILURE;
516 }
517
hualing chen87072a82020-03-12 16:20:12 +0800518 int found = 0;
519 int found_eq_id = 0;
520 list_for_each_entry(segment, &player->segment_list, head)
521 {
522 if (player->segment_is_open == DVR_FALSE) {
523 //get first segment from list, case segment is not open
524 if (!IS_FB(player->speed))
525 found = 1;
526 } else if (segment->segment_id == segmentid) {
527 //find cur segment, we need get next one
528 found_eq_id = 1;
529 if (!IS_FB(player->speed)) {
530 found = 1;
531 continue;
532 } else {
533 //if is fb mode.we need used pre segment
534 if (pre_segment != NULL) {
535 found = 1;
536 } else {
537 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800538 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800539 return DVR_FAILURE;
540 }
541 }
542 }
543 if (found == 1) {
544 found = 2;
545 break;
546 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800547 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800548 }
549 if (found != 2) {
550 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800551 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800552 return DVR_FAILURE;
553 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800554 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800555 return DVR_SUCCESS;
556}
557
558//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800559static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800560
hualing chen040df222020-01-17 13:35:02 +0800561 DVR_Playback_t *player = (DVR_Playback_t *) handle;
562 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800563 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen03fd4942021-07-15 15:56:41 +0800564 uint64_t segmentid;
hualing chen7ea70a72021-09-09 11:25:13 +0800565 uint32_t pos;
hualing chena540a7e2020-03-27 16:44:05 +0800566 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800567 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800568 return DVR_FAILURE;
569 }
570
hualing chen03fd4942021-07-15 15:56:41 +0800571 if (IS_FB(player->speed)
572 && dvr_playback_check_limit(handle)) {
573 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
574 //case cur id < segment id
575 if (player->cur_segment_id <= segmentid) {
576 //expired ts data is player,return error
577 DVR_PB_DG(1, "reach start segment ,return error");
578 return DVR_FAILURE;
579 }
hualing chen7ea70a72021-09-09 11:25:13 +0800580 DVR_PB_DG(1, "has segment to fb play [%lld][%u]", segmentid, pos);
hualing chen03fd4942021-07-15 15:56:41 +0800581 }
582
hualing chen86e7d482020-01-16 15:13:33 +0800583 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800584 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800585
hualing chen040df222020-01-17 13:35:02 +0800586 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800587 {
hualing chencc91e1c2020-02-28 13:26:17 +0800588 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800589 //get first segment from list, case segment is not open
590 if (!IS_FB(player->speed))
591 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800592 } else if (segment->segment_id == player->cur_segment_id) {
593 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800594 found_eq_id = 1;
595 if (!IS_FB(player->speed)) {
596 found = 1;
597 continue;
598 } else {
599 //if is fb mode.we need used pre segment
600 if (pre_segment != NULL) {
601 found = 1;
602 } else {
603 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800604 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800605 return DVR_FAILURE;
606 }
607 }
hualing chen86e7d482020-01-16 15:13:33 +0800608 }
609 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800610 if (IS_FB(player->speed)) {
611 //used pre segment
612 segment = pre_segment;
613 }
hualing chencc91e1c2020-02-28 13:26:17 +0800614 //save segment info
615 player->last_segment_id = player->cur_segment_id;
hualing chen969fe7b2021-05-26 15:13:17 +0800616 if (player->r_handle)
617 player->last_segment_tatol = segment_tell_total_time(player->r_handle);
hualing chen87072a82020-03-12 16:20:12 +0800618 player->last_segment.segment_id = player->cur_segment.segment_id;
619 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800620 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
621 //pids
622 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
623
hualing chen5cbe1a62020-02-10 16:36:36 +0800624 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800625 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800626 player->cur_segment_id = segment->segment_id;
627 player->cur_segment.segment_id = segment->segment_id;
628 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800629 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 +0800630 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800631 //pids
hualing chen040df222020-01-17 13:35:02 +0800632 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800633 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800634 break;
hualing chen86e7d482020-01-16 15:13:33 +0800635 }
hualing chen2aba4022020-03-02 13:49:55 +0800636 pre_segment = segment;
637 }
638 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
639 //used the last one segment to open
640 //get segment info
641 player->segment_is_open = DVR_TRUE;
642 player->cur_segment_id = pre_segment->segment_id;
643 player->cur_segment.segment_id = pre_segment->segment_id;
644 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800645 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 +0800646 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
647 //pids
648 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
649 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800650 }
651 if (found != 2) {
652 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800653 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800654 }
655 return DVR_SUCCESS;
656}
hualing chen040df222020-01-17 13:35:02 +0800657//open next segment to play,if reach list end return errro.
658static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800659{
hualing chen040df222020-01-17 13:35:02 +0800660 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800661 Segment_OpenParams_t params;
662 int ret = DVR_SUCCESS;
663
hualing chena540a7e2020-03-27 16:44:05 +0800664 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800665 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800666 return DVR_FAILURE;
667 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800668 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800669
670 ret = _dvr_get_next_segmentId(handle);
671 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800672 DVR_PB_DG(1, "not found segment info");
673 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800674 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800675 }
676
677 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800678 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800679 segment_close(player->r_handle);
680 player->r_handle = NULL;
681 }
682
683 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800684 //cp chur segment path to location
685 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800686 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800687 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800688 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
689
hualing chen86e7d482020-01-16 15:13:33 +0800690 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800691 if (ret == DVR_FAILURE) {
692 DVR_PB_DG(1, "open segment error");
693 }
hualing chen87072a82020-03-12 16:20:12 +0800694 pthread_mutex_unlock(&player->segment_lock);
695 int total = _dvr_get_end_time( handle);
696 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800697 if (IS_FB(player->speed)) {
698 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800699 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800700 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800701 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800702 }
hualing chen87072a82020-03-12 16:20:12 +0800703 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800704 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800705 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800706 return ret;
707}
708
hualing chen5cbe1a62020-02-10 16:36:36 +0800709//open next segment to play,if reach list end return errro.
710static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
711{
712 DVR_Playback_t *player = (DVR_Playback_t *) handle;
713 Segment_OpenParams_t params;
714 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800715 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800716 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800717 return DVR_FAILURE;
718 }
hualing chencc91e1c2020-02-28 13:26:17 +0800719 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800720 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800721 }
hualing chencc91e1c2020-02-28 13:26:17 +0800722 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800723 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800724 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800725 return DVR_FAILURE;
726 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800727 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800728 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800729
730 DVR_PlaybackSegmentInfo_t *segment;
731
732 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800733
hualing chen5cbe1a62020-02-10 16:36:36 +0800734 list_for_each_entry(segment, &player->segment_list, head)
735 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800736 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 +0800737 if (segment->segment_id == segment_id) {
738 found = 1;
739 }
740 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800741 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 +0800742 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800743 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800744 player->cur_segment_id = segment->segment_id;
745 player->cur_segment.segment_id = segment->segment_id;
746 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800747 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800748 //pids
749 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800750 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 +0800751 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800752 }
753 }
hualing chencc91e1c2020-02-28 13:26:17 +0800754 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800755 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800756 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800757 return DVR_FAILURE;
758 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800759 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800760 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800761 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800762 params.segment_id = (uint64_t)player->cur_segment.segment_id;
763 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800764 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 +0800765 if (player->r_handle != NULL) {
766 segment_close(player->r_handle);
767 player->r_handle = NULL;
768 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800769 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800770 if (ret == DVR_FAILURE) {
771 DVR_PB_DG(1, "segment opne error");
772 }
hualing chen2aba4022020-03-02 13:49:55 +0800773 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800774 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800775
hualing chen4b7c15d2020-04-07 16:13:48 +0800776 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 +0800777 return ret;
778}
779
780
781//get play info by segment id
782static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
783 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800784 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800785 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800786
787 DVR_Playback_t *player = (DVR_Playback_t *) handle;
788 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800789 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800790 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800791 return DVR_FAILURE;
792 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800793
794 int found = 0;
795
796 list_for_each_entry(segment, &player->segment_list, head)
797 {
hualing chen87072a82020-03-12 16:20:12 +0800798 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800799 //get first segment from list
800 found = 1;
801 }
802 if (segment->segment_id == segment_id) {
803 found = 1;
804 }
805 if (found == 1) {
806 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800807 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800808 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800809 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800810 player->cur_segment.segment_id = segment->segment_id;
811 player->cur_segment.flags = segment->flags;
812 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800813 player->cur_segment.pids.video.pid = segment->pids.video.pid;
814 player->cur_segment.pids.video.format = segment->pids.video.format;
815 player->cur_segment.pids.video.type = segment->pids.video.type;
816 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
817 player->cur_segment.pids.audio.format = segment->pids.audio.format;
818 player->cur_segment.pids.audio.type = segment->pids.audio.type;
819 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
820 player->cur_segment.pids.ad.format = segment->pids.ad.format;
821 player->cur_segment.pids.ad.type = segment->pids.ad.type;
822 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800823 //
hualing chen2aba4022020-03-02 13:49:55 +0800824 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800825 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800826 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800827 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800828 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
829 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800830 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 +0800831 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800832 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800833 }
834 }
hualing chencc91e1c2020-02-28 13:26:17 +0800835 if (found != 2) {
836 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800837 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800838 return DVR_FAILURE;
839 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800840
841 return DVR_SUCCESS;
842}
hualing chencc91e1c2020-02-28 13:26:17 +0800843static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
844 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800845 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800846 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800847 return DVR_FAILURE;
848 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800849
hualing chencc91e1c2020-02-28 13:26:17 +0800850 //compare cur segment
851 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
852 {
853 //check video pids, stop or restart
hualing chen99508642021-10-18 15:41:17 +0800854 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800855 //check sub audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800856 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800857 //check audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800858 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 1);
hualing chene3797f02021-01-13 14:53:28 +0800859 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 +0800860 //check pcr pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800861 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800862 }
hualing chena540a7e2020-03-27 16:44:05 +0800863 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800864}
hualing chen5cbe1a62020-02-10 16:36:36 +0800865
hualing chend241c7a2021-06-22 13:34:27 +0800866static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
867{
868 DVR_Playback_t *player = (DVR_Playback_t *) handle;
869 if (player == NULL) {
870 DVR_PB_DG(1, "player is NULL");
871 return DVR_TRUE;
872 }
873 char buf[10];
874 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
875 DVR_PB_DG(1, "player get prop[%d][%s]", atoi(buf), buf);
876
877 if (atoi(buf) != 1) {
878 //return DVR_TRUE;
879 }
880
hualing chen7ea70a72021-09-09 11:25:13 +0800881 DVR_PB_DG(1, ":play speed: %f ply dur: %u sys_dur: %u",
hualing chen03fd4942021-07-15 15:56:41 +0800882 player->speed,
883 player->con_spe.ply_dur,
884 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800885
886 if (player->speed != 1.0f)
887 return DVR_TRUE;
888
889 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800890 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800891 return DVR_FALSE;
892
893 return DVR_TRUE;
894}
895
hualing chencc91e1c2020-02-28 13:26:17 +0800896static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
897{
898 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800899 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800900 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800901 return DVR_FAILURE;
902 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800903 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
904 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
905 return DVR_SUCCESS;
906 }
hualing chen03fd4942021-07-15 15:56:41 +0800907 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]",
908 player->cur_segment.flags,
909 player->cur_segment.segment_id,
910 player->last_segment.flags,
911 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800912 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
913 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800914 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800915 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800916 AmTsPlayer_showVideo(player->handle);
917 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800918 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
919 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800920 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800921 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800922 AmTsPlayer_hideVideo(player->handle);
923 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800924 }
925 return DVR_SUCCESS;
926}
hualing chene3797f02021-01-13 14:53:28 +0800927/*
928if decodec sucess first time.
929sucess: return true
930fail: return false
931*/
hualing chena540a7e2020-03-27 16:44:05 +0800932static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
933 DVR_Playback_t *player = (DVR_Playback_t *) handle;
934 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800935 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800936 return DVR_TRUE;
937 }
hualing chene3797f02021-01-13 14:53:28 +0800938 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800939 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800940 } else {
941 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800942 }
943}
hualing chen86e7d482020-01-16 15:13:33 +0800944static void* _dvr_playback_thread(void *arg)
945{
hualing chen040df222020-01-17 13:35:02 +0800946 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800947 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800948 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800949 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800950 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800951
hualing chen39628212020-05-14 10:35:13 +0800952 #define MAX_REACHEND_TIMEOUT (3000)
953 int reach_end_timeout = 0;//ms
954 int cache_time = 0;
hualing chena5f03222021-12-02 11:22:35 +0800955 int timeout = 100;//ms
hualing chen40dd5462021-11-26 19:56:20 +0800956 int check_no_data_time = 4;
hualing chen2aba4022020-03-02 13:49:55 +0800957 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800958 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800959 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800960 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
hualing chen40dd5462021-11-26 19:56:20 +0800961 int first_write = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800962 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800963 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800964 DVR_Bool_t goto_rewrite = DVR_FALSE;
yinming ding0ce94922021-09-08 15:09:15 +0800965 char prop_buf[10];
966
967 memset(prop_buf, 0 ,sizeof(prop_buf));
968 dvr_prop_read("vendor.tv.libdvr.writetm", prop_buf, sizeof(prop_buf));
hualing chena5f03222021-12-02 11:22:35 +0800969 DVR_PB_DG(1, "---vendor.tv.libdvr.writetm get prop[%d][%s]block_size[%d]", atoi(prop_buf), prop_buf, player->openParams.block_size);
yinming ding0ce94922021-09-08 15:09:15 +0800970 if (atoi(prop_buf) > 0)
971 write_timeout_ms = atoi(prop_buf);
hualing chen03fd4942021-07-15 15:56:41 +0800972
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800973 if (player->is_secure_mode) {
974 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800975 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800976 return NULL;
977 }
978 }
hualing chen86e7d482020-01-16 15:13:33 +0800979 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800980 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800981 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800982 return NULL;
983 }
hualing chen2aba4022020-03-02 13:49:55 +0800984 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
985 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800986
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800987 dec_bufs.buf_data = malloc(dec_buf_size);
988 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800989 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800990 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800991 return NULL;
992 }
993 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
994 dec_bufs.buf_size = dec_buf_size;
995
hualing chencc91e1c2020-02-28 13:26:17 +0800996 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800997 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
998 }
hualing chen86e7d482020-01-16 15:13:33 +0800999
hualing chen86e7d482020-01-16 15:13:33 +08001000 if (ret != DVR_SUCCESS) {
1001 if (buf != NULL) {
1002 free(buf);
1003 buf = NULL;
1004 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001005 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +08001006 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +08001007 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +08001008 }
hualing chen1679f812021-11-08 15:17:46 +08001009 DVR_PB_DG(1, "--player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001010 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +08001011 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
1012 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
1013 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
1014 player->first_trans_ok = DVR_TRUE;
1015 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
1016 }
hualing chencc91e1c2020-02-28 13:26:17 +08001017 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +08001018 //set video show
1019 AmTsPlayer_showVideo(player->handle);
hualing chen40dd5462021-11-26 19:56:20 +08001020 if (player->vendor == DVR_PLAYBACK_VENDOR_AMAZON)
1021 check_no_data_time = 8;
hualing chen86e7d482020-01-16 15:13:33 +08001022 int trick_stat = 0;
1023 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001024
hualing chen86e7d482020-01-16 15:13:33 +08001025 //check trick stat
hualing chen40dd5462021-11-26 19:56:20 +08001026 DVR_PB_DG(1, "lock check_no_data_time:%d", check_no_data_time);
hualing chencc91e1c2020-02-28 13:26:17 +08001027 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001028
hualing chen2aba4022020-03-02 13:49:55 +08001029 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1030 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001031 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001032 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001033 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001034 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001035 {
hualing chen2aba4022020-03-02 13:49:55 +08001036 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1037 if (trick_stat > 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001038 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
1039 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001040 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 +08001041 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001042 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001043 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001044 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
1045 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +08001046 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
1047 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chen03fd4942021-07-15 15:56:41 +08001048 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
1049 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001050 //need change to pause state
1051 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1052 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +08001053 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08001054 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001055 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001056 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001057 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001058 AmTsPlayer_pauseVideoDecoding(player->handle);
1059 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001060 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001061 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001062 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001063 }
1064 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1065 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001066 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001067 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001068 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen7ea70a72021-09-09 11:25:13 +08001069 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001070 player->speed,
1071 player->fffb_current,
1072 _dvr_time_getClock(),
1073 _dvr_playback_state_toString(player->state),
1074 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001075 //used timeout wait need lock first,so we unlock and lock
1076 //pthread_mutex_unlock(&player->lock);
1077 //pthread_mutex_lock(&player->lock);
1078 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001079 DVR_PB_DG(1, "unlock---");
hualing chen2aba4022020-03-02 13:49:55 +08001080 pthread_mutex_unlock(&player->lock);
1081 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001082 } else if (_dvr_time_getClock() < player->next_fffb_time) {
hualing chen7ea70a72021-09-09 11:25:13 +08001083 DVR_PB_DG(1, "fffb timeout-to pause video---speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001084 player->speed,
1085 player->fffb_current,
1086 _dvr_time_getClock(),
1087 _dvr_playback_state_toString(player->state),
1088 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001089 //used timeout wait need lock first,so we unlock and lock
1090 //pthread_mutex_unlock(&player->lock);
1091 //pthread_mutex_lock(&player->lock);
1092 AmTsPlayer_pauseVideoDecoding(player->handle);
1093 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001094 DVR_PB_DG(1, "unlock---");
hualing chenb5cd42e2020-04-15 17:03:34 +08001095 pthread_mutex_unlock(&player->lock);
1096 continue;
hualing chen2aba4022020-03-02 13:49:55 +08001097 }
hualing chen03fd4942021-07-15 15:56:41 +08001098 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]",
1099 player->speed,
1100 goto_rewrite,
1101 real_read,
1102 _dvr_playback_state_toString(player->state),
1103 player->cmd.cur_cmd);
hualing chen1679f812021-11-08 15:17:46 +08001104 DVR_PB_DG(1, "unlock---");
hualing chen2aba4022020-03-02 13:49:55 +08001105 pthread_mutex_unlock(&player->lock);
1106 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001107 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001108 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1109 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001110 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001111 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08001112 DVR_PB_DG(1, "lock---");
hualing chen2aba4022020-03-02 13:49:55 +08001113 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08001114 } else if(player->state == DVR_PLAYBACK_STATE_PAUSE) {
1115 //on pause state,user seek to new pos,we need pause and wait
1116 //user to resume
1117 DVR_PB_DG(1, "pause, when got first frame event when user seek end");
1118 player->first_frame = 0;
1119 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1120 AmTsPlayer_pauseVideoDecoding(player->handle);
1121 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001122 }
hualing chen1ffd85b2021-08-16 15:18:43 +08001123 } else if (player->fffb_play == DVR_TRUE){
hualing chen4b7c15d2020-04-07 16:13:48 +08001124 //for first into fffb when reset speed
1125 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1126 _dvr_time_getClock() < player->next_fffb_time) {
hualing chen7ea70a72021-09-09 11:25:13 +08001127 DVR_PB_DG(1, "fffb timeout-fffb play---speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001128 player->speed,
1129 player->fffb_current,
1130 _dvr_time_getClock(),
1131 _dvr_playback_state_toString(player->state),
1132 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001133 //used timeout wait need lock first,so we unlock and lock
1134 //pthread_mutex_unlock(&player->lock);
1135 //pthread_mutex_lock(&player->lock);
1136 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001137 DVR_PB_DG(1, "unlock---");
hualing chen4b7c15d2020-04-07 16:13:48 +08001138 pthread_mutex_unlock(&player->lock);
1139 continue;
1140 }
hualing chen03fd4942021-07-15 15:56:41 +08001141 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
1142 player->speed,
1143 goto_rewrite,
1144 real_read,
1145 _dvr_playback_state_toString(player->state),
1146 player->cmd.cur_cmd,
1147 player->fffb_play);
hualing chen1679f812021-11-08 15:17:46 +08001148 DVR_PB_DG(1, "unlock---");
hualing chen4b7c15d2020-04-07 16:13:48 +08001149 pthread_mutex_unlock(&player->lock);
1150 goto_rewrite = DVR_FALSE;
1151 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001152 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001153 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1154 player->first_frame = 0;
1155 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1156 pthread_mutex_lock(&player->lock);
1157 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001158 }
hualing chenb31a6c62020-01-13 17:27:00 +08001159 }
hualing chen86e7d482020-01-16 15:13:33 +08001160
hualing chen30423862021-04-16 14:39:12 +08001161 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1162 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001163 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001164 DVR_PB_DG(1, "pause, continue");
hualing chen1679f812021-11-08 15:17:46 +08001165 DVR_PB_DG(1, "unlock---");
1166 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001167 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen1679f812021-11-08 15:17:46 +08001168 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001169 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001170 DVR_PB_DG(1, "unlock---");
hualing chen87072a82020-03-12 16:20:12 +08001171 pthread_mutex_unlock(&player->lock);
1172 continue;
1173 }
hualing chen266b9502020-04-04 17:39:39 +08001174 //when seek action is done. we need drop write timeout data.
1175 if (player->drop_ts == DVR_TRUE) {
1176 goto_rewrite = DVR_FALSE;
1177 real_read = 0;
1178 player->drop_ts = DVR_FALSE;
1179 }
hualing chen2aba4022020-03-02 13:49:55 +08001180 if (goto_rewrite == DVR_TRUE) {
1181 goto_rewrite = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08001182 DVR_PB_DG(1, "unlock---");
1183
hualing chen2aba4022020-03-02 13:49:55 +08001184 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001185 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001186 goto rewrite;
1187 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001188 //.check is need send time send end
hualing chen1679f812021-11-08 15:17:46 +08001189 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001190 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen1679f812021-11-08 15:17:46 +08001191 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001192 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001193 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001194 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001195 real_read = real_read + read;
1196 player->ts_cache_len = real_read;
hualing chenfbf8e022020-06-15 13:43:11 +08001197 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001198 pthread_mutex_unlock(&player->segment_lock);
hualing chen1679f812021-11-08 15:17:46 +08001199 DVR_PB_DG(1, "unlock---");
hualing chen87072a82020-03-12 16:20:12 +08001200 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001201 if (read < 0 && errno == EIO) {
1202 //EIO ERROR, EXIT THRAD
1203 DVR_PB_DG(1, "read error.EIO error, exit thread");
1204 DVR_Play_Notify_t notify;
1205 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1206 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001207 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001208 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001209 goto end;
1210 } else if (read < 0) {
1211 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1212 }
hualing chen87072a82020-03-12 16:20:12 +08001213 //if on fb mode and read file end , we need calculate pos to retry read.
1214 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001215 DVR_PB_DG(1, "recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]",
1216 read,
1217 real_read,
1218 buf_len,
1219 player->speed,
1220 player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001221 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
hualing chen1679f812021-11-08 15:17:46 +08001222 DVR_PB_DG(1, "lock---");
hualing chen87072a82020-03-12 16:20:12 +08001223 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001224 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001225 DVR_PB_DG(1, "unlock---");
hualing chen2aba4022020-03-02 13:49:55 +08001226 pthread_mutex_unlock(&player->lock);
1227 continue;
1228 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001229 //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 +08001230 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001231 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001232 #define MIN_CACHE_TIME (3000)
1233 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001234 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001235 if (_cache_time > MIN_CACHE_TIME) {
hualing chen1679f812021-11-08 15:17:46 +08001236 //pthread_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001237 /*if cache time > 20s , we think get time is error,*/
1238 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
1239 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1240 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1241 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1242 }
hualing chen1679f812021-11-08 15:17:46 +08001243 //_dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, ((_cache_time - MIN_CACHE_TIME) > MIN_CACHE_TIME ? MIN_CACHE_TIME : (_cache_time - MIN_CACHE_TIME)));
1244 //pthread_mutex_unlock(&player->lock);
1245 // 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 +08001246 //continue;
1247 }
hualing chen969fe7b2021-05-26 15:13:17 +08001248
hualing chen040df222020-01-17 13:35:02 +08001249 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001250 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001251 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001252
1253 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chen1679f812021-11-08 15:17:46 +08001254
1255 if (ret != DVR_SUCCESS && delay < MIN_TSPLAYER_DELAY_TIME) {
1256 player->noData++;
1257 DVR_PB_DG(1, "playback nodata[%d]", player->noData);
hualing chen40dd5462021-11-26 19:56:20 +08001258 if (player->noData == check_no_data_time) {
hualing chene3797f02021-01-13 14:53:28 +08001259 DVR_PB_DG(1, "playback send nodata event nodata[%d]", player->noData);
1260 //send event here and pause
1261 DVR_Play_Notify_t notify;
1262 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1263 notify.event = DVR_PLAYBACK_EVENT_NODATA;
hualing chen1679f812021-11-08 15:17:46 +08001264 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_NODATA--");
hualing chene3797f02021-01-13 14:53:28 +08001265 //get play statue not here
1266 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1267 }
1268 }
1269 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001270 if ((ret != DVR_SUCCESS &&
hualing chen03fd4942021-07-15 15:56:41 +08001271 (player->vendor != DVR_PLAYBACK_VENDOR_AMAZON) &&
hualing chen041c4092020-04-05 15:11:50 +08001272 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001273 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001274 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1275 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001276 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001277 DVR_Play_Notify_t notify;
1278 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1279 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1280 //get play statue not here
1281 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001282 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001283 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001284 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 +08001285 pthread_mutex_lock(&player->lock);
1286 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1287 pthread_mutex_unlock(&player->lock);
1288 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001289 } else if (ret != DVR_SUCCESS) {
hualing chen1679f812021-11-08 15:17:46 +08001290 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
1291 pthread_mutex_lock(&player->lock);
1292 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1293 pthread_mutex_unlock(&player->lock);
1294 delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chena540a7e2020-03-27 16:44:05 +08001295 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001296 if (delay < cache_time) {
1297 //delay time is changed and then has data to play, so not start timeout
hualing chen1679f812021-11-08 15:17:46 +08001298 reach_end_timeout = 0;
hualing chen39628212020-05-14 10:35:13 +08001299 } else {
1300 reach_end_timeout = reach_end_timeout + timeout;
1301 }
1302 cache_time = delay;
hualing chen31140872020-03-25 12:29:26 +08001303 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001304 }
hualing chen39628212020-05-14 10:35:13 +08001305 reach_end_timeout = 0;
1306 cache_time = 0;
hualing chen2932d372020-04-29 13:44:00 +08001307 //change next segment success case
1308 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen40dd5462021-11-26 19:56:20 +08001309 pthread_mutex_lock(&player->lock);
1310 player->noData = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001311 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001312 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1313 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen21a40372021-10-29 11:07:26 +08001314 pthread_mutex_lock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08001315 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen21a40372021-10-29 11:07:26 +08001316 real_read = real_read + read;
1317 player->ts_cache_len = real_read;
1318 pthread_mutex_unlock(&player->segment_lock);
1319
hualing chen87072a82020-03-12 16:20:12 +08001320 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001321 }//read len 0 check end
hualing chen40dd5462021-11-26 19:56:20 +08001322 if (player->noData >= check_no_data_time) {
hualing chene3797f02021-01-13 14:53:28 +08001323 player->noData = 0;
1324 DVR_PB_DG(1, "playback send data event resume[%d]", player->noData);
1325 //send event here and pause
1326 DVR_Play_Notify_t notify;
1327 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1328 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
hualing chen1679f812021-11-08 15:17:46 +08001329 DVR_PB_DG(1, "----send event DVR_PLAYBACK_EVENT_DATARESUME");
hualing chene3797f02021-01-13 14:53:28 +08001330 //get play statue not here
1331 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001332 }
hualing chen39628212020-05-14 10:35:13 +08001333 reach_end_timeout = 0;
hualing chen21a40372021-10-29 11:07:26 +08001334 //real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001335 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001336 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001337
hualing chena540a7e2020-03-27 16:44:05 +08001338 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001339 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001340 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001341 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001342 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001343 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001344 }
hualing chen266b9502020-04-04 17:39:39 +08001345 //if need write whole block size, we need check read buf len is eq block size.
1346 if (b_writed_whole_block == DVR_TRUE) {
1347 //buf_len is block size value.
1348 if (real_read < buf_len) {
1349 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001350 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001351 pthread_mutex_lock(&player->lock);
1352 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1353 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001354 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001355 continue;
1356 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001357 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 +08001358 }
1359 }
1360
Yahui Han1fbf3292021-11-08 18:17:19 +08001361 if (player->is_secure_mode && player->dec_func) {
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001362 DVR_CryptoParams_t crypto_params;
1363
1364 memset(&crypto_params, 0, sizeof(crypto_params));
1365 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1366 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1367 crypto_params.segment_id = player->cur_segment.segment_id;
hualing chen1f26ffa2020-11-03 10:39:20 +08001368 crypto_params.offset = segment_tell_position(player->r_handle) - wbufs.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001369 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1370 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001371 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1372 crypto_params.input_buffer.addr = (size_t)buf;
1373 crypto_params.input_buffer.size = real_read;
1374
Yahui Han1fbf3292021-11-08 18:17:19 +08001375 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1376 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1377 crypto_params.output_buffer.size = dec_buf_size;
1378 ret = player->dec_func(&crypto_params, player->dec_userdata);
1379 wbufs.buf_data = player->secure_buffer;
1380 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001381 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001382 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001383 }
pengfei.liufda2a972020-04-09 14:47:15 +08001384 wbufs.buf_size = crypto_params.output_size;
Yahui Han1fbf3292021-11-08 18:17:19 +08001385 } else if (player->cryptor) {
Yahui Han63b23b42021-12-07 15:37:46 +08001386 int len = real_read;
Yahui Han1fbf3292021-11-08 18:17:19 +08001387 am_crypt_des_crypt(player->cryptor, dec_bufs.buf_data, buf, &len, 1);
1388 wbufs.buf_data = dec_bufs.buf_data;
1389 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
1390 wbufs.buf_size = len;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001391 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001392rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001393 if (player->drop_ts == DVR_TRUE) {
1394 //need drop ts data when seek occur.we need read next loop,drop this ts data
1395 goto_rewrite = DVR_FALSE;
1396 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001397 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001398 player->drop_ts = DVR_FALSE;
hualing chena5f03222021-12-02 11:22:35 +08001399 DVR_PB_DG(1, "----drop ts");
hualing chenbcada022020-04-22 14:27:01 +08001400 continue;
1401 }
hualing chen21a40372021-10-29 11:07:26 +08001402
1403 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08001404 player->ts_cache_len = real_read;
hualing chenb9a02922021-12-14 11:29:47 +08001405 //used for printf first write data time.
1406 //to check change channel kpi.
1407 if (first_write == 0) {
1408 first_write++;
1409 DVR_PB_DG(1, "----firsr write ts data");
1410 }
1411
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001412 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1413 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001414 player->ts_cache_len = 0;
hualing chen21a40372021-10-29 11:07:26 +08001415 pthread_mutex_unlock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +08001416 real_read = 0;
1417 write_success++;
hualing chend241c7a2021-06-22 13:34:27 +08001418 if (CONTROL_SPEED_ENABLE == 1) {
1419check0:
1420 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
1421 pthread_mutex_lock(&player->lock);
1422 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
1423 pthread_mutex_unlock(&player->lock);
1424 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1425 goto check0;
1426 }
1427 }
hualing chen5605eed2020-05-26 18:18:06 +08001428 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001429 continue;
hualing chen87072a82020-03-12 16:20:12 +08001430 } else {
hualing chen21a40372021-10-29 11:07:26 +08001431 pthread_mutex_unlock(&player->segment_lock);
hualing chen7ea70a72021-09-09 11:25:13 +08001432 DVR_PB_DG(1, "write time out write_success:%d wbufs.buf_size:%d systime:%u",
hualing chen03fd4942021-07-15 15:56:41 +08001433 write_success,
1434 wbufs.buf_size,
1435 _dvr_time_getClock());
1436
hualing chena540a7e2020-03-27 16:44:05 +08001437 write_success = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001438 if (CONTROL_SPEED_ENABLE == 1) {
1439check1:
1440 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
1441 pthread_mutex_lock(&player->lock);
1442 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
1443 pthread_mutex_unlock(&player->lock);
1444 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1445 goto check1;
1446 }
1447 }
hualing chencc91e1c2020-02-28 13:26:17 +08001448 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001449 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001450 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001451 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001452 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001453 break;
1454 }
hualing chen2aba4022020-03-02 13:49:55 +08001455 goto_rewrite = DVR_TRUE;
1456 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001457 }
1458 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001459end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001460 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001461 free(buf);
1462 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001463 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001464}
1465
1466
hualing chen040df222020-01-17 13:35:02 +08001467static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001468{
hualing chen040df222020-01-17 13:35:02 +08001469 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001470
1471 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001472 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001473 return DVR_FAILURE;
1474 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001475 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001476 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001477 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001478 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001479 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001480 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001481 if (rc < 0)
1482 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001483 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001484}
1485
1486
hualing chen040df222020-01-17 13:35:02 +08001487static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001488{
hualing chen040df222020-01-17 13:35:02 +08001489 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001490
1491 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001492 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001493 return DVR_FAILURE;
1494 }
1495
hualing chen4b7c15d2020-04-07 16:13:48 +08001496 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001497 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001498 {
1499 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001500 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001501 pthread_join(player->playback_thread, NULL);
1502 }
1503 if (player->r_handle) {
1504 segment_close(player->r_handle);
1505 player->r_handle = NULL;
1506 }
hualing chen7a56cba2020-04-14 14:09:27 +08001507 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001508 return 0;
1509}
1510
hualing chen1679f812021-11-08 15:17:46 +08001511static int getFakePid()
1512{
1513 char fake_pid_prop[] = "vendor.tv.dtv.fake_pid";
1514 char buf[32];
1515 int pid = 0xffff;
1516
1517 dvr_prop_read(fake_pid_prop, buf, sizeof(buf));
1518
1519 if (sscanf(buf, "%i", &pid) != 1)
1520 {
1521 DVR_PB_DG(1, "get fake pid error");
1522 pid = 0xffff;
1523 }
1524 return pid;
1525}
1526
1527void dvr_playback_change_seek_state(DVR_PlaybackHandle_t handle,int pid) {
1528
1529 DVR_ASSERT(handle);
1530 DVR_Playback_t *player = (DVR_Playback_t *) handle;
1531 if (player == NULL) {
1532 DVR_PB_DG(1, "player is NULL");
1533 return ;
1534 }
1535 if (player->need_seek_start == DVR_FALSE) {
1536 DVR_PB_DG(1, "player need_seek_start is false");
1537 return ;
1538 }
1539
hualing chena5f03222021-12-02 11:22:35 +08001540 if (pid != player->fake_pid) {
hualing chen1679f812021-11-08 15:17:46 +08001541 player->need_seek_start = DVR_FALSE;
1542 }
1543 DVR_PB_DG(1, "player player->need_seek_start=%d", player->need_seek_start);
1544}
1545
hualing chenb31a6c62020-01-13 17:27:00 +08001546/**\brief Open an dvr palyback
1547 * \param[out] p_handle dvr playback addr
1548 * \param[in] params dvr playback open parameters
1549 * \retval DVR_SUCCESS On success
1550 * \return Error code
1551 */
hualing chen040df222020-01-17 13:35:02 +08001552int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001553
hualing chen040df222020-01-17 13:35:02 +08001554 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001555 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001556
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001557 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001558
hualing chen86e7d482020-01-16 15:13:33 +08001559 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001560 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001561 pthread_condattr_init(&cattr);
1562 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1563 pthread_cond_init(&player->cond, &cattr);
1564 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001565
hualing chen5cbe1a62020-02-10 16:36:36 +08001566 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001567 INIT_LIST_HEAD(&player->segment_list);
1568 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1569 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001570 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001571 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001572 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001573 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001574 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001575 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001576
hualing chen86e7d482020-01-16 15:13:33 +08001577 //store open params
hualing chen040df222020-01-17 13:35:02 +08001578 player->openParams.dmx_dev_id = params->dmx_dev_id;
1579 player->openParams.block_size = params->block_size;
hualing chena5f03222021-12-02 11:22:35 +08001580 DVR_PB_DG(1, "playback open block_size:[%d]",params->block_size);
hualing chen86e7d482020-01-16 15:13:33 +08001581 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001582 player->openParams.event_fn = params->event_fn;
1583 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001584 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001585 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001586
hualing chen5cbe1a62020-02-10 16:36:36 +08001587 player->has_pids = params->has_pids;
1588
hualing chen2aba4022020-03-02 13:49:55 +08001589 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001590
1591 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1592 //for test get callback
1593 if (0 && player->player_callback_func == NULL) {
1594 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1595 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen03fd4942021-07-15 15:56:41 +08001596 DVR_PB_DG(1, "playback open get callback[%p][%p][%p][%p]",
1597 player->player_callback_func,
1598 player->player_callback_userdata,
1599 _dvr_tsplayer_callback_test,
1600 player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001601 }
1602 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001603
hualing chen86e7d482020-01-16 15:13:33 +08001604 //init has audio and video
1605 player->has_video = DVR_FALSE;
1606 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001607 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001608 player->last_segment_id = 0LL;
1609 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001610
hualing chen5cbe1a62020-02-10 16:36:36 +08001611 //init ff fb time
hualing chen7ea70a72021-09-09 11:25:13 +08001612 player->fffb_current = 0;
1613 player->fffb_start = 0;
hualing chen03fd4942021-07-15 15:56:41 +08001614 player->fffb_start_pcr = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001615 //seek time
1616 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001617 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001618
Yahui Han1fbf3292021-11-08 18:17:19 +08001619 //allocate cryptor if have clearkey
1620 if (params->keylen > 0) {
1621 player->cryptor = am_crypt_des_open((uint8_t *)params->clearkey,
1622 (uint8_t *)params->cleariv,
1623 params->keylen * 8);
1624 if (!player->cryptor) {
1625 DVR_DEBUG(1, "%s , open des cryptor failed!!!\n", __func__);
1626 }
1627 } else {
1628 player->cryptor = NULL;
1629 }
1630
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001631 //init secure stuff
1632 player->dec_func = NULL;
1633 player->dec_userdata = NULL;
1634 player->is_secure_mode = 0;
1635 player->secure_buffer = NULL;
1636 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001637 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001638
hualing chen4b7c15d2020-04-07 16:13:48 +08001639 player->fffb_play = DVR_FALSE;
1640
1641 player->last_send_time_id = UINT64_MAX;
1642 player->last_cur_time = 0;
hualing chen30423862021-04-16 14:39:12 +08001643 player->seek_pause = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001644
hualing chend241c7a2021-06-22 13:34:27 +08001645 //speed con init
1646 if (CONTROL_SPEED_ENABLE == 1) {
1647 player->con_spe.ply_dur = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001648 player->con_spe.ply_sta = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001649 player->con_spe.sys_dur = 0;
1650 player->con_spe.sys_sta = 0;
1651 }
1652
hualing chen03fd4942021-07-15 15:56:41 +08001653 //limit info
1654 player->rec_start = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001655 player->limit = 0;
hualing chen8a657f32021-08-30 13:12:49 +08001656 //need seek to start pos
1657 player->first_start_time = 0;
1658 player->need_seek_start = DVR_TRUE;
hualing chena5f03222021-12-02 11:22:35 +08001659 //fake_pid init
1660 player->fake_pid = getFakePid();
hualing chen86e7d482020-01-16 15:13:33 +08001661 *p_handle = player;
1662 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001663}
1664
1665/**\brief Close an dvr palyback
1666 * \param[in] handle playback handle
1667 * \retval DVR_SUCCESS On success
1668 * \return Error code
1669 */
hualing chen040df222020-01-17 13:35:02 +08001670int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001671
hualing chen86e7d482020-01-16 15:13:33 +08001672 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001673 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001674 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001675 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001676 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001677 return DVR_FAILURE;
1678 }
1679
hualing chencc91e1c2020-02-28 13:26:17 +08001680 if (player->state != DVR_PLAYBACK_STATE_STOP)
1681 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001682 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
Yahui Han1fbf3292021-11-08 18:17:19 +08001683 if (player->cryptor) {
1684 am_crypt_des_close(player->cryptor);
1685 player->cryptor = NULL;
1686 }
hualing chencc91e1c2020-02-28 13:26:17 +08001687 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001688 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1689 } else {
1690 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001691 }
hualing chen7a56cba2020-04-14 14:09:27 +08001692 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001693 pthread_mutex_destroy(&player->lock);
1694 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001695
1696 if (player) {
1697 free(player);
1698 player = NULL;
1699 }
hualing chen7a56cba2020-04-14 14:09:27 +08001700 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001701 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001702}
1703
hualing chenb31a6c62020-01-13 17:27:00 +08001704/**\brief Start play audio and video, used start auido api and start video api
1705 * \param[in] handle playback handle
1706 * \param[in] params audio playback params,contains fmt and pid...
1707 * \retval DVR_SUCCESS On success
1708 * \return Error code
1709 */
hualing chen040df222020-01-17 13:35:02 +08001710int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1711 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001712 am_tsplayer_video_params vparams;
1713 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001714 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001715
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001716 memset(&vparams, 0, sizeof(vparams));
1717 memset(&aparams, 0, sizeof(aparams));
1718
hualing chena540a7e2020-03-27 16:44:05 +08001719 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001720 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001721 return DVR_FAILURE;
1722 }
hualing chencc91e1c2020-02-28 13:26:17 +08001723 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001724 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001725
hualing chena540a7e2020-03-27 16:44:05 +08001726 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001727 //can used start api to resume playback
1728 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1729 return dvr_playback_resume(handle);
1730 }
hualing chen87072a82020-03-12 16:20:12 +08001731 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001732 //if flag is puased and not decodec first frame. if user resume, we need
1733 //clear flag and set trickmode none
1734 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1735 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1736 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1737 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1738 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001739 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001740 return DVR_SUCCESS;
1741 }
hualing chen86e7d482020-01-16 15:13:33 +08001742 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001743 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001744 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001745 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001746 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001747 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001748 //start audio and video
hualing chena5f03222021-12-02 11:22:35 +08001749 if (vparams.pid != player->fake_pid && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001750 //audio abnd video pis is all invalid, return error.
hualing chena5f03222021-12-02 11:22:35 +08001751 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info [0x%x]", vparams.pid);
hualing chencc91e1c2020-02-28 13:26:17 +08001752 pthread_mutex_unlock(&player->lock);
1753 DVR_Play_Notify_t notify;
1754 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1755 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1756 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1757 notify.info.transition_failed_data.segment_id = segment_id;
1758 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001759 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001760 return -1;
1761 }
hualing chen31140872020-03-25 12:29:26 +08001762
hualing chencc91e1c2020-02-28 13:26:17 +08001763 {
hualing chen86e7d482020-01-16 15:13:33 +08001764 if (VALID_PID(vparams.pid)) {
1765 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001766 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001767 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001768 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001769 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1770 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001771 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001772 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001773 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001774 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001775 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001776 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001777 }
hualing chena93bbbc2020-12-22 17:23:42 +08001778 AmTsPlayer_showVideo(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001779 AmTsPlayer_setVideoParams(player->handle, &vparams);
hualing chen21a40372021-10-29 11:07:26 +08001780 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08001781 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001782 }
hualing chena540a7e2020-03-27 16:44:05 +08001783
hualing chen4b7c15d2020-04-07 16:13:48 +08001784 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1785 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001786 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1787 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1788 player->cmd.state = DVR_PLAYBACK_STATE_START;
1789 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001790 } else {
1791 player->cmd.last_cmd = player->cmd.cur_cmd;
1792 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001793 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001794 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001795 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001796 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001797 } else {
hualing chendf118dd2020-05-21 15:49:11 +08001798 if (VALID_PID(adparams.pid)) {
1799 player->has_ad_audio = DVR_TRUE;
1800 DVR_PB_DG(1, "start ad audio");
hualing chen1679f812021-11-08 15:17:46 +08001801 dvr_playback_change_seek_state(handle, adparams.pid);
hualing chendf118dd2020-05-21 15:49:11 +08001802 AmTsPlayer_setADParams(player->handle, &adparams);
1803 AmTsPlayer_enableADMix(player->handle);
1804 }
hualing chen969fe7b2021-05-26 15:13:17 +08001805 if (VALID_PID(aparams.pid)) {
1806 DVR_PB_DG(1, "start audio");
1807 player->has_audio = DVR_TRUE;
hualing chen1679f812021-11-08 15:17:46 +08001808 dvr_playback_change_seek_state(handle, aparams.pid);
hualing chen969fe7b2021-05-26 15:13:17 +08001809 AmTsPlayer_setAudioParams(player->handle, &aparams);
1810 AmTsPlayer_startAudioDecoding(player->handle);
1811 }
hualing chen31140872020-03-25 12:29:26 +08001812 }
hualing chencc91e1c2020-02-28 13:26:17 +08001813 player->cmd.state = DVR_PLAYBACK_STATE_START;
1814 player->state = DVR_PLAYBACK_STATE_START;
1815 }
hualing chen86e7d482020-01-16 15:13:33 +08001816 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001817 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001818 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001819 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001820 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001821}
hualing chen040df222020-01-17 13:35:02 +08001822/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001823 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001824 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001825 * \retval DVR_SUCCESS On success
1826 * \return Error code
1827 */
hualing chen040df222020-01-17 13:35:02 +08001828int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1829 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001830
hualing chena540a7e2020-03-27 16:44:05 +08001831 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001832 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001833 return DVR_FAILURE;
1834 }
1835
hualing chen4b7c15d2020-04-07 16:13:48 +08001836 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001837 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001838
hualing chen040df222020-01-17 13:35:02 +08001839 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1840 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001841
hualing chen86e7d482020-01-16 15:13:33 +08001842 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001843 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001844 //cp location
hualing chen040df222020-01-17 13:35:02 +08001845 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001846
hualing chen4b7c15d2020-04-07 16:13:48 +08001847 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 +08001848 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001849
1850 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001851 segment->pids.video.pid = info->pids.video.pid;
1852 segment->pids.video.format = info->pids.video.format;
1853 segment->pids.video.type = info->pids.video.type;
1854
hualing chen2aba4022020-03-02 13:49:55 +08001855 segment->pids.audio.pid = info->pids.audio.pid;
1856 segment->pids.audio.format = info->pids.audio.format;
1857 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001858
hualing chen2aba4022020-03-02 13:49:55 +08001859 segment->pids.ad.pid = info->pids.ad.pid;
1860 segment->pids.ad.format = info->pids.ad.format;
1861 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001862
1863 segment->pids.pcr.pid = info->pids.pcr.pid;
1864
hualing chen4b7c15d2020-04-07 16:13:48 +08001865 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 +08001866 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001867 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001868 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001869 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001870
hualing chen5cbe1a62020-02-10 16:36:36 +08001871 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001872}
hualing chen040df222020-01-17 13:35:02 +08001873/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001874 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001875 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001876 * \retval DVR_SUCCESS On success
1877 * \return Error code
1878 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001879int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001880 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001881 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001882 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001883 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001884 return DVR_FAILURE;
1885 }
1886
hualing chencc91e1c2020-02-28 13:26:17 +08001887 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001888 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001889 return DVR_FAILURE;
1890 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001891 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001892 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001893 DVR_PlaybackSegmentInfo_t *segment = NULL;
1894 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1895 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001896 {
hualing chen040df222020-01-17 13:35:02 +08001897 if (segment->segment_id == segment_id) {
1898 list_del(&segment->head);
1899 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001900 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001901 }
hualing chen86e7d482020-01-16 15:13:33 +08001902 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001903 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001904 pthread_mutex_unlock(&player->lock);
1905
1906 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001907}
hualing chen040df222020-01-17 13:35:02 +08001908/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001909 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001910 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001911 * \retval DVR_SUCCESS On success
1912 * \return Error code
1913 */
hualing chen040df222020-01-17 13:35:02 +08001914int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001915 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001916 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001917 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001918 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001919 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001920 return DVR_FAILURE;
1921 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001922 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1923 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1924 return DVR_SUCCESS;
1925 }
hualing chena540a7e2020-03-27 16:44:05 +08001926
hualing chen040df222020-01-17 13:35:02 +08001927 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001928 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001929 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001930 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001931 {
hualing chen040df222020-01-17 13:35:02 +08001932 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08001933 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001934 }
hualing chen86e7d482020-01-16 15:13:33 +08001935 // if encramble to free, only set flag and return;
1936
1937 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001938 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001939 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1940 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001941 //disable display, mute
hualing chen703f3572021-01-06 12:51:34 +08001942 DVR_PB_DG(1, "mute av");
hualing chen2aba4022020-03-02 13:49:55 +08001943 AmTsPlayer_hideVideo(player->handle);
1944 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001945 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1946 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001947 //enable display, unmute
hualing chen703f3572021-01-06 12:51:34 +08001948 DVR_PB_DG(1, "unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08001949 AmTsPlayer_showVideo(player->handle);
1950 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001951 } else {
1952 //do nothing
1953 }
1954 } else {
1955 //do nothing
1956 }
1957 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001958 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001959 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001960 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001961 pthread_mutex_unlock(&player->lock);
1962 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001963}
1964
1965
hualing chen99508642021-10-18 15:41:17 +08001966static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_PlaybackPids_t now_pids, DVR_PlaybackPids_t set_pids, int type) {
hualing chen040df222020-01-17 13:35:02 +08001967 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen275379e2021-06-15 17:57:21 +08001968 DVR_StreamInfo_t set_pid;
hualing chen99508642021-10-18 15:41:17 +08001969 DVR_StreamInfo_t now_pid;
hualing chen275379e2021-06-15 17:57:21 +08001970
1971 if (type == 0) {
1972 set_pid = set_pids.video;
hualing chen99508642021-10-18 15:41:17 +08001973 now_pid = now_pids.video;
hualing chen275379e2021-06-15 17:57:21 +08001974 } else if (type == 1) {
1975 set_pid = set_pids.audio;
hualing chen99508642021-10-18 15:41:17 +08001976 now_pid = now_pids.audio;
hualing chen275379e2021-06-15 17:57:21 +08001977 } else if (type == 2) {
1978 set_pid = set_pids.ad;
hualing chen99508642021-10-18 15:41:17 +08001979 now_pid = now_pids.ad;
hualing chen275379e2021-06-15 17:57:21 +08001980 } else {
1981 set_pid = set_pids.pcr;
hualing chen99508642021-10-18 15:41:17 +08001982 now_pid = now_pids.pcr;
hualing chen275379e2021-06-15 17:57:21 +08001983 }
1984
hualing chena540a7e2020-03-27 16:44:05 +08001985 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001986 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001987 return DVR_FAILURE;
1988 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001989 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001990 if (now_pid.pid == set_pid.pid) {
1991 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001992 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001993 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001994 if (VALID_PID(now_pid.pid)) {
1995 //stop now stream
1996 if (type == 0) {
1997 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001998 if (player->has_video == DVR_TRUE) {
1999 DVR_PB_DG(1, "stop video");
2000 AmTsPlayer_stopVideoDecoding(player->handle);
2001 player->has_video = DVR_FALSE;
2002 }
hualing chen86e7d482020-01-16 15:13:33 +08002003 } else if (type == 1) {
2004 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08002005 if (player->has_audio == DVR_TRUE) {
2006 DVR_PB_DG(1, "stop audio");
2007 AmTsPlayer_stopAudioDecoding(player->handle);
2008 player->has_audio = DVR_FALSE;
2009 }
hualing chen86e7d482020-01-16 15:13:33 +08002010 } else if (type == 2) {
2011 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08002012 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08002013 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002014 } else if (type == 3) {
2015 //pcr
2016 }
2017 }
2018 if (VALID_PID(set_pid.pid)) {
2019 //start
2020 if (type == 0) {
2021 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08002022 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08002023 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08002024 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002025 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002026 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08002027 AmTsPlayer_setVideoParams(player->handle, &vparams);
2028 AmTsPlayer_startVideoDecoding(player->handle);
2029 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08002030 } else if (type == 1) {
2031 //start audio
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002032 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen275379e2021-06-15 17:57:21 +08002033 if (VALID_PID(set_pids.ad.pid)) {
2034 am_tsplayer_audio_params adparams;
2035 adparams.pid = set_pids.ad.pid;
2036 adparams.codectype= _dvr_convert_stream_fmt(set_pids.ad.format, DVR_TRUE);
2037 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",adparams.pid, adparams.codectype);
2038 AmTsPlayer_setADParams(player->handle, &adparams);
2039 AmTsPlayer_enableADMix(player->handle);
2040 }
2041
hualing chenc70a8df2020-05-12 19:23:11 +08002042 am_tsplayer_audio_params aparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002043
2044 memset(&aparams, 0, sizeof(aparams));
2045
hualing chenc70a8df2020-05-12 19:23:11 +08002046 aparams.pid = set_pid.pid;
2047 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
2048 player->has_audio = DVR_TRUE;
2049 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
2050 AmTsPlayer_setAudioParams(player->handle, &aparams);
2051 AmTsPlayer_startAudioDecoding(player->handle);
2052 //playback_device_audio_start(player->handle,&aparams);
2053 }
hualing chen86e7d482020-01-16 15:13:33 +08002054 } else if (type == 2) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002055 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen99508642021-10-18 15:41:17 +08002056 if (set_pids.audio.pid == now_pids.audio.pid) {
2057 //stop audio if audio pid not change
2058 DVR_PB_DG(1, "stop audio when start ad");
2059 AmTsPlayer_stopAudioDecoding(player->handle);
2060 }
hualing chenc70a8df2020-05-12 19:23:11 +08002061 am_tsplayer_audio_params aparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002062
2063 memset(&aparams, 0, sizeof(aparams));
hualing chenc70a8df2020-05-12 19:23:11 +08002064 aparams.pid = set_pid.pid;
2065 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
2066 player->has_audio = DVR_TRUE;
2067 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
2068 AmTsPlayer_setADParams(player->handle, &aparams);
2069 AmTsPlayer_enableADMix(player->handle);
hualing chen99508642021-10-18 15:41:17 +08002070
2071 if (set_pids.audio.pid == now_pids.audio.pid) {
2072 am_tsplayer_audio_params aparams;
2073
2074 memset(&aparams, 0, sizeof(aparams));
2075
2076 aparams.pid = set_pids.audio.pid;
2077 aparams.codectype= _dvr_convert_stream_fmt(set_pids.audio.format, DVR_TRUE);
2078 player->has_audio = DVR_TRUE;
2079 DVR_PB_DG(1, "restart audio when start ad");
2080 AmTsPlayer_setAudioParams(player->handle, &aparams);
2081 AmTsPlayer_startAudioDecoding(player->handle);
2082 }
hualing chenc70a8df2020-05-12 19:23:11 +08002083 }
hualing chen86e7d482020-01-16 15:13:33 +08002084 } else if (type == 3) {
2085 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08002086 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08002087 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002088 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002089 //audio and video all close
2090 if (!player->has_audio && !player->has_video) {
2091 player->state = DVR_PLAYBACK_STATE_STOP;
2092 }
hualing chen86e7d482020-01-16 15:13:33 +08002093 }
2094 }
2095 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08002096}
hualing chena5f03222021-12-02 11:22:35 +08002097/**\brief dvr play back only update segment pids info
2098 * only update pid info not to start stop codec.
2099 * \param[in] handle playback handle
2100 * \param[in] segment_id need updated pids segment id
2101 * \param[in] p_pids need updated pids
2102 * \retval DVR_SUCCESS On success
2103 * \return Error code
2104 */
2105int dvr_playback_only_update_segment_pids(DVR_PlaybackHandle_t handle, uint64_t segment_id, DVR_PlaybackPids_t *p_pids) {
2106 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2107 if (player == NULL) {
2108 DVR_PB_DG(1, "player is NULL");
2109 return DVR_FAILURE;
2110 }
2111
2112 DVR_PlaybackSegmentInfo_t *segment;
2113 DVR_PB_DG(1, "lock");
2114 pthread_mutex_lock(&player->lock);
2115 DVR_PB_DG(1, "get lock update segment id: %lld cur id %lld", segment_id, player->cur_segment_id);
2116 list_for_each_entry(segment, &player->segment_list, head)
2117 {
2118 if (segment->segment_id == segment_id) {
2119 if (player->cur_segment_id == segment_id) {
2120 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2121 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2122 //do nothing when ff fb
2123 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
2124 pthread_mutex_unlock(&player->lock);
2125 return 0;
2126 }
2127 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
2128 }
2129 //save pids info
2130 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
2131 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
2132 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
2133 break;
2134 }
2135 }
2136 DVR_PB_DG(1, "unlock");
2137 pthread_mutex_unlock(&player->lock);
2138 return DVR_SUCCESS;
2139}
2140
hualing chen5cbe1a62020-02-10 16:36:36 +08002141/**\brief dvr play back update segment pids
2142 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08002143 * add pid stream and stop remove pid stream.
2144 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08002145 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08002146 * \retval DVR_SUCCESS On success
2147 * \return Error code
2148 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002149int 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 +08002150 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002151 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002152 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002153 return DVR_FAILURE;
2154 }
2155
hualing chen040df222020-01-17 13:35:02 +08002156 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08002157 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002158 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002159 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 +08002160
hualing chen040df222020-01-17 13:35:02 +08002161 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002162 {
hualing chen040df222020-01-17 13:35:02 +08002163 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002164
2165 if (player->cur_segment_id == segment_id) {
2166 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2167 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2168 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002169 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08002170 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002171 return 0;
2172 }
2173
2174 //if segment is on going segment,we need stop start stream
2175 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen1679f812021-11-08 15:17:46 +08002176 DVR_PB_DG(1, "unlock ---\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08002177 pthread_mutex_unlock(&player->lock);
hualing chen8a657f32021-08-30 13:12:49 +08002178 if (segment->pids.audio.pid != p_pids->audio.pid &&
2179 segment->pids.audio.pid == 0x1fff) {
hualing chena5f03222021-12-02 11:22:35 +08002180 //not used this to seek to start pos.we will
2181 //add uopdate only api. if need seek to start
2182 //pos, we will call only update api and used seek api
2183 //to start and stop av codec
2184 if (0 && player->need_seek_start == DVR_TRUE) {
hualing chen8a657f32021-08-30 13:12:49 +08002185 player->need_seek_start = DVR_FALSE;
2186 pthread_mutex_lock(&player->segment_lock);
2187 player->drop_ts = DVR_TRUE;
2188 player->ts_cache_len = 0;
2189 if (player->first_start_time > 0)
2190 player->first_start_time = player->first_start_time - 1;
2191 segment_seek(player->r_handle, (uint64_t)(player->first_start_time), player->openParams.block_size);
hualing chen1679f812021-11-08 15:17:46 +08002192 DVR_PB_DG(0, "unlock segment update need seek time_offset %llu [0x%x][0x%x]", player->first_start_time, segment->pids.audio.pid, segment->pids.ad.pid);
hualing chen8a657f32021-08-30 13:12:49 +08002193 pthread_mutex_unlock(&player->segment_lock);
2194 }
2195 }
hualing chen1679f812021-11-08 15:17:46 +08002196 //check video pids, stop or restart
2197 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 0);
2198 //check sub audio pids stop or restart
2199 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 2);
2200 //check audio pids stop or restart
2201 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 1);
2202 //check pcr pids stop or restart
2203 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids, *p_pids, 3);
2204
hualing chencc91e1c2020-02-28 13:26:17 +08002205 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002206 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
2207 //if state is pause, we need process at resume api. we only record change info
2208 int v_cmd = DVR_PLAYBACK_CMD_NONE;
2209 int a_cmd = DVR_PLAYBACK_CMD_NONE;
2210 if (VALID_PID(segment->pids.video.pid)
2211 && VALID_PID(p_pids->video.pid)
2212 && segment->pids.video.pid != p_pids->video.pid) {
2213 //restart video
2214 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
2215 }
2216 if (!VALID_PID(segment->pids.video.pid)
2217 && VALID_PID(p_pids->video.pid)
2218 && segment->pids.video.pid != p_pids->video.pid) {
2219 //start video
2220 v_cmd = DVR_PLAYBACK_CMD_VSTART;
2221 }
2222 if (VALID_PID(segment->pids.video.pid)
2223 && !VALID_PID(p_pids->video.pid)
2224 && segment->pids.video.pid != p_pids->video.pid) {
2225 //stop video
2226 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
2227 }
2228 if (VALID_PID(segment->pids.audio.pid)
2229 && VALID_PID(p_pids->audio.pid)
2230 && segment->pids.audio.pid != p_pids->audio.pid) {
2231 //restart audio
2232 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
2233 }
2234 if (!VALID_PID(segment->pids.audio.pid)
2235 && VALID_PID(p_pids->audio.pid)
2236 && segment->pids.audio.pid != p_pids->audio.pid) {
2237 //start audio
2238 a_cmd = DVR_PLAYBACK_CMD_ASTART;
2239 }
2240 if (VALID_PID(segment->pids.audio.pid)
2241 && !VALID_PID(p_pids->audio.pid)
2242 && segment->pids.audio.pid != p_pids->audio.pid) {
2243 //stop audio
2244 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
2245 }
2246 if (a_cmd == DVR_PLAYBACK_CMD_NONE
2247 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
2248 //do nothing
2249 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
2250 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
2251 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2252 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
2253 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
2254 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
2255 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
2256 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
2257 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2258 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
2259 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
2260 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2261 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2262 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
2263 } else {
2264 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2265 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
2266 }
2267
2268 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
2269 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
2270 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2271 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
2272 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
2273 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2274 //not occur this case
2275 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2276 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
2277 } else {
2278 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2279 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
2280 }
2281
2282 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
2283 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2284 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2285 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
2286 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
2287 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
2288 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2289 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
2290 } else {
2291 //not occur this case
2292 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2293 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2294 }
2295 }
2296 }
hualing chene10666f2020-04-14 13:58:37 +08002297 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08002298 }
hualing chen86e7d482020-01-16 15:13:33 +08002299 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08002300 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08002301 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08002302 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002303 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002304 }
hualing chen86e7d482020-01-16 15:13:33 +08002305 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002306 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002307 pthread_mutex_unlock(&player->lock);
2308 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002309}
2310/**\brief Stop play, will stop video and audio
2311 * \param[in] handle playback handle
2312 * \param[in] clear is clear last frame
2313 * \retval DVR_SUCCESS On success
2314 * \return Error code
2315 */
hualing chen040df222020-01-17 13:35:02 +08002316int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
2317 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002318 UNDVRUSED(clear);
hualing chena540a7e2020-03-27 16:44:05 +08002319 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002320 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002321 return DVR_FAILURE;
2322 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002323 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2324 DVR_PB_DG(1, ":playback is stoped");
2325 return DVR_SUCCESS;
2326 }
Ke Gong3c0caba2020-04-21 22:58:18 -07002327 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2328 DVR_PB_DG(1, ":playback is stoped");
2329 return DVR_SUCCESS;
2330 }
hualing chen87072a82020-03-12 16:20:12 +08002331 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08002332 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002333 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002334 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08002335 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002336 if (player->has_video) {
2337 AmTsPlayer_resumeVideoDecoding(player->handle);
2338 }
2339 if (player->has_audio) {
2340 AmTsPlayer_resumeAudioDecoding(player->handle);
2341 }
2342 if (player->has_video) {
2343 player->has_video = DVR_FALSE;
hualing chen10cdb162021-02-05 10:44:41 +08002344 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002345 AmTsPlayer_stopVideoDecoding(player->handle);
2346 }
2347 if (player->has_audio) {
2348 player->has_audio = DVR_FALSE;
2349 AmTsPlayer_stopAudioDecoding(player->handle);
2350 }
hualing chendf118dd2020-05-21 15:49:11 +08002351 if (player->has_ad_audio) {
2352 player->has_ad_audio =DVR_FALSE;
2353 AmTsPlayer_disableADMix(player->handle);
2354 }
hualing chen266b9502020-04-04 17:39:39 +08002355
hualing chen86e7d482020-01-16 15:13:33 +08002356 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002357 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2358 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2359 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002360 player->cur_segment_id = UINT64_MAX;
2361 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002362 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08002363 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08002364 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002365 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002366}
2367/**\brief Start play audio
2368 * \param[in] handle playback handle
2369 * \param[in] params audio playback params,contains fmt and pid...
2370 * \retval DVR_SUCCESS On success
2371 * \return Error code
2372 */
hualing chen2aba4022020-03-02 13:49:55 +08002373
hualing chendf118dd2020-05-21 15:49:11 +08002374int 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 +08002375 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002376
2377 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002378 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002379 return DVR_FAILURE;
2380 }
hualing chen86e7d482020-01-16 15:13:33 +08002381 _start_playback_thread(handle);
2382 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002383 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002384 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002385
hualing chendf118dd2020-05-21 15:49:11 +08002386 if (VALID_PID(adparam->pid)) {
2387 player->has_ad_audio = DVR_TRUE;
2388 DVR_PB_DG(1, "start ad audio");
2389 AmTsPlayer_setADParams(player->handle, adparam);
2390 AmTsPlayer_enableADMix(player->handle);
2391 }
hualing chen969fe7b2021-05-26 15:13:17 +08002392 if (VALID_PID(param->pid)) {
2393 DVR_PB_DG(1, "start audio");
2394 player->has_audio = DVR_TRUE;
2395 AmTsPlayer_setAudioParams(player->handle, param);
2396 AmTsPlayer_startAudioDecoding(player->handle);
2397 }
hualing chendf118dd2020-05-21 15:49:11 +08002398
hualing chen86e7d482020-01-16 15:13:33 +08002399 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002400 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
2401 player->cmd.state = DVR_PLAYBACK_STATE_START;
2402 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002403 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002404 pthread_mutex_unlock(&player->lock);
2405 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002406}
2407/**\brief Stop play audio
2408 * \param[in] handle playback handle
2409 * \retval DVR_SUCCESS On success
2410 * \return Error code
2411 */
hualing chen040df222020-01-17 13:35:02 +08002412int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2413 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002414
2415 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002416 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002417 return DVR_FAILURE;
2418 }
2419
hualing chen2aba4022020-03-02 13:49:55 +08002420 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002421 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002422 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2423 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002424 //destory thread
2425 _stop_playback_thread(handle);
2426 } else {
2427 //do nothing.video is playing
2428 }
hualing chen7a56cba2020-04-14 14:09:27 +08002429 DVR_PB_DG(1, "lock");
2430 pthread_mutex_lock(&player->lock);
2431
hualing chenf00cdc82020-06-10 14:23:35 +08002432 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002433 player->has_audio = DVR_FALSE;
2434 AmTsPlayer_stopAudioDecoding(player->handle);
2435 }
hualing chen87072a82020-03-12 16:20:12 +08002436
hualing chendf118dd2020-05-21 15:49:11 +08002437 if (player->has_ad_audio) {
2438 player->has_ad_audio =DVR_FALSE;
2439 AmTsPlayer_disableADMix(player->handle);
2440 }
2441
hualing chen87072a82020-03-12 16:20:12 +08002442 player->cmd.last_cmd = player->cmd.cur_cmd;
2443 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2444
hualing chen4b7c15d2020-04-07 16:13:48 +08002445 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002446 pthread_mutex_unlock(&player->lock);
2447 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002448}
2449/**\brief Start play video
2450 * \param[in] handle playback handle
2451 * \param[in] params video playback params,contains fmt and pid...
2452 * \retval DVR_SUCCESS On success
2453 * \return Error code
2454 */
hualing chen2aba4022020-03-02 13:49:55 +08002455int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002456 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002457
2458 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002459 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002460 return DVR_FAILURE;
2461 }
2462
hualing chen86e7d482020-01-16 15:13:33 +08002463 _start_playback_thread(handle);
2464 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002465 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002466 pthread_mutex_lock(&player->lock);
2467 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002468 AmTsPlayer_setVideoParams(player->handle, param);
hualing chen21a40372021-10-29 11:07:26 +08002469 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chena540a7e2020-03-27 16:44:05 +08002470 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002471
2472 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002473 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002474 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002475 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002476 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2477 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002478 }
2479 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002480 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2481 player->cmd.state = DVR_PLAYBACK_STATE_START;
2482 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002483 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002484 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002485 return DVR_SUCCESS;
2486}
2487/**\brief Stop play video
2488 * \param[in] handle playback handle
2489 * \retval DVR_SUCCESS On success
2490 * \return Error code
2491 */
hualing chen040df222020-01-17 13:35:02 +08002492int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2493 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002494
2495 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002496 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002497 return DVR_FAILURE;
2498 }
2499
hualing chen86e7d482020-01-16 15:13:33 +08002500 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002501 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2502 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002503 //destory thread
2504 _stop_playback_thread(handle);
2505 } else {
2506 //do nothing.audio is playing
2507 }
hualing chen7a56cba2020-04-14 14:09:27 +08002508
2509 DVR_PB_DG(1, "lock");
2510 pthread_mutex_lock(&player->lock);
2511
hualing chen87072a82020-03-12 16:20:12 +08002512 player->has_video = DVR_FALSE;
2513
2514 AmTsPlayer_stopVideoDecoding(player->handle);
2515 //playback_device_video_stop(player->handle);
2516
2517 player->cmd.last_cmd = player->cmd.cur_cmd;
2518 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2519
hualing chen4b7c15d2020-04-07 16:13:48 +08002520 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002521 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002522 return DVR_SUCCESS;
2523}
2524/**\brief Pause play
2525 * \param[in] handle playback handle
2526 * \param[in] flush whether its internal buffers should be flushed
2527 * \retval DVR_SUCCESS On success
2528 * \return Error code
2529 */
hualing chen040df222020-01-17 13:35:02 +08002530int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2531 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002532 UNDVRUSED(flush);
hualing chena540a7e2020-03-27 16:44:05 +08002533 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002534 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002535 return DVR_FAILURE;
2536 }
hualing chenf00cdc82020-06-10 14:23:35 +08002537 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2538 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002539 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002540 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002541 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002542 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002543 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002544 if (player->has_video)
2545 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002546 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002547 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002548
2549 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002550 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2551 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2552 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2553 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002554 } else {
2555 player->cmd.last_cmd = player->cmd.cur_cmd;
2556 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2557 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2558 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002559 }
hualing chen86e7d482020-01-16 15:13:33 +08002560 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002561 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002562
hualing chen86e7d482020-01-16 15:13:33 +08002563 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002564}
2565
hualing chen5cbe1a62020-02-10 16:36:36 +08002566//not add lock
2567static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2568{
2569 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2570
hualing chena540a7e2020-03-27 16:44:05 +08002571 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002572 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002573 return DVR_FAILURE;
2574 }
2575
hualing chen5cbe1a62020-02-10 16:36:36 +08002576 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002577 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002578 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002579 am_tsplayer_video_params vparams;
2580 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002581 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002582 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002583
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002584 memset(&vparams, 0, sizeof(vparams));
2585 memset(&aparams, 0, sizeof(aparams));
2586
hualing chendf118dd2020-05-21 15:49:11 +08002587 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002588 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002589 pthread_mutex_unlock(&player->lock);
2590
2591 switch (cmd) {
2592 case DVR_PLAYBACK_CMD_AVRESTART:
2593 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002594 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002595 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002596 break;
2597 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002598 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2599 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002600 break;
2601 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002602 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002603 break;
2604 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002605 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002606 break;
2607 case DVR_PLAYBACK_CMD_ARESTART:
2608 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002609 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002610 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002611 break;
2612 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002613 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002614 break;
2615 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002616 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002617 break;
2618 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002619 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2620 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2621 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002622 break;
2623 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002624 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2625 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002626 break;
2627 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002628 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2629 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002630 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002631 break;
2632 case DVR_PLAYBACK_CMD_STOP:
2633 break;
2634 case DVR_PLAYBACK_CMD_START:
2635 break;
2636 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002637 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2638 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002639 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002640 break;
2641 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002642 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2643 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002644 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002645 break;
2646 case DVR_PLAYBACK_CMD_FF:
2647 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002648 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002649 break;
2650 default:
2651 break;
2652 }
2653 return DVR_SUCCESS;
2654}
2655
2656/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002657 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002658 * \retval DVR_SUCCESS On success
2659 * \return Error code
2660 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002661int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2662 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08002663 uint32_t pos = 0;
hualing chen03fd4942021-07-15 15:56:41 +08002664 uint64_t segmentid = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002665 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002666 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002667 return DVR_FAILURE;
2668 }
2669
hualing chena991aa82021-08-16 10:21:15 +08002670 if (dvr_playback_check_limit(handle)) {
2671 //get id and pos to check if we can seek to this pos
hualing chen7ea70a72021-09-09 11:25:13 +08002672 DVR_PB_DG(1, "player start calculate time");
hualing chena991aa82021-08-16 10:21:15 +08002673 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2674 if (segmentid != player->cur_segment_id ||
2675 (segmentid == player->cur_segment_id &&
2676 pos > _dvr_get_cur_time(handle))) {
2677 //first to seek new pos and to resume
2678 DVR_PB_DG(1, "seek new pos and to resume");
2679 dvr_playback_seek(handle, segmentid, pos);
2680 }
hualing chen7ea70a72021-09-09 11:25:13 +08002681 } else {
2682 DVR_PB_DG(1, "player is not set limit");
hualing chen03fd4942021-07-15 15:56:41 +08002683 }
hualing chena991aa82021-08-16 10:21:15 +08002684
hualing chen5cbe1a62020-02-10 16:36:36 +08002685 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002686 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002687 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002688 player->first_frame = 0;
2689 if (player->has_video)
2690 AmTsPlayer_pauseVideoDecoding(player->handle);
2691 if (player->has_audio)
2692 AmTsPlayer_pauseAudioDecoding(player->handle);
2693
hualing chen266b9502020-04-04 17:39:39 +08002694 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002695 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002696 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2697 AmTsPlayer_resumeVideoDecoding(player->handle);
2698 }
2699 if (player->has_audio) {
2700 AmTsPlayer_resumeAudioDecoding(player->handle);
2701 }
2702 //check is has audio param,if has audio .we need start audio,
2703 //we will stop audio when ff fb, if reach end, we will pause.so we need
2704 //start audio when resume play
2705
2706 am_tsplayer_video_params vparams;
2707 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002708 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002709 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002710
2711 memset(&vparams, 0, sizeof(vparams));
2712 memset(&aparams, 0, sizeof(aparams));
hualing chendf118dd2020-05-21 15:49:11 +08002713 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002714 //valid audio pid, start audio
hualing chen969fe7b2021-05-26 15:13:17 +08002715 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2716 player->has_ad_audio = DVR_TRUE;
2717 DVR_PB_DG(1, "start ad audio");
hualing chen1679f812021-11-08 15:17:46 +08002718 dvr_playback_change_seek_state(handle, adparams.pid);
hualing chen969fe7b2021-05-26 15:13:17 +08002719 AmTsPlayer_setADParams(player->handle, &adparams);
2720 AmTsPlayer_enableADMix(player->handle);
2721 }
2722
hualing chenc70a8df2020-05-12 19:23:11 +08002723 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 +08002724 player->has_audio = DVR_TRUE;
hualing chen1679f812021-11-08 15:17:46 +08002725 dvr_playback_change_seek_state(handle, aparams.pid);
hualing chen266b9502020-04-04 17:39:39 +08002726 AmTsPlayer_setAudioParams(player->handle, &aparams);
2727 AmTsPlayer_startAudioDecoding(player->handle);
2728 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002729 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 +08002730 }
hualing chendf118dd2020-05-21 15:49:11 +08002731
hualing chen87072a82020-03-12 16:20:12 +08002732 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2733 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2734 player->cmd.state = DVR_PLAYBACK_STATE_START;
2735 player->state = DVR_PLAYBACK_STATE_START;
2736 } else {
2737 player->cmd.last_cmd = player->cmd.cur_cmd;
2738 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2739 player->cmd.state = DVR_PLAYBACK_STATE_START;
2740 player->state = DVR_PLAYBACK_STATE_START;
2741 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002742 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002743 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002744 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chen1ffd85b2021-08-16 15:18:43 +08002745 player->first_frame = 0;
2746 if (player->has_video)
2747 AmTsPlayer_pauseVideoDecoding(player->handle);
2748 if (player->has_audio)
2749 AmTsPlayer_pauseAudioDecoding(player->handle);
2750
hualing chene41f4372020-06-06 16:29:17 +08002751 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002752 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002753 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002754 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002755 }
hualing chen041c4092020-04-05 15:11:50 +08002756 if (player->has_audio)
2757 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002758 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002759 player->cmd.state = DVR_PLAYBACK_STATE_START;
2760 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002761 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2762 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002763 } else {
2764 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2765 {
hualing chen1679f812021-11-08 15:17:46 +08002766 DVR_PB_DG(1, "lock ---\r\n");
hualing chen041c4092020-04-05 15:11:50 +08002767 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002768 player->first_frame = 0;
2769 if (player->has_video)
2770 AmTsPlayer_pauseVideoDecoding(player->handle);
2771 if (player->has_audio)
2772 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen041c4092020-04-05 15:11:50 +08002773 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002774 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002775 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2776 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002777 if (player->has_video) {
2778 AmTsPlayer_resumeVideoDecoding(player->handle);
2779 }
2780 if (player->has_audio)
2781 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen1679f812021-11-08 15:17:46 +08002782 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen041c4092020-04-05 15:11:50 +08002783 pthread_mutex_unlock(&player->lock);
2784 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002785 }
2786 return DVR_SUCCESS;
2787}
2788
hualing chena540a7e2020-03-27 16:44:05 +08002789static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2790
2791 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2792 DVR_PlaybackSegmentInfo_t *segment = NULL;
2793 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2794 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2795
2796 list_for_each_entry(segment, &player->segment_list, head)
2797 {
2798 if (segment->segment_id == segment_id) {
2799 cur_segment = segment;
2800 }
2801 if (segment->segment_id == set_seg_id) {
2802 set_segment = segment;
2803 }
2804 if (cur_segment != NULL && set_segment != NULL) {
2805 break;
2806 }
2807 }
2808 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002809 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002810 return DVR_TRUE;
2811 }
2812 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2813 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2814 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2815 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002816 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 +08002817 return DVR_TRUE;
2818 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002819 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002820 return DVR_FALSE;
2821}
2822
hualing chen03fd4942021-07-15 15:56:41 +08002823/**\brief set limit
2824 * \param[in] handle playback handle
2825 * \param[in] rec start time ms
2826 * \param[in] rec limit time ms
2827 * \retval DVR_SUCCESS On success
2828 * \return Error code
2829 */
hualing chen7ea70a72021-09-09 11:25:13 +08002830int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint32_t time, uint32_t limit)
hualing chen03fd4942021-07-15 15:56:41 +08002831{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2832
2833 if (player == NULL) {
2834 DVR_PB_DG(1, "player is NULL");
2835 return DVR_FAILURE;
2836 }
hualing chen7ea70a72021-09-09 11:25:13 +08002837 _dvr_getClock_sec();
hualing chen1679f812021-11-08 15:17:46 +08002838 DVR_PB_DG(1, "lock time %lu limit: %u player->state:%d", time, limit, player->state);
hualing chen03fd4942021-07-15 15:56:41 +08002839 pthread_mutex_lock(&player->lock);
2840 player->rec_start = time;
2841 player->limit = limit;
hualing chen1679f812021-11-08 15:17:46 +08002842 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen03fd4942021-07-15 15:56:41 +08002843 pthread_mutex_unlock(&player->lock);
2844 return DVR_SUCCESS;
2845}
2846
hualing chen5cbe1a62020-02-10 16:36:36 +08002847/**\brief seek
2848 * \param[in] handle playback handle
2849 * \param[in] time_offset time offset base cur segment
2850 * \retval DVR_SUCCESS On success
2851 * \return Error code
2852 */
hualing chencc91e1c2020-02-28 13:26:17 +08002853int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002854 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002855 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08002856 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002857 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002858 return DVR_FAILURE;
2859 }
2860
hualing chen4b7c15d2020-04-07 16:13:48 +08002861 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 +08002862 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002863
hualing chen86e7d482020-01-16 15:13:33 +08002864 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002865 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002866 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002867
hualing chen5cbe1a62020-02-10 16:36:36 +08002868 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08002869 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08002870 if (ret ==DVR_FAILURE) {
hualing chen1679f812021-11-08 15:17:46 +08002871 DVR_PB_DG(1, "unlock seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002872 pthread_mutex_unlock(&player->lock);
2873 return DVR_FAILURE;
2874 }
2875 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2876 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002877 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002878 time_offset = _dvr_get_end_time(handle);
2879 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002880 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002881 time_offset = _dvr_get_end_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08002882 ret = DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08002883 }
2884 }
2885
hualing chen03fd4942021-07-15 15:56:41 +08002886 DVR_PB_DG(1, "seek open id[%lld]flag[0x%x] time_offset %u",
2887 player->cur_segment.segment_id,
2888 player->cur_segment.flags,
2889 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08002890 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002891 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2892 //forward playback.not seek end of file
2893 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2894 //default -2000ms
2895 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2896 }
hualing chen86e7d482020-01-16 15:13:33 +08002897 }
hualing chen8a657f32021-08-30 13:12:49 +08002898 if (player->need_seek_start == DVR_TRUE) {
2899 player->first_start_time = (uint64_t)time_offset + 1;//set first start time not eq 0
2900 }
hualing chen2aba4022020-03-02 13:49:55 +08002901 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002902 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002903 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002904 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002905 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 +08002906 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002907 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002908
hualing chen2aba4022020-03-02 13:49:55 +08002909 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002910
2911 player->last_send_time_id = UINT64_MAX;
hualing chen03fd4942021-07-15 15:56:41 +08002912 player->last_segment_tatol = 0LL;
2913 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08002914 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002915 player->fffb_current = _dvr_time_getClock();
2916 player->fffb_start = player->fffb_current;
2917 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2918 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002919 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002920 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002921 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002922 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002923 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002924 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002925 }
hualing chen86e7d482020-01-16 15:13:33 +08002926 //stop play
hualing chen03fd4942021-07-15 15:56:41 +08002927 DVR_PB_DG(0, "seek stop play, not inject data has video[%d]audio[%d]",
2928 player->has_video, player->has_audio);
hualing chen1ffd85b2021-08-16 15:18:43 +08002929
hualing chen266b9502020-04-04 17:39:39 +08002930 if (player->has_video) {
hualing chen7e14e532021-09-23 11:23:28 +08002931 //player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08002932 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08002933 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002934 }
2935
hualing chen40dd5462021-11-26 19:56:20 +08002936
hualing chen266b9502020-04-04 17:39:39 +08002937 if (player->has_audio) {
2938 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002939 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002940 }
hualing chendf118dd2020-05-21 15:49:11 +08002941 if (player->has_ad_audio) {
2942 player->has_ad_audio =DVR_FALSE;
2943 AmTsPlayer_disableADMix(player->handle);
2944 }
2945
hualing chen86e7d482020-01-16 15:13:33 +08002946 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002947 am_tsplayer_video_params vparams;
2948 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002949 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002950
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002951 memset(&vparams, 0, sizeof(vparams));
2952 memset(&aparams, 0, sizeof(aparams));
2953
hualing chen040df222020-01-17 13:35:02 +08002954 player->cur_segment_id = segment_id;
2955
2956 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002957 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002958 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002959 //start audio and video
hualing chena5f03222021-12-02 11:22:35 +08002960 if (vparams.pid != player->fake_pid && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2961 //audio and video pid is all invalid, return error.
2962 DVR_PB_DG(0, "unlock seek start dvr play back start error, not found audio and video info [0x%x]", vparams.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002963 pthread_mutex_unlock(&player->lock);
2964 return -1;
2965 }
hualing chena5f03222021-12-02 11:22:35 +08002966 DVR_PB_DG(0, "seek start[0x%x]", vparams.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002967 //add
hualing chen040df222020-01-17 13:35:02 +08002968 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002969 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002970 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002971 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002972 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002973 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002974 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002975 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002976 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002977 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002978 }
hualing chena5f03222021-12-02 11:22:35 +08002979 DVR_PB_DG(1, "start video");
hualing chen2aba4022020-03-02 13:49:55 +08002980 AmTsPlayer_setVideoParams(player->handle, &vparams);
hualing chen21a40372021-10-29 11:07:26 +08002981 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08002982 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002983 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2984 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2985 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2986 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2987 AmTsPlayer_stopFast(player->handle);
2988 }
hualing chen266b9502020-04-04 17:39:39 +08002989 player->has_video = DVR_TRUE;
hualing chen7e14e532021-09-23 11:23:28 +08002990 } else {
2991 player->has_video = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08002992 }
hualing chene41f4372020-06-06 16:29:17 +08002993 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002994 player->has_ad_audio = DVR_TRUE;
2995 DVR_PB_DG(1, "start ad audio");
hualing chen1679f812021-11-08 15:17:46 +08002996 dvr_playback_change_seek_state(handle, adparams.pid);
hualing chendf118dd2020-05-21 15:49:11 +08002997 AmTsPlayer_setADParams(player->handle, &adparams);
2998 AmTsPlayer_enableADMix(player->handle);
2999 }
hualing chen969fe7b2021-05-26 15:13:17 +08003000 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
3001 DVR_PB_DG(1, "start audio seek");
hualing chen1679f812021-11-08 15:17:46 +08003002 dvr_playback_change_seek_state(handle, aparams.pid);
hualing chen969fe7b2021-05-26 15:13:17 +08003003 AmTsPlayer_setAudioParams(player->handle, &aparams);
3004 AmTsPlayer_startAudioDecoding(player->handle);
3005 player->has_audio = DVR_TRUE;
3006 }
hualing chen86e7d482020-01-16 15:13:33 +08003007 }
hualing chen1ffd85b2021-08-16 15:18:43 +08003008 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen2aba4022020-03-02 13:49:55 +08003009 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
3010 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chena5f03222021-12-02 11:22:35 +08003011 if (VALID_PID(aparams.pid) || VALID_PID(vparams.pid))
3012 player->seek_pause = DVR_TRUE;
3013 DVR_PB_DG(1, "set state pause in seek vpid[0x%x]apid[0x%x]",vparams.pid, aparams.pid);
hualing chen87072a82020-03-12 16:20:12 +08003014 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3015 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08003016 player->speed > 1.0f||
3017 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003018 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08003019 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08003020 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08003021 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08003022 player->cmd.last_cmd = player->cmd.cur_cmd;
3023 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
3024 player->cmd.state = DVR_PLAYBACK_STATE_START;
3025 player->state = DVR_PLAYBACK_STATE_START;
3026 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003027 player->last_send_time_id = UINT64_MAX;
3028 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08003029 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003030
3031 return DVR_SUCCESS;
3032}
hualing chen5cbe1a62020-02-10 16:36:36 +08003033
hualing chen5cbe1a62020-02-10 16:36:36 +08003034static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
3035 //get cur time of segment
3036 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003037
Gong Ke2a0ebbe2021-05-25 15:22:50 +08003038 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003039 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003040 return DVR_FAILURE;
3041 }
3042
hualing chen31140872020-03-25 12:29:26 +08003043 int64_t cache = 0;//defalut es buf cache 500ms
hualing chen2aba4022020-03-02 13:49:55 +08003044 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003045 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
hualing chena5f03222021-12-02 11:22:35 +08003046 uint64_t cur = 0;
3047 if (player->ts_cache_len > 0 && pos < 0) {
3048 //this case is open new segment end,but cache data is last segment.
3049 //we need used last segment len to send play time.
3050 cur = 0;
3051 } else {
3052 cur = segment_tell_position_time(player->r_handle, pos);
3053 }
hualing chen21a40372021-10-29 11:07:26 +08003054 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08003055 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08003056 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 +08003057 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3058 cache = 0;
3059 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003060 int cur_time = (int)(cur > cache ? cur - cache : 0);
3061 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08003062}
3063
hualing chen969fe7b2021-05-26 15:13:17 +08003064static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
3065 //get cur time of segment
3066 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3067
hualing chen03fd4942021-07-15 15:56:41 +08003068 if (player == NULL || player->handle == 0) {
hualing chen969fe7b2021-05-26 15:13:17 +08003069 DVR_PB_DG(1, "player is NULL");
3070 return DVR_FAILURE;
3071 }
3072
3073 int64_t cache = 0;//defalut es buf cache 500ms
3074 int cur_time = 0;
hualing chen969fe7b2021-05-26 15:13:17 +08003075 pthread_mutex_lock(&player->segment_lock);
hualing chena5f03222021-12-02 11:22:35 +08003076 loff_t tmp_pos = segment_tell_position(player->r_handle);
3077 loff_t pos = tmp_pos - player->ts_cache_len;
3078 uint64_t cur = 0;
3079 if (player->ts_cache_len > 0 && (tmp_pos < player->ts_cache_len)) {
3080 //this case is open new segment end,but cache data is last segment.
3081 //we need used last segment len to send play time.
3082 cur = 0;
3083 DVR_PB_DG(1, "change segment [%lld][%lld]", player->last_segment_id, player->cur_segment_id);
3084 } else {
3085 cur = segment_tell_position_time(player->r_handle, pos);
3086 }
hualing chen21a40372021-10-29 11:07:26 +08003087 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen969fe7b2021-05-26 15:13:17 +08003088 pthread_mutex_unlock(&player->segment_lock);
hualing chena5f03222021-12-02 11:22:35 +08003089 DVR_PB_DG(1, "***get play cur time [%lld] cache:%lld cur id [%lld]last id [%lld] pb cache len [%d] [%lld][%lld]", cur, cache, player->cur_segment_id,player->last_send_time_id, player->ts_cache_len, pos, tmp_pos);
hualing chen969fe7b2021-05-26 15:13:17 +08003090 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3091 cache = 0;
3092 }
3093 if (cur > cache) {
3094 cur_time = (int)(cur - cache);
3095 *id = player->cur_segment_id;
3096 } else if (player->last_segment_tatol > 0) {
hualing chen8a657f32021-08-30 13:12:49 +08003097
3098 if (player->last_segment_tatol > (cache - cur))
3099 cur_time = (int)(player->last_segment_tatol - (cache - cur));
3100 else
3101 cur_time = (int)(player->last_segment_tatol - cur);
3102
hualing chen969fe7b2021-05-26 15:13:17 +08003103 *id = player->last_segment_id;
3104 DVR_PB_DG(1, "get play cur time[%lld][%lld][%d]", player->last_segment_id, player->cur_segment_id, player->last_segment_tatol);
3105 } else {
3106 cur_time = 0;
3107 *id = player->cur_segment_id;
3108 }
3109
3110 return cur_time;
3111}
3112
hualing chencc91e1c2020-02-28 13:26:17 +08003113//get current segment current pcr time of read pos
3114static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
3115 //get cur time of segment
3116 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003117
3118 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003119 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003120 return DVR_FAILURE;
3121 }
3122
hualing chen2aba4022020-03-02 13:49:55 +08003123 pthread_mutex_lock(&player->segment_lock);
3124 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003125 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08003126 pthread_mutex_unlock(&player->segment_lock);
3127 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08003128}
3129
hualing chen03fd4942021-07-15 15:56:41 +08003130DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
3131{
3132 //check is set limit info
3133 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3134
3135 if (player == NULL) {
3136 DVR_PB_DG(1, "player is NULL");
3137 return DVR_FALSE;
3138 }
3139 if (player->rec_start > 0 || player->limit > 0) {
3140 return DVR_TRUE;
3141 }
3142 return DVR_FALSE;
3143}
3144
3145/**\brief set DVR playback calculate expired time len
3146 * \param[in] handle, DVR playback session handle
3147 * \return DVR_SUCCESS on success
3148 * \return error code on failure
3149 */
hualing chen7ea70a72021-09-09 11:25:13 +08003150uint32_t dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
hualing chen03fd4942021-07-15 15:56:41 +08003151{
3152 //calculate expired time to play
3153 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08003154 uint32_t cur_time;
3155 uint32_t tmp_time;
3156 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003157 if (player == NULL) {
3158 DVR_PB_DG(1, "player is NULL");
3159 return expired;
3160 }
hualing chen7ea70a72021-09-09 11:25:13 +08003161 if (player->rec_start == 0 || player->limit == 0) {
3162 DVR_PB_DG(1, "rec limit 0");
hualing chen03fd4942021-07-15 15:56:41 +08003163 return expired;
3164 }
3165 //get system time
hualing chen7ea70a72021-09-09 11:25:13 +08003166 cur_time = _dvr_getClock_sec();
3167 if ((cur_time - player->rec_start) > player->limit) {
3168 tmp_time = (uint32_t)((cur_time - player->rec_start) - player->limit) * 1000U;
3169 expired = *(int*)&tmp_time;
3170 DVR_PB_DG(1, "cur_time:%u, rec start:%u limit:%d c_r_diff:%u expired:%u tmp_time:%u",
hualing chen03fd4942021-07-15 15:56:41 +08003171 cur_time,
3172 player->rec_start,
3173 player->limit,
hualing chen7ea70a72021-09-09 11:25:13 +08003174 (uint32_t)(cur_time - player->rec_start - player->limit), expired, tmp_time);
3175 }
hualing chen03fd4942021-07-15 15:56:41 +08003176 return expired;
3177}
3178
3179/**\brief set DVR playback obsolete time
3180 * \param[in] handle, DVR playback session handle
3181 * \param[in] obsolete, obsolete len
3182 * \return DVR_SUCCESS on success
3183 * \return error code on failure
3184 */
3185int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
3186{
3187 int expired = 0;
3188 //calculate expired time to play
3189 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3190
3191 if (player == NULL) {
3192 DVR_PB_DG(1, "player is NULL");
3193 return DVR_FALSE;
3194 }
3195 //get system time
hualing chen1679f812021-11-08 15:17:46 +08003196 DVR_PB_DG(1, "lock ---\r\n");
hualing chen03fd4942021-07-15 15:56:41 +08003197 pthread_mutex_lock(&player->lock);
3198 player->obsolete = obsolete;
hualing chen1679f812021-11-08 15:17:46 +08003199 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen03fd4942021-07-15 15:56:41 +08003200 pthread_mutex_unlock(&player->lock);
3201 return expired;
3202}
3203
3204/**\brief update DVR playback newest segment duration
3205 * \param[in] handle, DVR playback session handle
3206 * \param[in] segmentid, newest segment id
3207 * \param[in] dur dur time ms
3208 * \return DVR_SUCCESS on success
3209 * \return error code on failure
3210 */
3211int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
3212uint64_t segmentid, int dur)
3213{
3214 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3215 DVR_PlaybackSegmentInfo_t *segment;
3216 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
3217
3218 if (player == NULL) {
3219 DVR_PB_DG(1, " player is NULL");
3220 return DVR_FAILURE;
3221 }
3222 //update the newest segment duration on timeshift mode
3223 list_for_each_entry(segment, &player->segment_list, head)
3224 {
3225 if (segment->segment_id == segmentid) {
3226 segment->duration = dur;
3227 break;
3228 }
3229 pre_segment = segment;
3230 }
3231
3232 return DVR_SUCCESS;
3233}
3234
hualing chen7ea70a72021-09-09 11:25:13 +08003235static uint32_t dvr_playback_calculate_last_valid_segment(
3236 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos)
hualing chen03fd4942021-07-15 15:56:41 +08003237{
hualing chen7ea70a72021-09-09 11:25:13 +08003238 uint32_t off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003239 uint64_t segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003240 uint32_t pre_off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003241 uint64_t last_segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003242 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003243 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3244
3245 if (player == NULL) {
3246 DVR_PB_DG(1, "player is NULL");
3247 return DVR_FAILURE;
3248 }
3249 expired = dvr_playback_calculate_expiredlen(handle);
hualing chen7e14e532021-09-23 11:23:28 +08003250 if (expired == 0) {
3251 *segmentid = player->cur_segment_id;
3252 *pos = 0;
3253 return DVR_SUCCESS;
3254 }
hualing chen03fd4942021-07-15 15:56:41 +08003255 DVR_PlaybackSegmentInfo_t *pseg;
3256 list_for_each_entry_reverse(pseg, &player->segment_list, head) {
3257 segment_id = pseg->segment_id;
3258
3259 if ((player->obsolete + pre_off + pseg->duration) > expired)
3260 break;
3261
3262 last_segment_id = pseg->segment_id;
3263 pre_off += pseg->duration;
3264 }
3265
3266 if (last_segment_id == segment_id) {
3267 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
3268 off = expired;
3269 } else if (player->obsolete >= expired) {
3270 off = 0;
3271 } else {
3272 off = expired - pre_off - player->obsolete;
3273 }
3274 *segmentid = segment_id;
3275 *pos = off;
3276 return DVR_SUCCESS;
3277}
3278
hualing chen4b7c15d2020-04-07 16:13:48 +08003279#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003280//start replay
3281static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3282
3283 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3284 //calculate pcr seek time
3285 int t_diff = 0;
3286 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003287 uint64_t segmentid = 0;
3288 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003289 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003290 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003291 return DVR_FAILURE;
3292 }
3293
hualing chen5cbe1a62020-02-10 16:36:36 +08003294 if (player->fffb_start == -1) {
3295 //set fffb start time ms
3296 player->fffb_start = _dvr_time_getClock();
3297 player->fffb_current = player->fffb_start;
3298 //get segment current time pos
3299 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08003300 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
3301 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003302 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003303 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003304 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003305 } else {
3306 player->fffb_current = _dvr_time_getClock();
3307 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003308 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003309 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003310 if (seek_time <= 0) {
3311 //need seek to pre one segment
3312 seek_time = 0;
3313 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003314 //seek segment pos
3315 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003316 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003317 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003318 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3319 //set seek time to 0;
hualing chen03fd4942021-07-15 15:56:41 +08003320 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]",
3321 seek_time,
3322 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003323 seek_time = 0;
3324 }
hualing chen03fd4942021-07-15 15:56:41 +08003325 if (IS_FB(player->speed)
3326 && dvr_playback_check_limit(handle)) {
3327 //fb case.check expired time
3328 //get id and pos to check if we can seek to this pos
3329 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3330 //case cur id < segment id
3331 if (player->cur_segment_id < segmentid) {
3332 //expired ts data is player,return error
3333 //
3334 pthread_mutex_unlock(&player->segment_lock);
3335 return 0;
3336 } else if (player->cur_segment_id == segmentid) {
3337 //id is same,compare seek pos
3338 if (seek_time < pos) {
3339 //expired ts data is player,return error
3340 //
3341 pthread_mutex_unlock(&player->segment_lock);
3342 return 0;
3343 }
3344 }
3345 //case can play
3346 }
hualing chen041c4092020-04-05 15:11:50 +08003347 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3348 seek_time = 0;
3349 }
hualing chen2aba4022020-03-02 13:49:55 +08003350 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003351 } else {
3352 //
hualing chen4b7c15d2020-04-07 16:13:48 +08003353 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003354 }
hualing chen03fd4942021-07-15 15:56:41 +08003355 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
3356 seek_time,
3357 player->speed,
3358 player->cur_segment_id,
3359 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003360 }
hualing chen2aba4022020-03-02 13:49:55 +08003361 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003362}
3363
3364
3365//start replay
3366static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3367 //
3368 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003369
3370 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003371 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003372 return DVR_FAILURE;
3373 }
3374
hualing chen5cbe1a62020-02-10 16:36:36 +08003375 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003376 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003377 DVR_PB_DG(1, "fffb stop video");
hualing chen21a40372021-10-29 11:07:26 +08003378 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003379 AmTsPlayer_stopVideoDecoding(player->handle);
3380 }
3381 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003382 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003383 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003384 AmTsPlayer_stopAudioDecoding(player->handle);
3385 }
hualing chendf118dd2020-05-21 15:49:11 +08003386 if (player->has_ad_audio) {
3387 DVR_PB_DG(1, "fffb stop audio");
3388 player->has_ad_audio =DVR_FALSE;
3389 AmTsPlayer_disableADMix(player->handle);
3390 }
hualing chen2aba4022020-03-02 13:49:55 +08003391
hualing chen5cbe1a62020-02-10 16:36:36 +08003392 //start video and audio
3393
hualing chen2aba4022020-03-02 13:49:55 +08003394 am_tsplayer_video_params vparams;
3395 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003396 am_tsplayer_audio_params adparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003397
3398 memset(&vparams, 0, sizeof(vparams));
3399 memset(&aparams, 0, sizeof(aparams));
3400
hualing chen87072a82020-03-12 16:20:12 +08003401 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003402
3403 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08003404 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08003405 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003406 //start audio and video
3407 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
3408 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003409 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003410 return -1;
3411 }
3412
3413 if (VALID_PID(vparams.pid)) {
3414 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08003415 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08003416 //DVR_PB_DG(1, "fffb start video and save last frame");
3417 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003418 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003419 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
3420 AmTsPlayer_setVideoParams(player->handle, &vparams);
hualing chen21a40372021-10-29 11:07:26 +08003421 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003422 AmTsPlayer_startVideoDecoding(player->handle);
3423 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003424 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003425 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003426 }
hualing chen31140872020-03-25 12:29:26 +08003427 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08003428 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003429 AmTsPlayer_stopFast(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003430 return 0;
3431}
3432
3433static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3434 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003435 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003436 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003437 return DVR_FAILURE;
3438 }
3439
3440 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08003441 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003442 pthread_mutex_lock(&player->lock);
3443
hualing chen2aba4022020-03-02 13:49:55 +08003444 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003445 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 +08003446
hualing chen87072a82020-03-12 16:20:12 +08003447 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3448 //seek time set 0
3449 seek_time = 0;
3450 }
hualing chen041c4092020-04-05 15:11:50 +08003451 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003452 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3453 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003454 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003455 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003456 DVR_PB_DG(1, "unlock");
hualing chen87072a82020-03-12 16:20:12 +08003457 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003458 //send event here and pause
3459 DVR_Play_Notify_t notify;
3460 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003461 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003462 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003463 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08003464 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 +08003465 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003466 return DVR_SUCCESS;
3467 }
hualing chen2932d372020-04-29 13:44:00 +08003468 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003469 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003470 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003471 }
3472 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003473 _dvr_playback_fffb_replay(handle);
3474
3475 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003476 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003477
hualing chen5cbe1a62020-02-10 16:36:36 +08003478 return DVR_SUCCESS;
3479}
3480
hualing chen87072a82020-03-12 16:20:12 +08003481//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003482static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003483 //
3484 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003485
3486 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003487 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003488 return DVR_FAILURE;
3489 }
3490
hualing chen5cbe1a62020-02-10 16:36:36 +08003491 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003492 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003493 player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003494 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003495 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003496 }
3497
3498 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003499 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003500 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003501 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003502 //start video and audio
3503
hualing chen2aba4022020-03-02 13:49:55 +08003504 am_tsplayer_video_params vparams;
3505 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003506 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08003507 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003508
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003509 memset(&vparams, 0, sizeof(vparams));
3510 memset(&aparams, 0, sizeof(aparams));
3511
hualing chen5cbe1a62020-02-10 16:36:36 +08003512 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08003513 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08003514 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003515 //start audio and video
3516 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003517 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003518 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003519 return -1;
3520 }
3521
3522 if (VALID_PID(vparams.pid)) {
3523 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003524 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003525 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003526 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003527 }
hualing chen266b9502020-04-04 17:39:39 +08003528 else {
hualing chen2aba4022020-03-02 13:49:55 +08003529 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003530 }
hualing chen2aba4022020-03-02 13:49:55 +08003531 AmTsPlayer_setVideoParams(player->handle, &vparams);
hualing chen21a40372021-10-29 11:07:26 +08003532 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003533 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003534 }
hualing chena540a7e2020-03-27 16:44:05 +08003535
3536 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08003537 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08003538 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003539 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003540 } else {
hualing chendf118dd2020-05-21 15:49:11 +08003541 if (VALID_PID(adparams.pid)) {
3542 player->has_ad_audio = DVR_TRUE;
3543 DVR_PB_DG(1, "start ad audio");
3544 AmTsPlayer_setADParams(player->handle, &adparams);
3545 AmTsPlayer_enableADMix(player->handle);
3546 }
hualing chen969fe7b2021-05-26 15:13:17 +08003547 if (VALID_PID(aparams.pid)) {
3548 player->has_audio = DVR_TRUE;
3549 DVR_PB_DG(1, "start audio");
3550 AmTsPlayer_setAudioParams(player->handle, &aparams);
3551 AmTsPlayer_startAudioDecoding(player->handle);
3552 }
hualing chendf118dd2020-05-21 15:49:11 +08003553
hualing chen7a56cba2020-04-14 14:09:27 +08003554 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003555 AmTsPlayer_stopFast(player->handle);
3556 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3557 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3558 }
hualing chen2aba4022020-03-02 13:49:55 +08003559 player->cmd.last_cmd = player->cmd.cur_cmd;
3560 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003561 player->cmd.state = DVR_PLAYBACK_STATE_START;
3562 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08003563 return 0;
3564}
3565
3566
hualing chenb31a6c62020-01-13 17:27:00 +08003567/**\brief Set play speed
3568 * \param[in] handle playback handle
3569 * \param[in] speed playback speed
3570 * \retval DVR_SUCCESS On success
3571 * \return Error code
3572 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003573int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3574
3575 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003576
3577 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003578 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003579 return DVR_FAILURE;
3580 }
3581
hualing chena540a7e2020-03-27 16:44:05 +08003582 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003583 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003584 return DVR_FAILURE;
3585 }
hualing chenf00cdc82020-06-10 14:23:35 +08003586 if (speed.speed.speed == player->cmd.speed.speed.speed) {
3587 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
3588 return DVR_SUCCESS;
3589 }
hualing chen1679f812021-11-08 15:17:46 +08003590 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
3591
hualing chen5cbe1a62020-02-10 16:36:36 +08003592 pthread_mutex_lock(&player->lock);
3593 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3594 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3595 player->cmd.last_cmd = player->cmd.cur_cmd;
3596 }
hualing chene41f4372020-06-06 16:29:17 +08003597
hualing chen31140872020-03-25 12:29:26 +08003598 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003599 IS_KERNEL_SPEED(speed.speed.speed) ) {
3600 //case 1. not start play.only set speed
3601 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3602 //only set speed.and return;
3603 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3604 player->cmd.speed.speed = speed.speed;
3605 player->speed = (float)speed.speed.speed/(float)100;
3606 player->fffb_play = DVR_FALSE;
3607 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003608 DVR_PB_DG(1, "unlock");
hualing chenf00cdc82020-06-10 14:23:35 +08003609 return DVR_SUCCESS;
3610 }
3611 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003612 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3613 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003614 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003615 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3616 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003617 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003618 AmTsPlayer_stopFast(player->handle);
3619 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003620 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003621 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
hualing chen1679f812021-11-08 15:17:46 +08003622 DVR_PB_DG(1, "lock ---\r\n");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003623 pthread_mutex_lock(&player->lock);
3624 } else {
3625 //set play speed and if audio is start, stop audio.
3626 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003627 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003628 AmTsPlayer_stopAudioDecoding(player->handle);
3629 player->has_audio = DVR_FALSE;
3630 }
hualing chenb96aa2c2020-04-15 14:13:53 +08003631 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003632 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003633 }
hualing chenbcada022020-04-22 14:27:01 +08003634 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003635 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003636 player->cmd.speed.speed = speed.speed;
3637 player->speed = (float)speed.speed.speed/(float)100;
hualing chen1679f812021-11-08 15:17:46 +08003638 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003639 pthread_mutex_unlock(&player->lock);
3640 return DVR_SUCCESS;
3641 }
hualing chen31140872020-03-25 12:29:26 +08003642 //case 3 fffb mode
3643 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3644 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3645 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003646 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003647 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003648 player->cmd.speed.speed = speed.speed;
3649 player->speed = (float)speed.speed.speed/(float)100;
3650 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003651 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08003652 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003653 pthread_mutex_unlock(&player->lock);
3654 return DVR_SUCCESS;
3655 }
3656 }
3657 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003658 IS_KERNEL_SPEED(speed.speed.speed)) {
3659 //case 1. cur speed is kernel support speed,set kernel speed.
3660 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003661 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003662 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3663 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003664 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003665 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08003666 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003667 } else {
3668 //set play speed and if audio is start, stop audio.
3669 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003670 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003671 AmTsPlayer_stopAudioDecoding(player->handle);
3672 player->has_audio = DVR_FALSE;
3673 }
hualing chenf00cdc82020-06-10 14:23:35 +08003674 DVR_PB_DG(1, "start fast");
3675 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003676 }
hualing chena540a7e2020-03-27 16:44:05 +08003677 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003678 player->cmd.speed.speed = speed.speed;
3679 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003680 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08003681 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003682 pthread_mutex_unlock(&player->lock);
3683 return DVR_SUCCESS;
3684 }
3685 //case 2 fffb mode
3686 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3687 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3688 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003689 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003690 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003691 player->cmd.speed.speed = speed.speed;
3692 player->speed = (float)speed.speed.speed/(float)100;
3693 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08003694 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08003695 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003696 pthread_mutex_unlock(&player->lock);
3697 return DVR_SUCCESS;
3698 }
hualing chen31140872020-03-25 12:29:26 +08003699 }
hualing chena540a7e2020-03-27 16:44:05 +08003700 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3701 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003702 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003703 } else {
hualing chen31140872020-03-25 12:29:26 +08003704 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003705 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3706 else
3707 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003708 player->fffb_play = DVR_TRUE;
3709 }
3710 DVR_Bool_t init_last_time = DVR_FALSE;
3711 if (player->speed > 0.0f && speed.speed.speed < 0) {
3712 init_last_time = DVR_TRUE;
3713 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3714 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003715 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003716 player->cmd.speed.mode = speed.mode;
3717 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003718 player->speed = (float)speed.speed.speed/(float)100;
3719 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003720 _dvr_init_fffb_t(handle);
3721 if (init_last_time == DVR_TRUE)
3722 player->last_send_time_id = UINT64_MAX;
3723
hualing chen87072a82020-03-12 16:20:12 +08003724 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003725 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3726 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003727 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003728 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003729 _dvr_playback_replay(handle, DVR_FALSE);
3730 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3731 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3732 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003733 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003734 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003735 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003736 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003737 return DVR_SUCCESS;
3738}
hualing chen2932d372020-04-29 13:44:00 +08003739
hualing chenb31a6c62020-01-13 17:27:00 +08003740/**\brief Get playback status
3741 * \param[in] handle playback handle
3742 * \param[out] p_status playback status
3743 * \retval DVR_SUCCESS On success
3744 * \return Error code
3745 */
hualing chen2932d372020-04-29 13:44:00 +08003746static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3747 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003748//
3749 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003750 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003751 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003752 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003753 return DVR_FAILURE;
3754 }
hualing chen1679f812021-11-08 15:17:46 +08003755 if (is_lock ==DVR_TRUE) {
3756 DVR_PB_DG(1, "lock");
hualing chen2932d372020-04-29 13:44:00 +08003757 pthread_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003758 }
3759
hualing chen5cbe1a62020-02-10 16:36:36 +08003760 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003761 //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 +08003762 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3763 player->state == DVR_PLAYBACK_STATE_START) {
3764 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3765 }
hualing chen041c4092020-04-05 15:11:50 +08003766
hualing chencc91e1c2020-02-28 13:26:17 +08003767 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003768 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003769
3770 if (CONTROL_SPEED_ENABLE == 1) {
hualing chen7ea70a72021-09-09 11:25:13 +08003771 if (player->con_spe.ply_sta == 0) {
3772 DVR_PB_DG(1, "player dur[%u] sta[%u] cur[%d] -----reinit",
hualing chen03fd4942021-07-15 15:56:41 +08003773 player->con_spe.ply_dur,
3774 player->con_spe.ply_sta,
3775 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003776 player->con_spe.ply_sta = p_status->time_cur;
3777 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3778 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
hualing chen7ea70a72021-09-09 11:25:13 +08003779 DVR_PB_DG(1, "player dur[%u] sta[%u] cur[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003780 player->con_spe.ply_dur,
3781 player->con_spe.ply_sta,
3782 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003783 player->con_spe.ply_sta = p_status->time_cur;
3784 }
3785
3786 if (player->con_spe.sys_sta == 0) {
3787 player->con_spe.sys_sta = _dvr_time_getClock();
3788 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
3789 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
3790 player->con_spe.sys_sta = _dvr_time_getClock();
3791 }
3792 }
3793
hualing chen4b7c15d2020-04-07 16:13:48 +08003794 if (player->last_send_time_id == UINT64_MAX) {
3795 player->last_send_time_id = player->cur_segment_id;
3796 player->last_cur_time = p_status->time_cur;
3797 }
3798 if (player->last_send_time_id == player->cur_segment_id) {
3799 if (player->speed > 0.0f ) {
3800 //ff
3801 if (p_status->time_cur < player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003802 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]",
3803 player->last_cur_time,
3804 p_status->time_cur,
3805 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08003806 p_status->time_cur = player->last_cur_time;
3807 } else {
3808 player->last_cur_time = p_status->time_cur;
3809 }
hualing chene41f4372020-06-06 16:29:17 +08003810 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003811 //fb
3812 if (p_status->time_cur > player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003813 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]",
3814 player->last_cur_time,
3815 p_status->time_cur,
3816 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08003817 p_status->time_cur = player->last_cur_time;
3818 } else {
3819 player->last_cur_time = p_status->time_cur;
3820 }
3821 }
hualing chend241c7a2021-06-22 13:34:27 +08003822 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08003823 player->last_cur_time = p_status->time_cur;
3824 }
hualing chen969fe7b2021-05-26 15:13:17 +08003825 player->last_send_time_id = segment_id;
3826 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003827
hualing chen5cbe1a62020-02-10 16:36:36 +08003828 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003829 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003830 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003831 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 +08003832 _dvr_playback_state_toString(player->state),
3833 _dvr_playback_state_toString(p_status->state),
3834 p_status->time_cur, p_status->time_end,
3835 p_status->segment_id,player->play_flag,
3836 player->speed,
3837 is_lock);
hualing chen1679f812021-11-08 15:17:46 +08003838 if (is_lock ==DVR_TRUE) {
3839 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen2932d372020-04-29 13:44:00 +08003840 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003841 }
hualing chen2932d372020-04-29 13:44:00 +08003842 return DVR_SUCCESS;
3843}
3844
3845
3846/**\brief Get playback status
3847 * \param[in] handle playback handle
3848 * \param[out] p_status playback status
3849 * \retval DVR_SUCCESS On success
3850 * \return Error code
3851 */
3852int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3853 DVR_PlaybackStatus_t *p_status) {
3854//
3855 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3856
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003857 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3858
hualing chen2932d372020-04-29 13:44:00 +08003859 if (player == NULL) {
3860 DVR_PB_DG(1, "player is NULL");
3861 return DVR_FAILURE;
3862 }
hualing chen1679f812021-11-08 15:17:46 +08003863 DVR_PB_DG(1, "lock---");
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003864 pthread_mutex_lock(&player->lock);
3865 if (!player->has_video && !player->has_audio)
3866 p_status->time_cur = 0;
hualing chen1679f812021-11-08 15:17:46 +08003867 DVR_PB_DG(1, "unlock---");
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003868 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003869
hualing chenb31a6c62020-01-13 17:27:00 +08003870 return DVR_SUCCESS;
3871}
3872
hualing chen040df222020-01-17 13:35:02 +08003873void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3874 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003875 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3876 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3877 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3878 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3879 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3880 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3881 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 +08003882 }
hualing chenb31a6c62020-01-13 17:27:00 +08003883}
3884
hualing chen5cbe1a62020-02-10 16:36:36 +08003885int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003886 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003887
hualing chena540a7e2020-03-27 16:44:05 +08003888 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003889 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003890 return DVR_FAILURE;
3891 }
3892
hualing chen040df222020-01-17 13:35:02 +08003893 DVR_PlaybackSegmentInfo_t *segment;
3894 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003895 {
hualing chen040df222020-01-17 13:35:02 +08003896 if (segment_id >= 0) {
3897 if (segment->segment_id == segment_id) {
3898 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003899 break;
3900 }
3901 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003902 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003903 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003904 }
3905 }
3906 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003907}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003908
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003909int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003910{
3911 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3912 DVR_RETURN_IF_FALSE(player);
3913 DVR_RETURN_IF_FALSE(func);
3914
hualing chen4b7c15d2020-04-07 16:13:48 +08003915 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003916 pthread_mutex_lock(&player->lock);
3917
3918 player->dec_func = func;
3919 player->dec_userdata = userdata;
3920
3921 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003922 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003923 return DVR_SUCCESS;
3924}
3925
3926int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3927{
3928 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3929 DVR_RETURN_IF_FALSE(player);
3930 DVR_RETURN_IF_FALSE(p_secure_buf);
3931 DVR_RETURN_IF_FALSE(len);
3932
hualing chen4b7c15d2020-04-07 16:13:48 +08003933 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003934 pthread_mutex_lock(&player->lock);
3935
3936 player->is_secure_mode = 1;
3937 player->secure_buffer = p_secure_buf;
3938 player->secure_buffer_size = len;
3939
3940 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003941 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003942 return DVR_SUCCESS;
3943}