Home | History | Annotate | Download | only in core
      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 "AAudioStreamParameters"
     19 #include <utils/Log.h>
     20 #include <system/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 
     34 AAudioStreamParameters::AAudioStreamParameters() {}
     35 AAudioStreamParameters::~AAudioStreamParameters() {}
     36 
     37 void AAudioStreamParameters::copyFrom(const AAudioStreamParameters &other) {
     38     mSamplesPerFrame      = other.mSamplesPerFrame;
     39     mSampleRate           = other.mSampleRate;
     40     mDeviceId             = other.mDeviceId;
     41     mSessionId            = other.mSessionId;
     42     mSharingMode          = other.mSharingMode;
     43     mAudioFormat          = other.mAudioFormat;
     44     mDirection            = other.mDirection;
     45     mBufferCapacity       = other.mBufferCapacity;
     46     mUsage                = other.mUsage;
     47     mContentType          = other.mContentType;
     48     mInputPreset          = other.mInputPreset;
     49     mAllowedCapturePolicy = other.mAllowedCapturePolicy;
     50 }
     51 
     52 static aaudio_result_t isFormatValid(audio_format_t format) {
     53     switch (format) {
     54         case AUDIO_FORMAT_DEFAULT:
     55         case AUDIO_FORMAT_PCM_16_BIT:
     56         case AUDIO_FORMAT_PCM_FLOAT:
     57             break; // valid
     58         default:
     59             ALOGD("audioFormat not valid, audio_format_t = 0x%08x", format);
     60             return AAUDIO_ERROR_INVALID_FORMAT;
     61             // break;
     62     }
     63     return AAUDIO_OK;
     64 }
     65 
     66 aaudio_result_t AAudioStreamParameters::validate() const {
     67     if (mSamplesPerFrame != AAUDIO_UNSPECIFIED
     68         && (mSamplesPerFrame < SAMPLES_PER_FRAME_MIN || mSamplesPerFrame > SAMPLES_PER_FRAME_MAX)) {
     69         ALOGD("channelCount out of range = %d", mSamplesPerFrame);
     70         return AAUDIO_ERROR_OUT_OF_RANGE;
     71     }
     72 
     73     if (mDeviceId < 0) {
     74         ALOGD("deviceId out of range = %d", mDeviceId);
     75         return AAUDIO_ERROR_OUT_OF_RANGE;
     76     }
     77 
     78     // All Session ID values are legal.
     79     switch (mSessionId) {
     80         case AAUDIO_SESSION_ID_NONE:
     81         case AAUDIO_SESSION_ID_ALLOCATE:
     82             break;
     83         default:
     84             break;
     85     }
     86 
     87     switch (mSharingMode) {
     88         case AAUDIO_SHARING_MODE_EXCLUSIVE:
     89         case AAUDIO_SHARING_MODE_SHARED:
     90             break;
     91         default:
     92             ALOGD("illegal sharingMode = %d", mSharingMode);
     93             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
     94             // break;
     95     }
     96 
     97     aaudio_result_t result = isFormatValid (mAudioFormat);
     98     if (result != AAUDIO_OK) return result;
     99 
    100     if (mSampleRate != AAUDIO_UNSPECIFIED
    101         && (mSampleRate < SAMPLE_RATE_HZ_MIN || mSampleRate > SAMPLE_RATE_HZ_MAX)) {
    102         ALOGD("sampleRate out of range = %d", mSampleRate);
    103         return AAUDIO_ERROR_INVALID_RATE;
    104     }
    105 
    106     if (mBufferCapacity < 0) {
    107         ALOGD("bufferCapacity out of range = %d", mBufferCapacity);
    108         return AAUDIO_ERROR_OUT_OF_RANGE;
    109     }
    110 
    111     switch (mDirection) {
    112         case AAUDIO_DIRECTION_INPUT:
    113         case AAUDIO_DIRECTION_OUTPUT:
    114             break; // valid
    115         default:
    116             ALOGD("direction not valid = %d", mDirection);
    117             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    118             // break;
    119     }
    120 
    121     switch (mUsage) {
    122         case AAUDIO_UNSPECIFIED:
    123         case AAUDIO_USAGE_MEDIA:
    124         case AAUDIO_USAGE_VOICE_COMMUNICATION:
    125         case AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
    126         case AAUDIO_USAGE_ALARM:
    127         case AAUDIO_USAGE_NOTIFICATION:
    128         case AAUDIO_USAGE_NOTIFICATION_RINGTONE:
    129         case AAUDIO_USAGE_NOTIFICATION_EVENT:
    130         case AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
    131         case AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
    132         case AAUDIO_USAGE_ASSISTANCE_SONIFICATION:
    133         case AAUDIO_USAGE_GAME:
    134         case AAUDIO_USAGE_ASSISTANT:
    135             break; // valid
    136         default:
    137             ALOGD("usage not valid = %d", mUsage);
    138             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    139             // break;
    140     }
    141 
    142     switch (mContentType) {
    143         case AAUDIO_UNSPECIFIED:
    144         case AAUDIO_CONTENT_TYPE_MUSIC:
    145         case AAUDIO_CONTENT_TYPE_MOVIE:
    146         case AAUDIO_CONTENT_TYPE_SONIFICATION:
    147         case AAUDIO_CONTENT_TYPE_SPEECH:
    148             break; // valid
    149         default:
    150             ALOGD("content type not valid = %d", mContentType);
    151             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    152             // break;
    153     }
    154 
    155     switch (mInputPreset) {
    156         case AAUDIO_UNSPECIFIED:
    157         case AAUDIO_INPUT_PRESET_GENERIC:
    158         case AAUDIO_INPUT_PRESET_CAMCORDER:
    159         case AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION:
    160         case AAUDIO_INPUT_PRESET_VOICE_RECOGNITION:
    161         case AAUDIO_INPUT_PRESET_UNPROCESSED:
    162         case AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE:
    163             break; // valid
    164         default:
    165             ALOGD("input preset not valid = %d", mInputPreset);
    166             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    167             // break;
    168     }
    169 
    170     switch (mAllowedCapturePolicy) {
    171         case AAUDIO_UNSPECIFIED:
    172         case AAUDIO_ALLOW_CAPTURE_BY_ALL:
    173         case AAUDIO_ALLOW_CAPTURE_BY_SYSTEM:
    174         case AAUDIO_ALLOW_CAPTURE_BY_NONE:
    175             break; // valid
    176         default:
    177             ALOGD("allowed capture policy not valid = %d", mAllowedCapturePolicy);
    178             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
    179             // break;
    180     }
    181 
    182     return AAUDIO_OK;
    183 }
    184 
    185 void AAudioStreamParameters::dump() const {
    186     ALOGD("mDeviceId             = %6d", mDeviceId);
    187     ALOGD("mSessionId            = %6d", mSessionId);
    188     ALOGD("mSampleRate           = %6d", mSampleRate);
    189     ALOGD("mSamplesPerFrame      = %6d", mSamplesPerFrame);
    190     ALOGD("mSharingMode          = %6d", (int)mSharingMode);
    191     ALOGD("mAudioFormat          = %6d", (int)mAudioFormat);
    192     ALOGD("mDirection            = %6d", mDirection);
    193     ALOGD("mBufferCapacity       = %6d", mBufferCapacity);
    194     ALOGD("mUsage                = %6d", mUsage);
    195     ALOGD("mContentType          = %6d", mContentType);
    196     ALOGD("mInputPreset          = %6d", mInputPreset);
    197     ALOGD("mAllowedCapturePolicy = %6d", mAllowedCapturePolicy);
    198 }
    199