summaryrefslogtreecommitdiff
path: root/guest/hals/audio/legacy/vsoc_audio_input_stream.h
blob: 222194c2c098390aac349f7a2ff4d747ba290389 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <memory>

#include "common/libs/utils/simulated_buffer.h"
#include "common/vsoc/lib/vsoc_audio_message.h"
#include "guest/hals/audio/legacy/audio_hal.h"

namespace cvd {

namespace {
static const int IN_BUFFER_BYTES = 4096;
}

class GceAudio;

// Defines static callback functions for generic_stream_in HAL interface.
class GceAudioInputStream : public audio_stream_in {
 public:
  // These methods are internal to the GCE audio implementation.
  // Factory for new input streams.
  static int Open(
      GceAudio* dev, audio_io_handle_t handle,
      audio_devices_t devices, const audio_config& config,
      GceAudioInputStream** stream_in);

  // Gets a description of this stream
  gce_audio_message GetStreamDescriptor(
      uint32_t stream_number, gce_audio_message::message_t event);

  // audio_stream_in implementation. These definitions follow the ones
  // in hardware/libhardware/include/hardware/audio.h

  // Returns the sampling rate in Hz - eg. 44100.
  uint32_t GetSampleRate() const { return config_.sample_rate; }

  // Sets the sample rate
  // no direct calls from JB and later, but called indirectly from
  // GceAudio::SetStreamParamters when it finds
  // AUDIO_PARAMETER_STREAM_SAMPLING_RATE
  int SetSampleRate(uint32_t rate);

  // Returns the size of input/output buffer in bytes for this stream - eg.
  // 4800.
  // It should be a multiple of the frame size.  See also get_input_buffer_size
  size_t GetBufferSize() const {
    return IN_BUFFER_BYTES;
  }

  // Returns the channel mask -
  //   e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
  audio_channel_mask_t GetChannels() const {
    return config_.channel_mask;
  }

  // Returns the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
  audio_format_t GetFormat() const {
    return config_.format;
  }

  // Sets the audio format
  // no direct calls from JB and later, but called indirectly from
  // GceAudio::SetStreamParamters when it finds
  //   AUDIO_PARAMETER_STREAM_FORMAT
  int SetFormat(audio_format_t format);

  // Puts the audio hardware input/output into standby mode.
  // Driver should exit from standby mode at the next I/O operation.
  // Returns 0 on success and <0 on failure.
  int Standby() { return 0; }

  // Dumps the state of the audio input/output device
  int Dump(int fd) const;

  // Returns the set of device(s) which this stream is connected to
  audio_devices_t GetDevice() const {
    return device_;
  }

  // Sets the device this stream is connected to.
  // no direct calls from JB and later, but called indirectly from
  // GceAudio::SetStreamParamters when it finds
  //   AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
  //   AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
  //                                       input streams only.
  int SetDevice(audio_devices_t device) { device_ = device; return 0; }

  // sets audio stream parameters. The function accepts a list of
  // parameter key value pairs in the form: key1=value1;key2=value2;...
  //
  // Some keys are reserved for standard parameters (See AudioParameter class)
  //
  // If the implementation does not accept a parameter change while
  // the output is active but the parameter is acceptable otherwise, it must
  // return -ENOSYS.
  // The audio flinger will put the stream in standby and then change the
  // parameter value.
  // Uses GceAudio::SetStreamParameters

  // Returns a pointer to a heap allocated string. The caller is responsible
  // for freeing the memory for it using free().
  char* GetParameters(const char* keys) const;

  int AddAudioEffect(effect_handle_t /*effect*/) const {
    return 0;
  }

  int RemoveAudioEffect(effect_handle_t /*effect*/) const {
    return 0;
  }

  // Input stream specific methods

  // Sets the input gain for the audio driver. This method is for
  // for future use as of M.
  int SetGain(float gain) {
    gain_ = gain;
    return 0;
  }

  // Reads audio buffer in from audio driver. Returns number of bytes read, or
  // a negative android::status_t. If at least one frame was read prior to the error,
  //  read should return that byte count and then return an error in the
  // subsequent call.
  ssize_t Read(void* buffer, size_t bytes);

  // Return the amount of input frames lost in the audio driver since the
  // last call of this function.
  // Audio driver is expected to reset the value to 0 and restart counting
  // upon returning the current value by this function call.
  // Such loss typically occurs when the user space process is blocked
  // longer than the capacity of audio driver buffers.
  //
  // Unit: the number of input audio frames
  uint32_t GetInputFramesLost() {
    int64_t cur_lost_frames = buffer_model_->GetLostInputItems();
    uint32_t rval = cur_lost_frames - reported_lost_frames_;
    reported_lost_frames_ = cur_lost_frames;
    return rval;
  }

 private:
  GceAudioInputStream(cvd::GceAudio* dev, audio_devices_t devices,
                      const audio_config& config);
  std::unique_ptr<SimulatedInputBuffer> buffer_model_;
  cvd::GceAudio *dev_;
  audio_config config_;
  float gain_;
  audio_devices_t device_;
  size_t frame_size_;
  int64_t reported_lost_frames_;
};

}