From c74412b30484dc340fa9e2052dbc9700393ebf9f Mon Sep 17 00:00:00 2001 From: Harald Alvestrand Date: Thu, 6 Jun 2024 11:01:02 +0000 Subject: [PATCH] Deprecate rtc::RefCountInterface and move usages to webrtc::RefCountInterface This CL also moves more stuff to webrtc:: and adds backwards compatible aliases for them. Bug: webrtc:42225969 Change-Id: Iefb8542cff793bd8aa46bef8f2f3c66a1e979d07 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/353720 Reviewed-by: Florent Castelli Commit-Queue: Harald Alvestrand Cr-Commit-Position: refs/heads/main@{#42446} --- api/adaptation/BUILD.gn | 1 + api/adaptation/DEPS | 7 --- api/adaptation/resource.h | 4 +- api/audio/audio_mixer.h | 2 +- api/audio_codecs/audio_encoder_factory.h | 2 +- api/crypto/frame_decryptor_interface.h | 2 +- api/crypto/frame_encryptor_interface.h | 2 +- api/make_ref_counted.h | 52 +++++++++++++++++- api/ref_counted_base.h | 10 +++- api/stats/rtc_stats_collector_callback.h | 2 +- api/video/encoded_image.h | 2 +- audio/voip/audio_channel.h | 2 +- .../resource_adaptation_processor.h | 2 +- call/audio_state.h | 2 +- common_video/video_frame_buffer.cc | 4 +- common_video/video_frame_buffer_pool.cc | 48 ++++++++-------- media/engine/webrtc_voice_engine_unittest.cc | 2 +- .../async_audio_processing.h | 2 +- .../video_capture/windows/help_functions_ds.h | 2 +- pc/connection_context.h | 5 +- pc/peer_connection_media_unittest.cc | 4 +- pc/proxy.h | 2 +- pc/proxy_unittest.cc | 2 +- pc/rtc_stats_collector.h | 2 +- pc/rtc_stats_collector_unittest.cc | 2 +- rtc_base/ref_count.h | 7 ++- rtc_base/ref_counted_object.h | 55 ++++++++++++++++++- rtc_base/ref_counted_object_unittest.cc | 4 +- rtc_base/ref_counter.h | 6 +- rtc_base/weak_ptr.h | 2 +- .../src/java/org/webrtc/JniCommon.java | 2 +- sdk/android/src/jni/jni_common.cc | 6 +- test/mappable_native_buffer.h | 4 +- video/config/video_encoder_config.h | 4 +- video/video_stream_encoder_unittest.cc | 6 +- 35 files changed, 184 insertions(+), 79 deletions(-) delete mode 100644 api/adaptation/DEPS diff --git a/api/adaptation/BUILD.gn b/api/adaptation/BUILD.gn index 839ad2c24b..fe66065a36 100644 --- a/api/adaptation/BUILD.gn +++ b/api/adaptation/BUILD.gn @@ -15,6 +15,7 @@ rtc_source_set("resource_adaptation_api") { "resource.h", ] deps = [ + "..:ref_count", "../../api:scoped_refptr", "../../rtc_base:checks", "../../rtc_base:refcount", diff --git a/api/adaptation/DEPS b/api/adaptation/DEPS deleted file mode 100644 index 734e152497..0000000000 --- a/api/adaptation/DEPS +++ /dev/null @@ -1,7 +0,0 @@ -specific_include_rules = { - "resource\.h": [ - # ref_count.h is a public_deps of rtc_base:refcount. Necessary because of - # rtc::RefCountInterface. - "+rtc_base/ref_count.h", - ], -} \ No newline at end of file diff --git a/api/adaptation/resource.h b/api/adaptation/resource.h index 7d7c70b3eb..438424767e 100644 --- a/api/adaptation/resource.h +++ b/api/adaptation/resource.h @@ -13,8 +13,8 @@ #include +#include "api/ref_count.h" #include "api/scoped_refptr.h" -#include "rtc_base/ref_count.h" #include "rtc_base/system/rtc_export.h" namespace webrtc { @@ -50,7 +50,7 @@ class RTC_EXPORT ResourceListener { // The Resource is reference counted to prevent use-after-free when posting // between task queues. As such, the implementation MUST NOT make any // assumptions about which task queue Resource is destructed on. -class RTC_EXPORT Resource : public rtc::RefCountInterface { +class RTC_EXPORT Resource : public RefCountInterface { public: Resource(); // Destruction may happen on any task queue. diff --git a/api/audio/audio_mixer.h b/api/audio/audio_mixer.h index 3483df22bc..02995e4636 100644 --- a/api/audio/audio_mixer.h +++ b/api/audio/audio_mixer.h @@ -21,7 +21,7 @@ namespace webrtc { // WORK IN PROGRESS // This class is under development and is not yet intended for for use outside // of WebRtc/Libjingle. -class AudioMixer : public rtc::RefCountInterface { +class AudioMixer : public RefCountInterface { public: // A callback class that all mixer participants must inherit from/implement. class Source { diff --git a/api/audio_codecs/audio_encoder_factory.h b/api/audio_codecs/audio_encoder_factory.h index 6128b1b6f3..b7c4482d11 100644 --- a/api/audio_codecs/audio_encoder_factory.h +++ b/api/audio_codecs/audio_encoder_factory.h @@ -23,7 +23,7 @@ namespace webrtc { // A factory that creates AudioEncoders. -class AudioEncoderFactory : public rtc::RefCountInterface { +class AudioEncoderFactory : public RefCountInterface { public: // Returns a prioritized list of audio codecs, to use for signaling etc. virtual std::vector GetSupportedEncoders() = 0; diff --git a/api/crypto/frame_decryptor_interface.h b/api/crypto/frame_decryptor_interface.h index 2f6bdac4b4..f4123008e3 100644 --- a/api/crypto/frame_decryptor_interface.h +++ b/api/crypto/frame_decryptor_interface.h @@ -27,7 +27,7 @@ namespace webrtc { // without it. You may assume that this interface will have the same lifetime // as the RTPReceiver it is attached to. It must only be attached to one // RTPReceiver. Additional data may be null. -class FrameDecryptorInterface : public rtc::RefCountInterface { +class FrameDecryptorInterface : public RefCountInterface { public: // The Status enum represents all possible states that can be // returned when attempting to decrypt a frame. kRecoverable indicates that diff --git a/api/crypto/frame_encryptor_interface.h b/api/crypto/frame_encryptor_interface.h index 1452b80189..99148bd84f 100644 --- a/api/crypto/frame_encryptor_interface.h +++ b/api/crypto/frame_encryptor_interface.h @@ -24,7 +24,7 @@ namespace webrtc { // addition to the standard SRTP mechanism and is not intended to be used // without it. Implementations of this interface will have the same lifetime as // the RTPSenders it is attached to. Additional data may be null. -class FrameEncryptorInterface : public rtc::RefCountInterface { +class FrameEncryptorInterface : public RefCountInterface { public: ~FrameEncryptorInterface() override {} diff --git a/api/make_ref_counted.h b/api/make_ref_counted.h index e958da9774..40eb3ad87a 100644 --- a/api/make_ref_counted.h +++ b/api/make_ref_counted.h @@ -15,7 +15,7 @@ #include "rtc_base/ref_counted_object.h" -namespace rtc { +namespace webrtc { namespace webrtc_make_ref_counted_internal { // Determines if the given class has AddRef and Release methods. @@ -115,6 +115,56 @@ scoped_refptr> make_ref_counted(Args&&... args) { new FinalRefCountedObject(std::forward(args)...)); } +} // namespace webrtc + +// Backwards compatibe aliases. +// TODO: https://issues.webrtc.org/42225969 - deprecate and remove. +namespace rtc { +// This doesn't work: +// template +// using make_ref_counted(Args&&... args) = +// webrtc::make_ref_counted(Args&&... args); +// Instead, reproduce the templates. +template && + std::is_abstract_v, + T>::type* = nullptr> +scoped_refptr make_ref_counted(Args&&... args) { + return webrtc::scoped_refptr( + new webrtc::RefCountedObject(std::forward(args)...)); +} + +// `make_ref_counted` for complete classes that are not convertible to +// RefCountInterface and already carry a ref count. +template && + webrtc::webrtc_make_ref_counted_internal::HasAddRefAndRelease< + T>::value, + T>::type* = nullptr> +scoped_refptr make_ref_counted(Args&&... args) { + return webrtc::scoped_refptr(new T(std::forward(args)...)); +} + +// `make_ref_counted` for complete classes that are not convertible to +// RefCountInterface and have no ref count of their own. +template && + !webrtc::webrtc_make_ref_counted_internal:: + HasAddRefAndRelease::value, + + T>::type* = nullptr> +scoped_refptr> make_ref_counted( + Args&&... args) { + return webrtc::scoped_refptr>( + new webrtc::FinalRefCountedObject(std::forward(args)...)); +} + } // namespace rtc #endif // API_MAKE_REF_COUNTED_H_ diff --git a/api/ref_counted_base.h b/api/ref_counted_base.h index f20228b740..2638ac17cb 100644 --- a/api/ref_counted_base.h +++ b/api/ref_counted_base.h @@ -14,7 +14,7 @@ #include "rtc_base/ref_counter.h" -namespace rtc { +namespace webrtc { class RefCountedBase { public: @@ -93,6 +93,14 @@ class RefCountedNonVirtual { mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; }; +} // namespace webrtc + +// Backwards compatibe aliases. +// TODO: https://issues.webrtc.org/42225969 - deprecate and remove. +namespace rtc { +using RefCountedBase = webrtc::RefCountedBase; +template +using RefCountedNonVirtual = webrtc::RefCountedNonVirtual; } // namespace rtc #endif // API_REF_COUNTED_BASE_H_ diff --git a/api/stats/rtc_stats_collector_callback.h b/api/stats/rtc_stats_collector_callback.h index 506cc63e6f..acdbf26c17 100644 --- a/api/stats/rtc_stats_collector_callback.h +++ b/api/stats/rtc_stats_collector_callback.h @@ -17,7 +17,7 @@ namespace webrtc { -class RTCStatsCollectorCallback : public rtc::RefCountInterface { +class RTCStatsCollectorCallback : public RefCountInterface { public: ~RTCStatsCollectorCallback() override = default; diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h index 8f0226c7a7..3958c1672f 100644 --- a/api/video/encoded_image.h +++ b/api/video/encoded_image.h @@ -35,7 +35,7 @@ namespace webrtc { // Abstract interface for buffer storage. Intended to support buffers owned by // external encoders with special release requirements, e.g, java encoders with // releaseOutputBuffer. -class EncodedImageBufferInterface : public rtc::RefCountInterface { +class EncodedImageBufferInterface : public RefCountInterface { public: virtual const uint8_t* data() const = 0; // TODO(bugs.webrtc.org/9378): Make interface essentially read-only, delete diff --git a/audio/voip/audio_channel.h b/audio/voip/audio_channel.h index 7338d9faab..90ce8d4d57 100644 --- a/audio/voip/audio_channel.h +++ b/audio/voip/audio_channel.h @@ -29,7 +29,7 @@ namespace webrtc { // AudioChannel represents a single media session and provides APIs over // AudioIngress and AudioEgress. Note that a single RTP stack is shared with // these two classes as it has both sending and receiving capabilities. -class AudioChannel : public rtc::RefCountInterface { +class AudioChannel : public RefCountInterface { public: AudioChannel(Transport* transport, uint32_t local_ssrc, diff --git a/call/adaptation/resource_adaptation_processor.h b/call/adaptation/resource_adaptation_processor.h index db3b4c2506..c2d9c3e57f 100644 --- a/call/adaptation/resource_adaptation_processor.h +++ b/call/adaptation/resource_adaptation_processor.h @@ -84,7 +84,7 @@ class ResourceAdaptationProcessor : public ResourceAdaptationProcessorInterface, // If resource usage measurements happens off the adaptation task queue, this // class takes care of posting the measurement for the processor to handle it // on the adaptation task queue. - class ResourceListenerDelegate : public rtc::RefCountInterface, + class ResourceListenerDelegate : public RefCountInterface, public ResourceListener { public: explicit ResourceListenerDelegate(ResourceAdaptationProcessor* processor); diff --git a/call/audio_state.h b/call/audio_state.h index bd4386839d..3a8e7c8b48 100644 --- a/call/audio_state.h +++ b/call/audio_state.h @@ -23,7 +23,7 @@ class AudioTransport; // AudioState holds the state which must be shared between multiple instances of // webrtc::Call for audio processing purposes. -class AudioState : public rtc::RefCountInterface { +class AudioState : public RefCountInterface { public: struct Config { Config(); diff --git a/common_video/video_frame_buffer.cc b/common_video/video_frame_buffer.cc index ca2916e580..02525c8f70 100644 --- a/common_video/video_frame_buffer.cc +++ b/common_video/video_frame_buffer.cc @@ -60,7 +60,7 @@ class WrappedYuvBuffer : public Base { int StrideV() const override { return v_stride_; } private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; const int width_; const int height_; @@ -182,7 +182,7 @@ class WrappedYuv16BBuffer : public Base { int StrideV() const override { return v_stride_; } private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; const int width_; const int height_; diff --git a/common_video/video_frame_buffer_pool.cc b/common_video/video_frame_buffer_pool.cc index c0215110fd..749e6811e1 100644 --- a/common_video/video_frame_buffer_pool.cc +++ b/common_video/video_frame_buffer_pool.cc @@ -19,37 +19,37 @@ namespace webrtc { namespace { bool HasOneRef(const rtc::scoped_refptr& buffer) { - // Cast to rtc::RefCountedObject is safe because this function is only called + // Cast to RefCountedObject is safe because this function is only called // on locally created VideoFrameBuffers, which are either - // `rtc::RefCountedObject`, `rtc::RefCountedObject` or - // `rtc::RefCountedObject`. + // `RefCountedObject`, `RefCountedObject` or + // `RefCountedObject`. switch (buffer->type()) { case VideoFrameBuffer::Type::kI420: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } case VideoFrameBuffer::Type::kI444: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } case VideoFrameBuffer::Type::kI422: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } case VideoFrameBuffer::Type::kI010: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } case VideoFrameBuffer::Type::kI210: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } case VideoFrameBuffer::Type::kI410: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } case VideoFrameBuffer::Type::kNV12: { - return static_cast*>(buffer.get()) + return static_cast*>(buffer.get()) ->HasOneRef(); } default: @@ -118,8 +118,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateI420Buffer( // Cast is safe because the only way kI420 buffer is created is // in the same function below, where `RefCountedObject` is // created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); @@ -149,8 +149,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateI444Buffer( // Cast is safe because the only way kI444 buffer is created is // in the same function below, where |RefCountedObject| // is created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); @@ -180,8 +180,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateI422Buffer( // Cast is safe because the only way kI422 buffer is created is // in the same function below, where |RefCountedObject| // is created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); @@ -211,8 +211,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateNV12Buffer( // Cast is safe because the only way kI420 buffer is created is // in the same function below, where `RefCountedObject` is // created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); @@ -242,8 +242,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateI010Buffer( // Cast is safe because the only way kI010 buffer is created is // in the same function below, where |RefCountedObject| // is created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); @@ -269,8 +269,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateI210Buffer( // Cast is safe because the only way kI210 buffer is created is // in the same function below, where |RefCountedObject| // is created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); @@ -296,8 +296,8 @@ rtc::scoped_refptr VideoFrameBufferPool::CreateI410Buffer( // Cast is safe because the only way kI410 buffer is created is // in the same function below, where |RefCountedObject| // is created. - rtc::RefCountedObject* raw_buffer = - static_cast*>(existing_buffer.get()); + RefCountedObject* raw_buffer = + static_cast*>(existing_buffer.get()); // Creates a new scoped_refptr, which is also pointing to the same // RefCountedObject as buffer, increasing ref count. return rtc::scoped_refptr(raw_buffer); diff --git a/media/engine/webrtc_voice_engine_unittest.cc b/media/engine/webrtc_voice_engine_unittest.cc index ad9140b66a..c4d5006632 100644 --- a/media/engine/webrtc_voice_engine_unittest.cc +++ b/media/engine/webrtc_voice_engine_unittest.cc @@ -3739,7 +3739,7 @@ TEST(WebRtcVoiceEngineTest, StartupShutdownWithExternalADM) { } // The engine/channel should have dropped their references. EXPECT_EQ(adm.release()->Release(), - rtc::RefCountReleaseStatus::kDroppedLastRef); + webrtc::RefCountReleaseStatus::kDroppedLastRef); } } diff --git a/modules/async_audio_processing/async_audio_processing.h b/modules/async_audio_processing/async_audio_processing.h index 2e78e716a8..c5a28cf54a 100644 --- a/modules/async_audio_processing/async_audio_processing.h +++ b/modules/async_audio_processing/async_audio_processing.h @@ -30,7 +30,7 @@ class AsyncAudioProcessing final { public: // Helper class passing AudioFrameProcessor and TaskQueueFactory into // AsyncAudioProcessing constructor. - class Factory : public rtc::RefCountInterface { + class Factory : public RefCountInterface { public: Factory(const Factory&) = delete; Factory& operator=(const Factory&) = delete; diff --git a/modules/video_capture/windows/help_functions_ds.h b/modules/video_capture/windows/help_functions_ds.h index 29479157a8..a16f953d78 100644 --- a/modules/video_capture/windows/help_functions_ds.h +++ b/modules/video_capture/windows/help_functions_ds.h @@ -99,7 +99,7 @@ class ComRefCount : public T { STDMETHOD_(ULONG, Release)() override { const auto status = ref_count_.DecRef(); - if (status == rtc::RefCountReleaseStatus::kDroppedLastRef) { + if (status == RefCountReleaseStatus::kDroppedLastRef) { delete this; return 0; } diff --git a/pc/connection_context.h b/pc/connection_context.h index 28b2d1cdd5..51055d8347 100644 --- a/pc/connection_context.h +++ b/pc/connection_context.h @@ -44,8 +44,7 @@ namespace webrtc { // interferes with the operation of other PeerConnections. // // This class must be created and destroyed on the signaling thread. -class ConnectionContext final - : public rtc::RefCountedNonVirtual { +class ConnectionContext final : public RefCountedNonVirtual { public: // Creates a ConnectionContext. May return null if initialization fails. // The Dependencies class allows simple management of all new dependencies @@ -106,7 +105,7 @@ class ConnectionContext final ConnectionContext(const Environment& env, PeerConnectionFactoryDependencies* dependencies); - friend class rtc::RefCountedNonVirtual; + friend class RefCountedNonVirtual; ~ConnectionContext(); private: diff --git a/pc/peer_connection_media_unittest.cc b/pc/peer_connection_media_unittest.cc index 97faae5da5..70096242a3 100644 --- a/pc/peer_connection_media_unittest.cc +++ b/pc/peer_connection_media_unittest.cc @@ -84,7 +84,7 @@ using ::testing::Values; cricket::MediaSendChannelInterface* SendChannelInternal( rtc::scoped_refptr transceiver) { auto transceiver_with_internal = static_cast< - rtc::RefCountedObject>*>( + RefCountedObject>*>( transceiver.get()); auto transceiver_internal = static_cast(transceiver_with_internal->internal()); @@ -94,7 +94,7 @@ cricket::MediaSendChannelInterface* SendChannelInternal( cricket::MediaReceiveChannelInterface* ReceiveChannelInternal( rtc::scoped_refptr transceiver) { auto transceiver_with_internal = static_cast< - rtc::RefCountedObject>*>( + RefCountedObject>*>( transceiver.get()); auto transceiver_internal = static_cast(transceiver_with_internal->internal()); diff --git a/pc/proxy.h b/pc/proxy.h index eecd601048..bfb2c7f44f 100644 --- a/pc/proxy.h +++ b/pc/proxy.h @@ -21,7 +21,7 @@ // // Example usage: // -// class TestInterface : public rtc::RefCountInterface { +// class TestInterface : public RefCountInterface { // public: // std::string FooA() = 0; // std::string FooB(bool arg1) const = 0; diff --git a/pc/proxy_unittest.cc b/pc/proxy_unittest.cc index ebfde9fecf..c131095206 100644 --- a/pc/proxy_unittest.cc +++ b/pc/proxy_unittest.cc @@ -27,7 +27,7 @@ using ::testing::Return; namespace webrtc { // Interface used for testing here. -class FakeInterface : public rtc::RefCountInterface { +class FakeInterface : public RefCountInterface { public: virtual void VoidMethod0() = 0; virtual std::string Method0() = 0; diff --git a/pc/rtc_stats_collector.h b/pc/rtc_stats_collector.h index 17eaf7fa7d..1dc84b0509 100644 --- a/pc/rtc_stats_collector.h +++ b/pc/rtc_stats_collector.h @@ -56,7 +56,7 @@ class RtpReceiverInternal; // Stats are gathered on the signaling, worker and network threads // asynchronously. The callback is invoked on the signaling thread. Resulting // reports are cached for `cache_lifetime_` ms. -class RTCStatsCollector : public rtc::RefCountInterface { +class RTCStatsCollector : public RefCountInterface { public: static rtc::scoped_refptr Create( PeerConnectionInternal* pc, diff --git a/pc/rtc_stats_collector_unittest.cc b/pc/rtc_stats_collector_unittest.cc index 0aa9a62b26..f724731a5a 100644 --- a/pc/rtc_stats_collector_unittest.cc +++ b/pc/rtc_stats_collector_unittest.cc @@ -3759,7 +3759,7 @@ class FakeRTCStatsCollector : public RTCStatsCollector, // Satisfying the implementation of these methods and associating them with a // reference counter, will be done by RefCountedObject. virtual void AddRef() const = 0; - virtual rtc::RefCountReleaseStatus Release() const = 0; + virtual RefCountReleaseStatus Release() const = 0; // RTCStatsCollectorCallback implementation. void OnStatsDelivered( diff --git a/rtc_base/ref_count.h b/rtc_base/ref_count.h index 60a11fa5c7..45940e2801 100644 --- a/rtc_base/ref_count.h +++ b/rtc_base/ref_count.h @@ -18,8 +18,11 @@ namespace rtc { // TODO(bugs.webrtc.org/15622): Deprecate and remove these aliases. -using webrtc::RefCountInterface; -using webrtc::RefCountReleaseStatus; +using RefCountInterface [[deprecated("Use webrtc::RefCountInterface")]] = + webrtc::RefCountInterface; +using RefCountReleaseStatus + [[deprecated("Use webrtc::RefCountReleaseStatus")]] = + webrtc::RefCountReleaseStatus; } // namespace rtc diff --git a/rtc_base/ref_counted_object.h b/rtc_base/ref_counted_object.h index 418c3d80cc..007e9f942b 100644 --- a/rtc_base/ref_counted_object.h +++ b/rtc_base/ref_counted_object.h @@ -14,7 +14,7 @@ #include "rtc_base/ref_count.h" #include "rtc_base/ref_counter.h" -namespace rtc { +namespace webrtc { template class RefCountedObject : public T { @@ -84,6 +84,59 @@ class FinalRefCountedObject final : public T { mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; }; +} // namespace webrtc + +// Backwards compatibe aliases. +// TODO: https://issues.webrtc.org/42225969 - deprecate and remove. +namespace rtc { +// Because there are users of this template that use "friend +// rtc::RefCountedObject<>" to give access to a private destructor, some +// indirection is needed; "friend" declarations cannot span an "using" +// declaration. Since a templated class on top of a templated class can't access +// the subclass' protected members, we duplicate the entire class instead. +template +class RefCountedObject : public T { + public: + RefCountedObject() {} + + RefCountedObject(const RefCountedObject&) = delete; + RefCountedObject& operator=(const RefCountedObject&) = delete; + + template + explicit RefCountedObject(P0&& p0) : T(std::forward(p0)) {} + + template + RefCountedObject(P0&& p0, P1&& p1, Args&&... args) + : T(std::forward(p0), + std::forward(p1), + std::forward(args)...) {} + + void AddRef() const override { ref_count_.IncRef(); } + + webrtc::RefCountReleaseStatus Release() const override { + const auto status = ref_count_.DecRef(); + if (status == webrtc::RefCountReleaseStatus::kDroppedLastRef) { + delete this; + } + return status; + } + + // Return whether the reference count is one. If the reference count is used + // in the conventional way, a reference count of 1 implies that the current + // thread owns the reference and no other thread shares it. This call + // performs the test for a reference count of one, and performs the memory + // barrier needed for the owning thread to act on the object, knowing that it + // has exclusive access to the object. + virtual bool HasOneRef() const { return ref_count_.HasOneRef(); } + + protected: + ~RefCountedObject() override {} + + mutable webrtc::webrtc_impl::RefCounter ref_count_{0}; +}; + +template +using FinalRefCountedObject = webrtc::FinalRefCountedObject; } // namespace rtc #endif // RTC_BASE_REF_COUNTED_OBJECT_H_ diff --git a/rtc_base/ref_counted_object_unittest.cc b/rtc_base/ref_counted_object_unittest.cc index abeb1e9e67..fff301cac2 100644 --- a/rtc_base/ref_counted_object_unittest.cc +++ b/rtc_base/ref_counted_object_unittest.cc @@ -21,7 +21,7 @@ #include "rtc_base/ref_count.h" #include "test/gtest.h" -namespace rtc { +namespace webrtc { namespace { @@ -172,4 +172,4 @@ TEST(RefCounted, SmartPointers) { } } -} // namespace rtc +} // namespace webrtc diff --git a/rtc_base/ref_counter.h b/rtc_base/ref_counter.h index 6ffeda8ced..93ae3d21f2 100644 --- a/rtc_base/ref_counter.h +++ b/rtc_base/ref_counter.h @@ -34,7 +34,7 @@ class RefCounter { // Otherwise, returns kOtherRefsRemained (note that in case of multithreading, // some other caller may have dropped the last reference by the time this call // returns; all we know is that we didn't do it). - rtc::RefCountReleaseStatus DecRef() { + RefCountReleaseStatus DecRef() { // Use release-acquire barrier to ensure all actions on the protected // resource are finished before the resource can be freed. // When ref_count_after_subtract > 0, this function require @@ -47,8 +47,8 @@ class RefCounter { int ref_count_after_subtract = ref_count_.fetch_sub(1, std::memory_order_acq_rel) - 1; return ref_count_after_subtract == 0 - ? rtc::RefCountReleaseStatus::kDroppedLastRef - : rtc::RefCountReleaseStatus::kOtherRefsRemained; + ? RefCountReleaseStatus::kDroppedLastRef + : RefCountReleaseStatus::kOtherRefsRemained; } // Return whether the reference count is one. If the reference count is used diff --git a/rtc_base/weak_ptr.h b/rtc_base/weak_ptr.h index 67b6ec1542..bc317e83a8 100644 --- a/rtc_base/weak_ptr.h +++ b/rtc_base/weak_ptr.h @@ -102,7 +102,7 @@ class WeakReference { bool IsValid() const; private: - friend class FinalRefCountedObject; + friend class webrtc::FinalRefCountedObject; ~Flag() = default; diff --git a/sdk/android/src/java/org/webrtc/JniCommon.java b/sdk/android/src/java/org/webrtc/JniCommon.java index e1b2e513d7..f982368041 100644 --- a/sdk/android/src/java/org/webrtc/JniCommon.java +++ b/sdk/android/src/java/org/webrtc/JniCommon.java @@ -14,7 +14,7 @@ import java.nio.ByteBuffer; /** Class with static JNI helper functions that are used in many places. */ public class JniCommon { - /** Functions to increment/decrement an rtc::RefCountInterface pointer. */ + /** Functions to increment/decrement an RefCountInterface pointer. */ public static native void nativeAddRef(long refCountedPointer); public static native void nativeReleaseRef(long refCountedPointer); diff --git a/sdk/android/src/jni/jni_common.cc b/sdk/android/src/jni/jni_common.cc index b4ad2d5a6d..b99fc60864 100644 --- a/sdk/android/src/jni/jni_common.cc +++ b/sdk/android/src/jni/jni_common.cc @@ -17,14 +17,12 @@ namespace jni { static void JNI_JniCommon_AddRef(JNIEnv* jni, jlong j_native_ref_counted_pointer) { - reinterpret_cast(j_native_ref_counted_pointer) - ->AddRef(); + reinterpret_cast(j_native_ref_counted_pointer)->AddRef(); } static void JNI_JniCommon_ReleaseRef(JNIEnv* jni, jlong j_native_ref_counted_pointer) { - reinterpret_cast(j_native_ref_counted_pointer) - ->Release(); + reinterpret_cast(j_native_ref_counted_pointer)->Release(); } static ScopedJavaLocalRef JNI_JniCommon_AllocateByteBuffer(JNIEnv* jni, diff --git a/test/mappable_native_buffer.h b/test/mappable_native_buffer.h index 08f155e07f..2345f11e80 100644 --- a/test/mappable_native_buffer.h +++ b/test/mappable_native_buffer.h @@ -71,7 +71,7 @@ class MappableNativeBuffer : public VideoFrameBuffer { bool DidConvertToI420() const; private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; class ScaledBuffer : public VideoFrameBuffer { public: @@ -97,7 +97,7 @@ class MappableNativeBuffer : public VideoFrameBuffer { rtc::ArrayView types) override; private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; const rtc::scoped_refptr parent_; const int width_; diff --git a/video/config/video_encoder_config.h b/video/config/video_encoder_config.h index a29af1fe06..d3d0621a1d 100644 --- a/video/config/video_encoder_config.h +++ b/video/config/video_encoder_config.h @@ -91,7 +91,7 @@ class VideoEncoderConfig { // kept alive until all encoder_specific_settings go out of scope. // TODO(kthelgason): Consider removing the need for copying VideoEncoderConfig // and use absl::optional for encoder_specific_settings instead. - class EncoderSpecificSettings : public rtc::RefCountInterface { + class EncoderSpecificSettings : public RefCountInterface { public: // TODO(pbos): Remove FillEncoderSpecificSettings as soon as VideoCodec is // not in use and encoder implementations ask for codec-specific structs @@ -139,7 +139,7 @@ class VideoEncoderConfig { kScreen, }; - class VideoStreamFactoryInterface : public rtc::RefCountInterface { + class VideoStreamFactoryInterface : public RefCountInterface { public: // An implementation should return a std::vector with the // wanted VideoStream settings for the given video resolution. diff --git a/video/video_stream_encoder_unittest.cc b/video/video_stream_encoder_unittest.cc index c5ba0fc283..df620cf5c0 100644 --- a/video/video_stream_encoder_unittest.cc +++ b/video/video_stream_encoder_unittest.cc @@ -152,7 +152,7 @@ class TestBuffer : public webrtc::I420Buffer { : I420Buffer(width, height), event_(event) {} private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; ~TestBuffer() override { if (event_) event_->Set(); @@ -184,7 +184,7 @@ class FakeNativeBuffer : public webrtc::VideoFrameBuffer { } private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; ~FakeNativeBuffer() override { if (event_) event_->Set(); @@ -216,7 +216,7 @@ class FakeNV12NativeBuffer : public webrtc::VideoFrameBuffer { const NV12BufferInterface* GetNV12() const { return nv12_buffer_.get(); } private: - friend class rtc::RefCountedObject; + friend class RefCountedObject; ~FakeNV12NativeBuffer() override { if (event_) event_->Set();