diff --git a/dom/media/ChannelMediaResource.cpp b/dom/media/ChannelMediaResource.cpp index d011c4339ad1..a0cb0dda40bc 100644 --- a/dom/media/ChannelMediaResource.cpp +++ b/dom/media/ChannelMediaResource.cpp @@ -303,7 +303,7 @@ ChannelMediaResource::OnStartRequest(nsIRequest* aRequest, // TODO: Don't turn this on until we fix all data races. nsCOMPtr retarget; if (Preferences::GetBool("media.omt_data_delivery.enabled", false) && - (retarget = do_QueryInterface(aRequest)) && mCacheStream.OwnerThread()) { + (retarget = do_QueryInterface(aRequest))) { // Note this will not always succeed. We need to handle the case where // all resources sharing the same cache might run their data callbacks // on different threads. diff --git a/dom/media/MediaCache.cpp b/dom/media/MediaCache.cpp index b85e13b63534..bdca5d1b5a0b 100644 --- a/dom/media/MediaCache.cpp +++ b/dom/media/MediaCache.cpp @@ -290,18 +290,6 @@ protected: NS_ASSERTION(NS_IsMainThread(), "Only construct MediaCache on main thread"); MOZ_COUNT_CTOR(MediaCache); MediaCacheFlusher::RegisterMediaCache(this); - - if (!sThreadInit) { - sThreadInit = true; - nsCOMPtr thread; - nsresult rv = NS_NewNamedThread("MediaCache", getter_AddRefs(thread)); - if (NS_FAILED(rv)) { - NS_WARNING("Failed to create a thread for MediaCache."); - return; - } - sThread = thread.forget(); - ClearOnShutdown(this, ShutdownPhase::ShutdownThreads); - } } ~MediaCache() @@ -462,8 +450,7 @@ protected: // A list of resource IDs to notify about the change in suspended status. nsTArray mSuspendedStatusToNotify; // The thread on which we will run data callbacks from the channels. - // Could be null if failing to create the thread. Note this thread is shared - // among all MediaCache instances. + // Note this thread is shared among all MediaCache instances. static StaticRefPtr sThread; // True if we've tried to init sThread. Note we try once only so it is safe // to access sThread on all threads. @@ -729,6 +716,26 @@ MediaCache::CloseStreamsForPrivateBrowsing() MediaCache::GetMediaCache(int64_t aContentLength) { NS_ASSERTION(NS_IsMainThread(), "Only call on main thread"); + + if (!sThreadInit) { + sThreadInit = true; + nsCOMPtr thread; + nsresult rv = NS_NewNamedThread("MediaCache", getter_AddRefs(thread)); + if (NS_FAILED(rv)) { + NS_WARNING("Failed to create a thread for MediaCache."); + return nullptr; + } + sThread = thread.forget(); + // Note it is safe to pass an invalid pointer for operator=(std::nullptr_t) + // is non-virtual and it will not access |this|. + ClearOnShutdown(reinterpret_cast(0x1), + ShutdownPhase::ShutdownThreads); + } + + if (!sThread) { + return nullptr; + } + if (aContentLength > 0 && aContentLength <= int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024) { // Small-enough resource, use a new memory-backed MediaCache.