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