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 <android-base/logging.h>
19
20 #include "host/libs/audio_connector/shm_layout.h"
21
22 namespace cuttlefish {
23
~AudioCommand()24 AudioCommand::~AudioCommand() {
25 CHECK(status_ != AudioStatus::NOT_SET)
26 << "A command of type " << static_cast<uint32_t>(type())
27 << " went out of scope without reply";
28 }
29
StreamInfoCommand(uint32_t start_id,size_t count,virtio_snd_pcm_info * pcm_info)30 StreamInfoCommand::StreamInfoCommand(uint32_t start_id, size_t count,
31 virtio_snd_pcm_info* pcm_info)
32 : InfoCommand(AudioCommandType::VIRTIO_SND_R_PCM_INFO, start_id, count,
33 pcm_info) {}
34
Reply(AudioStatus status,const std::vector<virtio_snd_pcm_info> & reply)35 void StreamInfoCommand::Reply(AudioStatus status,
36 const std::vector<virtio_snd_pcm_info>& reply) {
37 MarkReplied(status);
38 if (status != AudioStatus::VIRTIO_SND_S_OK) {
39 return;
40 }
41 CHECK(reply.size() == count())
42 << "Returned unmatching info count: " << reply.size() << " vs "
43 << count();
44 for (int i = 0; i < reply.size(); ++i) {
45 info_reply()[i].hdr.hda_fn_nid = Le32(reply[i].hdr.hda_fn_nid);
46 info_reply()[i].features = Le32(reply[i].features);
47 info_reply()[i].formats = Le64(reply[i].formats);
48 info_reply()[i].rates = Le64(reply[i].rates);
49 info_reply()[i].direction = reply[i].direction;
50 info_reply()[i].channels_min = reply[i].channels_min;
51 info_reply()[i].channels_max = reply[i].channels_max;
52 // pcm_info[i].padding is supposed to be all zeros in virtio-snd but here we
53 // can just ignore it.
54 }
55 }
56
StreamControlCommand(AudioCommandType type,uint32_t stream_id)57 StreamControlCommand::StreamControlCommand(AudioCommandType type,
58 uint32_t stream_id)
59 : AudioCommand(type), stream_id_(stream_id) {}
60
Reply(AudioStatus status)61 void StreamControlCommand::Reply(AudioStatus status) {
62 // These commands don't expect a reply, this method just forces
63 // acknowledgement of the command.
64 MarkReplied(status);
65 }
66
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)67 StreamSetParamsCommand::StreamSetParamsCommand(
68 uint32_t stream_id, uint32_t buffer_bytes, uint32_t period_bytes,
69 uint32_t features, uint8_t channels, uint8_t format, uint8_t rate)
70 : StreamControlCommand(AudioCommandType::VIRTIO_SND_R_PCM_SET_PARAMS,
71 stream_id),
72 buffer_bytes_(buffer_bytes),
73 period_bytes_(period_bytes),
74 features_(features),
75 channels_(channels),
76 format_(format),
77 rate_(rate) {}
78
79 } // namespace cuttlefish
80