From: hcj <1224667075@qq.com> Date: Fri, 30 Jan 2026 08:05:29 +0000 (+0800) Subject: 优化opus解码问题 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=0b5787c3d8b41db2b8fdc037aa2800e9404f6c1b;p=mirrors%2FFD230.git 优化opus解码问题 --- diff --git a/code/sdk/zhihui/MQTT/uapi_mqtt_at.c b/code/sdk/zhihui/MQTT/uapi_mqtt_at.c index e7b3a3b8b..fbd4a7711 100644 --- a/code/sdk/zhihui/MQTT/uapi_mqtt_at.c +++ b/code/sdk/zhihui/MQTT/uapi_mqtt_at.c @@ -81,25 +81,6 @@ static void mqtt_at_message_cb(void *arg, const char *topic, const uint8_t *data } } -/* OTA-发送设备信息到服务器:AT^LN_OTA_SEND_DEVICE_INFO */ -// static uint32_t ln_ota_send_device_info_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd) -// { -// (void)para; -// (void)paraLen; -// (void)strCommonCmd; - -// printf("[LN][OTA_SEND_DEVICE_INFO]\n"); - -// // 直接调用封装函数,使用默认URL(NULL表示使用默认URL) -// int ret = ln_ota_send_device_info_json(NULL, NULL); -// if (ret == 0) { -// printf("+LN_OTA_SEND_DEVICE_INFO: OK\n"); -// return ERRCODE_SUCC; -// } else { -// printf("+LN_OTA_SEND_DEVICE_INFO: FAIL\n"); -// return ERRCODE_FAIL; -// } -// } /* MQTT-初始化:AT^LN_MQTT_INIT */ static uint32_t uapi_mqtt_init_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd) diff --git a/code/sdk/zhihui/leiniao/imsl_ln/LN_MQTT/ln_mqtt_api_at.c b/code/sdk/zhihui/leiniao/imsl_ln/LN_MQTT/ln_mqtt_api_at.c index 834e2eb42..d7a899023 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/LN_MQTT/ln_mqtt_api_at.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/LN_MQTT/ln_mqtt_api_at.c @@ -172,11 +172,34 @@ static uint32_t ln_mqtt_stop_service_at(uint8_t *para, uint32_t paraLen, char *s return ERRCODE_SUCC; } + +/* OTA-发送设备信息到服务器:AT^LN_OTA_SEND_DEVICE_INFO */ +static uint32_t ln_ota_send_device_info_at(uint8_t *para, uint32_t paraLen, char *strCommonCmd) +{ + (void)para; + (void)paraLen; + (void)strCommonCmd; + + printf("[LN][OTA_SEND_DEVICE_INFO]\n"); + + // 直接调用封装函数,使用默认URL(NULL表示使用默认URL) + int ret = ln_ota_send_device_info_json(NULL, NULL); + if (ret == 0) { + printf("+LN_OTA_SEND_DEVICE_INFO: OK\n"); + return ERRCODE_SUCC; + } else { + printf("+LN_OTA_SEND_DEVICE_INFO: FAIL\n"); + return ERRCODE_FAIL; + } +} + + static const at_cmd_table_t g_ln_mqtt_at_cmds[] = { {(char *)"AT^LN_MQTT_LISTEN", ln_mqtt_listen_at, (char *)"LN MQTT listen start/stop: AT^LN_MQTT_LISTEN=1/0"}, {(char *)"AT^LN_MQTT_HELLO", ln_mqtt_hello_at, (char *)"LN MQTT send hello: AT^LN_MQTT_HELLO"}, {(char *)"AT^LN_MQTT_START_SERVICE", ln_mqtt_start_service_at, (char *)"LN MQTT start service: AT^LN_MQTT_START_SERVICE"}, {(char *)"AT^LN_MQTT_STOP_SERVICE", ln_mqtt_stop_service_at, (char *)"LN MQTT stop service: AT^LN_MQTT_STOP_SERVICE"}, + {(char *)"AT^LN_OTA_SEND_DEVICE_INFO", ln_ota_send_device_info_at, (char *)"ln_ota_send_device_info_at: AT^ln_ota_send_device_info_json"}, }; at_cmd_table_t *get_ln_mqtt_at_cmd_table(uint32_t *cmd_count) diff --git a/code/sdk/zhihui/test_opus_frame_to_pcm/include/opus_frame_to_pcm.h b/code/sdk/zhihui/test_opus_frame_to_pcm/include/opus_frame_to_pcm.h index ea2c79088..2ce2b7c4f 100644 --- a/code/sdk/zhihui/test_opus_frame_to_pcm/include/opus_frame_to_pcm.h +++ b/code/sdk/zhihui/test_opus_frame_to_pcm/include/opus_frame_to_pcm.h @@ -5,6 +5,21 @@ extern "C" { #endif +/** + * @brief Initialize Opus decoder. Optional; decode_packet will auto-init if not initialized. + * + * @param sample_rate Sample rate in Hz (e.g. 16000/48000). + * @param channels Channel count (1=mono, 2=stereo). + * + * @return 0 on success, non-zero on failure. + */ +int opus_frame_to_pcm_decoder_init(int sample_rate, int channels); + +/** + * @brief Destroy Opus decoder. Call when decoder is no longer needed to free resources. + */ +void opus_frame_to_pcm_decoder_destroy(void); + /** * @brief Decode a single Opus packet (in-memory) into 16-bit PCM samples. * diff --git a/code/sdk/zhihui/test_opus_frame_to_pcm/src/opus_frame_to_pcm.c b/code/sdk/zhihui/test_opus_frame_to_pcm/src/opus_frame_to_pcm.c index bdcdf19b5..02580c919 100644 --- a/code/sdk/zhihui/test_opus_frame_to_pcm/src/opus_frame_to_pcm.c +++ b/code/sdk/zhihui/test_opus_frame_to_pcm/src/opus_frame_to_pcm.c @@ -41,13 +41,54 @@ #define MAX_PACKET_SIZE 15000 #define MAX_PCM_SAMPLES 5760 /* 120ms at 48kHz */ +static OpusDecoder *s_decoder = NULL; +static int s_decoder_sample_rate = 0; +static int s_decoder_channels = 0; + +int opus_frame_to_pcm_decoder_init(int sample_rate, int channels) +{ + int error = OPUS_OK; + + if (sample_rate <= 0 || channels <= 0) { + return 1; + } + + /* If already initialized with same params, reuse */ + if (s_decoder && s_decoder_sample_rate == sample_rate && s_decoder_channels == channels) { + return 0; + } + + /* Destroy old decoder if params changed */ + if (s_decoder) { + opus_decoder_destroy(s_decoder); + s_decoder = NULL; + } + + s_decoder = opus_decoder_create(sample_rate, channels, &error); + if (error != OPUS_OK) { + return 1; + } + + s_decoder_sample_rate = sample_rate; + s_decoder_channels = channels; + return 0; +} + +void opus_frame_to_pcm_decoder_destroy(void) +{ + if (s_decoder) { + opus_decoder_destroy(s_decoder); + s_decoder = NULL; + s_decoder_sample_rate = 0; + s_decoder_channels = 0; + } +} + int opus_frame_to_pcm_decode_packet(const unsigned char *packet_data, int packet_len, int sample_rate, int channels, short *out_pcm, int out_pcm_capacity, int *out_pcm_samples) { - OpusDecoder *decoder = NULL; - int error = OPUS_OK; int decoded_samples = 0; int total_samples = 0; @@ -76,28 +117,23 @@ int opus_frame_to_pcm_decode_packet(const unsigned char *packet_data, int packet } } - /* Create decoder */ - decoder = opus_decoder_create(sample_rate, channels, &error); - if (error != OPUS_OK) { + /* Auto-init if not initialized, or re-init if params changed */ + if (opus_frame_to_pcm_decoder_init(sample_rate, channels) != 0) { return 1; } /* Decode packet */ - decoded_samples = opus_decode(decoder, packet_data, packet_len, (opus_int16 *)out_pcm, MAX_PCM_SAMPLES, 0); + decoded_samples = opus_decode(s_decoder, packet_data, packet_len, (opus_int16 *)out_pcm, MAX_PCM_SAMPLES, 0); if (decoded_samples <= 0) { - opus_decoder_destroy(decoder); return 1; } total_samples = decoded_samples * channels; if (total_samples > out_pcm_capacity) { - opus_decoder_destroy(decoder); return 1; } *out_pcm_samples = total_samples; - /* Cleanup */ - opus_decoder_destroy(decoder); return 0; }