Loading media/jni/soundpool/SoundPool.cpp +50 −33 Original line number Diff line number Diff line Loading @@ -256,7 +256,7 @@ int SoundPool::play(int sampleID, float leftVolume, float rightVolume, dump(); // allocate a channel channel = allocateChannel_l(priority); channel = allocateChannel_l(priority, sampleID); // no channel allocated - return 0 if (!channel) { Loading @@ -271,13 +271,25 @@ int SoundPool::play(int sampleID, float leftVolume, float rightVolume, return channelID; } SoundChannel* SoundPool::allocateChannel_l(int priority) SoundChannel* SoundPool::allocateChannel_l(int priority, int sampleID) { List<SoundChannel*>::iterator iter; SoundChannel* channel = NULL; // allocate a channel // check if channel for given sampleID still available if (!mChannels.empty()) { for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) { if (sampleID == (*iter)->getPrevSampleID() && (*iter)->state() == SoundChannel::IDLE) { channel = *iter; mChannels.erase(iter); ALOGV("Allocated recycled channel for same sampleID"); break; } } } // allocate any channel if (!channel && !mChannels.empty()) { iter = mChannels.begin(); if (priority >= (*iter)->priority()) { channel = *iter; Loading Loading @@ -648,6 +660,7 @@ error: void SoundChannel::init(SoundPool* soundPool) { mSoundPool = soundPool; mPrevSampleID = -1; } // call with sound pool lock held Loading @@ -656,7 +669,7 @@ void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftV { sp<AudioTrack> oldTrack; sp<AudioTrack> newTrack; status_t status; status_t status = NO_ERROR; { // scope for the lock Mutex::Autolock lock(&mLock); Loading Loading @@ -703,6 +716,7 @@ void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftV } #endif if (!mAudioTrack.get() || mPrevSampleID != sample->sampleID()) { // mToggle toggles each time a track is started on a given channel. // The toggle is concatenated with the SoundChannel address and passed to AudioTrack // as callback user data. This enables the detection of callbacks received from the old Loading @@ -728,13 +742,15 @@ void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftV ALOGE("Error creating AudioTrack"); goto exit; } ALOGV("setVolume %p", newTrack.get()); newTrack->setVolume(leftVolume, rightVolume); newTrack->setLoop(0, frameCount, loop); // From now on, AudioTrack callbacks received with previous toggle value will be ignored. mToggle = toggle; mAudioTrack = newTrack; } else { newTrack = mAudioTrack; ALOGV("reusing track %p for sample %d", mAudioTrack.get(), sample->sampleID()); } newTrack->setVolume(leftVolume, rightVolume); newTrack->setLoop(0, frameCount, loop); mPos = 0; mSample = sample; mChannelID = nextChannelID; Loading Loading @@ -877,6 +893,7 @@ bool SoundChannel::doStop_l() setVolume_l(0, 0); ALOGV("stop"); mAudioTrack->stop(); mPrevSampleID = mSample->sampleID(); mSample.clear(); mState = IDLE; mPriority = IDLE_PRIORITY; Loading media/jni/soundpool/SoundPool.h +3 −1 Original line number Diff line number Diff line Loading @@ -136,6 +136,7 @@ public: void nextEvent(); int nextChannelID() { return mNextEvent.channelID(); } void dump(); int getPrevSampleID(void) { return mPrevSampleID; } private: static void callback(int event, void* user, void *info); Loading @@ -152,6 +153,7 @@ private: int mAudioBufferSize; unsigned long mToggle; bool mAutoPaused; int mPrevSampleID; }; // application object for managing a pool of sounds Loading Loading @@ -193,7 +195,7 @@ private: sp<Sample> findSample(int sampleID) { return mSamples.valueFor(sampleID); } SoundChannel* findChannel (int channelID); SoundChannel* findNextChannel (int channelID); SoundChannel* allocateChannel_l(int priority); SoundChannel* allocateChannel_l(int priority, int sampleID); void moveToFront_l(SoundChannel* channel); void notify(SoundPoolEvent event); void dump(); Loading Loading
media/jni/soundpool/SoundPool.cpp +50 −33 Original line number Diff line number Diff line Loading @@ -256,7 +256,7 @@ int SoundPool::play(int sampleID, float leftVolume, float rightVolume, dump(); // allocate a channel channel = allocateChannel_l(priority); channel = allocateChannel_l(priority, sampleID); // no channel allocated - return 0 if (!channel) { Loading @@ -271,13 +271,25 @@ int SoundPool::play(int sampleID, float leftVolume, float rightVolume, return channelID; } SoundChannel* SoundPool::allocateChannel_l(int priority) SoundChannel* SoundPool::allocateChannel_l(int priority, int sampleID) { List<SoundChannel*>::iterator iter; SoundChannel* channel = NULL; // allocate a channel // check if channel for given sampleID still available if (!mChannels.empty()) { for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) { if (sampleID == (*iter)->getPrevSampleID() && (*iter)->state() == SoundChannel::IDLE) { channel = *iter; mChannels.erase(iter); ALOGV("Allocated recycled channel for same sampleID"); break; } } } // allocate any channel if (!channel && !mChannels.empty()) { iter = mChannels.begin(); if (priority >= (*iter)->priority()) { channel = *iter; Loading Loading @@ -648,6 +660,7 @@ error: void SoundChannel::init(SoundPool* soundPool) { mSoundPool = soundPool; mPrevSampleID = -1; } // call with sound pool lock held Loading @@ -656,7 +669,7 @@ void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftV { sp<AudioTrack> oldTrack; sp<AudioTrack> newTrack; status_t status; status_t status = NO_ERROR; { // scope for the lock Mutex::Autolock lock(&mLock); Loading Loading @@ -703,6 +716,7 @@ void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftV } #endif if (!mAudioTrack.get() || mPrevSampleID != sample->sampleID()) { // mToggle toggles each time a track is started on a given channel. // The toggle is concatenated with the SoundChannel address and passed to AudioTrack // as callback user data. This enables the detection of callbacks received from the old Loading @@ -728,13 +742,15 @@ void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftV ALOGE("Error creating AudioTrack"); goto exit; } ALOGV("setVolume %p", newTrack.get()); newTrack->setVolume(leftVolume, rightVolume); newTrack->setLoop(0, frameCount, loop); // From now on, AudioTrack callbacks received with previous toggle value will be ignored. mToggle = toggle; mAudioTrack = newTrack; } else { newTrack = mAudioTrack; ALOGV("reusing track %p for sample %d", mAudioTrack.get(), sample->sampleID()); } newTrack->setVolume(leftVolume, rightVolume); newTrack->setLoop(0, frameCount, loop); mPos = 0; mSample = sample; mChannelID = nextChannelID; Loading Loading @@ -877,6 +893,7 @@ bool SoundChannel::doStop_l() setVolume_l(0, 0); ALOGV("stop"); mAudioTrack->stop(); mPrevSampleID = mSample->sampleID(); mSample.clear(); mState = IDLE; mPriority = IDLE_PRIORITY; Loading
media/jni/soundpool/SoundPool.h +3 −1 Original line number Diff line number Diff line Loading @@ -136,6 +136,7 @@ public: void nextEvent(); int nextChannelID() { return mNextEvent.channelID(); } void dump(); int getPrevSampleID(void) { return mPrevSampleID; } private: static void callback(int event, void* user, void *info); Loading @@ -152,6 +153,7 @@ private: int mAudioBufferSize; unsigned long mToggle; bool mAutoPaused; int mPrevSampleID; }; // application object for managing a pool of sounds Loading Loading @@ -193,7 +195,7 @@ private: sp<Sample> findSample(int sampleID) { return mSamples.valueFor(sampleID); } SoundChannel* findChannel (int channelID); SoundChannel* findNextChannel (int channelID); SoundChannel* allocateChannel_l(int priority); SoundChannel* allocateChannel_l(int priority, int sampleID); void moveToFront_l(SoundChannel* channel); void notify(SoundPoolEvent event); void dump(); Loading