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