Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Amlogic, Inc. All rights reserved. |
| 3 | * |
| 4 | * This source code is subject to the terms and conditions defined in the |
| 5 | * file 'LICENSE' which is part of this source code package. |
| 6 | * |
| 7 | * Description: |
| 8 | */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 9 | #include <errno.h> |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 10 | #include <pthread.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include <sys/time.h> |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 15 | #include <poll.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <sys/prctl.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <unistd.h> |
| 22 | //#include <linux/amlogic/msync.h> |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 23 | #include "aml_avsync.h" |
| 24 | #include "queue.h" |
| 25 | #include "pattern.h" |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 26 | #include "aml_avsync_log.h" |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 27 | #include "msync_util.h" |
| 28 | #include "msync.h" |
| 29 | #include <pthread.h> |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 30 | |
| 31 | enum sync_state { |
| 32 | AV_SYNC_STAT_INIT = 0, |
| 33 | AV_SYNC_STAT_RUNNING = 1, |
| 34 | AV_SYNC_STAT_SYNC_SETUP = 2, |
| 35 | AV_SYNC_STAT_SYNC_LOST = 3, |
| 36 | }; |
| 37 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 38 | enum audio_switch_state_ { |
| 39 | AUDIO_SWITCH_STAT_INIT = 0, |
| 40 | AUDIO_SWITCH_STAT_RESET = 1, |
| 41 | AUDIO_SWITCH_STAT_START = 2, |
| 42 | AUDIO_SWITCH_STAT_FINISH = 3, |
| 43 | }; |
| 44 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 45 | #define SESSION_DEV "avsync_s" |
| 46 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 47 | struct av_sync_session { |
| 48 | /* session id attached */ |
| 49 | int session_id; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 50 | int fd; |
| 51 | bool attached; |
| 52 | enum sync_mode mode; |
| 53 | /* for audio trickplay */ |
| 54 | enum sync_mode backup_mode; |
| 55 | enum sync_type type; |
| 56 | uint32_t start_policy; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 57 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 58 | /* playback time, will stop increasing during pause */ |
| 59 | pts90K vpts; |
| 60 | pts90K apts; |
| 61 | |
| 62 | /* phase adjustment of stream time for rate control (Video ONLY) */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 63 | pts90K phase; |
| 64 | bool phase_set; |
| 65 | |
| 66 | /* pts of last rendered frame */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 67 | pts90K last_wall; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 68 | pts90K last_pts; |
| 69 | struct vframe *last_frame; |
| 70 | |
| 71 | bool first_frame_toggled; |
| 72 | /* Whether in pause state */ |
| 73 | bool paused; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 74 | enum sync_state state; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 75 | void *pattern_detector; |
| 76 | void *frame_q; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 77 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 78 | /* start control */ |
| 79 | int start_thres; |
| 80 | audio_start_cb audio_start; |
| 81 | void *audio_start_priv; |
| 82 | |
| 83 | /* render property */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 84 | int delay; |
| 85 | pts90K vsync_interval; |
| 86 | |
| 87 | /* state lock */ |
| 88 | pthread_mutex_t lock; |
| 89 | /* pattern */ |
| 90 | int last_holding_peroid; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 91 | bool session_started; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 92 | |
| 93 | float speed; |
| 94 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 95 | /* pause pts */ |
| 96 | pts90K pause_pts; |
| 97 | pause_pts_done pause_pts_cb; |
| 98 | void *pause_cb_priv; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 99 | |
| 100 | /* log control */ |
| 101 | uint32_t last_systime; |
| 102 | uint32_t sync_lost_cnt; |
| 103 | struct timeval sync_lost_print_time; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 104 | |
| 105 | pthread_t poll_thread; |
| 106 | /* pcr master, IPTV only */ |
| 107 | bool quit_poll; |
| 108 | enum sync_mode active_mode; |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 109 | uint32_t disc_thres_min; |
| 110 | uint32_t disc_thres_max; |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 111 | |
| 112 | /* error detection */ |
| 113 | uint32_t last_poptime; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 114 | uint32_t outlier_cnt; |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 115 | pts90K last_disc_pts; |
| 116 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 117 | // indicate set audio switch |
| 118 | bool in_audio_switch; |
| 119 | enum audio_switch_state_ audio_switch_state; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | #define MAX_FRAME_NUM 32 |
| 123 | #define DEFAULT_START_THRESHOLD 2 |
| 124 | #define TIME_UNIT90K (90000) |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 125 | #define AV_DISC_THRES_MIN (TIME_UNIT90K / 3) |
| 126 | #define AV_DISC_THRES_MAX (TIME_UNIT90K * 10) |
Song Zhao | 7daf3a1 | 2021-05-10 22:22:25 -0700 | [diff] [blame] | 127 | #define A_ADJ_THREDHOLD_HB (900 * 6) //60ms |
| 128 | #define A_ADJ_THREDHOLD_LB (900 * 2) //20ms |
Song Zhao | 52f5519 | 2021-05-06 10:52:21 -0700 | [diff] [blame] | 129 | #define AV_PATTERN_RESET_THRES (TIME_UNIT90K / 10) |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 130 | #define SYNC_LOST_PRINT_THRESHOLD 10000000 //10 seconds In micro seconds |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 131 | #define LIVE_MODE(mode) ((mode) == AV_SYNC_MODE_PCR_MASTER || (mode) == AV_SYNC_MODE_IPTV) |
| 132 | |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 133 | #define STREAM_DISC_THRES (TIME_UNIT90K / 10) |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 134 | #define OUTLIER_MAX_CNT 8 |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 135 | |
| 136 | static uint64_t time_diff (struct timeval *b, struct timeval *a); |
| 137 | static bool frame_expire(struct av_sync_session* avsync, |
| 138 | uint32_t systime, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 139 | uint32_t interval, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 140 | struct vframe * frame, |
| 141 | struct vframe * next_frame, |
| 142 | int toggle_cnt); |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 143 | static bool pattern_detect(struct av_sync_session* avsync, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 144 | int cur_period, |
| 145 | int last_period); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 146 | static void * poll_thread(void * arg); |
| 147 | static void trigger_audio_start_cb(struct av_sync_session *avsync, |
| 148 | avs_ascb_reason reason); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 149 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 150 | int av_sync_open_session(int *session_id) |
| 151 | { |
| 152 | int fd = msync_create_session(); |
| 153 | int id, rc; |
| 154 | |
| 155 | if (fd < 0) { |
| 156 | log_error("fail"); |
| 157 | return -1; |
| 158 | } |
| 159 | rc = ioctl(fd, AMSYNC_IOC_ALLOC_SESSION, &id); |
| 160 | if (rc) { |
| 161 | log_error("new session errno:%d", errno); |
| 162 | return rc; |
| 163 | } |
| 164 | *session_id = id; |
| 165 | return fd; |
| 166 | } |
| 167 | |
| 168 | void av_sync_close_session(int session) |
| 169 | { |
| 170 | msync_destory_session(session); |
| 171 | } |
| 172 | |
| 173 | static void* create_internal(int session_id, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 174 | enum sync_mode mode, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 175 | enum sync_type type, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 176 | int start_thres, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 177 | bool attach) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 178 | { |
| 179 | struct av_sync_session *avsync = NULL; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 180 | char dev_name[20]; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 181 | |
| 182 | avsync = (struct av_sync_session *)calloc(1, sizeof(*avsync)); |
| 183 | if (!avsync) { |
| 184 | log_error("OOM"); |
| 185 | return NULL; |
| 186 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 187 | |
| 188 | if (type == AV_SYNC_TYPE_VIDEO) { |
| 189 | avsync->pattern_detector = create_pattern_detector(); |
| 190 | if (!avsync->pattern_detector) { |
| 191 | log_error("pd create fail"); |
| 192 | goto err; |
| 193 | } |
| 194 | |
| 195 | if (!start_thres) |
| 196 | avsync->start_thres = DEFAULT_START_THRESHOLD; |
| 197 | else { |
| 198 | if (start_thres > 5) { |
| 199 | log_error("start_thres too big: %d", start_thres); |
| 200 | goto err2; |
| 201 | } |
| 202 | avsync->start_thres = start_thres; |
| 203 | } |
| 204 | avsync->phase_set = false; |
| 205 | avsync->first_frame_toggled = false; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 206 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 207 | |
| 208 | avsync->type = type; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 209 | avsync->state = AV_SYNC_STAT_INIT; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 210 | avsync->paused = false; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 211 | avsync->session_id = session_id; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 212 | avsync->backup_mode = mode; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 213 | avsync->last_frame = NULL; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 214 | avsync->session_started = false; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 215 | avsync->speed = 1.0f; |
| 216 | avsync->pause_pts = AV_SYNC_INVALID_PAUSE_PTS; |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 217 | avsync->vsync_interval = -1; |
| 218 | avsync->last_disc_pts = -1; |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 219 | if (msync_session_get_disc_thres(session_id, |
| 220 | &avsync->disc_thres_min, &avsync->disc_thres_max)) { |
| 221 | log_error("fail to get disc thres", dev_name, errno); |
| 222 | avsync->disc_thres_min = AV_DISC_THRES_MIN; |
| 223 | avsync->disc_thres_max = AV_DISC_THRES_MAX; |
| 224 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 225 | |
| 226 | pthread_mutex_init(&avsync->lock, NULL); |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 227 | log_info("[%d] mode %d type %d start_thres %d disc_thres %u/%u", |
| 228 | session_id, mode, type, start_thres, |
| 229 | avsync->disc_thres_min, avsync->disc_thres_max); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 230 | |
| 231 | snprintf(dev_name, sizeof(dev_name), "/dev/%s%d", SESSION_DEV, session_id); |
| 232 | avsync->fd = open(dev_name, O_RDONLY | O_CLOEXEC); |
| 233 | if (avsync->fd < 0) { |
| 234 | log_error("open %s errno %d", dev_name, errno); |
| 235 | goto err2; |
| 236 | } |
| 237 | |
| 238 | if (!attach) { |
| 239 | msync_session_set_mode(avsync->fd, mode); |
| 240 | avsync->mode = mode; |
| 241 | } else { |
| 242 | avsync->attached = true; |
| 243 | if (msync_session_get_mode(avsync->fd, &avsync->mode)) { |
| 244 | log_error("get mode"); |
| 245 | goto err2; |
| 246 | } |
| 247 | avsync->backup_mode = avsync->mode; |
| 248 | if (msync_session_get_start_policy(avsync->fd, &avsync->start_policy)) { |
| 249 | log_error("get policy"); |
| 250 | goto err2; |
| 251 | } |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 252 | if (msync_session_get_stat(avsync->fd, &avsync->active_mode, |
| 253 | NULL, NULL, NULL, &avsync->in_audio_switch)) { |
| 254 | log_error("get state"); |
| 255 | goto err2; |
| 256 | } |
| 257 | if (avsync->in_audio_switch) { |
| 258 | log_info("audio_switch_state reseted the audio"); |
| 259 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_RESET; |
| 260 | } |
| 261 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 262 | log_info("[%d]retrieve sync mode %d policy %d", |
| 263 | session_id, avsync->mode, avsync->start_policy); |
| 264 | } |
| 265 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 266 | return avsync; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 267 | err2: |
| 268 | destroy_pattern_detector(avsync->pattern_detector); |
| 269 | err: |
| 270 | free(avsync); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | void* av_sync_create(int session_id, |
| 275 | enum sync_mode mode, |
| 276 | enum sync_type type, |
| 277 | int start_thres) |
| 278 | { |
| 279 | return create_internal(session_id, mode, |
| 280 | type, start_thres, false); |
| 281 | } |
| 282 | |
| 283 | void* av_sync_attach(int session_id, enum sync_type type) |
| 284 | { |
| 285 | return create_internal(session_id, AV_SYNC_MODE_MAX, |
| 286 | type, 0, true); |
| 287 | } |
| 288 | |
| 289 | int av_sync_video_config(void *sync, struct video_config* config) |
| 290 | { |
| 291 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 292 | |
| 293 | if (!avsync || !config) |
| 294 | return -1; |
| 295 | |
| 296 | if (config->delay != 1 && config->delay != 2) { |
| 297 | log_error("invalid delay: %d\n", config->delay); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | avsync->delay = config->delay; |
| 302 | |
| 303 | log_info("[%d] delay: %d", |
| 304 | avsync->session_id, config->delay); |
| 305 | return 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static int internal_stop(struct av_sync_session *avsync) |
| 309 | { |
| 310 | int ret = 0; |
| 311 | struct vframe *frame; |
| 312 | |
| 313 | pthread_mutex_lock(&avsync->lock); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 314 | while (!dqueue_item(avsync->frame_q, (void **)&frame)) { |
| 315 | frame->free(frame); |
| 316 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 317 | avsync->state = AV_SYNC_STAT_INIT; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 318 | pthread_mutex_unlock(&avsync->lock); |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | /* destroy and detach from kernel session */ |
| 323 | void av_sync_destroy(void *sync) |
| 324 | { |
| 325 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 326 | |
| 327 | if (!avsync) |
| 328 | return; |
| 329 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 330 | log_info("[%d]begin", avsync->session_id); |
| 331 | if (avsync->state != AV_SYNC_STAT_INIT) { |
| 332 | if (avsync->type == AV_SYNC_TYPE_VIDEO) |
| 333 | internal_stop(avsync); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 334 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 335 | avsync->quit_poll = true; |
| 336 | if (avsync->poll_thread) { |
| 337 | pthread_join(avsync->poll_thread, NULL); |
| 338 | avsync->poll_thread = 0; |
| 339 | } |
| 340 | trigger_audio_start_cb(avsync, AV_SYNC_ASCB_STOP); |
Song Zhao | af368d5 | 2021-02-17 17:53:45 -0800 | [diff] [blame] | 341 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 342 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 343 | if (avsync->session_started) { |
| 344 | if (avsync->type == AV_SYNC_TYPE_VIDEO) |
| 345 | msync_session_set_video_stop(avsync->fd); |
| 346 | else |
| 347 | msync_session_set_audio_stop(avsync->fd); |
| 348 | } |
| 349 | close(avsync->fd); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 350 | pthread_mutex_destroy(&avsync->lock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 351 | if (avsync->type == AV_SYNC_TYPE_VIDEO) { |
| 352 | destroy_q(avsync->frame_q); |
| 353 | destroy_pattern_detector(avsync->pattern_detector); |
| 354 | } |
| 355 | log_info("[%d]done", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 356 | free(avsync); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | int avs_sync_set_start_policy(void *sync, enum sync_start_policy policy) |
| 360 | { |
| 361 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 362 | |
| 363 | if (!avsync || !avsync->fd) |
| 364 | return -1; |
| 365 | |
| 366 | log_info("[%d]policy %u --> %u", avsync->start_policy, policy); |
| 367 | avsync->start_policy = policy; |
| 368 | /* v_peek will be handled by libamlavsync */ |
| 369 | if (policy != AV_SYNC_START_NONE && |
| 370 | policy != AV_SYNC_START_V_PEEK) |
| 371 | return msync_session_set_start_policy(avsync->fd, policy); |
| 372 | |
| 373 | return 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | int av_sync_pause(void *sync, bool pause) |
| 377 | { |
| 378 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 379 | bool v_active, a_active, v_timeout; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 380 | int rc; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 381 | |
| 382 | if (!avsync) |
| 383 | return -1; |
| 384 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 385 | if (avsync->mode == AV_SYNC_MODE_PCR_MASTER) |
| 386 | return -1; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 387 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 388 | rc = msync_session_get_stat(avsync->fd, &avsync->active_mode, |
| 389 | &v_active, &a_active, &v_timeout, |
| 390 | &avsync->in_audio_switch); |
| 391 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 392 | /* ignore */ |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 393 | if (avsync->mode == AV_SYNC_MODE_AMASTER |
| 394 | && avsync->type == AV_SYNC_TYPE_VIDEO |
| 395 | && a_active && !avsync->in_audio_switch) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 396 | return 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 397 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 398 | if (avsync->in_audio_switch && avsync->type == AV_SYNC_TYPE_AUDIO) { |
| 399 | log_info("[%d] ignore the pause from audio", avsync->session_id); |
| 400 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_RESET; |
| 401 | return 0; |
| 402 | } |
| 403 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 404 | rc = msync_session_set_pause(avsync->fd, pause); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 405 | avsync->paused = pause; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 406 | log_info("[%d]paused:%d type:%d rc %d", |
| 407 | avsync->session_id, pause, avsync->type, rc); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 408 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 409 | return rc; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | int av_sync_push_frame(void *sync , struct vframe *frame) |
| 413 | { |
| 414 | int ret; |
| 415 | struct vframe *prev; |
| 416 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 417 | |
| 418 | if (!avsync) |
| 419 | return -1; |
| 420 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 421 | if (!avsync->frame_q) { |
| 422 | /* policy should be final now */ |
| 423 | if (msync_session_get_start_policy(avsync->fd, &avsync->start_policy)) { |
| 424 | log_error("[%d]get policy", avsync->session_id); |
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | avsync->frame_q = create_q(MAX_FRAME_NUM); |
| 429 | if (!avsync->frame_q) { |
| 430 | log_error("[%d]create queue fail", avsync->session_id); |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | if (avsync->mode == AV_SYNC_MODE_PCR_MASTER || |
| 435 | avsync->mode == AV_SYNC_MODE_IPTV) { |
| 436 | int ret; |
| 437 | |
| 438 | ret = pthread_create(&avsync->poll_thread, NULL, poll_thread, avsync); |
| 439 | if (ret) { |
| 440 | log_error("[%d]create poll thread errno %d", avsync->session_id, errno); |
| 441 | destroy_q(avsync->frame_q); |
| 442 | return -1; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 447 | if (!peek_item(avsync->frame_q, (void **)&prev, 0)) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 448 | if (prev->pts == frame->pts && avsync->mode == AV_SYNC_MODE_AMASTER) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 449 | dqueue_item(avsync->frame_q, (void **)&prev); |
| 450 | prev->free(prev); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 451 | log_info ("[%d]drop frame with same pts %u", avsync->session_id, frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 455 | if (frame->duration == -1) |
| 456 | frame->duration = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 457 | frame->hold_period = 0; |
| 458 | ret = queue_item(avsync->frame_q, frame); |
| 459 | if (avsync->state == AV_SYNC_STAT_INIT && |
| 460 | queue_size(avsync->frame_q) >= avsync->start_thres) { |
| 461 | avsync->state = AV_SYNC_STAT_RUNNING; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 462 | log_info("[%d]state: init --> running", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (ret) |
| 466 | log_error("%s queue fail:%d", ret); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 467 | log_debug("[%d]push %u", avsync->session_id, frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 468 | return ret; |
| 469 | |
| 470 | } |
| 471 | |
| 472 | struct vframe *av_sync_pop_frame(void *sync) |
| 473 | { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 474 | struct vframe *frame = NULL, *enter_last_frame = NULL; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 475 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 476 | int toggle_cnt = 0; |
| 477 | uint32_t systime; |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 478 | bool pause_pts_reached = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 479 | uint32_t interval; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 480 | |
| 481 | pthread_mutex_lock(&avsync->lock); |
| 482 | if (avsync->state == AV_SYNC_STAT_INIT) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 483 | log_info("[%d]in state INIT", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 484 | goto exit; |
| 485 | } |
| 486 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 487 | if (!avsync->session_started) { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 488 | uint32_t pts; |
| 489 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 490 | if (peek_item(avsync->frame_q, (void **)&frame, 0) || !frame) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 491 | log_info("[%d]empty q", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 492 | goto exit; |
| 493 | } |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 494 | msync_session_get_wall(avsync->fd, &systime, &interval); |
| 495 | pts = frame->pts - avsync->delay * interval; |
| 496 | msync_session_set_video_start(avsync->fd, pts); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 497 | avsync->session_started = true; |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 498 | log_info("[%d]video start %u", avsync->session_id, pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 501 | if (avsync->start_policy == AV_SYNC_START_ALIGN && |
Song Zhao | d4109b6 | 2021-04-30 08:47:17 -0700 | [diff] [blame] | 502 | !avsync->first_frame_toggled && |
| 503 | !msync_clock_started(avsync->fd)) { |
| 504 | pthread_mutex_unlock(&avsync->lock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 505 | log_trace("[%d]clock not started", avsync->session_id); |
| 506 | return NULL; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 509 | enter_last_frame = avsync->last_frame; |
| 510 | msync_session_get_wall(avsync->fd, &systime, &interval); |
| 511 | |
| 512 | /* handle refresh rate change */ |
| 513 | if (avsync->vsync_interval == AV_SYNC_INVALID_PAUSE_PTS || |
| 514 | avsync->vsync_interval != interval) { |
| 515 | log_info("[%d]vsync interval update %d --> %u", |
| 516 | avsync->session_id, avsync->vsync_interval, interval); |
| 517 | avsync->vsync_interval = interval; |
| 518 | avsync->phase_set = false; |
| 519 | reset_pattern(avsync->pattern_detector); |
| 520 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 521 | while (!peek_item(avsync->frame_q, (void **)&frame, 0)) { |
| 522 | struct vframe *next_frame = NULL; |
| 523 | |
| 524 | peek_item(avsync->frame_q, (void **)&next_frame, 1); |
| 525 | if (next_frame) |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 526 | log_debug("[%d]cur_f %u next_f %u size %d", |
| 527 | avsync->session_id, frame->pts, next_frame->pts, queue_size(avsync->frame_q)); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 528 | if (frame_expire(avsync, systime, interval, |
| 529 | frame, next_frame, toggle_cnt)) { |
| 530 | log_debug("[%d]cur_f %u expire", avsync->session_id, frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 531 | toggle_cnt++; |
| 532 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 533 | if (pattern_detect(avsync, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 534 | (avsync->last_frame?avsync->last_frame->hold_period:0), |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 535 | avsync->last_holding_peroid)) |
| 536 | log_info("[%d] %u break the pattern", avsync->session_id, avsync->last_frame->pts); |
| 537 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 538 | if (avsync->last_frame) |
| 539 | avsync->last_holding_peroid = avsync->last_frame->hold_period; |
| 540 | |
| 541 | dqueue_item(avsync->frame_q, (void **)&frame); |
| 542 | if (avsync->last_frame) { |
| 543 | /* free frame that are not for display */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 544 | if (toggle_cnt > 1) { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 545 | log_debug("[%d]free %u cur %u system/d %u/%u", avsync->session_id, |
| 546 | avsync->last_frame->pts, frame->pts, systime, systime - avsync->last_poptime); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 547 | avsync->last_frame->free(avsync->last_frame); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 548 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 549 | } else { |
| 550 | avsync->first_frame_toggled = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 551 | log_info("[%d]first frame %u", avsync->session_id, frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 552 | } |
| 553 | avsync->last_frame = frame; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 554 | avsync->last_pts = frame->pts; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 555 | } else |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | /* pause pts */ |
| 560 | if (avsync->pause_pts != AV_SYNC_INVALID_PAUSE_PTS && avsync->last_frame) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 561 | if (avsync->pause_pts == AV_SYNC_STEP_PAUSE_PTS) |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 562 | pause_pts_reached = true; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 563 | else |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 564 | pause_pts_reached = (int)(avsync->last_frame->pts - avsync->pause_pts) >= 0; |
| 565 | } else if (avsync->pause_pts != AV_SYNC_INVALID_PAUSE_PTS) { |
| 566 | if (!peek_item(avsync->frame_q, (void **)&frame, 0)) |
| 567 | pause_pts_reached = (int)(frame->pts - avsync->pause_pts) >= 0; |
| 568 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 569 | |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 570 | if (pause_pts_reached) { |
| 571 | if (avsync->pause_pts_cb) |
| 572 | avsync->pause_pts_cb(avsync->pause_pts, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 573 | avsync->pause_cb_priv); |
| 574 | |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 575 | /* stay in paused until av_sync_pause(false) */ |
| 576 | avsync->paused = true; |
| 577 | avsync->pause_pts = AV_SYNC_INVALID_PAUSE_PTS; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 578 | log_info ("[%d]reach pause pts: %u", |
| 579 | avsync->session_id, avsync->last_frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | exit: |
| 583 | pthread_mutex_unlock(&avsync->lock); |
| 584 | if (avsync->last_frame) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 585 | if (enter_last_frame != avsync->last_frame) |
| 586 | log_debug("[%d]pop %u", avsync->session_id, avsync->last_frame->pts); |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 587 | log_trace("[%d]pop %u", avsync->session_id, avsync->last_frame->pts); |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 588 | /* don't update vpts for out_lier */ |
| 589 | if (avsync->last_frame->duration != -1) |
| 590 | msync_session_update_vpts(avsync->fd, systime, |
| 591 | avsync->last_frame->pts, interval * avsync->delay); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 592 | } else |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 593 | if (enter_last_frame != avsync->last_frame) |
| 594 | log_debug("[%d]pop (nil)", avsync->session_id); |
| 595 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 596 | avsync->last_poptime = systime; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 597 | if (avsync->last_frame) |
| 598 | avsync->last_frame->hold_period++; |
| 599 | return avsync->last_frame; |
| 600 | } |
| 601 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 602 | static inline uint32_t abs_diff(uint32_t a, uint32_t b) |
| 603 | { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 604 | return (int)(a - b) > 0 ? a - b : b - a; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static uint64_t time_diff (struct timeval *b, struct timeval *a) |
| 608 | { |
| 609 | return (b->tv_sec - a->tv_sec)*1000000 + (b->tv_usec - a->tv_usec); |
| 610 | } |
| 611 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 612 | static bool frame_expire(struct av_sync_session* avsync, |
| 613 | uint32_t systime, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 614 | uint32_t interval, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 615 | struct vframe * frame, |
| 616 | struct vframe * next_frame, |
| 617 | int toggle_cnt) |
| 618 | { |
| 619 | uint32_t fpts = frame->pts; |
| 620 | bool expire = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 621 | uint32_t pts_correction = avsync->delay * interval; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 622 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 623 | if (avsync->mode == AV_SYNC_MODE_FREE_RUN) |
| 624 | return true; |
| 625 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 626 | if (avsync->paused && avsync->pause_pts == AV_SYNC_INVALID_PAUSE_PTS) |
| 627 | return false; |
| 628 | |
| 629 | if (avsync->pause_pts == AV_SYNC_STEP_PAUSE_PTS) |
| 630 | return true; |
| 631 | |
| 632 | if (!fpts) { |
| 633 | if (avsync->last_frame) { |
| 634 | /* try to accumulate duration as PTS */ |
| 635 | fpts = avsync->vpts + avsync->last_frame->duration; |
| 636 | } else { |
| 637 | fpts = avsync->vpts; |
| 638 | } |
| 639 | } |
| 640 | systime += pts_correction; |
| 641 | |
| 642 | /* phase adjustment */ |
| 643 | if (avsync->phase_set) |
| 644 | systime += avsync->phase; |
| 645 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 646 | log_trace("[%d]systime:%u phase:%u correct:%u fpts:%u", |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 647 | avsync->session_id, systime, |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 648 | avsync->phase_set?avsync->phase:0, pts_correction, fpts); |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 649 | if (abs_diff(systime, fpts) > avsync->disc_thres_min && |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 650 | avsync->first_frame_toggled) { |
| 651 | /* ignore discontinity under pause */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 652 | if (avsync->paused) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 653 | return false; |
| 654 | |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 655 | if (avsync->last_systime != systime || avsync->last_pts != fpts) { |
| 656 | struct timeval now; |
| 657 | |
| 658 | gettimeofday(&now, NULL); |
| 659 | avsync->last_systime = systime; |
| 660 | avsync->last_pts = fpts; |
| 661 | if (time_diff(&now, &avsync->sync_lost_print_time) >= |
| 662 | SYNC_LOST_PRINT_THRESHOLD) { |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 663 | log_warn("[%d]sync lost systime:%u fpts:%u lost:%u", |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 664 | avsync->session_id, systime, fpts, avsync->sync_lost_cnt); |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 665 | avsync->sync_lost_cnt = 0; |
Song Zhao | bc6161d | 2021-03-08 09:59:33 -0800 | [diff] [blame] | 666 | gettimeofday(&avsync->sync_lost_print_time, NULL); |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 667 | } else |
| 668 | avsync->sync_lost_cnt++; |
| 669 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 670 | |
| 671 | if (avsync->state == AV_SYNC_STAT_SYNC_SETUP && |
| 672 | LIVE_MODE(avsync->mode) && |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 673 | abs_diff(avsync->last_pts, fpts) > STREAM_DISC_THRES) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 674 | /* outlier by stream error */ |
| 675 | avsync->outlier_cnt++; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 676 | frame->duration = -1; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 677 | if (avsync->outlier_cnt < OUTLIER_MAX_CNT) { |
| 678 | log_info("render outlier %u", fpts); |
| 679 | return true; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | avsync->outlier_cnt = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 684 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
| 685 | avsync->phase_set = false; |
Song Zhao | a58c3e9 | 2021-03-09 18:52:55 -0800 | [diff] [blame] | 686 | reset_pattern(avsync->pattern_detector); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 687 | if ((int)(systime - fpts) > 0) { |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 688 | if (LIVE_MODE(avsync->mode) && avsync->last_disc_pts != fpts) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 689 | log_info ("[%d]video disc %u --> %u", |
| 690 | avsync->session_id, systime, fpts); |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 691 | msync_session_set_video_dis(avsync->fd, fpts); |
| 692 | avsync->last_disc_pts = fpts; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 693 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 694 | /* catch up PCR */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 695 | return true; |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 696 | } else if (LIVE_MODE(avsync->mode) && avsync->last_disc_pts != fpts) { |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 697 | /* vpts wrapping or vpts gapping */ |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 698 | log_info ("[%d]video disc %u --> %u", |
| 699 | avsync->session_id, systime, fpts); |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 700 | msync_session_set_video_dis(avsync->fd, fpts); |
| 701 | avsync->last_disc_pts = fpts; |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 702 | /* clean up frames for wrapping case */ |
| 703 | if ((int)(fpts - systime) > avsync->disc_thres_max) |
| 704 | return true; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
Song Zhao | 52f5519 | 2021-05-06 10:52:21 -0700 | [diff] [blame] | 708 | /* In some cases, keeping pattern will enlarge the gap */ |
| 709 | if (abs_diff(systime, fpts) > AV_PATTERN_RESET_THRES && |
| 710 | avsync->first_frame_toggled) { |
| 711 | reset_pattern(avsync->pattern_detector); |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 712 | log_warn("sync pattern reset sys:%u fpts:%u", |
Song Zhao | 52f5519 | 2021-05-06 10:52:21 -0700 | [diff] [blame] | 713 | systime, fpts); |
| 714 | } |
| 715 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 716 | expire = (int)(systime - fpts) >= 0; |
| 717 | |
| 718 | /* scatter the frame in different vsync whenever possible */ |
| 719 | if (expire && next_frame && next_frame->pts && toggle_cnt) { |
| 720 | /* multi frame expired in current vsync but no frame in next vsync */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 721 | if (systime + interval < next_frame->pts) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 722 | expire = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 723 | log_debug("[%d]unset expire systime:%d inter:%d next_pts:%d toggle_cnt:%d", |
| 724 | avsync->session_id, systime, interval, next_frame->pts, toggle_cnt); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 725 | } |
| 726 | } else if (!expire && next_frame && next_frame->pts && !toggle_cnt |
| 727 | && avsync->first_frame_toggled) { |
| 728 | /* next vsync will have at least 2 frame expired */ |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 729 | if (systime + interval >= next_frame->pts) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 730 | expire = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 731 | log_debug("[%d]set expire systime:%d inter:%d next_pts:%d", |
| 732 | avsync->session_id, systime, interval, next_frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
Song Zhao | a58c3e9 | 2021-03-09 18:52:55 -0800 | [diff] [blame] | 736 | if (avsync->state == AV_SYNC_STAT_SYNC_SETUP) |
| 737 | correct_pattern(avsync->pattern_detector, frame, next_frame, |
| 738 | (avsync->last_frame?avsync->last_frame->hold_period:0), |
| 739 | avsync->last_holding_peroid, systime, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 740 | interval, &expire); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 741 | |
| 742 | if (expire) { |
| 743 | avsync->vpts = fpts; |
| 744 | /* phase adjustment */ |
| 745 | if (!avsync->phase_set) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 746 | uint32_t phase_thres = interval / 4; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 747 | if ( systime > fpts && (systime - fpts) < phase_thres) { |
| 748 | /* too aligned to current VSYNC, separate them to 1/4 VSYNC */ |
| 749 | avsync->phase += phase_thres - (systime - fpts); |
| 750 | avsync->phase_set = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 751 | log_info("[%d]adjust phase to %d", avsync->session_id, avsync->phase); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 752 | } |
| 753 | if (!avsync->phase_set && systime > fpts && |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 754 | systime < (fpts + interval) && |
| 755 | (systime - fpts) > interval - phase_thres) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 756 | /* too aligned to previous VSYNC, separate them to 1/4 VSYNC */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 757 | avsync->phase += phase_thres + fpts + interval - systime; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 758 | avsync->phase_set = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 759 | log_info("[%d]adjust phase to %d", avsync->session_id, avsync->phase); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | if (avsync->state != AV_SYNC_STAT_SYNC_SETUP) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 764 | log_info("[%d]sync setup", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 765 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 766 | avsync->sync_lost_cnt = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 767 | } |
| 768 | return expire; |
| 769 | } |
| 770 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 771 | static bool pattern_detect(struct av_sync_session* avsync, int cur_period, int last_period) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 772 | { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 773 | bool ret = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 774 | log_trace("[%d]cur_period: %d last_period: %d", |
| 775 | avsync->session_id, cur_period, last_period); |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 776 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P32, cur_period, last_period)) |
| 777 | ret = true; |
| 778 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P22, cur_period, last_period)) |
| 779 | ret = true; |
| 780 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P41, cur_period, last_period)) |
| 781 | ret = true; |
| 782 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P11, cur_period, last_period)) |
| 783 | ret = true; |
| 784 | |
| 785 | return ret; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | int av_sync_set_speed(void *sync, float speed) |
| 789 | { |
| 790 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 791 | |
| 792 | if (speed < 0.001f || speed > 100) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 793 | log_error("[%d]wrong speed %f [0.0001, 100]", avsync->session_id, speed); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 794 | return -1; |
| 795 | } |
| 796 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 797 | if (avsync->mode == AV_SYNC_MODE_PCR_MASTER || |
| 798 | avsync->mode == AV_SYNC_MODE_IPTV) { |
| 799 | log_info("[%d]ignore set speed in mode %d", avsync->session_id, avsync->mode); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 800 | return 0; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 801 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 802 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 803 | avsync->speed = speed; |
| 804 | |
| 805 | if (avsync->type == AV_SYNC_TYPE_AUDIO) { |
| 806 | if (speed == 1.0) { |
| 807 | avsync->mode = avsync->backup_mode; |
| 808 | log_info("[%d]audio back to mode %d", avsync->session_id, avsync->mode); |
| 809 | } else { |
| 810 | avsync->backup_mode = avsync->mode; |
| 811 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 812 | log_info("[%d]audio to freerun mode", avsync->session_id); |
| 813 | } |
| 814 | } |
| 815 | #if 0 |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 816 | if (avsync->mode != AV_SYNC_MODE_VMASTER) { |
| 817 | log_info("ignore set speed in mode %d", avsync->mode); |
| 818 | return 0; |
| 819 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 820 | #endif |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 821 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 822 | log_info("session[%d] set rate to %f", avsync->session_id, speed); |
| 823 | return msync_session_set_rate(avsync->fd, speed); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | int av_sync_change_mode(void *sync, enum sync_mode mode) |
| 827 | { |
| 828 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 829 | |
| 830 | if (!avsync) |
| 831 | return -1; |
| 832 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 833 | if (msync_session_set_mode(avsync->fd, mode)) { |
| 834 | log_error("[%d]fail to set mode %d", avsync->session_id, mode); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 835 | return -1; |
| 836 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 837 | avsync->mode = mode; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 838 | log_info("[%d]update sync mode to %d", avsync->session_id, mode); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Song Zhao | 01031bb | 2021-05-13 21:23:20 -0700 | [diff] [blame] | 842 | int av_sync_get_mode(void *sync, enum sync_mode *mode) |
| 843 | { |
| 844 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 845 | |
| 846 | if (!avsync || !mode) |
| 847 | return -1; |
| 848 | |
| 849 | *mode = avsync->mode; |
| 850 | return 0; |
| 851 | } |
| 852 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 853 | int av_sync_set_pause_pts(void *sync, pts90K pts) |
| 854 | { |
| 855 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 856 | |
| 857 | if (!avsync) |
| 858 | return -1; |
| 859 | |
| 860 | avsync->pause_pts = pts; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 861 | log_info("[%d]set pause pts: %u", avsync->session_id, pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | int av_sync_set_pause_pts_cb(void *sync, pause_pts_done cb, void *priv) |
| 866 | { |
| 867 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 868 | |
| 869 | if (!avsync) |
| 870 | return -1; |
| 871 | |
| 872 | avsync->pause_pts_cb = cb; |
| 873 | avsync->pause_cb_priv = priv; |
| 874 | return 0; |
| 875 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 876 | |
| 877 | static void trigger_audio_start_cb(struct av_sync_session *avsync, |
| 878 | avs_ascb_reason reason) |
| 879 | { |
| 880 | if (avsync) { |
| 881 | pthread_mutex_lock(&avsync->lock); |
| 882 | if (avsync->audio_start) { |
| 883 | avsync->audio_start(avsync->audio_start_priv, reason); |
| 884 | avsync->session_started = true; |
| 885 | avsync->audio_start = NULL; |
| 886 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
| 887 | } |
| 888 | pthread_mutex_unlock(&avsync->lock); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | avs_start_ret av_sync_audio_start( |
| 893 | void *sync, |
| 894 | pts90K pts, |
| 895 | pts90K delay, |
| 896 | audio_start_cb cb, |
| 897 | void *priv) |
| 898 | { |
| 899 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 900 | uint32_t start_mode; |
| 901 | avs_start_ret ret = AV_SYNC_ASTART_ERR; |
| 902 | bool create_poll_t = false; |
| 903 | |
| 904 | if (!avsync) |
| 905 | return ret; |
| 906 | |
| 907 | if (msync_session_set_audio_start(avsync->fd, pts, delay, &start_mode)) |
| 908 | log_error("[%d]fail to set audio start", avsync->session_id); |
| 909 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 910 | if (avsync->in_audio_switch |
| 911 | && avsync->audio_switch_state == AUDIO_SWITCH_STAT_RESET) { |
| 912 | log_info("%d audio_switch_state to start start mode %d", |
| 913 | avsync->session_id, start_mode); |
| 914 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_START; |
| 915 | } |
| 916 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 917 | if (start_mode == AVS_START_SYNC) { |
| 918 | ret = AV_SYNC_ASTART_SYNC; |
| 919 | avsync->session_started = true; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 920 | avsync->state = AV_SYNC_STAT_RUNNING; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 921 | } else if (start_mode == AVS_START_ASYNC) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 922 | ret = AV_SYNC_ASTART_ASYNC; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 923 | avsync->state = AV_SYNC_STAT_RUNNING; |
| 924 | } else if (start_mode == AVS_START_AGAIN) { |
| 925 | ret = AV_SYNC_ASTART_AGAIN; |
| 926 | } |
| 927 | |
| 928 | if (ret == AV_SYNC_ASTART_AGAIN) |
| 929 | goto exit; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 930 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 931 | if (avsync->mode == AV_SYNC_MODE_AMASTER || avsync->in_audio_switch) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 932 | create_poll_t = true; |
| 933 | if (start_mode == AVS_START_ASYNC) { |
| 934 | if (!cb) { |
| 935 | log_error("[%d]invalid cb", avsync->session_id); |
| 936 | return AV_SYNC_ASTART_ERR; |
| 937 | } |
| 938 | avsync->audio_start = cb; |
| 939 | avsync->audio_start_priv = priv; |
| 940 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 941 | } else if (LIVE_MODE(avsync->mode)) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 942 | create_poll_t = true; |
| 943 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 944 | if (create_poll_t && !avsync->poll_thread) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 945 | int ret; |
| 946 | |
| 947 | log_info("[%d]start poll thread", avsync->session_id); |
| 948 | avsync->quit_poll = false; |
| 949 | ret = pthread_create(&avsync->poll_thread, NULL, poll_thread, avsync); |
| 950 | if (ret) { |
| 951 | log_error("[%d]create poll thread errno %d", avsync->session_id, errno); |
| 952 | return AV_SYNC_ASTART_ERR; |
| 953 | } |
| 954 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 955 | if (LIVE_MODE(avsync->mode)) { |
| 956 | uint32_t systime; |
| 957 | msync_session_get_wall(avsync->fd, &systime, NULL); |
| 958 | log_info("[%d]return %u w %u pts %u d %u", |
| 959 | avsync->session_id, ret, systime, pts, delay); |
| 960 | } |
| 961 | exit: |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 962 | log_info("[%d]return %u", avsync->session_id, ret); |
| 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | int av_sync_audio_render( |
| 967 | void *sync, |
| 968 | pts90K pts, |
| 969 | struct audio_policy *policy) |
| 970 | { |
| 971 | int ret = 0; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 972 | bool out_lier = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 973 | uint32_t systime; |
| 974 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 975 | avs_audio_action action = AA_SYNC_AA_MAX; |
| 976 | |
| 977 | if (!avsync || !policy) |
| 978 | return -1; |
| 979 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 980 | msync_session_get_wall(avsync->fd, &systime, NULL); |
| 981 | |
| 982 | log_trace("audio render pts %u, systime %u, mode %u diff ms %d", |
| 983 | pts, systime, avsync->mode, (int)(pts-systime)/90); |
| 984 | |
| 985 | if (avsync->in_audio_switch |
| 986 | && avsync->audio_switch_state == AUDIO_SWITCH_STAT_START) { |
| 987 | if (abs_diff(systime, pts) < A_ADJ_THREDHOLD_HB) { |
| 988 | log_info("Audio pts in system range sys %u pts %u\n", systime, pts); |
| 989 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_FINISH; |
| 990 | action = AV_SYNC_AA_RENDER; |
| 991 | } else if ((int)(systime - pts) > 0) { |
| 992 | log_info("[%d] audio change drop %d ms sys %u pts %u", avsync->session_id, |
| 993 | (int)(systime - pts)/90, systime, pts); |
| 994 | action = AV_SYNC_AA_DROP; |
| 995 | } else { |
| 996 | action = AV_SYNC_AA_INSERT; |
| 997 | log_info("[%d] audio change insert %d ms sys %u pts %u", avsync->session_id, |
| 998 | (int)(pts - systime)/90, systime, pts); |
| 999 | } |
| 1000 | goto done; |
| 1001 | } |
| 1002 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1003 | if (avsync->mode == AV_SYNC_MODE_FREE_RUN || |
| 1004 | avsync->mode == AV_SYNC_MODE_AMASTER) { |
| 1005 | action = AV_SYNC_AA_RENDER; |
| 1006 | goto done; |
| 1007 | } |
| 1008 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1009 | /* stopping procedure, unblock audio rendering */ |
| 1010 | if (avsync->mode == AV_SYNC_MODE_PCR_MASTER && |
| 1011 | avsync->active_mode == AV_SYNC_MODE_FREE_RUN) { |
| 1012 | action = AV_SYNC_AA_DROP; |
| 1013 | goto done; |
| 1014 | } |
| 1015 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1016 | |
Song Zhao | 7daf3a1 | 2021-05-10 22:22:25 -0700 | [diff] [blame] | 1017 | if (avsync->mode == AV_SYNC_MODE_FREE_RUN || |
| 1018 | avsync->mode == AV_SYNC_MODE_AMASTER) { |
| 1019 | action = AV_SYNC_AA_RENDER; |
| 1020 | goto done; |
| 1021 | } |
| 1022 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1023 | if (avsync->state == AV_SYNC_STAT_SYNC_SETUP && |
| 1024 | LIVE_MODE(avsync->mode) && |
| 1025 | abs_diff(systime, pts) > STREAM_DISC_THRES) { |
| 1026 | /* outlier by stream error */ |
| 1027 | avsync->outlier_cnt++; |
| 1028 | if (avsync->outlier_cnt > OUTLIER_MAX_CNT) { |
| 1029 | /* treat as disc, just drop current frame */ |
| 1030 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
| 1031 | avsync->outlier_cnt = 0; |
| 1032 | action = AV_SYNC_AA_DROP; |
| 1033 | systime = pts; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1034 | goto done; |
| 1035 | } |
| 1036 | log_info("[%d]ignore outlier %u", avsync->session_id, pts); |
| 1037 | pts = systime; |
| 1038 | action = AV_SYNC_AA_RENDER; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 1039 | out_lier = true; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1040 | goto done; |
| 1041 | } |
| 1042 | |
| 1043 | avsync->outlier_cnt = 0; |
| 1044 | /* low bound from sync_lost to sync_setup */ |
| 1045 | if (abs_diff(systime, pts) < A_ADJ_THREDHOLD_LB) { |
| 1046 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
| 1047 | action = AV_SYNC_AA_RENDER; |
| 1048 | goto done; |
| 1049 | } |
| 1050 | |
| 1051 | /* high bound of sync_setup */ |
| 1052 | if (abs_diff(systime, pts) < A_ADJ_THREDHOLD_HB && |
| 1053 | avsync->state != AV_SYNC_STAT_SYNC_LOST) { |
| 1054 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1055 | action = AV_SYNC_AA_RENDER; |
| 1056 | goto done; |
| 1057 | } |
| 1058 | |
| 1059 | if ((int)(systime - pts) > 0) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1060 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1061 | action = AV_SYNC_AA_DROP; |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
| 1065 | if ((int)(systime - pts) < 0) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1066 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1067 | action = AV_SYNC_AA_INSERT; |
| 1068 | goto done; |
| 1069 | } |
| 1070 | |
| 1071 | done: |
| 1072 | policy->action = action; |
| 1073 | policy->delta = (int)(systime - pts); |
| 1074 | if (action == AV_SYNC_AA_RENDER) { |
| 1075 | avsync->apts = pts; |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1076 | if (!avsync->in_audio_switch) { |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 1077 | if (!out_lier) |
| 1078 | msync_session_update_apts(avsync->fd, systime, pts, 0); |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 1079 | log_debug("[%d]return %d sys %u - pts %u = %d", |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 1080 | avsync->session_id, action, systime, pts, systime - pts); |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1081 | } else if(avsync->audio_switch_state == AUDIO_SWITCH_STAT_FINISH) { |
| 1082 | msync_session_update_apts(avsync->fd, systime, pts, 0); |
| 1083 | log_info("[%d] audio switch done sys %u pts %u", |
| 1084 | avsync->session_id, systime, pts); |
| 1085 | msync_session_set_audio_switch(avsync->fd, false); |
| 1086 | avsync->in_audio_switch = false; |
| 1087 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_INIT; |
| 1088 | } else { |
| 1089 | log_trace("[%d] in audio switch ret %d sys %u - pts %u = %d", |
| 1090 | avsync->session_id, action, systime, pts, systime - pts); |
| 1091 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1092 | } else { |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 1093 | if (abs_diff(systime, pts) > AV_DISC_THRES_MIN && |
yongchun.li | 085b5a4 | 2021-05-24 04:22:53 -0700 | [diff] [blame] | 1094 | avsync->last_disc_pts != pts && |
| 1095 | !avsync->in_audio_switch) { |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 1096 | log_info ("[%d]audio disc %u --> %u", |
| 1097 | avsync->session_id, systime, pts); |
| 1098 | msync_session_set_audio_dis(avsync->fd, pts); |
| 1099 | avsync->last_disc_pts = pts; |
| 1100 | } |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 1101 | log_debug("[%d]return %d sys %u - pts %u = %d", |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1102 | avsync->session_id, action, systime, pts, systime - pts); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | int av_sync_get_clock(void *sync, pts90K *pts) |
| 1109 | { |
| 1110 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1111 | |
| 1112 | if (!avsync || !pts) |
| 1113 | return -1; |
| 1114 | return msync_session_get_wall(avsync->fd, pts, NULL); |
| 1115 | } |
| 1116 | |
| 1117 | static void handle_mode_change_a(struct av_sync_session* avsync, |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1118 | bool v_active, bool a_active, bool v_timeout) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1119 | { |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1120 | log_info("[%d]amode %d mode %d v/a/vt %d/%d/%d", avsync->session_id, |
| 1121 | avsync->active_mode, avsync->mode, v_active, a_active, v_timeout); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1122 | if (avsync->active_mode == AV_SYNC_MODE_AMASTER) { |
| 1123 | float speed; |
| 1124 | if (avsync->start_policy == AV_SYNC_START_ALIGN && |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1125 | a_active && avsync->audio_start) { |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1126 | if (v_active || v_timeout || avsync->in_audio_switch) { |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1127 | log_info("audio start cb"); |
| 1128 | trigger_audio_start_cb(avsync, AV_SYNC_ASCB_OK); |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1129 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | if (!msync_session_get_rate(avsync->fd, &speed)) { |
| 1133 | /* speed change is triggered by asink, |
| 1134 | * attached audio HAL will handle it |
| 1135 | */ |
| 1136 | if (speed != avsync->speed) |
| 1137 | log_info("[%d]new rate %f", avsync->session_id, speed); |
| 1138 | if (speed == 1.0) { |
| 1139 | avsync->mode = avsync->backup_mode; |
| 1140 | log_info("[%d]audio back to mode %d", avsync->session_id, avsync->mode); |
| 1141 | } else { |
| 1142 | avsync->backup_mode = avsync->mode; |
| 1143 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 1144 | log_info("[%d]audio to freerun mode", avsync->session_id); |
| 1145 | } |
| 1146 | avsync->speed = speed; |
| 1147 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1148 | } else if (avsync->active_mode == AV_SYNC_MODE_PCR_MASTER) { |
| 1149 | struct session_debug debug; |
| 1150 | if (!msync_session_get_debug_mode(avsync->fd, &debug)) { |
| 1151 | if (debug.debug_freerun) { |
| 1152 | avsync->backup_mode = avsync->mode; |
| 1153 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 1154 | log_warn("[%d]audio to freerun mode", avsync->session_id); |
| 1155 | } else { |
| 1156 | avsync->mode = avsync->backup_mode; |
| 1157 | log_warn("[%d]audio back to mode %d", |
| 1158 | avsync->session_id, avsync->mode); |
| 1159 | } |
| 1160 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | static void handle_mode_change_v(struct av_sync_session* avsync, |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1165 | bool v_active, bool a_active, bool v_timeout) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1166 | { |
| 1167 | log_info("[%d]amode mode %d %d v/a %d/%d", avsync->session_id, |
| 1168 | avsync->active_mode, avsync->mode, v_active, a_active); |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1169 | if (avsync->active_mode == AV_SYNC_MODE_PCR_MASTER) { |
| 1170 | struct session_debug debug; |
| 1171 | if (!msync_session_get_debug_mode(avsync->fd, &debug)) { |
| 1172 | if (debug.debug_freerun) { |
| 1173 | avsync->backup_mode = avsync->mode; |
| 1174 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 1175 | log_warn("[%d]video to freerun mode", avsync->session_id); |
| 1176 | } else |
| 1177 | avsync->mode = avsync->backup_mode; |
| 1178 | log_warn("[%d]video back to mode %d", |
| 1179 | avsync->session_id, avsync->mode); |
| 1180 | } |
| 1181 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static void * poll_thread(void * arg) |
| 1185 | { |
| 1186 | int ret = 0; |
| 1187 | struct av_sync_session *avsync = (struct av_sync_session *)arg; |
| 1188 | const int fd = avsync->fd; |
| 1189 | struct pollfd pfd = { |
| 1190 | /* default blocking capture */ |
| 1191 | .events = POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, |
| 1192 | .fd = avsync->fd, |
| 1193 | }; |
| 1194 | |
| 1195 | prctl (PR_SET_NAME, "avs_poll"); |
| 1196 | log_info("[%d]enter", avsync->session_id); |
| 1197 | while (!avsync->quit_poll) { |
| 1198 | for (;;) { |
| 1199 | ret = poll(&pfd, 1, 10); |
| 1200 | if (ret > 0) |
| 1201 | break; |
| 1202 | if (avsync->quit_poll) |
| 1203 | goto exit; |
| 1204 | if (errno == EINTR) |
| 1205 | continue; |
| 1206 | } |
| 1207 | |
| 1208 | /* error handling */ |
| 1209 | if (pfd.revents & POLLERR) |
| 1210 | log_error("[%d]POLLERR received", avsync->session_id); |
| 1211 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1212 | /* mode change. Non-exclusive wait so all the processes |
| 1213 | * shall be woken up |
| 1214 | */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1215 | if (pfd.revents & POLLPRI) { |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1216 | bool v_active, a_active, v_timeout; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1217 | |
| 1218 | msync_session_get_stat(fd, &avsync->active_mode, |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1219 | &v_active, &a_active, &v_timeout, &avsync->in_audio_switch); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1220 | |
| 1221 | if (avsync->type == AV_SYNC_TYPE_AUDIO) |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1222 | handle_mode_change_a(avsync, v_active, a_active, v_timeout); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1223 | else if (avsync->type == AV_SYNC_TYPE_VIDEO) |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1224 | handle_mode_change_v(avsync, v_active, a_active, v_timeout); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | exit: |
| 1228 | log_info("[%d]quit", avsync->session_id); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1232 | int av_sync_set_pcr_clock(void *sync, pts90K pts, uint64_t mono_clock) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1233 | { |
| 1234 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1235 | |
| 1236 | if (!avsync) |
| 1237 | return -1; |
| 1238 | |
| 1239 | if (avsync->type != AV_SYNC_TYPE_PCR) |
| 1240 | return -2; |
| 1241 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1242 | return msync_session_set_pcr(avsync->fd, pts, mono_clock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1245 | int av_sync_get_pcr_clock(void *sync, pts90K *pts, uint64_t * mono_clock) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1246 | { |
| 1247 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1248 | |
| 1249 | if (!avsync) |
| 1250 | return -1; |
| 1251 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1252 | return msync_session_get_pcr(avsync->fd, pts, mono_clock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | int av_sync_set_session_name(void *sync, const char *name) |
| 1256 | { |
| 1257 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1258 | |
| 1259 | if (!avsync) |
| 1260 | return -1; |
| 1261 | |
| 1262 | return msync_session_set_name(avsync->fd, name); |
| 1263 | } |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1264 | |
| 1265 | int av_sync_set_audio_switch(void *sync, bool start) |
| 1266 | { |
| 1267 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1268 | bool v_active, a_active, v_timeout; |
| 1269 | |
| 1270 | if (!avsync) |
| 1271 | return -1; |
| 1272 | if (msync_session_get_stat(avsync->fd, &avsync->active_mode, |
| 1273 | &v_active, &a_active, |
Song Zhao | be51357 | 2021-05-13 13:10:21 -0700 | [diff] [blame] | 1274 | &v_timeout, &avsync->in_audio_switch)) { |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1275 | log_error("[%d] can not get session state", |
| 1276 | avsync->session_id); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | if (!v_active || !a_active) { |
| 1280 | log_error("[%d] no apply if not AV both active v %d a %d", |
| 1281 | avsync->session_id, v_active, a_active); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | if (msync_session_set_audio_switch(avsync->fd, start)) { |
| 1285 | log_error("[%d]fail to set audio switch %d", avsync->session_id, start); |
| 1286 | return -1; |
| 1287 | } |
| 1288 | avsync->in_audio_switch = start; |
| 1289 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_INIT; |
| 1290 | log_info("[%d]update audio switch to %d", avsync->session_id, start); |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | int av_sync_get_audio_switch(void *sync, bool *start) |
| 1295 | { |
| 1296 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1297 | |
| 1298 | if (!avsync) |
| 1299 | return -1; |
| 1300 | if (msync_session_get_stat(avsync->fd, &avsync->active_mode, |
| 1301 | NULL, NULL, NULL, &avsync->in_audio_switch)) { |
| 1302 | log_error("[%d] can not audio seamless switch state", |
| 1303 | avsync->session_id); |
| 1304 | return -1; |
| 1305 | } |
| 1306 | if (start) *start = avsync->in_audio_switch; |
| 1307 | return 0; |
Song Zhao | 7daf3a1 | 2021-05-10 22:22:25 -0700 | [diff] [blame] | 1308 | } |
Song Zhao | 8039f56 | 2021-05-18 18:11:25 -0700 | [diff] [blame] | 1309 | |
| 1310 | enum clock_recovery_stat av_sync_get_clock_devication(void *sync, int32_t *ppm) |
| 1311 | { |
| 1312 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1313 | |
| 1314 | if (!avsync || avsync->mode != AV_SYNC_MODE_PCR_MASTER) |
| 1315 | return CLK_RECOVERY_NOT_RUNNING; |
| 1316 | |
| 1317 | //TODO |
| 1318 | return CLK_RECOVERY_ONGOING; |
| 1319 | } |