github:https://github.com/bigonelby/webrtcUml/tree/master/latest

這個圖展示了webrtc中的應(yīng)用帶寬估算模塊決策出的碼率
這里的起點是RtpTransportControllerSend,前文已經(jīng)提到,最終的帶寬決策的值,將通過RtpTransportControllerSend的CongestionControlHandler記錄,每次SetTargetRate的時候,都會更新類成員last_incoming_,如果有變化,則同時更新last_reported_
RtpTransportControllerSend模塊在更新后,執(zhí)行UpdateControlStat方法,這個方法,將從CongestionControlHandler中取出記錄,即last_reported_,同時觸發(fā)回調(diào)給observer_,這個observer就是TargetTransferRateObserver
這個Observer正是Call,原來Call在EnsureStarted的時候,就把自己注冊給RtpTransportControllerSend了,理所應(yīng)當,Call得到了這些數(shù)據(jù)。
接下來就是碼率分配了。這里決策出的碼率是帶寬碼率,是所有stream所共享的,因此自然要分配到人。誰來負責分配這個工作的?就是Call的成員BitrateAllocator,Call會調(diào)用其OnNetworkEstimateChanged方法,告知其網(wǎng)絡(luò)發(fā)生改變了。BitrateAllocator需要重新分配帶寬資源。
分配帶寬資源的主要就是AllocateBitrates方法。有幾種模式,如果帶寬不足,則執(zhí)行LowRateAllocation;正常的是NormalRateAllocation;如果帶寬非常充裕,則執(zhí)行MaxRateAllocation,這些分配函數(shù),會具體的將碼率分配給注冊的AllocatableTrack,為每一個track都構(gòu)建了一個結(jié)構(gòu)體BitrateAllocationUpdate,然后通過BitrateAllocatorObserver將數(shù)據(jù)反饋給上層
究竟誰是observer呢?實際上就是每個VideoSendStreamImpl,這些stream在其StartupVideoSendStream的時候,通過BitrateAllocator的AddObserver,將自己注冊至BitrateAllocator模塊中,每一個對應(yīng)一個AllocatableTrack
至此,VideoSendStreamImpl得到了分配好的碼率,他需要將這些碼率再進行計算交給編碼器。因為發(fā)送還有overhead的數(shù)據(jù),因此encoder顯然不能獨享這些碼率。通過RtpVideoSenderInterface的OnBitrateUpdated,告知發(fā)送模塊碼率發(fā)送了變化,RtpVideoSender會通過所有RtpStreamSender的ModuleRtpRtcpImpl2模塊拿到overhead的數(shù)據(jù),即通過方法ExpectedPerpacketOverhead,最終VideoSendStreamImpl通過RtpVideoSenderInterface得到編碼器應(yīng)分得的碼率,即GetPayloadBitrateBps
最后,就是給encoder設(shè)置了,通過VideoStreamEncoderInterface的OnBitrateUpdated方法進行設(shè)置,在其實現(xiàn)類VideoStreamEncoder中,再通過SetEncoderRates將碼率設(shè)置給具體的編碼器