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
17 #define LOG_TAG "AAudioStreamConfiguration"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <stdint.h>
22
23 #include <sys/mman.h>
24 #include <aaudio/AAudio.h>
25
26 #include <media/AidlConversion.h>
27
28 #include "binding/AAudioStreamConfiguration.h"
29
30 using namespace aaudio;
31
32 using android::media::audio::common::AudioFormatDescription;
33
AAudioStreamConfiguration(const StreamParameters & parcelable)34 AAudioStreamConfiguration::AAudioStreamConfiguration(const StreamParameters& parcelable) {
35 setChannelMask(parcelable.channelMask);
36 setSampleRate(parcelable.sampleRate);
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 }
47 static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(parcelable.sharingMode));
48 setSharingMode(parcelable.sharingMode);
49 auto convFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t(
50 parcelable.audioFormat);
51 setFormat(convFormat.ok() ? convFormat.value() : AUDIO_FORMAT_INVALID);
52 if (!convFormat.ok()) {
53 ALOGE("audioFormat (%s) aidl2legacy conversion failed",
54 parcelable.hardwareAudioFormat.toString().c_str());
55 }
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);
62 setTags(std::set(parcelable.tags.begin(), parcelable.tags.end()));
63 static_assert(sizeof(aaudio_spatialization_behavior_t) ==
64 sizeof(parcelable.spatializationBehavior));
65 setSpatializationBehavior(parcelable.spatializationBehavior);
66 setIsContentSpatialized(parcelable.isContentSpatialized);
67
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);
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 }
86 }
87
88 AAudioStreamConfiguration&
operator =(const StreamParameters & parcelable)89 AAudioStreamConfiguration::operator=(const StreamParameters& parcelable) {
90 this->~AAudioStreamConfiguration();
91 new (this) AAudioStreamConfiguration(parcelable);
92 return *this;
93 }
94
parcelable() const95 StreamParameters AAudioStreamConfiguration::parcelable() const {
96 StreamParameters result;
97 result.channelMask = getChannelMask();
98 result.sampleRate = getSampleRate();
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 }
108 static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(result.sharingMode));
109 result.sharingMode = getSharingMode();
110 auto convAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(getFormat());
111 if (convAudioFormat.ok()) {
112 result.audioFormat = convAudioFormat.value();
113 } else {
114 ALOGE("audioFormat (%s) legacy2aidl conversion failed",
115 audio_format_to_string(getFormat()));
116 result.audioFormat = AudioFormatDescription{};
117 result.audioFormat.type =
118 android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID;
119 }
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();
126 auto tags = getTags();
127 result.tags = std::vector(tags.begin(), tags.end());
128 static_assert(
129 sizeof(aaudio_spatialization_behavior_t) == sizeof(result.spatializationBehavior));
130 result.spatializationBehavior = getSpatializationBehavior();
131 result.isContentSpatialized = isContentSpatialized();
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();
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 }
153 return result;
154 }
155