AAudioStreamParameters.cpp revision 901f65deb49786f4dffd39ac965c0bb681392c0b
1/*
2 * Copyright 2017 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
18#define LOG_TAG "AAudio"
19#include <utils/Log.h>
20#include <hardware/audio.h>
21
22#include "AAudioStreamParameters.h"
23
24using namespace aaudio;
25
26// TODO These defines should be moved to a central place in audio.
27#define SAMPLES_PER_FRAME_MIN        1
28// TODO Remove 8 channel limitation.
29#define SAMPLES_PER_FRAME_MAX        FCC_8
30#define SAMPLE_RATE_HZ_MIN           8000
31// HDMI supports up to 32 channels at 1536000 Hz.
32#define SAMPLE_RATE_HZ_MAX           1600000
33
34AAudioStreamParameters::AAudioStreamParameters() {}
35AAudioStreamParameters::~AAudioStreamParameters() {}
36
37aaudio_result_t AAudioStreamParameters::validate() const {
38    if (mSamplesPerFrame != AAUDIO_UNSPECIFIED
39        && (mSamplesPerFrame < SAMPLES_PER_FRAME_MIN || mSamplesPerFrame > SAMPLES_PER_FRAME_MAX)) {
40        ALOGE("AAudioStreamParameters: channelCount out of range = %d", mSamplesPerFrame);
41        return AAUDIO_ERROR_OUT_OF_RANGE;
42    }
43
44    if (mDeviceId < 0) {
45        ALOGE("AAudioStreamParameters: deviceId out of range = %d", mDeviceId);
46        return AAUDIO_ERROR_OUT_OF_RANGE;
47    }
48
49    switch (mSharingMode) {
50        case AAUDIO_SHARING_MODE_EXCLUSIVE:
51        case AAUDIO_SHARING_MODE_SHARED:
52            break;
53        default:
54            ALOGE("AAudioStreamParameters: illegal sharingMode = %d", mSharingMode);
55            return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
56            // break;
57    }
58
59    switch (mAudioFormat) {
60        case AAUDIO_FORMAT_UNSPECIFIED:
61        case AAUDIO_FORMAT_PCM_I16:
62        case AAUDIO_FORMAT_PCM_FLOAT:
63            break; // valid
64        default:
65            ALOGE("AAudioStreamParameters: audioFormat not valid = %d", mAudioFormat);
66            return AAUDIO_ERROR_INVALID_FORMAT;
67            // break;
68    }
69
70    if (mSampleRate != AAUDIO_UNSPECIFIED
71        && (mSampleRate < SAMPLE_RATE_HZ_MIN || mSampleRate > SAMPLE_RATE_HZ_MAX)) {
72        ALOGE("AAudioStreamParameters: sampleRate out of range = %d", mSampleRate);
73        return AAUDIO_ERROR_INVALID_RATE;
74    }
75
76    if (mBufferCapacity < 0) {
77        ALOGE("AAudioStreamParameters: bufferCapacity out of range = %d", mBufferCapacity);
78        return AAUDIO_ERROR_OUT_OF_RANGE;
79    }
80
81    return AAUDIO_OK;
82}
83
84void AAudioStreamParameters::dump() const {
85    ALOGD("AAudioStreamParameters mDeviceId        = %d", mDeviceId);
86    ALOGD("AAudioStreamParameters mSampleRate      = %d", mSampleRate);
87    ALOGD("AAudioStreamParameters mSamplesPerFrame = %d", mSamplesPerFrame);
88    ALOGD("AAudioStreamParameters mSharingMode     = %d", (int)mSharingMode);
89    ALOGD("AAudioStreamParameters mAudioFormat     = %d", (int)mAudioFormat);
90    ALOGD("AAudioStreamParameters mBufferCapacity  = %d", mBufferCapacity);
91}