• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ANDROID_AUDIO_IO_DESCRIPTOR_H
18 #define ANDROID_AUDIO_IO_DESCRIPTOR_H
19 
20 #include <sstream>
21 #include <string>
22 
23 #include <system/audio.h>
24 #include <utils/RefBase.h>
25 
26 namespace android {
27 
28 enum audio_io_config_event_t {
29     AUDIO_OUTPUT_REGISTERED,
30     AUDIO_OUTPUT_OPENED,
31     AUDIO_OUTPUT_CLOSED,
32     AUDIO_OUTPUT_CONFIG_CHANGED,
33     AUDIO_INPUT_REGISTERED,
34     AUDIO_INPUT_OPENED,
35     AUDIO_INPUT_CLOSED,
36     AUDIO_INPUT_CONFIG_CHANGED,
37     AUDIO_CLIENT_STARTED,
38 };
39 
40 // audio input/output descriptor used to cache output configurations in client process to avoid
41 // frequent calls through IAudioFlinger
42 class AudioIoDescriptor : public virtual RefBase {
43 public:
44     AudioIoDescriptor() = default;
45     // For AUDIO_{INPUT|OUTPUT}_CLOSED events.
AudioIoDescriptor(audio_io_handle_t ioHandle)46     AudioIoDescriptor(audio_io_handle_t ioHandle) : mIoHandle(ioHandle) {}
47     // For AUDIO_CLIENT_STARTED events.
AudioIoDescriptor(audio_io_handle_t ioHandle,const audio_patch & patch,audio_port_handle_t portId)48     AudioIoDescriptor(
49             audio_io_handle_t ioHandle, const audio_patch& patch, audio_port_handle_t portId) :
50             mIoHandle(ioHandle), mPatch(patch), mPortId(portId) {}
51     // For everything else.
52     AudioIoDescriptor(
53             audio_io_handle_t ioHandle, const audio_patch& patch, bool isInput,
54             uint32_t samplingRate, audio_format_t format, audio_channel_mask_t channelMask,
55             size_t frameCount, size_t frameCountHal, uint32_t latency = 0,
56             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) :
mIoHandle(ioHandle)57             mIoHandle(ioHandle), mPatch(patch), mIsInput(isInput),
58             mSamplingRate(samplingRate), mFormat(format), mChannelMask(channelMask),
59             mFrameCount(frameCount), mFrameCountHAL(frameCountHal), mLatency(latency),
60             mPortId(portId) {}
61 
getIoHandle()62     audio_io_handle_t getIoHandle() const { return mIoHandle; }
getPatch()63     const audio_patch& getPatch() const { return mPatch; }
getIsInput()64     bool getIsInput() const { return mIsInput; }
getSamplingRate()65     uint32_t getSamplingRate() const { return mSamplingRate; }
getFormat()66     audio_format_t getFormat() const { return mFormat; }
getChannelMask()67     audio_channel_mask_t getChannelMask() const { return mChannelMask; }
getFrameCount()68     size_t getFrameCount() const { return mFrameCount; }
getFrameCountHAL()69     size_t getFrameCountHAL() const { return mFrameCountHAL; }
getLatency()70     uint32_t getLatency() const { return mLatency; }
getPortId()71     audio_port_handle_t getPortId() const { return mPortId; }
getDeviceId()72     audio_port_handle_t getDeviceId() const {
73         if (mPatch.num_sources != 0 && mPatch.num_sinks != 0) {
74             // FIXME: the API only returns the first device in case of multiple device selection
75             return mIsInput ? mPatch.sources[0].id : mPatch.sinks[0].id;
76         }
77         return AUDIO_PORT_HANDLE_NONE;
78     }
setPatch(const audio_patch & patch)79     void setPatch(const audio_patch& patch) { mPatch = patch; }
80 
toDebugString()81     std::string toDebugString() const {
82         std::ostringstream ss;
83         ss << mIoHandle << ", samplingRate " << mSamplingRate << ", "
84            << audio_format_to_string(mFormat) << ", "
85            << (audio_channel_mask_get_representation(mChannelMask) ==
86                    AUDIO_CHANNEL_REPRESENTATION_INDEX ?
87                    audio_channel_index_mask_to_string(mChannelMask) :
88                    (mIsInput ? audio_channel_in_mask_to_string(mChannelMask) :
89                            audio_channel_out_mask_to_string(mChannelMask)))
90            << ", frameCount " << mFrameCount << ", frameCountHAL " << mFrameCountHAL
91            << ", deviceId " << getDeviceId();
92         return ss.str();
93     }
94 
95   private:
96     const audio_io_handle_t    mIoHandle = AUDIO_IO_HANDLE_NONE;
97           struct audio_patch   mPatch = {};
98     const bool                 mIsInput = false;
99     const uint32_t             mSamplingRate = 0;
100     const audio_format_t       mFormat = AUDIO_FORMAT_DEFAULT;
101     const audio_channel_mask_t mChannelMask = AUDIO_CHANNEL_NONE;
102     const size_t               mFrameCount = 0;
103     const size_t               mFrameCountHAL = 0;
104     const uint32_t             mLatency = 0;
105     const audio_port_handle_t  mPortId = AUDIO_PORT_HANDLE_NONE;
106 };
107 
108 
109 };  // namespace android
110 
111 #endif  /*ANDROID_AUDIO_IO_DESCRIPTOR_H*/
112