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