summaryrefslogtreecommitdiff
path: root/media/jni/soundpool/Stream.cpp
blob: 73e319a5902e1d51ac638df88ee681f5bf599224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "SoundPool::Stream"
#include <utils/Log.h>

#include "Stream.h"

#include "StreamManager.h"

namespace android::soundpool {

Stream::~Stream()
{
    ALOGV("%s(%p)", __func__, this);
}

void Stream::autoPause()
{
    std::lock_guard lock(mLock);
    if (mState == PLAYING) {
        ALOGV("%s: track streamID: %d", __func__, (int)mStreamID);
        mState = PAUSED;
        mAutoPaused = true;
        if (mAudioTrack != nullptr) {
            mAudioTrack->pause();
        }
    }
}

void Stream::autoResume()
{
    std::lock_guard lock(mLock);
    if (mAutoPaused) {
        if (mState == PAUSED) {
            ALOGV("%s: track streamID: %d", __func__, (int)mStreamID);
            mState = PLAYING;
            if (mAudioTrack != nullptr) {
                mAudioTrack->start();
            }
        }
        mAutoPaused = false; // New for R: always reset autopause (consistent with API spec).
    }
}

void Stream::mute(bool muting)
{
    std::lock_guard lock(mLock);
    mMuted = muting;
    if (mAudioTrack != nullptr) {
        if (mMuted) {
            mAudioTrack->setVolume(0.0f, 0.0f);
        } else {
            mAudioTrack->setVolume(mLeftVolume, mRightVolume);
        }
    }
}

void Stream::pause(int32_t streamID)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
        if (mState == PLAYING) {
            ALOGV("%s: track streamID: %d", __func__, streamID);
            mState = PAUSED;
            if (mAudioTrack != nullptr) {
                mAudioTrack->pause();
            }
        }
    }
}

void Stream::resume(int32_t streamID)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
         if (mState == PAUSED) {
            ALOGV("%s: track streamID: %d", __func__, streamID);
            mState = PLAYING;
            if (mAudioTrack != nullptr) {
                mAudioTrack->start();
            }
            mAutoPaused = false; // TODO: is this right? (ambiguous per spec), move outside?
        }
    }
}

void Stream::setRate(int32_t streamID, float rate)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
        mRate = rate;
        if (mAudioTrack != nullptr && mSound != nullptr) {
            const auto sampleRate = (uint32_t)lround(double(mSound->getSampleRate()) * rate);
            mAudioTrack->setSampleRate(sampleRate);
        }
    }
}

void Stream::setVolume_l(float leftVolume, float rightVolume)
{
    mLeftVolume = leftVolume;
    mRightVolume = rightVolume;
    if (mAudioTrack != nullptr && !mMuted) {
        mAudioTrack->setVolume(leftVolume, rightVolume);
    }
}

void Stream::setVolume(int32_t streamID, float leftVolume, float rightVolume)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
        setVolume_l(leftVolume, rightVolume);
    }
}

void Stream::setPriority(int32_t streamID, int32_t priority)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
        mPriority = priority;
    }
}

void Stream::setLoop(int32_t streamID, int32_t loop)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
        if (mAudioTrack != nullptr && mSound != nullptr) {
            const uint32_t loopEnd = mSound->getSizeInBytes() / mSound->getChannelCount() /
                (mSound->getFormat() == AUDIO_FORMAT_PCM_16_BIT
                        ? sizeof(int16_t) : sizeof(uint8_t));
            mAudioTrack->setLoop(0, loopEnd, loop);
        }
        mLoop = loop;
    }
}

void Stream::setPlay(
        int32_t streamID, const std::shared_ptr<Sound> &sound, int32_t soundID,
        float leftVolume, float rightVolume, int32_t priority, int32_t loop, float rate)
{
    std::lock_guard lock(mLock);
    // We must be idle, or we must be repurposing a pending Stream.
    LOG_ALWAYS_FATAL_IF(mState != IDLE && mAudioTrack != nullptr, "State %d must be IDLE", mState);
    mSound = sound;
    mSoundID = soundID;
    mLeftVolume = leftVolume;
    mRightVolume = rightVolume;
    mPriority = priority;
    mLoop = loop;
    mRate = rate;
    mState = PLAYING;
    mAutoPaused = false;   // New for R (consistent with Java API spec).
    mStreamID = streamID;  // prefer this to be the last, as it is an atomic sync point
}

