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
21#include <media/IMediaHTTPService.h>
22#include <media/IMediaPlayerService.h>
23
24#include "android_LocAVPlayer.h"
25
26#include "HTTPHelper.h"
27
28namespace android {
29
30//--------------------------------------------------------------------------------------------------
31LocAVPlayer::LocAVPlayer(const AudioPlayback_Parameters* params, bool hasVideo) :
32        GenericMediaPlayer(params, hasVideo)
33{
34    SL_LOGD("LocAVPlayer::LocAVPlayer()");
35
36}
37
38
39LocAVPlayer::~LocAVPlayer() {
40    SL_LOGD("LocAVPlayer::~LocAVPlayer()");
41
42}
43
44
45//--------------------------------------------------
46// Event handlers
47void LocAVPlayer::onPrepare() {
48    SL_LOGD("LocAVPlayer::onPrepare()");
49    sp<IMediaPlayerService> mediaPlayerService(getMediaPlayerService());
50    if (mediaPlayerService != NULL) {
51        switch (mDataLocatorType) {
52        case kDataLocatorUri:
53            mPlayer = mediaPlayerService->create(mPlayerClient /*IMediaPlayerClient*/,
54                    mPlaybackParams.sessionId);
55            if (mPlayer == NULL) {
56                SL_LOGE("media player service failed to create player by URI");
57            } else {
58                sp <IMediaHTTPService> mediaHTTPService;
59#ifndef __BRILLO__
60                // As Brillo doesn't have a Java layer, we don't have to call this
61                // function since it would return NULL anyways. Not having this
62                // function call allows us to significantly reduce the size of the
63                // Brillo checkout.
64                mediaHTTPService = CreateHTTPServiceInCurrentJavaContext();
65#endif
66                status_t status =  mPlayer->setDataSource(
67                    mediaHTTPService, mDataLocator.uriRef, NULL /*headers*/);
68                if (status != NO_ERROR) {
69                    SL_LOGE("setDataSource failed");
70                    mPlayer.clear();
71                }
72            }
73            break;
74        case kDataLocatorFd:
75            mPlayer = mediaPlayerService->create(mPlayerClient /*IMediaPlayerClient*/,
76                    mPlaybackParams.sessionId);
77            if (mPlayer == NULL) {
78                SL_LOGE("media player service failed to create player by FD");
79            } else if (mPlayer->setDataSource(mDataLocator.fdi.fd, mDataLocator.fdi.offset,
80                    mDataLocator.fdi.length) != NO_ERROR) {
81                SL_LOGE("setDataSource failed");
82                mPlayer.clear();
83            }
84            // Binder dups the fd for use by mediaserver, so if we own the fd then OK to close now
85            if (mDataLocator.fdi.mCloseAfterUse) {
86                (void) ::close(mDataLocator.fdi.fd);
87                mDataLocator.fdi.fd = -1;
88                mDataLocator.fdi.mCloseAfterUse = false;
89            }
90            break;
91        case kDataLocatorNone:
92            SL_LOGE("no data locator for MediaPlayer object");
93            break;
94        default:
95            SL_LOGE("unsupported data locator %d for MediaPlayer object", mDataLocatorType);
96            break;
97        }
98    }
99    if (mPlayer == NULL) {
100        mStateFlags |= kFlagPreparedUnsuccessfully;
101    }
102    // blocks until mPlayer is prepared
103    GenericMediaPlayer::onPrepare();
104    SL_LOGD("LocAVPlayer::onPrepare() done");
105}
106
107} // namespace android
108