Cubietruck---31.蓝牙耳机与有线耳机的声音输出

来源:互联网 发布:安卓应用源码 编辑:程序博客网 时间:2024/04/28 04:07
getOutput的过程

./frameworks/av/services/audioflinger/AudioPolicyService.cpp
  1. audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream...)
  2. {
  3.     return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channelMask, flags);
  4. }


./hardware/libhardware_legacy/audio/audio_policy_hal.cpp
  1. static audio_io_handle_t ap_get_output(struct audio_policy *pol,
  2.                                        audio_stream_type_t stream,
  3.                                        uint32_t sampling_rate,
  4.                                        audio_format_t format,
  5.                                        audio_channel_mask_t channelMask,
  6.                                        audio_output_flags_t flags)
  7. {
  8.     struct legacy_audio_policy *lap = to_lap(pol);

  9.     dbmsg("tid %d", gettid());
  10.     return lap->apm->getOutput((AudioSystem::stream_type)stream,
  11.                                sampling_rate, (int) format, channelMask,
  12.                                (AudioSystem::output_flags)flags);
  13. }

./hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp
  1. audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
  2.                                     uint32_t samplingRate,
  3.                                     uint32_t format,
  4.                                     uint32_t channelMask,
  5.                                     AudioSystem::output_flags flags)
  6. {
  7.     audio_io_handle_t output = 0;
  8.     uint32_t latency = 0;
  9.     routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
  10.     audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
  11.     // open a direct output if required by specified parameters
  12.     IOProfile *profile = getProfileForDirectOutput(device,
  13.                                                    samplingRate,
  14.                                                    format,
  15.                                                    channelMask,
  16.                                                    (audio_output_flags_t)flags);
  17.     if (profile != NULL) {

  18.         dbmsg("getOutput() opening direct output device %x", device);

  19.         AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(profile);
  20.         outputDesc->mDevice = device;
  21.         outputDesc->mSamplingRate = samplingRate;
  22.         outputDesc->mFormat = (audio_format_t)format;
  23.         outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
  24.         outputDesc->mLatency = 0;
  25.         outputDesc->mFlags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);;
  26.         outputDesc->mRefCount[stream] = 0;
  27.         outputDesc->mStopTime[stream] = 0;
  28.         output = mpClientInterface->openOutput(profile->mModule->mHandle,
  29.                                         &outputDesc->mDevice,
  30.                                         &outputDesc->mSamplingRate,
  31.                                         &outputDesc->mFormat,
  32.                                         &outputDesc->mChannelMask,
  33.                                         &outputDesc->mLatency,
  34.                                         outputDesc->mFlags);

  35.         // only accept an output with the requested parameters
  36.         if (output == 0 ||
  37.             (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
  38.             (format != 0 && format != outputDesc->mFormat) ||
  39.             (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
  40.             dbmsg("getOutput() failed opening direct output: output %d samplingRate %d %d,"
  41.                     "format %d %d, channelMask %04x %04x", output, samplingRate,
  42.                     outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
  43.                     outputDesc->mChannelMask);
  44.             if (output != 0) {
  45.                 mpClientInterface->closeOutput(output);
  46.             }
  47.             delete outputDesc;
  48.             return 0;
  49.         }
  50.         addOutput(output, outputDesc);
  51.         dbmsg("getOutput() returns direct output %d", output);
  52.         return output;
  53.     }

  54.     // ignoring channel mask due to downmix capability in mixer

  55.     // open a non direct output

  56.     // get which output is suitable for the specified stream. The actual routing change will happen
  57.     // when startOutput() will be called
  58.     SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
  59.     output = selectOutput(outputs, flags);
  60.     ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d,"
  61.             "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);

  62.     dbmsg("getOutput() returns output %d", output);

  63.     return output;
  64. }





播放声音
./frameworks/base/media/java/android/media/SoundPool.java
SoundPool::play
  SoundChannel::play


一.数据的写入
frameworks/av/services/audioflinger/AudioFlinger.cpp
  1. void AudioFlinger::PlaybackThread::threadLoop_write()
  2. {
  3. ssize_t framesWritten = mNormalSink->write(mMixBuffer, count);
  4. }



frameworks/av/media/libnbaio/AudioStreamOutSink.cpp
  1. ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
  2. {
  3.     ssize_t ret = mStream->write(mStream, buffer, count << mBitShift);
  4.     return ret;
  5. }

1.2 耳机的输出


1.1 蓝牙耳机的输出
external/bluetooth/bluedroid/audio_a2dp_hw/audio_a2dp_hw.c
  1. static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
  2.                          size_t bytes)
  3. {
  4.     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
  5.     sent = skt_write(out->audio_fd, buffer, bytes);
  6.     return sent;
  7. }


  1. static int skt_write(int fd, const void *p, size_t len)
  2. {
  3.     int sent;
  4.     struct pollfd pfd;
  5.     pfd.fd = fd;
  6.     pfd.events = POLLOUT;
  7.     if (poll(&pfd, 1, 500) == 0)
  8.         return 0;
  9.     sent = send(fd, p, len, MSG_NOSIGNAL);
  10.     
  11.     return sent;
  12. }




蓝牙耳机的声音播放
在frameworks/av/services/audioflinger/AudioPolicyService.cpp中
  1. audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,...)
  2. {
  3.     if (mpAudioPolicy == NULL) {
  4.         return 0;
  5.     }
  6.     Mutex::Autolock _l(mLock);
  7.     return mpAudioPolicy->get_output(mpAudioPolicy, stream, ...);
  8. }
hardware/libhardware_legacy/audio/audio_policy_hal.cpp:ap_get_output(146)
  1. static audio_io_handle_t ap_get_output(struct audio_policy *pol, audio_stream_type_t stream,...)
  2. {
  3.     struct legacy_audio_policy *lap = to_lap(pol);
  4.     return lap->apm->getOutput((AudioSystem::stream_type)stream,);
  5. }
hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp:getDeviceForStrategy(2214), STRATEGY_MEDIA
  1. audio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream)
  2. {
  3.     routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
  4.     audio_devices_t device = getDeviceForStrategy(strategy, false );
  5.     IOProfile *profile = getProfileForDirectOutput(device,...);
  6.     if (profile != NULL) {
  7.     }
  8.     SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
  9.     output = selectOutput(outputs, flags);
  10.     return output;
  11. }


I/cong    ( 1346): hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp:getA2dpOutput(1919), 
I/cong    ( 1346): hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp:getOutput(522), getOutput() stream 1, samplingRate 0, format 0, channelMask 3, flags 0
I/cong    ( 1346): hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp:getOutput(614), getOutput() returns output 6
I/cong    ( 1346): frameworks/av/services/audioflinger/AudioPolicyService.cpp:getOutput(228), getOutput() tid 2538
0 0