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
24 using 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
AAudioStreamParameters()34 AAudioStreamParameters::AAudioStreamParameters() {}
~AAudioStreamParameters()35 AAudioStreamParameters::~AAudioStreamParameters() {}
36
copyFrom(const AAudioStreamParameters & other)37 void AAudioStreamParameters::copyFrom(const AAudioStreamParameters &other) {
38 mSamplesPerFrame = other.mSamplesPerFrame;
39 mSampleRate = other.mSampleRate;
40 mDeviceId = other.mDeviceId;
41 mSharingMode = other.mSharingMode;
42 mAudioFormat = other.mAudioFormat;
43 mDirection = other.mDirection;
44 mBufferCapacity = other.mBufferCapacity;
45 }
46
validate() const47 aaudio_result_t AAudioStreamParameters::validate() const {
48 if (mSamplesPerFrame != AAUDIO_UNSPECIFIED
49 && (mSamplesPerFrame < SAMPLES_PER_FRAME_MIN || mSamplesPerFrame > SAMPLES_PER_FRAME_MAX)) {
50 ALOGE("AAudioStreamParameters: channelCount out of range = %d", mSamplesPerFrame);
51 return AAUDIO_ERROR_OUT_OF_RANGE;
52 }
53
54 if (mDeviceId < 0) {
55 ALOGE("AAudioStreamParameters: deviceId out of range = %d", mDeviceId);
56 return AAUDIO_ERROR_OUT_OF_RANGE;
57 }
58
59 switch (mSharingMode) {
60 case AAUDIO_SHARING_MODE_EXCLUSIVE:
61 case AAUDIO_SHARING_MODE_SHARED:
62 break;
63 default:
64 ALOGE("AAudioStreamParameters: illegal sharingMode = %d", mSharingMode);
65 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
66 // break;
67 }
68
69 switch (mAudioFormat) {
70 case AAUDIO_FORMAT_UNSPECIFIED:
71 case AAUDIO_FORMAT_PCM_I16:
72 case AAUDIO_FORMAT_PCM_FLOAT:
73 break; // valid
74 default:
75 ALOGE("AAudioStreamParameters: audioFormat not valid = %d", mAudioFormat);
76 return AAUDIO_ERROR_INVALID_FORMAT;
77 // break;
78 }
79
80 if (mSampleRate != AAUDIO_UNSPECIFIED
81 && (mSampleRate < SAMPLE_RATE_HZ_MIN || mSampleRate > SAMPLE_RATE_HZ_MAX)) {
82 ALOGE("AAudioStreamParameters: sampleRate out of range = %d", mSampleRate);
83 return AAUDIO_ERROR_INVALID_RATE;
84 }
85
86 if (mBufferCapacity < 0) {
87 ALOGE("AAudioStreamParameters: bufferCapacity out of range = %d", mBufferCapacity);
88 return AAUDIO_ERROR_OUT_OF_RANGE;
89 }
90
91 switch (mDirection) {
92 case AAUDIO_DIRECTION_INPUT:
93 case AAUDIO_DIRECTION_OUTPUT:
94 break; // valid
95 default:
96 ALOGE("AAudioStreamParameters: direction not valid = %d", mDirection);
97 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
98 // break;
99 }
100
101 return AAUDIO_OK;
102 }
103
dump() const104 void AAudioStreamParameters::dump() const {
105 ALOGD("AAudioStreamParameters mDeviceId = %d", mDeviceId);
106 ALOGD("AAudioStreamParameters mSampleRate = %d", mSampleRate);
107 ALOGD("AAudioStreamParameters mSamplesPerFrame = %d", mSamplesPerFrame);
108 ALOGD("AAudioStreamParameters mSharingMode = %d", (int)mSharingMode);
109 ALOGD("AAudioStreamParameters mAudioFormat = %d", (int)mAudioFormat);
110 ALOGD("AAudioStreamParameters mDirection = %d", mDirection);
111 ALOGD("AAudioStreamParameters mBufferCapacity = %d", mBufferCapacity);
112 }
113
114