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 StreamInfoCommand : public InfoCommand<virtio_snd_pcm_info> { 64 public: 65 StreamInfoCommand(uint32_t start_id, size_t count, 66 virtio_snd_pcm_info* pcm_info); 67 68 void Reply(AudioStatus status, const std::vector<virtio_snd_pcm_info>& reply); 69 }; 70 71 // Serves the START, STOP, PREPARE and RELEASE commands. It's also the 72 // superclass of the class handling SET_PARAMS 73 struct StreamControlCommand : public AudioCommand { 74 public: 75 StreamControlCommand(AudioCommandType type, uint32_t stream_id); 76 stream_idStreamControlCommand77 uint32_t stream_id() const { return stream_id_; } 78 79 void Reply(AudioStatus status); 80 81 private: 82 const uint32_t stream_id_; 83 }; 84 85 struct StreamSetParamsCommand : public StreamControlCommand { 86 public: 87 StreamSetParamsCommand(uint32_t stream_id, uint32_t buffer_bytes, 88 uint32_t period_bytes, uint32_t features, 89 uint8_t channels, uint8_t format, uint8_t rate); 90 buffer_bytesStreamSetParamsCommand91 uint32_t buffer_bytes() const { return buffer_bytes_; } period_bytesStreamSetParamsCommand92 uint32_t period_bytes() const { return period_bytes_; } featuresStreamSetParamsCommand93 uint32_t features() const { return features_; } channelsStreamSetParamsCommand94 uint8_t channels() const { return channels_; } formatStreamSetParamsCommand95 uint8_t format() const { return format_; } rateStreamSetParamsCommand96 uint8_t rate() const { return rate_; } 97 98 private: 99 const uint32_t buffer_bytes_; 100 const uint32_t period_bytes_; 101 const uint32_t features_; 102 const uint8_t channels_; 103 const uint8_t format_; 104 const uint8_t rate_; 105 }; 106 107 } // namespace cuttlefish 108