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; } getDeviceIds()72 std::vector<audio_port_handle_t> getDeviceIds() const { 73 std::vector<audio_port_handle_t> deviceIds; 74 if (mPatch.num_sources == 0 || mPatch.num_sinks == 0) { 75 return deviceIds; 76 } 77 if (mIsInput) { 78 for (unsigned int i = 0; i < mPatch.num_sources; i++) { 79 deviceIds.push_back(mPatch.sources[i].id); 80 } 81 } else { 82 for (unsigned int i = 0; i < mPatch.num_sinks; i++) { 83 deviceIds.push_back(mPatch.sinks[i].id); 84 } 85 } 86 return deviceIds; 87 } setPatch(const audio_patch & patch)88 void setPatch(const audio_patch& patch) { mPatch = patch; } 89 toDebugString()90 std::string toDebugString() const { 91 std::ostringstream ss; 92 ss << mIoHandle << ", samplingRate " << mSamplingRate << ", " 93 << audio_format_to_string(mFormat) << ", " 94 << (audio_channel_mask_get_representation(mChannelMask) == 95 AUDIO_CHANNEL_REPRESENTATION_INDEX ? 96 audio_channel_index_mask_to_string(mChannelMask) : 97 (mIsInput ? audio_channel_in_mask_to_string(mChannelMask) : 98 audio_channel_out_mask_to_string(mChannelMask))) 99 << ", frameCount " << mFrameCount << ", frameCountHAL " << mFrameCountHAL 100 << ", deviceIds "; 101 102 std::vector<audio_port_handle_t> deviceIds = getDeviceIds(); 103 for (auto deviceId : deviceIds) { 104 ss << deviceId << " "; 105 } 106 107 return ss.str(); 108 } 109 110 private: 111 const audio_io_handle_t mIoHandle = AUDIO_IO_HANDLE_NONE; 112 struct audio_patch mPatch = {}; 113 const bool mIsInput = false; 114 const uint32_t mSamplingRate = 0; 115 const audio_format_t mFormat = AUDIO_FORMAT_DEFAULT; 116 const audio_channel_mask_t mChannelMask = AUDIO_CHANNEL_NONE; 117 const size_t mFrameCount = 0; 118 const size_t mFrameCountHAL = 0; 119 const uint32_t mLatency = 0; 120 const audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; 121 }; 122 123 124 }; // namespace android 125 126 #endif /*ANDROID_AUDIO_IO_DESCRIPTOR_H*/ 127