StreamDescriptor.cpp revision d0609ad390ff8bb1cafebdf363bf1d15e63b949f
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
31namespace android {
32
33// --- StreamDescriptor class implementation
34
35StreamDescriptor::StreamDescriptor()
36    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
37{
38    // Initialize the current stream's index to mIndexMax so volume isn't 0 in
39    // cases where the Java layer doesn't call into the audio policy service to
40    // set the default volume.
41    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, mIndexMax);
42}
43
44int StreamDescriptor::getVolumeIndex(audio_devices_t device) const
45{
46    device = Volume::getDeviceForVolume(device);
47    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
48    if (mIndexCur.indexOfKey(device) < 0) {
49        device = AUDIO_DEVICE_OUT_DEFAULT;
50    }
51    return mIndexCur.valueFor(device);
52}
53
54void StreamDescriptor::clearCurrentVolumeIndex()
55{
56    mIndexCur.clear();
57}
58
59void StreamDescriptor::addCurrentVolumeIndex(audio_devices_t device, int index)
60{
61    mIndexCur.add(device, index);
62}
63
64void StreamDescriptor::setVolumeIndexMin(int volIndexMin)
65{
66    mIndexMin = volIndexMin;
67}
68
69void StreamDescriptor::setVolumeIndexMax(int volIndexMax)
70{
71    mIndexMax = volIndexMax;
72}
73
74void StreamDescriptor::setVolumeCurvePoint(device_category deviceCategory,
75                                           const VolumeCurvePoint *point)
76{
77    mVolumeCurve[deviceCategory] = point;
78}
79
80void StreamDescriptor::dump(int fd) const
81{
82    const size_t SIZE = 256;
83    char buffer[SIZE];
84    String8 result;
85
86    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
87             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
88    result.append(buffer);
89    for (size_t i = 0; i < mIndexCur.size(); i++) {
90        snprintf(buffer, SIZE, "%04x : %02d, ",
91                 mIndexCur.keyAt(i),
92                 mIndexCur.valueAt(i));
93        result.append(buffer);
94    }
95    result.append("\n");
96
97    write(fd, result.string(), result.size());
98}
99
100StreamDescriptorCollection::StreamDescriptorCollection()
101{
102    for (size_t stream = 0 ; stream < AUDIO_STREAM_CNT; stream++) {
103        add(static_cast<audio_stream_type_t>(stream), StreamDescriptor());
104    }
105}
106
107bool StreamDescriptorCollection::canBeMuted(audio_stream_type_t stream)
108{
109    return valueAt(stream).canBeMuted();
110}
111
112void StreamDescriptorCollection::clearCurrentVolumeIndex(audio_stream_type_t stream)
113{
114    editValueAt(stream).clearCurrentVolumeIndex();
115}
116
117void StreamDescriptorCollection::addCurrentVolumeIndex(audio_stream_type_t stream,
118                                                       audio_devices_t device, int index)
119{
120    editValueAt(stream).addCurrentVolumeIndex(device, index);
121}
122
123void StreamDescriptorCollection::setVolumeCurvePoint(audio_stream_type_t stream,
124                                                     device_category deviceCategory,
125                                                     const VolumeCurvePoint *point)
126{
127    editValueAt(stream).setVolumeCurvePoint(deviceCategory, point);
128}
129
130const VolumeCurvePoint *StreamDescriptorCollection::getVolumeCurvePoint(audio_stream_type_t stream,
131                                                                        device_category deviceCategory) const
132{
133    return valueAt(stream).getVolumeCurvePoint(deviceCategory);
134}
135
136void StreamDescriptorCollection::setVolumeIndexMin(audio_stream_type_t stream,int volIndexMin)
137{
138    return editValueAt(stream).setVolumeIndexMin(volIndexMin);
139}
140
141void StreamDescriptorCollection::setVolumeIndexMax(audio_stream_type_t stream,int volIndexMax)
142{
143    return editValueAt(stream).setVolumeIndexMax(volIndexMax);
144}
145
146status_t StreamDescriptorCollection::dump(int fd) const
147{
148    const size_t SIZE = 256;
149    char buffer[SIZE];
150
151    snprintf(buffer, SIZE, "\nStreams dump:\n");
152    write(fd, buffer, strlen(buffer));
153    snprintf(buffer, SIZE,
154             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
155    write(fd, buffer, strlen(buffer));
156    for (size_t i = 0; i < size(); i++) {
157        snprintf(buffer, SIZE, " %02zu      ", i);
158        write(fd, buffer, strlen(buffer));
159        valueAt(i).dump(fd);
160    }
161
162    return NO_ERROR;
163}
164
165}; // namespace android
166