# AudioBridge outline
```
onStart() {
    - initializeTillStable();
        do
        - paCache.restart
        - syncInputs  (= upsert audio sources)
        - syncOutputs (= upsert audio sinks)
        - requestSyncOutputConfig(false)  // SHOULD BECOME synchronizeMeeting(this.cache.getMeeting(), true)
        while (isDirty)
    - subscribe();
    - synchronizeMeeting(this.cache.getMeeting());   //  REMOVE
```

### CALLBACKS FOR PA EVENTS:
```
_onPlaybackStreamAdded  (<-- new playbackstream reported by PA)
 - if incoming stream is tunnel sink and there is no outgoing tunnel sink to same device
        synchronizeMeeting

_onPlaybackStreamRemoved (<-- playbackstream gone reported by PA)
 - remove playbackstream from paCache

_updateInputs (<-- new input reported by PA)
 - paCache.updateSources
 - syncInputs  (= upsert audio sources)

_updateOutputs (<-- new sink or module reported by PA)
 - paCache.updateSinks
 - paCache.updateModules
 // syncOutputs missing ??? (only needed if the change was a hardware sink (not eg. a new mix or a tunnelsink that was lost or so))
 - _requestSyncOutputConfig
```

### CALLBACKS FOR METEOR (=MONGO DB) EVENTS:
```
_onThisDeviceChanged
    . volume or mute changed -> applyVolumeAndMuteSettingsToHardwareSinks
    . calibration state changed -> requestSyncOutputConfig(true)
    . contentDefaultVolume changed -> applyContentSharingDefaultVolume
    . audioSources changed -> applyVolumeAndMuteSettingsToSource
    . audioSinks changed -> applyVolumeAndMuteSettingsToSink
    . audioSinkConfig or audioSourceConfig changed -> requestSyncOutputConfig
    . role ->  _requestSyncOutputConfig

_onDeviceChanged
-> _synchronizeTunnelSinks

_onDeviceAdded
 -> _synchronizeTunnelSinks

_onDeviceRemoved
 -> _synchronizeTunnelSinks
```

synchronizeMeeting
 - update tile list
 - call _requestSyncOutputConfig if list changed or if forceReconfig=true


 _requestSyncOutputConfig
    as long as requestNewSync is true, keep on calling _syncOutputConfig


_syncOutputConfig(syncAvScraper)
 - if (isSubscribed)
        unsubscribe
 - doSyncOutputConfig
    - group sources per role
    - group sinks per role
    - syncRouting
        - getRequiredRouting
        - getCurrentRouting
        - if (current !== requested) 
            setupRouting
    - warm cache
    - set default source and sink
    - apply volumes to hardware sinks (=outputs)
    - apply content sharing volume
    - put playback streams to right sink 
    - apply volumes to hardware sources (=inputs)
 - if (syncAvScraper)
        syncAVScraperForAudioCalibration
 - if (wasSubscribed)
        subscribe