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 /* Volume implementation */ 18 19 #include "sles_allinclusive.h" 20 21 22 static SLresult IVolume_SetVolumeLevel(SLVolumeItf self, SLmillibel level) 23 { 24 SL_ENTER_INTERFACE 25 26 if (!((SL_MILLIBEL_MIN <= level) && (level <= PLATFORM_MILLIBEL_MAX_VOLUME))) { 27 result = SL_RESULT_PARAMETER_INVALID; 28 } else { 29 IVolume *thiz = (IVolume *) self; 30 interface_lock_exclusive(thiz); 31 SLmillibel oldLevel = thiz->mLevel; 32 if (oldLevel != level) { 33 thiz->mLevel = level; 34 interface_unlock_exclusive_attributes(thiz, ATTR_GAIN); 35 } else 36 interface_unlock_exclusive(thiz); 37 result = SL_RESULT_SUCCESS; 38 } 39 40 SL_LEAVE_INTERFACE 41 } 42 43 44 static SLresult IVolume_GetVolumeLevel(SLVolumeItf self, SLmillibel *pLevel) 45 { 46 SL_ENTER_INTERFACE 47 48 if (NULL == pLevel) { 49 result = SL_RESULT_PARAMETER_INVALID; 50 } else { 51 IVolume *thiz = (IVolume *) self; 52 interface_lock_shared(thiz); 53 SLmillibel level = thiz->mLevel; 54 interface_unlock_shared(thiz); 55 *pLevel = level; 56 result = SL_RESULT_SUCCESS; 57 } 58 59 SL_LEAVE_INTERFACE 60 } 61 62 63 static SLresult IVolume_GetMaxVolumeLevel(SLVolumeItf self, SLmillibel *pMaxLevel) 64 { 65 SL_ENTER_INTERFACE 66 67 if (NULL == pMaxLevel) { 68 result = SL_RESULT_PARAMETER_INVALID; 69 } else { 70 *pMaxLevel = PLATFORM_MILLIBEL_MAX_VOLUME; 71 result = SL_RESULT_SUCCESS; 72 } 73 74 SL_LEAVE_INTERFACE 75 } 76 77 78 static SLresult IVolume_SetMute(SLVolumeItf self, SLboolean mute) 79 { 80 SL_ENTER_INTERFACE 81 82 IVolume *thiz = (IVolume *) self; 83 mute = SL_BOOLEAN_FALSE != mute; // normalize 84 interface_lock_exclusive(thiz); 85 SLboolean oldMute = thiz->mMute; 86 if (oldMute != mute) { 87 thiz->mMute = (SLuint8) mute; 88 interface_unlock_exclusive_attributes(thiz, ATTR_GAIN); 89 } else 90 interface_unlock_exclusive(thiz); 91 result = SL_RESULT_SUCCESS; 92 93 SL_LEAVE_INTERFACE 94 } 95 96 97 static SLresult IVolume_GetMute(SLVolumeItf self, SLboolean *pMute) 98 { 99 SL_ENTER_INTERFACE 100 101 if (NULL == pMute) { 102 result = SL_RESULT_PARAMETER_INVALID; 103 } else { 104 IVolume *thiz = (IVolume *) self; 105 interface_lock_shared(thiz); 106 SLboolean mute = thiz->mMute; 107 interface_unlock_shared(thiz); 108 *pMute = mute; 109 result = SL_RESULT_SUCCESS; 110 } 111 112 SL_LEAVE_INTERFACE 113 } 114 115 116 static SLresult IVolume_EnableStereoPosition(SLVolumeItf self, SLboolean enable) 117 { 118 SL_ENTER_INTERFACE 119 120 IVolume *thiz = (IVolume *) self; 121 enable = SL_BOOLEAN_FALSE != enable; // normalize 122 interface_lock_exclusive(thiz); 123 SLboolean oldEnable = thiz->mEnableStereoPosition; 124 if (oldEnable != enable) { 125 thiz->mEnableStereoPosition = (SLuint8) enable; 126 interface_unlock_exclusive_attributes(thiz, ATTR_GAIN); 127 } else { 128 interface_unlock_exclusive(thiz); 129 } 130 result = SL_RESULT_SUCCESS; 131 132 SL_LEAVE_INTERFACE 133 } 134 135 136 static SLresult IVolume_IsEnabledStereoPosition(SLVolumeItf self, SLboolean *pEnable) 137 { 138 SL_ENTER_INTERFACE 139 140 if (NULL == pEnable) { 141 result = SL_RESULT_PARAMETER_INVALID; 142 } else { 143 IVolume *thiz = (IVolume *) self; 144 interface_lock_shared(thiz); 145 SLboolean enable = thiz->mEnableStereoPosition; 146 interface_unlock_shared(thiz); 147 *pEnable = enable; 148 result = SL_RESULT_SUCCESS; 149 } 150 151 SL_LEAVE_INTERFACE 152 } 153 154 155 static SLresult IVolume_SetStereoPosition(SLVolumeItf self, SLpermille stereoPosition) 156 { 157 SL_ENTER_INTERFACE 158 159 if (!((-1000 <= stereoPosition) && (1000 >= stereoPosition))) { 160 result = SL_RESULT_PARAMETER_INVALID; 161 } else { 162 IVolume *thiz = (IVolume *) self; 163 interface_lock_exclusive(thiz); 164 SLpermille oldStereoPosition = thiz->mStereoPosition; 165 if (oldStereoPosition != stereoPosition) { 166 thiz->mStereoPosition = stereoPosition; 167 interface_unlock_exclusive_attributes(thiz, ATTR_GAIN); 168 } else 169 interface_unlock_exclusive(thiz); 170 result = SL_RESULT_SUCCESS; 171 } 172 173 SL_LEAVE_INTERFACE 174 } 175 176 177 static SLresult IVolume_GetStereoPosition(SLVolumeItf self, SLpermille *pStereoPosition) 178 { 179 SL_ENTER_INTERFACE 180 181 if (NULL == pStereoPosition) { 182 result = SL_RESULT_PARAMETER_INVALID; 183 } else { 184 IVolume *thiz = (IVolume *) self; 185 interface_lock_shared(thiz); 186 SLpermille stereoPosition = thiz->mStereoPosition; 187 interface_unlock_shared(thiz); 188 *pStereoPosition = stereoPosition; 189 result = SL_RESULT_SUCCESS; 190 } 191 192 SL_LEAVE_INTERFACE 193 } 194 195 196 static const struct SLVolumeItf_ IVolume_Itf = { 197 IVolume_SetVolumeLevel, 198 IVolume_GetVolumeLevel, 199 IVolume_GetMaxVolumeLevel, 200 IVolume_SetMute, 201 IVolume_GetMute, 202 IVolume_EnableStereoPosition, 203 IVolume_IsEnabledStereoPosition, 204 IVolume_SetStereoPosition, 205 IVolume_GetStereoPosition 206 }; 207 208 void IVolume_init(void *self) 209 { 210 IVolume *thiz = (IVolume *) self; 211 thiz->mItf = &IVolume_Itf; 212 thiz->mLevel = 0; 213 thiz->mMute = SL_BOOLEAN_FALSE; 214 thiz->mEnableStereoPosition = SL_BOOLEAN_FALSE; 215 thiz->mStereoPosition = 0; 216 } 217