void Stream::setStopTimeNs(int64_t stopTimeNs)
{
    std::lock_guard lock(mLock);
    mStopTimeNs = stopTimeNs;
}

bool Stream::requestStop(int32_t streamID)
{
    std::lock_guard lock(mLock);
    if (streamID == mStreamID) {
        ALOGV("%s: track streamID: %d", __func__, streamID);
        if (mAudioTrack != nullptr) {
            if (mState == PLAYING && !mMuted && (mLeftVolume != 0.f || mRightVolume != 0.f)) {
                setVolume_l(0.f, 0.f);
                mStopTimeNs = systemTime() + kStopWaitTimeNs;
            } else {
                mStopTimeNs = systemTime();
            }
            return true; // must be queued on the restart list.
        }
        stop_l();
    }
    return false;
}

void Stream::stop()
{
    std::lock_guard lock(mLock);
    stop_l();
}

void Stream::stop_l()
{
    if (mState != IDLE) {
        ALOGV("%s: track(%p) streamID: %d", __func__, mAudioTrack.get(), (int)mStreamID);
        if (mAudioTrack != nullptr) {
            mAudioTrack->stop();
        }
        mSound.reset();
        mState = IDLE;
    }
}

void Stream::clearAudioTrack()
{
    sp<AudioTrack> release;  // release outside of lock.
    std::lock_guard lock(mLock);
    // This will invoke the destructor which waits for the AudioTrack thread to join,
    // and is currently the only safe way to ensure there are no callbacks afterwards.
    release = mAudioTrack;  // or std::swap if we had move semantics.
    mAudioTrack.clear();
}

Stream* Stream::getPairStream() const
{
   return mStreamManager->getPairStream(this);
}

Stream* Stream::playPairStream() {
    Stream* pairStream = getPairStream();
    LOG_ALWAYS_FATAL_IF(pairStream == nullptr, "No pair stream!");
    sp<AudioTrack> releaseTracks[2];
    {
        ALOGV("%s: track streamID: %d", __func__, (int)getStreamID());
        // TODO: Do we really want to force a simultaneous synchronization between
        // the stream and its pair?

        // note locking order - the paired stream is obtained before the queued stream.
        // we can invert the locking order, but it is slightly more optimal to do it this way.
        std::lock_guard lockp(pairStream->mLock);
        if (pairStream->mSound == nullptr) {
            return nullptr; // no pair sound
        }
        {
            std::lock_guard lock(mLock);
            LOG_ALWAYS_FATAL_IF(mState != IDLE, "State: %d must be IDLE", mState);
            // TODO: do we want a specific set() here?
            pairStream->mAudioTrack = mAudioTrack;
            pairStream->mSoundID = mSoundID; // optimization to reuse AudioTrack.
            pairStream->mToggle = mToggle;
            pairStream->mAutoPaused = mAutoPaused; // save autopause state
            pairStream->mMuted = mMuted;
            mAudioTrack.clear();  // the pair owns the audiotrack.
            mSound.reset();
            mSoundID = 0;
        }
        // TODO: do we need a specific play_l() anymore?
        const int pairState = pairStream->mState;
        pairStream->play_l(pairStream->mSound, pairStream->mStreamID,
                pairStream->mLeftVolume, pairStream->mRightVolume, pairStream->mPriority,
                pairStream->mLoop, pairStream->mRate, releaseTracks);
        if (pairStream->mState == IDLE) {
            return nullptr; // AudioTrack error
        }
        if (pairState == PAUSED) {  // reestablish pause
            pairStream->mState = PAUSED;
            pairStream->mAudioTrack->pause();
        }
    }
    // release tracks outside of Stream lock
    return pairStream;
}

