android_LocAVPlayer.cpp revision bcc5c7225e3b7a1dbf2e9e830987f69167acf06f
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 USE_LOG SLAndroidLogLevel_Verbose 18 19#include "sles_allinclusive.h" 20#include <media/IMediaPlayerService.h> 21 22 23namespace android { 24 25//-------------------------------------------------------------------------------------------------- 26LocAVPlayer::LocAVPlayer(AudioPlayback_Parameters* params) : AVPlayer(params), 27 mDataLocatorType(kDataLocatorNone) 28{ 29 SL_LOGI("LocAVPlayer::LocAVPlayer()"); 30 31} 32 33 34LocAVPlayer::~LocAVPlayer() { 35 SL_LOGI("LocAVPlayer::~LocAVPlayer()"); 36 37 resetDataLocator(); 38} 39 40 41//-------------------------------------------------- 42// Event handlers 43void LocAVPlayer::onPrepare() { 44 SL_LOGI("LocAVPlayer::onPrepare()"); 45 Mutex::Autolock _l(mLock); 46 switch (mDataLocatorType) { 47 case kDataLocatorUri: 48 mPlayer = mMediaPlayerService->create(getpid(), mPlayerClient /*IMediaPlayerClient*/, 49 mDataLocator.uri /*url*/, NULL /*headers*/, mPlaybackParams.sessionId); 50 break; 51 case kDataLocatorFd: 52 mPlayer = mMediaPlayerService->create(getpid(), mPlayerClient /*IMediaPlayerClient*/, 53 mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length, 54 mPlaybackParams.sessionId); 55 break; 56 case kDataLocatorNone: 57 SL_LOGE("no data locator for MediaPlayer object"); 58 break; 59 default: 60 SL_LOGE("unsupported data locator %d for MediaPlayer object", mDataLocatorType); 61 break; 62 } 63 // blocks until mPlayer is prepared 64 AVPlayer::onPrepare(); 65 SL_LOGI("LocAVPlayer::onPrepare() done"); 66} 67 68 69//-------------------------------------------------- 70/* 71 * post-condition: mDataLocatorType == kDataLocatorNone 72 * 73 */ 74void LocAVPlayer::resetDataLocator() { 75 if (kDataLocatorUri == mDataLocatorType) { 76 if (NULL != mDataLocator.uri) { 77 free(mDataLocator.uri); 78 mDataLocator.uri = NULL; 79 } 80 } 81 mDataLocatorType = kDataLocatorNone; 82} 83 84 85void LocAVPlayer::setDataSource(const char *uri) { 86 resetDataLocator(); 87 88 // FIXME: a copy of the URI has already been made and is guaranteed to exist 89 // as long as the SLES/OMXAL object exists, so the copy here is not necessary 90 size_t len = strlen((const char *) uri); 91 char* newUri = (char*) malloc(len + 1); 92 if (NULL == newUri) { 93 // mem issue 94 SL_LOGE("LocAVPlayer::setDataSource: not enough memory to allocator URI string"); 95 return; 96 } 97 memcpy(newUri, uri, len + 1); 98 mDataLocator.uri = newUri; 99 100 mDataLocatorType = kDataLocatorUri; 101} 102 103 104void LocAVPlayer::setDataSource(const int fd, const int64_t offset, const int64_t length) { 105 resetDataLocator(); 106 107 mDataLocator.fdi.fd = fd; 108 109 struct stat sb; 110 int ret = fstat(fd, &sb); 111 if (ret != 0) { 112 SL_LOGE("LocAVPlayer::setDataSource: fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); 113 return; 114 } 115 116 if (offset >= sb.st_size) { 117 SL_LOGE("SfPlayer::setDataSource: invalid offset"); 118 return; 119 } 120 mDataLocator.fdi.offset = offset; 121 122 if (PLAYER_FD_FIND_FILE_SIZE == length) { 123 mDataLocator.fdi.length = sb.st_size; 124 } else if (offset + length > sb.st_size) { 125 mDataLocator.fdi.length = sb.st_size - offset; 126 } else { 127 mDataLocator.fdi.length = length; 128 } 129 130 mDataLocatorType = kDataLocatorFd; 131} 132 133} // namespace android 134