| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 // getLoaderMap() returns the static hash map that contains the mapping between | 42 // getLoaderMap() returns the static hash map that contains the mapping between |
| 43 // the sample rate and the corresponding HRTF database. | 43 // the sample rate and the corresponding HRTF database. |
| 44 static LoaderMap& getLoaderMap() { | 44 static LoaderMap& getLoaderMap() { |
| 45 DEFINE_STATIC_LOCAL(LoaderMap*, map, (new LoaderMap)); | 45 DEFINE_STATIC_LOCAL(LoaderMap*, map, (new LoaderMap)); |
| 46 return *map; | 46 return *map; |
| 47 } | 47 } |
| 48 | 48 |
| 49 PassRefPtr<HRTFDatabaseLoader> | 49 PassRefPtr<HRTFDatabaseLoader> |
| 50 HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(float sampleRate) { | 50 HRTFDatabaseLoader::createAndLoadAsynchronouslyIfNecessary(float sampleRate) { |
| 51 ASSERT(isMainThread()); | 51 DCHECK(isMainThread()); |
| 52 | 52 |
| 53 RefPtr<HRTFDatabaseLoader> loader = getLoaderMap().at(sampleRate); | 53 RefPtr<HRTFDatabaseLoader> loader = getLoaderMap().at(sampleRate); |
| 54 if (loader) { | 54 if (loader) { |
| 55 ASSERT(sampleRate == loader->databaseSampleRate()); | 55 ASSERT(sampleRate == loader->databaseSampleRate()); |
| 56 return loader.release(); | 56 return loader.release(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 loader = adoptRef(new HRTFDatabaseLoader(sampleRate)); | 59 loader = adoptRef(new HRTFDatabaseLoader(sampleRate)); |
| 60 getLoaderMap().insert(sampleRate, loader.get()); | 60 getLoaderMap().insert(sampleRate, loader.get()); |
| 61 loader->loadAsynchronously(); | 61 loader->loadAsynchronously(); |
| 62 return loader.release(); | 62 return loader.release(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate) | 65 HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate) |
| 66 : m_databaseSampleRate(sampleRate) { | 66 : m_databaseSampleRate(sampleRate) { |
| 67 ASSERT(isMainThread()); | 67 DCHECK(isMainThread()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 HRTFDatabaseLoader::~HRTFDatabaseLoader() { | 70 HRTFDatabaseLoader::~HRTFDatabaseLoader() { |
| 71 ASSERT(isMainThread()); | 71 DCHECK(isMainThread()); |
| 72 ASSERT(!m_thread); | 72 DCHECK(!m_thread); |
| 73 getLoaderMap().erase(m_databaseSampleRate); | 73 getLoaderMap().erase(m_databaseSampleRate); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void HRTFDatabaseLoader::loadTask() { | 76 void HRTFDatabaseLoader::loadTask() { |
| 77 DCHECK(!isMainThread()); | 77 DCHECK(!isMainThread()); |
| 78 DCHECK(!m_hrtfDatabase); | 78 DCHECK(!m_hrtfDatabase); |
| 79 | 79 |
| 80 // Protect access to m_hrtfDatabase, which can be accessed from the audio | 80 // Protect access to m_hrtfDatabase, which can be accessed from the audio |
| 81 // thread. | 81 // thread. |
| 82 MutexLocker locker(m_lock); | 82 MutexLocker locker(m_lock); |
| 83 // Load the default HRTF database. | 83 // Load the default HRTF database. |
| 84 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate); | 84 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void HRTFDatabaseLoader::loadAsynchronously() { | 87 void HRTFDatabaseLoader::loadAsynchronously() { |
| 88 ASSERT(isMainThread()); | 88 DCHECK(isMainThread()); |
| 89 | 89 |
| 90 // m_hrtfDatabase and m_thread should both be unset because this should be a | 90 // m_hrtfDatabase and m_thread should both be unset because this should be a |
| 91 // new HRTFDatabaseLoader object that was just created by | 91 // new HRTFDatabaseLoader object that was just created by |
| 92 // createAndLoadAsynchronouslyIfNecessary and because we haven't started | 92 // createAndLoadAsynchronouslyIfNecessary and because we haven't started |
| 93 // loadTask yet for this object. | 93 // loadTask yet for this object. |
| 94 DCHECK(!m_hrtfDatabase); | 94 DCHECK(!m_hrtfDatabase); |
| 95 DCHECK(!m_thread); | 95 DCHECK(!m_thread); |
| 96 | 96 |
| 97 // Start the asynchronous database loading process. | 97 // Start the asynchronous database loading process. |
| 98 m_thread = WTF::wrapUnique( | 98 m_thread = WTF::wrapUnique( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 // TODO(alexclarke): Should this be posted as a loading task? | 130 // TODO(alexclarke): Should this be posted as a loading task? |
| 131 m_thread->getWebTaskRunner()->postTask( | 131 m_thread->getWebTaskRunner()->postTask( |
| 132 BLINK_FROM_HERE, crossThreadBind(&HRTFDatabaseLoader::cleanupTask, | 132 BLINK_FROM_HERE, crossThreadBind(&HRTFDatabaseLoader::cleanupTask, |
| 133 crossThreadUnretained(this), | 133 crossThreadUnretained(this), |
| 134 crossThreadUnretained(&sync))); | 134 crossThreadUnretained(&sync))); |
| 135 sync.wait(); | 135 sync.wait(); |
| 136 m_thread.reset(); | 136 m_thread.reset(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 } // namespace blink | 139 } // namespace blink |
| OLD | NEW |