1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32
33 #include "HRTFDatabaseLoader.h"
34
35 #include "HRTFDatabase.h"
36
37 namespace WebCore {
38
39 // Singleton
40 HRTFDatabaseLoader* HRTFDatabaseLoader::s_loader = 0;
41
createAndLoadAsynchronouslyIfNecessary(double sampleRate)42 PassRefPtr<HRTFDatabaseLoader> HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(double sampleRate)
43 {
44 ASSERT(isMainThread());
45
46 RefPtr<HRTFDatabaseLoader> loader;
47
48 if (!s_loader) {
49 // Lazily create and load.
50 loader = adoptRef(new HRTFDatabaseLoader(sampleRate));
51 s_loader = loader.get();
52 loader->loadAsynchronously();
53 } else {
54 loader = s_loader;
55 ASSERT(sampleRate == loader->databaseSampleRate());
56 }
57
58 return loader;
59 }
60
HRTFDatabaseLoader(double sampleRate)61 HRTFDatabaseLoader::HRTFDatabaseLoader(double sampleRate)
62 : m_hrtfDatabase(0)
63 , m_databaseLoaderThread(0)
64 , m_startedLoadingDatabase(false)
65 , m_databaseSampleRate(sampleRate)
66 {
67 ASSERT(isMainThread());
68 }
69
~HRTFDatabaseLoader()70 HRTFDatabaseLoader::~HRTFDatabaseLoader()
71 {
72 ASSERT(isMainThread());
73
74 if (m_startedLoadingDatabase)
75 waitForThreadCompletion(m_databaseLoaderThread, 0);
76
77 m_startedLoadingDatabase = false;
78 m_databaseLoaderThread = 0;
79
80 m_hrtfDatabase.clear();
81
82 // Clear out singleton.
83 ASSERT(this == s_loader);
84 s_loader = 0;
85 }
86
87
88 // Asynchronously load the database in this thread.
databaseLoaderEntry(void * threadData)89 static void* databaseLoaderEntry(void* threadData)
90 {
91 HRTFDatabaseLoader* loader = reinterpret_cast<HRTFDatabaseLoader*>(threadData);
92 ASSERT(loader);
93 loader->load();
94
95 return 0;
96 }
97
load()98 void HRTFDatabaseLoader::load()
99 {
100 ASSERT(!isMainThread());
101 if (!m_hrtfDatabase.get()) {
102 // Load the default HRTF database.
103 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate);
104 }
105 }
106
loadAsynchronously()107 void HRTFDatabaseLoader::loadAsynchronously()
108 {
109 ASSERT(isMainThread());
110
111 if (!m_hrtfDatabase.get() && !m_startedLoadingDatabase) {
112 // Start the asynchronous database loading process.
113 m_startedLoadingDatabase = true;
114 m_databaseLoaderThread = createThread(databaseLoaderEntry, this, "HRTF database loader");
115 }
116 }
117
isLoaded() const118 bool HRTFDatabaseLoader::isLoaded() const
119 {
120 return m_hrtfDatabase.get();
121 }
122
123
waitForLoaderThreadCompletion()124 void HRTFDatabaseLoader::waitForLoaderThreadCompletion()
125 {
126 ASSERT(!isMainThread());
127 ASSERT(m_databaseLoaderThread);
128 waitForThreadCompletion(m_databaseLoaderThread, 0);
129 }
130
defaultHRTFDatabase()131 HRTFDatabase* HRTFDatabaseLoader::defaultHRTFDatabase()
132 {
133 if (!s_loader)
134 return 0;
135
136 return s_loader->database();
137 }
138
139 } // namespace WebCore
140
141 #endif // ENABLE(WEB_AUDIO)
142