Bug 1430993 - Replace RefPtr with StaticRefPtr to avoid the static constructor. r?jib
MozReview-Commit-ID: 9xqP5QFlbZJ
--- a/dom/media/systemservices/CamerasParent.cpp
+++ b/dom/media/systemservices/CamerasParent.cpp
@@ -59,17 +59,17 @@ ResolutionFeasibilityDistance(int32_t ca
}
uint32_t
FeasibilityDistance(int32_t candidate, int32_t requested)
{
return std::abs(candidate - requested) * 1000 / std::max(candidate, requested);
}
-RefPtr<VideoEngine> CamerasParent::sEngines[CaptureEngine::MaxEngine];
+StaticRefPtr<VideoEngine> CamerasParent::sEngines[CaptureEngine::MaxEngine];
int32_t CamerasParent::sNumOfOpenCamerasParentEngines = 0;
int32_t CamerasParent::sNumOfCamerasParents = 0;
base::Thread* CamerasParent::sVideoCaptureThread = nullptr;
Monitor* CamerasParent::sThreadMonitor = nullptr;
StaticMutex CamerasParent::sMutex;
// 3 threads are involved in this code:
// - the main thread for some setups, and occassionally for video capture setup
@@ -333,20 +333,20 @@ CamerasParent::RecvReleaseFrame(mozilla:
mShmemPool.Put(ShmemBuffer(s));
return IPC_OK();
}
bool
CamerasParent::SetupEngine(CaptureEngine aCapEngine)
{
LOG((__PRETTY_FUNCTION__));
- RefPtr<mozilla::camera::VideoEngine>* engine = &sEngines[aCapEngine];
+ StaticRefPtr<VideoEngine>& engine = sEngines[aCapEngine];
- if (!engine->get()) {
- webrtc::CaptureDeviceInfo *captureDeviceInfo = nullptr;
+ if (!engine) {
+ webrtc::CaptureDeviceInfo *captureDeviceInfo = nullptr;
UniquePtr<webrtc::Config> config(new webrtc::Config);
switch (aCapEngine) {
case ScreenEngine:
captureDeviceInfo =
new webrtc::CaptureDeviceInfo(webrtc::CaptureDeviceType::Screen);
break;
case BrowserEngine:
@@ -367,27 +367,27 @@ CamerasParent::SetupEngine(CaptureEngine
break;
default:
LOG(("Invalid webrtc Video engine"));
MOZ_CRASH();
break;
}
config->Set<webrtc::CaptureDeviceInfo>(captureDeviceInfo);
- *engine = mozilla::camera::VideoEngine::Create(UniquePtr<const webrtc::Config>(config.release()));
+ engine = VideoEngine::Create(Move(config));
- if (!engine->get()) {
+ if (!engine) {
LOG(("VideoEngine::Create failed"));
return false;
}
}
if (aCapEngine == CameraEngine && !mCameraObserver) {
mCameraObserver = new InputObserver(this);
- auto device_info = engine->get()->GetOrCreateVideoCaptureDeviceInfo();
+ auto device_info = engine->GetOrCreateVideoCaptureDeviceInfo();
MOZ_ASSERT(device_info);
if (device_info) {
device_info->RegisterVideoInputFeedBack(mCameraObserver.get());
}
}
return true;
}
@@ -405,32 +405,32 @@ CamerasParent::CloseEngines()
while (mCallbacks.Length()) {
auto capEngine = mCallbacks[0]->mCapEngine;
auto streamNum = mCallbacks[0]->mStreamId;
LOG(("Forcing shutdown of engine %d, capturer %d", capEngine, streamNum));
StopCapture(capEngine, streamNum);
Unused << ReleaseCaptureDevice(capEngine, streamNum);
}
- auto engine = sEngines[CameraEngine].get();
+ StaticRefPtr<VideoEngine>& engine = sEngines[CameraEngine];
if (engine && mCameraObserver) {
auto device_info = engine->GetOrCreateVideoCaptureDeviceInfo();
MOZ_ASSERT(device_info);
if (device_info) {
device_info->DeRegisterVideoInputFeedBack(mCameraObserver.get());
}
mCameraObserver = nullptr;
}
// CloseEngines() is protected by sThreadMonitor
sNumOfOpenCamerasParentEngines--;
if (sNumOfOpenCamerasParentEngines == 0) {
for (auto& engine : sEngines) {
- if (engine.get()) {
- mozilla::camera::VideoEngine::Delete(engine.get());
+ if (engine) {
+ VideoEngine::Delete(engine);
engine = nullptr;
}
}
}
mWebRTCAlive = false;
}
@@ -749,17 +749,17 @@ CamerasParent::RecvAllocateCaptureDevice
// After retrieving the permission (or not) on the main thread,
// bounce to the WebRTC thread to allocate the device (or not),
// then bounce back to the IPC thread for the reply to content.
RefPtr<Runnable> webrtc_runnable =
media::NewRunnableFrom([self, allowed, aCapEngine, unique_id]() -> nsresult {
int numdev = -1;
int error = -1;
if (allowed && self->EnsureInitialized(aCapEngine)) {
- auto engine = self->sEngines[aCapEngine].get();
+ StaticRefPtr<VideoEngine>& engine = self->sEngines[aCapEngine];
engine->CreateVideoCapture(numdev, unique_id.get());
engine->WithEntry(numdev, [&error](VideoEngine::CaptureEntry& cap) {
if (cap.VideoCapture()) {
error = 0;
}
});
}
RefPtr<nsIRunnable> ipc_runnable =
--- a/dom/media/systemservices/CamerasParent.h
+++ b/dom/media/systemservices/CamerasParent.h
@@ -136,17 +136,17 @@ protected:
// Can't take already_AddRefed because it can fail in stupid ways.
nsresult DispatchToVideoCaptureThread(Runnable* event);
// sEngines will be accessed by VideoCapture thread only
// sNumOfCamerasParent, sNumOfOpenCamerasParentEngines, and sVideoCaptureThread will
// be accessed by main thread / PBackground thread / VideoCapture thread
// sNumOfCamerasParent and sThreadMonitor create & delete are protected by sMutex
// sNumOfOpenCamerasParentEngines and sVideoCaptureThread are protected by sThreadMonitor
- static RefPtr<VideoEngine> sEngines[CaptureEngine::MaxEngine];
+ static StaticRefPtr<VideoEngine> sEngines[CaptureEngine::MaxEngine];
static int32_t sNumOfOpenCamerasParentEngines;
static int32_t sNumOfCamerasParents;
nsTArray<CallbackHelper*> mCallbacks;
// image buffers
mozilla::ShmemPool mShmemPool;
// PBackground parent thread
--- a/dom/media/systemservices/VideoEngine.cpp
+++ b/dom/media/systemservices/VideoEngine.cpp
@@ -173,22 +173,23 @@ VideoEngine::GetOrCreateVideoCaptureDevi
return mDeviceInfo;
}
const UniquePtr<const webrtc::Config>&
VideoEngine::GetConfiguration() {
return mConfig;
}
-RefPtr<VideoEngine> VideoEngine::Create(UniquePtr<const webrtc::Config>&& aConfig) {
+already_AddRefed<VideoEngine>
+VideoEngine::Create(UniquePtr<const webrtc::Config>&& aConfig)
+{
LOG((__PRETTY_FUNCTION__));
LOG(("Creating new VideoEngine with CaptureDeviceType %s",
aConfig->Get<webrtc::CaptureDeviceInfo>().TypeName()));
- RefPtr<VideoEngine> engine(new VideoEngine(std::move(aConfig)));
- return engine;
+ return do_AddRef(new VideoEngine(Move(aConfig)));
}
VideoEngine::CaptureEntry::CaptureEntry(int32_t aCapnum,
rtc::scoped_refptr<webrtc::VideoCaptureModule> aCapture)
: mCapnum(aCapnum)
, mVideoCaptureModule(aCapture)
{}
--- a/dom/media/systemservices/VideoEngine.h
+++ b/dom/media/systemservices/VideoEngine.h
@@ -30,17 +30,17 @@ private:
// Note because cameras use HW plug event detection, this
// only applies to screen based modes.
static const int64_t kCacheExpiryPeriodMs = 2000;
public:
VideoEngine (){};
NS_INLINE_DECL_REFCOUNTING(VideoEngine)
- static RefPtr<VideoEngine> Create(UniquePtr<const webrtc::Config>&& aConfig);
+ static already_AddRefed<VideoEngine> Create(UniquePtr<const webrtc::Config>&& aConfig);
#if defined(ANDROID)
static int SetAndroidObjects(JavaVM* javaVM);
#endif
void CreateVideoCapture(int32_t& id, const char* deviceUniqueIdUTF8);
int ReleaseVideoCapture(const int32_t id);
// VideoEngine is responsible for any cleanup in its modules