@@ -179,6 +179,17 @@ bool SoundPool::startThreads()
179
179
return mDecodeThread != NULL ;
180
180
}
181
181
182
+ sp<Sample> SoundPool::findSample (int sampleID)
183
+ {
184
+ Mutex::Autolock lock (&mLock );
185
+ return findSample_l (sampleID);
186
+ }
187
+
188
+ sp<Sample> SoundPool::findSample_l (int sampleID)
189
+ {
190
+ return mSamples .valueFor (sampleID);
191
+ }
192
+
182
193
SoundChannel* SoundPool::findChannel (int channelID)
183
194
{
184
195
for (int i = 0 ; i < mMaxChannels ; ++i) {
@@ -202,29 +213,42 @@ SoundChannel* SoundPool::findNextChannel(int channelID)
202
213
int SoundPool::load (const char * path, int priority)
203
214
{
204
215
ALOGV (" load: path=%s, priority=%d" , path, priority);
205
- Mutex::Autolock lock (&mLock );
206
- sp<Sample> sample = new Sample (++mNextSampleID , path);
207
- mSamples .add (sample->sampleID (), sample);
208
- doLoad (sample);
209
- return sample->sampleID ();
216
+ int sampleID;
217
+ {
218
+ Mutex::Autolock lock (&mLock );
219
+ sampleID = ++mNextSampleID ;
220
+ sp<Sample> sample = new Sample (sampleID, path);
221
+ mSamples .add (sampleID, sample);
222
+ sample->startLoad ();
223
+ }
224
+ // mDecodeThread->loadSample() must be called outside of mLock.
225
+ // mDecodeThread->loadSample() may block on mDecodeThread message queue space;
226
+ // the message queue emptying may block on SoundPool::findSample().
227
+ //
228
+ // It theoretically possible that sample loads might decode out-of-order.
229
+ mDecodeThread ->loadSample (sampleID);
230
+ return sampleID;
210
231
}
211
232
212
233
int SoundPool::load (int fd, int64_t offset, int64_t length, int priority)
213
234
{
214
235
ALOGV (" load: fd=%d, offset=%lld, length=%lld, priority=%d" ,
215
236
fd, offset, length, priority);
216
- Mutex::Autolock lock (&mLock );
217
- sp<Sample> sample = new Sample (++mNextSampleID , fd, offset, length);
218
- mSamples .add (sample->sampleID (), sample);
219
- doLoad (sample);
220
- return sample->sampleID ();
221
- }
222
-
223
- void SoundPool::doLoad (sp<Sample>& sample)
224
- {
225
- ALOGV (" doLoad: loading sample sampleID=%d" , sample->sampleID ());
226
- sample->startLoad ();
227
- mDecodeThread ->loadSample (sample->sampleID ());
237
+ int sampleID;
238
+ {
239
+ Mutex::Autolock lock (&mLock );
240
+ sampleID = ++mNextSampleID ;
241
+ sp<Sample> sample = new Sample (sampleID, fd, offset, length);
242
+ mSamples .add (sampleID, sample);
243
+ sample->startLoad ();
244
+ }
245
+ // mDecodeThread->loadSample() must be called outside of mLock.
246
+ // mDecodeThread->loadSample() may block on mDecodeThread message queue space;
247
+ // the message queue emptying may block on SoundPool::findSample().
248
+ //
249
+ // It theoretically possible that sample loads might decode out-of-order.
250
+ mDecodeThread ->loadSample (sampleID);
251
+ return sampleID;
228
252
}
229
253
230
254
bool SoundPool::unload (int sampleID)
@@ -239,7 +263,6 @@ int SoundPool::play(int sampleID, float leftVolume, float rightVolume,
239
263
{
240
264
ALOGV (" play sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f" ,
241
265
sampleID, leftVolume, rightVolume, priority, loop, rate);
242
- sp<Sample> sample;
243
266
SoundChannel* channel;
244
267
int channelID;
245
268
@@ -249,7 +272,7 @@ int SoundPool::play(int sampleID, float leftVolume, float rightVolume,
249
272
return 0 ;
250
273
}
251
274
// is sample ready?
252
- sample = findSample ( sampleID);
275
+ sp<Sample> sample ( findSample_l ( sampleID) );
253
276
if ((sample == 0 ) || (sample->state () != Sample::READY)) {
254
277
ALOGW (" sample %d not READY" , sampleID);
255
278
return 0 ;
0 commit comments