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#include <stdint.h>
18
19#include <sys/mman.h>
20#include <aaudio/AAudio.h>
21
22#include <binder/Parcel.h>
23#include <binder/Parcelable.h>
24
25#include "binding/AAudioStreamConfiguration.h"
26
27using android::NO_ERROR;
28using android::status_t;
29using android::Parcel;
30using android::Parcelable;
31
32using namespace aaudio;
33
34AAudioStreamConfiguration::AAudioStreamConfiguration() {}
35AAudioStreamConfiguration::~AAudioStreamConfiguration() {}
36
37status_t AAudioStreamConfiguration::writeToParcel(Parcel* parcel) const {
38    status_t status;
39    status = parcel->writeInt32(mDeviceId);
40    if (status != NO_ERROR) goto error;
41    status = parcel->writeInt32(mSampleRate);
42    if (status != NO_ERROR) goto error;
43    status = parcel->writeInt32(mSamplesPerFrame);
44    if (status != NO_ERROR) goto error;
45    status = parcel->writeInt32((int32_t) mSharingMode);
46    if (status != NO_ERROR) goto error;
47    status = parcel->writeInt32((int32_t) mAudioFormat);
48    if (status != NO_ERROR) goto error;
49    status = parcel->writeInt32(mBufferCapacity);
50    if (status != NO_ERROR) goto error;
51    return NO_ERROR;
52error:
53    ALOGE("AAudioStreamConfiguration.writeToParcel(): write failed = %d", status);
54    return status;
55}
56
57status_t AAudioStreamConfiguration::readFromParcel(const Parcel* parcel) {
58    status_t status = parcel->readInt32(&mDeviceId);
59    if (status != NO_ERROR) goto error;
60    status = parcel->readInt32(&mSampleRate);
61    if (status != NO_ERROR) goto error;
62    status = parcel->readInt32(&mSamplesPerFrame);
63    if (status != NO_ERROR) goto error;
64    status = parcel->readInt32(&mSharingMode);
65    if (status != NO_ERROR) goto error;
66    status = parcel->readInt32(&mAudioFormat);
67    if (status != NO_ERROR) goto error;
68    status = parcel->readInt32(&mBufferCapacity);
69    if (status != NO_ERROR) goto error;
70    return NO_ERROR;
71error:
72    ALOGE("AAudioStreamConfiguration.readFromParcel(): read failed = %d", status);
73    return status;
74}
75
76aaudio_result_t AAudioStreamConfiguration::validate() const {
77    // Validate results of the open.
78    if (mSampleRate < 0 || mSampleRate >= 8 * 48000) { // TODO review limits
79        ALOGE("AAudioStreamConfiguration.validate(): invalid sampleRate = %d", mSampleRate);
80        return AAUDIO_ERROR_INTERNAL;
81    }
82
83    if (mSamplesPerFrame < 1 || mSamplesPerFrame >= 32) { // TODO review limits
84        ALOGE("AAudioStreamConfiguration.validate() invalid samplesPerFrame = %d", mSamplesPerFrame);
85        return AAUDIO_ERROR_INTERNAL;
86    }
87
88    switch (mAudioFormat) {
89    case AAUDIO_FORMAT_PCM_I16:
90    case AAUDIO_FORMAT_PCM_FLOAT:
91        break;
92    default:
93        ALOGE("AAudioStreamConfiguration.validate() invalid audioFormat = %d", mAudioFormat);
94        return AAUDIO_ERROR_INTERNAL;
95    }
96
97    if (mBufferCapacity < 0) {
98        ALOGE("AAudioStreamConfiguration.validate() invalid mBufferCapacity = %d", mBufferCapacity);
99        return AAUDIO_ERROR_INTERNAL;
100    }
101    return AAUDIO_OK;
102}
103
104void AAudioStreamConfiguration::dump() const {
105    ALOGD("AAudioStreamConfiguration mDeviceId        = %d", mDeviceId);
106    ALOGD("AAudioStreamConfiguration mSampleRate      = %d", mSampleRate);
107    ALOGD("AAudioStreamConfiguration mSamplesPerFrame = %d", mSamplesPerFrame);
108    ALOGD("AAudioStreamConfiguration mSharingMode     = %d", (int)mSharingMode);
109    ALOGD("AAudioStreamConfiguration mAudioFormat     = %d", (int)mAudioFormat);
110    ALOGD("AAudioStreamConfiguration mBufferCapacity  = %d", mBufferCapacity);
111}
112