// static status_t AudioRecord::getMinFrameCount( size_t* frameCount, uint32_t sampleRate, audio_format_t format, audio_channel_mask_t channelMask) { if (frameCount == NULL) { return BAD_VALUE; } size_t size; status_t status = AudioSystem::getInputBufferSize(sampleRate, format, channelMask, &size); if (status != NO_ERROR) { ALOGE("AudioSystem could not query the input buffer size for sampleRate %u, format %#x, " "channelMask %#x; status %d", sampleRate, format, channelMask, status); return status; } // handle non-linear-pcm formats and update frameCount if (!audio_is_linear_pcm(format)) { *frameCount = (size * 2) / sizeof(uint8_t); return NO_ERROR; } // We double the size of input buffer for ping pong use of record buffer. // Assumes audio_is_linear_pcm(format) if ((*frameCount = (size * 2) / (audio_channel_count_from_in_mask(channelMask) * audio_bytes_per_sample(format))) == 0) { ALOGE("Unsupported configuration: sampleRate %u, format %#x, channelMask %#x", sampleRate, format, channelMask); return BAD_VALUE; } return NO_ERROR; }
status_t AudioGain::checkConfig(const struct audio_gain_config *config) { if ((config->mode & ~mGain.mode) != 0) { return BAD_VALUE; } if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { if ((config->values[0] < mGain.min_value) || (config->values[0] > mGain.max_value)) { return BAD_VALUE; } } else { if ((config->channel_mask & ~mGain.channel_mask) != 0) { return BAD_VALUE; } uint32_t numValues; if (mUseInChannelMask) { numValues = audio_channel_count_from_in_mask(config->channel_mask); } else { numValues = audio_channel_count_from_out_mask(config->channel_mask); } for (size_t i = 0; i < numValues; i++) { if ((config->values[i] < mGain.min_value) || (config->values[i] > mGain.max_value)) { return BAD_VALUE; } } } if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { if ((config->ramp_duration_ms < mGain.min_ramp_ms) || (config->ramp_duration_ms > mGain.max_ramp_ms)) { return BAD_VALUE; } } return NO_ERROR; }
ssize_t AudioStreamInStub::read(void* buffer, ssize_t bytes) { // fake timing for audio input usleep(bytes * 1000000 / sizeof(int16_t) / audio_channel_count_from_in_mask(channels()) / sampleRate()); memset(buffer, 0, bytes); return bytes; }
void AudioPort::pickChannelMask(audio_channel_mask_t &pickedChannelMask, const ChannelsVector &channelMasks) const { pickedChannelMask = AUDIO_CHANNEL_NONE; // For direct outputs, pick minimum channel count: this helps ensuring that the // channel count / sampling rate combination chosen will be supported by the connected // sink if (isDirectOutput()) { uint32_t channelCount = UINT_MAX; for (size_t i = 0; i < channelMasks.size(); i ++) { uint32_t cnlCount; if (useInputChannelMask()) { cnlCount = audio_channel_count_from_in_mask(channelMasks[i]); } else { cnlCount = audio_channel_count_from_out_mask(channelMasks[i]); } if ((cnlCount < channelCount) && (cnlCount > 0)) { pickedChannelMask = channelMasks[i]; channelCount = cnlCount; } } } else { uint32_t channelCount = 0; uint32_t maxCount = MAX_MIXER_CHANNEL_COUNT; // For mixed output and inputs, use max mixer channel count. Do not // limit channel count otherwise if (mType != AUDIO_PORT_TYPE_MIX) { maxCount = UINT_MAX; } for (size_t i = 0; i < channelMasks.size(); i ++) { uint32_t cnlCount; if (useInputChannelMask()) { cnlCount = audio_channel_count_from_in_mask(channelMasks[i]); } else { cnlCount = audio_channel_count_from_out_mask(channelMasks[i]); } if ((cnlCount > channelCount) && (cnlCount <= maxCount)) { pickedChannelMask = channelMasks[i]; channelCount = cnlCount; } } } }
void AudioGain::getDefaultConfig(struct audio_gain_config *config) { config->index = mIndex; config->mode = mGain.mode; config->channel_mask = mGain.channel_mask; if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) { config->values[0] = mGain.default_value; } else { uint32_t numValues; if (mUseInChannelMask) { numValues = audio_channel_count_from_in_mask(mGain.channel_mask); } else { numValues = audio_channel_count_from_out_mask(mGain.channel_mask); } for (size_t i = 0; i < numValues; i++) { config->values[i] = mGain.default_value; } } if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) { config->ramp_duration_ms = mGain.min_ramp_ms; } }
status_t AudioRecord::set( audio_source_t inputSource, uint32_t sampleRate, audio_format_t format, audio_channel_mask_t channelMask, size_t frameCount, callback_t cbf, void* user, uint32_t notificationFrames, bool threadCanCallJava, int sessionId, transfer_type transferType, audio_input_flags_t flags) { ALOGV("set(): inputSource %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, " "notificationFrames %u, sessionId %d, transferType %d, flags %#x", inputSource, sampleRate, format, channelMask, frameCount, notificationFrames, sessionId, transferType, flags); switch (transferType) { case TRANSFER_DEFAULT: if (cbf == NULL || threadCanCallJava) { transferType = TRANSFER_SYNC; } else { transferType = TRANSFER_CALLBACK; } break; case TRANSFER_CALLBACK: if (cbf == NULL) { ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL"); return BAD_VALUE; } break; case TRANSFER_OBTAIN: case TRANSFER_SYNC: break; default: ALOGE("Invalid transfer type %d", transferType); return BAD_VALUE; } mTransfer = transferType; AutoMutex lock(mLock); // invariant that mAudioRecord != 0 is true only after set() returns successfully if (mAudioRecord != 0) { ALOGE("Track already in use"); return INVALID_OPERATION; } // handle default values first. if (inputSource == AUDIO_SOURCE_DEFAULT) { inputSource = AUDIO_SOURCE_MIC; } mInputSource = inputSource; if (sampleRate == 0) { ALOGE("Invalid sample rate %u", sampleRate); return BAD_VALUE; } mSampleRate = sampleRate; // these below should probably come from the audioFlinger too... if (format == AUDIO_FORMAT_DEFAULT) { format = AUDIO_FORMAT_PCM_16_BIT; } // validate parameters if (!audio_is_valid_format(format)) { ALOGE("Invalid format %#x", format); return BAD_VALUE; } // Temporary restriction: AudioFlinger currently supports 16-bit PCM only if (format != AUDIO_FORMAT_PCM_16_BIT) { ALOGE("Format %#x is not supported", format); return BAD_VALUE; } mFormat = format; if (!audio_is_input_channel(channelMask)) { ALOGE("Invalid channel mask %#x", channelMask); return BAD_VALUE; } mChannelMask = channelMask; uint32_t channelCount = audio_channel_count_from_in_mask(channelMask); mChannelCount = channelCount; if (audio_is_linear_pcm(format)) { mFrameSize = channelCount * audio_bytes_per_sample(format); } else { mFrameSize = sizeof(uint8_t); } // mFrameCount is initialized in openRecord_l mReqFrameCount = frameCount; mNotificationFramesReq = notificationFrames; // mNotificationFramesAct is initialized in openRecord_l if (sessionId == AUDIO_SESSION_ALLOCATE) { mSessionId = AudioSystem::newAudioUniqueId(); } else { mSessionId = sessionId; } ALOGV("set(): mSessionId %d", mSessionId); mFlags = flags; mCbf = cbf; if (cbf != NULL) { mAudioRecordThread = new AudioRecordThread(*this, threadCanCallJava); mAudioRecordThread->run("AudioRecord", ANDROID_PRIORITY_AUDIO); } // create the IAudioRecord status_t status = openRecord_l(0 /*epoch*/); if (status != NO_ERROR) { if (mAudioRecordThread != 0) { mAudioRecordThread->requestExit(); // see comment in AudioRecord.h mAudioRecordThread->requestExitAndWait(); mAudioRecordThread.clear(); } return status; } mStatus = NO_ERROR; mActive = false; mUserData = user; // TODO: add audio hardware input latency here mLatency = (1000*mFrameCount) / sampleRate; mMarkerPosition = 0; mMarkerReached = false; mNewPosition = 0; mUpdatePeriod = 0; AudioSystem::acquireAudioSessionId(mSessionId, -1); mSequence = 1; mObservedSequence = mSequence; mInOverrun = false; return NO_ERROR; }
static int adev_open_input_stream(struct audio_hw_device *dev, audio_io_handle_t handle, audio_devices_t devices, struct audio_config *config, struct audio_stream_in **stream_in, audio_input_flags_t flags __unused, const char *address /*__unused*/, audio_source_t source __unused) { ALOGV("in adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8, config->sample_rate, config->channel_mask, config->format); struct stream_in *in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); int ret = 0; if (in == NULL) return -ENOMEM; /* setup function pointers */ in->stream.common.get_sample_rate = in_get_sample_rate; in->stream.common.set_sample_rate = in_set_sample_rate; in->stream.common.get_buffer_size = in_get_buffer_size; in->stream.common.get_channels = in_get_channels; in->stream.common.get_format = in_get_format; in->stream.common.set_format = in_set_format; in->stream.common.standby = in_standby; in->stream.common.dump = in_dump; in->stream.common.set_parameters = in_set_parameters; in->stream.common.get_parameters = in_get_parameters; in->stream.common.add_audio_effect = in_add_audio_effect; in->stream.common.remove_audio_effect = in_remove_audio_effect; in->stream.set_gain = in_set_gain; in->stream.read = in_read; in->stream.get_input_frames_lost = in_get_input_frames_lost; pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); in->dev = (struct audio_device *)dev; pthread_mutex_lock(&in->dev->lock); in->profile = &in->dev->in_profile; struct pcm_config proxy_config; memset(&proxy_config, 0, sizeof(proxy_config)); /* Pull out the card/device pair */ parse_card_device_params(false, &(in->profile->card), &(in->profile->device)); profile_read_device_info(in->profile); pthread_mutex_unlock(&in->dev->lock); /* Rate */ if (config->sample_rate == 0) { proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile); } else if (profile_is_sample_rate_valid(in->profile, config->sample_rate)) { proxy_config.rate = config->sample_rate; } else { ALOGE("%s: The requested sample rate (%d) is not valid", __func__, config->sample_rate); proxy_config.rate = config->sample_rate = profile_get_default_sample_rate(in->profile); ret = -EINVAL; } /* Format */ if (config->format == AUDIO_FORMAT_DEFAULT) { proxy_config.format = profile_get_default_format(in->profile); config->format = audio_format_from_pcm_format(proxy_config.format); } else { enum pcm_format fmt = pcm_format_from_audio_format(config->format); if (profile_is_format_valid(in->profile, fmt)) { proxy_config.format = fmt; } else { ALOGE("%s: The requested format (0x%x) is not valid", __func__, config->format); proxy_config.format = profile_get_default_format(in->profile); config->format = audio_format_from_pcm_format(proxy_config.format); ret = -EINVAL; } } /* Channels */ unsigned proposed_channel_count = 0; if (k_force_channels) { proposed_channel_count = k_force_channels; } else if (config->channel_mask == AUDIO_CHANNEL_NONE) { proposed_channel_count = profile_get_default_channel_count(in->profile); } if (proposed_channel_count != 0) { config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count); if (config->channel_mask == AUDIO_CHANNEL_INVALID) config->channel_mask = audio_channel_mask_for_index_assignment_from_count(proposed_channel_count); in->hal_channel_count = proposed_channel_count; } else { in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask); } /* we can expose any channel mask, and emulate internally based on channel count. */ in->hal_channel_mask = config->channel_mask; proxy_config.channels = profile_get_default_channel_count(in->profile); proxy_prepare(&in->proxy, in->profile, &proxy_config); in->standby = true; in->conversion_buffer = NULL; in->conversion_buffer_size = 0; *stream_in = &in->stream; return ret; }
status_t AudioRecord::set( audio_source_t inputSource, uint32_t sampleRate, audio_format_t format, audio_channel_mask_t channelMask, size_t frameCount, callback_t cbf, void* user, uint32_t notificationFrames, bool threadCanCallJava, audio_session_t sessionId, transfer_type transferType, audio_input_flags_t flags, int uid, pid_t pid, const audio_attributes_t* pAttributes) { ALOGV("set(): inputSource %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, " "notificationFrames %u, sessionId %d, transferType %d, flags %#x, opPackageName %s " "uid %d, pid %d", inputSource, sampleRate, format, channelMask, frameCount, notificationFrames, sessionId, transferType, flags, String8(mOpPackageName).string(), uid, pid); switch (transferType) { case TRANSFER_DEFAULT: if (cbf == NULL || threadCanCallJava) { transferType = TRANSFER_SYNC; } else { transferType = TRANSFER_CALLBACK; } break; case TRANSFER_CALLBACK: if (cbf == NULL) { ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL"); return BAD_VALUE; } break; case TRANSFER_OBTAIN: case TRANSFER_SYNC: break; default: ALOGE("Invalid transfer type %d", transferType); return BAD_VALUE; } mTransfer = transferType; // invariant that mAudioRecord != 0 is true only after set() returns successfully if (mAudioRecord != 0) { ALOGE("Track already in use"); return INVALID_OPERATION; } if (pAttributes == NULL) { memset(&mAttributes, 0, sizeof(audio_attributes_t)); mAttributes.source = inputSource; } else { // stream type shouldn't be looked at, this track has audio attributes memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t)); ALOGV("Building AudioRecord with attributes: source=%d flags=0x%x tags=[%s]", mAttributes.source, mAttributes.flags, mAttributes.tags); } mSampleRate = sampleRate; // these below should probably come from the audioFlinger too... if (format == AUDIO_FORMAT_DEFAULT) { format = AUDIO_FORMAT_PCM_16_BIT; } // validate parameters // AudioFlinger capture only supports linear PCM if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) { ALOGE("Format %#x is not linear pcm", format); return BAD_VALUE; } mFormat = format; if (!audio_is_input_channel(channelMask)) { ALOGE("Invalid channel mask %#x", channelMask); return BAD_VALUE; } mChannelMask = channelMask; uint32_t channelCount = audio_channel_count_from_in_mask(channelMask); mChannelCount = channelCount; if (audio_is_linear_pcm(format)) { mFrameSize = channelCount * audio_bytes_per_sample(format); } else { mFrameSize = sizeof(uint8_t); } // mFrameCount is initialized in openRecord_l mReqFrameCount = frameCount; mNotificationFramesReq = notificationFrames; // mNotificationFramesAct is initialized in openRecord_l if (sessionId == AUDIO_SESSION_ALLOCATE) { mSessionId = (audio_session_t) AudioSystem::newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); } else { mSessionId = sessionId; } ALOGV("set(): mSessionId %d", mSessionId); int callingpid = IPCThreadState::self()->getCallingPid(); int mypid = getpid(); if (uid == -1 || (callingpid != mypid)) { mClientUid = IPCThreadState::self()->getCallingUid(); } else { mClientUid = uid; } if (pid == -1 || (callingpid != mypid)) { mClientPid = callingpid; } else { mClientPid = pid; } mOrigFlags = mFlags = flags; mCbf = cbf; if (cbf != NULL) { mAudioRecordThread = new AudioRecordThread(*this, threadCanCallJava); mAudioRecordThread->run("AudioRecord", ANDROID_PRIORITY_AUDIO); // thread begins in paused state, and will not reference us until start() } // create the IAudioRecord status_t status = openRecord_l(0 /*epoch*/, mOpPackageName); if (status != NO_ERROR) { if (mAudioRecordThread != 0) { mAudioRecordThread->requestExit(); // see comment in AudioRecord.h mAudioRecordThread->requestExitAndWait(); mAudioRecordThread.clear(); } return status; } mStatus = NO_ERROR; mUserData = user; // TODO: add audio hardware input latency here mLatency = (1000 * mFrameCount) / mSampleRate; mMarkerPosition = 0; mMarkerReached = false; mNewPosition = 0; mUpdatePeriod = 0; AudioSystem::acquireAudioSessionId(mSessionId, -1); mSequence = 1; mObservedSequence = mSequence; mInOverrun = false; mFramesRead = 0; mFramesReadServerOffset = 0; return NO_ERROR; }