NuPlayerDriver.cpp revision 1173118eace0e9e347cb007f0da817cee87579ed
1/*
2 * Copyright (C) 2010 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 LOG_NDEBUG 0
18#define LOG_TAG "NuPlayerDriver"
19#include <utils/Log.h>
20
21#include "NuPlayerDriver.h"
22
23#include "NuPlayer.h"
24
25#include <media/stagefright/foundation/ADebug.h>
26#include <media/stagefright/foundation/ALooper.h>
27
28namespace android {
29
30NuPlayerDriver::NuPlayerDriver()
31    : mResetInProgress(false),
32      mDurationUs(-1),
33      mPositionUs(-1),
34      mLooper(new ALooper),
35      mState(UNINITIALIZED),
36      mStartupSeekTimeUs(-1) {
37    mLooper->setName("NuPlayerDriver Looper");
38
39    mLooper->start(
40            false, /* runOnCallingThread */
41            true,  /* canCallJava */
42            PRIORITY_AUDIO);
43
44    mPlayer = new NuPlayer;
45    mLooper->registerHandler(mPlayer);
46
47    mPlayer->setDriver(this);
48}
49
50NuPlayerDriver::~NuPlayerDriver() {
51    mLooper->stop();
52}
53
54status_t NuPlayerDriver::initCheck() {
55    return OK;
56}
57
58status_t NuPlayerDriver::setDataSource(
59        const char *url, const KeyedVector<String8, String8> *headers) {
60    CHECK_EQ((int)mState, (int)UNINITIALIZED);
61
62    mPlayer->setDataSource(url, headers);
63
64    mState = STOPPED;
65
66    return OK;
67}
68
69status_t NuPlayerDriver::setDataSource(int fd, int64_t offset, int64_t length) {
70    return INVALID_OPERATION;
71}
72
73status_t NuPlayerDriver::setDataSource(const sp<IStreamSource> &source) {
74    CHECK_EQ((int)mState, (int)UNINITIALIZED);
75
76    mPlayer->setDataSource(source);
77
78    mState = STOPPED;
79
80    return OK;
81}
82
83status_t NuPlayerDriver::setVideoSurface(const sp<Surface> &surface) {
84    mPlayer->setVideoSurface(surface);
85
86    return OK;
87}
88
89status_t NuPlayerDriver::setVideoSurfaceTexture(
90        const sp<ISurfaceTexture> &surfaceTexture) {
91    mPlayer->setVideoSurfaceTexture(surfaceTexture);
92
93    return OK;
94}
95
96status_t NuPlayerDriver::prepare() {
97    return OK;
98}
99
100status_t NuPlayerDriver::prepareAsync() {
101    sendEvent(MEDIA_PREPARED);
102
103    return OK;
104}
105
106status_t NuPlayerDriver::start() {
107    switch (mState) {
108        case UNINITIALIZED:
109            return INVALID_OPERATION;
110        case STOPPED:
111        {
112            mPlayer->start();
113
114            if (mStartupSeekTimeUs >= 0) {
115                mPlayer->seekToAsync(mStartupSeekTimeUs);
116                mStartupSeekTimeUs = -1;
117            }
118            break;
119        }
120        case PLAYING:
121            return OK;
122        default:
123        {
124            CHECK_EQ((int)mState, (int)PAUSED);
125
126            mPlayer->resume();
127            break;
128        }
129    }
130
131    mState = PLAYING;
132
133    return OK;
134}
135
136status_t NuPlayerDriver::stop() {
137    return pause();
138}
139
140status_t NuPlayerDriver::pause() {
141    switch (mState) {
142        case UNINITIALIZED:
143            return INVALID_OPERATION;
144        case STOPPED:
145            return OK;
146        case PLAYING:
147            mPlayer->pause();
148            break;
149        default:
150        {
151            CHECK_EQ((int)mState, (int)PAUSED);
152            return OK;
153        }
154    }
155
156    mState = PAUSED;
157
158    return OK;
159}
160
161bool NuPlayerDriver::isPlaying() {
162    return mState == PLAYING;
163}
164
165status_t NuPlayerDriver::seekTo(int msec) {
166    int64_t seekTimeUs = msec * 1000ll;
167
168    switch (mState) {
169        case UNINITIALIZED:
170            return INVALID_OPERATION;
171        case STOPPED:
172        {
173            mStartupSeekTimeUs = seekTimeUs;
174            break;
175        }
176        case PLAYING:
177        case PAUSED:
178        {
179            mPlayer->seekToAsync(seekTimeUs);
180            break;
181        }
182
183        default:
184            TRESPASS();
185            break;
186    }
187
188    return OK;
189}
190
191status_t NuPlayerDriver::getCurrentPosition(int *msec) {
192    Mutex::Autolock autoLock(mLock);
193
194    if (mPositionUs < 0) {
195        *msec = 0;
196    } else {
197        *msec = (mPositionUs + 500ll) / 1000;
198    }
199
200    return OK;
201}
202
203status_t NuPlayerDriver::getDuration(int *msec) {
204    Mutex::Autolock autoLock(mLock);
205
206    if (mDurationUs < 0) {
207        *msec = 0;
208    } else {
209        *msec = (mDurationUs + 500ll) / 1000;
210    }
211
212    return OK;
213}
214
215status_t NuPlayerDriver::reset() {
216    Mutex::Autolock autoLock(mLock);
217    mResetInProgress = true;
218
219    mPlayer->resetAsync();
220
221    while (mResetInProgress) {
222        mCondition.wait(mLock);
223    }
224
225    mDurationUs = -1;
226    mPositionUs = -1;
227    mState = UNINITIALIZED;
228    mStartupSeekTimeUs = -1;
229
230    return OK;
231}
232
233status_t NuPlayerDriver::setLooping(int loop) {
234    return INVALID_OPERATION;
235}
236
237player_type NuPlayerDriver::playerType() {
238    return NU_PLAYER;
239}
240
241status_t NuPlayerDriver::invoke(const Parcel &request, Parcel *reply) {
242    return INVALID_OPERATION;
243}
244
245void NuPlayerDriver::setAudioSink(const sp<AudioSink> &audioSink) {
246    mPlayer->setAudioSink(audioSink);
247}
248
249status_t NuPlayerDriver::getMetadata(
250        const media::Metadata::Filter& ids, Parcel *records) {
251    return INVALID_OPERATION;
252}
253
254void NuPlayerDriver::notifyResetComplete() {
255    Mutex::Autolock autoLock(mLock);
256    CHECK(mResetInProgress);
257    mResetInProgress = false;
258    mCondition.broadcast();
259}
260
261void NuPlayerDriver::notifyDuration(int64_t durationUs) {
262    Mutex::Autolock autoLock(mLock);
263    mDurationUs = durationUs;
264}
265
266void NuPlayerDriver::notifyPosition(int64_t positionUs) {
267    Mutex::Autolock autoLock(mLock);
268    mPositionUs = positionUs;
269}
270
271void NuPlayerDriver::notifySeekComplete() {
272    sendEvent(MEDIA_SEEK_COMPLETE);
273}
274
275}  // namespace android
276