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/* 3DDoppler implementation */
18
19#include "sles_allinclusive.h"
20
21
22static SLresult I3DDoppler_SetVelocityCartesian(SL3DDopplerItf self, const SLVec3D *pVelocity)
23{
24    SL_ENTER_INTERFACE
25
26    if (NULL == pVelocity) {
27        result = SL_RESULT_PARAMETER_INVALID;
28    } else {
29        I3DDoppler *thiz = (I3DDoppler *) self;
30        SLVec3D velocityCartesian = *pVelocity;
31        interface_lock_exclusive(thiz);
32        thiz->mVelocityCartesian = velocityCartesian;
33        thiz->mVelocityActive = CARTESIAN_SET_SPHERICAL_UNKNOWN;
34        interface_unlock_exclusive(thiz);
35        result = SL_RESULT_SUCCESS;
36    }
37
38    SL_LEAVE_INTERFACE
39}
40
41
42static SLresult I3DDoppler_SetVelocitySpherical(SL3DDopplerItf self,
43    SLmillidegree azimuth, SLmillidegree elevation, SLmillimeter speed)
44{
45    SL_ENTER_INTERFACE
46
47    I3DDoppler *thiz = (I3DDoppler *) self;
48    interface_lock_exclusive(thiz);
49    thiz->mVelocitySpherical.mAzimuth = azimuth;
50    thiz->mVelocitySpherical.mElevation = elevation;
51    thiz->mVelocitySpherical.mSpeed = speed;
52    thiz->mVelocityActive = CARTESIAN_UNKNOWN_SPHERICAL_SET;
53    interface_unlock_exclusive(thiz);
54    result = SL_RESULT_SUCCESS;
55
56    SL_LEAVE_INTERFACE
57}
58
59
60static SLresult I3DDoppler_GetVelocityCartesian(SL3DDopplerItf self, SLVec3D *pVelocity)
61{
62    SL_ENTER_INTERFACE
63
64    if (NULL == pVelocity) {
65        result = SL_RESULT_PARAMETER_INVALID;
66    } else {
67        I3DDoppler *thiz = (I3DDoppler *) self;
68        interface_lock_exclusive(thiz);
69        for (;;) {
70            enum CartesianSphericalActive velocityActive = thiz->mVelocityActive;
71            switch (velocityActive) {
72            case CARTESIAN_COMPUTED_SPHERICAL_SET:
73            case CARTESIAN_SET_SPHERICAL_COMPUTED:  // not in 1.0.1
74            case CARTESIAN_SET_SPHERICAL_REQUESTED: // not in 1.0.1
75            case CARTESIAN_SET_SPHERICAL_UNKNOWN:
76                {
77                SLVec3D velocityCartesian = thiz->mVelocityCartesian;
78                interface_unlock_exclusive(thiz);
79                *pVelocity = velocityCartesian;
80                }
81                break;
82            case CARTESIAN_UNKNOWN_SPHERICAL_SET:
83                thiz->mVelocityActive = CARTESIAN_REQUESTED_SPHERICAL_SET;
84                // fall through
85            case CARTESIAN_REQUESTED_SPHERICAL_SET:
86                // matched by cond_broadcast in case multiple requesters
87#if 0
88                interface_cond_wait(thiz);
89#else
90                thiz->mVelocityActive = CARTESIAN_COMPUTED_SPHERICAL_SET;
91#endif
92                continue;
93            default:
94                assert(SL_BOOLEAN_FALSE);
95                interface_unlock_exclusive(thiz);
96                pVelocity->x = 0;
97                pVelocity->y = 0;
98                pVelocity->z = 0;
99                break;
100            }
101            break;
102        }
103        result = SL_RESULT_SUCCESS;
104    }
105
106    SL_LEAVE_INTERFACE
107}
108
109
110static SLresult I3DDoppler_SetDopplerFactor(SL3DDopplerItf self, SLpermille dopplerFactor)
111{
112    SL_ENTER_INTERFACE
113
114    I3DDoppler *thiz = (I3DDoppler *) self;
115    interface_lock_poke(thiz);
116    thiz->mDopplerFactor = dopplerFactor;
117    interface_unlock_poke(thiz);
118    result = SL_RESULT_SUCCESS;
119
120    SL_LEAVE_INTERFACE
121}
122
123
124static SLresult I3DDoppler_GetDopplerFactor(SL3DDopplerItf self, SLpermille *pDopplerFactor)
125{
126    SL_ENTER_INTERFACE
127
128    if (NULL == pDopplerFactor) {
129        result = SL_RESULT_PARAMETER_INVALID;
130    } else {
131        I3DDoppler *thiz = (I3DDoppler *) self;
132        interface_lock_peek(thiz);
133        SLpermille dopplerFactor = thiz->mDopplerFactor;
134        interface_unlock_peek(thiz);
135        *pDopplerFactor = dopplerFactor;
136        result = SL_RESULT_SUCCESS;
137    }
138
139    SL_LEAVE_INTERFACE
140}
141
142
143static const struct SL3DDopplerItf_ I3DDoppler_Itf = {
144    I3DDoppler_SetVelocityCartesian,
145    I3DDoppler_SetVelocitySpherical,
146    I3DDoppler_GetVelocityCartesian,
147    I3DDoppler_SetDopplerFactor,
148    I3DDoppler_GetDopplerFactor
149};
150
151void I3DDoppler_init(void *self)
152{
153    I3DDoppler *thiz = (I3DDoppler *) self;
154    thiz->mItf = &I3DDoppler_Itf;
155    thiz->mVelocityCartesian.x = 0;
156    thiz->mVelocityCartesian.y = 0;
157    thiz->mVelocityCartesian.z = 0;
158    memset(&thiz->mVelocitySpherical, 0x55, sizeof(thiz->mVelocitySpherical));
159    thiz->mVelocityActive = CARTESIAN_SET_SPHERICAL_UNKNOWN;
160    thiz->mDopplerFactor = 1000;
161}
162