Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Andreas Huber | dae04ca | 2009-11-05 16:34:39 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 18 | #define LOG_TAG "AudioPlayer" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | 25155ff | 2010-03-01 14:27:00 -0800 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 22 | #include <media/AudioTrack.h> |
| 23 | #include <media/stagefright/AudioPlayer.h> |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 24 | #include <media/stagefright/MediaDebug.h> |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 25 | #include <media/stagefright/MediaDefs.h> |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 26 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 27 | #include <media/stagefright/MediaSource.h> |
| 28 | #include <media/stagefright/MetaData.h> |
| 29 | |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 30 | #include "include/AwesomePlayer.h" |
| 31 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 34 | AudioPlayer::AudioPlayer( |
| 35 | const sp<MediaPlayerBase::AudioSink> &audioSink, |
| 36 | AwesomePlayer *observer) |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 37 | : mAudioTrack(NULL), |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 38 | mInputBuffer(NULL), |
| 39 | mSampleRate(0), |
| 40 | mLatencyUs(0), |
| 41 | mFrameSize(0), |
| 42 | mNumFramesPlayed(0), |
| 43 | mPositionTimeMediaUs(-1), |
| 44 | mPositionTimeRealUs(-1), |
| 45 | mSeeking(false), |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 46 | mReachedEOS(false), |
Andreas Huber | 5295c0c | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 47 | mFinalStatus(OK), |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 48 | mStarted(false), |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 49 | mIsFirstBuffer(false), |
| 50 | mFirstBufferResult(OK), |
| 51 | mFirstBuffer(NULL), |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 52 | mAudioSink(audioSink), |
| 53 | mObserver(observer) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | AudioPlayer::~AudioPlayer() { |
| 57 | if (mStarted) { |
Andreas Huber | b2e3954 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 58 | reset(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 62 | void AudioPlayer::setSource(const sp<MediaSource> &source) { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 63 | CHECK_EQ(mSource, NULL); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 64 | mSource = source; |
| 65 | } |
| 66 | |
Andreas Huber | e7e3b78 | 2010-03-08 15:46:13 -0800 | [diff] [blame] | 67 | status_t AudioPlayer::start(bool sourceAlreadyStarted) { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 68 | CHECK(!mStarted); |
| 69 | CHECK(mSource != NULL); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 70 | |
Andreas Huber | e7e3b78 | 2010-03-08 15:46:13 -0800 | [diff] [blame] | 71 | status_t err; |
| 72 | if (!sourceAlreadyStarted) { |
| 73 | err = mSource->start(); |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 74 | |
Andreas Huber | e7e3b78 | 2010-03-08 15:46:13 -0800 | [diff] [blame] | 75 | if (err != OK) { |
| 76 | return err; |
| 77 | } |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 78 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 79 | |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 80 | // We allow an optional INFO_FORMAT_CHANGED at the very beginning |
| 81 | // of playback, if there is one, getFormat below will retrieve the |
| 82 | // updated format, if there isn't, we'll stash away the valid buffer |
| 83 | // of data to be used on the first audio callback. |
| 84 | |
| 85 | CHECK(mFirstBuffer == NULL); |
| 86 | |
Andreas Huber | c0dfc5b | 2011-05-10 13:56:39 -0700 | [diff] [blame] | 87 | MediaSource::ReadOptions options; |
| 88 | if (mSeeking) { |
| 89 | options.setSeekTo(mSeekTimeUs); |
| 90 | mSeeking = false; |
| 91 | } |
| 92 | |
| 93 | mFirstBufferResult = mSource->read(&mFirstBuffer, &options); |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 94 | if (mFirstBufferResult == INFO_FORMAT_CHANGED) { |
| 95 | LOGV("INFO_FORMAT_CHANGED!!!"); |
| 96 | |
| 97 | CHECK(mFirstBuffer == NULL); |
| 98 | mFirstBufferResult = OK; |
| 99 | mIsFirstBuffer = false; |
| 100 | } else { |
| 101 | mIsFirstBuffer = true; |
| 102 | } |
| 103 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 104 | sp<MetaData> format = mSource->getFormat(); |
| 105 | const char *mime; |
| 106 | bool success = format->findCString(kKeyMIMEType, &mime); |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 107 | CHECK(success); |
Andreas Huber | 18291bc | 2009-09-10 14:13:30 -0700 | [diff] [blame] | 108 | CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 109 | |
| 110 | success = format->findInt32(kKeySampleRate, &mSampleRate); |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 111 | CHECK(success); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 112 | |
| 113 | int32_t numChannels; |
| 114 | success = format->findInt32(kKeyChannelCount, &numChannels); |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 115 | CHECK(success); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 116 | |
| 117 | if (mAudioSink.get() != NULL) { |
| 118 | status_t err = mAudioSink->open( |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 119 | mSampleRate, numChannels, AUDIO_FORMAT_PCM_16_BIT, |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 120 | DEFAULT_AUDIOSINK_BUFFERCOUNT, |
| 121 | &AudioPlayer::AudioSinkCallback, this); |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 122 | if (err != OK) { |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 123 | if (mFirstBuffer != NULL) { |
| 124 | mFirstBuffer->release(); |
| 125 | mFirstBuffer = NULL; |
| 126 | } |
| 127 | |
| 128 | if (!sourceAlreadyStarted) { |
| 129 | mSource->stop(); |
| 130 | } |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 131 | |
| 132 | return err; |
| 133 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 134 | |
| 135 | mLatencyUs = (int64_t)mAudioSink->latency() * 1000; |
| 136 | mFrameSize = mAudioSink->frameSize(); |
| 137 | |
| 138 | mAudioSink->start(); |
| 139 | } else { |
| 140 | mAudioTrack = new AudioTrack( |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 141 | AUDIO_STREAM_MUSIC, mSampleRate, AUDIO_FORMAT_PCM_16_BIT, |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 142 | (numChannels == 2) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 143 | ? AUDIO_CHANNEL_OUT_STEREO |
| 144 | : AUDIO_CHANNEL_OUT_MONO, |
Andreas Huber | a98420e | 2010-04-13 09:58:39 -0700 | [diff] [blame] | 145 | 0, 0, &AudioCallback, this, 0); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 146 | |
Kenny Root | 32dcebf | 2010-02-18 09:03:06 -0800 | [diff] [blame] | 147 | if ((err = mAudioTrack->initCheck()) != OK) { |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 148 | delete mAudioTrack; |
| 149 | mAudioTrack = NULL; |
| 150 | |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 151 | if (mFirstBuffer != NULL) { |
| 152 | mFirstBuffer->release(); |
| 153 | mFirstBuffer = NULL; |
| 154 | } |
| 155 | |
| 156 | if (!sourceAlreadyStarted) { |
| 157 | mSource->stop(); |
| 158 | } |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 159 | |
Kenny Root | 32dcebf | 2010-02-18 09:03:06 -0800 | [diff] [blame] | 160 | return err; |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 161 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 162 | |
| 163 | mLatencyUs = (int64_t)mAudioTrack->latency() * 1000; |
| 164 | mFrameSize = mAudioTrack->frameSize(); |
| 165 | |
| 166 | mAudioTrack->start(); |
| 167 | } |
| 168 | |
| 169 | mStarted = true; |
Andreas Huber | 88c030e | 2010-01-29 13:24:58 -0800 | [diff] [blame] | 170 | |
| 171 | return OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Andreas Huber | b2e3954 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 174 | void AudioPlayer::pause(bool playPendingSamples) { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 175 | CHECK(mStarted); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 176 | |
Andreas Huber | b2e3954 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 177 | if (playPendingSamples) { |
| 178 | if (mAudioSink.get() != NULL) { |
| 179 | mAudioSink->stop(); |
| 180 | } else { |
| 181 | mAudioTrack->stop(); |
| 182 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 183 | } else { |
Andreas Huber | b2e3954 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 184 | if (mAudioSink.get() != NULL) { |
| 185 | mAudioSink->pause(); |
| 186 | } else { |
| 187 | mAudioTrack->pause(); |
| 188 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | void AudioPlayer::resume() { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 193 | CHECK(mStarted); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 194 | |
| 195 | if (mAudioSink.get() != NULL) { |
| 196 | mAudioSink->start(); |
| 197 | } else { |
| 198 | mAudioTrack->start(); |
| 199 | } |
| 200 | } |
| 201 | |
Andreas Huber | b2e3954 | 2010-10-05 10:25:34 -0700 | [diff] [blame] | 202 | void AudioPlayer::reset() { |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 203 | CHECK(mStarted); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 204 | |
| 205 | if (mAudioSink.get() != NULL) { |
| 206 | mAudioSink->stop(); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 207 | mAudioSink->close(); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 208 | } else { |
| 209 | mAudioTrack->stop(); |
| 210 | |
| 211 | delete mAudioTrack; |
| 212 | mAudioTrack = NULL; |
| 213 | } |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 214 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 215 | // Make sure to release any buffer we hold onto so that the |
| 216 | // source is able to stop(). |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 217 | |
| 218 | if (mFirstBuffer != NULL) { |
| 219 | mFirstBuffer->release(); |
| 220 | mFirstBuffer = NULL; |
| 221 | } |
| 222 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 223 | if (mInputBuffer != NULL) { |
Andreas Huber | dae04ca | 2009-11-05 16:34:39 -0800 | [diff] [blame] | 224 | LOGV("AudioPlayer releasing input buffer."); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 225 | |
| 226 | mInputBuffer->release(); |
| 227 | mInputBuffer = NULL; |
| 228 | } |
| 229 | |
| 230 | mSource->stop(); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 231 | |
Andreas Huber | 25155ff | 2010-03-01 14:27:00 -0800 | [diff] [blame] | 232 | // The following hack is necessary to ensure that the OMX |
| 233 | // component is completely released by the time we may try |
| 234 | // to instantiate it again. |
| 235 | wp<MediaSource> tmp = mSource; |
| 236 | mSource.clear(); |
| 237 | while (tmp.promote() != NULL) { |
| 238 | usleep(1000); |
| 239 | } |
| 240 | IPCThreadState::self()->flushCommands(); |
| 241 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 242 | mNumFramesPlayed = 0; |
| 243 | mPositionTimeMediaUs = -1; |
| 244 | mPositionTimeRealUs = -1; |
| 245 | mSeeking = false; |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 246 | mReachedEOS = false; |
Andreas Huber | 5295c0c | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 247 | mFinalStatus = OK; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 248 | mStarted = false; |
| 249 | } |
| 250 | |
| 251 | // static |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame^] | 252 | void AudioPlayer::AudioCallback(int event, void *user, void *info) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 253 | static_cast<AudioPlayer *>(user)->AudioCallback(event, info); |
| 254 | } |
| 255 | |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 256 | bool AudioPlayer::isSeeking() { |
| 257 | Mutex::Autolock autoLock(mLock); |
| 258 | return mSeeking; |
| 259 | } |
| 260 | |
Andreas Huber | 5295c0c | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 261 | bool AudioPlayer::reachedEOS(status_t *finalStatus) { |
| 262 | *finalStatus = OK; |
| 263 | |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 264 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 5295c0c | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 265 | *finalStatus = mFinalStatus; |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 266 | return mReachedEOS; |
| 267 | } |
| 268 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 269 | // static |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 270 | size_t AudioPlayer::AudioSinkCallback( |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 271 | MediaPlayerBase::AudioSink *audioSink, |
| 272 | void *buffer, size_t size, void *cookie) { |
| 273 | AudioPlayer *me = (AudioPlayer *)cookie; |
| 274 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 275 | return me->fillBuffer(buffer, size); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame^] | 278 | void AudioPlayer::AudioCallback(int event, void *info) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 279 | if (event != AudioTrack::EVENT_MORE_DATA) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 284 | size_t numBytesWritten = fillBuffer(buffer->raw, buffer->size); |
| 285 | |
| 286 | buffer->size = numBytesWritten; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Andreas Huber | 84b343f | 2011-03-22 10:31:21 -0700 | [diff] [blame] | 289 | uint32_t AudioPlayer::getNumFramesPendingPlayout() const { |
| 290 | uint32_t numFramesPlayedOut; |
| 291 | status_t err; |
| 292 | |
| 293 | if (mAudioSink != NULL) { |
| 294 | err = mAudioSink->getPosition(&numFramesPlayedOut); |
| 295 | } else { |
| 296 | err = mAudioTrack->getPosition(&numFramesPlayedOut); |
| 297 | } |
| 298 | |
| 299 | if (err != OK || mNumFramesPlayed < numFramesPlayedOut) { |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | // mNumFramesPlayed is the number of frames submitted |
| 304 | // to the audio sink for playback, but not all of them |
| 305 | // may have played out by now. |
| 306 | return mNumFramesPlayed - numFramesPlayedOut; |
| 307 | } |
| 308 | |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 309 | size_t AudioPlayer::fillBuffer(void *data, size_t size) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 310 | if (mNumFramesPlayed == 0) { |
Andreas Huber | dae04ca | 2009-11-05 16:34:39 -0800 | [diff] [blame] | 311 | LOGV("AudioCallback"); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 314 | if (mReachedEOS) { |
Andreas Huber | 51c1e0e | 2011-04-04 11:43:40 -0700 | [diff] [blame] | 315 | return 0; |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Andreas Huber | bd7b717 | 2011-04-04 09:12:56 -0700 | [diff] [blame] | 318 | bool postSeekComplete = false; |
| 319 | bool postEOS = false; |
| 320 | int64_t postEOSDelayUs = 0; |
| 321 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 322 | size_t size_done = 0; |
| 323 | size_t size_remaining = size; |
| 324 | while (size_remaining > 0) { |
| 325 | MediaSource::ReadOptions options; |
| 326 | |
| 327 | { |
| 328 | Mutex::Autolock autoLock(mLock); |
| 329 | |
| 330 | if (mSeeking) { |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 331 | if (mIsFirstBuffer) { |
| 332 | if (mFirstBuffer != NULL) { |
| 333 | mFirstBuffer->release(); |
| 334 | mFirstBuffer = NULL; |
| 335 | } |
| 336 | mIsFirstBuffer = false; |
| 337 | } |
| 338 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 339 | options.setSeekTo(mSeekTimeUs); |
| 340 | |
| 341 | if (mInputBuffer != NULL) { |
| 342 | mInputBuffer->release(); |
| 343 | mInputBuffer = NULL; |
| 344 | } |
Gloria Wang | cb9859b | 2010-02-04 17:43:09 -0800 | [diff] [blame] | 345 | |
| 346 | mSeeking = false; |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 347 | if (mObserver) { |
Andreas Huber | bd7b717 | 2011-04-04 09:12:56 -0700 | [diff] [blame] | 348 | postSeekComplete = true; |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 349 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | if (mInputBuffer == NULL) { |
Andreas Huber | 3cc219d | 2010-07-20 09:44:34 -0700 | [diff] [blame] | 354 | status_t err; |
| 355 | |
| 356 | if (mIsFirstBuffer) { |
| 357 | mInputBuffer = mFirstBuffer; |
| 358 | mFirstBuffer = NULL; |
| 359 | err = mFirstBufferResult; |
| 360 | |
| 361 | mIsFirstBuffer = false; |
| 362 | } else { |
| 363 | err = mSource->read(&mInputBuffer, &options); |
| 364 | } |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 365 | |
Andreas Huber | 0c89199 | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 366 | CHECK((err == OK && mInputBuffer != NULL) |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 367 | || (err != OK && mInputBuffer == NULL)); |
| 368 | |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 369 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | bfa6b2d | 2009-11-20 09:32:46 -0800 | [diff] [blame] | 370 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 371 | if (err != OK) { |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 372 | if (mObserver && !mReachedEOS) { |
Andreas Huber | 84b343f | 2011-03-22 10:31:21 -0700 | [diff] [blame] | 373 | // We don't want to post EOS right away but only |
| 374 | // after all frames have actually been played out. |
| 375 | |
| 376 | // These are the number of frames submitted to the |
| 377 | // AudioTrack that you haven't heard yet. |
| 378 | uint32_t numFramesPendingPlayout = |
| 379 | getNumFramesPendingPlayout(); |
| 380 | |
| 381 | // These are the number of frames we're going to |
| 382 | // submit to the AudioTrack by returning from this |
| 383 | // callback. |
| 384 | uint32_t numAdditionalFrames = size_done / mFrameSize; |
| 385 | |
| 386 | numFramesPendingPlayout += numAdditionalFrames; |
| 387 | |
| 388 | int64_t timeToCompletionUs = |
| 389 | (1000000ll * numFramesPendingPlayout) / mSampleRate; |
| 390 | |
| 391 | LOGV("total number of frames played: %lld (%lld us)", |
| 392 | (mNumFramesPlayed + numAdditionalFrames), |
| 393 | 1000000ll * (mNumFramesPlayed + numAdditionalFrames) |
| 394 | / mSampleRate); |
| 395 | |
| 396 | LOGV("%d frames left to play, %lld us (%.2f secs)", |
| 397 | numFramesPendingPlayout, |
| 398 | timeToCompletionUs, timeToCompletionUs / 1E6); |
| 399 | |
Andreas Huber | bd7b717 | 2011-04-04 09:12:56 -0700 | [diff] [blame] | 400 | postEOS = true; |
| 401 | postEOSDelayUs = timeToCompletionUs + mLatencyUs; |
Andreas Huber | ed54ad0 | 2010-09-28 11:56:39 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 404 | mReachedEOS = true; |
Andreas Huber | 5295c0c | 2010-02-23 13:45:33 -0800 | [diff] [blame] | 405 | mFinalStatus = err; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | |
Andreas Huber | 48c948b | 2009-10-08 10:07:49 -0700 | [diff] [blame] | 409 | CHECK(mInputBuffer->meta_data()->findInt64( |
| 410 | kKeyTime, &mPositionTimeMediaUs)); |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 411 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 412 | mPositionTimeRealUs = |
Andreas Huber | 80a68de | 2010-02-03 15:35:59 -0800 | [diff] [blame] | 413 | ((mNumFramesPlayed + size_done / mFrameSize) * 1000000) |
Andreas Huber | 0024245 | 2009-07-28 10:03:13 -0700 | [diff] [blame] | 414 | / mSampleRate; |
Andreas Huber | dae04ca | 2009-11-05 16:34:39 -0800 | [diff] [blame] | 415 | |
| 416 | LOGV("buffer->size() = %d, " |
| 417 | "mPositionTimeMediaUs=%.2f mPositionTimeRealUs=%.2f", |
| 418 | mInputBuffer->range_length(), |
| 419 | mPositionTimeMediaUs / 1E6, mPositionTimeRealUs / 1E6); |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (mInputBuffer->range_length() == 0) { |
| 423 | mInputBuffer->release(); |
| 424 | mInputBuffer = NULL; |
| 425 | |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | size_t copy = size_remaining; |
| 430 | if (copy > mInputBuffer->range_length()) { |
| 431 | copy = mInputBuffer->range_length(); |
| 432 | } |
| 433 | |
| 434 | memcpy((char *)data + size_done, |
| 435 | (const char *)mInputBuffer->data() + mInputBuffer->range_offset(), |
| 436 | copy); |
| 437 | |
| 438 | mInputBuffer->set_range(mInputBuffer->range_offset() + copy, |
| 439 | mInputBuffer->range_length() - copy); |
Andreas Huber | fc9ba09 | 2010-01-11 15:35:19 -0800 | [diff] [blame] | 440 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 441 | size_done += copy; |
| 442 | size_remaining -= copy; |
| 443 | } |
| 444 | |
Andreas Huber | bd7b717 | 2011-04-04 09:12:56 -0700 | [diff] [blame] | 445 | { |
| 446 | Mutex::Autolock autoLock(mLock); |
| 447 | mNumFramesPlayed += size_done / mFrameSize; |
| 448 | } |
| 449 | |
| 450 | if (postEOS) { |
| 451 | mObserver->postAudioEOS(postEOSDelayUs); |
| 452 | } |
| 453 | |
| 454 | if (postSeekComplete) { |
| 455 | mObserver->postAudioSeekComplete(); |
| 456 | } |
Andreas Huber | 7d5b8a7 | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 457 | |
| 458 | return size_done; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | int64_t AudioPlayer::getRealTimeUs() { |
| 462 | Mutex::Autolock autoLock(mLock); |
| 463 | return getRealTimeUsLocked(); |
| 464 | } |
| 465 | |
| 466 | int64_t AudioPlayer::getRealTimeUsLocked() const { |
| 467 | return -mLatencyUs + (mNumFramesPlayed * 1000000) / mSampleRate; |
| 468 | } |
| 469 | |
| 470 | int64_t AudioPlayer::getMediaTimeUs() { |
| 471 | Mutex::Autolock autoLock(mLock); |
| 472 | |
Andreas Huber | ad6516d | 2010-02-03 10:23:26 -0800 | [diff] [blame] | 473 | if (mPositionTimeMediaUs < 0 || mPositionTimeRealUs < 0) { |
Andreas Huber | fe9b719 | 2011-03-25 16:36:13 -0700 | [diff] [blame] | 474 | if (mSeeking) { |
| 475 | return mSeekTimeUs; |
| 476 | } |
| 477 | |
Andreas Huber | ad6516d | 2010-02-03 10:23:26 -0800 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Andreas Huber | 80a68de | 2010-02-03 15:35:59 -0800 | [diff] [blame] | 481 | int64_t realTimeOffset = getRealTimeUsLocked() - mPositionTimeRealUs; |
| 482 | if (realTimeOffset < 0) { |
| 483 | realTimeOffset = 0; |
| 484 | } |
| 485 | |
| 486 | return mPositionTimeMediaUs + realTimeOffset; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | bool AudioPlayer::getMediaTimeMapping( |
| 490 | int64_t *realtime_us, int64_t *mediatime_us) { |
| 491 | Mutex::Autolock autoLock(mLock); |
| 492 | |
| 493 | *realtime_us = mPositionTimeRealUs; |
| 494 | *mediatime_us = mPositionTimeMediaUs; |
| 495 | |
Andreas Huber | ad6516d | 2010-02-03 10:23:26 -0800 | [diff] [blame] | 496 | return mPositionTimeRealUs != -1 && mPositionTimeMediaUs != -1; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | status_t AudioPlayer::seekTo(int64_t time_us) { |
| 500 | Mutex::Autolock autoLock(mLock); |
| 501 | |
| 502 | mSeeking = true; |
Andreas Huber | fe9b719 | 2011-03-25 16:36:13 -0700 | [diff] [blame] | 503 | mPositionTimeRealUs = mPositionTimeMediaUs = -1; |
Andreas Huber | 1862a33 | 2010-02-03 11:37:29 -0800 | [diff] [blame] | 504 | mReachedEOS = false; |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 505 | mSeekTimeUs = time_us; |
| 506 | |
Andreas Huber | c7d368d | 2010-09-28 13:13:38 -0700 | [diff] [blame] | 507 | if (mAudioSink != NULL) { |
| 508 | mAudioSink->flush(); |
| 509 | } else { |
| 510 | mAudioTrack->flush(); |
| 511 | } |
| 512 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 513 | return OK; |
| 514 | } |
| 515 | |
| 516 | } |