summaryrefslogtreecommitdiffstats
path: root/src/itf/IVolume.c
blob: aaef4a99b6cd8273c277ee669f678164f605c159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Volume implementation */

#include "sles_allinclusive.h"


static SLresult IVolume_SetVolumeLevel(SLVolumeItf self, SLmillibel level)
{
    SL_ENTER_INTERFACE

    if (!((SL_MILLIBEL_MIN <= level) && (level <= PLATFORM_MILLIBEL_MAX_VOLUME))) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IVolume *thiz = (IVolume *) self;
        interface_lock_exclusive(thiz);
        SLmillibel oldLevel = thiz->mLevel;
        if (oldLevel != level) {
            thiz->mLevel = level;
            interface_unlock_exclusive_attributes(thiz, ATTR_GAIN);
        } else {
            interface_unlock_exclusive(thiz);
        }
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_GetVolumeLevel(SLVolumeItf self, SLmillibel *pLevel)
{
    SL_ENTER_INTERFACE

    if (NULL == pLevel) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IVolume *thiz = (IVolume *) self;
        interface_lock_shared(thiz);
        SLmillibel level = thiz->mLevel;
        interface_unlock_shared(thiz);
        *pLevel = level;
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_GetMaxVolumeLevel(SLVolumeItf self, SLmillibel *pMaxLevel)
{
    SL_ENTER_INTERFACE

    if (NULL == pMaxLevel) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        *pMaxLevel = PLATFORM_MILLIBEL_MAX_VOLUME;
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_SetMute(SLVolumeItf self, SLboolean mute)
{
    SL_ENTER_INTERFACE

    IVolume *thiz = (IVolume *) self;
    mute = SL_BOOLEAN_FALSE != mute; // normalize
    interface_lock_exclusive(thiz);
    SLboolean oldMute = thiz->mMute;
    if (oldMute != mute) {
        thiz->mMute = (SLuint8) mute;
        interface_unlock_exclusive_attributes(thiz, ATTR_GAIN);
    } else {
        interface_unlock_exclusive(thiz);
    }
    result = SL_RESULT_SUCCESS;

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_GetMute(SLVolumeItf self, SLboolean *pMute)
{
    SL_ENTER_INTERFACE

    if (NULL == pMute) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IVolume *thiz = (IVolume *) self;
        interface_lock_shared(thiz);
        SLboolean mute = thiz->mMute;
        interface_unlock_shared(thiz);
        *pMute = mute;
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_EnableStereoPosition(SLVolumeItf self, SLboolean enable)
{
    SL_ENTER_INTERFACE

    IVolume *thiz = (IVolume *) self;
    enable = SL_BOOLEAN_FALSE != enable; // normalize
    interface_lock_exclusive(thiz);
    SLboolean oldEnable = thiz->mEnableStereoPosition;
    if (oldEnable != enable) {
        thiz->mEnableStereoPosition = (SLuint8) enable;
        interface_unlock_exclusive_attributes(thiz, ATTR_GAIN);
    } else {
        interface_unlock_exclusive(thiz);
    }
    result = SL_RESULT_SUCCESS;

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_IsEnabledStereoPosition(SLVolumeItf self, SLboolean *pEnable)
{
    SL_ENTER_INTERFACE

    if (NULL == pEnable) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IVolume *thiz = (IVolume *) self;
        interface_lock_shared(thiz);
        SLboolean enable = thiz->mEnableStereoPosition;
        interface_unlock_shared(thiz);
        *pEnable = enable;
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_SetStereoPosition(SLVolumeItf self, SLpermille stereoPosition)
{
    SL_ENTER_INTERFACE

    if (!((-1000 <= stereoPosition) && (1000 >= stereoPosition))) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IVolume *thiz = (IVolume *) self;
        interface_lock_exclusive(thiz);
        SLpermille oldStereoPosition = thiz->mStereoPosition;
        if (oldStereoPosition != stereoPosition) {
            thiz->mStereoPosition = stereoPosition;
            interface_unlock_exclusive_attributes(thiz, ATTR_GAIN);
        } else {
            interface_unlock_exclusive(thiz);
        }
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static SLresult IVolume_GetStereoPosition(SLVolumeItf self, SLpermille *pStereoPosition)
{
    SL_ENTER_INTERFACE

    if (NULL == pStereoPosition) {
        result = SL_RESULT_PARAMETER_INVALID;
    } else {
        IVolume *thiz = (IVolume *) self;
        interface_lock_shared(thiz);
        SLpermille stereoPosition = thiz->mStereoPosition;
        interface_unlock_shared(thiz);
        *pStereoPosition = stereoPosition;
        result = SL_RESULT_SUCCESS;
    }

    SL_LEAVE_INTERFACE
}


static const struct SLVolumeItf_ IVolume_Itf = {
    IVolume_SetVolumeLevel,
    IVolume_GetVolumeLevel,
    IVolume_GetMaxVolumeLevel,
    IVolume_SetMute,
    IVolume_GetMute,
    IVolume_EnableStereoPosition,
    IVolume_IsEnabledStereoPosition,
    IVolume_SetStereoPosition,
    IVolume_GetStereoPosition
};

void IVolume_init(void *self)
{
    IVolume *thiz = (IVolume *) self;
    thiz->mItf = &IVolume_Itf;
    thiz->mLevel = 0;
    thiz->mMute = SL_BOOLEAN_FALSE;
    thiz->mEnableStereoPosition = SL_BOOLEAN_FALSE;
    thiz->mStereoPosition = 0;
}