summaryrefslogtreecommitdiffstats
path: root/media/audio/linux/cras_output.h
blob: d5de15a55f1177ee2ee9d7cf9b4eecc03ab59e90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Creates an output stream based on the cras (ChromeOS audio server) interface.
//
// CrasOutputStream object is *not* thread-safe and should only be used
// from the audio thread.

#ifndef MEDIA_AUDIO_LINUX_CRAS_OUTPUT_H_
#define MEDIA_AUDIO_LINUX_CRAS_OUTPUT_H_

#include <alsa/asoundlib.h>
#include <cras_client.h>
#include <ostream>

#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "media/audio/audio_io.h"

class AudioManagerLinux;
class AudioParameters;

// Implementation of AudioOuputStream for Chrome OS using the Chrome OS audio
// server.
class MEDIA_EXPORT CrasOutputStream : public AudioOutputStream {
 public:
  // The ctor takes all the usual parameters, plus |manager| which is the
  // audio manager who is creating this object.
  CrasOutputStream(const AudioParameters& params, AudioManagerLinux* manager);

  // The dtor is typically called by the AudioManager only and it is usually
  // triggered by calling AudioOutputStream::Close().
  virtual ~CrasOutputStream();

  // Implementation of AudioOutputStream.
  virtual bool Open() OVERRIDE;
  virtual void Close() OVERRIDE;
  virtual void Start(AudioSourceCallback* callback) OVERRIDE;
  virtual void Stop() OVERRIDE;
  virtual void SetVolume(double volume) OVERRIDE;
  virtual void GetVolume(double* volume) OVERRIDE;

  // Flags indicating the state of the stream.
  enum InternalState {
    kInError = 0,
    kCreated,
    kIsOpened,
    kIsPlaying,
    kIsStopped,
    kIsClosed
  };
  friend std::ostream& operator<<(std::ostream& os, InternalState);
  // Reports the current state for unit testing.
  InternalState state();

 private:
  // Handles requests to put samples in the provided buffer.  This will be
  // called by the audio server when it needs more data.
  static int PutSamples(cras_client* client,
                        cras_stream_id_t stream_id,
                        uint8* samples,
                        size_t frames,
                        const timespec* sample_ts,
                        void* arg);

  // Handles notificaiton that there was an error with the playback stream.
  static int StreamError(cras_client* client,
                         cras_stream_id_t stream_id,
                         int err,
                         void* arg);

  // Actually fills buffer with audio data.  Called from PutSamples().
  uint32 Render(size_t frames, uint8* buffer, const timespec* sample_ts);

  // Deals with an error that occured in the stream.  Called from StreamError().
  void NotifyStreamError(int err);

  // Functions to safeguard state transitions.  All changes to the object state
  // should go through these functions.
  bool CanTransitionTo(InternalState to);
  InternalState TransitionTo(InternalState to);

  // The client used to communicate with the audio server.
  cras_client* client_;

  // ID of the playing stream.
  cras_stream_id_t stream_id_;

  // Packet size in samples.
  uint32 samples_per_packet_;

  // Rate in Hz.
  size_t frame_rate_;

  // Number of channels.
  size_t num_channels_;

  // PCM format for Alsa.
  const snd_pcm_format_t pcm_format_;

  // Current state.
  InternalState state_;

  // Volume level from 0.0 to 1.0.
  float volume_;

  // Audio manager that created us.  Used to report that we've been closed.
  AudioManagerLinux* manager_;

  // Callback to get audio samples.
  AudioSourceCallback* source_callback_;

  DISALLOW_COPY_AND_ASSIGN(CrasOutputStream);
};

#endif  // MEDIA_AUDIO_LINUX_CRAS_OUTPUT_H_