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