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    if (status != NO_ERROR) goto error;
47c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32((int32_t) mAudioFormat);
48c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
49c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->writeInt32(mBufferCapacity);
50c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
51c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return NO_ERROR;
52c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkerror:
53c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGE("AAudioStreamConfiguration.writeToParcel(): write failed = %d", status);
54c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return status;
55204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
56204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkstatus_t AAudioStreamConfiguration::readFromParcel(const Parcel* parcel) {
58c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status_t status = parcel->readInt32(&mDeviceId);
59c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
60c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&mSampleRate);
61c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
62c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&mSamplesPerFrame);
63c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
649dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk    status = parcel->readInt32(&mSharingMode);
65c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
669dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk    status = parcel->readInt32(&mAudioFormat);
67c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
68c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    status = parcel->readInt32(&mBufferCapacity);
69c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    if (status != NO_ERROR) goto error;
70c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return NO_ERROR;
71c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkerror:
72c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGE("AAudioStreamConfiguration.readFromParcel(): read failed = %d", status);
73c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    return status;
74204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
75204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
76c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkaaudio_result_t AAudioStreamConfiguration::validate() const {
77204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    // Validate results of the open.
78204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSampleRate < 0 || mSampleRate >= 8 * 48000) { // TODO review limits
795ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        ALOGE("AAudioStreamConfiguration.validate(): invalid sampleRate = %d", mSampleRate);
805ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
81204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
82204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
83204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    if (mSamplesPerFrame < 1 || mSamplesPerFrame >= 32) { // TODO review limits
845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        ALOGE("AAudioStreamConfiguration.validate() invalid samplesPerFrame = %d", mSamplesPerFrame);
855ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
86204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
87204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
88204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    switch (mAudioFormat) {
895ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    case AAUDIO_FORMAT_PCM_I16:
905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    case AAUDIO_FORMAT_PCM_FLOAT:
91204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk        break;
92204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    default:
935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        ALOGE("AAudioStreamConfiguration.validate() invalid audioFormat = %d", mAudioFormat);
945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_INTERNAL;
95204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk    }
963df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk
973df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    if (mBufferCapacity < 0) {
983df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        ALOGE("AAudioStreamConfiguration.validate() invalid mBufferCapacity = %d", mBufferCapacity);
993df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk        return AAUDIO_ERROR_INTERNAL;
1003df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    }
1015ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return AAUDIO_OK;
102204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
103204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk
104c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burkvoid AAudioStreamConfiguration::dump() const {
105c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration mDeviceId        = %d", mDeviceId);
106c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration mSampleRate      = %d", mSampleRate);
1075ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    ALOGD("AAudioStreamConfiguration mSamplesPerFrame = %d", mSamplesPerFrame);
108c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    ALOGD("AAudioStreamConfiguration mSharingMode     = %d", (int)mSharingMode);
1093df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    ALOGD("AAudioStreamConfiguration mAudioFormat     = %d", (int)mAudioFormat);
1103df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk    ALOGD("AAudioStreamConfiguration mBufferCapacity  = %d", mBufferCapacity);
111204a163c86f357a878873fe7d4c4164f3d55c9b6Phil Burk}
112