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