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 
16 #include "host/libs/audio_connector/commands.h"
17 
18 #include <algorithm>
19 #include <cstdint>
20 #include <vector>
21 
22 #include <android-base/logging.h>
23 
24 #include "host/libs/audio_connector/shm_layout.h"
25 
26 namespace cuttlefish {
27 
~AudioCommand()28 AudioCommand::~AudioCommand() {
29   CHECK(status_ != AudioStatus::NOT_SET)
30       << "A command of type " << static_cast<uint32_t>(type())
31       << " went out of scope without reply";
32 }
33 
JackInfoCommand(uint32_t start_id,size_t count,virtio_snd_jack_info * jack_info)34 JackInfoCommand::JackInfoCommand(uint32_t start_id, size_t count,
35                                  virtio_snd_jack_info* jack_info)
36     : InfoCommand(AudioCommandType::VIRTIO_SND_R_CHMAP_INFO, start_id, count,
37                   jack_info) {}
38 
Reply(AudioStatus status,const std::vector<virtio_snd_jack_info> & reply)39 void JackInfoCommand::Reply(AudioStatus status,
40                             const std::vector<virtio_snd_jack_info>& reply) {
41   MarkReplied(status);
42   if (status != AudioStatus::VIRTIO_SND_S_OK) {
43     return;
44   }
45   CHECK(reply.size() == count())
46       << "Returned unmatching info count: " << reply.size() << " vs "
47       << count();
48   for (int i = 0; i < reply.size(); ++i) {
49     info_reply()[i] = reply[i];
50   }
51 }
52 
ChmapInfoCommand(uint32_t start_id,size_t count,virtio_snd_chmap_info * chmap_info)53 ChmapInfoCommand::ChmapInfoCommand(uint32_t start_id, size_t count,
54                                    virtio_snd_chmap_info* chmap_info)
55     : InfoCommand(AudioCommandType::VIRTIO_SND_R_CHMAP_INFO, start_id, count,
56                   chmap_info) {}
57 
Reply(AudioStatus status,const std::vector<virtio_snd_chmap_info> & reply)58 void ChmapInfoCommand::Reply(AudioStatus status,
59                              const std::vector<virtio_snd_chmap_info>& reply) {
60   MarkReplied(status);
61   if (status != AudioStatus::VIRTIO_SND_S_OK) {
62     return;
63   }
64   CHECK(reply.size() == count())
65       << "Returned unmatching info count: " << reply.size() << " vs "
66       << count();
67   for (int i = 0; i < reply.size(); ++i) {
68     info_reply()[i].hdr.hda_fn_nid = Le32(reply[i].hdr.hda_fn_nid);
69     info_reply()[i].direction = reply[i].direction;
70     auto channels = std::min(VIRTIO_SND_CHMAP_MAX_SIZE, reply[i].channels);
71     info_reply()[i].channels = channels;
72     for (int j = 0; j < channels; ++j) {
73 	    info_reply()[i].positions[j] = reply[i].positions[j];
74     }
75   }
76 }
77 
StreamInfoCommand(uint32_t start_id,size_t count,virtio_snd_pcm_info * pcm_info)78 StreamInfoCommand::StreamInfoCommand(uint32_t start_id, size_t count,
79                                      virtio_snd_pcm_info* pcm_info)
80     : InfoCommand(AudioCommandType::VIRTIO_SND_R_PCM_INFO, start_id, count,
81                   pcm_info) {}
82 
Reply(AudioStatus status,const std::vector<virtio_snd_pcm_info> & reply)83 void StreamInfoCommand::Reply(AudioStatus status,
84                               const std::vector<virtio_snd_pcm_info>& reply) {
85   MarkReplied(status);
86   if (status != AudioStatus::VIRTIO_SND_S_OK) {
87     return;
88   }
89   CHECK(reply.size() == count())
90       << "Returned unmatching info count: " << reply.size() << " vs "
91       << count();
92   for (int i = 0; i < reply.size(); ++i) {
93     info_reply()[i].hdr.hda_fn_nid = Le32(reply[i].hdr.hda_fn_nid);
94     info_reply()[i].features = Le32(reply[i].features);
95     info_reply()[i].formats = Le64(reply[i].formats);
96     info_reply()[i].rates = Le64(reply[i].rates);
97     info_reply()[i].direction = reply[i].direction;
98     info_reply()[i].channels_min = reply[i].channels_min;
99     info_reply()[i].channels_max = reply[i].channels_max;
100     // pcm_info[i].padding is supposed to be all zeros in virtio-snd but here we
101     // can just ignore it.
102   }
103 }
104 
StreamControlCommand(AudioCommandType type,uint32_t stream_id)105 StreamControlCommand::StreamControlCommand(AudioCommandType type,
106                                            uint32_t stream_id)
107     : AudioCommand(type), stream_id_(stream_id) {}
108 
Reply(AudioStatus status)109 void StreamControlCommand::Reply(AudioStatus status) {
110   // These commands don't expect a reply, this method just forces
111   // acknowledgement of the command.
112   MarkReplied(status);
113 }
114 
StreamSetParamsCommand(uint32_t stream_id,uint32_t buffer_bytes,uint32_t period_bytes,uint32_t features,uint8_t channels,uint8_t format,uint8_t rate)115 StreamSetParamsCommand::StreamSetParamsCommand(
116     uint32_t stream_id, uint32_t buffer_bytes, uint32_t period_bytes,
117     uint32_t features, uint8_t channels, uint8_t format, uint8_t rate)
118     : StreamControlCommand(AudioCommandType::VIRTIO_SND_R_PCM_SET_PARAMS,
119                            stream_id),
120       buffer_bytes_(buffer_bytes),
121       period_bytes_(period_bytes),
122       features_(features),
123       channels_(channels),
124       format_(format),
125       rate_(rate) {}
126 
127 }  // namespace cuttlefish
128