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