void Stream::play_l(const std::shared_ptr<Sound>& sound, int32_t nextStreamID,
        float leftVolume, float rightVolume, int32_t priority, int32_t loop, float rate,
        sp<AudioTrack> releaseTracks[2])
{
    // These tracks are released without the lock.
    sp<AudioTrack> &oldTrack = releaseTracks[0];
    sp<AudioTrack> &newTrack = releaseTracks[1];
    status_t status = NO_ERROR;

    {
        ALOGV("%s(%p)(soundID=%d, streamID=%d, leftVolume=%f, rightVolume=%f,"
                " priority=%d, loop=%d, rate=%f)",
                __func__, this, sound->getSoundID(), nextStreamID, leftVolume, rightVolume,
                priority, loop, rate);

        // initialize track
        const audio_stream_type_t streamType =
                AudioSystem::attributesToStreamType(*mStreamManager->getAttributes());
        const int32_t channelCount = sound->getChannelCount();
        const auto sampleRate = (uint32_t)lround(double(sound->getSampleRate()) * rate);
        size_t frameCount = 0;

        if (loop) {
            const audio_format_t format = sound->getFormat();
            const size_t frameSize = audio_is_linear_pcm(format)
                    ? channelCount * audio_bytes_per_sample(format) : 1;
            frameCount = sound->getSizeInBytes() / frameSize;
        }

        // check if the existing track has the same sound id.
        if (mAudioTrack != nullptr && mSoundID == sound->getSoundID()) {
            // the sample rate may fail to change if the audio track is a fast track.
            if (mAudioTrack->setSampleRate(sampleRate) == NO_ERROR) {
                newTrack = mAudioTrack;
                ALOGV("%s: reusing track %p for sound %d",
                        __func__, mAudioTrack.get(), sound->getSoundID());
            }
        }
        if (newTrack == nullptr) {
            // mToggle toggles each time a track is started on a given stream.
            // The toggle is concatenated with the Stream address and passed to AudioTrack
            // as callback user data. This enables the detection of callbacks received from the old
            // audio track while the new one is being started and avoids processing them with
            // wrong audio audio buffer size  (mAudioBufferSize)
            auto toggle = mToggle ^ 1;
            void* userData = (void*)((uintptr_t)this | toggle);
            audio_channel_mask_t soundChannelMask = sound->getChannelMask();
            // When sound contains a valid channel mask, use it as is.
            // Otherwise, use stream count to calculate channel mask.
            audio_channel_mask_t channelMask = soundChannelMask != AUDIO_CHANNEL_NONE
                    ? soundChannelMask : audio_channel_out_mask_from_count(channelCount);

            // do not create a new audio track if current track is compatible with sound parameters

            newTrack = new AudioTrack(streamType, sampleRate, sound->getFormat(),
                    channelMask, sound->getIMemory(), AUDIO_OUTPUT_FLAG_FAST,
                    staticCallback, userData,
                    0 /*default notification frames*/, AUDIO_SESSION_ALLOCATE,
                    AudioTrack::TRANSFER_DEFAULT,
                    nullptr /*offloadInfo*/, -1 /*uid*/, -1 /*pid*/,
                    mStreamManager->getAttributes(),
                    false /*doNotReconnect*/, 1.0f /*maxRequiredSpeed*/,
                    mStreamManager->getOpPackageName());
            // Set caller name so it can be logged in destructor.
            // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_SOUNDPOOL
            newTrack->setCallerName("soundpool");
            oldTrack = mAudioTrack;
            status = newTrack->initCheck();
            if (status != NO_ERROR) {
                ALOGE("%s: error creating AudioTrack", __func__);
                // newTrack goes out of scope, so reference count drops to zero
                goto exit;
            }
            // From now on, AudioTrack callbacks received with previous toggle value will be ignored.
            mToggle = toggle;
            mAudioTrack = newTrack;
            ALOGV("%s: using new track %p for sound %d",
                    __func__, newTrack.get(), sound->getSoundID());
        }
        if (mMuted) {
            newTrack->setVolume(0.0f, 0.0f);
        } else {
            newTrack->setVolume(leftVolume, rightVolume);
        }
        newTrack->setLoop(0, frameCount, loop);
        mAudioTrack->start();
        mSound = sound;
        mSoundID = sound->getSoundID();
        mPriority = priority;
        mLoop = loop;
        mLeftVolume = leftVolume;
        mRightVolume = rightVolume;
        mRate = rate;
        mState = PLAYING;
        mStopTimeNs = 0;
        mStreamID = nextStreamID;  // prefer this to be the last, as it is an atomic sync point
    }

exit:
    ALOGV("%s: delete oldTrack %p", __func__, oldTrack.get());
    if (status != NO_ERROR) {
        // TODO: should we consider keeping the soundID if the old track is OK?
        // Do not attempt to restart this track (should we remove the stream id?)
        mState = IDLE;
        mSoundID = 0;
        mSound.reset();
        mAudioTrack.clear();  // actual release from releaseTracks[]
    }
}

