forked from xiaozhi/xiaozhi-esp32
update protocol to support manual response mode
This commit is contained in:
@@ -72,9 +72,9 @@ bool MqttProtocol::StartMqttClient() {
|
||||
} else if (strcmp(type->valuestring, "goodbye") == 0) {
|
||||
auto session_id = cJSON_GetObjectItem(root, "session_id");
|
||||
if (session_id == nullptr || session_id_ == session_id->valuestring) {
|
||||
if (on_audio_channel_closed_ != nullptr) {
|
||||
on_audio_channel_closed_();
|
||||
}
|
||||
Application::GetInstance().Schedule([this]() {
|
||||
CloseAudioChannel();
|
||||
});
|
||||
}
|
||||
} else if (on_incoming_json_ != nullptr) {
|
||||
on_incoming_json_(root);
|
||||
@@ -129,23 +129,6 @@ void MqttProtocol::SendAudio(const std::string& data) {
|
||||
udp_->Send(encrypted);
|
||||
}
|
||||
|
||||
void MqttProtocol::SendState(const std::string& state) {
|
||||
std::string message = "{";
|
||||
message += "\"session_id\":\"" + session_id_ + "\",";
|
||||
message += "\"type\":\"state\",";
|
||||
message += "\"state\":\"" + state + "\"";
|
||||
message += "}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
void MqttProtocol::SendAbort() {
|
||||
std::string message = "{";
|
||||
message += "\"session_id\":\"" + session_id_ + "\",";
|
||||
message += "\"type\":\"abort\"";
|
||||
message += "}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
void MqttProtocol::CloseAudioChannel() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(channel_mutex_);
|
||||
|
||||
@@ -26,9 +26,6 @@ public:
|
||||
~MqttProtocol();
|
||||
|
||||
void SendAudio(const std::string& data) override;
|
||||
void SendText(const std::string& text) override;
|
||||
void SendState(const std::string& state) override;
|
||||
void SendAbort() override;
|
||||
bool OpenAudioChannel() override;
|
||||
void CloseAudioChannel() override;
|
||||
bool IsAudioChannelOpened() const override;
|
||||
@@ -52,11 +49,12 @@ private:
|
||||
int udp_port_;
|
||||
uint32_t local_sequence_;
|
||||
uint32_t remote_sequence_;
|
||||
std::string session_id_;
|
||||
|
||||
bool StartMqttClient();
|
||||
void ParseServerHello(const cJSON* root);
|
||||
std::string DecodeHexString(const std::string& hex_string);
|
||||
|
||||
void SendText(const std::string& text) override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -23,3 +23,37 @@ void Protocol::OnAudioChannelClosed(std::function<void()> callback) {
|
||||
void Protocol::OnNetworkError(std::function<void(const std::string& message)> callback) {
|
||||
on_network_error_ = callback;
|
||||
}
|
||||
|
||||
void Protocol::SendAbortSpeaking(AbortReason reason) {
|
||||
std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"abort\"";
|
||||
if (reason == kAbortReasonWakeWordDetected) {
|
||||
message += ",\"reason\":\"wake_word_detected\"";
|
||||
}
|
||||
message += "}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
void Protocol::SendWakeWordDetected(const std::string& wake_word) {
|
||||
std::string json = "{\"session_id\":\"" + session_id_ +
|
||||
"\",\"type\":\"listen\",\"state\":\"detect\",\"text\":\"" + wake_word + "\"}";
|
||||
SendText(json);
|
||||
}
|
||||
|
||||
void Protocol::SendStartListening(ListeningMode mode) {
|
||||
std::string message = "{\"session_id\":\"" + session_id_ + "\"";
|
||||
message += ",\"type\":\"listen\",\"state\":\"start\"";
|
||||
if (mode == kListeningModeAlwaysOn) {
|
||||
message += ",\"mode\":\"realtime\"";
|
||||
} else if (mode == kListeningModeAutoStop) {
|
||||
message += ",\"mode\":\"auto\"";
|
||||
} else {
|
||||
message += ",\"mode\":\"manual\"";
|
||||
}
|
||||
message += "}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
void Protocol::SendStopListening() {
|
||||
std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"listen\",\"state\":\"stop\"}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,16 @@ struct BinaryProtocol3 {
|
||||
uint8_t payload[];
|
||||
} __attribute__((packed));
|
||||
|
||||
enum AbortReason {
|
||||
kAbortReasonNone,
|
||||
kAbortReasonWakeWordDetected
|
||||
};
|
||||
|
||||
enum ListeningMode {
|
||||
kListeningModeAutoStop,
|
||||
kListeningModeManualStop,
|
||||
kListeningModeAlwaysOn // 需要 AEC 支持
|
||||
};
|
||||
|
||||
class Protocol {
|
||||
public:
|
||||
@@ -27,13 +37,14 @@ public:
|
||||
void OnAudioChannelClosed(std::function<void()> callback);
|
||||
void OnNetworkError(std::function<void(const std::string& message)> callback);
|
||||
|
||||
virtual void SendAudio(const std::string& data) = 0;
|
||||
virtual void SendText(const std::string& text) = 0;
|
||||
virtual void SendState(const std::string& state) = 0;
|
||||
virtual void SendAbort() = 0;
|
||||
virtual bool OpenAudioChannel() = 0;
|
||||
virtual void CloseAudioChannel() = 0;
|
||||
virtual bool IsAudioChannelOpened() const = 0;
|
||||
virtual void SendAudio(const std::string& data) = 0;
|
||||
virtual void SendWakeWordDetected(const std::string& wake_word);
|
||||
virtual void SendStartListening(ListeningMode mode);
|
||||
virtual void SendStopListening();
|
||||
virtual void SendAbortSpeaking(AbortReason reason);
|
||||
|
||||
protected:
|
||||
std::function<void(const cJSON* root)> on_incoming_json_;
|
||||
@@ -43,6 +54,9 @@ protected:
|
||||
std::function<void(const std::string& message)> on_network_error_;
|
||||
|
||||
int server_sample_rate_ = 16000;
|
||||
std::string session_id_;
|
||||
|
||||
virtual void SendText(const std::string& text) = 0;
|
||||
};
|
||||
|
||||
#endif // PROTOCOL_H
|
||||
|
||||
@@ -39,21 +39,6 @@ void WebsocketProtocol::SendText(const std::string& text) {
|
||||
websocket_->Send(text);
|
||||
}
|
||||
|
||||
void WebsocketProtocol::SendState(const std::string& state) {
|
||||
std::string message = "{";
|
||||
message += "\"type\":\"state\",";
|
||||
message += "\"state\":\"" + state + "\"";
|
||||
message += "}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
void WebsocketProtocol::SendAbort() {
|
||||
std::string message = "{";
|
||||
message += "\"type\":\"abort\"";
|
||||
message += "}";
|
||||
SendText(message);
|
||||
}
|
||||
|
||||
bool WebsocketProtocol::IsAudioChannelOpened() const {
|
||||
return websocket_ != nullptr;
|
||||
}
|
||||
|
||||
@@ -16,9 +16,6 @@ public:
|
||||
~WebsocketProtocol();
|
||||
|
||||
void SendAudio(const std::string& data) override;
|
||||
void SendText(const std::string& text) override;
|
||||
void SendState(const std::string& state) override;
|
||||
void SendAbort() override;
|
||||
bool OpenAudioChannel() override;
|
||||
void CloseAudioChannel() override;
|
||||
bool IsAudioChannelOpened() const override;
|
||||
@@ -28,6 +25,7 @@ private:
|
||||
WebSocket* websocket_ = nullptr;
|
||||
|
||||
void ParseServerHello(const cJSON* root);
|
||||
void SendText(const std::string& text) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user