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 class MEDIA_EXPORT CrasUnifiedStream
: public AudioOutputStream
{
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
);
31 // The dtor is typically called by the AudioManager only and it is usually
32 // triggered by calling AudioUnifiedStream::Close().
33 virtual ~CrasUnifiedStream();
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
;
44 // Convert Latency in time to bytes.
45 uint32
GetBytesLatency(const struct timespec
& latency
);
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
,
51 uint8
* output_samples
,
53 const timespec
* input_ts
,
54 const timespec
* output_ts
,
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
,
63 // Chooses the correct audio callback based on stream direction.
64 uint32
DispatchCallback(size_t frames
,
66 uint8
* output_samples
,
67 const timespec
* input_ts
,
68 const timespec
* output_ts
);
70 // Receives input samples and write output samples for a unified I/O stream.
71 uint32
ReadWriteAudio(size_t frames
,
73 uint8
* output_samples
,
74 const timespec
* input_ts
,
75 const timespec
* output_ts
);
77 // Writes audio for a playback stream.
78 uint32
WriteAudio(size_t frames
, uint8
* buffer
, const timespec
* sample_ts
);
80 // Deals with an error that occured in the stream. Called from StreamError().
81 void NotifyStreamError(int err
);
83 // The client used to communicate with the audio server.
86 // ID of the playing stream.
87 cras_stream_id_t stream_id_
;
89 // PCM parameters for the stream.
90 AudioParameters params_
;
92 // Size of frame in bytes.
93 uint32 bytes_per_frame_
;
95 // True if stream is playing.
98 // Volume level from 0.0 to 1.0.
101 // Audio manager that created us. Used to report that we've been closed.
102 AudioManagerCras
* manager_
;
104 // Callback to get audio samples.
105 AudioSourceCallback
* source_callback_
;
107 // Container for exchanging data with AudioSourceCallback::OnMoreIOData().
108 scoped_ptr
<AudioBus
> input_bus_
;
109 scoped_ptr
<AudioBus
> output_bus_
;
111 // Direciton of the stream.
112 CRAS_STREAM_DIRECTION stream_direction_
;
114 DISALLOW_COPY_AND_ASSIGN(CrasUnifiedStream
);
119 #endif // MEDIA_AUDIO_LINUX_CRAS_UNIFIED_H_