Custom Anbox installation files & patches, including patched Android OS image file.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

4 years ago
  1. From c5b93022b4fdd15a56f238ad3b9928f2302f4d40 Mon Sep 17 00:00:00 2001
  2. From: awakening <lucidsunlight@yandex.ru>
  3. Date: Sun, 20 Jan 2019 22:20:27 +0700
  4. Subject: [PATCH] Implement audio timing
  5. ---
  6. android/audio/audio_hw.cpp | 8 ++++++++
  7. src/anbox/platform/sdl/audio_sink.cpp | 17 ++++++++++++++++-
  8. 2 files changed, 24 insertions(+), 1 deletion(-)
  9. diff --git a/android/audio/audio_hw.cpp b/android/audio/audio_hw.cpp
  10. index f44e73826..39b845669 100644
  11. --- a/android/audio/audio_hw.cpp
  12. +++ b/android/audio/audio_hw.cpp
  13. @@ -180,6 +180,14 @@ static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
  14. pthread_mutex_lock(&adev->lock);
  15. if (out->fd >= 0)
  16. bytes = write(out->fd, buffer, bytes);
  17. +
  18. + // wait until session writes the data we sent,
  19. + // this will block if sink queue is full,
  20. + // acting as synchronization to time audio
  21. + int64_t arrived_us;
  22. + read(out->fd, &arrived_us, sizeof(arrived_us));
  23. + (void) arrived_us;
  24. +
  25. pthread_mutex_unlock(&adev->lock);
  26. return bytes;
  27. }
  28. diff --git a/src/anbox/platform/sdl/audio_sink.cpp b/src/anbox/platform/sdl/audio_sink.cpp
  29. index 118093de7..480fe1844 100644
  30. --- a/src/anbox/platform/sdl/audio_sink.cpp
  31. +++ b/src/anbox/platform/sdl/audio_sink.cpp
  32. @@ -19,11 +19,12 @@
  33. #include "anbox/logger.h"
  34. #include <stdexcept>
  35. +#include <time.h>
  36. #include <boost/throw_exception.hpp>
  37. namespace {
  38. -const constexpr size_t max_queue_size{16};
  39. +const constexpr size_t max_queue_size{1};
  40. }
  41. namespace anbox {
  42. @@ -114,6 +115,20 @@ void AudioSink::write_data(const std::vector<std::uint8_t> &data) {
  43. }
  44. graphics::Buffer buffer{data.data(), data.data() + data.size()};
  45. queue_.push_locked(std::move(buffer), l);
  46. +
  47. + // Android side is waiting for "confirmation" that data arrived,
  48. + // if sink queue is full, Android audio thread will be blocked
  49. + // untill there's space available in the queue
  50. + //
  51. + // this acts as sychronization to time audio
  52. + //
  53. + // at the time of implementation,
  54. + // the data we send to Android is not actually used
  55. + timespec t;
  56. + clock_gettime(CLOCK_MONOTONIC, &t);
  57. + int64_t now_us = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
  58. +
  59. + messenger_->send(reinterpret_cast<char*>(&now_us), sizeof(now_us));
  60. }
  61. } // namespace sdl
  62. } // namespace platform