forked from xiaozhi/xiaozhi-esp32
Audio stream packet with timestamp
This commit is contained in:
@@ -121,24 +121,24 @@ bool MqttProtocol::SendText(const std::string& text) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void MqttProtocol::SendAudio(const std::vector<uint8_t>& data) {
|
||||
void MqttProtocol::SendAudio(const AudioStreamPacket& packet) {
|
||||
std::lock_guard<std::mutex> lock(channel_mutex_);
|
||||
if (udp_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string nonce(aes_nonce_);
|
||||
*(uint16_t*)&nonce[2] = htons(data.size());
|
||||
*(uint16_t*)&nonce[2] = htons(packet.payload.size());
|
||||
*(uint32_t*)&nonce[12] = htonl(++local_sequence_);
|
||||
|
||||
std::string encrypted;
|
||||
encrypted.resize(aes_nonce_.size() + data.size());
|
||||
encrypted.resize(aes_nonce_.size() + packet.payload.size());
|
||||
memcpy(encrypted.data(), nonce.data(), nonce.size());
|
||||
|
||||
size_t nc_off = 0;
|
||||
uint8_t stream_block[16] = {0};
|
||||
if (mbedtls_aes_crypt_ctr(&aes_ctx_, data.size(), &nc_off, (uint8_t*)nonce.c_str(), stream_block,
|
||||
(uint8_t*)data.data(), (uint8_t*)&encrypted[nonce.size()]) != 0) {
|
||||
if (mbedtls_aes_crypt_ctr(&aes_ctx_, packet.payload.size(), &nc_off, (uint8_t*)nonce.c_str(), stream_block,
|
||||
(uint8_t*)packet.payload.data(), (uint8_t*)&encrypted[nonce.size()]) != 0) {
|
||||
ESP_LOGE(TAG, "Failed to encrypt audio data");
|
||||
return;
|
||||
}
|
||||
@@ -229,20 +229,20 @@ bool MqttProtocol::OpenAudioChannel() {
|
||||
ESP_LOGW(TAG, "Received audio packet with wrong sequence: %lu, expected: %lu", sequence, remote_sequence_ + 1);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> decrypted;
|
||||
size_t decrypted_size = data.size() - aes_nonce_.size();
|
||||
size_t nc_off = 0;
|
||||
uint8_t stream_block[16] = {0};
|
||||
decrypted.resize(decrypted_size);
|
||||
auto nonce = (uint8_t*)data.data();
|
||||
auto encrypted = (uint8_t*)data.data() + aes_nonce_.size();
|
||||
int ret = mbedtls_aes_crypt_ctr(&aes_ctx_, decrypted_size, &nc_off, nonce, stream_block, encrypted, (uint8_t*)decrypted.data());
|
||||
AudioStreamPacket packet;
|
||||
packet.payload.resize(decrypted_size);
|
||||
int ret = mbedtls_aes_crypt_ctr(&aes_ctx_, decrypted_size, &nc_off, nonce, stream_block, encrypted, (uint8_t*)packet.payload.data());
|
||||
if (ret != 0) {
|
||||
ESP_LOGE(TAG, "Failed to decrypt audio data, ret: %d", ret);
|
||||
return;
|
||||
}
|
||||
if (on_incoming_audio_ != nullptr) {
|
||||
on_incoming_audio_(std::move(decrypted));
|
||||
on_incoming_audio_(std::move(packet));
|
||||
}
|
||||
remote_sequence_ = sequence;
|
||||
last_incoming_time_ = std::chrono::steady_clock::now();
|
||||
|
||||
Reference in New Issue
Block a user