2024-11-14 23:15:43 +08:00
|
|
|
#ifndef PROTOCOL_H
|
|
|
|
|
#define PROTOCOL_H
|
|
|
|
|
|
|
|
|
|
#include <cJSON.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
2024-11-16 05:49:35 +08:00
|
|
|
struct BinaryProtocol3 {
|
|
|
|
|
uint8_t type;
|
|
|
|
|
uint8_t reserved;
|
|
|
|
|
uint16_t payload_size;
|
|
|
|
|
uint8_t payload[];
|
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
|
|
|
2024-11-14 23:15:43 +08:00
|
|
|
class Protocol {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Protocol() = default;
|
|
|
|
|
|
2024-11-16 05:49:35 +08:00
|
|
|
inline int server_sample_rate() const {
|
|
|
|
|
return server_sample_rate_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnIncomingAudio(std::function<void(const std::string& data)> callback);
|
|
|
|
|
void OnIncomingJson(std::function<void(const cJSON* root)> callback);
|
|
|
|
|
void OnAudioChannelOpened(std::function<void()> callback);
|
|
|
|
|
void OnAudioChannelClosed(std::function<void()> callback);
|
2024-11-20 03:28:52 +08:00
|
|
|
void OnNetworkError(std::function<void(const std::string& message)> callback);
|
2024-11-16 05:49:35 +08:00
|
|
|
|
2024-11-14 23:15:43 +08:00
|
|
|
virtual void SendAudio(const std::string& data) = 0;
|
|
|
|
|
virtual void SendText(const std::string& text) = 0;
|
2024-11-15 04:44:53 +08:00
|
|
|
virtual void SendState(const std::string& state) = 0;
|
|
|
|
|
virtual void SendAbort() = 0;
|
2024-11-14 23:15:43 +08:00
|
|
|
virtual bool OpenAudioChannel() = 0;
|
|
|
|
|
virtual void CloseAudioChannel() = 0;
|
2024-11-15 04:44:53 +08:00
|
|
|
virtual bool IsAudioChannelOpened() const = 0;
|
2024-11-16 05:49:35 +08:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::function<void(const cJSON* root)> on_incoming_json_;
|
|
|
|
|
std::function<void(const std::string& data)> on_incoming_audio_;
|
|
|
|
|
std::function<void()> on_audio_channel_opened_;
|
|
|
|
|
std::function<void()> on_audio_channel_closed_;
|
2024-11-20 03:28:52 +08:00
|
|
|
std::function<void(const std::string& message)> on_network_error_;
|
2024-11-16 05:49:35 +08:00
|
|
|
|
|
|
|
|
int server_sample_rate_ = 16000;
|
2024-11-14 23:15:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // PROTOCOL_H
|
|
|
|
|
|