2、新增HttpsPlaybackFromUrl通用HTTPS音频下载播放方法,obtain_story同时支持HTTPS URL和WebSocket两种方式; 3、新增RTC↔HTTPS双向音频切换三标志位状态机(opus_playback_active_/https_playback_active_/https_playback_abort_),HTTPS播放期间静默丢弃RTC PCM包,OnAudioOutput捕获is_opus_frame防止残留Opus帧杂音; 4、新增AbortHttpsPlayback中止方法,使用独立高优先级任务(priority=10)执行DMA flush; 5、协议层新增OnBotMessage回调,Bot下行消息立即中止HTTPS播放;volc_rtc_protocol移除is_binary依赖改为直接前缀检测,新增info前缀和subv跳过逻辑; 6、新增subtitle字幕消息解析,通过bot_前缀区分USER/AI,用户说话时立即中止HTTPS播放; 7、AbortSpeaking新增HTTPS中止信号和DMA缓冲区flush; 8、Kconfig新增STORY_API_URL故事播放API地址配置; Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
98 lines
3.5 KiB
C++
98 lines
3.5 KiB
C++
#ifndef PROTOCOL_H
|
|
#define PROTOCOL_H
|
|
|
|
#include <cJSON.h>
|
|
#include <string>
|
|
#include <functional>
|
|
#include <chrono>
|
|
|
|
struct BinaryProtocol3 {
|
|
uint8_t type;
|
|
uint8_t reserved;
|
|
uint16_t payload_size;
|
|
uint8_t payload[];
|
|
} __attribute__((packed));
|
|
|
|
enum AbortReason {
|
|
kAbortReasonNone,
|
|
kAbortReasonWakeWordDetected,
|
|
kAbortReasonVoiceInterrupt
|
|
//kAbortReasonNewStory // websocket推送新故事时中断当前播放
|
|
};
|
|
|
|
enum ListeningMode {
|
|
kListeningModeAutoStop,
|
|
kListeningModeManualStop,
|
|
kListeningModeRealtime // 需要 AEC 支持
|
|
};
|
|
|
|
class Protocol {
|
|
public:
|
|
virtual ~Protocol() = default;
|
|
|
|
inline int server_sample_rate() const {
|
|
return server_sample_rate_;
|
|
}
|
|
inline int server_frame_duration() const {
|
|
return server_frame_duration_;
|
|
}
|
|
inline bool downlink_is_pcm() const {
|
|
return downlink_is_pcm_;
|
|
}
|
|
inline const std::string& session_id() const {
|
|
return session_id_;
|
|
}
|
|
inline void SetSuppressIncomingMessageLog(bool v) { suppress_incoming_message_log_ = v; }
|
|
|
|
void OnIncomingAudio(std::function<void(std::vector<uint8_t>&& data)> callback);
|
|
void OnIncomingJson(std::function<void(const cJSON* root)> callback);
|
|
void OnAudioChannelOpened(std::function<void()> callback);
|
|
void OnAudioChannelClosed(std::function<void()> callback);
|
|
void OnNetworkError(std::function<void(const std::string& message)> callback);
|
|
void OnBotMessage(std::function<void()> callback);
|
|
|
|
virtual void Start() = 0;
|
|
virtual bool OpenAudioChannel() = 0;
|
|
virtual void CloseAudioChannel() = 0;
|
|
virtual bool IsAudioChannelOpened() const = 0;
|
|
virtual void SendAudio(const std::vector<uint8_t>& data) = 0;
|
|
virtual void SendPcm(const std::vector<uint8_t>& data) {}
|
|
virtual void SendG711A(const std::vector<uint8_t>& data) {}
|
|
virtual void SendWakeWordDetected(const std::string& wake_word);
|
|
virtual void SendStartListening(ListeningMode mode);
|
|
virtual void SendStopListening();
|
|
virtual void SendAbortSpeaking(AbortReason reason);
|
|
virtual void SendTextMessage(const std::string& text);
|
|
virtual void SendStoryRequest(); // 声明 发送讲故事请求 【新增】
|
|
virtual void SendIotDescriptors(const std::string& descriptors);
|
|
virtual void SendIotStates(const std::string& states);
|
|
virtual void SendFunctionResult(const std::string& tool_call_id, const std::string& content) {
|
|
(void)tool_call_id;
|
|
SendTextMessage(content);
|
|
}
|
|
|
|
protected:
|
|
std::function<void(const cJSON* root)> on_incoming_json_;
|
|
std::function<void(std::vector<uint8_t>&& data)> on_incoming_audio_;
|
|
std::function<void()> on_audio_channel_opened_;
|
|
std::function<void()> on_audio_channel_closed_;
|
|
std::function<void(const std::string& message)> on_network_error_;
|
|
std::function<void()> on_bot_message_;
|
|
|
|
int server_sample_rate_ = 24000;
|
|
int server_frame_duration_ = 60;
|
|
bool downlink_is_pcm_ = false;// 是否是PCM格式
|
|
bool error_occurred_ = false;
|
|
std::string session_id_;
|
|
bool start_listening_pending_ = false;// 是否有待处理的监听请求
|
|
ListeningMode pending_listening_mode_ = kListeningModeRealtime;// 待处理的监听模式
|
|
std::chrono::time_point<std::chrono::steady_clock> last_incoming_time_;
|
|
bool suppress_incoming_message_log_ = false;
|
|
|
|
virtual void SendText(const std::string& text) = 0;
|
|
virtual void SetError(const std::string& message);
|
|
virtual bool IsTimeout() const;
|
|
};
|
|
|
|
#endif // PROTOCOL_H
|