|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Teluu Inc. (http://www.teluu.com) |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | + */ |
| 18 | +#ifndef __PJMEDIA_AV_SYNC_H__ |
| 19 | +#define __PJMEDIA_AV_SYNC_H__ |
| 20 | + |
| 21 | + /** |
| 22 | + * @file av_sync.h |
| 23 | + * @brief Inter-media Synchronization. |
| 24 | + */ |
| 25 | +#include <pjmedia/types.h> |
| 26 | + |
| 27 | + |
| 28 | +PJ_BEGIN_DECL |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * @defgroup PJMEDIA_AV_SYNC Inter-media Synchronization |
| 33 | + * @ingroup PJMEDIA_SESSION |
| 34 | + * @brief Synchronize presentation time of multiple media in a session. |
| 35 | + * @{ |
| 36 | + * |
| 37 | + * A call session may consist of multiple media, e.g: some audio and some |
| 38 | + * video, which frequently have different delays when presented in the |
| 39 | + * receiver side. This module synchronizes all media in the same session |
| 40 | + * based on NTP timestamp & RTP timestamp info provided by the sender in |
| 41 | + * RTCP SR. |
| 42 | + * |
| 43 | + * Here are steps to use this module: |
| 44 | + * 1. Create AV sync using #pjmedia_av_sync_create(). |
| 45 | + * 2. Adds all media to be synchronized using #pjmedia_av_sync_add_media(). |
| 46 | + * 3. Call #pjmedia_av_sync_update_ref() each time the media receiving |
| 47 | + * an RTCP SR packet. |
| 48 | + * 4. Call #pjmedia_av_sync_update_pts() each time the media returning |
| 49 | + * a frame to be presented, e.g: via port.get_frame(). The function may |
| 50 | + * request the media to adjust its delay. |
| 51 | + * 5. Call #pjmedia_av_sync_del_media() when a media is removed from the |
| 52 | + * session. |
| 53 | + * 6. Call #pjmedia_av_sync_destroy() when the session is ended. |
| 54 | + * |
| 55 | + * The primary synchronization logic is implemented within the |
| 56 | + * #pjmedia_av_sync_update_pts() function. This function will calculate |
| 57 | + * the lag between the calling media to the earliest media and will provide |
| 58 | + * a feedback to the calling media whether it is in synchronized state, |
| 59 | + * late, or early so the media can respond accordingly. |
| 60 | + * Initially this function will try to request slower media to speed up. |
| 61 | + * If after a specific number of requests (i.e: configurable via |
| 62 | + * PJMEDIA_AVSYNC_MAX_SPEEDUP_REQ_CNT) and the lag is still beyond a tolerable |
| 63 | + * value (i.e: configurable via PJMEDIA_AVSYNC_MAX_TOLERABLE_LAG_MSEC), the |
| 64 | + * function will issue slow down request to the fastest media. |
| 65 | + */ |
| 66 | + |
| 67 | + |
| 68 | +/** |
| 69 | + * Inter-media synchronizer, opaque. |
| 70 | + */ |
| 71 | +typedef struct pjmedia_av_sync pjmedia_av_sync; |
| 72 | + |
| 73 | + |
| 74 | +/** |
| 75 | + * Media synchronization handle, opaque. |
| 76 | + */ |
| 77 | +typedef struct pjmedia_av_sync_media pjmedia_av_sync_media; |
| 78 | + |
| 79 | + |
| 80 | +/** |
| 81 | + * Synchronizer settings. |
| 82 | + */ |
| 83 | +typedef struct { |
| 84 | + /** |
| 85 | + * Name of the syncrhonizer |
| 86 | + */ |
| 87 | + char *name; |
| 88 | + |
| 89 | + /** |
| 90 | + * Streaming mode. If set to PJ_TRUE, the delay adjustment values will |
| 91 | + * be smoothened and marked up to prevent possible delay increase on |
| 92 | + * all media. |
| 93 | + */ |
| 94 | + pj_bool_t is_streaming; |
| 95 | + |
| 96 | +} pjmedia_av_sync_setting; |
| 97 | + |
| 98 | + |
| 99 | +/** |
| 100 | + * Media settings. |
| 101 | + */ |
| 102 | +typedef struct { |
| 103 | + /** |
| 104 | + * Name of the media |
| 105 | + */ |
| 106 | + char *name; |
| 107 | + |
| 108 | + /** |
| 109 | + * Media type. |
| 110 | + */ |
| 111 | + pjmedia_type type; |
| 112 | + |
| 113 | + /** |
| 114 | + * Media clock rate or sampling rate. |
| 115 | + */ |
| 116 | + unsigned clock_rate; |
| 117 | + |
| 118 | +} pjmedia_av_sync_media_setting; |
| 119 | + |
| 120 | + |
| 121 | +/** |
| 122 | + * Get default settings for synchronizer. |
| 123 | + * |
| 124 | + * @param setting The synchronizer settings. |
| 125 | + */ |
| 126 | +PJ_DECL(void) pjmedia_av_sync_setting_default( |
| 127 | + pjmedia_av_sync_setting *setting); |
| 128 | + |
| 129 | +/** |
| 130 | + * Get default settings for media. |
| 131 | + * |
| 132 | + * @param setting The media settings. |
| 133 | + */ |
| 134 | +PJ_DECL(void) pjmedia_av_sync_media_setting_default( |
| 135 | + pjmedia_av_sync_media_setting *setting); |
| 136 | + |
| 137 | +/** |
| 138 | + * Create media synchronizer. |
| 139 | + * |
| 140 | + * @param pool The memory pool. |
| 141 | + * @param option The synchronizer settings. |
| 142 | + * @param av_sync The pointer to receive the media synchronizer. |
| 143 | + * |
| 144 | + * @return PJ_SUCCESS on success. |
| 145 | + */ |
| 146 | +PJ_DECL(pj_status_t) pjmedia_av_sync_create( |
| 147 | + pj_pool_t *pool, |
| 148 | + const pjmedia_av_sync_setting *setting, |
| 149 | + pjmedia_av_sync **av_sync); |
| 150 | + |
| 151 | + |
| 152 | +/** |
| 153 | + * Destroy media synchronizer. |
| 154 | + * |
| 155 | + * @param av_sync The media synchronizer. |
| 156 | + */ |
| 157 | +PJ_DECL(void) pjmedia_av_sync_destroy(pjmedia_av_sync *av_sync); |
| 158 | + |
| 159 | + |
| 160 | +/** |
| 161 | + * Reset synchronization states. Any existing media will NOT be removed, |
| 162 | + * but their states will be reset. |
| 163 | + * |
| 164 | + * @param av_sync The media synchronizer. |
| 165 | + * |
| 166 | + * @return PJ_SUCCESS on success. |
| 167 | + */ |
| 168 | +PJ_DECL(pj_status_t) pjmedia_av_sync_reset(pjmedia_av_sync *av_sync); |
| 169 | + |
| 170 | + |
| 171 | +/** |
| 172 | + * Add a media to synchronizer. |
| 173 | + * |
| 174 | + * @param av_sync The media synchronizer. |
| 175 | + * @param setting The media settings. |
| 176 | + * @param av_sync_media The pointer to receive the media synchronization |
| 177 | + * handle. |
| 178 | + * |
| 179 | + * @return PJ_SUCCESS on success. |
| 180 | + */ |
| 181 | +PJ_DECL(pj_status_t) pjmedia_av_sync_add_media( |
| 182 | + pjmedia_av_sync* av_sync, |
| 183 | + const pjmedia_av_sync_media_setting *setting, |
| 184 | + pjmedia_av_sync_media **av_sync_media); |
| 185 | + |
| 186 | + |
| 187 | +/** |
| 188 | + * Remove a media from synchronizer. |
| 189 | + * |
| 190 | + * @param av_sync The media synchronizer. |
| 191 | + * @param av_sync_media The media synchronization handle. |
| 192 | + * |
| 193 | + * @return PJ_SUCCESS on success. |
| 194 | + */ |
| 195 | +PJ_DECL(pj_status_t) pjmedia_av_sync_del_media( |
| 196 | + pjmedia_av_sync *av_sync, |
| 197 | + pjmedia_av_sync_media *av_sync_media); |
| 198 | + |
| 199 | + |
| 200 | +/** |
| 201 | + * Update synchronizer about the last presentation timestamp of the specified |
| 202 | + * media. Normally this function is called each time the media produces |
| 203 | + * a frame to be rendered (e.g: in port's get_frame() method). Upon returning, |
| 204 | + * the media may be requested to adjust its delay so it matches to the |
| 205 | + * earliest or the latest media, i.e: by speeding up or slowing down. |
| 206 | + * |
| 207 | + * Initially this function will try to request slower media to speed up. |
| 208 | + * If after a specific number of requests (i.e: configurable via |
| 209 | + * PJMEDIA_AVSYNC_MAX_SPEEDUP_REQ_CNT) and the lag is still beyond a tolerable |
| 210 | + * value (i.e: configurable via PJMEDIA_AVSYNC_MAX_TOLERABLE_LAG_MSEC), the |
| 211 | + * function will issue slow down request to the fastest media. |
| 212 | + * |
| 213 | + * @param av_sync_media The media synchronization handle. |
| 214 | + * @param pts The presentation timestamp. |
| 215 | + * @param adjust_delay Optional pointer to receive adjustment delay |
| 216 | + * required, in milliseconds, to make this media |
| 217 | + * synchronized to the fastest media. |
| 218 | + * Possible output values are: |
| 219 | + * 0 when no action is needed, |
| 220 | + * possitive value when increasing delay is needed, |
| 221 | + * or negative value when decreasing delay is needed. |
| 222 | + * |
| 223 | + * @return PJ_SUCCESS on success. |
| 224 | + */ |
| 225 | +PJ_DECL(pj_status_t) pjmedia_av_sync_update_pts( |
| 226 | + pjmedia_av_sync_media *av_sync_media, |
| 227 | + const pj_timestamp *pts, |
| 228 | + pj_int32_t *adjust_delay); |
| 229 | + |
| 230 | + |
| 231 | +/** |
| 232 | + * Update synchronizer about reference timestamps of the specified media. |
| 233 | + * Normally this function is called each time the media receives RTCP SR |
| 234 | + * packet. |
| 235 | + * |
| 236 | + * @param av_sync_media The media synchronization handle. |
| 237 | + * @param ntp The NTP timestamp info from RTCP SR. |
| 238 | + * @param ts The RTP timestamp info from RTCP SR. |
| 239 | + * |
| 240 | + * @return PJ_SUCCESS on success. |
| 241 | + */ |
| 242 | +PJ_DECL(pj_status_t) pjmedia_av_sync_update_ref( |
| 243 | + pjmedia_av_sync_media *av_sync_media, |
| 244 | + const pj_timestamp *ntp, |
| 245 | + const pj_timestamp *ts); |
| 246 | + |
| 247 | + |
| 248 | +/** |
| 249 | + * @} |
| 250 | + */ |
| 251 | + |
| 252 | + |
| 253 | +PJ_END_DECL |
| 254 | + |
| 255 | + |
| 256 | +#endif /* __PJMEDIA_AV_SYNC_H__ */ |
0 commit comments