1 /*
2  * Copyright (C) 2015 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 #define LOG_TAG "APM::Volumes"
18 //#define LOG_NDEBUG 0
19 
20 //#define VERY_VERBOSE_LOGGING
21 #ifdef VERY_VERBOSE_LOGGING
22 #define ALOGVV ALOGV
23 #else
24 #define ALOGVV(a...) do { } while(0)
25 #endif
26 
27 #include "StreamDescriptor.h"
28 #include <utils/Log.h>
29 #include <utils/String8.h>
30 
31 namespace android {
32 
33 // --- StreamDescriptor class implementation
34 
StreamDescriptor()35 StreamDescriptor::StreamDescriptor()
36     :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
37 {
38     mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
39 }
40 
getVolumeIndex(audio_devices_t device) const41 int StreamDescriptor::getVolumeIndex(audio_devices_t device) const
42 {
43     device = Volume::getDeviceForVolume(device);
44     // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
45     if (mIndexCur.indexOfKey(device) < 0) {
46         device = AUDIO_DEVICE_OUT_DEFAULT;
47     }
48     return mIndexCur.valueFor(device);
49 }
50 
clearCurrentVolumeIndex()51 void StreamDescriptor::clearCurrentVolumeIndex()
52 {
53     mIndexCur.clear();
54 }
55 
addCurrentVolumeIndex(audio_devices_t device,int index)56 void StreamDescriptor::addCurrentVolumeIndex(audio_devices_t device, int index)
57 {
58     mIndexCur.add(device, index);
59 }
60 
setVolumeIndexMin(int volIndexMin)61 void StreamDescriptor::setVolumeIndexMin(int volIndexMin)
62 {
63     mIndexMin = volIndexMin;
64 }
65 
setVolumeIndexMax(int volIndexMax)66 void StreamDescriptor::setVolumeIndexMax(int volIndexMax)
67 {
68     mIndexMax = volIndexMax;
69 }
70 
setVolumeCurvePoint(Volume::device_category deviceCategory,const VolumeCurvePoint * point)71 void StreamDescriptor::setVolumeCurvePoint(Volume::device_category deviceCategory,
72                                            const VolumeCurvePoint *point)
73 {
74     mVolumeCurve[deviceCategory] = point;
75 }
76 
dump(int fd) const77 void StreamDescriptor::dump(int fd) const
78 {
79     const size_t SIZE = 256;
80     char buffer[SIZE];
81     String8 result;
82 
83     snprintf(buffer, SIZE, "%s         %02d         %02d         ",
84              mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
85     result.append(buffer);
86     for (size_t i = 0; i < mIndexCur.size(); i++) {
87         snprintf(buffer, SIZE, "%04x : %02d, ",
88                  mIndexCur.keyAt(i),
89                  mIndexCur.valueAt(i));
90         result.append(buffer);
91     }
92     result.append("\n");
93 
94     write(fd, result.string(), result.size());
95 }
96 
StreamDescriptorCollection()97 StreamDescriptorCollection::StreamDescriptorCollection()
98 {
99     for (size_t stream = 0 ; stream < AUDIO_STREAM_CNT; stream++) {
100         add(static_cast<audio_stream_type_t>(stream), StreamDescriptor());
101     }
102 }
103 
canBeMuted(audio_stream_type_t stream)104 bool StreamDescriptorCollection::canBeMuted(audio_stream_type_t stream)
105 {
106     return valueAt(stream).canBeMuted();
107 }
108 
clearCurrentVolumeIndex(audio_stream_type_t stream)109 void StreamDescriptorCollection::clearCurrentVolumeIndex(audio_stream_type_t stream)
110 {
111     editValueAt(stream).clearCurrentVolumeIndex();
112 }
113 
addCurrentVolumeIndex(audio_stream_type_t stream,audio_devices_t device,int index)114 void StreamDescriptorCollection::addCurrentVolumeIndex(audio_stream_type_t stream,
115                                                        audio_devices_t device, int index)
116 {
117     editValueAt(stream).addCurrentVolumeIndex(device, index);
118 }
119 
setVolumeCurvePoint(audio_stream_type_t stream,Volume::device_category deviceCategory,const VolumeCurvePoint * point)120 void StreamDescriptorCollection::setVolumeCurvePoint(audio_stream_type_t stream,
121                                                      Volume::device_category deviceCategory,
122                                                      const VolumeCurvePoint *point)
123 {
124     editValueAt(stream).setVolumeCurvePoint(deviceCategory, point);
125 }
126 
getVolumeCurvePoint(audio_stream_type_t stream,Volume::device_category deviceCategory) const127 const VolumeCurvePoint *StreamDescriptorCollection::getVolumeCurvePoint(audio_stream_type_t stream,
128                                                                         Volume::device_category deviceCategory) const
129 {
130     return valueAt(stream).getVolumeCurvePoint(deviceCategory);
131 }
132 
setVolumeIndexMin(audio_stream_type_t stream,int volIndexMin)133 void StreamDescriptorCollection::setVolumeIndexMin(audio_stream_type_t stream,int volIndexMin)
134 {
135     return editValueAt(stream).setVolumeIndexMin(volIndexMin);
136 }
137 
setVolumeIndexMax(audio_stream_type_t stream,int volIndexMax)138 void StreamDescriptorCollection::setVolumeIndexMax(audio_stream_type_t stream,int volIndexMax)
139 {
140     return editValueAt(stream).setVolumeIndexMax(volIndexMax);
141 }
142 
dump(int fd) const143 status_t StreamDescriptorCollection::dump(int fd) const
144 {
145     const size_t SIZE = 256;
146     char buffer[SIZE];
147 
148     snprintf(buffer, SIZE, "\nStreams dump:\n");
149     write(fd, buffer, strlen(buffer));
150     snprintf(buffer, SIZE,
151              " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
152     write(fd, buffer, strlen(buffer));
153     for (size_t i = 0; i < size(); i++) {
154         snprintf(buffer, SIZE, " %02zu      ", i);
155         write(fd, buffer, strlen(buffer));
156         valueAt(i).dump(fd);
157     }
158 
159     return NO_ERROR;
160 }
161 
162 }; // namespace android
163