From 1d1944187fe1304b5933bc8f8a8a36a54da3e705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= Date: Mon, 21 Mar 2016 16:44:31 +0100 Subject: [PATCH] Replace RefCountImpl with rtc::RefCountedObject. Removes code duplication and use of the dangerous public destructor in RefCountImpl. Also making wider use of scoped_refptr and fixing various leaks in the process. BUG=webrtc:5229 R=tommi@webrtc.org Review URL: https://codereview.webrtc.org/1477013005 . Cr-Commit-Position: refs/heads/master@{#12075} --- .../modules/audio_device/audio_device_impl.cc | 29 +++---- .../modules/audio_device/audio_device_impl.h | 3 +- .../audio_device/include/audio_device.h | 3 - .../test/audio_device_test_api.cc | 34 +++----- .../desktop_capture/differ_block_unittest.cc | 1 - .../source/forward_error_correction.h | 2 +- .../external/video_capture_external.cc | 8 +- .../video_capture/ios/video_capture_ios.h | 6 +- .../video_capture/ios/video_capture_ios.mm | 23 +++--- .../video_capture/linux/device_info_linux.cc | 10 +-- .../linux/video_capture_linux.cc | 26 +++--- .../video_capture/mac/video_capture_mac.mm | 49 ++++------- .../video_capture/video_capture_factory.cc | 9 +- .../video_capture/video_capture_factory.h | 10 ++- .../video_capture/video_capture_impl.cc | 22 ++--- .../video_capture/video_capture_impl.h | 11 ++- .../video_capture/windows/device_info_ds.cc | 1 - .../windows/video_capture_factory_windows.cc | 18 ++-- webrtc/system_wrappers/BUILD.gn | 1 - webrtc/system_wrappers/include/ref_count.h | 82 ------------------- webrtc/system_wrappers/system_wrappers.gyp | 1 - webrtc/test/vcm_capturer.cc | 10 +-- webrtc/test/vcm_capturer.h | 3 +- webrtc/video/video_capture_input_unittest.cc | 2 +- webrtc/video/video_send_stream_tests.cc | 1 - webrtc/voice_engine/shared_data.cc | 11 +-- webrtc/voice_engine/shared_data.h | 8 +- 27 files changed, 124 insertions(+), 260 deletions(-) delete mode 100644 webrtc/system_wrappers/include/ref_count.h diff --git a/webrtc/modules/audio_device/audio_device_impl.cc b/webrtc/modules/audio_device/audio_device_impl.cc index 6496f5efc4..d881bcd40c 100644 --- a/webrtc/modules/audio_device/audio_device_impl.cc +++ b/webrtc/modules/audio_device/audio_device_impl.cc @@ -8,10 +8,10 @@ * be found in the AUTHORS file in the root of the source tree. */ +#include "webrtc/base/refcount.h" #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" #include "webrtc/modules/audio_device/audio_device_config.h" #include "webrtc/modules/audio_device/audio_device_impl.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/tick_util.h" #include @@ -65,13 +65,7 @@ }; \ } -namespace webrtc -{ - -AudioDeviceModule* CreateAudioDeviceModule( - int32_t id, AudioDeviceModule::AudioLayer audioLayer) { - return AudioDeviceModuleImpl::Create(id, audioLayer); -} +namespace webrtc { // ============================================================================ // Static methods @@ -81,33 +75,30 @@ AudioDeviceModule* CreateAudioDeviceModule( // AudioDeviceModule::Create() // ---------------------------------------------------------------------------- -AudioDeviceModule* AudioDeviceModuleImpl::Create(const int32_t id, - const AudioLayer audioLayer) -{ +rtc::scoped_refptr AudioDeviceModuleImpl::Create( + const int32_t id, + const AudioLayer audioLayer) { // Create the generic ref counted (platform independent) implementation. - RefCountImpl* audioDevice = - new RefCountImpl(id, audioLayer); + rtc::scoped_refptr audioDevice( + new rtc::RefCountedObject(id, audioLayer)); // Ensure that the current platform is supported. if (audioDevice->CheckPlatform() == -1) { - delete audioDevice; - return NULL; + return nullptr; } // Create the platform-dependent implementation. if (audioDevice->CreatePlatformSpecificObjects() == -1) { - delete audioDevice; - return NULL; + return nullptr; } // Ensure that the generic audio buffer can communicate with the // platform-specific parts. if (audioDevice->AttachAudioBuffer() == -1) { - delete audioDevice; - return NULL; + return nullptr; } WebRtcSpl_Init(); diff --git a/webrtc/modules/audio_device/audio_device_impl.h b/webrtc/modules/audio_device/audio_device_impl.h index d7c48abf17..a112e3e3bf 100644 --- a/webrtc/modules/audio_device/audio_device_impl.h +++ b/webrtc/modules/audio_device/audio_device_impl.h @@ -16,6 +16,7 @@ #include #include "webrtc/base/checks.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/audio_device/audio_device_buffer.h" #include "webrtc/modules/audio_device/include/audio_device.h" @@ -48,7 +49,7 @@ class AudioDeviceModuleImpl : public AudioDeviceModule { void Process() override; // Factory methods (resource allocation/deallocation) - static AudioDeviceModule* Create( + static rtc::scoped_refptr Create( const int32_t id, const AudioLayer audioLayer = kPlatformDefaultAudio); diff --git a/webrtc/modules/audio_device/include/audio_device.h b/webrtc/modules/audio_device/include/audio_device.h index 15e08730c7..d8df05cec8 100644 --- a/webrtc/modules/audio_device/include/audio_device.h +++ b/webrtc/modules/audio_device/include/audio_device.h @@ -211,9 +211,6 @@ class AudioDeviceModule : public RefCountedModule { virtual ~AudioDeviceModule() {} }; -AudioDeviceModule* CreateAudioDeviceModule( - int32_t id, AudioDeviceModule::AudioLayer audioLayer); - } // namespace webrtc #endif // MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_H_ diff --git a/webrtc/modules/audio_device/test/audio_device_test_api.cc b/webrtc/modules/audio_device/test/audio_device_test_api.cc index a564e35584..f37d89cd9c 100644 --- a/webrtc/modules/audio_device/test/audio_device_test_api.cc +++ b/webrtc/modules/audio_device/test/audio_device_test_api.cc @@ -48,11 +48,11 @@ using namespace webrtc; class AudioEventObserverAPI: public AudioDeviceObserver { public: - AudioEventObserverAPI(AudioDeviceModule* audioDevice) + AudioEventObserverAPI( + const rtc::scoped_refptr& audioDevice) : error_(kRecordingError), warning_(kRecordingWarning), - audio_device_(audioDevice) { - } + audio_device_(audioDevice) {} ~AudioEventObserverAPI() {} @@ -72,12 +72,12 @@ class AudioEventObserverAPI: public AudioDeviceObserver { ErrorCode error_; WarningCode warning_; private: - AudioDeviceModule* audio_device_; + rtc::scoped_refptr audio_device_; }; class AudioTransportAPI: public AudioTransport { public: - AudioTransportAPI(AudioDeviceModule* audioDevice) + AudioTransportAPI(const rtc::scoped_refptr& audioDevice) : rec_count_(0), play_count_(0) { } @@ -161,13 +161,11 @@ class AudioDeviceAPITest: public testing::Test { // create default implementation (=Core Audio) instance EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL); - audio_device_->AddRef(); - EXPECT_EQ(0, audio_device_->Release()); + EXPECT_EQ(0, audio_device_.release()->Release()); // create non-default (=Wave Audio) instance EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kWindowsWaveAudio)) != NULL); - audio_device_->AddRef(); - EXPECT_EQ(0, audio_device_->Release()); + EXPECT_EQ(0, audio_device_.release()->Release()); // explicitly specify usage of Core Audio (same as default) EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kWindowsCoreAudio)) != NULL); @@ -178,8 +176,7 @@ class AudioDeviceAPITest: public testing::Test { // create default implementation (=Wave Audio) instance EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL); - audio_device_->AddRef(); - EXPECT_EQ(0, audio_device_->Release()); + EXPECT_EQ(0, audio_device_.release()->Release()); // explicitly specify usage of Wave Audio (same as default) EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kWindowsWaveAudio)) != NULL); @@ -207,9 +204,8 @@ class AudioDeviceAPITest: public testing::Test { // create default implementation instance EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kPlatformDefaultAudio)) != NULL); - audio_device_->AddRef(); EXPECT_EQ(0, audio_device_->Terminate()); - EXPECT_EQ(0, audio_device_->Release()); + EXPECT_EQ(0, audio_device_.release()->Release()); // explicitly specify usage of Pulse Audio (same as default) EXPECT_TRUE((audio_device_ = AudioDeviceModuleImpl::Create( kId, AudioDeviceModule::kLinuxPulseAudio)) != NULL); @@ -234,9 +230,6 @@ class AudioDeviceAPITest: public testing::Test { FAIL() << "Failed creating audio device object!"; } - // The ADM is reference counted. - audio_device_->AddRef(); - process_thread_->RegisterModule(audio_device_); AudioDeviceModule::AudioLayer audio_layer = @@ -261,9 +254,8 @@ class AudioDeviceAPITest: public testing::Test { delete audio_transport_; audio_transport_ = NULL; } - if (audio_device_) { - EXPECT_EQ(0, audio_device_->Release()); - } + if (audio_device_) + EXPECT_EQ(0, audio_device_.release()->Release()); PRINT_TEST_RESULTS; } @@ -304,7 +296,7 @@ class AudioDeviceAPITest: public testing::Test { // TODO(henrika): Get rid of globals. static bool linux_alsa_; static std::unique_ptr process_thread_; - static AudioDeviceModule* audio_device_; + static rtc::scoped_refptr audio_device_; static AudioTransportAPI* audio_transport_; static AudioEventObserverAPI* event_observer_; }; @@ -312,7 +304,7 @@ class AudioDeviceAPITest: public testing::Test { // Must be initialized like this to handle static SetUpTestCase() above. bool AudioDeviceAPITest::linux_alsa_ = false; std::unique_ptr AudioDeviceAPITest::process_thread_; -AudioDeviceModule* AudioDeviceAPITest::audio_device_ = NULL; +rtc::scoped_refptr AudioDeviceAPITest::audio_device_; AudioTransportAPI* AudioDeviceAPITest::audio_transport_ = NULL; AudioEventObserverAPI* AudioDeviceAPITest::event_observer_ = NULL; diff --git a/webrtc/modules/desktop_capture/differ_block_unittest.cc b/webrtc/modules/desktop_capture/differ_block_unittest.cc index df9f4d517a..636f453a3b 100644 --- a/webrtc/modules/desktop_capture/differ_block_unittest.cc +++ b/webrtc/modules/desktop_capture/differ_block_unittest.cc @@ -10,7 +10,6 @@ #include "testing/gmock/include/gmock/gmock.h" #include "webrtc/modules/desktop_capture/differ_block.h" -#include "webrtc/system_wrappers/include/ref_count.h" namespace webrtc { diff --git a/webrtc/modules/rtp_rtcp/source/forward_error_correction.h b/webrtc/modules/rtp_rtcp/source/forward_error_correction.h index 9ba6ce0438..cbeb97e7b1 100644 --- a/webrtc/modules/rtp_rtcp/source/forward_error_correction.h +++ b/webrtc/modules/rtp_rtcp/source/forward_error_correction.h @@ -14,9 +14,9 @@ #include #include +#include "webrtc/base/refcount.h" #include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/typedefs.h" namespace webrtc { diff --git a/webrtc/modules/video_capture/external/video_capture_external.cc b/webrtc/modules/video_capture/external/video_capture_external.cc index 29b161263c..d636eeeef1 100644 --- a/webrtc/modules/video_capture/external/video_capture_external.cc +++ b/webrtc/modules/video_capture/external/video_capture_external.cc @@ -8,19 +8,17 @@ * be found in the AUTHORS file in the root of the source tree. */ +#include "webrtc/base/refcount.h" #include "webrtc/modules/video_capture/video_capture_impl.h" -#include "webrtc/system_wrappers/include/ref_count.h" namespace webrtc { namespace videocapturemodule { -VideoCaptureModule* VideoCaptureImpl::Create( +rtc::scoped_refptr VideoCaptureImpl::Create( const int32_t id, const char* deviceUniqueIdUTF8) { - RefCountImpl* implementation = - new RefCountImpl(id); - return implementation; + return new rtc::RefCountedObject(id); } } // namespace videocapturemodule diff --git a/webrtc/modules/video_capture/ios/video_capture_ios.h b/webrtc/modules/video_capture/ios/video_capture_ios.h index 1afcbaddf5..5172855161 100644 --- a/webrtc/modules/video_capture/ios/video_capture_ios.h +++ b/webrtc/modules/video_capture/ios/video_capture_ios.h @@ -11,6 +11,7 @@ #ifndef WEBRTC_MODULES_VIDEO_CAPTURE_IOS_VIDEO_CAPTURE_IOS_H_ #define WEBRTC_MODULES_VIDEO_CAPTURE_IOS_VIDEO_CAPTURE_IOS_H_ +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/video_capture/video_capture_impl.h" @class RTCVideoCaptureIosObjC; @@ -22,8 +23,9 @@ class VideoCaptureIos : public VideoCaptureImpl { explicit VideoCaptureIos(const int32_t capture_id); virtual ~VideoCaptureIos(); - static VideoCaptureModule* Create(const int32_t capture_id, - const char* device_unique_id_utf8); + static rtc::scoped_refptr Create( + const int32_t capture_id, + const char* device_unique_id_utf8); // Implementation of VideoCaptureImpl. int32_t StartCapture(const VideoCaptureCapability& capability) override; diff --git a/webrtc/modules/video_capture/ios/video_capture_ios.mm b/webrtc/modules/video_capture/ios/video_capture_ios.mm index ae9b7e0805..7a1f17bd13 100644 --- a/webrtc/modules/video_capture/ios/video_capture_ios.mm +++ b/webrtc/modules/video_capture/ios/video_capture_ios.mm @@ -12,16 +12,18 @@ #error "This file requires ARC support." #endif +#include "webrtc/base/refcount.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/video_capture/ios/device_info_ios_objc.h" #include "webrtc/modules/video_capture/ios/rtc_video_capture_ios_objc.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/trace.h" using namespace webrtc; using namespace videocapturemodule; -VideoCaptureModule* VideoCaptureImpl::Create(const int32_t capture_id, - const char* deviceUniqueIdUTF8) { +rtc::scoped_refptr VideoCaptureImpl::Create( + const int32_t capture_id, + const char* deviceUniqueIdUTF8) { return VideoCaptureIos::Create(capture_id, deviceUniqueIdUTF8); } @@ -40,18 +42,19 @@ VideoCaptureIos::~VideoCaptureIos() { } } -VideoCaptureModule* VideoCaptureIos::Create(const int32_t capture_id, - const char* deviceUniqueIdUTF8) { +rtc::scoped_refptr VideoCaptureIos::Create( + const int32_t capture_id, + const char* deviceUniqueIdUTF8) { if (!deviceUniqueIdUTF8[0]) { return NULL; } - RefCountImpl* capture_module = - new RefCountImpl(capture_id); + rtc::scoped_refptr capture_module( + new rtc::RefCountedObject(capture_id)); const int32_t name_length = strlen(deviceUniqueIdUTF8); if (name_length > kVideoCaptureUniqueNameLength) - return NULL; + return nullptr; capture_module->_deviceUniqueId = new char[name_length + 1]; strncpy(capture_module->_deviceUniqueId, deviceUniqueIdUTF8, name_length + 1); @@ -61,13 +64,13 @@ VideoCaptureModule* VideoCaptureIos::Create(const int32_t capture_id, [[RTCVideoCaptureIosObjC alloc] initWithOwner:capture_module captureId:capture_module->id_]; if (!capture_module->capture_device_) { - return NULL; + return nullptr; } if (![capture_module->capture_device_ setCaptureDeviceByUniqueId:[ [NSString alloc] initWithCString:deviceUniqueIdUTF8 encoding:NSUTF8StringEncoding]]) { - return NULL; + return nullptr; } return capture_module; } diff --git a/webrtc/modules/video_capture/linux/device_info_linux.cc b/webrtc/modules/video_capture/linux/device_info_linux.cc index d3a10abb56..ddcfe4bca1 100644 --- a/webrtc/modules/video_capture/linux/device_info_linux.cc +++ b/webrtc/modules/video_capture/linux/device_info_linux.cc @@ -20,7 +20,6 @@ //v4l includes #include -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/trace.h" @@ -31,14 +30,7 @@ namespace videocapturemodule VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo(const int32_t id) { - videocapturemodule::DeviceInfoLinux *deviceInfo = - new videocapturemodule::DeviceInfoLinux(id); - if (!deviceInfo) - { - deviceInfo = NULL; - } - - return deviceInfo; + return new videocapturemodule::DeviceInfoLinux(id); } DeviceInfoLinux::DeviceInfoLinux(const int32_t id) diff --git a/webrtc/modules/video_capture/linux/video_capture_linux.cc b/webrtc/modules/video_capture/linux/video_capture_linux.cc index 13149dcafc..cee1355c09 100644 --- a/webrtc/modules/video_capture/linux/video_capture_linux.cc +++ b/webrtc/modules/video_capture/linux/video_capture_linux.cc @@ -21,26 +21,22 @@ #include #include +#include "webrtc/base/refcount.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/video_capture/linux/video_capture_linux.h" #include "webrtc/system_wrappers/include/critical_section_wrapper.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/trace.h" -namespace webrtc -{ -namespace videocapturemodule -{ -VideoCaptureModule* VideoCaptureImpl::Create(const int32_t id, - const char* deviceUniqueId) -{ - RefCountImpl* implementation = - new RefCountImpl(id); +namespace webrtc { +namespace videocapturemodule { +rtc::scoped_refptr VideoCaptureImpl::Create( + const int32_t id, + const char* deviceUniqueId) { + rtc::scoped_refptr implementation( + new rtc::RefCountedObject(id)); - if (!implementation || implementation->Init(deviceUniqueId) != 0) - { - delete implementation; - implementation = NULL; - } + if (implementation->Init(deviceUniqueId) != 0) + return nullptr; return implementation; } diff --git a/webrtc/modules/video_capture/mac/video_capture_mac.mm b/webrtc/modules/video_capture/mac/video_capture_mac.mm index a9dab96e93..08b2aeb39a 100644 --- a/webrtc/modules/video_capture/mac/video_capture_mac.mm +++ b/webrtc/modules/video_capture/mac/video_capture_mac.mm @@ -15,10 +15,11 @@ #include +#include "webrtc/base/refcount.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/video_capture/device_info_impl.h" #include "webrtc/modules/video_capture/video_capture_config.h" #include "webrtc/modules/video_capture/video_capture_impl.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/trace.h" // 10.4 support must be decided runtime. We will just decide which framework to @@ -109,25 +110,23 @@ bool CheckQTVersion() * version buffer */ -VideoCaptureModule* VideoCaptureImpl::Create( - const int32_t id, const char* deviceUniqueIdUTF8) -{ - - if (webrtc::videocapturemodule::CheckOSVersion() == false) - { +rtc::scoped_refptr VideoCaptureImpl::Create( + const int32_t id, + const char* deviceUniqueIdUTF8) { + if (!CheckOSVersion()) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, id, "OS version is too old. Could not create video capture " "module. Returning NULL"); - return NULL; + return nullptr; } #if __MAC_OS_X_VERSION_MIN_REQUIRED == __MAC_10_4 // QuickTime version - if (webrtc::videocapturemodule::CheckQTVersion() == false) + if (!CheckQTVersion()) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, id, "QuickTime version is too old. Could not create video " "capture module. Returning NULL"); - return NULL; + return nullptr; } WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, id, @@ -135,17 +134,8 @@ VideoCaptureModule* VideoCaptureImpl::Create( "QuickTime framework to capture video", __FILE__, __LINE__); - RefCountImpl* - newCaptureModule = - new RefCountImpl(id); - - if (!newCaptureModule) - { - WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, id, - "could not Create for unique device %s, !newCaptureModule", - deviceUniqueIdUTF8); - return NULL; - } + rtc::scoped_refptr newCaptureModule( + new rtc::RefCountedObject(id)); if (newCaptureModule->Init(id, deviceUniqueIdUTF8) != 0) { @@ -153,8 +143,7 @@ VideoCaptureModule* VideoCaptureImpl::Create( "could not Create for unique device %s, " "newCaptureModule->Init()!=0", deviceUniqueIdUTF8); - delete newCaptureModule; - return NULL; + return nullptr; } // Successfully created VideoCaptureMacQuicktime. Return it @@ -169,23 +158,15 @@ VideoCaptureModule* VideoCaptureImpl::Create( WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, id, "Using QTKit framework to capture video", id); - RefCountImpl* newCaptureModule = - new RefCountImpl(id); + rtc::scoped_refptr newCaptureModule( + new rtc::RefCountedObject(id)); - if(!newCaptureModule) - { - WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, id, - "could not Create for unique device %s, !newCaptureModule", - deviceUniqueIdUTF8); - return NULL; - } if(newCaptureModule->Init(id, deviceUniqueIdUTF8) != 0) { WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCapture, id, "could not Create for unique device %s, " "newCaptureModule->Init()!=0", deviceUniqueIdUTF8); - delete newCaptureModule; - return NULL; + return nullptr; } // Successfully created VideoCaptureMacQuicktime. Return it diff --git a/webrtc/modules/video_capture/video_capture_factory.cc b/webrtc/modules/video_capture/video_capture_factory.cc index 618c08bac6..927beba080 100644 --- a/webrtc/modules/video_capture/video_capture_factory.cc +++ b/webrtc/modules/video_capture/video_capture_factory.cc @@ -12,10 +12,10 @@ #include "webrtc/modules/video_capture/video_capture_impl.h" -namespace webrtc -{ +namespace webrtc { -VideoCaptureModule* VideoCaptureFactory::Create(const int32_t id, +rtc::scoped_refptr VideoCaptureFactory::Create( + const int32_t id, const char* deviceUniqueIdUTF8) { #if defined(ANDROID) return nullptr; @@ -24,7 +24,8 @@ VideoCaptureModule* VideoCaptureFactory::Create(const int32_t id, #endif } -VideoCaptureModule* VideoCaptureFactory::Create(const int32_t id, +rtc::scoped_refptr VideoCaptureFactory::Create( + const int32_t id, VideoCaptureExternal*& externalCapture) { return videocapturemodule::VideoCaptureImpl::Create(id, externalCapture); } diff --git a/webrtc/modules/video_capture/video_capture_factory.h b/webrtc/modules/video_capture/video_capture_factory.h index 4765be1fde..f05609a643 100644 --- a/webrtc/modules/video_capture/video_capture_factory.h +++ b/webrtc/modules/video_capture/video_capture_factory.h @@ -24,14 +24,16 @@ class VideoCaptureFactory { // id - unique identifier of this video capture module object. // deviceUniqueIdUTF8 - name of the device. // Available names can be found by using GetDeviceName - static VideoCaptureModule* Create(const int32_t id, - const char* deviceUniqueIdUTF8); + static rtc::scoped_refptr Create( + const int32_t id, + const char* deviceUniqueIdUTF8); // Create a video capture module object used for external capture. // id - unique identifier of this video capture module object // externalCapture - [out] interface to call when a new frame is captured. - static VideoCaptureModule* Create(const int32_t id, - VideoCaptureExternal*& externalCapture); + static rtc::scoped_refptr Create( + const int32_t id, + VideoCaptureExternal*& externalCapture); static VideoCaptureModule::DeviceInfo* CreateDeviceInfo( const int32_t id); diff --git a/webrtc/modules/video_capture/video_capture_impl.cc b/webrtc/modules/video_capture/video_capture_impl.cc index 8319a21562..b1e697edc2 100644 --- a/webrtc/modules/video_capture/video_capture_impl.cc +++ b/webrtc/modules/video_capture/video_capture_impl.cc @@ -12,6 +12,7 @@ #include +#include "webrtc/base/refcount.h" #include "webrtc/base/trace_event.h" #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" #include "webrtc/modules/include/module_common_types.h" @@ -19,22 +20,17 @@ #include "webrtc/system_wrappers/include/clock.h" #include "webrtc/system_wrappers/include/critical_section_wrapper.h" #include "webrtc/system_wrappers/include/logging.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/tick_util.h" -namespace webrtc -{ - -namespace videocapturemodule -{ -VideoCaptureModule* VideoCaptureImpl::Create( +namespace webrtc { +namespace videocapturemodule { +rtc::scoped_refptr VideoCaptureImpl::Create( const int32_t id, - VideoCaptureExternal*& externalCapture) -{ - RefCountImpl* implementation = - new RefCountImpl(id); - externalCapture = implementation; - return implementation; + VideoCaptureExternal*& externalCapture) { + rtc::scoped_refptr implementation( + new rtc::RefCountedObject(id)); + externalCapture = implementation.get(); + return implementation; } const char* VideoCaptureImpl::CurrentDeviceName() const diff --git a/webrtc/modules/video_capture/video_capture_impl.h b/webrtc/modules/video_capture/video_capture_impl.h index 4aa39394c7..9c2cad7c95 100644 --- a/webrtc/modules/video_capture/video_capture_impl.h +++ b/webrtc/modules/video_capture/video_capture_impl.h @@ -15,6 +15,7 @@ * video_capture_impl.h */ +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" #include "webrtc/common_video/rotation.h" #include "webrtc/modules/video_capture/video_capture.h" @@ -38,8 +39,9 @@ public: * id - unique identifier of this video capture module object * deviceUniqueIdUTF8 - name of the device. Available names can be found by using GetDeviceName */ - static VideoCaptureModule* Create(const int32_t id, - const char* deviceUniqueIdUTF8); + static rtc::scoped_refptr Create( + const int32_t id, + const char* deviceUniqueIdUTF8); /* * Create a video capture module object used for external capture. @@ -47,8 +49,9 @@ public: * id - unique identifier of this video capture module object * externalCapture - [out] interface to call when a new frame is captured. */ - static VideoCaptureModule* Create(const int32_t id, - VideoCaptureExternal*& externalCapture); + static rtc::scoped_refptr Create( + const int32_t id, + VideoCaptureExternal*& externalCapture); static DeviceInfo* CreateDeviceInfo(const int32_t id); diff --git a/webrtc/modules/video_capture/windows/device_info_ds.cc b/webrtc/modules/video_capture/windows/device_info_ds.cc index 066a741839..6b459c5787 100644 --- a/webrtc/modules/video_capture/windows/device_info_ds.cc +++ b/webrtc/modules/video_capture/windows/device_info_ds.cc @@ -13,7 +13,6 @@ #include "webrtc/modules/video_capture/video_capture_config.h" #include "webrtc/modules/video_capture/video_capture_delay.h" #include "webrtc/modules/video_capture/windows/help_functions_ds.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/trace.h" #include diff --git a/webrtc/modules/video_capture/windows/video_capture_factory_windows.cc b/webrtc/modules/video_capture/windows/video_capture_factory_windows.cc index 747d3d60cf..0d347cb64f 100644 --- a/webrtc/modules/video_capture/windows/video_capture_factory_windows.cc +++ b/webrtc/modules/video_capture/windows/video_capture_factory_windows.cc @@ -8,9 +8,10 @@ * be found in the AUTHORS file in the root of the source tree. */ +#include "webrtc/base/refcount.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/video_capture/windows/video_capture_ds.h" #include "webrtc/modules/video_capture/windows/video_capture_mf.h" -#include "webrtc/system_wrappers/include/ref_count.h" namespace webrtc { namespace videocapturemodule { @@ -22,16 +23,17 @@ VideoCaptureModule::DeviceInfo* VideoCaptureImpl::CreateDeviceInfo( return DeviceInfoDS::Create(id); } -VideoCaptureModule* VideoCaptureImpl::Create(const int32_t id, - const char* device_id) { - if (device_id == NULL) - return NULL; +rtc::scoped_refptr VideoCaptureImpl::Create( + const int32_t id, + const char* device_id) { + if (device_id == nullptr) + return nullptr; // TODO(tommi): Use Media Foundation implementation for Vista and up. - RefCountImpl* capture = new RefCountImpl(id); + rtc::scoped_refptr capture( + new rtc::RefCountedObject(id)); if (capture->Init(id, device_id) != 0) { - delete capture; - capture = NULL; + return nullptr; } return capture; diff --git a/webrtc/system_wrappers/BUILD.gn b/webrtc/system_wrappers/BUILD.gn index b288230820..e789260531 100644 --- a/webrtc/system_wrappers/BUILD.gn +++ b/webrtc/system_wrappers/BUILD.gn @@ -27,7 +27,6 @@ static_library("system_wrappers") { "include/fix_interlocked_exchange_pointer_win.h", "include/logging.h", "include/metrics.h", - "include/ref_count.h", "include/rtp_to_ntp.h", "include/rw_lock_wrapper.h", "include/scoped_vector.h", diff --git a/webrtc/system_wrappers/include/ref_count.h b/webrtc/system_wrappers/include/ref_count.h deleted file mode 100644 index 3dd335a8da..0000000000 --- a/webrtc/system_wrappers/include/ref_count.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef SYSTEM_WRAPPERS_INCLUDE_REF_COUNT_H_ -#define SYSTEM_WRAPPERS_INCLUDE_REF_COUNT_H_ - -#include "webrtc/system_wrappers/include/atomic32.h" - -namespace webrtc { - -// This class can be used for instantiating -// reference counted objects. -// int32_t AddRef() and int32_t Release(). -// Usage: -// RefCountImpl* implementation = new RefCountImpl(p); -// -// Example: -// class MyInterface { -// public: -// virtual void DoSomething() = 0; -// virtual int32_t AddRef() = 0; -// virtual int32_t Release() = 0: -// private: -// virtual ~MyInterface(){}; -// } -// class MyImplementation : public MyInterface { -// public: -// virtual DoSomething() { printf("hello"); }; -// }; -// MyImplementation* CreateMyImplementation() { -// RefCountImpl* implementation = -// new RefCountImpl(); -// return implementation; -// } - -template -class RefCountImpl : public T { - public: - RefCountImpl() : ref_count_(0) {} - - template - explicit RefCountImpl(P p) : T(p), ref_count_(0) {} - - template - RefCountImpl(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) {} - - template - RefCountImpl(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) {} - - template - RefCountImpl(P1 p1, P2 p2, P3 p3, P4 p4) : T(p1, p2, p3, p4), ref_count_(0) {} - - template - RefCountImpl(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) - : T(p1, p2, p3, p4, p5), ref_count_(0) {} - - int32_t AddRef() const override { - return ++ref_count_; - } - - int32_t Release() const override { - int32_t ref_count; - ref_count = --ref_count_; - if (ref_count == 0) - delete this; - return ref_count; - } - - protected: - mutable Atomic32 ref_count_; -}; - -} // namespace webrtc - -#endif // SYSTEM_WRAPPERS_INCLUDE_REF_COUNT_H_ diff --git a/webrtc/system_wrappers/system_wrappers.gyp b/webrtc/system_wrappers/system_wrappers.gyp index dc0de49957..0ae941761d 100644 --- a/webrtc/system_wrappers/system_wrappers.gyp +++ b/webrtc/system_wrappers/system_wrappers.gyp @@ -35,7 +35,6 @@ 'include/logging.h', 'include/metrics.h', 'include/ntp_time.h', - 'include/ref_count.h', 'include/rtp_to_ntp.h', 'include/rw_lock_wrapper.h', 'include/scoped_vector.h', diff --git a/webrtc/test/vcm_capturer.cc b/webrtc/test/vcm_capturer.cc index 0a82236c98..792b97ff08 100644 --- a/webrtc/test/vcm_capturer.cc +++ b/webrtc/test/vcm_capturer.cc @@ -79,17 +79,13 @@ void VcmCapturer::Stop() { } void VcmCapturer::Destroy() { - if (vcm_ == NULL) { + if (!vcm_) return; - } vcm_->StopCapture(); vcm_->DeRegisterCaptureDataCallback(); - vcm_->Release(); - - // TODO(pbos): How do I destroy the VideoCaptureModule? This still leaves - // non-freed memory. - vcm_ = NULL; + // Release reference to VCM. + vcm_ = nullptr; } VcmCapturer::~VcmCapturer() { Destroy(); } diff --git a/webrtc/test/vcm_capturer.h b/webrtc/test/vcm_capturer.h index 6c30dd50e0..65916ec52e 100644 --- a/webrtc/test/vcm_capturer.h +++ b/webrtc/test/vcm_capturer.h @@ -11,6 +11,7 @@ #define WEBRTC_TEST_VCM_CAPTURER_H_ #include "webrtc/base/criticalsection.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/common_types.h" #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" #include "webrtc/modules/video_capture/video_capture.h" @@ -41,7 +42,7 @@ class VcmCapturer : public VideoCapturer, public VideoCaptureDataCallback { rtc::CriticalSection crit_; bool started_ GUARDED_BY(crit_); - VideoCaptureModule* vcm_; + rtc::scoped_refptr vcm_; VideoCaptureCapability capability_; }; } // test diff --git a/webrtc/video/video_capture_input_unittest.cc b/webrtc/video/video_capture_input_unittest.cc index 075bee9016..9c8987f9a3 100644 --- a/webrtc/video/video_capture_input_unittest.cc +++ b/webrtc/video/video_capture_input_unittest.cc @@ -14,7 +14,7 @@ #include "testing/gtest/include/gtest/gtest.h" #include "webrtc/base/event.h" -#include "webrtc/system_wrappers/include/ref_count.h" +#include "webrtc/base/refcount.h" #include "webrtc/system_wrappers/include/scoped_vector.h" #include "webrtc/test/fake_texture_frame.h" #include "webrtc/video/send_statistics_proxy.h" diff --git a/webrtc/video/video_send_stream_tests.cc b/webrtc/video/video_send_stream_tests.cc index de5f4b0f28..d9fc61875e 100644 --- a/webrtc/video/video_send_stream_tests.cc +++ b/webrtc/video/video_send_stream_tests.cc @@ -28,7 +28,6 @@ #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" #include "webrtc/modules/rtp_rtcp/source/rtp_format_vp9.h" #include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h" -#include "webrtc/system_wrappers/include/ref_count.h" #include "webrtc/system_wrappers/include/sleep.h" #include "webrtc/test/call_test.h" #include "webrtc/test/configurable_frame_size_encoder.h" diff --git a/webrtc/voice_engine/shared_data.cc b/webrtc/voice_engine/shared_data.cc index b21578c927..997f51b439 100644 --- a/webrtc/voice_engine/shared_data.cc +++ b/webrtc/voice_engine/shared_data.cc @@ -54,14 +54,9 @@ SharedData::~SharedData() Trace::ReturnTrace(); } -void SharedData::set_audio_device(AudioDeviceModule* audio_device) -{ - // AddRef first in case the pointers are equal. - if (audio_device) - audio_device->AddRef(); - if (_audioDevicePtr) - _audioDevicePtr->Release(); - _audioDevicePtr = audio_device; +void SharedData::set_audio_device( + const rtc::scoped_refptr& audio_device) { + _audioDevicePtr = audio_device; } void SharedData::set_audio_processing(AudioProcessing* audioproc) { diff --git a/webrtc/voice_engine/shared_data.h b/webrtc/voice_engine/shared_data.h index a4a852a435..ea84effd1b 100644 --- a/webrtc/voice_engine/shared_data.h +++ b/webrtc/voice_engine/shared_data.h @@ -14,6 +14,7 @@ #include #include "webrtc/base/criticalsection.h" +#include "webrtc/base/scoped_ref_ptr.h" #include "webrtc/modules/audio_device/include/audio_device.h" #include "webrtc/modules/audio_processing/include/audio_processing.h" #include "webrtc/modules/utility/include/process_thread.h" @@ -38,8 +39,9 @@ public: uint32_t instance_id() const { return _instanceId; } Statistics& statistics() { return _engineStatistics; } ChannelManager& channel_manager() { return _channelManager; } - AudioDeviceModule* audio_device() { return _audioDevicePtr; } - void set_audio_device(AudioDeviceModule* audio_device); + AudioDeviceModule* audio_device() { return _audioDevicePtr.get(); } + void set_audio_device( + const rtc::scoped_refptr& audio_device); AudioProcessing* audio_processing() { return audioproc_.get(); } void set_audio_processing(AudioProcessing* audio_processing); TransmitMixer* transmit_mixer() { return _transmitMixerPtr; } @@ -67,7 +69,7 @@ protected: rtc::CriticalSection _apiCritPtr; ChannelManager _channelManager; Statistics _engineStatistics; - AudioDeviceModule* _audioDevicePtr; + rtc::scoped_refptr _audioDevicePtr; OutputMixer* _outputMixerPtr; TransmitMixer* _transmitMixerPtr; std::unique_ptr audioproc_;