content/media/webaudio/blink/HRTFDatabaseLoader.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/blink/HRTFDatabaseLoader.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,223 @@
     1.4 +/*
     1.5 + * Copyright (C) 2010 Google Inc. All rights reserved.
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + *
    1.11 + * 1.  Redistributions of source code must retain the above copyright
    1.12 + *     notice, this list of conditions and the following disclaimer.
    1.13 + * 2.  Redistributions in binary form must reproduce the above copyright
    1.14 + *     notice, this list of conditions and the following disclaimer in the
    1.15 + *     documentation and/or other materials provided with the distribution.
    1.16 + * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
    1.17 + *     its contributors may be used to endorse or promote products derived
    1.18 + *     from this software without specific prior written permission.
    1.19 + *
    1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
    1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
    1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +#include "HRTFDatabaseLoader.h"
    1.33 +#include "HRTFDatabase.h"
    1.34 +
    1.35 +using namespace mozilla;
    1.36 +
    1.37 +namespace WebCore {
    1.38 +
    1.39 +// Singleton
    1.40 +nsTHashtable<HRTFDatabaseLoader::LoaderByRateEntry>*
    1.41 +    HRTFDatabaseLoader::s_loaderMap = nullptr;
    1.42 +
    1.43 +TemporaryRef<HRTFDatabaseLoader> HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(float sampleRate)
    1.44 +{
    1.45 +    MOZ_ASSERT(NS_IsMainThread());
    1.46 +
    1.47 +    RefPtr<HRTFDatabaseLoader> loader;
    1.48 +    
    1.49 +    if (!s_loaderMap) {
    1.50 +        s_loaderMap = new nsTHashtable<LoaderByRateEntry>();
    1.51 +    }
    1.52 +
    1.53 +    LoaderByRateEntry* entry = s_loaderMap->PutEntry(sampleRate);
    1.54 +    loader = entry->mLoader;
    1.55 +    if (loader) { // existing entry
    1.56 +        MOZ_ASSERT(sampleRate == loader->databaseSampleRate());
    1.57 +        return loader;
    1.58 +    }
    1.59 +
    1.60 +    loader = new HRTFDatabaseLoader(sampleRate);
    1.61 +    entry->mLoader = loader;
    1.62 +
    1.63 +    loader->loadAsynchronously();
    1.64 +
    1.65 +    return loader;
    1.66 +}
    1.67 +
    1.68 +HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate)
    1.69 +    : m_refCnt(0)
    1.70 +    , m_threadLock("HRTFDatabaseLoader")
    1.71 +    , m_databaseLoaderThread(nullptr)
    1.72 +    , m_databaseSampleRate(sampleRate)
    1.73 +{
    1.74 +    MOZ_ASSERT(NS_IsMainThread());
    1.75 +}
    1.76 +
    1.77 +HRTFDatabaseLoader::~HRTFDatabaseLoader()
    1.78 +{
    1.79 +    MOZ_ASSERT(NS_IsMainThread());
    1.80 +
    1.81 +    waitForLoaderThreadCompletion();
    1.82 +    m_hrtfDatabase.reset();
    1.83 +
    1.84 +    if (s_loaderMap) {
    1.85 +        // Remove ourself from the map.
    1.86 +        s_loaderMap->RemoveEntry(m_databaseSampleRate);
    1.87 +        if (s_loaderMap->Count() == 0) {
    1.88 +            delete s_loaderMap;
    1.89 +            s_loaderMap = nullptr;
    1.90 +        }
    1.91 +    }
    1.92 +}
    1.93 +
    1.94 +size_t HRTFDatabaseLoader::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
    1.95 +{
    1.96 +    size_t amount = aMallocSizeOf(this);
    1.97 +
    1.98 +    // NB: Need to make sure we're not competing with the loader thread.
    1.99 +    const_cast<HRTFDatabaseLoader*>(this)->waitForLoaderThreadCompletion();
   1.100 +
   1.101 +    if (m_hrtfDatabase) {
   1.102 +        amount += m_hrtfDatabase->sizeOfIncludingThis(aMallocSizeOf);
   1.103 +    }
   1.104 +
   1.105 +    return amount;
   1.106 +}
   1.107 +
   1.108 +class HRTFDatabaseLoader::ProxyReleaseEvent MOZ_FINAL : public nsRunnable {
   1.109 +public:
   1.110 +    explicit ProxyReleaseEvent(HRTFDatabaseLoader* loader) : mLoader(loader) {}
   1.111 +    NS_IMETHOD Run() MOZ_OVERRIDE
   1.112 +    {
   1.113 +        mLoader->MainThreadRelease();
   1.114 +        return NS_OK;
   1.115 +    }
   1.116 +private:
   1.117 +    HRTFDatabaseLoader* mLoader;
   1.118 +};
   1.119 +
   1.120 +void HRTFDatabaseLoader::ProxyRelease()
   1.121 +{
   1.122 +    nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
   1.123 +    if (MOZ_LIKELY(mainThread)) {
   1.124 +        nsRefPtr<ProxyReleaseEvent> event = new ProxyReleaseEvent(this);
   1.125 +        DebugOnly<nsresult> rv =
   1.126 +            mainThread->Dispatch(event, NS_DISPATCH_NORMAL);
   1.127 +        MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to dispatch release event");
   1.128 +    } else {
   1.129 +        // Should be in XPCOM shutdown.
   1.130 +        MOZ_ASSERT(NS_IsMainThread(),
   1.131 +                   "Main thread is not available for dispatch.");
   1.132 +        MainThreadRelease();
   1.133 +    }
   1.134 +}
   1.135 +
   1.136 +void HRTFDatabaseLoader::MainThreadRelease()
   1.137 +{
   1.138 +    MOZ_ASSERT(NS_IsMainThread());
   1.139 +    int count = --m_refCnt;
   1.140 +    MOZ_ASSERT(count >= 0, "extra release");
   1.141 +    NS_LOG_RELEASE(this, count, "HRTFDatabaseLoader");
   1.142 +    if (count == 0) {
   1.143 +        // It is safe to delete here as the first reference can only be added
   1.144 +        // on this (main) thread.
   1.145 +        delete this;
   1.146 +    }
   1.147 +}
   1.148 +
   1.149 +// Asynchronously load the database in this thread.
   1.150 +static void databaseLoaderEntry(void* threadData)
   1.151 +{
   1.152 +    PR_SetCurrentThreadName("HRTFDatabaseLdr");
   1.153 +
   1.154 +    HRTFDatabaseLoader* loader = reinterpret_cast<HRTFDatabaseLoader*>(threadData);
   1.155 +    MOZ_ASSERT(loader);
   1.156 +    loader->load();
   1.157 +}
   1.158 +
   1.159 +void HRTFDatabaseLoader::load()
   1.160 +{
   1.161 +    MOZ_ASSERT(!NS_IsMainThread());
   1.162 +    MOZ_ASSERT(!m_hrtfDatabase.get(), "Called twice");
   1.163 +    // Load the default HRTF database.
   1.164 +    m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate);
   1.165 +    // Notifies the main thread of completion.  See loadAsynchronously().
   1.166 +    Release();
   1.167 +}
   1.168 +
   1.169 +void HRTFDatabaseLoader::loadAsynchronously()
   1.170 +{
   1.171 +    MOZ_ASSERT(NS_IsMainThread());
   1.172 +    MOZ_ASSERT(m_refCnt, "Must not be called before a reference is added");
   1.173 +
   1.174 +    // Add a reference so that the destructor won't run and wait for the
   1.175 +    // loader thread, until load() has completed.
   1.176 +    AddRef();
   1.177 +
   1.178 +    MutexAutoLock locker(m_threadLock);
   1.179 +    
   1.180 +    MOZ_ASSERT(!m_hrtfDatabase.get() && !m_databaseLoaderThread,
   1.181 +               "Called twice");
   1.182 +    // Start the asynchronous database loading process.
   1.183 +    m_databaseLoaderThread =
   1.184 +        PR_CreateThread(PR_USER_THREAD, databaseLoaderEntry, this,
   1.185 +                        PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
   1.186 +                        PR_JOINABLE_THREAD, 0);
   1.187 +}
   1.188 +
   1.189 +bool HRTFDatabaseLoader::isLoaded() const
   1.190 +{
   1.191 +    return m_hrtfDatabase.get();
   1.192 +}
   1.193 +
   1.194 +void HRTFDatabaseLoader::waitForLoaderThreadCompletion()
   1.195 +{
   1.196 +    MutexAutoLock locker(m_threadLock);
   1.197 +    
   1.198 +    // waitForThreadCompletion() should not be called twice for the same thread.
   1.199 +    if (m_databaseLoaderThread) {
   1.200 +        DebugOnly<PRStatus> status = PR_JoinThread(m_databaseLoaderThread);
   1.201 +        MOZ_ASSERT(status == PR_SUCCESS, "PR_JoinThread failed");
   1.202 +    }
   1.203 +    m_databaseLoaderThread = nullptr;
   1.204 +}
   1.205 +
   1.206 +PLDHashOperator
   1.207 +HRTFDatabaseLoader::shutdownEnumFunc(LoaderByRateEntry *entry, void* unused)
   1.208 +{
   1.209 +    // Ensure the loader thread's reference is removed for leak analysis.
   1.210 +    entry->mLoader->waitForLoaderThreadCompletion();
   1.211 +    return PLDHashOperator::PL_DHASH_NEXT;
   1.212 +}
   1.213 +
   1.214 +void HRTFDatabaseLoader::shutdown()
   1.215 +{
   1.216 +    MOZ_ASSERT(NS_IsMainThread());
   1.217 +    if (s_loaderMap) {
   1.218 +        // Set s_loaderMap to nullptr so that the hashtable is not modified on
   1.219 +        // reference release during enumeration.
   1.220 +        nsTHashtable<LoaderByRateEntry>* loaderMap = s_loaderMap;
   1.221 +        s_loaderMap = nullptr;
   1.222 +        loaderMap->EnumerateEntries(shutdownEnumFunc, nullptr);
   1.223 +        delete loaderMap;
   1.224 +    }
   1.225 +}
   1.226 +} // namespace WebCore

mercurial