Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // Creates an output stream based on the cras (ChromeOS audio server) interface. | 5 // Creates a unified stream based on the cras (ChromeOS audio server) interface. |
| 6 // | 6 // |
| 7 // CrasOutputStream object is *not* thread-safe and should only be used | 7 // CrasUnifiedStream object is *not* thread-safe and should only be used |
|
scherkus (not reviewing)
2013/01/23 20:51:21
This diff makes it pretty clear that unified imple
| |
| 8 // from the audio thread. | 8 // from the audio thread. |
| 9 | 9 |
| 10 #ifndef MEDIA_AUDIO_LINUX_CRAS_OUTPUT_H_ | 10 #ifndef MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_ |
| 11 #define MEDIA_AUDIO_LINUX_CRAS_OUTPUT_H_ | 11 #define MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_ |
| 12 | 12 |
| 13 #include <alsa/asoundlib.h> | 13 #include <alsa/asoundlib.h> |
| 14 #include <cras_client.h> | 14 #include <cras_client.h> |
| 15 #include <ostream> | 15 #include <ostream> |
| 16 | 16 |
| 17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 18 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 19 #include "media/audio/audio_io.h" | 19 #include "media/audio/audio_io.h" |
| 20 | 20 |
| 21 namespace media { | 21 namespace media { |
| 22 | 22 |
| 23 class AudioManagerLinux; | 23 class AudioManagerLinux; |
| 24 class AudioParameters; | 24 class AudioParameters; |
| 25 | 25 |
| 26 // Implementation of AudioOuputStream for Chrome OS using the Chrome OS audio | 26 // Implementation of AudioOuputStream for Chrome OS using the Chrome OS audio |
| 27 // server. | 27 // server. |
| 28 class MEDIA_EXPORT CrasOutputStream : public AudioOutputStream { | 28 class MEDIA_EXPORT CrasUnifiedStream : public AudioOutputStream { |
| 29 public: | 29 public: |
| 30 // The ctor takes all the usual parameters, plus |manager| which is the | 30 // The ctor takes all the usual parameters, plus |manager| which is the |
| 31 // audio manager who is creating this object. | 31 // audio manager who is creating this object. |
| 32 CrasOutputStream(const AudioParameters& params, AudioManagerLinux* manager); | 32 CrasUnifiedStream(const AudioParameters& params, AudioManagerLinux* manager); |
| 33 | 33 |
| 34 // The dtor is typically called by the AudioManager only and it is usually | 34 // The dtor is typically called by the AudioManager only and it is usually |
| 35 // triggered by calling AudioOutputStream::Close(). | 35 // triggered by calling AudioUnifiedStream::Close(). |
| 36 virtual ~CrasOutputStream(); | 36 virtual ~CrasUnifiedStream(); |
| 37 | 37 |
| 38 // Implementation of AudioOutputStream. | 38 // Implementation of AudioOutputStream. |
| 39 virtual bool Open() OVERRIDE; | 39 virtual bool Open() OVERRIDE; |
| 40 virtual void Close() OVERRIDE; | 40 virtual void Close() OVERRIDE; |
| 41 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | 41 virtual void Start(AudioSourceCallback* callback) OVERRIDE; |
| 42 virtual void Stop() OVERRIDE; | 42 virtual void Stop() OVERRIDE; |
| 43 virtual void SetVolume(double volume) OVERRIDE; | 43 virtual void SetVolume(double volume) OVERRIDE; |
| 44 virtual void GetVolume(double* volume) OVERRIDE; | 44 virtual void GetVolume(double* volume) OVERRIDE; |
| 45 | 45 |
| 46 // Flags indicating the state of the stream. | |
| 47 enum InternalState { | |
| 48 kInError = 0, | |
| 49 kCreated, | |
| 50 kIsOpened, | |
| 51 kIsPlaying, | |
| 52 kIsStopped, | |
| 53 kIsClosed | |
| 54 }; | |
| 55 friend std::ostream& operator<<(std::ostream& os, InternalState); | |
| 56 // Reports the current state for unit testing. | |
| 57 InternalState state(); | |
| 58 | |
| 59 private: | 46 private: |
| 60 // Handles requests to put samples in the provided buffer. This will be | 47 // Handles captured audio and fills the ouput with audio to be played. |
| 61 // called by the audio server when it needs more data. | 48 static int UnifiedCallback(cras_client* client, |
| 62 static int PutSamples(cras_client* client, | 49 cras_stream_id_t stream_id, |
| 63 cras_stream_id_t stream_id, | 50 uint8* input_samples, |
| 64 uint8* samples, | 51 uint8* output_samples, |
| 65 size_t frames, | 52 unsigned int frames, |
| 66 const timespec* sample_ts, | 53 const timespec* input_ts, |
| 67 void* arg); | 54 const timespec* output_ts, |
| 55 void* arg); | |
| 68 | 56 |
| 69 // Handles notificaiton that there was an error with the playback stream. | 57 // Handles notificaiton that there was an error with the playback stream. |
| 70 static int StreamError(cras_client* client, | 58 static int StreamError(cras_client* client, |
| 71 cras_stream_id_t stream_id, | 59 cras_stream_id_t stream_id, |
| 72 int err, | 60 int err, |
| 73 void* arg); | 61 void* arg); |
| 74 | 62 |
| 75 // Actually fills buffer with audio data. Called from PutSamples(). | 63 // Fills buffer with audio data. Called from UnifiedSamples(). |
| 76 uint32 Render(size_t frames, uint8* buffer, const timespec* sample_ts); | 64 uint32 Render(size_t frames, |
| 65 uint8* input_samples, | |
| 66 uint8* output_samples, | |
| 67 const timespec* input_ts, | |
| 68 const timespec* output_ts); | |
| 77 | 69 |
| 78 // Deals with an error that occured in the stream. Called from StreamError(). | 70 // Deals with an error that occured in the stream. Called from StreamError(). |
| 79 void NotifyStreamError(int err); | 71 void NotifyStreamError(int err); |
| 80 | 72 |
| 81 // Functions to safeguard state transitions. All changes to the object state | |
| 82 // should go through these functions. | |
| 83 bool CanTransitionTo(InternalState to); | |
| 84 InternalState TransitionTo(InternalState to); | |
| 85 | |
| 86 // The client used to communicate with the audio server. | 73 // The client used to communicate with the audio server. |
| 87 cras_client* client_; | 74 cras_client* client_; |
| 88 | 75 |
| 89 // ID of the playing stream. | 76 // ID of the playing stream. |
| 90 cras_stream_id_t stream_id_; | 77 cras_stream_id_t stream_id_; |
| 91 | 78 |
| 92 // Packet size in samples. | 79 // Packet size in samples. |
| 93 uint32 samples_per_packet_; | 80 uint32 samples_per_packet_; |
| 94 | 81 |
| 95 // Size of frame in bytes. | 82 // Size of frame in bytes. |
| 96 uint32 bytes_per_frame_; | 83 uint32 bytes_per_frame_; |
| 97 | 84 |
| 98 // Rate in Hz. | 85 // Rate in Hz. |
| 99 size_t frame_rate_; | 86 size_t frame_rate_; |
| 100 | 87 |
| 101 // Number of channels. | 88 // Number of channels. |
| 102 size_t num_channels_; | 89 size_t num_channels_; |
| 103 | 90 |
| 104 // PCM format for Alsa. | 91 // PCM format for Alsa. |
| 105 const snd_pcm_format_t pcm_format_; | 92 const snd_pcm_format_t pcm_format_; |
| 106 | 93 |
| 107 // Current state. | 94 // True if stream is playing. |
| 108 InternalState state_; | 95 bool is_playing_; |
| 109 | 96 |
| 110 // Volume level from 0.0 to 1.0. | 97 // Volume level from 0.0 to 1.0. |
| 111 float volume_; | 98 float volume_; |
| 112 | 99 |
| 113 // Audio manager that created us. Used to report that we've been closed. | 100 // Audio manager that created us. Used to report that we've been closed. |
| 114 AudioManagerLinux* manager_; | 101 AudioManagerLinux* manager_; |
| 115 | 102 |
| 116 // Callback to get audio samples. | 103 // Callback to get audio samples. |
| 117 AudioSourceCallback* source_callback_; | 104 AudioSourceCallback* source_callback_; |
| 118 | 105 |
| 119 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 106 // Container for exchanging data with AudioSourceCallback::OnMoreIOData(). |
| 120 scoped_ptr<AudioBus> audio_bus_; | 107 scoped_ptr<AudioBus> input_bus_; |
| 108 scoped_ptr<AudioBus> output_bus_; | |
| 121 | 109 |
| 122 DISALLOW_COPY_AND_ASSIGN(CrasOutputStream); | 110 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStream); |
| 123 }; | 111 }; |
| 124 | 112 |
| 125 } // namespace media | 113 } // namespace media |
| 126 | 114 |
| 127 #endif // MEDIA_AUDIO_LINUX_CRAS_OUTPUT_H_ | 115 #endif // MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_ |
| OLD | NEW |