blob: 56d13c6fbab0722d3a7fad3a9cf9899fc2b12765 [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 chenb31a6c62020-01-13 17:27:00 +080015
hualing chenb31a6c62020-01-13 17:27:00 +080016#include "dvr_playback.h"
17
hualing chen4b7c15d2020-04-07 16:13:48 +080018#define DVR_PB_DG(_level, _fmt, ...) \
19 DVR_DEBUG(_level, "playback %-30.30s:%d " _fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
20
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
24
25#define FF_SPEED (2.0f)
26#define FB_SPEED (-1.0f)
27#define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED)
28#define IS_FB(_SPEED_) ((_SPEED_) < FB_SPEED)
29
30#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))
31#define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8))
32
hualing chenb31a6c62020-01-13 17:27:00 +080033
hualing chenb5cd42e2020-04-15 17:03:34 +080034#define FFFB_SLEEP_TIME (1000)//500ms
hualing chen4b7c15d2020-04-07 16:13:48 +080035#define FB_DEFAULT_LEFT_TIME (10000)
hualing chen31140872020-03-25 12:29:26 +080036//if tsplayer delay time < 200 and no data can read, we will pause
37#define MIN_TSPLAYER_DELAY_TIME (200)
38
hualing chen041c4092020-04-05 15:11:50 +080039#define MAX_CACHE_TIME (30000)
40
hualing chena540a7e2020-03-27 16:44:05 +080041static int write_success = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +080042//
43static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle);
hualing chencc91e1c2020-02-28 13:26:17 +080044static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type);
45static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle);
46static int _dvr_get_end_time(DVR_PlaybackHandle_t handle);
hualing chen2aba4022020-03-02 13:49:55 +080047static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle);
hualing chen87072a82020-03-12 16:20:12 +080048static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ;
hualing chen2932d372020-04-29 13:44:00 +080049static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
50 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock);
hualing chen87072a82020-03-12 16:20:12 +080051
hualing chenbcada022020-04-22 14:27:01 +080052
53static char* _cmd_toString(int cmd)
54{
55
56 char *string[DVR_PLAYBACK_CMD_NONE+1]={
57 "start",
58 "stop",
59 "vstart",
60 "astart",
61 "vstop",
62 "astop",
63 "vrestart",
64 "arestart",
65 "avrestart",
66 "vstopastart",
67 "astopvstart",
68 "vstoparestart",
69 "astopvrestart",
70 "vstartarestart",
71 "astartvrestart",
72 "pause",
73 "resume",
74 "seek",
75 "ff",
76 "fb",
77 "NONE"
78 };
79
80 if (cmd > DVR_PLAYBACK_CMD_NONE) {
81 return "unkown";
82 } else {
83 return string[cmd];
84 }
85}
86
87
hualing chen6d24aa92020-03-23 18:43:47 +080088static char* _dvr_playback_state_toString(int stat)
89{
90 char *string[DVR_PLAYBACK_STATE_FB+1]={
91 "start",
hualing chen6d24aa92020-03-23 18:43:47 +080092 "stop",
hualing chen31140872020-03-25 12:29:26 +080093 "pause",
hualing chen6d24aa92020-03-23 18:43:47 +080094 "ff",
95 "fb"
96 };
97
98 if (stat > DVR_PLAYBACK_STATE_FB) {
99 return "unkown";
100 } else {
101 return string[stat];
102 }
103}
hualing chena540a7e2020-03-27 16:44:05 +0800104
105static DVR_Bool_t _dvr_support_speed(int speed) {
106
107 DVR_Bool_t ret = DVR_FALSE;
108
109 switch (speed) {
110 case PLAYBACK_SPEED_FBX2:
111 case PLAYBACK_SPEED_FBX4:
112 case PLAYBACK_SPEED_FBX8:
hualing chen041c4092020-04-05 15:11:50 +0800113 case PLAYBACK_SPEED_FBX16:
114 case PLAYBACK_SPEED_FBX12:
115 case PLAYBACK_SPEED_FBX32:
116 case PLAYBACK_SPEED_FBX48:
117 case PLAYBACK_SPEED_FBX64:
118 case PLAYBACK_SPEED_FBX128:
hualing chena540a7e2020-03-27 16:44:05 +0800119 case PLAYBACK_SPEED_S2:
120 case PLAYBACK_SPEED_S4:
121 case PLAYBACK_SPEED_S8:
122 case PLAYBACK_SPEED_X1:
123 case PLAYBACK_SPEED_X2:
124 case PLAYBACK_SPEED_X4:
hualing chena540a7e2020-03-27 16:44:05 +0800125 case PLAYBACK_SPEED_X3:
126 case PLAYBACK_SPEED_X5:
127 case PLAYBACK_SPEED_X6:
128 case PLAYBACK_SPEED_X7:
hualing chen041c4092020-04-05 15:11:50 +0800129 case PLAYBACK_SPEED_X8:
130 case PLAYBACK_SPEED_X12:
131 case PLAYBACK_SPEED_X16:
132 case PLAYBACK_SPEED_X32:
133 case PLAYBACK_SPEED_X48:
134 case PLAYBACK_SPEED_X64:
135 case PLAYBACK_SPEED_X128:
hualing chena540a7e2020-03-27 16:44:05 +0800136 ret = DVR_TRUE;
137 break;
138 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800139 DVR_PB_DG(1, "not support speed is set [%d]", speed);
hualing chena540a7e2020-03-27 16:44:05 +0800140 break;
141 }
142 return ret;
143}
hualing chen6e4bfa52020-03-13 14:37:11 +0800144void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event)
145{
hualing chen4b7c15d2020-04-07 16:13:48 +0800146 DVR_PB_DG(1, "in callback test ");
hualing chen6e4bfa52020-03-13 14:37:11 +0800147 DVR_Playback_t *player = NULL;
148 if (user_data != NULL) {
hualing chena540a7e2020-03-27 16:44:05 +0800149 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800150 DVR_PB_DG(1, "play speed [%f] in callback test ", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800151 }
152 switch (event->type) {
153 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
154 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800155 DVR_PB_DG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800156 event->event.video_format.frame_width,
157 event->event.video_format.frame_height,
158 event->event.video_format.frame_rate);
159 break;
160 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800161 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
162 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800163 DVR_PB_DG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800164 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800165 break;
166 }
167 default:
168 break;
169 }
170}
hualing chen2aba4022020-03-02 13:49:55 +0800171void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event)
172{
hualing chen6e4bfa52020-03-13 14:37:11 +0800173 DVR_Playback_t *player = NULL;
174 if (user_data != NULL) {
175 player = (DVR_Playback_t *) user_data;
hualing chen4b7c15d2020-04-07 16:13:48 +0800176 DVR_PB_DG(1, "play speed [%f] in-- callback", player->speed);
hualing chen6e4bfa52020-03-13 14:37:11 +0800177 }
hualing chen2aba4022020-03-02 13:49:55 +0800178 switch (event->type) {
hualing chen6e4bfa52020-03-13 14:37:11 +0800179 case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED:
180 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800181 DVR_PB_DG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n",
hualing chen6e4bfa52020-03-13 14:37:11 +0800182 event->event.video_format.frame_width,
183 event->event.video_format.frame_height,
184 event->event.video_format.frame_rate);
185 break;
186 }
hualing chen6e4bfa52020-03-13 14:37:11 +0800187 case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME:
188 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800189 DVR_PB_DG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n");
hualing chena540a7e2020-03-27 16:44:05 +0800190 if (player != NULL)
191 player->first_frame = 1;
hualing chen6e4bfa52020-03-13 14:37:11 +0800192 break;
193 }
194 default:
hualing chen4b7c15d2020-04-07 16:13:48 +0800195 DVR_PB_DG(1, "[evt]unkown event [%d]\n", event->type);
hualing chen6e4bfa52020-03-13 14:37:11 +0800196 break;
197 }
198 if (player&&player->player_callback_func) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800199 DVR_PB_DG(1, "player is nonull, --call callback\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800200 player->player_callback_func(player->player_callback_userdata, event);
201 } else if (player == NULL){
hualing chen4b7c15d2020-04-07 16:13:48 +0800202 DVR_PB_DG(1, "player is null, get userdata error\n");
hualing chen6e4bfa52020-03-13 14:37:11 +0800203 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800204 DVR_PB_DG(1, "player callback is null, get callback error\n");
hualing chen2aba4022020-03-02 13:49:55 +0800205 }
206}
hualing chencc91e1c2020-02-28 13:26:17 +0800207
hualing chen5cbe1a62020-02-10 16:36:36 +0800208//convert video and audio fmt
209static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) {
210 int format = 0;
211 if (is_audio == DVR_FALSE) {
212 //for video fmt
213 switch (fmt)
214 {
215 case DVR_VIDEO_FORMAT_MPEG1:
hualing chen2aba4022020-03-02 13:49:55 +0800216 format = AV_VIDEO_CODEC_MPEG1;
hualing chen5cbe1a62020-02-10 16:36:36 +0800217 break;
218 case DVR_VIDEO_FORMAT_MPEG2:
hualing chen2aba4022020-03-02 13:49:55 +0800219 format = AV_VIDEO_CODEC_MPEG2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800220 break;
221 case DVR_VIDEO_FORMAT_HEVC:
hualing chen2aba4022020-03-02 13:49:55 +0800222 format = AV_VIDEO_CODEC_H265;
hualing chen5cbe1a62020-02-10 16:36:36 +0800223 break;
224 case DVR_VIDEO_FORMAT_H264:
hualing chen2aba4022020-03-02 13:49:55 +0800225 format = AV_VIDEO_CODEC_H264;
hualing chen5cbe1a62020-02-10 16:36:36 +0800226 break;
hualing chena540a7e2020-03-27 16:44:05 +0800227 case DVR_VIDEO_FORMAT_VP9:
228 format = AV_VIDEO_CODEC_VP9;
229 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800230 }
231 } else {
232 //for audio fmt
233 switch (fmt)
234 {
235 case DVR_AUDIO_FORMAT_MPEG:
hualing chen2aba4022020-03-02 13:49:55 +0800236 format = AV_AUDIO_CODEC_MP2;
hualing chen5cbe1a62020-02-10 16:36:36 +0800237 break;
238 case DVR_AUDIO_FORMAT_AC3:
hualing chen2aba4022020-03-02 13:49:55 +0800239 format = AV_AUDIO_CODEC_AC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800240 break;
241 case DVR_AUDIO_FORMAT_EAC3:
hualing chen2aba4022020-03-02 13:49:55 +0800242 format = AV_AUDIO_CODEC_EAC3;
hualing chen5cbe1a62020-02-10 16:36:36 +0800243 break;
244 case DVR_AUDIO_FORMAT_DTS:
hualing chen2aba4022020-03-02 13:49:55 +0800245 format = AV_AUDIO_CODEC_DTS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800246 break;
hualing chena540a7e2020-03-27 16:44:05 +0800247 case DVR_AUDIO_FORMAT_AAC:
248 format = AV_AUDIO_CODEC_AAC;
249 break;
250 case DVR_AUDIO_FORMAT_LATM:
251 format = AV_AUDIO_CODEC_LATM;
252 break;
253 case DVR_AUDIO_FORMAT_PCM:
254 format = AV_AUDIO_CODEC_PCM;
255 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800256 }
257 }
258 return format;
259}
hualing chen040df222020-01-17 13:35:02 +0800260static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800261{
hualing chen040df222020-01-17 13:35:02 +0800262 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800263
hualing chena540a7e2020-03-27 16:44:05 +0800264 if (player == NULL || player->handle == NULL)
hualing chen86e7d482020-01-16 15:13:33 +0800265 return -1;
266
hualing chena540a7e2020-03-27 16:44:05 +0800267 return player->first_frame;
hualing chen86e7d482020-01-16 15:13:33 +0800268}
hualing chena540a7e2020-03-27 16:44:05 +0800269
hualing chen5cbe1a62020-02-10 16:36:36 +0800270//get sys time ms
271static int _dvr_time_getClock(void)
272{
273 struct timespec ts;
274 int ms;
275
276 clock_gettime(CLOCK_MONOTONIC, &ts);
277 ms = ts.tv_sec*1000+ts.tv_nsec/1000000;
278
279 return ms;
280}
hualing chen86e7d482020-01-16 15:13:33 +0800281
hualing chenb31a6c62020-01-13 17:27:00 +0800282
283//timeout wait sibnal
hualing chen040df222020-01-17 13:35:02 +0800284static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms)
hualing chenb31a6c62020-01-13 17:27:00 +0800285{
hualing chen040df222020-01-17 13:35:02 +0800286 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +0800287
hualing chena540a7e2020-03-27 16:44:05 +0800288
289 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800290 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800291 return DVR_FAILURE;
292 }
293
hualing chen86e7d482020-01-16 15:13:33 +0800294 struct timespec ts;
295 clock_gettime(CLOCK_MONOTONIC, &ts);
296 //ms为毫秒,换算成秒
297 ts.tv_sec += ms/1000;
298 //在outtime的基础上,增加ms毫秒
299 //outtime.tv_nsec为纳秒,1微秒=1000纳秒
300 //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理
301 uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒
302 //us的值有可能超过1秒,
303 ts.tv_sec += us / 1000000;
304 us = us % 1000000;
305 ts.tv_nsec = us * 1000;//换算成纳秒
hualing chen86e7d482020-01-16 15:13:33 +0800306 pthread_cond_timedwait(&player->cond, &player->lock, &ts);
307 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800308}
hualing chen31140872020-03-25 12:29:26 +0800309//get tsplay delay time ms
310static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) {
311 DVR_Playback_t *player = (DVR_Playback_t *) handle;
312 int64_t cache = 0;
313 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800314 DVR_PB_DG(1, "tsplayer delay time error, handle is NULL");
hualing chen31140872020-03-25 12:29:26 +0800315 return 0;
316 }
317 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen4b7c15d2020-04-07 16:13:48 +0800318 DVR_PB_DG(1, "tsplayer cache time [%lld]ms", cache);
hualing chen31140872020-03-25 12:29:26 +0800319 return cache;
320}
hualing chenb31a6c62020-01-13 17:27:00 +0800321//send signal
hualing chen040df222020-01-17 13:35:02 +0800322static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +0800323{
hualing chen87072a82020-03-12 16:20:12 +0800324 DVR_Playback_t *player = (DVR_Playback_t *) handle;\
hualing chena540a7e2020-03-27 16:44:05 +0800325
326 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800327 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800328 return DVR_FAILURE;
329 }
330
hualing chen87072a82020-03-12 16:20:12 +0800331 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +0800332 pthread_cond_signal(&player->cond);
hualing chen87072a82020-03-12 16:20:12 +0800333 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800334 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +0800335}
336
hualing chen2932d372020-04-29 13:44:00 +0800337//send playback event, need check is need lock first
338static 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 +0800339
340 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800341
342 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800343 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800344 return DVR_FAILURE;
345 }
346
hualing chencc91e1c2020-02-28 13:26:17 +0800347 switch (evt) {
348 case DVR_PLAYBACK_EVENT_ERROR:
hualing chen2932d372020-04-29 13:44:00 +0800349 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800350 break;
351 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
352 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800353 DVR_PB_DG(1, "trans ok EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800354 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800355 break;
356 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
357 break;
358 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
359 break;
360 case DVR_PLAYBACK_EVENT_NO_KEY:
361 break;
362 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chen2aba4022020-03-02 13:49:55 +0800363 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800364 DVR_PB_DG(1, "reached begin EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800365 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800366 break;
367 case DVR_PLAYBACK_EVENT_REACHED_END:
368 //GET STATE
hualing chen4b7c15d2020-04-07 16:13:48 +0800369 DVR_PB_DG(1, "reached end EVENT");
hualing chen2932d372020-04-29 13:44:00 +0800370 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800371 break;
hualing chen6e4bfa52020-03-13 14:37:11 +0800372 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chen2932d372020-04-29 13:44:00 +0800373 _dvr_playback_get_status(handle, &(notify->play_status), is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800374 break;
hualing chencc91e1c2020-02-28 13:26:17 +0800375 default:
376 break;
377 }
378 if (player->openParams.event_fn != NULL)
379 player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata);
hualing chencc91e1c2020-02-28 13:26:17 +0800380 return DVR_SUCCESS;
381}
hualing chen2932d372020-04-29 13:44:00 +0800382static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chencc91e1c2020-02-28 13:26:17 +0800383{
384 DVR_Play_Notify_t notify;
385 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
386 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
387 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800388 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, &notify, is_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800389 return DVR_SUCCESS;
390}
391
hualing chen2932d372020-04-29 13:44:00 +0800392static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle, DVR_Bool_t is_lock)
hualing chen6e4bfa52020-03-13 14:37:11 +0800393{
394 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800395
hualing chen4b7c15d2020-04-07 16:13:48 +0800396 if (1) {
397 return DVR_SUCCESS;
398 }
hualing chena540a7e2020-03-27 16:44:05 +0800399 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800400 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800401 return DVR_FAILURE;
402 }
403
hualing chen6e4bfa52020-03-13 14:37:11 +0800404 if (player->send_time ==0) {
405 player->send_time = _dvr_time_getClock() + 1000;
406 } else if (player->send_time > _dvr_time_getClock()) {
407 return DVR_SUCCESS;
408 }
409 player->send_time = _dvr_time_getClock() + 1000;
410 DVR_Play_Notify_t notify;
411 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
412 notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME;
413 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800414 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, &notify, is_lock);
hualing chen6e4bfa52020-03-13 14:37:11 +0800415 return DVR_SUCCESS;
416}
417
hualing chencc91e1c2020-02-28 13:26:17 +0800418//check is ongoing segment
419static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) {
420
421 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen87072a82020-03-12 16:20:12 +0800422 int ret = DVR_FAILURE;
hualing chena540a7e2020-03-27 16:44:05 +0800423
424 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800425 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800426 return DVR_FAILURE;
427 }
hualing chen87072a82020-03-12 16:20:12 +0800428 ret = segment_ongoing(player->r_handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800429 if (ret != DVR_SUCCESS) {
hualing chencc91e1c2020-02-28 13:26:17 +0800430 return DVR_FALSE;
431 }
hualing chencc91e1c2020-02-28 13:26:17 +0800432 return DVR_TRUE;
433}
hualing chen4b7c15d2020-04-07 16:13:48 +0800434
435
436static int _dvr_init_fffb_t(DVR_PlaybackHandle_t handle) {
437 DVR_Playback_t *player = (DVR_Playback_t *) handle;
438 player->fffb_start = _dvr_time_getClock();
439 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
440 player->fffb_current = player->fffb_start;
441 //get segment current time pos
442 player->fffb_start_pcr = _dvr_get_cur_time(handle);
443 //player->fffb_current = -1;
444 //player->fffb_start = -1;
445 //player->fffb_start_pcr = -1;
446 player->next_fffb_time = _dvr_time_getClock();
447
448 return DVR_SUCCESS;
449}
450
hualing chen2aba4022020-03-02 13:49:55 +0800451static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) {
452 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +0800453 player->fffb_start = _dvr_time_getClock();
454 DVR_PB_DG(1, " player->fffb_start:%d", player->fffb_start);
455 player->fffb_current = player->fffb_start;
456 //get segment current time pos
457 player->fffb_start_pcr = _dvr_get_cur_time(handle);
458 //player->fffb_current = -1;
459 //player->fffb_start = -1;
460 //player->fffb_start_pcr = -1;
hualing chen2aba4022020-03-02 13:49:55 +0800461 player->next_fffb_time = _dvr_time_getClock();
hualing chen4b7c15d2020-04-07 16:13:48 +0800462 player->last_send_time_id = UINT64_MAX;
hualing chen2aba4022020-03-02 13:49:55 +0800463 return DVR_SUCCESS;
464}
hualing chencc91e1c2020-02-28 13:26:17 +0800465//get next segment id
hualing chen87072a82020-03-12 16:20:12 +0800466static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) {
467
468 DVR_Playback_t *player = (DVR_Playback_t *) handle;
469 DVR_PlaybackSegmentInfo_t *segment;
470 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
471
hualing chena540a7e2020-03-27 16:44:05 +0800472 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800473 DVR_PB_DG(1, " player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800474 return DVR_FAILURE;
475 }
476
hualing chen87072a82020-03-12 16:20:12 +0800477 int found = 0;
478 int found_eq_id = 0;
479 list_for_each_entry(segment, &player->segment_list, head)
480 {
481 if (player->segment_is_open == DVR_FALSE) {
482 //get first segment from list, case segment is not open
483 if (!IS_FB(player->speed))
484 found = 1;
485 } else if (segment->segment_id == segmentid) {
486 //find cur segment, we need get next one
487 found_eq_id = 1;
488 if (!IS_FB(player->speed)) {
489 found = 1;
490 continue;
491 } else {
492 //if is fb mode.we need used pre segment
493 if (pre_segment != NULL) {
494 found = 1;
495 } else {
496 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800497 DVR_PB_DG(1, "not has find next segment on fb mode");
hualing chen87072a82020-03-12 16:20:12 +0800498 return DVR_FAILURE;
499 }
500 }
501 }
502 if (found == 1) {
503 found = 2;
504 break;
505 }
506 }
507 if (found != 2) {
508 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800509 DVR_PB_DG(1, "not found next segment return failure");
hualing chen87072a82020-03-12 16:20:12 +0800510 return DVR_FAILURE;
511 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800512 DVR_PB_DG(1, "found next segment return success");
hualing chen87072a82020-03-12 16:20:12 +0800513 return DVR_SUCCESS;
514}
515
516//get next segment id
hualing chen040df222020-01-17 13:35:02 +0800517static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +0800518
hualing chen040df222020-01-17 13:35:02 +0800519 DVR_Playback_t *player = (DVR_Playback_t *) handle;
520 DVR_PlaybackSegmentInfo_t *segment;
hualing chen2aba4022020-03-02 13:49:55 +0800521 DVR_PlaybackSegmentInfo_t *pre_segment = NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800522
hualing chena540a7e2020-03-27 16:44:05 +0800523 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800524 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800525 return DVR_FAILURE;
526 }
527
hualing chen86e7d482020-01-16 15:13:33 +0800528 int found = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800529 int found_eq_id = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800530
hualing chen040df222020-01-17 13:35:02 +0800531 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +0800532 {
hualing chencc91e1c2020-02-28 13:26:17 +0800533 if (player->segment_is_open == DVR_FALSE) {
hualing chen2aba4022020-03-02 13:49:55 +0800534 //get first segment from list, case segment is not open
535 if (!IS_FB(player->speed))
536 found = 1;
hualing chen040df222020-01-17 13:35:02 +0800537 } else if (segment->segment_id == player->cur_segment_id) {
538 //find cur segment, we need get next one
hualing chen2aba4022020-03-02 13:49:55 +0800539 found_eq_id = 1;
540 if (!IS_FB(player->speed)) {
541 found = 1;
542 continue;
543 } else {
544 //if is fb mode.we need used pre segment
545 if (pre_segment != NULL) {
546 found = 1;
547 } else {
548 //not find next id.
hualing chen4b7c15d2020-04-07 16:13:48 +0800549 DVR_PB_DG(1, "not find next segment on fb mode");
hualing chen2aba4022020-03-02 13:49:55 +0800550 return DVR_FAILURE;
551 }
552 }
hualing chen86e7d482020-01-16 15:13:33 +0800553 }
554 if (found == 1) {
hualing chen2aba4022020-03-02 13:49:55 +0800555 if (IS_FB(player->speed)) {
556 //used pre segment
557 segment = pre_segment;
558 }
hualing chencc91e1c2020-02-28 13:26:17 +0800559 //save segment info
560 player->last_segment_id = player->cur_segment_id;
hualing chen87072a82020-03-12 16:20:12 +0800561 player->last_segment.segment_id = player->cur_segment.segment_id;
562 player->last_segment.flags = player->cur_segment.flags;
hualing chencc91e1c2020-02-28 13:26:17 +0800563 memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
564 //pids
565 memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
566
hualing chen5cbe1a62020-02-10 16:36:36 +0800567 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800568 player->segment_is_open = DVR_TRUE;
hualing chen040df222020-01-17 13:35:02 +0800569 player->cur_segment_id = segment->segment_id;
570 player->cur_segment.segment_id = segment->segment_id;
571 player->cur_segment.flags = segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800572 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 +0800573 memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE);
hualing chen86e7d482020-01-16 15:13:33 +0800574 //pids
hualing chen040df222020-01-17 13:35:02 +0800575 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen86e7d482020-01-16 15:13:33 +0800576 found = 2;
hualing chen2aba4022020-03-02 13:49:55 +0800577 break;
hualing chen86e7d482020-01-16 15:13:33 +0800578 }
hualing chen2aba4022020-03-02 13:49:55 +0800579 pre_segment = segment;
580 }
581 if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) {
582 //used the last one segment to open
583 //get segment info
584 player->segment_is_open = DVR_TRUE;
585 player->cur_segment_id = pre_segment->segment_id;
586 player->cur_segment.segment_id = pre_segment->segment_id;
587 player->cur_segment.flags = pre_segment->flags;
hualing chen4b7c15d2020-04-07 16:13:48 +0800588 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 +0800589 memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE);
590 //pids
591 memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t));
592 return DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800593 }
594 if (found != 2) {
595 //list is null or reache list end
hualing chen2aba4022020-03-02 13:49:55 +0800596 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800597 }
598 return DVR_SUCCESS;
599}
hualing chen040df222020-01-17 13:35:02 +0800600//open next segment to play,if reach list end return errro.
601static int _change_to_next_segment(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +0800602{
hualing chen040df222020-01-17 13:35:02 +0800603 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen86e7d482020-01-16 15:13:33 +0800604 Segment_OpenParams_t params;
605 int ret = DVR_SUCCESS;
606
hualing chena540a7e2020-03-27 16:44:05 +0800607 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800608 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800609 return DVR_FAILURE;
610 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800611 pthread_mutex_lock(&player->segment_lock);
hualing chena540a7e2020-03-27 16:44:05 +0800612
613 ret = _dvr_get_next_segmentId(handle);
614 if (ret == DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800615 DVR_PB_DG(1, "not found segment info");
616 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800617 return DVR_FAILURE;
hualing chen86e7d482020-01-16 15:13:33 +0800618 }
619
620 if (player->r_handle != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800621 DVR_PB_DG(1, "close segment");
hualing chen86e7d482020-01-16 15:13:33 +0800622 segment_close(player->r_handle);
623 player->r_handle = NULL;
624 }
625
626 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800627 //cp chur segment path to location
628 memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE);
hualing chen040df222020-01-17 13:35:02 +0800629 params.segment_id = (uint64_t)player->cur_segment.segment_id;
hualing chen86e7d482020-01-16 15:13:33 +0800630 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800631 DVR_PB_DG(1, "open segment location[%s]id[%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags);
632
hualing chen86e7d482020-01-16 15:13:33 +0800633 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800634 if (ret == DVR_FAILURE) {
635 DVR_PB_DG(1, "open segment error");
636 }
hualing chen87072a82020-03-12 16:20:12 +0800637 pthread_mutex_unlock(&player->segment_lock);
638 int total = _dvr_get_end_time( handle);
639 pthread_mutex_lock(&player->segment_lock);
hualing chen2aba4022020-03-02 13:49:55 +0800640 if (IS_FB(player->speed)) {
641 //seek end pos -FB_DEFAULT_LEFT_TIME
hualing chen266b9502020-04-04 17:39:39 +0800642 segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +0800643 DVR_PB_DG(1, "seek pos [%d]", total - FB_DEFAULT_LEFT_TIME);
hualing chen2aba4022020-03-02 13:49:55 +0800644 }
hualing chen87072a82020-03-12 16:20:12 +0800645 player->dur = total;
hualing chen2aba4022020-03-02 13:49:55 +0800646 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +0800647 DVR_PB_DG(1, "next segment dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags);
hualing chen86e7d482020-01-16 15:13:33 +0800648 return ret;
649}
650
hualing chen5cbe1a62020-02-10 16:36:36 +0800651//open next segment to play,if reach list end return errro.
652static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id)
653{
654 DVR_Playback_t *player = (DVR_Playback_t *) handle;
655 Segment_OpenParams_t params;
656 int ret = DVR_SUCCESS;
hualing chena540a7e2020-03-27 16:44:05 +0800657 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800658 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800659 return DVR_FAILURE;
660 }
hualing chencc91e1c2020-02-28 13:26:17 +0800661 if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) {
hualing chen87072a82020-03-12 16:20:12 +0800662 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +0800663 }
hualing chencc91e1c2020-02-28 13:26:17 +0800664 uint64_t id = segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +0800665 if (id < 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800666 DVR_PB_DG(1, "not found segment info");
hualing chen5cbe1a62020-02-10 16:36:36 +0800667 return DVR_FAILURE;
668 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800669 DVR_PB_DG(1, "start found segment[%lld]info", id);
hualing chen2aba4022020-03-02 13:49:55 +0800670 pthread_mutex_lock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800671
672 DVR_PlaybackSegmentInfo_t *segment;
673
674 int found = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800675
hualing chen5cbe1a62020-02-10 16:36:36 +0800676 list_for_each_entry(segment, &player->segment_list, head)
677 {
hualing chen4b7c15d2020-04-07 16:13:48 +0800678 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 +0800679 if (segment->segment_id == segment_id) {
680 found = 1;
681 }
682 if (found == 1) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800683 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 +0800684 //get segment info
hualing chencc91e1c2020-02-28 13:26:17 +0800685 player->segment_is_open = DVR_TRUE;
hualing chen5cbe1a62020-02-10 16:36:36 +0800686 player->cur_segment_id = segment->segment_id;
687 player->cur_segment.segment_id = segment->segment_id;
688 player->cur_segment.flags = segment->flags;
hualing chen31140872020-03-25 12:29:26 +0800689 strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE
hualing chen5cbe1a62020-02-10 16:36:36 +0800690 //pids
691 memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t));
hualing chen4b7c15d2020-04-07 16:13:48 +0800692 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 +0800693 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800694 }
695 }
hualing chencc91e1c2020-02-28 13:26:17 +0800696 if (found == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800697 DVR_PB_DG(1, "not found segment info.error..");
hualing chen2aba4022020-03-02 13:49:55 +0800698 pthread_mutex_unlock(&player->segment_lock);
hualing chencc91e1c2020-02-28 13:26:17 +0800699 return DVR_FAILURE;
700 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800701 memset(params.location, 0, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +0800702 //cp cur segment path to location
hualing chen31140872020-03-25 12:29:26 +0800703 strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location));
hualing chen5cbe1a62020-02-10 16:36:36 +0800704 params.segment_id = (uint64_t)player->cur_segment.segment_id;
705 params.mode = SEGMENT_MODE_READ;
hualing chen4b7c15d2020-04-07 16:13:48 +0800706 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 +0800707 if (player->r_handle != NULL) {
708 segment_close(player->r_handle);
709 player->r_handle = NULL;
710 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800711 ret = segment_open(&params, &(player->r_handle));
hualing chen4b7c15d2020-04-07 16:13:48 +0800712 if (ret == DVR_FAILURE) {
713 DVR_PB_DG(1, "segment opne error");
714 }
hualing chen2aba4022020-03-02 13:49:55 +0800715 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +0800716 player->dur = _dvr_get_end_time(handle);
hualing chencc91e1c2020-02-28 13:26:17 +0800717
hualing chen4b7c15d2020-04-07 16:13:48 +0800718 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 +0800719 return ret;
720}
721
722
723//get play info by segment id
724static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle,
725 uint64_t segment_id,
hualing chen2aba4022020-03-02 13:49:55 +0800726 am_tsplayer_video_params *vparam,
727 am_tsplayer_audio_params *aparam) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800728
729 DVR_Playback_t *player = (DVR_Playback_t *) handle;
730 DVR_PlaybackSegmentInfo_t *segment;
hualing chena540a7e2020-03-27 16:44:05 +0800731 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800732 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800733 return DVR_FAILURE;
734 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800735
736 int found = 0;
737
738 list_for_each_entry(segment, &player->segment_list, head)
739 {
hualing chen87072a82020-03-12 16:20:12 +0800740 if (segment_id == UINT64_MAX) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800741 //get first segment from list
742 found = 1;
743 }
744 if (segment->segment_id == segment_id) {
745 found = 1;
746 }
747 if (found == 1) {
748 //get segment info
hualing chen87072a82020-03-12 16:20:12 +0800749 if (player->cur_segment_id != UINT64_MAX)
hualing chen5cbe1a62020-02-10 16:36:36 +0800750 player->cur_segment_id = segment->segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +0800751 DVR_PB_DG(1, "get play info id [%lld]", player->cur_segment_id);
hualing chen5cbe1a62020-02-10 16:36:36 +0800752 player->cur_segment.segment_id = segment->segment_id;
753 player->cur_segment.flags = segment->flags;
754 //pids
hualing chen2aba4022020-03-02 13:49:55 +0800755 player->cur_segment.pids.video.pid = segment->pids.video.pid;
756 player->cur_segment.pids.video.format = segment->pids.video.format;
757 player->cur_segment.pids.video.type = segment->pids.video.type;
758 player->cur_segment.pids.audio.pid = segment->pids.audio.pid;
759 player->cur_segment.pids.audio.format = segment->pids.audio.format;
760 player->cur_segment.pids.audio.type = segment->pids.audio.type;
761 player->cur_segment.pids.ad.pid = segment->pids.ad.pid;
762 player->cur_segment.pids.ad.format = segment->pids.ad.format;
763 player->cur_segment.pids.ad.type = segment->pids.ad.type;
764 player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid;
hualing chen5cbe1a62020-02-10 16:36:36 +0800765 //
hualing chen2aba4022020-03-02 13:49:55 +0800766 vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800767 vparam->pid = segment->pids.video.pid;
hualing chen2aba4022020-03-02 13:49:55 +0800768 aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE);
hualing chen5cbe1a62020-02-10 16:36:36 +0800769 aparam->pid = segment->pids.audio.pid;
hualing chen4b7c15d2020-04-07 16:13:48 +0800770 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 +0800771 found = 2;
hualing chencc91e1c2020-02-28 13:26:17 +0800772 break;
hualing chen5cbe1a62020-02-10 16:36:36 +0800773 }
774 }
hualing chencc91e1c2020-02-28 13:26:17 +0800775 if (found != 2) {
776 //list is null or reache list end
hualing chen4b7c15d2020-04-07 16:13:48 +0800777 DVR_PB_DG(1, "get play info fail");
hualing chencc91e1c2020-02-28 13:26:17 +0800778 return DVR_FAILURE;
779 }
hualing chen5cbe1a62020-02-10 16:36:36 +0800780
781 return DVR_SUCCESS;
782}
hualing chencc91e1c2020-02-28 13:26:17 +0800783static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) {
784 DVR_Playback_t *player = (DVR_Playback_t *) handle;
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
hualing chencc91e1c2020-02-28 13:26:17 +0800790 //compare cur segment
791 //if (player->cmd.state == DVR_PLAYBACK_STATE_START)
792 {
793 //check video pids, stop or restart
794 _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids.video, 0);
795 //check audio pids stop or restart
hualing chen7a56cba2020-04-14 14:09:27 +0800796 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 +0800797 _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids.audio, 1);
798 //check sub audio pids stop or restart
799 _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids.ad, 2);
800 //check pcr pids stop or restart
801 _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids.pcr, 3);
802 }
hualing chena540a7e2020-03-27 16:44:05 +0800803 return DVR_SUCCESS;
hualing chencc91e1c2020-02-28 13:26:17 +0800804}
hualing chen5cbe1a62020-02-10 16:36:36 +0800805
hualing chencc91e1c2020-02-28 13:26:17 +0800806static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle)
807{
808 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +0800809 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800810 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800811 return DVR_FAILURE;
812 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800813 DVR_PB_DG(1, "flag[0x%x]id[%lld]last[0x%x][%llu]", player->cur_segment.flags, player->cur_segment.segment_id, player->last_segment.flags, player->last_segment.segment_id);
hualing chen87072a82020-03-12 16:20:12 +0800814 if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE &&
815 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +0800816 //enable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800817 DVR_PB_DG(1, "unmute");
hualing chen2aba4022020-03-02 13:49:55 +0800818 AmTsPlayer_showVideo(player->handle);
819 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen87072a82020-03-12 16:20:12 +0800820 } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
821 (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chen2aba4022020-03-02 13:49:55 +0800822 //disable display
hualing chen4b7c15d2020-04-07 16:13:48 +0800823 DVR_PB_DG(1, "mute");
hualing chen2aba4022020-03-02 13:49:55 +0800824 AmTsPlayer_hideVideo(player->handle);
825 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chencc91e1c2020-02-28 13:26:17 +0800826 }
827 return DVR_SUCCESS;
828}
hualing chena540a7e2020-03-27 16:44:05 +0800829static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) {
830 DVR_Playback_t *player = (DVR_Playback_t *) handle;
831 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800832 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +0800833 return DVR_TRUE;
834 }
hualing chen266b9502020-04-04 17:39:39 +0800835 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chena540a7e2020-03-27 16:44:05 +0800836 if (player->first_frame == 1) {
837 return DVR_TRUE;
838 } else {
839 return DVR_FALSE;
840 }
841 } else {
842 return DVR_TRUE;
843 }
844}
hualing chen86e7d482020-01-16 15:13:33 +0800845static void* _dvr_playback_thread(void *arg)
846{
hualing chen040df222020-01-17 13:35:02 +0800847 DVR_Playback_t *player = (DVR_Playback_t *) arg;
hualing chencc91e1c2020-02-28 13:26:17 +0800848 //int need_open_segment = 1;
hualing chen2aba4022020-03-02 13:49:55 +0800849 am_tsplayer_input_buffer wbufs;
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800850 am_tsplayer_input_buffer dec_bufs;
hualing chen5cbe1a62020-02-10 16:36:36 +0800851 int ret = DVR_SUCCESS;
hualing chen86e7d482020-01-16 15:13:33 +0800852
hualing chen39628212020-05-14 10:35:13 +0800853 #define MAX_REACHEND_TIMEOUT (3000)
854 int reach_end_timeout = 0;//ms
855 int cache_time = 0;
hualing chen6d24aa92020-03-23 18:43:47 +0800856 int timeout = 300;//ms
hualing chen2aba4022020-03-02 13:49:55 +0800857 uint64_t write_timeout_ms = 50;
hualing chen86e7d482020-01-16 15:13:33 +0800858 uint8_t *buf = NULL;
hualing chen040df222020-01-17 13:35:02 +0800859 int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024);
hualing chen266b9502020-04-04 17:39:39 +0800860 DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE;
861
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800862 int dec_buf_size = buf_len + 188;
hualing chen86e7d482020-01-16 15:13:33 +0800863 int real_read = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800864 DVR_Bool_t goto_rewrite = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +0800865
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800866 if (player->is_secure_mode) {
867 if (dec_buf_size > player->secure_buffer_size) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800868 DVR_PB_DG(1, "playback blocksize too large");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800869 return NULL;
870 }
871 }
hualing chen86e7d482020-01-16 15:13:33 +0800872 buf = malloc(buf_len);
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800873 if (!buf) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800874 DVR_PB_DG(1, "Malloc buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800875 return NULL;
876 }
hualing chen2aba4022020-03-02 13:49:55 +0800877 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
878 wbufs.buf_size = 0;
hualing chencc91e1c2020-02-28 13:26:17 +0800879
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800880 dec_bufs.buf_data = malloc(dec_buf_size);
881 if (!dec_bufs.buf_data) {
hualing chen4b7c15d2020-04-07 16:13:48 +0800882 DVR_PB_DG(1, "Malloc dec buffer failed");
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800883 return NULL;
884 }
885 dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
886 dec_bufs.buf_size = dec_buf_size;
887
hualing chencc91e1c2020-02-28 13:26:17 +0800888 if (player->segment_is_open == DVR_FALSE) {
hualing chen5cbe1a62020-02-10 16:36:36 +0800889 ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
890 }
hualing chen86e7d482020-01-16 15:13:33 +0800891
hualing chen86e7d482020-01-16 15:13:33 +0800892 if (ret != DVR_SUCCESS) {
893 if (buf != NULL) {
894 free(buf);
895 buf = NULL;
896 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +0800897 free(dec_bufs.buf_data);
hualing chen4b7c15d2020-04-07 16:13:48 +0800898 DVR_PB_DG(1, "get segment error");
hualing chenb31a6c62020-01-13 17:27:00 +0800899 return NULL;
hualing chen86e7d482020-01-16 15:13:33 +0800900 }
hualing chencc91e1c2020-02-28 13:26:17 +0800901 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +0800902 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_TRUE);
hualing chencc91e1c2020-02-28 13:26:17 +0800903 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen6d24aa92020-03-23 18:43:47 +0800904 //set video show
905 AmTsPlayer_showVideo(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +0800906
hualing chen86e7d482020-01-16 15:13:33 +0800907 int trick_stat = 0;
908 while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) {
hualing chenb31a6c62020-01-13 17:27:00 +0800909
hualing chen86e7d482020-01-16 15:13:33 +0800910 //check trick stat
hualing chencc91e1c2020-02-28 13:26:17 +0800911 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +0800912
hualing chen2aba4022020-03-02 13:49:55 +0800913 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK ||
914 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +0800915 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB ||
hualing chena540a7e2020-03-27 16:44:05 +0800916 player->speed > FF_SPEED ||player->speed <= FB_SPEED ||
hualing chen39628212020-05-14 10:35:13 +0800917 (player->state == DVR_PLAYBACK_STATE_PAUSE) ||
hualing chen31140872020-03-25 12:29:26 +0800918 (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
hualing chen86e7d482020-01-16 15:13:33 +0800919 {
hualing chen2aba4022020-03-02 13:49:55 +0800920 trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player);
921 if (trick_stat > 0) {
hualing chenbcada022020-04-22 14:27:01 +0800922 DVR_PB_DG(1, "trick stat[%d] is > 0 cur cmd[%d]last cmd[%d]flag[0x%x]", player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen87072a82020-03-12 16:20:12 +0800923 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 +0800924 //check last cmd
hualing chenbcada022020-04-22 14:27:01 +0800925 if (player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE
hualing chen31140872020-03-25 12:29:26 +0800926 || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE
hualing chen87072a82020-03-12 16:20:12 +0800927 && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START
928 ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART
hualing chen2aba4022020-03-02 13:49:55 +0800929 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART
930 || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) {
hualing chenbcada022020-04-22 14:27:01 +0800931 DVR_PB_DG(1, "pause play-------cur cmd[%d]last cmd[%d]flag[0x%x]", player->cmd.cur_cmd, player->cmd.last_cmd, player->play_flag);
hualing chen2aba4022020-03-02 13:49:55 +0800932 //need change to pause state
933 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
934 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen31140872020-03-25 12:29:26 +0800935 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +0800936 //clear flag
hualing chen31140872020-03-25 12:29:26 +0800937 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
hualing chena540a7e2020-03-27 16:44:05 +0800938 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800939 AmTsPlayer_pauseVideoDecoding(player->handle);
940 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2bd8a7a2020-04-02 11:31:03 +0800941 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +0800942 DVR_PB_DG(1, "clear first frame value-------");
hualing chen2bd8a7a2020-04-02 11:31:03 +0800943 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800944 }
945 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF
946 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chena540a7e2020-03-27 16:44:05 +0800947 ||player->speed > FF_SPEED ||player->speed < FB_SPEED) {
hualing chen2aba4022020-03-02 13:49:55 +0800948 //restart play stream if speed > 2
hualing chenb5cd42e2020-04-15 17:03:34 +0800949 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
950 DVR_PB_DG(1, "fffb pause state----speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
hualing chen2aba4022020-03-02 13:49:55 +0800951 //used timeout wait need lock first,so we unlock and lock
952 //pthread_mutex_unlock(&player->lock);
953 //pthread_mutex_lock(&player->lock);
954 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
955 pthread_mutex_unlock(&player->lock);
956 continue;
hualing chenb5cd42e2020-04-15 17:03:34 +0800957 } else if (_dvr_time_getClock() < player->next_fffb_time) {
958 DVR_PB_DG(1, "fffb timeout-to pause video---speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
959 //used timeout wait need lock first,so we unlock and lock
960 //pthread_mutex_unlock(&player->lock);
961 //pthread_mutex_lock(&player->lock);
962 AmTsPlayer_pauseVideoDecoding(player->handle);
963 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
964 pthread_mutex_unlock(&player->lock);
965 continue;
966
hualing chen2aba4022020-03-02 13:49:55 +0800967 }
hualing chen2932d372020-04-29 13:44:00 +0800968 DVR_PB_DG(1, "fffb play-------speed[%f][%d][%d][%s][%d]", player->speed, goto_rewrite, real_read, _dvr_playback_state_toString(player->state), player->cmd);
hualing chen2aba4022020-03-02 13:49:55 +0800969 pthread_mutex_unlock(&player->lock);
970 goto_rewrite = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +0800971 real_read = 0;
hualing chena540a7e2020-03-27 16:44:05 +0800972 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
973 player->first_frame = 0;
hualing chen2aba4022020-03-02 13:49:55 +0800974 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +0800975 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +0800976 pthread_mutex_lock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +0800977 }
hualing chen4b7c15d2020-04-07 16:13:48 +0800978 }else if (player->fffb_play == DVR_TRUE){
979 //for first into fffb when reset speed
980 if (player->state == DVR_PLAYBACK_STATE_PAUSE ||
981 _dvr_time_getClock() < player->next_fffb_time) {
982 DVR_PB_DG(1, "fffb timeout-fffb play---speed[%f] fffb cur[%d] cur sys[%d] [%s] [%d]", player->speed, player->fffb_current,_dvr_time_getClock(),_dvr_playback_state_toString(player->state), player->next_fffb_time);
983 //used timeout wait need lock first,so we unlock and lock
984 //pthread_mutex_unlock(&player->lock);
985 //pthread_mutex_lock(&player->lock);
986 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
987 pthread_mutex_unlock(&player->lock);
988 continue;
989 }
hualing chen2932d372020-04-29 13:44:00 +0800990 DVR_PB_DG(1, "fffb replay-------speed[%f][%d][%d][%s][%d]player->fffb_play[%d]", player->speed, goto_rewrite, real_read, _dvr_playback_state_toString(player->state), player->cmd, player->fffb_play);
hualing chen4b7c15d2020-04-07 16:13:48 +0800991 pthread_mutex_unlock(&player->lock);
992 goto_rewrite = DVR_FALSE;
993 real_read = 0;
994 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
995 player->first_frame = 0;
996 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
997 pthread_mutex_lock(&player->lock);
998 player->fffb_play = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +0800999 }
hualing chenb31a6c62020-01-13 17:27:00 +08001000 }
hualing chen86e7d482020-01-16 15:13:33 +08001001
hualing chen87072a82020-03-12 16:20:12 +08001002 if (player->state == DVR_PLAYBACK_STATE_PAUSE) {
hualing chen6e4bfa52020-03-13 14:37:11 +08001003 //check is need send time send end
hualing chenc70a8df2020-05-12 19:23:11 +08001004 DVR_PB_DG(1, "pause, continue");
hualing chen2932d372020-04-29 13:44:00 +08001005 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen87072a82020-03-12 16:20:12 +08001006 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1007 pthread_mutex_unlock(&player->lock);
1008 continue;
1009 }
hualing chen266b9502020-04-04 17:39:39 +08001010 //when seek action is done. we need drop write timeout data.
1011 if (player->drop_ts == DVR_TRUE) {
1012 goto_rewrite = DVR_FALSE;
1013 real_read = 0;
1014 player->drop_ts = DVR_FALSE;
1015 }
hualing chen2aba4022020-03-02 13:49:55 +08001016 if (goto_rewrite == DVR_TRUE) {
1017 goto_rewrite = DVR_FALSE;
1018 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001019 //DVR_PB_DG(1, "rewrite-player->speed[%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08001020 goto rewrite;
1021 }
hualing chen6e4bfa52020-03-13 14:37:11 +08001022 //.check is need send time send end
hualing chen2932d372020-04-29 13:44:00 +08001023 _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001024 pthread_mutex_lock(&player->segment_lock);
hualing chen39628212020-05-14 10:35:13 +08001025 DVR_PB_DG(1, "start read");
hualing chen87072a82020-03-12 16:20:12 +08001026 int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen39628212020-05-14 10:35:13 +08001027 DVR_PB_DG(1, "start read end");
hualing chen4b7c15d2020-04-07 16:13:48 +08001028 pthread_mutex_unlock(&player->segment_lock);
hualing chen87072a82020-03-12 16:20:12 +08001029 pthread_mutex_unlock(&player->lock);
hualing chenb5cd42e2020-04-15 17:03:34 +08001030 if (read < 0 && errno == EIO) {
1031 //EIO ERROR, EXIT THRAD
1032 DVR_PB_DG(1, "read error.EIO error, exit thread");
1033 DVR_Play_Notify_t notify;
1034 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1035 notify.event = DVR_PLAYBACK_EVENT_ERROR;
hualing chen2932d372020-04-29 13:44:00 +08001036 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player,DVR_PLAYBACK_EVENT_ERROR, &notify, DVR_TRUE);
hualing chenb5cd42e2020-04-15 17:03:34 +08001037 goto end;
1038 } else if (read < 0) {
1039 DVR_PB_DG(1, "read error.:%d EIO:%d", errno, EIO);
1040 }
hualing chen87072a82020-03-12 16:20:12 +08001041 //if on fb mode and read file end , we need calculate pos to retry read.
1042 if (read == 0 && IS_FB(player->speed) && real_read == 0) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001043 DVR_PB_DG(1, "recalculate read [%d] readed [%d]buf_len[%d]speed[%f]id=[%llu]", read,real_read, buf_len, player->speed,player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08001044 _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player);
1045 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08001046 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1047 pthread_mutex_unlock(&player->lock);
1048 continue;
1049 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001050 //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 +08001051 if (read == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08001052 //file end.need to play next segment
hualing chen040df222020-01-17 13:35:02 +08001053 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen2aba4022020-03-02 13:49:55 +08001054 //init fffb time if change segment
hualing chen041c4092020-04-05 15:11:50 +08001055 _dvr_init_fffb_time((DVR_PlaybackHandle_t)player);
hualing chen31140872020-03-25 12:29:26 +08001056
1057 int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player);
hualing chenbcada022020-04-22 14:27:01 +08001058 //del delay time max check.
hualing chen39628212020-05-14 10:35:13 +08001059 if ((ret != DVR_SUCCESS &&
hualing chen041c4092020-04-05 15:11:50 +08001060 (delay <= MIN_TSPLAYER_DELAY_TIME ||
hualing chen4b7c15d2020-04-07 16:13:48 +08001061 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) &&
hualing chen39628212020-05-14 10:35:13 +08001062 _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) ||
1063 (reach_end_timeout >= MAX_REACHEND_TIMEOUT )) {
hualing chena540a7e2020-03-27 16:44:05 +08001064 //send end event to hal
hualing chen31140872020-03-25 12:29:26 +08001065 DVR_Play_Notify_t notify;
1066 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1067 notify.event = DVR_PLAYBACK_EVENT_REACHED_END;
1068 //get play statue not here
1069 dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen2932d372020-04-29 13:44:00 +08001070 _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, &notify, DVR_TRUE);
hualing chen31140872020-03-25 12:29:26 +08001071 //continue,timeshift mode, when read end,need wait cur recording segment
hualing chen39628212020-05-14 10:35:13 +08001072 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 +08001073 pthread_mutex_lock(&player->lock);
1074 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1075 pthread_mutex_unlock(&player->lock);
1076 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001077 } else if (ret != DVR_SUCCESS) {
1078 //not send event and pause,sleep and go to next time to recheck
hualing chen39628212020-05-14 10:35:13 +08001079 if (delay < cache_time) {
1080 //delay time is changed and then has data to play, so not start timeout
1081 } else {
1082 reach_end_timeout = reach_end_timeout + timeout;
1083 }
1084 cache_time = delay;
hualing chen4b7c15d2020-04-07 16:13:48 +08001085 DVR_PB_DG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player));
hualing chen31140872020-03-25 12:29:26 +08001086 pthread_mutex_lock(&player->lock);
1087 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1088 pthread_mutex_unlock(&player->lock);
1089 continue;
hualing chen86e7d482020-01-16 15:13:33 +08001090 }
hualing chen39628212020-05-14 10:35:13 +08001091 reach_end_timeout = 0;
1092 cache_time = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001093 pthread_mutex_lock(&player->lock);
hualing chen2932d372020-04-29 13:44:00 +08001094 //change next segment success case
1095 _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen4b7c15d2020-04-07 16:13:48 +08001096 DVR_PB_DG(1, "_dvr_replay_changed_pid:start");
hualing chencc91e1c2020-02-28 13:26:17 +08001097 _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player);
1098 _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player);
hualing chen86e7d482020-01-16 15:13:33 +08001099 read = segment_read(player->r_handle, buf + real_read, buf_len - real_read);
hualing chen87072a82020-03-12 16:20:12 +08001100 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001101 }
hualing chen39628212020-05-14 10:35:13 +08001102 reach_end_timeout = 0;
hualing chen86e7d482020-01-16 15:13:33 +08001103 real_read = real_read + read;
hualing chen2aba4022020-03-02 13:49:55 +08001104 wbufs.buf_size = real_read;
hualing chen2aba4022020-03-02 13:49:55 +08001105 wbufs.buf_data = buf;
hualing chena540a7e2020-03-27 16:44:05 +08001106 //check read data len,iflen < 0, we need continue
hualing chen7a56cba2020-04-14 14:09:27 +08001107 if (wbufs.buf_size <= 0 || wbufs.buf_data == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001108 DVR_PB_DG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data);
hualing chen5cbe1a62020-02-10 16:36:36 +08001109 real_read = 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001110 continue;
hualing chena540a7e2020-03-27 16:44:05 +08001111 }
hualing chen266b9502020-04-04 17:39:39 +08001112 //if need write whole block size, we need check read buf len is eq block size.
1113 if (b_writed_whole_block == DVR_TRUE) {
1114 //buf_len is block size value.
1115 if (real_read < buf_len) {
1116 //coontinue to read data from file
hualing chen4b7c15d2020-04-07 16:13:48 +08001117 DVR_PB_DG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001118 pthread_mutex_lock(&player->lock);
1119 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
1120 pthread_mutex_unlock(&player->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001121 DVR_PB_DG(1, "read buf len[%d] is < block size [%d] continue", real_read, buf_len);
hualing chen266b9502020-04-04 17:39:39 +08001122 continue;
1123 } else if (real_read > buf_len) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001124 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 +08001125 }
1126 }
1127
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001128 if (player->dec_func) {
1129 DVR_CryptoParams_t crypto_params;
1130
1131 memset(&crypto_params, 0, sizeof(crypto_params));
1132 crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT;
1133 memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location));
1134 crypto_params.segment_id = player->cur_segment.segment_id;
1135 crypto_params.offset = segment_tell_position(player->r_handle);
1136
1137 crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1138 crypto_params.input_buffer.addr = (size_t)buf;
1139 crypto_params.input_buffer.size = real_read;
1140
1141 if (player->is_secure_mode) {
1142 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE;
1143 crypto_params.output_buffer.addr = (size_t)player->secure_buffer;
1144 crypto_params.output_buffer.size = dec_buf_size;
1145 ret = player->dec_func(&crypto_params, player->dec_userdata);
1146 wbufs.buf_data = player->secure_buffer;
pengfei.liufda2a972020-04-09 14:47:15 +08001147 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001148 } else {
1149 crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL;
1150 crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data;
1151 crypto_params.output_buffer.size = dec_buf_size;
1152 ret = player->dec_func(&crypto_params, player->dec_userdata);
1153 wbufs.buf_data = dec_bufs.buf_data;
pengfei.liufda2a972020-04-09 14:47:15 +08001154 wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001155 }
1156 if (ret != DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001157 DVR_PB_DG(1, "decrypt failed");
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001158 }
pengfei.liufda2a972020-04-09 14:47:15 +08001159 wbufs.buf_size = crypto_params.output_size;
pengfei.liu27cc4ec2020-04-03 16:28:16 +08001160 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001161rewrite:
hualing chenbcada022020-04-22 14:27:01 +08001162 if (player->drop_ts == DVR_TRUE) {
1163 //need drop ts data when seek occur.we need read next loop,drop this ts data
1164 goto_rewrite = DVR_FALSE;
1165 real_read = 0;
1166 player->drop_ts = DVR_FALSE;
1167 continue;
1168 }
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001169 ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms);
1170 if (ret == AM_TSPLAYER_OK) {
hualing chena540a7e2020-03-27 16:44:05 +08001171 real_read = 0;
1172 write_success++;
1173 continue;
hualing chen87072a82020-03-12 16:20:12 +08001174 } else {
hualing chen2932d372020-04-29 13:44:00 +08001175 DVR_PB_DG(1, "write time out write_success:%d wbufs.buf_size:%d", write_success, wbufs.buf_size);
hualing chena540a7e2020-03-27 16:44:05 +08001176 write_success = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001177 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001178 _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout);
hualing chencc91e1c2020-02-28 13:26:17 +08001179 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001180 if (!player->is_running) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001181 DVR_PB_DG(1, "playback thread exit");
hualing chen86e7d482020-01-16 15:13:33 +08001182 break;
1183 }
hualing chen2aba4022020-03-02 13:49:55 +08001184 goto_rewrite = DVR_TRUE;
1185 //goto rewrite;
hualing chen86e7d482020-01-16 15:13:33 +08001186 }
1187 }
hualing chenb5cd42e2020-04-15 17:03:34 +08001188end:
hualing chen4b7c15d2020-04-07 16:13:48 +08001189 DVR_PB_DG(1, "playback thread is end");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001190 free(buf);
1191 free(dec_bufs.buf_data);
hualing chen86e7d482020-01-16 15:13:33 +08001192 return NULL;
hualing chenb31a6c62020-01-13 17:27:00 +08001193}
1194
1195
hualing chen040df222020-01-17 13:35:02 +08001196static int _start_playback_thread(DVR_PlaybackHandle_t handle)
hualing chenb31a6c62020-01-13 17:27:00 +08001197{
hualing chen040df222020-01-17 13:35:02 +08001198 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001199
1200 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001201 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001202 return DVR_FAILURE;
1203 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001204 DVR_PB_DG(1, "start thread is_running:[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001205 if (player->is_running == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001206 return 0;
hualing chen86e7d482020-01-16 15:13:33 +08001207 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001208 player->is_running = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001209 int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08001210 if (rc < 0)
1211 player->is_running = DVR_FALSE;
hualing chen86e7d482020-01-16 15:13:33 +08001212 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001213}
1214
1215
hualing chen040df222020-01-17 13:35:02 +08001216static int _stop_playback_thread(DVR_PlaybackHandle_t handle)
hualing chen86e7d482020-01-16 15:13:33 +08001217{
hualing chen040df222020-01-17 13:35:02 +08001218 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001219
1220 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001221 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001222 return DVR_FAILURE;
1223 }
1224
hualing chen4b7c15d2020-04-07 16:13:48 +08001225 DVR_PB_DG(1, "stopthread------[%d]", player->is_running);
hualing chencc91e1c2020-02-28 13:26:17 +08001226 if (player->is_running == DVR_TRUE)
hualing chen86e7d482020-01-16 15:13:33 +08001227 {
1228 player->is_running = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001229 _dvr_playback_sendSignal(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001230 pthread_join(player->playback_thread, NULL);
1231 }
1232 if (player->r_handle) {
1233 segment_close(player->r_handle);
1234 player->r_handle = NULL;
1235 }
hualing chen7a56cba2020-04-14 14:09:27 +08001236 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001237 return 0;
1238}
1239
hualing chenb31a6c62020-01-13 17:27:00 +08001240/**\brief Open an dvr palyback
1241 * \param[out] p_handle dvr playback addr
1242 * \param[in] params dvr playback open parameters
1243 * \retval DVR_SUCCESS On success
1244 * \return Error code
1245 */
hualing chen040df222020-01-17 13:35:02 +08001246int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) {
hualing chenb31a6c62020-01-13 17:27:00 +08001247
hualing chen040df222020-01-17 13:35:02 +08001248 DVR_Playback_t *player;
hualing chen86e7d482020-01-16 15:13:33 +08001249 pthread_condattr_t cattr;
hualing chenb31a6c62020-01-13 17:27:00 +08001250
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001251 player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001252
hualing chen86e7d482020-01-16 15:13:33 +08001253 pthread_mutex_init(&player->lock, NULL);
hualing chen2aba4022020-03-02 13:49:55 +08001254 pthread_mutex_init(&player->segment_lock, NULL);
hualing chen86e7d482020-01-16 15:13:33 +08001255 pthread_condattr_init(&cattr);
1256 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
1257 pthread_cond_init(&player->cond, &cattr);
1258 pthread_condattr_destroy(&cattr);
hualing chenb31a6c62020-01-13 17:27:00 +08001259
hualing chen5cbe1a62020-02-10 16:36:36 +08001260 //init segment list head
hualing chen040df222020-01-17 13:35:02 +08001261 INIT_LIST_HEAD(&player->segment_list);
1262 player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP;
1263 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
hualing chen5cbe1a62020-02-10 16:36:36 +08001264 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
hualing chen040df222020-01-17 13:35:02 +08001265 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
hualing chen2aba4022020-03-02 13:49:55 +08001266 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001267 player->cmd.pos = 0;
hualing chen31140872020-03-25 12:29:26 +08001268 player->speed = 1.0f;
hualing chen2aba4022020-03-02 13:49:55 +08001269
hualing chen86e7d482020-01-16 15:13:33 +08001270 //store open params
hualing chen040df222020-01-17 13:35:02 +08001271 player->openParams.dmx_dev_id = params->dmx_dev_id;
1272 player->openParams.block_size = params->block_size;
hualing chen86e7d482020-01-16 15:13:33 +08001273 player->openParams.is_timeshift = params->is_timeshift;
hualing chencc91e1c2020-02-28 13:26:17 +08001274 player->openParams.event_fn = params->event_fn;
1275 player->openParams.event_userdata = params->event_userdata;
1276
hualing chen5cbe1a62020-02-10 16:36:36 +08001277 player->has_pids = params->has_pids;
1278
hualing chen2aba4022020-03-02 13:49:55 +08001279 player->handle = params->player_handle ;
hualing chen6e4bfa52020-03-13 14:37:11 +08001280
1281 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
1282 //for test get callback
1283 if (0 && player->player_callback_func == NULL) {
1284 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player);
1285 AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata);
hualing chen4b7c15d2020-04-07 16:13:48 +08001286 DVR_PB_DG(1, "playback open get callback[%p][%p][%p][%p]",player->player_callback_func, player->player_callback_userdata, _dvr_tsplayer_callback_test,player);
hualing chen6e4bfa52020-03-13 14:37:11 +08001287 }
1288 AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player);
hualing chen040df222020-01-17 13:35:02 +08001289
hualing chen86e7d482020-01-16 15:13:33 +08001290 //init has audio and video
1291 player->has_video = DVR_FALSE;
1292 player->has_audio = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08001293 player->cur_segment_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001294 player->last_segment_id = 0LL;
1295 player->segment_is_open = DVR_FALSE;
hualing chenb31a6c62020-01-13 17:27:00 +08001296
hualing chen5cbe1a62020-02-10 16:36:36 +08001297 //init ff fb time
1298 player->fffb_current = -1;
1299 player->fffb_start =-1;
1300 player->fffb_start_pcr = -1;
1301 //seek time
1302 player->seek_time = 0;
hualing chen6e4bfa52020-03-13 14:37:11 +08001303 player->send_time = 0;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001304
1305 //init secure stuff
1306 player->dec_func = NULL;
1307 player->dec_userdata = NULL;
1308 player->is_secure_mode = 0;
1309 player->secure_buffer = NULL;
1310 player->secure_buffer_size = 0;
hualing chen266b9502020-04-04 17:39:39 +08001311 player->drop_ts = DVR_FALSE;
pengfei.liu07ddc8a2020-03-24 23:36:53 +08001312
hualing chen4b7c15d2020-04-07 16:13:48 +08001313 player->fffb_play = DVR_FALSE;
1314
1315 player->last_send_time_id = UINT64_MAX;
1316 player->last_cur_time = 0;
1317
hualing chen86e7d482020-01-16 15:13:33 +08001318 *p_handle = player;
1319 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001320}
1321
1322/**\brief Close an dvr palyback
1323 * \param[in] handle playback handle
1324 * \retval DVR_SUCCESS On success
1325 * \return Error code
1326 */
hualing chen040df222020-01-17 13:35:02 +08001327int dvr_playback_close(DVR_PlaybackHandle_t handle) {
hualing chenb31a6c62020-01-13 17:27:00 +08001328
hualing chen86e7d482020-01-16 15:13:33 +08001329 DVR_ASSERT(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001330 DVR_PB_DG(1, ":into");
hualing chen040df222020-01-17 13:35:02 +08001331 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001332 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001333 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001334 return DVR_FAILURE;
1335 }
1336
hualing chencc91e1c2020-02-28 13:26:17 +08001337 if (player->state != DVR_PLAYBACK_STATE_STOP)
1338 {
hualing chenb96aa2c2020-04-15 14:13:53 +08001339 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chencc91e1c2020-02-28 13:26:17 +08001340 dvr_playback_stop(handle, DVR_TRUE);
hualing chenb96aa2c2020-04-15 14:13:53 +08001341 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
1342 } else {
1343 DVR_PB_DG(1, ":is stoped state");
hualing chencc91e1c2020-02-28 13:26:17 +08001344 }
hualing chen7a56cba2020-04-14 14:09:27 +08001345 DVR_PB_DG(1, ":into");
hualing chen86e7d482020-01-16 15:13:33 +08001346 pthread_mutex_destroy(&player->lock);
1347 pthread_cond_destroy(&player->cond);
hualing chen040df222020-01-17 13:35:02 +08001348
1349 if (player) {
1350 free(player);
1351 player = NULL;
1352 }
hualing chen7a56cba2020-04-14 14:09:27 +08001353 DVR_PB_DG(1, ":end");
hualing chen86e7d482020-01-16 15:13:33 +08001354 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001355}
1356
hualing chenb31a6c62020-01-13 17:27:00 +08001357/**\brief Start play audio and video, used start auido api and start video api
1358 * \param[in] handle playback handle
1359 * \param[in] params audio playback params,contains fmt and pid...
1360 * \retval DVR_SUCCESS On success
1361 * \return Error code
1362 */
hualing chen040df222020-01-17 13:35:02 +08001363int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) {
1364 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen2aba4022020-03-02 13:49:55 +08001365 am_tsplayer_video_params vparams;
1366 am_tsplayer_audio_params aparams;
hualing chena540a7e2020-03-27 16:44:05 +08001367
1368 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001369 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001370 return DVR_FAILURE;
1371 }
hualing chencc91e1c2020-02-28 13:26:17 +08001372 uint64_t segment_id = player->cur_segment_id;
hualing chen4b7c15d2020-04-07 16:13:48 +08001373 DVR_PB_DG(1, "[%p]segment_id:[%lld]", handle, segment_id);
hualing chenb31a6c62020-01-13 17:27:00 +08001374
hualing chena540a7e2020-03-27 16:44:05 +08001375 player->first_frame = 0;
hualing chencc91e1c2020-02-28 13:26:17 +08001376 //can used start api to resume playback
1377 if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1378 return dvr_playback_resume(handle);
1379 }
hualing chen87072a82020-03-12 16:20:12 +08001380 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001381 DVR_PB_DG(1, "stat is start, not need into start play");
hualing chen87072a82020-03-12 16:20:12 +08001382 return DVR_SUCCESS;
1383 }
hualing chen86e7d482020-01-16 15:13:33 +08001384 player->play_flag = flag;
hualing chen5cbe1a62020-02-10 16:36:36 +08001385 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08001386 DVR_PB_DG(1, "lock flag:0x%x", flag);
hualing chen86e7d482020-01-16 15:13:33 +08001387 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001388 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams);
hualing chen86e7d482020-01-16 15:13:33 +08001389 //start audio and video
1390 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
1391 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08001392 DVR_PB_DG(0, "unlock dvr play back start error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08001393 pthread_mutex_unlock(&player->lock);
1394 DVR_Play_Notify_t notify;
1395 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
1396 notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED;
1397 notify.info.error_reason = DVR_PLAYBACK_PID_ERROR;
1398 notify.info.transition_failed_data.segment_id = segment_id;
1399 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08001400 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, &notify, DVR_TRUE);
hualing chen86e7d482020-01-16 15:13:33 +08001401 return -1;
1402 }
hualing chen31140872020-03-25 12:29:26 +08001403
hualing chencc91e1c2020-02-28 13:26:17 +08001404 {
hualing chen86e7d482020-01-16 15:13:33 +08001405 if (VALID_PID(vparams.pid)) {
1406 player->has_video = DVR_TRUE;
hualing chen86e7d482020-01-16 15:13:33 +08001407 //if set flag is pause live, we need set trick mode
hualing chen31140872020-03-25 12:29:26 +08001408 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001409 DVR_PB_DG(1, "set trick mode -pauselive flag--");
hualing chen31140872020-03-25 12:29:26 +08001410 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1411 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
hualing chen2aba4022020-03-02 13:49:55 +08001412 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001413 DVR_PB_DG(1, "set trick mode -fffb--at pause live");
hualing chen2aba4022020-03-02 13:49:55 +08001414 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08001415 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08001416 DVR_PB_DG(1, "set trick mode ---none");
hualing chen87072a82020-03-12 16:20:12 +08001417 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08001418 }
1419 AmTsPlayer_setVideoParams(player->handle, &vparams);
1420 AmTsPlayer_startVideoDecoding(player->handle);
hualing chenb31a6c62020-01-13 17:27:00 +08001421 }
hualing chena540a7e2020-03-27 16:44:05 +08001422
hualing chen4b7c15d2020-04-07 16:13:48 +08001423 DVR_PB_DG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid);
1424 player->last_send_time_id = UINT64_MAX;
hualing chencc91e1c2020-02-28 13:26:17 +08001425 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB
1426 || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) {
1427 player->cmd.state = DVR_PLAYBACK_STATE_START;
1428 player->state = DVR_PLAYBACK_STATE_START;
hualing chencc91e1c2020-02-28 13:26:17 +08001429 } else {
1430 player->cmd.last_cmd = player->cmd.cur_cmd;
1431 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chena540a7e2020-03-27 16:44:05 +08001432 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08001433 //set fast play
hualing chenb96aa2c2020-04-15 14:13:53 +08001434 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08001435 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f);
hualing chena540a7e2020-03-27 16:44:05 +08001436 } else {
1437 if (VALID_PID(aparams.pid)) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001438 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08001439 player->has_audio = DVR_TRUE;
1440 AmTsPlayer_setAudioParams(player->handle, &aparams);
1441 AmTsPlayer_startAudioDecoding(player->handle);
1442 }
hualing chen31140872020-03-25 12:29:26 +08001443 }
hualing chencc91e1c2020-02-28 13:26:17 +08001444 player->cmd.state = DVR_PLAYBACK_STATE_START;
1445 player->state = DVR_PLAYBACK_STATE_START;
1446 }
hualing chen86e7d482020-01-16 15:13:33 +08001447 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001448 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001449 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001450 _start_playback_thread(handle);
hualing chen86e7d482020-01-16 15:13:33 +08001451 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001452}
hualing chen040df222020-01-17 13:35:02 +08001453/**\brief dvr play back add segment info to segment list
hualing chenb31a6c62020-01-13 17:27:00 +08001454 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001455 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001456 * \retval DVR_SUCCESS On success
1457 * \return Error code
1458 */
hualing chen040df222020-01-17 13:35:02 +08001459int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) {
1460 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08001461
hualing chena540a7e2020-03-27 16:44:05 +08001462 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001463 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001464 return DVR_FAILURE;
1465 }
1466
hualing chen4b7c15d2020-04-07 16:13:48 +08001467 DVR_PB_DG(1, "add segment id: %lld %p", info->segment_id, handle);
hualing chen040df222020-01-17 13:35:02 +08001468 DVR_PlaybackSegmentInfo_t *segment;
hualing chenb31a6c62020-01-13 17:27:00 +08001469
hualing chen040df222020-01-17 13:35:02 +08001470 segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t));
1471 memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t));
hualing chenb31a6c62020-01-13 17:27:00 +08001472
hualing chen86e7d482020-01-16 15:13:33 +08001473 //not memcpy chun info.
hualing chen040df222020-01-17 13:35:02 +08001474 segment->segment_id = info->segment_id;
hualing chen86e7d482020-01-16 15:13:33 +08001475 //cp location
hualing chen040df222020-01-17 13:35:02 +08001476 memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE);
hualing chencc91e1c2020-02-28 13:26:17 +08001477
hualing chen4b7c15d2020-04-07 16:13:48 +08001478 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 +08001479 segment->flags = info->flags;
hualing chen5cbe1a62020-02-10 16:36:36 +08001480
1481 //pids
hualing chencc91e1c2020-02-28 13:26:17 +08001482 segment->pids.video.pid = info->pids.video.pid;
1483 segment->pids.video.format = info->pids.video.format;
1484 segment->pids.video.type = info->pids.video.type;
1485
hualing chen2aba4022020-03-02 13:49:55 +08001486 segment->pids.audio.pid = info->pids.audio.pid;
1487 segment->pids.audio.format = info->pids.audio.format;
1488 segment->pids.audio.type = info->pids.audio.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001489
hualing chen2aba4022020-03-02 13:49:55 +08001490 segment->pids.ad.pid = info->pids.ad.pid;
1491 segment->pids.ad.format = info->pids.ad.format;
1492 segment->pids.ad.type = info->pids.ad.type;
hualing chencc91e1c2020-02-28 13:26:17 +08001493
1494 segment->pids.pcr.pid = info->pids.pcr.pid;
1495
hualing chen4b7c15d2020-04-07 16:13:48 +08001496 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 +08001497 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001498 list_add_tail(&segment->head, &player->segment_list);
hualing chen86e7d482020-01-16 15:13:33 +08001499 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001500 DVR_PB_DG(1, "unlock");
hualing chenb31a6c62020-01-13 17:27:00 +08001501
hualing chen5cbe1a62020-02-10 16:36:36 +08001502 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001503}
hualing chen040df222020-01-17 13:35:02 +08001504/**\brief dvr play back remove segment info by segment_id
hualing chenb31a6c62020-01-13 17:27:00 +08001505 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001506 * \param[in] segment_id need removed segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001507 * \retval DVR_SUCCESS On success
1508 * \return Error code
1509 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001510int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08001511 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001512 DVR_PB_DG(1, "remove segment id: %lld", segment_id);
hualing chena540a7e2020-03-27 16:44:05 +08001513 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001514 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001515 return DVR_FAILURE;
1516 }
1517
hualing chencc91e1c2020-02-28 13:26:17 +08001518 if (segment_id == player->cur_segment_id) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001519 DVR_PB_DG(1, "not suport remove curren segment id: %lld", segment_id);
hualing chencc91e1c2020-02-28 13:26:17 +08001520 return DVR_FAILURE;
1521 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001522 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001523 pthread_mutex_lock(&player->lock);
hualing chena540a7e2020-03-27 16:44:05 +08001524 DVR_PlaybackSegmentInfo_t *segment = NULL;
1525 DVR_PlaybackSegmentInfo_t *segment_tmp = NULL;
1526 list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001527 {
hualing chen040df222020-01-17 13:35:02 +08001528 if (segment->segment_id == segment_id) {
1529 list_del(&segment->head);
1530 free(segment);
hualing chen86e7d482020-01-16 15:13:33 +08001531 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001532 }
hualing chen86e7d482020-01-16 15:13:33 +08001533 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001534 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001535 pthread_mutex_unlock(&player->lock);
1536
1537 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001538}
hualing chen040df222020-01-17 13:35:02 +08001539/**\brief dvr play back add segment info
hualing chenb31a6c62020-01-13 17:27:00 +08001540 * \param[in] handle playback handle
hualing chen040df222020-01-17 13:35:02 +08001541 * \param[in] info added segment info,con vpid fmt apid fmt.....
hualing chenb31a6c62020-01-13 17:27:00 +08001542 * \retval DVR_SUCCESS On success
1543 * \return Error code
1544 */
hualing chen040df222020-01-17 13:35:02 +08001545int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle,
hualing chen5cbe1a62020-02-10 16:36:36 +08001546 uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) {
hualing chen040df222020-01-17 13:35:02 +08001547 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chen4b7c15d2020-04-07 16:13:48 +08001548 DVR_PB_DG(1, "update segment id: %lld flag:%d", segment_id, flags);
hualing chena540a7e2020-03-27 16:44:05 +08001549 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001550 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001551 return DVR_FAILURE;
1552 }
1553
hualing chen040df222020-01-17 13:35:02 +08001554 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001555 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001556 pthread_mutex_lock(&player->lock);
hualing chen040df222020-01-17 13:35:02 +08001557 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001558 {
hualing chen040df222020-01-17 13:35:02 +08001559 if (segment->segment_id != segment_id) {
hualing chen86e7d482020-01-16 15:13:33 +08001560 continue;
hualing chenb31a6c62020-01-13 17:27:00 +08001561 }
hualing chen86e7d482020-01-16 15:13:33 +08001562 // if encramble to free, only set flag and return;
1563
1564 //if displayable to none, we need mute audio and video
hualing chen040df222020-01-17 13:35:02 +08001565 if (segment_id == player->cur_segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001566 if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE
1567 && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) {
hualing chencc91e1c2020-02-28 13:26:17 +08001568 //disable display, mute
hualing chen2aba4022020-03-02 13:49:55 +08001569 AmTsPlayer_hideVideo(player->handle);
1570 AmTsPlayer_setAudioMute(player->handle, 1, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001571 } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 &&
1572 (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) {
hualing chencc91e1c2020-02-28 13:26:17 +08001573 //enable display, unmute
hualing chen2aba4022020-03-02 13:49:55 +08001574 AmTsPlayer_showVideo(player->handle);
1575 AmTsPlayer_setAudioMute(player->handle, 0, 0);
hualing chen86e7d482020-01-16 15:13:33 +08001576 } else {
1577 //do nothing
1578 }
1579 } else {
1580 //do nothing
1581 }
1582 //continue , only set flag
hualing chen040df222020-01-17 13:35:02 +08001583 segment->flags = flags;
hualing chen86e7d482020-01-16 15:13:33 +08001584 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001585 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001586 pthread_mutex_unlock(&player->lock);
1587 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001588}
1589
1590
hualing chen5cbe1a62020-02-10 16:36:36 +08001591static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type) {
hualing chen040df222020-01-17 13:35:02 +08001592 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001593 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001594 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001595 return DVR_FAILURE;
1596 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001597 DVR_PB_DG(1, " do check");
hualing chen86e7d482020-01-16 15:13:33 +08001598 if (now_pid.pid == set_pid.pid) {
1599 //do nothing
hualing chenb31a6c62020-01-13 17:27:00 +08001600 return 0;
hualing chen5cbe1a62020-02-10 16:36:36 +08001601 } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chen86e7d482020-01-16 15:13:33 +08001602 if (VALID_PID(now_pid.pid)) {
1603 //stop now stream
1604 if (type == 0) {
1605 //stop vieo
hualing chenc70a8df2020-05-12 19:23:11 +08001606 if (player->has_video == DVR_TRUE) {
1607 DVR_PB_DG(1, "stop video");
1608 AmTsPlayer_stopVideoDecoding(player->handle);
1609 player->has_video = DVR_FALSE;
1610 }
hualing chen86e7d482020-01-16 15:13:33 +08001611 } else if (type == 1) {
1612 //stop audio
hualing chenc70a8df2020-05-12 19:23:11 +08001613 if (player->has_audio == DVR_TRUE) {
1614 DVR_PB_DG(1, "stop audio");
1615 AmTsPlayer_stopAudioDecoding(player->handle);
1616 player->has_audio = DVR_FALSE;
1617 }
hualing chen86e7d482020-01-16 15:13:33 +08001618 } else if (type == 2) {
1619 //stop sub audio
hualing chen4b7c15d2020-04-07 16:13:48 +08001620 DVR_PB_DG(1, "stop ad");
hualing chena540a7e2020-03-27 16:44:05 +08001621 AmTsPlayer_disableADMix(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001622 } else if (type == 3) {
1623 //pcr
1624 }
1625 }
1626 if (VALID_PID(set_pid.pid)) {
1627 //start
1628 if (type == 0) {
1629 //start vieo
hualing chen2aba4022020-03-02 13:49:55 +08001630 am_tsplayer_video_params vparams;
hualing chen86e7d482020-01-16 15:13:33 +08001631 vparams.pid = set_pid.pid;
hualing chen2aba4022020-03-02 13:49:55 +08001632 vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08001633 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001634 DVR_PB_DG(1, "start video pid[%d]fmt[%d]",vparams.pid, vparams.codectype);
hualing chen2aba4022020-03-02 13:49:55 +08001635 AmTsPlayer_setVideoParams(player->handle, &vparams);
1636 AmTsPlayer_startVideoDecoding(player->handle);
1637 //playback_device_video_start(player->handle,&vparams);
hualing chen86e7d482020-01-16 15:13:33 +08001638 } else if (type == 1) {
1639 //start audio
hualing chenc70a8df2020-05-12 19:23:11 +08001640 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
1641 am_tsplayer_audio_params aparams;
1642 aparams.pid = set_pid.pid;
1643 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1644 player->has_audio = DVR_TRUE;
1645 DVR_PB_DG(1, "start audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1646 AmTsPlayer_setAudioParams(player->handle, &aparams);
1647 AmTsPlayer_startAudioDecoding(player->handle);
1648 //playback_device_audio_start(player->handle,&aparams);
1649 }
hualing chen86e7d482020-01-16 15:13:33 +08001650 } else if (type == 2) {
hualing chen39628212020-05-14 10:35:13 +08001651 if ((player->cmd.speed.speed.speed == PLAYBACK_SPEED_X1)) {
hualing chenc70a8df2020-05-12 19:23:11 +08001652 am_tsplayer_audio_params aparams;
1653 aparams.pid = set_pid.pid;
1654 aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE);
1655 player->has_audio = DVR_TRUE;
1656 DVR_PB_DG(1, "start ad audio pid[%d]fmt[%d]",aparams.pid, aparams.codectype);
1657 AmTsPlayer_setADParams(player->handle, &aparams);
1658 AmTsPlayer_enableADMix(player->handle);
1659 //playback_device_audio_start(player->handle,&aparams);
1660 }
hualing chen86e7d482020-01-16 15:13:33 +08001661 } else if (type == 3) {
1662 //pcr
hualing chen4b7c15d2020-04-07 16:13:48 +08001663 DVR_PB_DG(1, "start set pcr [%d]", set_pid.pid);
hualing chen2aba4022020-03-02 13:49:55 +08001664 AmTsPlayer_setPcrPid(player->handle, set_pid.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001665 }
hualing chen5cbe1a62020-02-10 16:36:36 +08001666 //audio and video all close
1667 if (!player->has_audio && !player->has_video) {
1668 player->state = DVR_PLAYBACK_STATE_STOP;
1669 }
hualing chen86e7d482020-01-16 15:13:33 +08001670 }
1671 }
1672 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08001673}
hualing chen5cbe1a62020-02-10 16:36:36 +08001674/**\brief dvr play back update segment pids
1675 * if updated segment is ongoing segment, we need start new
hualing chenb31a6c62020-01-13 17:27:00 +08001676 * add pid stream and stop remove pid stream.
1677 * \param[in] handle playback handle
hualing chen5cbe1a62020-02-10 16:36:36 +08001678 * \param[in] segment_id need updated pids segment id
hualing chenb31a6c62020-01-13 17:27:00 +08001679 * \retval DVR_SUCCESS On success
1680 * \return Error code
1681 */
hualing chen5cbe1a62020-02-10 16:36:36 +08001682int 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 +08001683 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001684 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001685 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001686 return DVR_FAILURE;
1687 }
1688
hualing chen040df222020-01-17 13:35:02 +08001689 DVR_PlaybackSegmentInfo_t *segment;
hualing chen4b7c15d2020-04-07 16:13:48 +08001690 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001691 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08001692 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 +08001693
hualing chen040df222020-01-17 13:35:02 +08001694 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08001695 {
hualing chen040df222020-01-17 13:35:02 +08001696 if (segment->segment_id == segment_id) {
hualing chen5cbe1a62020-02-10 16:36:36 +08001697
1698 if (player->cur_segment_id == segment_id) {
1699 if (player->cmd.state == DVR_PLAYBACK_STATE_FF
1700 || player->cmd.state == DVR_PLAYBACK_STATE_FF) {
1701 //do nothing when ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08001702 DVR_PB_DG(1, "unlock now is ff fb, not to update cur segment info\r\n");
hualing chencc91e1c2020-02-28 13:26:17 +08001703 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001704 return 0;
1705 }
1706
1707 //if segment is on going segment,we need stop start stream
1708 if (player->cmd.state == DVR_PLAYBACK_STATE_START) {
hualing chencc91e1c2020-02-28 13:26:17 +08001709 pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001710 //check video pids, stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001711 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, p_pids->video, 0);
hualing chen5cbe1a62020-02-10 16:36:36 +08001712 //check audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001713 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, p_pids->audio, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08001714 //check sub audio pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001715 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, p_pids->ad, 2);
hualing chen5cbe1a62020-02-10 16:36:36 +08001716 //check pcr pids stop or restart
hualing chencc91e1c2020-02-28 13:26:17 +08001717 _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, p_pids->pcr, 3);
1718 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08001719 } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) {
1720 //if state is pause, we need process at resume api. we only record change info
1721 int v_cmd = DVR_PLAYBACK_CMD_NONE;
1722 int a_cmd = DVR_PLAYBACK_CMD_NONE;
1723 if (VALID_PID(segment->pids.video.pid)
1724 && VALID_PID(p_pids->video.pid)
1725 && segment->pids.video.pid != p_pids->video.pid) {
1726 //restart video
1727 v_cmd = DVR_PLAYBACK_CMD_VRESTART;
1728 }
1729 if (!VALID_PID(segment->pids.video.pid)
1730 && VALID_PID(p_pids->video.pid)
1731 && segment->pids.video.pid != p_pids->video.pid) {
1732 //start video
1733 v_cmd = DVR_PLAYBACK_CMD_VSTART;
1734 }
1735 if (VALID_PID(segment->pids.video.pid)
1736 && !VALID_PID(p_pids->video.pid)
1737 && segment->pids.video.pid != p_pids->video.pid) {
1738 //stop video
1739 v_cmd = DVR_PLAYBACK_CMD_VSTOP;
1740 }
1741 if (VALID_PID(segment->pids.audio.pid)
1742 && VALID_PID(p_pids->audio.pid)
1743 && segment->pids.audio.pid != p_pids->audio.pid) {
1744 //restart audio
1745 a_cmd = DVR_PLAYBACK_CMD_ARESTART;
1746 }
1747 if (!VALID_PID(segment->pids.audio.pid)
1748 && VALID_PID(p_pids->audio.pid)
1749 && segment->pids.audio.pid != p_pids->audio.pid) {
1750 //start audio
1751 a_cmd = DVR_PLAYBACK_CMD_ASTART;
1752 }
1753 if (VALID_PID(segment->pids.audio.pid)
1754 && !VALID_PID(p_pids->audio.pid)
1755 && segment->pids.audio.pid != p_pids->audio.pid) {
1756 //stop audio
1757 a_cmd = DVR_PLAYBACK_CMD_ASTOP;
1758 }
1759 if (a_cmd == DVR_PLAYBACK_CMD_NONE
1760 && v_cmd == DVR_PLAYBACK_CMD_NONE) {
1761 //do nothing
1762 } else if (a_cmd == DVR_PLAYBACK_CMD_NONE
1763 || v_cmd == DVR_PLAYBACK_CMD_NONE) {
1764 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1765 player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd;
1766 } else if (a_cmd != DVR_PLAYBACK_CMD_NONE
1767 && v_cmd != DVR_PLAYBACK_CMD_NONE) {
1768 if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1769 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1770 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1771 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
1772 }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART
1773 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1774 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1775 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART;
1776 } else {
1777 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1778 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART;
1779 }
1780
1781 if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1782 && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) {
1783 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1784 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART;
1785 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART
1786 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1787 //not occur this case
1788 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1789 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
1790 } else {
1791 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1792 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART;
1793 }
1794
1795 if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1796 && a_cmd == DVR_PLAYBACK_CMD_ASTART) {
1797 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1798 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART;
1799 } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP
1800 && a_cmd == DVR_PLAYBACK_CMD_ARESTART) {
1801 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1802 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART;
1803 } else {
1804 //not occur this case
1805 player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE;
1806 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1807 }
1808 }
1809 }
hualing chene10666f2020-04-14 13:58:37 +08001810 memcpy(&player->cur_segment.pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chen5cbe1a62020-02-10 16:36:36 +08001811 }
hualing chen86e7d482020-01-16 15:13:33 +08001812 //save pids info
hualing chenb5cd42e2020-04-15 17:03:34 +08001813 DVR_PB_DG(1, ":apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen040df222020-01-17 13:35:02 +08001814 memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t));
hualing chenb5cd42e2020-04-15 17:03:34 +08001815 DVR_PB_DG(1, ":cp apid :%d %d", segment->pids.audio.pid, p_pids->audio.pid);
hualing chen86e7d482020-01-16 15:13:33 +08001816 break;
hualing chenb31a6c62020-01-13 17:27:00 +08001817 }
hualing chen86e7d482020-01-16 15:13:33 +08001818 }
hualing chen4b7c15d2020-04-07 16:13:48 +08001819 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001820 pthread_mutex_unlock(&player->lock);
1821 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001822}
1823/**\brief Stop play, will stop video and audio
1824 * \param[in] handle playback handle
1825 * \param[in] clear is clear last frame
1826 * \retval DVR_SUCCESS On success
1827 * \return Error code
1828 */
hualing chen040df222020-01-17 13:35:02 +08001829int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) {
1830 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001831
1832 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001833 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001834 return DVR_FAILURE;
1835 }
hualing chenb96aa2c2020-04-15 14:13:53 +08001836 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1837 DVR_PB_DG(1, ":playback is stoped");
1838 return DVR_SUCCESS;
1839 }
Ke Gong3c0caba2020-04-21 22:58:18 -07001840 if (player->state == DVR_PLAYBACK_STATE_STOP) {
1841 DVR_PB_DG(1, ":playback is stoped");
1842 return DVR_SUCCESS;
1843 }
hualing chen7a56cba2020-04-14 14:09:27 +08001844 DVR_PB_DG(1, ":into");
hualing chen87072a82020-03-12 16:20:12 +08001845 _stop_playback_thread(handle);
hualing chen7a56cba2020-04-14 14:09:27 +08001846 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001847 pthread_mutex_lock(&player->lock);
hualing chen7a56cba2020-04-14 14:09:27 +08001848 DVR_PB_DG(1, ":get lock into stop fast");
hualing chen31140872020-03-25 12:29:26 +08001849 AmTsPlayer_stopFast(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08001850 if (player->has_video) {
1851 AmTsPlayer_resumeVideoDecoding(player->handle);
1852 }
1853 if (player->has_audio) {
1854 AmTsPlayer_resumeAudioDecoding(player->handle);
1855 }
hualing chen7a56cba2020-04-14 14:09:27 +08001856 DVR_PB_DG(1, ":into");
hualing chen266b9502020-04-04 17:39:39 +08001857 if (player->has_video) {
1858 player->has_video = DVR_FALSE;
1859 AmTsPlayer_showVideo(player->handle);
1860 AmTsPlayer_stopVideoDecoding(player->handle);
1861 }
hualing chen7a56cba2020-04-14 14:09:27 +08001862 DVR_PB_DG(1, ":into");
hualing chen266b9502020-04-04 17:39:39 +08001863 if (player->has_audio) {
1864 player->has_audio = DVR_FALSE;
1865 AmTsPlayer_stopAudioDecoding(player->handle);
1866 }
hualing chen7a56cba2020-04-14 14:09:27 +08001867 DVR_PB_DG(1, ":into");
hualing chen266b9502020-04-04 17:39:39 +08001868
hualing chen86e7d482020-01-16 15:13:33 +08001869 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001870 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP;
1871 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1872 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen87072a82020-03-12 16:20:12 +08001873 player->cur_segment_id = UINT64_MAX;
1874 player->segment_is_open = DVR_FALSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08001875 DVR_PB_DG(1, "unlock");
hualing chenb96aa2c2020-04-15 14:13:53 +08001876 DVR_PB_DG(1, "player->state %s", _dvr_playback_state_toString(player->state));
hualing chen86e7d482020-01-16 15:13:33 +08001877 pthread_mutex_unlock(&player->lock);
hualing chen86e7d482020-01-16 15:13:33 +08001878 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001879}
1880/**\brief Start play audio
1881 * \param[in] handle playback handle
1882 * \param[in] params audio playback params,contains fmt and pid...
1883 * \retval DVR_SUCCESS On success
1884 * \return Error code
1885 */
hualing chen2aba4022020-03-02 13:49:55 +08001886
1887int dvr_playback_audio_start(DVR_PlaybackHandle_t handle, am_tsplayer_audio_params *param) {
hualing chen040df222020-01-17 13:35:02 +08001888 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001889
1890 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001891 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001892 return DVR_FAILURE;
1893 }
hualing chen86e7d482020-01-16 15:13:33 +08001894 _start_playback_thread(handle);
1895 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08001896 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001897 pthread_mutex_lock(&player->lock);
1898 player->has_audio = DVR_TRUE;
hualing chen2aba4022020-03-02 13:49:55 +08001899 AmTsPlayer_setAudioParams(player->handle, param);
1900 AmTsPlayer_startAudioDecoding(player->handle);
1901 //playback_device_audio_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08001902 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001903 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART;
1904 player->cmd.state = DVR_PLAYBACK_STATE_START;
1905 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08001906 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001907 pthread_mutex_unlock(&player->lock);
1908 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001909}
1910/**\brief Stop play audio
1911 * \param[in] handle playback handle
1912 * \retval DVR_SUCCESS On success
1913 * \return Error code
1914 */
hualing chen040df222020-01-17 13:35:02 +08001915int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) {
1916 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001917
1918 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001919 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001920 return DVR_FAILURE;
1921 }
1922
hualing chen2aba4022020-03-02 13:49:55 +08001923 //playback_device_audio_stop(player->handle);
hualing chen86e7d482020-01-16 15:13:33 +08001924 if (player->has_video == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08001925 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1926 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001927 //destory thread
1928 _stop_playback_thread(handle);
1929 } else {
1930 //do nothing.video is playing
1931 }
hualing chen7a56cba2020-04-14 14:09:27 +08001932 DVR_PB_DG(1, "lock");
1933 pthread_mutex_lock(&player->lock);
1934
hualing chen87072a82020-03-12 16:20:12 +08001935 player->has_audio = DVR_FALSE;
1936
1937 AmTsPlayer_stopAudioDecoding(player->handle);
1938 player->cmd.last_cmd = player->cmd.cur_cmd;
1939 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP;
1940
hualing chen4b7c15d2020-04-07 16:13:48 +08001941 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001942 pthread_mutex_unlock(&player->lock);
1943 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08001944}
1945/**\brief Start play video
1946 * \param[in] handle playback handle
1947 * \param[in] params video playback params,contains fmt and pid...
1948 * \retval DVR_SUCCESS On success
1949 * \return Error code
1950 */
hualing chen2aba4022020-03-02 13:49:55 +08001951int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) {
hualing chen040df222020-01-17 13:35:02 +08001952 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001953
1954 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001955 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001956 return DVR_FAILURE;
1957 }
1958
hualing chen86e7d482020-01-16 15:13:33 +08001959 _start_playback_thread(handle);
1960 //start audio and video
hualing chen4b7c15d2020-04-07 16:13:48 +08001961 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08001962 pthread_mutex_lock(&player->lock);
1963 player->has_video = DVR_TRUE;
hualing chena540a7e2020-03-27 16:44:05 +08001964 AmTsPlayer_setVideoParams(player->handle, param);
1965 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08001966
1967 //playback_device_video_start(player->handle , param);
hualing chen86e7d482020-01-16 15:13:33 +08001968 //if set flag is pause live, we need set trick mode
hualing chen5cbe1a62020-02-10 16:36:36 +08001969 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001970 DVR_PB_DG(1, "settrick mode at video start");
hualing chen2aba4022020-03-02 13:49:55 +08001971 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
1972 //playback_device_trick_mode(player->handle, 1);
hualing chen86e7d482020-01-16 15:13:33 +08001973 }
1974 player->cmd.last_cmd = player->cmd.cur_cmd;
hualing chen040df222020-01-17 13:35:02 +08001975 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART;
1976 player->cmd.state = DVR_PLAYBACK_STATE_START;
1977 player->state = DVR_PLAYBACK_STATE_START;
hualing chen4b7c15d2020-04-07 16:13:48 +08001978 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08001979 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08001980 return DVR_SUCCESS;
1981}
1982/**\brief Stop play video
1983 * \param[in] handle playback handle
1984 * \retval DVR_SUCCESS On success
1985 * \return Error code
1986 */
hualing chen040df222020-01-17 13:35:02 +08001987int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) {
1988 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08001989
1990 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08001991 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08001992 return DVR_FAILURE;
1993 }
1994
hualing chen86e7d482020-01-16 15:13:33 +08001995 if (player->has_audio == DVR_FALSE) {
hualing chen040df222020-01-17 13:35:02 +08001996 player->cmd.state = DVR_PLAYBACK_STATE_STOP;
1997 player->state = DVR_PLAYBACK_STATE_STOP;
hualing chen86e7d482020-01-16 15:13:33 +08001998 //destory thread
1999 _stop_playback_thread(handle);
2000 } else {
2001 //do nothing.audio is playing
2002 }
hualing chen7a56cba2020-04-14 14:09:27 +08002003
2004 DVR_PB_DG(1, "lock");
2005 pthread_mutex_lock(&player->lock);
2006
hualing chen87072a82020-03-12 16:20:12 +08002007 player->has_video = DVR_FALSE;
2008
2009 AmTsPlayer_stopVideoDecoding(player->handle);
2010 //playback_device_video_stop(player->handle);
2011
2012 player->cmd.last_cmd = player->cmd.cur_cmd;
2013 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP;
2014
hualing chen4b7c15d2020-04-07 16:13:48 +08002015 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002016 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002017 return DVR_SUCCESS;
2018}
2019/**\brief Pause play
2020 * \param[in] handle playback handle
2021 * \param[in] flush whether its internal buffers should be flushed
2022 * \retval DVR_SUCCESS On success
2023 * \return Error code
2024 */
hualing chen040df222020-01-17 13:35:02 +08002025int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) {
2026 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002027
2028 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002029 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002030 return DVR_FAILURE;
2031 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002032 DVR_PB_DG(1, "lock");
hualing chen86e7d482020-01-16 15:13:33 +08002033 pthread_mutex_lock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002034 DVR_PB_DG(1, "get lock");
hualing chen266b9502020-04-04 17:39:39 +08002035 if (player->has_video)
2036 AmTsPlayer_pauseVideoDecoding(player->handle);
2037 if (player->has_video)
2038 AmTsPlayer_pauseAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002039
2040 //playback_device_pause(player->handle);
hualing chen87072a82020-03-12 16:20:12 +08002041 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2042 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2043 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2044 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002045 } else {
2046 player->cmd.last_cmd = player->cmd.cur_cmd;
2047 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE;
2048 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2049 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen87072a82020-03-12 16:20:12 +08002050 }
hualing chen86e7d482020-01-16 15:13:33 +08002051 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002052 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002053
hualing chen86e7d482020-01-16 15:13:33 +08002054 return DVR_SUCCESS;
hualing chenb31a6c62020-01-13 17:27:00 +08002055}
2056
hualing chen5cbe1a62020-02-10 16:36:36 +08002057//not add lock
2058static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd)
2059{
2060 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2061
hualing chena540a7e2020-03-27 16:44:05 +08002062 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002063 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002064 return DVR_FAILURE;
2065 }
2066
hualing chen5cbe1a62020-02-10 16:36:36 +08002067 //get video params and audio params
hualing chen4b7c15d2020-04-07 16:13:48 +08002068 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002069 pthread_mutex_lock(&player->lock);
hualing chen2aba4022020-03-02 13:49:55 +08002070 am_tsplayer_video_params vparams;
2071 am_tsplayer_audio_params aparams;
hualing chencc91e1c2020-02-28 13:26:17 +08002072 uint64_t segmentid = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002073
2074 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams);
hualing chen4b7c15d2020-04-07 16:13:48 +08002075 DVR_PB_DG(1, "unlock cmd: %d", cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002076 pthread_mutex_unlock(&player->lock);
2077
2078 switch (cmd) {
2079 case DVR_PLAYBACK_CMD_AVRESTART:
2080 //av restart
hualing chen4b7c15d2020-04-07 16:13:48 +08002081 DVR_PB_DG(1, "do_cmd avrestart");
hualing chen87072a82020-03-12 16:20:12 +08002082 _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE);
hualing chen5cbe1a62020-02-10 16:36:36 +08002083 break;
2084 case DVR_PLAYBACK_CMD_VRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002085 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2086 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002087 break;
2088 case DVR_PLAYBACK_CMD_VSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002089 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002090 break;
2091 case DVR_PLAYBACK_CMD_VSTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002092 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002093 break;
2094 case DVR_PLAYBACK_CMD_ARESTART:
2095 //a restart
hualing chen2aba4022020-03-02 13:49:55 +08002096 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2097 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002098 break;
2099 case DVR_PLAYBACK_CMD_ASTART:
hualing chen2aba4022020-03-02 13:49:55 +08002100 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002101 break;
2102 case DVR_PLAYBACK_CMD_ASTOP:
hualing chen2aba4022020-03-02 13:49:55 +08002103 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002104 break;
2105 case DVR_PLAYBACK_CMD_ASTOPVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002106 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2107 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2108 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002109 break;
2110 case DVR_PLAYBACK_CMD_ASTOPVSTART:
hualing chen2aba4022020-03-02 13:49:55 +08002111 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2112 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002113 break;
2114 case DVR_PLAYBACK_CMD_VSTOPARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002115 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2116 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2117 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002118 break;
2119 case DVR_PLAYBACK_CMD_STOP:
2120 break;
2121 case DVR_PLAYBACK_CMD_START:
2122 break;
2123 case DVR_PLAYBACK_CMD_ASTARTVRESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002124 dvr_playback_video_stop((DVR_PlaybackHandle_t)player);
2125 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
2126 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002127 break;
2128 case DVR_PLAYBACK_CMD_VSTARTARESTART:
hualing chen2aba4022020-03-02 13:49:55 +08002129 dvr_playback_audio_stop((DVR_PlaybackHandle_t)player);
2130 dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams);
2131 dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002132 break;
2133 case DVR_PLAYBACK_CMD_FF:
2134 case DVR_PLAYBACK_CMD_FB:
hualing chen2aba4022020-03-02 13:49:55 +08002135 _dvr_playback_fffb((DVR_PlaybackHandle_t)player);
hualing chen5cbe1a62020-02-10 16:36:36 +08002136 break;
2137 default:
2138 break;
2139 }
2140 return DVR_SUCCESS;
2141}
2142
2143/**\brief Resume play
hualing chenb31a6c62020-01-13 17:27:00 +08002144 * \param[in] handle playback handle
hualing chenb31a6c62020-01-13 17:27:00 +08002145 * \retval DVR_SUCCESS On success
2146 * \return Error code
2147 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002148int dvr_playback_resume(DVR_PlaybackHandle_t handle) {
2149 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2150
hualing chena540a7e2020-03-27 16:44:05 +08002151 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002152 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002153 return DVR_FAILURE;
2154 }
2155
hualing chen5cbe1a62020-02-10 16:36:36 +08002156 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002157 DVR_PB_DG(1, "lock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002158 pthread_mutex_lock(&player->lock);
hualing chen266b9502020-04-04 17:39:39 +08002159 if (player->has_video) {
2160 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2161 AmTsPlayer_resumeVideoDecoding(player->handle);
2162 }
2163 if (player->has_audio) {
2164 AmTsPlayer_resumeAudioDecoding(player->handle);
2165 }
2166 //check is has audio param,if has audio .we need start audio,
2167 //we will stop audio when ff fb, if reach end, we will pause.so we need
2168 //start audio when resume play
2169
2170 am_tsplayer_video_params vparams;
2171 am_tsplayer_audio_params aparams;
2172 uint64_t segmentid = player->cur_segment_id;
2173 _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams);
2174 //valid audio pid, start audio
hualing chenc70a8df2020-05-12 19:23:11 +08002175 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 +08002176 player->has_audio = DVR_TRUE;
2177 AmTsPlayer_setAudioParams(player->handle, &aparams);
2178 AmTsPlayer_startAudioDecoding(player->handle);
2179 } else {
hualing chenc70a8df2020-05-12 19:23:11 +08002180 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 +08002181 }
hualing chen87072a82020-03-12 16:20:12 +08002182 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2183 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2184 player->cmd.state = DVR_PLAYBACK_STATE_START;
2185 player->state = DVR_PLAYBACK_STATE_START;
2186 } else {
2187 player->cmd.last_cmd = player->cmd.cur_cmd;
2188 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME;
2189 player->cmd.state = DVR_PLAYBACK_STATE_START;
2190 player->state = DVR_PLAYBACK_STATE_START;
2191 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002192 DVR_PB_DG(1, "unlock");
hualing chen5cbe1a62020-02-10 16:36:36 +08002193 pthread_mutex_unlock(&player->lock);
hualing chen041c4092020-04-05 15:11:50 +08002194 } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){
2195 if (player->has_video)
2196 AmTsPlayer_resumeVideoDecoding(player->handle);
2197 if (player->has_audio)
2198 AmTsPlayer_resumeAudioDecoding(player->handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002199 DVR_PB_DG(1, "set start state cur cmd[%d]", player->cmd.cur_cmd);
hualing chen2aba4022020-03-02 13:49:55 +08002200 player->cmd.state = DVR_PLAYBACK_STATE_START;
2201 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002202 _dvr_cmd(handle, player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002203 } else {
2204 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
2205 {
2206 pthread_mutex_lock(&player->lock);
2207 //clear flag
hualing chen4b7c15d2020-04-07 16:13:48 +08002208 DVR_PB_DG(1, "clear pause live flag cur cmd[%d]", player->cmd.cur_cmd);
hualing chen041c4092020-04-05 15:11:50 +08002209 player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE);
2210 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
2211 pthread_mutex_unlock(&player->lock);
2212 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002213 }
2214 return DVR_SUCCESS;
2215}
2216
hualing chena540a7e2020-03-27 16:44:05 +08002217static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){
2218
2219 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2220 DVR_PlaybackSegmentInfo_t *segment = NULL;
2221 DVR_PlaybackSegmentInfo_t *cur_segment = NULL;
2222 DVR_PlaybackSegmentInfo_t *set_segment = NULL;
2223
2224 list_for_each_entry(segment, &player->segment_list, head)
2225 {
2226 if (segment->segment_id == segment_id) {
2227 cur_segment = segment;
2228 }
2229 if (segment->segment_id == set_seg_id) {
2230 set_segment = segment;
2231 }
2232 if (cur_segment != NULL && set_segment != NULL) {
2233 break;
2234 }
2235 }
2236 if (cur_segment == NULL || set_segment == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002237 DVR_PB_DG(1, "set segmen or cur segment is null");
hualing chena540a7e2020-03-27 16:44:05 +08002238 return DVR_TRUE;
2239 }
2240 if (cur_segment->pids.video.format != set_segment->pids.video.format ||
2241 cur_segment->pids.video.pid != set_segment->pids.video.pid ||
2242 cur_segment->pids.audio.format != set_segment->pids.audio.format ||
2243 cur_segment->pids.audio.pid != set_segment->pids.audio.pid) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002244 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 +08002245 return DVR_TRUE;
2246 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002247 DVR_PB_DG(1, "play info not change");
hualing chena540a7e2020-03-27 16:44:05 +08002248 return DVR_FALSE;
2249}
2250
hualing chen5cbe1a62020-02-10 16:36:36 +08002251/**\brief seek
2252 * \param[in] handle playback handle
2253 * \param[in] time_offset time offset base cur segment
2254 * \retval DVR_SUCCESS On success
2255 * \return Error code
2256 */
hualing chencc91e1c2020-02-28 13:26:17 +08002257int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) {
hualing chen040df222020-01-17 13:35:02 +08002258 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002259
2260 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002261 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002262 return DVR_FAILURE;
2263 }
2264
hualing chen4b7c15d2020-04-07 16:13:48 +08002265 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 +08002266 pthread_mutex_lock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002267
hualing chen86e7d482020-01-16 15:13:33 +08002268 int offset = -1;
hualing chena540a7e2020-03-27 16:44:05 +08002269 DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id);
hualing chen4b7c15d2020-04-07 16:13:48 +08002270 DVR_PB_DG(1, "player->state[%d]-replay[%d]--get lock-", player->state, replay);
hualing chena540a7e2020-03-27 16:44:05 +08002271
hualing chen5cbe1a62020-02-10 16:36:36 +08002272 //open segment if id is not current segment
hualing chen87072a82020-03-12 16:20:12 +08002273 int ret = _dvr_open_segment(handle, segment_id);
2274 if (ret ==DVR_FAILURE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002275 DVR_PB_DG(1, "seek error at open segment");
hualing chen87072a82020-03-12 16:20:12 +08002276 pthread_mutex_unlock(&player->lock);
2277 return DVR_FAILURE;
2278 }
2279 if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) {
2280 if (segment_ongoing(player->r_handle) == DVR_SUCCESS) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002281 DVR_PB_DG(1, "is ongoing segment when seek end, need return success");
hualing chen87072a82020-03-12 16:20:12 +08002282 //pthread_mutex_unlock(&player->lock);
2283 //return DVR_SUCCESS;
2284 time_offset = _dvr_get_end_time(handle);
2285 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002286 DVR_PB_DG(1, "is not ongoing segment when seek end, return failure");
hualing chen87072a82020-03-12 16:20:12 +08002287 pthread_mutex_unlock(&player->lock);
2288 return DVR_FAILURE;
2289 }
2290 }
2291
hualing chen4b7c15d2020-04-07 16:13:48 +08002292 DVR_PB_DG(1, "seek open id[%lld]flag[0x%x] time_offset %u", player->cur_segment.segment_id, player->cur_segment.flags, time_offset);
hualing chen86e7d482020-01-16 15:13:33 +08002293 //get file offset by time
hualing chen2aba4022020-03-02 13:49:55 +08002294 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2295 //forward playback.not seek end of file
2296 if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) {
2297 //default -2000ms
2298 time_offset = time_offset -FB_DEFAULT_LEFT_TIME;
2299 }
hualing chen86e7d482020-01-16 15:13:33 +08002300 }
hualing chen2aba4022020-03-02 13:49:55 +08002301 pthread_mutex_lock(&player->segment_lock);
hualing chen266b9502020-04-04 17:39:39 +08002302 player->drop_ts = DVR_TRUE;
2303 offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size);
hualing chen4b7c15d2020-04-07 16:13:48 +08002304 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 +08002305 pthread_mutex_unlock(&player->segment_lock);
hualing chen86e7d482020-01-16 15:13:33 +08002306 player->offset = offset;
hualing chen87072a82020-03-12 16:20:12 +08002307
hualing chen2aba4022020-03-02 13:49:55 +08002308 _dvr_get_end_time(handle);
Zhiqiang Han8e4e6db2020-05-15 10:52:20 +08002309
2310 player->last_send_time_id = UINT64_MAX;
2311
hualing chen2aba4022020-03-02 13:49:55 +08002312 //init fffb time
hualing chen87072a82020-03-12 16:20:12 +08002313 player->fffb_current = _dvr_time_getClock();
2314 player->fffb_start = player->fffb_current;
2315 player->fffb_start_pcr = _dvr_get_cur_time(handle);
2316 player->next_fffb_time = player->fffb_current;
hualing chena540a7e2020-03-27 16:44:05 +08002317 //pause state if need to replayer false
hualing chen39628212020-05-14 10:35:13 +08002318 if (player->state == DVR_PLAYBACK_STATE_STOP) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002319 //only seek file,not start
hualing chen4b7c15d2020-04-07 16:13:48 +08002320 DVR_PB_DG(1, "unlock");
hualing chencc91e1c2020-02-28 13:26:17 +08002321 pthread_mutex_unlock(&player->lock);
hualing chen87072a82020-03-12 16:20:12 +08002322 return DVR_SUCCESS;
hualing chen5cbe1a62020-02-10 16:36:36 +08002323 }
hualing chen86e7d482020-01-16 15:13:33 +08002324 //stop play
hualing chen4b7c15d2020-04-07 16:13:48 +08002325 DVR_PB_DG(0, "seek stop play, not inject data has video[%d]audio[%d]", player->has_video, player->has_audio);
hualing chen266b9502020-04-04 17:39:39 +08002326 if (player->has_video) {
2327 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002328 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002329 }
2330
2331 if (player->has_audio) {
2332 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002333 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002334 }
hualing chen86e7d482020-01-16 15:13:33 +08002335 //start play
hualing chen2aba4022020-03-02 13:49:55 +08002336 am_tsplayer_video_params vparams;
2337 am_tsplayer_audio_params aparams;
hualing chenb31a6c62020-01-13 17:27:00 +08002338
hualing chen040df222020-01-17 13:35:02 +08002339 player->cur_segment_id = segment_id;
2340
2341 int sync = DVR_PLAYBACK_SYNC;
hualing chen5cbe1a62020-02-10 16:36:36 +08002342 //get segment info and audio video pid fmt ;
2343 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams);
hualing chen86e7d482020-01-16 15:13:33 +08002344 //start audio and video
2345 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2346 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002347 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 +08002348 pthread_mutex_unlock(&player->lock);
2349 return -1;
2350 }
2351 //add
hualing chen040df222020-01-17 13:35:02 +08002352 if (sync == DVR_PLAYBACK_SYNC) {
hualing chen86e7d482020-01-16 15:13:33 +08002353 if (VALID_PID(vparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002354 //player->has_video;
hualing chen2aba4022020-03-02 13:49:55 +08002355 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE ||
hualing chen31140872020-03-25 12:29:26 +08002356 player->speed > 1.0f||
2357 player->speed <= -1.0f) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002358 //if is pause state. we need set trick mode.
hualing chen4b7c15d2020-04-07 16:13:48 +08002359 DVR_PB_DG(1, "seek set trick mode player->speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002360 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen5cbe1a62020-02-10 16:36:36 +08002361 }
hualing chen2aba4022020-03-02 13:49:55 +08002362 AmTsPlayer_setVideoParams(player->handle, &vparams);
2363 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002364 player->has_video = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002365 }
hualing chen86e7d482020-01-16 15:13:33 +08002366 if (VALID_PID(aparams.pid)) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002367 DVR_PB_DG(1, "start audio seek");
hualing chen2aba4022020-03-02 13:49:55 +08002368 AmTsPlayer_setAudioParams(player->handle, &aparams);
2369 AmTsPlayer_startAudioDecoding(player->handle);
hualing chen266b9502020-04-04 17:39:39 +08002370 player->has_audio = DVR_TRUE;
hualing chenb31a6c62020-01-13 17:27:00 +08002371 }
hualing chen86e7d482020-01-16 15:13:33 +08002372 }
hualing chen2aba4022020-03-02 13:49:55 +08002373 if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
2374 ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2375 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) ||
2376 (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF ||
2377 player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))) {
2378 player->cmd.state = DVR_PLAYBACK_STATE_PAUSE;
2379 player->state = DVR_PLAYBACK_STATE_PAUSE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002380 DVR_PB_DG(1, "set state pause in seek");
hualing chen87072a82020-03-12 16:20:12 +08002381 } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2382 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
hualing chen31140872020-03-25 12:29:26 +08002383 player->speed > 1.0f||
2384 player->speed <= -1.0f) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002385 DVR_PB_DG(1, "not set cmd to seek");
hualing chen87072a82020-03-12 16:20:12 +08002386 //not pause state, we need not set cur cmd
hualing chen2aba4022020-03-02 13:49:55 +08002387 } else {
hualing chen4b7c15d2020-04-07 16:13:48 +08002388 DVR_PB_DG(1, "set cmd to seek");
hualing chen2aba4022020-03-02 13:49:55 +08002389 player->cmd.last_cmd = player->cmd.cur_cmd;
2390 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK;
2391 player->cmd.state = DVR_PLAYBACK_STATE_START;
2392 player->state = DVR_PLAYBACK_STATE_START;
2393 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002394 player->last_send_time_id = UINT64_MAX;
2395 DVR_PB_DG(1, "unlock");
hualing chen86e7d482020-01-16 15:13:33 +08002396 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002397
2398 return DVR_SUCCESS;
2399}
hualing chen5cbe1a62020-02-10 16:36:36 +08002400
hualing chen5cbe1a62020-02-10 16:36:36 +08002401static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) {
2402 //get cur time of segment
2403 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002404
2405 if (player == NULL || player->handle == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002406 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002407 return DVR_FAILURE;
2408 }
2409
hualing chen31140872020-03-25 12:29:26 +08002410 int64_t cache = 0;//defalut es buf cache 500ms
2411 AmTsPlayer_getDelayTime(player->handle, &cache);
hualing chen2aba4022020-03-02 13:49:55 +08002412 pthread_mutex_lock(&player->segment_lock);
2413 uint64_t cur = segment_tell_current_time(player->r_handle);
2414 pthread_mutex_unlock(&player->segment_lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002415 DVR_PB_DG(1, "get cur time [%lld] cache:%lld cur id [%lld]", cur, cache, player->cur_segment_id);
hualing chen87072a82020-03-12 16:20:12 +08002416 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2417 cache = 0;
2418 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002419 int cur_time = (int)(cur > cache ? cur - cache : 0);
2420 return cur_time;
hualing chencc91e1c2020-02-28 13:26:17 +08002421}
2422
2423//get current segment current pcr time of read pos
2424static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) {
2425 //get cur time of segment
2426 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002427
2428 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002429 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002430 return DVR_FAILURE;
2431 }
2432
hualing chen2aba4022020-03-02 13:49:55 +08002433 pthread_mutex_lock(&player->segment_lock);
2434 uint64_t end = segment_tell_total_time(player->r_handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002435 DVR_PB_DG(1, "get tatal time [%lld]", end);
hualing chen2aba4022020-03-02 13:49:55 +08002436 pthread_mutex_unlock(&player->segment_lock);
2437 return (int)end;
hualing chen5cbe1a62020-02-10 16:36:36 +08002438}
2439
hualing chen4b7c15d2020-04-07 16:13:48 +08002440#define FB_MIX_SEEK_TIME 2000
hualing chen5cbe1a62020-02-10 16:36:36 +08002441//start replay
2442static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) {
2443
2444 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2445 //calculate pcr seek time
2446 int t_diff = 0;
2447 int seek_time = 0;
hualing chena540a7e2020-03-27 16:44:05 +08002448
2449 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002450 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002451 return DVR_FAILURE;
2452 }
2453
hualing chen5cbe1a62020-02-10 16:36:36 +08002454 if (player->fffb_start == -1) {
2455 //set fffb start time ms
2456 player->fffb_start = _dvr_time_getClock();
2457 player->fffb_current = player->fffb_start;
2458 //get segment current time pos
2459 player->fffb_start_pcr = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002460 DVR_PB_DG(1, "calculate seek pos player->fffb_start_pcr[%d]ms, speed[%f]", player->fffb_start_pcr, player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002461 t_diff = 0;
hualing chen2aba4022020-03-02 13:49:55 +08002462 //default first time 1ms seek
hualing chen87072a82020-03-12 16:20:12 +08002463 seek_time = FB_MIX_SEEK_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002464 } else {
2465 player->fffb_current = _dvr_time_getClock();
2466 t_diff = player->fffb_current - player->fffb_start;
hualing chen2aba4022020-03-02 13:49:55 +08002467 //if speed is < 0, cmd is fb.
hualing chen5cbe1a62020-02-10 16:36:36 +08002468 seek_time = player->fffb_start_pcr + t_diff *player->speed;
hualing chen2aba4022020-03-02 13:49:55 +08002469 if (seek_time <= 0) {
2470 //need seek to pre one segment
2471 seek_time = 0;
2472 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002473 //seek segment pos
2474 if (player->r_handle) {
hualing chen2aba4022020-03-02 13:49:55 +08002475 pthread_mutex_lock(&player->segment_lock);
hualing chen041c4092020-04-05 15:11:50 +08002476 if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) {
2477 seek_time = 0;
2478 }
hualing chen2aba4022020-03-02 13:49:55 +08002479 pthread_mutex_unlock(&player->segment_lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002480 } else {
2481 //
hualing chen4b7c15d2020-04-07 16:13:48 +08002482 DVR_PB_DG(1, "segment not open,can not seek");
hualing chen5cbe1a62020-02-10 16:36:36 +08002483 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002484 DVR_PB_DG(1, "calculate seek pos seek_time[%d]ms, speed[%f]id[%lld]cur [%d]", seek_time, player->speed,player->cur_segment_id, _dvr_get_cur_time(handle));
hualing chen5cbe1a62020-02-10 16:36:36 +08002485 }
hualing chen2aba4022020-03-02 13:49:55 +08002486 return seek_time;
hualing chen5cbe1a62020-02-10 16:36:36 +08002487}
2488
2489
2490//start replay
2491static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) {
2492 //
2493 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002494
2495 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002496 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002497 return DVR_FAILURE;
2498 }
2499
hualing chen5cbe1a62020-02-10 16:36:36 +08002500 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002501 if (player->has_video) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002502 DVR_PB_DG(1, "fffb stop video");
hualing chen2aba4022020-03-02 13:49:55 +08002503 AmTsPlayer_stopVideoDecoding(player->handle);
2504 }
2505 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002506 DVR_PB_DG(1, "fffb stop audio");
hualing chen266b9502020-04-04 17:39:39 +08002507 player->has_audio =DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002508 AmTsPlayer_stopAudioDecoding(player->handle);
2509 }
2510
hualing chen5cbe1a62020-02-10 16:36:36 +08002511 //start video and audio
2512
hualing chen2aba4022020-03-02 13:49:55 +08002513 am_tsplayer_video_params vparams;
2514 am_tsplayer_audio_params aparams;
hualing chen87072a82020-03-12 16:20:12 +08002515 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002516
2517 //get segment info and audio video pid fmt ;
hualing chencc91e1c2020-02-28 13:26:17 +08002518 //pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002519 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams);
2520 //start audio and video
2521 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
2522 //audio abnd video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002523 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chencc91e1c2020-02-28 13:26:17 +08002524 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002525 return -1;
2526 }
2527
2528 if (VALID_PID(vparams.pid)) {
2529 player->has_video = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002530 DVR_PB_DG(1, "fffb start video");
hualing chen31140872020-03-25 12:29:26 +08002531 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen2aba4022020-03-02 13:49:55 +08002532 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
2533 AmTsPlayer_setVideoParams(player->handle, &vparams);
2534 AmTsPlayer_startVideoDecoding(player->handle);
2535 //playback_device_video_start(player->handle , &vparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002536 //if set flag is pause live, we need set trick mode
hualing chen2aba4022020-03-02 13:49:55 +08002537 //playback_device_trick_mode(player->handle, 1);
hualing chen5cbe1a62020-02-10 16:36:36 +08002538 }
hualing chena540a7e2020-03-27 16:44:05 +08002539 if (0 && VALID_PID(aparams.pid)) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002540 player->has_audio = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002541 DVR_PB_DG(1, "fffb start audio");
hualing chen2aba4022020-03-02 13:49:55 +08002542 AmTsPlayer_setAudioParams(player->handle, &aparams);
2543 AmTsPlayer_startAudioDecoding(player->handle);
2544 //playback_device_audio_start(player->handle , &aparams);
hualing chen5cbe1a62020-02-10 16:36:36 +08002545 }
hualing chen31140872020-03-25 12:29:26 +08002546 //fffb mode need stop fast;
hualing chen7a56cba2020-04-14 14:09:27 +08002547 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002548 AmTsPlayer_stopFast(player->handle);
hualing chencc91e1c2020-02-28 13:26:17 +08002549 //pthread_mutex_unlock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002550 return 0;
2551}
2552
2553static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) {
2554 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002555 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002556 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002557 return DVR_FAILURE;
2558 }
2559
2560 player->first_frame = 0;
hualing chen4b7c15d2020-04-07 16:13:48 +08002561 DVR_PB_DG(1, "lock speed [%f]", player->speed);
hualing chen5cbe1a62020-02-10 16:36:36 +08002562 pthread_mutex_lock(&player->lock);
2563
hualing chen2aba4022020-03-02 13:49:55 +08002564 int seek_time = _dvr_playback_calculate_seekpos(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002565 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 +08002566
hualing chen87072a82020-03-12 16:20:12 +08002567 if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) {
2568 //seek time set 0
2569 seek_time = 0;
2570 }
hualing chen041c4092020-04-05 15:11:50 +08002571 if (seek_time == 0) {
hualing chen2aba4022020-03-02 13:49:55 +08002572 //for fb cmd, we need open pre segment.if reach first one segment, send begin event
2573 int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player);
hualing chen041c4092020-04-05 15:11:50 +08002574 if (ret != DVR_SUCCESS && IS_FB(player->speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002575 pthread_mutex_unlock(&player->lock);
2576 dvr_playback_pause(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002577 //send event here and pause
2578 DVR_Play_Notify_t notify;
2579 memset(&notify, 0 , sizeof(DVR_Play_Notify_t));
hualing chen87072a82020-03-12 16:20:12 +08002580 notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN;
hualing chen2aba4022020-03-02 13:49:55 +08002581 //get play statue not here
hualing chen2932d372020-04-29 13:44:00 +08002582 _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, &notify, DVR_TRUE);
hualing chen4b7c15d2020-04-07 16:13:48 +08002583 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 +08002584 //change to pause
hualing chen2aba4022020-03-02 13:49:55 +08002585 return DVR_SUCCESS;
2586 }
hualing chen2932d372020-04-29 13:44:00 +08002587 _dvr_playback_sent_transition_ok(handle, DVR_FALSE);
hualing chen2aba4022020-03-02 13:49:55 +08002588 _dvr_init_fffb_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002589 DVR_PB_DG(1, "*******************send trans ok event speed [%f]", player->speed);
hualing chen2aba4022020-03-02 13:49:55 +08002590 }
2591 player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME;
hualing chen5cbe1a62020-02-10 16:36:36 +08002592 _dvr_playback_fffb_replay(handle);
2593
2594 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002595 DVR_PB_DG(1, "unlock");
hualing chen2aba4022020-03-02 13:49:55 +08002596
hualing chen5cbe1a62020-02-10 16:36:36 +08002597 return DVR_SUCCESS;
2598}
2599
hualing chen87072a82020-03-12 16:20:12 +08002600//start replay, need get lock at extern
hualing chen2aba4022020-03-02 13:49:55 +08002601static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002602 //
2603 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002604
2605 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002606 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002607 return DVR_FAILURE;
2608 }
2609
hualing chen5cbe1a62020-02-10 16:36:36 +08002610 //stop
hualing chen2aba4022020-03-02 13:49:55 +08002611 if (player->has_video) {
hualing chen266b9502020-04-04 17:39:39 +08002612 player->has_video = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002613 AmTsPlayer_stopVideoDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002614 }
2615
2616 if (player->has_audio) {
hualing chen266b9502020-04-04 17:39:39 +08002617 player->has_audio = DVR_FALSE;
hualing chen2aba4022020-03-02 13:49:55 +08002618 AmTsPlayer_stopAudioDecoding(player->handle);
hualing chen2aba4022020-03-02 13:49:55 +08002619 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002620 //start video and audio
2621
hualing chen2aba4022020-03-02 13:49:55 +08002622 am_tsplayer_video_params vparams;
2623 am_tsplayer_audio_params aparams;
hualing chen87072a82020-03-12 16:20:12 +08002624 uint64_t segment_id = player->cur_segment_id;
hualing chen5cbe1a62020-02-10 16:36:36 +08002625
2626 //get segment info and audio video pid fmt ;
hualing chen4b7c15d2020-04-07 16:13:48 +08002627 DVR_PB_DG(1, "into");
hualing chen5cbe1a62020-02-10 16:36:36 +08002628 _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams);
2629 //start audio and video
2630 if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) {
hualing chen2aba4022020-03-02 13:49:55 +08002631 //audio and video pis is all invalid, return error.
hualing chen4b7c15d2020-04-07 16:13:48 +08002632 DVR_PB_DG(0, "dvr play back restart error, not found audio and video info");
hualing chen5cbe1a62020-02-10 16:36:36 +08002633 return -1;
2634 }
2635
2636 if (VALID_PID(vparams.pid)) {
2637 player->has_video = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002638 if (trick == DVR_TRUE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002639 DVR_PB_DG(1, "settrick mode at replay");
hualing chen2aba4022020-03-02 13:49:55 +08002640 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT);
hualing chen87072a82020-03-12 16:20:12 +08002641 }
hualing chen266b9502020-04-04 17:39:39 +08002642 else {
hualing chen2aba4022020-03-02 13:49:55 +08002643 AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE);
hualing chen266b9502020-04-04 17:39:39 +08002644 }
hualing chen2aba4022020-03-02 13:49:55 +08002645 AmTsPlayer_setVideoParams(player->handle, &vparams);
2646 AmTsPlayer_startVideoDecoding(player->handle);
hualing chen5cbe1a62020-02-10 16:36:36 +08002647 }
hualing chena540a7e2020-03-27 16:44:05 +08002648
2649 if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) {
hualing chen7a56cba2020-04-14 14:09:27 +08002650 DVR_PB_DG(1, "start fast");
hualing chen31140872020-03-25 12:29:26 +08002651 AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002652 player->speed = (float)player->cmd.speed.speed.speed/100.0f;
hualing chen31140872020-03-25 12:29:26 +08002653 } else {
hualing chena540a7e2020-03-27 16:44:05 +08002654 if (VALID_PID(aparams.pid)) {
2655 player->has_audio = DVR_TRUE;
hualing chen4b7c15d2020-04-07 16:13:48 +08002656 DVR_PB_DG(1, "start audio");
hualing chena540a7e2020-03-27 16:44:05 +08002657 AmTsPlayer_startAudioDecoding(player->handle);
2658 AmTsPlayer_setAudioParams(player->handle, &aparams);
hualing chena540a7e2020-03-27 16:44:05 +08002659 }
hualing chen7a56cba2020-04-14 14:09:27 +08002660 DVR_PB_DG(1, "stop fast");
hualing chen31140872020-03-25 12:29:26 +08002661 AmTsPlayer_stopFast(player->handle);
2662 player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1;
2663 player->speed = (float)PLAYBACK_SPEED_X1/100.0f;
2664 }
hualing chen2aba4022020-03-02 13:49:55 +08002665 player->cmd.last_cmd = player->cmd.cur_cmd;
2666 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START;
hualing chen2aba4022020-03-02 13:49:55 +08002667 player->cmd.state = DVR_PLAYBACK_STATE_START;
2668 player->state = DVR_PLAYBACK_STATE_START;
hualing chen5cbe1a62020-02-10 16:36:36 +08002669 return 0;
2670}
2671
2672
hualing chenb31a6c62020-01-13 17:27:00 +08002673/**\brief Set play speed
2674 * \param[in] handle playback handle
2675 * \param[in] speed playback speed
2676 * \retval DVR_SUCCESS On success
2677 * \return Error code
2678 */
hualing chen5cbe1a62020-02-10 16:36:36 +08002679int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) {
2680
2681 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002682
2683 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002684 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002685 return DVR_FAILURE;
2686 }
2687
hualing chen4b7c15d2020-04-07 16:13:48 +08002688 DVR_PB_DG(1, "lock func: speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002689 if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002690 DVR_PB_DG(1, " func: not support speed [%d]", speed.speed.speed);
hualing chena540a7e2020-03-27 16:44:05 +08002691 return DVR_FAILURE;
2692 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002693 pthread_mutex_lock(&player->lock);
2694 if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF
2695 && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) {
2696 player->cmd.last_cmd = player->cmd.cur_cmd;
2697 }
hualing chen31140872020-03-25 12:29:26 +08002698 if (player->state != DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08002699 IS_KERNEL_SPEED(speed.speed.speed)) {
2700 //case 1. cur speed is 100,set 200 50 25 12 .
2701 //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb.
2702 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen87072a82020-03-12 16:20:12 +08002703 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002704 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2705 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002706 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002707 AmTsPlayer_stopFast(player->handle);
2708 pthread_mutex_unlock(&player->lock);
2709 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
2710 pthread_mutex_lock(&player->lock);
2711 } else {
2712 //set play speed and if audio is start, stop audio.
2713 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002714 DVR_PB_DG(1, "fast play stop audio");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002715 AmTsPlayer_stopAudioDecoding(player->handle);
2716 player->has_audio = DVR_FALSE;
2717 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002718 DVR_PB_DG(1, "start fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002719 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chena540a7e2020-03-27 16:44:05 +08002720 }
hualing chenbcada022020-04-22 14:27:01 +08002721 player->fffb_play = DVR_FALSE;
hualing chena540a7e2020-03-27 16:44:05 +08002722 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002723 player->cmd.speed.speed = speed.speed;
2724 player->speed = (float)speed.speed.speed/(float)100;
2725 pthread_mutex_unlock(&player->lock);
2726 return DVR_SUCCESS;
2727 }
2728 //case 2. not start play
2729 if (player->state == DVR_PLAYBACK_STATE_STOP) {
2730 //only set speed.and return;
hualing chena540a7e2020-03-27 16:44:05 +08002731 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002732 player->cmd.speed.speed = speed.speed;
2733 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08002734 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002735 pthread_mutex_unlock(&player->lock);
2736 return DVR_SUCCESS;
hualing chen87072a82020-03-12 16:20:12 +08002737 }
hualing chen31140872020-03-25 12:29:26 +08002738 //case 3 fffb mode
2739 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2740 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2741 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002742 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002743 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002744 player->cmd.speed.speed = speed.speed;
2745 player->speed = (float)speed.speed.speed/(float)100;
2746 _dvr_playback_replay(handle, DVR_FALSE);
hualing chenbcada022020-04-22 14:27:01 +08002747 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002748 pthread_mutex_unlock(&player->lock);
2749 return DVR_SUCCESS;
2750 }
2751 }
2752 else if (player->state == DVR_PLAYBACK_STATE_PAUSE &&
hualing chena540a7e2020-03-27 16:44:05 +08002753 IS_KERNEL_SPEED(speed.speed.speed)) {
2754 //case 1. cur speed is kernel support speed,set kernel speed.
2755 if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) {
hualing chen31140872020-03-25 12:29:26 +08002756 //if last speed is x2 or s2, we need stop fast
hualing chen2bd8a7a2020-04-02 11:31:03 +08002757 if (speed.speed.speed == PLAYBACK_SPEED_X1) {
2758 // resume audio and stop fast play
hualing chen7a56cba2020-04-14 14:09:27 +08002759 DVR_PB_DG(1, "stop fast");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002760 AmTsPlayer_stopFast(player->handle);
2761 pthread_mutex_unlock(&player->lock);
2762 _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART);
2763 pthread_mutex_lock(&player->lock);
2764 } else {
2765 //set play speed and if audio is start, stop audio.
2766 if (player->has_audio) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002767 DVR_PB_DG(1, "fast play stop audio at pause");
hualing chen2bd8a7a2020-04-02 11:31:03 +08002768 AmTsPlayer_stopAudioDecoding(player->handle);
2769 player->has_audio = DVR_FALSE;
2770 }
hualing chenb96aa2c2020-04-15 14:13:53 +08002771 DVR_PB_DG(1, "start fast");
2772 AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100);
hualing chen2bd8a7a2020-04-02 11:31:03 +08002773 }
hualing chena540a7e2020-03-27 16:44:05 +08002774 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002775 player->cmd.speed.speed = speed.speed;
2776 player->speed = (float)speed.speed.speed/(float)100;
hualing chenbcada022020-04-22 14:27:01 +08002777 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002778 pthread_mutex_unlock(&player->lock);
2779 return DVR_SUCCESS;
2780 }
2781 //case 2 fffb mode
2782 if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2783 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) {
2784 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002785 DVR_PB_DG(1, "set speed x1 s2 and replay playback");
hualing chena540a7e2020-03-27 16:44:05 +08002786 player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT;
hualing chen31140872020-03-25 12:29:26 +08002787 player->cmd.speed.speed = speed.speed;
2788 player->speed = (float)speed.speed.speed/(float)100;
2789 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chenbcada022020-04-22 14:27:01 +08002790 player->fffb_play = DVR_FALSE;
hualing chen31140872020-03-25 12:29:26 +08002791 pthread_mutex_unlock(&player->lock);
2792 return DVR_SUCCESS;
2793 }
hualing chen31140872020-03-25 12:29:26 +08002794 }
hualing chena540a7e2020-03-27 16:44:05 +08002795 if (IS_KERNEL_SPEED(speed.speed.speed)) {
2796 //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb.
hualing chenbcada022020-04-22 14:27:01 +08002797 player->fffb_play = DVR_FALSE;
hualing chen87072a82020-03-12 16:20:12 +08002798 } else {
hualing chen31140872020-03-25 12:29:26 +08002799 if ((float)speed.speed.speed > 1.0f)
hualing chen87072a82020-03-12 16:20:12 +08002800 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF;
2801 else
2802 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB;
hualing chen4b7c15d2020-04-07 16:13:48 +08002803 player->fffb_play = DVR_TRUE;
2804 }
2805 DVR_Bool_t init_last_time = DVR_FALSE;
2806 if (player->speed > 0.0f && speed.speed.speed < 0) {
2807 init_last_time = DVR_TRUE;
2808 } else if (player->speed < 0.0f && speed.speed.speed > 0) {
2809 init_last_time = DVR_TRUE;
hualing chen87072a82020-03-12 16:20:12 +08002810 }
hualing chen5cbe1a62020-02-10 16:36:36 +08002811 player->cmd.speed.mode = speed.mode;
2812 player->cmd.speed.speed = speed.speed;
hualing chen31140872020-03-25 12:29:26 +08002813 player->speed = (float)speed.speed.speed/(float)100;
hualing chen4b7c15d2020-04-07 16:13:48 +08002814#if 0
2815 if (abs((int)(player->speed)) > 1) {
2816 //seek speed*1000 ms at fb mode
2817 pthread_mutex_lock(&player->segment_lock);
2818 uint64_t cur = segment_tell_current_time(player->r_handle);
2819 int diff = abs((int)(player->speed * 1000));
2820 DVR_PB_DG(1, " cur:%ull diff:%d", cur, diff);
2821 if (cur > diff)
2822 cur = cur - diff;
2823 else
2824 cur = 0;
2825 segment_seek(player->r_handle, cur, player->openParams.block_size);
2826 pthread_mutex_unlock(&player->segment_lock);
2827 }
2828#endif
hualing chen31140872020-03-25 12:29:26 +08002829 //reset fffb time, if change speed value
hualing chen4b7c15d2020-04-07 16:13:48 +08002830 _dvr_init_fffb_t(handle);
2831 if (init_last_time == DVR_TRUE)
2832 player->last_send_time_id = UINT64_MAX;
2833
hualing chen87072a82020-03-12 16:20:12 +08002834 if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
hualing chen6d24aa92020-03-23 18:43:47 +08002835 (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF ||
2836 player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) {
hualing chen87072a82020-03-12 16:20:12 +08002837 //restart play at normal speed exit ff fb
hualing chen4b7c15d2020-04-07 16:13:48 +08002838 DVR_PB_DG(1, "set speed normal and replay playback");
hualing chen87072a82020-03-12 16:20:12 +08002839 _dvr_playback_replay(handle, DVR_FALSE);
2840 } else if (speed.speed.speed == PLAYBACK_SPEED_X1 &&
2841 (player->state == DVR_PLAYBACK_STATE_PAUSE)) {
2842 player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART;
hualing chen4b7c15d2020-04-07 16:13:48 +08002843 DVR_PB_DG(1, "set speed normal at pause state ,set cur cmd");
hualing chen87072a82020-03-12 16:20:12 +08002844 }
hualing chen4b7c15d2020-04-07 16:13:48 +08002845 DVR_PB_DG(1, "unlock speed[%f]cmd[%d]", player->speed, player->cmd.cur_cmd);
hualing chen5cbe1a62020-02-10 16:36:36 +08002846 pthread_mutex_unlock(&player->lock);
hualing chenb31a6c62020-01-13 17:27:00 +08002847 return DVR_SUCCESS;
2848}
hualing chen2932d372020-04-29 13:44:00 +08002849
hualing chenb31a6c62020-01-13 17:27:00 +08002850/**\brief Get playback status
2851 * \param[in] handle playback handle
2852 * \param[out] p_status playback status
2853 * \retval DVR_SUCCESS On success
2854 * \return Error code
2855 */
hualing chen2932d372020-04-29 13:44:00 +08002856static int _dvr_playback_get_status(DVR_PlaybackHandle_t handle,
2857 DVR_PlaybackStatus_t *p_status, DVR_Bool_t is_lock) {
hualing chen5cbe1a62020-02-10 16:36:36 +08002858//
2859 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chena540a7e2020-03-27 16:44:05 +08002860
2861 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002862 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002863 return DVR_FAILURE;
2864 }
hualing chen2932d372020-04-29 13:44:00 +08002865 if (is_lock ==DVR_TRUE)
2866 pthread_mutex_lock(&player->lock);
hualing chen5cbe1a62020-02-10 16:36:36 +08002867 p_status->state = player->state;
hualing chen31140872020-03-25 12:29:26 +08002868 //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 +08002869 if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE &&
2870 player->state == DVR_PLAYBACK_STATE_START) {
2871 p_status->state = DVR_PLAYBACK_STATE_PAUSE;
2872 }
hualing chen041c4092020-04-05 15:11:50 +08002873
hualing chencc91e1c2020-02-28 13:26:17 +08002874 p_status->time_end = _dvr_get_end_time(handle);
hualing chen2aba4022020-03-02 13:49:55 +08002875 p_status->time_cur = _dvr_get_cur_time(handle);
hualing chen4b7c15d2020-04-07 16:13:48 +08002876 if (player->last_send_time_id == UINT64_MAX) {
2877 player->last_send_time_id = player->cur_segment_id;
2878 player->last_cur_time = p_status->time_cur;
2879 }
2880 if (player->last_send_time_id == player->cur_segment_id) {
2881 if (player->speed > 0.0f ) {
2882 //ff
2883 if (p_status->time_cur < player->last_cur_time ) {
2884 DVR_PB_DG(1, "get ff time error last[%d]cur[%d]diff[%d]", player->last_cur_time, p_status->time_cur, player->last_cur_time - p_status->time_cur);
2885 p_status->time_cur = player->last_cur_time;
2886 } else {
2887 player->last_cur_time = p_status->time_cur;
2888 }
2889 } else if (player->speed < -1.0f){
2890 //fb
2891 if (p_status->time_cur > player->last_cur_time ) {
2892 DVR_PB_DG(1, "get fb time error last[%d]cur[%d]diff[%d]", player->last_cur_time, p_status->time_cur, p_status->time_cur - player->last_cur_time );
2893 p_status->time_cur = player->last_cur_time;
2894 } else {
2895 player->last_cur_time = p_status->time_cur;
2896 }
2897 }
2898 } else {
2899 player->last_cur_time = p_status->time_cur;
2900 }
2901 player->last_send_time_id = player->cur_segment_id;
hualing chen041c4092020-04-05 15:11:50 +08002902 p_status->segment_id = player->cur_segment_id;
hualing chen2aba4022020-03-02 13:49:55 +08002903
hualing chen5cbe1a62020-02-10 16:36:36 +08002904 memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t));
hualing chencc91e1c2020-02-28 13:26:17 +08002905 p_status->speed = player->cmd.speed.speed.speed;
hualing chen5cbe1a62020-02-10 16:36:36 +08002906 p_status->flags = player->cur_segment.flags;
hualing chen2932d372020-04-29 13:44:00 +08002907 DVR_PB_DG(1, "player real state[%s]state[%s]cur[%d]end[%d] id[%lld]playflag[%d]speed[%f]is_lock[%d]",
hualing chen6d24aa92020-03-23 18:43:47 +08002908 _dvr_playback_state_toString(player->state),
2909 _dvr_playback_state_toString(p_status->state),
hualing chena540a7e2020-03-27 16:44:05 +08002910 p_status->time_cur, p_status->time_end,
2911 p_status->segment_id,player->play_flag,
hualing chen2932d372020-04-29 13:44:00 +08002912 player->speed,
2913 is_lock);
2914 if (is_lock ==DVR_TRUE)
2915 pthread_mutex_unlock(&player->lock);
2916 return DVR_SUCCESS;
2917}
2918
2919
2920/**\brief Get playback status
2921 * \param[in] handle playback handle
2922 * \param[out] p_status playback status
2923 * \retval DVR_SUCCESS On success
2924 * \return Error code
2925 */
2926int dvr_playback_get_status(DVR_PlaybackHandle_t handle,
2927 DVR_PlaybackStatus_t *p_status) {
2928//
2929 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2930
2931 if (player == NULL) {
2932 DVR_PB_DG(1, "player is NULL");
2933 return DVR_FAILURE;
2934 }
2935 _dvr_playback_get_status(handle, p_status, DVR_TRUE);
2936
hualing chenb31a6c62020-01-13 17:27:00 +08002937 return DVR_SUCCESS;
2938}
2939
hualing chen040df222020-01-17 13:35:02 +08002940void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) {
2941 if (segment != NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002942 DVR_PB_DG(1, "segment id: %lld", segment->segment_id);
2943 DVR_PB_DG(1, "segment flag: %d", segment->flags);
2944 DVR_PB_DG(1, "segment location: [%s]", segment->location);
2945 DVR_PB_DG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format);
2946 DVR_PB_DG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format);
2947 DVR_PB_DG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format);
2948 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 +08002949 }
hualing chenb31a6c62020-01-13 17:27:00 +08002950}
2951
hualing chen5cbe1a62020-02-10 16:36:36 +08002952int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) {
hualing chen040df222020-01-17 13:35:02 +08002953 DVR_Playback_t *player = (DVR_Playback_t *) handle;
hualing chenb31a6c62020-01-13 17:27:00 +08002954
hualing chena540a7e2020-03-27 16:44:05 +08002955 if (player == NULL) {
hualing chen4b7c15d2020-04-07 16:13:48 +08002956 DVR_PB_DG(1, "player is NULL");
hualing chena540a7e2020-03-27 16:44:05 +08002957 return DVR_FAILURE;
2958 }
2959
hualing chen040df222020-01-17 13:35:02 +08002960 DVR_PlaybackSegmentInfo_t *segment;
2961 list_for_each_entry(segment, &player->segment_list, head)
hualing chen86e7d482020-01-16 15:13:33 +08002962 {
hualing chen040df222020-01-17 13:35:02 +08002963 if (segment_id >= 0) {
2964 if (segment->segment_id == segment_id) {
2965 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08002966 break;
2967 }
2968 } else {
hualing chen5cbe1a62020-02-10 16:36:36 +08002969 //printf segment info
hualing chen040df222020-01-17 13:35:02 +08002970 _dvr_dump_segment(segment);
hualing chen86e7d482020-01-16 15:13:33 +08002971 }
2972 }
2973 return 0;
hualing chenb31a6c62020-01-13 17:27:00 +08002974}
pengfei.liu07ddc8a2020-03-24 23:36:53 +08002975
pengfei.liu27cc4ec2020-04-03 16:28:16 +08002976int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata)
pengfei.liu07ddc8a2020-03-24 23:36:53 +08002977{
2978 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2979 DVR_RETURN_IF_FALSE(player);
2980 DVR_RETURN_IF_FALSE(func);
2981
hualing chen4b7c15d2020-04-07 16:13:48 +08002982 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08002983 pthread_mutex_lock(&player->lock);
2984
2985 player->dec_func = func;
2986 player->dec_userdata = userdata;
2987
2988 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08002989 DVR_PB_DG(1, "out ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08002990 return DVR_SUCCESS;
2991}
2992
2993int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len)
2994{
2995 DVR_Playback_t *player = (DVR_Playback_t *) handle;
2996 DVR_RETURN_IF_FALSE(player);
2997 DVR_RETURN_IF_FALSE(p_secure_buf);
2998 DVR_RETURN_IF_FALSE(len);
2999
hualing chen4b7c15d2020-04-07 16:13:48 +08003000 DVR_PB_DG(1, "in ");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003001 pthread_mutex_lock(&player->lock);
3002
3003 player->is_secure_mode = 1;
3004 player->secure_buffer = p_secure_buf;
3005 player->secure_buffer_size = len;
3006
3007 pthread_mutex_unlock(&player->lock);
hualing chen4b7c15d2020-04-07 16:13:48 +08003008 DVR_PB_DG(1, "out");
pengfei.liu07ddc8a2020-03-24 23:36:53 +08003009 return DVR_SUCCESS;
3010}