blob: 5ed10b0d785f14133b06ade111fc53080c66863f [file] [log] [blame]
Andy Hunge7937b92019-08-28 21:02:23 -07001/*
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
23namespace android::soundpool {
24
25// Maximum Samples that can be background decoded before we block the caller.
26static 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.
30static constexpr int32_t kWaitTimeBeforeCloseMs = 1000;
31
32SoundDecoder::SoundDecoder(SoundManager* soundManager, size_t threads)
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}
41
42SoundDecoder::~SoundDecoder()
43{
44 ALOGV("%s()", __func__);
45 quit();
46}
47
48void SoundDecoder::quit()
49{
50 ALOGV("%s()", __func__);
51 {
52 std::lock_guard lock(mLock);
53 mQuit = true;
54 mQueueSpaceAvailable.notify_all(); // notify all load waiters
55 mQueueDataAvailable.notify_all(); // notify all worker threads
56 }
57 mThreadPool->quit();
58}
59
Andy Hung77eb2bd2020-05-19 10:42:09 -070060void SoundDecoder::run(int32_t id)
Andy Hunge7937b92019-08-28 21:02:23 -070061{
62 ALOGV("%s(%d): entering", __func__, id);
63 std::unique_lock lock(mLock);
64 while (!mQuit) {
65 if (mSoundIDs.size() == 0) {
66 ALOGV("%s(%d): waiting", __func__, id);
67 mQueueDataAvailable.wait_for(
68 lock, std::chrono::duration<int32_t, std::milli>(kWaitTimeBeforeCloseMs));
69 if (mSoundIDs.size() == 0) {
70 break; // no new sound, exit this thread.
71 }
72 continue;
73 }
74 const int32_t soundID = mSoundIDs.front();
75 mSoundIDs.pop_front();
76 mQueueSpaceAvailable.notify_one();
77 ALOGV("%s(%d): processing soundID: %d size: %zu", __func__, id, soundID, mSoundIDs.size());
78 lock.unlock();
79 std::shared_ptr<Sound> sound = mSoundManager->findSound(soundID);
80 status_t status = NO_INIT;
81 if (sound.get() != nullptr) {
82 status = sound->doLoad();
83 }
84 ALOGV("%s(%d): notifying loaded soundID:%d status:%d", __func__, id, soundID, status);
85 mSoundManager->notify(SoundPoolEvent(SoundPoolEvent::SOUND_LOADED, soundID, status));
86 lock.lock();
87 }
88 ALOGV("%s(%d): exiting", __func__, id);
89}
90
91void SoundDecoder::loadSound(int32_t soundID)
92{
93 ALOGV("%s(%d)", __func__, soundID);
94 size_t pendingSounds;
95 {
96 std::unique_lock lock(mLock);
97 while (mSoundIDs.size() == kMaxQueueSize) {
98 if (mQuit) return;
99 ALOGV("%s: waiting soundID: %d size: %zu", __func__, soundID, mSoundIDs.size());
100 mQueueSpaceAvailable.wait(lock);
101 }
102 if (mQuit) return;
103 mSoundIDs.push_back(soundID);
104 mQueueDataAvailable.notify_one();
105 ALOGV("%s: adding soundID: %d size: %zu", __func__, soundID, mSoundIDs.size());
106 pendingSounds = mSoundIDs.size();
107 }
108 // Launch threads as needed. The "as needed" is weakly consistent as we release mLock.
109 if (pendingSounds > mThreadPool->getActiveThreadCount()) {
Andy Hungce8e6da2020-06-01 09:46:03 -0700110 const int32_t id = mThreadPool->launch([this](int32_t id) { run(id); });
111 (void)id; // avoid clang warning -Wunused-variable -Wused-but-marked-unused
Andy Hunge7937b92019-08-28 21:02:23 -0700112 ALOGV_IF(id != 0, "%s: launched thread %d", __func__, id);
113 }
114}
115
116} // end namespace android::soundpool