android_LocAVPlayer.cpp revision 3376429dfe306b9bfebf3a50941e2ccf44984495
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#include "android_LocAVPlayer.h"
22
23
24namespace android {
25
26//--------------------------------------------------------------------------------------------------
27LocAVPlayer::LocAVPlayer(const AudioPlayback_Parameters* params, bool hasVideo) :
28        GenericMediaPlayer(params, hasVideo)
29{
30    SL_LOGD("LocAVPlayer::LocAVPlayer()");
31
32}
33
34
35LocAVPlayer::~LocAVPlayer() {
36    SL_LOGD("LocAVPlayer::~LocAVPlayer()");
37
38}
39
40
41//--------------------------------------------------
42// Event handlers
43void LocAVPlayer::onPrepare() {
44    SL_LOGD("LocAVPlayer::onPrepare()");
45    sp<IMediaPlayerService> mediaPlayerService(getMediaPlayerService());
46    if (mediaPlayerService != NULL) {
47        switch (mDataLocatorType) {
48        case kDataLocatorUri:
49            mPlayer = mediaPlayerService->create(mPlayerClient /*IMediaPlayerClient*/,
50                    mPlaybackParams.sessionId);
51            if (mPlayer == NULL) {
52                SL_LOGE("media player service failed to create player by URI");
53            } else if (mPlayer->setDataSource(mDataLocator.uriRef, NULL /*headers*/) != NO_ERROR) {
54                SL_LOGE("setDataSource failed");
55                mPlayer.clear();
56            }
57            break;
58        case kDataLocatorFd:
59            mPlayer = mediaPlayerService->create(mPlayerClient /*IMediaPlayerClient*/,
60                    mPlaybackParams.sessionId);
61            if (mPlayer == NULL) {
62                SL_LOGE("media player service failed to create player by FD");
63            } else if (mPlayer->setDataSource(mDataLocator.fdi.fd, mDataLocator.fdi.offset,
64                    mDataLocator.fdi.length) != NO_ERROR) {
65                SL_LOGE("setDataSource failed");
66                mPlayer.clear();
67            }
68            // Binder dups the fd for use by mediaserver, so if we own the fd then OK to close now
69            if (mDataLocator.fdi.mCloseAfterUse) {
70                (void) ::close(mDataLocator.fdi.fd);
71                mDataLocator.fdi.fd = -1;
72                mDataLocator.fdi.mCloseAfterUse = false;
73            }
74            break;
75        case kDataLocatorNone:
76            SL_LOGE("no data locator for MediaPlayer object");
77            break;
78        default:
79            SL_LOGE("unsupported data locator %d for MediaPlayer object", mDataLocatorType);
80            break;
81        }
82    }
83    if (mPlayer == NULL) {
84        mStateFlags |= kFlagPreparedUnsuccessfully;
85    }
86    // blocks until mPlayer is prepared
87    GenericMediaPlayer::onPrepare();
88    SL_LOGD("LocAVPlayer::onPrepare() done");
89}
90
91} // namespace android
92