 Chromium Code Reviews
 Chromium Code Reviews Issue 2894873002:
  Improved audio buffer handling for iOS  (Closed)
    
  
    Issue 2894873002:
  Improved audio buffer handling for iOS  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 /* | 1 /* | 
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 
| 3 * | 3 * | 
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license | 
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source | 
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found | 
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may | 
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. | 
| 9 */ | 9 */ | 
| 10 | 10 | 
| 11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ | 
| 12 #define WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ | 12 #define WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ | 
| 13 | 13 | 
| 14 #include <memory> | 14 #include <memory> | 
| 15 | 15 | 
| 16 #include "webrtc/base/array_view.h" | |
| 16 #include "webrtc/base/buffer.h" | 17 #include "webrtc/base/buffer.h" | 
| 17 #include "webrtc/typedefs.h" | 18 #include "webrtc/typedefs.h" | 
| 18 | 19 | 
| 19 namespace webrtc { | 20 namespace webrtc { | 
| 20 | 21 | 
| 21 class AudioDeviceBuffer; | 22 class AudioDeviceBuffer; | 
| 22 | 23 | 
| 23 // FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with audio data | 24 // FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with audio data | 
| 24 // corresponding to 10ms of data. It then allows for this data to be pulled in | 25 // corresponding to 10ms of data. It then allows for this data to be pulled in | 
| 25 // a finer or coarser granularity. I.e. interacting with this class instead of | 26 // a finer or coarser granularity. I.e. interacting with this class instead of | 
| 26 // directly with the AudioDeviceBuffer one can ask for any number of audio data | 27 // directly with the AudioDeviceBuffer one can ask for any number of audio data | 
| 27 // samples. This class also ensures that audio data can be delivered to the ADB | 28 // samples. This class also ensures that audio data can be delivered to the ADB | 
| 28 // in 10ms chunks when the size of the provided audio buffers differs from 10ms. | 29 // in 10ms chunks when the size of the provided audio buffers differs from 10ms. | 
| 29 // As an example: calling DeliverRecordedData() with 5ms buffers will deliver | 30 // As an example: calling DeliverRecordedData() with 5ms buffers will deliver | 
| 30 // accumulated 10ms worth of data to the ADB every second call. | 31 // accumulated 10ms worth of data to the ADB every second call. | 
| 32 // TODO(henrika): add support for stereo when mobile platforms need it. | |
| 31 class FineAudioBuffer { | 33 class FineAudioBuffer { | 
| 32 public: | 34 public: | 
| 33 // |device_buffer| is a buffer that provides 10ms of audio data. | 35 // |device_buffer| is a buffer that provides 10ms of audio data. | 
| 34 // |desired_frame_size_bytes| is the number of bytes of audio data | |
| 35 // GetPlayoutData() should return on success. It is also the required size of | |
| 36 // each recorded buffer used in DeliverRecordedData() calls. | |
| 37 // |sample_rate| is the sample rate of the audio data. This is needed because | 36 // |sample_rate| is the sample rate of the audio data. This is needed because | 
| 38 // |device_buffer| delivers 10ms of data. Given the sample rate the number | 37 // |device_buffer| delivers 10ms of data. Given the sample rate the number | 
| 39 // of samples can be calculated. | 38 // of samples can be calculated. The |capacity| ensures that the buffer size | 
| 39 // can be increased to at least capacity without further reallocation. | |
| 40 FineAudioBuffer(AudioDeviceBuffer* device_buffer, | 40 FineAudioBuffer(AudioDeviceBuffer* device_buffer, | 
| 41 size_t desired_frame_size_bytes, | 41 int sample_rate, | 
| 42 int sample_rate); | 42 size_t capacity); | 
| 43 ~FineAudioBuffer(); | 43 ~FineAudioBuffer(); | 
| 44 | 44 | 
| 45 // Clears buffers and counters dealing with playour and/or recording. | 45 // Clears buffers and counters dealing with playour and/or recording. | 
| 46 void ResetPlayout(); | 46 void ResetPlayout(); | 
| 47 void ResetRecord(); | 47 void ResetRecord(); | 
| 48 | 48 | 
| 49 // |buffer| must be of equal or greater size than what is returned by | 49 // Copies audio samples into |audio_buffer| where number of requested | 
| 50 // RequiredBufferSize(). This is to avoid unnecessary memcpy. | 50 // elements is specified by |audio.buffer.size()|. | 
| 
kwiberg-webrtc
2017/05/29 04:09:02
One . should be _
 
henrika_webrtc
2017/05/29 10:33:51
Done.
 | |
| 51 void GetPlayoutData(int8_t* buffer); | 51 void GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer); | 
| 
kwiberg-webrtc
2017/05/29 04:09:02
Can it ever be the case that you're not able to fi
 
henrika_webrtc
2017/05/29 10:33:51
No. We ask native WebRTC until we get enough and i
 
kwiberg-webrtc
2017/05/29 11:07:01
OK. Document this?
 
henrika_webrtc
2017/05/29 14:30:05
Done.
 | |
| 52 | 52 | 
| 53 // Consumes the audio data in |buffer| and sends it to the WebRTC layer in | 53 // Consumes the audio data in |buffer| and sends it to the WebRTC layer in | 
| 54 // chunks of 10ms. The provided delay estimates in |playout_delay_ms| and | 54 // chunks of 10ms. The provided delay estimates in |playout_delay_ms| and | 
| 55 // |record_delay_ms| are given to the AEC in the audio processing module. | 55 // |record_delay_ms| are given to the AEC in the audio processing module. | 
| 56 // They can be fixed values on most platforms and they are ignored if an | 56 // They can be fixed values on most platforms and they are ignored if an | 
| 57 // external (hardware/built-in) AEC is used. | 57 // external (hardware/built-in) AEC is used. | 
| 58 // The size of |buffer| is given by |size_in_bytes| and must be equal to | 58 // The size of |buffer| is given by |size_in_bytes| and must be equal to | 
| 59 // |desired_frame_size_bytes_|. | 59 // |desired_frame_size_bytes_|. | 
| 60 // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores | 60 // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores | 
| 61 // 5ms of data and sends a total of 10ms to WebRTC and clears the intenal | 61 // 5ms of data and sends a total of 10ms to WebRTC and clears the intenal | 
| 62 // cache. Call #3 restarts the scheme above. | 62 // cache. Call #3 restarts the scheme above. | 
| 63 void DeliverRecordedData(const int8_t* buffer, | 63 void DeliverRecordedData(const int8_t* buffer, | 
| 64 size_t size_in_bytes, | 64 size_t size_in_bytes, | 
| 65 int playout_delay_ms, | 65 int playout_delay_ms, | 
| 66 int record_delay_ms); | 66 int record_delay_ms); | 
| 67 | 67 | 
| 68 private: | 68 private: | 
| 69 // Device buffer that works with 10ms chunks of data both for playout and | 69 // Device buffer that works with 10ms chunks of data both for playout and | 
| 70 // for recording. I.e., the WebRTC side will always be asked for audio to be | 70 // for recording. I.e., the WebRTC side will always be asked for audio to be | 
| 71 // played out in 10ms chunks and recorded audio will be sent to WebRTC in | 71 // played out in 10ms chunks and recorded audio will be sent to WebRTC in | 
| 72 // 10ms chunks as well. This pointer is owned by the constructor of this | 72 // 10ms chunks as well. This pointer is owned by the constructor of this | 
| 73 // class and the owner must ensure that the pointer is valid during the life- | 73 // class and the owner must ensure that the pointer is valid during the life- | 
| 74 // time of this object. | 74 // time of this object. | 
| 75 AudioDeviceBuffer* const device_buffer_; | 75 AudioDeviceBuffer* const device_buffer_; | 
| 76 // Number of bytes delivered by GetPlayoutData() call and provided to | |
| 77 // DeliverRecordedData(). | |
| 78 const size_t desired_frame_size_bytes_; | |
| 79 // Sample rate in Hertz. | 76 // Sample rate in Hertz. | 
| 80 const int sample_rate_; | 77 const int sample_rate_; | 
| 81 // Number of audio samples per 10ms. | 78 // Number of audio samples per 10ms. | 
| 82 const size_t samples_per_10_ms_; | 79 const size_t samples_per_10_ms_; | 
| 83 // Number of audio bytes per 10ms. | 80 // Number of audio bytes per 10ms. | 
| 84 const size_t bytes_per_10_ms_; | 81 const size_t bytes_per_10_ms_; | 
| 82 // Storage for output samples from which a consumer can read audio buffers | |
| 83 // in any size using GetPlayoutData(). | |
| 85 rtc::BufferT<int8_t> playout_buffer_; | 84 rtc::BufferT<int8_t> playout_buffer_; | 
| 86 // Storage for input samples that are about to be delivered to the WebRTC | 85 // Storage for input samples that are about to be delivered to the WebRTC | 
| 87 // ADB or remains from the last successful delivery of a 10ms audio buffer. | 86 // ADB or remains from the last successful delivery of a 10ms audio buffer. | 
| 88 rtc::BufferT<int8_t> record_buffer_; | 87 rtc::BufferT<int8_t> record_buffer_; | 
| 89 }; | 88 }; | 
| 90 | 89 | 
| 91 } // namespace webrtc | 90 } // namespace webrtc | 
| 92 | 91 | 
| 93 #endif // WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ | 92 #endif // WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ | 
| OLD | NEW |