1 /* 2 * Copyright 2020 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef CALL_ADAPTATION_BROADCAST_RESOURCE_LISTENER_H_ 12 #define CALL_ADAPTATION_BROADCAST_RESOURCE_LISTENER_H_ 13 14 #include <vector> 15 16 #include "api/adaptation/resource.h" 17 #include "api/scoped_refptr.h" 18 #include "rtc_base/synchronization/mutex.h" 19 20 namespace webrtc { 21 22 // Responsible for forwarding 1 resource usage measurement to N listeners by 23 // creating N "adapter" resources. 24 // 25 // Example: 26 // If we have ResourceA, ResourceListenerX and ResourceListenerY we can create a 27 // BroadcastResourceListener that listens to ResourceA, use CreateAdapter() to 28 // spawn adapter resources ResourceX and ResourceY and let ResourceListenerX 29 // listen to ResourceX and ResourceListenerY listen to ResourceY. When ResourceA 30 // makes a measurement it will be echoed by both ResourceX and ResourceY. 31 // 32 // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor is 33 // moved to call there will only be one ResourceAdaptationProcessor that needs 34 // to listen to the injected resources. When this is the case, delete this class 35 // and DCHECK that a Resource's listener is never overwritten. 36 class BroadcastResourceListener : public ResourceListener { 37 public: 38 explicit BroadcastResourceListener( 39 rtc::scoped_refptr<Resource> source_resource); 40 ~BroadcastResourceListener() override; 41 42 rtc::scoped_refptr<Resource> SourceResource() const; 43 void StartListening(); 44 void StopListening(); 45 46 // Creates a Resource that redirects any resource usage measurements that 47 // BroadcastResourceListener receives to its listener. 48 rtc::scoped_refptr<Resource> CreateAdapterResource(); 49 50 // Unregister the adapter from the BroadcastResourceListener; it will no 51 // longer receive resource usage measurement and will no longer be referenced. 52 // Use this to prevent memory leaks of old adapters. 53 void RemoveAdapterResource(rtc::scoped_refptr<Resource> resource); 54 std::vector<rtc::scoped_refptr<Resource>> GetAdapterResources(); 55 56 // ResourceListener implementation. 57 void OnResourceUsageStateMeasured(rtc::scoped_refptr<Resource> resource, 58 ResourceUsageState usage_state) override; 59 60 private: 61 class AdapterResource; 62 friend class AdapterResource; 63 64 const rtc::scoped_refptr<Resource> source_resource_; 65 Mutex lock_; 66 bool is_listening_ RTC_GUARDED_BY(lock_); 67 // The AdapterResource unregisters itself prior to destruction, guaranteeing 68 // that these pointers are safe to use. 69 std::vector<rtc::scoped_refptr<AdapterResource>> adapters_ 70 RTC_GUARDED_BY(lock_); 71 }; 72 73 } // namespace webrtc 74 75 #endif // CALL_ADAPTATION_BROADCAST_RESOURCE_LISTENER_H_ 76