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     setDeviceId(parcelable.deviceId);
38     static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(parcelable.sharingMode));
39     setSharingMode(parcelable.sharingMode);
40     auto convFormat = android::aidl2legacy_AudioFormatDescription_audio_format_t(
41             parcelable.audioFormat);
42     setFormat(convFormat.ok() ? convFormat.value() : AUDIO_FORMAT_INVALID);
43     if (!convFormat.ok()) {
44         ALOGE("audioFormat (%s) aidl2legacy conversion failed",
45               parcelable.hardwareAudioFormat.toString().c_str());
46     }
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);
53 
54     static_assert(sizeof(aaudio_spatialization_behavior_t) ==
55             sizeof(parcelable.spatializationBehavior));
56     setSpatializationBehavior(parcelable.spatializationBehavior);
57     setIsContentSpatialized(parcelable.isContentSpatialized);
58 
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);
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     }
77 }
78 
79 AAudioStreamConfiguration&
operator =(const StreamParameters & parcelable)80 AAudioStreamConfiguration::operator=(const StreamParameters& parcelable) {
81     this->~AAudioStreamConfiguration();
82     new (this) AAudioStreamConfiguration(parcelable);
83     return *this;
84 }
85 
parcelable() const86 StreamParameters AAudioStreamConfiguration::parcelable() const {
87     StreamParameters result;
88     result.channelMask = getChannelMask();
89     result.sampleRate = getSampleRate();
90     result.deviceId = getDeviceId();
91     static_assert(sizeof(aaudio_sharing_mode_t) == sizeof(result.sharingMode));
92     result.sharingMode = getSharingMode();
93     auto convAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(getFormat());
94     if (convAudioFormat.ok()) {
95         result.audioFormat = convAudioFormat.value();
96     } else {
97         ALOGE("audioFormat (%s) legacy2aidl conversion failed",
98               audio_format_to_string(getFormat()));
99         result.audioFormat = AudioFormatDescription{};
100         result.audioFormat.type =
101                 android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID;
102     }
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();
109     static_assert(
110             sizeof(aaudio_spatialization_behavior_t) == sizeof(result.spatializationBehavior));
111     result.spatializationBehavior = getSpatializationBehavior();
112     result.isContentSpatialized = isContentSpatialized();
113     static_assert(sizeof(aaudio_input_preset_t) == sizeof(result.inputPreset));
114     result.inputPreset = getInputPreset();
115     result.bufferCapacity = getBufferCapacity();
116     static_assert(sizeof(aaudio_allowed_capture_policy_t) == sizeof(result.allowedCapturePolicy));
117     result.allowedCapturePolicy = getAllowedCapturePolicy();
118     static_assert(sizeof(aaudio_session_id_t) == sizeof(result.sessionId));
119     result.sessionId = getSessionId();
120     result.isPrivacySensitive = isPrivacySensitive();
121     result.hardwareSamplesPerFrame = getHardwareSamplesPerFrame();
122     result.hardwareSampleRate = getHardwareSampleRate();
123     auto convHardwareAudioFormat = android::legacy2aidl_audio_format_t_AudioFormatDescription(
124             getHardwareFormat());
125     if (convHardwareAudioFormat.ok()) {
126         result.hardwareAudioFormat = convHardwareAudioFormat.value();
127     } else {
128         ALOGE("hardwareAudioFormat (%s) legacy2aidl conversion failed",
129               audio_format_to_string(getHardwareFormat()));
130         result.hardwareAudioFormat = AudioFormatDescription{};
131         result.hardwareAudioFormat.type =
132                 android::media::audio::common::AudioFormatType::SYS_RESERVED_INVALID;
133     }
134     return result;
135 }
136