AudioStreamOutSink.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2012 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. #define LOG_TAG "AudioStreamOutSink"
  17. //#define LOG_NDEBUG 0
  18. #include <utils/Log.h>
  19. #include <audio_utils/clock.h>
  20. #include <media/audiohal/StreamHalInterface.h>
  21. #include <media/nbaio/AudioStreamOutSink.h>
  22. namespace android {
  23. AudioStreamOutSink::AudioStreamOutSink(sp<StreamOutHalInterface> stream) :
  24. NBAIO_Sink(),
  25. mStream(stream),
  26. mStreamBufferSizeBytes(0)
  27. {
  28. ALOG_ASSERT(stream != 0);
  29. }
  30. AudioStreamOutSink::~AudioStreamOutSink()
  31. {
  32. mStream.clear();
  33. }
  34. ssize_t AudioStreamOutSink::negotiate(const NBAIO_Format offers[], size_t numOffers,
  35. NBAIO_Format counterOffers[], size_t& numCounterOffers)
  36. {
  37. if (!Format_isValid(mFormat)) {
  38. status_t result;
  39. result = mStream->getBufferSize(&mStreamBufferSizeBytes);
  40. if (result != OK) return result;
  41. audio_format_t streamFormat;
  42. uint32_t sampleRate;
  43. audio_channel_mask_t channelMask;
  44. result = mStream->getAudioProperties(&sampleRate, &channelMask, &streamFormat);
  45. if (result != OK) return result;
  46. mFormat = Format_from_SR_C(sampleRate,
  47. audio_channel_count_from_out_mask(channelMask), streamFormat);
  48. mFrameSize = Format_frameSize(mFormat);
  49. }
  50. return NBAIO_Sink::negotiate(offers, numOffers, counterOffers, numCounterOffers);
  51. }
  52. ssize_t AudioStreamOutSink::write(const void *buffer, size_t count)
  53. {
  54. if (!mNegotiated) {
  55. return NEGOTIATE;
  56. }
  57. ALOG_ASSERT(Format_isValid(mFormat));
  58. size_t written;
  59. status_t ret = mStream->write(buffer, count * mFrameSize, &written);
  60. if (ret == OK && written > 0) {
  61. written /= mFrameSize;
  62. mFramesWritten += written;
  63. return written;
  64. } else {
  65. // FIXME verify HAL implementations are returning the correct error codes e.g. WOULD_BLOCK
  66. ALOGE_IF(ret != OK, "Error while writing data to HAL: %d", ret);
  67. return ret;
  68. }
  69. }
  70. status_t AudioStreamOutSink::getTimestamp(ExtendedTimestamp &timestamp)
  71. {
  72. uint64_t position64;
  73. struct timespec time;
  74. if (mStream->getPresentationPosition(&position64, &time) != OK) {
  75. return INVALID_OPERATION;
  76. }
  77. timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position64;
  78. timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = audio_utils_ns_from_timespec(&time);
  79. return OK;
  80. }
  81. } // namespace android