content/media/webaudio/blink/HRTFDatabaseLoader.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/webaudio/blink/HRTFDatabaseLoader.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     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 +#ifndef HRTFDatabaseLoader_h
    1.33 +#define HRTFDatabaseLoader_h
    1.34 +
    1.35 +#include "nsHashKeys.h"
    1.36 +#include "mozilla/RefPtr.h"
    1.37 +#include "mozilla/MemoryReporting.h"
    1.38 +#include "mozilla/Mutex.h"
    1.39 +#include "HRTFDatabase.h"
    1.40 +
    1.41 +template <class EntryType> class nsTHashtable;
    1.42 +template <class T> class nsAutoRef;
    1.43 +
    1.44 +namespace WebCore {
    1.45 +
    1.46 +// HRTFDatabaseLoader will asynchronously load the default HRTFDatabase in a new thread.
    1.47 +
    1.48 +class HRTFDatabaseLoader {
    1.49 +public:
    1.50 +    // Lazily creates a HRTFDatabaseLoader (if not already created) for the given sample-rate
    1.51 +    // and starts loading asynchronously (when created the first time).
    1.52 +    // Returns the HRTFDatabaseLoader.
    1.53 +    // Must be called from the main thread.
    1.54 +    static mozilla::TemporaryRef<HRTFDatabaseLoader> createAndLoadAsynchronouslyIfNecessary(float sampleRate);
    1.55 +
    1.56 +    // AddRef and Release may be called from any thread.
    1.57 +    void AddRef()
    1.58 +    {
    1.59 +#if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
    1.60 +        int count =
    1.61 +#endif
    1.62 +          ++m_refCnt;
    1.63 +        MOZ_ASSERT(count > 0, "invalid ref count");
    1.64 +        NS_LOG_ADDREF(this, count, "HRTFDatabaseLoader", sizeof(*this));
    1.65 +    }
    1.66 +
    1.67 +    void Release()
    1.68 +    {
    1.69 +        // The last reference can't be removed on a non-main thread because
    1.70 +        // the object can be accessed on the main thread from the hash
    1.71 +        // table via createAndLoadAsynchronouslyIfNecessary().
    1.72 +        int count = m_refCnt;
    1.73 +        MOZ_ASSERT(count > 0, "extra release");
    1.74 +        // Optimization attempt to possibly skip proxying the release to the
    1.75 +        // main thread.
    1.76 +        if (count != 1 && m_refCnt.compareExchange(count, count - 1)) {
    1.77 +            NS_LOG_RELEASE(this, count - 1, "HRTFDatabaseLoader");
    1.78 +            return;
    1.79 +        }
    1.80 +
    1.81 +        ProxyRelease();
    1.82 +    }
    1.83 +
    1.84 +    // Returns true once the default database has been completely loaded.
    1.85 +    bool isLoaded() const;
    1.86 +
    1.87 +    // waitForLoaderThreadCompletion() may be called more than once,
    1.88 +    // on any thread except m_databaseLoaderThread.
    1.89 +    void waitForLoaderThreadCompletion();
    1.90 +    
    1.91 +    HRTFDatabase* database() { return m_hrtfDatabase.get(); }
    1.92 +
    1.93 +    float databaseSampleRate() const { return m_databaseSampleRate; }
    1.94 +
    1.95 +    static void shutdown();
    1.96 +    
    1.97 +    // Called in asynchronous loading thread.
    1.98 +    void load();
    1.99 +
   1.100 +    size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
   1.101 +
   1.102 +private:
   1.103 +    // Both constructor and destructor must be called from the main thread.
   1.104 +    explicit HRTFDatabaseLoader(float sampleRate);
   1.105 +    ~HRTFDatabaseLoader();
   1.106 +    
   1.107 +    void ProxyRelease(); // any thread
   1.108 +    void MainThreadRelease(); // main thread only
   1.109 +    class ProxyReleaseEvent;
   1.110 +
   1.111 +    // If it hasn't already been loaded, creates a new thread and initiates asynchronous loading of the default database.
   1.112 +    // This must be called from the main thread.
   1.113 +    void loadAsynchronously();
   1.114 +
   1.115 +    // Map from sample-rate to loader.
   1.116 +    class LoaderByRateEntry : public nsFloatHashKey {
   1.117 +    public:
   1.118 +        LoaderByRateEntry(KeyTypePointer aKey)
   1.119 +            : nsFloatHashKey(aKey)
   1.120 +            , mLoader() // so PutEntry() will zero-initialize
   1.121 +        {
   1.122 +        }
   1.123 +        HRTFDatabaseLoader* mLoader;
   1.124 +    };
   1.125 +
   1.126 +    static PLDHashOperator shutdownEnumFunc(LoaderByRateEntry *entry,
   1.127 +                                            void* unused);
   1.128 +
   1.129 +    // Keeps track of loaders on a per-sample-rate basis.
   1.130 +    static nsTHashtable<LoaderByRateEntry> *s_loaderMap; // singleton
   1.131 +
   1.132 +    mozilla::Atomic<int> m_refCnt;
   1.133 +
   1.134 +    nsAutoRef<HRTFDatabase> m_hrtfDatabase;
   1.135 +
   1.136 +    // Holding a m_threadLock is required when accessing m_databaseLoaderThread.
   1.137 +    mozilla::Mutex m_threadLock;
   1.138 +    PRThread* m_databaseLoaderThread;
   1.139 +
   1.140 +    float m_databaseSampleRate;
   1.141 +};
   1.142 +
   1.143 +} // namespace WebCore
   1.144 +
   1.145 +#endif // HRTFDatabaseLoader_h

mercurial