• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::StreamManager"
19 #include <utils/Log.h>
20 
21 #include "StreamManager.h"
22 
23 #include <audio_utils/clock.h>
24 #include <audio_utils/roundup.h>
25 
26 namespace android::soundpool {
27 
28 // kMaxStreams is number that should be less than the current AudioTrack max per UID of 40.
29 // It is the maximum number of AudioTrack resources allowed in the SoundPool.
30 // We suggest a value at least 4 or greater to allow CTS tests to pass.
31 static constexpr int32_t kMaxStreams = 32;
32 
33 // kStealActiveStream_OldestFirst = false historically (Q and earlier)
34 // Changing to true could break app expectations but could change behavior beneficially.
35 // In R, we change this to true, as it is the correct way per SoundPool documentation.
36 static constexpr bool kStealActiveStream_OldestFirst = true;
37 
38 // kPlayOnCallingThread = true prior to R.
39 // Changing to false means calls to play() are almost instantaneous instead of taking around
40 // ~10ms to launch the AudioTrack. It is perhaps 100x faster.
41 static constexpr bool kPlayOnCallingThread = true;
42 
43 // Amount of time for a StreamManager thread to wait before closing.
44 static constexpr int64_t kWaitTimeBeforeCloseNs = 9 * NANOS_PER_SECOND;
45 
46 // Debug flag:
47 // kForceLockStreamManagerStop is set to true to force lock the StreamManager
48 // worker thread during stop. This limits concurrency of Stream processing.
49 // Normally we lock the StreamManager worker thread during stop ONLY
50 // for SoundPools configured with a single Stream.
51 //
52 static constexpr bool kForceLockStreamManagerStop = false;
53 
54 ////////////
55 
StreamMap(int32_t streams)56 StreamMap::StreamMap(int32_t streams) {
57     ALOGV("%s(%d)", __func__, streams);
58     if (streams > kMaxStreams) {
59         ALOGW("%s: requested %d streams, clamping to %d", __func__, streams, kMaxStreams);
60         streams = kMaxStreams;
61     } else if (streams < 1) {
62         ALOGW("%s: requested %d streams, clamping to 1", __func__, streams);
63         streams = 1;
64     }
65     mStreamPoolSize = streams * 2;
66     mStreamPool = std::make_unique<Stream[]>(mStreamPoolSize); // create array of streams.
67     // we use a perfect hash table with 2x size to map StreamIDs to Stream pointers.
68     mPerfectHash = std::make_unique<PerfectHash<int32_t, Stream *>>(roundup(mStreamPoolSize * 2));
69 }
70 
findStream(int32_t streamID) const71 Stream* StreamMap::findStream(int32_t streamID) const
72 {
73     Stream *stream = lookupStreamFromId(streamID);
74     return stream != nullptr && stream->getStreamID() == streamID ? stream : nullptr;
75 }
76 
streamPosition(const Stream * stream) const77 size_t StreamMap::streamPosition(const Stream* stream) const
78 {
79     ptrdiff_t index = stream - mStreamPool.get();
80     LOG_ALWAYS_FATAL_IF(index < 0 || (size_t)index >= mStreamPoolSize,
81             "%s: stream position out of range: %td", __func__, index);
82     return (size_t)index;
83 }
84 
lookupStreamFromId(int32_t streamID) const85 Stream* StreamMap::lookupStreamFromId(int32_t streamID) const
86 {
87     return streamID > 0 ? mPerfectHash->getValue(streamID).load() : nullptr;
88 }
89 
getNextIdForStream(Stream * stream) const90 int32_t StreamMap::getNextIdForStream(Stream* stream) const {
91     // even though it is const, it mutates the internal hash table.
92     const int32_t id = mPerfectHash->generateKey(
93         stream,
94         [] (Stream *stream) {
95             return stream == nullptr ? 0 : stream->getStreamID();
96         }, /* getKforV() */
97         stream->getStreamID() /* oldID */);
98     return id;
99 }
100 
101 ////////////
102 
103 // Thread safety analysis is supposed to be disabled for constructors and destructors
104 // but clang in R seems to have a bug.  We use pragma to disable.
105 #pragma clang diagnostic push
106 #pragma clang diagnostic ignored "-Wthread-safety-analysis"
107 
StreamManager(int32_t streams,size_t threads,const audio_attributes_t * attributes,std::string opPackageName)108 StreamManager::StreamManager(
109         int32_t streams, size_t threads, const audio_attributes_t* attributes,
110         std::string opPackageName)
111     : StreamMap(streams)
112     , mAttributes(*attributes)
113     , mOpPackageName(std::move(opPackageName))
114     , mLockStreamManagerStop(streams == 1 || kForceLockStreamManagerStop)
115 {
116     ALOGV("%s(%d, %zu, ...)", __func__, streams, threads);
117     forEach([this](Stream *stream) {
118         stream->setStreamManager(this);
119         if ((streamPosition(stream) & 1) == 0) { // put the first stream of pair as available.
120             mAvailableStreams.insert(stream);
121         }
122     });
123 
124     mThreadPool = std::make_unique<ThreadPool>(
125             std::min((size_t)streams,  // do not make more threads than streams to play
126                     std::min(threads, (size_t)std::thread::hardware_concurrency())),
127             "SoundPool_");
128 }
129 
130 #pragma clang diagnostic pop
131 
~StreamManager()132 StreamManager::~StreamManager()
133 {
134     ALOGV("%s", __func__);
135     {
136         std::unique_lock lock(mStreamManagerLock);
137         mQuit = true;
138         mStreamManagerCondition.notify_all();
139     }
140     mThreadPool->quit();
141 
142     // call stop on the stream pool
143     forEach([](Stream *stream) { stream->stop(); });
144 
145     // This invokes the destructor on the AudioTracks -
146     // we do it here to ensure that AudioTrack callbacks will not occur
147     // afterwards.
148     forEach([](Stream *stream) { stream->clearAudioTrack(); });
149 }
150 
151 
queueForPlay(const std::shared_ptr<Sound> & sound,int32_t soundID,float leftVolume,float rightVolume,int32_t priority,int32_t loop,float rate)152 int32_t StreamManager::queueForPlay(const std::shared_ptr<Sound> &sound,
153         int32_t soundID, float leftVolume, float rightVolume,
154         int32_t priority, int32_t loop, float rate)
155 {
156     ALOGV("%s(sound=%p, soundID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f)",
157             __func__, sound.get(), soundID, leftVolume, rightVolume, priority, loop, rate);
158     bool launchThread = false;
159     int32_t streamID = 0;
160 
161     { // for lock
162         std::unique_lock lock(mStreamManagerLock);
163         Stream *newStream = nullptr;
164         bool fromAvailableQueue = false;
165         ALOGV("%s: mStreamManagerLock lock acquired", __func__);
166 
167         sanityCheckQueue_l();
168         // find an available stream, prefer one that has matching sound id.
169         if (mAvailableStreams.size() > 0) {
170             for (auto stream : mAvailableStreams) {
171                 if (stream->getSoundID() == soundID) {
172                     newStream = stream;
173                     ALOGV("%s: found soundID %d in available queue", __func__, soundID);
174                     break;
175                 }
176             }
177             if (newStream == nullptr) {
178                 ALOGV("%s: found stream in available queue", __func__);
179                 newStream = *mAvailableStreams.begin();
180             }
181             newStream->setStopTimeNs(systemTime());
182             fromAvailableQueue = true;
183         }
184 
185         // also look in the streams restarting (if the paired stream doesn't have a pending play)
186         if (newStream == nullptr || newStream->getSoundID() != soundID) {
187             for (auto [unused , stream] : mRestartStreams) {
188                 if (!stream->getPairStream()->hasSound()) {
189                     if (stream->getSoundID() == soundID) {
190                         ALOGV("%s: found soundID %d in restart queue", __func__, soundID);
191                         newStream = stream;
192                         fromAvailableQueue = false;
193                         break;
194                     } else if (newStream == nullptr) {
195                         ALOGV("%s: found stream in restart queue", __func__);
196                         newStream = stream;
197                     }
198                 }
199             }
200         }
201 
202         // no available streams, look for one to steal from the active list
203         if (newStream == nullptr) {
204             for (auto stream : mActiveStreams) {
205                 if (stream->getPriority() <= priority) {
206                     if (newStream == nullptr
207                             || newStream->getPriority() > stream->getPriority()) {
208                         newStream = stream;
209                         ALOGV("%s: found stream in active queue", __func__);
210                     }
211                 }
212             }
213             if (newStream != nullptr) { // we need to mute as it is still playing.
214                 (void)newStream->requestStop(newStream->getStreamID());
215             }
216         }
217 
218         // none found, look for a stream that is restarting, evict one.
219         if (newStream == nullptr) {
220             for (auto [unused, stream] : mRestartStreams) {
221                 if (stream->getPairPriority() <= priority) {
222                     ALOGV("%s: evict stream from restart queue", __func__);
223                     newStream = stream;
224                     break;
225                 }
226             }
227         }
228 
229         // DO NOT LOOK into mProcessingStreams as those are held by the StreamManager threads.
230 
231         if (newStream == nullptr) {
232             ALOGD("%s: unable to find stream, returning 0", __func__);
233             return 0; // unable to find available stream
234         }
235 
236         Stream *pairStream = newStream->getPairStream();
237         streamID = getNextIdForStream(pairStream);
238         ALOGV("%s: newStream:%p  pairStream:%p, streamID:%d",
239                 __func__, newStream, pairStream, streamID);
240         pairStream->setPlay(
241                 streamID, sound, soundID, leftVolume, rightVolume, priority, loop, rate);
242         if (fromAvailableQueue && kPlayOnCallingThread) {
243             removeFromQueues_l(newStream);
244             mProcessingStreams.emplace(newStream);
245             lock.unlock();
246             if (Stream* nextStream = newStream->playPairStream()) {
247                 lock.lock();
248                 ALOGV("%s: starting streamID:%d", __func__, nextStream->getStreamID());
249                 addToActiveQueue_l(nextStream);
250             } else {
251                 lock.lock();
252                 mAvailableStreams.insert(newStream);
253                 streamID = 0;
254             }
255             mProcessingStreams.erase(newStream);
256         } else {
257             launchThread = moveToRestartQueue_l(newStream) && needMoreThreads_l();
258         }
259         sanityCheckQueue_l();
260         ALOGV("%s: mStreamManagerLock released", __func__);
261     } // lock
262 
263     if (launchThread) {
264         const int32_t id = mThreadPool->launch([this](int32_t id) { run(id); });
265         (void)id; // avoid clang warning -Wunused-variable -Wused-but-marked-unused
266         ALOGV_IF(id != 0, "%s: launched thread %d", __func__, id);
267     }
268     ALOGV("%s: returning %d", __func__, streamID);
269     return streamID;
270 }
271 
moveToRestartQueue(Stream * stream,int32_t activeStreamIDToMatch)272 void StreamManager::moveToRestartQueue(
273         Stream* stream, int32_t activeStreamIDToMatch)
274 {
275     ALOGV("%s(stream(ID)=%d, activeStreamIDToMatch=%d)",
276             __func__, stream->getStreamID(), activeStreamIDToMatch);
277     bool restart;
278     {
279         std::lock_guard lock(mStreamManagerLock);
280         sanityCheckQueue_l();
281         if (mProcessingStreams.count(stream) > 0 ||
282                 mProcessingStreams.count(stream->getPairStream()) > 0) {
283             ALOGD("%s: attempting to restart processing stream(%d)",
284                     __func__, stream->getStreamID());
285             restart = false;
286         } else {
287             moveToRestartQueue_l(stream, activeStreamIDToMatch);
288             restart = needMoreThreads_l();
289         }
290         sanityCheckQueue_l();
291     }
292     if (restart) {
293         const int32_t id = mThreadPool->launch([this](int32_t id) { run(id); });
294         (void)id; // avoid clang warning -Wunused-variable -Wused-but-marked-unused
295         ALOGV_IF(id != 0, "%s: launched thread %d", __func__, id);
296     }
297 }
298 
moveToRestartQueue_l(Stream * stream,int32_t activeStreamIDToMatch)299 bool StreamManager::moveToRestartQueue_l(
300         Stream* stream, int32_t activeStreamIDToMatch)
301 {
302     ALOGV("%s(stream(ID)=%d, activeStreamIDToMatch=%d)",
303             __func__, stream->getStreamID(), activeStreamIDToMatch);
304     if (activeStreamIDToMatch > 0 && stream->getStreamID() != activeStreamIDToMatch) {
305         return false;
306     }
307     const ssize_t found = removeFromQueues_l(stream, activeStreamIDToMatch);
308     if (found < 0) return false;
309 
310     LOG_ALWAYS_FATAL_IF(found > 1, "stream on %zd > 1 stream lists", found);
311 
312     addToRestartQueue_l(stream);
313     mStreamManagerCondition.notify_one();
314     return true;
315 }
316 
removeFromQueues_l(Stream * stream,int32_t activeStreamIDToMatch)317 ssize_t StreamManager::removeFromQueues_l(
318         Stream* stream, int32_t activeStreamIDToMatch) {
319     size_t found = 0;
320     for (auto it = mActiveStreams.begin(); it != mActiveStreams.end(); ++it) {
321         if (*it == stream) {
322             mActiveStreams.erase(it); // we erase the iterator and break (otherwise it not safe).
323             ++found;
324             break;
325         }
326     }
327     // activeStreamIDToMatch is nonzero indicates we proceed only if found.
328     if (found == 0 && activeStreamIDToMatch > 0) {
329         return -1;  // special code: not present on active streams, ignore restart request
330     }
331 
332     for (auto it = mRestartStreams.begin(); it != mRestartStreams.end(); ++it) {
333         if (it->second == stream) {
334             mRestartStreams.erase(it);
335             ++found;
336             break;
337         }
338     }
339     found += mAvailableStreams.erase(stream);
340 
341     // streams on mProcessingStreams are undergoing processing by the StreamManager thread
342     // and do not participate in normal stream migration.
343     return (ssize_t)found;
344 }
345 
addToRestartQueue_l(Stream * stream)346 void StreamManager::addToRestartQueue_l(Stream *stream) {
347     mRestartStreams.emplace(stream->getStopTimeNs(), stream);
348 }
349 
addToActiveQueue_l(Stream * stream)350 void StreamManager::addToActiveQueue_l(Stream *stream) {
351     if (kStealActiveStream_OldestFirst) {
352         mActiveStreams.push_back(stream);  // oldest to newest
353     } else {
354         mActiveStreams.push_front(stream); // newest to oldest
355     }
356 }
357 
run(int32_t id)358 void StreamManager::run(int32_t id)
359 {
360     ALOGV("%s(%d) entering", __func__, id);
361     int64_t waitTimeNs = 0;  // on thread start, mRestartStreams can be non-empty.
362     std::unique_lock lock(mStreamManagerLock);
363     while (!mQuit) {
364         if (waitTimeNs > 0) {
365             mStreamManagerCondition.wait_for(
366                     lock, std::chrono::duration<int64_t, std::nano>(waitTimeNs));
367         }
368         ALOGV("%s(%d) awake lock waitTimeNs:%lld", __func__, id, (long long)waitTimeNs);
369 
370         sanityCheckQueue_l();
371 
372         if (mQuit || (mRestartStreams.empty() && waitTimeNs == kWaitTimeBeforeCloseNs)) {
373             break;  // end the thread
374         }
375 
376         waitTimeNs = kWaitTimeBeforeCloseNs;
377         while (!mQuit && !mRestartStreams.empty()) {
378             const nsecs_t nowNs = systemTime();
379             auto it = mRestartStreams.begin();
380             Stream* const stream = it->second;
381             const int64_t diffNs = stream->getStopTimeNs() - nowNs;
382             if (diffNs > 0) {
383                 waitTimeNs = std::min(waitTimeNs, diffNs);
384                 break;
385             }
386             mRestartStreams.erase(it);
387             mProcessingStreams.emplace(stream);
388             if (!mLockStreamManagerStop) lock.unlock();
389             stream->stop();
390             ALOGV("%s(%d) stopping streamID:%d", __func__, id, stream->getStreamID());
391             if (Stream* nextStream = stream->playPairStream()) {
392                 ALOGV("%s(%d) starting streamID:%d", __func__, id, nextStream->getStreamID());
393                 if (!mLockStreamManagerStop) lock.lock();
394                 if (nextStream->getStopTimeNs() > 0) {
395                     // the next stream was stopped before we can move it to the active queue.
396                     ALOGV("%s(%d) stopping started streamID:%d",
397                             __func__, id, nextStream->getStreamID());
398                     moveToRestartQueue_l(nextStream);
399                 } else {
400                     addToActiveQueue_l(nextStream);
401                 }
402             } else {
403                 if (!mLockStreamManagerStop) lock.lock();
404                 mAvailableStreams.insert(stream);
405             }
406             mProcessingStreams.erase(stream);
407             sanityCheckQueue_l();
408         }
409     }
410     ALOGV("%s(%d) exiting", __func__, id);
411 }
412 
dump() const413 void StreamManager::dump() const
414 {
415     forEach([](const Stream *stream) { stream->dump(); });
416 }
417 
sanityCheckQueue_l() const418 void StreamManager::sanityCheckQueue_l() const
419 {
420     // We want to preserve the invariant that each stream pair is exactly on one of the queues.
421     const size_t availableStreams = mAvailableStreams.size();
422     const size_t restartStreams = mRestartStreams.size();
423     const size_t activeStreams = mActiveStreams.size();
424     const size_t processingStreams = mProcessingStreams.size();
425     const size_t managedStreams = availableStreams + restartStreams + activeStreams
426                 + processingStreams;
427     const size_t totalStreams = getStreamMapSize() >> 1;
428     LOG_ALWAYS_FATAL_IF(managedStreams != totalStreams,
429             "%s: mAvailableStreams:%zu + mRestartStreams:%zu + "
430             "mActiveStreams:%zu + mProcessingStreams:%zu = %zu != total streams %zu",
431             __func__, availableStreams, restartStreams, activeStreams, processingStreams,
432             managedStreams, totalStreams);
433     ALOGV("%s: mAvailableStreams:%zu + mRestartStreams:%zu + "
434             "mActiveStreams:%zu + mProcessingStreams:%zu = %zu (total streams: %zu)",
435             __func__, availableStreams, restartStreams, activeStreams, processingStreams,
436             managedStreams, totalStreams);
437 }
438 
439 } // namespace android::soundpool
440