IVolume.c revision 262059f71a68edc5e510427c63f5f1623d3672a8
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 22static 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 *this = (IVolume *) self; 30 interface_lock_exclusive(this); 31 SLmillibel oldLevel = this->mLevel; 32 if (oldLevel != level) { 33 this->mLevel = level; 34 interface_unlock_exclusive_attributes(this, ATTR_GAIN); 35 } else 36 interface_unlock_exclusive(this); 37 result = SL_RESULT_SUCCESS; 38 } 39 40 SL_LEAVE_INTERFACE 41} 42 43 44static 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 *this = (IVolume *) self; 52 interface_lock_peek(this); 53 SLmillibel level = this->mLevel; 54 interface_unlock_peek(this); 55 *pLevel = level; 56 result = SL_RESULT_SUCCESS; 57 } 58 59 SL_LEAVE_INTERFACE 60} 61 62 63static 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 78static SLresult IVolume_SetMute(SLVolumeItf self, SLboolean mute) 79{ 80 SL_ENTER_INTERFACE 81 82 IVolume *this = (IVolume *) self; 83 mute = SL_BOOLEAN_FALSE != mute; // normalize 84 interface_lock_exclusive(this); 85 SLboolean oldMute = this->mMute; 86 if (oldMute != mute) { 87 this->mMute = (SLuint8) mute; 88 interface_unlock_exclusive_attributes(this, ATTR_GAIN); 89 } else 90 interface_unlock_exclusive(this); 91 result = SL_RESULT_SUCCESS; 92 93 SL_LEAVE_INTERFACE 94} 95 96 97static 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 *this = (IVolume *) self; 105 interface_lock_peek(this); 106 SLboolean mute = this->mMute; 107 interface_unlock_peek(this); 108 *pMute = mute; 109 result = SL_RESULT_SUCCESS; 110 } 111 112 SL_LEAVE_INTERFACE 113} 114 115 116static SLresult IVolume_EnableStereoPosition(SLVolumeItf self, SLboolean enable) 117{ 118 SL_ENTER_INTERFACE 119 120 IVolume *this = (IVolume *) self; 121 enable = SL_BOOLEAN_FALSE != enable; // normalize 122 interface_lock_exclusive(this); 123 SLboolean oldEnable = this->mEnableStereoPosition; 124 if (oldEnable != enable) { 125 this->mEnableStereoPosition = (SLuint8) enable; 126 interface_unlock_exclusive_attributes(this, ATTR_GAIN); 127 } else { 128 interface_unlock_exclusive(this); 129 } 130 result = SL_RESULT_SUCCESS; 131 132 SL_LEAVE_INTERFACE 133} 134 135 136static 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 *this = (IVolume *) self; 144 interface_lock_peek(this); 145 SLboolean enable = this->mEnableStereoPosition; 146 interface_unlock_peek(this); 147 *pEnable = enable; 148 result = SL_RESULT_SUCCESS; 149 } 150 151 SL_LEAVE_INTERFACE 152} 153 154 155static 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 *this = (IVolume *) self; 163 interface_lock_exclusive(this); 164 SLpermille oldStereoPosition = this->mStereoPosition; 165 if (oldStereoPosition != stereoPosition) { 166 this->mStereoPosition = stereoPosition; 167 interface_unlock_exclusive_attributes(this, ATTR_GAIN); 168 } else 169 interface_unlock_exclusive(this); 170 result = SL_RESULT_SUCCESS; 171 } 172 173 SL_LEAVE_INTERFACE 174} 175 176 177static 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 *this = (IVolume *) self; 185 interface_lock_peek(this); 186 SLpermille stereoPosition = this->mStereoPosition; 187 interface_unlock_peek(this); 188 *pStereoPosition = stereoPosition; 189 result = SL_RESULT_SUCCESS; 190 } 191 192 SL_LEAVE_INTERFACE 193} 194 195 196static 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 208void IVolume_init(void *self) 209{ 210 IVolume *this = (IVolume *) self; 211 this->mItf = &IVolume_Itf; 212 this->mLevel = 0; 213 this->mMute = SL_BOOLEAN_FALSE; 214 this->mEnableStereoPosition = SL_BOOLEAN_FALSE; 215 this->mStereoPosition = 0; 216} 217