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> |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 21 | #include <time.h> |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | //#include <linux/amlogic/msync.h> |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 24 | #include "aml_avsync.h" |
Song Zhao | 4f63295 | 2021-12-16 09:00:18 -0800 | [diff] [blame] | 25 | #include "aml_queue.h" |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 26 | #include "pattern.h" |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 27 | #include "aml_avsync_log.h" |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 28 | #include "msync_util.h" |
| 29 | #include "msync.h" |
| 30 | #include <pthread.h> |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 31 | #include "pcr_monitor.h" |
fei.deng | 66b4e81 | 2022-04-14 12:23:01 +0800 | [diff] [blame] | 32 | #include "aml_version.h" |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 33 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 34 | enum sync_state { |
| 35 | AV_SYNC_STAT_INIT = 0, |
| 36 | AV_SYNC_STAT_RUNNING = 1, |
| 37 | AV_SYNC_STAT_SYNC_SETUP = 2, |
| 38 | AV_SYNC_STAT_SYNC_LOST = 3, |
| 39 | }; |
| 40 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 41 | enum audio_switch_state_ { |
| 42 | AUDIO_SWITCH_STAT_INIT = 0, |
| 43 | AUDIO_SWITCH_STAT_RESET = 1, |
| 44 | AUDIO_SWITCH_STAT_START = 2, |
| 45 | AUDIO_SWITCH_STAT_FINISH = 3, |
yongchun.li | 59e873d | 2021-07-07 11:42:38 -0700 | [diff] [blame] | 46 | AUDIO_SWITCH_STAT_AGAIN = 4, |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 49 | #define SESSION_DEV "avsync_s" |
| 50 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 51 | struct av_sync_session { |
| 52 | /* session id attached */ |
| 53 | int session_id; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 54 | int fd; |
| 55 | bool attached; |
| 56 | enum sync_mode mode; |
| 57 | /* for audio trickplay */ |
| 58 | enum sync_mode backup_mode; |
| 59 | enum sync_type type; |
| 60 | uint32_t start_policy; |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 61 | int timeout; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 62 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 63 | /* playback time, will stop increasing during pause */ |
| 64 | pts90K vpts; |
| 65 | pts90K apts; |
| 66 | |
| 67 | /* phase adjustment of stream time for rate control (Video ONLY) */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 68 | pts90K phase; |
| 69 | bool phase_set; |
Song Zhao | 1561e54 | 2022-01-12 10:37:55 -0800 | [diff] [blame] | 70 | bool phase_adjusted; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 71 | |
| 72 | /* pts of last rendered frame */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 73 | pts90K last_wall; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 74 | pts90K last_pts; |
| 75 | struct vframe *last_frame; |
| 76 | |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 77 | /* pts of last pushed frame */ |
| 78 | pts90K last_q_pts; |
| 79 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 80 | bool first_frame_toggled; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 81 | bool paused; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 82 | enum sync_state state; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 83 | void *pattern_detector; |
| 84 | void *frame_q; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 85 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 86 | /* start control */ |
| 87 | int start_thres; |
| 88 | audio_start_cb audio_start; |
| 89 | void *audio_start_priv; |
| 90 | |
| 91 | /* render property */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 92 | int delay; |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 93 | int extra_delay; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 94 | pts90K vsync_interval; |
| 95 | |
| 96 | /* state lock */ |
| 97 | pthread_mutex_t lock; |
| 98 | /* pattern */ |
| 99 | int last_holding_peroid; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 100 | bool session_started; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 101 | |
| 102 | float speed; |
| 103 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 104 | /* pause pts */ |
| 105 | pts90K pause_pts; |
| 106 | pause_pts_done pause_pts_cb; |
| 107 | void *pause_cb_priv; |
yongchun.li | 428390d | 2022-02-17 17:15:40 -0800 | [diff] [blame] | 108 | /* underflow */ |
| 109 | underflow_detected underflow_cb; |
| 110 | void *underflow_cb_priv; |
| 111 | struct underflow_config underflow_cfg; |
| 112 | struct timespec frame_last_update_time; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 113 | |
| 114 | /* log control */ |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 115 | uint32_t last_log_syst; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 116 | uint32_t sync_lost_cnt; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 117 | struct timespec sync_lost_print_time; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 118 | |
| 119 | pthread_t poll_thread; |
| 120 | /* pcr master, IPTV only */ |
| 121 | bool quit_poll; |
| 122 | enum sync_mode active_mode; |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 123 | uint32_t disc_thres_min; |
| 124 | uint32_t disc_thres_max; |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 125 | |
| 126 | /* error detection */ |
| 127 | uint32_t last_poptime; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 128 | uint32_t outlier_cnt; |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 129 | pts90K last_disc_pts; |
| 130 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 131 | // indicate set audio switch |
| 132 | bool in_audio_switch; |
| 133 | enum audio_switch_state_ audio_switch_state; |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 134 | |
| 135 | //pcr monitor handle |
| 136 | void *pcr_monitor; |
| 137 | int ppm; |
Song Zhao | 623e2f1 | 2021-09-03 15:54:04 -0700 | [diff] [blame] | 138 | bool ppm_adjusted; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 139 | |
| 140 | //video FPS detection |
| 141 | pts90K last_fpts; |
| 142 | int fps_interval; |
| 143 | int fps_interval_acc; |
| 144 | int fps_cnt; |
| 145 | |
| 146 | //video freerun with rate control |
| 147 | uint32_t last_r_syst; |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 148 | bool debug_freerun; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 149 | |
| 150 | //Audio dropping detection |
| 151 | uint32_t audio_drop_cnt; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 152 | struct timespec audio_drop_start; |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 153 | |
| 154 | /*system mono time for current vsync interrupt */ |
| 155 | uint64_t msys; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | #define MAX_FRAME_NUM 32 |
| 159 | #define DEFAULT_START_THRESHOLD 2 |
| 160 | #define TIME_UNIT90K (90000) |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 161 | #define AV_DISC_THRES_MIN (TIME_UNIT90K / 3) |
| 162 | #define AV_DISC_THRES_MAX (TIME_UNIT90K * 10) |
Song Zhao | 7daf3a1 | 2021-05-10 22:22:25 -0700 | [diff] [blame] | 163 | #define A_ADJ_THREDHOLD_HB (900 * 6) //60ms |
| 164 | #define A_ADJ_THREDHOLD_LB (900 * 2) //20ms |
Song Zhao | 52f5519 | 2021-05-06 10:52:21 -0700 | [diff] [blame] | 165 | #define AV_PATTERN_RESET_THRES (TIME_UNIT90K / 10) |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 166 | #define SYNC_LOST_PRINT_THRESHOLD 10000000 //10 seconds In micro seconds |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 167 | #define LIVE_MODE(m) ((m) == AV_SYNC_MODE_PCR_MASTER || (m) == AV_SYNC_MODE_IPTV) |
Song Zhao | 7e24b18 | 2022-04-01 08:46:40 -0700 | [diff] [blame] | 168 | #define V_DISC_MODE(mode) (LIVE_MODE(mode) || (mode) == AV_SYNC_MODE_VMASTER) |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 169 | |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 170 | #define STREAM_DISC_THRES (TIME_UNIT90K / 10) |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 171 | #define OUTLIER_MAX_CNT 8 |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 172 | #define VALID_TS(x) ((x) != -1) |
yongchun.li | 428390d | 2022-02-17 17:15:40 -0800 | [diff] [blame] | 173 | #define UNDERFLOW_CHECK_THRESH_MS (100) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 174 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 175 | static uint64_t time_diff (struct timespec *b, struct timespec *a); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 176 | static bool frame_expire(struct av_sync_session* avsync, |
| 177 | uint32_t systime, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 178 | uint32_t interval, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 179 | struct vframe * frame, |
| 180 | struct vframe * next_frame, |
| 181 | int toggle_cnt); |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 182 | static bool pattern_detect(struct av_sync_session* avsync, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 183 | int cur_period, |
| 184 | int last_period); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 185 | static void * poll_thread(void * arg); |
| 186 | static void trigger_audio_start_cb(struct av_sync_session *avsync, |
| 187 | avs_ascb_reason reason); |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 188 | static struct vframe * video_mono_pop_frame(struct av_sync_session *avsync); |
| 189 | static int video_mono_push_frame(struct av_sync_session *avsync, struct vframe *frame); |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 190 | pthread_mutex_t glock = PTHREAD_MUTEX_INITIALIZER; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 191 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 192 | int av_sync_open_session(int *session_id) |
| 193 | { |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 194 | int fd = -1; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 195 | int id, rc; |
| 196 | |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 197 | pthread_mutex_lock(&glock); |
| 198 | fd = msync_create_session(); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 199 | if (fd < 0) { |
| 200 | log_error("fail"); |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 201 | goto exit; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 202 | } |
| 203 | rc = ioctl(fd, AMSYNC_IOC_ALLOC_SESSION, &id); |
| 204 | if (rc) { |
| 205 | log_error("new session errno:%d", errno); |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 206 | msync_destory_session(fd); |
| 207 | goto exit; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 208 | } |
| 209 | *session_id = id; |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 210 | log_debug("new avsession id %d fd %d", id, fd); |
| 211 | exit: |
| 212 | pthread_mutex_unlock(&glock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 213 | return fd; |
| 214 | } |
| 215 | |
| 216 | void av_sync_close_session(int session) |
| 217 | { |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 218 | log_debug("session closed fd %d", session); |
| 219 | pthread_mutex_lock(&glock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 220 | msync_destory_session(session); |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 221 | pthread_mutex_unlock(&glock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void* create_internal(int session_id, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 225 | enum sync_mode mode, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 226 | enum sync_type type, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 227 | int start_thres, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 228 | bool attach) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 229 | { |
| 230 | struct av_sync_session *avsync = NULL; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 231 | char dev_name[20]; |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 232 | int retry = 10; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 233 | |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 234 | log_info("[%d] mode %d type %d", session_id, mode, type); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 235 | avsync = (struct av_sync_session *)calloc(1, sizeof(*avsync)); |
| 236 | if (!avsync) { |
| 237 | log_error("OOM"); |
| 238 | return NULL; |
| 239 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 240 | |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 241 | if (type == AV_SYNC_TYPE_VIDEO && |
| 242 | mode == AV_SYNC_MODE_VIDEO_MONO) { |
| 243 | if (session_id < AV_SYNC_SESSION_V_MONO) { |
| 244 | log_error("wrong session id %d", session_id); |
| 245 | goto err; |
| 246 | } |
| 247 | avsync->type = type; |
| 248 | avsync->mode = mode; |
| 249 | avsync->fd = -1; |
| 250 | avsync->session_id = session_id; |
| 251 | log_info("[%d]init", avsync->session_id); |
| 252 | return avsync; |
| 253 | } |
| 254 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 255 | if (type == AV_SYNC_TYPE_VIDEO) { |
| 256 | avsync->pattern_detector = create_pattern_detector(); |
| 257 | if (!avsync->pattern_detector) { |
| 258 | log_error("pd create fail"); |
| 259 | goto err; |
| 260 | } |
| 261 | |
| 262 | if (!start_thres) |
| 263 | avsync->start_thres = DEFAULT_START_THRESHOLD; |
| 264 | else { |
| 265 | if (start_thres > 5) { |
| 266 | log_error("start_thres too big: %d", start_thres); |
| 267 | goto err2; |
| 268 | } |
| 269 | avsync->start_thres = start_thres; |
| 270 | } |
| 271 | avsync->phase_set = false; |
Song Zhao | 1561e54 | 2022-01-12 10:37:55 -0800 | [diff] [blame] | 272 | avsync->phase_adjusted = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 273 | avsync->first_frame_toggled = false; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 274 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 275 | |
| 276 | avsync->type = type; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 277 | avsync->state = AV_SYNC_STAT_INIT; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 278 | avsync->paused = false; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 279 | avsync->session_id = session_id; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 280 | avsync->backup_mode = mode; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 281 | avsync->last_frame = NULL; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 282 | avsync->session_started = false; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 283 | avsync->speed = 1.0f; |
| 284 | avsync->pause_pts = AV_SYNC_INVALID_PAUSE_PTS; |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 285 | avsync->vsync_interval = -1; |
| 286 | avsync->last_disc_pts = -1; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 287 | avsync->last_log_syst = -1; |
| 288 | avsync->last_pts = -1; |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 289 | avsync->last_q_pts = -1; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 290 | avsync->last_wall = -1; |
| 291 | avsync->fps_interval = -1; |
| 292 | avsync->last_r_syst = -1; |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 293 | avsync->timeout = -1; |
| 294 | |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 295 | if (msync_session_get_disc_thres(session_id, |
| 296 | &avsync->disc_thres_min, &avsync->disc_thres_max)) { |
| 297 | log_error("fail to get disc thres", dev_name, errno); |
| 298 | avsync->disc_thres_min = AV_DISC_THRES_MIN; |
| 299 | avsync->disc_thres_max = AV_DISC_THRES_MAX; |
| 300 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 301 | |
| 302 | pthread_mutex_init(&avsync->lock, NULL); |
Song Zhao | 9c09f77 | 2022-01-07 14:26:50 -0800 | [diff] [blame] | 303 | log_info("[%d] start_thres %d disc_thres %u/%u", session_id, |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 304 | start_thres, avsync->disc_thres_min, avsync->disc_thres_max); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 305 | |
| 306 | snprintf(dev_name, sizeof(dev_name), "/dev/%s%d", SESSION_DEV, session_id); |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 307 | while (retry) { |
| 308 | /* wait for sysfs to update */ |
| 309 | avsync->fd = open(dev_name, O_RDONLY | O_CLOEXEC); |
| 310 | if (avsync->fd > 0) |
| 311 | break; |
| 312 | |
| 313 | retry--; |
| 314 | if (!retry) { |
| 315 | log_error("open %s errno %d", dev_name, errno); |
| 316 | goto err2; |
| 317 | } |
| 318 | usleep(20000); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 319 | } |
| 320 | |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 321 | if (avsync->type == AV_SYNC_TYPE_PCR) { |
| 322 | if (pcr_monitor_init(&avsync->pcr_monitor)) { |
| 323 | log_error("pcr monitor init"); |
Song Zhao | 9cd27a5 | 2021-07-26 15:27:38 +0000 | [diff] [blame] | 324 | goto err3; |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 328 | if (!attach) { |
| 329 | msync_session_set_mode(avsync->fd, mode); |
| 330 | avsync->mode = mode; |
Song Zhao | 7e24b18 | 2022-04-01 08:46:40 -0700 | [diff] [blame] | 331 | if (avsync->mode == AV_SYNC_MODE_VMASTER) |
| 332 | msync_session_set_wall_adj_thres(avsync->fd, avsync->disc_thres_min); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 333 | } else { |
| 334 | avsync->attached = true; |
| 335 | if (msync_session_get_mode(avsync->fd, &avsync->mode)) { |
| 336 | log_error("get mode"); |
Song Zhao | 9cd27a5 | 2021-07-26 15:27:38 +0000 | [diff] [blame] | 337 | goto err4; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 338 | } |
| 339 | avsync->backup_mode = avsync->mode; |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 340 | if (msync_session_get_start_policy(avsync->fd, &avsync->start_policy, &avsync->timeout)) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 341 | log_error("get policy"); |
Song Zhao | 9cd27a5 | 2021-07-26 15:27:38 +0000 | [diff] [blame] | 342 | goto err4; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 343 | } |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 344 | if (msync_session_get_stat(avsync->fd, &avsync->active_mode, NULL, |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 345 | NULL, NULL, NULL, &avsync->in_audio_switch, SRC_A)) { |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 346 | log_error("get state"); |
Song Zhao | 9cd27a5 | 2021-07-26 15:27:38 +0000 | [diff] [blame] | 347 | goto err4; |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 348 | } |
| 349 | if (avsync->in_audio_switch) { |
| 350 | log_info("audio_switch_state reseted the audio"); |
| 351 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_RESET; |
| 352 | } |
| 353 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 354 | log_info("[%d]retrieve sync mode %d policy %d", |
| 355 | session_id, avsync->mode, avsync->start_policy); |
| 356 | } |
| 357 | |
Song Zhao | 67c937b | 2021-10-01 11:21:32 -0700 | [diff] [blame] | 358 | /* debug log level */ |
| 359 | { |
| 360 | const char *env= getenv( "AML_AVSYNC_DEBUG_LEVEL"); |
| 361 | if ( env ) { |
| 362 | log_set_level(atoi(env)); |
| 363 | } |
| 364 | } |
| 365 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 366 | return avsync; |
Song Zhao | 9cd27a5 | 2021-07-26 15:27:38 +0000 | [diff] [blame] | 367 | err4: |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 368 | if (avsync->pcr_monitor) |
| 369 | pcr_monitor_destroy(avsync->pcr_monitor); |
Song Zhao | 9cd27a5 | 2021-07-26 15:27:38 +0000 | [diff] [blame] | 370 | err3: |
| 371 | close(avsync->fd); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 372 | err2: |
| 373 | destroy_pattern_detector(avsync->pattern_detector); |
| 374 | err: |
| 375 | free(avsync); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | void* av_sync_create(int session_id, |
| 380 | enum sync_mode mode, |
| 381 | enum sync_type type, |
| 382 | int start_thres) |
| 383 | { |
| 384 | return create_internal(session_id, mode, |
| 385 | type, start_thres, false); |
| 386 | } |
| 387 | |
| 388 | void* av_sync_attach(int session_id, enum sync_type type) |
| 389 | { |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 390 | if (type == AV_SYNC_TYPE_VIDEO) |
| 391 | return NULL; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 392 | return create_internal(session_id, AV_SYNC_MODE_MAX, |
| 393 | type, 0, true); |
| 394 | } |
| 395 | |
| 396 | int av_sync_video_config(void *sync, struct video_config* config) |
| 397 | { |
| 398 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 399 | |
| 400 | if (!avsync || !config) |
| 401 | return -1; |
| 402 | |
| 403 | if (config->delay != 1 && config->delay != 2) { |
| 404 | log_error("invalid delay: %d\n", config->delay); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | avsync->delay = config->delay; |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 409 | avsync->extra_delay = config->extra_delay * 90; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 410 | |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 411 | log_info("[%d] vsync delay: %d extra_delay: %d ms", |
| 412 | avsync->session_id, config->delay, config->extra_delay); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 413 | return 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int internal_stop(struct av_sync_session *avsync) |
| 417 | { |
| 418 | int ret = 0; |
| 419 | struct vframe *frame; |
| 420 | |
| 421 | pthread_mutex_lock(&avsync->lock); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 422 | while (!dqueue_item(avsync->frame_q, (void **)&frame)) { |
| 423 | frame->free(frame); |
| 424 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 425 | avsync->state = AV_SYNC_STAT_INIT; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 426 | pthread_mutex_unlock(&avsync->lock); |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | /* destroy and detach from kernel session */ |
| 431 | void av_sync_destroy(void *sync) |
| 432 | { |
| 433 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 434 | |
| 435 | if (!avsync) |
| 436 | return; |
| 437 | |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 438 | if (avsync->type == AV_SYNC_TYPE_VIDEO && |
| 439 | avsync->mode == AV_SYNC_MODE_VIDEO_MONO) { |
| 440 | log_info("[%d]done", avsync->session_id); |
| 441 | internal_stop(avsync); |
| 442 | destroy_q(avsync->frame_q); |
| 443 | free(avsync); |
| 444 | return; |
| 445 | } |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 446 | log_info("[%d]begin type %d", avsync->session_id, avsync->type); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 447 | if (avsync->state != AV_SYNC_STAT_INIT) { |
| 448 | if (avsync->type == AV_SYNC_TYPE_VIDEO) |
| 449 | internal_stop(avsync); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 450 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 451 | avsync->quit_poll = true; |
| 452 | if (avsync->poll_thread) { |
| 453 | pthread_join(avsync->poll_thread, NULL); |
| 454 | avsync->poll_thread = 0; |
| 455 | } |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 456 | if (avsync->type == AV_SYNC_TYPE_AUDIO) |
| 457 | trigger_audio_start_cb(avsync, AV_SYNC_ASCB_STOP); |
Song Zhao | af368d5 | 2021-02-17 17:53:45 -0800 | [diff] [blame] | 458 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 459 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 460 | if (avsync->session_started) { |
| 461 | if (avsync->type == AV_SYNC_TYPE_VIDEO) |
| 462 | msync_session_set_video_stop(avsync->fd); |
| 463 | else |
| 464 | msync_session_set_audio_stop(avsync->fd); |
| 465 | } |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 466 | |
| 467 | if(avsync->pcr_monitor) |
| 468 | pcr_monitor_destroy(avsync->pcr_monitor); |
| 469 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 470 | close(avsync->fd); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 471 | pthread_mutex_destroy(&avsync->lock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 472 | if (avsync->type == AV_SYNC_TYPE_VIDEO) { |
| 473 | destroy_q(avsync->frame_q); |
| 474 | destroy_pattern_detector(avsync->pattern_detector); |
| 475 | } |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 476 | log_info("[%d]done type %d", avsync->session_id, avsync->type); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 477 | free(avsync); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 478 | } |
| 479 | |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 480 | int avs_sync_set_start_policy(void *sync, struct start_policy* st_policy) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 481 | { |
| 482 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 483 | |
| 484 | if (!avsync || !avsync->fd) |
| 485 | return -1; |
| 486 | |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 487 | log_info("[%d]policy %u --> %u, timeout %d --> %d", avsync->start_policy, st_policy->policy, avsync->timeout, st_policy->timeout); |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 488 | if (LIVE_MODE(avsync->mode) && |
| 489 | st_policy->policy != AV_SYNC_START_ASAP) { |
| 490 | log_error("policy %d not supported in live mode", st_policy->policy); |
| 491 | return -1; |
| 492 | } |
| 493 | |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 494 | avsync->start_policy = st_policy->policy; |
| 495 | avsync->timeout = st_policy->timeout; |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 496 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 497 | /* v_peek will be handled by libamlavsync */ |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 498 | if (st_policy->policy != AV_SYNC_START_NONE && |
| 499 | st_policy->policy != AV_SYNC_START_V_PEEK) |
| 500 | return msync_session_set_start_policy(avsync->fd, st_policy->policy, st_policy->timeout); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 501 | |
| 502 | return 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | int av_sync_pause(void *sync, bool pause) |
| 506 | { |
| 507 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 508 | bool v_active, a_active, v_timeout; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 509 | int rc; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 510 | |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 511 | if (!avsync) { |
| 512 | log_error("invalid handle"); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 513 | return -1; |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 514 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 515 | |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 516 | if (avsync->mode == AV_SYNC_MODE_PCR_MASTER) { |
| 517 | log_warn("ignore pause in pcr master mode"); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 518 | return -1; |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 519 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 520 | |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 521 | rc = msync_session_get_stat(avsync->fd, &avsync->active_mode, NULL, |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 522 | &v_active, &a_active, &v_timeout, |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 523 | &avsync->in_audio_switch, SRC_A); |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 524 | |
yongchun.li | 5f52fb0 | 2021-06-04 18:13:05 -0700 | [diff] [blame] | 525 | /* ignore only when video try to pause when audio is acive, on which |
| 526 | the control of the STC will be relays. |
| 527 | When resume,it can do always as it is possible that video just |
| 528 | paused earlier without audio yet,then audio added later before resume. |
| 529 | We shall not igore that otherwise it could cause video freeze. */ |
| 530 | if (avsync->mode == AV_SYNC_MODE_AMASTER && |
| 531 | avsync->type == AV_SYNC_TYPE_VIDEO && |
| 532 | a_active && |
| 533 | !avsync->in_audio_switch) { |
| 534 | if (!pause) { |
| 535 | log_info("[%d] clear video pause when audio active", |
| 536 | avsync->session_id); |
| 537 | avsync->paused = pause; |
| 538 | } else { |
| 539 | log_info("[%d] ignore the pause from video when audio active", |
| 540 | avsync->session_id); |
| 541 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 542 | return 0; |
yongchun.li | 5f52fb0 | 2021-06-04 18:13:05 -0700 | [diff] [blame] | 543 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 544 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 545 | if (avsync->in_audio_switch && avsync->type == AV_SYNC_TYPE_AUDIO) { |
| 546 | log_info("[%d] ignore the pause from audio", avsync->session_id); |
| 547 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_RESET; |
| 548 | return 0; |
| 549 | } |
| 550 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 551 | rc = msync_session_set_pause(avsync->fd, pause); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 552 | avsync->paused = pause; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 553 | log_info("[%d]paused:%d type:%d rc %d", |
| 554 | avsync->session_id, pause, avsync->type, rc); |
yongchun.li | 428390d | 2022-02-17 17:15:40 -0800 | [diff] [blame] | 555 | if (!avsync->paused && avsync->first_frame_toggled) { |
| 556 | clock_gettime(CLOCK_MONOTONIC_RAW, &avsync->frame_last_update_time); |
| 557 | log_info("[%d] resume update new frame time", avsync->session_id); |
| 558 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 559 | return rc; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | int av_sync_push_frame(void *sync , struct vframe *frame) |
| 563 | { |
| 564 | int ret; |
Song Zhao | ee6a151 | 2021-09-08 11:28:57 -0700 | [diff] [blame] | 565 | struct vframe *prev = NULL; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 566 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 567 | |
| 568 | if (!avsync) |
| 569 | return -1; |
| 570 | |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 571 | if (avsync->type == AV_SYNC_TYPE_VIDEO && |
| 572 | avsync->mode == AV_SYNC_MODE_VIDEO_MONO) |
| 573 | return video_mono_push_frame(avsync, frame); |
| 574 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 575 | if (!avsync->frame_q) { |
| 576 | /* policy should be final now */ |
bo.xiao | 5821fc7 | 2021-07-11 22:47:00 -0400 | [diff] [blame] | 577 | if (msync_session_get_start_policy(avsync->fd, &avsync->start_policy, &avsync->timeout)) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 578 | log_error("[%d]get policy", avsync->session_id); |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | avsync->frame_q = create_q(MAX_FRAME_NUM); |
| 583 | if (!avsync->frame_q) { |
| 584 | log_error("[%d]create queue fail", avsync->session_id); |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 585 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 586 | return -1; |
| 587 | } |
| 588 | |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 589 | { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 590 | int ret; |
| 591 | |
| 592 | ret = pthread_create(&avsync->poll_thread, NULL, poll_thread, avsync); |
| 593 | if (ret) { |
| 594 | log_error("[%d]create poll thread errno %d", avsync->session_id, errno); |
| 595 | destroy_q(avsync->frame_q); |
| 596 | return -1; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 601 | if (avsync->last_q_pts != -1) { |
Song Zhao | 7e24b18 | 2022-04-01 08:46:40 -0700 | [diff] [blame] | 602 | if (frame->pts != -1 && avsync->mode == AV_SYNC_MODE_VMASTER) { |
| 603 | /* Sometimes app will fake PTS for trickplay, video PTS gap |
| 604 | * is really big depending on the speed. Have to adjust the |
| 605 | * threshold dynamically. |
| 606 | */ |
| 607 | int gap = (int)(frame->pts - avsync->last_q_pts); |
| 608 | if (gap > avsync->disc_thres_min) { |
| 609 | avsync->disc_thres_min = gap * 6; |
| 610 | avsync->disc_thres_max = gap * 20; |
| 611 | msync_session_set_wall_adj_thres(avsync->fd, avsync->disc_thres_min); |
| 612 | msync_session_set_disc_thres(avsync->session_id, |
| 613 | avsync->disc_thres_min, avsync->disc_thres_max); |
| 614 | log_info("[%d] update disc_thres to %d/%d",avsync->session_id, |
| 615 | avsync->disc_thres_min, avsync->disc_thres_max); |
| 616 | } |
| 617 | } |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 618 | if (avsync->last_q_pts == frame->pts && avsync->mode == AV_SYNC_MODE_AMASTER) { |
| 619 | /* TODO: wrong, should remove from back of queue */ |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 620 | dqueue_item(avsync->frame_q, (void **)&prev); |
Song Zhao | ee6a151 | 2021-09-08 11:28:57 -0700 | [diff] [blame] | 621 | if (prev) { |
| 622 | prev->free(prev); |
| 623 | log_info ("[%d]drop frame with same pts %u", avsync->session_id, frame->pts); |
| 624 | } |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 625 | } else if (avsync->fps_cnt < 100) { |
| 626 | int32_t interval = frame->pts - avsync->last_q_pts; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 627 | |
| 628 | if (interval > 0 && interval <= 4500) { |
| 629 | if (avsync->fps_interval_acc == -1) { |
| 630 | avsync->fps_interval_acc = interval; |
| 631 | avsync->fps_cnt = 1; |
| 632 | } else { |
| 633 | avsync->fps_interval_acc += interval; |
| 634 | avsync->fps_cnt++; |
| 635 | avsync->fps_interval = avsync->fps_interval_acc / avsync->fps_cnt; |
| 636 | if (avsync->fps_cnt == 100) |
| 637 | log_info("[%d] fps_interval = %d", avsync->session_id, avsync->fps_interval); |
| 638 | } |
| 639 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 643 | if (frame->duration == -1) |
| 644 | frame->duration = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 645 | frame->hold_period = 0; |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 646 | avsync->last_q_pts = frame->pts; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 647 | ret = queue_item(avsync->frame_q, frame); |
| 648 | if (avsync->state == AV_SYNC_STAT_INIT && |
| 649 | queue_size(avsync->frame_q) >= avsync->start_thres) { |
| 650 | avsync->state = AV_SYNC_STAT_RUNNING; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 651 | log_info("[%d]state: init --> running", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (ret) |
Song Zhao | e1b6560 | 2022-01-06 08:03:46 -0800 | [diff] [blame] | 655 | log_error("queue fail:%d", ret); |
bo.xiao | 1f94b35 | 2021-08-02 03:53:47 -0400 | [diff] [blame] | 656 | log_debug("[%d]push %u, QNum=%d", avsync->session_id, frame->pts, queue_size(avsync->frame_q)); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 657 | return ret; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | struct vframe *av_sync_pop_frame(void *sync) |
| 661 | { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 662 | struct vframe *frame = NULL, *enter_last_frame = NULL; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 663 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 664 | int toggle_cnt = 0; |
| 665 | uint32_t systime; |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 666 | bool pause_pts_reached = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 667 | uint32_t interval; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 668 | |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 669 | if (avsync->type == AV_SYNC_TYPE_VIDEO && |
| 670 | avsync->mode == AV_SYNC_MODE_VIDEO_MONO) |
| 671 | return video_mono_pop_frame(avsync); |
| 672 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 673 | pthread_mutex_lock(&avsync->lock); |
| 674 | if (avsync->state == AV_SYNC_STAT_INIT) { |
Song Zhao | 2f6744b | 2021-11-03 21:23:50 -0700 | [diff] [blame] | 675 | log_debug("[%d]in state INIT", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 676 | goto exit; |
| 677 | } |
| 678 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 679 | if (!avsync->session_started) { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 680 | uint32_t pts; |
| 681 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 682 | if (peek_item(avsync->frame_q, (void **)&frame, 0) || !frame) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 683 | log_info("[%d]empty q", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 684 | goto exit; |
| 685 | } |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 686 | msync_session_get_wall(avsync->fd, &systime, &interval); |
| 687 | pts = frame->pts - avsync->delay * interval; |
| 688 | msync_session_set_video_start(avsync->fd, pts); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 689 | avsync->session_started = true; |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 690 | log_info("[%d]video start %u", avsync->session_id, pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 693 | if (avsync->start_policy == AV_SYNC_START_ALIGN && |
Song Zhao | d4109b6 | 2021-04-30 08:47:17 -0700 | [diff] [blame] | 694 | !avsync->first_frame_toggled && |
| 695 | !msync_clock_started(avsync->fd)) { |
| 696 | pthread_mutex_unlock(&avsync->lock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 697 | log_trace("[%d]clock not started", avsync->session_id); |
| 698 | return NULL; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 701 | enter_last_frame = avsync->last_frame; |
| 702 | msync_session_get_wall(avsync->fd, &systime, &interval); |
| 703 | |
| 704 | /* handle refresh rate change */ |
| 705 | if (avsync->vsync_interval == AV_SYNC_INVALID_PAUSE_PTS || |
| 706 | avsync->vsync_interval != interval) { |
| 707 | log_info("[%d]vsync interval update %d --> %u", |
| 708 | avsync->session_id, avsync->vsync_interval, interval); |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 709 | if (avsync->fps_interval == -1) |
| 710 | avsync->fps_interval = interval; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 711 | avsync->vsync_interval = interval; |
| 712 | avsync->phase_set = false; |
Song Zhao | 1561e54 | 2022-01-12 10:37:55 -0800 | [diff] [blame] | 713 | avsync->phase_adjusted = false; |
Song Zhao | de5f73b | 2021-07-23 22:38:07 -0700 | [diff] [blame] | 714 | avsync->phase = 0; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 715 | reset_pattern(avsync->pattern_detector); |
| 716 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 717 | while (!peek_item(avsync->frame_q, (void **)&frame, 0)) { |
| 718 | struct vframe *next_frame = NULL; |
| 719 | |
| 720 | peek_item(avsync->frame_q, (void **)&next_frame, 1); |
| 721 | if (next_frame) |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 722 | log_debug("[%d]cur_f %u next_f %u size %d", |
| 723 | 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] | 724 | if (frame_expire(avsync, systime, interval, |
| 725 | frame, next_frame, toggle_cnt)) { |
| 726 | log_debug("[%d]cur_f %u expire", avsync->session_id, frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 727 | toggle_cnt++; |
| 728 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 729 | if (pattern_detect(avsync, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 730 | (avsync->last_frame?avsync->last_frame->hold_period:0), |
Song Zhao | 67c937b | 2021-10-01 11:21:32 -0700 | [diff] [blame] | 731 | avsync->last_holding_peroid)) { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 732 | log_info("[%d] %u break the pattern", avsync->session_id, avsync->last_frame->pts); |
Song Zhao | 67c937b | 2021-10-01 11:21:32 -0700 | [diff] [blame] | 733 | log_info("[%d] cur frame %u sys %u", avsync->session_id, frame->pts, systime); |
| 734 | if (next_frame) |
| 735 | log_info("[%d] next frame %u", avsync->session_id, next_frame->pts); |
| 736 | } |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 737 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 738 | if (avsync->last_frame) |
| 739 | avsync->last_holding_peroid = avsync->last_frame->hold_period; |
| 740 | |
| 741 | dqueue_item(avsync->frame_q, (void **)&frame); |
| 742 | if (avsync->last_frame) { |
| 743 | /* free frame that are not for display */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 744 | if (toggle_cnt > 1) { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 745 | log_debug("[%d]free %u cur %u system/d %u/%u", avsync->session_id, |
| 746 | avsync->last_frame->pts, frame->pts, systime, systime - avsync->last_poptime); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 747 | avsync->last_frame->free(avsync->last_frame); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 748 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 749 | } else { |
| 750 | avsync->first_frame_toggled = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 751 | log_info("[%d]first frame %u", avsync->session_id, frame->pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 752 | } |
| 753 | avsync->last_frame = frame; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 754 | avsync->last_pts = frame->pts; |
yongchun.li | 428390d | 2022-02-17 17:15:40 -0800 | [diff] [blame] | 755 | clock_gettime(CLOCK_MONOTONIC_RAW, &avsync->frame_last_update_time); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 756 | } else |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | /* pause pts */ |
| 761 | if (avsync->pause_pts != AV_SYNC_INVALID_PAUSE_PTS && avsync->last_frame) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 762 | if (avsync->pause_pts == AV_SYNC_STEP_PAUSE_PTS) |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 763 | pause_pts_reached = true; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 764 | else |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 765 | pause_pts_reached = (int)(avsync->last_frame->pts - avsync->pause_pts) >= 0; |
| 766 | } else if (avsync->pause_pts != AV_SYNC_INVALID_PAUSE_PTS) { |
| 767 | if (!peek_item(avsync->frame_q, (void **)&frame, 0)) |
| 768 | pause_pts_reached = (int)(frame->pts - avsync->pause_pts) >= 0; |
| 769 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 770 | |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 771 | if (pause_pts_reached) { |
| 772 | if (avsync->pause_pts_cb) |
| 773 | avsync->pause_pts_cb(avsync->pause_pts, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 774 | avsync->pause_cb_priv); |
| 775 | |
Song Zhao | 468fd65 | 2021-01-15 22:13:04 -0800 | [diff] [blame] | 776 | /* stay in paused until av_sync_pause(false) */ |
| 777 | avsync->paused = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 778 | log_info ("[%d]reach pause pts: %u", |
Song Zhao | b5458b3 | 2021-11-12 15:58:47 -0800 | [diff] [blame] | 779 | avsync->session_id, avsync->pause_pts); |
Song Zhao | ad7c823 | 2021-12-14 11:46:48 -0800 | [diff] [blame] | 780 | avsync->pause_pts = AV_SYNC_INVALID_PAUSE_PTS; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | exit: |
| 784 | pthread_mutex_unlock(&avsync->lock); |
yongchun.li | 428390d | 2022-02-17 17:15:40 -0800 | [diff] [blame] | 785 | |
| 786 | /* underflow check */ |
| 787 | if (avsync->session_started && avsync->first_frame_toggled && |
| 788 | (avsync->paused == false) && (avsync->state >= AV_SYNC_STAT_RUNNING) && |
| 789 | avsync->underflow_cb && peek_item(avsync->frame_q, (void **)&frame, 0)) |
| 790 | {/* empty queue in normal play */ |
| 791 | struct timespec now; |
| 792 | int diff_ms; |
| 793 | clock_gettime(CLOCK_MONOTONIC_RAW, &now); |
| 794 | diff_ms = time_diff(&now, &avsync->frame_last_update_time)/1000; |
| 795 | if(diff_ms >= (avsync->underflow_cfg.time_thresh |
| 796 | + avsync->vsync_interval*avsync->last_holding_peroid/90)) { |
| 797 | log_info ("[%d]underflow detected: %u", avsync->session_id, avsync->last_pts); |
| 798 | avsync->underflow_cb (avsync->last_pts, |
| 799 | avsync->underflow_cb_priv); |
| 800 | /* update time to control the underflow check call backs */ |
| 801 | avsync->frame_last_update_time = now; |
| 802 | } |
| 803 | } |
| 804 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 805 | if (avsync->last_frame) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 806 | if (enter_last_frame != avsync->last_frame) |
| 807 | log_debug("[%d]pop %u", avsync->session_id, avsync->last_frame->pts); |
bo.xiao | 1f94b35 | 2021-08-02 03:53:47 -0400 | [diff] [blame] | 808 | log_trace("[%d]pop=%u, stc=%u, QNum=%d", avsync->session_id, avsync->last_frame->pts, systime, queue_size(avsync->frame_q)); |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 809 | /* don't update vpts for out_lier */ |
| 810 | if (avsync->last_frame->duration != -1) |
| 811 | msync_session_update_vpts(avsync->fd, systime, |
| 812 | avsync->last_frame->pts, interval * avsync->delay); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 813 | } else |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 814 | if (enter_last_frame != avsync->last_frame) |
| 815 | log_debug("[%d]pop (nil)", avsync->session_id); |
| 816 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 817 | avsync->last_poptime = systime; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 818 | if (avsync->last_frame) |
| 819 | avsync->last_frame->hold_period++; |
| 820 | return avsync->last_frame; |
| 821 | } |
| 822 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 823 | static inline uint32_t abs_diff(uint32_t a, uint32_t b) |
| 824 | { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 825 | return (int)(a - b) > 0 ? a - b : b - a; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 826 | } |
| 827 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 828 | static uint64_t time_diff (struct timespec *b, struct timespec *a) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 829 | { |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 830 | return (b->tv_sec - a->tv_sec)*1000000 + (b->tv_nsec/1000 - a->tv_nsec/1000); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 833 | static bool frame_expire(struct av_sync_session* avsync, |
| 834 | uint32_t systime, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 835 | uint32_t interval, |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 836 | struct vframe * frame, |
| 837 | struct vframe * next_frame, |
| 838 | int toggle_cnt) |
| 839 | { |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 840 | uint32_t fpts = frame->pts + avsync->extra_delay; |
| 841 | uint32_t nfpts = -1; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 842 | bool expire = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 843 | uint32_t pts_correction = avsync->delay * interval; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 844 | |
| 845 | if (avsync->paused && avsync->pause_pts == AV_SYNC_INVALID_PAUSE_PTS) |
| 846 | return false; |
| 847 | |
| 848 | if (avsync->pause_pts == AV_SYNC_STEP_PAUSE_PTS) |
| 849 | return true; |
| 850 | |
Song Zhao | ad7c823 | 2021-12-14 11:46:48 -0800 | [diff] [blame] | 851 | if (avsync->pause_pts != AV_SYNC_INVALID_PAUSE_PTS && |
| 852 | avsync->pause_pts == frame->pts) |
| 853 | return true; |
| 854 | |
Song Zhao | 08fa16b | 2021-12-08 14:30:17 -0800 | [diff] [blame] | 855 | if (systime == AV_SYNC_INVALID_PTS && |
| 856 | avsync->mode == AV_SYNC_MODE_AMASTER) |
| 857 | return false; |
| 858 | |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 859 | if (next_frame) |
| 860 | nfpts = next_frame->pts + avsync->extra_delay; |
| 861 | |
Song Zhao | 7e24b18 | 2022-04-01 08:46:40 -0700 | [diff] [blame] | 862 | if (avsync->mode == AV_SYNC_MODE_FREE_RUN) { |
fei.deng | 63e43e1 | 2021-09-23 19:44:01 +0800 | [diff] [blame] | 863 | /* We need to ensure that the video outputs smoothly, |
| 864 | so output video frame by frame hold_period */ |
| 865 | if ((abs_diff(systime, fpts) > AV_PATTERN_RESET_THRES) && |
| 866 | avsync->last_frame && |
| 867 | (avsync->last_frame->hold_period >= (avsync->fps_interval/interval))) { |
| 868 | log_debug("[%d]vmaster/freerun systime:%u --> fpts:%u,last hold_period:%d", |
| 869 | avsync->session_id, systime, fpts,avsync->last_frame->hold_period); |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 870 | return true; |
| 871 | } |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 874 | if (!fpts) { |
| 875 | if (avsync->last_frame) { |
| 876 | /* try to accumulate duration as PTS */ |
| 877 | fpts = avsync->vpts + avsync->last_frame->duration; |
| 878 | } else { |
| 879 | fpts = avsync->vpts; |
| 880 | } |
| 881 | } |
| 882 | systime += pts_correction; |
| 883 | |
| 884 | /* phase adjustment */ |
| 885 | if (avsync->phase_set) |
| 886 | systime += avsync->phase; |
| 887 | |
yongchun.li | a50b1e9 | 2021-08-07 01:11:54 +0000 | [diff] [blame] | 888 | log_trace("[%d]systime:%u phase:%d correct:%u fpts:%u", |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 889 | avsync->session_id, systime, |
yongchun.li | a50b1e9 | 2021-08-07 01:11:54 +0000 | [diff] [blame] | 890 | avsync->phase_set? (int)avsync->phase:0, pts_correction, fpts); |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 891 | if (abs_diff(systime, fpts) > avsync->disc_thres_min) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 892 | /* ignore discontinity under pause */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 893 | if (avsync->paused) |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 894 | return false; |
| 895 | |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 896 | if ((VALID_TS(avsync->last_log_syst) && avsync->last_log_syst != systime) || |
| 897 | (VALID_TS(avsync->last_pts) && avsync->last_pts != fpts)) { |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 898 | struct timespec now; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 899 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 900 | clock_gettime(CLOCK_MONOTONIC_RAW, &now); |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 901 | avsync->last_log_syst = systime; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 902 | avsync->last_pts = fpts; |
| 903 | if (time_diff(&now, &avsync->sync_lost_print_time) >= |
| 904 | SYNC_LOST_PRINT_THRESHOLD) { |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 905 | log_warn("[%d]sync lost systime:%u fpts:%u lost:%u", |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 906 | avsync->session_id, systime, fpts, avsync->sync_lost_cnt); |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 907 | avsync->sync_lost_cnt = 0; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 908 | clock_gettime(CLOCK_MONOTONIC_RAW, &avsync->sync_lost_print_time); |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 909 | } else |
| 910 | avsync->sync_lost_cnt++; |
| 911 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 912 | |
| 913 | if (avsync->state == AV_SYNC_STAT_SYNC_SETUP && |
| 914 | LIVE_MODE(avsync->mode) && |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 915 | VALID_TS(avsync->last_pts) && |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 916 | abs_diff(avsync->last_pts, fpts) > STREAM_DISC_THRES) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 917 | /* outlier by stream error */ |
| 918 | avsync->outlier_cnt++; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 919 | frame->duration = -1; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 920 | if (avsync->outlier_cnt < OUTLIER_MAX_CNT) { |
| 921 | log_info("render outlier %u", fpts); |
| 922 | return true; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | avsync->outlier_cnt = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 927 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
| 928 | avsync->phase_set = false; |
Song Zhao | 1561e54 | 2022-01-12 10:37:55 -0800 | [diff] [blame] | 929 | avsync->phase_adjusted = false; |
Song Zhao | de5f73b | 2021-07-23 22:38:07 -0700 | [diff] [blame] | 930 | avsync->phase = 0; |
Song Zhao | a58c3e9 | 2021-03-09 18:52:55 -0800 | [diff] [blame] | 931 | reset_pattern(avsync->pattern_detector); |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 932 | |
Song Zhao | 7e24b18 | 2022-04-01 08:46:40 -0700 | [diff] [blame] | 933 | if (V_DISC_MODE(avsync->mode) && avsync->last_disc_pts != fpts) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 934 | log_info ("[%d]video disc %u --> %u", |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 935 | avsync->session_id, systime, fpts); |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 936 | msync_session_set_video_dis(avsync->fd, fpts); |
| 937 | avsync->last_disc_pts = fpts; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | if ((int)(systime - fpts) > 0) { |
| 941 | if ((int)(systime - fpts) < avsync->disc_thres_max) { |
| 942 | /* catch up PCR */ |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 943 | return true; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 944 | } else { |
| 945 | /* render according to FPS */ |
| 946 | if (!VALID_TS(avsync->last_r_syst) || |
| 947 | (int)(systime - avsync->last_r_syst) >= avsync->fps_interval) { |
| 948 | avsync->last_r_syst = systime; |
| 949 | return true; |
| 950 | } |
| 951 | return false; |
| 952 | } |
| 953 | } else if (LIVE_MODE(avsync->mode)) { |
| 954 | /* hold if the gap is small */ |
| 955 | if ((int)(fpts - systime) < avsync->disc_thres_max) { |
| 956 | return false; |
| 957 | } else { |
| 958 | /* render according to FPS */ |
| 959 | if (!VALID_TS(avsync->last_r_syst) || |
| 960 | (int)(systime - avsync->last_r_syst) >= avsync->fps_interval) { |
| 961 | avsync->last_r_syst = systime; |
| 962 | return true; |
| 963 | } |
| 964 | return false; |
| 965 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Song Zhao | 52f5519 | 2021-05-06 10:52:21 -0700 | [diff] [blame] | 969 | /* In some cases, keeping pattern will enlarge the gap */ |
| 970 | if (abs_diff(systime, fpts) > AV_PATTERN_RESET_THRES && |
| 971 | avsync->first_frame_toggled) { |
| 972 | reset_pattern(avsync->pattern_detector); |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 973 | log_warn("sync pattern reset sys:%u fpts:%u", |
Song Zhao | 52f5519 | 2021-05-06 10:52:21 -0700 | [diff] [blame] | 974 | systime, fpts); |
| 975 | } |
| 976 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 977 | expire = (int)(systime - fpts) >= 0; |
| 978 | |
| 979 | /* scatter the frame in different vsync whenever possible */ |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 980 | if (expire && nfpts != -1 && nfpts && toggle_cnt) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 981 | /* multi frame expired in current vsync but no frame in next vsync */ |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 982 | if (systime + interval < nfpts) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 983 | expire = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 984 | log_debug("[%d]unset expire systime:%d inter:%d next_pts:%d toggle_cnt:%d", |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 985 | avsync->session_id, systime, interval, nfpts, toggle_cnt); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 986 | } |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 987 | } else if (!expire && nfpts != -1 && nfpts && !toggle_cnt |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 988 | && avsync->first_frame_toggled) { |
| 989 | /* next vsync will have at least 2 frame expired */ |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 990 | if (systime + interval >= nfpts) { |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 991 | expire = true; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 992 | log_debug("[%d]set expire systime:%d inter:%d next_pts:%d", |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 993 | avsync->session_id, systime, interval, nfpts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
Song Zhao | a58c3e9 | 2021-03-09 18:52:55 -0800 | [diff] [blame] | 997 | if (avsync->state == AV_SYNC_STAT_SYNC_SETUP) |
Song Zhao | 6dce267 | 2022-01-13 11:32:44 -0800 | [diff] [blame] | 998 | correct_pattern(avsync->pattern_detector, fpts, nfpts, |
Song Zhao | a58c3e9 | 2021-03-09 18:52:55 -0800 | [diff] [blame] | 999 | (avsync->last_frame?avsync->last_frame->hold_period:0), |
| 1000 | avsync->last_holding_peroid, systime, |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1001 | interval, &expire); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1002 | |
| 1003 | if (expire) { |
| 1004 | avsync->vpts = fpts; |
| 1005 | /* phase adjustment */ |
| 1006 | if (!avsync->phase_set) { |
yongchun.li | a50b1e9 | 2021-08-07 01:11:54 +0000 | [diff] [blame] | 1007 | //always adjust to the half v-sync to give most pts tolerace and unify behavior |
Song Zhao | 9c09f77 | 2022-01-07 14:26:50 -0800 | [diff] [blame] | 1008 | if ((int)(systime - fpts) >= 0 && |
| 1009 | (int)(fpts + interval - systime) > 0) { |
| 1010 | avsync->phase = interval / 2 + fpts - systime; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1011 | avsync->phase_set = true; |
yongchun.li | a50b1e9 | 2021-08-07 01:11:54 +0000 | [diff] [blame] | 1012 | log_info("[%d]adjust phase to %d", avsync->session_id, (int)avsync->phase); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1013 | } |
Song Zhao | 1561e54 | 2022-01-12 10:37:55 -0800 | [diff] [blame] | 1014 | } else if (get_pattern(avsync->pattern_detector) < 0 && !avsync->phase_adjusted) { |
Song Zhao | 9c09f77 | 2022-01-07 14:26:50 -0800 | [diff] [blame] | 1015 | if ((int)(systime - fpts) >= 0 && |
| 1016 | (int)(fpts + interval - systime) > 0) { |
| 1017 | int vsync_pts_delta = (int)(systime - fpts); |
| 1018 | |
| 1019 | if (vsync_pts_delta < 10 || vsync_pts_delta > (interval - 10)) { |
| 1020 | avsync->phase += interval / 8; |
Song Zhao | 1561e54 | 2022-01-12 10:37:55 -0800 | [diff] [blame] | 1021 | avsync->phase_adjusted = true; |
Song Zhao | 9c09f77 | 2022-01-07 14:26:50 -0800 | [diff] [blame] | 1022 | log_info("[%d] too aligned adjust phase to %d", |
| 1023 | avsync->session_id, (int)avsync->phase); |
| 1024 | } |
| 1025 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1026 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1027 | if (avsync->state != AV_SYNC_STAT_SYNC_SETUP) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1028 | log_info("[%d]sync setup", avsync->session_id); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1029 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
Song Zhao | 5d2b477 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1030 | avsync->sync_lost_cnt = 0; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1031 | } |
| 1032 | return expire; |
| 1033 | } |
| 1034 | |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 1035 | 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] | 1036 | { |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 1037 | bool ret = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1038 | log_trace("[%d]cur_period: %d last_period: %d", |
| 1039 | avsync->session_id, cur_period, last_period); |
Song Zhao | 35a82df | 2021-04-15 10:58:49 -0700 | [diff] [blame] | 1040 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P32, cur_period, last_period)) |
| 1041 | ret = true; |
| 1042 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P22, cur_period, last_period)) |
| 1043 | ret = true; |
| 1044 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P41, cur_period, last_period)) |
| 1045 | ret = true; |
| 1046 | if (detect_pattern(avsync->pattern_detector, AV_SYNC_FRAME_P11, cur_period, last_period)) |
| 1047 | ret = true; |
| 1048 | |
| 1049 | return ret; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | int av_sync_set_speed(void *sync, float speed) |
| 1053 | { |
| 1054 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1055 | |
| 1056 | if (speed < 0.001f || speed > 100) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1057 | 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] | 1058 | return -1; |
| 1059 | } |
| 1060 | |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1061 | if (LIVE_MODE(avsync->mode)) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1062 | 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] | 1063 | return 0; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1064 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1065 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1066 | avsync->speed = speed; |
| 1067 | |
| 1068 | if (avsync->type == AV_SYNC_TYPE_AUDIO) { |
| 1069 | if (speed == 1.0) { |
| 1070 | avsync->mode = avsync->backup_mode; |
| 1071 | log_info("[%d]audio back to mode %d", avsync->session_id, avsync->mode); |
| 1072 | } else { |
| 1073 | avsync->backup_mode = avsync->mode; |
| 1074 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 1075 | log_info("[%d]audio to freerun mode", avsync->session_id); |
| 1076 | } |
| 1077 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1078 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1079 | log_info("session[%d] set rate to %f", avsync->session_id, speed); |
| 1080 | return msync_session_set_rate(avsync->fd, speed); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | int av_sync_change_mode(void *sync, enum sync_mode mode) |
| 1084 | { |
| 1085 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1086 | |
| 1087 | if (!avsync) |
| 1088 | return -1; |
| 1089 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1090 | if (msync_session_set_mode(avsync->fd, mode)) { |
| 1091 | log_error("[%d]fail to set mode %d", avsync->session_id, mode); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1092 | return -1; |
| 1093 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1094 | avsync->mode = mode; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1095 | log_info("[%d]update sync mode to %d", avsync->session_id, mode); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
Song Zhao | 01031bb | 2021-05-13 21:23:20 -0700 | [diff] [blame] | 1099 | int av_sync_get_mode(void *sync, enum sync_mode *mode) |
| 1100 | { |
| 1101 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1102 | |
| 1103 | if (!avsync || !mode) |
| 1104 | return -1; |
| 1105 | |
| 1106 | *mode = avsync->mode; |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1110 | int av_sync_set_pause_pts(void *sync, pts90K pts) |
| 1111 | { |
| 1112 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1113 | |
| 1114 | if (!avsync) |
| 1115 | return -1; |
| 1116 | |
| 1117 | avsync->pause_pts = pts; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1118 | log_info("[%d]set pause pts: %u", avsync->session_id, pts); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | int av_sync_set_pause_pts_cb(void *sync, pause_pts_done cb, void *priv) |
| 1123 | { |
| 1124 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1125 | |
| 1126 | if (!avsync) |
| 1127 | return -1; |
| 1128 | |
| 1129 | avsync->pause_pts_cb = cb; |
| 1130 | avsync->pause_cb_priv = priv; |
| 1131 | return 0; |
| 1132 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1133 | |
yongchun.li | 428390d | 2022-02-17 17:15:40 -0800 | [diff] [blame] | 1134 | int av_sync_set_underflow_check_cb(void *sync, underflow_detected cb, void *priv, struct underflow_config *cfg) |
| 1135 | { |
| 1136 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1137 | |
| 1138 | if (!avsync) |
| 1139 | return -1; |
| 1140 | |
| 1141 | avsync->underflow_cb = cb; |
| 1142 | avsync->underflow_cb_priv = priv; |
| 1143 | |
| 1144 | if (cfg) |
| 1145 | avsync->underflow_cfg.time_thresh = cfg->time_thresh; |
| 1146 | else |
| 1147 | avsync->underflow_cfg.time_thresh = UNDERFLOW_CHECK_THRESH_MS; |
| 1148 | |
| 1149 | log_info("[%d] av_sync_set_underflow_check_cb %p priv %p time %d", |
| 1150 | avsync->session_id, avsync->underflow_cb, avsync->underflow_cb_priv, |
| 1151 | avsync->underflow_cfg.time_thresh); |
| 1152 | return 0; |
| 1153 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1154 | static void trigger_audio_start_cb(struct av_sync_session *avsync, |
| 1155 | avs_ascb_reason reason) |
| 1156 | { |
| 1157 | if (avsync) { |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1158 | log_info("audio start cb"); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1159 | pthread_mutex_lock(&avsync->lock); |
| 1160 | if (avsync->audio_start) { |
| 1161 | avsync->audio_start(avsync->audio_start_priv, reason); |
| 1162 | avsync->session_started = true; |
| 1163 | avsync->audio_start = NULL; |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1164 | avsync->audio_start_priv = NULL; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1165 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
| 1166 | } |
| 1167 | pthread_mutex_unlock(&avsync->lock); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | avs_start_ret av_sync_audio_start( |
| 1172 | void *sync, |
| 1173 | pts90K pts, |
| 1174 | pts90K delay, |
| 1175 | audio_start_cb cb, |
| 1176 | void *priv) |
| 1177 | { |
| 1178 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1179 | uint32_t start_mode; |
yongchun.li | 59e873d | 2021-07-07 11:42:38 -0700 | [diff] [blame] | 1180 | uint32_t systime; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1181 | avs_start_ret ret = AV_SYNC_ASTART_ERR; |
| 1182 | bool create_poll_t = false; |
| 1183 | |
| 1184 | if (!avsync) |
| 1185 | return ret; |
| 1186 | |
yongchun.li | 59e873d | 2021-07-07 11:42:38 -0700 | [diff] [blame] | 1187 | log_info("%d av_sync_audio_start pts(ms) %d delay %d ms", |
| 1188 | avsync->session_id, (int)pts/90, (int)delay/90); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1189 | |
yongchun.li | 59e873d | 2021-07-07 11:42:38 -0700 | [diff] [blame] | 1190 | if (avsync->in_audio_switch && |
| 1191 | avsync->audio_switch_state == AUDIO_SWITCH_STAT_AGAIN) |
| 1192 | { |
| 1193 | start_mode = AVS_START_SYNC; |
| 1194 | log_info("%d AUDIO_SWITCH_STAT_AGAIN", avsync->session_id); |
| 1195 | } else { |
| 1196 | if (msync_session_set_audio_start(avsync->fd, pts, delay, &start_mode)) |
| 1197 | log_error("[%d]fail to set audio start", avsync->session_id); |
| 1198 | } |
| 1199 | if (avsync->in_audio_switch && |
| 1200 | (avsync->audio_switch_state == AUDIO_SWITCH_STAT_RESET || |
| 1201 | avsync->audio_switch_state == AUDIO_SWITCH_STAT_AGAIN)) { |
| 1202 | msync_session_get_wall(avsync->fd, &systime, NULL); |
| 1203 | if ((int)(systime - pts) > A_ADJ_THREDHOLD_LB && |
| 1204 | start_mode == AVS_START_SYNC) { |
| 1205 | log_info("%d audio_switch audio need drop first.ahead %d ms", |
| 1206 | avsync->session_id, (int)(systime - pts)/90); |
| 1207 | ret = AV_SYNC_ASTART_AGAIN; |
| 1208 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_AGAIN; |
| 1209 | goto exit; |
| 1210 | } |
| 1211 | else { |
| 1212 | int diff = (int)(pts - systime); |
| 1213 | log_info("%d audio_switch_state to start mode %d diff %d ms", |
| 1214 | avsync->session_id, start_mode, diff/90); |
| 1215 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_START; |
| 1216 | if (diff < A_ADJ_THREDHOLD_LB) { |
| 1217 | log_info("%d orig mode %d already close enough direct start", |
| 1218 | avsync->session_id, start_mode); |
| 1219 | start_mode = AVS_START_SYNC; |
| 1220 | } |
| 1221 | } |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1224 | if (start_mode == AVS_START_SYNC) { |
| 1225 | ret = AV_SYNC_ASTART_SYNC; |
| 1226 | avsync->session_started = true; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 1227 | avsync->state = AV_SYNC_STAT_RUNNING; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1228 | } else if (start_mode == AVS_START_ASYNC) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1229 | ret = AV_SYNC_ASTART_ASYNC; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1230 | avsync->state = AV_SYNC_STAT_RUNNING; |
| 1231 | } else if (start_mode == AVS_START_AGAIN) { |
| 1232 | ret = AV_SYNC_ASTART_AGAIN; |
| 1233 | } |
| 1234 | |
| 1235 | if (ret == AV_SYNC_ASTART_AGAIN) |
| 1236 | goto exit; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1237 | |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1238 | if (avsync->mode == AV_SYNC_MODE_AMASTER || |
| 1239 | avsync->in_audio_switch || LIVE_MODE(avsync->mode)) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1240 | create_poll_t = true; |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1241 | |
| 1242 | if (start_mode == AVS_START_ASYNC) { |
| 1243 | if (!cb) { |
| 1244 | log_error("[%d]invalid cb", avsync->session_id); |
| 1245 | return AV_SYNC_ASTART_ERR; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1246 | } |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1247 | avsync->audio_start = cb; |
| 1248 | avsync->audio_start_priv = priv; |
| 1249 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1250 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1251 | if (create_poll_t && !avsync->poll_thread) { |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1252 | int ret; |
| 1253 | |
| 1254 | log_info("[%d]start poll thread", avsync->session_id); |
| 1255 | avsync->quit_poll = false; |
| 1256 | ret = pthread_create(&avsync->poll_thread, NULL, poll_thread, avsync); |
| 1257 | if (ret) { |
| 1258 | log_error("[%d]create poll thread errno %d", avsync->session_id, errno); |
| 1259 | return AV_SYNC_ASTART_ERR; |
| 1260 | } |
| 1261 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1262 | if (LIVE_MODE(avsync->mode)) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1263 | msync_session_get_wall(avsync->fd, &systime, NULL); |
| 1264 | log_info("[%d]return %u w %u pts %u d %u", |
| 1265 | avsync->session_id, ret, systime, pts, delay); |
| 1266 | } |
| 1267 | exit: |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1268 | log_info("[%d]return %u", avsync->session_id, ret); |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
| 1272 | int av_sync_audio_render( |
| 1273 | void *sync, |
| 1274 | pts90K pts, |
| 1275 | struct audio_policy *policy) |
| 1276 | { |
| 1277 | int ret = 0; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 1278 | bool out_lier = false; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1279 | uint32_t systime; |
| 1280 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1281 | avs_audio_action action = AA_SYNC_AA_MAX; |
| 1282 | |
| 1283 | if (!avsync || !policy) |
| 1284 | return -1; |
| 1285 | |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1286 | msync_session_get_wall(avsync->fd, &systime, NULL); |
| 1287 | |
| 1288 | log_trace("audio render pts %u, systime %u, mode %u diff ms %d", |
| 1289 | pts, systime, avsync->mode, (int)(pts-systime)/90); |
| 1290 | |
| 1291 | if (avsync->in_audio_switch |
| 1292 | && avsync->audio_switch_state == AUDIO_SWITCH_STAT_START) { |
| 1293 | if (abs_diff(systime, pts) < A_ADJ_THREDHOLD_HB) { |
| 1294 | log_info("Audio pts in system range sys %u pts %u\n", systime, pts); |
| 1295 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_FINISH; |
| 1296 | action = AV_SYNC_AA_RENDER; |
| 1297 | } else if ((int)(systime - pts) > 0) { |
| 1298 | log_info("[%d] audio change drop %d ms sys %u pts %u", avsync->session_id, |
| 1299 | (int)(systime - pts)/90, systime, pts); |
| 1300 | action = AV_SYNC_AA_DROP; |
| 1301 | } else { |
| 1302 | action = AV_SYNC_AA_INSERT; |
| 1303 | log_info("[%d] audio change insert %d ms sys %u pts %u", avsync->session_id, |
| 1304 | (int)(pts - systime)/90, systime, pts); |
| 1305 | } |
| 1306 | goto done; |
| 1307 | } |
| 1308 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1309 | if (avsync->mode == AV_SYNC_MODE_FREE_RUN || |
| 1310 | avsync->mode == AV_SYNC_MODE_AMASTER) { |
| 1311 | action = AV_SYNC_AA_RENDER; |
| 1312 | goto done; |
| 1313 | } |
| 1314 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1315 | /* stopping procedure, unblock audio rendering */ |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1316 | if (LIVE_MODE(avsync->mode) && |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1317 | avsync->active_mode == AV_SYNC_MODE_FREE_RUN) { |
| 1318 | action = AV_SYNC_AA_DROP; |
| 1319 | goto done; |
| 1320 | } |
| 1321 | |
Song Zhao | 7daf3a1 | 2021-05-10 22:22:25 -0700 | [diff] [blame] | 1322 | if (avsync->mode == AV_SYNC_MODE_FREE_RUN || |
| 1323 | avsync->mode == AV_SYNC_MODE_AMASTER) { |
| 1324 | action = AV_SYNC_AA_RENDER; |
| 1325 | goto done; |
| 1326 | } |
| 1327 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1328 | if (avsync->state == AV_SYNC_STAT_SYNC_SETUP && |
| 1329 | LIVE_MODE(avsync->mode) && |
| 1330 | abs_diff(systime, pts) > STREAM_DISC_THRES) { |
| 1331 | /* outlier by stream error */ |
| 1332 | avsync->outlier_cnt++; |
| 1333 | if (avsync->outlier_cnt > OUTLIER_MAX_CNT) { |
| 1334 | /* treat as disc, just drop current frame */ |
| 1335 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
| 1336 | avsync->outlier_cnt = 0; |
| 1337 | action = AV_SYNC_AA_DROP; |
| 1338 | systime = pts; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1339 | goto done; |
| 1340 | } |
| 1341 | log_info("[%d]ignore outlier %u", avsync->session_id, pts); |
| 1342 | pts = systime; |
| 1343 | action = AV_SYNC_AA_RENDER; |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 1344 | out_lier = true; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1345 | goto done; |
| 1346 | } |
| 1347 | |
| 1348 | avsync->outlier_cnt = 0; |
| 1349 | /* low bound from sync_lost to sync_setup */ |
| 1350 | if (abs_diff(systime, pts) < A_ADJ_THREDHOLD_LB) { |
| 1351 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
| 1352 | action = AV_SYNC_AA_RENDER; |
| 1353 | goto done; |
| 1354 | } |
| 1355 | |
| 1356 | /* high bound of sync_setup */ |
| 1357 | if (abs_diff(systime, pts) < A_ADJ_THREDHOLD_HB && |
| 1358 | avsync->state != AV_SYNC_STAT_SYNC_LOST) { |
| 1359 | avsync->state = AV_SYNC_STAT_SYNC_SETUP; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1360 | action = AV_SYNC_AA_RENDER; |
| 1361 | goto done; |
| 1362 | } |
| 1363 | |
| 1364 | if ((int)(systime - pts) > 0) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1365 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1366 | action = AV_SYNC_AA_DROP; |
| 1367 | goto done; |
| 1368 | } |
| 1369 | |
| 1370 | if ((int)(systime - pts) < 0) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1371 | avsync->state = AV_SYNC_STAT_SYNC_LOST; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1372 | action = AV_SYNC_AA_INSERT; |
| 1373 | goto done; |
| 1374 | } |
| 1375 | |
| 1376 | done: |
| 1377 | policy->action = action; |
| 1378 | policy->delta = (int)(systime - pts); |
| 1379 | if (action == AV_SYNC_AA_RENDER) { |
| 1380 | avsync->apts = pts; |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1381 | if (!avsync->in_audio_switch) { |
Song Zhao | 065800e | 2021-05-26 15:56:06 -0700 | [diff] [blame] | 1382 | if (!out_lier) |
| 1383 | msync_session_update_apts(avsync->fd, systime, pts, 0); |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 1384 | log_debug("[%d]return %d sys %u - pts %u = %d", |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 1385 | avsync->session_id, action, systime, pts, systime - pts); |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1386 | } else if(avsync->audio_switch_state == AUDIO_SWITCH_STAT_FINISH) { |
| 1387 | msync_session_update_apts(avsync->fd, systime, pts, 0); |
| 1388 | log_info("[%d] audio switch done sys %u pts %u", |
| 1389 | avsync->session_id, systime, pts); |
| 1390 | msync_session_set_audio_switch(avsync->fd, false); |
| 1391 | avsync->in_audio_switch = false; |
| 1392 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_INIT; |
| 1393 | } else { |
| 1394 | log_trace("[%d] in audio switch ret %d sys %u - pts %u = %d", |
| 1395 | avsync->session_id, action, systime, pts, systime - pts); |
| 1396 | } |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 1397 | avsync->audio_drop_cnt = 0; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1398 | } else { |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 1399 | if (abs_diff(systime, pts) > avsync->disc_thres_min && |
yongchun.li | 085b5a4 | 2021-05-24 04:22:53 -0700 | [diff] [blame] | 1400 | avsync->last_disc_pts != pts && |
| 1401 | !avsync->in_audio_switch) { |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 1402 | log_info ("[%d]audio disc %u --> %u", |
| 1403 | avsync->session_id, systime, pts); |
| 1404 | msync_session_set_audio_dis(avsync->fd, pts); |
| 1405 | avsync->last_disc_pts = pts; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 1406 | } else if (action == AV_SYNC_AA_DROP) { |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 1407 | struct timespec now; |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 1408 | |
| 1409 | /* dropping recovery */ |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame] | 1410 | clock_gettime(CLOCK_MONOTONIC_RAW, &now); |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 1411 | if (!avsync->audio_drop_cnt) |
| 1412 | avsync->audio_drop_start = now; |
| 1413 | avsync->audio_drop_cnt++; |
| 1414 | if (time_diff(&now, &avsync->audio_drop_start) > 500000) { |
| 1415 | log_info ("[%d]audio keep dropping sys %u vs a %u", |
| 1416 | avsync->session_id, systime, pts); |
| 1417 | msync_session_set_audio_dis(avsync->fd, pts); |
| 1418 | } |
Song Zhao | 409739b | 2021-05-12 22:21:40 -0700 | [diff] [blame] | 1419 | } |
Song Zhao | a2985cb | 2021-06-24 12:01:47 -0700 | [diff] [blame] | 1420 | if (action != AV_SYNC_AA_DROP) |
| 1421 | avsync->audio_drop_cnt = 0; |
Song Zhao | f46932e | 2021-05-21 01:51:45 -0700 | [diff] [blame] | 1422 | log_debug("[%d]return %d sys %u - pts %u = %d", |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1423 | avsync->session_id, action, systime, pts, systime - pts); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | int av_sync_get_clock(void *sync, pts90K *pts) |
| 1430 | { |
| 1431 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1432 | |
| 1433 | if (!avsync || !pts) |
| 1434 | return -1; |
| 1435 | return msync_session_get_wall(avsync->fd, pts, NULL); |
| 1436 | } |
| 1437 | |
| 1438 | static void handle_mode_change_a(struct av_sync_session* avsync, |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1439 | enum internal_sync_stat stat, |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1440 | bool v_active, bool a_active, bool v_timeout) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1441 | { |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1442 | log_info("[%d]av_sync amode %d mode %d v/a/vt %d/%d/%d stat %d", |
| 1443 | avsync->session_id, avsync->active_mode, avsync->mode, |
| 1444 | v_active, a_active, v_timeout, stat); |
| 1445 | |
| 1446 | /* iptv delayed start */ |
| 1447 | if (avsync->mode == AV_SYNC_MODE_IPTV && avsync->audio_start) |
| 1448 | trigger_audio_start_cb(avsync, AV_SYNC_ASCB_OK); |
| 1449 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1450 | if (avsync->active_mode == AV_SYNC_MODE_AMASTER) { |
| 1451 | float speed; |
Song Zhao | 7e8c68f | 2022-07-12 16:24:30 -0700 | [diff] [blame] | 1452 | if (a_active && avsync->audio_start) { |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1453 | if (v_active || v_timeout || avsync->in_audio_switch) |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1454 | trigger_audio_start_cb(avsync, AV_SYNC_ASCB_OK); |
Song Zhao | 6183ca9 | 2022-07-29 09:57:03 -0700 | [diff] [blame^] | 1455 | } else if (!a_active && !avsync->session_started) { |
| 1456 | /* quit waiting ASAP */ |
| 1457 | trigger_audio_start_cb(avsync, AV_SYNC_ASCB_STOP); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | if (!msync_session_get_rate(avsync->fd, &speed)) { |
| 1461 | /* speed change is triggered by asink, |
| 1462 | * attached audio HAL will handle it |
| 1463 | */ |
| 1464 | if (speed != avsync->speed) |
| 1465 | log_info("[%d]new rate %f", avsync->session_id, speed); |
| 1466 | if (speed == 1.0) { |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1467 | if (avsync->mode != avsync->backup_mode) { |
| 1468 | avsync->mode = avsync->backup_mode; |
| 1469 | log_info("[%d]audio back to mode %d", avsync->session_id, avsync->mode); |
| 1470 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1471 | } else { |
| 1472 | avsync->backup_mode = avsync->mode; |
| 1473 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 1474 | log_info("[%d]audio to freerun mode", avsync->session_id); |
| 1475 | } |
| 1476 | avsync->speed = speed; |
| 1477 | } |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1478 | } else if (avsync->active_mode == AV_SYNC_MODE_PCR_MASTER) { |
| 1479 | struct session_debug debug; |
| 1480 | if (!msync_session_get_debug_mode(avsync->fd, &debug)) { |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1481 | if (debug.debug_freerun && !avsync->debug_freerun) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1482 | avsync->backup_mode = avsync->mode; |
| 1483 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1484 | avsync->debug_freerun = true; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1485 | log_warn("[%d]audio to freerun mode", avsync->session_id); |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1486 | } else if (!debug.debug_freerun && avsync->debug_freerun) { |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1487 | avsync->mode = avsync->backup_mode; |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1488 | avsync->debug_freerun = false; |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1489 | log_warn("[%d]audio back to mode %d", |
| 1490 | avsync->session_id, avsync->mode); |
| 1491 | } |
| 1492 | } |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1493 | } else if (avsync->active_mode == AV_SYNC_MODE_VMASTER) { |
| 1494 | log_info("[%d]running in vmaster mode", avsync->session_id); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | static void handle_mode_change_v(struct av_sync_session* avsync, |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1499 | enum internal_sync_stat stat, |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1500 | bool v_active, bool a_active, bool v_timeout) |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1501 | { |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1502 | struct session_debug debug; |
| 1503 | |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1504 | log_info("[%d]av_sync amode mode %d %d v/a %d/%d stat %d", avsync->session_id, |
| 1505 | avsync->active_mode, avsync->mode, v_active, a_active, stat); |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1506 | if (!msync_session_get_debug_mode(avsync->fd, &debug)) { |
| 1507 | if (debug.debug_freerun && !avsync->debug_freerun) { |
| 1508 | avsync->backup_mode = avsync->mode; |
| 1509 | avsync->mode = AV_SYNC_MODE_FREE_RUN; |
| 1510 | avsync->debug_freerun = true; |
| 1511 | log_warn("[%d]video to freerun mode", avsync->session_id); |
| 1512 | } else if (!debug.debug_freerun && avsync->debug_freerun) { |
| 1513 | avsync->mode = avsync->backup_mode; |
| 1514 | avsync->debug_freerun = false; |
| 1515 | log_warn("[%d]video back to mode %d", |
| 1516 | avsync->session_id, avsync->mode); |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1517 | } |
| 1518 | } |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static void * poll_thread(void * arg) |
| 1522 | { |
| 1523 | int ret = 0; |
| 1524 | struct av_sync_session *avsync = (struct av_sync_session *)arg; |
| 1525 | const int fd = avsync->fd; |
| 1526 | struct pollfd pfd = { |
| 1527 | /* default blocking capture */ |
| 1528 | .events = POLLIN | POLLRDNORM | POLLPRI | POLLOUT | POLLWRNORM, |
| 1529 | .fd = avsync->fd, |
| 1530 | }; |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1531 | enum src_flag sflag = SRC_A; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1532 | |
| 1533 | prctl (PR_SET_NAME, "avs_poll"); |
| 1534 | log_info("[%d]enter", avsync->session_id); |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1535 | |
| 1536 | if (avsync->type == AV_SYNC_TYPE_AUDIO) |
| 1537 | sflag = SRC_A; |
| 1538 | else if (avsync->type == AV_SYNC_TYPE_VIDEO) |
| 1539 | sflag = SRC_V; |
| 1540 | |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1541 | while (!avsync->quit_poll) { |
| 1542 | for (;;) { |
| 1543 | ret = poll(&pfd, 1, 10); |
| 1544 | if (ret > 0) |
| 1545 | break; |
| 1546 | if (avsync->quit_poll) |
| 1547 | goto exit; |
| 1548 | if (errno == EINTR) |
| 1549 | continue; |
| 1550 | } |
| 1551 | |
| 1552 | /* error handling */ |
| 1553 | if (pfd.revents & POLLERR) |
| 1554 | log_error("[%d]POLLERR received", avsync->session_id); |
| 1555 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1556 | /* mode change. Non-exclusive wait so all the processes |
| 1557 | * shall be woken up |
| 1558 | */ |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1559 | if (pfd.revents & POLLPRI) { |
Song Zhao | e208d69 | 2021-04-19 15:38:52 -0700 | [diff] [blame] | 1560 | bool v_active, a_active, v_timeout; |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1561 | enum internal_sync_stat stat; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1562 | |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1563 | msync_session_get_stat(fd, &avsync->active_mode, &stat, |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1564 | &v_active, &a_active, &v_timeout, &avsync->in_audio_switch, sflag); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1565 | |
| 1566 | if (avsync->type == AV_SYNC_TYPE_AUDIO) |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1567 | handle_mode_change_a(avsync, stat, v_active, a_active, v_timeout); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1568 | else if (avsync->type == AV_SYNC_TYPE_VIDEO) |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1569 | handle_mode_change_v(avsync, stat, v_active, a_active, v_timeout); |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1570 | usleep(10000); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | exit: |
| 1574 | log_info("[%d]quit", avsync->session_id); |
| 1575 | return NULL; |
| 1576 | } |
| 1577 | |
Song Zhao | 623e2f1 | 2021-09-03 15:54:04 -0700 | [diff] [blame] | 1578 | #define DEMOD_NODE "/sys/class/dtvdemod/atsc_para" |
| 1579 | /* return ppm between demod and PCR clock */ |
| 1580 | int32_t static dmod_get_sfo_dev(struct av_sync_session *avsync) |
| 1581 | { |
| 1582 | int fd = -1, ppm = 0, nread; |
| 1583 | char buf[128]; |
| 1584 | uint32_t reg_v, lock; |
| 1585 | float val; |
| 1586 | |
| 1587 | fd = open(DEMOD_NODE, O_RDWR); |
| 1588 | if (fd < 0) { |
| 1589 | log_warn("node not found %s", DEMOD_NODE); |
| 1590 | /* do not retry */ |
| 1591 | avsync->ppm_adjusted = true; |
| 1592 | return 0; |
| 1593 | } |
| 1594 | snprintf(buf, sizeof(buf), "%d", 5); |
| 1595 | write(fd, buf, 2); |
| 1596 | |
| 1597 | lseek(fd, 0, SEEK_SET); |
| 1598 | |
| 1599 | nread = read(fd, buf, sizeof(buf)); |
| 1600 | if (nread <= 0) { |
| 1601 | log_error("read error"); |
| 1602 | goto err; |
| 1603 | } |
| 1604 | buf[nread] = 0; |
| 1605 | if (sscanf(buf, "ck=0x%x lock=%d", ®_v, &lock) != 2) { |
| 1606 | log_error("wrong format %s", buf); |
| 1607 | goto err; |
| 1608 | } |
| 1609 | if (lock != 0x1f) { |
| 1610 | log_info("demod not locked"); |
| 1611 | goto err; |
| 1612 | } |
| 1613 | if (reg_v > ((2 << 20) - 1)) |
| 1614 | reg_v -= (2 << 21); |
| 1615 | val = reg_v * 10.762238f / 12 * 1000000 / (2 << 25); |
| 1616 | ppm = val; |
| 1617 | log_info("ppm from SFO %d", ppm); |
| 1618 | avsync->ppm_adjusted = true; |
| 1619 | |
| 1620 | err: |
| 1621 | if (fd >= 0) |
| 1622 | close(fd); |
| 1623 | return ppm; |
| 1624 | } |
| 1625 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1626 | 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] | 1627 | { |
| 1628 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 1629 | struct pcr_info pcr; |
| 1630 | enum pcr_monitor_status status; |
| 1631 | int ppm; |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1632 | if (!avsync) |
| 1633 | return -1; |
| 1634 | |
| 1635 | if (avsync->type != AV_SYNC_TYPE_PCR) |
| 1636 | return -2; |
| 1637 | |
Song Zhao | 623e2f1 | 2021-09-03 15:54:04 -0700 | [diff] [blame] | 1638 | /* initial estimation from Demod SFO HW */ |
| 1639 | if (!avsync->ppm_adjusted) { |
| 1640 | ppm = dmod_get_sfo_dev(avsync); |
| 1641 | if (ppm != 0) { |
| 1642 | /* ppm > 0 means board clock is faster */ |
| 1643 | msync_session_set_clock_dev(avsync->fd, -ppm); |
| 1644 | } |
| 1645 | } |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 1646 | pcr.monoclk = mono_clock / 1000; |
| 1647 | pcr.pts = (long long) pts * 1000 / 90; |
| 1648 | pcr_monitor_process(avsync->pcr_monitor, &pcr); |
| 1649 | |
| 1650 | status = pcr_monitor_get_status(avsync->pcr_monitor); |
| 1651 | |
| 1652 | if (status >= DEVIATION_READY) { |
| 1653 | pcr_monitor_get_deviation(avsync->pcr_monitor, &ppm); |
| 1654 | if (avsync->ppm != ppm) { |
| 1655 | avsync->ppm = ppm; |
| 1656 | log_info("[%d]ppm:%d", avsync->session_id, ppm); |
| 1657 | if (msync_session_set_clock_dev(avsync->fd, ppm)) |
| 1658 | log_error("set clock dev fail"); |
Song Zhao | 623e2f1 | 2021-09-03 15:54:04 -0700 | [diff] [blame] | 1659 | else |
| 1660 | avsync->ppm_adjusted = true; |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1664 | return msync_session_set_pcr(avsync->fd, pts, mono_clock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1667 | 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] | 1668 | { |
| 1669 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1670 | |
| 1671 | if (!avsync) |
| 1672 | return -1; |
| 1673 | |
Song Zhao | d62bb39 | 2021-04-23 12:25:49 -0700 | [diff] [blame] | 1674 | return msync_session_get_pcr(avsync->fd, pts, mono_clock); |
Song Zhao | ea5a041 | 2021-01-18 16:40:08 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | int av_sync_set_session_name(void *sync, const char *name) |
| 1678 | { |
| 1679 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1680 | |
| 1681 | if (!avsync) |
| 1682 | return -1; |
| 1683 | |
| 1684 | return msync_session_set_name(avsync->fd, name); |
| 1685 | } |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1686 | |
| 1687 | int av_sync_set_audio_switch(void *sync, bool start) |
| 1688 | { |
| 1689 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1690 | bool v_active, a_active, v_timeout; |
| 1691 | |
| 1692 | if (!avsync) |
| 1693 | return -1; |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1694 | if (msync_session_get_stat(avsync->fd, &avsync->active_mode, NULL, |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1695 | &v_active, &a_active, |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1696 | &v_timeout, &avsync->in_audio_switch, SRC_A)) { |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1697 | log_error("[%d] can not get session state", |
| 1698 | avsync->session_id); |
| 1699 | return -1; |
| 1700 | } |
| 1701 | if (!v_active || !a_active) { |
| 1702 | log_error("[%d] no apply if not AV both active v %d a %d", |
| 1703 | avsync->session_id, v_active, a_active); |
| 1704 | return -1; |
| 1705 | } |
| 1706 | if (msync_session_set_audio_switch(avsync->fd, start)) { |
| 1707 | log_error("[%d]fail to set audio switch %d", avsync->session_id, start); |
| 1708 | return -1; |
| 1709 | } |
| 1710 | avsync->in_audio_switch = start; |
| 1711 | avsync->audio_switch_state = AUDIO_SWITCH_STAT_INIT; |
| 1712 | log_info("[%d]update audio switch to %d", avsync->session_id, start); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | int av_sync_get_audio_switch(void *sync, bool *start) |
| 1717 | { |
| 1718 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1719 | |
| 1720 | if (!avsync) |
| 1721 | return -1; |
Song Zhao | 7600528 | 2022-03-08 16:49:41 -0800 | [diff] [blame] | 1722 | if (msync_session_get_stat(avsync->fd, &avsync->active_mode, NULL, |
Song Zhao | e0bf6ad | 2021-07-09 11:28:42 -0700 | [diff] [blame] | 1723 | NULL, NULL, NULL, &avsync->in_audio_switch, SRC_A)) { |
yongchun.li | 107a616 | 2021-05-05 02:38:57 -0700 | [diff] [blame] | 1724 | log_error("[%d] can not audio seamless switch state", |
| 1725 | avsync->session_id); |
| 1726 | return -1; |
| 1727 | } |
| 1728 | if (start) *start = avsync->in_audio_switch; |
| 1729 | return 0; |
Song Zhao | 7daf3a1 | 2021-05-10 22:22:25 -0700 | [diff] [blame] | 1730 | } |
Song Zhao | 8039f56 | 2021-05-18 18:11:25 -0700 | [diff] [blame] | 1731 | |
Song Zhao | 623e2f1 | 2021-09-03 15:54:04 -0700 | [diff] [blame] | 1732 | enum clock_recovery_stat av_sync_get_clock_deviation(void *sync, int32_t *ppm) |
Song Zhao | 8039f56 | 2021-05-18 18:11:25 -0700 | [diff] [blame] | 1733 | { |
| 1734 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1735 | |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 1736 | if (!avsync || !ppm) |
| 1737 | return CLK_RECOVERY_ERR; |
| 1738 | if (avsync->mode != AV_SYNC_MODE_PCR_MASTER) |
Song Zhao | 8039f56 | 2021-05-18 18:11:25 -0700 | [diff] [blame] | 1739 | return CLK_RECOVERY_NOT_RUNNING; |
| 1740 | |
wei.du | bcc2ed2 | 2021-05-19 07:16:10 -0400 | [diff] [blame] | 1741 | if (msync_session_get_clock_dev(avsync->fd, ppm)) |
| 1742 | return CLK_RECOVERY_ERR; |
| 1743 | |
| 1744 | if (*ppm == 0) |
| 1745 | return CLK_RECOVERY_ONGOING; |
| 1746 | else |
| 1747 | return CLK_RECOVERY_READY; |
Song Zhao | 8039f56 | 2021-05-18 18:11:25 -0700 | [diff] [blame] | 1748 | } |
Song Zhao | 95bd092 | 2021-09-21 14:07:46 -0700 | [diff] [blame] | 1749 | |
| 1750 | static int video_mono_push_frame(struct av_sync_session *avsync, struct vframe *frame) |
| 1751 | { |
| 1752 | int ret; |
| 1753 | |
| 1754 | if (!avsync->frame_q) { |
| 1755 | avsync->frame_q = create_q(MAX_FRAME_NUM); |
| 1756 | if (!avsync->frame_q) { |
| 1757 | log_error("[%d]create queue fail", avsync->session_id); |
| 1758 | |
| 1759 | return -1; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | ret = queue_item(avsync->frame_q, frame); |
| 1764 | if (ret) |
| 1765 | log_error("%s queue fail:%d", ret); |
| 1766 | log_debug("[%d]push %llu, QNum=%d", avsync->session_id, frame->mts, queue_size(avsync->frame_q)); |
| 1767 | return ret; |
| 1768 | } |
| 1769 | |
| 1770 | int av_sync_set_vsync_mono_time(void *sync , uint64_t msys) |
| 1771 | { |
| 1772 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1773 | |
| 1774 | if (!avsync) |
| 1775 | return -1; |
| 1776 | avsync->msys = msys; |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | static struct vframe * video_mono_pop_frame(struct av_sync_session *avsync) |
| 1781 | { |
| 1782 | struct vframe *frame = NULL, *enter_last_frame = NULL; |
| 1783 | uint64_t systime; |
| 1784 | int toggle_cnt = 0; |
| 1785 | |
| 1786 | enter_last_frame = avsync->last_frame; |
| 1787 | systime = avsync->msys; |
| 1788 | log_debug("[%d]sys %llu", avsync->session_id, systime); |
| 1789 | while (!peek_item(avsync->frame_q, (void **)&frame, 0)) { |
| 1790 | if (systime >= frame->mts) { |
| 1791 | log_debug("[%d]cur_f %llu expire", avsync->session_id, frame->mts); |
| 1792 | toggle_cnt++; |
| 1793 | |
| 1794 | if (avsync->last_frame) |
| 1795 | avsync->last_holding_peroid = avsync->last_frame->hold_period; |
| 1796 | |
| 1797 | dqueue_item(avsync->frame_q, (void **)&frame); |
| 1798 | if (avsync->last_frame) { |
| 1799 | /* free frame that are not for display */ |
| 1800 | if (toggle_cnt > 1) { |
| 1801 | log_debug("[%d]free %llu cur %llu system %llu", avsync->session_id, |
| 1802 | avsync->last_frame->mts, frame->mts, systime); |
| 1803 | avsync->last_frame->free(avsync->last_frame); |
| 1804 | } |
| 1805 | } else { |
| 1806 | avsync->first_frame_toggled = true; |
| 1807 | log_info("[%d]first frame %llu", avsync->session_id, frame->mts); |
| 1808 | } |
| 1809 | avsync->last_frame = frame; |
| 1810 | } else |
| 1811 | break; |
| 1812 | } |
| 1813 | |
| 1814 | if (avsync->last_frame) { |
| 1815 | if (enter_last_frame != avsync->last_frame) |
| 1816 | log_debug("[%d]pop %llu", avsync->session_id, avsync->last_frame->pts); |
| 1817 | log_trace("[%d]pop=%llu, system=%llu, diff %d(ms), QNum=%d", avsync->session_id, |
| 1818 | avsync->last_frame->mts, |
| 1819 | systime, (systime - avsync->last_frame->mts) / 1000000, |
| 1820 | queue_size(avsync->frame_q)); |
| 1821 | } else |
| 1822 | if (enter_last_frame != avsync->last_frame) |
| 1823 | log_debug("[%d]pop (nil)", avsync->session_id); |
| 1824 | |
| 1825 | if (avsync->last_frame) |
| 1826 | avsync->last_frame->hold_period++; |
| 1827 | return avsync->last_frame; |
| 1828 | } |
Song Zhao | 6183ca9 | 2022-07-29 09:57:03 -0700 | [diff] [blame^] | 1829 | |
| 1830 | int avs_sync_stop_audio(void *sync) |
| 1831 | { |
| 1832 | struct av_sync_session *avsync = (struct av_sync_session *)sync; |
| 1833 | |
| 1834 | if (!avsync) |
| 1835 | return -1; |
| 1836 | |
| 1837 | return msync_session_stop_audio(avsync->fd); |
| 1838 | } |