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