Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 1 |
| 18 | #define LOG_TAG "VideoEditorPlayer" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "VideoEditorPlayer.h" |
| 22 | #include "PreviewPlayer.h" |
| 23 | |
| 24 | #include <media/Metadata.h> |
| 25 | #include <media/stagefright/MediaExtractor.h> |
Dima Zavin | 6859837 | 2011-04-05 16:13:49 -0700 | [diff] [blame] | 26 | |
Dima Zavin | 272eb55 | 2011-05-11 14:15:37 -0700 | [diff] [blame] | 27 | #include <system/audio.h> |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Chih-Chung Chang | 43fcc39 | 2011-08-02 16:17:39 +0800 | [diff] [blame] | 31 | VideoEditorPlayer::VideoEditorPlayer(NativeWindowRenderer* renderer) |
| 32 | : mPlayer(new PreviewPlayer(renderer)) { |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 33 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 34 | ALOGV("VideoEditorPlayer"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 35 | mPlayer->setListener(this); |
| 36 | } |
| 37 | |
| 38 | VideoEditorPlayer::~VideoEditorPlayer() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 39 | ALOGV("~VideoEditorPlayer"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 40 | |
| 41 | reset(); |
| 42 | mVeAudioSink.clear(); |
| 43 | |
| 44 | delete mPlayer; |
| 45 | mPlayer = NULL; |
| 46 | } |
| 47 | |
| 48 | status_t VideoEditorPlayer::initCheck() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 49 | ALOGV("initCheck"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 50 | return OK; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | status_t VideoEditorPlayer::setAudioPlayer(VideoEditorAudioPlayer *audioPlayer) { |
| 55 | return mPlayer->setAudioPlayer(audioPlayer); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | status_t VideoEditorPlayer::setDataSource( |
| 60 | const char *url, const KeyedVector<String8, String8> *headers) { |
Steve Block | ec9e663 | 2012-01-04 20:06:05 +0000 | [diff] [blame] | 61 | ALOGI("setDataSource('%s')", url); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 62 | |
| 63 | return mPlayer->setDataSource(url, headers); |
| 64 | } |
| 65 | |
| 66 | //We donot use this in preview, dummy implimentation as this is pure virtual |
| 67 | status_t VideoEditorPlayer::setDataSource(int fd, int64_t offset, |
| 68 | int64_t length) { |
Steve Block | f8bd29c | 2012-01-08 10:14:44 +0000 | [diff] [blame] | 69 | ALOGE("setDataSource(%d, %lld, %lld) Not supported", fd, offset, length); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 70 | return (!OK); |
| 71 | } |
| 72 | |
| 73 | status_t VideoEditorPlayer::setVideoSurface(const sp<Surface> &surface) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 74 | ALOGV("setVideoSurface"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 75 | |
| 76 | mPlayer->setSurface(surface); |
| 77 | return OK; |
| 78 | } |
| 79 | |
| 80 | status_t VideoEditorPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 81 | ALOGV("setVideoSurfaceTexture"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 82 | |
| 83 | mPlayer->setSurfaceTexture(surfaceTexture); |
| 84 | return OK; |
| 85 | } |
| 86 | |
| 87 | status_t VideoEditorPlayer::prepare() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 88 | ALOGV("prepare"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 89 | return mPlayer->prepare(); |
| 90 | } |
| 91 | |
| 92 | status_t VideoEditorPlayer::prepareAsync() { |
| 93 | return mPlayer->prepareAsync(); |
| 94 | } |
| 95 | |
| 96 | status_t VideoEditorPlayer::start() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 97 | ALOGV("start"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 98 | return mPlayer->play(); |
| 99 | } |
| 100 | |
| 101 | status_t VideoEditorPlayer::stop() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 102 | ALOGV("stop"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 103 | return pause(); |
| 104 | } |
| 105 | |
| 106 | status_t VideoEditorPlayer::pause() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 107 | ALOGV("pause"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 108 | return mPlayer->pause(); |
| 109 | } |
| 110 | |
| 111 | bool VideoEditorPlayer::isPlaying() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 112 | ALOGV("isPlaying"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 113 | return mPlayer->isPlaying(); |
| 114 | } |
| 115 | |
| 116 | status_t VideoEditorPlayer::seekTo(int msec) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 117 | ALOGV("seekTo"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 118 | status_t err = mPlayer->seekTo((int64_t)msec * 1000); |
| 119 | return err; |
| 120 | } |
| 121 | |
| 122 | status_t VideoEditorPlayer::getCurrentPosition(int *msec) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 123 | ALOGV("getCurrentPosition"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 124 | int64_t positionUs; |
| 125 | status_t err = mPlayer->getPosition(&positionUs); |
| 126 | |
| 127 | if (err != OK) { |
| 128 | return err; |
| 129 | } |
| 130 | |
| 131 | *msec = (positionUs + 500) / 1000; |
| 132 | return OK; |
| 133 | } |
| 134 | |
| 135 | status_t VideoEditorPlayer::getDuration(int *msec) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 136 | ALOGV("getDuration"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 137 | |
| 138 | int64_t durationUs; |
| 139 | status_t err = mPlayer->getDuration(&durationUs); |
| 140 | |
| 141 | if (err != OK) { |
| 142 | *msec = 0; |
| 143 | return OK; |
| 144 | } |
| 145 | |
| 146 | *msec = (durationUs + 500) / 1000; |
| 147 | return OK; |
| 148 | } |
| 149 | |
| 150 | status_t VideoEditorPlayer::reset() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 151 | ALOGV("reset"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 152 | mPlayer->reset(); |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | status_t VideoEditorPlayer::setLooping(int loop) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 157 | ALOGV("setLooping"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 158 | return mPlayer->setLooping(loop); |
| 159 | } |
| 160 | |
Gloria Wang | acb62af | 2011-04-25 17:29:16 -0700 | [diff] [blame] | 161 | status_t VideoEditorPlayer::setParameter(int key, const Parcel &request) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 162 | ALOGV("setParameter"); |
Gloria Wang | acb62af | 2011-04-25 17:29:16 -0700 | [diff] [blame] | 163 | return mPlayer->setParameter(key, request); |
| 164 | } |
| 165 | |
| 166 | status_t VideoEditorPlayer::getParameter(int key, Parcel *reply) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 167 | ALOGV("getParameter"); |
Gloria Wang | acb62af | 2011-04-25 17:29:16 -0700 | [diff] [blame] | 168 | return mPlayer->getParameter(key, reply); |
| 169 | } |
| 170 | |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 171 | player_type VideoEditorPlayer::playerType() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 172 | ALOGV("playerType"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 173 | return STAGEFRIGHT_PLAYER; |
| 174 | } |
| 175 | |
Raghavender Palla | fa31daf | 2011-03-18 22:32:51 -0700 | [diff] [blame] | 176 | void VideoEditorPlayer::acquireLock() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 177 | ALOGV("acquireLock"); |
Raghavender Palla | fa31daf | 2011-03-18 22:32:51 -0700 | [diff] [blame] | 178 | mPlayer->acquireLock(); |
| 179 | } |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 180 | |
| 181 | void VideoEditorPlayer::releaseLock() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 182 | ALOGV("releaseLock"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 183 | mPlayer->releaseLock(); |
| 184 | } |
| 185 | |
| 186 | status_t VideoEditorPlayer::invoke(const Parcel &request, Parcel *reply) { |
| 187 | return INVALID_OPERATION; |
| 188 | } |
| 189 | |
| 190 | void VideoEditorPlayer::setAudioSink(const sp<AudioSink> &audioSink) { |
| 191 | MediaPlayerInterface::setAudioSink(audioSink); |
| 192 | |
| 193 | mPlayer->setAudioSink(audioSink); |
| 194 | } |
| 195 | |
| 196 | status_t VideoEditorPlayer::getMetadata( |
| 197 | const media::Metadata::Filter& ids, Parcel *records) { |
| 198 | using media::Metadata; |
| 199 | |
| 200 | uint32_t flags = mPlayer->flags(); |
| 201 | |
| 202 | Metadata metadata(records); |
| 203 | |
| 204 | metadata.appendBool( |
| 205 | Metadata::kPauseAvailable, |
| 206 | flags & MediaExtractor::CAN_PAUSE); |
| 207 | |
| 208 | metadata.appendBool( |
| 209 | Metadata::kSeekBackwardAvailable, |
| 210 | flags & MediaExtractor::CAN_SEEK_BACKWARD); |
| 211 | |
| 212 | metadata.appendBool( |
| 213 | Metadata::kSeekForwardAvailable, |
| 214 | flags & MediaExtractor::CAN_SEEK_FORWARD); |
| 215 | |
| 216 | metadata.appendBool( |
| 217 | Metadata::kSeekAvailable, |
| 218 | flags & MediaExtractor::CAN_SEEK); |
| 219 | |
| 220 | return OK; |
| 221 | } |
| 222 | |
| 223 | status_t VideoEditorPlayer::loadEffectsSettings( |
| 224 | M4VSS3GPP_EffectSettings* pEffectSettings, int nEffects) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 225 | ALOGV("loadEffectsSettings"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 226 | return mPlayer->loadEffectsSettings(pEffectSettings, nEffects); |
| 227 | } |
| 228 | |
| 229 | status_t VideoEditorPlayer::loadAudioMixSettings( |
| 230 | M4xVSS_AudioMixingSettings* pAudioMixSettings) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 231 | ALOGV("VideoEditorPlayer: loadAudioMixSettings"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 232 | return mPlayer->loadAudioMixSettings(pAudioMixSettings); |
| 233 | } |
| 234 | |
| 235 | status_t VideoEditorPlayer::setAudioMixPCMFileHandle( |
| 236 | M4OSA_Context pAudioMixPCMFileHandle) { |
| 237 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 238 | ALOGV("VideoEditorPlayer: loadAudioMixSettings"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 239 | return mPlayer->setAudioMixPCMFileHandle(pAudioMixPCMFileHandle); |
| 240 | } |
| 241 | |
| 242 | status_t VideoEditorPlayer::setAudioMixStoryBoardParam( |
| 243 | M4OSA_UInt32 audioMixStoryBoardTS, |
| 244 | M4OSA_UInt32 currentMediaBeginCutTime, |
| 245 | M4OSA_UInt32 primaryTrackVolValue) { |
| 246 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 247 | ALOGV("VideoEditorPlayer: loadAudioMixSettings"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 248 | return mPlayer->setAudioMixStoryBoardParam(audioMixStoryBoardTS, |
| 249 | currentMediaBeginCutTime, primaryTrackVolValue); |
| 250 | } |
| 251 | |
| 252 | status_t VideoEditorPlayer::setPlaybackBeginTime(uint32_t msec) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 253 | ALOGV("setPlaybackBeginTime"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 254 | return mPlayer->setPlaybackBeginTime(msec); |
| 255 | } |
| 256 | |
| 257 | status_t VideoEditorPlayer::setPlaybackEndTime(uint32_t msec) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 258 | ALOGV("setPlaybackEndTime"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 259 | return mPlayer->setPlaybackEndTime(msec); |
| 260 | } |
| 261 | |
| 262 | status_t VideoEditorPlayer::setStoryboardStartTime(uint32_t msec) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 263 | ALOGV("setStoryboardStartTime"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 264 | return mPlayer->setStoryboardStartTime(msec); |
| 265 | } |
| 266 | |
| 267 | status_t VideoEditorPlayer::setProgressCallbackInterval(uint32_t cbInterval) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 268 | ALOGV("setProgressCallbackInterval"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 269 | return mPlayer->setProgressCallbackInterval(cbInterval); |
| 270 | } |
| 271 | |
| 272 | status_t VideoEditorPlayer::setMediaRenderingMode( |
| 273 | M4xVSS_MediaRendering mode, |
| 274 | M4VIDEOEDITING_VideoFrameSize outputVideoSize) { |
| 275 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 276 | ALOGV("setMediaRenderingMode"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 277 | return mPlayer->setMediaRenderingMode(mode, outputVideoSize); |
| 278 | } |
| 279 | |
| 280 | status_t VideoEditorPlayer::resetJniCallbackTimeStamp() { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 281 | ALOGV("resetJniCallbackTimeStamp"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 282 | return mPlayer->resetJniCallbackTimeStamp(); |
| 283 | } |
| 284 | |
| 285 | status_t VideoEditorPlayer::setImageClipProperties( |
| 286 | uint32_t width, uint32_t height) { |
| 287 | return mPlayer->setImageClipProperties(width, height); |
| 288 | } |
| 289 | |
| 290 | status_t VideoEditorPlayer::readFirstVideoFrame() { |
| 291 | return mPlayer->readFirstVideoFrame(); |
| 292 | } |
| 293 | |
| 294 | status_t VideoEditorPlayer::getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs) { |
| 295 | mPlayer->getLastRenderedTimeMs(lastRenderedTimeMs); |
| 296 | return NO_ERROR; |
| 297 | } |
| 298 | |
| 299 | /* Implementation of AudioSink interface */ |
| 300 | #undef LOG_TAG |
| 301 | #define LOG_TAG "VeAudioSink" |
| 302 | |
| 303 | int VideoEditorPlayer::VeAudioOutput::mMinBufferCount = 4; |
| 304 | bool VideoEditorPlayer::VeAudioOutput::mIsOnEmulator = false; |
| 305 | |
| 306 | VideoEditorPlayer::VeAudioOutput::VeAudioOutput() |
| 307 | : mCallback(NULL), |
| 308 | mCallbackCookie(NULL) { |
| 309 | mTrack = 0; |
| 310 | mStreamType = AUDIO_STREAM_MUSIC; |
| 311 | mLeftVolume = 1.0; |
| 312 | mRightVolume = 1.0; |
| 313 | mLatency = 0; |
| 314 | mMsecsPerFrame = 0; |
| 315 | mNumFramesWritten = 0; |
| 316 | setMinBufferCount(); |
| 317 | } |
| 318 | |
| 319 | VideoEditorPlayer::VeAudioOutput::~VeAudioOutput() { |
| 320 | close(); |
| 321 | } |
| 322 | |
| 323 | void VideoEditorPlayer::VeAudioOutput::setMinBufferCount() { |
| 324 | |
| 325 | mIsOnEmulator = false; |
| 326 | mMinBufferCount = 4; |
| 327 | } |
| 328 | |
| 329 | bool VideoEditorPlayer::VeAudioOutput::isOnEmulator() { |
| 330 | |
| 331 | setMinBufferCount(); |
| 332 | return mIsOnEmulator; |
| 333 | } |
| 334 | |
| 335 | int VideoEditorPlayer::VeAudioOutput::getMinBufferCount() { |
| 336 | |
| 337 | setMinBufferCount(); |
| 338 | return mMinBufferCount; |
| 339 | } |
| 340 | |
| 341 | ssize_t VideoEditorPlayer::VeAudioOutput::bufferSize() const { |
| 342 | |
| 343 | if (mTrack == 0) return NO_INIT; |
| 344 | return mTrack->frameCount() * frameSize(); |
| 345 | } |
| 346 | |
| 347 | ssize_t VideoEditorPlayer::VeAudioOutput::frameCount() const { |
| 348 | |
| 349 | if (mTrack == 0) return NO_INIT; |
| 350 | return mTrack->frameCount(); |
| 351 | } |
| 352 | |
| 353 | ssize_t VideoEditorPlayer::VeAudioOutput::channelCount() const |
| 354 | { |
| 355 | if (mTrack == 0) return NO_INIT; |
| 356 | return mTrack->channelCount(); |
| 357 | } |
| 358 | |
| 359 | ssize_t VideoEditorPlayer::VeAudioOutput::frameSize() const |
| 360 | { |
| 361 | if (mTrack == 0) return NO_INIT; |
| 362 | return mTrack->frameSize(); |
| 363 | } |
| 364 | |
| 365 | uint32_t VideoEditorPlayer::VeAudioOutput::latency () const |
| 366 | { |
| 367 | return mLatency; |
| 368 | } |
| 369 | |
| 370 | float VideoEditorPlayer::VeAudioOutput::msecsPerFrame() const |
| 371 | { |
| 372 | return mMsecsPerFrame; |
| 373 | } |
| 374 | |
| 375 | status_t VideoEditorPlayer::VeAudioOutput::getPosition(uint32_t *position) { |
| 376 | |
| 377 | if (mTrack == 0) return NO_INIT; |
| 378 | return mTrack->getPosition(position); |
| 379 | } |
| 380 | |
| 381 | status_t VideoEditorPlayer::VeAudioOutput::open( |
Glenn Kasten | 8dcbd11 | 2012-01-04 09:30:49 -0800 | [diff] [blame^] | 382 | uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount, |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 383 | AudioCallback cb, void *cookie) { |
| 384 | |
| 385 | mCallback = cb; |
| 386 | mCallbackCookie = cookie; |
| 387 | |
| 388 | // Check argument "bufferCount" against the mininum buffer count |
| 389 | if (bufferCount < mMinBufferCount) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 390 | ALOGV("bufferCount (%d) is too small and increased to %d", |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 391 | bufferCount, mMinBufferCount); |
| 392 | bufferCount = mMinBufferCount; |
| 393 | |
| 394 | } |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 395 | ALOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 396 | if (mTrack) close(); |
| 397 | int afSampleRate; |
| 398 | int afFrameCount; |
| 399 | int frameCount; |
| 400 | |
| 401 | if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != |
| 402 | NO_ERROR) { |
| 403 | return NO_INIT; |
| 404 | } |
| 405 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != |
| 406 | NO_ERROR) { |
| 407 | return NO_INIT; |
| 408 | } |
| 409 | |
| 410 | frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate; |
| 411 | |
| 412 | AudioTrack *t; |
| 413 | if (mCallback != NULL) { |
| 414 | t = new AudioTrack( |
| 415 | mStreamType, |
| 416 | sampleRate, |
| 417 | format, |
| 418 | (channelCount == 2) ? |
| 419 | AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO, |
| 420 | frameCount, |
| 421 | 0 /* flags */, |
| 422 | CallbackWrapper, |
| 423 | this); |
| 424 | } else { |
| 425 | t = new AudioTrack( |
| 426 | mStreamType, |
| 427 | sampleRate, |
| 428 | format, |
| 429 | (channelCount == 2) ? |
| 430 | AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO, |
| 431 | frameCount); |
| 432 | } |
| 433 | |
| 434 | if ((t == 0) || (t->initCheck() != NO_ERROR)) { |
Steve Block | f8bd29c | 2012-01-08 10:14:44 +0000 | [diff] [blame] | 435 | ALOGE("Unable to create audio track"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 436 | delete t; |
| 437 | return NO_INIT; |
| 438 | } |
| 439 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 440 | ALOGV("setVolume"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 441 | t->setVolume(mLeftVolume, mRightVolume); |
| 442 | mMsecsPerFrame = 1.e3 / (float) sampleRate; |
| 443 | mLatency = t->latency(); |
| 444 | mTrack = t; |
| 445 | return NO_ERROR; |
| 446 | } |
| 447 | |
| 448 | void VideoEditorPlayer::VeAudioOutput::start() { |
| 449 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 450 | ALOGV("start"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 451 | if (mTrack) { |
| 452 | mTrack->setVolume(mLeftVolume, mRightVolume); |
| 453 | mTrack->start(); |
| 454 | mTrack->getPosition(&mNumFramesWritten); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void VideoEditorPlayer::VeAudioOutput::snoopWrite( |
| 459 | const void* buffer, size_t size) { |
| 460 | // Visualization buffers not supported |
| 461 | return; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | ssize_t VideoEditorPlayer::VeAudioOutput::write( |
| 466 | const void* buffer, size_t size) { |
| 467 | |
| 468 | LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback."); |
| 469 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 470 | //ALOGV("write(%p, %u)", buffer, size); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 471 | if (mTrack) { |
| 472 | snoopWrite(buffer, size); |
| 473 | ssize_t ret = mTrack->write(buffer, size); |
| 474 | mNumFramesWritten += ret / 4; // assume 16 bit stereo |
| 475 | return ret; |
| 476 | } |
| 477 | return NO_INIT; |
| 478 | } |
| 479 | |
| 480 | void VideoEditorPlayer::VeAudioOutput::stop() { |
| 481 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 482 | ALOGV("stop"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 483 | if (mTrack) mTrack->stop(); |
| 484 | } |
| 485 | |
| 486 | void VideoEditorPlayer::VeAudioOutput::flush() { |
| 487 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 488 | ALOGV("flush"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 489 | if (mTrack) mTrack->flush(); |
| 490 | } |
| 491 | |
| 492 | void VideoEditorPlayer::VeAudioOutput::pause() { |
| 493 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 494 | ALOGV("VeAudioOutput::pause"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 495 | if (mTrack) mTrack->pause(); |
| 496 | } |
| 497 | |
| 498 | void VideoEditorPlayer::VeAudioOutput::close() { |
| 499 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 500 | ALOGV("close"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 501 | delete mTrack; |
| 502 | mTrack = 0; |
| 503 | } |
| 504 | |
| 505 | void VideoEditorPlayer::VeAudioOutput::setVolume(float left, float right) { |
| 506 | |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 507 | ALOGV("setVolume(%f, %f)", left, right); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 508 | mLeftVolume = left; |
| 509 | mRightVolume = right; |
| 510 | if (mTrack) { |
| 511 | mTrack->setVolume(left, right); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // static |
| 516 | void VideoEditorPlayer::VeAudioOutput::CallbackWrapper( |
| 517 | int event, void *cookie, void *info) { |
Steve Block | 2703f23 | 2011-10-20 11:56:09 +0100 | [diff] [blame] | 518 | //ALOGV("VeAudioOutput::callbackwrapper"); |
Chih-Chung Chang | 9969866 | 2011-06-30 14:21:38 +0800 | [diff] [blame] | 519 | if (event != AudioTrack::EVENT_MORE_DATA) { |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | VeAudioOutput *me = (VeAudioOutput *)cookie; |
| 524 | AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; |
| 525 | |
| 526 | size_t actualSize = (*me->mCallback)( |
| 527 | me, buffer->raw, buffer->size, me->mCallbackCookie); |
| 528 | |
| 529 | buffer->size = actualSize; |
| 530 | |
| 531 | if (actualSize > 0) { |
| 532 | me->snoopWrite(buffer->raw, actualSize); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | status_t VideoEditorPlayer::VeAudioOutput::dump(int fd, const Vector<String16>& args) const |
| 537 | { |
| 538 | const size_t SIZE = 256; |
| 539 | char buffer[SIZE]; |
| 540 | String8 result; |
| 541 | |
| 542 | result.append(" VeAudioOutput\n"); |
| 543 | snprintf(buffer, SIZE-1, " stream type(%d), left - right volume(%f, %f)\n", |
| 544 | mStreamType, mLeftVolume, mRightVolume); |
| 545 | result.append(buffer); |
| 546 | snprintf(buffer, SIZE-1, " msec per frame(%f), latency (%d)\n", |
| 547 | mMsecsPerFrame, mLatency); |
| 548 | result.append(buffer); |
| 549 | ::write(fd, result.string(), result.size()); |
| 550 | if (mTrack != 0) { |
| 551 | mTrack->dump(fd, args); |
| 552 | } |
| 553 | return NO_ERROR; |
| 554 | } |
| 555 | |
| 556 | int VideoEditorPlayer::VeAudioOutput::getSessionId() { |
| 557 | |
| 558 | return mSessionId; |
| 559 | } |
| 560 | |
| 561 | } // namespace android |