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#pragma once
18
19#include "IVolumeCurvesCollection.h"
20#include <utils/KeyedVector.h>
21#include <utils/StrongPointer.h>
22#include <utils/SortedVector.h>
23#include <system/audio.h>
24
25namespace android {
26
27// stream descriptor used for volume control
28class StreamDescriptor
29{
30public:
31    StreamDescriptor();
32
33    int getVolumeIndex(audio_devices_t device) const;
34    bool canBeMuted() const { return mCanBeMuted; }
35    void clearCurrentVolumeIndex();
36    void addCurrentVolumeIndex(audio_devices_t device, int index);
37    int getVolumeIndexMin() const { return mIndexMin; }
38    int getVolumeIndexMax() const { return mIndexMax; }
39    void setVolumeIndexMin(int volIndexMin);
40    void setVolumeIndexMax(int volIndexMax);
41    bool hasVolumeIndexForDevice(audio_devices_t device) const
42    {
43        device = Volume::getDeviceForVolume(device);
44        return mIndexCur.indexOfKey(device) >= 0;
45    }
46
47    void dump(int fd) const;
48
49    void setVolumeCurvePoint(device_category deviceCategory, const VolumeCurvePoint *point);
50    const VolumeCurvePoint *getVolumeCurvePoint(device_category deviceCategory) const
51    {
52        return mVolumeCurve[deviceCategory];
53    }
54
55private:
56    const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
57    KeyedVector<audio_devices_t, int> mIndexCur; /**< current volume index per device. */
58    int mIndexMin; /**< min volume index. */
59    int mIndexMax; /**< max volume index. */
60    bool mCanBeMuted; /**< true is the stream can be muted. */
61};
62
63/**
64 * stream descriptors collection for volume control
65 */
66class StreamDescriptorCollection : public DefaultKeyedVector<audio_stream_type_t, StreamDescriptor>,
67                                   public IVolumeCurvesCollection
68{
69public:
70    StreamDescriptorCollection();
71
72    virtual void clearCurrentVolumeIndex(audio_stream_type_t stream);
73    virtual void addCurrentVolumeIndex(audio_stream_type_t stream, audio_devices_t device,
74                                       int index);
75    virtual bool canBeMuted(audio_stream_type_t stream);
76    virtual int getVolumeIndexMin(audio_stream_type_t stream) const
77    {
78        return valueFor(stream).getVolumeIndexMin();
79    }
80    virtual int getVolumeIndex(audio_stream_type_t stream, audio_devices_t device)
81    {
82        return valueFor(stream).getVolumeIndex(device);
83    }
84    virtual int getVolumeIndexMax(audio_stream_type_t stream) const
85    {
86        return valueFor(stream).getVolumeIndexMax();
87    }
88    virtual float volIndexToDb(audio_stream_type_t stream, device_category device,
89                               int indexInUi) const;
90    virtual status_t initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax);
91    virtual void initializeVolumeCurves(bool isSpeakerDrcEnabled);
92    virtual void switchVolumeCurve(audio_stream_type_t streamSrc, audio_stream_type_t streamDst);
93    virtual bool hasVolumeIndexForDevice(audio_stream_type_t stream,
94                                         audio_devices_t device) const
95    {
96        return valueFor(stream).hasVolumeIndexForDevice(device);
97    }
98
99    virtual status_t dump(int fd) const;
100
101private:
102    void setVolumeCurvePoint(audio_stream_type_t stream, device_category deviceCategory,
103                             const VolumeCurvePoint *point);
104    const VolumeCurvePoint *getVolumeCurvePoint(audio_stream_type_t stream,
105                                                device_category deviceCategory) const;
106    void setVolumeIndexMin(audio_stream_type_t stream,int volIndexMin);
107    void setVolumeIndexMax(audio_stream_type_t stream,int volIndexMax);
108};
109
110}; // namespace android
111