blob: 1cdb90302e948aee515b6db6730721de64c55dc9 [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 chen926a8ec2021-12-20 20:38:24 +0800669retry:
hualing chena540a7e2020-03-27 16:44:05 +0800670 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");
hualing chen926a8ec2021-12-20 20:38:24 +0800693 goto retry;
hualing chen4b7c15d2020-04-07 16:13:48 +0800694 }
hualing chen87072a82020-03-12 16:20:12 +0800695 pthread_mutex_unlock(&player->segment_lock);
696 int total = _dvr_get_end_time( handle);
697 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800698 if (IS_FB(player->speed)) {
699 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800700 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800701 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800702 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800703 }
hualing chen87072a82020-03-12 16:20:12 +0800704 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800705 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800706 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800707 return ret;
708}
709
hualing chen5cbe1a62020-02-10 16:36:36 +0800710//open next segment to play,if reach list end return errro.
711static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
712{
713 DVR_Playback_t *player = (DVR_Playback_t *) handle;
714 Segment_OpenParams_t params;
715 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800716 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800717 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800718 return DVR_FAILURE;
719 }
hualing chencc91e1c2020-02-28 13:26:17 +0800720 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800721 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800722 }
hualing chencc91e1c2020-02-28 13:26:17 +0800723 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800724 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800725 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800726 return DVR_FAILURE;
727 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800728 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800729 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800730
731 DVR_PlaybackSegmentInfo_t *segment;
732
733 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800734
hualing chen5cbe1a62020-02-10 16:36:36 +0800735 list_for_each_entry(segment, &player->segment_list, head)
736 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800737 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 +0800738 if (segment->segment_id == segment_id) {
739 found = 1;
740 }
741 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800742 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 +0800743 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800744 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800745 player->cur_segment_id = segment->segment_id;
746 player->cur_segment.segment_id = segment->segment_id;
747 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800748 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800749 //pids
750 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800751 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 +0800752 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800753 }
754 }
hualing chencc91e1c2020-02-28 13:26:17 +0800755 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800756 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800757 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800758 return DVR_FAILURE;
759 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800760 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800761 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800762 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800763 params.segment_id = (uint64_t)player->cur_segment.segment_id;
764 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800765 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 +0800766 if (player->r_handle != NULL) {
767 segment_close(player->r_handle);
768 player->r_handle = NULL;
769 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800770 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800771 if (ret == DVR_FAILURE) {
772 DVR_PB_DG(1, "segment opne error");
773 }
hualing chen2aba4022020-03-02 13:49:55 +0800774 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800775 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800776
hualing chen4b7c15d2020-04-07 16:13:48 +0800777 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 +0800778 return ret;
779}
780
781
782//get play info by segment id
783static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
784 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800785 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800786 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800787
788 DVR_Playback_t *player = (DVR_Playback_t *) handle;
789 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800790 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800791 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800792 return DVR_FAILURE;
793 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800794
795 int found = 0;
796
797 list_for_each_entry(segment, &player->segment_list, head)
798 {
hualing chen87072a82020-03-12 16:20:12 +0800799 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800800 //get first segment from list
801 found = 1;
802 }
803 if (segment->segment_id == segment_id) {
804 found = 1;
805 }
806 if (found == 1) {
807 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800808 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800809 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800810 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800811 player->cur_segment.segment_id = segment->segment_id;
812 player->cur_segment.flags = segment->flags;
813 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800814 player->cur_segment.pids.video.pid = segment->pids.video.pid;
815 player->cur_segment.pids.video.format = segment->pids.video.format;
816 player->cur_segment.pids.video.type = segment->pids.video.type;
817 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
818 player->cur_segment.pids.audio.format = segment->pids.audio.format;
819 player->cur_segment.pids.audio.type = segment->pids.audio.type;
820 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
821 player->cur_segment.pids.ad.format = segment->pids.ad.format;
822 player->cur_segment.pids.ad.type = segment->pids.ad.type;
823 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800824 //
hualing chen2aba4022020-03-02 13:49:55 +0800825 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800826 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800827 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800828 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800829 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
830 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800831 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 +0800832 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800833 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800834 }
835 }
hualing chencc91e1c2020-02-28 13:26:17 +0800836 if (found != 2) {
837 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800838 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800839 return DVR_FAILURE;
840 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800841
842 return DVR_SUCCESS;
843}
hualing chencc91e1c2020-02-28 13:26:17 +0800844static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
845 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800846 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800847 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800848 return DVR_FAILURE;
849 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800850
hualing chencc91e1c2020-02-28 13:26:17 +0800851 //compare cur segment
852 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
853 {
854 //check video pids, stop or restart
hualing chen99508642021-10-18 15:41:17 +0800855 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800856 //check sub audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800857 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800858 //check audio pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800859 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 1);
hualing chene3797f02021-01-13 14:53:28 +0800860 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 +0800861 //check pcr pids stop or restart
hualing chen99508642021-10-18 15:41:17 +0800862 _do_check_pid_info(handle, player->last_segment.pids, player->cur_segment.pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800863 }
hualing chena540a7e2020-03-27 16:44:05 +0800864 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800865}
hualing chen5cbe1a62020-02-10 16:36:36 +0800866
hualing chend241c7a2021-06-22 13:34:27 +0800867static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
868{
869 DVR_Playback_t *player = (DVR_Playback_t *) handle;
870 if (player == NULL) {
871 DVR_PB_DG(1, "player is NULL");
872 return DVR_TRUE;
873 }
874 char buf[10];
875 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
876 DVR_PB_DG(1, "player get prop[%d][%s]", atoi(buf), buf);
877
878 if (atoi(buf) != 1) {
879 //return DVR_TRUE;
880 }
881
hualing chen7ea70a72021-09-09 11:25:13 +0800882 DVR_PB_DG(1, ":play speed: %f ply dur: %u sys_dur: %u",
hualing chen03fd4942021-07-15 15:56:41 +0800883 player->speed,
884 player->con_spe.ply_dur,
885 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800886
887 if (player->speed != 1.0f)
888 return DVR_TRUE;
889
890 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800891 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800892 return DVR_FALSE;
893
894 return DVR_TRUE;
895}
896
hualing chencc91e1c2020-02-28 13:26:17 +0800897static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
898{
899 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800900 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800901 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800902 return DVR_FAILURE;
903 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800904 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
905 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
906 return DVR_SUCCESS;
907 }
hualing chen03fd4942021-07-15 15:56:41 +0800908 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]",
909 player->cur_segment.flags,
910 player->cur_segment.segment_id,
911 player->last_segment.flags,
912 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800913 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
914 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800915 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800916 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800917 AmTsPlayer_showVideo(player->handle);
918 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800919 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
920 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800921 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800922 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800923 AmTsPlayer_hideVideo(player->handle);
924 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800925 }
926 return DVR_SUCCESS;
927}
hualing chene3797f02021-01-13 14:53:28 +0800928/*
929if decodec sucess first time.
930sucess: return true
931fail: return false
932*/
hualing chena540a7e2020-03-27 16:44:05 +0800933static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
934 DVR_Playback_t *player = (DVR_Playback_t *) handle;
935 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800936 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800937 return DVR_TRUE;
938 }
hualing chene3797f02021-01-13 14:53:28 +0800939 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800940 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800941 } else {
942 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800943 }
944}
hualing chen86e7d482020-01-16 15:13:33 +0800945static void* _dvr_playback_thread(void *arg)
946{
hualing chen040df222020-01-17 13:35:02 +0800947 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800948 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800949 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800950 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800951 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800952
hualing chen39628212020-05-14 10:35:13 +0800953 #define MAX_REACHEND_TIMEOUT (3000)
954 int reach_end_timeout = 0;//ms
955 int cache_time = 0;
hualing chena5f03222021-12-02 11:22:35 +0800956 int timeout = 100;//ms
hualing chen40dd5462021-11-26 19:56:20 +0800957 int check_no_data_time = 4;
hualing chen2aba4022020-03-02 13:49:55 +0800958 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800959 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800960 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800961 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
hualing chen40dd5462021-11-26 19:56:20 +0800962 int first_write = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800963 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800964 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800965 DVR_Bool_t goto_rewrite = DVR_FALSE;
yinming ding0ce94922021-09-08 15:09:15 +0800966 char prop_buf[10];
967
968 memset(prop_buf, 0 ,sizeof(prop_buf));
969 dvr_prop_read("vendor.tv.libdvr.writetm", prop_buf, sizeof(prop_buf));
hualing chena5f03222021-12-02 11:22:35 +0800970 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 +0800971 if (atoi(prop_buf) > 0)
972 write_timeout_ms = atoi(prop_buf);
hualing chen03fd4942021-07-15 15:56:41 +0800973
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800974 if (player->is_secure_mode) {
975 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800976 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800977 return NULL;
978 }
979 }
hualing chen86e7d482020-01-16 15:13:33 +0800980 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800981 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800982 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800983 return NULL;
984 }
hualing chen2aba4022020-03-02 13:49:55 +0800985 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
986 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800987
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800988 dec_bufs.buf_data = malloc(dec_buf_size);
989 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800990 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800991 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800992 return NULL;
993 }
994 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
995 dec_bufs.buf_size = dec_buf_size;
996
hualing chencc91e1c2020-02-28 13:26:17 +0800997 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800998 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
999 }
hualing chen86e7d482020-01-16 15:13:33 +08001000
hualing chen86e7d482020-01-16 15:13:33 +08001001 if (ret != DVR_SUCCESS) {
1002 if (buf != NULL) {
1003 free(buf);
1004 buf = NULL;
1005 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001006 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +08001007 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +08001008 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +08001009 }
hualing chen1679f812021-11-08 15:17:46 +08001010 DVR_PB_DG(1, "--player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001011 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +08001012 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
1013 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
1014 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
1015 player->first_trans_ok = DVR_TRUE;
1016 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
1017 }
hualing chencc91e1c2020-02-28 13:26:17 +08001018 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +08001019 //set video show
1020 AmTsPlayer_showVideo(player->handle);
hualing chen40dd5462021-11-26 19:56:20 +08001021 if (player->vendor == DVR_PLAYBACK_VENDOR_AMAZON)
1022 check_no_data_time = 8;
hualing chen86e7d482020-01-16 15:13:33 +08001023 int trick_stat = 0;
1024 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001025
hualing chen86e7d482020-01-16 15:13:33 +08001026 //check trick stat
hualing chen926a8ec2021-12-20 20:38:24 +08001027 //DVR_PB_DG(1, "lock check_no_data_time:%d", check_no_data_time);
hualing chencc91e1c2020-02-28 13:26:17 +08001028 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001029
hualing chen2aba4022020-03-02 13:49:55 +08001030 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1031 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001032 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001033 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001034 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001035 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001036 {
hualing chen2aba4022020-03-02 13:49:55 +08001037 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1038 if (trick_stat > 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001039 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
1040 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001041 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 +08001042 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001043 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001044 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001045 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
1046 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +08001047 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
1048 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chen03fd4942021-07-15 15:56:41 +08001049 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
1050 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001051 //need change to pause state
1052 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1053 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +08001054 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08001055 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001056 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001057 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001058 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001059 AmTsPlayer_pauseVideoDecoding(player->handle);
1060 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001061 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001062 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001063 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001064 }
1065 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1066 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001067 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001068 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001069 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen7ea70a72021-09-09 11:25:13 +08001070 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001071 player->speed,
1072 player->fffb_current,
1073 _dvr_time_getClock(),
1074 _dvr_playback_state_toString(player->state),
1075 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001076 //used timeout wait need lock first,so we unlock and lock
1077 //pthread_mutex_unlock(&player->lock);
1078 //pthread_mutex_lock(&player->lock);
1079 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001080 DVR_PB_DG(1, "unlock---");
hualing chen2aba4022020-03-02 13:49:55 +08001081 pthread_mutex_unlock(&player->lock);
1082 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001083 } else if (_dvr_time_getClock() < player->next_fffb_time) {
hualing chen7ea70a72021-09-09 11:25:13 +08001084 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 +08001085 player->speed,
1086 player->fffb_current,
1087 _dvr_time_getClock(),
1088 _dvr_playback_state_toString(player->state),
1089 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001090 //used timeout wait need lock first,so we unlock and lock
1091 //pthread_mutex_unlock(&player->lock);
1092 //pthread_mutex_lock(&player->lock);
1093 AmTsPlayer_pauseVideoDecoding(player->handle);
1094 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001095 DVR_PB_DG(1, "unlock---");
hualing chenb5cd42e2020-04-15 17:03:34 +08001096 pthread_mutex_unlock(&player->lock);
1097 continue;
hualing chen2aba4022020-03-02 13:49:55 +08001098 }
hualing chen03fd4942021-07-15 15:56:41 +08001099 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]",
1100 player->speed,
1101 goto_rewrite,
1102 real_read,
1103 _dvr_playback_state_toString(player->state),
1104 player->cmd.cur_cmd);
hualing chen1679f812021-11-08 15:17:46 +08001105 DVR_PB_DG(1, "unlock---");
hualing chen2aba4022020-03-02 13:49:55 +08001106 pthread_mutex_unlock(&player->lock);
1107 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001108 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001109 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1110 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001111 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001112 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08001113 DVR_PB_DG(1, "lock---");
hualing chen2aba4022020-03-02 13:49:55 +08001114 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08001115 } else if(player->state == DVR_PLAYBACK_STATE_PAUSE) {
1116 //on pause state,user seek to new pos,we need pause and wait
1117 //user to resume
1118 DVR_PB_DG(1, "pause, when got first frame event when user seek end");
1119 player->first_frame = 0;
1120 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1121 AmTsPlayer_pauseVideoDecoding(player->handle);
1122 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001123 }
hualing chen1ffd85b2021-08-16 15:18:43 +08001124 } else if (player->fffb_play == DVR_TRUE){
hualing chen4b7c15d2020-04-07 16:13:48 +08001125 //for first into fffb when reset speed
1126 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1127 _dvr_time_getClock() < player->next_fffb_time) {
hualing chen7ea70a72021-09-09 11:25:13 +08001128 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 +08001129 player->speed,
1130 player->fffb_current,
1131 _dvr_time_getClock(),
1132 _dvr_playback_state_toString(player->state),
1133 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001134 //used timeout wait need lock first,so we unlock and lock
1135 //pthread_mutex_unlock(&player->lock);
1136 //pthread_mutex_lock(&player->lock);
1137 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001138 DVR_PB_DG(1, "unlock---");
hualing chen4b7c15d2020-04-07 16:13:48 +08001139 pthread_mutex_unlock(&player->lock);
1140 continue;
1141 }
hualing chen03fd4942021-07-15 15:56:41 +08001142 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
1143 player->speed,
1144 goto_rewrite,
1145 real_read,
1146 _dvr_playback_state_toString(player->state),
1147 player->cmd.cur_cmd,
1148 player->fffb_play);
hualing chen1679f812021-11-08 15:17:46 +08001149 DVR_PB_DG(1, "unlock---");
hualing chen4b7c15d2020-04-07 16:13:48 +08001150 pthread_mutex_unlock(&player->lock);
1151 goto_rewrite = DVR_FALSE;
1152 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001153 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001154 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1155 player->first_frame = 0;
1156 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1157 pthread_mutex_lock(&player->lock);
1158 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001159 }
hualing chenb31a6c62020-01-13 17:27:00 +08001160 }
hualing chen86e7d482020-01-16 15:13:33 +08001161
hualing chen30423862021-04-16 14:39:12 +08001162 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1163 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001164 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001165 DVR_PB_DG(1, "pause, continue");
hualing chen1679f812021-11-08 15:17:46 +08001166 DVR_PB_DG(1, "unlock---");
1167 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001168 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen1679f812021-11-08 15:17:46 +08001169 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08001170 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chen1679f812021-11-08 15:17:46 +08001171 DVR_PB_DG(1, "unlock---");
hualing chen87072a82020-03-12 16:20:12 +08001172 pthread_mutex_unlock(&player->lock);
1173 continue;
1174 }
hualing chen266b9502020-04-04 17:39:39 +08001175 //when seek action is done. we need drop write timeout data.
1176 if (player->drop_ts == DVR_TRUE) {
1177 goto_rewrite = DVR_FALSE;
1178 real_read = 0;
1179 player->drop_ts = DVR_FALSE;
1180 }
hualing chen2aba4022020-03-02 13:49:55 +08001181 if (goto_rewrite == DVR_TRUE) {
1182 goto_rewrite = DVR_FALSE;
hualing chen926a8ec2021-12-20 20:38:24 +08001183 //DVR_PB_DG(1, "unlock---");
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 chen926a8ec2021-12-20 20:38:24 +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;
hualing chen926a8ec2021-12-20 20:38:24 +08003083 DVR_PB_DG(1, "change segment [%lld][%lld]",
3084 player->last_segment_id, player->cur_segment_id);
hualing chena5f03222021-12-02 11:22:35 +08003085 } else {
3086 cur = segment_tell_position_time(player->r_handle, pos);
3087 }
hualing chen21a40372021-10-29 11:07:26 +08003088 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen969fe7b2021-05-26 15:13:17 +08003089 pthread_mutex_unlock(&player->segment_lock);
hualing chen926a8ec2021-12-20 20:38:24 +08003090 DVR_PB_DG(1, "***get play cur time [%lld] cache:%lld cur id [%lld]\
3091 last id [%lld] pb cache len [%d] pos [%lld][%lld]",
3092 cur,
3093 cache,
3094 player->cur_segment_id,
3095 player->last_send_time_id,
3096 player->ts_cache_len,
3097 pos,
3098 tmp_pos);
hualing chen969fe7b2021-05-26 15:13:17 +08003099 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3100 cache = 0;
3101 }
3102 if (cur > cache) {
3103 cur_time = (int)(cur - cache);
3104 *id = player->cur_segment_id;
3105 } else if (player->last_segment_tatol > 0) {
hualing chen8a657f32021-08-30 13:12:49 +08003106
3107 if (player->last_segment_tatol > (cache - cur))
3108 cur_time = (int)(player->last_segment_tatol - (cache - cur));
3109 else
3110 cur_time = (int)(player->last_segment_tatol - cur);
3111
hualing chen969fe7b2021-05-26 15:13:17 +08003112 *id = player->last_segment_id;
3113 DVR_PB_DG(1, "get play cur time[%lld][%lld][%d]", player->last_segment_id, player->cur_segment_id, player->last_segment_tatol);
3114 } else {
3115 cur_time = 0;
3116 *id = player->cur_segment_id;
3117 }
3118
3119 return cur_time;
3120}
3121
hualing chencc91e1c2020-02-28 13:26:17 +08003122//get current segment current pcr time of read pos
3123static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
3124 //get cur time of segment
3125 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003126
3127 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003128 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003129 return DVR_FAILURE;
3130 }
3131
hualing chen2aba4022020-03-02 13:49:55 +08003132 pthread_mutex_lock(&player->segment_lock);
3133 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003134 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08003135 pthread_mutex_unlock(&player->segment_lock);
3136 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08003137}
3138
hualing chen03fd4942021-07-15 15:56:41 +08003139DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
3140{
3141 //check is set limit info
3142 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3143
3144 if (player == NULL) {
3145 DVR_PB_DG(1, "player is NULL");
3146 return DVR_FALSE;
3147 }
3148 if (player->rec_start > 0 || player->limit > 0) {
3149 return DVR_TRUE;
3150 }
3151 return DVR_FALSE;
3152}
3153
3154/**\brief set DVR playback calculate expired time len
3155 * \param[in] handle, DVR playback session handle
3156 * \return DVR_SUCCESS on success
3157 * \return error code on failure
3158 */
hualing chen7ea70a72021-09-09 11:25:13 +08003159uint32_t dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
hualing chen03fd4942021-07-15 15:56:41 +08003160{
3161 //calculate expired time to play
3162 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08003163 uint32_t cur_time;
3164 uint32_t tmp_time;
3165 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003166 if (player == NULL) {
3167 DVR_PB_DG(1, "player is NULL");
3168 return expired;
3169 }
hualing chen7ea70a72021-09-09 11:25:13 +08003170 if (player->rec_start == 0 || player->limit == 0) {
3171 DVR_PB_DG(1, "rec limit 0");
hualing chen03fd4942021-07-15 15:56:41 +08003172 return expired;
3173 }
3174 //get system time
hualing chen7ea70a72021-09-09 11:25:13 +08003175 cur_time = _dvr_getClock_sec();
3176 if ((cur_time - player->rec_start) > player->limit) {
3177 tmp_time = (uint32_t)((cur_time - player->rec_start) - player->limit) * 1000U;
3178 expired = *(int*)&tmp_time;
3179 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 +08003180 cur_time,
3181 player->rec_start,
3182 player->limit,
hualing chen7ea70a72021-09-09 11:25:13 +08003183 (uint32_t)(cur_time - player->rec_start - player->limit), expired, tmp_time);
3184 }
hualing chen03fd4942021-07-15 15:56:41 +08003185 return expired;
3186}
3187
3188/**\brief set DVR playback obsolete time
3189 * \param[in] handle, DVR playback session handle
3190 * \param[in] obsolete, obsolete len
3191 * \return DVR_SUCCESS on success
3192 * \return error code on failure
3193 */
3194int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
3195{
3196 int expired = 0;
3197 //calculate expired time to play
3198 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3199
3200 if (player == NULL) {
3201 DVR_PB_DG(1, "player is NULL");
3202 return DVR_FALSE;
3203 }
3204 //get system time
hualing chen1679f812021-11-08 15:17:46 +08003205 DVR_PB_DG(1, "lock ---\r\n");
hualing chen03fd4942021-07-15 15:56:41 +08003206 pthread_mutex_lock(&player->lock);
3207 player->obsolete = obsolete;
hualing chen1679f812021-11-08 15:17:46 +08003208 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen03fd4942021-07-15 15:56:41 +08003209 pthread_mutex_unlock(&player->lock);
3210 return expired;
3211}
3212
3213/**\brief update DVR playback newest segment duration
3214 * \param[in] handle, DVR playback session handle
3215 * \param[in] segmentid, newest segment id
3216 * \param[in] dur dur time ms
3217 * \return DVR_SUCCESS on success
3218 * \return error code on failure
3219 */
3220int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
3221uint64_t segmentid, int dur)
3222{
3223 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3224 DVR_PlaybackSegmentInfo_t *segment;
3225 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
3226
3227 if (player == NULL) {
3228 DVR_PB_DG(1, " player is NULL");
3229 return DVR_FAILURE;
3230 }
3231 //update the newest segment duration on timeshift mode
3232 list_for_each_entry(segment, &player->segment_list, head)
3233 {
3234 if (segment->segment_id == segmentid) {
3235 segment->duration = dur;
3236 break;
3237 }
3238 pre_segment = segment;
3239 }
3240
3241 return DVR_SUCCESS;
3242}
3243
hualing chen7ea70a72021-09-09 11:25:13 +08003244static uint32_t dvr_playback_calculate_last_valid_segment(
3245 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos)
hualing chen03fd4942021-07-15 15:56:41 +08003246{
hualing chen7ea70a72021-09-09 11:25:13 +08003247 uint32_t off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003248 uint64_t segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003249 uint32_t pre_off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003250 uint64_t last_segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003251 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003252 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3253
3254 if (player == NULL) {
3255 DVR_PB_DG(1, "player is NULL");
3256 return DVR_FAILURE;
3257 }
3258 expired = dvr_playback_calculate_expiredlen(handle);
hualing chen7e14e532021-09-23 11:23:28 +08003259 if (expired == 0) {
3260 *segmentid = player->cur_segment_id;
3261 *pos = 0;
3262 return DVR_SUCCESS;
3263 }
hualing chen03fd4942021-07-15 15:56:41 +08003264 DVR_PlaybackSegmentInfo_t *pseg;
3265 list_for_each_entry_reverse(pseg, &player->segment_list, head) {
3266 segment_id = pseg->segment_id;
3267
3268 if ((player->obsolete + pre_off + pseg->duration) > expired)
3269 break;
3270
3271 last_segment_id = pseg->segment_id;
3272 pre_off += pseg->duration;
3273 }
3274
3275 if (last_segment_id == segment_id) {
3276 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
3277 off = expired;
3278 } else if (player->obsolete >= expired) {
3279 off = 0;
3280 } else {
3281 off = expired - pre_off - player->obsolete;
3282 }
3283 *segmentid = segment_id;
3284 *pos = off;
3285 return DVR_SUCCESS;
3286}
3287
hualing chen4b7c15d2020-04-07 16:13:48 +08003288#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003289//start replay
3290static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3291
3292 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3293 //calculate pcr seek time
3294 int t_diff = 0;
3295 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003296 uint64_t segmentid = 0;
3297 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003298 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003299 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003300 return DVR_FAILURE;
3301 }
3302
hualing chen5cbe1a62020-02-10 16:36:36 +08003303 if (player->fffb_start == -1) {
3304 //set fffb start time ms
3305 player->fffb_start = _dvr_time_getClock();
3306 player->fffb_current = player->fffb_start;
3307 //get segment current time pos
3308 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08003309 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
3310 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003311 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003312 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003313 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003314 } else {
3315 player->fffb_current = _dvr_time_getClock();
3316 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003317 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003318 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003319 if (seek_time <= 0) {
3320 //need seek to pre one segment
3321 seek_time = 0;
3322 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003323 //seek segment pos
3324 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003325 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003326 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003327 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3328 //set seek time to 0;
hualing chen03fd4942021-07-15 15:56:41 +08003329 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]",
3330 seek_time,
3331 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003332 seek_time = 0;
3333 }
hualing chen03fd4942021-07-15 15:56:41 +08003334 if (IS_FB(player->speed)
3335 && dvr_playback_check_limit(handle)) {
3336 //fb case.check expired time
3337 //get id and pos to check if we can seek to this pos
3338 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3339 //case cur id < segment id
3340 if (player->cur_segment_id < segmentid) {
3341 //expired ts data is player,return error
3342 //
3343 pthread_mutex_unlock(&player->segment_lock);
3344 return 0;
3345 } else if (player->cur_segment_id == segmentid) {
3346 //id is same,compare seek pos
3347 if (seek_time < pos) {
3348 //expired ts data is player,return error
3349 //
3350 pthread_mutex_unlock(&player->segment_lock);
3351 return 0;
3352 }
3353 }
3354 //case can play
3355 }
hualing chen041c4092020-04-05 15:11:50 +08003356 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3357 seek_time = 0;
3358 }
hualing chen2aba4022020-03-02 13:49:55 +08003359 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003360 } else {
3361 //
hualing chen4b7c15d2020-04-07 16:13:48 +08003362 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003363 }
hualing chen03fd4942021-07-15 15:56:41 +08003364 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
3365 seek_time,
3366 player->speed,
3367 player->cur_segment_id,
3368 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003369 }
hualing chen2aba4022020-03-02 13:49:55 +08003370 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003371}
3372
3373
3374//start replay
3375static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3376 //
3377 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003378
3379 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003380 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003381 return DVR_FAILURE;
3382 }
3383
hualing chen5cbe1a62020-02-10 16:36:36 +08003384 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003385 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003386 DVR_PB_DG(1, "fffb stop video");
hualing chen21a40372021-10-29 11:07:26 +08003387 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003388 AmTsPlayer_stopVideoDecoding(player->handle);
3389 }
3390 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003391 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003392 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003393 AmTsPlayer_stopAudioDecoding(player->handle);
3394 }
hualing chendf118dd2020-05-21 15:49:11 +08003395 if (player->has_ad_audio) {
3396 DVR_PB_DG(1, "fffb stop audio");
3397 player->has_ad_audio =DVR_FALSE;
3398 AmTsPlayer_disableADMix(player->handle);
3399 }
hualing chen2aba4022020-03-02 13:49:55 +08003400
hualing chen5cbe1a62020-02-10 16:36:36 +08003401 //start video and audio
3402
hualing chen2aba4022020-03-02 13:49:55 +08003403 am_tsplayer_video_params vparams;
3404 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003405 am_tsplayer_audio_params adparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003406
3407 memset(&vparams, 0, sizeof(vparams));
3408 memset(&aparams, 0, sizeof(aparams));
3409
hualing chen87072a82020-03-12 16:20:12 +08003410 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003411
3412 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08003413 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08003414 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003415 //start audio and video
3416 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
3417 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003418 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003419 return -1;
3420 }
3421
3422 if (VALID_PID(vparams.pid)) {
3423 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08003424 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08003425 //DVR_PB_DG(1, "fffb start video and save last frame");
3426 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003427 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003428 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
3429 AmTsPlayer_setVideoParams(player->handle, &vparams);
hualing chen21a40372021-10-29 11:07:26 +08003430 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003431 AmTsPlayer_startVideoDecoding(player->handle);
3432 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003433 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003434 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003435 }
hualing chen31140872020-03-25 12:29:26 +08003436 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08003437 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003438 AmTsPlayer_stopFast(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003439 return 0;
3440}
3441
3442static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3443 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003444 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003445 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003446 return DVR_FAILURE;
3447 }
3448
3449 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08003450 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003451 pthread_mutex_lock(&player->lock);
3452
hualing chen2aba4022020-03-02 13:49:55 +08003453 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003454 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 +08003455
hualing chen87072a82020-03-12 16:20:12 +08003456 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3457 //seek time set 0
3458 seek_time = 0;
3459 }
hualing chen041c4092020-04-05 15:11:50 +08003460 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003461 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3462 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003463 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003464 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003465 DVR_PB_DG(1, "unlock");
hualing chen87072a82020-03-12 16:20:12 +08003466 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003467 //send event here and pause
3468 DVR_Play_Notify_t notify;
3469 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003470 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003471 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003472 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08003473 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 +08003474 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003475 return DVR_SUCCESS;
3476 }
hualing chen2932d372020-04-29 13:44:00 +08003477 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003478 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003479 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003480 }
3481 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003482 _dvr_playback_fffb_replay(handle);
3483
3484 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003485 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003486
hualing chen5cbe1a62020-02-10 16:36:36 +08003487 return DVR_SUCCESS;
3488}
3489
hualing chen87072a82020-03-12 16:20:12 +08003490//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003491static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003492 //
3493 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003494
3495 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003496 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003497 return DVR_FAILURE;
3498 }
3499
hualing chen5cbe1a62020-02-10 16:36:36 +08003500 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003501 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003502 player->has_video = DVR_FALSE;
hualing chen21a40372021-10-29 11:07:26 +08003503 AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen2aba4022020-03-02 13:49:55 +08003504 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003505 }
3506
3507 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003508 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003509 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003510 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003511 //start video and audio
3512
hualing chen2aba4022020-03-02 13:49:55 +08003513 am_tsplayer_video_params vparams;
3514 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003515 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08003516 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003517
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003518 memset(&vparams, 0, sizeof(vparams));
3519 memset(&aparams, 0, sizeof(aparams));
3520
hualing chen5cbe1a62020-02-10 16:36:36 +08003521 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08003522 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08003523 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003524 //start audio and video
3525 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003526 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003527 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003528 return -1;
3529 }
3530
3531 if (VALID_PID(vparams.pid)) {
3532 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003533 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003534 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003535 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003536 }
hualing chen266b9502020-04-04 17:39:39 +08003537 else {
hualing chen2aba4022020-03-02 13:49:55 +08003538 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003539 }
hualing chen2aba4022020-03-02 13:49:55 +08003540 AmTsPlayer_setVideoParams(player->handle, &vparams);
hualing chen21a40372021-10-29 11:07:26 +08003541 AmTsPlayer_setVideoBlackOut(player->handle, 1);
hualing chen2aba4022020-03-02 13:49:55 +08003542 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003543 }
hualing chena540a7e2020-03-27 16:44:05 +08003544
3545 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08003546 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08003547 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003548 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003549 } else {
hualing chendf118dd2020-05-21 15:49:11 +08003550 if (VALID_PID(adparams.pid)) {
3551 player->has_ad_audio = DVR_TRUE;
3552 DVR_PB_DG(1, "start ad audio");
3553 AmTsPlayer_setADParams(player->handle, &adparams);
3554 AmTsPlayer_enableADMix(player->handle);
3555 }
hualing chen969fe7b2021-05-26 15:13:17 +08003556 if (VALID_PID(aparams.pid)) {
3557 player->has_audio = DVR_TRUE;
3558 DVR_PB_DG(1, "start audio");
3559 AmTsPlayer_setAudioParams(player->handle, &aparams);
3560 AmTsPlayer_startAudioDecoding(player->handle);
3561 }
hualing chendf118dd2020-05-21 15:49:11 +08003562
hualing chen7a56cba2020-04-14 14:09:27 +08003563 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003564 AmTsPlayer_stopFast(player->handle);
3565 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3566 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3567 }
hualing chen2aba4022020-03-02 13:49:55 +08003568 player->cmd.last_cmd = player->cmd.cur_cmd;
3569 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003570 player->cmd.state = DVR_PLAYBACK_STATE_START;
3571 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08003572 return 0;
3573}
3574
3575
hualing chenb31a6c62020-01-13 17:27:00 +08003576/**\brief Set play speed
3577 * \param[in] handle playback handle
3578 * \param[in] speed playback speed
3579 * \retval DVR_SUCCESS On success
3580 * \return Error code
3581 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003582int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3583
3584 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003585
3586 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003587 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003588 return DVR_FAILURE;
3589 }
3590
hualing chena540a7e2020-03-27 16:44:05 +08003591 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003592 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003593 return DVR_FAILURE;
3594 }
hualing chenf00cdc82020-06-10 14:23:35 +08003595 if (speed.speed.speed == player->cmd.speed.speed.speed) {
3596 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
3597 return DVR_SUCCESS;
3598 }
hualing chen1679f812021-11-08 15:17:46 +08003599 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
3600
hualing chen5cbe1a62020-02-10 16:36:36 +08003601 pthread_mutex_lock(&player->lock);
3602 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3603 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3604 player->cmd.last_cmd = player->cmd.cur_cmd;
3605 }
hualing chene41f4372020-06-06 16:29:17 +08003606
hualing chen31140872020-03-25 12:29:26 +08003607 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003608 IS_KERNEL_SPEED(speed.speed.speed) ) {
3609 //case 1. not start play.only set speed
3610 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3611 //only set speed.and return;
3612 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3613 player->cmd.speed.speed = speed.speed;
3614 player->speed = (float)speed.speed.speed/(float)100;
3615 player->fffb_play = DVR_FALSE;
3616 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003617 DVR_PB_DG(1, "unlock");
hualing chenf00cdc82020-06-10 14:23:35 +08003618 return DVR_SUCCESS;
3619 }
3620 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003621 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3622 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003623 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003624 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3625 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003626 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003627 AmTsPlayer_stopFast(player->handle);
3628 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003629 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003630 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
hualing chen1679f812021-11-08 15:17:46 +08003631 DVR_PB_DG(1, "lock ---\r\n");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003632 pthread_mutex_lock(&player->lock);
3633 } else {
3634 //set play speed and if audio is start, stop audio.
3635 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003636 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003637 AmTsPlayer_stopAudioDecoding(player->handle);
3638 player->has_audio = DVR_FALSE;
3639 }
hualing chenb96aa2c2020-04-15 14:13:53 +08003640 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003641 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003642 }
hualing chenbcada022020-04-22 14:27:01 +08003643 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003644 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003645 player->cmd.speed.speed = speed.speed;
3646 player->speed = (float)speed.speed.speed/(float)100;
hualing chen1679f812021-11-08 15:17:46 +08003647 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003648 pthread_mutex_unlock(&player->lock);
3649 return DVR_SUCCESS;
3650 }
hualing chen31140872020-03-25 12:29:26 +08003651 //case 3 fffb mode
3652 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3653 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3654 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003655 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003656 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003657 player->cmd.speed.speed = speed.speed;
3658 player->speed = (float)speed.speed.speed/(float)100;
3659 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003660 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08003661 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003662 pthread_mutex_unlock(&player->lock);
3663 return DVR_SUCCESS;
3664 }
3665 }
3666 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003667 IS_KERNEL_SPEED(speed.speed.speed)) {
3668 //case 1. cur speed is kernel support speed,set kernel speed.
3669 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003670 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003671 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3672 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003673 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003674 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08003675 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003676 } else {
3677 //set play speed and if audio is start, stop audio.
3678 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003679 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003680 AmTsPlayer_stopAudioDecoding(player->handle);
3681 player->has_audio = DVR_FALSE;
3682 }
hualing chenf00cdc82020-06-10 14:23:35 +08003683 DVR_PB_DG(1, "start fast");
3684 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003685 }
hualing chena540a7e2020-03-27 16:44:05 +08003686 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003687 player->cmd.speed.speed = speed.speed;
3688 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003689 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08003690 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003691 pthread_mutex_unlock(&player->lock);
3692 return DVR_SUCCESS;
3693 }
3694 //case 2 fffb mode
3695 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3696 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3697 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003698 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003699 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003700 player->cmd.speed.speed = speed.speed;
3701 player->speed = (float)speed.speed.speed/(float)100;
3702 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08003703 player->fffb_play = DVR_FALSE;
hualing chen1679f812021-11-08 15:17:46 +08003704 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen31140872020-03-25 12:29:26 +08003705 pthread_mutex_unlock(&player->lock);
3706 return DVR_SUCCESS;
3707 }
hualing chen31140872020-03-25 12:29:26 +08003708 }
hualing chena540a7e2020-03-27 16:44:05 +08003709 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3710 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003711 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003712 } else {
hualing chen31140872020-03-25 12:29:26 +08003713 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003714 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3715 else
3716 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003717 player->fffb_play = DVR_TRUE;
3718 }
3719 DVR_Bool_t init_last_time = DVR_FALSE;
3720 if (player->speed > 0.0f && speed.speed.speed < 0) {
3721 init_last_time = DVR_TRUE;
3722 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3723 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003724 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003725 player->cmd.speed.mode = speed.mode;
3726 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003727 player->speed = (float)speed.speed.speed/(float)100;
3728 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003729 _dvr_init_fffb_t(handle);
3730 if (init_last_time == DVR_TRUE)
3731 player->last_send_time_id = UINT64_MAX;
3732
hualing chen87072a82020-03-12 16:20:12 +08003733 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003734 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3735 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003736 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003737 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003738 _dvr_playback_replay(handle, DVR_FALSE);
3739 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3740 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3741 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003742 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003743 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003744 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003745 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003746 return DVR_SUCCESS;
3747}
hualing chen2932d372020-04-29 13:44:00 +08003748
hualing chenb31a6c62020-01-13 17:27:00 +08003749/**\brief Get playback status
3750 * \param[in] handle playback handle
3751 * \param[out] p_status playback status
3752 * \retval DVR_SUCCESS On success
3753 * \return Error code
3754 */
hualing chen2932d372020-04-29 13:44:00 +08003755static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3756 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003757//
3758 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003759 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003760 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003761 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003762 return DVR_FAILURE;
3763 }
hualing chen1679f812021-11-08 15:17:46 +08003764 if (is_lock ==DVR_TRUE) {
3765 DVR_PB_DG(1, "lock");
hualing chen2932d372020-04-29 13:44:00 +08003766 pthread_mutex_lock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003767 }
3768
hualing chen5cbe1a62020-02-10 16:36:36 +08003769 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003770 //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 +08003771 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3772 player->state == DVR_PLAYBACK_STATE_START) {
3773 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3774 }
hualing chen041c4092020-04-05 15:11:50 +08003775
hualing chencc91e1c2020-02-28 13:26:17 +08003776 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003777 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003778
3779 if (CONTROL_SPEED_ENABLE == 1) {
hualing chen7ea70a72021-09-09 11:25:13 +08003780 if (player->con_spe.ply_sta == 0) {
3781 DVR_PB_DG(1, "player dur[%u] sta[%u] cur[%d] -----reinit",
hualing chen03fd4942021-07-15 15:56:41 +08003782 player->con_spe.ply_dur,
3783 player->con_spe.ply_sta,
3784 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003785 player->con_spe.ply_sta = p_status->time_cur;
3786 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3787 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
hualing chen7ea70a72021-09-09 11:25:13 +08003788 DVR_PB_DG(1, "player dur[%u] sta[%u] cur[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003789 player->con_spe.ply_dur,
3790 player->con_spe.ply_sta,
3791 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003792 player->con_spe.ply_sta = p_status->time_cur;
3793 }
3794
3795 if (player->con_spe.sys_sta == 0) {
3796 player->con_spe.sys_sta = _dvr_time_getClock();
3797 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
3798 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
3799 player->con_spe.sys_sta = _dvr_time_getClock();
3800 }
3801 }
3802
hualing chen4b7c15d2020-04-07 16:13:48 +08003803 if (player->last_send_time_id == UINT64_MAX) {
3804 player->last_send_time_id = player->cur_segment_id;
3805 player->last_cur_time = p_status->time_cur;
3806 }
3807 if (player->last_send_time_id == player->cur_segment_id) {
3808 if (player->speed > 0.0f ) {
3809 //ff
3810 if (p_status->time_cur < player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003811 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]",
3812 player->last_cur_time,
3813 p_status->time_cur,
3814 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08003815 p_status->time_cur = player->last_cur_time;
3816 } else {
3817 player->last_cur_time = p_status->time_cur;
3818 }
hualing chene41f4372020-06-06 16:29:17 +08003819 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003820 //fb
3821 if (p_status->time_cur > player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003822 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]",
3823 player->last_cur_time,
3824 p_status->time_cur,
3825 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08003826 p_status->time_cur = player->last_cur_time;
3827 } else {
3828 player->last_cur_time = p_status->time_cur;
3829 }
3830 }
hualing chend241c7a2021-06-22 13:34:27 +08003831 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08003832 player->last_cur_time = p_status->time_cur;
3833 }
hualing chen969fe7b2021-05-26 15:13:17 +08003834 player->last_send_time_id = segment_id;
3835 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003836
hualing chen5cbe1a62020-02-10 16:36:36 +08003837 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003838 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003839 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003840 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 +08003841 _dvr_playback_state_toString(player->state),
3842 _dvr_playback_state_toString(p_status->state),
3843 p_status->time_cur, p_status->time_end,
3844 p_status->segment_id,player->play_flag,
3845 player->speed,
3846 is_lock);
hualing chen1679f812021-11-08 15:17:46 +08003847 if (is_lock ==DVR_TRUE) {
3848 DVR_PB_DG(1, "unlock ---\r\n");
hualing chen2932d372020-04-29 13:44:00 +08003849 pthread_mutex_unlock(&player->lock);
hualing chen1679f812021-11-08 15:17:46 +08003850 }
hualing chen2932d372020-04-29 13:44:00 +08003851 return DVR_SUCCESS;
3852}
3853
3854
3855/**\brief Get playback status
3856 * \param[in] handle playback handle
3857 * \param[out] p_status playback status
3858 * \retval DVR_SUCCESS On success
3859 * \return Error code
3860 */
3861int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3862 DVR_PlaybackStatus_t *p_status) {
3863//
3864 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3865
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003866 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3867
hualing chen2932d372020-04-29 13:44:00 +08003868 if (player == NULL) {
3869 DVR_PB_DG(1, "player is NULL");
3870 return DVR_FAILURE;
3871 }
hualing chen1679f812021-11-08 15:17:46 +08003872 DVR_PB_DG(1, "lock---");
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003873 pthread_mutex_lock(&player->lock);
3874 if (!player->has_video && !player->has_audio)
3875 p_status->time_cur = 0;
hualing chen1679f812021-11-08 15:17:46 +08003876 DVR_PB_DG(1, "unlock---");
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003877 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003878
hualing chenb31a6c62020-01-13 17:27:00 +08003879 return DVR_SUCCESS;
3880}
3881
hualing chen040df222020-01-17 13:35:02 +08003882void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3883 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003884 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3885 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3886 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3887 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3888 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3889 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3890 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 +08003891 }
hualing chenb31a6c62020-01-13 17:27:00 +08003892}
3893
hualing chen5cbe1a62020-02-10 16:36:36 +08003894int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003895 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003896
hualing chena540a7e2020-03-27 16:44:05 +08003897 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003898 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003899 return DVR_FAILURE;
3900 }
3901
hualing chen040df222020-01-17 13:35:02 +08003902 DVR_PlaybackSegmentInfo_t *segment;
3903 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003904 {
hualing chen040df222020-01-17 13:35:02 +08003905 if (segment_id >= 0) {
3906 if (segment->segment_id == segment_id) {
3907 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003908 break;
3909 }
3910 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003911 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003912 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003913 }
3914 }
3915 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003916}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003917
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003918int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003919{
3920 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3921 DVR_RETURN_IF_FALSE(player);
3922 DVR_RETURN_IF_FALSE(func);
3923
hualing chen4b7c15d2020-04-07 16:13:48 +08003924 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003925 pthread_mutex_lock(&player->lock);
3926
3927 player->dec_func = func;
3928 player->dec_userdata = userdata;
3929
3930 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003931 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003932 return DVR_SUCCESS;
3933}
3934
3935int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3936{
3937 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3938 DVR_RETURN_IF_FALSE(player);
3939 DVR_RETURN_IF_FALSE(p_secure_buf);
3940 DVR_RETURN_IF_FALSE(len);
3941
hualing chen4b7c15d2020-04-07 16:13:48 +08003942 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003943 pthread_mutex_lock(&player->lock);
3944
3945 player->is_secure_mode = 1;
3946 player->secure_buffer = p_secure_buf;
3947 player->secure_buffer_size = len;
3948
3949 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003950 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003951 return DVR_SUCCESS;
3952}