| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 5 #ifndef MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
| 6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 6 #define MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
| 11 #include "media/base/seekable_buffer.h" | 11 #include "media/base/seekable_buffer.h" |
| 12 | 12 |
| 13 // An audio source that produces a pure sinusoidal tone. | 13 // An audio source that produces a pure sinusoidal tone. |
| 14 class SineWaveAudioSource : public AudioOutputStream::AudioSourceCallback { | 14 class MEDIA_EXPORT SineWaveAudioSource |
| 15 : public AudioOutputStream::AudioSourceCallback { |
| 15 public: | 16 public: |
| 16 enum Format { | 17 enum Format { |
| 17 FORMAT_8BIT_LINEAR_PCM, | 18 FORMAT_8BIT_LINEAR_PCM, |
| 18 FORMAT_16BIT_LINEAR_PCM, | 19 FORMAT_16BIT_LINEAR_PCM, |
| 19 }; | 20 }; |
| 20 // |channels| is the number of audio channels, |freq| is the frequency in | 21 // |channels| is the number of audio channels, |freq| is the frequency in |
| 21 // hertz and it has to be less than half of the sampling frequency | 22 // hertz and it has to be less than half of the sampling frequency |
| 22 // |sample_freq| or else you will get aliasing. | 23 // |sample_freq| or else you will get aliasing. |
| 23 SineWaveAudioSource(Format format, int channels, | 24 SineWaveAudioSource(Format format, int channels, |
| 24 double freq, double sample_freq); | 25 double freq, double sample_freq); |
| 25 virtual ~SineWaveAudioSource() {} | 26 virtual ~SineWaveAudioSource() {} |
| 26 | 27 |
| 27 // Implementation of AudioSourceCallback. | 28 // Implementation of AudioSourceCallback. |
| 28 virtual uint32 OnMoreData( | 29 virtual uint32 OnMoreData( |
| 29 AudioOutputStream* stream, uint8* dest, uint32 max_size, | 30 AudioOutputStream* stream, uint8* dest, uint32 max_size, |
| 30 AudioBuffersState audio_buffers); | 31 AudioBuffersState audio_buffers); |
| 31 virtual void OnError(AudioOutputStream* stream, int code); | 32 virtual void OnError(AudioOutputStream* stream, int code); |
| 32 | 33 |
| 33 protected: | 34 protected: |
| 34 Format format_; | 35 Format format_; |
| 35 int channels_; | 36 int channels_; |
| 36 double freq_; | 37 double freq_; |
| 37 double sample_freq_; | 38 double sample_freq_; |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 // Defines an interface for pushing audio output. In contrast, the interfaces | 41 // Defines an interface for pushing audio output. In contrast, the interfaces |
| 41 // defined by AudioSourceCallback are pull model only. | 42 // defined by AudioSourceCallback are pull model only. |
| 42 class PushAudioOutput { | 43 class MEDIA_EXPORT PushAudioOutput { |
| 43 public: | 44 public: |
| 44 virtual ~PushAudioOutput() {} | 45 virtual ~PushAudioOutput() {} |
| 45 | 46 |
| 46 // Write audio data to the audio device. It will be played eventually. | 47 // Write audio data to the audio device. It will be played eventually. |
| 47 // Returns false on failure. | 48 // Returns false on failure. |
| 48 virtual bool Write(const void* data, uint32 len) = 0; | 49 virtual bool Write(const void* data, uint32 len) = 0; |
| 49 | 50 |
| 50 // Returns the number of bytes that have been buffered but not yet given | 51 // Returns the number of bytes that have been buffered but not yet given |
| 51 // to the audio device. | 52 // to the audio device. |
| 52 virtual uint32 UnProcessedBytes() = 0; | 53 virtual uint32 UnProcessedBytes() = 0; |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 // A fairly basic class to connect a push model provider PushAudioOutput to | 56 // A fairly basic class to connect a push model provider PushAudioOutput to |
| 56 // a pull model provider AudioSourceCallback. Fundamentally it manages a series | 57 // a pull model provider AudioSourceCallback. Fundamentally it manages a series |
| 57 // of audio buffers and is unaware of the actual audio format. | 58 // of audio buffers and is unaware of the actual audio format. |
| 58 // Note that the PushSource is not thread safe and user need to provide locking. | 59 // Note that the PushSource is not thread safe and user need to provide locking. |
| 59 class PushSource | 60 class MEDIA_EXPORT PushSource |
| 60 : public AudioOutputStream::AudioSourceCallback, | 61 : public AudioOutputStream::AudioSourceCallback, |
| 61 public PushAudioOutput { | 62 public PushAudioOutput { |
| 62 public: | 63 public: |
| 63 PushSource(); | 64 PushSource(); |
| 64 virtual ~PushSource(); | 65 virtual ~PushSource(); |
| 65 | 66 |
| 66 // Write one buffer. | 67 // Write one buffer. |
| 67 virtual bool Write(const void* data, uint32 len); | 68 virtual bool Write(const void* data, uint32 len); |
| 68 | 69 |
| 69 // Return the total number of bytes not given to the audio device yet. | 70 // Return the total number of bytes not given to the audio device yet. |
| 70 virtual uint32 UnProcessedBytes(); | 71 virtual uint32 UnProcessedBytes(); |
| 71 | 72 |
| 72 // Implementation of AudioSourceCallback. | 73 // Implementation of AudioSourceCallback. |
| 73 virtual uint32 OnMoreData(AudioOutputStream* stream, uint8* dest, | 74 virtual uint32 OnMoreData(AudioOutputStream* stream, uint8* dest, |
| 74 uint32 max_size, AudioBuffersState buffers_state); | 75 uint32 max_size, AudioBuffersState buffers_state); |
| 75 virtual void OnError(AudioOutputStream* stream, int code); | 76 virtual void OnError(AudioOutputStream* stream, int code); |
| 76 | 77 |
| 77 // Discard all buffered data and reset to initial state. | 78 // Discard all buffered data and reset to initial state. |
| 78 void ClearAll(); | 79 void ClearAll(); |
| 79 | 80 |
| 80 private: | 81 private: |
| 81 // Free acquired resources. | 82 // Free acquired resources. |
| 82 void CleanUp(); | 83 void CleanUp(); |
| 83 | 84 |
| 84 media::SeekableBuffer buffer_; | 85 media::SeekableBuffer buffer_; |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ | 88 #endif // MEDIA_AUDIO_SIMPLE_SOURCES_H_ |
| OLD | NEW |