hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 4 | #include <string.h> |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 5 | #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> |
| 14 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 15 | #include "dvr_playback.h" |
| 16 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 17 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 18 | #define VALID_PID(_pid_) ((_pid_)>0 && (_pid_)<0x1fff) |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 19 | |
| 20 | |
| 21 | #define FF_SPEED (2.0f) |
| 22 | #define FB_SPEED (-1.0f) |
| 23 | #define IS_FFFB(_SPEED_) ((_SPEED_) > FF_SPEED && (_SPEED_) < FB_SPEED) |
| 24 | #define IS_FB(_SPEED_) ((_SPEED_) < FB_SPEED) |
| 25 | |
| 26 | #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)) |
| 27 | #define IS_FAST_SPEED(_SPEED_) (((_SPEED_) == PLAYBACK_SPEED_X2) || ((_SPEED_) == PLAYBACK_SPEED_S2) || ((_SPEED_) == PLAYBACK_SPEED_S4) || ((_SPEED_) == PLAYBACK_SPEED_S8)) |
| 28 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 29 | |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 30 | #define FFFB_SLEEP_TIME (500)//500ms |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 31 | #define FB_DEFAULT_LEFT_TIME (2000) |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 32 | //if tsplayer delay time < 200 and no data can read, we will pause |
| 33 | #define MIN_TSPLAYER_DELAY_TIME (200) |
| 34 | |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 35 | //get current segment current pcr time of read pos |
| 36 | static int g_cache_time = 0; |
| 37 | #define MAX_CACHE_TIME (30000) |
| 38 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 39 | static int write_success = 0; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 40 | // |
| 41 | static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 42 | static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type); |
| 43 | static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle); |
| 44 | static int _dvr_get_end_time(DVR_PlaybackHandle_t handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 45 | static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 46 | static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) ; |
| 47 | |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 48 | static char* _dvr_playback_state_toString(int stat) |
| 49 | { |
| 50 | char *string[DVR_PLAYBACK_STATE_FB+1]={ |
| 51 | "start", |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 52 | "stop", |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 53 | "pause", |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 54 | "ff", |
| 55 | "fb" |
| 56 | }; |
| 57 | |
| 58 | if (stat > DVR_PLAYBACK_STATE_FB) { |
| 59 | return "unkown"; |
| 60 | } else { |
| 61 | return string[stat]; |
| 62 | } |
| 63 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 64 | |
| 65 | static DVR_Bool_t _dvr_support_speed(int speed) { |
| 66 | |
| 67 | DVR_Bool_t ret = DVR_FALSE; |
| 68 | |
| 69 | switch (speed) { |
| 70 | case PLAYBACK_SPEED_FBX2: |
| 71 | case PLAYBACK_SPEED_FBX4: |
| 72 | case PLAYBACK_SPEED_FBX8: |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 73 | case PLAYBACK_SPEED_FBX16: |
| 74 | case PLAYBACK_SPEED_FBX12: |
| 75 | case PLAYBACK_SPEED_FBX32: |
| 76 | case PLAYBACK_SPEED_FBX48: |
| 77 | case PLAYBACK_SPEED_FBX64: |
| 78 | case PLAYBACK_SPEED_FBX128: |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 79 | case PLAYBACK_SPEED_S2: |
| 80 | case PLAYBACK_SPEED_S4: |
| 81 | case PLAYBACK_SPEED_S8: |
| 82 | case PLAYBACK_SPEED_X1: |
| 83 | case PLAYBACK_SPEED_X2: |
| 84 | case PLAYBACK_SPEED_X4: |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 85 | case PLAYBACK_SPEED_X3: |
| 86 | case PLAYBACK_SPEED_X5: |
| 87 | case PLAYBACK_SPEED_X6: |
| 88 | case PLAYBACK_SPEED_X7: |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 89 | case PLAYBACK_SPEED_X8: |
| 90 | case PLAYBACK_SPEED_X12: |
| 91 | case PLAYBACK_SPEED_X16: |
| 92 | case PLAYBACK_SPEED_X32: |
| 93 | case PLAYBACK_SPEED_X48: |
| 94 | case PLAYBACK_SPEED_X64: |
| 95 | case PLAYBACK_SPEED_X128: |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 96 | ret = DVR_TRUE; |
| 97 | break; |
| 98 | default: |
| 99 | DVR_DEBUG(1, "not support speed is set [%d]", speed); |
| 100 | break; |
| 101 | } |
| 102 | return ret; |
| 103 | } |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 104 | void _dvr_tsplayer_callback_test(void *user_data, am_tsplayer_event *event) |
| 105 | { |
| 106 | DVR_DEBUG(1, "in callback test "); |
| 107 | DVR_Playback_t *player = NULL; |
| 108 | if (user_data != NULL) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 109 | player = (DVR_Playback_t *) user_data; |
| 110 | DVR_DEBUG(1, "play speed [%f] in callback test ", player->speed); |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 111 | } |
| 112 | switch (event->type) { |
| 113 | case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: |
| 114 | { |
| 115 | DVR_DEBUG(1,"[evt] test AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n", |
| 116 | event->event.video_format.frame_width, |
| 117 | event->event.video_format.frame_height, |
| 118 | event->event.video_format.frame_rate); |
| 119 | break; |
| 120 | } |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 121 | case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME: |
| 122 | { |
| 123 | DVR_DEBUG(1, "[evt] test AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n"); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 124 | player->first_frame = 1; |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 131 | void _dvr_tsplayer_callback(void *user_data, am_tsplayer_event *event) |
| 132 | { |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 133 | DVR_Playback_t *player = NULL; |
| 134 | if (user_data != NULL) { |
| 135 | player = (DVR_Playback_t *) user_data; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 136 | DVR_DEBUG(1, "play speed [%f] in-- callback", player->speed); |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 137 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 138 | switch (event->type) { |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 139 | case AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: |
| 140 | { |
| 141 | DVR_DEBUG(1,"[evt] AM_TSPLAYER_EVENT_TYPE_VIDEO_CHANGED: %d x %d @%d\n", |
| 142 | event->event.video_format.frame_width, |
| 143 | event->event.video_format.frame_height, |
| 144 | event->event.video_format.frame_rate); |
| 145 | break; |
| 146 | } |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 147 | case AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME: |
| 148 | { |
| 149 | DVR_DEBUG(1, "[evt] AM_TSPLAYER_EVENT_TYPE_FIRST_FRAME\n"); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 150 | if (player != NULL) |
| 151 | player->first_frame = 1; |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | default: |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 155 | DVR_DEBUG(1, "[evt]unkown event [%d]\n", event->type); |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | if (player&&player->player_callback_func) { |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 159 | DVR_DEBUG(1, "player is nonull, --call callback\n"); |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 160 | player->player_callback_func(player->player_callback_userdata, event); |
| 161 | } else if (player == NULL){ |
| 162 | DVR_DEBUG(1, "player is null, get userdata error\n"); |
| 163 | } else { |
| 164 | DVR_DEBUG(1, "player callback is null, get callback error\n"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 165 | } |
| 166 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 167 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 168 | //convert video and audio fmt |
| 169 | static int _dvr_convert_stream_fmt(int fmt, DVR_Bool_t is_audio) { |
| 170 | int format = 0; |
| 171 | if (is_audio == DVR_FALSE) { |
| 172 | //for video fmt |
| 173 | switch (fmt) |
| 174 | { |
| 175 | case DVR_VIDEO_FORMAT_MPEG1: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 176 | format = AV_VIDEO_CODEC_MPEG1; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 177 | break; |
| 178 | case DVR_VIDEO_FORMAT_MPEG2: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 179 | format = AV_VIDEO_CODEC_MPEG2; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 180 | break; |
| 181 | case DVR_VIDEO_FORMAT_HEVC: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 182 | format = AV_VIDEO_CODEC_H265; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 183 | break; |
| 184 | case DVR_VIDEO_FORMAT_H264: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 185 | format = AV_VIDEO_CODEC_H264; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 186 | break; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 187 | case DVR_VIDEO_FORMAT_VP9: |
| 188 | format = AV_VIDEO_CODEC_VP9; |
| 189 | break; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 190 | } |
| 191 | } else { |
| 192 | //for audio fmt |
| 193 | switch (fmt) |
| 194 | { |
| 195 | case DVR_AUDIO_FORMAT_MPEG: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 196 | format = AV_AUDIO_CODEC_MP2; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 197 | break; |
| 198 | case DVR_AUDIO_FORMAT_AC3: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 199 | format = AV_AUDIO_CODEC_AC3; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 200 | break; |
| 201 | case DVR_AUDIO_FORMAT_EAC3: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 202 | format = AV_AUDIO_CODEC_EAC3; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 203 | break; |
| 204 | case DVR_AUDIO_FORMAT_DTS: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 205 | format = AV_AUDIO_CODEC_DTS; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 206 | break; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 207 | case DVR_AUDIO_FORMAT_AAC: |
| 208 | format = AV_AUDIO_CODEC_AAC; |
| 209 | break; |
| 210 | case DVR_AUDIO_FORMAT_LATM: |
| 211 | format = AV_AUDIO_CODEC_LATM; |
| 212 | break; |
| 213 | case DVR_AUDIO_FORMAT_PCM: |
| 214 | format = AV_AUDIO_CODEC_PCM; |
| 215 | break; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | return format; |
| 219 | } |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 220 | static int _dvr_playback_get_trick_stat(DVR_PlaybackHandle_t handle) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 221 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 222 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 223 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 224 | if (player == NULL || player->handle == NULL) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 225 | return -1; |
| 226 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 227 | return player->first_frame; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 228 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 229 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 230 | //get sys time ms |
| 231 | static int _dvr_time_getClock(void) |
| 232 | { |
| 233 | struct timespec ts; |
| 234 | int ms; |
| 235 | |
| 236 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 237 | ms = ts.tv_sec*1000+ts.tv_nsec/1000000; |
| 238 | |
| 239 | return ms; |
| 240 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 241 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 242 | |
| 243 | //timeout wait sibnal |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 244 | static int _dvr_playback_timeoutwait(DVR_PlaybackHandle_t handle , int ms) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 245 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 246 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 247 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 248 | |
| 249 | if (player == NULL) { |
| 250 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 251 | return DVR_FAILURE; |
| 252 | } |
| 253 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 254 | struct timespec ts; |
| 255 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 256 | //ms为毫秒,换算成秒 |
| 257 | ts.tv_sec += ms/1000; |
| 258 | //在outtime的基础上,增加ms毫秒 |
| 259 | //outtime.tv_nsec为纳秒,1微秒=1000纳秒 |
| 260 | //tv_nsec此值再加上剩余的毫秒数 ms%1000,有可能超过1秒。需要特殊处理 |
| 261 | uint64_t us = ts.tv_nsec/1000 + 1000 * (ms % 1000); //微秒 |
| 262 | //us的值有可能超过1秒, |
| 263 | ts.tv_sec += us / 1000000; |
| 264 | us = us % 1000000; |
| 265 | ts.tv_nsec = us * 1000;//换算成纳秒 |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 266 | pthread_cond_timedwait(&player->cond, &player->lock, &ts); |
| 267 | return 0; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 268 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 269 | //get tsplay delay time ms |
| 270 | static int _dvr_playback_get_delaytime(DVR_PlaybackHandle_t handle ) { |
| 271 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 272 | int64_t cache = 0; |
| 273 | if (player == NULL || player->handle == NULL) { |
| 274 | DVR_DEBUG(1, "tsplayer delay time error, handle is NULL"); |
| 275 | return 0; |
| 276 | } |
| 277 | AmTsPlayer_getDelayTime(player->handle, &cache); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 278 | DVR_DEBUG(1, "tsplayer cache time [%lld]ms", cache); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 279 | return cache; |
| 280 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 281 | //send signal |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 282 | static int _dvr_playback_sendSignal(DVR_PlaybackHandle_t handle) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 283 | { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 284 | DVR_Playback_t *player = (DVR_Playback_t *) handle;\ |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 285 | |
| 286 | if (player == NULL) { |
| 287 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 288 | return DVR_FAILURE; |
| 289 | } |
| 290 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 291 | pthread_mutex_lock(&player->lock); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 292 | pthread_cond_signal(&player->cond); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 293 | pthread_mutex_unlock(&player->lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 294 | return 0; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 295 | } |
| 296 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 297 | //send playback event |
| 298 | static int _dvr_playback_sent_event(DVR_PlaybackHandle_t handle, DVR_PlaybackEvent_t evt, DVR_Play_Notify_t *notify) { |
| 299 | |
| 300 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 301 | |
| 302 | if (player == NULL) { |
| 303 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 304 | return DVR_FAILURE; |
| 305 | } |
| 306 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 307 | switch (evt) { |
| 308 | case DVR_PLAYBACK_EVENT_ERROR: |
| 309 | break; |
| 310 | case DVR_PLAYBACK_EVENT_TRANSITION_OK: |
| 311 | //GET STATE |
| 312 | DVR_DEBUG(1, "trans ok----"); |
| 313 | dvr_playback_get_status(handle, &(notify->play_status)); |
| 314 | break; |
| 315 | case DVR_PLAYBACK_EVENT_TRANSITION_FAILED: |
| 316 | break; |
| 317 | case DVR_PLAYBACK_EVENT_KEY_FAILURE: |
| 318 | break; |
| 319 | case DVR_PLAYBACK_EVENT_NO_KEY: |
| 320 | break; |
| 321 | case DVR_PLAYBACK_EVENT_REACHED_BEGIN: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 322 | //GET STATE |
| 323 | DVR_DEBUG(1, "reached begin---"); |
| 324 | dvr_playback_get_status(handle, &(notify->play_status)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 325 | break; |
| 326 | case DVR_PLAYBACK_EVENT_REACHED_END: |
| 327 | //GET STATE |
| 328 | DVR_DEBUG(1, "reached end---"); |
| 329 | dvr_playback_get_status(handle, &(notify->play_status)); |
| 330 | break; |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 331 | case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME: |
| 332 | //DVR_DEBUG(1, "send playtime---"); |
| 333 | dvr_playback_get_status(handle, &(notify->play_status)); |
| 334 | break; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 335 | default: |
| 336 | break; |
| 337 | } |
| 338 | if (player->openParams.event_fn != NULL) |
| 339 | player->openParams.event_fn(evt, (void*)notify, player->openParams.event_userdata); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 340 | return DVR_SUCCESS; |
| 341 | } |
| 342 | static int _dvr_playback_sent_transition_ok(DVR_PlaybackHandle_t handle) |
| 343 | { |
| 344 | DVR_Play_Notify_t notify; |
| 345 | memset(¬ify, 0 , sizeof(DVR_Play_Notify_t)); |
| 346 | notify.event = DVR_PLAYBACK_EVENT_TRANSITION_OK; |
| 347 | //get play statue not here |
| 348 | _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_OK, ¬ify); |
| 349 | return DVR_SUCCESS; |
| 350 | } |
| 351 | |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 352 | static int _dvr_playback_sent_playtime(DVR_PlaybackHandle_t handle) |
| 353 | { |
| 354 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 355 | |
| 356 | if (player == NULL) { |
| 357 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 358 | return DVR_FAILURE; |
| 359 | } |
| 360 | |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 361 | if (player->send_time ==0) { |
| 362 | player->send_time = _dvr_time_getClock() + 1000; |
| 363 | } else if (player->send_time > _dvr_time_getClock()) { |
| 364 | return DVR_SUCCESS; |
| 365 | } |
| 366 | player->send_time = _dvr_time_getClock() + 1000; |
| 367 | DVR_Play_Notify_t notify; |
| 368 | memset(¬ify, 0 , sizeof(DVR_Play_Notify_t)); |
| 369 | notify.event = DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME; |
| 370 | //get play statue not here |
| 371 | _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME, ¬ify); |
| 372 | return DVR_SUCCESS; |
| 373 | } |
| 374 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 375 | //check is ongoing segment |
| 376 | static int _dvr_check_segment_ongoing(DVR_PlaybackHandle_t handle) { |
| 377 | |
| 378 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 379 | int ret = DVR_FAILURE; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 380 | |
| 381 | if (player == NULL) { |
| 382 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 383 | return DVR_FAILURE; |
| 384 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 385 | ret = segment_ongoing(player->r_handle); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 386 | if (ret != DVR_SUCCESS) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 387 | return DVR_FALSE; |
| 388 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 389 | return DVR_TRUE; |
| 390 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 391 | static int _dvr_init_fffb_time(DVR_PlaybackHandle_t handle) { |
| 392 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 393 | player->fffb_current = -1; |
| 394 | player->fffb_start = -1; |
| 395 | player->fffb_start_pcr = -1; |
| 396 | player->next_fffb_time = _dvr_time_getClock(); |
| 397 | return DVR_SUCCESS; |
| 398 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 399 | //get next segment id |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 400 | static int _dvr_has_next_segmentId(DVR_PlaybackHandle_t handle, int segmentid) { |
| 401 | |
| 402 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 403 | DVR_PlaybackSegmentInfo_t *segment; |
| 404 | DVR_PlaybackSegmentInfo_t *pre_segment = NULL; |
| 405 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 406 | if (player == NULL) { |
| 407 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 408 | return DVR_FAILURE; |
| 409 | } |
| 410 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 411 | int found = 0; |
| 412 | int found_eq_id = 0; |
| 413 | list_for_each_entry(segment, &player->segment_list, head) |
| 414 | { |
| 415 | if (player->segment_is_open == DVR_FALSE) { |
| 416 | //get first segment from list, case segment is not open |
| 417 | if (!IS_FB(player->speed)) |
| 418 | found = 1; |
| 419 | } else if (segment->segment_id == segmentid) { |
| 420 | //find cur segment, we need get next one |
| 421 | found_eq_id = 1; |
| 422 | if (!IS_FB(player->speed)) { |
| 423 | found = 1; |
| 424 | continue; |
| 425 | } else { |
| 426 | //if is fb mode.we need used pre segment |
| 427 | if (pre_segment != NULL) { |
| 428 | found = 1; |
| 429 | } else { |
| 430 | //not find next id. |
| 431 | DVR_DEBUG(1, "not has find next segment on fb mode"); |
| 432 | return DVR_FAILURE; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | if (found == 1) { |
| 437 | found = 2; |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | if (found != 2) { |
| 442 | //list is null or reache list end |
| 443 | DVR_DEBUG(1, "not found next segment return failure"); |
| 444 | return DVR_FAILURE; |
| 445 | } |
| 446 | DVR_DEBUG(1, "found next segment return success"); |
| 447 | return DVR_SUCCESS; |
| 448 | } |
| 449 | |
| 450 | //get next segment id |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 451 | static int _dvr_get_next_segmentId(DVR_PlaybackHandle_t handle) { |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 452 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 453 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 454 | DVR_PlaybackSegmentInfo_t *segment; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 455 | DVR_PlaybackSegmentInfo_t *pre_segment = NULL; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 456 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 457 | if (player == NULL) { |
| 458 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 459 | return DVR_FAILURE; |
| 460 | } |
| 461 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 462 | int found = 0; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 463 | int found_eq_id = 0; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 464 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 465 | list_for_each_entry(segment, &player->segment_list, head) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 466 | { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 467 | if (player->segment_is_open == DVR_FALSE) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 468 | //get first segment from list, case segment is not open |
| 469 | if (!IS_FB(player->speed)) |
| 470 | found = 1; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 471 | } else if (segment->segment_id == player->cur_segment_id) { |
| 472 | //find cur segment, we need get next one |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 473 | found_eq_id = 1; |
| 474 | if (!IS_FB(player->speed)) { |
| 475 | found = 1; |
| 476 | continue; |
| 477 | } else { |
| 478 | //if is fb mode.we need used pre segment |
| 479 | if (pre_segment != NULL) { |
| 480 | found = 1; |
| 481 | } else { |
| 482 | //not find next id. |
| 483 | DVR_DEBUG(1, "not find next segment on fb mode"); |
| 484 | return DVR_FAILURE; |
| 485 | } |
| 486 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 487 | } |
| 488 | if (found == 1) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 489 | if (IS_FB(player->speed)) { |
| 490 | //used pre segment |
| 491 | segment = pre_segment; |
| 492 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 493 | //save segment info |
| 494 | player->last_segment_id = player->cur_segment_id; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 495 | player->last_segment.segment_id = player->cur_segment.segment_id; |
| 496 | player->last_segment.flags = player->cur_segment.flags; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 497 | memcpy(player->last_segment.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE); |
| 498 | //pids |
| 499 | memcpy(&player->last_segment.pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t)); |
| 500 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 501 | //get segment info |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 502 | player->segment_is_open = DVR_TRUE; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 503 | player->cur_segment_id = segment->segment_id; |
| 504 | player->cur_segment.segment_id = segment->segment_id; |
| 505 | player->cur_segment.flags = segment->flags; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 506 | DVR_DEBUG(1, "cur flag[0x%x]segment->flags flag[0x%x] id [%lld]", player->cur_segment.flags, segment->flags, segment->segment_id); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 507 | memcpy(player->cur_segment.location, segment->location, DVR_MAX_LOCATION_SIZE); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 508 | //pids |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 509 | memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t)); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 510 | found = 2; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 511 | break; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 512 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 513 | pre_segment = segment; |
| 514 | } |
| 515 | if (player->segment_is_open == DVR_FALSE && IS_FB(player->speed)) { |
| 516 | //used the last one segment to open |
| 517 | //get segment info |
| 518 | player->segment_is_open = DVR_TRUE; |
| 519 | player->cur_segment_id = pre_segment->segment_id; |
| 520 | player->cur_segment.segment_id = pre_segment->segment_id; |
| 521 | player->cur_segment.flags = pre_segment->flags; |
| 522 | DVR_DEBUG(1, "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); |
| 523 | memcpy(player->cur_segment.location, pre_segment->location, DVR_MAX_LOCATION_SIZE); |
| 524 | //pids |
| 525 | memcpy(&player->cur_segment.pids, &pre_segment->pids, sizeof(DVR_PlaybackPids_t)); |
| 526 | return DVR_SUCCESS; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 527 | } |
| 528 | if (found != 2) { |
| 529 | //list is null or reache list end |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 530 | return DVR_FAILURE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 531 | } |
| 532 | return DVR_SUCCESS; |
| 533 | } |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 534 | //open next segment to play,if reach list end return errro. |
| 535 | static int _change_to_next_segment(DVR_PlaybackHandle_t handle) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 536 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 537 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 538 | Segment_OpenParams_t params; |
| 539 | int ret = DVR_SUCCESS; |
| 540 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 541 | if (player == NULL) { |
| 542 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 543 | return DVR_FAILURE; |
| 544 | } |
| 545 | |
| 546 | ret = _dvr_get_next_segmentId(handle); |
| 547 | if (ret == DVR_FAILURE) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 548 | DVR_DEBUG(1, "not found segment info"); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 549 | return DVR_FAILURE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (player->r_handle != NULL) { |
| 553 | segment_close(player->r_handle); |
| 554 | player->r_handle = NULL; |
| 555 | } |
| 556 | |
| 557 | memset(params.location, 0, DVR_MAX_LOCATION_SIZE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 558 | //cp chur segment path to location |
| 559 | memcpy(params.location, player->cur_segment.location, DVR_MAX_LOCATION_SIZE); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 560 | params.segment_id = (uint64_t)player->cur_segment.segment_id; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 561 | params.mode = SEGMENT_MODE_READ; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 562 | DVR_DEBUG(1, "open segment info[%s][%lld]flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 563 | pthread_mutex_lock(&player->segment_lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 564 | ret = segment_open(¶ms, &(player->r_handle)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 565 | pthread_mutex_unlock(&player->segment_lock); |
| 566 | int total = _dvr_get_end_time( handle); |
| 567 | pthread_mutex_lock(&player->segment_lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 568 | if (IS_FB(player->speed)) { |
| 569 | //seek end pos -FB_DEFAULT_LEFT_TIME |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 570 | segment_seek(player->r_handle, total - FB_DEFAULT_LEFT_TIME, player->openParams.block_size); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 571 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 572 | player->dur = total; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 573 | pthread_mutex_unlock(&player->segment_lock); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 574 | DVR_DEBUG(1, "next player->dur [%d] flag [0x%x]", player->dur, player->cur_segment.flags); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 578 | //open next segment to play,if reach list end return errro. |
| 579 | static int _dvr_open_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) |
| 580 | { |
| 581 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 582 | Segment_OpenParams_t params; |
| 583 | int ret = DVR_SUCCESS; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 584 | if (player == NULL) { |
| 585 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 586 | return DVR_FAILURE; |
| 587 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 588 | if (segment_id == player->cur_segment_id && player->segment_is_open == DVR_TRUE) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 589 | return DVR_SUCCESS; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 590 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 591 | uint64_t id = segment_id; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 592 | if (id < 0) { |
| 593 | DVR_DEBUG(1, "not found segment info"); |
| 594 | return DVR_FAILURE; |
| 595 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 596 | DVR_DEBUG(1, "start found segment[%lld][%lld] info", id,segment_id); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 597 | pthread_mutex_lock(&player->segment_lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 598 | |
| 599 | DVR_PlaybackSegmentInfo_t *segment; |
| 600 | |
| 601 | int found = 0; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 602 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 603 | list_for_each_entry(segment, &player->segment_list, head) |
| 604 | { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 605 | DVR_DEBUG(1, "see 1 location [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 606 | if (segment->segment_id == segment_id) { |
| 607 | found = 1; |
| 608 | } |
| 609 | if (found == 1) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 610 | DVR_DEBUG(1, "found [%s]id[%lld]flag[%x]segment_id[%lld]", segment->location, segment->segment_id, segment->flags, segment_id); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 611 | //get segment info |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 612 | player->segment_is_open = DVR_TRUE; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 613 | player->cur_segment_id = segment->segment_id; |
| 614 | player->cur_segment.segment_id = segment->segment_id; |
| 615 | player->cur_segment.flags = segment->flags; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 616 | strncpy(player->cur_segment.location, segment->location, sizeof(segment->location));//DVR_MAX_LOCATION_SIZE |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 617 | //pids |
| 618 | memcpy(&player->cur_segment.pids, &segment->pids, sizeof(DVR_PlaybackPids_t)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 619 | DVR_DEBUG(1, "cur found location [%s]id[%lld]flag[%x]", player->cur_segment.location, player->cur_segment.segment_id,player->cur_segment.flags); |
| 620 | break; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 621 | } |
| 622 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 623 | if (found == 0) { |
| 624 | DVR_DEBUG(1, "not found segment info.error.."); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 625 | pthread_mutex_unlock(&player->segment_lock); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 626 | return DVR_FAILURE; |
| 627 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 628 | memset(params.location, 0, DVR_MAX_LOCATION_SIZE); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 629 | //cp cur segment path to location |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 630 | strncpy(params.location, player->cur_segment.location, sizeof(player->cur_segment.location)); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 631 | params.segment_id = (uint64_t)player->cur_segment.segment_id; |
| 632 | params.mode = SEGMENT_MODE_READ; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 633 | DVR_DEBUG(1, "open segment location[%s][%lld]cur flag[0x%x]", params.location, params.segment_id, player->cur_segment.flags); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 634 | if (player->r_handle != NULL) { |
| 635 | segment_close(player->r_handle); |
| 636 | player->r_handle = NULL; |
| 637 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 638 | ret = segment_open(¶ms, &(player->r_handle)); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 639 | pthread_mutex_unlock(&player->segment_lock); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 640 | player->dur = _dvr_get_end_time(handle); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 641 | |
| 642 | DVR_DEBUG(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 chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | //get play info by segment id |
| 648 | static int _dvr_playback_get_playinfo(DVR_PlaybackHandle_t handle, |
| 649 | uint64_t segment_id, |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 650 | am_tsplayer_video_params *vparam, |
| 651 | am_tsplayer_audio_params *aparam) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 652 | |
| 653 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 654 | DVR_PlaybackSegmentInfo_t *segment; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 655 | if (player == NULL) { |
| 656 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 657 | return DVR_FAILURE; |
| 658 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 659 | |
| 660 | int found = 0; |
| 661 | |
| 662 | list_for_each_entry(segment, &player->segment_list, head) |
| 663 | { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 664 | if (segment_id == UINT64_MAX) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 665 | //get first segment from list |
| 666 | found = 1; |
| 667 | } |
| 668 | if (segment->segment_id == segment_id) { |
| 669 | found = 1; |
| 670 | } |
| 671 | if (found == 1) { |
| 672 | //get segment info |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 673 | if (player->cur_segment_id != UINT64_MAX) |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 674 | player->cur_segment_id = segment->segment_id; |
| 675 | DVR_DEBUG(1, "get play info id [%lld]", player->cur_segment_id); |
| 676 | player->cur_segment.segment_id = segment->segment_id; |
| 677 | player->cur_segment.flags = segment->flags; |
| 678 | //pids |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 679 | player->cur_segment.pids.video.pid = segment->pids.video.pid; |
| 680 | player->cur_segment.pids.video.format = segment->pids.video.format; |
| 681 | player->cur_segment.pids.video.type = segment->pids.video.type; |
| 682 | player->cur_segment.pids.audio.pid = segment->pids.audio.pid; |
| 683 | player->cur_segment.pids.audio.format = segment->pids.audio.format; |
| 684 | player->cur_segment.pids.audio.type = segment->pids.audio.type; |
| 685 | player->cur_segment.pids.ad.pid = segment->pids.ad.pid; |
| 686 | player->cur_segment.pids.ad.format = segment->pids.ad.format; |
| 687 | player->cur_segment.pids.ad.type = segment->pids.ad.type; |
| 688 | player->cur_segment.pids.pcr.pid = segment->pids.pcr.pid; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 689 | // |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 690 | vparam->codectype = _dvr_convert_stream_fmt(segment->pids.video.format, DVR_FALSE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 691 | vparam->pid = segment->pids.video.pid; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 692 | aparam->codectype = _dvr_convert_stream_fmt(segment->pids.audio.format, DVR_TRUE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 693 | aparam->pid = segment->pids.audio.pid; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 694 | DVR_DEBUG(1, "get play info sucess[0x%x]apid[0x%x]vfmt[%d]afmt[%d]", vparam->pid, aparam->pid, vparam->codectype, aparam->codectype); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 695 | found = 2; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 696 | break; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 697 | } |
| 698 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 699 | if (found != 2) { |
| 700 | //list is null or reache list end |
| 701 | DVR_DEBUG(1, "get play info fail"); |
| 702 | return DVR_FAILURE; |
| 703 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 704 | |
| 705 | return DVR_SUCCESS; |
| 706 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 707 | static int _dvr_replay_changed_pid(DVR_PlaybackHandle_t handle) { |
| 708 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 709 | if (player == NULL) { |
| 710 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 711 | return DVR_FAILURE; |
| 712 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 713 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 714 | //compare cur segment |
| 715 | //if (player->cmd.state == DVR_PLAYBACK_STATE_START) |
| 716 | { |
| 717 | //check video pids, stop or restart |
| 718 | _do_check_pid_info(handle, player->last_segment.pids.video, player->cur_segment.pids.video, 0); |
| 719 | //check audio pids stop or restart |
| 720 | _do_check_pid_info(handle, player->last_segment.pids.audio, player->cur_segment.pids.audio, 1); |
| 721 | //check sub audio pids stop or restart |
| 722 | _do_check_pid_info(handle, player->last_segment.pids.ad, player->cur_segment.pids.ad, 2); |
| 723 | //check pcr pids stop or restart |
| 724 | _do_check_pid_info(handle, player->last_segment.pids.pcr, player->cur_segment.pids.pcr, 3); |
| 725 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 726 | return DVR_SUCCESS; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 727 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 728 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 729 | static int _dvr_check_cur_segment_flag(DVR_PlaybackHandle_t handle) |
| 730 | { |
| 731 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 732 | if (player == NULL) { |
| 733 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 734 | return DVR_FAILURE; |
| 735 | } |
| 736 | DVR_DEBUG(1, "call %s flag[0x%x]id[%lld]last[0x%x][%llu]", __func__, player->cur_segment.flags, player->cur_segment.segment_id, player->last_segment.flags, player->last_segment.segment_id); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 737 | if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE && |
| 738 | (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 739 | //enable display |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 740 | DVR_DEBUG(1, "call %s unmute", __func__); |
| 741 | AmTsPlayer_showVideo(player->handle); |
| 742 | AmTsPlayer_setAudioMute(player->handle, 0, 0); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 743 | } else if ((player->cur_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 && |
| 744 | (player->last_segment.flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 745 | //disable display |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 746 | DVR_DEBUG(1, "call %s mute", __func__); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 747 | AmTsPlayer_hideVideo(player->handle); |
| 748 | AmTsPlayer_setAudioMute(player->handle, 1, 1); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 749 | } |
| 750 | return DVR_SUCCESS; |
| 751 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 752 | static DVR_Bool_t _dvr_pauselive_decode_sucess(DVR_PlaybackHandle_t handle) { |
| 753 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 754 | if (player == NULL) { |
| 755 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 756 | return DVR_TRUE; |
| 757 | } |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 758 | if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 759 | if (player->first_frame == 1) { |
| 760 | return DVR_TRUE; |
| 761 | } else { |
| 762 | return DVR_FALSE; |
| 763 | } |
| 764 | } else { |
| 765 | return DVR_TRUE; |
| 766 | } |
| 767 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 768 | static void* _dvr_playback_thread(void *arg) |
| 769 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 770 | DVR_Playback_t *player = (DVR_Playback_t *) arg; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 771 | //int need_open_segment = 1; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 772 | am_tsplayer_input_buffer wbufs; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 773 | am_tsplayer_input_buffer dec_bufs; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 774 | int ret = DVR_SUCCESS; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 775 | |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 776 | int timeout = 300;//ms |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 777 | uint64_t write_timeout_ms = 50; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 778 | uint8_t *buf = NULL; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 779 | int buf_len = player->openParams.block_size > 0 ? player->openParams.block_size : (256 * 1024); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 780 | DVR_Bool_t b_writed_whole_block = player->openParams.block_size > 0 ? DVR_TRUE:DVR_FALSE; |
| 781 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 782 | int dec_buf_size = buf_len + 188; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 783 | int real_read = 0; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 784 | DVR_Bool_t goto_rewrite = DVR_FALSE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 785 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 786 | if (player->is_secure_mode) { |
| 787 | if (dec_buf_size > player->secure_buffer_size) { |
| 788 | DVR_DEBUG(1, "%s, playback blocksize too large", __func__); |
| 789 | return NULL; |
| 790 | } |
| 791 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 792 | buf = malloc(buf_len); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 793 | if (!buf) { |
| 794 | DVR_DEBUG(1, "%s, Malloc buffer failed", __func__); |
| 795 | return NULL; |
| 796 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 797 | wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL; |
| 798 | wbufs.buf_size = 0; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 799 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 800 | dec_bufs.buf_data = malloc(dec_buf_size); |
| 801 | if (!dec_bufs.buf_data) { |
| 802 | DVR_DEBUG(1, "%s, Malloc dec buffer failed", __func__); |
| 803 | return NULL; |
| 804 | } |
| 805 | dec_bufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL; |
| 806 | dec_bufs.buf_size = dec_buf_size; |
| 807 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 808 | if (player->segment_is_open == DVR_FALSE) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 809 | ret = _change_to_next_segment((DVR_PlaybackHandle_t)player); |
| 810 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 811 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 812 | if (ret != DVR_SUCCESS) { |
| 813 | if (buf != NULL) { |
| 814 | free(buf); |
| 815 | buf = NULL; |
| 816 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 817 | free(dec_bufs.buf_data); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 818 | DVR_DEBUG(1, "get segment error"); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 819 | return NULL; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 820 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 821 | //get play statue not here |
| 822 | _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player); |
| 823 | _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player); |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 824 | //set video show |
| 825 | AmTsPlayer_showVideo(player->handle); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 826 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 827 | int trick_stat = 0; |
| 828 | while (player->is_running/* || player->cmd.last_cmd != player->cmd.cur_cmd*/) { |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 829 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 830 | //check trick stat |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 831 | pthread_mutex_lock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 832 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 833 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK || |
| 834 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 835 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB || |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 836 | player->speed > FF_SPEED ||player->speed <= FB_SPEED || |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 837 | (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 838 | { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 839 | trick_stat = _dvr_playback_get_trick_stat((DVR_PlaybackHandle_t)player); |
| 840 | if (trick_stat > 0) { |
| 841 | DVR_DEBUG(1, "trick stat[%d] is > 0", trick_stat); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 842 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_SEEK || (player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 843 | //check last cmd |
| 844 | if(player->cmd.last_cmd == DVR_PLAYBACK_CMD_PAUSE |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 845 | || ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 846 | && ( player->cmd.cur_cmd == DVR_PLAYBACK_CMD_START |
| 847 | ||player->cmd.last_cmd == DVR_PLAYBACK_CMD_VSTART |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 848 | || player->cmd.last_cmd == DVR_PLAYBACK_CMD_ASTART |
| 849 | || player->cmd.last_cmd == DVR_PLAYBACK_CMD_START))) { |
| 850 | DVR_DEBUG(1, "pause play-------"); |
| 851 | //need change to pause state |
| 852 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE; |
| 853 | player->cmd.state = DVR_PLAYBACK_STATE_PAUSE; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 854 | player->state = DVR_PLAYBACK_STATE_PAUSE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 855 | //clear flag |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 856 | player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 857 | player->first_frame = 0; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 858 | AmTsPlayer_pauseVideoDecoding(player->handle); |
| 859 | AmTsPlayer_pauseAudioDecoding(player->handle); |
hualing chen | 2bd8a7a | 2020-04-02 11:31:03 +0800 | [diff] [blame] | 860 | } else { |
| 861 | DVR_DEBUG(1, "clear first frame value-------"); |
| 862 | player->first_frame = 0; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 863 | } |
| 864 | } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF |
| 865 | || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 866 | ||player->speed > FF_SPEED ||player->speed < FB_SPEED) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 867 | //restart play stream if speed > 2 |
| 868 | if (player->state == DVR_PLAYBACK_STATE_PAUSE || |
| 869 | _dvr_time_getClock() < player->next_fffb_time) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 870 | DVR_DEBUG(1, "fffb timeout----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 chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 871 | //used timeout wait need lock first,so we unlock and lock |
| 872 | //pthread_mutex_unlock(&player->lock); |
| 873 | //pthread_mutex_lock(&player->lock); |
| 874 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
| 875 | pthread_mutex_unlock(&player->lock); |
| 876 | continue; |
| 877 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 878 | DVR_DEBUG(1, "fffb play-------speed[%f][%d][%d]", player->speed, goto_rewrite, real_read); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 879 | pthread_mutex_unlock(&player->lock); |
| 880 | goto_rewrite = DVR_FALSE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 881 | real_read = 0; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 882 | player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE); |
| 883 | player->first_frame = 0; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 884 | _dvr_playback_fffb((DVR_PlaybackHandle_t)player); |
| 885 | pthread_mutex_lock(&player->lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 886 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 887 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 888 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 889 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 890 | if (player->state == DVR_PLAYBACK_STATE_PAUSE) { |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 891 | //check is need send time send end |
| 892 | _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 893 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
| 894 | pthread_mutex_unlock(&player->lock); |
| 895 | continue; |
| 896 | } |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 897 | //when seek action is done. we need drop write timeout data. |
| 898 | if (player->drop_ts == DVR_TRUE) { |
| 899 | goto_rewrite = DVR_FALSE; |
| 900 | real_read = 0; |
| 901 | player->drop_ts = DVR_FALSE; |
| 902 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 903 | if (goto_rewrite == DVR_TRUE) { |
| 904 | goto_rewrite = DVR_FALSE; |
| 905 | pthread_mutex_unlock(&player->lock); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 906 | //DVR_DEBUG(1, "rewrite-player->speed[%f]", player->speed); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 907 | goto rewrite; |
| 908 | } |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 909 | //.check is need send time send end |
| 910 | _dvr_playback_sent_playtime((DVR_PlaybackHandle_t)player); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 911 | |
| 912 | int read = segment_read(player->r_handle, buf + real_read, buf_len - real_read); |
| 913 | pthread_mutex_unlock(&player->lock); |
| 914 | //if on fb mode and read file end , we need calculate pos to retry read. |
| 915 | if (read == 0 && IS_FB(player->speed) && real_read == 0) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 916 | DVR_DEBUG(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 chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 917 | _dvr_playback_calculate_seekpos((DVR_PlaybackHandle_t)player); |
| 918 | pthread_mutex_lock(&player->lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 919 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
| 920 | pthread_mutex_unlock(&player->lock); |
| 921 | continue; |
| 922 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 923 | //DVR_DEBUG(1, "read ts [%d]buf_len[%d]speed[%f]real_read:%d", read, buf_len, player->speed, real_read); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 924 | if (read == 0) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 925 | //file end.need to play next segment |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 926 | int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 927 | //init fffb time if change segment |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 928 | _dvr_init_fffb_time((DVR_PlaybackHandle_t)player); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 929 | |
| 930 | int delay = _dvr_playback_get_delaytime((DVR_PlaybackHandle_t)player); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 931 | if (ret != DVR_SUCCESS && |
| 932 | (delay <= MIN_TSPLAYER_DELAY_TIME || |
| 933 | delay > MAX_CACHE_TIME || |
| 934 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 935 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) && |
| 936 | _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 937 | //send end event to hal |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 938 | DVR_Play_Notify_t notify; |
| 939 | memset(¬ify, 0 , sizeof(DVR_Play_Notify_t)); |
| 940 | notify.event = DVR_PLAYBACK_EVENT_REACHED_END; |
| 941 | //get play statue not here |
| 942 | dvr_playback_pause((DVR_PlaybackHandle_t)player, DVR_FALSE); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 943 | _dvr_playback_sent_event((DVR_PlaybackHandle_t)player, DVR_PLAYBACK_EVENT_REACHED_END, ¬ify); |
| 944 | //continue,timeshift mode, when read end,need wait cur recording segment |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 945 | DVR_DEBUG(1, "playback is send end delay:[%d]", delay); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 946 | pthread_mutex_lock(&player->lock); |
| 947 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
| 948 | pthread_mutex_unlock(&player->lock); |
| 949 | continue; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 950 | } else if (ret != DVR_SUCCESS) { |
| 951 | //not send event and pause,sleep and go to next time to recheck |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 952 | DVR_DEBUG(1, "delay:%d pauselive:%d", delay, _dvr_pauselive_decode_sucess((DVR_PlaybackHandle_t)player)); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 953 | pthread_mutex_lock(&player->lock); |
| 954 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
| 955 | pthread_mutex_unlock(&player->lock); |
| 956 | continue; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 957 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 958 | //change next segment success case |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 959 | _dvr_playback_sent_transition_ok((DVR_PlaybackHandle_t)player); |
| 960 | pthread_mutex_lock(&player->lock); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 961 | DVR_DEBUG(1, "_dvr_replay_changed_pid:start"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 962 | _dvr_replay_changed_pid((DVR_PlaybackHandle_t)player); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 963 | DVR_DEBUG(1, "_dvr_replay_changed_pid:end"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 964 | _dvr_check_cur_segment_flag((DVR_PlaybackHandle_t)player); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 965 | read = segment_read(player->r_handle, buf + real_read, buf_len - real_read); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 966 | pthread_mutex_unlock(&player->lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 967 | } |
| 968 | real_read = real_read + read; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 969 | wbufs.buf_size = real_read; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 970 | wbufs.buf_data = buf; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 971 | //check read data len,iflen < 0, we need continue |
| 972 | if (wbufs.buf_size == 0 || wbufs.buf_data == NULL) { |
| 973 | DVR_DEBUG(1, "error occur read_read [%d],buf=[%p]",wbufs.buf_size, wbufs.buf_data); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 974 | real_read = 0; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 975 | continue; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 976 | } |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 977 | //if need write whole block size, we need check read buf len is eq block size. |
| 978 | if (b_writed_whole_block == DVR_TRUE) { |
| 979 | //buf_len is block size value. |
| 980 | if (real_read < buf_len) { |
| 981 | //coontinue to read data from file |
| 982 | DVR_DEBUG(1, "read buf len[%d] is < block size [%d]", real_read, buf_len); |
| 983 | pthread_mutex_lock(&player->lock); |
| 984 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
| 985 | pthread_mutex_unlock(&player->lock); |
| 986 | continue; |
| 987 | } else if (real_read > buf_len) { |
| 988 | DVR_DEBUG(1, "read buf len[%d] is > block size [%d],this error occur", real_read, buf_len); |
| 989 | } |
| 990 | } |
| 991 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 992 | if (player->dec_func) { |
| 993 | DVR_CryptoParams_t crypto_params; |
| 994 | |
| 995 | memset(&crypto_params, 0, sizeof(crypto_params)); |
| 996 | crypto_params.type = DVR_CRYPTO_TYPE_DECRYPT; |
| 997 | memcpy(crypto_params.location, player->cur_segment.location, strlen(player->cur_segment.location)); |
| 998 | crypto_params.segment_id = player->cur_segment.segment_id; |
| 999 | crypto_params.offset = segment_tell_position(player->r_handle); |
| 1000 | |
| 1001 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 1002 | crypto_params.input_buffer.addr = (size_t)buf; |
| 1003 | crypto_params.input_buffer.size = real_read; |
| 1004 | |
| 1005 | if (player->is_secure_mode) { |
| 1006 | crypto_params.output_buffer.type = DVR_BUFFER_TYPE_SECURE; |
| 1007 | crypto_params.output_buffer.addr = (size_t)player->secure_buffer; |
| 1008 | crypto_params.output_buffer.size = dec_buf_size; |
| 1009 | ret = player->dec_func(&crypto_params, player->dec_userdata); |
| 1010 | wbufs.buf_data = player->secure_buffer; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 1011 | wbufs.buf_type = TS_INPUT_BUFFER_TYPE_SECURE; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 1012 | } else { |
| 1013 | crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 1014 | crypto_params.output_buffer.addr = (size_t)dec_bufs.buf_data; |
| 1015 | crypto_params.output_buffer.size = dec_buf_size; |
| 1016 | ret = player->dec_func(&crypto_params, player->dec_userdata); |
| 1017 | wbufs.buf_data = dec_bufs.buf_data; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 1018 | wbufs.buf_type = TS_INPUT_BUFFER_TYPE_NORMAL; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 1019 | } |
| 1020 | if (ret != DVR_SUCCESS) { |
| 1021 | DVR_DEBUG(1, "%s, decrypt failed", __func__); |
| 1022 | } |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 1023 | wbufs.buf_size = crypto_params.output_size; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 1024 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1025 | rewrite: |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 1026 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1027 | ret = AmTsPlayer_writeData(player->handle, &wbufs, write_timeout_ms); |
| 1028 | if (ret == AM_TSPLAYER_OK) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1029 | real_read = 0; |
| 1030 | write_success++; |
| 1031 | continue; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1032 | } else { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1033 | DVR_DEBUG(1, "write time out write_success:%d", write_success); |
| 1034 | write_success = 0; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1035 | pthread_mutex_lock(&player->lock); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1036 | _dvr_playback_timeoutwait((DVR_PlaybackHandle_t)player, timeout); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1037 | pthread_mutex_unlock(&player->lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1038 | if (!player->is_running) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1039 | DVR_DEBUG(1, "playback thread exit"); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1040 | break; |
| 1041 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1042 | goto_rewrite = DVR_TRUE; |
| 1043 | //goto rewrite; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1044 | } |
| 1045 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1046 | DVR_DEBUG(1, "playback thread is end"); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1047 | free(buf); |
| 1048 | free(dec_bufs.buf_data); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1049 | return NULL; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1053 | static int _start_playback_thread(DVR_PlaybackHandle_t handle) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1054 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1055 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1056 | DVR_DEBUG(1, "start thread------[%d]", player->is_running); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1057 | |
| 1058 | if (player == NULL) { |
| 1059 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1060 | return DVR_FAILURE; |
| 1061 | } |
| 1062 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1063 | if (player->is_running == DVR_TRUE) { |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1066 | player->is_running = DVR_TRUE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1067 | int rc = pthread_create(&player->playback_thread, NULL, _dvr_playback_thread, (void*)player); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1068 | if (rc < 0) |
| 1069 | player->is_running = DVR_FALSE; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1070 | DVR_DEBUG(1, "start thread------[%d] end", player->is_running); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1071 | return 0; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1075 | static int _stop_playback_thread(DVR_PlaybackHandle_t handle) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1076 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1077 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1078 | |
| 1079 | if (player == NULL) { |
| 1080 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1081 | return DVR_FAILURE; |
| 1082 | } |
| 1083 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1084 | DVR_DEBUG(1, "stopthread------[%d]", player->is_running); |
| 1085 | if (player->is_running == DVR_TRUE) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1086 | { |
| 1087 | player->is_running = DVR_FALSE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1088 | _dvr_playback_sendSignal(handle); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1089 | //pthread_cond_signal(&player->cond); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1090 | pthread_join(player->playback_thread, NULL); |
| 1091 | } |
| 1092 | if (player->r_handle) { |
| 1093 | segment_close(player->r_handle); |
| 1094 | player->r_handle = NULL; |
| 1095 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1096 | DVR_DEBUG(1, "stopthread------[%d]", player->is_running); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1100 | /**\brief Open an dvr palyback |
| 1101 | * \param[out] p_handle dvr playback addr |
| 1102 | * \param[in] params dvr playback open parameters |
| 1103 | * \retval DVR_SUCCESS On success |
| 1104 | * \return Error code |
| 1105 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1106 | int dvr_playback_open(DVR_PlaybackHandle_t *p_handle, DVR_PlaybackOpenParams_t *params) { |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1107 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1108 | DVR_Playback_t *player; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1109 | pthread_condattr_t cattr; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1110 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1111 | player = (DVR_Playback_t*)calloc(1, sizeof(DVR_Playback_t)); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1112 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1113 | pthread_mutex_init(&player->lock, NULL); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1114 | pthread_mutex_init(&player->segment_lock, NULL); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1115 | pthread_condattr_init(&cattr); |
| 1116 | pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC); |
| 1117 | pthread_cond_init(&player->cond, &cattr); |
| 1118 | pthread_condattr_destroy(&cattr); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1119 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1120 | //init segment list head |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1121 | INIT_LIST_HEAD(&player->segment_list); |
| 1122 | player->cmd.last_cmd = DVR_PLAYBACK_CMD_STOP; |
| 1123 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1124 | player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1125 | player->cmd.state = DVR_PLAYBACK_STATE_STOP; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1126 | player->state = DVR_PLAYBACK_STATE_STOP; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1127 | player->cmd.pos = 0; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1128 | player->speed = 1.0f; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1129 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1130 | //store open params |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1131 | player->openParams.dmx_dev_id = params->dmx_dev_id; |
| 1132 | player->openParams.block_size = params->block_size; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1133 | player->openParams.is_timeshift = params->is_timeshift; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1134 | player->openParams.event_fn = params->event_fn; |
| 1135 | player->openParams.event_userdata = params->event_userdata; |
| 1136 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1137 | player->has_pids = params->has_pids; |
| 1138 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1139 | player->handle = params->player_handle ; |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 1140 | |
| 1141 | AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata); |
| 1142 | //for test get callback |
| 1143 | if (0 && player->player_callback_func == NULL) { |
| 1144 | AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback_test, player); |
| 1145 | AmTsPlayer_getCb(player->handle, &player->player_callback_func, &player->player_callback_userdata); |
| 1146 | DVR_DEBUG(1, "playback open get callback[%p][%p][%p][%p]",player->player_callback_func, player->player_callback_userdata, _dvr_tsplayer_callback_test,player); |
| 1147 | } |
| 1148 | AmTsPlayer_registerCb(player->handle, _dvr_tsplayer_callback, player); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1149 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1150 | //init has audio and video |
| 1151 | player->has_video = DVR_FALSE; |
| 1152 | player->has_audio = DVR_FALSE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1153 | player->cur_segment_id = UINT64_MAX; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1154 | player->last_segment_id = 0LL; |
| 1155 | player->segment_is_open = DVR_FALSE; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1156 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1157 | //init ff fb time |
| 1158 | player->fffb_current = -1; |
| 1159 | player->fffb_start =-1; |
| 1160 | player->fffb_start_pcr = -1; |
| 1161 | //seek time |
| 1162 | player->seek_time = 0; |
hualing chen | 6e4bfa5 | 2020-03-13 14:37:11 +0800 | [diff] [blame] | 1163 | player->send_time = 0; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1164 | |
| 1165 | //init secure stuff |
| 1166 | player->dec_func = NULL; |
| 1167 | player->dec_userdata = NULL; |
| 1168 | player->is_secure_mode = 0; |
| 1169 | player->secure_buffer = NULL; |
| 1170 | player->secure_buffer_size = 0; |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1171 | player->drop_ts = DVR_FALSE; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1172 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1173 | *p_handle = player; |
| 1174 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /**\brief Close an dvr palyback |
| 1178 | * \param[in] handle playback handle |
| 1179 | * \retval DVR_SUCCESS On success |
| 1180 | * \return Error code |
| 1181 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1182 | int dvr_playback_close(DVR_PlaybackHandle_t handle) { |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1183 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1184 | DVR_ASSERT(handle); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1185 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1186 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1187 | DVR_DEBUG(1, "func: %s, will resume", __func__); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1188 | |
| 1189 | if (player == NULL) { |
| 1190 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1191 | return DVR_FAILURE; |
| 1192 | } |
| 1193 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1194 | if (player->state != DVR_PLAYBACK_STATE_STOP) |
| 1195 | { |
| 1196 | dvr_playback_stop(handle, DVR_TRUE); |
| 1197 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1198 | //AmTsPlayer_resumeVideoDecoding(player->handle); |
| 1199 | //AmTsPlayer_resumeAudioDecoding(player->handle); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1200 | pthread_mutex_destroy(&player->lock); |
| 1201 | pthread_cond_destroy(&player->cond); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1202 | |
| 1203 | if (player) { |
| 1204 | free(player); |
| 1205 | player = NULL; |
| 1206 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1207 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1208 | } |
| 1209 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1210 | /**\brief Start play audio and video, used start auido api and start video api |
| 1211 | * \param[in] handle playback handle |
| 1212 | * \param[in] params audio playback params,contains fmt and pid... |
| 1213 | * \retval DVR_SUCCESS On success |
| 1214 | * \return Error code |
| 1215 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1216 | int dvr_playback_start(DVR_PlaybackHandle_t handle, DVR_PlaybackFlag_t flag) { |
| 1217 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1218 | am_tsplayer_video_params vparams; |
| 1219 | am_tsplayer_audio_params aparams; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1220 | |
| 1221 | if (player == NULL) { |
| 1222 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1223 | return DVR_FAILURE; |
| 1224 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1225 | uint64_t segment_id = player->cur_segment_id; |
| 1226 | DVR_DEBUG(1, "[%s][%p]segment_id:[%lld]",__func__, handle, segment_id); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1227 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1228 | player->first_frame = 0; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1229 | //can used start api to resume playback |
| 1230 | if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) { |
| 1231 | return dvr_playback_resume(handle); |
| 1232 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1233 | if (player->cmd.state == DVR_PLAYBACK_STATE_START) { |
| 1234 | DVR_DEBUG(1, "stat is start, not need into start play"); |
| 1235 | return DVR_SUCCESS; |
| 1236 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1237 | player->play_flag = flag; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1238 | //get segment info and audio video pid fmt ; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1239 | DVR_DEBUG(1, "lock func: %s %p flag:0x%x", __func__, handle, flag); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1240 | pthread_mutex_lock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1241 | _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1242 | //start audio and video |
| 1243 | if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) { |
| 1244 | //audio abnd video pis is all invalid, return error. |
| 1245 | DVR_DEBUG(0, "dvr play back start error, not found audio and video info"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1246 | DVR_DEBUG(1, "unlock func: %s", __func__); |
| 1247 | pthread_mutex_unlock(&player->lock); |
| 1248 | DVR_Play_Notify_t notify; |
| 1249 | memset(¬ify, 0 , sizeof(DVR_Play_Notify_t)); |
| 1250 | notify.event = DVR_PLAYBACK_EVENT_TRANSITION_FAILED; |
| 1251 | notify.info.error_reason = DVR_PLAYBACK_PID_ERROR; |
| 1252 | notify.info.transition_failed_data.segment_id = segment_id; |
| 1253 | //get play statue not here |
| 1254 | _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_TRANSITION_FAILED, ¬ify); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1255 | return -1; |
| 1256 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1257 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1258 | { |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1259 | if (VALID_PID(vparams.pid)) { |
| 1260 | player->has_video = DVR_TRUE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1261 | //if set flag is pause live, we need set trick mode |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1262 | if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) { |
| 1263 | DVR_DEBUG(1, "set trick mode -pauselive flag--"); |
| 1264 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT); |
| 1265 | } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1266 | || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) { |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1267 | DVR_DEBUG(1, "set trick mode -fffb--at pause live"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1268 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1269 | } else { |
| 1270 | DVR_DEBUG(1, "set trick mode ---none"); |
| 1271 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1272 | } |
| 1273 | AmTsPlayer_setVideoParams(player->handle, &vparams); |
| 1274 | AmTsPlayer_startVideoDecoding(player->handle); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1275 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1276 | |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 1277 | DVR_DEBUG(1, "player->cmd.cur_cmd:%d vpid[0x%x]apis[0x%x]", player->cmd.cur_cmd, vparams.pid, aparams.pid); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1278 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB |
| 1279 | || player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF) { |
| 1280 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 1281 | player->state = DVR_PLAYBACK_STATE_START; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1282 | } else { |
| 1283 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 1284 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1285 | if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) { |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1286 | //set fast play |
| 1287 | DVR_DEBUG(1, "dvr start fast mode"); |
| 1288 | AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/100.0f); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1289 | } else { |
| 1290 | if (VALID_PID(aparams.pid)) { |
| 1291 | DVR_DEBUG(1, "%s :start audio---", __func__); |
| 1292 | player->has_audio = DVR_TRUE; |
| 1293 | AmTsPlayer_setAudioParams(player->handle, &aparams); |
| 1294 | AmTsPlayer_startAudioDecoding(player->handle); |
| 1295 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1296 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1297 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 1298 | player->state = DVR_PLAYBACK_STATE_START; |
| 1299 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1300 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1301 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1302 | pthread_mutex_unlock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1303 | _start_playback_thread(handle); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1304 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1305 | } |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1306 | /**\brief dvr play back add segment info to segment list |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1307 | * \param[in] handle playback handle |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1308 | * \param[in] info added segment info,con vpid fmt apid fmt..... |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1309 | * \retval DVR_SUCCESS On success |
| 1310 | * \return Error code |
| 1311 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1312 | int dvr_playback_add_segment(DVR_PlaybackHandle_t handle, DVR_PlaybackSegmentInfo_t *info) { |
| 1313 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1314 | DVR_DEBUG(1, "[%s][%p]",__func__, handle); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1315 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1316 | if (player == NULL) { |
| 1317 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1318 | return DVR_FAILURE; |
| 1319 | } |
| 1320 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1321 | DVR_DEBUG(1, "add segment id: %lld %p", info->segment_id, handle); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1322 | DVR_PlaybackSegmentInfo_t *segment; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1323 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1324 | segment = malloc(sizeof(DVR_PlaybackSegmentInfo_t)); |
| 1325 | memset(segment, 0, sizeof(DVR_PlaybackSegmentInfo_t)); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1326 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1327 | //not memcpy chun info. |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1328 | segment->segment_id = info->segment_id; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1329 | //cp location |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1330 | memcpy(segment->location, info->location, DVR_MAX_LOCATION_SIZE); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1331 | |
| 1332 | DVR_DEBUG(1, "add location [%s]id[%lld]flag[%x]", segment->location, segment->segment_id, info->flags); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1333 | segment->flags = info->flags; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1334 | |
| 1335 | //pids |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1336 | segment->pids.video.pid = info->pids.video.pid; |
| 1337 | segment->pids.video.format = info->pids.video.format; |
| 1338 | segment->pids.video.type = info->pids.video.type; |
| 1339 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1340 | segment->pids.audio.pid = info->pids.audio.pid; |
| 1341 | segment->pids.audio.format = info->pids.audio.format; |
| 1342 | segment->pids.audio.type = info->pids.audio.type; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1343 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1344 | segment->pids.ad.pid = info->pids.ad.pid; |
| 1345 | segment->pids.ad.format = info->pids.ad.format; |
| 1346 | segment->pids.ad.type = info->pids.ad.type; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1347 | |
| 1348 | segment->pids.pcr.pid = info->pids.pcr.pid; |
| 1349 | |
| 1350 | DVR_DEBUG(1, "lock func: %s pid [0x%x][0x%x][0x%x][0x%x]", __func__,segment->pids.video.pid,segment->pids.audio.pid, info->pids.video.pid,info->pids.audio.pid); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1351 | pthread_mutex_lock(&player->lock); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1352 | list_add_tail(&segment->head, &player->segment_list); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1353 | pthread_mutex_unlock(&player->lock); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1354 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1355 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1356 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1357 | } |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1358 | /**\brief dvr play back remove segment info by segment_id |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1359 | * \param[in] handle playback handle |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1360 | * \param[in] segment_id need removed segment id |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1361 | * \retval DVR_SUCCESS On success |
| 1362 | * \return Error code |
| 1363 | */ |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1364 | int dvr_playback_remove_segment(DVR_PlaybackHandle_t handle, uint64_t segment_id) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1365 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1366 | DVR_DEBUG(1, "remove segment id: %lld", segment_id); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1367 | |
| 1368 | if (player == NULL) { |
| 1369 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1370 | return DVR_FAILURE; |
| 1371 | } |
| 1372 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1373 | if (segment_id == player->cur_segment_id) { |
| 1374 | DVR_DEBUG(1, "not suport remove curren segment id: %lld", segment_id); |
| 1375 | return DVR_FAILURE; |
| 1376 | } |
| 1377 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1378 | pthread_mutex_lock(&player->lock); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1379 | DVR_PlaybackSegmentInfo_t *segment = NULL; |
| 1380 | DVR_PlaybackSegmentInfo_t *segment_tmp = NULL; |
| 1381 | list_for_each_entry_safe(segment, segment_tmp, &player->segment_list, head) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1382 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1383 | if (segment->segment_id == segment_id) { |
| 1384 | list_del(&segment->head); |
| 1385 | free(segment); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1386 | break; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1387 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1388 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1389 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1390 | pthread_mutex_unlock(&player->lock); |
| 1391 | |
| 1392 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1393 | } |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1394 | /**\brief dvr play back add segment info |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1395 | * \param[in] handle playback handle |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1396 | * \param[in] info added segment info,con vpid fmt apid fmt..... |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1397 | * \retval DVR_SUCCESS On success |
| 1398 | * \return Error code |
| 1399 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1400 | int dvr_playback_update_segment_flags(DVR_PlaybackHandle_t handle, |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1401 | uint64_t segment_id, DVR_PlaybackSegmentFlag_t flags) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1402 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1403 | DVR_DEBUG(1, "update segment id: %lld flag:%d", segment_id, flags); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1404 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1405 | if (player == NULL) { |
| 1406 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1407 | return DVR_FAILURE; |
| 1408 | } |
| 1409 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1410 | DVR_PlaybackSegmentInfo_t *segment; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1411 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1412 | pthread_mutex_lock(&player->lock); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1413 | list_for_each_entry(segment, &player->segment_list, head) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1414 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1415 | if (segment->segment_id != segment_id) { |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1416 | continue; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1417 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1418 | // if encramble to free, only set flag and return; |
| 1419 | |
| 1420 | //if displayable to none, we need mute audio and video |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1421 | if (segment_id == player->cur_segment_id) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1422 | if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE |
| 1423 | && (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1424 | //disable display, mute |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1425 | AmTsPlayer_hideVideo(player->handle); |
| 1426 | AmTsPlayer_setAudioMute(player->handle, 1, 1); |
| 1427 | //playback_device_mute_audio(player->handle, 1); |
| 1428 | //playback_device_mute_video(player->handle, 1); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1429 | } else if ((segment->flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == 0 && |
| 1430 | (flags & DVR_PLAYBACK_SEGMENT_DISPLAYABLE) == DVR_PLAYBACK_SEGMENT_DISPLAYABLE) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1431 | //enable display, unmute |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1432 | AmTsPlayer_showVideo(player->handle); |
| 1433 | AmTsPlayer_setAudioMute(player->handle, 0, 0); |
| 1434 | //playback_device_mute_audio(player->handle, 0); |
| 1435 | //playback_device_mute_video(player->handle, 0); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1436 | } else { |
| 1437 | //do nothing |
| 1438 | } |
| 1439 | } else { |
| 1440 | //do nothing |
| 1441 | } |
| 1442 | //continue , only set flag |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1443 | segment->flags = flags; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1444 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1445 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1446 | pthread_mutex_unlock(&player->lock); |
| 1447 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1451 | static int _do_check_pid_info(DVR_PlaybackHandle_t handle, DVR_StreamInfo_t now_pid, DVR_StreamInfo_t set_pid, int type) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1452 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1453 | DVR_DEBUG(1, "[%s][%p]",__func__, handle); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1454 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1455 | if (player == NULL) { |
| 1456 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1457 | return DVR_FAILURE; |
| 1458 | } |
| 1459 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1460 | if (now_pid.pid == set_pid.pid) { |
| 1461 | //do nothing |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1462 | return 0; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1463 | } else if (player->cmd.state == DVR_PLAYBACK_STATE_START) { |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1464 | if (VALID_PID(now_pid.pid)) { |
| 1465 | //stop now stream |
| 1466 | if (type == 0) { |
| 1467 | //stop vieo |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1468 | DVR_DEBUG(1, "[%s]stop video",__func__); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1469 | AmTsPlayer_stopVideoDecoding(player->handle); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1470 | player->has_video = DVR_FALSE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1471 | } else if (type == 1) { |
| 1472 | //stop audio |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1473 | DVR_DEBUG(1, "[%s]stop audio",__func__); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1474 | AmTsPlayer_stopAudioDecoding(player->handle); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1475 | player->has_audio = DVR_FALSE; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1476 | } else if (type == 2) { |
| 1477 | //stop sub audio |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1478 | DVR_DEBUG(1, "[%s]stop ad",__func__); |
| 1479 | AmTsPlayer_disableADMix(player->handle); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1480 | } else if (type == 3) { |
| 1481 | //pcr |
| 1482 | } |
| 1483 | } |
| 1484 | if (VALID_PID(set_pid.pid)) { |
| 1485 | //start |
| 1486 | if (type == 0) { |
| 1487 | //start vieo |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1488 | am_tsplayer_video_params vparams; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1489 | vparams.pid = set_pid.pid; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1490 | vparams.codectype = _dvr_convert_stream_fmt(set_pid.format, DVR_FALSE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1491 | player->has_video = DVR_TRUE; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1492 | DVR_DEBUG(1, "[%s]start video pid[%d]fmt[%d]",__func__,vparams.pid, vparams.codectype); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1493 | AmTsPlayer_setVideoParams(player->handle, &vparams); |
| 1494 | AmTsPlayer_startVideoDecoding(player->handle); |
| 1495 | //playback_device_video_start(player->handle,&vparams); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1496 | } else if (type == 1) { |
| 1497 | //start audio |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1498 | am_tsplayer_audio_params aparams; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1499 | aparams.pid = set_pid.pid; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1500 | aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1501 | player->has_audio = DVR_TRUE; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1502 | DVR_DEBUG(1, "[%s]start audio pid[%d]fmt[%d]",__func__,aparams.pid, aparams.codectype); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1503 | AmTsPlayer_setAudioParams(player->handle, &aparams); |
| 1504 | AmTsPlayer_startAudioDecoding(player->handle); |
| 1505 | //playback_device_audio_start(player->handle,&aparams); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1506 | } else if (type == 2) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1507 | am_tsplayer_audio_params aparams; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1508 | aparams.pid = set_pid.pid; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1509 | aparams.codectype= _dvr_convert_stream_fmt(set_pid.format, DVR_TRUE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1510 | player->has_audio = DVR_TRUE; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1511 | DVR_DEBUG(1, "[%s]start ad audio pid[%d]fmt[%d]",__func__,aparams.pid, aparams.codectype); |
| 1512 | AmTsPlayer_setADParams(player->handle, &aparams); |
| 1513 | AmTsPlayer_enableADMix(player->handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1514 | //playback_device_audio_start(player->handle,&aparams); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1515 | } else if (type == 3) { |
| 1516 | //pcr |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1517 | DVR_DEBUG(1, "[%s]start set pcr [%d]",__func__, set_pid.pid); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1518 | AmTsPlayer_setPcrPid(player->handle, set_pid.pid); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1519 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1520 | //audio and video all close |
| 1521 | if (!player->has_audio && !player->has_video) { |
| 1522 | player->state = DVR_PLAYBACK_STATE_STOP; |
| 1523 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | return 0; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1527 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1528 | /**\brief dvr play back update segment pids |
| 1529 | * if updated segment is ongoing segment, we need start new |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1530 | * add pid stream and stop remove pid stream. |
| 1531 | * \param[in] handle playback handle |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1532 | * \param[in] segment_id need updated pids segment id |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1533 | * \retval DVR_SUCCESS On success |
| 1534 | * \return Error code |
| 1535 | */ |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1536 | int dvr_playback_update_segment_pids(DVR_PlaybackHandle_t handle, uint64_t segment_id, DVR_PlaybackPids_t *p_pids) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1537 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1538 | DVR_DEBUG(1, "update segment id: %lld", segment_id); |
| 1539 | DVR_DEBUG(1, "[%s][%p]",__func__, handle); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1540 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1541 | if (player == NULL) { |
| 1542 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1543 | return DVR_FAILURE; |
| 1544 | } |
| 1545 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1546 | DVR_PlaybackSegmentInfo_t *segment; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1547 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1548 | pthread_mutex_lock(&player->lock); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1549 | DVR_DEBUG(1, "get lock [%p] update segment id: %lld cur id %lld",handle, segment_id, player->cur_segment_id); |
| 1550 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1551 | list_for_each_entry(segment, &player->segment_list, head) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1552 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1553 | if (segment->segment_id == segment_id) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1554 | |
| 1555 | if (player->cur_segment_id == segment_id) { |
| 1556 | if (player->cmd.state == DVR_PLAYBACK_STATE_FF |
| 1557 | || player->cmd.state == DVR_PLAYBACK_STATE_FF) { |
| 1558 | //do nothing when ff fb |
| 1559 | DVR_DEBUG(1, "now is ff fb, not to update cur segment info\r\n"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1560 | DVR_DEBUG(1, "unlock func: %s", __func__); |
| 1561 | pthread_mutex_unlock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1562 | return 0; |
| 1563 | } |
| 1564 | |
| 1565 | //if segment is on going segment,we need stop start stream |
| 1566 | if (player->cmd.state == DVR_PLAYBACK_STATE_START) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1567 | pthread_mutex_unlock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1568 | //check video pids, stop or restart |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1569 | _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.video, p_pids->video, 0); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1570 | //check audio pids stop or restart |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1571 | _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.audio, p_pids->audio, 1); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1572 | //check sub audio pids stop or restart |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1573 | _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.ad, p_pids->ad, 2); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1574 | //check pcr pids stop or restart |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1575 | _do_check_pid_info((DVR_PlaybackHandle_t)player, segment->pids.pcr, p_pids->pcr, 3); |
| 1576 | pthread_mutex_lock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1577 | } else if (player->cmd.state == DVR_PLAYBACK_STATE_PAUSE) { |
| 1578 | //if state is pause, we need process at resume api. we only record change info |
| 1579 | int v_cmd = DVR_PLAYBACK_CMD_NONE; |
| 1580 | int a_cmd = DVR_PLAYBACK_CMD_NONE; |
| 1581 | if (VALID_PID(segment->pids.video.pid) |
| 1582 | && VALID_PID(p_pids->video.pid) |
| 1583 | && segment->pids.video.pid != p_pids->video.pid) { |
| 1584 | //restart video |
| 1585 | v_cmd = DVR_PLAYBACK_CMD_VRESTART; |
| 1586 | } |
| 1587 | if (!VALID_PID(segment->pids.video.pid) |
| 1588 | && VALID_PID(p_pids->video.pid) |
| 1589 | && segment->pids.video.pid != p_pids->video.pid) { |
| 1590 | //start video |
| 1591 | v_cmd = DVR_PLAYBACK_CMD_VSTART; |
| 1592 | } |
| 1593 | if (VALID_PID(segment->pids.video.pid) |
| 1594 | && !VALID_PID(p_pids->video.pid) |
| 1595 | && segment->pids.video.pid != p_pids->video.pid) { |
| 1596 | //stop video |
| 1597 | v_cmd = DVR_PLAYBACK_CMD_VSTOP; |
| 1598 | } |
| 1599 | if (VALID_PID(segment->pids.audio.pid) |
| 1600 | && VALID_PID(p_pids->audio.pid) |
| 1601 | && segment->pids.audio.pid != p_pids->audio.pid) { |
| 1602 | //restart audio |
| 1603 | a_cmd = DVR_PLAYBACK_CMD_ARESTART; |
| 1604 | } |
| 1605 | if (!VALID_PID(segment->pids.audio.pid) |
| 1606 | && VALID_PID(p_pids->audio.pid) |
| 1607 | && segment->pids.audio.pid != p_pids->audio.pid) { |
| 1608 | //start audio |
| 1609 | a_cmd = DVR_PLAYBACK_CMD_ASTART; |
| 1610 | } |
| 1611 | if (VALID_PID(segment->pids.audio.pid) |
| 1612 | && !VALID_PID(p_pids->audio.pid) |
| 1613 | && segment->pids.audio.pid != p_pids->audio.pid) { |
| 1614 | //stop audio |
| 1615 | a_cmd = DVR_PLAYBACK_CMD_ASTOP; |
| 1616 | } |
| 1617 | if (a_cmd == DVR_PLAYBACK_CMD_NONE |
| 1618 | && v_cmd == DVR_PLAYBACK_CMD_NONE) { |
| 1619 | //do nothing |
| 1620 | } else if (a_cmd == DVR_PLAYBACK_CMD_NONE |
| 1621 | || v_cmd == DVR_PLAYBACK_CMD_NONE) { |
| 1622 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1623 | player->cmd.cur_cmd = a_cmd != DVR_PLAYBACK_CMD_NONE ? a_cmd : v_cmd; |
| 1624 | } else if (a_cmd != DVR_PLAYBACK_CMD_NONE |
| 1625 | && v_cmd != DVR_PLAYBACK_CMD_NONE) { |
| 1626 | if (v_cmd == DVR_PLAYBACK_CMD_VRESTART |
| 1627 | && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) { |
| 1628 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1629 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART; |
| 1630 | }else if (v_cmd == DVR_PLAYBACK_CMD_VRESTART |
| 1631 | && a_cmd == DVR_PLAYBACK_CMD_ASTART) { |
| 1632 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1633 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTARTVRESTART; |
| 1634 | } else { |
| 1635 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1636 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVRESTART; |
| 1637 | } |
| 1638 | |
| 1639 | if (v_cmd == DVR_PLAYBACK_CMD_VSTART |
| 1640 | && (a_cmd == DVR_PLAYBACK_CMD_ARESTART)) { |
| 1641 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1642 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTARTARESTART; |
| 1643 | } else if (v_cmd == DVR_PLAYBACK_CMD_VSTART |
| 1644 | && a_cmd == DVR_PLAYBACK_CMD_ASTART) { |
| 1645 | //not occur this case |
| 1646 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1647 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START; |
| 1648 | } else { |
| 1649 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1650 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOPVSTART; |
| 1651 | } |
| 1652 | |
| 1653 | if (v_cmd == DVR_PLAYBACK_CMD_VSTOP |
| 1654 | && a_cmd == DVR_PLAYBACK_CMD_ASTART) { |
| 1655 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1656 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPASTART; |
| 1657 | } else if (v_cmd == DVR_PLAYBACK_CMD_VSTOP |
| 1658 | && a_cmd == DVR_PLAYBACK_CMD_ARESTART) { |
| 1659 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1660 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOPARESTART; |
| 1661 | } else { |
| 1662 | //not occur this case |
| 1663 | player->cmd.last_cmd =DVR_PLAYBACK_CMD_PAUSE; |
| 1664 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP; |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1669 | //save pids info |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1670 | memcpy(&segment->pids, p_pids, sizeof(DVR_PlaybackPids_t)); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1671 | break; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1672 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1673 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1674 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1675 | pthread_mutex_unlock(&player->lock); |
| 1676 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1677 | } |
| 1678 | /**\brief Stop play, will stop video and audio |
| 1679 | * \param[in] handle playback handle |
| 1680 | * \param[in] clear is clear last frame |
| 1681 | * \retval DVR_SUCCESS On success |
| 1682 | * \return Error code |
| 1683 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1684 | int dvr_playback_stop(DVR_PlaybackHandle_t handle, DVR_Bool_t clear) { |
| 1685 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1686 | |
| 1687 | if (player == NULL) { |
| 1688 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1689 | return DVR_FAILURE; |
| 1690 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1691 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1692 | _stop_playback_thread(handle); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1693 | pthread_mutex_lock(&player->lock); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 1694 | AmTsPlayer_stopFast(player->handle); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1695 | if (player->has_video) { |
| 1696 | AmTsPlayer_resumeVideoDecoding(player->handle); |
| 1697 | } |
| 1698 | if (player->has_audio) { |
| 1699 | AmTsPlayer_resumeAudioDecoding(player->handle); |
| 1700 | } |
| 1701 | if (player->has_video) { |
| 1702 | player->has_video = DVR_FALSE; |
| 1703 | AmTsPlayer_showVideo(player->handle); |
| 1704 | AmTsPlayer_stopVideoDecoding(player->handle); |
| 1705 | } |
| 1706 | if (player->has_audio) { |
| 1707 | player->has_audio = DVR_FALSE; |
| 1708 | AmTsPlayer_stopAudioDecoding(player->handle); |
| 1709 | } |
| 1710 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1711 | player->cmd.last_cmd = player->cmd.cur_cmd; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1712 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_STOP; |
| 1713 | player->cmd.state = DVR_PLAYBACK_STATE_STOP; |
| 1714 | player->state = DVR_PLAYBACK_STATE_STOP; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1715 | player->cur_segment_id = UINT64_MAX; |
| 1716 | player->segment_is_open = DVR_FALSE; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1717 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1718 | pthread_mutex_unlock(&player->lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1719 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1720 | } |
| 1721 | /**\brief Start play audio |
| 1722 | * \param[in] handle playback handle |
| 1723 | * \param[in] params audio playback params,contains fmt and pid... |
| 1724 | * \retval DVR_SUCCESS On success |
| 1725 | * \return Error code |
| 1726 | */ |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1727 | |
| 1728 | int dvr_playback_audio_start(DVR_PlaybackHandle_t handle, am_tsplayer_audio_params *param) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1729 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1730 | |
| 1731 | if (player == NULL) { |
| 1732 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1733 | return DVR_FAILURE; |
| 1734 | } |
| 1735 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1736 | DVR_DEBUG(1, "[%s][%p]",__func__, handle); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1737 | _start_playback_thread(handle); |
| 1738 | //start audio and video |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1739 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1740 | pthread_mutex_lock(&player->lock); |
| 1741 | player->has_audio = DVR_TRUE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1742 | AmTsPlayer_setAudioParams(player->handle, param); |
| 1743 | AmTsPlayer_startAudioDecoding(player->handle); |
| 1744 | //playback_device_audio_start(player->handle , param); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1745 | player->cmd.last_cmd = player->cmd.cur_cmd; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1746 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTART; |
| 1747 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 1748 | player->state = DVR_PLAYBACK_STATE_START; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1749 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1750 | pthread_mutex_unlock(&player->lock); |
| 1751 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1752 | } |
| 1753 | /**\brief Stop play audio |
| 1754 | * \param[in] handle playback handle |
| 1755 | * \retval DVR_SUCCESS On success |
| 1756 | * \return Error code |
| 1757 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1758 | int dvr_playback_audio_stop(DVR_PlaybackHandle_t handle) { |
| 1759 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1760 | |
| 1761 | if (player == NULL) { |
| 1762 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1763 | return DVR_FAILURE; |
| 1764 | } |
| 1765 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1766 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1767 | pthread_mutex_lock(&player->lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1768 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1769 | //playback_device_audio_stop(player->handle); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1770 | if (player->has_video == DVR_FALSE) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1771 | player->cmd.state = DVR_PLAYBACK_STATE_STOP; |
| 1772 | player->state = DVR_PLAYBACK_STATE_STOP; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1773 | //destory thread |
| 1774 | _stop_playback_thread(handle); |
| 1775 | } else { |
| 1776 | //do nothing.video is playing |
| 1777 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1778 | player->has_audio = DVR_FALSE; |
| 1779 | |
| 1780 | AmTsPlayer_stopAudioDecoding(player->handle); |
| 1781 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 1782 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_ASTOP; |
| 1783 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1784 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1785 | pthread_mutex_unlock(&player->lock); |
| 1786 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1787 | } |
| 1788 | /**\brief Start play video |
| 1789 | * \param[in] handle playback handle |
| 1790 | * \param[in] params video playback params,contains fmt and pid... |
| 1791 | * \retval DVR_SUCCESS On success |
| 1792 | * \return Error code |
| 1793 | */ |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1794 | int dvr_playback_video_start(DVR_PlaybackHandle_t handle, am_tsplayer_video_params *param) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1795 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1796 | |
| 1797 | if (player == NULL) { |
| 1798 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1799 | return DVR_FAILURE; |
| 1800 | } |
| 1801 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1802 | _start_playback_thread(handle); |
| 1803 | //start audio and video |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1804 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1805 | pthread_mutex_lock(&player->lock); |
| 1806 | player->has_video = DVR_TRUE; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1807 | AmTsPlayer_setVideoParams(player->handle, param); |
| 1808 | AmTsPlayer_startVideoDecoding(player->handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1809 | |
| 1810 | //playback_device_video_start(player->handle , param); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1811 | //if set flag is pause live, we need set trick mode |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1812 | if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1813 | DVR_DEBUG(1, "settrick mode at video start"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1814 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT); |
| 1815 | //playback_device_trick_mode(player->handle, 1); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1816 | } |
| 1817 | player->cmd.last_cmd = player->cmd.cur_cmd; |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1818 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTART; |
| 1819 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 1820 | player->state = DVR_PLAYBACK_STATE_START; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1821 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1822 | pthread_mutex_unlock(&player->lock); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1823 | return DVR_SUCCESS; |
| 1824 | } |
| 1825 | /**\brief Stop play video |
| 1826 | * \param[in] handle playback handle |
| 1827 | * \retval DVR_SUCCESS On success |
| 1828 | * \return Error code |
| 1829 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1830 | int dvr_playback_video_stop(DVR_PlaybackHandle_t handle) { |
| 1831 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1832 | |
| 1833 | if (player == NULL) { |
| 1834 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1835 | return DVR_FAILURE; |
| 1836 | } |
| 1837 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1838 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1839 | pthread_mutex_lock(&player->lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1840 | |
| 1841 | if (player->has_audio == DVR_FALSE) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1842 | player->cmd.state = DVR_PLAYBACK_STATE_STOP; |
| 1843 | player->state = DVR_PLAYBACK_STATE_STOP; |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1844 | //destory thread |
| 1845 | _stop_playback_thread(handle); |
| 1846 | } else { |
| 1847 | //do nothing.audio is playing |
| 1848 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1849 | player->has_video = DVR_FALSE; |
| 1850 | |
| 1851 | AmTsPlayer_stopVideoDecoding(player->handle); |
| 1852 | //playback_device_video_stop(player->handle); |
| 1853 | |
| 1854 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 1855 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_VSTOP; |
| 1856 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1857 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1858 | pthread_mutex_unlock(&player->lock); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1859 | return DVR_SUCCESS; |
| 1860 | } |
| 1861 | /**\brief Pause play |
| 1862 | * \param[in] handle playback handle |
| 1863 | * \param[in] flush whether its internal buffers should be flushed |
| 1864 | * \retval DVR_SUCCESS On success |
| 1865 | * \return Error code |
| 1866 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 1867 | int dvr_playback_pause(DVR_PlaybackHandle_t handle, DVR_Bool_t flush) { |
| 1868 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1869 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1870 | |
| 1871 | if (player == NULL) { |
| 1872 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1873 | return DVR_FAILURE; |
| 1874 | } |
| 1875 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1876 | pthread_mutex_lock(&player->lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1877 | DVR_DEBUG(1, "get lock func: %s", __func__); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1878 | if (player->has_video) |
| 1879 | AmTsPlayer_pauseVideoDecoding(player->handle); |
| 1880 | if (player->has_video) |
| 1881 | AmTsPlayer_pauseAudioDecoding(player->handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1882 | |
| 1883 | //playback_device_pause(player->handle); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1884 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 1885 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) { |
| 1886 | player->cmd.state = DVR_PLAYBACK_STATE_PAUSE; |
| 1887 | player->state = DVR_PLAYBACK_STATE_PAUSE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1888 | } else { |
| 1889 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 1890 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_PAUSE; |
| 1891 | player->cmd.state = DVR_PLAYBACK_STATE_PAUSE; |
| 1892 | player->state = DVR_PLAYBACK_STATE_PAUSE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1893 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1894 | pthread_mutex_unlock(&player->lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1895 | DVR_DEBUG(1, "unlock func: %s", __func__); |
| 1896 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 1897 | return DVR_SUCCESS; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1898 | } |
| 1899 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1900 | //not add lock |
| 1901 | static int _dvr_cmd(DVR_PlaybackHandle_t handle, int cmd) |
| 1902 | { |
| 1903 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 1904 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1905 | if (player == NULL) { |
| 1906 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1907 | return DVR_FAILURE; |
| 1908 | } |
| 1909 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1910 | //get video params and audio params |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1911 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1912 | pthread_mutex_lock(&player->lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1913 | am_tsplayer_video_params vparams; |
| 1914 | am_tsplayer_audio_params aparams; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 1915 | uint64_t segmentid = player->cur_segment_id; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1916 | |
| 1917 | _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1918 | DVR_DEBUG(1, "unlock func: %s cmd: %d", __func__, cmd); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1919 | pthread_mutex_unlock(&player->lock); |
| 1920 | |
| 1921 | switch (cmd) { |
| 1922 | case DVR_PLAYBACK_CMD_AVRESTART: |
| 1923 | //av restart |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 1924 | DVR_DEBUG(1, "do_cmd avrestart"); |
| 1925 | _dvr_playback_replay((DVR_PlaybackHandle_t)player, DVR_FALSE); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1926 | break; |
| 1927 | case DVR_PLAYBACK_CMD_VRESTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1928 | dvr_playback_video_stop((DVR_PlaybackHandle_t)player); |
| 1929 | dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1930 | break; |
| 1931 | case DVR_PLAYBACK_CMD_VSTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1932 | dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1933 | break; |
| 1934 | case DVR_PLAYBACK_CMD_VSTOP: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1935 | dvr_playback_video_stop((DVR_PlaybackHandle_t)player); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1936 | break; |
| 1937 | case DVR_PLAYBACK_CMD_ARESTART: |
| 1938 | //a restart |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1939 | dvr_playback_audio_stop((DVR_PlaybackHandle_t)player); |
| 1940 | dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1941 | break; |
| 1942 | case DVR_PLAYBACK_CMD_ASTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1943 | dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1944 | break; |
| 1945 | case DVR_PLAYBACK_CMD_ASTOP: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1946 | dvr_playback_audio_stop((DVR_PlaybackHandle_t)player); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1947 | break; |
| 1948 | case DVR_PLAYBACK_CMD_ASTOPVRESTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1949 | dvr_playback_audio_stop((DVR_PlaybackHandle_t)player); |
| 1950 | dvr_playback_video_stop((DVR_PlaybackHandle_t)player); |
| 1951 | dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1952 | break; |
| 1953 | case DVR_PLAYBACK_CMD_ASTOPVSTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1954 | dvr_playback_audio_stop((DVR_PlaybackHandle_t)player); |
| 1955 | dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1956 | break; |
| 1957 | case DVR_PLAYBACK_CMD_VSTOPARESTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1958 | dvr_playback_video_stop((DVR_PlaybackHandle_t)player); |
| 1959 | dvr_playback_audio_stop((DVR_PlaybackHandle_t)player); |
| 1960 | dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1961 | break; |
| 1962 | case DVR_PLAYBACK_CMD_STOP: |
| 1963 | break; |
| 1964 | case DVR_PLAYBACK_CMD_START: |
| 1965 | break; |
| 1966 | case DVR_PLAYBACK_CMD_ASTARTVRESTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1967 | dvr_playback_video_stop((DVR_PlaybackHandle_t)player); |
| 1968 | dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams); |
| 1969 | dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1970 | break; |
| 1971 | case DVR_PLAYBACK_CMD_VSTARTARESTART: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1972 | dvr_playback_audio_stop((DVR_PlaybackHandle_t)player); |
| 1973 | dvr_playback_video_start((DVR_PlaybackHandle_t)player, &vparams); |
| 1974 | dvr_playback_audio_start((DVR_PlaybackHandle_t)player, &aparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1975 | break; |
| 1976 | case DVR_PLAYBACK_CMD_FF: |
| 1977 | case DVR_PLAYBACK_CMD_FB: |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 1978 | _dvr_playback_fffb((DVR_PlaybackHandle_t)player); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1979 | break; |
| 1980 | default: |
| 1981 | break; |
| 1982 | } |
| 1983 | return DVR_SUCCESS; |
| 1984 | } |
| 1985 | |
| 1986 | /**\brief Resume play |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1987 | * \param[in] handle playback handle |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1988 | * \retval DVR_SUCCESS On success |
| 1989 | * \return Error code |
| 1990 | */ |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1991 | int dvr_playback_resume(DVR_PlaybackHandle_t handle) { |
| 1992 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 1993 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 1994 | if (player == NULL) { |
| 1995 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 1996 | return DVR_FAILURE; |
| 1997 | } |
| 1998 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 1999 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE) { |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2000 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2001 | pthread_mutex_lock(&player->lock); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2002 | if (player->has_video) { |
| 2003 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE); |
| 2004 | AmTsPlayer_resumeVideoDecoding(player->handle); |
| 2005 | } |
| 2006 | if (player->has_audio) { |
| 2007 | AmTsPlayer_resumeAudioDecoding(player->handle); |
| 2008 | } |
| 2009 | //check is has audio param,if has audio .we need start audio, |
| 2010 | //we will stop audio when ff fb, if reach end, we will pause.so we need |
| 2011 | //start audio when resume play |
| 2012 | |
| 2013 | am_tsplayer_video_params vparams; |
| 2014 | am_tsplayer_audio_params aparams; |
| 2015 | uint64_t segmentid = player->cur_segment_id; |
| 2016 | _dvr_playback_get_playinfo(handle, segmentid, &vparams, &aparams); |
| 2017 | //valid audio pid, start audio |
| 2018 | if (player->has_audio == DVR_FALSE && VALID_PID(aparams.pid)) { |
| 2019 | player->has_audio = DVR_TRUE; |
| 2020 | AmTsPlayer_setAudioParams(player->handle, &aparams); |
| 2021 | AmTsPlayer_startAudioDecoding(player->handle); |
| 2022 | } else { |
| 2023 | DVR_DEBUG(1, "aparams.pid:%d player->has_audio:%d", aparams.pid, player->has_audio); |
| 2024 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2025 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 2026 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) { |
| 2027 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 2028 | player->state = DVR_PLAYBACK_STATE_START; |
| 2029 | } else { |
| 2030 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 2031 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_RESUME; |
| 2032 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 2033 | player->state = DVR_PLAYBACK_STATE_START; |
| 2034 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2035 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2036 | pthread_mutex_unlock(&player->lock); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2037 | } else if (player->state == DVR_PLAYBACK_STATE_PAUSE){ |
| 2038 | if (player->has_video) |
| 2039 | AmTsPlayer_resumeVideoDecoding(player->handle); |
| 2040 | if (player->has_audio) |
| 2041 | AmTsPlayer_resumeAudioDecoding(player->handle); |
| 2042 | DVR_DEBUG(1, "func: %s, set start state cur cmd[%d]", __func__, player->cmd.cur_cmd, player->state, player->cmd.state); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2043 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 2044 | player->state = DVR_PLAYBACK_STATE_START; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2045 | _dvr_cmd(handle, player->cmd.cur_cmd); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2046 | } else { |
| 2047 | if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) |
| 2048 | { |
| 2049 | pthread_mutex_lock(&player->lock); |
| 2050 | //clear flag |
| 2051 | DVR_DEBUG(1, "func: %s, clear pause live flag cur cmd[%d]", __func__, player->cmd.cur_cmd); |
| 2052 | player->play_flag = player->play_flag & (~DVR_PLAYBACK_STARTED_PAUSEDLIVE); |
| 2053 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE); |
| 2054 | pthread_mutex_unlock(&player->lock); |
| 2055 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2056 | } |
| 2057 | return DVR_SUCCESS; |
| 2058 | } |
| 2059 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2060 | static DVR_Bool_t _dvr_check_playinfo_changed(DVR_PlaybackHandle_t handle, int segment_id, int set_seg_id){ |
| 2061 | |
| 2062 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 2063 | DVR_PlaybackSegmentInfo_t *segment = NULL; |
| 2064 | DVR_PlaybackSegmentInfo_t *cur_segment = NULL; |
| 2065 | DVR_PlaybackSegmentInfo_t *set_segment = NULL; |
| 2066 | |
| 2067 | list_for_each_entry(segment, &player->segment_list, head) |
| 2068 | { |
| 2069 | if (segment->segment_id == segment_id) { |
| 2070 | cur_segment = segment; |
| 2071 | } |
| 2072 | if (segment->segment_id == set_seg_id) { |
| 2073 | set_segment = segment; |
| 2074 | } |
| 2075 | if (cur_segment != NULL && set_segment != NULL) { |
| 2076 | break; |
| 2077 | } |
| 2078 | } |
| 2079 | if (cur_segment == NULL || set_segment == NULL) { |
| 2080 | DVR_DEBUG(1, "set segmen or cur segment is null"); |
| 2081 | return DVR_TRUE; |
| 2082 | } |
| 2083 | if (cur_segment->pids.video.format != set_segment->pids.video.format || |
| 2084 | cur_segment->pids.video.pid != set_segment->pids.video.pid || |
| 2085 | cur_segment->pids.audio.format != set_segment->pids.audio.format || |
| 2086 | cur_segment->pids.audio.pid != set_segment->pids.audio.pid) { |
| 2087 | DVR_DEBUG(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); |
| 2088 | return DVR_TRUE; |
| 2089 | } |
| 2090 | DVR_DEBUG(1, "%s:play info not change", __func__); |
| 2091 | return DVR_FALSE; |
| 2092 | } |
| 2093 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2094 | /**\brief seek |
| 2095 | * \param[in] handle playback handle |
| 2096 | * \param[in] time_offset time offset base cur segment |
| 2097 | * \retval DVR_SUCCESS On success |
| 2098 | * \return Error code |
| 2099 | */ |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2100 | int dvr_playback_seek(DVR_PlaybackHandle_t handle, uint64_t segment_id, uint32_t time_offset) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2101 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2102 | |
| 2103 | if (player == NULL) { |
| 2104 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2105 | return DVR_FAILURE; |
| 2106 | } |
| 2107 | |
| 2108 | DVR_DEBUG(1, "lock func: %s segment_id %llu cur id %llu time_offset %u cur end: %d", __func__, segment_id,player->cur_segment_id, (uint32_t)time_offset, _dvr_get_end_time(handle)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2109 | DVR_DEBUG(1, "[%s][%p]---player->state[%d]----",__func__, handle, player->state); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2110 | pthread_mutex_lock(&player->lock); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2111 | |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2112 | int offset = -1; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2113 | DVR_Bool_t replay = _dvr_check_playinfo_changed(handle, player->cur_segment_id, segment_id); |
| 2114 | DVR_DEBUG(1, "[%s][%p]---player->state[%d]-replay[%d]--get lock-",__func__, handle, player->state, replay); |
| 2115 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2116 | //open segment if id is not current segment |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2117 | int ret = _dvr_open_segment(handle, segment_id); |
| 2118 | if (ret ==DVR_FAILURE) { |
| 2119 | DVR_DEBUG(1, "seek error at open segment"); |
| 2120 | pthread_mutex_unlock(&player->lock); |
| 2121 | return DVR_FAILURE; |
| 2122 | } |
| 2123 | if (time_offset >_dvr_get_end_time(handle) &&_dvr_has_next_segmentId(handle, segment_id) == DVR_FAILURE) { |
| 2124 | if (segment_ongoing(player->r_handle) == DVR_SUCCESS) { |
| 2125 | DVR_DEBUG(1, "is ongoing segment when seek end, need return success"); |
| 2126 | //pthread_mutex_unlock(&player->lock); |
| 2127 | //return DVR_SUCCESS; |
| 2128 | time_offset = _dvr_get_end_time(handle); |
| 2129 | } else { |
| 2130 | DVR_DEBUG(1, "is not ongoing segment when seek end, return failure"); |
| 2131 | pthread_mutex_unlock(&player->lock); |
| 2132 | return DVR_FAILURE; |
| 2133 | } |
| 2134 | } |
| 2135 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2136 | DVR_DEBUG(1, "seek open id[%lld]flag[0x%x] time_offset %u", player->cur_segment.segment_id, player->cur_segment.flags, time_offset); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2137 | //get file offset by time |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2138 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) { |
| 2139 | //forward playback.not seek end of file |
| 2140 | if (time_offset != 0 && time_offset > FB_DEFAULT_LEFT_TIME) { |
| 2141 | //default -2000ms |
| 2142 | time_offset = time_offset -FB_DEFAULT_LEFT_TIME; |
| 2143 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2144 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2145 | pthread_mutex_lock(&player->segment_lock); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2146 | player->drop_ts = DVR_TRUE; |
| 2147 | offset = segment_seek(player->r_handle, (uint64_t)time_offset, player->openParams.block_size); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2148 | DVR_DEBUG(0, "---seek get offset by time offset, offset=%d time_offset %u",offset, time_offset); |
| 2149 | pthread_mutex_unlock(&player->segment_lock); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2150 | player->offset = offset; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2151 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2152 | _dvr_get_end_time(handle); |
| 2153 | //init fffb time |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2154 | player->fffb_current = _dvr_time_getClock(); |
| 2155 | player->fffb_start = player->fffb_current; |
| 2156 | player->fffb_start_pcr = _dvr_get_cur_time(handle); |
| 2157 | player->next_fffb_time = player->fffb_current; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2158 | //pause state if need to replayer false |
| 2159 | if (player->state == DVR_PLAYBACK_STATE_STOP || |
| 2160 | (player->state == DVR_PLAYBACK_STATE_PAUSE)) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2161 | //only seek file,not start |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2162 | DVR_DEBUG(1, "unlock func: %s", __func__); |
| 2163 | pthread_mutex_unlock(&player->lock); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2164 | return DVR_SUCCESS; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2165 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2166 | //stop play |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2167 | DVR_DEBUG(0, "seek stop play, not inject data has video[%d]audio[%d]", player->has_video, player->has_audio); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2168 | if (player->has_video) { |
| 2169 | player->has_video = DVR_FALSE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2170 | AmTsPlayer_stopVideoDecoding(player->handle); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | if (player->has_audio) { |
| 2174 | player->has_audio =DVR_FALSE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2175 | AmTsPlayer_stopAudioDecoding(player->handle); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2176 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2177 | //start play |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2178 | am_tsplayer_video_params vparams; |
| 2179 | am_tsplayer_audio_params aparams; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2180 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2181 | player->cur_segment_id = segment_id; |
| 2182 | |
| 2183 | int sync = DVR_PLAYBACK_SYNC; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2184 | //get segment info and audio video pid fmt ; |
| 2185 | _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2186 | //start audio and video |
| 2187 | if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) { |
| 2188 | //audio abnd video pis is all invalid, return error. |
| 2189 | DVR_DEBUG(0, "seek start dvr play back start error, not found audio and video info"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2190 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2191 | pthread_mutex_unlock(&player->lock); |
| 2192 | return -1; |
| 2193 | } |
| 2194 | //add |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2195 | if (sync == DVR_PLAYBACK_SYNC) { |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2196 | if (VALID_PID(vparams.pid)) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2197 | //player->has_video; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2198 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_PAUSE || |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2199 | player->speed > 1.0f|| |
| 2200 | player->speed <= -1.0f) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2201 | //if is pause state. we need set trick mode. |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2202 | DVR_DEBUG(1, "[%s]seek set trick mode player->speed [%f]", __func__, player->speed); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2203 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2204 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2205 | AmTsPlayer_setVideoParams(player->handle, &vparams); |
| 2206 | AmTsPlayer_startVideoDecoding(player->handle); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2207 | player->has_video = DVR_TRUE; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2208 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2209 | if (VALID_PID(aparams.pid)) { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2210 | DVR_DEBUG(1, " func: %s start audio seek", __func__); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2211 | AmTsPlayer_setAudioParams(player->handle, &aparams); |
| 2212 | AmTsPlayer_startAudioDecoding(player->handle); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2213 | player->has_audio = DVR_TRUE; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2214 | } |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2215 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2216 | if (player->state == DVR_PLAYBACK_STATE_PAUSE && |
| 2217 | ((player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 2218 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) || |
| 2219 | (player->cmd.last_cmd == DVR_PLAYBACK_CMD_FF || |
| 2220 | player->cmd.last_cmd == DVR_PLAYBACK_CMD_FB))) { |
| 2221 | player->cmd.state = DVR_PLAYBACK_STATE_PAUSE; |
| 2222 | player->state = DVR_PLAYBACK_STATE_PAUSE; |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2223 | DVR_DEBUG(1, "set state pause in seek"); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2224 | } else if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 2225 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2226 | player->speed > 1.0f|| |
| 2227 | player->speed <= -1.0f) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2228 | DVR_DEBUG(1, "not set cmd to seek"); |
| 2229 | //not pause state, we need not set cur cmd |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2230 | } else { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2231 | DVR_DEBUG(1, "set cmd to seek"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2232 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 2233 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_SEEK; |
| 2234 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 2235 | player->state = DVR_PLAYBACK_STATE_START; |
| 2236 | } |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2237 | DVR_DEBUG(1, "unlock func: %s ---", __func__); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2238 | pthread_mutex_unlock(&player->lock); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2239 | |
| 2240 | return DVR_SUCCESS; |
| 2241 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2242 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2243 | static int _dvr_get_cur_time(DVR_PlaybackHandle_t handle) { |
| 2244 | //get cur time of segment |
| 2245 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2246 | |
| 2247 | if (player == NULL || player->handle == NULL) { |
| 2248 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2249 | return DVR_FAILURE; |
| 2250 | } |
| 2251 | |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2252 | int64_t cache = 0;//defalut es buf cache 500ms |
| 2253 | AmTsPlayer_getDelayTime(player->handle, &cache); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2254 | pthread_mutex_lock(&player->segment_lock); |
| 2255 | uint64_t cur = segment_tell_current_time(player->r_handle); |
| 2256 | pthread_mutex_unlock(&player->segment_lock); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2257 | DVR_DEBUG(1, "get cur time [%lld] cache:%lld cur id [%d]", cur, cache, player->cur_segment_id); |
| 2258 | if (cache > MAX_CACHE_TIME) { |
| 2259 | DVR_DEBUG(1, "get cache:%lld ERRRO", cache); |
| 2260 | cache = g_cache_time; |
| 2261 | } else { |
| 2262 | g_cache_time = cache; |
| 2263 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2264 | if (player->state == DVR_PLAYBACK_STATE_STOP) { |
| 2265 | cache = 0; |
| 2266 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2267 | return (int)(cur > cache ? cur - cache : 0); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | //get current segment current pcr time of read pos |
| 2271 | static int _dvr_get_end_time(DVR_PlaybackHandle_t handle) { |
| 2272 | //get cur time of segment |
| 2273 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2274 | |
| 2275 | if (player == NULL) { |
| 2276 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2277 | return DVR_FAILURE; |
| 2278 | } |
| 2279 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2280 | pthread_mutex_lock(&player->segment_lock); |
| 2281 | uint64_t end = segment_tell_total_time(player->r_handle); |
| 2282 | DVR_DEBUG(1, "get tatal time [%lld]", end); |
| 2283 | pthread_mutex_unlock(&player->segment_lock); |
| 2284 | return (int)end; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2285 | } |
| 2286 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2287 | #define FB_MIX_SEEK_TIME 1000 |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2288 | //start replay |
| 2289 | static int _dvr_playback_calculate_seekpos(DVR_PlaybackHandle_t handle) { |
| 2290 | |
| 2291 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 2292 | //calculate pcr seek time |
| 2293 | int t_diff = 0; |
| 2294 | int seek_time = 0; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2295 | |
| 2296 | if (player == NULL) { |
| 2297 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2298 | return DVR_FAILURE; |
| 2299 | } |
| 2300 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2301 | if (player->fffb_start == -1) { |
| 2302 | //set fffb start time ms |
| 2303 | player->fffb_start = _dvr_time_getClock(); |
| 2304 | player->fffb_current = player->fffb_start; |
| 2305 | //get segment current time pos |
| 2306 | player->fffb_start_pcr = _dvr_get_cur_time(handle); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2307 | DVR_DEBUG(1, "calculate seek posplayer->fffb_start_pcr[%d]ms, speed[%f]", player->fffb_start_pcr, player->speed); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2308 | t_diff = 0; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2309 | //default first time 1ms seek |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2310 | seek_time = FB_MIX_SEEK_TIME; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2311 | } else { |
| 2312 | player->fffb_current = _dvr_time_getClock(); |
| 2313 | t_diff = player->fffb_current - player->fffb_start; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2314 | //if speed is < 0, cmd is fb. |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2315 | seek_time = player->fffb_start_pcr + t_diff *player->speed; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2316 | if (seek_time <= 0) { |
| 2317 | //need seek to pre one segment |
| 2318 | seek_time = 0; |
| 2319 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2320 | //seek segment pos |
| 2321 | if (player->r_handle) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2322 | pthread_mutex_lock(&player->segment_lock); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2323 | if (segment_seek(player->r_handle, seek_time, player->openParams.block_size) == DVR_FAILURE) { |
| 2324 | seek_time = 0; |
| 2325 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2326 | pthread_mutex_unlock(&player->segment_lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2327 | } else { |
| 2328 | // |
| 2329 | DVR_DEBUG(1, "segment not open,can not seek"); |
| 2330 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2331 | DVR_DEBUG(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 chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2332 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2333 | return seek_time; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | //start replay |
| 2338 | static int _dvr_playback_fffb_replay(DVR_PlaybackHandle_t handle) { |
| 2339 | // |
| 2340 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2341 | |
| 2342 | if (player == NULL) { |
| 2343 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2344 | return DVR_FAILURE; |
| 2345 | } |
| 2346 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2347 | //stop |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2348 | if (player->has_video) { |
| 2349 | DVR_DEBUG(1, "fffb stop video"); |
| 2350 | AmTsPlayer_stopVideoDecoding(player->handle); |
| 2351 | } |
| 2352 | if (player->has_audio) { |
| 2353 | DVR_DEBUG(1, "fffb stop audio"); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2354 | player->has_audio =DVR_FALSE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2355 | AmTsPlayer_stopAudioDecoding(player->handle); |
| 2356 | } |
| 2357 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2358 | //start video and audio |
| 2359 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2360 | am_tsplayer_video_params vparams; |
| 2361 | am_tsplayer_audio_params aparams; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2362 | uint64_t segment_id = player->cur_segment_id; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2363 | |
| 2364 | //get segment info and audio video pid fmt ; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2365 | //pthread_mutex_lock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2366 | _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams); |
| 2367 | //start audio and video |
| 2368 | if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) { |
| 2369 | //audio abnd video pis is all invalid, return error. |
| 2370 | DVR_DEBUG(0, "dvr play back restart error, not found audio and video info"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2371 | //pthread_mutex_unlock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2372 | return -1; |
| 2373 | } |
| 2374 | |
| 2375 | if (VALID_PID(vparams.pid)) { |
| 2376 | player->has_video = DVR_TRUE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2377 | DVR_DEBUG(1, "fffb start video"); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2378 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2379 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT); |
| 2380 | AmTsPlayer_setVideoParams(player->handle, &vparams); |
| 2381 | AmTsPlayer_startVideoDecoding(player->handle); |
| 2382 | //playback_device_video_start(player->handle , &vparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2383 | //if set flag is pause live, we need set trick mode |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2384 | //playback_device_trick_mode(player->handle, 1); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2385 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2386 | if (0 && VALID_PID(aparams.pid)) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2387 | player->has_audio = DVR_TRUE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2388 | DVR_DEBUG(1, "fffb start audio"); |
| 2389 | AmTsPlayer_setAudioParams(player->handle, &aparams); |
| 2390 | AmTsPlayer_startAudioDecoding(player->handle); |
| 2391 | //playback_device_audio_start(player->handle , &aparams); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2392 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2393 | //fffb mode need stop fast; |
| 2394 | AmTsPlayer_stopFast(player->handle); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2395 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2396 | //pthread_mutex_unlock(&player->lock); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2397 | return 0; |
| 2398 | } |
| 2399 | |
| 2400 | static int _dvr_playback_fffb(DVR_PlaybackHandle_t handle) { |
| 2401 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2402 | |
| 2403 | if (player == NULL) { |
| 2404 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2405 | return DVR_FAILURE; |
| 2406 | } |
| 2407 | |
| 2408 | player->first_frame = 0; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2409 | DVR_DEBUG(1, "lock func: %s speed [%f]", __func__, player->speed); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2410 | pthread_mutex_lock(&player->lock); |
| 2411 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2412 | int seek_time = _dvr_playback_calculate_seekpos(handle); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2413 | DVR_DEBUG(1, "get lock func: %s speed [%f]id [%lld]seek_time[%d]", __func__, player->speed, player->cur_segment_id, seek_time); |
| 2414 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2415 | if (_dvr_has_next_segmentId(handle, player->cur_segment_id) == DVR_FAILURE && seek_time < FB_MIX_SEEK_TIME && IS_FB(player->speed)) { |
| 2416 | //seek time set 0 |
| 2417 | seek_time = 0; |
| 2418 | } |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2419 | if (seek_time == 0) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2420 | //for fb cmd, we need open pre segment.if reach first one segment, send begin event |
| 2421 | int ret = _change_to_next_segment((DVR_PlaybackHandle_t)player); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2422 | if (ret != DVR_SUCCESS && IS_FB(player->speed)) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2423 | pthread_mutex_unlock(&player->lock); |
| 2424 | dvr_playback_pause(handle, DVR_FALSE); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2425 | //send event here and pause |
| 2426 | DVR_Play_Notify_t notify; |
| 2427 | memset(¬ify, 0 , sizeof(DVR_Play_Notify_t)); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2428 | notify.event = DVR_PLAYBACK_EVENT_REACHED_BEGIN; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2429 | //get play statue not here |
| 2430 | _dvr_playback_sent_event(handle, DVR_PLAYBACK_EVENT_REACHED_BEGIN, ¬ify); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2431 | DVR_DEBUG(1, "*******************send begin event func: %s speed [%f] cur [%d]", __func__, player->speed, _dvr_get_cur_time(handle)); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2432 | //change to pause |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2433 | return DVR_SUCCESS; |
| 2434 | } |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2435 | _dvr_playback_sent_transition_ok(handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2436 | _dvr_init_fffb_time(handle); |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2437 | DVR_DEBUG(1, "*******************send trans ok event func: %s speed [%f]", __func__, player->speed); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2438 | } |
| 2439 | player->next_fffb_time =_dvr_time_getClock() + FFFB_SLEEP_TIME; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2440 | _dvr_playback_fffb_replay(handle); |
| 2441 | |
| 2442 | pthread_mutex_unlock(&player->lock); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2443 | DVR_DEBUG(1, "unlock func: %s", __func__); |
| 2444 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2445 | return DVR_SUCCESS; |
| 2446 | } |
| 2447 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2448 | //start replay, need get lock at extern |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2449 | static int _dvr_playback_replay(DVR_PlaybackHandle_t handle, DVR_Bool_t trick) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2450 | // |
| 2451 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2452 | |
| 2453 | if (player == NULL) { |
| 2454 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2455 | return DVR_FAILURE; |
| 2456 | } |
| 2457 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2458 | //stop |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2459 | if (player->has_video) { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2460 | player->has_video = DVR_FALSE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2461 | AmTsPlayer_stopVideoDecoding(player->handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | if (player->has_audio) { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2465 | player->has_audio = DVR_FALSE; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2466 | AmTsPlayer_stopAudioDecoding(player->handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2467 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2468 | //start video and audio |
| 2469 | |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2470 | am_tsplayer_video_params vparams; |
| 2471 | am_tsplayer_audio_params aparams; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2472 | uint64_t segment_id = player->cur_segment_id; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2473 | |
| 2474 | //get segment info and audio video pid fmt ; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2475 | DVR_DEBUG(1, "lock func: %s", __func__); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2476 | _dvr_playback_get_playinfo(handle, segment_id, &vparams, &aparams); |
| 2477 | //start audio and video |
| 2478 | if (!VALID_PID(vparams.pid) && !VALID_PID(aparams.pid)) { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2479 | //audio and video pis is all invalid, return error. |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2480 | DVR_DEBUG(0, "dvr play back restart error, not found audio and video info"); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2481 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2482 | return -1; |
| 2483 | } |
| 2484 | |
| 2485 | if (VALID_PID(vparams.pid)) { |
| 2486 | player->has_video = DVR_TRUE; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2487 | if (trick == DVR_TRUE) { |
| 2488 | DVR_DEBUG(1, "settrick mode at replay"); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2489 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_PAUSE_NEXT); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2490 | } |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2491 | else { |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2492 | AmTsPlayer_setTrickMode(player->handle, AV_VIDEO_TRICK_MODE_NONE); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2493 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2494 | AmTsPlayer_setVideoParams(player->handle, &vparams); |
| 2495 | AmTsPlayer_startVideoDecoding(player->handle); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2496 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2497 | |
| 2498 | if (IS_FAST_SPEED(player->cmd.speed.speed.speed)) { |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2499 | AmTsPlayer_startFast(player->handle, (float)player->cmd.speed.speed.speed/(float)100); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2500 | player->speed = (float)player->cmd.speed.speed.speed/100.0f; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2501 | } else { |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2502 | if (VALID_PID(aparams.pid)) { |
| 2503 | player->has_audio = DVR_TRUE; |
| 2504 | DVR_DEBUG(1, " func: %s start audio", __func__); |
| 2505 | AmTsPlayer_startAudioDecoding(player->handle); |
| 2506 | AmTsPlayer_setAudioParams(player->handle, &aparams); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2507 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2508 | AmTsPlayer_stopFast(player->handle); |
| 2509 | player->cmd.speed.speed.speed = PLAYBACK_SPEED_X1; |
| 2510 | player->speed = (float)PLAYBACK_SPEED_X1/100.0f; |
| 2511 | } |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2512 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 2513 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_START; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2514 | player->cmd.state = DVR_PLAYBACK_STATE_START; |
| 2515 | player->state = DVR_PLAYBACK_STATE_START; |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2516 | DVR_DEBUG(1, "unlock func: %s", __func__); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2517 | return 0; |
| 2518 | } |
| 2519 | |
| 2520 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2521 | /**\brief Set play speed |
| 2522 | * \param[in] handle playback handle |
| 2523 | * \param[in] speed playback speed |
| 2524 | * \retval DVR_SUCCESS On success |
| 2525 | * \return Error code |
| 2526 | */ |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2527 | int dvr_playback_set_speed(DVR_PlaybackHandle_t handle, DVR_PlaybackSpeed_t speed) { |
| 2528 | |
| 2529 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2530 | |
| 2531 | if (player == NULL) { |
| 2532 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2533 | return DVR_FAILURE; |
| 2534 | } |
| 2535 | |
| 2536 | DVR_DEBUG(1, "lock func: %s speed [%d]", __func__, speed.speed.speed); |
| 2537 | if (_dvr_support_speed(speed.speed.speed) == DVR_FALSE) { |
| 2538 | DVR_DEBUG(1, " func: %s not support speed [%d]", __func__, speed.speed.speed); |
| 2539 | return DVR_FAILURE; |
| 2540 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2541 | pthread_mutex_lock(&player->lock); |
| 2542 | if (player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FF |
| 2543 | && player->cmd.cur_cmd != DVR_PLAYBACK_CMD_FB) { |
| 2544 | player->cmd.last_cmd = player->cmd.cur_cmd; |
| 2545 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2546 | if (player->state != DVR_PLAYBACK_STATE_PAUSE && |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2547 | IS_KERNEL_SPEED(speed.speed.speed)) { |
| 2548 | //case 1. cur speed is 100,set 200 50 25 12 . |
| 2549 | //we think x1 and x2 s1/2 s 1/4 s 1/8 is normal speed. is not ff fb. |
| 2550 | if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2551 | //if last speed is x2 or s2, we need stop fast |
hualing chen | 2bd8a7a | 2020-04-02 11:31:03 +0800 | [diff] [blame] | 2552 | if (speed.speed.speed == PLAYBACK_SPEED_X1) { |
| 2553 | // resume audio and stop fast play |
| 2554 | AmTsPlayer_stopFast(player->handle); |
| 2555 | pthread_mutex_unlock(&player->lock); |
| 2556 | _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART); |
| 2557 | pthread_mutex_lock(&player->lock); |
| 2558 | } else { |
| 2559 | //set play speed and if audio is start, stop audio. |
| 2560 | if (player->has_audio) { |
| 2561 | DVR_DEBUG(1, "fast play stop audio"); |
| 2562 | AmTsPlayer_stopAudioDecoding(player->handle); |
| 2563 | player->has_audio = DVR_FALSE; |
| 2564 | } |
| 2565 | AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2566 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2567 | player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2568 | player->cmd.speed.speed = speed.speed; |
| 2569 | player->speed = (float)speed.speed.speed/(float)100; |
| 2570 | pthread_mutex_unlock(&player->lock); |
| 2571 | return DVR_SUCCESS; |
| 2572 | } |
| 2573 | //case 2. not start play |
| 2574 | if (player->state == DVR_PLAYBACK_STATE_STOP) { |
| 2575 | //only set speed.and return; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2576 | player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2577 | player->cmd.speed.speed = speed.speed; |
| 2578 | player->speed = (float)speed.speed.speed/(float)100; |
| 2579 | pthread_mutex_unlock(&player->lock); |
| 2580 | return DVR_SUCCESS; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2581 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2582 | //case 3 fffb mode |
| 2583 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 2584 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) { |
| 2585 | //restart play at normal speed exit ff fb |
| 2586 | DVR_DEBUG(1, "set speed normal and replay playback"); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2587 | player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2588 | player->cmd.speed.speed = speed.speed; |
| 2589 | player->speed = (float)speed.speed.speed/(float)100; |
| 2590 | _dvr_playback_replay(handle, DVR_FALSE); |
| 2591 | pthread_mutex_unlock(&player->lock); |
| 2592 | return DVR_SUCCESS; |
| 2593 | } |
| 2594 | } |
| 2595 | else if (player->state == DVR_PLAYBACK_STATE_PAUSE && |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2596 | IS_KERNEL_SPEED(speed.speed.speed)) { |
| 2597 | //case 1. cur speed is kernel support speed,set kernel speed. |
| 2598 | if (IS_KERNEL_SPEED(player->cmd.speed.speed.speed)) { |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2599 | //if last speed is x2 or s2, we need stop fast |
hualing chen | 2bd8a7a | 2020-04-02 11:31:03 +0800 | [diff] [blame] | 2600 | if (speed.speed.speed == PLAYBACK_SPEED_X1) { |
| 2601 | // resume audio and stop fast play |
| 2602 | AmTsPlayer_stopFast(player->handle); |
| 2603 | pthread_mutex_unlock(&player->lock); |
| 2604 | _dvr_cmd(handle, DVR_PLAYBACK_CMD_ASTART); |
| 2605 | pthread_mutex_lock(&player->lock); |
| 2606 | } else { |
| 2607 | //set play speed and if audio is start, stop audio. |
| 2608 | if (player->has_audio) { |
| 2609 | DVR_DEBUG(1, "fast play stop audio at pause"); |
| 2610 | AmTsPlayer_stopAudioDecoding(player->handle); |
| 2611 | player->has_audio = DVR_FALSE; |
| 2612 | } |
| 2613 | AmTsPlayer_startFast(player->handle, (float)speed.speed.speed/(float)100); |
| 2614 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2615 | player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2616 | player->cmd.speed.speed = speed.speed; |
| 2617 | player->speed = (float)speed.speed.speed/(float)100; |
| 2618 | pthread_mutex_unlock(&player->lock); |
| 2619 | return DVR_SUCCESS; |
| 2620 | } |
| 2621 | //case 2 fffb mode |
| 2622 | if (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 2623 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB) { |
| 2624 | //restart play at normal speed exit ff fb |
| 2625 | DVR_DEBUG(1, "set speed x1 s2 and replay playback"); |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2626 | player->cmd.speed.mode = DVR_PLAYBACK_KERNEL_SUPPORT; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2627 | player->cmd.speed.speed = speed.speed; |
| 2628 | player->speed = (float)speed.speed.speed/(float)100; |
| 2629 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART; |
| 2630 | pthread_mutex_unlock(&player->lock); |
| 2631 | return DVR_SUCCESS; |
| 2632 | } |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2633 | } |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2634 | if (IS_KERNEL_SPEED(speed.speed.speed)) { |
| 2635 | //we think x1 and s2 s4 s8 x2is normal speed. is not ff fb. |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2636 | } else { |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2637 | if ((float)speed.speed.speed > 1.0f) |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2638 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FF; |
| 2639 | else |
| 2640 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_FB; |
| 2641 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2642 | player->cmd.speed.mode = speed.mode; |
| 2643 | player->cmd.speed.speed = speed.speed; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2644 | player->speed = (float)speed.speed.speed/(float)100; |
| 2645 | //reset fffb time, if change speed value |
| 2646 | _dvr_init_fffb_time(handle); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2647 | if (speed.speed.speed == PLAYBACK_SPEED_X1 && |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 2648 | (player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FF || |
| 2649 | player->cmd.cur_cmd == DVR_PLAYBACK_CMD_FB)) { |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2650 | //restart play at normal speed exit ff fb |
| 2651 | DVR_DEBUG(1, "set speed normal and replay playback"); |
| 2652 | _dvr_playback_replay(handle, DVR_FALSE); |
| 2653 | } else if (speed.speed.speed == PLAYBACK_SPEED_X1 && |
| 2654 | (player->state == DVR_PLAYBACK_STATE_PAUSE)) { |
| 2655 | player->cmd.cur_cmd = DVR_PLAYBACK_CMD_AVRESTART; |
| 2656 | DVR_DEBUG(1, "set speed normal at pause state ,set cur cmd"); |
| 2657 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2658 | //need exit ff fb |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2659 | DVR_DEBUG(1, "unlock func: %s speed[%f]cmd[%d]", __func__, player->speed, player->cmd.cur_cmd); |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2660 | pthread_mutex_unlock(&player->lock); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2661 | return DVR_SUCCESS; |
| 2662 | } |
| 2663 | /**\brief Get playback status |
| 2664 | * \param[in] handle playback handle |
| 2665 | * \param[out] p_status playback status |
| 2666 | * \retval DVR_SUCCESS On success |
| 2667 | * \return Error code |
| 2668 | */ |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2669 | int dvr_playback_get_status(DVR_PlaybackHandle_t handle, |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2670 | DVR_PlaybackStatus_t *p_status) { |
| 2671 | // |
| 2672 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2673 | |
| 2674 | if (player == NULL) { |
| 2675 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2676 | return DVR_FAILURE; |
| 2677 | } |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2678 | |
| 2679 | p_status->state = player->state; |
hualing chen | 3114087 | 2020-03-25 12:29:26 +0800 | [diff] [blame] | 2680 | //when got first frame we will change to pause state.this only from start play to got first frame |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 2681 | if ((player->play_flag&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE && |
| 2682 | player->state == DVR_PLAYBACK_STATE_START) { |
| 2683 | p_status->state = DVR_PLAYBACK_STATE_PAUSE; |
| 2684 | } |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2685 | |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2686 | p_status->time_end = _dvr_get_end_time(handle); |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2687 | p_status->time_cur = _dvr_get_cur_time(handle); |
hualing chen | 041c409 | 2020-04-05 15:11:50 +0800 | [diff] [blame] | 2688 | p_status->segment_id = player->cur_segment_id; |
hualing chen | 2aba402 | 2020-03-02 13:49:55 +0800 | [diff] [blame] | 2689 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2690 | memcpy(&p_status->pids, &player->cur_segment.pids, sizeof(DVR_PlaybackPids_t)); |
hualing chen | cc91e1c | 2020-02-28 13:26:17 +0800 | [diff] [blame] | 2691 | p_status->speed = player->cmd.speed.speed.speed; |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2692 | p_status->flags = player->cur_segment.flags; |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2693 | DVR_DEBUG(1, "get stat end player state[%s]state[%s]cur[%d]end[%d] id[%lld]playflag[%d]speed[%f]", |
hualing chen | 6d24aa9 | 2020-03-23 18:43:47 +0800 | [diff] [blame] | 2694 | _dvr_playback_state_toString(player->state), |
| 2695 | _dvr_playback_state_toString(p_status->state), |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2696 | p_status->time_cur, p_status->time_end, |
| 2697 | p_status->segment_id,player->play_flag, |
| 2698 | player->speed); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2699 | return DVR_SUCCESS; |
| 2700 | } |
| 2701 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2702 | void _dvr_dump_segment(DVR_PlaybackSegmentInfo_t *segment) { |
| 2703 | if (segment != NULL) { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2704 | DVR_DEBUG(1, "segment id: %lld", segment->segment_id); |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2705 | DVR_DEBUG(1, "segment flag: %d", segment->flags); |
| 2706 | DVR_DEBUG(1, "segment location: [%s]", segment->location); |
| 2707 | DVR_DEBUG(1, "segment vpid: 0x%x vfmt:0x%x", segment->pids.video.pid,segment->pids.video.format); |
| 2708 | DVR_DEBUG(1, "segment apid: 0x%x afmt:0x%x", segment->pids.audio.pid,segment->pids.audio.format); |
| 2709 | DVR_DEBUG(1, "segment pcr pid: 0x%x pcr fmt:0x%x", segment->pids.pcr.pid,segment->pids.pcr.format); |
| 2710 | DVR_DEBUG(1, "segment sub apid: 0x%x sub afmt:0x%x", segment->pids.ad.pid,segment->pids.ad.format); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2711 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2712 | } |
| 2713 | |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2714 | int dvr_dump_segmentinfo(DVR_PlaybackHandle_t handle, uint64_t segment_id) { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2715 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2716 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 2717 | if (player == NULL) { |
| 2718 | DVR_DEBUG(1, "func [%s] player is NULL", __func__); |
| 2719 | return DVR_FAILURE; |
| 2720 | } |
| 2721 | |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2722 | DVR_PlaybackSegmentInfo_t *segment; |
| 2723 | list_for_each_entry(segment, &player->segment_list, head) |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2724 | { |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2725 | if (segment_id >= 0) { |
| 2726 | if (segment->segment_id == segment_id) { |
| 2727 | _dvr_dump_segment(segment); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2728 | break; |
| 2729 | } |
| 2730 | } else { |
hualing chen | 5cbe1a6 | 2020-02-10 16:36:36 +0800 | [diff] [blame] | 2731 | //printf segment info |
hualing chen | 040df22 | 2020-01-17 13:35:02 +0800 | [diff] [blame] | 2732 | _dvr_dump_segment(segment); |
hualing chen | 86e7d48 | 2020-01-16 15:13:33 +0800 | [diff] [blame] | 2733 | } |
| 2734 | } |
| 2735 | return 0; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 2736 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 2737 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 2738 | int dvr_playback_set_decrypt_callback(DVR_PlaybackHandle_t handle, DVR_CryptoFunction_t func, void *userdata) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 2739 | { |
| 2740 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 2741 | DVR_RETURN_IF_FALSE(player); |
| 2742 | DVR_RETURN_IF_FALSE(func); |
| 2743 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2744 | DVR_DEBUG(1, "in %s func:%#x", __func__, func); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 2745 | pthread_mutex_lock(&player->lock); |
| 2746 | |
| 2747 | player->dec_func = func; |
| 2748 | player->dec_userdata = userdata; |
| 2749 | |
| 2750 | pthread_mutex_unlock(&player->lock); |
| 2751 | DVR_DEBUG(1, "out %s", __func__); |
| 2752 | return DVR_SUCCESS; |
| 2753 | } |
| 2754 | |
| 2755 | int dvr_playback_set_secure_buffer(DVR_PlaybackHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 2756 | { |
| 2757 | DVR_Playback_t *player = (DVR_Playback_t *) handle; |
| 2758 | DVR_RETURN_IF_FALSE(player); |
| 2759 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 2760 | DVR_RETURN_IF_FALSE(len); |
| 2761 | |
| 2762 | DVR_DEBUG(1, "in %s", __func__); |
| 2763 | pthread_mutex_lock(&player->lock); |
| 2764 | |
| 2765 | player->is_secure_mode = 1; |
| 2766 | player->secure_buffer = p_secure_buf; |
| 2767 | player->secure_buffer_size = len; |
| 2768 | |
| 2769 | pthread_mutex_unlock(&player->lock); |
| 2770 | DVR_DEBUG(1, "out %s", __func__); |
| 2771 | return DVR_SUCCESS; |
| 2772 | } |