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 /* DeviceVolume implementation */
18 
19 #include "sles_allinclusive.h"
20 
21 
IDeviceVolume_GetVolumeScale(SLDeviceVolumeItf self,SLuint32 deviceID,SLint32 * pMinValue,SLint32 * pMaxValue,SLboolean * pIsMillibelScale)22 static SLresult IDeviceVolume_GetVolumeScale(SLDeviceVolumeItf self, SLuint32 deviceID,
23     SLint32 *pMinValue, SLint32 *pMaxValue, SLboolean *pIsMillibelScale)
24 {
25     SL_ENTER_INTERFACE
26 
27     switch (deviceID) {
28     case SL_DEFAULTDEVICEID_AUDIOINPUT:
29     case SL_DEFAULTDEVICEID_AUDIOOUTPUT:
30     case DEVICE_ID_HEADSET:
31     case DEVICE_ID_HANDSFREE:
32         if (NULL != pMinValue)
33             *pMinValue = 0;
34         if (NULL != pMaxValue)
35             *pMaxValue = 10;
36         if (NULL != pIsMillibelScale)
37             *pIsMillibelScale = SL_BOOLEAN_FALSE;
38         result = SL_RESULT_SUCCESS;
39         break;
40     default:
41         result = SL_RESULT_PARAMETER_INVALID;
42         break;
43     }
44 
45     SL_LEAVE_INTERFACE
46 }
47 
48 
IDeviceVolume_SetVolume(SLDeviceVolumeItf self,SLuint32 deviceID,SLint32 volume)49 static SLresult IDeviceVolume_SetVolume(SLDeviceVolumeItf self, SLuint32 deviceID, SLint32 volume)
50 {
51     SL_ENTER_INTERFACE
52 
53     switch (deviceID) {
54     // These are treated same as generic audio output for now
55     case DEVICE_ID_HEADSET:
56     case DEVICE_ID_HANDSFREE:
57         deviceID = SL_DEFAULTDEVICEID_AUDIOOUTPUT;
58         // fall through
59     case SL_DEFAULTDEVICEID_AUDIOINPUT:
60     case SL_DEFAULTDEVICEID_AUDIOOUTPUT:
61         {
62         IDeviceVolume *thiz = (IDeviceVolume *) self;
63         interface_lock_poke(thiz);
64         thiz->mVolume[~deviceID] = volume;
65         interface_unlock_poke(thiz);
66         result = SL_RESULT_SUCCESS;
67         }
68         break;
69     default:
70         result = SL_RESULT_PARAMETER_INVALID;
71         break;
72     }
73 
74     SL_LEAVE_INTERFACE
75 }
76 
77 
IDeviceVolume_GetVolume(SLDeviceVolumeItf self,SLuint32 deviceID,SLint32 * pVolume)78 static SLresult IDeviceVolume_GetVolume(SLDeviceVolumeItf self, SLuint32 deviceID, SLint32 *pVolume)
79 {
80     SL_ENTER_INTERFACE
81 
82     if (NULL == pVolume) {
83         result = SL_RESULT_PARAMETER_INVALID;
84     } else {
85         switch (deviceID) {
86         // These are treated same as generic audio output for now
87         case DEVICE_ID_HEADSET:
88         case DEVICE_ID_HANDSFREE:
89             deviceID = SL_DEFAULTDEVICEID_AUDIOOUTPUT;
90             // fall through
91         case SL_DEFAULTDEVICEID_AUDIOINPUT:
92         case SL_DEFAULTDEVICEID_AUDIOOUTPUT:
93             {
94             IDeviceVolume *thiz = (IDeviceVolume *) self;
95             interface_lock_peek(thiz);
96             SLint32 volume = thiz->mVolume[~deviceID];
97             interface_unlock_peek(thiz);
98             *pVolume = volume;
99             result = SL_RESULT_SUCCESS;
100             }
101             break;
102         default:
103             result = SL_RESULT_PARAMETER_INVALID;
104             break;
105         }
106     }
107 
108     SL_LEAVE_INTERFACE
109 }
110 
111 
112 static const struct SLDeviceVolumeItf_ IDeviceVolume_Itf = {
113     IDeviceVolume_GetVolumeScale,
114     IDeviceVolume_SetVolume,
115     IDeviceVolume_GetVolume
116 };
117 
IDeviceVolume_init(void * self)118 void IDeviceVolume_init(void *self)
119 {
120     IDeviceVolume *thiz = (IDeviceVolume *) self;
121     thiz->mItf = &IDeviceVolume_Itf;
122     unsigned i;
123     for (i = 0; i < MAX_DEVICE; ++i) {
124         thiz->mVolume[i] = 10;
125     }
126 }
127