ISeek.c revision e31a69fcbe27894a597176d05a08e04c06092021
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/* Seek implementation */
18
19#include "sles_allinclusive.h"
20
21
22static SLresult ISeek_SetPosition(SLSeekItf self, SLmillisecond pos, SLuint32 seekMode)
23{
24    SL_ENTER_INTERFACE
25
26    switch (seekMode) {
27    case SL_SEEKMODE_FAST:
28    case SL_SEEKMODE_ACCURATE:
29        {
30        // maximum position is a special value that indicates a seek is not pending
31        if (SL_TIME_UNKNOWN == pos) {
32            pos = SL_TIME_UNKNOWN - 1;
33        }
34        ISeek *thiz = (ISeek *) self;
35        interface_lock_exclusive(thiz);
36        thiz->mPos = pos;
37        // at this point the seek is merely pending, so do not yet update other fields
38        interface_unlock_exclusive_attributes(thiz, ATTR_POSITION);
39        result = SL_RESULT_SUCCESS;
40        }
41        break;
42    default:
43        result = SL_RESULT_PARAMETER_INVALID;
44        break;
45    }
46
47    SL_LEAVE_INTERFACE
48}
49
50
51static SLresult ISeek_SetLoop(SLSeekItf self, SLboolean loopEnable,
52    SLmillisecond startPos, SLmillisecond endPos)
53{
54    SL_ENTER_INTERFACE
55
56    if (!(startPos < endPos)) {
57        result = SL_RESULT_PARAMETER_INVALID;
58    } else {
59        ISeek *thiz = (ISeek *) self;
60        interface_lock_exclusive(thiz);
61#ifdef ANDROID
62        if ((startPos != 0) && (endPos != SL_TIME_UNKNOWN)) {
63            result = SL_RESULT_FEATURE_UNSUPPORTED;
64        } else {
65            switch (IObjectToObjectID((thiz)->mThis)) {
66              case SL_OBJECTID_AUDIOPLAYER: {
67                CAudioPlayer *ap = InterfaceToCAudioPlayer(thiz);
68                if (NULL != ap) {
69                    // FIXME should return a result
70                    android_audioPlayer_loop(ap, loopEnable);
71                    result = SL_RESULT_SUCCESS;
72                } else {
73                    result = SL_RESULT_PARAMETER_INVALID;
74                }
75                break;
76              }
77              case XA_OBJECTID_MEDIAPLAYER: {
78                CMediaPlayer *mp = InterfaceToCMediaPlayer(thiz);
79                if (NULL != mp) {
80                    result = android_Player_loop(mp, loopEnable);
81                } else {
82                    result = SL_RESULT_PARAMETER_INVALID;
83                }
84                break;
85              }
86              default: {
87                result = SL_RESULT_PARAMETER_INVALID;
88              }
89            }
90            if (SL_RESULT_SUCCESS == result) {
91                thiz->mLoopEnabled = SL_BOOLEAN_FALSE != loopEnable; // normalize
92                // start and end positions already initialized to [0, end of stream]
93                /*thiz->mStartPos = 0;
94                  thiz->mEndPos = (SLmillisecond) SL_TIME_UNKNOWN;*/
95            }
96        }
97#else
98        thiz->mLoopEnabled = SL_BOOLEAN_FALSE != loopEnable; // normalize
99        thiz->mStartPos = startPos;
100        thiz->mEndPos = endPos;
101        result = SL_RESULT_SUCCESS;
102#endif
103        interface_unlock_exclusive(thiz);
104    }
105
106    SL_LEAVE_INTERFACE
107}
108
109
110static SLresult ISeek_GetLoop(SLSeekItf self, SLboolean *pLoopEnabled,
111    SLmillisecond *pStartPos, SLmillisecond *pEndPos)
112{
113    SL_ENTER_INTERFACE
114
115    if (NULL == pLoopEnabled || NULL == pStartPos || NULL == pEndPos) {
116        result = SL_RESULT_PARAMETER_INVALID;
117    } else {
118        ISeek *thiz = (ISeek *) self;
119        interface_lock_shared(thiz);
120        SLboolean loopEnabled = thiz->mLoopEnabled;
121        SLmillisecond startPos = thiz->mStartPos;
122        SLmillisecond endPos = thiz->mEndPos;
123        interface_unlock_shared(thiz);
124        *pLoopEnabled = loopEnabled;
125        *pStartPos = startPos;
126        *pEndPos = endPos;
127        result = SL_RESULT_SUCCESS;
128    }
129
130    SL_LEAVE_INTERFACE
131}
132
133
134static const struct SLSeekItf_ ISeek_Itf = {
135    ISeek_SetPosition,
136    ISeek_SetLoop,
137    ISeek_GetLoop
138};
139
140void ISeek_init(void *self)
141{
142    ISeek *thiz = (ISeek *) self;
143    thiz->mItf = &ISeek_Itf;
144    thiz->mPos = (SLmillisecond) SL_TIME_UNKNOWN;
145    thiz->mStartPos = (SLmillisecond) 0;
146    thiz->mEndPos = (SLmillisecond) SL_TIME_UNKNOWN;
147    thiz->mLoopEnabled = SL_BOOLEAN_FALSE;
148}
149