| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2004-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bsynclib_delay_element.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/6 $ |
|---|
| 12 | * $brcm_Date: 3/19/10 2:44p $ |
|---|
| 13 | * |
|---|
| 14 | * Revision History: |
|---|
| 15 | * |
|---|
| 16 | * $brcm_Log: /magnum/syslib/synclib/noarch/bsynclib_delay_element.c $ |
|---|
| 17 | * |
|---|
| 18 | * Hydra_Software_Devel/6 3/19/10 2:44p bandrews |
|---|
| 19 | * SW7405-3774: merge mute control |
|---|
| 20 | * |
|---|
| 21 | * Hydra_Software_Devel/SW7405-3774/2 3/18/10 9:34p bandrews |
|---|
| 22 | * SW7405-4042: rework delay estimation so that imprecise lipsync works |
|---|
| 23 | * correctly |
|---|
| 24 | * |
|---|
| 25 | * Hydra_Software_Devel/SW7405-3774/1 2/17/10 6:17p bandrews |
|---|
| 26 | * SW7405-3774: attempt to fix early audio unmute then mute |
|---|
| 27 | * |
|---|
| 28 | * Hydra_Software_Devel/5 12/10/09 9:18p bandrews |
|---|
| 29 | * SW7401-3634: adding PWC (now JTI) support to synclib |
|---|
| 30 | * |
|---|
| 31 | * Hydra_Software_Devel/4 7/24/09 4:14p bandrews |
|---|
| 32 | * PR52812: Added any-time source or display rate change support |
|---|
| 33 | * |
|---|
| 34 | * Hydra_Software_Devel/3 2/25/09 7:53p bandrews |
|---|
| 35 | * PR52514: needed another const |
|---|
| 36 | * |
|---|
| 37 | * Hydra_Software_Devel/2 10/6/08 8:07p bandrews |
|---|
| 38 | * PR44510: Implement delay capacity for video sinks |
|---|
| 39 | * |
|---|
| 40 | * Hydra_Software_Devel/1 3/24/08 3:09p bandrews |
|---|
| 41 | * PR40865: Fixed |
|---|
| 42 | * |
|---|
| 43 | * Hydra_Software_Devel/9 2/27/08 8:33p bandrews |
|---|
| 44 | * PR37951: Fixed units conversion |
|---|
| 45 | * |
|---|
| 46 | * Hydra_Software_Devel/8 2/26/08 3:19p bandrews |
|---|
| 47 | * PR37951: Fixed expiry of timers |
|---|
| 48 | * |
|---|
| 49 | * Hydra_Software_Devel/7 2/25/08 9:33p bandrews |
|---|
| 50 | * PR37951: Fixed various bugs |
|---|
| 51 | * |
|---|
| 52 | * Hydra_Software_Devel/6 2/22/08 9:20p bandrews |
|---|
| 53 | * PR37951: Added RMD support |
|---|
| 54 | * |
|---|
| 55 | * Hydra_Software_Devel/5 2/22/08 8:28p bandrews |
|---|
| 56 | * PR37951: Fixed bug in units conversion |
|---|
| 57 | * |
|---|
| 58 | * Hydra_Software_Devel/4 2/22/08 3:06p bandrews |
|---|
| 59 | * PR37951: Debugging callbacks |
|---|
| 60 | * |
|---|
| 61 | * Hydra_Software_Devel/3 2/20/08 10:03p bandrews |
|---|
| 62 | * PR37951: Updated based on feedback from usage |
|---|
| 63 | * |
|---|
| 64 | * Hydra_Software_Devel/2 1/3/08 5:17p bandrews |
|---|
| 65 | * PR37951: Updated based on initial feedback |
|---|
| 66 | * |
|---|
| 67 | * Hydra_Software_Devel/1 12/12/07 2:53p bandrews |
|---|
| 68 | * PR37951: Initial check-in |
|---|
| 69 | ***************************************************************************/ |
|---|
| 70 | |
|---|
| 71 | #include "bstd.h" |
|---|
| 72 | #include "bsynclib_delay_element.h" |
|---|
| 73 | |
|---|
| 74 | BDBG_MODULE(synclib); |
|---|
| 75 | |
|---|
| 76 | #ifdef BDBG_DEBUG_BUILD |
|---|
| 77 | const char * const BSYNClib_DelayElement_LifecycleEventNames[] = |
|---|
| 78 | { |
|---|
| 79 | "none", |
|---|
| 80 | "started", |
|---|
| 81 | "stopped" |
|---|
| 82 | }; |
|---|
| 83 | const char * const BSYNClib_DelayElement_SynchronizationEventNames[] = |
|---|
| 84 | { |
|---|
| 85 | "none", |
|---|
| 86 | "synchronized", |
|---|
| 87 | "ignored" |
|---|
| 88 | }; |
|---|
| 89 | #endif |
|---|
| 90 | |
|---|
| 91 | void BSYNClib_DelayElement_Init(BSYNClib_DelayElement * psElement) |
|---|
| 92 | { |
|---|
| 93 | BDBG_ENTER(BSYNClib_DelayElement_Init); |
|---|
| 94 | |
|---|
| 95 | BDBG_ASSERT(psElement); |
|---|
| 96 | |
|---|
| 97 | BKNI_Memset(psElement, 0, sizeof(BSYNClib_DelayElement)); |
|---|
| 98 | |
|---|
| 99 | BSYNClib_Delay_Init(&psElement->sDelay); |
|---|
| 100 | BSYNClib_DelayNotification_Init(&psElement->sNotification); |
|---|
| 101 | |
|---|
| 102 | BDBG_LEAVE(BSYNClib_DelayElement_Init); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | void BSYNClib_DelayElement_Reset_isr(BSYNClib_DelayElement * psElement) |
|---|
| 106 | { |
|---|
| 107 | BDBG_ENTER(BSYNClib_DelayElement_Reset_isr); |
|---|
| 108 | |
|---|
| 109 | BDBG_ASSERT(psElement); |
|---|
| 110 | |
|---|
| 111 | psElement->sData.bStarted = false; |
|---|
| 112 | |
|---|
| 113 | BSYNClib_Delay_Reset_isr(&psElement->sDelay); |
|---|
| 114 | BSYNClib_DelayNotification_Reset_isr(&psElement->sNotification); |
|---|
| 115 | |
|---|
| 116 | BDBG_LEAVE(BSYNClib_DelayElement_Reset_isr); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void BSYNClib_DelayElement_Snapshot_isr(BSYNClib_DelayElement * psElement) |
|---|
| 120 | { |
|---|
| 121 | BDBG_ENTER(BSYNClib_DelayElement_Snapshot_isr); |
|---|
| 122 | |
|---|
| 123 | BDBG_ASSERT(psElement); |
|---|
| 124 | |
|---|
| 125 | psElement->sSnapshot = psElement->sData; |
|---|
| 126 | |
|---|
| 127 | BSYNClib_Delay_Snapshot_isr(&psElement->sDelay); |
|---|
| 128 | BSYNClib_DelayNotification_Snapshot_isr(&psElement->sNotification); |
|---|
| 129 | |
|---|
| 130 | BDBG_LEAVE(BSYNClib_DelayElement_Snapshot_isr); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | void BSYNClib_Delay_Init(BSYNClib_Delay * psDelay) |
|---|
| 134 | { |
|---|
| 135 | BDBG_ENTER(BSYNClib_Delay_Init); |
|---|
| 136 | |
|---|
| 137 | BDBG_ASSERT(psDelay); |
|---|
| 138 | |
|---|
| 139 | BKNI_Memset(psDelay, 0, sizeof(BSYNClib_Delay)); |
|---|
| 140 | |
|---|
| 141 | BDBG_LEAVE(BSYNClib_Delay_Init); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | void BSYNClib_Delay_Reset_isr(BSYNClib_Delay * psDelay) |
|---|
| 145 | { |
|---|
| 146 | BDBG_ENTER(BSYNClib_Delay_Reset_isr); |
|---|
| 147 | |
|---|
| 148 | BDBG_ASSERT(psDelay); |
|---|
| 149 | |
|---|
| 150 | psDelay->sData.bValid = false; |
|---|
| 151 | psDelay->sData.uiCustom = 0; |
|---|
| 152 | psDelay->sData.uiMeasured = 0; |
|---|
| 153 | |
|---|
| 154 | psDelay->sSnapshot.bValid = false; |
|---|
| 155 | psDelay->sSnapshot.uiCustom = 0; |
|---|
| 156 | psDelay->sSnapshot.uiMeasured = 0; |
|---|
| 157 | |
|---|
| 158 | psDelay->sResults.bAccepted = false; |
|---|
| 159 | psDelay->sResults.bEstimated = false; |
|---|
| 160 | psDelay->sResults.uiApplied = 0; |
|---|
| 161 | psDelay->sResults.uiDesired = 0; |
|---|
| 162 | |
|---|
| 163 | BDBG_LEAVE(BSYNClib_Delay_Reset_isr); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void BSYNClib_Delay_Snapshot_isr(BSYNClib_Delay * psDelay) |
|---|
| 167 | { |
|---|
| 168 | BDBG_ENTER(BSYNClib_Delay_Snapshot_isr); |
|---|
| 169 | |
|---|
| 170 | BDBG_ASSERT(psDelay); |
|---|
| 171 | |
|---|
| 172 | psDelay->sSnapshot = psDelay->sData; |
|---|
| 173 | BDBG_LEAVE(BSYNClib_Delay_Snapshot_isr); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | void BSYNClib_DelayNotification_Init(BSYNClib_DelayNotification * psNotification) |
|---|
| 177 | { |
|---|
| 178 | BDBG_ENTER(BSYNClib_DelayNotification_Init); |
|---|
| 179 | |
|---|
| 180 | BDBG_ASSERT(psNotification); |
|---|
| 181 | |
|---|
| 182 | BKNI_Memset(psNotification, 0, sizeof(BSYNClib_DelayNotification)); |
|---|
| 183 | psNotification->sData.bEnabled = true; |
|---|
| 184 | psNotification->sSnapshot.bEnabled = true; |
|---|
| 185 | psNotification->sResults.bEnabled = true; |
|---|
| 186 | |
|---|
| 187 | BDBG_LEAVE(BSYNClib_DelayNotification_Init); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void BSYNClib_DelayNotification_Reset_isr(BSYNClib_DelayNotification * psNotification) |
|---|
| 191 | { |
|---|
| 192 | BDBG_ENTER(BSYNClib_DelayNotification_Reset_isr); |
|---|
| 193 | |
|---|
| 194 | BDBG_ASSERT(psNotification); |
|---|
| 195 | |
|---|
| 196 | psNotification->sData.bEnabled = true; /* TODO: should be based on callback installed */ |
|---|
| 197 | psNotification->sData.bReceived = false; |
|---|
| 198 | |
|---|
| 199 | psNotification->sSnapshot.bEnabled = true; |
|---|
| 200 | psNotification->sSnapshot.bReceived = false; |
|---|
| 201 | |
|---|
| 202 | psNotification->sResults.bEnabled = true; |
|---|
| 203 | psNotification->sResults.bGenerateCallback = false; |
|---|
| 204 | |
|---|
| 205 | BDBG_LEAVE(BSYNClib_DelayNotification_Reset_isr); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | void BSYNClib_DelayNotification_Snapshot_isr(BSYNClib_DelayNotification * psNotification) |
|---|
| 209 | { |
|---|
| 210 | BDBG_ENTER(BSYNClib_DelayNotification_Snapshot_isr); |
|---|
| 211 | |
|---|
| 212 | BDBG_ASSERT(psNotification); |
|---|
| 213 | |
|---|
| 214 | psNotification->sSnapshot = psNotification->sData; |
|---|
| 215 | |
|---|
| 216 | BDBG_LEAVE(BSYNClib_DelayNotification_Snapshot_isr); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | void BSYNClib_DelayElement_CheckLifecycle_isr( |
|---|
| 220 | bool bStarted, |
|---|
| 221 | const BSYNClib_DelayElement * psCurrent, |
|---|
| 222 | BSYNClib_DelayElement_DiffResults * psResults |
|---|
| 223 | ) |
|---|
| 224 | { |
|---|
| 225 | BDBG_ENTER(BSYNClib_DelayElement_CheckLifecycle_isr); |
|---|
| 226 | |
|---|
| 227 | BDBG_ASSERT(psCurrent); |
|---|
| 228 | BDBG_ASSERT(psResults); |
|---|
| 229 | |
|---|
| 230 | if (bStarted && !psCurrent->sData.bStarted) |
|---|
| 231 | { |
|---|
| 232 | psResults->bChanged = true; |
|---|
| 233 | psResults->eLifecycleEvent = BSYNClib_DelayElement_LifecycleEvent_eStarted; |
|---|
| 234 | } |
|---|
| 235 | else if (!bStarted && psCurrent->sData.bStarted) |
|---|
| 236 | { |
|---|
| 237 | psResults->bChanged = true; |
|---|
| 238 | psResults->eLifecycleEvent = BSYNClib_DelayElement_LifecycleEvent_eStopped; |
|---|
| 239 | } |
|---|
| 240 | else |
|---|
| 241 | { |
|---|
| 242 | psResults->eLifecycleEvent = BSYNClib_DelayElement_LifecycleEvent_eNone; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | BDBG_LEAVE(BSYNClib_DelayElement_CheckLifecycle_isr); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | void BSYNClib_DelayElement_Diff_isr( |
|---|
| 250 | const BSYNClib_DelayElement * psDesired, |
|---|
| 251 | const BSYNClib_DelayElement * psCurrent, |
|---|
| 252 | BSYNClib_DelayElement_DiffResults * psResults |
|---|
| 253 | ) |
|---|
| 254 | { |
|---|
| 255 | BDBG_ENTER(BSYNClib_DelayElement_Diff_isr); |
|---|
| 256 | |
|---|
| 257 | BDBG_ASSERT(psDesired); |
|---|
| 258 | BDBG_ASSERT(psCurrent); |
|---|
| 259 | BDBG_ASSERT(psResults); |
|---|
| 260 | |
|---|
| 261 | /* check if synchronization for this element was just enabled */ |
|---|
| 262 | if (psDesired->sData.bSynchronize && !psCurrent->sData.bSynchronize) |
|---|
| 263 | { |
|---|
| 264 | psResults->bChanged = true; |
|---|
| 265 | psResults->bGenerateDelayCallback = true; |
|---|
| 266 | psResults->bGenerateNotificationCallback = true; |
|---|
| 267 | psResults->eSynchronizationEvent = BSYNClib_DelayElement_SynchronizationEvent_eSynchronized; |
|---|
| 268 | } |
|---|
| 269 | else if (!psDesired->sData.bSynchronize && psCurrent->sData.bSynchronize) |
|---|
| 270 | { |
|---|
| 271 | /* or disabled */ |
|---|
| 272 | psResults->bChanged = true; |
|---|
| 273 | psResults->bGenerateDelayCallback = false; |
|---|
| 274 | psResults->bGenerateNotificationCallback = false; |
|---|
| 275 | psResults->eSynchronizationEvent = BSYNClib_DelayElement_SynchronizationEvent_eIgnored; |
|---|
| 276 | } |
|---|
| 277 | else |
|---|
| 278 | { |
|---|
| 279 | psResults->eSynchronizationEvent = BSYNClib_DelayElement_SynchronizationEvent_eNone; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /* copy user custom delay */ |
|---|
| 283 | if (psCurrent->sDelay.sData.uiCustom != psDesired->sDelay.sData.uiCustom) |
|---|
| 284 | { |
|---|
| 285 | psResults->bChanged = true; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /* if delay notification is being enabled (or we are starting with |
|---|
| 289 | delay notification enabled), generate callback for threshold value */ |
|---|
| 290 | if ((!psCurrent->sNotification.sData.bEnabled && psDesired->sNotification.sData.bEnabled) |
|---|
| 291 | || (psDesired->sNotification.sData.bEnabled && psResults->eLifecycleEvent == BSYNClib_DelayElement_LifecycleEvent_eStarted)) |
|---|
| 292 | { |
|---|
| 293 | psResults->bGenerateNotificationCallback = true; |
|---|
| 294 | psResults->bChanged = true; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | /* if delay notification is enabled, check that we received one, otherwise, copy delay level and validate */ |
|---|
| 298 | if (psCurrent->sNotification.sData.bEnabled) |
|---|
| 299 | { |
|---|
| 300 | /* received a delay notification? */ |
|---|
| 301 | if (psDesired->sNotification.sData.bReceived) |
|---|
| 302 | { |
|---|
| 303 | psResults->bDelayReceived = true; |
|---|
| 304 | psResults->bChanged = true; |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | else |
|---|
| 308 | { |
|---|
| 309 | if (psCurrent->sDelay.sData.uiMeasured != psDesired->sDelay.sData.uiMeasured) |
|---|
| 310 | { |
|---|
| 311 | psResults->bChanged = true; |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | if (psCurrent->sDelay.sData.uiCapacity != psDesired->sDelay.sData.uiCapacity) |
|---|
| 316 | { |
|---|
| 317 | psResults->bChanged = true; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | BDBG_LEAVE(BSYNClib_DelayElement_Diff_isr); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | void BSYNClib_DelayElement_Patch_isr( |
|---|
| 324 | const BSYNClib_DelayElement * psDesired, |
|---|
| 325 | BSYNClib_DelayElement * psCurrent, |
|---|
| 326 | BSYNClib_DelayElement_DiffResults * psResults |
|---|
| 327 | ) |
|---|
| 328 | { |
|---|
| 329 | BDBG_ENTER(BSYNClib_DelayElement_Patch_isr); |
|---|
| 330 | |
|---|
| 331 | BDBG_ASSERT(psCurrent); |
|---|
| 332 | BDBG_ASSERT(psDesired); |
|---|
| 333 | BDBG_ASSERT(psResults); |
|---|
| 334 | |
|---|
| 335 | if (psResults->bChanged) |
|---|
| 336 | { |
|---|
| 337 | psCurrent->sData = psDesired->sData; |
|---|
| 338 | psCurrent->sDelay.sData = psDesired->sDelay.sData; |
|---|
| 339 | psCurrent->sNotification.sData = psDesired->sNotification.sData; |
|---|
| 340 | |
|---|
| 341 | if (psResults->bDelayReceived) |
|---|
| 342 | { |
|---|
| 343 | /* validate */ |
|---|
| 344 | psCurrent->sDelay.sData.bValid = true; |
|---|
| 345 | /* accept only after timer expires */ |
|---|
| 346 | psCurrent->sDelay.sResults.bAccepted = false; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | /* Accept and validate the delay if notification was disabled */ |
|---|
| 350 | if (!psCurrent->sNotification.sData.bEnabled) |
|---|
| 351 | { |
|---|
| 352 | psCurrent->sDelay.sData.bValid = true; |
|---|
| 353 | /* TODO: do elsewhere? */ |
|---|
| 354 | #if 0 |
|---|
| 355 | psCurrent->sDelay.sResults.bAccepted = true; |
|---|
| 356 | #endif |
|---|
| 357 | /* TODO: assume avg delay? */ |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | if (psResults->bGenerateDelayCallback) |
|---|
| 361 | { |
|---|
| 362 | psCurrent->sDelay.sResults.bGenerateCallback = true; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | if (psResults->bGenerateNotificationCallback) |
|---|
| 366 | { |
|---|
| 367 | psCurrent->sNotification.sResults.bGenerateCallback = true; |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | BDBG_LEAVE(BSYNClib_DelayElement_Patch_isr); |
|---|
| 372 | } |
|---|
| 373 | |
|---|