status_t AudioTrack::set( int streamType, uint32_t sampleRate, int format, int channelMask, int frameCount, uint32_t flags, callback_t cbf, void* user, int notificationFrames, const sp<IMemory>& sharedBuffer, bool threadCanCallJava, int sessionId) { LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); AutoMutex lock(mLock); if (mAudioTrack != 0) { LOGE("Track already in use"); return INVALID_OPERATION; } int afSampleRate; if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { return NO_INIT; } uint32_t afLatency; if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { return NO_INIT; } // handle default values first. if (streamType == AUDIO_STREAM_DEFAULT) { streamType = AUDIO_STREAM_MUSIC; } if (sampleRate == 0) { sampleRate = afSampleRate; } // these below should probably come from the audioFlinger too... if (format == 0) { format = AUDIO_FORMAT_PCM_16_BIT; } if (channelMask == 0) { channelMask = AUDIO_CHANNEL_OUT_STEREO; } // validate parameters if (!audio_is_valid_format(format)) { LOGE("Invalid format"); return BAD_VALUE; } // force direct flag if format is not linear PCM if (!audio_is_linear_pcm(format)) { flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT; } if (!audio_is_output_channel(channelMask)) { LOGE("Invalid channel mask"); return BAD_VALUE; } uint32_t channelCount = popcount(channelMask); audio_io_handle_t output = AudioSystem::getOutput( (audio_stream_type_t)streamType, sampleRate,format, channelMask, (audio_policy_output_flags_t)flags); if (output == 0) { LOGE("Could not get audio output for stream type %d", streamType); return BAD_VALUE; } mVolume[LEFT] = 1.0f; mVolume[RIGHT] = 1.0f; mSendLevel = 0; mFrameCount = frameCount; mNotificationFramesReq = notificationFrames; mSessionId = sessionId; mAuxEffectId = 0; // create the IAudioTrack status_t status = createTrack_l(streamType, sampleRate, (uint32_t)format, (uint32_t)channelMask, frameCount, flags, sharedBuffer, output, true); if (status != NO_ERROR) { return status; } if (cbf != 0) { mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); if (mAudioTrackThread == 0) { LOGE("Could not create callback thread"); return NO_INIT; } } mStatus = NO_ERROR; mStreamType = streamType; mFormat = (uint32_t)format; mChannelMask = (uint32_t)channelMask; mChannelCount = channelCount; mSharedBuffer = sharedBuffer; mMuted = false; mActive = 0; mCbf = cbf; mUserData = user; mLoopCount = 0; mMarkerPosition = 0; mMarkerReached = false; mNewPosition = 0; mUpdatePeriod = 0; mFlushed = false; mFlags = flags; AudioSystem::acquireAudioSessionId(mSessionId); mRestoreStatus = NO_ERROR; return NO_ERROR; }
// checks if the IO profile is compatible with specified parameters. // Sampling rate, format and channel mask must be specified in order to // get a valid a match bool IOProfile::isCompatibleProfile(audio_devices_t device, const String8& address, uint32_t samplingRate, uint32_t *updatedSamplingRate, audio_format_t format, audio_format_t *updatedFormat, audio_channel_mask_t channelMask, audio_channel_mask_t *updatedChannelMask, uint32_t flags) const { const bool isPlaybackThread = getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SOURCE; const bool isRecordThread = getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SINK; ALOG_ASSERT(isPlaybackThread != isRecordThread); if (device != AUDIO_DEVICE_NONE) { // just check types if multiple devices are selected if (popcount(device & ~AUDIO_DEVICE_BIT_IN) > 1) { if ((mSupportedDevices.types() & device) != device) { return false; } } else if (mSupportedDevices.getDevice(device, address) == 0) { return false; } } if (!audio_is_valid_format(format) || (isPlaybackThread && (samplingRate == 0 || !audio_is_output_channel(channelMask))) || (isRecordThread && (!audio_is_input_channel(channelMask)))) { return false; } audio_format_t myUpdatedFormat = format; audio_channel_mask_t myUpdatedChannelMask = channelMask; uint32_t myUpdatedSamplingRate = samplingRate; if (isRecordThread) { if (checkCompatibleAudioProfile( myUpdatedSamplingRate, myUpdatedChannelMask, myUpdatedFormat) != NO_ERROR) { return false; } } else { if (checkExactAudioProfile(samplingRate, channelMask, format) != NO_ERROR) { return false; } } if (isPlaybackThread && (getFlags() & flags) != flags) { return false; } // The only input flag that is allowed to be different is the fast flag. // An existing fast stream is compatible with a normal track request. // An existing normal stream is compatible with a fast track request, // but the fast request will be denied by AudioFlinger and converted to normal track. if (isRecordThread && ((getFlags() ^ flags) & ~AUDIO_INPUT_FLAG_FAST)) { return false; } if (updatedSamplingRate != NULL) { *updatedSamplingRate = myUpdatedSamplingRate; } if (updatedFormat != NULL) { *updatedFormat = myUpdatedFormat; } if (updatedChannelMask != NULL) { *updatedChannelMask = myUpdatedChannelMask; } return true; }