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