Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 1 | #ifndef AUDIO_CLIENT_H |
| 2 | #define AUDIO_CLIENT_H |
| 3 | |
| 4 | #include <boost/interprocess/managed_shared_memory.hpp> |
| 5 | #include <cstdlib> |
| 6 | #include <iomanip> |
| 7 | #include <unistd.h> |
Tim Yao | 41ac587 | 2022-12-11 13:07:32 -0800 | [diff] [blame^] | 8 | #include <fstream> |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 9 | |
| 10 | #include <hardware/hardware.h> |
| 11 | #include <hardware/audio.h> |
Tim Yao | aaa3bc5 | 2020-12-30 17:40:14 -0800 | [diff] [blame] | 12 | #include <IpcBuffer/IpcBuffer.h> |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 13 | |
| 14 | #include <grpc/grpc.h> |
| 15 | #include <grpcpp/channel.h> |
| 16 | #include <grpcpp/client_context.h> |
| 17 | #include <grpcpp/create_channel.h> |
| 18 | #include <grpcpp/security/credentials.h> |
cheng tong | 7d90788 | 2020-09-04 18:53:04 +0800 | [diff] [blame] | 19 | #include "audio_effect_if.h" |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 20 | #include "audio_service.grpc.pb.h" |
| 21 | |
| 22 | using grpc::Channel; |
| 23 | using grpc::ClientContext; |
| 24 | using grpc::ClientReader; |
| 25 | using grpc::ClientReaderWriter; |
| 26 | using grpc::ClientWriter; |
| 27 | using grpc::Status; |
| 28 | using google::protobuf::Empty; |
| 29 | |
| 30 | typedef struct audio_stream_client { |
| 31 | char name[32]; |
| 32 | struct audio_stream stream; |
| 33 | } audio_stream_client_t; |
| 34 | |
| 35 | typedef struct audio_stream_out_client { |
| 36 | char name[32]; |
| 37 | struct audio_stream_out stream_out; |
| 38 | } audio_stream_out_client_t; |
| 39 | |
| 40 | typedef struct audio_stream_in_client { |
| 41 | char name[32]; |
| 42 | struct audio_stream_in stream_in; |
| 43 | } audio_stream_in_client_t; |
| 44 | |
| 45 | template< class T, class M > |
| 46 | static inline constexpr ptrdiff_t offset_of( const M T::*member ) { |
| 47 | return reinterpret_cast< ptrdiff_t >( &( reinterpret_cast< T* >( 0 )->*member ) ); |
| 48 | } |
| 49 | |
| 50 | template< class T, class M > |
| 51 | static inline T* container_of( const M *ptr, const M T::*member ) { |
| 52 | return reinterpret_cast< T* >( reinterpret_cast< intptr_t >( ptr ) - offset_of( member ) ); |
| 53 | } |
| 54 | |
| 55 | inline audio_stream_in_client_t* audio_stream_in_to_client(const audio_stream_in *p) |
| 56 | { |
| 57 | return container_of(p, &audio_stream_in_client::stream_in); |
| 58 | } |
| 59 | |
| 60 | inline audio_stream_out_client_t* audio_stream_out_to_client(const audio_stream_out *p) |
| 61 | { |
| 62 | return container_of(p, &audio_stream_out_client::stream_out); |
| 63 | } |
| 64 | |
| 65 | inline audio_stream_client_t* audio_stream_to_client(const audio_stream *p) |
| 66 | { |
| 67 | return container_of(p, &audio_stream_client::stream); |
| 68 | } |
| 69 | |
| 70 | using namespace audio_service; |
| 71 | |
| 72 | class AudioClient { |
| 73 | public: |
| 74 | AudioClient(std::shared_ptr<Channel> channel) |
Tim Yao | aaa3bc5 | 2020-12-30 17:40:14 -0800 | [diff] [blame] | 75 | : stub_(AudioService::NewStub(channel)) {} |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 76 | |
| 77 | // Device methods |
| 78 | int Device_common_close(struct hw_device_t* device); |
| 79 | int Device_init_check(const struct audio_hw_device *dev); |
| 80 | int Device_set_voice_volume(struct audio_hw_device *dev, float volume); |
| 81 | int Device_set_master_volume(struct audio_hw_device *dev, float volume); |
| 82 | int Device_get_master_volume(struct audio_hw_device *dev, float *volume); |
| 83 | int Device_set_mode(struct audio_hw_device *dev, audio_mode_t mode); |
| 84 | int Device_set_mic_mute(struct audio_hw_device *dev, bool state); |
| 85 | int Device_get_mic_mute(const struct audio_hw_device *dev, bool *state); |
| 86 | int Device_set_parameters(struct audio_hw_device *dev, const char *kv_pairs); |
| 87 | char * Device_get_parameters(const struct audio_hw_device *dev, const char *keys); |
| 88 | size_t Device_get_input_buffer_size(const struct audio_hw_device *dev, |
| 89 | const struct audio_config *config); |
| 90 | int Device_open_output_stream(struct audio_hw_device *dev, |
| 91 | audio_io_handle_t handle, |
| 92 | audio_devices_t devices, |
| 93 | audio_output_flags_t flags, |
| 94 | struct audio_config *config, |
| 95 | audio_stream_out_client_t *stream_out, |
| 96 | const char *address); |
| 97 | void Device_close_output_stream(struct audio_hw_device *dev, |
| 98 | struct audio_stream_out* stream_out); |
| 99 | int Device_open_input_stream(struct audio_hw_device *dev, |
| 100 | audio_io_handle_t handle, |
| 101 | audio_devices_t devices, |
| 102 | struct audio_config *config, |
| 103 | audio_stream_in_client_t *stream_in, |
| 104 | audio_input_flags_t flags, |
| 105 | const char *address, |
| 106 | audio_source_t source); |
| 107 | void Device_close_input_stream(struct audio_hw_device *dev, |
| 108 | struct audio_stream_in *stream_in); |
Tim Yao | ab2a3a6 | 2020-10-29 15:33:55 -0700 | [diff] [blame] | 109 | char * Device_dump(const struct audio_hw_device *dev, int fd); |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 110 | int Device_set_master_mute(struct audio_hw_device *dev, bool mute); |
| 111 | int Device_get_master_mute(struct audio_hw_device *dev, bool *mute); |
| 112 | int Device_create_audio_patch(struct audio_hw_device *dev, |
| 113 | unsigned int num_sources, |
| 114 | const struct audio_port_config *sources, |
| 115 | unsigned int num_sinks, |
| 116 | const struct audio_port_config *sinks, |
| 117 | audio_patch_handle_t *handle); |
| 118 | int Device_release_audio_patch(struct audio_hw_device *dev, |
| 119 | audio_patch_handle_t handle); |
| 120 | int Device_set_audio_port_config(struct audio_hw_device *dev, |
| 121 | const struct audio_port_config *config); |
| 122 | |
| 123 | // stream in methods |
| 124 | int stream_in_set_gain(struct audio_stream_in *stream, float gain); |
| 125 | ssize_t stream_in_read(struct audio_stream_in *stream, void* buffer, |
| 126 | size_t bytes); |
| 127 | uint32_t stream_in_get_input_frames_lost(struct audio_stream_in *stream); |
| 128 | int stream_in_get_capture_position(const struct audio_stream_in *stream, |
| 129 | int64_t *frames, int64_t *time); |
| 130 | |
| 131 | // stream_out methods |
| 132 | uint32_t stream_out_get_latency(const struct audio_stream_out *stream); |
| 133 | int stream_out_set_volume(struct audio_stream_out *stream, float left, float right); |
| 134 | ssize_t stream_out_write(struct audio_stream_out *stream, const void* buffer, |
| 135 | size_t bytes); |
| 136 | int stream_out_get_render_position(const struct audio_stream_out *stream, |
| 137 | uint32_t *dsp_frames); |
| 138 | int stream_out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 139 | int64_t *timestamp); |
| 140 | int stream_out_pause(struct audio_stream_out* stream); |
| 141 | int stream_out_resume(struct audio_stream_out* stream); |
| 142 | int stream_out_flush(struct audio_stream_out* stream); |
| 143 | int stream_out_get_presentation_position(const struct audio_stream_out *stream, |
| 144 | uint64_t *frames, struct timespec *timestamp); |
| 145 | |
| 146 | // stream common methods |
| 147 | uint32_t stream_get_sample_rate(const struct audio_stream *stream); |
| 148 | size_t stream_get_buffer_size(const struct audio_stream *stream); |
| 149 | audio_channel_mask_t stream_get_channels(const struct audio_stream *stream); |
| 150 | audio_format_t stream_get_format(const struct audio_stream *stream); |
| 151 | int stream_standby(struct audio_stream *stream); |
| 152 | int stream_dump(const struct audio_stream *stream, int fd); |
| 153 | audio_devices_t stream_get_device(const struct audio_stream *stream); |
| 154 | int stream_set_parameters(struct audio_stream *stream, const char *kv_pairs); |
| 155 | char * stream_get_parameters(const struct audio_stream *stream, |
| 156 | const char *keys); |
cheng tong | 7d90788 | 2020-09-04 18:53:04 +0800 | [diff] [blame] | 157 | int Effect_set_parameters(aml_audio_effect_type_e type, effect_param_t *param); |
| 158 | int Effect_get_parameters(aml_audio_effect_type_e type, effect_param_t *param); |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 159 | |
| 160 | const int kSharedBufferSize = 256 * 1024; |
| 161 | |
| 162 | private: |
| 163 | std::string new_stream_name(char *name, size_t size) { |
| 164 | int pid = ::getpid(); |
| 165 | int seq = (stream_seq_++); |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 166 | snprintf(name, size, "%d-%d", pid, seq); |
Tim Yao | 41ac587 | 2022-12-11 13:07:32 -0800 | [diff] [blame^] | 167 | printf("pid=%d seq=%d name=%s\n", pid, seq, name); |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 168 | return std::string(name); |
| 169 | } |
Tim Yao | 41ac587 | 2022-12-11 13:07:32 -0800 | [diff] [blame^] | 170 | void update_stream_name(char *name, size_t size, int id) { |
| 171 | int pid = ::getpid(); |
| 172 | if ((id != pid) && (id > 0)) { |
| 173 | int seq; |
| 174 | if (sscanf(name, "%d-%d", &pid, &seq) == 2) { |
| 175 | printf("vpid %d -> pid %d\n", pid, id); |
| 176 | snprintf(name, size, "%d-%d", id, seq); |
| 177 | } |
| 178 | } |
| 179 | } |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 180 | |
| 181 | std::unique_ptr<AudioService::Stub> stub_; |
Tim Yao | e8c0d4a | 2019-11-27 14:47:35 -0800 | [diff] [blame] | 182 | static std::atomic_int stream_seq_; |
| 183 | }; |
| 184 | |
| 185 | #endif // AUDIO_CLIENT_H |
| 186 | |