input_sync_writer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_AUDIO_INPUT_SYNC_WRITER_H_
  5. #define SERVICES_AUDIO_INPUT_SYNC_WRITER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/read_only_shared_memory_region.h"
  13. #include "base/sync_socket.h"
  14. #include "base/time/time.h"
  15. #include "build/build_config.h"
  16. #include "media/base/audio_bus.h"
  17. #include "media/base/audio_parameters.h"
  18. #include "services/audio/input_controller.h"
  19. namespace audio {
  20. // A InputController::SyncWriter implementation using SyncSocket. This
  21. // is used by InputController to provide a low latency data source for
  22. // transmitting audio packets between the browser process and the renderer
  23. // process.
  24. class InputSyncWriter final : public InputController::SyncWriter {
  25. public:
  26. // Maximum fifo size (|overflow_buses_| and |overflow_params_|) in number of
  27. // media::AudioBuses.
  28. enum { kMaxOverflowBusesSize = 100 };
  29. InputSyncWriter() = delete;
  30. // Create() automatically initializes the InputSyncWriter correctly,
  31. // and should be strongly preferred over calling the constructor directly!
  32. InputSyncWriter(
  33. base::RepeatingCallback<void(const std::string&)> log_callback,
  34. base::MappedReadOnlyRegion shared_memory,
  35. std::unique_ptr<base::CancelableSyncSocket> socket,
  36. uint32_t shared_memory_segment_count,
  37. const media::AudioParameters& params);
  38. InputSyncWriter(const InputSyncWriter&) = delete;
  39. InputSyncWriter& operator=(const InputSyncWriter&) = delete;
  40. ~InputSyncWriter() final;
  41. static std::unique_ptr<InputSyncWriter> Create(
  42. base::RepeatingCallback<void(const std::string&)> log_callback,
  43. uint32_t shared_memory_segment_count,
  44. const media::AudioParameters& params,
  45. base::CancelableSyncSocket* foreign_socket);
  46. // Transfers shared memory region ownership to a caller. It shouldn't be
  47. // called more than once.
  48. base::ReadOnlySharedMemoryRegion TakeSharedMemoryRegion();
  49. size_t shared_memory_segment_count() const { return audio_buses_.size(); }
  50. // InputController::SyncWriter implementation.
  51. void Write(const media::AudioBus* data,
  52. double volume,
  53. bool key_pressed,
  54. base::TimeTicks capture_time) final;
  55. void Close() final;
  56. private:
  57. friend class InputSyncWriterTest;
  58. // Called by Write(). Checks the time since last called and if larger than a
  59. // threshold logs info about that.
  60. void CheckTimeSinceLastWrite();
  61. // Push |data| and metadata to |audio_buffer_fifo_|. Returns true if
  62. // successful. Logs error and returns false if the fifo already reached the
  63. // maximum size.
  64. bool PushDataToFifo(const media::AudioBus* data,
  65. double volume,
  66. bool key_pressed,
  67. base::TimeTicks capture_time);
  68. // Writes as much data as possible from the fifo (|overflow_buses_|) to the
  69. // shared memory ring buffer. Returns true if all operations were successful,
  70. // otherwise false.
  71. bool WriteDataFromFifoToSharedMemory();
  72. // Write audio parameters to current segment in shared memory.
  73. void WriteParametersToCurrentSegment(double volume,
  74. bool key_pressed,
  75. base::TimeTicks capture_time);
  76. // Signals over the socket that data has been written to the current segment.
  77. // Updates counters and returns true if successful. Logs error and returns
  78. // false if failure.
  79. bool SignalDataWrittenAndUpdateCounters();
  80. const base::RepeatingCallback<void(const std::string&)> log_callback_;
  81. // Socket used to signal that audio data is ready.
  82. const std::unique_ptr<base::CancelableSyncSocket> socket_;
  83. // Shared memory for audio data and associated metadata.
  84. base::ReadOnlySharedMemoryRegion shared_memory_region_;
  85. const base::WritableSharedMemoryMapping shared_memory_mapping_;
  86. // The size in bytes of a single audio segment in the shared memory.
  87. const uint32_t shared_memory_segment_size_;
  88. // Index of next segment to write.
  89. uint32_t current_segment_id_ = 0;
  90. // The time of the creation of this object.
  91. base::TimeTicks creation_time_;
  92. // The time of the last Write call.
  93. base::TimeTicks last_write_time_;
  94. // Size in bytes of each audio bus.
  95. const int audio_bus_memory_size_;
  96. // Increasing ID used for checking audio buffers are in correct sequence at
  97. // read side.
  98. uint32_t next_buffer_id_ = 0;
  99. // Next expected audio buffer index to have been read at the other side. We
  100. // will get the index read at the other side over the socket. Note that this
  101. // index does not correspond to |next_buffer_id_|, it's two separate counters.
  102. uint32_t next_read_buffer_index_ = 0;
  103. // Keeps track of number of filled buffer segments in the ring buffer to
  104. // ensure the we don't overwrite data that hasn't been read yet.
  105. size_t number_of_filled_segments_ = 0;
  106. // Counts the total number of calls to Write().
  107. size_t write_count_ = 0;
  108. // Counts the number of writes to the fifo instead of to the shared memory.
  109. size_t write_to_fifo_count_ = 0;
  110. // Counts the number of errors that causes data to be dropped, due to either
  111. // the fifo or the socket buffer being full.
  112. size_t write_error_count_ = 0;
  113. // Denotes that the most recent socket error has been logged. Used to avoid
  114. // log spam.
  115. bool had_socket_error_ = false;
  116. // Counts the fifo writes and errors we get during renderer process teardown
  117. // so that we can account for that (subtract) when we calculate the overall
  118. // counts.
  119. size_t trailing_write_to_fifo_count_ = 0;
  120. size_t trailing_write_error_count_ = 0;
  121. // Vector of audio buses allocated during construction and deleted in the
  122. // destructor.
  123. std::vector<std::unique_ptr<media::AudioBus>> audio_buses_;
  124. // Fifo for audio that is used in case there isn't room in the shared memory.
  125. // This can for example happen under load when the consumer side is starved.
  126. // It should ideally be rare, but we need to guarantee that the data arrives
  127. // since audio processing such as echo cancelling requires that to perform
  128. // properly.
  129. struct OverflowData {
  130. OverflowData(double volume,
  131. bool key_pressed,
  132. base::TimeTicks capture_time,
  133. std::unique_ptr<media::AudioBus> audio_bus);
  134. OverflowData(const OverflowData&) = delete;
  135. OverflowData& operator=(const OverflowData&) = delete;
  136. OverflowData(OverflowData&&);
  137. OverflowData& operator=(OverflowData&& other);
  138. ~OverflowData();
  139. double volume_;
  140. bool key_pressed_;
  141. base::TimeTicks capture_time_;
  142. std::unique_ptr<media::AudioBus> audio_bus_;
  143. };
  144. std::vector<OverflowData> overflow_data_;
  145. };
  146. } // namespace audio
  147. #endif // SERVICES_AUDIO_INPUT_SYNC_WRITER_H_