AAudioStreamConfiguration.cpp revision a4eb0d86a29be2763be5fac51727858d5095794b
1204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk/*
2204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Copyright 2016 The Android Open Source Project
3204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
4204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * you may not use this file except in compliance with the License.
6204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * You may obtain a copy of the License at
7204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
8204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk *
10204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * Unless required by applicable law or agreed to in writing, software
11204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * See the License for the specific language governing permissions and
14204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk * limitations under the License.
15204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk */
16204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
17204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <stdint.h>
18204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
19204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <sys/mman.h>
20a4eb0d86a29be2763be5fac51727858d5095794bPhil Burk#include <aaudio/AAudio.h>
21a4eb0d86a29be2763be5fac51727858d5095794bPhil Burk
22204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <binder/Parcel.h>
23204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk#include <binder/Parcelable.h>
24204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
255ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "binding/AAudioStreamConfiguration.h"
26204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
27204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::NO_ERROR;
28204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::status_t;
29204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::Parcel;
30204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burkusing android::Parcelable;
31204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkusing namespace aaudio;
33204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil BurkAAudioStreamConfiguration::AAudioStreamConfiguration() {}
355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil BurkAAudioStreamConfiguration::~AAudioStreamConfiguration() {}
36204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkstatus_t AAudioStreamConfiguration::writeToParcel(Parcel* parcel) const {
38c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status_t status;
39c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32(mDeviceId);
40c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
41c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32(mSampleRate);
42c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
43c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32(mSamplesPerFrame);
44c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
45c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32((int32_t) mSharingMode);
46c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration.writeToParcel(): mSharingMode = %d", mSharingMode);
47c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
48c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32((int32_t) mAudioFormat);
49c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
50c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32(mBufferCapacity);
51c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
52c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return NO_ERROR;
53c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkerror:
54c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGE("AAudioStreamConfiguration.writeToParcel(): write failed = %d", status);
55c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return status;
56204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
57204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkstatus_t AAudioStreamConfiguration::readFromParcel(const Parcel* parcel) {
59204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    int32_t temp;
60c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status_t status = parcel->readInt32(&mDeviceId);
61c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
62c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&mSampleRate);
63c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
64c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&mSamplesPerFrame);
65c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
66c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&temp);
67c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
68c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    mSharingMode = (aaudio_sharing_mode_t) temp;
69c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration.readFromParcel(): mSharingMode = %d", mSharingMode);
70c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&temp);
71c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    mAudioFormat = (aaudio_audio_format_t) temp;
73c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&mBufferCapacity);
74c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
75c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return NO_ERROR;
76c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkerror:
77c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGE("AAudioStreamConfiguration.readFromParcel(): read failed = %d", status);
78c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return status;
79204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
80204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
81c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkaaudio_result_t AAudioStreamConfiguration::validate() const {
82204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    // Validate results of the open.
83204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSampleRate < 0 || mSampleRate >= 8 * 48000) { // TODO review limits
845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        ALOGE("AAudioStreamConfiguration.validate(): invalid sampleRate = %d", mSampleRate);
855ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
86204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
87204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
88204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSamplesPerFrame < 1 || mSamplesPerFrame >= 32) { // TODO review limits
895ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        ALOGE("AAudioStreamConfiguration.validate() invalid samplesPerFrame = %d", mSamplesPerFrame);
905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
91204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
92204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
93204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    switch (mAudioFormat) {
945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    case AAUDIO_FORMAT_PCM_I16:
955ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    case AAUDIO_FORMAT_PCM_FLOAT:
965ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    case AAUDIO_FORMAT_PCM_I8_24:
975ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    case AAUDIO_FORMAT_PCM_I32:
98204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        break;
99204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    default:
1005ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        ALOGE("AAudioStreamConfiguration.validate() invalid audioFormat = %d", mAudioFormat);
1015ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
102204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
1033df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk
1043df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    if (mBufferCapacity < 0) {
1053df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        ALOGE("AAudioStreamConfiguration.validate() invalid mBufferCapacity = %d", mBufferCapacity);
1063df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        return AAUDIO_ERROR_INTERNAL;
1073df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    }
1085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return AAUDIO_OK;
109204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
110204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
111c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkvoid AAudioStreamConfiguration::dump() const {
112c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration mDeviceId        = %d", mDeviceId);
113c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration mSampleRate      = %d", mSampleRate);
1145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    ALOGD("AAudioStreamConfiguration mSamplesPerFrame = %d", mSamplesPerFrame);
115c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration mSharingMode     = %d", (int)mSharingMode);
1163df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    ALOGD("AAudioStreamConfiguration mAudioFormat     = %d", (int)mAudioFormat);
1173df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    ALOGD("AAudioStreamConfiguration mBufferCapacity  = %d", mBufferCapacity);
118204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
119