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> 26 27 #include <system/audio.h> 28 29 namespace android { 30 31 VideoEditorPlayer::VideoEditorPlayer(NativeWindowRenderer* renderer) 32 : mPlayer(new PreviewPlayer(renderer)) { 33 34 ALOGV("VideoEditorPlayer"); 35 mPlayer->setListener(this); 36 } 37 38 VideoEditorPlayer::~VideoEditorPlayer() { 39 ALOGV("~VideoEditorPlayer"); 40 41 reset(); 42 mVeAudioSink.clear(); 43 44 delete mPlayer; 45 mPlayer = NULL; 46 } 47 48 status_t VideoEditorPlayer::initCheck() { 49 ALOGV("initCheck"); 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) { 61 ALOGI("setDataSource('%s')", url); 62 if (headers != NULL) { 63 ALOGE("Headers parameter is not supported"); 64 return INVALID_OPERATION; 65 } 66 67 return mPlayer->setDataSource(url); 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) { 73 ALOGE("setDataSource(%d, %lld, %lld) Not supported", fd, offset, length); 74 return (!OK); 75 } 76 77 status_t VideoEditorPlayer::setVideoSurface(const sp<Surface> &surface) { 78 ALOGV("setVideoSurface"); 79 80 mPlayer->setSurface(surface); 81 return OK; 82 } 83 84 status_t VideoEditorPlayer::setVideoSurfaceTexture(const sp<IGraphicBufferProducer> &bufferProducer) { 85 ALOGV("setVideoSurfaceTexture"); 86 87 mPlayer->setSurfaceTexture(bufferProducer); 88 return OK; 89 } 90 91 status_t VideoEditorPlayer::prepare() { 92 ALOGV("prepare"); 93 return mPlayer->prepare(); 94 } 95 96 status_t VideoEditorPlayer::prepareAsync() { 97 return mPlayer->prepareAsync(); 98 } 99 100 status_t VideoEditorPlayer::start() { 101 ALOGV("start"); 102 return mPlayer->play(); 103 } 104 105 status_t VideoEditorPlayer::stop() { 106 ALOGV("stop"); 107 return pause(); 108 } 109 110 status_t VideoEditorPlayer::pause() { 111 ALOGV("pause"); 112 return mPlayer->pause(); 113 } 114 115 bool VideoEditorPlayer::isPlaying() { 116 ALOGV("isPlaying"); 117 return mPlayer->isPlaying(); 118 } 119 120 status_t VideoEditorPlayer::seekTo(int msec) { 121 ALOGV("seekTo"); 122 status_t err = mPlayer->seekTo((int64_t)msec * 1000); 123 return err; 124 } 125 126 status_t VideoEditorPlayer::getCurrentPosition(int *msec) { 127 ALOGV("getCurrentPosition"); 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) { 140 ALOGV("getDuration"); 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() { 155 ALOGV("reset"); 156 mPlayer->reset(); 157 return OK; 158 } 159 160 status_t VideoEditorPlayer::setLooping(int loop) { 161 ALOGV("setLooping"); 162 return mPlayer->setLooping(loop); 163 } 164 165 status_t VideoEditorPlayer::setParameter(int key, const Parcel &request) { 166 ALOGE("setParameter not implemented"); 167 return INVALID_OPERATION; 168 } 169 170 status_t VideoEditorPlayer::getParameter(int key, Parcel *reply) { 171 ALOGE("getParameter not implemented"); 172 return INVALID_OPERATION; 173 } 174 175 player_type VideoEditorPlayer::playerType() { 176 ALOGV("playerType"); 177 return STAGEFRIGHT_PLAYER; 178 } 179 180 void VideoEditorPlayer::acquireLock() { 181 ALOGV("acquireLock"); 182 mPlayer->acquireLock(); 183 } 184 185 void VideoEditorPlayer::releaseLock() { 186 ALOGV("releaseLock"); 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 204 uint32_t flags = mPlayer->getSourceSeekFlags(); 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) { 229 ALOGV("loadEffectsSettings"); 230 return mPlayer->loadEffectsSettings(pEffectSettings, nEffects); 231 } 232 233 status_t VideoEditorPlayer::loadAudioMixSettings( 234 M4xVSS_AudioMixingSettings* pAudioMixSettings) { 235 ALOGV("VideoEditorPlayer: loadAudioMixSettings"); 236 return mPlayer->loadAudioMixSettings(pAudioMixSettings); 237 } 238 239 status_t VideoEditorPlayer::setAudioMixPCMFileHandle( 240 M4OSA_Context pAudioMixPCMFileHandle) { 241 242 ALOGV("VideoEditorPlayer: loadAudioMixSettings"); 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 251 ALOGV("VideoEditorPlayer: loadAudioMixSettings"); 252 return mPlayer->setAudioMixStoryBoardParam(audioMixStoryBoardTS, 253 currentMediaBeginCutTime, primaryTrackVolValue); 254 } 255 256 status_t VideoEditorPlayer::setPlaybackBeginTime(uint32_t msec) { 257 ALOGV("setPlaybackBeginTime"); 258 return mPlayer->setPlaybackBeginTime(msec); 259 } 260 261 status_t VideoEditorPlayer::setPlaybackEndTime(uint32_t msec) { 262 ALOGV("setPlaybackEndTime"); 263 return mPlayer->setPlaybackEndTime(msec); 264 } 265 266 status_t VideoEditorPlayer::setStoryboardStartTime(uint32_t msec) { 267 ALOGV("setStoryboardStartTime"); 268 return mPlayer->setStoryboardStartTime(msec); 269 } 270 271 status_t VideoEditorPlayer::setProgressCallbackInterval(uint32_t cbInterval) { 272 ALOGV("setProgressCallbackInterval"); 273 return mPlayer->setProgressCallbackInterval(cbInterval); 274 } 275 276 status_t VideoEditorPlayer::setMediaRenderingMode( 277 M4xVSS_MediaRendering mode, 278 M4VIDEOEDITING_VideoFrameSize outputVideoSize) { 279 280 ALOGV("setMediaRenderingMode"); 281 return mPlayer->setMediaRenderingMode(mode, outputVideoSize); 282 } 283 284 status_t VideoEditorPlayer::resetJniCallbackTimeStamp() { 285 ALOGV("resetJniCallbackTimeStamp"); 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) { 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 378 status_t VideoEditorPlayer::VeAudioOutput::getPosition(uint32_t *position) const { 379 380 if (mTrack == 0) return NO_INIT; 381 return mTrack->getPosition(position); 382 } 383 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 391 status_t VideoEditorPlayer::VeAudioOutput::open( 392 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, 393 audio_format_t format, int bufferCount, 394 AudioCallback cb, void *cookie, audio_output_flags_t flags, 395 const audio_offload_info_t *offloadInfo) { 396 397 mCallback = cb; 398 mCallbackCookie = cookie; 399 400 // Check argument "bufferCount" against the mininum buffer count 401 if (bufferCount < mMinBufferCount) { 402 ALOGV("bufferCount (%d) is too small and increased to %d", 403 bufferCount, mMinBufferCount); 404 bufferCount = mMinBufferCount; 405 406 } 407 ALOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount); 408 if (mTrack != 0) close(); 409 uint32_t afSampleRate; 410 size_t afFrameCount; 411 int frameCount; 412 413 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != 414 NO_ERROR) { 415 return NO_INIT; 416 } 417 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != 418 NO_ERROR) { 419 return NO_INIT; 420 } 421 422 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate; 423 424 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) { 425 switch(channelCount) { 426 case 1: 427 channelMask = AUDIO_CHANNEL_OUT_MONO; 428 break; 429 case 2: 430 channelMask = AUDIO_CHANNEL_OUT_STEREO; 431 break; 432 default: 433 return NO_INIT; 434 } 435 } 436 437 sp<AudioTrack> t; 438 if (mCallback != NULL) { 439 t = new AudioTrack( 440 mStreamType, 441 sampleRate, 442 format, 443 channelMask, 444 frameCount, 445 flags, 446 CallbackWrapper, 447 this); 448 } else { 449 t = new AudioTrack( 450 mStreamType, 451 sampleRate, 452 format, 453 channelMask, 454 frameCount, 455 flags); 456 } 457 458 if ((t == 0) || (t->initCheck() != NO_ERROR)) { 459 ALOGE("Unable to create audio track"); 460 return NO_INIT; 461 } 462 463 ALOGV("setVolume"); 464 t->setVolume(mLeftVolume, mRightVolume); 465 mMsecsPerFrame = 1.e3 / (float) sampleRate; 466 mLatency = t->latency(); 467 mTrack = t; 468 return NO_ERROR; 469 } 470 471 status_t VideoEditorPlayer::VeAudioOutput::start() { 472 473 ALOGV("start"); 474 if (mTrack != 0) { 475 mTrack->setVolume(mLeftVolume, mRightVolume); 476 status_t status = mTrack->start(); 477 if (status == NO_ERROR) { 478 mTrack->getPosition(&mNumFramesWritten); 479 } 480 return status; 481 } 482 return NO_INIT; 483 } 484 485 void VideoEditorPlayer::VeAudioOutput::snoopWrite( 486 const void* buffer, size_t size) { 487 // Visualization buffers not supported 488 return; 489 490 } 491 492 ssize_t VideoEditorPlayer::VeAudioOutput::write( 493 const void* buffer, size_t size) { 494 495 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback."); 496 497 //ALOGV("write(%p, %u)", buffer, size); 498 if (mTrack != 0) { 499 snoopWrite(buffer, size); 500 ssize_t ret = mTrack->write(buffer, size); 501 mNumFramesWritten += ret / 4; // assume 16 bit stereo 502 return ret; 503 } 504 return NO_INIT; 505 } 506 507 void VideoEditorPlayer::VeAudioOutput::stop() { 508 509 ALOGV("stop"); 510 if (mTrack != 0) mTrack->stop(); 511 } 512 513 void VideoEditorPlayer::VeAudioOutput::flush() { 514 515 ALOGV("flush"); 516 if (mTrack != 0) mTrack->flush(); 517 } 518 519 void VideoEditorPlayer::VeAudioOutput::pause() { 520 521 ALOGV("VeAudioOutput::pause"); 522 if (mTrack != 0) mTrack->pause(); 523 } 524 525 void VideoEditorPlayer::VeAudioOutput::close() { 526 527 ALOGV("close"); 528 mTrack.clear(); 529 } 530 531 void VideoEditorPlayer::VeAudioOutput::setVolume(float left, float right) { 532 533 ALOGV("setVolume(%f, %f)", left, right); 534 mLeftVolume = left; 535 mRightVolume = right; 536 if (mTrack != 0) { 537 mTrack->setVolume(left, right); 538 } 539 } 540 541 // static 542 void VideoEditorPlayer::VeAudioOutput::CallbackWrapper( 543 int event, void *cookie, void *info) { 544 //ALOGV("VeAudioOutput::callbackwrapper"); 545 if (event != AudioTrack::EVENT_MORE_DATA) { 546 return; 547 } 548 549 VeAudioOutput *me = (VeAudioOutput *)cookie; 550 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info; 551 552 size_t actualSize = (*me->mCallback)( 553 me, buffer->raw, buffer->size, me->mCallbackCookie, 554 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER); 555 556 buffer->size = actualSize; 557 558 if (actualSize > 0) { 559 me->snoopWrite(buffer->raw, actualSize); 560 } 561 } 562 563 status_t VideoEditorPlayer::VeAudioOutput::dump(int fd, const Vector<String16>& args) const 564 { 565 const size_t SIZE = 256; 566 char buffer[SIZE]; 567 String8 result; 568 569 result.append(" VeAudioOutput\n"); 570 snprintf(buffer, SIZE-1, " stream type(%d), left - right volume(%f, %f)\n", 571 mStreamType, mLeftVolume, mRightVolume); 572 result.append(buffer); 573 snprintf(buffer, SIZE-1, " msec per frame(%f), latency (%d)\n", 574 mMsecsPerFrame, mLatency); 575 result.append(buffer); 576 ::write(fd, result.string(), result.size()); 577 if (mTrack != 0) { 578 mTrack->dump(fd, args); 579 } 580 return NO_ERROR; 581 } 582 583 int VideoEditorPlayer::VeAudioOutput::getSessionId() const { 584 585 return mSessionId; 586 } 587 588 uint32_t VideoEditorPlayer::VeAudioOutput::getSampleRate() const { 589 if (mMsecsPerFrame == 0) { 590 return 0; 591 } 592 return (uint32_t)(1.e3 / mMsecsPerFrame); 593 } 594 595 } // namespace android 596