/* static */
void Stream::staticCallback(int event, void* user, void* info)
{
    const auto userAsInt = (uintptr_t)user;
    auto stream = reinterpret_cast<Stream*>(userAsInt & ~1);
    stream->callback(event, info, int(userAsInt & 1), 0 /* tries */);
}

void Stream::callback(int event, void* info, int toggle, int tries)
{
    int32_t activeStreamIDToRestart = 0;
    {
        std::unique_lock lock(mLock);
        ALOGV("%s track(%p) streamID %d", __func__, mAudioTrack.get(), (int)mStreamID);

        if (mAudioTrack == nullptr) {
            // The AudioTrack is either with this stream or its pair.
            // if this swaps a few times, the toggle is bound to be wrong, so we fail then.
            //
            // TODO: Modify AudioTrack callbacks to avoid the hacky toggle and retry
            // logic here.
            if (tries < 3) {
                lock.unlock();
                ALOGV("%s streamID %d going to pair stream", __func__, (int)mStreamID);
                getPairStream()->callback(event, info, toggle, tries + 1);
            } else {
                ALOGW("%s streamID %d cannot find track", __func__, (int)mStreamID);
            }
            return;
        }
        if (mToggle != toggle) {
            ALOGD("%s streamID %d wrong toggle", __func__, (int)mStreamID);
            return;
        }
        switch (event) {
        case AudioTrack::EVENT_MORE_DATA:
            ALOGW("%s streamID %d Invalid EVENT_MORE_DATA for static track",
                    __func__, (int)mStreamID);
            break;
        case AudioTrack::EVENT_UNDERRUN:
            ALOGW("%s streamID %d Invalid EVENT_UNDERRUN for static track",
                    __func__, (int)mStreamID);
            break;
        case AudioTrack::EVENT_BUFFER_END:
            ALOGV("%s streamID %d EVENT_BUFFER_END", __func__, (int)mStreamID);
            if (mState != IDLE) {
                activeStreamIDToRestart = mStreamID;
                mStopTimeNs = systemTime();
            }
            break;
        case AudioTrack::EVENT_LOOP_END:
            ALOGV("%s streamID %d EVENT_LOOP_END", __func__, (int)mStreamID);
            break;
        case AudioTrack::EVENT_NEW_IAUDIOTRACK:
            ALOGV("%s streamID %d NEW_IAUDIOTRACK", __func__, (int)mStreamID);
            break;
        default:
            ALOGW("%s streamID %d Invalid event %d", __func__, (int)mStreamID, event);
            break;
        }
    } // lock ends here.  This is on the callback thread, no need to be precise.
    if (activeStreamIDToRestart > 0) {
        // Restart only if a particular streamID is still current and active.
        ALOGV("%s: moveToRestartQueue %d", __func__, activeStreamIDToRestart);
        mStreamManager->moveToRestartQueue(this, activeStreamIDToRestart);
    }
}

void Stream::dump() const
{
    // TODO: consider std::try_lock() - ok for now for ALOGV.
    ALOGV("mPairStream=%p, mState=%d, mStreamID=%d, mSoundID=%d, mPriority=%d, mLoop=%d",
            getPairStream(), mState, (int)getStreamID(), getSoundID(), mPriority, mLoop);
}

} // namespace android::soundpool