1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "SoundPool::SoundDecoder"
19 #include "utils/Log.h"
20 
21 #include "SoundDecoder.h"
22 
23 namespace android::soundpool {
24 
25 // Maximum Samples that can be background decoded before we block the caller.
26 static constexpr size_t kMaxQueueSize = 128;
27 
28 // The amount of time we wait for a new Sound decode request
29 // before the SoundDecoder thread closes.
30 static constexpr int32_t kWaitTimeBeforeCloseMs = 1000;
31 
SoundDecoder(SoundManager * soundManager,size_t threads,int32_t threadPriority)32 SoundDecoder::SoundDecoder(SoundManager* soundManager, size_t threads, int32_t threadPriority)
33     : mSoundManager(soundManager)
34 {
35     ALOGV("%s(%p, %zu)", __func__, soundManager, threads);
36     // ThreadPool is created, but we don't launch any threads.
37     mThreadPool = std::make_unique<ThreadPool>(
38             std::min(threads, (size_t)std::thread::hardware_concurrency()),
39             "SoundDecoder_",
40             threadPriority);
41 }
42 
~SoundDecoder()43 SoundDecoder::~SoundDecoder()
44 {
45     ALOGV("%s()", __func__);
46     quit();
47 }
48 
quit()49 void SoundDecoder::quit()
50 {
51     ALOGV("%s()", __func__);
52     {
53         std::lock_guard lock(mLock);
54         mQuit = true;
55         mQueueSpaceAvailable.notify_all(); // notify all load waiters
56         mQueueDataAvailable.notify_all();  // notify all worker threads
57     }
58     mThreadPool->quit();
59 }
60 
run(int32_t id)61 void SoundDecoder::run(int32_t id)
62 {
63     ALOGV("%s(%d): entering", __func__, id);
64     std::unique_lock lock(mLock);
65     while (!mQuit) {
66         if (mSoundIDs.size() == 0) {
67             ALOGV("%s(%d): waiting", __func__, id);
68             mQueueDataAvailable.wait_for(
69                     lock, std::chrono::duration<int32_t, std::milli>(kWaitTimeBeforeCloseMs));
70             if (mSoundIDs.size() == 0) {
71                 break; // no new sound, exit this thread.
72             }
73             continue;
74         }
75         const int32_t soundID = mSoundIDs.front();
76         mSoundIDs.pop_front();
77         mQueueSpaceAvailable.notify_one();
78         ALOGV("%s(%d): processing soundID: %d  size: %zu", __func__, id, soundID, mSoundIDs.size());
79         lock.unlock();
80         std::shared_ptr<Sound> sound = mSoundManager->findSound(soundID);
81         status_t status = NO_INIT;
82         if (sound.get() != nullptr) {
83             status = sound->doLoad();
84         }
85         ALOGV("%s(%d): notifying loaded soundID:%d  status:%d", __func__, id, soundID, status);
86         mSoundManager->notify(SoundPoolEvent(SoundPoolEvent::SOUND_LOADED, soundID, status));
87         lock.lock();
88     }
89     ALOGV("%s(%d): exiting", __func__, id);
90 }
91 
loadSound(int32_t soundID)92 void SoundDecoder::loadSound(int32_t soundID)
93 {
94     ALOGV("%s(%d)", __func__, soundID);
95     size_t pendingSounds;
96     {
97         std::unique_lock lock(mLock);
98         while (mSoundIDs.size() == kMaxQueueSize) {
99             if (mQuit) return;
100             ALOGV("%s: waiting soundID: %d size: %zu", __func__, soundID, mSoundIDs.size());
101             mQueueSpaceAvailable.wait(lock);
102         }
103         if (mQuit) return;
104         mSoundIDs.push_back(soundID);
105         mQueueDataAvailable.notify_one();
106         ALOGV("%s: adding soundID: %d  size: %zu", __func__, soundID, mSoundIDs.size());
107         pendingSounds = mSoundIDs.size();
108     }
109     // Launch threads as needed.  The "as needed" is weakly consistent as we release mLock.
110     if (pendingSounds > mThreadPool->getActiveThreadCount()) {
111         const int32_t id = mThreadPool->launch([this](int32_t id) { run(id); });
112         (void)id; // avoid clang warning -Wunused-variable -Wused-but-marked-unused
113         ALOGV_IF(id != 0, "%s: launched thread %d", __func__, id);
114     }
115 }
116 
117 } // end namespace android::soundpool
118