1 //
2 // Copyright (C) 2020 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 #pragma once
16 
17 #include <stdint.h>
18 
19 #include <vector>
20 
21 #include "host/libs/audio_connector/shm_layout.h"
22 
23 namespace cuttlefish {
24 
25 class AudioCommand {
26  public:
27   virtual ~AudioCommand();
28 
type()29   AudioCommandType type() const { return type_; }
status()30   AudioStatus status() const { return status_; }
31 
32  protected:
AudioCommand(AudioCommandType type)33   AudioCommand(AudioCommandType type) : type_(type) {}
34 
MarkReplied(AudioStatus status)35   void MarkReplied(AudioStatus status) { status_ = status; }
36 
37  private:
38   AudioStatus status_ = AudioStatus::NOT_SET;
39   const AudioCommandType type_;
40 };
41 
42 template <typename R>
43 class InfoCommand : public AudioCommand {
44  public:
InfoCommand(AudioCommandType type,uint32_t start_id,size_t count,R * reply)45   InfoCommand(AudioCommandType type, uint32_t start_id, size_t count, R* reply)
46       : AudioCommand(type),
47         start_id_(start_id),
48         count_(count),
49         info_reply_(reply) {}
50 
start_id()51   uint32_t start_id() const { return start_id_; }
count()52   uint32_t count() const { return count_; }
53 
54  protected:
info_reply()55   R* info_reply() { return info_reply_; }
56 
57  private:
58   const uint32_t start_id_;
59   const size_t count_;
60   R* info_reply_;
61 };
62 
63 class ChmapInfoCommand : public InfoCommand<virtio_snd_chmap_info> {
64  public:
65   ChmapInfoCommand(uint32_t start_id, size_t count,
66                    virtio_snd_chmap_info* chmap_info);
67 
68   void Reply(AudioStatus status,
69              const std::vector<virtio_snd_chmap_info>& reply);
70 };
71 
72 class JackInfoCommand : public InfoCommand<virtio_snd_jack_info> {
73  public:
74   JackInfoCommand(uint32_t start_id, size_t count,
75                    virtio_snd_jack_info* jack_info);
76 
77   void Reply(AudioStatus status,
78              const std::vector<virtio_snd_jack_info>& reply);
79 };
80 
81 class StreamInfoCommand : public InfoCommand<virtio_snd_pcm_info> {
82  public:
83   StreamInfoCommand(uint32_t start_id, size_t count,
84                     virtio_snd_pcm_info* pcm_info);
85 
86   void Reply(AudioStatus status, const std::vector<virtio_snd_pcm_info>& reply);
87 };
88 
89 // Serves the START, STOP, PREPARE and RELEASE commands. It's also the
90 // superclass of the class handling SET_PARAMS
91 struct StreamControlCommand : public AudioCommand {
92  public:
93   StreamControlCommand(AudioCommandType type, uint32_t stream_id);
94 
stream_idStreamControlCommand95   uint32_t stream_id() const { return stream_id_; }
96 
97   void Reply(AudioStatus status);
98 
99  private:
100   const uint32_t stream_id_;
101 };
102 
103 struct StreamSetParamsCommand : public StreamControlCommand {
104  public:
105   StreamSetParamsCommand(uint32_t stream_id, uint32_t buffer_bytes,
106                          uint32_t period_bytes, uint32_t features,
107                          uint8_t channels, uint8_t format, uint8_t rate);
108 
buffer_bytesStreamSetParamsCommand109   uint32_t buffer_bytes() const { return buffer_bytes_; }
period_bytesStreamSetParamsCommand110   uint32_t period_bytes() const { return period_bytes_; }
featuresStreamSetParamsCommand111   uint32_t features() const { return features_; }
channelsStreamSetParamsCommand112   uint8_t channels() const { return channels_; }
formatStreamSetParamsCommand113   uint8_t format() const { return format_; }
rateStreamSetParamsCommand114   uint8_t rate() const { return rate_; }
115 
116  private:
117   const uint32_t buffer_bytes_;
118   const uint32_t period_bytes_;
119   const uint32_t features_;
120   const uint8_t channels_;
121   const uint8_t format_;
122   const uint8_t rate_;
123 };
124 
125 }  // namespace cuttlefish
126