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