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 22 matching lines...) Expand all Loading... |
33 #include "public/platform/WebThread.h" | 33 #include "public/platform/WebThread.h" |
34 #include "wtf/HashMap.h" | 34 #include "wtf/HashMap.h" |
35 #include "wtf/RefCounted.h" | 35 #include "wtf/RefCounted.h" |
36 #include "wtf/ThreadingPrimitives.h" | 36 #include "wtf/ThreadingPrimitives.h" |
37 #include <memory> | 37 #include <memory> |
38 | 38 |
39 namespace blink { | 39 namespace blink { |
40 | 40 |
41 class WaitableEvent; | 41 class WaitableEvent; |
42 | 42 |
43 // HRTFDatabaseLoader will asynchronously load the default HRTFDatabase in a new
thread. | 43 // HRTFDatabaseLoader will asynchronously load the default HRTFDatabase in a new |
| 44 // thread. |
44 class PLATFORM_EXPORT HRTFDatabaseLoader final | 45 class PLATFORM_EXPORT HRTFDatabaseLoader final |
45 : public RefCounted<HRTFDatabaseLoader> { | 46 : public RefCounted<HRTFDatabaseLoader> { |
46 public: | 47 public: |
47 // Lazily creates a HRTFDatabaseLoader (if not already created) for the given
sample-rate | 48 // Lazily creates a HRTFDatabaseLoader (if not already created) for the given |
48 // and starts loading asynchronously (when created the first time). | 49 // sample-rate and starts loading asynchronously (when created the first |
| 50 // time). |
49 // Returns the HRTFDatabaseLoader. | 51 // Returns the HRTFDatabaseLoader. |
50 // Must be called from the main thread. | 52 // Must be called from the main thread. |
51 static PassRefPtr<HRTFDatabaseLoader> createAndLoadAsynchronouslyIfNecessary( | 53 static PassRefPtr<HRTFDatabaseLoader> createAndLoadAsynchronouslyIfNecessary( |
52 float sampleRate); | 54 float sampleRate); |
53 | 55 |
54 // Both constructor and destructor must be called from the main thread. | 56 // Both constructor and destructor must be called from the main thread. |
55 ~HRTFDatabaseLoader(); | 57 ~HRTFDatabaseLoader(); |
56 | 58 |
57 // Returns true once the default database has been completely loaded. This | 59 // Returns true once the default database has been completely loaded. This |
58 // must be called from the audio thread. | 60 // must be called from the audio thread. |
59 bool isLoaded() { return database(); } | 61 bool isLoaded() { return database(); } |
60 | 62 |
61 // waitForLoaderThreadCompletion() may be called more than once and is thread-
safe. | 63 // waitForLoaderThreadCompletion() may be called more than once and is |
| 64 // thread-safe. |
62 void waitForLoaderThreadCompletion(); | 65 void waitForLoaderThreadCompletion(); |
63 | 66 |
64 // Returns the database or nullptr if the database doesn't yet exist. Must | 67 // Returns the database or nullptr if the database doesn't yet exist. Must |
65 // be called from the audio thread. | 68 // be called from the audio thread. |
66 HRTFDatabase* database(); | 69 HRTFDatabase* database(); |
67 | 70 |
68 float databaseSampleRate() const { return m_databaseSampleRate; } | 71 float databaseSampleRate() const { return m_databaseSampleRate; } |
69 | 72 |
70 private: | 73 private: |
71 // Both constructor and destructor must be called from the main thread. | 74 // Both constructor and destructor must be called from the main thread. |
72 explicit HRTFDatabaseLoader(float sampleRate); | 75 explicit HRTFDatabaseLoader(float sampleRate); |
73 | 76 |
74 // If it hasn't already been loaded, creates a new thread and initiates asynch
ronous loading of the default database. | 77 // If it hasn't already been loaded, creates a new thread and initiates |
| 78 // asynchronous loading of the default database. |
75 // This must be called from the main thread. | 79 // This must be called from the main thread. |
76 void loadAsynchronously(); | 80 void loadAsynchronously(); |
77 | 81 |
78 // Called in asynchronous loading thread. | 82 // Called in asynchronous loading thread. |
79 void loadTask(); | 83 void loadTask(); |
80 void cleanupTask(WaitableEvent*); | 84 void cleanupTask(WaitableEvent*); |
81 | 85 |
82 // Holding a m_lock is required when accessing m_hrtfDatabase since we access
it from multiple threads. | 86 // Holding a m_lock is required when accessing m_hrtfDatabase since we access |
| 87 // it from multiple threads. |
83 Mutex m_lock; | 88 Mutex m_lock; |
84 std::unique_ptr<HRTFDatabase> m_hrtfDatabase; | 89 std::unique_ptr<HRTFDatabase> m_hrtfDatabase; |
85 | 90 |
86 std::unique_ptr<WebThread> m_thread; | 91 std::unique_ptr<WebThread> m_thread; |
87 | 92 |
88 float m_databaseSampleRate; | 93 float m_databaseSampleRate; |
89 }; | 94 }; |
90 | 95 |
91 } // namespace blink | 96 } // namespace blink |
92 | 97 |
93 #endif // HRTFDatabaseLoader_h | 98 #endif // HRTFDatabaseLoader_h |
OLD | NEW |