blob: c53a897f6767baf60768c3bc2f0deae16153e234 [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
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 Burk0127c1b2018-03-29 13:48:06 -070017#define LOG_TAG "AAudioStreamConfiguration"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk5ed503c2017-02-01 09:38:15 -080021#include <stdint.h>
22
23#include <sys/mman.h>
Phil Burka4eb0d82017-04-12 15:44:06 -070024#include <aaudio/AAudio.h>
25
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -070026#include <media/AidlConversion.h>
27
Phil Burk5ed503c2017-02-01 09:38:15 -080028#include "binding/AAudioStreamConfiguration.h"
29
Phil Burk5ed503c2017-02-01 09:38:15 -080030using namespace aaudio;
31
Mikhail Naganov57bd06f2021-08-10 16:41:54 -070032using android::media::audio::common::AudioFormatDescription;
Phil Burk5ed503c2017-02-01 09:38:15 -080033
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070034AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& parcelable) {
jiabina9094092021-06-28 20:36:45 +000035 setChannelMask(parcelable.channelMask);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070036 setSampleRate(parcelable.sampleRate);
37 setDeviceId(parcelable.deviceId);
38 static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(parcelable.sharingMode));
39 setSharingMode(parcelable.sharingMode);
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -070040 auto convFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t(
41 parcelable.audioFormat);
42 setFormat(convFormat.ok() ? convFormat.value() : AUDIO_FORMAT_INVALID);
Robert Wu310037a2022-09-06 21:48:18 +000043 if (!convFormat.ok()) {
44 ALOGE("audioFormat (%s) aidl2legacy conversion failed",
45 parcelable.hardwareAudioFormat.toString().c_str());
46 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070047 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 Huang51a8a772024-10-30 21:57:48 +000053 setTags(parcelable.tags);
Jean-Michel Trivi656bfdc2021-09-20 18:42:37 -070054 static_assert(sizeof(aaudio_spatialization_behavior_t) ==
55 sizeof(parcelable.spatializationBehavior));
56 setSpatializationBehavior(parcelable.spatializationBehavior);
57 setIsContentSpatialized(parcelable.isContentSpatialized);
58
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070059 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 Wu310037a2022-09-06 21:48:18 +000068 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 Burk5ed503c2017-02-01 09:38:15 -080077}
78
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070079AAudioStreamConfiguration&
80AAudioStreamConfiguration::operator=(const StreamParameters& parcelable) {
81 this->~AAudioStreamConfiguration();
82 new (this) AAudioStreamConfiguration(parcelable);
83 return *this;
84}
Phil Burk0127c1b2018-03-29 13:48:06 -070085
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070086StreamParameters AAudioStreamConfiguration::parcelable() const {
87 StreamParameters result;
jiabina9094092021-06-28 20:36:45 +000088 result.channelMask = getChannelMask();
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070089 result.sampleRate = getSampleRate();
90 result.deviceId = getDeviceId();
91 static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(result.sharingMode));
92 result.sharingMode = getSharingMode();
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -070093 auto convAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(getFormat());
94 if (convAudioFormat.ok()) {
95 result.audioFormat = convAudioFormat.value();
96 } else {
Robert Wu310037a2022-09-06 21:48:18 +000097 ALOGE("audioFormat (%s) legacy2aidl conversion failed",
98 audio_format_to_string(getFormat()));
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -070099 result.audioFormat = AudioFormatDescription{};
Mikhail Naganov57bd06f2021-08-10 16:41:54 -0700100 result.audioFormat.type =
101 android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID;
Mikhail Naganovb60bd1b2021-07-15 17:31:43 -0700102 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700103 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 Huang51a8a772024-10-30 21:57:48 +0000109 std::optional<std::string> tags = getTags();
110 result.tags = tags.has_value() ? tags.value() : "";
François Gaffieeb1159f2022-12-05 08:38:26 +0100111 static_assert(
112 sizeof(aaudio_spatialization_behavior_t) == sizeof(result.spatializationBehavior));
113 result.spatializationBehavior = getSpatializationBehavior();
114 result.isContentSpatialized = isContentSpatialized();
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700115 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 Wu310037a2022-09-06 21:48:18 +0000123 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-Tsvic5f45872020-08-18 10:39:44 -0700136 return result;
Phil Burk15f7cab2017-08-04 09:13:31 -0700137}