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> |
| 18 | #include "audio_service.grpc.pb.h" |
| 19 | |
| 20 | using grpc::Channel; |
| 21 | using grpc::ClientContext; |
| 22 | using grpc::ClientReader; |
| 23 | using grpc::ClientReaderWriter; |
| 24 | using grpc::ClientWriter; |
| 25 | using grpc::Status; |
| 26 | using google::protobuf::Empty; |
| 27 | |
| 28 | typedef struct audio_stream_client { |
| 29 | char name[32]; |
| 30 | struct audio_stream stream; |
| 31 | } audio_stream_client_t; |
| 32 | |
| 33 | typedef struct audio_stream_out_client { |
| 34 | char name[32]; |
| 35 | struct audio_stream_out stream_out; |
| 36 | } audio_stream_out_client_t; |
| 37 | |
| 38 | typedef struct audio_stream_in_client { |
| 39 | char name[32]; |
| 40 | struct audio_stream_in stream_in; |
| 41 | } audio_stream_in_client_t; |
| 42 | |
| 43 | template< class T, class M > |
| 44 | static inline constexpr ptrdiff_t offset_of( const M T::*member ) { |
| 45 | return reinterpret_cast< ptrdiff_t >( &( reinterpret_cast< T* >( 0 )->*member ) ); |
| 46 | } |
| 47 | |
| 48 | template< class T, class M > |
| 49 | static inline T* container_of( const M *ptr, const M T::*member ) { |
| 50 | return reinterpret_cast< T* >( reinterpret_cast< intptr_t >( ptr ) - offset_of( member ) ); |
| 51 | } |
| 52 | |
| 53 | inline audio_stream_in_client_t* audio_stream_in_to_client(const audio_stream_in *p) |
| 54 | { |
| 55 | return container_of(p, &audio_stream_in_client::stream_in); |
| 56 | } |
| 57 | |
| 58 | inline audio_stream_out_client_t* audio_stream_out_to_client(const audio_stream_out *p) |
| 59 | { |
| 60 | return container_of(p, &audio_stream_out_client::stream_out); |
| 61 | } |
| 62 | |
| 63 | inline audio_stream_client_t* audio_stream_to_client(const audio_stream *p) |
| 64 | { |
| 65 | return container_of(p, &audio_stream_client::stream); |
| 66 | } |
| 67 | |
| 68 | using namespace audio_service; |
| 69 | |
| 70 | class AudioClient { |
| 71 | public: |
| 72 | AudioClient(std::shared_ptr<Channel> channel) |
| 73 | : stub_(AudioService::NewStub(channel)) { |
| 74 | shm_ = std::make_unique<managed_shared_memory>(open_only, "AudioServiceShmem"); |
| 75 | } |
| 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); |
| 109 | int Device_dump(const struct audio_hw_device *dev, int fd); |
| 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); |
| 157 | |
| 158 | const int kSharedBufferSize = 256 * 1024; |
| 159 | |
| 160 | private: |
| 161 | std::string new_stream_name(char *name, size_t size) { |
| 162 | int pid = ::getpid(); |
| 163 | int seq = (stream_seq_++); |
| 164 | printf("pid=%d seq=%d\n", pid, seq); |
| 165 | snprintf(name, size, "%d-%d", pid, seq); |
| 166 | printf("name = %s\n", name); |
| 167 | return std::string(name); |
| 168 | } |
| 169 | |
| 170 | std::unique_ptr<AudioService::Stub> stub_; |
| 171 | std::unique_ptr<managed_shared_memory> shm_; |
| 172 | static std::atomic_int stream_seq_; |
| 173 | }; |
| 174 | |
| 175 | #endif // AUDIO_CLIENT_H |
| 176 | |