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