blob: 300923d54105ed27e5e2772b3d7d175fd3f5cf92 [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001#include <stdio.h>
2#include <stdlib.h>
3
hualing chen5cbe1a62020-02-10 16:36:36 +08004#include <string.h>
hualing chenb31a6c62020-01-13 17:27:00 +08005#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/ioctl.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <poll.h>
11#include <errno.h>
12#include <signal.h>
13#include <pthread.h>
hualing chenb5cd42e2020-04-15 17:03:34 +080014#include <errno.h>
hualing chen03fd4942021-07-15 15:56:41 +080015#include "dvr_utils.h"
16#include "dvr_types.h"
hualing chenb31a6c62020-01-13 17:27:00 +080017#include "dvr_playback.h"
18
Gong Ke2a0ebbe2021-05-25 15:22:50 +080019#define DVR_PB_DG(_level, _fmt...) \
20 DVR_DEBUG_FL(_level, "playback", _fmt)
hualing chena540a7e2020-03-27 16:44:05 +080021
hualing chenb31a6c62020-01-13 17:27:00 +080022#define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff)
hualing chena540a7e2020-03-27 16:44:05 +080023
hualing chend241c7a2021-06-22 13:34:27 +080024#define CONTROL_SPEED_ENABLE 0
hualing chena540a7e2020-03-27 16:44:05 +080025
26#define FF_SPEED (2.0f)
27#define FB_SPEED (-1.0f)
28#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
hualing chene41f4372020-06-06 16:29:17 +080029#define IS_FB(_SPEED_) ((_SPEED_) <= FB_SPEED)
hualing chena540a7e2020-03-27 16:44:05 +080030
31#define IS_KERNEL_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_X1) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
32#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
33
hualing chenb31a6c62020-01-13 17:27:00 +080034
hualing chenb5cd42e2020-04-15 17:03:34 +080035#define FFFB_SLEEP_TIME (1000)//500ms
hualing chene41f4372020-06-06 16:29:17 +080036#define FB_DEFAULT_LEFT_TIME (3000)
hualing chen31140872020-03-25 12:29:26 +080037//if tsplayer delay time < 200 and no data can read, we will pause
38#define MIN_TSPLAYER_DELAY_TIME (200)
39
hualing chen041c4092020-04-05 15:11:50 +080040#define MAX_CACHE_TIME (30000)
41
hualing chena540a7e2020-03-27 16:44:05 +080042static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080043//
44static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chen275379e2021-06-15 17:57:21 +080045static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_PlaybackPids_t pids, int type);
hualing chencc91e1c2020-02-28 13:26:17 +080046static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
47static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080048static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080049static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080050static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
51 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chene41f4372020-06-06 16:29:17 +080052static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock);
hualing chen7ea70a72021-09-09 11:25:13 +080053static uint32_t dvr_playback_calculate_last_valid_segment(
54 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos);
hualing chen87072a82020-03-12 16:20:12 +080055
hualing chenbcada022020-04-22 14:27:01 +080056
57static char* _cmd_toString(int cmd)
58{
59
60 char *string[DVR_PLAYBACK_CMD_NONE+1]={
61 "start",
62 "stop",
63 "vstart",
64 "astart",
65 "vstop",
66 "astop",
67 "vrestart",
68 "arestart",
69 "avrestart",
70 "vstopastart",
71 "astopvstart",
72 "vstoparestart",
73 "astopvrestart",
74 "vstartarestart",
75 "astartvrestart",
76 "pause",
77 "resume",
78 "seek",
79 "ff",
80 "fb",
81 "NONE"
82 };
83
84 if (cmd > DVR_PLAYBACK_CMD_NONE) {
85 return "unkown";
86 } else {
87 return string[cmd];
88 }
89}
90
91
hualing chen6d24aa92020-03-23 18:43:47 +080092static char* _dvr_playback_state_toString(int stat)
93{
94 char *string[DVR_PLAYBACK_STATE_FB+1]={
95 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080096 "stop",
hualing chen31140872020-03-25 12:29:26 +080097 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +080098 "ff",
99 "fb"
100 };
101
102 if (stat > DVR_PLAYBACK_STATE_FB) {
103 return "unkown";
104 } else {
105 return string[stat];
106 }
107}
hualing chena540a7e2020-03-27 16:44:05 +0800108
109static DVR_Bool_t _dvr_support_speed(int speed) {
110
111 DVR_Bool_t ret = DVR_FALSE;
112
113 switch (speed) {
hualing chene41f4372020-06-06 16:29:17 +0800114 case PLAYBACK_SPEED_FBX1:
hualing chena540a7e2020-03-27 16:44:05 +0800115 case PLAYBACK_SPEED_FBX2:
116 case PLAYBACK_SPEED_FBX4:
117 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800118 case PLAYBACK_SPEED_FBX16:
119 case PLAYBACK_SPEED_FBX12:
120 case PLAYBACK_SPEED_FBX32:
121 case PLAYBACK_SPEED_FBX48:
122 case PLAYBACK_SPEED_FBX64:
123 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800124 case PLAYBACK_SPEED_S2:
125 case PLAYBACK_SPEED_S4:
126 case PLAYBACK_SPEED_S8:
127 case PLAYBACK_SPEED_X1:
128 case PLAYBACK_SPEED_X2:
129 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800130 case PLAYBACK_SPEED_X3:
131 case PLAYBACK_SPEED_X5:
132 case PLAYBACK_SPEED_X6:
133 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800134 case PLAYBACK_SPEED_X8:
135 case PLAYBACK_SPEED_X12:
136 case PLAYBACK_SPEED_X16:
137 case PLAYBACK_SPEED_X32:
138 case PLAYBACK_SPEED_X48:
139 case PLAYBACK_SPEED_X64:
140 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800141 ret = DVR_TRUE;
142 break;
143 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800144 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800145 break;
146 }
147 return ret;
148}
hualing chen6e4bfa52020-03-13 14:37:11 +0800149void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
150{
hualing chen4b7c15d2020-04-07 16:13:48 +0800151 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800152 DVR_Playback_t *player = NULL;
153 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800154 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800155 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800156 }
157 switch (event->type) {
158 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
159 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800160 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800161 event->event.video_format.frame_width,
162 event->event.video_format.frame_height,
163 event->event.video_format.frame_rate);
164 break;
165 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800166 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
167 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800168 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800169 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800170 break;
171 }
172 default:
173 break;
174 }
175}
hualing chen2aba4022020-03-02 13:49:55 +0800176void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
177{
hualing chen6e4bfa52020-03-13 14:37:11 +0800178 DVR_Playback_t *player = NULL;
179 if (user_data != NULL) {
180 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800181 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800182 }
hualing chen2aba4022020-03-02 13:49:55 +0800183 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800184 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
185 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800186 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800187 event->event.video_format.frame_width,
188 event->event.video_format.frame_height,
189 event->event.video_format.frame_rate);
190 break;
191 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800192 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
193 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800194 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chene41f4372020-06-06 16:29:17 +0800195 if (player->first_trans_ok == DVR_FALSE) {
196 player->first_trans_ok = DVR_TRUE;
197 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
198 }
hualing chen30423862021-04-16 14:39:12 +0800199 if (player != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800200 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800201 player->seek_pause = DVR_FALSE;
202 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800203 break;
204 }
hualing chen487ae6d2020-07-22 10:34:11 +0800205 case AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO:
206 if (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE) {
207 player->first_trans_ok = DVR_TRUE;
208 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
209 }
210 if (player != NULL && player->has_video == DVR_FALSE) {
211 DVR_PB_DG(1, "[evt]AM_TSPLAYER_EVENT_TYPE_DECODE_FIRST_FRAME_AUDIO [%d]\n", event->type);
212 player->first_frame = 1;
hualing chen30423862021-04-16 14:39:12 +0800213 player->seek_pause = DVR_FALSE;
hualing chen487ae6d2020-07-22 10:34:11 +0800214 }
215 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800216 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800217 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800218 break;
219 }
220 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800221 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800222 player->player_callback_func(player->player_callback_userdata, event);
223 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800224 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800225 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800226 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800227 }
228}
hualing chencc91e1c2020-02-28 13:26:17 +0800229
hualing chen5cbe1a62020-02-10 16:36:36 +0800230//convert video and audio fmt
231static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
232 int format = 0;
233 if (is_audio == DVR_FALSE) {
234 //for video fmt
235 switch (fmt)
236 {
237 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800238 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800239 break;
240 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800241 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800242 break;
243 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800244 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800245 break;
246 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800247 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800248 break;
hualing chena540a7e2020-03-27 16:44:05 +0800249 case DVR_VIDEO_FORMAT_VP9:
250 format = AV_VIDEO_CODEC_VP9;
251 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800252 }
253 } else {
254 //for audio fmt
255 switch (fmt)
256 {
257 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800258 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800259 break;
260 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800261 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800262 break;
263 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800264 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800265 break;
266 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800267 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800268 break;
hualing chena540a7e2020-03-27 16:44:05 +0800269 case DVR_AUDIO_FORMAT_AAC:
270 format = AV_AUDIO_CODEC_AAC;
271 break;
272 case DVR_AUDIO_FORMAT_LATM:
273 format = AV_AUDIO_CODEC_LATM;
274 break;
275 case DVR_AUDIO_FORMAT_PCM:
276 format = AV_AUDIO_CODEC_PCM;
277 break;
hualing chenee0e52b2021-04-09 16:58:44 +0800278 case DVR_AUDIO_FORMAT_AC4:
279 format = AV_AUDIO_CODEC_AC4;
280 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800281 }
282 }
283 return format;
284}
hualing chen040df222020-01-17 13:35:02 +0800285static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800286{
hualing chen040df222020-01-17 13:35:02 +0800287 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800288
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800289 if (player == NULL || player->handle == (am_tsplayer_handle)NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800290 return -1;
291
hualing chena540a7e2020-03-27 16:44:05 +0800292 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800293}
hualing chena540a7e2020-03-27 16:44:05 +0800294
hualing chen7ea70a72021-09-09 11:25:13 +0800295
296//get sys time sec
297static uint32_t _dvr_getClock_sec(void)
hualing chen5cbe1a62020-02-10 16:36:36 +0800298{
299 struct timespec ts;
hualing chen7ea70a72021-09-09 11:25:13 +0800300 uint32_t s;
hualing chen03fd4942021-07-15 15:56:41 +0800301 clock_gettime(CLOCK_REALTIME, &ts);
hualing chen7ea70a72021-09-09 11:25:13 +0800302 s = (uint32_t)(ts.tv_sec);
303 DVR_PB_DG(1, "n:%u", s);
304 return s;
hualing chen5cbe1a62020-02-10 16:36:36 +0800305}
hualing chen86e7d482020-01-16 15:13:33 +0800306
hualing chen7ea70a72021-09-09 11:25:13 +0800307//get sys time ms
308static uint32_t _dvr_time_getClock(void)
309{
310 struct timespec ts;
311 uint32_t ms;
312 clock_gettime(CLOCK_REALTIME, &ts);
313 ms = (uint32_t)(ts.tv_sec*1000+ts.tv_nsec/1000000);
314 return ms;
315}
hualing chenb31a6c62020-01-13 17:27:00 +0800316
317//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800318static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800319{
hualing chen040df222020-01-17 13:35:02 +0800320 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800321
hualing chena540a7e2020-03-27 16:44:05 +0800322
323 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800324 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800325 return DVR_FAILURE;
326 }
327
hualing chen86e7d482020-01-16 15:13:33 +0800328 struct timespec ts;
329 clock_gettime(CLOCK_MONOTONIC, &ts);
330 //ms为毫秒,换算成秒
331 ts.tv_sec += ms/1000;
332 //在outtime的基础上,增加ms毫秒
333 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
334 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
335 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
336 //us的值有可能超过1秒,
337 ts.tv_sec += us / 1000000;
338 us = us % 1000000;
339 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800340 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
341 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800342}
hualing chen31140872020-03-25 12:29:26 +0800343//get tsplay delay time ms
344static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
345 DVR_Playback_t *player = (DVR_Playback_t *) handle;
346 int64_t cache = 0;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800347 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800348 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800349 return 0;
350 }
351 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800352 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800353 return cache;
354}
hualing chenb31a6c62020-01-13 17:27:00 +0800355//send signal
hualing chen040df222020-01-17 13:35:02 +0800356static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800357{
hualing chen87072a82020-03-12 16:20:12 +0800358 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800359
360 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800361 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800362 return DVR_FAILURE;
363 }
364
hualing chen87072a82020-03-12 16:20:12 +0800365 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800366 pthread_cond_signal(&player->cond);
hualing chen87072a82020-03-12 16:20:12 +0800367 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800368 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800369}
370
hualing chen2932d372020-04-29 13:44:00 +0800371//send playback event, need check is need lock first
372static 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 +0800373
374 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800375
376 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800377 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800378 return DVR_FAILURE;
379 }
380
hualing chencc91e1c2020-02-28 13:26:17 +0800381 switch (evt) {
382 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800383 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800384 break;
385 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
386 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800387 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800388 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800389 break;
390 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
391 break;
392 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
393 break;
394 case DVR_PLAYBACK_EVENT_NO_KEY:
395 break;
396 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800397 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800398 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800399 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800400 break;
401 case DVR_PLAYBACK_EVENT_REACHED_END:
402 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800403 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800404 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800405 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800406 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800407 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800408 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800409 default:
410 break;
411 }
412 if (player->openParams.event_fn != NULL)
413 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800414 return DVR_SUCCESS;
415}
hualing chen2932d372020-04-29 13:44:00 +0800416static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800417{
418 DVR_Play_Notify_t notify;
419 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
420 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
421 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800422 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800423 return DVR_SUCCESS;
424}
425
hualing chen2932d372020-04-29 13:44:00 +0800426static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800427{
428 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800429
hualing chene3797f02021-01-13 14:53:28 +0800430 if (player->openParams.is_notify_time == DVR_FALSE) {
hualing chend241c7a2021-06-22 13:34:27 +0800431 if (CONTROL_SPEED_ENABLE == 0)
432 return DVR_SUCCESS;
hualing chen4b7c15d2020-04-07 16:13:48 +0800433 }
hualing chena540a7e2020-03-27 16:44:05 +0800434 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800435 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800436 return DVR_FAILURE;
437 }
438
hualing chen03fd4942021-07-15 15:56:41 +0800439 if (player->send_time == 0) {
hualing chend241c7a2021-06-22 13:34:27 +0800440 if (CONTROL_SPEED_ENABLE == 0)
441 player->send_time = _dvr_time_getClock() + 500;
442 else
443 player->send_time = _dvr_time_getClock() + 20;
hualing chen0888c032020-12-18 17:54:57 +0800444 } else if (player->send_time >= _dvr_time_getClock()) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800445 return DVR_SUCCESS;
446 }
hualing chend241c7a2021-06-22 13:34:27 +0800447 if (CONTROL_SPEED_ENABLE == 0)
448 player->send_time = _dvr_time_getClock() + 500;
449 else
450 player->send_time = _dvr_time_getClock() + 20;
451
hualing chen6e4bfa52020-03-13 14:37:11 +0800452 DVR_Play_Notify_t notify;
453 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
454 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
455 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800456 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800457 return DVR_SUCCESS;
458}
459
hualing chencc91e1c2020-02-28 13:26:17 +0800460//check is ongoing segment
461static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
462
463 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800464 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800465
466 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800467 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800468 return DVR_FAILURE;
469 }
hualing chen87072a82020-03-12 16:20:12 +0800470 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800471 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800472 return DVR_FALSE;
473 }
hualing chencc91e1c2020-02-28 13:26:17 +0800474 return DVR_TRUE;
475}
hualing chen4b7c15d2020-04-07 16:13:48 +0800476
477
478static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
479 DVR_Playback_t *player = (DVR_Playback_t *) handle;
480 player->fffb_start = _dvr_time_getClock();
hualing chen7ea70a72021-09-09 11:25:13 +0800481 DVR_PB_DG(1, " player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800482 player->fffb_current = player->fffb_start;
483 //get segment current time pos
484 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +0800485 player->next_fffb_time = _dvr_time_getClock();
486
487 return DVR_SUCCESS;
488}
489
hualing chen2aba4022020-03-02 13:49:55 +0800490static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
491 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800492 player->fffb_start = _dvr_time_getClock();
hualing chen7ea70a72021-09-09 11:25:13 +0800493 DVR_PB_DG(1, " player->fffb_start:%u", player->fffb_start);
hualing chen4b7c15d2020-04-07 16:13:48 +0800494 player->fffb_current = player->fffb_start;
495 //get segment current time pos
496 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +0800497
hualing chen2aba4022020-03-02 13:49:55 +0800498 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800499 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800500 return DVR_SUCCESS;
501}
hualing chencc91e1c2020-02-28 13:26:17 +0800502//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800503static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
504
505 DVR_Playback_t *player = (DVR_Playback_t *) handle;
506 DVR_PlaybackSegmentInfo_t *segment;
507 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
508
hualing chena540a7e2020-03-27 16:44:05 +0800509 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800510 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800511 return DVR_FAILURE;
512 }
513
hualing chen87072a82020-03-12 16:20:12 +0800514 int found = 0;
515 int found_eq_id = 0;
516 list_for_each_entry(segment, &player->segment_list, head)
517 {
518 if (player->segment_is_open == DVR_FALSE) {
519 //get first segment from list, case segment is not open
520 if (!IS_FB(player->speed))
521 found = 1;
522 } else if (segment->segment_id == segmentid) {
523 //find cur segment, we need get next one
524 found_eq_id = 1;
525 if (!IS_FB(player->speed)) {
526 found = 1;
527 continue;
528 } else {
529 //if is fb mode.we need used pre segment
530 if (pre_segment != NULL) {
531 found = 1;
532 } else {
533 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800534 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800535 return DVR_FAILURE;
536 }
537 }
538 }
539 if (found == 1) {
540 found = 2;
541 break;
542 }
hualing chenc7aa4c82021-02-03 15:41:37 +0800543 pre_segment = segment;
hualing chen87072a82020-03-12 16:20:12 +0800544 }
545 if (found != 2) {
546 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800547 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800548 return DVR_FAILURE;
549 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800550 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800551 return DVR_SUCCESS;
552}
553
554//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800555static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800556
hualing chen040df222020-01-17 13:35:02 +0800557 DVR_Playback_t *player = (DVR_Playback_t *) handle;
558 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800559 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen03fd4942021-07-15 15:56:41 +0800560 uint64_t segmentid;
hualing chen7ea70a72021-09-09 11:25:13 +0800561 uint32_t pos;
hualing chena540a7e2020-03-27 16:44:05 +0800562 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800563 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800564 return DVR_FAILURE;
565 }
566
hualing chen03fd4942021-07-15 15:56:41 +0800567 if (IS_FB(player->speed)
568 && dvr_playback_check_limit(handle)) {
569 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
570 //case cur id < segment id
571 if (player->cur_segment_id <= segmentid) {
572 //expired ts data is player,return error
573 DVR_PB_DG(1, "reach start segment ,return error");
574 return DVR_FAILURE;
575 }
hualing chen7ea70a72021-09-09 11:25:13 +0800576 DVR_PB_DG(1, "has segment to fb play [%lld][%u]", segmentid, pos);
hualing chen03fd4942021-07-15 15:56:41 +0800577 }
578
hualing chen86e7d482020-01-16 15:13:33 +0800579 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800580 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800581
hualing chen040df222020-01-17 13:35:02 +0800582 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800583 {
hualing chencc91e1c2020-02-28 13:26:17 +0800584 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800585 //get first segment from list, case segment is not open
586 if (!IS_FB(player->speed))
587 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800588 } else if (segment->segment_id == player->cur_segment_id) {
589 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800590 found_eq_id = 1;
591 if (!IS_FB(player->speed)) {
592 found = 1;
593 continue;
594 } else {
595 //if is fb mode.we need used pre segment
596 if (pre_segment != NULL) {
597 found = 1;
598 } else {
599 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800600 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800601 return DVR_FAILURE;
602 }
603 }
hualing chen86e7d482020-01-16 15:13:33 +0800604 }
605 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800606 if (IS_FB(player->speed)) {
607 //used pre segment
608 segment = pre_segment;
609 }
hualing chencc91e1c2020-02-28 13:26:17 +0800610 //save segment info
611 player->last_segment_id = player->cur_segment_id;
hualing chen969fe7b2021-05-26 15:13:17 +0800612 if (player->r_handle)
613 player->last_segment_tatol = segment_tell_total_time(player->r_handle);
hualing chen87072a82020-03-12 16:20:12 +0800614 player->last_segment.segment_id = player->cur_segment.segment_id;
615 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800616 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
617 //pids
618 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
619
hualing chen5cbe1a62020-02-10 16:36:36 +0800620 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800621 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800622 player->cur_segment_id = segment->segment_id;
623 player->cur_segment.segment_id = segment->segment_id;
624 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800625 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 +0800626 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800627 //pids
hualing chen040df222020-01-17 13:35:02 +0800628 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800629 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800630 break;
hualing chen86e7d482020-01-16 15:13:33 +0800631 }
hualing chen2aba4022020-03-02 13:49:55 +0800632 pre_segment = segment;
633 }
634 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
635 //used the last one segment to open
636 //get segment info
637 player->segment_is_open = DVR_TRUE;
638 player->cur_segment_id = pre_segment->segment_id;
639 player->cur_segment.segment_id = pre_segment->segment_id;
640 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800641 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 +0800642 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
643 //pids
644 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
645 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800646 }
647 if (found != 2) {
648 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800649 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800650 }
651 return DVR_SUCCESS;
652}
hualing chen040df222020-01-17 13:35:02 +0800653//open next segment to play,if reach list end return errro.
654static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800655{
hualing chen040df222020-01-17 13:35:02 +0800656 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800657 Segment_OpenParams_t params;
658 int ret = DVR_SUCCESS;
659
hualing chena540a7e2020-03-27 16:44:05 +0800660 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800661 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800662 return DVR_FAILURE;
663 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800664 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800665
666 ret = _dvr_get_next_segmentId(handle);
667 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800668 DVR_PB_DG(1, "not found segment info");
669 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800670 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800671 }
672
673 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800674 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800675 segment_close(player->r_handle);
676 player->r_handle = NULL;
677 }
678
679 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800680 //cp chur segment path to location
681 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800682 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800683 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800684 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
685
hualing chen86e7d482020-01-16 15:13:33 +0800686 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800687 if (ret == DVR_FAILURE) {
688 DVR_PB_DG(1, "open segment error");
689 }
hualing chen87072a82020-03-12 16:20:12 +0800690 pthread_mutex_unlock(&player->segment_lock);
691 int total = _dvr_get_end_time( handle);
692 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800693 if (IS_FB(player->speed)) {
694 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen5605eed2020-05-26 18:18:06 +0800695 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +0800696 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800697 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800698 }
hualing chen87072a82020-03-12 16:20:12 +0800699 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800700 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800701 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800702 return ret;
703}
704
hualing chen5cbe1a62020-02-10 16:36:36 +0800705//open next segment to play,if reach list end return errro.
706static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
707{
708 DVR_Playback_t *player = (DVR_Playback_t *) handle;
709 Segment_OpenParams_t params;
710 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800711 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800712 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800713 return DVR_FAILURE;
714 }
hualing chencc91e1c2020-02-28 13:26:17 +0800715 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800716 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800717 }
hualing chencc91e1c2020-02-28 13:26:17 +0800718 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800719 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800720 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800721 return DVR_FAILURE;
722 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800723 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800724 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800725
726 DVR_PlaybackSegmentInfo_t *segment;
727
728 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800729
hualing chen5cbe1a62020-02-10 16:36:36 +0800730 list_for_each_entry(segment, &player->segment_list, head)
731 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800732 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 +0800733 if (segment->segment_id == segment_id) {
734 found = 1;
735 }
736 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800737 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 +0800738 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800739 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800740 player->cur_segment_id = segment->segment_id;
741 player->cur_segment.segment_id = segment->segment_id;
742 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800743 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800744 //pids
745 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800746 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 +0800747 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800748 }
749 }
hualing chencc91e1c2020-02-28 13:26:17 +0800750 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800751 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800752 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800753 return DVR_FAILURE;
754 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800755 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800756 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800757 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800758 params.segment_id = (uint64_t)player->cur_segment.segment_id;
759 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800760 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 +0800761 if (player->r_handle != NULL) {
762 segment_close(player->r_handle);
763 player->r_handle = NULL;
764 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800765 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800766 if (ret == DVR_FAILURE) {
767 DVR_PB_DG(1, "segment opne error");
768 }
hualing chen2aba4022020-03-02 13:49:55 +0800769 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800770 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800771
hualing chen4b7c15d2020-04-07 16:13:48 +0800772 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 +0800773 return ret;
774}
775
776
777//get play info by segment id
778static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
779 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800780 am_tsplayer_video_params *vparam,
hualing chendf118dd2020-05-21 15:49:11 +0800781 am_tsplayer_audio_params *aparam, am_tsplayer_audio_params *adparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800782
783 DVR_Playback_t *player = (DVR_Playback_t *) handle;
784 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800785 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800786 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800787 return DVR_FAILURE;
788 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800789
790 int found = 0;
791
792 list_for_each_entry(segment, &player->segment_list, head)
793 {
hualing chen87072a82020-03-12 16:20:12 +0800794 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800795 //get first segment from list
796 found = 1;
797 }
798 if (segment->segment_id == segment_id) {
799 found = 1;
800 }
801 if (found == 1) {
802 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800803 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800804 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800805 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800806 player->cur_segment.segment_id = segment->segment_id;
807 player->cur_segment.flags = segment->flags;
808 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800809 player->cur_segment.pids.video.pid = segment->pids.video.pid;
810 player->cur_segment.pids.video.format = segment->pids.video.format;
811 player->cur_segment.pids.video.type = segment->pids.video.type;
812 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
813 player->cur_segment.pids.audio.format = segment->pids.audio.format;
814 player->cur_segment.pids.audio.type = segment->pids.audio.type;
815 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
816 player->cur_segment.pids.ad.format = segment->pids.ad.format;
817 player->cur_segment.pids.ad.type = segment->pids.ad.type;
818 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800819 //
hualing chen2aba4022020-03-02 13:49:55 +0800820 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800821 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800822 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800823 aparam->pid = segment->pids.audio.pid;
hualing chendf118dd2020-05-21 15:49:11 +0800824 adparam->codectype =_dvr_convert_stream_fmt(segment->pids.ad.format, DVR_TRUE);
825 adparam->pid =segment->pids.ad.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800826 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 +0800827 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800828 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800829 }
830 }
hualing chencc91e1c2020-02-28 13:26:17 +0800831 if (found != 2) {
832 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800833 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800834 return DVR_FAILURE;
835 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800836
837 return DVR_SUCCESS;
838}
hualing chencc91e1c2020-02-28 13:26:17 +0800839static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
840 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800841 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800842 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800843 return DVR_FAILURE;
844 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800845
hualing chencc91e1c2020-02-28 13:26:17 +0800846 //compare cur segment
847 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
848 {
849 //check video pids, stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800850 _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids, 0);
hualing chencc91e1c2020-02-28 13:26:17 +0800851 //check sub audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800852 _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids, 2);
hualing chen969fe7b2021-05-26 15:13:17 +0800853 //check audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800854 _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids, 1);
hualing chene3797f02021-01-13 14:53:28 +0800855 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 +0800856 //check pcr pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +0800857 _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids, 3);
hualing chencc91e1c2020-02-28 13:26:17 +0800858 }
hualing chena540a7e2020-03-27 16:44:05 +0800859 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800860}
hualing chen5cbe1a62020-02-10 16:36:36 +0800861
hualing chend241c7a2021-06-22 13:34:27 +0800862static int _dvr_check_speed_con(DVR_PlaybackHandle_t handle)
863{
864 DVR_Playback_t *player = (DVR_Playback_t *) handle;
865 if (player == NULL) {
866 DVR_PB_DG(1, "player is NULL");
867 return DVR_TRUE;
868 }
869 char buf[10];
870 dvr_prop_read("vendor.tv.libdvr.con", buf, sizeof(buf));
871 DVR_PB_DG(1, "player get prop[%d][%s]", atoi(buf), buf);
872
873 if (atoi(buf) != 1) {
874 //return DVR_TRUE;
875 }
876
hualing chen7ea70a72021-09-09 11:25:13 +0800877 DVR_PB_DG(1, ":play speed: %f ply dur: %u sys_dur: %u",
hualing chen03fd4942021-07-15 15:56:41 +0800878 player->speed,
879 player->con_spe.ply_dur,
880 player->con_spe.sys_dur);
hualing chend241c7a2021-06-22 13:34:27 +0800881
882 if (player->speed != 1.0f)
883 return DVR_TRUE;
884
885 if (player->con_spe.ply_dur > 0
hualing chen03fd4942021-07-15 15:56:41 +0800886 && 2 * player->con_spe.ply_dur > 3 * player->con_spe.sys_dur)
hualing chend241c7a2021-06-22 13:34:27 +0800887 return DVR_FALSE;
888
889 return DVR_TRUE;
890}
891
hualing chencc91e1c2020-02-28 13:26:17 +0800892static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
893{
894 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800895 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800896 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800897 return DVR_FAILURE;
898 }
hualing chenf43b8ba2020-07-28 13:11:42 +0800899 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
900 DVR_PB_DG(1, "vendor is amlogic. no used segment flag to hide or show av");
901 return DVR_SUCCESS;
902 }
hualing chen03fd4942021-07-15 15:56:41 +0800903 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]",
904 player->cur_segment.flags,
905 player->cur_segment.segment_id,
906 player->last_segment.flags,
907 player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800908 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
909 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800910 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800911 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800912 AmTsPlayer_showVideo(player->handle);
913 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800914 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
915 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800916 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800917 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800918 AmTsPlayer_hideVideo(player->handle);
919 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800920 }
921 return DVR_SUCCESS;
922}
hualing chene3797f02021-01-13 14:53:28 +0800923/*
924if decodec sucess first time.
925sucess: return true
926fail: return false
927*/
hualing chena540a7e2020-03-27 16:44:05 +0800928static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
929 DVR_Playback_t *player = (DVR_Playback_t *) handle;
930 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800931 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800932 return DVR_TRUE;
933 }
hualing chene3797f02021-01-13 14:53:28 +0800934 if (player->first_frame == 1) {
hualing chena540a7e2020-03-27 16:44:05 +0800935 return DVR_TRUE;
hualing chene3797f02021-01-13 14:53:28 +0800936 } else {
937 return DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +0800938 }
939}
hualing chen86e7d482020-01-16 15:13:33 +0800940static void* _dvr_playback_thread(void *arg)
941{
hualing chen040df222020-01-17 13:35:02 +0800942 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800943 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800944 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800945 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800946 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800947
hualing chen39628212020-05-14 10:35:13 +0800948 #define MAX_REACHEND_TIMEOUT (3000)
949 int reach_end_timeout = 0;//ms
950 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800951 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800952 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800953 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800954 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800955 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
956
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800957 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800958 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800959 DVR_Bool_t goto_rewrite = DVR_FALSE;
yinming ding0ce94922021-09-08 15:09:15 +0800960 char prop_buf[10];
961
962 memset(prop_buf, 0 ,sizeof(prop_buf));
963 dvr_prop_read("vendor.tv.libdvr.writetm", prop_buf, sizeof(prop_buf));
964 DVR_PB_DG(1, "vendor.tv.libdvr.writetm get prop[%d][%s]", atoi(prop_buf), prop_buf);
965 if (atoi(prop_buf) > 0)
966 write_timeout_ms = atoi(prop_buf);
hualing chen03fd4942021-07-15 15:56:41 +0800967
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800968 if (player->is_secure_mode) {
969 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800970 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800971 return NULL;
972 }
973 }
hualing chen86e7d482020-01-16 15:13:33 +0800974 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800975 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800976 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800977 return NULL;
978 }
hualing chen2aba4022020-03-02 13:49:55 +0800979 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
980 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800981
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800982 dec_bufs.buf_data = malloc(dec_buf_size);
983 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800984 DVR_PB_DG(1, "Malloc dec buffer failed");
Pengfei Liufaf38e42020-05-22 00:28:02 +0800985 free(buf);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800986 return NULL;
987 }
988 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
989 dec_bufs.buf_size = dec_buf_size;
990
hualing chencc91e1c2020-02-28 13:26:17 +0800991 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800992 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
993 }
hualing chen86e7d482020-01-16 15:13:33 +0800994
hualing chen86e7d482020-01-16 15:13:33 +0800995 if (ret != DVR_SUCCESS) {
996 if (buf != NULL) {
997 free(buf);
998 buf = NULL;
999 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001000 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +08001001 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +08001002 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +08001003 }
hualing chen4fe3bee2020-10-23 13:58:52 +08001004 DVR_PB_DG(1, "player->vendor %d,player->has_video[%d] bufsize[0x%x]whole block[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08001005 player->vendor, player->has_video, buf_len, b_writed_whole_block);
hualing chenfbf8e022020-06-15 13:43:11 +08001006 //get play statue not here,send ok event when vendor is aml or only audio channel if not send ok event
1007 if (((player->first_trans_ok == DVR_FALSE) && (player->vendor == DVR_PLAYBACK_VENDOR_AML) ) ||
1008 (player->first_trans_ok == DVR_FALSE && player->has_video == DVR_FALSE)) {
1009 player->first_trans_ok = DVR_TRUE;
1010 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
1011 }
hualing chencc91e1c2020-02-28 13:26:17 +08001012 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +08001013 //set video show
1014 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08001015
hualing chen86e7d482020-01-16 15:13:33 +08001016 int trick_stat = 0;
1017 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +08001018
hualing chen86e7d482020-01-16 15:13:33 +08001019 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +08001020 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001021
hualing chen2aba4022020-03-02 13:49:55 +08001022 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
1023 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08001024 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +08001025 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +08001026 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +08001027 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +08001028 {
hualing chen2aba4022020-03-02 13:49:55 +08001029 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
1030 if (trick_stat > 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001031 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]",
1032 trick_stat, player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +08001033 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 +08001034 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +08001035 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +08001036 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +08001037 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
1038 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +08001039 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
1040 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chen03fd4942021-07-15 15:56:41 +08001041 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]",
1042 player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +08001043 //need change to pause state
1044 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
1045 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +08001046 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08001047 //clear flag
hualing chen31140872020-03-25 12:29:26 +08001048 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +08001049 player->first_frame = 0;
hualing chen10cdb162021-02-05 10:44:41 +08001050 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001051 AmTsPlayer_pauseVideoDecoding(player->handle);
1052 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +08001053 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001054 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +08001055 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001056 }
1057 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
1058 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +08001059 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +08001060 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +08001061 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen7ea70a72021-09-09 11:25:13 +08001062 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%u] cur sys[%u] [%s] [%u]",
hualing chen03fd4942021-07-15 15:56:41 +08001063 player->speed,
1064 player->fffb_current,
1065 _dvr_time_getClock(),
1066 _dvr_playback_state_toString(player->state),
1067 player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +08001068 //used timeout wait need lock first,so we unlock and lock
1069 //pthread_mutex_unlock(&player->lock);
1070 //pthread_mutex_lock(&player->lock);
1071 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1072 pthread_mutex_unlock(&player->lock);
1073 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +08001074 } else if (_dvr_time_getClock() < player->next_fffb_time) {
hualing chen7ea70a72021-09-09 11:25:13 +08001075 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 +08001076 player->speed,
1077 player->fffb_current,
1078 _dvr_time_getClock(),
1079 _dvr_playback_state_toString(player->state),
1080 player->next_fffb_time);
hualing chenb5cd42e2020-04-15 17:03:34 +08001081 //used timeout wait need lock first,so we unlock and lock
1082 //pthread_mutex_unlock(&player->lock);
1083 //pthread_mutex_lock(&player->lock);
1084 AmTsPlayer_pauseVideoDecoding(player->handle);
1085 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1086 pthread_mutex_unlock(&player->lock);
1087 continue;
1088
hualing chen2aba4022020-03-02 13:49:55 +08001089 }
hualing chen03fd4942021-07-15 15:56:41 +08001090 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]",
1091 player->speed,
1092 goto_rewrite,
1093 real_read,
1094 _dvr_playback_state_toString(player->state),
1095 player->cmd.cur_cmd);
1096
hualing chen2aba4022020-03-02 13:49:55 +08001097 pthread_mutex_unlock(&player->lock);
1098 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001099 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001100 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1101 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +08001102 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001103 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001104 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08001105 } else if(player->state == DVR_PLAYBACK_STATE_PAUSE) {
1106 //on pause state,user seek to new pos,we need pause and wait
1107 //user to resume
1108 DVR_PB_DG(1, "pause, when got first frame event when user seek end");
1109 player->first_frame = 0;
1110 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1111 AmTsPlayer_pauseVideoDecoding(player->handle);
1112 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001113 }
hualing chen1ffd85b2021-08-16 15:18:43 +08001114 } else if (player->fffb_play == DVR_TRUE){
hualing chen4b7c15d2020-04-07 16:13:48 +08001115 //for first into fffb when reset speed
1116 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
1117 _dvr_time_getClock() < player->next_fffb_time) {
hualing chen7ea70a72021-09-09 11:25:13 +08001118 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 +08001119 player->speed,
1120 player->fffb_current,
1121 _dvr_time_getClock(),
1122 _dvr_playback_state_toString(player->state),
1123 player->next_fffb_time);
hualing chen4b7c15d2020-04-07 16:13:48 +08001124 //used timeout wait need lock first,so we unlock and lock
1125 //pthread_mutex_unlock(&player->lock);
1126 //pthread_mutex_lock(&player->lock);
1127 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1128 pthread_mutex_unlock(&player->lock);
1129 continue;
1130 }
hualing chen03fd4942021-07-15 15:56:41 +08001131 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]",
1132 player->speed,
1133 goto_rewrite,
1134 real_read,
1135 _dvr_playback_state_toString(player->state),
1136 player->cmd.cur_cmd,
1137 player->fffb_play);
hualing chen4b7c15d2020-04-07 16:13:48 +08001138 pthread_mutex_unlock(&player->lock);
1139 goto_rewrite = DVR_FALSE;
1140 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001141 player->ts_cache_len = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08001142 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1143 player->first_frame = 0;
1144 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
1145 pthread_mutex_lock(&player->lock);
1146 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001147 }
hualing chenb31a6c62020-01-13 17:27:00 +08001148 }
hualing chen86e7d482020-01-16 15:13:33 +08001149
hualing chen30423862021-04-16 14:39:12 +08001150 if (player->state == DVR_PLAYBACK_STATE_PAUSE
1151 && player->seek_pause == DVR_FALSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001152 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001153 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001154 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001155 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1156 pthread_mutex_unlock(&player->lock);
1157 continue;
1158 }
hualing chen266b9502020-04-04 17:39:39 +08001159 //when seek action is done. we need drop write timeout data.
1160 if (player->drop_ts == DVR_TRUE) {
1161 goto_rewrite = DVR_FALSE;
1162 real_read = 0;
1163 player->drop_ts = DVR_FALSE;
1164 }
hualing chen2aba4022020-03-02 13:49:55 +08001165 if (goto_rewrite == DVR_TRUE) {
1166 goto_rewrite = DVR_FALSE;
1167 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001168 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001169 goto rewrite;
1170 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001171 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001172 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001173 pthread_mutex_lock(&player->segment_lock);
hualing chene41f4372020-06-06 16:29:17 +08001174 //DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001175 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chenfbf8e022020-06-15 13:43:11 +08001176 //DVR_PB_DG(1, "start read end [%d]", read);
hualing chen4b7c15d2020-04-07 16:13:48 +08001177 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001178 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001179 if (read < 0 && errno == EIO) {
1180 //EIO ERROR, EXIT THRAD
1181 DVR_PB_DG(1, "read error.EIO error, exit thread");
1182 DVR_Play_Notify_t notify;
1183 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1184 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen9b434f02020-06-10 15:06:54 +08001185 notify.info.error_reason = DVR_ERROR_REASON_READ;
hualing chen2932d372020-04-29 13:44:00 +08001186 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001187 goto end;
1188 } else if (read < 0) {
1189 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1190 }
hualing chen87072a82020-03-12 16:20:12 +08001191 //if on fb mode and read file end , we need calculate pos to retry read.
1192 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen03fd4942021-07-15 15:56:41 +08001193 DVR_PB_DG(1, "recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]",
1194 read,
1195 real_read,
1196 buf_len,
1197 player->speed,
1198 player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001199 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1200 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001201 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1202 pthread_mutex_unlock(&player->lock);
1203 continue;
1204 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001205 //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 +08001206 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001207 //file end.need to play next segment
hualing chene41f4372020-06-06 16:29:17 +08001208 #define MIN_CACHE_TIME (3000)
1209 int _cache_time = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player) ;
hualing chene3797f02021-01-13 14:53:28 +08001210 /*if cache time is > min cache time ,not read next segment,wait cache data to play*/
hualing chene41f4372020-06-06 16:29:17 +08001211 if (_cache_time > MIN_CACHE_TIME) {
hualing chene41f4372020-06-06 16:29:17 +08001212 pthread_mutex_lock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001213 /*if cache time > 20s , we think get time is error,*/
1214 if (_cache_time - MIN_CACHE_TIME > 20 * 1000) {
1215 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1216 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1217 DVR_PB_DG(1, "read end but cache time is %d > 20s, this is an error at media_hal", _cache_time);
1218 }
1219 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, ((_cache_time - MIN_CACHE_TIME) > MIN_CACHE_TIME ? MIN_CACHE_TIME : (_cache_time - MIN_CACHE_TIME)));
hualing chene41f4372020-06-06 16:29:17 +08001220 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001221 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 +08001222 //continue;
1223 }
hualing chen969fe7b2021-05-26 15:13:17 +08001224
hualing chen040df222020-01-17 13:35:02 +08001225 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001226 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001227 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001228
1229 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chene3797f02021-01-13 14:53:28 +08001230 if (ret != DVR_SUCCESS) {
hualing chene8735082021-10-13 16:44:20 +08001231 if (player->vendor == DVR_PLAYBACK_VENDOR_AMAZON) {
1232 if (delay > 700) {
1233 DVR_PB_DG(1, "delay time [%d] > 700, not send nodata event", delay);
1234 } else {
1235 player->noData++;
1236 }
1237 } else {
1238 player->noData++;
1239 }
hualing chene3797f02021-01-13 14:53:28 +08001240 if (player->noData == 4) {
1241 DVR_PB_DG(1, "playback send nodata event nodata[%d]", player->noData);
1242 //send event here and pause
1243 DVR_Play_Notify_t notify;
1244 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1245 notify.event = DVR_PLAYBACK_EVENT_NODATA;
1246 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_NODATA");
1247 //get play statue not here
1248 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_NODATA, &notify, DVR_FALSE);
1249 }
1250 }
1251 //send reached event
hualing chen39628212020-05-14 10:35:13 +08001252 if ((ret != DVR_SUCCESS &&
hualing chen03fd4942021-07-15 15:56:41 +08001253 (player->vendor != DVR_PLAYBACK_VENDOR_AMAZON) &&
hualing chen041c4092020-04-05 15:11:50 +08001254 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001255 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001256 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1257 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001258 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001259 DVR_Play_Notify_t notify;
1260 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1261 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1262 //get play statue not here
1263 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001264 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001265 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001266 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 +08001267 pthread_mutex_lock(&player->lock);
1268 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1269 pthread_mutex_unlock(&player->lock);
1270 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001271 } else if (ret != DVR_SUCCESS) {
1272 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001273 if (delay < cache_time) {
1274 //delay time is changed and then has data to play, so not start timeout
1275 } else {
1276 reach_end_timeout = reach_end_timeout + timeout;
1277 }
1278 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001279 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001280 pthread_mutex_lock(&player->lock);
1281 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1282 pthread_mutex_unlock(&player->lock);
1283 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001284 }
hualing chen39628212020-05-14 10:35:13 +08001285 reach_end_timeout = 0;
1286 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001287 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001288 //change next segment success case
1289 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001290 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001291 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1292 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001293 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001294 pthread_mutex_unlock(&player->lock);
hualing chene3797f02021-01-13 14:53:28 +08001295 }//read len 0 check end
1296 if (player->noData > 0) {
1297 player->noData = 0;
1298 DVR_PB_DG(1, "playback send data event resume[%d]", player->noData);
1299 //send event here and pause
1300 DVR_Play_Notify_t notify;
1301 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1302 notify.event = DVR_PLAYBACK_EVENT_DATARESUME;
1303 DVR_PB_DG(1, "send event DVR_PLAYBACK_EVENT_DATARESUME");
1304 //get play statue not here
1305 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_DATARESUME, &notify, DVR_FALSE);
hualing chen86e7d482020-01-16 15:13:33 +08001306 }
hualing chen39628212020-05-14 10:35:13 +08001307 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001308 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001309 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001310 wbufs.buf_data = buf;
hualing chen5605eed2020-05-26 18:18:06 +08001311
hualing chena540a7e2020-03-27 16:44:05 +08001312 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001313 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001314 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001315 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001316 player->ts_cache_len = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001317 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001318 }
hualing chen266b9502020-04-04 17:39:39 +08001319 //if need write whole block size, we need check read buf len is eq block size.
1320 if (b_writed_whole_block == DVR_TRUE) {
1321 //buf_len is block size value.
1322 if (real_read < buf_len) {
1323 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001324 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001325 pthread_mutex_lock(&player->lock);
1326 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1327 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001328 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001329 continue;
1330 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001331 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 +08001332 }
1333 }
1334
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001335 if (player->dec_func) {
1336 DVR_CryptoParams_t crypto_params;
1337
1338 memset(&crypto_params, 0, sizeof(crypto_params));
1339 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1340 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1341 crypto_params.segment_id = player->cur_segment.segment_id;
hualing chen1f26ffa2020-11-03 10:39:20 +08001342 crypto_params.offset = segment_tell_position(player->r_handle) - wbufs.buf_size;
hualing chenbafc62d2020-11-02 15:44:05 +08001343 if ((crypto_params.offset % (player->openParams.block_size)) != 0)
1344 DVR_PB_DG(1, "offset is not block_size %d", player->openParams.block_size);
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001345 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1346 crypto_params.input_buffer.addr = (size_t)buf;
1347 crypto_params.input_buffer.size = real_read;
1348
1349 if (player->is_secure_mode) {
1350 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1351 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1352 crypto_params.output_buffer.size = dec_buf_size;
1353 ret = player->dec_func(&crypto_params, player->dec_userdata);
1354 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001355 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001356 } else {
1357 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1358 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1359 crypto_params.output_buffer.size = dec_buf_size;
1360 ret = player->dec_func(&crypto_params, player->dec_userdata);
1361 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001362 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001363 }
1364 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001365 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001366 }
pengfei.liufda2a972020-04-09 14:47:15 +08001367 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001368 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001369rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001370 if (player->drop_ts == DVR_TRUE) {
1371 //need drop ts data when seek occur.we need read next loop,drop this ts data
1372 goto_rewrite = DVR_FALSE;
1373 real_read = 0;
hualing chen5605eed2020-05-26 18:18:06 +08001374 player->ts_cache_len = 0;
hualing chenbcada022020-04-22 14:27:01 +08001375 player->drop_ts = DVR_FALSE;
1376 continue;
1377 }
hualing chen5605eed2020-05-26 18:18:06 +08001378 player->ts_cache_len = real_read;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001379 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1380 if (ret == AM_TSPLAYER_OK) {
hualing chen5605eed2020-05-26 18:18:06 +08001381 player->ts_cache_len = 0;
hualing chena540a7e2020-03-27 16:44:05 +08001382 real_read = 0;
1383 write_success++;
hualing chend241c7a2021-06-22 13:34:27 +08001384 if (CONTROL_SPEED_ENABLE == 1) {
1385check0:
1386 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
1387 pthread_mutex_lock(&player->lock);
1388 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
1389 pthread_mutex_unlock(&player->lock);
1390 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1391 goto check0;
1392 }
1393 }
hualing chen5605eed2020-05-26 18:18:06 +08001394 //DVR_PB_DG(1, "write write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001395 continue;
hualing chen87072a82020-03-12 16:20:12 +08001396 } else {
hualing chen7ea70a72021-09-09 11:25:13 +08001397 DVR_PB_DG(1, "write time out write_success:%d wbufs.buf_size:%d systime:%u",
hualing chen03fd4942021-07-15 15:56:41 +08001398 write_success,
1399 wbufs.buf_size,
1400 _dvr_time_getClock());
1401
hualing chena540a7e2020-03-27 16:44:05 +08001402 write_success = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001403 if (CONTROL_SPEED_ENABLE == 1) {
1404check1:
1405 if (_dvr_check_speed_con((DVR_PlaybackHandle_t)player) == DVR_FALSE){
1406 pthread_mutex_lock(&player->lock);
1407 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, 50);
1408 pthread_mutex_unlock(&player->lock);
1409 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
1410 goto check1;
1411 }
1412 }
hualing chencc91e1c2020-02-28 13:26:17 +08001413 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001414 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001415 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001416 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001417 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001418 break;
1419 }
hualing chen2aba4022020-03-02 13:49:55 +08001420 goto_rewrite = DVR_TRUE;
1421 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001422 }
1423 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001424end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001425 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001426 free(buf);
1427 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001428 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001429}
1430
1431
hualing chen040df222020-01-17 13:35:02 +08001432static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001433{
hualing chen040df222020-01-17 13:35:02 +08001434 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001435
1436 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001437 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001438 return DVR_FAILURE;
1439 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001440 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001441 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001442 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001443 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001444 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001445 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001446 if (rc < 0)
1447 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001448 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001449}
1450
1451
hualing chen040df222020-01-17 13:35:02 +08001452static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001453{
hualing chen040df222020-01-17 13:35:02 +08001454 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001455
1456 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001457 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001458 return DVR_FAILURE;
1459 }
1460
hualing chen4b7c15d2020-04-07 16:13:48 +08001461 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001462 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001463 {
1464 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001465 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001466 pthread_join(player->playback_thread, NULL);
1467 }
1468 if (player->r_handle) {
1469 segment_close(player->r_handle);
1470 player->r_handle = NULL;
1471 }
hualing chen7a56cba2020-04-14 14:09:27 +08001472 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001473 return 0;
1474}
1475
hualing chenb31a6c62020-01-13 17:27:00 +08001476/**\brief Open an dvr palyback
1477 * \param[out] p_handle dvr playback addr
1478 * \param[in] params dvr playback open parameters
1479 * \retval DVR_SUCCESS On success
1480 * \return Error code
1481 */
hualing chen040df222020-01-17 13:35:02 +08001482int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001483
hualing chen040df222020-01-17 13:35:02 +08001484 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001485 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001486
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001487 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001488
hualing chen86e7d482020-01-16 15:13:33 +08001489 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001490 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001491 pthread_condattr_init(&cattr);
1492 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1493 pthread_cond_init(&player->cond, &cattr);
1494 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001495
hualing chen5cbe1a62020-02-10 16:36:36 +08001496 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001497 INIT_LIST_HEAD(&player->segment_list);
1498 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1499 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001500 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001501 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001502 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001503 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001504 player->speed = 1.0f;
hualing chene41f4372020-06-06 16:29:17 +08001505 player->first_trans_ok = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08001506
hualing chen86e7d482020-01-16 15:13:33 +08001507 //store open params
hualing chen040df222020-01-17 13:35:02 +08001508 player->openParams.dmx_dev_id = params->dmx_dev_id;
1509 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001510 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001511 player->openParams.event_fn = params->event_fn;
1512 player->openParams.event_userdata = params->event_userdata;
hualing chene3797f02021-01-13 14:53:28 +08001513 player->openParams.is_notify_time = params->is_notify_time;
hualing chenfbf8e022020-06-15 13:43:11 +08001514 player->vendor = params->vendor;
hualing chencc91e1c2020-02-28 13:26:17 +08001515
hualing chen5cbe1a62020-02-10 16:36:36 +08001516 player->has_pids = params->has_pids;
1517
hualing chen2aba4022020-03-02 13:49:55 +08001518 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001519
1520 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1521 //for test get callback
1522 if (0 && player->player_callback_func == NULL) {
1523 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1524 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen03fd4942021-07-15 15:56:41 +08001525 DVR_PB_DG(1, "playback open get callback[%p][%p][%p][%p]",
1526 player->player_callback_func,
1527 player->player_callback_userdata,
1528 _dvr_tsplayer_callback_test,
1529 player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001530 }
1531 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001532
hualing chen86e7d482020-01-16 15:13:33 +08001533 //init has audio and video
1534 player->has_video = DVR_FALSE;
1535 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001536 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001537 player->last_segment_id = 0LL;
1538 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001539
hualing chen5cbe1a62020-02-10 16:36:36 +08001540 //init ff fb time
hualing chen7ea70a72021-09-09 11:25:13 +08001541 player->fffb_current = 0;
1542 player->fffb_start = 0;
hualing chen03fd4942021-07-15 15:56:41 +08001543 player->fffb_start_pcr = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001544 //seek time
1545 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001546 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001547
1548 //init secure stuff
1549 player->dec_func = NULL;
1550 player->dec_userdata = NULL;
1551 player->is_secure_mode = 0;
1552 player->secure_buffer = NULL;
1553 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001554 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001555
hualing chen4b7c15d2020-04-07 16:13:48 +08001556 player->fffb_play = DVR_FALSE;
1557
1558 player->last_send_time_id = UINT64_MAX;
1559 player->last_cur_time = 0;
hualing chen30423862021-04-16 14:39:12 +08001560 player->seek_pause = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001561
hualing chend241c7a2021-06-22 13:34:27 +08001562 //speed con init
1563 if (CONTROL_SPEED_ENABLE == 1) {
1564 player->con_spe.ply_dur = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001565 player->con_spe.ply_sta = 0;
hualing chend241c7a2021-06-22 13:34:27 +08001566 player->con_spe.sys_dur = 0;
1567 player->con_spe.sys_sta = 0;
1568 }
1569
hualing chen03fd4942021-07-15 15:56:41 +08001570 //limit info
1571 player->rec_start = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08001572 player->limit = 0;
hualing chen8a657f32021-08-30 13:12:49 +08001573 //need seek to start pos
1574 player->first_start_time = 0;
1575 player->need_seek_start = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001576 *p_handle = player;
1577 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001578}
1579
1580/**\brief Close an dvr palyback
1581 * \param[in] handle playback handle
1582 * \retval DVR_SUCCESS On success
1583 * \return Error code
1584 */
hualing chen040df222020-01-17 13:35:02 +08001585int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001586
hualing chen86e7d482020-01-16 15:13:33 +08001587 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001588 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001589 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001590 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001591 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001592 return DVR_FAILURE;
1593 }
1594
hualing chencc91e1c2020-02-28 13:26:17 +08001595 if (player->state != DVR_PLAYBACK_STATE_STOP)
1596 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001597 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001598 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001599 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1600 } else {
1601 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001602 }
hualing chen7a56cba2020-04-14 14:09:27 +08001603 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001604 pthread_mutex_destroy(&player->lock);
1605 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001606
1607 if (player) {
1608 free(player);
1609 player = NULL;
1610 }
hualing chen7a56cba2020-04-14 14:09:27 +08001611 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001612 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001613}
1614
hualing chenb31a6c62020-01-13 17:27:00 +08001615/**\brief Start play audio and video, used start auido api and start video api
1616 * \param[in] handle playback handle
1617 * \param[in] params audio playback params,contains fmt and pid...
1618 * \retval DVR_SUCCESS On success
1619 * \return Error code
1620 */
hualing chen040df222020-01-17 13:35:02 +08001621int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1622 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001623 am_tsplayer_video_params vparams;
1624 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08001625 am_tsplayer_audio_params adparams;
hualing chena540a7e2020-03-27 16:44:05 +08001626
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001627 memset(&vparams, 0, sizeof(vparams));
1628 memset(&aparams, 0, sizeof(aparams));
1629
hualing chena540a7e2020-03-27 16:44:05 +08001630 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001631 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001632 return DVR_FAILURE;
1633 }
hualing chencc91e1c2020-02-28 13:26:17 +08001634 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001635 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001636
hualing chena540a7e2020-03-27 16:44:05 +08001637 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001638 //can used start api to resume playback
1639 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1640 return dvr_playback_resume(handle);
1641 }
hualing chen87072a82020-03-12 16:20:12 +08001642 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen9b434f02020-06-10 15:06:54 +08001643 //if flag is puased and not decodec first frame. if user resume, we need
1644 //clear flag and set trickmode none
1645 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
1646 DVR_PB_DG(1, "[%p]clear pause live flag and clear trick mode", handle);
1647 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
1648 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
1649 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001650 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001651 return DVR_SUCCESS;
1652 }
hualing chen86e7d482020-01-16 15:13:33 +08001653 player->play_flag = flag;
hualing chene41f4372020-06-06 16:29:17 +08001654 player->first_trans_ok = DVR_FALSE;
hualing chen5cbe1a62020-02-10 16:36:36 +08001655 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001656 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001657 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08001658 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08001659 //start audio and video
Zhiqiang Hana9d261b2020-11-11 18:38:10 +08001660 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08001661 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001662 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001663 pthread_mutex_unlock(&player->lock);
1664 DVR_Play_Notify_t notify;
1665 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1666 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1667 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1668 notify.info.transition_failed_data.segment_id = segment_id;
1669 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001670 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001671 return -1;
1672 }
hualing chen31140872020-03-25 12:29:26 +08001673
hualing chencc91e1c2020-02-28 13:26:17 +08001674 {
hualing chen86e7d482020-01-16 15:13:33 +08001675 if (VALID_PID(vparams.pid)) {
1676 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001677 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001678 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001679 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001680 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1681 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001682 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001683 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001684 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001685 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001686 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001687 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001688 }
hualing chena93bbbc2020-12-22 17:23:42 +08001689 AmTsPlayer_showVideo(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001690 AmTsPlayer_setVideoParams(player->handle, &vparams);
1691 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001692 }
hualing chena540a7e2020-03-27 16:44:05 +08001693
hualing chen4b7c15d2020-04-07 16:13:48 +08001694 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1695 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001696 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1697 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1698 player->cmd.state = DVR_PLAYBACK_STATE_START;
1699 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001700 } else {
1701 player->cmd.last_cmd = player->cmd.cur_cmd;
1702 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001703 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001704 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001705 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001706 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001707 } else {
hualing chendf118dd2020-05-21 15:49:11 +08001708 if (VALID_PID(adparams.pid)) {
1709 player->has_ad_audio = DVR_TRUE;
1710 DVR_PB_DG(1, "start ad audio");
1711 AmTsPlayer_setADParams(player->handle, &adparams);
1712 AmTsPlayer_enableADMix(player->handle);
1713 }
hualing chen969fe7b2021-05-26 15:13:17 +08001714 if (VALID_PID(aparams.pid)) {
1715 DVR_PB_DG(1, "start audio");
1716 player->has_audio = DVR_TRUE;
1717 AmTsPlayer_setAudioParams(player->handle, &aparams);
1718 AmTsPlayer_startAudioDecoding(player->handle);
1719 }
hualing chen31140872020-03-25 12:29:26 +08001720 }
hualing chencc91e1c2020-02-28 13:26:17 +08001721 player->cmd.state = DVR_PLAYBACK_STATE_START;
1722 player->state = DVR_PLAYBACK_STATE_START;
1723 }
hualing chen86e7d482020-01-16 15:13:33 +08001724 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001725 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001726 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001727 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001728 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001729}
hualing chen040df222020-01-17 13:35:02 +08001730/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001731 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001732 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001733 * \retval DVR_SUCCESS On success
1734 * \return Error code
1735 */
hualing chen040df222020-01-17 13:35:02 +08001736int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1737 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001738
hualing chena540a7e2020-03-27 16:44:05 +08001739 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001740 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001741 return DVR_FAILURE;
1742 }
1743
hualing chen4b7c15d2020-04-07 16:13:48 +08001744 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001745 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001746
hualing chen040df222020-01-17 13:35:02 +08001747 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1748 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001749
hualing chen86e7d482020-01-16 15:13:33 +08001750 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001751 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001752 //cp location
hualing chen040df222020-01-17 13:35:02 +08001753 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001754
hualing chen4b7c15d2020-04-07 16:13:48 +08001755 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 +08001756 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001757
1758 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001759 segment->pids.video.pid = info->pids.video.pid;
1760 segment->pids.video.format = info->pids.video.format;
1761 segment->pids.video.type = info->pids.video.type;
1762
hualing chen2aba4022020-03-02 13:49:55 +08001763 segment->pids.audio.pid = info->pids.audio.pid;
1764 segment->pids.audio.format = info->pids.audio.format;
1765 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001766
hualing chen2aba4022020-03-02 13:49:55 +08001767 segment->pids.ad.pid = info->pids.ad.pid;
1768 segment->pids.ad.format = info->pids.ad.format;
1769 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001770
1771 segment->pids.pcr.pid = info->pids.pcr.pid;
1772
hualing chen4b7c15d2020-04-07 16:13:48 +08001773 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 +08001774 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001775 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001776 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001777 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001778
hualing chen5cbe1a62020-02-10 16:36:36 +08001779 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001780}
hualing chen040df222020-01-17 13:35:02 +08001781/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001782 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001783 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001784 * \retval DVR_SUCCESS On success
1785 * \return Error code
1786 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001787int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001788 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001789 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001790 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001791 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001792 return DVR_FAILURE;
1793 }
1794
hualing chencc91e1c2020-02-28 13:26:17 +08001795 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001796 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001797 return DVR_FAILURE;
1798 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001799 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001800 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001801 DVR_PlaybackSegmentInfo_t *segment = NULL;
1802 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1803 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001804 {
hualing chen040df222020-01-17 13:35:02 +08001805 if (segment->segment_id == segment_id) {
1806 list_del(&segment->head);
1807 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001808 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001809 }
hualing chen86e7d482020-01-16 15:13:33 +08001810 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001811 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001812 pthread_mutex_unlock(&player->lock);
1813
1814 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001815}
hualing chen040df222020-01-17 13:35:02 +08001816/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001817 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001818 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001819 * \retval DVR_SUCCESS On success
1820 * \return Error code
1821 */
hualing chen040df222020-01-17 13:35:02 +08001822int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001823 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001824 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001825 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001826 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001827 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001828 return DVR_FAILURE;
1829 }
hualing chenf43b8ba2020-07-28 13:11:42 +08001830 if (player->vendor == DVR_PLAYBACK_VENDOR_AML) {
1831 DVR_PB_DG(1, "vendor is amlogic. not hide or show av and update segment");
1832 return DVR_SUCCESS;
1833 }
hualing chena540a7e2020-03-27 16:44:05 +08001834
hualing chen040df222020-01-17 13:35:02 +08001835 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001836 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001837 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001838 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001839 {
hualing chen040df222020-01-17 13:35:02 +08001840 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08001841 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001842 }
hualing chen86e7d482020-01-16 15:13:33 +08001843 // if encramble to free, only set flag and return;
1844
1845 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001846 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001847 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1848 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001849 //disable display, mute
hualing chen703f3572021-01-06 12:51:34 +08001850 DVR_PB_DG(1, "mute av");
hualing chen2aba4022020-03-02 13:49:55 +08001851 AmTsPlayer_hideVideo(player->handle);
1852 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001853 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1854 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001855 //enable display, unmute
hualing chen703f3572021-01-06 12:51:34 +08001856 DVR_PB_DG(1, "unmute av");
hualing chen2aba4022020-03-02 13:49:55 +08001857 AmTsPlayer_showVideo(player->handle);
1858 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001859 } else {
1860 //do nothing
1861 }
1862 } else {
1863 //do nothing
1864 }
1865 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001866 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001867 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001868 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001869 pthread_mutex_unlock(&player->lock);
1870 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001871}
1872
1873
hualing chen275379e2021-06-15 17:57:21 +08001874static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_PlaybackPids_t set_pids, int type) {
hualing chen040df222020-01-17 13:35:02 +08001875 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen275379e2021-06-15 17:57:21 +08001876 DVR_StreamInfo_t set_pid;
1877
1878 if (type == 0) {
1879 set_pid = set_pids.video;
1880 } else if (type == 1) {
1881 set_pid = set_pids.audio;
1882 } else if (type == 2) {
1883 set_pid = set_pids.ad;
1884 } else {
1885 set_pid = set_pids.pcr;
1886 }
1887
hualing chena540a7e2020-03-27 16:44:05 +08001888 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001889 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001890 return DVR_FAILURE;
1891 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001892 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001893 if (now_pid.pid == set_pid.pid) {
1894 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001895 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001896 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001897 if (VALID_PID(now_pid.pid)) {
1898 //stop now stream
1899 if (type == 0) {
1900 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001901 if (player->has_video == DVR_TRUE) {
1902 DVR_PB_DG(1, "stop video");
1903 AmTsPlayer_stopVideoDecoding(player->handle);
1904 player->has_video = DVR_FALSE;
1905 }
hualing chen86e7d482020-01-16 15:13:33 +08001906 } else if (type == 1) {
1907 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001908 if (player->has_audio == DVR_TRUE) {
1909 DVR_PB_DG(1, "stop audio");
1910 AmTsPlayer_stopAudioDecoding(player->handle);
1911 player->has_audio = DVR_FALSE;
1912 }
hualing chen86e7d482020-01-16 15:13:33 +08001913 } else if (type == 2) {
1914 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001915 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001916 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001917 } else if (type == 3) {
1918 //pcr
1919 }
1920 }
1921 if (VALID_PID(set_pid.pid)) {
1922 //start
1923 if (type == 0) {
1924 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001925 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001926 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001927 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001928 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001929 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001930 AmTsPlayer_setVideoParams(player->handle, &vparams);
1931 AmTsPlayer_startVideoDecoding(player->handle);
1932 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001933 } else if (type == 1) {
1934 //start audio
Gong Ke2a0ebbe2021-05-25 15:22:50 +08001935 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chen275379e2021-06-15 17:57:21 +08001936 if (VALID_PID(set_pids.ad.pid)) {
1937 am_tsplayer_audio_params adparams;
1938 adparams.pid = set_pids.ad.pid;
1939 adparams.codectype= _dvr_convert_stream_fmt(set_pids.ad.format, DVR_TRUE);
1940 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",adparams.pid, adparams.codectype);
1941 AmTsPlayer_setADParams(player->handle, &adparams);
1942 AmTsPlayer_enableADMix(player->handle);
1943 }
1944
hualing chenc70a8df2020-05-12 19:23:11 +08001945 am_tsplayer_audio_params aparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001946
1947 memset(&aparams, 0, sizeof(aparams));
1948
hualing chenc70a8df2020-05-12 19:23:11 +08001949 aparams.pid = set_pid.pid;
1950 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1951 player->has_audio = DVR_TRUE;
1952 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1953 AmTsPlayer_setAudioParams(player->handle, &aparams);
1954 AmTsPlayer_startAudioDecoding(player->handle);
1955 //playback_device_audio_start(player->handle,&aparams);
1956 }
hualing chen86e7d482020-01-16 15:13:33 +08001957 } else if (type == 2) {
Gong Ke2a0ebbe2021-05-25 15:22:50 +08001958 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
hualing chenc70a8df2020-05-12 19:23:11 +08001959 am_tsplayer_audio_params aparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08001960
1961 memset(&aparams, 0, sizeof(aparams));
hualing chenc70a8df2020-05-12 19:23:11 +08001962 aparams.pid = set_pid.pid;
1963 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1964 player->has_audio = DVR_TRUE;
1965 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1966 AmTsPlayer_setADParams(player->handle, &aparams);
1967 AmTsPlayer_enableADMix(player->handle);
1968 //playback_device_audio_start(player->handle,&aparams);
1969 }
hualing chen86e7d482020-01-16 15:13:33 +08001970 } else if (type == 3) {
1971 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001972 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001973 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001974 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001975 //audio and video all close
1976 if (!player->has_audio && !player->has_video) {
1977 player->state = DVR_PLAYBACK_STATE_STOP;
1978 }
hualing chen86e7d482020-01-16 15:13:33 +08001979 }
1980 }
1981 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001982}
hualing chen5cbe1a62020-02-10 16:36:36 +08001983/**\brief dvr play back update segment pids
1984 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001985 * add pid stream and stop remove pid stream.
1986 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001987 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001988 * \retval DVR_SUCCESS On success
1989 * \return Error code
1990 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001991int 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 +08001992 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001993 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001994 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001995 return DVR_FAILURE;
1996 }
1997
hualing chen040df222020-01-17 13:35:02 +08001998 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001999 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002000 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002001 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 +08002002
hualing chen040df222020-01-17 13:35:02 +08002003 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002004 {
hualing chen040df222020-01-17 13:35:02 +08002005 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002006
2007 if (player->cur_segment_id == segment_id) {
2008 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
2009 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
2010 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002011 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08002012 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002013 return 0;
2014 }
2015
2016 //if segment is on going segment,we need stop start stream
2017 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08002018 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002019 //check video pids, stop or restart
hualing chen275379e2021-06-15 17:57:21 +08002020 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, *p_pids, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08002021 //check sub audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +08002022 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, *p_pids, 2);
jianchuan.pinge5f8c5a2021-02-03 19:11:05 +08002023 //check audio pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +08002024 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, *p_pids, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002025 //check pcr pids stop or restart
hualing chen275379e2021-06-15 17:57:21 +08002026 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, *p_pids, 3);
hualing chen8a657f32021-08-30 13:12:49 +08002027 if (segment->pids.audio.pid != p_pids->audio.pid &&
2028 segment->pids.audio.pid == 0x1fff) {
2029 if (player->need_seek_start == DVR_TRUE) {
2030 player->need_seek_start = DVR_FALSE;
2031 pthread_mutex_lock(&player->segment_lock);
2032 player->drop_ts = DVR_TRUE;
2033 player->ts_cache_len = 0;
2034 if (player->first_start_time > 0)
2035 player->first_start_time = player->first_start_time - 1;
2036 segment_seek(player->r_handle, (uint64_t)(player->first_start_time), player->openParams.block_size);
2037 DVR_PB_DG(0, "update need seek time_offset %llu", player->first_start_time);
2038 pthread_mutex_unlock(&player->segment_lock);
2039 }
2040 }
hualing chencc91e1c2020-02-28 13:26:17 +08002041 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002042 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
2043 //if state is pause, we need process at resume api. we only record change info
2044 int v_cmd = DVR_PLAYBACK_CMD_NONE;
2045 int a_cmd = DVR_PLAYBACK_CMD_NONE;
2046 if (VALID_PID(segment->pids.video.pid)
2047 && VALID_PID(p_pids->video.pid)
2048 && segment->pids.video.pid != p_pids->video.pid) {
2049 //restart video
2050 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
2051 }
2052 if (!VALID_PID(segment->pids.video.pid)
2053 && VALID_PID(p_pids->video.pid)
2054 && segment->pids.video.pid != p_pids->video.pid) {
2055 //start video
2056 v_cmd = DVR_PLAYBACK_CMD_VSTART;
2057 }
2058 if (VALID_PID(segment->pids.video.pid)
2059 && !VALID_PID(p_pids->video.pid)
2060 && segment->pids.video.pid != p_pids->video.pid) {
2061 //stop video
2062 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
2063 }
2064 if (VALID_PID(segment->pids.audio.pid)
2065 && VALID_PID(p_pids->audio.pid)
2066 && segment->pids.audio.pid != p_pids->audio.pid) {
2067 //restart audio
2068 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
2069 }
2070 if (!VALID_PID(segment->pids.audio.pid)
2071 && VALID_PID(p_pids->audio.pid)
2072 && segment->pids.audio.pid != p_pids->audio.pid) {
2073 //start audio
2074 a_cmd = DVR_PLAYBACK_CMD_ASTART;
2075 }
2076 if (VALID_PID(segment->pids.audio.pid)
2077 && !VALID_PID(p_pids->audio.pid)
2078 && segment->pids.audio.pid != p_pids->audio.pid) {
2079 //stop audio
2080 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
2081 }
2082 if (a_cmd == DVR_PLAYBACK_CMD_NONE
2083 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
2084 //do nothing
2085 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
2086 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
2087 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2088 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
2089 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
2090 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
2091 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
2092 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
2093 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2094 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
2095 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
2096 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2097 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2098 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
2099 } else {
2100 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2101 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
2102 }
2103
2104 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
2105 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
2106 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2107 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
2108 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
2109 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2110 //not occur this case
2111 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2112 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
2113 } else {
2114 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2115 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
2116 }
2117
2118 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
2119 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
2120 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2121 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
2122 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
2123 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
2124 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2125 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
2126 } else {
2127 //not occur this case
2128 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
2129 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2130 }
2131 }
2132 }
hualing chene10666f2020-04-14 13:58:37 +08002133 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08002134 }
hualing chen86e7d482020-01-16 15:13:33 +08002135 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08002136 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08002137 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08002138 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08002139 break;
hualing chenb31a6c62020-01-13 17:27:00 +08002140 }
hualing chen86e7d482020-01-16 15:13:33 +08002141 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002142 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002143 pthread_mutex_unlock(&player->lock);
2144 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002145}
2146/**\brief Stop play, will stop video and audio
2147 * \param[in] handle playback handle
2148 * \param[in] clear is clear last frame
2149 * \retval DVR_SUCCESS On success
2150 * \return Error code
2151 */
hualing chen040df222020-01-17 13:35:02 +08002152int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
2153 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002154 UNDVRUSED(clear);
hualing chena540a7e2020-03-27 16:44:05 +08002155 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002156 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002157 return DVR_FAILURE;
2158 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002159 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2160 DVR_PB_DG(1, ":playback is stoped");
2161 return DVR_SUCCESS;
2162 }
Ke Gong3c0caba2020-04-21 22:58:18 -07002163 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2164 DVR_PB_DG(1, ":playback is stoped");
2165 return DVR_SUCCESS;
2166 }
hualing chen87072a82020-03-12 16:20:12 +08002167 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08002168 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002169 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08002170 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08002171 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002172 if (player->has_video) {
2173 AmTsPlayer_resumeVideoDecoding(player->handle);
2174 }
2175 if (player->has_audio) {
2176 AmTsPlayer_resumeAudioDecoding(player->handle);
2177 }
2178 if (player->has_video) {
2179 player->has_video = DVR_FALSE;
hualing chen10cdb162021-02-05 10:44:41 +08002180 AmTsPlayer_hideVideo(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002181 AmTsPlayer_stopVideoDecoding(player->handle);
2182 }
2183 if (player->has_audio) {
2184 player->has_audio = DVR_FALSE;
2185 AmTsPlayer_stopAudioDecoding(player->handle);
2186 }
hualing chendf118dd2020-05-21 15:49:11 +08002187 if (player->has_ad_audio) {
2188 player->has_ad_audio =DVR_FALSE;
2189 AmTsPlayer_disableADMix(player->handle);
2190 }
hualing chen266b9502020-04-04 17:39:39 +08002191
hualing chen86e7d482020-01-16 15:13:33 +08002192 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002193 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
2194 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2195 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08002196 player->cur_segment_id = UINT64_MAX;
2197 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002198 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08002199 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08002200 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08002201 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002202}
2203/**\brief Start play audio
2204 * \param[in] handle playback handle
2205 * \param[in] params audio playback params,contains fmt and pid...
2206 * \retval DVR_SUCCESS On success
2207 * \return Error code
2208 */
hualing chen2aba4022020-03-02 13:49:55 +08002209
hualing chendf118dd2020-05-21 15:49:11 +08002210int 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 +08002211 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002212
2213 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002214 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002215 return DVR_FAILURE;
2216 }
hualing chen86e7d482020-01-16 15:13:33 +08002217 _start_playback_thread(handle);
2218 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002219 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002220 pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08002221
hualing chendf118dd2020-05-21 15:49:11 +08002222 if (VALID_PID(adparam->pid)) {
2223 player->has_ad_audio = DVR_TRUE;
2224 DVR_PB_DG(1, "start ad audio");
2225 AmTsPlayer_setADParams(player->handle, adparam);
2226 AmTsPlayer_enableADMix(player->handle);
2227 }
hualing chen969fe7b2021-05-26 15:13:17 +08002228 if (VALID_PID(param->pid)) {
2229 DVR_PB_DG(1, "start audio");
2230 player->has_audio = DVR_TRUE;
2231 AmTsPlayer_setAudioParams(player->handle, param);
2232 AmTsPlayer_startAudioDecoding(player->handle);
2233 }
hualing chendf118dd2020-05-21 15:49:11 +08002234
hualing chen86e7d482020-01-16 15:13:33 +08002235 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002236 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
2237 player->cmd.state = DVR_PLAYBACK_STATE_START;
2238 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002239 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002240 pthread_mutex_unlock(&player->lock);
2241 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002242}
2243/**\brief Stop play audio
2244 * \param[in] handle playback handle
2245 * \retval DVR_SUCCESS On success
2246 * \return Error code
2247 */
hualing chen040df222020-01-17 13:35:02 +08002248int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
2249 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002250
2251 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002252 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002253 return DVR_FAILURE;
2254 }
2255
hualing chen2aba4022020-03-02 13:49:55 +08002256 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08002257 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002258 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2259 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002260 //destory thread
2261 _stop_playback_thread(handle);
2262 } else {
2263 //do nothing.video is playing
2264 }
hualing chen7a56cba2020-04-14 14:09:27 +08002265 DVR_PB_DG(1, "lock");
2266 pthread_mutex_lock(&player->lock);
2267
hualing chenf00cdc82020-06-10 14:23:35 +08002268 if (player->has_audio) {
hualing chendf118dd2020-05-21 15:49:11 +08002269 player->has_audio = DVR_FALSE;
2270 AmTsPlayer_stopAudioDecoding(player->handle);
2271 }
hualing chen87072a82020-03-12 16:20:12 +08002272
hualing chendf118dd2020-05-21 15:49:11 +08002273 if (player->has_ad_audio) {
2274 player->has_ad_audio =DVR_FALSE;
2275 AmTsPlayer_disableADMix(player->handle);
2276 }
2277
hualing chen87072a82020-03-12 16:20:12 +08002278 player->cmd.last_cmd = player->cmd.cur_cmd;
2279 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
2280
hualing chen4b7c15d2020-04-07 16:13:48 +08002281 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002282 pthread_mutex_unlock(&player->lock);
2283 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002284}
2285/**\brief Start play video
2286 * \param[in] handle playback handle
2287 * \param[in] params video playback params,contains fmt and pid...
2288 * \retval DVR_SUCCESS On success
2289 * \return Error code
2290 */
hualing chen2aba4022020-03-02 13:49:55 +08002291int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08002292 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002293
2294 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002295 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002296 return DVR_FAILURE;
2297 }
2298
hualing chen86e7d482020-01-16 15:13:33 +08002299 _start_playback_thread(handle);
2300 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08002301 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002302 pthread_mutex_lock(&player->lock);
2303 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08002304 AmTsPlayer_setVideoParams(player->handle, param);
2305 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002306
2307 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08002308 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08002309 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002310 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08002311 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2312 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08002313 }
2314 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08002315 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
2316 player->cmd.state = DVR_PLAYBACK_STATE_START;
2317 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08002318 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002319 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002320 return DVR_SUCCESS;
2321}
2322/**\brief Stop play video
2323 * \param[in] handle playback handle
2324 * \retval DVR_SUCCESS On success
2325 * \return Error code
2326 */
hualing chen040df222020-01-17 13:35:02 +08002327int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
2328 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002329
2330 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002331 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002332 return DVR_FAILURE;
2333 }
2334
hualing chen86e7d482020-01-16 15:13:33 +08002335 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08002336 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
2337 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08002338 //destory thread
2339 _stop_playback_thread(handle);
2340 } else {
2341 //do nothing.audio is playing
2342 }
hualing chen7a56cba2020-04-14 14:09:27 +08002343
2344 DVR_PB_DG(1, "lock");
2345 pthread_mutex_lock(&player->lock);
2346
hualing chen87072a82020-03-12 16:20:12 +08002347 player->has_video = DVR_FALSE;
2348
2349 AmTsPlayer_stopVideoDecoding(player->handle);
2350 //playback_device_video_stop(player->handle);
2351
2352 player->cmd.last_cmd = player->cmd.cur_cmd;
2353 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2354
hualing chen4b7c15d2020-04-07 16:13:48 +08002355 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002356 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002357 return DVR_SUCCESS;
2358}
2359/**\brief Pause play
2360 * \param[in] handle playback handle
2361 * \param[in] flush whether its internal buffers should be flushed
2362 * \retval DVR_SUCCESS On success
2363 * \return Error code
2364 */
hualing chen040df222020-01-17 13:35:02 +08002365int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2366 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002367 UNDVRUSED(flush);
hualing chena540a7e2020-03-27 16:44:05 +08002368 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002369 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002370 return DVR_FAILURE;
2371 }
hualing chenf00cdc82020-06-10 14:23:35 +08002372 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||player->state == DVR_PLAYBACK_STATE_STOP ) {
2373 DVR_PB_DG(1, "player state is [%d] pause or stop", player->state);
hualing chenbd977fd2020-06-29 19:14:18 +08002374 return DVR_SUCCESS;
hualing chenf00cdc82020-06-10 14:23:35 +08002375 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002376 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002377 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002378 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002379 if (player->has_video)
2380 AmTsPlayer_pauseVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002381 if (player->has_audio)
hualing chen266b9502020-04-04 17:39:39 +08002382 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002383
2384 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002385 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2386 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2387 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2388 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002389 } else {
2390 player->cmd.last_cmd = player->cmd.cur_cmd;
2391 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2392 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2393 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002394 }
hualing chen86e7d482020-01-16 15:13:33 +08002395 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002396 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002397
hualing chen86e7d482020-01-16 15:13:33 +08002398 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002399}
2400
hualing chen5cbe1a62020-02-10 16:36:36 +08002401//not add lock
2402static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2403{
2404 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2405
hualing chena540a7e2020-03-27 16:44:05 +08002406 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002407 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002408 return DVR_FAILURE;
2409 }
2410
hualing chen5cbe1a62020-02-10 16:36:36 +08002411 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002412 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002413 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002414 am_tsplayer_video_params vparams;
2415 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002416 am_tsplayer_audio_params adparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002417 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002418
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002419 memset(&vparams, 0, sizeof(vparams));
2420 memset(&aparams, 0, sizeof(aparams));
2421
hualing chendf118dd2020-05-21 15:49:11 +08002422 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002423 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002424 pthread_mutex_unlock(&player->lock);
2425
2426 switch (cmd) {
2427 case DVR_PLAYBACK_CMD_AVRESTART:
2428 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002429 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002430 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002431 break;
2432 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002433 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2434 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002435 break;
2436 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002437 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002438 break;
2439 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002440 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002441 break;
2442 case DVR_PLAYBACK_CMD_ARESTART:
2443 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002444 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002445 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002446 break;
2447 case DVR_PLAYBACK_CMD_ASTART:
hualing chendf118dd2020-05-21 15:49:11 +08002448 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002449 break;
2450 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002451 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002452 break;
2453 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002454 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2455 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2456 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002457 break;
2458 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002459 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2460 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002461 break;
2462 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002463 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2464 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chendf118dd2020-05-21 15:49:11 +08002465 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002466 break;
2467 case DVR_PLAYBACK_CMD_STOP:
2468 break;
2469 case DVR_PLAYBACK_CMD_START:
2470 break;
2471 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002472 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2473 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002474 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002475 break;
2476 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002477 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2478 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chendf118dd2020-05-21 15:49:11 +08002479 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002480 break;
2481 case DVR_PLAYBACK_CMD_FF:
2482 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002483 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002484 break;
2485 default:
2486 break;
2487 }
2488 return DVR_SUCCESS;
2489}
2490
2491/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002492 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002493 * \retval DVR_SUCCESS On success
2494 * \return Error code
2495 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002496int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2497 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08002498 uint32_t pos = 0;
hualing chen03fd4942021-07-15 15:56:41 +08002499 uint64_t segmentid = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002500 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002501 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002502 return DVR_FAILURE;
2503 }
2504
hualing chena991aa82021-08-16 10:21:15 +08002505 if (dvr_playback_check_limit(handle)) {
2506 //get id and pos to check if we can seek to this pos
hualing chen7ea70a72021-09-09 11:25:13 +08002507 DVR_PB_DG(1, "player start calculate time");
hualing chena991aa82021-08-16 10:21:15 +08002508 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
2509 if (segmentid != player->cur_segment_id ||
2510 (segmentid == player->cur_segment_id &&
2511 pos > _dvr_get_cur_time(handle))) {
2512 //first to seek new pos and to resume
2513 DVR_PB_DG(1, "seek new pos and to resume");
2514 dvr_playback_seek(handle, segmentid, pos);
2515 }
hualing chen7ea70a72021-09-09 11:25:13 +08002516 } else {
2517 DVR_PB_DG(1, "player is not set limit");
hualing chen03fd4942021-07-15 15:56:41 +08002518 }
hualing chena991aa82021-08-16 10:21:15 +08002519
hualing chen5cbe1a62020-02-10 16:36:36 +08002520 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002521 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002522 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002523 player->first_frame = 0;
2524 if (player->has_video)
2525 AmTsPlayer_pauseVideoDecoding(player->handle);
2526 if (player->has_audio)
2527 AmTsPlayer_pauseAudioDecoding(player->handle);
2528
hualing chen266b9502020-04-04 17:39:39 +08002529 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002530 DVR_PB_DG(1, "dvr_playback_resume set trick mode none");
hualing chen266b9502020-04-04 17:39:39 +08002531 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2532 AmTsPlayer_resumeVideoDecoding(player->handle);
2533 }
2534 if (player->has_audio) {
2535 AmTsPlayer_resumeAudioDecoding(player->handle);
2536 }
2537 //check is has audio param,if has audio .we need start audio,
2538 //we will stop audio when ff fb, if reach end, we will pause.so we need
2539 //start audio when resume play
2540
2541 am_tsplayer_video_params vparams;
2542 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002543 am_tsplayer_audio_params adparams;
hualing chen266b9502020-04-04 17:39:39 +08002544 uint64_t segmentid = player->cur_segment_id;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002545
2546 memset(&vparams, 0, sizeof(vparams));
2547 memset(&aparams, 0, sizeof(aparams));
hualing chendf118dd2020-05-21 15:49:11 +08002548 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams, &adparams);
hualing chen266b9502020-04-04 17:39:39 +08002549 //valid audio pid, start audio
hualing chen969fe7b2021-05-26 15:13:17 +08002550 if (player->has_ad_audio == DVR_FALSE && VALID_PID(adparams.pid) && (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
2551 player->has_ad_audio = DVR_TRUE;
2552 DVR_PB_DG(1, "start ad audio");
2553 AmTsPlayer_setADParams(player->handle, &adparams);
2554 AmTsPlayer_enableADMix(player->handle);
2555 }
2556
hualing chenc70a8df2020-05-12 19:23:11 +08002557 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 +08002558 player->has_audio = DVR_TRUE;
2559 AmTsPlayer_setAudioParams(player->handle, &aparams);
2560 AmTsPlayer_startAudioDecoding(player->handle);
2561 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002562 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 +08002563 }
hualing chendf118dd2020-05-21 15:49:11 +08002564
hualing chen87072a82020-03-12 16:20:12 +08002565 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2566 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2567 player->cmd.state = DVR_PLAYBACK_STATE_START;
2568 player->state = DVR_PLAYBACK_STATE_START;
2569 } else {
2570 player->cmd.last_cmd = player->cmd.cur_cmd;
2571 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2572 player->cmd.state = DVR_PLAYBACK_STATE_START;
2573 player->state = DVR_PLAYBACK_STATE_START;
2574 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002575 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002576 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002577 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
hualing chen1ffd85b2021-08-16 15:18:43 +08002578 player->first_frame = 0;
2579 if (player->has_video)
2580 AmTsPlayer_pauseVideoDecoding(player->handle);
2581 if (player->has_audio)
2582 AmTsPlayer_pauseAudioDecoding(player->handle);
2583
hualing chene41f4372020-06-06 16:29:17 +08002584 if (player->has_video) {
hualing chenf00cdc82020-06-10 14:23:35 +08002585 DVR_PB_DG(1, "dvr_playback_resume set trick mode none 1");
hualing chene41f4372020-06-06 16:29:17 +08002586 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen041c4092020-04-05 15:11:50 +08002587 AmTsPlayer_resumeVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002588 }
hualing chen041c4092020-04-05 15:11:50 +08002589 if (player->has_audio)
2590 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002591 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002592 player->cmd.state = DVR_PLAYBACK_STATE_START;
2593 player->state = DVR_PLAYBACK_STATE_START;
hualing chen9811b212020-10-29 11:21:44 +08002594 if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)
2595 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002596 } else {
2597 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2598 {
2599 pthread_mutex_lock(&player->lock);
hualing chen1ffd85b2021-08-16 15:18:43 +08002600 player->first_frame = 0;
2601 if (player->has_video)
2602 AmTsPlayer_pauseVideoDecoding(player->handle);
2603 if (player->has_audio)
2604 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen041c4092020-04-05 15:11:50 +08002605 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002606 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002607 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2608 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen05d09432021-01-25 15:26:55 +08002609 if (player->has_video) {
2610 AmTsPlayer_resumeVideoDecoding(player->handle);
2611 }
2612 if (player->has_audio)
2613 AmTsPlayer_resumeAudioDecoding(player->handle);
2614
hualing chen041c4092020-04-05 15:11:50 +08002615 pthread_mutex_unlock(&player->lock);
2616 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002617 }
2618 return DVR_SUCCESS;
2619}
2620
hualing chena540a7e2020-03-27 16:44:05 +08002621static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2622
2623 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2624 DVR_PlaybackSegmentInfo_t *segment = NULL;
2625 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2626 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2627
2628 list_for_each_entry(segment, &player->segment_list, head)
2629 {
2630 if (segment->segment_id == segment_id) {
2631 cur_segment = segment;
2632 }
2633 if (segment->segment_id == set_seg_id) {
2634 set_segment = segment;
2635 }
2636 if (cur_segment != NULL && set_segment != NULL) {
2637 break;
2638 }
2639 }
2640 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002641 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002642 return DVR_TRUE;
2643 }
2644 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2645 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2646 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2647 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002648 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 +08002649 return DVR_TRUE;
2650 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002651 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002652 return DVR_FALSE;
2653}
2654
hualing chen03fd4942021-07-15 15:56:41 +08002655/**\brief set limit
2656 * \param[in] handle playback handle
2657 * \param[in] rec start time ms
2658 * \param[in] rec limit time ms
2659 * \retval DVR_SUCCESS On success
2660 * \return Error code
2661 */
hualing chen7ea70a72021-09-09 11:25:13 +08002662int dvr_playback_setlimit(DVR_PlaybackHandle_t handle, uint32_t time, uint32_t limit)
hualing chen03fd4942021-07-15 15:56:41 +08002663{ DVR_Playback_t *player = (DVR_Playback_t *) handle;
2664
2665 if (player == NULL) {
2666 DVR_PB_DG(1, "player is NULL");
2667 return DVR_FAILURE;
2668 }
hualing chen7ea70a72021-09-09 11:25:13 +08002669 _dvr_getClock_sec();
2670 DVR_PB_DG(1, "time %lu limit: %u player->state:%d", time, limit, player->state);
hualing chen03fd4942021-07-15 15:56:41 +08002671 pthread_mutex_lock(&player->lock);
2672 player->rec_start = time;
2673 player->limit = limit;
2674 pthread_mutex_unlock(&player->lock);
2675 return DVR_SUCCESS;
2676}
2677
hualing chen5cbe1a62020-02-10 16:36:36 +08002678/**\brief seek
2679 * \param[in] handle playback handle
2680 * \param[in] time_offset time offset base cur segment
2681 * \retval DVR_SUCCESS On success
2682 * \return Error code
2683 */
hualing chencc91e1c2020-02-28 13:26:17 +08002684int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002685 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen03fd4942021-07-15 15:56:41 +08002686 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +08002687 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002688 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002689 return DVR_FAILURE;
2690 }
2691
hualing chen4b7c15d2020-04-07 16:13:48 +08002692 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 +08002693 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002694
hualing chen86e7d482020-01-16 15:13:33 +08002695 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002696 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002697 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002698
hualing chen5cbe1a62020-02-10 16:36:36 +08002699 //open segment if id is not current segment
hualing chen03fd4942021-07-15 15:56:41 +08002700 ret = _dvr_open_segment(handle, segment_id);
hualing chen87072a82020-03-12 16:20:12 +08002701 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002702 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002703 pthread_mutex_unlock(&player->lock);
2704 return DVR_FAILURE;
2705 }
2706 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2707 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002708 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002709 //pthread_mutex_unlock(&player->lock);
2710 //return DVR_SUCCESS;
2711 time_offset = _dvr_get_end_time(handle);
2712 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002713 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chene41f4372020-06-06 16:29:17 +08002714 time_offset = _dvr_get_end_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08002715 //pthread_mutex_unlock(&player->lock);
2716 //return DVR_FAILURE;
2717 ret = DVR_FAILURE;
hualing chen87072a82020-03-12 16:20:12 +08002718 }
2719 }
2720
hualing chen03fd4942021-07-15 15:56:41 +08002721 DVR_PB_DG(1, "seek open id[%lld]flag[0x%x] time_offset %u",
2722 player->cur_segment.segment_id,
2723 player->cur_segment.flags,
2724 time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08002725 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002726 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2727 //forward playback.not seek end of file
2728 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2729 //default -2000ms
2730 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2731 }
hualing chen86e7d482020-01-16 15:13:33 +08002732 }
hualing chen8a657f32021-08-30 13:12:49 +08002733 if (player->need_seek_start == DVR_TRUE) {
2734 player->first_start_time = (uint64_t)time_offset + 1;//set first start time not eq 0
2735 }
hualing chen2aba4022020-03-02 13:49:55 +08002736 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002737 player->drop_ts = DVR_TRUE;
hualing chen5605eed2020-05-26 18:18:06 +08002738 player->ts_cache_len = 0;
hualing chen266b9502020-04-04 17:39:39 +08002739 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002740 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 +08002741 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002742 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002743
hualing chen2aba4022020-03-02 13:49:55 +08002744 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002745
2746 player->last_send_time_id = UINT64_MAX;
hualing chen03fd4942021-07-15 15:56:41 +08002747 player->last_segment_tatol = 0LL;
2748 player->last_segment_id = 0LL;
hualing chen2aba4022020-03-02 13:49:55 +08002749 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002750 player->fffb_current = _dvr_time_getClock();
2751 player->fffb_start = player->fffb_current;
2752 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2753 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002754 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002755 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002756 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002757 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002758 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002759 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002760 }
hualing chen86e7d482020-01-16 15:13:33 +08002761 //stop play
hualing chen03fd4942021-07-15 15:56:41 +08002762 DVR_PB_DG(0, "seek stop play, not inject data has video[%d]audio[%d]",
2763 player->has_video, player->has_audio);
hualing chen1ffd85b2021-08-16 15:18:43 +08002764
hualing chen266b9502020-04-04 17:39:39 +08002765 if (player->has_video) {
hualing chen7e14e532021-09-23 11:23:28 +08002766 //player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002767 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002768 }
2769
2770 if (player->has_audio) {
2771 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002772 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002773 }
hualing chendf118dd2020-05-21 15:49:11 +08002774 if (player->has_ad_audio) {
2775 player->has_ad_audio =DVR_FALSE;
2776 AmTsPlayer_disableADMix(player->handle);
2777 }
2778
hualing chen86e7d482020-01-16 15:13:33 +08002779 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002780 am_tsplayer_video_params vparams;
2781 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08002782 am_tsplayer_audio_params adparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002783
jiangfei.hanb8fbad42021-07-29 15:04:48 +08002784 memset(&vparams, 0, sizeof(vparams));
2785 memset(&aparams, 0, sizeof(aparams));
2786
hualing chen040df222020-01-17 13:35:02 +08002787 player->cur_segment_id = segment_id;
2788
2789 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002790 //get segment info and audio video pid fmt ;
hualing chendf118dd2020-05-21 15:49:11 +08002791 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen86e7d482020-01-16 15:13:33 +08002792 //start audio and video
Zhiqiang Han9adc9722020-11-11 18:38:10 +08002793 if (vparams.pid != 0x2fff && !VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen86e7d482020-01-16 15:13:33 +08002794 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002795 DVR_PB_DG(0, "unlock seek start dvr play back start error, not found audio and video info");
hualing chen86e7d482020-01-16 15:13:33 +08002796 pthread_mutex_unlock(&player->lock);
2797 return -1;
2798 }
2799 //add
hualing chen040df222020-01-17 13:35:02 +08002800 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002801 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002802 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002803 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chene41f4372020-06-06 16:29:17 +08002804 player->state == DVR_PLAYBACK_STATE_PAUSE ||
hualing chendf118dd2020-05-21 15:49:11 +08002805 player->speed > 2.0f||
hualing chen31140872020-03-25 12:29:26 +08002806 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002807 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002808 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002809 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002810 }
hualing chen2aba4022020-03-02 13:49:55 +08002811 AmTsPlayer_setVideoParams(player->handle, &vparams);
2812 AmTsPlayer_startVideoDecoding(player->handle);
hualing chene41f4372020-06-06 16:29:17 +08002813 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed) &&
2814 player->cmd.speed.speed.speed != PLAYBACK_SPEED_X1) {
2815 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
2816 } else if (player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1) {
2817 AmTsPlayer_stopFast(player->handle);
2818 }
hualing chen266b9502020-04-04 17:39:39 +08002819 player->has_video = DVR_TRUE;
hualing chen7e14e532021-09-23 11:23:28 +08002820 } else {
2821 player->has_video = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08002822 }
hualing chene41f4372020-06-06 16:29:17 +08002823 if (VALID_PID(adparams.pid) && player->speed == 1.0) {
hualing chendf118dd2020-05-21 15:49:11 +08002824 player->has_ad_audio = DVR_TRUE;
2825 DVR_PB_DG(1, "start ad audio");
2826 AmTsPlayer_setADParams(player->handle, &adparams);
2827 AmTsPlayer_enableADMix(player->handle);
2828 }
hualing chen969fe7b2021-05-26 15:13:17 +08002829 if (VALID_PID(aparams.pid) && player->speed == 1.0) {
2830 DVR_PB_DG(1, "start audio seek");
2831 AmTsPlayer_setAudioParams(player->handle, &aparams);
2832 AmTsPlayer_startAudioDecoding(player->handle);
2833 player->has_audio = DVR_TRUE;
2834 }
hualing chen86e7d482020-01-16 15:13:33 +08002835 }
hualing chen1ffd85b2021-08-16 15:18:43 +08002836 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen2aba4022020-03-02 13:49:55 +08002837 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2838 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen30423862021-04-16 14:39:12 +08002839 player->seek_pause = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002840 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002841 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2842 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002843 player->speed > 1.0f||
2844 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002845 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002846 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002847 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002848 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002849 player->cmd.last_cmd = player->cmd.cur_cmd;
2850 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2851 player->cmd.state = DVR_PLAYBACK_STATE_START;
2852 player->state = DVR_PLAYBACK_STATE_START;
2853 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002854 player->last_send_time_id = UINT64_MAX;
2855 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002856 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002857
2858 return DVR_SUCCESS;
2859}
hualing chen5cbe1a62020-02-10 16:36:36 +08002860
hualing chen5cbe1a62020-02-10 16:36:36 +08002861static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2862 //get cur time of segment
2863 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002864
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002865 if (player == NULL || player->handle == (am_tsplayer_handle)NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002866 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002867 return DVR_FAILURE;
2868 }
2869
hualing chen31140872020-03-25 12:29:26 +08002870 int64_t cache = 0;//defalut es buf cache 500ms
2871 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002872 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08002873 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2874 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
hualing chen2aba4022020-03-02 13:49:55 +08002875 pthread_mutex_unlock(&player->segment_lock);
hualing chenfbf8e022020-06-15 13:43:11 +08002876 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 +08002877 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2878 cache = 0;
2879 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002880 int cur_time = (int)(cur > cache ? cur - cache : 0);
2881 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002882}
2883
hualing chen969fe7b2021-05-26 15:13:17 +08002884static int _dvr_get_play_cur_time(DVR_PlaybackHandle_t handle, uint64_t *id) {
2885 //get cur time of segment
2886 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2887
hualing chen03fd4942021-07-15 15:56:41 +08002888 if (player == NULL || player->handle == 0) {
hualing chen969fe7b2021-05-26 15:13:17 +08002889 DVR_PB_DG(1, "player is NULL");
2890 return DVR_FAILURE;
2891 }
2892
2893 int64_t cache = 0;//defalut es buf cache 500ms
2894 int cur_time = 0;
2895 AmTsPlayer_getDelayTime(player->handle, &cache);
2896 pthread_mutex_lock(&player->segment_lock);
2897 loff_t pos = segment_tell_position(player->r_handle) -player->ts_cache_len;
2898 uint64_t cur = segment_tell_position_time(player->r_handle, pos);
2899 pthread_mutex_unlock(&player->segment_lock);
2900 DVR_PB_DG(1, "get play cur time [%lld] cache:%lld cur id [%lld]last id [%lld] pb cache len [%d] [%lld]", cur, cache, player->cur_segment_id,player->last_send_time_id, player->ts_cache_len, pos);
2901 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2902 cache = 0;
2903 }
2904 if (cur > cache) {
2905 cur_time = (int)(cur - cache);
2906 *id = player->cur_segment_id;
2907 } else if (player->last_segment_tatol > 0) {
hualing chen8a657f32021-08-30 13:12:49 +08002908
2909 if (player->last_segment_tatol > (cache - cur))
2910 cur_time = (int)(player->last_segment_tatol - (cache - cur));
2911 else
2912 cur_time = (int)(player->last_segment_tatol - cur);
2913
hualing chen969fe7b2021-05-26 15:13:17 +08002914 *id = player->last_segment_id;
2915 DVR_PB_DG(1, "get play cur time[%lld][%lld][%d]", player->last_segment_id, player->cur_segment_id, player->last_segment_tatol);
2916 } else {
2917 cur_time = 0;
2918 *id = player->cur_segment_id;
2919 }
2920
2921 return cur_time;
2922}
2923
hualing chencc91e1c2020-02-28 13:26:17 +08002924//get current segment current pcr time of read pos
2925static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2926 //get cur time of segment
2927 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002928
2929 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002930 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002931 return DVR_FAILURE;
2932 }
2933
hualing chen2aba4022020-03-02 13:49:55 +08002934 pthread_mutex_lock(&player->segment_lock);
2935 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002936 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002937 pthread_mutex_unlock(&player->segment_lock);
2938 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002939}
2940
hualing chen03fd4942021-07-15 15:56:41 +08002941DVR_Bool_t dvr_playback_check_limit(DVR_PlaybackHandle_t handle)
2942{
2943 //check is set limit info
2944 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2945
2946 if (player == NULL) {
2947 DVR_PB_DG(1, "player is NULL");
2948 return DVR_FALSE;
2949 }
2950 if (player->rec_start > 0 || player->limit > 0) {
2951 return DVR_TRUE;
2952 }
2953 return DVR_FALSE;
2954}
2955
2956/**\brief set DVR playback calculate expired time len
2957 * \param[in] handle, DVR playback session handle
2958 * \return DVR_SUCCESS on success
2959 * \return error code on failure
2960 */
hualing chen7ea70a72021-09-09 11:25:13 +08002961uint32_t dvr_playback_calculate_expiredlen(DVR_PlaybackHandle_t handle)
hualing chen03fd4942021-07-15 15:56:41 +08002962{
2963 //calculate expired time to play
2964 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen7ea70a72021-09-09 11:25:13 +08002965 uint32_t cur_time;
2966 uint32_t tmp_time;
2967 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08002968 if (player == NULL) {
2969 DVR_PB_DG(1, "player is NULL");
2970 return expired;
2971 }
hualing chen7ea70a72021-09-09 11:25:13 +08002972 if (player->rec_start == 0 || player->limit == 0) {
2973 DVR_PB_DG(1, "rec limit 0");
hualing chen03fd4942021-07-15 15:56:41 +08002974 return expired;
2975 }
2976 //get system time
hualing chen7ea70a72021-09-09 11:25:13 +08002977 cur_time = _dvr_getClock_sec();
2978 if ((cur_time - player->rec_start) > player->limit) {
2979 tmp_time = (uint32_t)((cur_time - player->rec_start) - player->limit) * 1000U;
2980 expired = *(int*)&tmp_time;
2981 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 +08002982 cur_time,
2983 player->rec_start,
2984 player->limit,
hualing chen7ea70a72021-09-09 11:25:13 +08002985 (uint32_t)(cur_time - player->rec_start - player->limit), expired, tmp_time);
2986 }
hualing chen03fd4942021-07-15 15:56:41 +08002987 return expired;
2988}
2989
2990/**\brief set DVR playback obsolete time
2991 * \param[in] handle, DVR playback session handle
2992 * \param[in] obsolete, obsolete len
2993 * \return DVR_SUCCESS on success
2994 * \return error code on failure
2995 */
2996int dvr_playback_set_obsolete(DVR_PlaybackHandle_t handle, int obsolete)
2997{
2998 int expired = 0;
2999 //calculate expired time to play
3000 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3001
3002 if (player == NULL) {
3003 DVR_PB_DG(1, "player is NULL");
3004 return DVR_FALSE;
3005 }
3006 //get system time
3007 pthread_mutex_lock(&player->lock);
3008 player->obsolete = obsolete;
3009 pthread_mutex_unlock(&player->lock);
3010 return expired;
3011}
3012
3013/**\brief update DVR playback newest segment duration
3014 * \param[in] handle, DVR playback session handle
3015 * \param[in] segmentid, newest segment id
3016 * \param[in] dur dur time ms
3017 * \return DVR_SUCCESS on success
3018 * \return error code on failure
3019 */
3020int dvr_playback_update_duration(DVR_PlaybackHandle_t handle,
3021uint64_t segmentid, int dur)
3022{
3023 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3024 DVR_PlaybackSegmentInfo_t *segment;
3025 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
3026
3027 if (player == NULL) {
3028 DVR_PB_DG(1, " player is NULL");
3029 return DVR_FAILURE;
3030 }
3031 //update the newest segment duration on timeshift mode
3032 list_for_each_entry(segment, &player->segment_list, head)
3033 {
3034 if (segment->segment_id == segmentid) {
3035 segment->duration = dur;
3036 break;
3037 }
3038 pre_segment = segment;
3039 }
3040
3041 return DVR_SUCCESS;
3042}
3043
hualing chen7ea70a72021-09-09 11:25:13 +08003044static uint32_t dvr_playback_calculate_last_valid_segment(
3045 DVR_PlaybackHandle_t handle, uint64_t *segmentid, uint32_t *pos)
hualing chen03fd4942021-07-15 15:56:41 +08003046{
hualing chen7ea70a72021-09-09 11:25:13 +08003047 uint32_t off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003048 uint64_t segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003049 uint32_t pre_off = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003050 uint64_t last_segment_id = 0;
hualing chen7ea70a72021-09-09 11:25:13 +08003051 uint32_t expired = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003052 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3053
3054 if (player == NULL) {
3055 DVR_PB_DG(1, "player is NULL");
3056 return DVR_FAILURE;
3057 }
3058 expired = dvr_playback_calculate_expiredlen(handle);
hualing chen7e14e532021-09-23 11:23:28 +08003059 if (expired == 0) {
3060 *segmentid = player->cur_segment_id;
3061 *pos = 0;
3062 return DVR_SUCCESS;
3063 }
hualing chen03fd4942021-07-15 15:56:41 +08003064 DVR_PlaybackSegmentInfo_t *pseg;
3065 list_for_each_entry_reverse(pseg, &player->segment_list, head) {
3066 segment_id = pseg->segment_id;
3067
3068 if ((player->obsolete + pre_off + pseg->duration) > expired)
3069 break;
3070
3071 last_segment_id = pseg->segment_id;
3072 pre_off += pseg->duration;
3073 }
3074
3075 if (last_segment_id == segment_id) {
3076 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
3077 off = expired;
3078 } else if (player->obsolete >= expired) {
3079 off = 0;
3080 } else {
3081 off = expired - pre_off - player->obsolete;
3082 }
3083 *segmentid = segment_id;
3084 *pos = off;
3085 return DVR_SUCCESS;
3086}
3087
hualing chen4b7c15d2020-04-07 16:13:48 +08003088#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08003089//start replay
3090static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
3091
3092 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3093 //calculate pcr seek time
3094 int t_diff = 0;
3095 int seek_time = 0;
hualing chen03fd4942021-07-15 15:56:41 +08003096 uint64_t segmentid = 0;
3097 int pos = 0;
hualing chena540a7e2020-03-27 16:44:05 +08003098 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003099 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003100 return DVR_FAILURE;
3101 }
3102
hualing chen5cbe1a62020-02-10 16:36:36 +08003103 if (player->fffb_start == -1) {
3104 //set fffb start time ms
3105 player->fffb_start = _dvr_time_getClock();
3106 player->fffb_current = player->fffb_start;
3107 //get segment current time pos
3108 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen03fd4942021-07-15 15:56:41 +08003109 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]",
3110 player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003111 t_diff = 0;
hualing chene41f4372020-06-06 16:29:17 +08003112 //default first time 2s seek
hualing chen87072a82020-03-12 16:20:12 +08003113 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003114 } else {
3115 player->fffb_current = _dvr_time_getClock();
3116 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08003117 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08003118 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08003119 if (seek_time <= 0) {
3120 //need seek to pre one segment
3121 seek_time = 0;
3122 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003123 //seek segment pos
3124 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08003125 pthread_mutex_lock(&player->segment_lock);
hualing chen5605eed2020-05-26 18:18:06 +08003126 player->ts_cache_len = 0;
hualing chene41f4372020-06-06 16:29:17 +08003127 if (seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3128 //set seek time to 0;
hualing chen03fd4942021-07-15 15:56:41 +08003129 DVR_PB_DG(1, "segment seek to 0 at fb mode [%d]id[%lld]",
3130 seek_time,
3131 player->cur_segment_id);
hualing chene41f4372020-06-06 16:29:17 +08003132 seek_time = 0;
3133 }
hualing chen03fd4942021-07-15 15:56:41 +08003134 if (IS_FB(player->speed)
3135 && dvr_playback_check_limit(handle)) {
3136 //fb case.check expired time
3137 //get id and pos to check if we can seek to this pos
3138 dvr_playback_calculate_last_valid_segment(handle, &segmentid, &pos);
3139 //case cur id < segment id
3140 if (player->cur_segment_id < segmentid) {
3141 //expired ts data is player,return error
3142 //
3143 pthread_mutex_unlock(&player->segment_lock);
3144 return 0;
3145 } else if (player->cur_segment_id == segmentid) {
3146 //id is same,compare seek pos
3147 if (seek_time < pos) {
3148 //expired ts data is player,return error
3149 //
3150 pthread_mutex_unlock(&player->segment_lock);
3151 return 0;
3152 }
3153 }
3154 //case can play
3155 }
hualing chen041c4092020-04-05 15:11:50 +08003156 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
3157 seek_time = 0;
3158 }
hualing chen2aba4022020-03-02 13:49:55 +08003159 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003160 } else {
3161 //
hualing chen4b7c15d2020-04-07 16:13:48 +08003162 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08003163 }
hualing chen03fd4942021-07-15 15:56:41 +08003164 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]",
3165 seek_time,
3166 player->speed,
3167 player->cur_segment_id,
3168 _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08003169 }
hualing chen2aba4022020-03-02 13:49:55 +08003170 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08003171}
3172
3173
3174//start replay
3175static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
3176 //
3177 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003178
3179 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003180 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003181 return DVR_FAILURE;
3182 }
3183
hualing chen5cbe1a62020-02-10 16:36:36 +08003184 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003185 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003186 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08003187 AmTsPlayer_stopVideoDecoding(player->handle);
3188 }
3189 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003190 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08003191 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003192 AmTsPlayer_stopAudioDecoding(player->handle);
3193 }
hualing chendf118dd2020-05-21 15:49:11 +08003194 if (player->has_ad_audio) {
3195 DVR_PB_DG(1, "fffb stop audio");
3196 player->has_ad_audio =DVR_FALSE;
3197 AmTsPlayer_disableADMix(player->handle);
3198 }
hualing chen2aba4022020-03-02 13:49:55 +08003199
hualing chen5cbe1a62020-02-10 16:36:36 +08003200 //start video and audio
3201
hualing chen2aba4022020-03-02 13:49:55 +08003202 am_tsplayer_video_params vparams;
3203 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003204 am_tsplayer_audio_params adparams;
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003205
3206 memset(&vparams, 0, sizeof(vparams));
3207 memset(&aparams, 0, sizeof(aparams));
3208
hualing chen87072a82020-03-12 16:20:12 +08003209 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003210
3211 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08003212 //pthread_mutex_lock(&player->lock);
hualing chendf118dd2020-05-21 15:49:11 +08003213 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003214 //start audio and video
3215 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
3216 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003217 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08003218 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003219 return -1;
3220 }
3221
3222 if (VALID_PID(vparams.pid)) {
3223 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08003224 DVR_PB_DG(1, "fffb start video");
hualing chen0888c032020-12-18 17:54:57 +08003225 //DVR_PB_DG(1, "fffb start video and save last frame");
3226 //AmTsPlayer_setVideoBlackOut(player->handle, 0);
hualing chen31140872020-03-25 12:29:26 +08003227 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08003228 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
3229 AmTsPlayer_setVideoParams(player->handle, &vparams);
3230 AmTsPlayer_startVideoDecoding(player->handle);
3231 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003232 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08003233 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08003234 }
hualing chen31140872020-03-25 12:29:26 +08003235 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08003236 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003237 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08003238 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003239 return 0;
3240}
3241
3242static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
3243 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003244 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003245 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003246 return DVR_FAILURE;
3247 }
3248
3249 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08003250 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08003251 pthread_mutex_lock(&player->lock);
3252
hualing chen2aba4022020-03-02 13:49:55 +08003253 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003254 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 +08003255
hualing chen87072a82020-03-12 16:20:12 +08003256 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
3257 //seek time set 0
3258 seek_time = 0;
3259 }
hualing chen041c4092020-04-05 15:11:50 +08003260 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08003261 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
3262 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08003263 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003264 pthread_mutex_unlock(&player->lock);
3265 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003266 //send event here and pause
3267 DVR_Play_Notify_t notify;
3268 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08003269 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08003270 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08003271 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08003272 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 +08003273 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08003274 return DVR_SUCCESS;
3275 }
hualing chen2932d372020-04-29 13:44:00 +08003276 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08003277 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08003278 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08003279 }
3280 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08003281 _dvr_playback_fffb_replay(handle);
3282
3283 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003284 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08003285
hualing chen5cbe1a62020-02-10 16:36:36 +08003286 return DVR_SUCCESS;
3287}
3288
hualing chen87072a82020-03-12 16:20:12 +08003289//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08003290static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003291 //
3292 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003293
3294 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003295 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003296 return DVR_FAILURE;
3297 }
3298
hualing chen5cbe1a62020-02-10 16:36:36 +08003299 //stop
hualing chen2aba4022020-03-02 13:49:55 +08003300 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08003301 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003302 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003303 }
3304
3305 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08003306 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08003307 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08003308 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003309 //start video and audio
3310
hualing chen2aba4022020-03-02 13:49:55 +08003311 am_tsplayer_video_params vparams;
3312 am_tsplayer_audio_params aparams;
hualing chendf118dd2020-05-21 15:49:11 +08003313 am_tsplayer_audio_params adparams;
hualing chen87072a82020-03-12 16:20:12 +08003314 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08003315
jiangfei.hanb8fbad42021-07-29 15:04:48 +08003316 memset(&vparams, 0, sizeof(vparams));
3317 memset(&aparams, 0, sizeof(aparams));
3318
hualing chen5cbe1a62020-02-10 16:36:36 +08003319 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08003320 DVR_PB_DG(1, "into");
hualing chendf118dd2020-05-21 15:49:11 +08003321 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams, &adparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08003322 //start audio and video
3323 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08003324 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08003325 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08003326 return -1;
3327 }
3328
3329 if (VALID_PID(vparams.pid)) {
3330 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003331 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003332 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08003333 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08003334 }
hualing chen266b9502020-04-04 17:39:39 +08003335 else {
hualing chen2aba4022020-03-02 13:49:55 +08003336 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08003337 }
hualing chen2aba4022020-03-02 13:49:55 +08003338 AmTsPlayer_setVideoParams(player->handle, &vparams);
3339 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08003340 }
hualing chena540a7e2020-03-27 16:44:05 +08003341
3342 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08003343 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08003344 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003345 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08003346 } else {
hualing chendf118dd2020-05-21 15:49:11 +08003347 if (VALID_PID(adparams.pid)) {
3348 player->has_ad_audio = DVR_TRUE;
3349 DVR_PB_DG(1, "start ad audio");
3350 AmTsPlayer_setADParams(player->handle, &adparams);
3351 AmTsPlayer_enableADMix(player->handle);
3352 }
hualing chen969fe7b2021-05-26 15:13:17 +08003353 if (VALID_PID(aparams.pid)) {
3354 player->has_audio = DVR_TRUE;
3355 DVR_PB_DG(1, "start audio");
3356 AmTsPlayer_setAudioParams(player->handle, &aparams);
3357 AmTsPlayer_startAudioDecoding(player->handle);
3358 }
hualing chendf118dd2020-05-21 15:49:11 +08003359
hualing chen7a56cba2020-04-14 14:09:27 +08003360 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08003361 AmTsPlayer_stopFast(player->handle);
3362 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
3363 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
3364 }
hualing chen2aba4022020-03-02 13:49:55 +08003365 player->cmd.last_cmd = player->cmd.cur_cmd;
3366 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08003367 player->cmd.state = DVR_PLAYBACK_STATE_START;
3368 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08003369 return 0;
3370}
3371
3372
hualing chenb31a6c62020-01-13 17:27:00 +08003373/**\brief Set play speed
3374 * \param[in] handle playback handle
3375 * \param[in] speed playback speed
3376 * \retval DVR_SUCCESS On success
3377 * \return Error code
3378 */
hualing chen5cbe1a62020-02-10 16:36:36 +08003379int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
3380
3381 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08003382
3383 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003384 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003385 return DVR_FAILURE;
3386 }
3387
hualing chen4b7c15d2020-04-07 16:13:48 +08003388 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003389 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003390 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08003391 return DVR_FAILURE;
3392 }
hualing chenf00cdc82020-06-10 14:23:35 +08003393 if (speed.speed.speed == player->cmd.speed.speed.speed) {
3394 DVR_PB_DG(1, " func: eq speed [%d]", speed.speed.speed);
3395 return DVR_SUCCESS;
3396 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003397 pthread_mutex_lock(&player->lock);
3398 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
3399 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
3400 player->cmd.last_cmd = player->cmd.cur_cmd;
3401 }
hualing chene41f4372020-06-06 16:29:17 +08003402
hualing chen31140872020-03-25 12:29:26 +08003403 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chenf00cdc82020-06-10 14:23:35 +08003404 IS_KERNEL_SPEED(speed.speed.speed) ) {
3405 //case 1. not start play.only set speed
3406 if (player->state == DVR_PLAYBACK_STATE_STOP) {
3407 //only set speed.and return;
3408 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
3409 player->cmd.speed.speed = speed.speed;
3410 player->speed = (float)speed.speed.speed/(float)100;
3411 player->fffb_play = DVR_FALSE;
3412 pthread_mutex_unlock(&player->lock);
3413 return DVR_SUCCESS;
3414 }
3415 //case 2. cur speed is 100,set 200 50 25 12 .
hualing chena540a7e2020-03-27 16:44:05 +08003416 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
3417 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08003418 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003419 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3420 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003421 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003422 AmTsPlayer_stopFast(player->handle);
3423 pthread_mutex_unlock(&player->lock);
3424 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
3425 pthread_mutex_lock(&player->lock);
3426 } else {
3427 //set play speed and if audio is start, stop audio.
3428 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003429 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003430 AmTsPlayer_stopAudioDecoding(player->handle);
3431 player->has_audio = DVR_FALSE;
3432 }
hualing chenb96aa2c2020-04-15 14:13:53 +08003433 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003434 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08003435 }
hualing chenbcada022020-04-22 14:27:01 +08003436 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08003437 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003438 player->cmd.speed.speed = speed.speed;
3439 player->speed = (float)speed.speed.speed/(float)100;
3440 pthread_mutex_unlock(&player->lock);
3441 return DVR_SUCCESS;
3442 }
hualing chen31140872020-03-25 12:29:26 +08003443 //case 3 fffb mode
3444 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3445 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3446 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003447 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003448 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003449 player->cmd.speed.speed = speed.speed;
3450 player->speed = (float)speed.speed.speed/(float)100;
3451 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08003452 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003453 pthread_mutex_unlock(&player->lock);
3454 return DVR_SUCCESS;
3455 }
3456 }
3457 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08003458 IS_KERNEL_SPEED(speed.speed.speed)) {
3459 //case 1. cur speed is kernel support speed,set kernel speed.
3460 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08003461 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08003462 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
3463 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08003464 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003465 AmTsPlayer_stopFast(player->handle);
hualing chenf00cdc82020-06-10 14:23:35 +08003466 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
hualing chen2bd8a7a2020-04-02 11:31:03 +08003467 } else {
3468 //set play speed and if audio is start, stop audio.
3469 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003470 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08003471 AmTsPlayer_stopAudioDecoding(player->handle);
3472 player->has_audio = DVR_FALSE;
3473 }
hualing chenf00cdc82020-06-10 14:23:35 +08003474 DVR_PB_DG(1, "start fast");
3475 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08003476 }
hualing chena540a7e2020-03-27 16:44:05 +08003477 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003478 player->cmd.speed.speed = speed.speed;
3479 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08003480 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003481 pthread_mutex_unlock(&player->lock);
3482 return DVR_SUCCESS;
3483 }
3484 //case 2 fffb mode
3485 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3486 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
3487 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003488 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08003489 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08003490 player->cmd.speed.speed = speed.speed;
3491 player->speed = (float)speed.speed.speed/(float)100;
3492 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08003493 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08003494 pthread_mutex_unlock(&player->lock);
3495 return DVR_SUCCESS;
3496 }
hualing chen31140872020-03-25 12:29:26 +08003497 }
hualing chena540a7e2020-03-27 16:44:05 +08003498 if (IS_KERNEL_SPEED(speed.speed.speed)) {
3499 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08003500 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08003501 } else {
hualing chen31140872020-03-25 12:29:26 +08003502 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08003503 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
3504 else
3505 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08003506 player->fffb_play = DVR_TRUE;
3507 }
3508 DVR_Bool_t init_last_time = DVR_FALSE;
3509 if (player->speed > 0.0f && speed.speed.speed < 0) {
3510 init_last_time = DVR_TRUE;
3511 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
3512 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08003513 }
hualing chen5cbe1a62020-02-10 16:36:36 +08003514 player->cmd.speed.mode = speed.mode;
3515 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08003516 player->speed = (float)speed.speed.speed/(float)100;
3517 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08003518 _dvr_init_fffb_t(handle);
3519 if (init_last_time == DVR_TRUE)
3520 player->last_send_time_id = UINT64_MAX;
3521
hualing chen87072a82020-03-12 16:20:12 +08003522 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08003523 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
3524 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08003525 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08003526 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08003527 _dvr_playback_replay(handle, DVR_FALSE);
3528 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
3529 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
3530 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08003531 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08003532 }
hualing chen4b7c15d2020-04-07 16:13:48 +08003533 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08003534 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08003535 return DVR_SUCCESS;
3536}
hualing chen2932d372020-04-29 13:44:00 +08003537
hualing chenb31a6c62020-01-13 17:27:00 +08003538/**\brief Get playback status
3539 * \param[in] handle playback handle
3540 * \param[out] p_status playback status
3541 * \retval DVR_SUCCESS On success
3542 * \return Error code
3543 */
hualing chen2932d372020-04-29 13:44:00 +08003544static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3545 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08003546//
3547 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen969fe7b2021-05-26 15:13:17 +08003548 uint64_t segment_id = 0LL;
hualing chena540a7e2020-03-27 16:44:05 +08003549 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003550 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003551 return DVR_FAILURE;
3552 }
hualing chen2932d372020-04-29 13:44:00 +08003553 if (is_lock ==DVR_TRUE)
3554 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08003555 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08003556 //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 +08003557 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
3558 player->state == DVR_PLAYBACK_STATE_START) {
3559 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
3560 }
hualing chen041c4092020-04-05 15:11:50 +08003561
hualing chencc91e1c2020-02-28 13:26:17 +08003562 p_status->time_end = _dvr_get_end_time(handle);
hualing chen969fe7b2021-05-26 15:13:17 +08003563 p_status->time_cur = _dvr_get_play_cur_time(handle, &segment_id);
hualing chend241c7a2021-06-22 13:34:27 +08003564
3565 if (CONTROL_SPEED_ENABLE == 1) {
hualing chen7ea70a72021-09-09 11:25:13 +08003566 if (player->con_spe.ply_sta == 0) {
3567 DVR_PB_DG(1, "player dur[%u] sta[%u] cur[%d] -----reinit",
hualing chen03fd4942021-07-15 15:56:41 +08003568 player->con_spe.ply_dur,
3569 player->con_spe.ply_sta,
3570 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003571 player->con_spe.ply_sta = p_status->time_cur;
3572 } else if (player->speed == 1.0f && player->con_spe.ply_sta < p_status->time_cur) {
3573 player->con_spe.ply_dur += (p_status->time_cur - player->con_spe.ply_sta);
hualing chen7ea70a72021-09-09 11:25:13 +08003574 DVR_PB_DG(1, "player dur[%u] sta[%u] cur[%d]",
hualing chen03fd4942021-07-15 15:56:41 +08003575 player->con_spe.ply_dur,
3576 player->con_spe.ply_sta,
3577 p_status->time_cur);
hualing chend241c7a2021-06-22 13:34:27 +08003578 player->con_spe.ply_sta = p_status->time_cur;
3579 }
3580
3581 if (player->con_spe.sys_sta == 0) {
3582 player->con_spe.sys_sta = _dvr_time_getClock();
3583 } else if (player->speed == 1.0f && player->con_spe.sys_sta > 0) {
3584 player->con_spe.sys_dur += (_dvr_time_getClock() - player->con_spe.sys_sta);
3585 player->con_spe.sys_sta = _dvr_time_getClock();
3586 }
3587 }
3588
hualing chen4b7c15d2020-04-07 16:13:48 +08003589 if (player->last_send_time_id == UINT64_MAX) {
3590 player->last_send_time_id = player->cur_segment_id;
3591 player->last_cur_time = p_status->time_cur;
3592 }
3593 if (player->last_send_time_id == player->cur_segment_id) {
3594 if (player->speed > 0.0f ) {
3595 //ff
3596 if (p_status->time_cur < player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003597 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]",
3598 player->last_cur_time,
3599 p_status->time_cur,
3600 player->last_cur_time - p_status->time_cur);
hualing chen4b7c15d2020-04-07 16:13:48 +08003601 p_status->time_cur = player->last_cur_time;
3602 } else {
3603 player->last_cur_time = p_status->time_cur;
3604 }
hualing chene41f4372020-06-06 16:29:17 +08003605 } else if (player->speed <= -1.0f){
hualing chen4b7c15d2020-04-07 16:13:48 +08003606 //fb
3607 if (p_status->time_cur > player->last_cur_time ) {
hualing chen03fd4942021-07-15 15:56:41 +08003608 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]",
3609 player->last_cur_time,
3610 p_status->time_cur,
3611 p_status->time_cur - player->last_cur_time );
hualing chen4b7c15d2020-04-07 16:13:48 +08003612 p_status->time_cur = player->last_cur_time;
3613 } else {
3614 player->last_cur_time = p_status->time_cur;
3615 }
3616 }
hualing chend241c7a2021-06-22 13:34:27 +08003617 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08003618 player->last_cur_time = p_status->time_cur;
3619 }
hualing chen969fe7b2021-05-26 15:13:17 +08003620 player->last_send_time_id = segment_id;
3621 p_status->segment_id = segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08003622
hualing chen5cbe1a62020-02-10 16:36:36 +08003623 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08003624 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08003625 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08003626 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 +08003627 _dvr_playback_state_toString(player->state),
3628 _dvr_playback_state_toString(p_status->state),
3629 p_status->time_cur, p_status->time_end,
3630 p_status->segment_id,player->play_flag,
3631 player->speed,
3632 is_lock);
hualing chen2932d372020-04-29 13:44:00 +08003633 if (is_lock ==DVR_TRUE)
3634 pthread_mutex_unlock(&player->lock);
3635 return DVR_SUCCESS;
3636}
3637
3638
3639/**\brief Get playback status
3640 * \param[in] handle playback handle
3641 * \param[out] p_status playback status
3642 * \retval DVR_SUCCESS On success
3643 * \return Error code
3644 */
3645int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
3646 DVR_PlaybackStatus_t *p_status) {
3647//
3648 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3649
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003650 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
3651
hualing chen2932d372020-04-29 13:44:00 +08003652 if (player == NULL) {
3653 DVR_PB_DG(1, "player is NULL");
3654 return DVR_FAILURE;
3655 }
Zhiqiang Han9adc9722020-11-11 18:38:10 +08003656 pthread_mutex_lock(&player->lock);
3657 if (!player->has_video && !player->has_audio)
3658 p_status->time_cur = 0;
3659 pthread_mutex_unlock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08003660
hualing chenb31a6c62020-01-13 17:27:00 +08003661 return DVR_SUCCESS;
3662}
3663
hualing chen040df222020-01-17 13:35:02 +08003664void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
3665 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003666 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
3667 DVR_PB_DG(1, "segment flag: %d", segment->flags);
3668 DVR_PB_DG(1, "segment location: [%s]", segment->location);
3669 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
3670 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
3671 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
3672 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 +08003673 }
hualing chenb31a6c62020-01-13 17:27:00 +08003674}
3675
hualing chen5cbe1a62020-02-10 16:36:36 +08003676int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08003677 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08003678
hualing chena540a7e2020-03-27 16:44:05 +08003679 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08003680 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08003681 return DVR_FAILURE;
3682 }
3683
hualing chen040df222020-01-17 13:35:02 +08003684 DVR_PlaybackSegmentInfo_t *segment;
3685 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08003686 {
hualing chen040df222020-01-17 13:35:02 +08003687 if (segment_id >= 0) {
3688 if (segment->segment_id == segment_id) {
3689 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003690 break;
3691 }
3692 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08003693 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08003694 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08003695 }
3696 }
3697 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08003698}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003699
pengfei.liu27cc4ec2020-04-03 16:28:16 +08003700int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003701{
3702 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3703 DVR_RETURN_IF_FALSE(player);
3704 DVR_RETURN_IF_FALSE(func);
3705
hualing chen4b7c15d2020-04-07 16:13:48 +08003706 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003707 pthread_mutex_lock(&player->lock);
3708
3709 player->dec_func = func;
3710 player->dec_userdata = userdata;
3711
3712 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003713 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003714 return DVR_SUCCESS;
3715}
3716
3717int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
3718{
3719 DVR_Playback_t *player = (DVR_Playback_t *) handle;
3720 DVR_RETURN_IF_FALSE(player);
3721 DVR_RETURN_IF_FALSE(p_secure_buf);
3722 DVR_RETURN_IF_FALSE(len);
3723
hualing chen4b7c15d2020-04-07 16:13:48 +08003724 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003725 pthread_mutex_lock(&player->lock);
3726
3727 player->is_secure_mode = 1;
3728 player->secure_buffer = p_secure_buf;
3729 player->secure_buffer_size = len;
3730
3731 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003732 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003733 return DVR_SUCCESS;
3734}