IPlay.c revision 35a5a30fdad179ccf38d8d756590411326159a89
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/* Play implementation */
18
19#include "sles_allinclusive.h"
20
21
22static SLresult IPlay_SetPlayState(SLPlayItf self, SLuint32 state)
23{
24    SL_ENTER_INTERFACE
25
26    switch (state) {
27    case SL_PLAYSTATE_STOPPED:
28    case SL_PLAYSTATE_PAUSED:
29    case SL_PLAYSTATE_PLAYING:
30        {
31        IPlay *thiz = (IPlay *) self;
32        unsigned attr = ATTR_NONE;
33        result = SL_RESULT_SUCCESS;
34#ifdef USE_OUTPUTMIXEXT
35        CAudioPlayer *audioPlayer = (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) ?
36            (CAudioPlayer *) thiz->mThis : NULL;
37#endif
38        interface_lock_exclusive(thiz);
39        SLuint32 oldState = thiz->mState;
40        if (state != oldState) {
41#ifdef USE_OUTPUTMIXEXT
42          for (;; interface_cond_wait(thiz)) {
43
44            // We are comparing the old state (left) vs. new state (right).
45            // Note that the old state is 3 bits wide, but new state is only 2 bits wide.
46            // That is why the old state is on the left and new state is on the right.
47            switch ((oldState << 2) | state) {
48
49            case (SL_PLAYSTATE_STOPPED  << 2) | SL_PLAYSTATE_STOPPED:
50            case (SL_PLAYSTATE_PAUSED   << 2) | SL_PLAYSTATE_PAUSED:
51            case (SL_PLAYSTATE_PLAYING  << 2) | SL_PLAYSTATE_PLAYING:
52               // no-op, and unreachable due to earlier "if (state != oldState)"
53                break;
54
55            case (SL_PLAYSTATE_STOPPED  << 2) | SL_PLAYSTATE_PLAYING:
56            case (SL_PLAYSTATE_PAUSED   << 2) | SL_PLAYSTATE_PLAYING:
57                attr = ATTR_PLAY_STATE;
58                // set enqueue attribute if queue is non-empty and state becomes PLAYING
59                if ((NULL != audioPlayer) && (audioPlayer->mBufferQueue.mFront !=
60                    audioPlayer->mBufferQueue.mRear)) {
61                    // note that USE_OUTPUTMIXEXT does not support ATTR_ABQ_ENQUEUE
62                    attr |= ATTR_BQ_ENQUEUE;
63                }
64                // fall through
65
66            case (SL_PLAYSTATE_STOPPED  << 2) | SL_PLAYSTATE_PAUSED:
67            case (SL_PLAYSTATE_PLAYING  << 2) | SL_PLAYSTATE_PAUSED:
68                // easy
69                thiz->mState = state;
70                break;
71
72            case (SL_PLAYSTATE_STOPPING << 2) | SL_PLAYSTATE_STOPPED:
73                // either spurious wakeup, or someone else requested same transition
74                continue;
75
76            case (SL_PLAYSTATE_STOPPING << 2) | SL_PLAYSTATE_PAUSED:
77            case (SL_PLAYSTATE_STOPPING << 2) | SL_PLAYSTATE_PLAYING:
78                // wait for other guy to finish his transition, then retry ours
79                continue;
80
81            case (SL_PLAYSTATE_PAUSED   << 2) | SL_PLAYSTATE_STOPPED:
82            case (SL_PLAYSTATE_PLAYING  << 2) | SL_PLAYSTATE_STOPPED:
83                // tell mixer to stop, then wait for mixer to acknowledge the request to stop
84                thiz->mState = SL_PLAYSTATE_STOPPING;
85                continue;
86
87            default:
88                // unexpected state
89                assert(SL_BOOLEAN_FALSE);
90                result = SL_RESULT_INTERNAL_ERROR;
91                break;
92
93            }
94
95            break;
96          }
97#else
98          // Here life looks easy for an Android, but there are other troubles in play land
99          thiz->mState = state;
100          attr = ATTR_PLAY_STATE;
101          // no need to set ATTR_BQ_ENQUEUE or ATTR_ABQ_ENQUEUE
102#endif
103        }
104        // SL_LOGD("set play state %d", state);
105        interface_unlock_exclusive_attributes(thiz, attr);
106        }
107        break;
108    default:
109        result = SL_RESULT_PARAMETER_INVALID;
110        break;
111    }
112
113    SL_LEAVE_INTERFACE
114}
115
116
117static SLresult IPlay_GetPlayState(SLPlayItf self, SLuint32 *pState)
118{
119    SL_ENTER_INTERFACE
120
121    if (NULL == pState) {
122        result = SL_RESULT_PARAMETER_INVALID;
123    } else {
124        IPlay *thiz = (IPlay *) self;
125        interface_lock_peek(thiz);
126        SLuint32 state = thiz->mState;
127        interface_unlock_peek(thiz);
128        result = SL_RESULT_SUCCESS;
129#ifdef USE_OUTPUTMIXEXT
130        switch (state) {
131        case SL_PLAYSTATE_STOPPED:  // as is
132        case SL_PLAYSTATE_PAUSED:
133        case SL_PLAYSTATE_PLAYING:
134            break;
135        case SL_PLAYSTATE_STOPPING: // these states require re-mapping
136            state = SL_PLAYSTATE_STOPPED;
137            break;
138        default:                    // impossible
139            assert(SL_BOOLEAN_FALSE);
140            state = SL_PLAYSTATE_STOPPED;
141            result = SL_RESULT_INTERNAL_ERROR;
142            break;
143        }
144#endif
145        *pState = state;
146    }
147
148    SL_LEAVE_INTERFACE
149}
150
151
152static SLresult IPlay_GetDuration(SLPlayItf self, SLmillisecond *pMsec)
153{
154    SL_ENTER_INTERFACE
155
156    if (NULL == pMsec) {
157        result = SL_RESULT_PARAMETER_INVALID;
158    } else {
159        result = SL_RESULT_SUCCESS;
160        IPlay *thiz = (IPlay *) self;
161        // even though this is a getter, it can modify state due to caching
162        interface_lock_exclusive(thiz);
163        SLmillisecond duration = thiz->mDuration;
164#ifdef ANDROID
165        if ((SL_TIME_UNKNOWN == duration) &&
166            (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz))) {
167            SLmillisecond temp;
168            result = android_audioPlayer_getDuration(thiz, &temp);
169            if (SL_RESULT_SUCCESS == result) {
170                duration = temp;
171                thiz->mDuration = duration;
172            }
173        }
174        //FIXME support GetDuration for XA_OBJECTID_MEDIAPLAYER
175#else
176        // will be set by containing AudioPlayer or MidiPlayer object at Realize, if known,
177        // otherwise the duration will be updated each time a new maximum position is detected
178#endif
179        interface_unlock_exclusive(thiz);
180        *pMsec = duration;
181    }
182
183    SL_LEAVE_INTERFACE
184}
185
186
187static SLresult IPlay_GetPosition(SLPlayItf self, SLmillisecond *pMsec)
188{
189    SL_ENTER_INTERFACE
190
191    if (NULL == pMsec) {
192        result = SL_RESULT_PARAMETER_INVALID;
193    } else {
194        IPlay *thiz = (IPlay *) self;
195        SLmillisecond position;
196        interface_lock_shared(thiz);
197#ifdef ANDROID
198        // Android does not use the mPosition field for audio and media players
199        //  and doesn't cache the position
200        switch (IObjectToObjectID((thiz)->mThis)) {
201          case SL_OBJECTID_AUDIOPLAYER:
202            android_audioPlayer_getPosition(thiz, &position);
203            break;
204          case XA_OBJECTID_MEDIAPLAYER:
205            android_Player_getPosition(thiz, &position);
206            break;
207          default:
208            // we shouldn'be here
209            assert(SL_BOOLEAN_FALSE);
210        }
211#else
212        // on other platforms we depend on periodic updates to the current position
213        position = thiz->mPosition;
214        // if a seek is pending, then lie about current position so the seek appears synchronous
215        if (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) {
216            CAudioPlayer *audioPlayer = (CAudioPlayer *) thiz->mThis;
217            SLmillisecond pos = audioPlayer->mSeek.mPos;
218            if (SL_TIME_UNKNOWN != pos) {
219                position = pos;
220            }
221        }
222#endif
223        interface_unlock_shared(thiz);
224        *pMsec = position;
225        result = SL_RESULT_SUCCESS;
226    }
227
228    SL_LEAVE_INTERFACE
229}
230
231
232static SLresult IPlay_RegisterCallback(SLPlayItf self, slPlayCallback callback, void *pContext)
233{
234    SL_ENTER_INTERFACE
235
236    IPlay *thiz = (IPlay *) self;
237    interface_lock_exclusive(thiz);
238    thiz->mCallback = callback;
239    thiz->mContext = pContext;
240    // omits _attributes b/c noone cares deeply enough about these fields to need quick notification
241    interface_unlock_exclusive(thiz);
242    result = SL_RESULT_SUCCESS;
243
244    SL_LEAVE_INTERFACE
245}
246
247
248static SLresult IPlay_SetCallbackEventsMask(SLPlayItf self, SLuint32 eventFlags)
249{
250    SL_ENTER_INTERFACE
251
252    if (eventFlags & ~(SL_PLAYEVENT_HEADATEND | SL_PLAYEVENT_HEADATMARKER |
253            SL_PLAYEVENT_HEADATNEWPOS | SL_PLAYEVENT_HEADMOVING | SL_PLAYEVENT_HEADSTALLED)) {
254        result = SL_RESULT_PARAMETER_INVALID;
255    } else {
256        IPlay *thiz = (IPlay *) self;
257        interface_lock_exclusive(thiz);
258        if (thiz->mEventFlags != eventFlags) {
259#ifdef USE_OUTPUTMIXEXT
260            // enabling the "head at new position" play event will postpone the next update event
261            if (!(thiz->mEventFlags & SL_PLAYEVENT_HEADATNEWPOS) &&
262                    (eventFlags & SL_PLAYEVENT_HEADATNEWPOS)) {
263                thiz->mFramesSincePositionUpdate = 0;
264            }
265#endif
266            thiz->mEventFlags = eventFlags;
267            interface_unlock_exclusive_attributes(thiz, ATTR_TRANSPORT);
268        } else
269            interface_unlock_exclusive(thiz);
270        result = SL_RESULT_SUCCESS;
271    }
272
273    SL_LEAVE_INTERFACE
274}
275
276
277static SLresult IPlay_GetCallbackEventsMask(SLPlayItf self, SLuint32 *pEventFlags)
278{
279    SL_ENTER_INTERFACE
280
281    if (NULL == pEventFlags) {
282        result = SL_RESULT_PARAMETER_INVALID;
283    } else {
284        IPlay *thiz = (IPlay *) self;
285        interface_lock_peek(thiz);
286        SLuint32 eventFlags = thiz->mEventFlags;
287        interface_unlock_peek(thiz);
288        *pEventFlags = eventFlags;
289        result = SL_RESULT_SUCCESS;
290    }
291
292    SL_LEAVE_INTERFACE
293}
294
295
296static SLresult IPlay_SetMarkerPosition(SLPlayItf self, SLmillisecond mSec)
297{
298    SL_ENTER_INTERFACE
299
300    IPlay *thiz = (IPlay *) self;
301    interface_lock_exclusive(thiz);
302    if (thiz->mMarkerPosition != mSec) {
303        thiz->mMarkerPosition = mSec;
304        interface_unlock_exclusive_attributes(thiz, ATTR_TRANSPORT);
305    } else
306        interface_unlock_exclusive(thiz);
307    result = SL_RESULT_SUCCESS;
308
309    SL_LEAVE_INTERFACE
310}
311
312
313static SLresult IPlay_ClearMarkerPosition(SLPlayItf self)
314{
315    SL_ENTER_INTERFACE
316
317    IPlay *thiz = (IPlay *) self;
318    interface_lock_exclusive(thiz);
319#ifdef ANDROID
320    if (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) {
321        // clearing the marker position is equivalent to setting the marker at 0
322        thiz->mMarkerPosition = 0;
323    }
324#endif
325    interface_unlock_exclusive_attributes(thiz, ATTR_TRANSPORT);
326    result = SL_RESULT_SUCCESS;
327
328    SL_LEAVE_INTERFACE
329}
330
331
332static SLresult IPlay_GetMarkerPosition(SLPlayItf self, SLmillisecond *pMsec)
333{
334    SL_ENTER_INTERFACE
335
336    if (NULL == pMsec) {
337        result = SL_RESULT_PARAMETER_INVALID;
338    } else {
339        IPlay *thiz = (IPlay *) self;
340        interface_lock_peek(thiz);
341        SLmillisecond markerPosition = thiz->mMarkerPosition;
342        interface_unlock_peek(thiz);
343        *pMsec = markerPosition;
344        result = SL_RESULT_SUCCESS;
345    }
346
347    SL_LEAVE_INTERFACE
348}
349
350
351static SLresult IPlay_SetPositionUpdatePeriod(SLPlayItf self, SLmillisecond mSec)
352{
353    SL_ENTER_INTERFACE
354
355    if (0 == mSec) {
356        result = SL_RESULT_PARAMETER_INVALID;
357    } else {
358        IPlay *thiz = (IPlay *) self;
359        interface_lock_exclusive(thiz);
360        if (thiz->mPositionUpdatePeriod != mSec) {
361            thiz->mPositionUpdatePeriod = mSec;
362#ifdef ANDROID
363            if (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) {
364                // result = android_audioPlayer_useEventMask(thiz, thiz->mEventFlags);
365            }
366#endif
367#ifdef USE_OUTPUTMIXEXT
368            if (SL_OBJECTID_AUDIOPLAYER == InterfaceToObjectID(thiz)) {
369                CAudioPlayer *audioPlayer = (CAudioPlayer *) thiz->mThis;
370                SLuint32 frameUpdatePeriod = ((long long) mSec *
371                    (long long) audioPlayer->mSampleRateMilliHz) / 1000000LL;
372                if (0 == frameUpdatePeriod)
373                    frameUpdatePeriod = ~0;
374                thiz->mFrameUpdatePeriod = frameUpdatePeriod;
375                // setting a new update period postpones the next callback
376                thiz->mFramesSincePositionUpdate = 0;
377            }
378#endif
379            interface_unlock_exclusive_attributes(thiz, ATTR_TRANSPORT);
380        } else
381            interface_unlock_exclusive(thiz);
382        result = SL_RESULT_SUCCESS;
383    }
384
385    SL_LEAVE_INTERFACE
386}
387
388
389static SLresult IPlay_GetPositionUpdatePeriod(SLPlayItf self, SLmillisecond *pMsec)
390{
391    SL_ENTER_INTERFACE
392
393    if (NULL == pMsec) {
394        result = SL_RESULT_PARAMETER_INVALID;
395    } else {
396        IPlay *thiz = (IPlay *) self;
397        interface_lock_peek(thiz);
398        SLmillisecond positionUpdatePeriod = thiz->mPositionUpdatePeriod;
399        interface_unlock_peek(thiz);
400        *pMsec = positionUpdatePeriod;
401        result = SL_RESULT_SUCCESS;
402    }
403
404    SL_LEAVE_INTERFACE
405}
406
407
408static const struct SLPlayItf_ IPlay_Itf = {
409    IPlay_SetPlayState,
410    IPlay_GetPlayState,
411    IPlay_GetDuration,
412    IPlay_GetPosition,
413    IPlay_RegisterCallback,
414    IPlay_SetCallbackEventsMask,
415    IPlay_GetCallbackEventsMask,
416    IPlay_SetMarkerPosition,
417    IPlay_ClearMarkerPosition,
418    IPlay_GetMarkerPosition,
419    IPlay_SetPositionUpdatePeriod,
420    IPlay_GetPositionUpdatePeriod
421};
422
423void IPlay_init(void *self)
424{
425    IPlay *thiz = (IPlay *) self;
426    thiz->mItf = &IPlay_Itf;
427    thiz->mState = SL_PLAYSTATE_STOPPED;
428    thiz->mDuration = SL_TIME_UNKNOWN;  // will be set by containing player object
429    thiz->mPosition = (SLmillisecond) 0;
430    thiz->mCallback = NULL;
431    thiz->mContext = NULL;
432    thiz->mEventFlags = 0;
433    thiz->mMarkerPosition = 0;
434    thiz->mPositionUpdatePeriod = 1000;
435#ifdef USE_OUTPUTMIXEXT
436    thiz->mFrameUpdatePeriod = 0;   // because we don't know the sample rate yet
437    thiz->mLastSeekPosition = 0;
438    thiz->mFramesSinceLastSeek = 0;
439    thiz->mFramesSincePositionUpdate = 0;
440#endif
441}
442