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