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.
5 // Creates a unified stream based on the cras (ChromeOS audio server) interface.
7 // CrasUnifiedStream object is *not* thread-safe and should only be used
8 // from the audio thread.
10 #ifndef MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_
11 #define MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_
13 #include <cras_client.h>
15 #include "base/compiler_specific.h"
16 #include "media/audio/audio_io.h"
17 #include "media/audio/audio_parameters.h"
21 class AudioManagerCras
;
23 // Implementation of AudioOuputStream for Chrome OS using the Chrome OS audio
25 // TODO(dgreid): This class is used for only output, either remove all the
26 // relevant input code and change the class to CrasOutputStream or merge
27 // cras_input.cc into this unified implementation.
28 class MEDIA_EXPORT CrasUnifiedStream
: public AudioOutputStream
{
30 // The ctor takes all the usual parameters, plus |manager| which is the
31 // audio manager who is creating this object.
32 CrasUnifiedStream(const AudioParameters
& params
, AudioManagerCras
* manager
);
34 // The dtor is typically called by the AudioManager only and it is usually
35 // triggered by calling AudioUnifiedStream::Close().
36 virtual ~CrasUnifiedStream();
38 // Implementation of AudioOutputStream.
39 virtual bool Open() OVERRIDE
;
40 virtual void Close() OVERRIDE
;
41 virtual void Start(AudioSourceCallback
* callback
) OVERRIDE
;
42 virtual void Stop() OVERRIDE
;
43 virtual void SetVolume(double volume
) OVERRIDE
;
44 virtual void GetVolume(double* volume
) OVERRIDE
;
47 // Convert Latency in time to bytes.
48 uint32
GetBytesLatency(const struct timespec
& latency
);
50 // Handles captured audio and fills the ouput with audio to be played.
51 static int UnifiedCallback(cras_client
* client
,
52 cras_stream_id_t stream_id
,
54 uint8
* output_samples
,
56 const timespec
* input_ts
,
57 const timespec
* output_ts
,
60 // Handles notification that there was an error with the playback stream.
61 static int StreamError(cras_client
* client
,
62 cras_stream_id_t stream_id
,
66 // Chooses the correct audio callback based on stream direction.
67 uint32
DispatchCallback(size_t frames
,
69 uint8
* output_samples
,
70 const timespec
* input_ts
,
71 const timespec
* output_ts
);
73 // Receives input samples and write output samples for a unified I/O stream.
74 uint32
ReadWriteAudio(size_t frames
,
76 uint8
* output_samples
,
77 const timespec
* input_ts
,
78 const timespec
* output_ts
);
80 // Writes audio for a playback stream.
81 uint32
WriteAudio(size_t frames
, uint8
* buffer
, const timespec
* sample_ts
);
83 // Deals with an error that occured in the stream. Called from StreamError().
84 void NotifyStreamError(int err
);
86 // The client used to communicate with the audio server.
89 // ID of the playing stream.
90 cras_stream_id_t stream_id_
;
92 // PCM parameters for the stream.
93 AudioParameters params_
;
95 // Size of frame in bytes.
96 uint32 bytes_per_frame_
;
98 // True if stream is playing.
101 // Volume level from 0.0 to 1.0.
104 // Audio manager that created us. Used to report that we've been closed.
105 AudioManagerCras
* manager_
;
107 // Callback to get audio samples.
108 AudioSourceCallback
* source_callback_
;
110 // Container for exchanging data with AudioSourceCallback::OnMoreData().
111 scoped_ptr
<AudioBus
> input_bus_
;
112 scoped_ptr
<AudioBus
> output_bus_
;
114 // Direciton of the stream.
115 CRAS_STREAM_DIRECTION stream_direction_
;
117 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStream
);
122 #endif // MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_