Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioStreamConfiguration" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <sys/mman.h> |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 24 | #include <aaudio/AAudio.h> |
| 25 | |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 26 | #include <media/AidlConversion.h> |
| 27 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 28 | #include "binding/AAudioStreamConfiguration.h" |
| 29 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 30 | using namespace aaudio; |
| 31 | |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 32 | using android::media::audio::common::AudioFormatDescription; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 33 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 34 | AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& parcelable) { |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 35 | setChannelMask(parcelable.channelMask); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 36 | setSampleRate(parcelable.sampleRate); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame^] | 37 | auto deviceIds = android::convertContainer<android::DeviceIdVector>( |
| 38 | parcelable.deviceIds, android::aidl2legacy_int32_t_audio_port_handle_t); |
| 39 | if (deviceIds.ok()) { |
| 40 | setDeviceIds(deviceIds.value()); |
| 41 | } else { |
| 42 | ALOGE("deviceIds (%s) aidl2legacy conversion failed", |
| 43 | android::toString(parcelable.deviceIds).c_str()); |
| 44 | android::DeviceIdVector emptyDeviceIds; |
| 45 | setDeviceIds(emptyDeviceIds); |
| 46 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 47 | static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(parcelable.sharingMode)); |
| 48 | setSharingMode(parcelable.sharingMode); |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 49 | auto convFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t( |
| 50 | parcelable.audioFormat); |
| 51 | setFormat(convFormat.ok() ? convFormat.value() : AUDIO_FORMAT_INVALID); |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 52 | if (!convFormat.ok()) { |
| 53 | ALOGE("audioFormat (%s) aidl2legacy conversion failed", |
| 54 | parcelable.hardwareAudioFormat.toString().c_str()); |
| 55 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 56 | static_assert(sizeof(aaudio_direction_t) == sizeof(parcelable.direction)); |
| 57 | setDirection(parcelable.direction); |
| 58 | static_assert(sizeof(audio_usage_t) == sizeof(parcelable.usage)); |
| 59 | setUsage(parcelable.usage); |
| 60 | static_assert(sizeof(aaudio_content_type_t) == sizeof(parcelable.contentType)); |
| 61 | setContentType(parcelable.contentType); |
Jiabin Huang | 51a8a77 | 2024-10-30 21:57:48 +0000 | [diff] [blame] | 62 | setTags(parcelable.tags); |
Jean-Michel Trivi | 656bfdc | 2021-09-20 18:42:37 -0700 | [diff] [blame] | 63 | static_assert(sizeof(aaudio_spatialization_behavior_t) == |
| 64 | sizeof(parcelable.spatializationBehavior)); |
| 65 | setSpatializationBehavior(parcelable.spatializationBehavior); |
| 66 | setIsContentSpatialized(parcelable.isContentSpatialized); |
| 67 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 68 | static_assert(sizeof(aaudio_input_preset_t) == sizeof(parcelable.inputPreset)); |
| 69 | setInputPreset(parcelable.inputPreset); |
| 70 | setBufferCapacity(parcelable.bufferCapacity); |
| 71 | static_assert( |
| 72 | sizeof(aaudio_allowed_capture_policy_t) == sizeof(parcelable.allowedCapturePolicy)); |
| 73 | setAllowedCapturePolicy(parcelable.allowedCapturePolicy); |
| 74 | static_assert(sizeof(aaudio_session_id_t) == sizeof(parcelable.sessionId)); |
| 75 | setSessionId(parcelable.sessionId); |
| 76 | setPrivacySensitive(parcelable.isPrivacySensitive); |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 77 | setHardwareSamplesPerFrame(parcelable.hardwareSamplesPerFrame); |
| 78 | setHardwareSampleRate(parcelable.hardwareSampleRate); |
| 79 | auto convHardwareFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t( |
| 80 | parcelable.hardwareAudioFormat); |
| 81 | setHardwareFormat(convHardwareFormat.ok() ? convHardwareFormat.value() : AUDIO_FORMAT_INVALID); |
| 82 | if (!convHardwareFormat.ok()) { |
| 83 | ALOGE("hardwareAudioFormat (%s) aidl2legacy conversion failed", |
| 84 | parcelable.hardwareAudioFormat.toString().c_str()); |
| 85 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 88 | AAudioStreamConfiguration& |
| 89 | AAudioStreamConfiguration::operator=(const StreamParameters& parcelable) { |
| 90 | this->~AAudioStreamConfiguration(); |
| 91 | new (this) AAudioStreamConfiguration(parcelable); |
| 92 | return *this; |
| 93 | } |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 94 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 95 | StreamParameters AAudioStreamConfiguration::parcelable() const { |
| 96 | StreamParameters result; |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 97 | result.channelMask = getChannelMask(); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 98 | result.sampleRate = getSampleRate(); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame^] | 99 | auto deviceIds = android::convertContainer<std::vector<int32_t>>( |
| 100 | getDeviceIds(), android::legacy2aidl_audio_port_handle_t_int32_t); |
| 101 | if (deviceIds.ok()) { |
| 102 | result.deviceIds = deviceIds.value(); |
| 103 | } else { |
| 104 | ALOGE("deviceIds (%s) legacy2aidl conversion failed", |
| 105 | android::toString(getDeviceIds()).c_str()); |
| 106 | result.deviceIds = {}; |
| 107 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 108 | static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(result.sharingMode)); |
| 109 | result.sharingMode = getSharingMode(); |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 110 | auto convAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(getFormat()); |
| 111 | if (convAudioFormat.ok()) { |
| 112 | result.audioFormat = convAudioFormat.value(); |
| 113 | } else { |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 114 | ALOGE("audioFormat (%s) legacy2aidl conversion failed", |
| 115 | audio_format_to_string(getFormat())); |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 116 | result.audioFormat = AudioFormatDescription{}; |
Mikhail Naganov | 57bd06f | 2021-08-10 16:41:54 -0700 | [diff] [blame] | 117 | result.audioFormat.type = |
| 118 | android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID; |
Mikhail Naganov | b60bd1b | 2021-07-15 17:31:43 -0700 | [diff] [blame] | 119 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 120 | static_assert(sizeof(aaudio_direction_t) == sizeof(result.direction)); |
| 121 | result.direction = getDirection(); |
| 122 | static_assert(sizeof(audio_usage_t) == sizeof(result.usage)); |
| 123 | result.usage = getUsage(); |
| 124 | static_assert(sizeof(aaudio_content_type_t) == sizeof(result.contentType)); |
| 125 | result.contentType = getContentType(); |
Jiabin Huang | 51a8a77 | 2024-10-30 21:57:48 +0000 | [diff] [blame] | 126 | std::optional<std::string> tags = getTags(); |
| 127 | result.tags = tags.has_value() ? tags.value() : ""; |
François Gaffie | eb1159f | 2022-12-05 08:38:26 +0100 | [diff] [blame] | 128 | static_assert( |
| 129 | sizeof(aaudio_spatialization_behavior_t) == sizeof(result.spatializationBehavior)); |
| 130 | result.spatializationBehavior = getSpatializationBehavior(); |
| 131 | result.isContentSpatialized = isContentSpatialized(); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 132 | static_assert(sizeof(aaudio_input_preset_t) == sizeof(result.inputPreset)); |
| 133 | result.inputPreset = getInputPreset(); |
| 134 | result.bufferCapacity = getBufferCapacity(); |
| 135 | static_assert(sizeof(aaudio_allowed_capture_policy_t) == sizeof(result.allowedCapturePolicy)); |
| 136 | result.allowedCapturePolicy = getAllowedCapturePolicy(); |
| 137 | static_assert(sizeof(aaudio_session_id_t) == sizeof(result.sessionId)); |
| 138 | result.sessionId = getSessionId(); |
| 139 | result.isPrivacySensitive = isPrivacySensitive(); |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 140 | result.hardwareSamplesPerFrame = getHardwareSamplesPerFrame(); |
| 141 | result.hardwareSampleRate = getHardwareSampleRate(); |
| 142 | auto convHardwareAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription( |
| 143 | getHardwareFormat()); |
| 144 | if (convHardwareAudioFormat.ok()) { |
| 145 | result.hardwareAudioFormat = convHardwareAudioFormat.value(); |
| 146 | } else { |
| 147 | ALOGE("hardwareAudioFormat (%s) legacy2aidl conversion failed", |
| 148 | audio_format_to_string(getHardwareFormat())); |
| 149 | result.hardwareAudioFormat = AudioFormatDescription{}; |
| 150 | result.hardwareAudioFormat.type = |
| 151 | android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID; |
| 152 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 153 | return result; |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 154 | } |