Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 // | |
| 5 // Creates a unified stream based on the cras (ChromeOS audio server) interface. | |
| 6 // | |
| 7 // CrasUnifiedStream object is *not* thread-safe and should only be used | |
| 8 // from the audio thread. | |
| 9 | |
| 10 #ifndef MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_ | |
| 11 #define MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_ | |
| 12 | |
| 13 #include <alsa/asoundlib.h> | |
| 14 #include <cras_client.h> | |
| 15 | |
| 16 #include "base/compiler_specific.h" | |
| 17 #include "base/gtest_prod_util.h" | |
| 18 #include "media/audio/audio_io.h" | |
| 19 | |
| 20 namespace media { | |
| 21 | |
| 22 class AudioManagerCras; | |
| 23 class AudioParameters; | |
| 24 | |
| 25 // Implementation of AudioOuputStream for Chrome OS using the Chrome OS audio | |
| 26 // server. | |
| 27 class MEDIA_EXPORT CrasUnifiedStream : public AudioOutputStream { | |
| 28 public: | |
| 29 // The ctor takes all the usual parameters, plus |manager| which is the | |
| 30 // audio manager who is creating this object. | |
| 31 CrasUnifiedStream(const AudioParameters& params, AudioManagerCras* manager); | |
| 32 | |
| 33 // The dtor is typically called by the AudioManager only and it is usually | |
| 34 // triggered by calling AudioUnifiedStream::Close(). | |
| 35 virtual ~CrasUnifiedStream(); | |
| 36 | |
| 37 // Implementation of AudioOutputStream. | |
| 38 virtual bool Open() OVERRIDE; | |
| 39 virtual void Close() OVERRIDE; | |
| 40 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | |
| 41 virtual void Stop() OVERRIDE; | |
| 42 virtual void SetVolume(double volume) OVERRIDE; | |
| 43 virtual void GetVolume(double* volume) OVERRIDE; | |
| 44 | |
| 45 private: | |
| 46 // Convert Latency in time to bytes. | |
| 47 unsigned int GetBytesLatency(const struct timespec& latency); | |
| 48 | |
| 49 // Handles captured audio and fills the ouput with audio to be played. | |
| 50 static int UnifiedCallback(cras_client* client, | |
| 51 cras_stream_id_t stream_id, | |
| 52 uint8* input_samples, | |
| 53 uint8* output_samples, | |
| 54 unsigned int frames, | |
| 55 const timespec* input_ts, | |
| 56 const timespec* output_ts, | |
| 57 void* arg); | |
| 58 | |
| 59 // Handles notificaiton that there was an error with the playback stream. | |
| 60 static int StreamError(cras_client* client, | |
| 61 cras_stream_id_t stream_id, | |
| 62 int err, | |
| 63 void* arg); | |
| 64 | |
| 65 // Chooses the correct audio callback based on stream direction. | |
| 66 uint32 DispatchCalback(size_t frames, | |
|
DaleCurtis
2013/03/26 19:23:01
Callback
| |
| 67 uint8* input_samples, | |
| 68 uint8* output_samples, | |
| 69 const timespec* input_ts, | |
| 70 const timespec* output_ts); | |
| 71 | |
| 72 // Receives input samples and write output samples for a unified I/O stream. | |
| 73 uint32 ReadWriteAudio(size_t frames, | |
| 74 uint8* input_samples, | |
| 75 uint8* output_samples, | |
| 76 const timespec* input_ts, | |
| 77 const timespec* output_ts); | |
| 78 | |
| 79 // Writes audio for a playback stream. | |
| 80 uint32 WriteAudio(size_t frames, uint8* buffer, const timespec* sample_ts); | |
| 81 | |
| 82 // Deals with an error that occured in the stream. Called from StreamError(). | |
| 83 void NotifyStreamError(int err); | |
| 84 | |
| 85 // The client used to communicate with the audio server. | |
| 86 cras_client* client_; | |
| 87 | |
| 88 // ID of the playing stream. | |
| 89 cras_stream_id_t stream_id_; | |
| 90 | |
| 91 // Packet size in samples. | |
| 92 uint32 samples_per_packet_; | |
| 93 | |
| 94 // Size of frame in bytes. | |
| 95 uint32 bytes_per_frame_; | |
| 96 | |
| 97 // Rate in Hz. | |
| 98 size_t frame_rate_; | |
| 99 | |
| 100 // Number of channels. | |
| 101 size_t output_channels_; | |
| 102 | |
| 103 // PCM format for Alsa. | |
| 104 const snd_pcm_format_t pcm_format_; | |
| 105 | |
| 106 // True if stream is playing. | |
| 107 bool is_playing_; | |
| 108 | |
| 109 // Volume level from 0.0 to 1.0. | |
| 110 float volume_; | |
| 111 | |
| 112 // Audio manager that created us. Used to report that we've been closed. | |
| 113 AudioManagerCras* manager_; | |
| 114 | |
| 115 // Callback to get audio samples. | |
| 116 AudioSourceCallback* source_callback_; | |
| 117 | |
| 118 // Container for exchanging data with AudioSourceCallback::OnMoreIOData(). | |
| 119 scoped_ptr<AudioBus> input_bus_; | |
| 120 scoped_ptr<AudioBus> output_bus_; | |
| 121 | |
| 122 // Direciton of the stream. | |
| 123 CRAS_STREAM_DIRECTION stream_direction_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStream); | |
| 126 }; | |
| 127 | |
| 128 } // namespace media | |
| 129 | |
| 130 #endif // MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_ | |
| OLD | NEW |