1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <memory>
19
20#include "common/vsoc/lib/vsoc_audio_message.h"
21#include "guest/hals/audio/audio_hal.h"
22#include "guest/hals/audio/simulated_buffer.h"
23
24namespace cvd {
25
26namespace {
27static const int IN_BUFFER_BYTES = 4096;
28}
29
30class GceAudio;
31
32// Defines static callback functions for generic_stream_in HAL interface.
33class GceAudioInputStream : public audio_stream_in {
34 public:
35  // These methods are internal to the GCE audio implementation.
36  // Factory for new input streams.
37  static int Open(
38      GceAudio* dev, audio_io_handle_t handle,
39      audio_devices_t devices, const audio_config& config,
40      GceAudioInputStream** stream_in);
41
42  // Gets a description of this stream
43  gce_audio_message GetStreamDescriptor(
44      uint32_t stream_number, gce_audio_message::message_t event);
45
46  // audio_stream_in implementation. These definitions follow the ones
47  // in hardware/libhardware/include/hardware/audio.h
48
49  // Returns the sampling rate in Hz - eg. 44100.
50  uint32_t GetSampleRate() const { return config_.sample_rate; }
51
52  // Sets the sample rate
53  // no direct calls from JB and later, but called indirectly from
54  // GceAudio::SetStreamParamters when it finds
55  // AUDIO_PARAMETER_STREAM_SAMPLING_RATE
56  int SetSampleRate(uint32_t rate);
57
58  // Returns the size of input/output buffer in bytes for this stream - eg.
59  // 4800.
60  // It should be a multiple of the frame size.  See also get_input_buffer_size
61  size_t GetBufferSize() const {
62    return IN_BUFFER_BYTES;
63  }
64
65  // Returns the channel mask -
66  //   e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
67  audio_channel_mask_t GetChannels() const {
68    return config_.channel_mask;
69  }
70
71  // Returns the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
72  audio_format_t GetFormat() const {
73    return config_.format;
74  }
75
76  // Sets the audio format
77  // no direct calls from JB and later, but called indirectly from
78  // GceAudio::SetStreamParamters when it finds
79  //   AUDIO_PARAMETER_STREAM_FORMAT
80  int SetFormat(audio_format_t format);
81
82  // Puts the audio hardware input/output into standby mode.
83  // Driver should exit from standby mode at the next I/O operation.
84  // Returns 0 on success and <0 on failure.
85  int Standby() { return 0; }
86
87  // Dumps the state of the audio input/output device
88  int Dump(int fd) const;
89
90  // Returns the set of device(s) which this stream is connected to
91  audio_devices_t GetDevice() const {
92    return device_;
93  }
94
95  // Sets the device this stream is connected to.
96  // no direct calls from JB and later, but called indirectly from
97  // GceAudio::SetStreamParamters when it finds
98  //   AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
99  //   AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
100  //                                       input streams only.
101  int SetDevice(audio_devices_t device) { device_ = device; return 0; }
102
103  // sets audio stream parameters. The function accepts a list of
104  // parameter key value pairs in the form: key1=value1;key2=value2;...
105  //
106  // Some keys are reserved for standard parameters (See AudioParameter class)
107  //
108  // If the implementation does not accept a parameter change while
109  // the output is active but the parameter is acceptable otherwise, it must
110  // return -ENOSYS.
111  // The audio flinger will put the stream in standby and then change the
112  // parameter value.
113  // Uses GceAudio::SetStreamParameters
114
115  // Returns a pointer to a heap allocated string. The caller is responsible
116  // for freeing the memory for it using free().
117  char* GetParameters(const char* keys) const;
118
119  int AddAudioEffect(effect_handle_t /*effect*/) const {
120    return 0;
121  }
122
123  int RemoveAudioEffect(effect_handle_t /*effect*/) const {
124    return 0;
125  }
126
127  // Input stream specific methods
128
129  // Sets the input gain for the audio driver. This method is for
130  // for future use as of M.
131  int SetGain(float gain) {
132    gain_ = gain;
133    return 0;
134  }
135
136  // Reads audio buffer in from audio driver. Returns number of bytes read, or
137  // a negative android::status_t. If at least one frame was read prior to the error,
138  //  read should return that byte count and then return an error in the
139  // subsequent call.
140  ssize_t Read(void* buffer, size_t bytes);
141
142  // Return the amount of input frames lost in the audio driver since the
143  // last call of this function.
144  // Audio driver is expected to reset the value to 0 and restart counting
145  // upon returning the current value by this function call.
146  // Such loss typically occurs when the user space process is blocked
147  // longer than the capacity of audio driver buffers.
148  //
149  // Unit: the number of input audio frames
150  uint32_t GetInputFramesLost() {
151    int64_t cur_lost_frames = buffer_model_->GetLostInputItems();
152    uint32_t rval = cur_lost_frames - reported_lost_frames_;
153    reported_lost_frames_ = cur_lost_frames;
154    return rval;
155  }
156
157 private:
158  GceAudioInputStream(cvd::GceAudio* dev, audio_devices_t devices,
159                      const audio_config& config);
160  std::unique_ptr<SimulatedInputBuffer> buffer_model_;
161  cvd::GceAudio *dev_;
162  audio_config config_;
163  float gain_;
164  audio_devices_t device_;
165  size_t frame_size_;
166  int64_t reported_lost_frames_;
167};
168
169}
170