作者 ookk303

更新

正在显示 38 个修改的文件 包含 4800 行增加0 行删除

要显示太多修改。

为保证性能只显示 38 of 38+ 个文件。

  1 +#include <stdio.h>
  2 +#include <pthread.h>
  3 +#include <stdint.h>
  4 +#include <stdio.h>
  5 +
  6 +#include "JZsdkLib.h"
  7 +#include "../AudioDeal.h"
  8 +#include "../FF_Statement.h"
  9 +
  10 +#include "./pcm_AlsaPlay.h"
  11 +#include "../AudioDealThread/AudioDealThread.h"
  12 +#include "alsa/asoundlib.h"
  13 +
  14 +#define FRAME_SIZE 4 // 假设是双声道16位PCM,即每个帧4字节
  15 +
  16 +
  17 +struct pcm_AlsaInfo
  18 +{
  19 + int SampleRate;
  20 + snd_pcm_t* g_handle;
  21 + snd_pcm_hw_params_t* g_params;
  22 + snd_pcm_sw_params_t* g_sw_params;
  23 + snd_pcm_status_t* pcm_status;
  24 +
  25 + snd_pcm_uframes_t g_period_size; //实际处理的数数据长度
  26 + int g_gorup_size; //每次处理获取的数据长度
  27 +
  28 + int dealFlag; //处理标志位,用于避免卡死
  29 +}pcm_AlsaInfo;
  30 +
  31 +extern T_JZsdkReturnCode g_AudioDealPauseFlag;
  32 +
  33 +/**********
  34 + *
  35 + * 参数初始化
  36 + *
  37 + * *****/
  38 +static T_JZsdkReturnCode AlsaParamInit(struct pcm_AlsaInfo *AlsaInfo, int SampleRate)
  39 +{
  40 + //设置采样率
  41 + AlsaInfo->SampleRate = SampleRate;
  42 +
  43 + //音频设备模式为 0 ,也可以用SND_PCM_ASYNC模式,该模式允许以异步方式进行数据的读写操作
  44 + static int open_mode = 0;
  45 + //open_mode = open_mode | SND_PCM_ASYNC;
  46 +
  47 + //调用默认default音频设备
  48 + int ret = snd_pcm_open(&(AlsaInfo->g_handle), "default", SND_PCM_STREAM_PLAYBACK, open_mode);
  49 + if(ret < 0)
  50 + {
  51 + printf("open device failed\n");
  52 + return 0;
  53 + }
  54 +
  55 + //设置解码的周期
  56 + //g_period_size 的值太大,可能会导致写入操作失败,返回一个负数的错误码;如果 g_period_size 的值太小,可能会导致写入操作成功,但音频播放可能会出现不连续或延迟。
  57 + AlsaInfo->g_period_size = 384;
  58 + //AlsaInfo->g_period_size = 512;
  59 + AlsaInfo->g_gorup_size = AlsaInfo->g_period_size * 2 * 16 / 8; //2声道数 16位深 8字节长度 即真正的一组数据的长度
  60 +
  61 + /***************
  62 + *
  63 + * 硬件参数分配
  64 + *
  65 + * **************/
  66 +
  67 + //为硬件参数分配内存空间,并将其指针保存在 g_params 中
  68 + snd_pcm_hw_params_alloca(&AlsaInfo->g_params);
  69 +
  70 + //函数初始化硬件参数为默认值
  71 + snd_pcm_hw_params_any(AlsaInfo->g_handle, AlsaInfo->g_params);
  72 +
  73 + /* 采样位数 Signed 16-bit little-endian format */
  74 + ret = snd_pcm_hw_params_set_format(AlsaInfo->g_handle, AlsaInfo->g_params, SND_PCM_FORMAT_S16);
  75 + if (ret < 0)
  76 + {
  77 + JZSDK_LOG_ERROR("unable to set hw format: %s\n",snd_strerror(ret));
  78 + }
  79 +
  80 + /* 交错模式 Interleaved mode */
  81 + ret = snd_pcm_hw_params_set_access(AlsaInfo->g_handle, AlsaInfo->g_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  82 + if (ret < 0)
  83 + {
  84 + JZSDK_LOG_ERROR("unable to set hw access: %s\n",snd_strerror(ret));
  85 + }
  86 +
  87 + /* 通道数 */
  88 + ret = snd_pcm_hw_params_set_channels(AlsaInfo->g_handle, AlsaInfo->g_params, 2);
  89 + if (ret < 0) {
  90 + JZSDK_LOG_ERROR("unable to set hw channels: %s\n",snd_strerror(ret));
  91 + }
  92 + // ret = snd_pcm_hw_params_set_channels(AlsaInfo->g_handle, AlsaInfo->g_params, 1);
  93 + // if (ret < 0) {
  94 + // JZSDK_LOG_ERROR("unable to set hw channels: %s\n",snd_strerror(ret));
  95 + // }
  96 +
  97 + /* 采样率 44100 bits/second sampling rate (CD quality) */
  98 + ret = snd_pcm_hw_params_set_rate_near(AlsaInfo->g_handle, AlsaInfo->g_params, &AlsaInfo->SampleRate, NULL);
  99 + if (ret < 0)
  100 + {
  101 + JZSDK_LOG_ERROR("unable to set hw sampleRate: %s\n",snd_strerror(ret));
  102 + }
  103 +
  104 + //设置一个周期的多少帧
  105 + ret = snd_pcm_hw_params_set_period_size_near(AlsaInfo->g_handle, AlsaInfo->g_params, &AlsaInfo->g_period_size, NULL);
  106 + if (ret < 0) {
  107 + JZSDK_LOG_ERROR("unable to set hw period_size: %s\n",snd_strerror(ret));
  108 + }
  109 +
  110 + /* 将设置好的参数写入驱动 */
  111 + ret = snd_pcm_hw_params(AlsaInfo->g_handle, AlsaInfo->g_params);
  112 + if (ret < 0) {
  113 + JZSDK_LOG_ERROR("unable to set hw parameters: %s\n",snd_strerror(ret));
  114 + return 0;
  115 + }
  116 +
  117 +/***************
  118 + *
  119 + * 软件参数分配
  120 + *
  121 + * **************/
  122 + snd_pcm_sw_params_alloca(&AlsaInfo->g_sw_params);
  123 + snd_pcm_uframes_t start_t = 1;
  124 + snd_pcm_uframes_t stop_t = 65536 * 2;
  125 + ret = snd_pcm_sw_params_current(AlsaInfo->g_handle, AlsaInfo->g_sw_params);
  126 + if (ret < 0) {
  127 + JZSDK_LOG_ERROR("unable to get current sw params: %s\n",snd_strerror(ret));
  128 + }
  129 + ret = snd_pcm_sw_params_set_start_threshold(AlsaInfo->g_handle, AlsaInfo->g_sw_params, start_t);
  130 + if (ret < 0) {
  131 + JZSDK_LOG_ERROR("unable to set sw start_threshold: %s\n",snd_strerror(ret));
  132 + }
  133 + ret = snd_pcm_sw_params_set_stop_threshold(AlsaInfo->g_handle, AlsaInfo->g_sw_params, stop_t);
  134 + if (ret < 0) {
  135 + JZSDK_LOG_ERROR("unable to set sw stop_threshold: %s\n",snd_strerror(ret));
  136 + }
  137 + ret = snd_pcm_sw_params(AlsaInfo->g_handle, AlsaInfo->g_sw_params);/* 将设置好的参数写入驱动 */
  138 + if (ret < 0) {
  139 + JZSDK_LOG_ERROR("unable to set sw parameters: %s\n",snd_strerror(ret));
  140 + return 0;
  141 + }
  142 +
  143 + //分配音频设备的状态结构体内存
  144 + snd_pcm_status_malloc(&AlsaInfo->pcm_status);
  145 +
  146 + printf("播放参数初始化完毕\n");
  147 +
  148 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  149 +}
  150 +
  151 +/**********
  152 + *
  153 + * 播放等待参数
  154 + *
  155 + * *****/
  156 +static T_JZsdkReturnCode AlsaWaitPlayEnd(struct pcm_AlsaInfo *AlsaInfo)
  157 +{
  158 + while(1){
  159 + snd_pcm_sframes_t delay_samples = 0;
  160 + snd_pcm_delay(AlsaInfo->g_handle, &delay_samples);
  161 + //未播放的组小于15个就可以返还了
  162 + if(delay_samples <= (AlsaInfo->g_period_size * 15))
  163 + {
  164 + break;
  165 + }
  166 + delayUs(100);
  167 + }
  168 +
  169 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  170 +}
  171 +
  172 +/**********
  173 + *
  174 + * pcm播放线程
  175 + *
  176 + * *****/
  177 +static void *Alsa_CheckThread(void *arg)
  178 +{
  179 + struct AudioDealInfo *IndexInfo = (struct AudioDealInfo *) arg;
  180 + struct pcm_AlsaInfo *AlsaInfo = (struct pcm_AlsaInfo *)IndexInfo->AlsaInfo;
  181 + int realLen = 0;//实际数据读取长度
  182 +
  183 + int LastStatus = JZ_FLAGCODE_OFF;
  184 +
  185 + //每10ms检测一次
  186 + while (1)
  187 + {
  188 + snd_pcm_sframes_t delay_samples = 0;
  189 + snd_pcm_delay(AlsaInfo->g_handle, &delay_samples);
  190 + //JZSDK_LOG_INFO("当前alsa存放了%d组",delay_samples);
  191 + if ( (delay_samples > 0) && (LastStatus == JZ_FLAGCODE_OFF))
  192 + {
  193 + //设置alsa状态
  194 + Set_AudioDeal_Alsa_Flag(IndexInfo , JZ_FLAGCODE_ON);
  195 + LastStatus = JZ_FLAGCODE_ON;
  196 + }
  197 + else if ( ( delay_samples == 0 ) && (LastStatus == JZ_FLAGCODE_ON))
  198 + {
  199 + //设置alsa状态
  200 + Set_AudioDeal_Alsa_Flag(IndexInfo , JZ_FLAGCODE_OFF);
  201 + LastStatus = JZ_FLAGCODE_OFF;
  202 + }
  203 +
  204 + delayMs(10);
  205 + }
  206 +}
  207 +
  208 +/**********
  209 + *
  210 + * alsa play反初始化
  211 + *
  212 + * *****/
  213 +// T_JZsdkReturnCode Pcm_Play_deInit(struct pcm_AlsaInfo *AlsaInfo)
  214 +// {
  215 +
  216 +// //printf("snd_pcm_hw_params_free\n");
  217 +// //snd_pcm_hw_params_free(g_params);
  218 +// printf("snd_pcm_drain\n");
  219 +// snd_pcm_drain(AlsaInfo->g_handle);
  220 +// printf("snd_pcm_close\n");
  221 +// snd_pcm_close(AlsaInfo->g_handle);
  222 +// }
  223 +
  224 +
  225 +/**********
  226 + *
  227 + * alsa play初始化
  228 + *
  229 + * *****/
  230 +T_JZsdkReturnCode AlsaPlay_Init(struct AudioDealInfo *IndexInfo)
  231 +{
  232 + // 尝试获取或分配ResampleInfo
  233 + struct pcm_AlsaInfo **AlsaInfoPtr = (struct pcm_AlsaInfo **)&IndexInfo->AlsaInfo;
  234 + if (*AlsaInfoPtr == NULL) {
  235 + // 分配内存
  236 + *AlsaInfoPtr = (struct pcm_AlsaInfo *)malloc(sizeof(struct pcm_AlsaInfo));
  237 + if (*AlsaInfoPtr == NULL) {
  238 + // 内存分配失败处理
  239 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 使用更具体的错误码
  240 + }
  241 + }
  242 + else
  243 + {
  244 + JZsdk_Free(*AlsaInfoPtr);
  245 + // 分配内存
  246 + *AlsaInfoPtr = (struct pcm_AlsaInfo *)malloc(sizeof(struct pcm_AlsaInfo));
  247 + if (*AlsaInfoPtr == NULL) {
  248 + // 内存分配失败处理
  249 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 使用更具体的错误码
  250 + }
  251 + }
  252 +
  253 + struct pcm_AlsaInfo *AlsaInfo = *AlsaInfoPtr;
  254 +
  255 + //设置alsa参数
  256 + AlsaParamInit(AlsaInfo, IndexInfo->Target_SampleRate);
  257 +
  258 + //创建pcm检测线程
  259 + pthread_t alsa_check_task;
  260 + pthread_attr_t task_attribute; //线程属性
  261 + pthread_attr_init(&task_attribute); //初始化线程属性
  262 + pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
  263 +
  264 + int tts_ret = pthread_create(&alsa_check_task,&task_attribute,Alsa_CheckThread,(void *)IndexInfo); //TTS线程
  265 + if(tts_ret != 0)
  266 + {
  267 + JZSDK_LOG_ERROR("创建pcm检测线程失败!\n");
  268 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  269 + }
  270 +
  271 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  272 +}
  273 +
  274 +
  275 +T_JZsdkReturnCode Pcm_AlsaPlay(struct AudioDealInfo *IndexInfo, unsigned char *buf, int num_samples)
  276 +{
  277 + struct pcm_AlsaInfo *AlsaInfo = (struct pcm_AlsaInfo *)IndexInfo->AlsaInfo;
  278 +
  279 + //打开alsa播放标志
  280 + IndexInfo->AudioDeal_Alsa_Execute_Flag = JZ_FLAGCODE_ON;
  281 +
  282 + int ret = 0;
  283 + //printf("num_samples: %d, g_period_size: %d\n", num_samples, (int)AlsaInfo->g_period_size);
  284 + //当输入的数据长度 》 每次写入音频设备时处理的帧数量384
  285 + int UnDeal_samples = num_samples;
  286 +
  287 + while(UnDeal_samples > 0 && IndexInfo->AudioDeal_Alsa_Execute_Flag != JZ_FLAGCODE_OFF)
  288 + {
  289 +
  290 + int dealSamples = 0;
  291 + if (UnDeal_samples >= AlsaInfo->g_period_size)
  292 + {
  293 + dealSamples = AlsaInfo->g_period_size;
  294 + UnDeal_samples = UnDeal_samples - dealSamples;
  295 + }
  296 + else
  297 + {
  298 + dealSamples = UnDeal_samples;
  299 + UnDeal_samples = UnDeal_samples - dealSamples;
  300 + }
  301 +
  302 + //将数据写入alsa音频设备
  303 + ret = snd_pcm_writei(AlsaInfo->g_handle, (void*)buf, dealSamples);
  304 +
  305 + //当前设备不可用
  306 + if(ret == -EAGAIN)
  307 + {
  308 + //等待一段时间后重试
  309 + printf("again");
  310 + snd_pcm_wait(AlsaInfo->g_handle, 1000);
  311 + }
  312 +
  313 + //音频设备溢出错误
  314 + else if(ret == -EPIPE)
  315 + {
  316 + printf("snd_pcm_prepare 1\n");
  317 + //重置音频设备
  318 + snd_pcm_prepare(AlsaInfo->g_handle);
  319 + }
  320 +
  321 + //音频设备需要挂起 忙时
  322 + else if(ret == -ESTRPIPE)
  323 + {
  324 + printf("Need suspend!\n");
  325 + }
  326 +
  327 + //出现其他错误
  328 + else if(ret < 0)
  329 + {
  330 + printf("snd_pcm_writei error:%s\n", snd_strerror(ret));
  331 + }
  332 +
  333 + //位移数据
  334 + //buf + 解码的帧数*单帧长度 //单帧长度 2个16bit长度的数据 即2*16/8 = 4字节
  335 + buf += dealSamples * FRAME_SIZE;
  336 + }
  337 +
  338 + //等待播放完毕
  339 + AlsaWaitPlayEnd(AlsaInfo);
  340 +
  341 + //暂停播放
  342 + while(g_AudioDealPauseFlag)
  343 + {
  344 + delayMs(1);
  345 + }
  346 +
  347 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  348 +}
  349 +
  350 +/**********
  351 + *
  352 + * 清空pcm数据
  353 + *
  354 + *
  355 + * ***********/
  356 +T_JZsdkReturnCode Alsa_DropPcm(struct AudioDealInfo *IndexInfo)
  357 +{
  358 + struct pcm_AlsaInfo *AlsaInfo = (struct pcm_AlsaInfo *)IndexInfo->AlsaInfo;
  359 + snd_pcm_drop(AlsaInfo->g_handle);
  360 + snd_pcm_prepare(AlsaInfo->g_handle);
  361 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  362 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file pcm_AlsaPlay.h
  4 + * pcm_AlsaPlay的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef PCM_ALSAPLAY_H
  11 +#define PCM_ALSAPLAY_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "../AudioDeal.h"
  16 +
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +
  21 +/* Exported constants --------------------------------------------------------*/
  22 +/* 常亮定义*/
  23 +
  24 +/* Exported types ------------------------------------------------------------*/
  25 +
  26 +
  27 +/* Exported functions --------------------------------------------------------*/
  28 +T_JZsdkReturnCode AlsaPlay_Init(struct AudioDealInfo *IndexInfo);
  29 +T_JZsdkReturnCode Pcm_AlsaPlay(struct AudioDealInfo *IndexInfo, unsigned char *buf, int num_samples);
  30 +T_JZsdkReturnCode Alsa_DropPcm(struct AudioDealInfo *IndexInfo);
  31 +
  32 +#ifdef __cplusplus
  33 +}
  34 +#endif
  35 +
  36 +#endif
  1 +#include <stdio.h>
  2 +#include <pthread.h>
  3 +#include <alsa/asoundlib.h>
  4 +#include <stdlib.h>
  5 +#include <string.h>
  6 +#include <unistd.h>
  7 +
  8 +#include "FF_Statement.h"
  9 +#include "./AudioDeal.h"
  10 +#include "JZsdkLib.h"
  11 +
  12 +
  13 +
  14 +#include "./AudioDealThread/AudioDealThread.h"
  15 +#include "./Alsa/pcm_AlsaPlay.h"
  16 +
  17 +#include "Resample/pcm_Resample.h"
  18 +#include "AudioStreamDeal/AudioStreamDeal.h"
  19 +#include "Filter/FF_Filter.h"
  20 +
  21 +#define MP3_DIR "/root/sdcard/1.mp3"
  22 +#define PCM_16000_DIR "/root/sdcard/16000_test.pcm"
  23 +#define READ_SIZE (2000)
  24 +
  25 +//音频库索引值,用于管理所有的音频库信息
  26 +struct AudioDealInfo *AudioDeakInfo_index = NULL;
  27 +T_JZsdkReturnCode g_AudioDealPauseFlag = JZ_FLAGCODE_OFF;
  28 +
  29 +T_JZsdkReturnCode AudioDeal_Init()
  30 +{
  31 + T_JZsdkReturnCode ret;
  32 +
  33 + ret = JZsdk_Malloc((void **)&AudioDeakInfo_index, sizeof(struct AudioDealInfo));
  34 +
  35 + //初始化参数
  36 + AudioDeakInfo_index->AudioDeal_ResampleAndFilter_Execute_Flag = JZ_FLAGCODE_OFF;
  37 + AudioDeakInfo_index->AudioDeal_ResampleAndFilterAndPlay_Finish_Flag = JZ_FLAGCODE_OFF;
  38 +
  39 + AudioDeakInfo_index->Target_SampleRate = 44100; //cd采样率
  40 + AudioDeakInfo_index->Target_SampleFormat = AV_SAMPLE_FMT_S16; //16位深
  41 +
  42 + av_channel_layout_copy(&(AudioDeakInfo_index->Target_ChannelLayout), &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO);
  43 + //AudioDeakInfo_index->Target_ChannelLayout = AV_CH_LAYOUT_STEREO; //立体声道
  44 + //AudioDeakInfo_index->Target_ChannelLayout = AV_CH_LAYOUT_MONO; //dan声道
  45 +
  46 + AudioDeakInfo_index->Raw_SampleRate = 0;
  47 +
  48 + AudioDeakInfo_index->FilterMode = JZ_FLAGCODE_ON; //滤波模式
  49 +
  50 + //JZSDK_LOG_DEBUG("thread_init开始");
  51 +
  52 + //初始化thread线程管理
  53 + AudioDealThread_Init(AudioDeakInfo_index);
  54 +
  55 + AudioDeakInfo_index->AlsaInfo = NULL;
  56 + AlsaPlay_Init(AudioDeakInfo_index);
  57 +
  58 + JZSDK_LOG_DEBUG("初始化重采样器开始");
  59 +
  60 + //初始化重采样器, 以下为默认文本的参数
  61 + AudioDeakInfo_index->ResampleInfo = NULL;
  62 +
  63 + FF_Resample_Init(AudioDeakInfo_index, 16000, (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO, AV_SAMPLE_FMT_S16);
  64 +
  65 + JZSDK_LOG_DEBUG("初始化滤波器开始");
  66 +
  67 + //初始化滤波器
  68 + AudioDeakInfo_index->FilterInfo = NULL;
  69 + FF_Filter_Init(AudioDeakInfo_index, 0x00);
  70 +
  71 + JZSDK_LOG_INFO("MODULE_AUDIODEL_INIT_COMPLETE");
  72 +}
  73 +
  74 +T_JZsdkReturnCode AudioDeal_FilterReset(int mode)
  75 +{
  76 + FF_Filter_Init(AudioDeakInfo_index, mode);
  77 +}
  78 +
  79 +
  80 +/******************************
  81 + *
  82 + * 采样器重初始化
  83 + *
  84 + *
  85 + * *********************************************/
  86 +T_JZsdkReturnCode AudioDeal_ResampleRest(unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format)
  87 +{
  88 + FF_Resample_Reset(AudioDeakInfo_index ,in_sampleRate, in_ch_layout, in_format);
  89 +}
  90 +
  91 +/***************************************************************************************************************************************************
  92 + *
  93 + * 各音频播放接口
  94 + *
  95 + *
  96 + * ***************************************************************************************************************************************************/
  97 +
  98 +/***************************
  99 + *
  100 + * pcm数据接入接口
  101 + *
  102 + * ************/
  103 +T_JZsdkReturnCode AudioDeal_PcmDataInput(int In_Bitrate, unsigned char *buffer, int bytesRead)
  104 +{
  105 + if (AudioDeakInfo_index == NULL)
  106 + {
  107 + JZSDK_LOG_ERROR("音频处理器未注册");
  108 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  109 + }
  110 +
  111 + //1、打开音频库的处理标志位,该标志可以重复打开,只有强制关闭音频时,需要关闭该标志
  112 + AudioDeakInfo_index->AudioDeal_ResampleAndFilter_Execute_Flag = JZ_FLAGCODE_ON;
  113 +
  114 + //Set_AudioDeal_ResampleAndFilterAndPlay_Flag(AudioDeakInfo_index, JZ_FLAGCODE_ON);
  115 +
  116 + //默认数据pcm接入口都是 单声道,16位
  117 + PCM_PooL_Interface_PcmData(AudioDeakInfo_index, In_Bitrate, (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO, AV_SAMPLE_FMT_S16, buffer, bytesRead);
  118 + //printf("Read %zu bytes from the PCM file.\n", bytesRead);
  119 +
  120 + //标志音频库已经结束,不过alsa库内有10组缓存,该标志位结束,只能认为重采样和滤波结束
  121 + //Set_AudioDeal_ResampleAndFilterAndPlay_Flag(AudioDeakInfo_index, JZ_FLAGCODE_OFF);
  122 +
  123 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  124 +}
  125 +
  126 +/***************************
  127 + *
  128 + * 无回复接口
  129 + *
  130 + * ************/
  131 +T_JZsdkReturnCode AudioDeal_PcmDataInput_WithoutReply(int In_Bitrate, unsigned char *buffer, int bytesRead)
  132 +{
  133 + if (AudioDeakInfo_index == NULL)
  134 + {
  135 + JZSDK_LOG_ERROR("音频处理器未注册");
  136 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  137 + }
  138 +
  139 + //1、打开音频库的处理标志位,该标志可以重复打开,只有强制关闭音频时,需要关闭该标志
  140 + AudioDeakInfo_index->AudioDeal_ResampleAndFilter_Execute_Flag = JZ_FLAGCODE_ON;
  141 +
  142 + //默认数据pcm接入口都是 单声道,16位
  143 + PCM_PooL_Interface_PcmData(AudioDeakInfo_index, In_Bitrate, (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO, AV_SAMPLE_FMT_S16, buffer, bytesRead);
  144 +
  145 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  146 +}
  147 +
  148 +
  149 +
  150 +/***************************
  151 + *
  152 + * mp3数据接入接口
  153 + *
  154 + * ************/
  155 +T_JZsdkReturnCode AudioDeal_Mp3DataInput(int In_Bitrate, unsigned char *buffer, int bytesRead)
  156 +{
  157 + if (AudioDeakInfo_index == NULL)
  158 + {
  159 + JZSDK_LOG_ERROR("音频处理器未注册");
  160 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  161 + }
  162 +
  163 + //1、打开音频库的处理标志位,该标志可以重复打开,只有强制关闭音频时,需要关闭该标志
  164 + AudioDeakInfo_index->AudioDeal_ResampleAndFilter_Execute_Flag = JZ_FLAGCODE_ON;
  165 +
  166 + mp3_Stream_Interface_Mp3Data(AudioDeakInfo_index, In_Bitrate, buffer, bytesRead);
  167 +
  168 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  169 +}
  170 +
  171 +/*******************
  172 + *
  173 + * 暂停播放
  174 + * jzon 为暂停播放
  175 + * jzoff 为继续播放
  176 + * *********************/
  177 +T_JZsdkReturnCode AudioDeal_PauseAndContinuePlay(int status)
  178 +{
  179 + //如果处于实时喊话,禁止使用该功能
  180 + g_AudioDealPauseFlag = status;
  181 +}
  182 +
  183 +/***************************
  184 + *
  185 + * 音频文件数据接入接口
  186 + *
  187 + * ************/
  188 +T_JZsdkReturnCode AudioFile_Stream_DataInput(AVFrame *frame)
  189 +{
  190 + if (AudioDeakInfo_index == NULL)
  191 + {
  192 + JZSDK_LOG_ERROR("音频处理器未注册");
  193 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  194 + }
  195 +
  196 + //1、打开音频库的处理标志位,该标志可以重复打开,只有强制关闭音频时,需要关闭该标志
  197 + AudioDeakInfo_index->AudioDeal_ResampleAndFilter_Execute_Flag = JZ_FLAGCODE_ON;
  198 +
  199 + AudioFile_Stream_Interface_PcmData(AudioDeakInfo_index, frame);
  200 +
  201 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  202 +}
  203 +
  204 +/*********************
  205 + *
  206 + * 音频库停止处理函数
  207 + *
  208 + * *********************/
  209 +T_JZsdkReturnCode AudioDeal_StopDeal()
  210 +{
  211 + if (AudioDeakInfo_index == NULL)
  212 + {
  213 + JZSDK_LOG_ERROR("音频处理器未注册");
  214 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  215 + }
  216 +
  217 + //1、关闭数据处理和alsa部分
  218 + AudioDeakInfo_index->AudioDeal_ResampleAndFilter_Execute_Flag = JZ_FLAGCODE_OFF;
  219 + AudioDeakInfo_index->AudioDeal_Alsa_Execute_Flag = JZ_FLAGCODE_OFF;
  220 +
  221 + //2、等候完成
  222 + while (AudioDeakInfo_index->AudioDeal_ResampleAndFilterAndPlay_Finish_Flag != JZ_FLAGCODE_OFF)
  223 + {
  224 + delayMs(1);
  225 + }
  226 +
  227 + //清空alsa里的缓冲区
  228 + Alsa_DropPcm(AudioDeakInfo_index);
  229 +
  230 + while (AudioDeakInfo_index->AudioDeal_Alsa_Finish_Flag != JZ_FLAGCODE_OFF)
  231 + {
  232 + delayMs(1);
  233 + }
  234 +
  235 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  236 +}
  237 +
  238 +/**************
  239 + *
  240 + * 获取音频库的处理状态
  241 + *
  242 + * ***********/
  243 +T_JZsdkReturnCode Get_AudioDealStatus()
  244 +{
  245 + return Get_AudioDealThreadStatus();
  246 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file AudioDeal.h
  4 + * AudioDeal的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef AUDIO_DEAL_H
  11 +#define AUDIO_DEAL_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "libavutil/channel_layout.h"
  16 +#include "libavutil/frame.h"
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +
  21 +/* Exported constants --------------------------------------------------------*/
  22 +/* 常亮定义*/
  23 +
  24 +/* Exported types ------------------------------------------------------------*/
  25 +/*******
  26 + *
  27 + * 音频库信息结构体
  28 + *
  29 + * ********/
  30 +struct AudioDealInfo
  31 +{
  32 +
  33 + int Flag_AudioPlayStatus; //播放状态线程 用于标识当前的播放状态
  34 + int Flag_AudioSwicthStatus; //音频切换状态标志位 用于是否是播放中切换播放中
  35 + int Flag_AudioDataGeneration; //音频数据产生的标志位 用于判断是否是
  36 + int Flag_AlsaPlay; //alsa库的播放标志位,用于标志alsa库是否处于播放中
  37 +
  38 + int Flag_AudioDataGenerationImplement; //数据产生线程执行中
  39 + int Flag_AudioDataGenerationFinsh; //数据产生线程执行完毕
  40 +
  41 + int AudioDeal_ResampleAndFilter_Execute_Flag; //音频库的滤波及重采样的执行标志位,用于进行管理音频库的线程使用
  42 + int AudioDeal_ResampleAndFilterAndPlay_Finish_Flag; //音频库的滤波及重采样的执行完成的函数
  43 +
  44 + //用于管理alsa部分的内容
  45 + int AudioDeal_Alsa_Execute_Flag;
  46 + int AudioDeal_Alsa_Finish_Flag;
  47 +
  48 + int FilterMode; //滤波模式
  49 +
  50 +//输出的pcm部分
  51 + void *PcmPoolIndex;
  52 +
  53 + unsigned int Target_SampleRate; //目标采样率
  54 + unsigned int Target_SampleFormat; //目标采样深度
  55 + //unsigned int Target_ChannelLayout; //目标频道数
  56 + AVChannelLayout Target_ChannelLayout; //目标通道布局
  57 +
  58 +//alsa部分
  59 + void *AlsaInfo;
  60 +
  61 +//重采样
  62 + void *ResampleInfo;
  63 +
  64 +//滤波
  65 + void *FilterInfo;
  66 +
  67 +//原始数据信息
  68 + unsigned int Raw_SampleRate;
  69 + unsigned int Raw_SampleFormat; //原始数据信息采样深度
  70 + //unsigned int Raw_ChannelLayout; //原始数据信息频道数
  71 + AVChannelLayout Raw_ChannelLayout; //原始数据通道布局
  72 +
  73 +}AudioDealInfo;
  74 +
  75 +/*******
  76 + *
  77 + * 音频库播放类型枚举
  78 + *
  79 + * ********/
  80 +enum AudioDealPlayType
  81 +{
  82 + AUDIO_TYPE_IDLE = 0x00, //空闲状态
  83 + AUDIO_TYPE_FILE = 0x01, //文件播放状态
  84 + AUDIO_TYPE_MP3_STREAM = 0x02, //mp3流播放状态
  85 + AUDIO_TYPE_PCM_STREAM = 0x03, //pcm流播放状态
  86 +
  87 +}AudioDealPlayType;
  88 +
  89 +
  90 +/* Exported functions --------------------------------------------------------*/
  91 +T_JZsdkReturnCode AudioDeal_Init();
  92 +T_JZsdkReturnCode AudioDeal_PcmDataInput(int In_Bitrate, unsigned char *buffer, int bytesRead);
  93 +T_JZsdkReturnCode AudioDeal_StopDeal();
  94 +
  95 +T_JZsdkReturnCode AudioDeal_FilePlayInput(unsigned char *FilePath);
  96 +T_JZsdkReturnCode AudioDeal_ResampleRest(unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format);
  97 +T_JZsdkReturnCode AudioFile_Stream_DataInput(AVFrame *frame);
  98 +T_JZsdkReturnCode AudioDeal_Mp3DataInput(int In_Bitrate, unsigned char *buffer, int bytesRead);
  99 +T_JZsdkReturnCode AudioDeal_PauseAndContinuePlay(int status);
  100 +T_JZsdkReturnCode Get_AudioDealStatus();
  101 +T_JZsdkReturnCode AudioDeal_FilterReset(int mode);
  102 +
  103 +#ifdef __cplusplus
  104 +}
  105 +#endif
  106 +
  107 +#endif
  1 +#include <stdio.h>
  2 +#include <pthread.h>
  3 +#include <stdlib.h>
  4 +#include <string.h>
  5 +#include <unistd.h>
  6 +
  7 +
  8 +#include "JZsdkLib.h"
  9 +#include "../AudioDeal.h"
  10 +#include "Megaphone/Megaphone.h"
  11 +#include "version_choose.h"
  12 +
  13 +static pthread_mutex_t LibStatusPthread_mutex = PTHREAD_MUTEX_INITIALIZER;
  14 +static pthread_cond_t LibStatusPthread_cond = PTHREAD_COND_INITIALIZER;
  15 +
  16 +static pthread_mutex_t PlayStatus_mutex = PTHREAD_MUTEX_INITIALIZER;
  17 +
  18 +static int AudioDealThreadStatus = JZ_FLAGCODE_OFF;
  19 +
  20 +/**************
  21 + *
  22 + * 设置音频库的处理状态
  23 + *
  24 + * ***********/
  25 +static T_JZsdkReturnCode Set_AudioThreadStatus(int status)
  26 +{
  27 + AudioDealThreadStatus = status;
  28 +}
  29 +
  30 +/**************
  31 + *
  32 + * 获取音频库的处理状态
  33 + *
  34 + * ***********/
  35 +T_JZsdkReturnCode Get_AudioDealThreadStatus()
  36 +{
  37 + return AudioDealThreadStatus;
  38 +}
  39 +
  40 +
  41 +/**************
  42 + *
  43 + * 音频库重采样与滤波标志位
  44 + *
  45 + * ***********/
  46 +T_JZsdkReturnCode Set_AudioDeal_ResampleAndFilterAndPlay_Flag(struct AudioDealInfo *IndexInfo ,int value)
  47 +{
  48 + pthread_mutex_lock(&LibStatusPthread_mutex);
  49 +
  50 + //状态无变化
  51 + if (IndexInfo->AudioDeal_ResampleAndFilterAndPlay_Finish_Flag == value)
  52 + {
  53 + //直接退出
  54 + pthread_mutex_unlock(&LibStatusPthread_mutex);
  55 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  56 + }
  57 +
  58 + //1、修改结构体的值
  59 + IndexInfo->AudioDeal_ResampleAndFilterAndPlay_Finish_Flag = value;
  60 +
  61 + //2、通知播放状态管理线程
  62 + pthread_cond_signal(&LibStatusPthread_cond);
  63 +
  64 + pthread_mutex_unlock(&LibStatusPthread_mutex);
  65 +
  66 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  67 +}
  68 +
  69 +/**************
  70 + *
  71 + * 音频库alsa标志位
  72 + *
  73 + * ***********/
  74 +T_JZsdkReturnCode Set_AudioDeal_Alsa_Flag(struct AudioDealInfo *IndexInfo ,int value)
  75 +{
  76 + pthread_mutex_lock(&LibStatusPthread_mutex);
  77 +
  78 + //状态无变化
  79 + if (IndexInfo->AudioDeal_Alsa_Finish_Flag == value)
  80 + {
  81 + //直接退出
  82 + pthread_mutex_unlock(&LibStatusPthread_mutex);
  83 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  84 + }
  85 +
  86 + //1、修改结构体的值
  87 + IndexInfo->AudioDeal_Alsa_Finish_Flag = value;
  88 +
  89 + //2、通知播放状态管理线程
  90 + pthread_cond_signal(&LibStatusPthread_cond);
  91 +
  92 + pthread_mutex_unlock(&LibStatusPthread_mutex);
  93 +
  94 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  95 +}
  96 +
  97 +/************************
  98 + *
  99 + * 播放状态线程初始化
  100 + *
  101 + * *******************/
  102 +static void *AudioDeal_LibStatusThread(void *arg)
  103 +{
  104 + struct AudioDealInfo *IndexInfo = (struct AudioDealInfo *) arg;
  105 +
  106 + while (1)
  107 + {
  108 + pthread_mutex_lock(&LibStatusPthread_mutex);
  109 +
  110 + //等候解锁线程
  111 + pthread_cond_wait(&LibStatusPthread_cond, &LibStatusPthread_mutex);
  112 +
  113 + if (IndexInfo->AudioDeal_Alsa_Finish_Flag == JZ_FLAGCODE_OFF
  114 + && IndexInfo->AudioDeal_ResampleAndFilterAndPlay_Finish_Flag == JZ_FLAGCODE_OFF)
  115 + {
  116 + //广播音频库的处理部分处于空闲状态
  117 + Set_AudioThreadStatus(JZ_FLAGCODE_OFF);
  118 + //printf("音频库状态变化off");
  119 + }
  120 + else
  121 + {
  122 + Set_AudioThreadStatus(JZ_FLAGCODE_ON);
  123 + //printf("音频库状态变化on");
  124 + }
  125 +
  126 + pthread_mutex_unlock(&LibStatusPthread_mutex);
  127 + }
  128 +}
  129 +
  130 +static T_JZsdkReturnCode AudioDeal_LibStatus_Init(struct AudioDealInfo *IndexInfo)
  131 +{
  132 + pthread_t alsa_play_task;
  133 + pthread_attr_t task_attribute; //线程属性
  134 + pthread_attr_init(&task_attribute); //初始化线程属性
  135 + pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
  136 +
  137 + int tts_ret = pthread_create(&alsa_play_task,&task_attribute,AudioDeal_LibStatusThread,(void *)IndexInfo); //TTS线程
  138 + if(tts_ret != 0)
  139 + {
  140 + JZSDK_LOG_ERROR("创建播放线程失败!\n");
  141 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  142 + }
  143 +}
  144 +
  145 +/************************
  146 + *
  147 + * 三大线程的管理文件
  148 + * 分别是 数据生成线程 播放线程 播放状态线程
  149 + *
  150 + * *******************/
  151 +T_JZsdkReturnCode AudioDealThread_Init(struct AudioDealInfo *IndexInfo)
  152 +{
  153 + pthread_cond_init(&LibStatusPthread_cond, NULL);
  154 + pthread_mutex_init(&LibStatusPthread_mutex, NULL);
  155 + pthread_mutex_init(&PlayStatus_mutex, NULL);
  156 +
  157 + AudioDeal_LibStatus_Init(IndexInfo);
  158 +
  159 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  160 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file AudioDealThread.h
  4 + * AudioDealThread
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef AUDIO_DEAL_THREAD_H
  11 +#define AUDIO_DEAL_THREAD_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "../AudioDeal.h"
  16 +
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +
  21 +T_JZsdkReturnCode Pcm_Data_Pool_Init(struct AudioDealInfo *IndexInfo);
  22 +T_JZsdkReturnCode Set_AudioPlayStatus(struct AudioDealInfo *IndexInfo ,enum AudioDealPlayType status, int PlayStatus);
  23 +T_JZsdkReturnCode Set_AudioSwitchStatus(struct AudioDealInfo *IndexInfo ,int value);
  24 +T_JZsdkReturnCode Set_AudioDataGeneration(struct AudioDealInfo *IndexInfo ,int value);
  25 +//T_JZsdkReturnCode Set_AudioDataClear(struct AudioDealInfo *IndexInfo ,int value);
  26 +T_JZsdkReturnCode Set_AlsaPlay(struct AudioDealInfo *IndexInfo ,int value);
  27 +T_JZsdkReturnCode AudioDealThread_Init(struct AudioDealInfo *IndexInfo);
  28 +
  29 +
  30 +T_JZsdkReturnCode Set_AudioDeal_ResampleAndFilterAndPlay_Flag(struct AudioDealInfo *IndexInfo ,int value);
  31 +T_JZsdkReturnCode Set_AudioDeal_Alsa_Flag(struct AudioDealInfo *IndexInfo ,int value);
  32 +T_JZsdkReturnCode Get_AudioDealThreadStatus();
  33 +
  34 +/* Exported constants --------------------------------------------------------*/
  35 +/* 常亮定义*/
  36 +
  37 +/* Exported types ------------------------------------------------------------*/
  38 +
  39 +/* Exported functions --------------------------------------------------------*/
  40 +
  41 +#ifdef __cplusplus
  42 +}
  43 +#endif
  44 +
  45 +#endif
  1 +#include "libavutil/opt.h"
  2 +#include "libavutil/channel_layout.h"
  3 +#include "libavutil/samplefmt.h"
  4 +#include "libavformat/avformat.h"
  5 +#include "libavfilter/avfilter.h"
  6 +#include "libavfilter/buffersink.h"
  7 +#include "libavfilter/buffersrc.h"
  8 +#include "libavcodec/avcodec.h"
  9 +
  10 +#include "AudioDeal/FF_Statement.h"
  11 +
  12 +#include <stdio.h>
  13 +#include "JZsdkLib.h"
  14 +#include "../AudioDeal.h"
  15 +#include "../Resample/pcm_Resample.h"
  16 +#include "../Filter/FF_Filter.h"
  17 +#include "../Alsa/pcm_AlsaPlay.h"
  18 +#include "./AudioStreamDeal.h"
  19 +
  20 +//音频文件直接播放
  21 +T_JZsdkReturnCode AudioFile_StartPlay_Interface(struct AudioDealInfo *AD_Info, unsigned char* FilePath)
  22 +{
  23 + T_JZsdkReturnCode jzret;
  24 +
  25 + //1、检测这个文件是否存在
  26 + jzret = JZsdk_check_file_exists((const char *)FilePath);
  27 + if (jzret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  28 + {
  29 + JZSDK_LOG_INFO("播放文件不存在");
  30 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  31 + }
  32 +
  33 + //2、创建音频解码器
  34 + const AVCodec *codec;
  35 + AVCodecContext *codec_ctx = NULL;
  36 +
  37 + double total_duration = 0; // 累计播放时间(秒)
  38 +
  39 + //3、创建一个音频上下文
  40 + AVFormatContext *fmt_ctx;
  41 +
  42 + //4、打开输出的音频文件
  43 + int ret = avformat_open_input(&fmt_ctx, FilePath, NULL, NULL);
  44 + if (ret < 0) {
  45 + JZSDK_LOG_ERROR("歌曲播放文件打开失败");
  46 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  47 + }
  48 +
  49 + //5、检索音频的流信息
  50 + if (avformat_find_stream_info(fmt_ctx, NULL) < 0)
  51 + {
  52 + JZSDK_LOG_ERROR("检索音频的流信息失败");
  53 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  54 + }
  55 +
  56 + //6、找到音频流信息
  57 + ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
  58 + if (ret < 0) {
  59 + JZSDK_LOG_ERROR("无法从文件中找到音频流信息 %d",ret);
  60 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  61 + }
  62 + int audio_stream_index = ret;
  63 +
  64 + // //7、查找解码器
  65 + // codec = avcodec_find_decoder(fmt_ctx->streams[audio_stream_index]->codecpar->codec_id);
  66 + // if (!codec)
  67 + // {
  68 + // JZSDK_LOG_ERROR("无法找到解码器");
  69 + // return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  70 + // }
  71 +
  72 + //8、分配解码器上下文
  73 + codec_ctx = avcodec_alloc_context3(codec);
  74 + if (!codec_ctx) {
  75 + JZSDK_LOG_ERROR("Could not allocate audio codec context");
  76 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  77 + }
  78 + if (avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[audio_stream_index]->codecpar) < 0)
  79 + {
  80 + JZSDK_LOG_ERROR("Could not copy codec parameters to decoder context");
  81 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  82 + }
  83 + codec_ctx->pkt_timebase = fmt_ctx->streams[audio_stream_index]->time_base;
  84 +
  85 + //9、打开解码器
  86 + if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
  87 + JZSDK_LOG_ERROR("Could not open codec");
  88 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  89 + }
  90 +
  91 + //10、分配AVPacket和AVFrame
  92 + AVPacket *packet = av_packet_alloc();
  93 + AVFrame *frame = av_frame_alloc();
  94 + AVFrame *temp_frame = av_frame_alloc();
  95 + AVFrame *eq_frame = av_frame_alloc();
  96 +
  97 + if (!packet || !frame || !temp_frame || !eq_frame)
  98 + {
  99 + JZSDK_LOG_ERROR("Could not allocate packet or frame");
  100 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  101 + }
  102 +
  103 + //如果此次输入的音频数据不同于采样的当前设置,重设采样
  104 + FF_Resample_Reset(AD_Info, codec_ctx->sample_rate, codec_ctx->ch_layout, codec_ctx->sample_fmt);
  105 +
  106 + //读取,并解码歌曲数据
  107 + while (AD_Info->AudioDeal_FileRead_Execute_Flag == JZ_FLAGCODE_ON)
  108 + {
  109 + //读取音频内容
  110 + if (av_read_frame(fmt_ctx, packet) < 0) //没有正常取出数据包
  111 + {
  112 + //执行读取数据完毕之类的操作
  113 + JZSDK_LOG_ERROR("没有正常取出数据包");
  114 + break;
  115 + }
  116 +
  117 + //如果音频流指引值不相同
  118 + if (packet->stream_index != audio_stream_index)
  119 + {
  120 + av_packet_unref(packet);
  121 + continue;
  122 + }
  123 +
  124 + // 发送数据包到解码器
  125 + ret = avcodec_send_packet(codec_ctx, packet);
  126 + if (ret < 0)
  127 + {
  128 + char errbuf[128];
  129 + av_strerror(ret, errbuf, sizeof(errbuf));
  130 + JZSDK_LOG_ERROR("Error while sending a packet to the decoder %s",errbuf);
  131 + break;
  132 + }
  133 +
  134 + // 接收解码后的帧
  135 + while ( (ret >= 0) && (AD_Info->AudioDeal_ResampleAndFilter_Execute_Flag == JZ_FLAGCODE_ON) )
  136 + {
  137 + ret = avcodec_receive_frame(codec_ctx, frame);
  138 + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  139 + {
  140 + //JZSDK_LOG_ERROR("解码完毕");
  141 + break;
  142 + }
  143 + else if (ret < 0)
  144 + {
  145 + JZSDK_LOG_ERROR("Error while receiving a frame from the decoder\n");
  146 + break;
  147 + }
  148 +
  149 + // 计算当前帧的持续时间(秒)
  150 + double frame_duration = (double)frame->nb_samples / codec_ctx->sample_rate;
  151 + //JZSDK_LOG_INFO("nb:%d rate:%d duration:%f", frame->nb_samples, codec_ctx->sample_rate, frame_duration);
  152 +
  153 + // 累计总持续时间
  154 + total_duration += frame_duration;
  155 + //JZSDK_LOG_INFO("当前播放时间%f",total_duration);
  156 +
  157 + int out_nb_samples = 0;
  158 +
  159 + //丢进重采样器
  160 + void* resampledData = FF_Resample_Send_And_Get_ResampleData(AD_Info ,frame->data, frame->nb_samples, &out_nb_samples);
  161 +
  162 + //滤波
  163 + if(AD_Info->FilterMode != JZ_FLAGCODE_OFF)
  164 + {
  165 + temp_frame->data[0] = (unsigned char*)resampledData;
  166 + temp_frame->nb_samples = out_nb_samples;
  167 +
  168 + //将临时帧 放入 均衡滤波器
  169 + FF_Filter_push_frame_to_fliter(AD_Info, temp_frame);
  170 +
  171 + while(AD_Info->Flag_AudioDataGenerationImplement == JZ_FLAGCODE_ON)
  172 + {
  173 + //得到滤波器输出的音频帧 eq_frame
  174 + int fret = FF_Filter_get_frame_from_filter(AD_Info, eq_frame);
  175 + if (fret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  176 + {
  177 + break;
  178 + }
  179 +
  180 + //播放改滤波后的帧
  181 + Pcm_AlsaPlay(AD_Info, (unsigned char*)eq_frame->data[0], eq_frame->nb_samples);
  182 +
  183 + av_frame_unref(eq_frame);
  184 + }
  185 +
  186 + av_frame_unref(temp_frame);
  187 + }
  188 + else //不滤波
  189 + {
  190 + //直接播放
  191 + //JZSDK_LOG_INFO("播放 %d 数据",out_nb_samples);
  192 + Pcm_AlsaPlay(AD_Info ,resampledData, out_nb_samples);
  193 + }
  194 +
  195 + FF_Resample_freeReasmpleData(resampledData);
  196 + }
  197 +
  198 + av_packet_unref(packet);
  199 + }
  200 +
  201 + // 清理资源
  202 + av_frame_free(&frame);
  203 + av_frame_free(&temp_frame);
  204 + av_frame_free(&eq_frame);
  205 + av_packet_free(&packet);
  206 + avcodec_free_context(&codec_ctx);
  207 + avformat_close_input(&fmt_ctx);
  208 +
  209 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  210 +}
  1 +#include "libavutil/opt.h"
  2 +#include "libavutil/channel_layout.h"
  3 +#include "libavutil/samplefmt.h"
  4 +#include "libavformat/avformat.h"
  5 +#include "libavfilter/avfilter.h"
  6 +#include "libavfilter/buffersink.h"
  7 +#include "libavfilter/buffersrc.h"
  8 +#include "libavcodec/avcodec.h"
  9 +
  10 +#include "AudioDeal/FF_Statement.h"
  11 +
  12 +#include <stdio.h>
  13 +#include "JZsdkLib.h"
  14 +#include "../AudioDeal.h"
  15 +#include "../Resample/pcm_Resample.h"
  16 +#include "../Filter/FF_Filter.h"
  17 +#include "../Alsa/pcm_AlsaPlay.h"
  18 +#include "./AudioStreamDeal.h"
  19 +
  20 +/***************************
  21 + *
  22 + * 文件播放的数据入口
  23 + *
  24 + * ************/
  25 +T_JZsdkReturnCode AudioFile_Stream_Interface_PcmData(struct AudioDealInfo *AD_Info, AVFrame *frame)
  26 +{
  27 + AVFrame *eq_frame = av_frame_alloc();
  28 + AVFrame *temp_frame = av_frame_alloc();
  29 +
  30 + if (!eq_frame || !temp_frame)
  31 + {
  32 + JZSDK_LOG_ERROR("Could not allocate packet or frame");
  33 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  34 + }
  35 +
  36 + int out_nb_samples = 0;
  37 +
  38 + //丢进重采样器
  39 + void* resampledData = FF_Resample_Send_And_Get_ResampleData(AD_Info ,frame->data, frame->nb_samples, &out_nb_samples);
  40 +
  41 + //JZSDK_LOG_INFO("滤波已完成");
  42 +
  43 + //滤波
  44 + if(AD_Info->FilterMode != JZ_FLAGCODE_OFF)
  45 + {
  46 + temp_frame->data[0] = (unsigned char*)resampledData;
  47 + temp_frame->nb_samples = out_nb_samples;
  48 +
  49 + //将临时帧 放入 均衡滤波器
  50 + FF_Filter_push_frame_to_fliter(AD_Info, temp_frame);
  51 +
  52 + //JZSDK_LOG_INFO("放入均衡器");
  53 +
  54 + while(AD_Info->AudioDeal_ResampleAndFilter_Execute_Flag == JZ_FLAGCODE_ON)
  55 + {
  56 + //得到滤波器输出的音频帧 eq_frame
  57 + int fret = FF_Filter_get_frame_from_filter(AD_Info, eq_frame);
  58 + if (fret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  59 + {
  60 + break;
  61 + }
  62 +
  63 + //播放改滤波后的帧
  64 + Pcm_AlsaPlay(AD_Info, (unsigned char*)eq_frame->data[0], eq_frame->nb_samples);
  65 + //JZSDK_LOG_INFO("代替播放 %d", eq_frame->nb_samples);
  66 +
  67 + av_frame_unref(eq_frame);
  68 + }
  69 +
  70 + av_frame_unref(temp_frame);
  71 + }
  72 + else //不滤波
  73 + {
  74 + //直接播放
  75 + //JZSDK_LOG_INFO("播放 %d 数据",out_nb_samples);
  76 + Pcm_AlsaPlay(AD_Info ,resampledData, out_nb_samples);
  77 + }
  78 +
  79 + FF_Resample_freeReasmpleData(resampledData);
  80 +
  81 + av_frame_free(&eq_frame);
  82 + av_frame_free(&temp_frame);
  83 +}
  84 +
  1 +/**
  2 + ********************************************************************
  3 + * @file AudioStreamDeal.h
  4 + * AudioStreamDeal的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef AUDIO_STREAM_DEAL_H
  11 +#define AUDIO_STREAM_DEAL_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "AudioDeal/FF_Statement.h"
  16 +#include "../AudioDeal.h"
  17 +
  18 +#ifdef __cplusplus
  19 +extern "C" {
  20 +#endif
  21 +
  22 +/* Exported constants --------------------------------------------------------*/
  23 +/* 常亮定义*/
  24 +
  25 +/* Exported types ------------------------------------------------------------*/
  26 +
  27 +
  28 +/* Exported functions --------------------------------------------------------*/
  29 +int PCM_PooL_Interface_PcmData(struct AudioDealInfo *AD_Info,unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format , unsigned char* data, int dataSize);
  30 +int PCM_PooL_Interface_PcmData_WithoutReply(struct AudioDealInfo *AD_Info,unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format , unsigned char* data, int dataSize);
  31 +T_JZsdkReturnCode AudioFile_Stream_Interface_PcmData(struct AudioDealInfo *AD_Info, AVFrame *frame);
  32 +T_JZsdkReturnCode mp3_Stream_Interface_Mp3Data(struct AudioDealInfo *AD_Info, unsigned int in_sampleRate, unsigned char *data, int dataSize);
  33 +
  34 +
  35 +#ifdef __cplusplus
  36 +}
  37 +#endif
  38 +
  39 +#endif
  1 +#include "AudioDeal/FF_Statement.h"
  2 +#include "../Resample/pcm_Resample.h"
  3 +#include "AudioDeal/AudioDeal.h"
  4 +#include "AudioDeal/Filter/FF_Filter.h"
  5 +
  6 +#include "JZsdkLib.h"
  7 +#include "AudioDeal/Alsa/pcm_AlsaPlay.h"
  8 +
  9 +/******************************
  10 + *
  11 + * 用于处理mp3流的.c
  12 + * 输入mp3数据 → 解码成pcm → pcm流处理模块
  13 + *
  14 + * ******************************/
  15 +
  16 +static AVCodecParserContext *parser = NULL;
  17 +static AVCodecContext *cdc_ctx = NULL;
  18 +static AVPacket *pkt;
  19 +static AVFrame *decoded_frame = NULL;
  20 +static const AVCodec *codec;
  21 +
  22 +T_JZsdkReturnCode Stream_Player_decode(struct AudioDealInfo *AD_Info, AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame);
  23 +
  24 +int File_Stream_deal_Init(enum AVCodecID id)
  25 +{
  26 + pkt = av_packet_alloc();
  27 + if(!pkt)
  28 + {
  29 + JZSDK_LOG_ERROR("av_packet_alloc failed.");
  30 + }
  31 +
  32 + codec = avcodec_find_decoder(id);
  33 + if (!codec) {
  34 + JZSDK_LOG_ERROR("Codec not found\n");
  35 + }
  36 +
  37 + parser = av_parser_init(codec->id);
  38 + if (!parser) {
  39 + JZSDK_LOG_ERROR("Parser not found\n");
  40 + }
  41 +
  42 + cdc_ctx = avcodec_alloc_context3(codec);
  43 + if (!cdc_ctx) {
  44 + JZSDK_LOG_ERROR("Could not allocate audio codec context\n");
  45 + }
  46 +
  47 + /* open it */
  48 + if (avcodec_open2(cdc_ctx, codec, NULL) < 0)
  49 + {
  50 + JZSDK_LOG_ERROR("Could not open codec\n");
  51 + }
  52 +
  53 + //如果解码器不存在,初始化解码器
  54 + if (!decoded_frame)
  55 + {
  56 + if (!(decoded_frame = av_frame_alloc()))
  57 + {
  58 + JZSDK_LOG_ERROR("Could not allocate audio frame\n");
  59 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  60 + }
  61 + }
  62 +}
  63 +
  64 +//输入mp3的实时数据,以及本次数据的长度
  65 +T_JZsdkReturnCode mp3_Stream_Interface_Mp3Data(struct AudioDealInfo *AD_Info, unsigned int in_sampleRate, unsigned char *data, int dataSize)
  66 +{
  67 + //将数据输入到
  68 + while(dataSize > 0)
  69 + {
  70 +
  71 + //检查参数,并将正确的数据输入到pkt中
  72 + //parser 解析器
  73 + //cdc_ctx 上下文
  74 + //pkt输出的数据指针
  75 + //data datasize 输入的数据指针
  76 + //pts、dts、pos:时间戳和位置信息,一般可以设置为AV_NOPTS_VALUE和0。
  77 + int ret = av_parser_parse2(parser, cdc_ctx, &pkt->data, &pkt->size, data, dataSize, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  78 + if (ret < 0) {
  79 + printf("Error while parsing\n");
  80 + return -1;
  81 + }
  82 +
  83 + //重置重采样器
  84 + FF_Resample_Reset(AD_Info, in_sampleRate, (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO, AV_SAMPLE_FMT_S16);
  85 +
  86 + //数据指针 往后一个解析长度
  87 + //长度指针 减少一个被解析数据的长度
  88 + data += ret;
  89 + dataSize -= ret;
  90 +
  91 + //如果输出有长度 解码输出的数据
  92 + if (pkt->size > 0)
  93 + {
  94 + Stream_Player_decode(AD_Info, cdc_ctx, pkt, decoded_frame);
  95 + }
  96 + }
  97 +}
  98 +
  99 +
  100 +//
  101 +
  102 +T_JZsdkReturnCode Stream_Player_decode(struct AudioDealInfo *AD_Info, AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame)
  103 +{
  104 + int ret;
  105 +
  106 + //发送数据包给解码器解码,已将数据解码为pcm原始数据
  107 + ret = avcodec_send_packet(dec_ctx, pkt);
  108 + if (ret < 0)
  109 + {
  110 + JZSDK_LOG_ERROR("Error submitting the packet to the decoder, ret=%d\n",ret);
  111 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  112 + }
  113 +
  114 + //申请内存
  115 + AVFrame *eq_frame = av_frame_alloc();
  116 + AVFrame *temp_frame = av_frame_alloc();
  117 +
  118 + /* read all the output frames (in general there may be any number of them */
  119 + //读取输出的帧
  120 + while ( (ret >= 0) && (AD_Info->Flag_AudioDataGenerationImplement == JZ_FLAGCODE_ON) )
  121 + {
  122 + //从解码器中读取解码后的音频帧数据
  123 + ret = avcodec_receive_frame(dec_ctx, frame);
  124 + //输出帧不可用 、 输出帧 已用完
  125 + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  126 + {
  127 + break;
  128 + }
  129 + else if (ret < 0)
  130 + {
  131 + JZSDK_LOG_ERROR("Error during decoding\n");
  132 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  133 + }
  134 +
  135 + int out_nb_samples = 0;
  136 +
  137 + //重采样解码后的数据
  138 + void* resampledData = FF_Resample_Send_And_Get_ResampleData(AD_Info, frame->data, frame->nb_samples, &out_nb_samples);
  139 +
  140 + //滤波
  141 + if(AD_Info->FilterMode != JZ_FLAGCODE_OFF)
  142 + {
  143 + temp_frame->data[0] = (unsigned char*)resampledData;
  144 + temp_frame->nb_samples = out_nb_samples;
  145 +
  146 + //将临时帧 放入 均衡滤波器
  147 + FF_Filter_push_frame_to_fliter(AD_Info, temp_frame);
  148 +
  149 + while(AD_Info->Flag_AudioDataGenerationImplement == JZ_FLAGCODE_ON)
  150 + {
  151 + //得到滤波器输出的音频帧 eq_frame
  152 + int fret = FF_Filter_get_frame_from_filter(AD_Info, eq_frame);
  153 + if (fret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  154 + {
  155 + break;
  156 + }
  157 +
  158 + //播放改滤波后的帧
  159 + Pcm_AlsaPlay(AD_Info, (unsigned char*)eq_frame->data[0], eq_frame->nb_samples);
  160 +
  161 + av_frame_unref(eq_frame);
  162 + }
  163 +
  164 + av_frame_unref(temp_frame);
  165 + }
  166 + else //不滤波
  167 + {
  168 + //直接播放
  169 + //JZSDK_LOG_INFO("播放 %d 数据",out_nb_samples);
  170 + Pcm_AlsaPlay(AD_Info ,resampledData, out_nb_samples);
  171 + }
  172 +
  173 + FF_Resample_freeReasmpleData(resampledData);
  174 + }
  175 +
  176 + //释放掉输出的变量
  177 + av_frame_unref(temp_frame);
  178 + av_frame_unref(eq_frame);
  179 +}
  1 +#include <stdio.h>
  2 +
  3 +#include "AudioDeal/FF_Statement.h"
  4 +
  5 +#include "../Alsa/pcm_AlsaPlay.h"
  6 +#include "AudioDeal/AudioDeal.h"
  7 +#include "../Filter/FF_Filter.h"
  8 +#include "../Resample/pcm_Resample.h"
  9 +#include "JZsdkLib.h"
  10 +
  11 +#include "./AudioStreamDeal.h"
  12 +
  13 +#define MEM_BLOCK_LEN (4096) //设置内存块处理长度为4096
  14 +
  15 +/***************************
  16 + *
  17 + * pcm数据池子,pcm数据的接入接口
  18 + *
  19 + * ************/
  20 +int PCM_PooL_Interface_PcmData(struct AudioDealInfo *AD_Info,unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format , unsigned char* data, int dataSize)
  21 +{
  22 + T_JZsdkReturnCode ret;
  23 +
  24 + //这段代码遍历 data,将连续的 0x00 0x00 替换为 0x00 0x01。这可能是为了确保音频数据中没有静默的连续帧,或者是为了满足某种特定的播放要求。
  25 + for(int i=0; i<dataSize; i+=2)
  26 + {
  27 + if(i+1 < dataSize && data[i]==0x00 && data[i+1]==0x00)
  28 + {
  29 + data[i+1] = 0x01; // MSB,LSB
  30 + }
  31 + }
  32 +
  33 + //分配了两个 AVFrame 对象,一个用于临时存储重采样后的数据(temp_frame),另一个用于从过滤器中获取数据(eq_frame)。
  34 + AVFrame *temp_frame = av_frame_alloc();
  35 + AVFrame *eq_frame = av_frame_alloc();
  36 +
  37 + //如果此次输入的音频数据不同于采样的当前设置,重设采样
  38 + FF_Resample_Reset(AD_Info, in_sampleRate, in_ch_layout, in_format);
  39 +
  40 + int out_nb_samples = 0; //重采样输出的数据长度
  41 + int UnDeal_DataSize = dataSize; //未处理的数据长度
  42 +
  43 + while( (UnDeal_DataSize > 0) && (AD_Info->AudioDeal_ResampleAndFilter_Execute_Flag == JZ_FLAGCODE_ON))
  44 + {
  45 + int dealLen = 0;
  46 + if (UnDeal_DataSize > MEM_BLOCK_LEN)
  47 + {
  48 + dealLen = MEM_BLOCK_LEN;
  49 + UnDeal_DataSize = UnDeal_DataSize - dealLen;
  50 + }
  51 + else
  52 + {
  53 + dealLen = UnDeal_DataSize;
  54 + UnDeal_DataSize = UnDeal_DataSize - dealLen;
  55 + }
  56 +
  57 + //重采样 //仅处理一半样本量
  58 + void *resampledData = FF_Resample_Send_And_Get_ResampleData(AD_Info, &data, dealLen/2, &out_nb_samples);
  59 +
  60 + //如果滤波打开
  61 + //滤波处理
  62 + if(AD_Info->FilterMode != JZ_FLAGCODE_OFF)
  63 + {
  64 + temp_frame->data[0] = (unsigned char*)resampledData;
  65 + temp_frame->nb_samples = out_nb_samples;
  66 +
  67 + //将临时帧 放入 均衡滤波器
  68 + FF_Filter_push_frame_to_fliter(AD_Info, temp_frame);
  69 +
  70 + while(AD_Info->AudioDeal_ResampleAndFilter_Execute_Flag == JZ_FLAGCODE_ON)
  71 + {
  72 + //得到滤波器输出的音频帧 eq_frame
  73 + int fret = FF_Filter_get_frame_from_filter(AD_Info, eq_frame);
  74 + if (fret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  75 + {
  76 + break;
  77 + }
  78 +
  79 + //播放改滤波后的帧
  80 + Pcm_AlsaPlay(AD_Info, (unsigned char*)eq_frame->data[0], eq_frame->nb_samples);
  81 +
  82 + av_frame_unref(eq_frame);
  83 + }
  84 +
  85 + av_frame_unref(temp_frame);
  86 + }
  87 + else //不滤波
  88 + {
  89 + //直接播放
  90 + //JZSDK_LOG_INFO("播放 %d 数据",out_nb_samples);
  91 + Pcm_AlsaPlay(AD_Info ,resampledData, out_nb_samples);
  92 + }
  93 +
  94 + //先改用保存
  95 + //AudioDeal_Test_DataSave((unsigned char *)resampledData, out_nb_samples, ResampleInfo->Out_SampleRate, 1, 16);
  96 +
  97 + FF_Resample_freeReasmpleData(resampledData);
  98 +
  99 + data += dealLen;
  100 + }
  101 +
  102 +
  103 + av_frame_free(&temp_frame);
  104 + av_frame_free(&eq_frame);
  105 +}
  106 +
  107 +
  1 +/**
  2 + ********************************************************************
  3 + * @file FF_Resample.h
  4 + * FF_Resample
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef FF_STATEMENT_H
  11 +#define FF_STATEMENT_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "libavutil/opt.h"
  15 +#include "libavutil/channel_layout.h"
  16 +#include "libavutil/samplefmt.h"
  17 +#include "libswresample/swresample.h"
  18 +#include "libavformat/avformat.h"
  19 +#include "libavcodec/avcodec.h"
  20 +#include "libavutil/frame.h"
  21 +#include "libavutil/avutil.h"
  22 +#include "libavutil/samplefmt.h"
  23 +#include "libavfilter/avfilter.h"
  24 +
  25 +
  26 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  27 +
  28 +#ifdef __cplusplus
  29 +extern "C" {
  30 +#endif
  31 +
  32 +
  33 +/* Exported constants --------------------------------------------------------*/
  34 +/* 常亮定义*/
  35 +
  36 +/* Exported types ------------------------------------------------------------*/
  37 +
  38 +/* Exported functions --------------------------------------------------------*/
  39 +
  40 +#ifdef __cplusplus
  41 +}
  42 +#endif
  43 +
  44 +#endif
  1 +#include "libavutil/opt.h"
  2 +#include "libavutil/channel_layout.h"
  3 +#include "libavutil/samplefmt.h"
  4 +#include "libavutil/frame.h"
  5 +
  6 +#include "libavformat/avformat.h"
  7 +#include "libavfilter/avfilter.h"
  8 +#include "libavfilter/buffersink.h"
  9 +#include "libavfilter/buffersrc.h"
  10 +#include "libavcodec/avcodec.h"
  11 +
  12 +#include "AudioDeal/FF_Statement.h"
  13 +
  14 +#include <stdio.h>
  15 +#include "JZsdkLib.h"
  16 +#include "../AudioDeal.h"
  17 +#include "./FF_FilterParam.h"
  18 +
  19 +struct FF_Filter
  20 +{
  21 + //创建滤波器上下文
  22 + AVFilterContext *abuffersrc_ctx;
  23 + const AVFilter *abuffersrc;
  24 + AVFilterContext *abuffersink_ctx;
  25 + const AVFilter *abuffersink;
  26 +
  27 + // 创建滤波器图
  28 + AVFilterGraph *filter_graph;
  29 + AVFilterInOut *outputs, *inputs;
  30 + AVFilterLink *outlink;
  31 + AVChannelLayout ch_layout;
  32 +
  33 + //输入音频的参数
  34 + int In_SampleRate;
  35 + enum AVSampleFormat In_Format;
  36 +
  37 +}FF_Filter;
  38 +
  39 +static int FilterFlag = JZ_FLAGCODE_OFF;
  40 +
  41 +static T_JZsdkReturnCode FF_Filter_ParamInit(struct AudioDealInfo *AD_Info, unsigned char *FilterParam)
  42 +{
  43 +// 尝试获取或分配FilterInfo
  44 + struct FF_Filter **FilterInfoPtr = (struct FF_Filter **)&AD_Info->FilterInfo;
  45 + if (*FilterInfoPtr == NULL) {
  46 + // 分配内存
  47 + *FilterInfoPtr = (struct FF_Filter *)malloc(sizeof(struct FF_Filter));
  48 + if (*FilterInfoPtr == NULL) {
  49 + // 内存分配失败处理
  50 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 使用更具体的错误码
  51 + }
  52 + }
  53 +
  54 +/*************
  55 + *
  56 + * 初始化
  57 + *
  58 + * ****************/
  59 + //创建一个记录结结构体
  60 + struct FF_Filter *FilterInfo = *FilterInfoPtr;
  61 +
  62 + //参数填写数组
  63 + char args[512];
  64 + int ret = 0;
  65 + // 创建滤波器发送实例
  66 + FilterInfo->abuffersrc = avfilter_get_by_name("abuffer");
  67 +
  68 + // 创建滤波器接收实例
  69 + FilterInfo->abuffersink = avfilter_get_by_name("abuffersink");
  70 +
  71 + //注册端点
  72 + FilterInfo->inputs = avfilter_inout_alloc();
  73 + if (!FilterInfo->inputs)
  74 + {
  75 + JZSDK_LOG_ERROR("in端点创建失败");
  76 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  77 + }
  78 +
  79 + FilterInfo->outputs = avfilter_inout_alloc();
  80 + if (!FilterInfo->outputs)
  81 + {
  82 + JZSDK_LOG_ERROR("out端点创建失败");
  83 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  84 + }
  85 +
  86 + enum AVSampleFormat out_sample_fmts[] = { AD_Info->Target_SampleFormat, -1 };
  87 + int out_sample_rates[] = { AD_Info->Target_SampleRate, -1 };
  88 + FilterInfo->outlink = NULL;
  89 +
  90 + //没有做时基
  91 + //AVRational time_base = fmt_ctx->streams[audio_stream_index]->time_base;
  92 +
  93 + // 创建滤波器图
  94 + FilterInfo->filter_graph = avfilter_graph_alloc();
  95 + if (!FilterInfo->filter_graph)
  96 + {
  97 + // 错误处理:无法分配滤波器图
  98 + JZSDK_LOG_ERROR("滤波器图创建失败");
  99 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  100 + }
  101 +
  102 + printf("Filter初始化完成\n");
  103 +
  104 +/*************
  105 + *
  106 + * 创建滤波器链
  107 + *
  108 + * ****************/
  109 + // if (AD_Info->Target_ChannelLayout == AV_CHANNEL_LAYOUT_STEREO)
  110 + // {
  111 + // FilterInfo->In_Channels = 2;
  112 + // }
  113 + // else
  114 + // {
  115 + // FilterInfo->In_Channels = 1;
  116 + // }
  117 + //FilterInfo->In_Channels = AD_Info->Target_ChannelLayout.nb_channels;
  118 + //av_channel_layout_default(&FilterInfo->ch_layout, FilterInfo->In_Channels);
  119 + av_channel_layout_copy(&FilterInfo->ch_layout, &AD_Info->Target_ChannelLayout);
  120 +
  121 + memset(args, 0 ,sizeof(args));
  122 + ret = snprintf(args, sizeof(args),
  123 + "sample_rate=%d:sample_fmt=%s:channel_layout=",
  124 + AD_Info->Target_SampleRate,
  125 + av_get_sample_fmt_name(AD_Info->Target_SampleFormat));
  126 + av_channel_layout_describe(&FilterInfo->ch_layout, args + ret, sizeof(args) - ret);
  127 +
  128 + ret = avfilter_graph_create_filter(&FilterInfo->abuffersrc_ctx, FilterInfo->abuffersrc, "in",
  129 + args, NULL, FilterInfo->filter_graph);
  130 + if (ret < 0)
  131 + {
  132 + // 错误处理:无法创建abuffer滤波器
  133 + JZSDK_LOG_ERROR("无法创建abuffer滤波器 ");
  134 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  135 + }
  136 +
  137 + //建立输出参数
  138 + if (avfilter_graph_create_filter(&FilterInfo->abuffersink_ctx, FilterInfo->abuffersink, "out", NULL, NULL, FilterInfo->filter_graph) < 0)
  139 + {
  140 + // 错误处理:无法创建abuffersink滤波器
  141 + JZSDK_LOG_ERROR("无法创建abuffersink滤波器 ");
  142 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  143 + }
  144 +
  145 + ret = av_opt_set_int_list(FilterInfo->abuffersink_ctx, "sample_fmts", out_sample_fmts, -1, AV_OPT_SEARCH_CHILDREN);
  146 + if (ret < 0) {
  147 + JZSDK_LOG_ERROR("Cannot set output sample format");
  148 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  149 + }
  150 +
  151 + ret = av_opt_set(FilterInfo->abuffersink_ctx, "ch_layouts", "stereo",
  152 + AV_OPT_SEARCH_CHILDREN);
  153 + if (ret < 0)
  154 + {
  155 + JZSDK_LOG_ERROR("Cannot set output channel layout");
  156 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  157 + }
  158 +
  159 + ret = av_opt_set_int_list(FilterInfo->abuffersink_ctx, "sample_rates", out_sample_rates, -1,
  160 + AV_OPT_SEARCH_CHILDREN);
  161 + if (ret < 0)
  162 + {
  163 + JZSDK_LOG_ERROR("Cannot set output sample rate");
  164 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  165 + }
  166 +
  167 + printf("Filter滤波器链完成\n");
  168 +
  169 +/*************
  170 + *
  171 + * 设置滤镜图的端点。这个滤镜图将依据filters_descr中的描述进行链接。
  172 + *
  173 + * ****************/
  174 +// 设置输出端点的信息,其名称设为"in",关联的滤镜上下文为buffersrc_ctx,焊盘索引为0,并设置下一个端点为NULL。
  175 + FilterInfo->outputs->name = av_strdup("in");
  176 + FilterInfo->outputs->filter_ctx = FilterInfo->abuffersrc_ctx;
  177 + FilterInfo->outputs->pad_idx = 0;
  178 + FilterInfo->outputs->next = NULL;
  179 +
  180 +/*
  181 + * 缓冲区源(buffersrc)的输出应连接到filters_descr中描述的第一个滤镜的输入焊盘;
  182 + * 如果没有明确指定第一个滤镜的输入标签,则默认使用"in"。
  183 + */
  184 +
  185 +// 设置输入端点的信息,其名称设为"out",关联的滤镜上下文为buffersink_ctx,焊盘索引为0,并设置下一个端点为NULL。
  186 + FilterInfo->inputs->name = av_strdup("out");
  187 + FilterInfo->inputs->filter_ctx = FilterInfo->abuffersink_ctx;
  188 + FilterInfo->inputs->pad_idx = 0;
  189 + FilterInfo->inputs->next = NULL;
  190 +
  191 + printf("Filter端点完成\n");
  192 +
  193 +/*************
  194 + *
  195 + * 链接滤波器
  196 + *
  197 + * ****************/
  198 + char filters_descr[1024];
  199 + // sprintf(filters_descr,
  200 + // "anequalizer=c0 f=250 w=500 g=-48 t=2|c1 f=250 w=500 g=-48 t=2,anequalizer=c0 f=3000 w=1000 g=-48 t=2|c1 f=3000 w=1000 g=-48 t=2"
  201 + // );
  202 + sprintf(filters_descr,
  203 + "%s",FilterParam
  204 + );
  205 +
  206 + //使用avfilter_graph_parse_ptr函数解析滤镜图,将filters_descr中的描述转换成实际的滤镜和连接。
  207 + ret = avfilter_graph_parse_ptr(FilterInfo->filter_graph, filters_descr, &FilterInfo->inputs, &FilterInfo->outputs, NULL);
  208 + if (ret < 0)
  209 + {
  210 + JZSDK_LOG_ERROR("滤波配置出错");
  211 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  212 + }
  213 +
  214 + /*
  215 + * 使用avfilter_graph_config函数配置滤镜图,使其准备好处理数据。
  216 + * 如果配置失败,则返回错误码。
  217 + */
  218 + if ((ret = avfilter_graph_config(FilterInfo->filter_graph, NULL)) < 0){
  219 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  220 + }
  221 +
  222 + /*
  223 + * 打印接收器缓冲区(buffersink)的摘要。
  224 + * 注意:args缓冲区被重用以存储声道布局字符串。
  225 + */
  226 + FilterInfo->outlink = FilterInfo->abuffersink_ctx->inputs[0];
  227 + av_channel_layout_describe(&FilterInfo->outlink->ch_layout, args, sizeof(args));
  228 + av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n",
  229 + (int)FilterInfo->outlink->sample_rate,
  230 + (char *)av_x_if_null(av_get_sample_fmt_name(FilterInfo->outlink->format), "?"),
  231 + args);
  232 +
  233 + /*
  234 + * 释放inputs和outputs结构体所占用的内存。
  235 + */
  236 + avfilter_inout_free(&FilterInfo->inputs);
  237 + avfilter_inout_free(&FilterInfo->outputs);
  238 +
  239 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  240 +}
  241 +
  242 +T_JZsdkReturnCode FF_Filter_Release(struct AudioDealInfo *AD_Info)
  243 +{
  244 + if (AD_Info == NULL || AD_Info->FilterInfo == NULL)
  245 + {
  246 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  247 + }
  248 +
  249 + struct FF_Filter *FilterInfo = (struct FF_Filter *)AD_Info->FilterInfo;
  250 +
  251 + // 释放滤波器图
  252 + if (FilterInfo->filter_graph) {
  253 + avfilter_graph_free(&FilterInfo->filter_graph);
  254 + FilterInfo->filter_graph = NULL; // 避免野指针
  255 + }
  256 +
  257 + // // 释放过滤器实例
  258 + // if (FilterInfo->abuffersrc_ctx) {
  259 + // avfilter_free(FilterInfo->abuffersrc_ctx);
  260 + // FilterInfo->abuffersrc_ctx = NULL; // 避免野指针
  261 + // }
  262 + // if (FilterInfo->abuffersink_ctx) {
  263 + // avfilter_free(FilterInfo->abuffersink_ctx);
  264 + // FilterInfo->abuffersink_ctx = NULL; // 避免野指针
  265 + // }
  266 +
  267 + // 释放输入输出端点
  268 + if (FilterInfo->inputs) {
  269 + avfilter_inout_free(&FilterInfo->inputs);
  270 + FilterInfo->inputs = NULL; // 避免野指针
  271 + }
  272 + if (FilterInfo->outputs) {
  273 + avfilter_inout_free(&FilterInfo->outputs);
  274 + FilterInfo->outputs = NULL; // 避免野指针
  275 + }
  276 +
  277 + // 释放FilterInfo结构体本身
  278 + free(FilterInfo);
  279 + AD_Info->FilterInfo = NULL;
  280 +
  281 + FilterFlag = JZ_FLAGCODE_OFF;
  282 +
  283 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  284 +}
  285 +
  286 +/***********************
  287 + *
  288 + * 滤波器初始化
  289 + *
  290 + * **************/
  291 +T_JZsdkReturnCode FF_Filter_Init(struct AudioDealInfo *AD_Info, int mode)
  292 +{
  293 + T_JZsdkReturnCode ret;
  294 +
  295 + //如果已经注册过滤波器
  296 + if (FilterFlag == JZ_FLAGCODE_ON)
  297 + {
  298 + ret = FF_Filter_Release(AD_Info);
  299 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  300 + {
  301 + JZSDK_LOG_ERROR("filter init error");
  302 + }
  303 +
  304 + }
  305 +
  306 + //初始化滤波器
  307 + if(mode == 0x00)
  308 + {
  309 + ret =FF_Filter_ParamInit(AD_Info, FILTER_PARAM);
  310 +
  311 + }
  312 + else if (mode == 0x01)
  313 + {
  314 + ret =FF_Filter_ParamInit(AD_Info, Filter_M30);
  315 + }
  316 + else
  317 + {
  318 + ret =FF_Filter_ParamInit(AD_Info, FILTER_PARAM);
  319 + }
  320 +
  321 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  322 + {
  323 + JZSDK_LOG_ERROR("filter init error");
  324 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  325 + }
  326 +
  327 + FilterFlag = JZ_FLAGCODE_ON;
  328 +
  329 + JZSDK_LOG_DEBUG("Filter注册完成\n");
  330 +
  331 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  332 +}
  333 +
  334 +
  335 +
  336 +//将帧放入滤波器
  337 +T_JZsdkReturnCode FF_Filter_push_frame_to_fliter(struct AudioDealInfo *AD_Info ,AVFrame* in_frame)
  338 +{
  339 + struct FF_Filter *FilterInfo = (struct FF_Filter *)AD_Info->FilterInfo;
  340 +
  341 + //给inframe除了音频数据和长度外的参数
  342 + in_frame->sample_rate = AD_Info->Target_SampleRate;
  343 + in_frame->format = AD_Info->Target_SampleFormat;
  344 + //av_channel_layout_copy(&frame->ch_layout, &INPUT_CHANNEL_LAYOUT);
  345 + // in_frame->channel_layout = AV_CH_LAYOUT_STEREO;
  346 + // in_frame->channels = 2;
  347 + av_channel_layout_copy(&in_frame->ch_layout, &AD_Info->Target_ChannelLayout);
  348 +
  349 + int ret = av_buffersrc_add_frame_flags(FilterInfo->abuffersrc_ctx, in_frame, AV_BUFFERSRC_FLAG_KEEP_REF);
  350 + if (ret < 0)
  351 + {
  352 + JZSDK_LOG_ERROR("Error while feeding the audio filtergraph");
  353 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  354 + }
  355 +
  356 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  357 +}
  358 +
  359 +T_JZsdkReturnCode FF_Filter_get_frame_from_filter(struct AudioDealInfo *AD_Info ,AVFrame* out_frame)
  360 +{
  361 + struct FF_Filter *FilterInfo = (struct FF_Filter *)AD_Info->FilterInfo;
  362 +
  363 + int ret = av_buffersink_get_frame(FilterInfo->abuffersink_ctx, out_frame);
  364 + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  365 + {
  366 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  367 + }
  368 +
  369 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  370 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file FF_Filter.h
  4 + * FF_Filter
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef FF_FILTER_H
  11 +#define FF_FILTER_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "AudioDeal/FF_Statement.h"
  16 +
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +T_JZsdkReturnCode FF_Filter_Init(struct AudioDealInfo *AD_Info, int mode);
  21 +T_JZsdkReturnCode FF_Filter_push_frame_to_fliter(struct AudioDealInfo *AD_Info ,AVFrame* in_frame);
  22 +T_JZsdkReturnCode FF_Filter_get_frame_from_filter(struct AudioDealInfo *AD_Info ,AVFrame* out_frame);
  23 +
  24 +/* Exported constants --------------------------------------------------------*/
  25 +/* 常亮定义*/
  26 +
  27 +/* Exported types ------------------------------------------------------------*/
  28 +
  29 +/* Exported functions --------------------------------------------------------*/
  30 +
  31 +#ifdef __cplusplus
  32 +}
  33 +#endif
  34 +
  35 +#endif
  1 +/**
  2 + ********************************************************************
  3 + * @file FF_FilterParam.h
  4 + * FF_FilterParam.h
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef FF_FILTER_PARAM_H
  11 +#define FF_FILTER_PARAM_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +
  21 +/* Exported constants --------------------------------------------------------*/
  22 +/* 常亮定义*/
  23 +
  24 +//流程 先用 acompressor 压缩动态范围 会影响声音大小
  25 +
  26 +#define Filter_No1 "anequalizer=c0 f=2000 w=2000 g=+12 t=2|c1 f=2000 w=2000 g=+12 t=2"
  27 +#define Filter_No2 "acompressor=level_in=1.2:mode=downward:threshold=0.2:ratio=3:attack=50:release=300:makeup=1.2:knee=4:link=average:detection=rms:mix=0.8,highpass=f=300:p=2:mix=1,lowpass=f=5000:p=2:mix=1,lowpass=f=6000:p=2:mix=1,anequalizer=c0 f=2300 w=1500 g=+12 t=2|c1 f=2300 w=1500 g=+12 t=2"
  28 +
  29 +/*
  30 +-level_in=1.5:设置输入增益为1.5,这通常用于增加输入信号的响度。
  31 +-mode=downward:设置压缩器的工作模式为向下压缩,即仅当信号超过阈值时减少增益。
  32 +-threshold=0.5:设置阈值,具体单位取决于该工具的实现,但通常与信号电平相关。
  33 +-ratio=3:1:设置压缩比为3:1,意味着每增加3dB的输入电平,输出电平仅增加1dB。
  34 +-attack=50ms:设置攻击时间为50毫秒,即信号需要超过阈值50毫秒后才开始压缩。
  35 +-release=300ms:设置释放时间为300毫秒,即信号需要低于阈值300毫秒后压缩效果才会逐渐减弱。
  36 +-makeup=1.2:设置补偿增益为1.2,用于在压缩后增加信号的总体响度。
  37 +-knee=4:设置拐点为4,用于在阈值附近平滑地过渡压缩效果。
  38 +-link=average:选择使用所有通道的平均电平来影响压缩,而不是仅使用最响的通道。
  39 +-detection=rms:选择使用RMS(均方根)检测来确定信号电平,这通常比峰值检测更平滑。
  40 +-mix=0.8:设置混合比例为0.8,意味着在输出中80%使用压缩后的信号,20%使用原始信号(这个参数的具体实现可能因工具而异)。
  41 +
  42 +level_in:原信号的输入增益,相当于前置放大器,默认为1,范围[0.015625, 64]
  43 +mode:压缩模式,有 upward和downward两种模式, 默认为downward
  44 +threshold:如果媒体流信号达到此阈值,会引起增益减少。默认为0.125,范围[0.00097563, 1]
  45 +ratio:信号压缩的比例因子,默认为2,范围[1, 20]
  46 +attack:信号提升到阈值所用的毫秒数,默认为20,范围[0.01, 2000]
  47 +release:信号降低到阈值所用的毫秒数,默认为250,范围[0.01, 9000]
  48 +makeup:在处理后,多少信号被放大. 默认为1,范围[1, 64]
  49 +knee:增益降低的阶数,默认为2.82843,范围[1, 8]
  50 +link:信号衰减的average和maximum两种模式, 默认为average
  51 +detection:采用peak峰值信号或rms均方根信号,默认采用更加平滑的rms
  52 +mix:输出时使用多少压缩信号, 默认为1,范围[0, 1]
  53 +2、acrossfade
  54 +*/
  55 +
  56 +/**
  57 + *
  58 +Attenuate 10 dB at 1000 Hz, with a bandwidth of 200 Hz:
  59 +equalizer=f=1000:t=h:width=200:g=-10
  60 +Apply 2 dB gain at 1000 Hz with Q 1 and attenuate 5 dB at 100 Hz with Q 2:
  61 +equalizer=f=1000:t=q:w=1:g=2,equalizer=f=100:t=q:w=2:g=-5
  62 +
  63 +在您给出的例子中,您正在使用 equalizer 滤镜来调整音频信号的特定频率点。这些命令很可能是为了某种音频处理软件或库(如 FFmpeg 的滤镜系统,尽管 FFmpeg 的标准滤镜中可能没有直接名为 equalizer 的滤镜,但这里我们假设它是一个自定义或第三方滤镜)设计的。
  64 +
  65 +第一个例子
  66 +Attenuate 10 dB at 1000 Hz, with a bandwidth of 200 Hz:
  67 +
  68 +equalizer=f=1000:t=h:width=200:g=-10
  69 +f=1000 设置滤波器的中心频率为 1000 Hz。
  70 +t=h 或 t=Hz(尽管 t=h 可能是一个简写,但通常更明确的是 t=Hz)指定带宽的单位为 Hz。
  71 +width=200 设置滤波器的带宽为 200 Hz。
  72 +g=-10 设置在中心频率处的增益为 -10 dB,即衰减 10 dB。
  73 +第二个例子
  74 +Apply 2 dB gain at 1000 Hz with Q 1 and attenuate 5 dB at 100 Hz with Q 2:
  75 +
  76 +equalizer=f=1000:t=q:w=1:g=2,equalizer=f=100:t=q:w=2:g=-5
  77 +第一个 equalizer 滤镜设置:
  78 +f=1000 设置中心频率为 1000 Hz。
  79 +t=q 指定带宽的单位为 Q 因子。
  80 +w=1 设置 Q 因子为 1,这会影响滤波器的带宽和尖锐程度。Q 因子越高,带宽越窄,滤波器越尖锐。
  81 +g=2 设置在中心频率处的增益为 2 dB。
  82 +第二个 equalizer 滤镜设置:
  83 +f=100 设置中心频率为 100 Hz。
  84 +同样地,t=q 指定带宽的单位为 Q 因子。
  85 +w=2 设置 Q 因子为 2,这会影响 100 Hz 处的滤波效果。
  86 +g=-5 设置在中心频率处的增益为 -5 dB,即衰减 5 dB。
  87 +请注意,这些命令假设了 equalizer 滤镜能够处理多个实例并应用于同一音频流,且每个实例都独立设置其参数。这在许多音频处理软件中是常见的,但在具体实现时可能会有所不同。
  88 +
  89 +此外,如果您的环境不支持直接以这种方式链接多个滤镜实例(例如,在 FFmpeg 中,您可能需要使用分号 ; 而不是逗号 , 来分隔不同的滤镜,或者将滤镜放入单独的链中),您可能需要调整命令格式以符合您所使用的软件或库的语法要求。然而,在您给出的例子中,逗号 , 被用作分隔符,这通常是在某些特定上下文(如某些音频编辑软件的内部命令或脚本)中使用的。
  90 +
  91 +
  92 +t=h 和t=q的区别是什么 请详细说说
  93 +
  94 +在音频处理或信号处理领域中,t=h 和 t=q 在设置均衡器(equalizer)或滤波器(filter)时代表了不同的带宽(bandwidth)指定方式,它们主要影响滤波器对频率的响应形状和尖锐程度。以下是详细的区别说明:
  95 +
  96 +t=h(或 Hz)
  97 +含义:t=h 或 t=Hz 表示带宽(bandwidth)的单位是赫兹(Hz)。在这种情况下,你直接指定了滤波器频率响应曲线在中心频率两侧下降一定幅度(通常是3dB)所覆盖的频率范围。
  98 +影响:使用Hz作为带宽单位时,滤波器的形状(如峰形滤波器、带通滤波器或带阻滤波器等)在频率轴上的展宽是固定的,不随中心频率的变化而变化。这有助于精确控制滤波器对特定频率范围的影响。
  99 +应用:当你需要精确控制滤波器影响的频率范围时,使用Hz作为带宽单位非常有用。例如,在音频制作中,你可能想要增强或减弱某个特定频率及其附近很小范围内的声音。
  100 +t=q(Q-Factor)
  101 +含义:t=q 表示带宽的指定方式是使用Q因子(Q-Factor)。Q因子是中心频率与滤波器带宽的比值,它描述了滤波器对中心频率的选择性,即滤波器如何尖锐地聚焦于中心频率。
  102 +影响:Q因子越高,滤波器的带宽越窄,滤波器对中心频率的选择性越强,响应曲线越尖锐。相反,Q因子越低,滤波器的带宽越宽,滤波器对中心频率的选择性越弱,响应曲线越平坦。
  103 +应用:当你需要创建一个尖锐的滤波器响应,以便仅影响中心频率附近的非常小的频率范围时,使用Q因子作为带宽指定方式非常合适。例如,在电子音乐制作中,你可能想要创建一个非常尖锐的陷波滤波器来去除某个不想要的频率。
  104 +总结
  105 +t=h(或Hz)和t=q的主要区别在于它们如何指定滤波器的带宽。
  106 +使用Hz时,你直接指定了滤波器在频率轴上的展宽。
  107 +使用Q因子时,你指定了滤波器对中心频率的选择性,即滤波器的尖锐程度。
  108 +选择哪种方式取决于你的具体需求,比如你需要精确控制哪个频率范围或你想要滤波器有多尖锐。
  109 + *
  110 + *
  111 +*/
  112 +
  113 +// ffmpeg -i 人民警察警歌.mp3 -af "equalizer=f=50:t=q:w=2.0:g=-48, equalizer=f=100:t=q:w=2.0:g=-48, \
  114 +// equalizer=f=250:t=q:w=2.0:g=-48, equalizer=f=500:t=q:w=2.0:g=-24, \
  115 +// equalizer=f=1000:t=q:w=2.0:g=-24, equalizer=f=4000:t=q:w=2.0:g=0, \
  116 +// equalizer=f=8000:t=q:w=2.0:g=0, equalizer=f=16000:t=q:w=2.0:g=0, \
  117 +// anequalizer=c0 f=250 w=500 g=-48 t=2|c1 f=250 w=500 g=-48 t=2" out1.mp3
  118 +
  119 +//无滤波113 114
  120 +
  121 +
  122 +//112.6 111.8
  123 +#define Filter_No3 "equalizer=f=50:t=q:w=2.0:g=-48, \
  124 + equalizer=f=100:t=q:w=2.0:g=-48, \
  125 + equalizer=f=250:t=q:w=2.0:g=-48, \
  126 + equalizer=f=500:t=q:w=2.0:g=-24, \
  127 + equalizer=f=1000:t=q:w=2.0:g=-12, \
  128 + equalizer=f=2000:t=q:w=2.0:g=4, \
  129 + equalizer=f=4000:t=q:w=2.0:g=0, \
  130 + equalizer=f=8000:t=q:w=2.0:g=-12, \
  131 + equalizer=f=16000:t=q:w=2.0:g=-24, \
  132 + equalizer=f=32000:t=q:w=2.0:g=-48, \
  133 + bass=g=-96:f=200:t=q:w=10"
  134 +
  135 +//114 //117 太尖锐了
  136 +#define Filter_No4 "equalizer=f=50:t=q:w=2.0:g=-24, \
  137 + equalizer=f=100:t=q:w=2.0:g=-12, \
  138 + equalizer=f=250:t=q:w=2.0:g=-12, \
  139 + equalizer=f=500:t=q:w=2.0:g=-8, \
  140 + equalizer=f=1000:t=q:w=2.0:g=4, \
  141 + equalizer=f=2000:t=q:w=2.0:g=4, \
  142 + equalizer=f=4000:t=q:w=2.0:g=4, \
  143 + equalizer=f=8000:t=q:w=2.0:g=0, \
  144 + equalizer=f=16000:t=q:w=2.0:g=-6, \
  145 + equalizer=f=32000:t=q:w=2.0:g=-24, \
  146 + bass=g=-96:f=150:t=q:w=10"
  147 +
  148 +//113 //115 还是尖锐
  149 +#define Filter_No5 "equalizer=f=50:t=q:w=2.0:g=-24, \
  150 + equalizer=f=100:t=q:w=2.0:g=-8, \
  151 + equalizer=f=250:t=q:w=2.0:g=-8, \
  152 + equalizer=f=500:t=q:w=2.0:g=-4, \
  153 + equalizer=f=1000:t=q:w=2.0:g=4, \
  154 + equalizer=f=2000:t=q:w=2.0:g=0, \
  155 + equalizer=f=4000:t=q:w=2.0:g=0, \
  156 + equalizer=f=8000:t=q:w=2.0:g=0, \
  157 + equalizer=f=16000:t=q:w=2.0:g=-6, \
  158 + equalizer=f=32000:t=q:w=2.0:g=-24, \
  159 + bass=g=-96:f=130:t=q:w=10"
  160 +
  161 +//113 //115 声音效果跟平常差不多
  162 +#define Filter_No6 "equalizer=f=50:t=q:w=2.0:g=-24, \
  163 + equalizer=f=100:t=q:w=2.0:g=-24, \
  164 + equalizer=f=250:t=q:w=2.0:g=-12, \
  165 + equalizer=f=500:t=q:w=2.0:g=-12, \
  166 + equalizer=f=1000:t=q:w=2.0:g=-6, \
  167 + equalizer=f=2000:t=q:w=2.0:g=0, \
  168 + equalizer=f=4000:t=q:w=2.0:g=0, \
  169 + equalizer=f=8000:t=q:w=2.0:g=0, \
  170 + equalizer=f=16000:t=q:w=2.0:g=-6, \
  171 + equalizer=f=32000:t=q:w=2.0:g=-24"
  172 +
  173 +//
  174 +#define Filter_No7 "equalizer=f=50:t=q:w=2.0:g=-24, \
  175 + equalizer=f=100:t=q:w=2.0:g=-24, \
  176 + equalizer=f=250:t=q:w=2.0:g=-12, \
  177 + equalizer=f=500:t=q:w=2.0:g=-12, \
  178 + equalizer=f=1000:t=q:w=2.0:g=-6, \
  179 + equalizer=f=2000:t=q:w=2.0:g=0, \
  180 + equalizer=f=4000:t=q:w=2.0:g=0, \
  181 + equalizer=f=8000:t=q:w=2.0:g=0, \
  182 + equalizer=f=16000:t=q:w=2.0:g=-6, \
  183 + equalizer=f=32000:t=q:w=2.0:g=-24, \
  184 + bass=g=-96:f=50:t=q:w=8"
  185 +
  186 +//650 那段会影响乐器声 1400那段影响人声
  187 +#define Filter_No8 "equalizer=f=100:t=h:width=100:g=-48, \
  188 + equalizer=f=350:t=h:width=100:g=-36, \
  189 + equalizer=f=650:t=h:width=150:g=-12, \
  190 + equalizer=f=1400:t=h:width=600:g=0, \
  191 + equalizer=f=2500:t=h:width=500:g=0, \
  192 + equalizer=f=3500:t=h:width=600:g=0, \
  193 + equalizer=f=4500:t=h:width=500:g=+4, \
  194 + equalizer=f=5500:t=h:width=500:g=+8, \
  195 + equalizer=f=8000:t=h:width=2000:g=+12, \
  196 + bass=g=-96:f=50:t=q:w=8"
  197 +
  198 +
  199 +//11分钟
  200 +#define OLD_FILTER "equalizer=f=31:t=q:w=2.0:g=-48, \
  201 + equalizer=f=62:t=q:w=2.0:g=-48, \
  202 + equalizer=f=125:t=q:w=2.0:g=-48, \
  203 + equalizer=f=250:t=q:w=2.0:g=-24, \
  204 + equalizer=f=500:t=q:w=2.0:g=-24, \
  205 + equalizer=f=1000:t=q:w=2.0:g=-4, \
  206 + equalizer=f=2000:t=q:w=2.0:g=0, \
  207 + equalizer=f=4000:t=q:w=2.0:g=0, \
  208 + equalizer=f=8000:t=q:w=2.0:g=0, \
  209 + equalizer=f=16000:t=q:w=2.0:g=0"
  210 +
  211 +//111.0 111.4 保持46度
  212 +#define Filter_No9 "equalizer=f=31:t=q:w=2.0:g=-48, \
  213 + equalizer=f=62:t=q:w=2.0:g=-48, \
  214 + equalizer=f=125:t=q:w=2.0:g=-48, \
  215 + equalizer=f=250:t=q:w=2.0:g=-48, \
  216 + equalizer=f=500:t=q:w=2.0:g=-48, \
  217 + equalizer=f=1000:t=q:w=2.0:g=-24, \
  218 + equalizer=f=2000:t=q:w=2.0:g=0, \
  219 + equalizer=f=4000:t=q:w=2.0:g=0, \
  220 + equalizer=f=8000:t=q:w=2.0:g=0, \
  221 + equalizer=f=16000:t=q:w=2.0:g=0"
  222 +//旧式机115
  223 +#define Filter_No10 "equalizer=f=31:t=q:w=2.0:g=-48, \
  224 + equalizer=f=62:t=q:w=2.0:g=-48, \
  225 + equalizer=f=125:t=q:w=2.0:g=-48, \
  226 + equalizer=f=250:t=q:w=2.0:g=-32, \
  227 + equalizer=f=500:t=q:w=2.0:g=-24, \
  228 + equalizer=f=1000:t=q:w=2.0:g=-12, \
  229 + equalizer=f=2000:t=q:w=2.0:g=0, \
  230 + equalizer=f=4000:t=q:w=2.0:g=0, \
  231 + equalizer=f=8000:t=q:w=2.0:g=0, \
  232 + equalizer=f=16000:t=q:w=2.0:g=0"
  233 +
  234 +
  235 +#define Filter_M30 "equalizer=f=31:t=q:w=2.0:g=-48, \
  236 + equalizer=f=62:t=q:w=2.0:g=-48, \
  237 + equalizer=f=125:t=q:w=2.0:g=-48, \
  238 + equalizer=f=250:t=q:w=2.0:g=-32, \
  239 + equalizer=f=500:t=q:w=2.0:g=-24, \
  240 + equalizer=f=1000:t=q:w=2.0:g=-12, \
  241 + equalizer=f=2000:t=q:w=2.0:g=0, \
  242 + equalizer=f=4000:t=q:w=2.0:g=0, \
  243 + equalizer=f=8000:t=q:w=2.0:g=0, \
  244 + equalizer=f=16000:t=q:w=2.0:g=0, \
  245 + anequalizer=c0 f=3250 w=750 g=-48 t=2|c1 f=3250 w=750 g=-48 t=2"
  246 +
  247 +#define FILTER_PARAM Filter_No10
  248 +
  249 +/* Exported types ------------------------------------------------------------*/
  250 +
  251 +/* Exported functions --------------------------------------------------------*/
  252 +
  253 +#ifdef __cplusplus
  254 +}
  255 +#endif
  256 +
  257 +#endif
  1 +/**
  2 + ********************************************************************
  3 + * @file FF_FilterParam.h
  4 + * FF_FilterParam.h
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef FF_FILTER_PARAM_DISCARD_H
  11 +#define FF_FILTER_PARAM_DISCARD_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +
  21 +// 比一的发热还高 6分半
  22 +#define OLD_FILTER_2 "equalizer=f=31:t=q:w=2.0:g=-48, \
  23 + equalizer=f=62:t=q:w=2.0:g=-48, \
  24 + equalizer=f=125:t=q:w=2.0:g=-48, \
  25 + equalizer=f=250:t=q:w=2.0:g=-24, \
  26 + equalizer=f=500:t=q:w=2.0:g=-24, \
  27 + equalizer=f=1000:t=q:w=2.0:g=-4, \
  28 + equalizer=f=2000:t=q:w=2.0:g=0, \
  29 + equalizer=f=4000:t=q:w=2.0:g=0, \
  30 + equalizer=f=8000:t=q:w=2.0:g=0, \
  31 + equalizer=f=16000:t=q:w=2.0:g=0, \
  32 + bass=g=-96:f=50:t=q:w=8"
  33 +//240 过热
  34 +#define OLD_FILTER_3 "equalizer=f=31:t=q:w=2.0:g=-48, \
  35 + equalizer=f=62:t=q:w=2.0:g=-48, \
  36 + equalizer=f=125:t=q:w=2.0:g=-48, \
  37 + equalizer=f=250:t=q:w=2.0:g=-24, \
  38 + equalizer=f=500:t=q:w=2.0:g=-24, \
  39 + equalizer=f=1000:t=q:w=2.0:g=-4, \
  40 + equalizer=f=2000:t=q:w=2.0:g=0, \
  41 + equalizer=f=4000:t=q:w=2.0:g=0, \
  42 + equalizer=f=8000:t=q:w=2.0:g=0, \
  43 + equalizer=f=16000:t=q:w=2.0:g=0, \
  44 + bass=g=-96:f=200:t=h:w=10"
  45 +
  46 +/* Exported types ------------------------------------------------------------*/
  47 +
  48 +/* Exported functions --------------------------------------------------------*/
  49 +
  50 +#ifdef __cplusplus
  51 +}
  52 +#endif
  53 +
  54 +#endif
  1 +#include "AudioDeal/FF_Statement.h"
  2 +#include "pcm_Resample.h"
  3 +#include "JZsdkLib.h"
  4 +#include "../AudioDeal.h"
  5 +
  6 +//pcm的重采样结构体记录组
  7 +struct pcm_Resample
  8 +{
  9 + SwrContext *m_swr;
  10 +
  11 + int In_SampleRate; //输入音频的码率
  12 + //unsigned int InCHLayout;
  13 + AVChannelLayout InCHLayout;
  14 + enum AVSampleFormat InFormat;
  15 +
  16 + int Out_SampleRate; //输出音频的码率
  17 + //unsigned int Out_CHLayout;
  18 + AVChannelLayout Out_CHLayout;
  19 + enum AVSampleFormat Out_Format;
  20 +}pcm_Resample;
  21 +
  22 +
  23 +//初始化重编码器
  24 +/*****************
  25 + *
  26 + * 输入变量
  27 + * 音频库结构体
  28 + * 输入的音频采样率 输入的音频频道数, 输入的音频格式
  29 + * *************/
  30 +T_JZsdkReturnCode FF_Resample_Init(struct AudioDealInfo *AD_Info,
  31 + unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format)
  32 +{
  33 + // 尝试获取或分配ResampleInfo
  34 + struct pcm_Resample **ResampleInfoPtr = (struct pcm_Resample **)&AD_Info->ResampleInfo;
  35 + if (*ResampleInfoPtr == NULL) {
  36 + // 分配内存
  37 + *ResampleInfoPtr = (struct pcm_Resample *)malloc(sizeof(struct pcm_Resample));
  38 + if (*ResampleInfoPtr == NULL) {
  39 + // 内存分配失败处理
  40 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 使用更具体的错误码
  41 + }
  42 + (*ResampleInfoPtr)->m_swr = NULL;
  43 + }
  44 +
  45 + // 现在你可以安全地使用ResampleInfo了
  46 + struct pcm_Resample *ResampleInfo = *ResampleInfoPtr;
  47 +
  48 + if (ResampleInfo->m_swr == NULL) {
  49 + ResampleInfo->m_swr = swr_alloc();
  50 + if (!ResampleInfo->m_swr) {
  51 + JZSDK_LOG_ERROR("Failed to allocate the resampling context");
  52 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; // 或者更具体的错误码
  53 + }
  54 + }
  55 +
  56 + ResampleInfo->In_SampleRate = in_sampleRate;
  57 + ResampleInfo->InFormat = in_format;
  58 + //ResampleInfo->InCHLayout = in_ch_layout;
  59 + av_channel_layout_copy(&(ResampleInfo->InCHLayout), &in_ch_layout);
  60 +
  61 + ResampleInfo->Out_SampleRate = AD_Info->Target_SampleRate;
  62 + ResampleInfo->Out_Format = AD_Info->Target_SampleFormat;
  63 + //ResampleInfo->Out_CHLayout = AD_Info->Target_ChannelLayout;
  64 + av_channel_layout_copy(&(ResampleInfo->Out_CHLayout), &AD_Info->Target_ChannelLayout);
  65 +
  66 + //记录当前输入的比特率
  67 + AD_Info->Raw_SampleRate = in_sampleRate;
  68 + AD_Info->Raw_SampleFormat = in_format;
  69 + //AD_Info->Raw_ChannelLayout = in_ch_layout;
  70 + av_channel_layout_copy(&(AD_Info->Raw_ChannelLayout), &in_ch_layout);
  71 +
  72 + // 设置输入参数
  73 + //av_opt_set_int(ResampleInfo->m_swr, "in_channel_layout", ResampleInfo->InCHLayout, 0);
  74 + av_opt_set_chlayout(ResampleInfo->m_swr, "in_chlayout", &ResampleInfo->InCHLayout, 0);
  75 + av_opt_set_sample_fmt(ResampleInfo->m_swr, "in_sample_fmt", ResampleInfo->InFormat, 0);
  76 + av_opt_set_int(ResampleInfo->m_swr, "in_sample_rate", ResampleInfo->In_SampleRate, 0);
  77 +
  78 + // 设置输出参数
  79 + //av_opt_set_int(ResampleInfo->m_swr, "out_channel_layout", ResampleInfo->Out_CHLayout, 0);
  80 + av_opt_set_chlayout(ResampleInfo->m_swr, "out_chlayout", &ResampleInfo->Out_CHLayout, 0);
  81 + av_opt_set_sample_fmt(ResampleInfo->m_swr, "out_sample_fmt", ResampleInfo->Out_Format, 0);
  82 + av_opt_set_int(ResampleInfo->m_swr, "out_sample_rate", ResampleInfo->Out_SampleRate, 0);
  83 +
  84 + // 初始化重采样上下文
  85 + if (swr_init(ResampleInfo->m_swr) < 0) {
  86 + printf("Failed to initialize the resampling context\n");
  87 + swr_free(&ResampleInfo->m_swr); // 释放已分配的 SwrContext
  88 + ResampleInfo->m_swr = NULL;
  89 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  90 + }
  91 +
  92 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  93 +}
  94 +
  95 +//重置重编码器
  96 +T_JZsdkReturnCode FF_Resample_Reset(struct AudioDealInfo *AD_Info,
  97 + unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format)
  98 +{
  99 +
  100 + int ResetFlag = JZ_FLAGCODE_OFF;
  101 +
  102 + //对比编码器新设置与当前设置是否相同
  103 + int ret = av_channel_layout_compare(&in_ch_layout, &AD_Info->Raw_ChannelLayout);
  104 + if (ret != 0)
  105 + {
  106 + ResetFlag = JZ_FLAGCODE_ON;
  107 + }
  108 +
  109 + if (in_sampleRate != AD_Info->Raw_SampleRate)
  110 + {
  111 + ResetFlag = JZ_FLAGCODE_ON;
  112 + }
  113 +
  114 + if (in_format != AD_Info->Raw_SampleFormat)
  115 + {
  116 + ResetFlag = JZ_FLAGCODE_ON;
  117 + }
  118 +
  119 + if (ResetFlag == JZ_FLAGCODE_OFF)
  120 + {
  121 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  122 + }
  123 +
  124 + struct pcm_Resample *ResampleInfo = (struct pcm_Resample *)AD_Info->ResampleInfo;
  125 +
  126 + ret = 0;
  127 + if(ResampleInfo->m_swr != NULL)
  128 + {
  129 + swr_free(&ResampleInfo->m_swr);
  130 + ResampleInfo->m_swr = NULL;
  131 + }
  132 +
  133 + return FF_Resample_Init(AD_Info, in_sampleRate, in_ch_layout, in_format);
  134 +}
  135 +
  136 +
  137 +void *FF_Resample_Send_And_Get_ResampleData(struct AudioDealInfo *AD_Info, unsigned char **src, int nb_samples, int *out_nb_samples)
  138 +{
  139 + int ret = 0;
  140 + unsigned char *dst = NULL;
  141 +
  142 + struct pcm_Resample *ResampleInfo = (struct pcm_Resample *)AD_Info->ResampleInfo;
  143 +
  144 + *out_nb_samples = av_rescale_rnd(nb_samples, ResampleInfo->Out_SampleRate, ResampleInfo->In_SampleRate, AV_ROUND_UP);
  145 + //printf("重采样得到的预计输出样本数量为%d\n",*out_nb_samples);
  146 + ret = av_samples_alloc(&dst, NULL, 2, *out_nb_samples, AV_SAMPLE_FMT_S16, 0);
  147 + if (ret < 0) {
  148 + printf("[ERROR][Resample]av_samples_alloc failed\n");
  149 + return NULL;
  150 + }
  151 +
  152 + ret = swr_convert(ResampleInfo->m_swr, &dst, *out_nb_samples, (const uint8_t**)src, nb_samples);
  153 + if (ret < 0) {
  154 + printf("[ERROR][Resample]swr_convert failed\n");
  155 + av_freep(&dst); // 释放之前分配的内存
  156 + return NULL;
  157 + }
  158 +
  159 + *out_nb_samples = ret;
  160 +
  161 + return (void*)dst;
  162 +}
  163 +
  164 +//用于释放掉,执行重采样后得到的新数据的内存
  165 +T_JZsdkReturnCode FF_Resample_freeReasmpleData(void *dst)
  166 +{
  167 + if (dst != NULL)
  168 + {
  169 + av_free(dst);
  170 + dst = NULL;
  171 + }
  172 +
  173 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  174 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file pcm_Resample.h
  4 + * AudioDeal的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef PCM_REAMPLE_H
  11 +#define PCM_REAMPLE_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "AudioDeal/FF_Statement.h"
  16 +#include "AudioDeal/AudioDeal.h"
  17 +
  18 +#ifdef __cplusplus
  19 +extern "C" {
  20 +#endif
  21 +
  22 +/* Exported constants --------------------------------------------------------*/
  23 +/* 常亮定义*/
  24 +
  25 +/* Exported types ------------------------------------------------------------*/
  26 +
  27 +/* Exported functions --------------------------------------------------------*/
  28 +T_JZsdkReturnCode FF_Resample_Init(struct AudioDealInfo *AD_Info,
  29 + unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format);
  30 +T_JZsdkReturnCode FF_Resample_Reset(struct AudioDealInfo *AD_Info,
  31 + unsigned int in_sampleRate, AVChannelLayout in_ch_layout, enum AVSampleFormat in_format);
  32 +
  33 +void *FF_Resample_Send_And_Get_ResampleData(struct AudioDealInfo *AD_Info, unsigned char **src, int nb_samples, int *out_nb_samples);
  34 +
  35 +
  36 +T_JZsdkReturnCode FF_Resample_freeReasmpleData(void *dst);
  37 +
  38 +
  39 +#ifdef __cplusplus
  40 +}
  41 +#endif
  42 +
  43 +#endif
  1 +jzsdk的音频处理模块
  2 +用于处理底层的音频流,通常搭配如tts模块等使用
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include "JZsdkLib.h"
  4 +#include "BaseConfig.h"
  5 +
  6 +#include "./DeviceMessage/DeviceMessage.h"
  7 +#include "DeviceInfo/DeviceInfo.h"
  8 +#include "DeviceInfo/HardwareInfo/HardwareInfo.h"
  9 +
  10 +typedef struct DeviceInfo
  11 +{
  12 + unsigned int Port;
  13 + unsigned int DeviceID;
  14 + unsigned char SerialNumber[30];
  15 + unsigned int MajorVersion;
  16 + unsigned int MinjorVersion;
  17 + unsigned int ModifyVersion;
  18 + unsigned int DebugVersion;
  19 +}DeviceInfo;
  20 +
  21 +static struct DeviceInfo g_DeviceID[6];
  22 +int g_DeviceIDNum = 0; //设备总数
  23 +
  24 +/***************************************************
  25 + *
  26 + * 设备信息初始化
  27 + * 暂时无用
  28 + *
  29 + * *************************************************/
  30 +T_JZsdkReturnCode DeviceInfo_Init()
  31 +{
  32 + //硬件版本号初始化
  33 + HardwareInfo_Init();
  34 +
  35 + //序列号模块初始化
  36 + SerialMAT_Init();
  37 +
  38 + //设备打印信息模块初始化
  39 + DeviceMessage_Init();
  40 +
  41 + //设备本地设备名字
  42 + DeviceInfo_SetDeviceName(DEVICE_PSDK, DEVICE_VERSION);
  43 +
  44 + //设置本地序列号
  45 + unsigned char SerialNumber[30];
  46 + int SerialNumberLen = 0;
  47 + SerialMAT_Get_SerialNumber(SerialNumber, &SerialNumberLen);
  48 + DeviceInfo_SetAllSerialNumber(DEVICE_VERSION, SerialNumber, SerialNumberLen);
  49 +
  50 + //设置本地的版本号
  51 + DeviceInfo_SetAllVerision(DEVICE_PSDK, MAJOR_VERSION, MINOR_VERSION, MODIFY_VERSION, DEBUG_VERSION);
  52 +
  53 + JZSDK_LOG_INFO("MODULE_DEVICE_INFO_INIT_COMPLETE");
  54 +}
  55 +
  56 +
  57 +/************************
  58 + *
  59 + * 设置设备名
  60 + *
  61 + * *****************/
  62 +T_JZsdkReturnCode DeviceInfo_SetDeviceName(unsigned int Port, int DeviceID)
  63 +{
  64 + //检查一遍是否存在
  65 + for (int i = 0; i < g_DeviceIDNum; i++)
  66 + {
  67 + if (g_DeviceID[i].Port == Port)
  68 + {
  69 + g_DeviceID[i].Port = Port;
  70 + g_DeviceID[i].DeviceID = DeviceID;
  71 + printf("接口0x%x, id:0x%x\n",Port,DeviceID);
  72 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  73 + }
  74 + }
  75 +
  76 + //不存在则写入新的
  77 + g_DeviceID[g_DeviceIDNum].Port = Port;
  78 + g_DeviceID[g_DeviceIDNum].DeviceID = DeviceID;
  79 + g_DeviceIDNum++;
  80 + if (g_DeviceIDNum == 6)
  81 + {
  82 + JZSDK_LOG_ERROR("错误,写入了超过5个设备");
  83 + g_DeviceIDNum = 5;
  84 + }
  85 +
  86 + JZSDK_LOG_INFO("接入新设备0x%x, 0x%x",Port,DeviceID);
  87 +
  88 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  89 +}
  90 +
  91 +/************************
  92 + *
  93 + * 查询设备名
  94 + *
  95 + * *****************/
  96 +T_JZsdkReturnCode DeviceInfo_GetDeviceName(unsigned int Port, int *DeviceID)
  97 +{
  98 + if (DeviceID == NULL)
  99 + {
  100 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  101 + }
  102 +
  103 + //检查一遍是否存在
  104 + for (int i = 0; i < g_DeviceIDNum; i++)
  105 + {
  106 + if (g_DeviceID[i].Port == Port)
  107 + {
  108 + *DeviceID = g_DeviceID[i].DeviceID;
  109 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  110 + }
  111 + }
  112 +
  113 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  114 +}
  115 +
  116 +
  117 +/************************
  118 + *
  119 + * 查询所有设备名
  120 + *
  121 + * *****************/
  122 +T_JZsdkReturnCode DeviceInfo_GetAllDeviceName(unsigned int *DeviceNum, unsigned int DeviceID[][2])
  123 +{
  124 + if (DeviceNum == NULL || DeviceID == NULL)
  125 + {
  126 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  127 + }
  128 +
  129 + *DeviceNum = g_DeviceIDNum;
  130 + if (*DeviceNum == 0)
  131 + {
  132 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  133 + }
  134 +
  135 + //检查一遍是否存在
  136 + for (int i = 0; i < g_DeviceIDNum; i++)
  137 + {
  138 + DeviceID[i][0] = g_DeviceID[i].Port;
  139 + DeviceID[i][1] = g_DeviceID[i].DeviceID;
  140 + }
  141 +
  142 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  143 +}
  144 +
  145 +
  146 +/************************
  147 + *
  148 + * 设置所有序列号
  149 + *
  150 + * *****************/
  151 +T_JZsdkReturnCode DeviceInfo_SetAllSerialNumber(unsigned int DeviceId, unsigned char *SerialNumber, unsigned int SerialNumberLen)
  152 +{
  153 +
  154 + //检查一遍设备是否存在
  155 + for (int i = 0; i < g_DeviceIDNum; i++)
  156 + {
  157 + if (g_DeviceID[i].DeviceID == DeviceId)
  158 + {
  159 + memcpy(g_DeviceID[i].SerialNumber, SerialNumber, SerialNumberLen);
  160 + JZSDK_LOG_INFO("序列号0x%x %s",g_DeviceID[i].SerialNumber, g_DeviceID[i].SerialNumber);
  161 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  162 + }
  163 + }
  164 +
  165 + JZSDK_LOG_ERROR("设置所有序列号失败");
  166 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  167 +}
  168 +
  169 +
  170 +/************************
  171 + *
  172 + * 返回设备序列号
  173 + *
  174 + * *****************/
  175 +T_JZsdkReturnCode DeviceInfo_GetAllSerialNumber(unsigned int DeviceId, unsigned char *SerialNumber, unsigned int *SerialNumberLen)
  176 +{
  177 +
  178 + //检查一遍设备是否存在
  179 + for (int i = 0; i < g_DeviceIDNum; i++)
  180 + {
  181 + if (g_DeviceID[i].DeviceID == DeviceId)
  182 + {
  183 + *SerialNumberLen = strlen(g_DeviceID[i].SerialNumber);
  184 + memcpy(SerialNumber, g_DeviceID[i].SerialNumber, *SerialNumberLen);
  185 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  186 + }
  187 + }
  188 +
  189 + JZSDK_LOG_ERROR("该设备未登记");
  190 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  191 +}
  192 +
  193 +
  194 +
  195 +
  196 +/************************
  197 + *
  198 + * 设置所有版本号
  199 + *
  200 + * *****************/
  201 +T_JZsdkReturnCode DeviceInfo_SetAllVerision(unsigned int DeviceId, unsigned int MajorVersion, unsigned int MinjorVersion, unsigned int ModifyVersion, unsigned int DebugVersion)
  202 +{
  203 + //检查一遍设备是否存在
  204 + for (int i = 0; i < g_DeviceIDNum; i++)
  205 + {
  206 + if (g_DeviceID[i].DeviceID == DeviceId)
  207 + {
  208 + g_DeviceID[i].MajorVersion = MajorVersion;
  209 + g_DeviceID[i].MinjorVersion = MinjorVersion;
  210 + g_DeviceID[i].ModifyVersion = ModifyVersion;
  211 + g_DeviceID[i].DebugVersion = DebugVersion;
  212 +
  213 + JZSDK_LOG_INFO("设置版本号 0x%x %d %d %d %d",g_DeviceID[i].SerialNumber,
  214 + g_DeviceID[i].MajorVersion,
  215 + g_DeviceID[i].MinjorVersion,
  216 + g_DeviceID[i].ModifyVersion,
  217 + g_DeviceID[i].DebugVersion
  218 + );
  219 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  220 + }
  221 + }
  222 +
  223 + JZSDK_LOG_ERROR("设置失败,该设备未登记");
  224 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  225 +}
  226 +
  227 +
  228 +/************************
  229 + *
  230 + * 返回设备版本号
  231 + *
  232 + * *****************/
  233 +T_JZsdkReturnCode DeviceInfo_GetAllVerision(unsigned int DeviceId, unsigned int *MajorVersion, unsigned int *MinjorVersion, unsigned int *ModifyVersion, unsigned int *DebugVersion)
  234 +{
  235 + //检查一遍设备是否存在
  236 + for (int i = 0; i < g_DeviceIDNum; i++)
  237 + {
  238 + if (g_DeviceID[i].DeviceID == DeviceId)
  239 + {
  240 + *MajorVersion = g_DeviceID[i].MajorVersion;
  241 + *MinjorVersion = g_DeviceID[i].MinjorVersion;
  242 + *ModifyVersion = g_DeviceID[i].ModifyVersion;
  243 + *DebugVersion = g_DeviceID[i].DebugVersion;
  244 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  245 + }
  246 + }
  247 +
  248 + JZSDK_LOG_ERROR("返回失败,该设备未登记");
  249 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  250 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file DeviceInfo.h
  4 + * DeviceInfo.h的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef DEVICE_INFO_H
  11 +#define DEVICE_INFO_H
  12 +
  13 +#include "DeviceInfo/DeviceMessage/DeviceMessage.h"
  14 +#include "SerialNumberProc/SerialProc.h"
  15 +#include "DeviceInfo/HardwareInfo/HardwareInfo.h"
  16 +
  17 +/* Includes ------------------------------------------------------------------*/
  18 +#ifdef __cplusplus
  19 +extern "C" {
  20 +#endif
  21 +
  22 +
  23 +
  24 +/* Exported constants --------------------------------------------------------*/
  25 +/* 常亮定义*/
  26 +
  27 +/* Exported types ------------------------------------------------------------*/
  28 +T_JZsdkReturnCode DeviceInfo_Init();
  29 +T_JZsdkReturnCode DeviceInfo_SetAllVerision(unsigned int DeviceId, unsigned int MajorVersion, unsigned int MinjorVersion, unsigned int ModifyVersion, unsigned int DebugVersion);
  30 +T_JZsdkReturnCode DeviceInfo_GetAllVerision(unsigned int DeviceId, unsigned int *MajorVersion, unsigned int *MinjorVersion, unsigned int *ModifyVersion, unsigned int *DebugVersion);
  31 +
  32 +T_JZsdkReturnCode DeviceInfo_GetAllDeviceName(unsigned int *DeviceNum, unsigned int DeviceID[][2]);
  33 +T_JZsdkReturnCode DeviceInfo_SetDeviceName(unsigned int Port, int DeviceID);
  34 +
  35 +T_JZsdkReturnCode DeviceInfo_SetAllSerialNumber(unsigned int DeviceId, unsigned char *SerialNumber, unsigned int SerialNumberLen);
  36 +T_JZsdkReturnCode DeviceInfo_GetAllSerialNumber(unsigned int DeviceId, unsigned char *SerialNumber, unsigned int *SerialNumberLen);
  37 +
  38 +#ifdef __cplusplus
  39 +}
  40 +#endif
  41 +
  42 +#endif
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <string.h>
  4 +
  5 +#include "JZsdkLib.h"
  6 +#include "./DeviceMessage.h"
  7 +#include "version_choose.h"
  8 +#include "BaseConfig.h"
  9 +#include "../SerialNumberProc/SerialProc.h"
  10 +#include "Gimbal/Gimbal_InAndOut.h"
  11 +#include "UI_control/Psdk_UI_io.h"
  12 +
  13 +#if MEGAPHONE_CONFIG_STATUS == VERSION_SWITCH_ON
  14 + #include "Megaphone/Megaphone.h"
  15 + #include "Megaphone/MegTempControl/MegTempControl.h"
  16 +#endif
  17 +
  18 +#include "Lighting/Lighting_InAndOut.h"
  19 +
  20 +#define MESSAGE_MAX_LEN 1024
  21 +
  22 +static DeviceMessageMode g_DeviceMessageMode = DEVICE_MESSAGE_DEFAULT;
  23 +static unsigned char DeviceMessage[MESSAGE_MAX_LEN] = "默认打印信息\n"; //设备信息最多显示512长度
  24 +static DeviceMessageLanguage g_MessageLanguage = DEVICE_MESSAGE_CHINESE;
  25 +
  26 +int Widget_RealTimeOpusFlag = JZ_FLAGCODE_OFF; //用于标志ui里的实时语音开关
  27 +
  28 +#if IRC_CONFIG_STATUS == VERSION_SWITCH_ON
  29 +extern int g_temp_GasValueMax;
  30 +extern int g_temp_GasValueMin;
  31 +#endif
  32 +
  33 +
  34 +/***************************************************
  35 + *
  36 + * 设备信息初始化
  37 + * 暂时无用
  38 + *
  39 + * *************************************************/
  40 +T_JZsdkReturnCode DeviceMessage_Init()
  41 +{
  42 +
  43 +}
  44 +
  45 +/***************************************************
  46 + *
  47 + * 是否可用信息录入
  48 + * 如果设备不可用,直接返回错误,从而不录入其他信息
  49 + *
  50 + * *************************************************/
  51 +static T_JZsdkReturnCode DeviceMessage_Enter_UsegePermissions(unsigned char *message)
  52 +{
  53 + unsigned char old_message[MESSAGE_MAX_LEN];
  54 + unsigned char new_message[MESSAGE_MAX_LEN];
  55 + memset(message ,0, MESSAGE_MAX_LEN);
  56 + memset(old_message, 0, MESSAGE_MAX_LEN);
  57 + memset(new_message, 0, MESSAGE_MAX_LEN);
  58 +
  59 + unsigned char Jz_SerialNumber[256];
  60 + int Jz_SerialNumberLenth;
  61 + SerialMAT_Get_SerialNumber(Jz_SerialNumber, &Jz_SerialNumberLenth);
  62 +
  63 + //判断是否能用
  64 + int UseFlag = Main_Device_Wheather_Use();
  65 + if(UseFlag== 1)
  66 + {
  67 + //判断为未激活
  68 + //显示未激活
  69 + memset(new_message,0,sizeof(new_message));
  70 + memset(old_message,0,sizeof(old_message));
  71 + memset(message,0,sizeof(message));
  72 +
  73 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  74 + {
  75 + snprintf(new_message,MESSAGE_MAX_LEN,"提示:未激活,功能不可用。\n请进入微信小程序“极至服务”\n输入设备SN码:%s\n获取激活码激活设备。\n",Jz_SerialNumber);
  76 + }
  77 + else
  78 + {
  79 + snprintf(new_message,MESSAGE_MAX_LEN,"Reminder: Not activated, function not available.\nPlease enter https://jzdrones.com/\nInput device SN code: %s\nObtain an activation code to activate the device.\n",Jz_SerialNumber);
  80 + }
  81 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  82 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  83 +
  84 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  85 + }
  86 +
  87 + else if (UseFlag == 2)
  88 + {
  89 + //判断为海外版
  90 + //显示为海外版不可用
  91 + memset(new_message,0,sizeof(new_message));
  92 + memset(old_message,0,sizeof(old_message));
  93 + memset(message,0,sizeof(message));
  94 +
  95 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  96 + {
  97 + snprintf(new_message,MESSAGE_MAX_LEN,"海外版无法在中国境内使用\n");
  98 + }
  99 + else
  100 + {
  101 + snprintf(new_message,MESSAGE_MAX_LEN,"The international version cannot be used in China\n");
  102 + }
  103 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  104 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  105 +
  106 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  107 + }
  108 +
  109 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  110 +}
  111 +
  112 +/***************************************************
  113 + *
  114 + * 设备信息录入
  115 + *
  116 + *
  117 + * *************************************************/
  118 +static T_JZsdkReturnCode DeviceMessage_Enter_Default(unsigned char *message)
  119 +{
  120 + unsigned char old_message[MESSAGE_MAX_LEN];
  121 + unsigned char new_message[MESSAGE_MAX_LEN];
  122 + memset(message ,0, MESSAGE_MAX_LEN);
  123 + memset(old_message, 0, MESSAGE_MAX_LEN);
  124 + memset(new_message, 0, MESSAGE_MAX_LEN);
  125 +
  126 +#if MEGAPHONE_CONFIG_STATUS == VERSION_SWITCH_ON
  127 + //如果实时语音按键打开了
  128 + if (Widget_RealTimeOpusFlag == JZ_FLAGCODE_ON)
  129 + {
  130 + memset(message ,0, MESSAGE_MAX_LEN);
  131 + memset(old_message, 0, MESSAGE_MAX_LEN);
  132 + memset(new_message, 0, MESSAGE_MAX_LEN);
  133 +
  134 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  135 + {
  136 + snprintf(new_message,MESSAGE_MAX_LEN,"已开启实时语音\n禁止其他音频操作\n");
  137 + }
  138 + else
  139 + {
  140 + snprintf(new_message,MESSAGE_MAX_LEN,"Real-time Intercom has been enabled.\nOther audio operations are prohibited\n");
  141 + }
  142 +
  143 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  144 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  145 +
  146 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  147 + }
  148 + //正常获取歌曲列表
  149 + else
  150 + {
  151 + //获取音频信息 用于显示歌曲列表
  152 + memset(old_message, 0, MESSAGE_MAX_LEN);
  153 + memset(new_message, 0, MESSAGE_MAX_LEN);
  154 + if(Megaphone_GetMusicListMessage(new_message) != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  155 + {
  156 + memset(message, 0, sizeof(message));
  157 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  158 + {
  159 + snprintf(new_message,MESSAGE_MAX_LEN,"无歌曲\n",sizeof("无歌曲\n"));
  160 + }
  161 + else
  162 + {
  163 + snprintf(new_message,MESSAGE_MAX_LEN,"No Songs\n", sizeof("No Songs"));
  164 + }
  165 + }
  166 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  167 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  168 + //JZSDK_LOG_INFO("获取的信息%s",message);
  169 + }
  170 +#endif
  171 +
  172 +#if IRC_CONFIG_STATUS == VERSION_SWITCH_ON
  173 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  174 + {
  175 + memset(new_message,0,sizeof(new_message));
  176 + memset(old_message,0,sizeof(old_message));
  177 + snprintf(new_message,MESSAGE_MAX_LEN,"气体阈值\n上限:%d\n下限:%d\n",g_temp_GasValueMax , g_temp_GasValueMin);
  178 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  179 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  180 +
  181 + }
  182 + else
  183 + {
  184 + memset(new_message,0,sizeof(new_message));
  185 + memset(old_message,0,sizeof(old_message));
  186 + snprintf(new_message,MESSAGE_MAX_LEN,"Gas Threshold\nUpper Limit: %d\nLower Limit: %d\n",g_temp_GasValueMax , g_temp_GasValueMin);
  187 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  188 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  189 + }
  190 +#endif
  191 +
  192 + //如果为组合版
  193 +#if (DEVICE_VERSION == TF_A1 || DEVICE_VERSION == JZ_U3 || DEVICE_VERSION == JZ_U3S ||DEVICE_VERSION == JZ_U3D)
  194 +
  195 + // //组合板写入
  196 + // snprintf(new_message,128,"光斑y值%d\n",Widget_GetCenter_ValueY());
  197 + // snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  198 + // snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  199 +
  200 + static int SearchLight_LeftTemperature = 0;
  201 + static int SearchLight_RightTemperature = 0;
  202 +
  203 + //获取温度
  204 + Lighting_Get_SearchLightTemperature(&SearchLight_LeftTemperature, &SearchLight_RightTemperature);
  205 +
  206 + memset(new_message,0,sizeof(new_message));
  207 + memset(old_message,0,sizeof(old_message));
  208 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  209 + {
  210 + snprintf(new_message,MESSAGE_MAX_LEN,"左灯温度:%d\n",SearchLight_LeftTemperature);
  211 + }
  212 + else
  213 + {
  214 + snprintf(new_message,MESSAGE_MAX_LEN,"SearchLight-LeftTemp:%d\n",SearchLight_LeftTemperature);
  215 + }
  216 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  217 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  218 +
  219 + memset(new_message,0,sizeof(new_message));
  220 + memset(old_message,0,sizeof(old_message));
  221 + if (g_MessageLanguage == DEVICE_MESSAGE_CHINESE)
  222 + {
  223 + snprintf(new_message,MESSAGE_MAX_LEN,"右灯温度:%d\n",SearchLight_RightTemperature);
  224 + }
  225 + else
  226 + {
  227 + snprintf(new_message,MESSAGE_MAX_LEN,"SearchLight-RightTemp:%d\n",SearchLight_RightTemperature);
  228 + }
  229 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  230 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  231 +
  232 +#endif
  233 +
  234 +}
  235 +
  236 +
  237 +/***************************************************
  238 + *
  239 + * 设备信息录入
  240 + *
  241 + *
  242 + * *************************************************/
  243 +static T_JZsdkReturnCode DeviceMessage_Enter_Debug(unsigned char *message)
  244 +{
  245 + unsigned char old_message[MESSAGE_MAX_LEN];
  246 + unsigned char new_message[MESSAGE_MAX_LEN];
  247 + memset(message ,0, MESSAGE_MAX_LEN);
  248 + memset(old_message, 0, MESSAGE_MAX_LEN);
  249 + memset(new_message, 0, MESSAGE_MAX_LEN);
  250 +
  251 + snprintf(new_message,MESSAGE_MAX_LEN,"调试界面\n");
  252 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  253 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  254 +
  255 + //如果拥有云台,将会显示云台微调值
  256 + if (Get_JZsdk_GimbalStatusFlag() == 1)
  257 + {
  258 + memset(new_message,0,sizeof(new_message));
  259 + memset(old_message,0,sizeof(old_message));
  260 + snprintf(new_message,MESSAGE_MAX_LEN,"云台值%d\n云台微调值%d\n实际云台值%d\n",
  261 + JZsdk_Psdk_UI_io_Get_PitchAngle(),
  262 + JZsdk_Psdk_UI_io_Get_PitchFineTuninge(),
  263 + JZsdk_Psdk_UI_io_Get_PitchRealPitchAngle());
  264 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  265 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  266 + }
  267 +
  268 +
  269 +#if MEGAPHONE_CONFIG_STATUS == VERSION_SWITCH_ON
  270 + memset(new_message,0,sizeof(new_message));
  271 + memset(old_message,0,sizeof(old_message));
  272 + snprintf(new_message,MESSAGE_MAX_LEN,"rtv:%d rsv:%d",Megaphone_get_RealTargetVolume(), Megaphone_get_RealSetVolume());
  273 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  274 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  275 +
  276 + //如果温控值不为0
  277 + double Megtemp = MegTempControl_GetTemp();
  278 + if (Megtemp != 0)
  279 + {
  280 + memset(new_message,0,sizeof(new_message));
  281 + memset(old_message,0,sizeof(old_message));
  282 + snprintf(new_message,MESSAGE_MAX_LEN,"喊话器温度%f\n",Megtemp);
  283 + snprintf(old_message,MESSAGE_MAX_LEN,"%s",message);
  284 + snprintf(message,MESSAGE_MAX_LEN,"%s%s",old_message,new_message);
  285 + }
  286 +#endif
  287 +}
  288 +
  289 +
  290 +/***************************************************
  291 + *
  292 + * 用于获取设备信息的函数入口
  293 + *
  294 + *
  295 + * *************************************************/
  296 +T_JZsdkReturnCode DeviceMessage_GetMessage(unsigned char *str, int MaxReadLen)
  297 +{
  298 + T_JZsdkReturnCode ret;
  299 + memset(DeviceMessage,0,sizeof(DeviceMessage));
  300 +
  301 + if (MaxReadLen >= sizeof(DeviceMessage))
  302 + {
  303 + MaxReadLen = sizeof(DeviceMessage);
  304 + }
  305 +
  306 + //接下来信息很重要,首先获取消息吧!
  307 + if (g_DeviceMessageMode == DEVICE_MESSAGE_ALL)
  308 + {
  309 + JZSDK_LOG_INFO("查获所有信息");
  310 + }
  311 + else
  312 + {
  313 + ret = DeviceMessage_Enter_UsegePermissions(DeviceMessage);
  314 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  315 + {
  316 + memcpy(str, DeviceMessage, MaxReadLen);
  317 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  318 + }
  319 +
  320 + if (g_DeviceMessageMode == DEVICE_MESSAGE_DEFAULT)
  321 + {
  322 + DeviceMessage_Enter_Default(DeviceMessage);
  323 + memcpy(str, DeviceMessage, MaxReadLen);
  324 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  325 + }
  326 + else if (g_DeviceMessageMode == DEVICE_MESSAGE_DEBUG)
  327 + {
  328 + DeviceMessage_Enter_Debug(DeviceMessage);
  329 + memcpy(str, DeviceMessage, MaxReadLen);
  330 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  331 + }
  332 + else
  333 + {
  334 + printf("获取设备信息失败\n");
  335 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  336 + }
  337 + }
  338 +
  339 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  340 +}
  341 +
  342 +/*****************************
  343 + *
  344 + * 设置信息模式
  345 + *
  346 + *
  347 + * **************************/
  348 +T_JZsdkReturnCode DeviceMessage_Mode(DeviceMessageMode mode)
  349 +{
  350 + if (mode < 0 || mode > 2)
  351 + {
  352 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  353 + }
  354 +
  355 + if (mode == g_DeviceMessageMode)
  356 + {
  357 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  358 + }
  359 +
  360 + g_DeviceMessageMode = mode;
  361 +
  362 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  363 +}
  364 +
  365 +/*****************************
  366 + *
  367 + * 设置设备信息的语言
  368 + *
  369 + *
  370 + * **************************/
  371 +T_JZsdkReturnCode DeviceMessage_SetMessageLanguage(DeviceMessageLanguage value)
  372 +{
  373 + if (value < 0 || value > 1)
  374 + {
  375 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  376 + }
  377 +
  378 + if (value == g_MessageLanguage)
  379 + {
  380 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  381 + }
  382 +
  383 + g_MessageLanguage = value;
  384 +
  385 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  386 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file DeviceMessage.h
  4 + * DeviceMessage.h 的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef DEVICE_MESSAGE_H
  11 +#define DEVICE_MESSAGE_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +
  23 +/* Exported types ------------------------------------------------------------*/
  24 +typedef enum DeviceMessageMode
  25 +{
  26 + DEVICE_MESSAGE_DEFAULT = 0x0000, //默认打印信息
  27 + DEVICE_MESSAGE_DEBUG = 0x0001, //调试模式下打印的信息
  28 + DEVICE_MESSAGE_ALL = 0x0002, //全内容打印,用于串口调试使用
  29 +}DeviceMessageMode;
  30 +
  31 +typedef enum DeviceMessageLanguage
  32 +{
  33 + DEVICE_MESSAGE_CHINESE = 0x0000, //中文
  34 + DEVICE_MESSAGE_ENGLISH = 0x0001, //英文
  35 +}DeviceMessageLanguage;
  36 +
  37 +/* Exported functions --------------------------------------------------------*/
  38 +T_JZsdkReturnCode DeviceMessage_GetMessage(unsigned char *str, int MaxReadLen);
  39 +T_JZsdkReturnCode DeviceMessage_Mode(DeviceMessageMode mode);
  40 +T_JZsdkReturnCode DeviceMessage_SetMessageLanguage(DeviceMessageLanguage value);
  41 +T_JZsdkReturnCode DeviceMessage_Init();
  42 +
  43 +
  44 +#ifdef __cplusplus
  45 +}
  46 +#endif
  47 +
  48 +#endif
  1 +#include <stdio.h>
  2 +#include "JZsdkLib.h"
  3 +
  4 +#define HARDWARE_DIR "/root/HardwareIdeNum"
  5 +#define MAX_LINE_LENGTH 100
  6 +
  7 +static int HardwareInfo_MajorVersion = 0;
  8 +static int HardwareInfo_MinorVersion = 0;
  9 +static int HardwareInfo_ModifyVersion = 0;
  10 +static int HardwareInfo_DebugVersion = 0;
  11 +
  12 +/***********************
  13 + * 获取硬件版本号
  14 + * major 0
  15 + * minor 1
  16 + * modiy 2
  17 + * debug 3
  18 + * ************/
  19 +T_JZsdkReturnCode HardwareInfo_GetVersion(int flag)
  20 +{
  21 + unsigned int version = 0;
  22 +
  23 + switch (flag)
  24 + {
  25 + case 0:
  26 + version = HardwareInfo_MajorVersion;
  27 + break;
  28 +
  29 + case 1:
  30 + version = HardwareInfo_MinorVersion;
  31 + break;
  32 +
  33 + case 2:
  34 + version = HardwareInfo_ModifyVersion;
  35 + break;
  36 +
  37 + case 3:
  38 + version = HardwareInfo_DebugVersion;
  39 + break;
  40 +
  41 + default:
  42 + break;
  43 + }
  44 +
  45 + return version;
  46 +}
  47 +
  48 +T_JZsdkReturnCode HardwareInfo_Init()
  49 +{
  50 + T_JZsdkReturnCode ret;
  51 + ret = JZsdk_check_file_exists(HARDWARE_DIR);
  52 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  53 + {
  54 + JZSDK_LOG_ERROR("无硬件版本号");
  55 + return ret;
  56 + }
  57 +
  58 + //加载硬件版本号
  59 + FILE *file;
  60 + char buffer[MAX_LINE_LENGTH];
  61 + int num1, num2, num3, num4;
  62 + char str[MAX_LINE_LENGTH];
  63 +
  64 + // 打开文件
  65 + file = fopen(HARDWARE_DIR, "r");
  66 + if (file == NULL) {
  67 + JZSDK_LOG_ERROR("Error opening file");
  68 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  69 + }
  70 +
  71 + // 读取前四行到整数变量
  72 + if (fscanf(file, "%d", &num1) != 1) {
  73 + JZSDK_LOG_ERROR("Error reading hardware number 1");
  74 + fclose(file);
  75 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  76 + }
  77 + if (fscanf(file, "%d", &num2) != 1) {
  78 + JZSDK_LOG_ERROR("Error reading hardware number 2");
  79 + fclose(file);
  80 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  81 + }
  82 + if (fscanf(file, "%d", &num3) != 1) {
  83 + JZSDK_LOG_ERROR("Error reading hardware number 3");
  84 + fclose(file);
  85 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  86 + }
  87 + if (fscanf(file, "%d", &num4) != 1) {
  88 + JZSDK_LOG_ERROR("Error reading hardware number 4");
  89 + fclose(file);
  90 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  91 + }
  92 +
  93 + // 读取第五行到字符串变量
  94 + // 清除之前的换行符
  95 + fgetc(file); // 读取并忽略换行符
  96 + if (fgets(str, MAX_LINE_LENGTH, file) == NULL)
  97 + {
  98 + JZSDK_LOG_ERROR("Error reading string");
  99 + fclose(file);
  100 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  101 + }
  102 +
  103 + // 去除字符串末尾的换行符(如果有)
  104 + str[strcspn(str, "\n")] = 0;
  105 +
  106 + HardwareInfo_MajorVersion = num1;
  107 + HardwareInfo_MinorVersion = num2;
  108 + HardwareInfo_ModifyVersion = num3;
  109 + HardwareInfo_DebugVersion = num4;
  110 +
  111 + // 输出结果
  112 + JZSDK_LOG_INFO("Hardware Version: %d, %d, %d, %d\n", HardwareInfo_MajorVersion, HardwareInfo_MinorVersion, HardwareInfo_ModifyVersion, HardwareInfo_DebugVersion);
  113 + JZSDK_LOG_INFO("Hardware Info: %s\n", str);
  114 +
  115 + // 关闭文件
  116 + fclose(file);
  117 +
  118 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  119 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file HardwareInfo.h
  4 + * HardwareInfo.h 的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef HARDWARE_INFO_H
  11 +#define HARDWARE_INFO_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +
  23 +/* Exported types ------------------------------------------------------------*/
  24 +
  25 +/* Exported functions --------------------------------------------------------*/
  26 +T_JZsdkReturnCode HardwareInfo_Init();
  27 +T_JZsdkReturnCode HardwareInfo_GetVersion(int flag);
  28 +
  29 +
  30 +#ifdef __cplusplus
  31 +}
  32 +#endif
  33 +
  34 +#endif
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdlib.h>
  4 +
  5 +#include "./ActivateMAT.h"
  6 +#include "JZsdkLib.h"
  7 +#include "JZsdk_Base/JZsdk_Code/JZsdk_SMT_Code.h"
  8 +#include "./version_choose.h"
  9 +
  10 +static T_JZsdkReturnCode ActivateMAT_create_SN_LOCK_file() ;
  11 +static T_JZsdkReturnCode ActivateMAT_delete_SN_LOCK_file() ;
  12 +static T_JZsdkReturnCode ActivateMAT_check_SN_LOCK_exists() ;
  13 +static T_JZsdkReturnCode ActivateMAT_UnLockDevice(char *data, unsigned long *deac_code);
  14 +static T_JZsdkReturnCode ActivateMAT_LockDevice(char *data, unsigned long *ac_code);
  15 +static T_JZsdkReturnCode ActivateMAT_ActivateAndUnActivate(int flag, unsigned char *SerialNumber,unsigned char *ActivationCode);
  16 +
  17 +
  18 +
  19 +#define SN_LOCL_FILE_PATH "/root/SN_LOCK"
  20 +#define CRC32_POLYNOMIAL_UNLOCK 0xEDB88320L //激活解锁
  21 +#define CRC32_POLYNOMIAL_LOCK 0xEDB88321L //反激活锁住
  22 +
  23 +/**********************************
  24 + *
  25 + * 判断是否需要激活/反激活,且进行激活或者反激活
  26 + * 输入 激活状态 序列号 激活码 激活码长度
  27 + * 失败 返回JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE
  28 + * 有进行了激活或者 反激活 则返回JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS
  29 + * *******************************/
  30 +T_JZsdkReturnCode ActivateMAT_ActivateInterface(int SN_status, unsigned char *SerialNumber ,unsigned char *data, int data_length)
  31 +{
  32 + T_JZsdkReturnCode ret;
  33 +
  34 + //如果数组指针为空,直接退出
  35 + if (data == NULL)
  36 + {
  37 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  38 + }
  39 +
  40 + //如果已经激活了
  41 + if (SN_status == SNM_HAVE_ACTIVATED)
  42 + {
  43 + //判断是否是要反激活的
  44 + if (data[0] == '#' && data[1] == '#')
  45 + {
  46 + //复合反激活的格式 指 ##+8位数
  47 + if (data_length == 10)
  48 + {
  49 + ret = ActivateMAT_ActivateAndUnActivate(1, SerialNumber, data+2);
  50 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  51 + {
  52 + printf("反激活失败");
  53 + return ret;
  54 + }
  55 + }
  56 + else
  57 + {
  58 + printf("试图输入##反激活,但是长度为%d\n", data_length);
  59 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  60 + }
  61 +
  62 + }
  63 + else
  64 + {
  65 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  66 + }
  67 +
  68 + }
  69 + //如果是未激活
  70 + else if(SN_status == SNM_NOT_ACTIVATED)
  71 + {
  72 + //判断是否是要激活的
  73 + if (data_length == 8)
  74 + {
  75 + ret = ActivateMAT_ActivateAndUnActivate(0, SerialNumber, data);
  76 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  77 + {
  78 + return ret;
  79 + }
  80 + }
  81 + else
  82 + {
  83 + JZSDK_LOG_ERROR("输入的并非是激活码,或者是激活码长度不对");
  84 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  85 + }
  86 + }
  87 + //既不是激活也不是反激活
  88 + else
  89 + {
  90 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  91 + }
  92 +
  93 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  94 +}
  95 +
  96 +/******************
  97 + *
  98 + * 激活与反激活
  99 + * flag 0为激活 flag 1为反激活
  100 + * ******************/
  101 +static T_JZsdkReturnCode ActivateMAT_ActivateAndUnActivate(int flag, unsigned char *SerialNumber,unsigned char *ActivationCode)
  102 +{
  103 + unsigned long Inputput_crc_code; //输入的字符串转换得到的校验码
  104 + unsigned long local_crc_code; //根据本地序列号得到的校验码
  105 + T_JZsdkReturnCode ret;
  106 +
  107 + //将输入的激活或反激活码字符串转化为 识别用的16进制校验码
  108 + Inputput_crc_code = strtoul(ActivationCode, NULL, 16); //将字符串转换为16进制unsigned long
  109 + printf("输入激活码得到的校验码为:%08lX\n", Inputput_crc_code);
  110 +
  111 + //激活计算
  112 + if (flag == 0)
  113 + {
  114 + //1、获取本地激活码
  115 + ActivateMAT_UnLockDevice(SerialNumber, &local_crc_code);
  116 + printf("计算得到的激活码:%08X\n", local_crc_code);
  117 +
  118 + //如果校验码 等于本地激活码
  119 + if (Inputput_crc_code == local_crc_code)
  120 + {
  121 + //激活码正确
  122 + JZSDK_LOG_INFO("激活码正确");
  123 +
  124 + //删除锁文件
  125 + ret = ActivateMAT_delete_SN_LOCK_file();
  126 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  127 + {
  128 + JZSDK_LOG_ERROR("解除激活码锁失败");
  129 + return ret;
  130 + }
  131 +
  132 + //检查锁是否成功解除
  133 + ret = ActivateMAT_check_SN_LOCK_exists();
  134 + if (ret == JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS) //等于就是锁还存在
  135 + {
  136 + JZSDK_LOG_ERROR("解除激活码锁失败,锁仍然存在");
  137 + return ret;
  138 + }
  139 + }
  140 + else
  141 + {
  142 + //激活码错误
  143 + JZSDK_LOG_ERROR("激活码错误");
  144 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  145 + }
  146 + }
  147 + //反激活计算
  148 + else if(flag == 1)
  149 + {
  150 + //1、获取本地激活码
  151 + ActivateMAT_LockDevice(SerialNumber, &local_crc_code);
  152 + printf("计算得到的反激活码:%08X\n", local_crc_code);
  153 +
  154 + //如果校验码 等于本地激活码
  155 + if (Inputput_crc_code == local_crc_code)
  156 + {
  157 + //激活码正确
  158 + printf("反激活正确\n");
  159 + //创建锁文件
  160 + ret = ActivateMAT_create_SN_LOCK_file();
  161 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  162 + {
  163 + JZSDK_LOG_ERROR("激活码锁创建失败");
  164 + return ret;
  165 + }
  166 +
  167 + //检查锁是否成功上锁
  168 + ret = ActivateMAT_check_SN_LOCK_exists();
  169 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS) //不等于就是锁还=不存在
  170 + {
  171 + JZSDK_LOG_ERROR("激活码锁创建失败,锁存在");
  172 + return ret;
  173 + }
  174 + }
  175 + else
  176 + {
  177 + //反激活码错误
  178 + printf("反激活码错误\n");
  179 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  180 + }
  181 + }
  182 + else
  183 + {
  184 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  185 + }
  186 +
  187 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  188 +}
  189 +
  190 +/******************
  191 + *
  192 + * 激活状态判断,输入序列号类型,以及 序列号状态的地址
  193 + * 返回成功或者失败
  194 + * ******************/
  195 + T_JZsdkReturnCode ActivateMAT_DeviceActivateStatus(int SN_Type, int *SN_status)
  196 +{
  197 + // //如果序列号类型处于 UNDEFINED_SNM STANDARD_JZ_1_SNM
  198 + // //都是认为默认激活
  199 + // if (SN_Type == UNDEFINED_SNM || SN_Type == STANDARD_JZ_1_SNM)
  200 + // {
  201 + // *SN_status = SNM_HAVE_ACTIVATED;
  202 + // return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  203 + // }
  204 +
  205 + // //如果为极至标准2类型 或者 dji环类型的序列号类型
  206 + // //正常判断是否有激活
  207 +
  208 + //如果是串口程序,默认为已经激活
  209 + if (APP_VERSION == APP_UART)
  210 + {
  211 + *SN_status = SNM_HAVE_ACTIVATED;
  212 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  213 + }
  214 +
  215 +
  216 + //如果sn锁文件存在
  217 + if(ActivateMAT_check_SN_LOCK_exists() == JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  218 + {
  219 + printf("检测到sn存在\n");
  220 + *SN_status = SNM_NOT_ACTIVATED;
  221 + }
  222 + else
  223 + {
  224 + printf("检测到不sn存在\n");
  225 + //如果没有sn锁,就是已经激活设备
  226 + *SN_status = SNM_HAVE_ACTIVATED;
  227 + }
  228 +
  229 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  230 +}
  231 +
  232 +
  233 +/**********************************
  234 + *
  235 + * 创建SN_LOCK文件
  236 + *
  237 + * *******************************/
  238 +// 函数名:create_SN_LOCK_file
  239 +// 参数:文件路径
  240 +static T_JZsdkReturnCode ActivateMAT_create_SN_LOCK_file()
  241 +{
  242 + return JZsdk_create_file(SN_LOCL_FILE_PATH);
  243 +}
  244 +
  245 +/**********************************
  246 + *
  247 + * 删除SN_LOCK文件
  248 + *
  249 + * *******************************/
  250 +// 函数名:delete_SN_LOCK_file
  251 +// 参数:文件路径
  252 +static T_JZsdkReturnCode ActivateMAT_delete_SN_LOCK_file()
  253 +{
  254 + return JZsdk_delete_file(SN_LOCL_FILE_PATH);
  255 +}
  256 +
  257 +/**********************************
  258 + *
  259 + * 检查SN_LOCK锁定文件是否存在
  260 + *
  261 + * *******************************/
  262 +static T_JZsdkReturnCode ActivateMAT_check_SN_LOCK_exists()
  263 +{
  264 + return JZsdk_check_file_exists(SN_LOCL_FILE_PATH);
  265 +}
  266 +
  267 +/**********************************
  268 + *
  269 + * 激活计算函数
  270 + *
  271 + * *******************************/
  272 +static T_JZsdkReturnCode ActivateMAT_UnLockDevice(char *data, unsigned long *deac_code)
  273 +{
  274 + unsigned long crc = 0xFFFFFFFF;
  275 + int length = strlen(data);
  276 +
  277 + for (int i = 0; i < length; i++)
  278 + {
  279 + crc ^= (unsigned long)data[i];
  280 +
  281 + for (int j = 0; j < 8; j++) {
  282 + if (crc & 1)
  283 + crc = (crc >> 1) ^ CRC32_POLYNOMIAL_UNLOCK;
  284 + else
  285 + crc >>= 1;
  286 + }
  287 + }
  288 +
  289 + crc ^= 0xFFFFFFFF;
  290 +
  291 + *deac_code = crc;
  292 +
  293 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  294 +}
  295 +
  296 +/**********************************
  297 + *
  298 + * 反激活计算函数
  299 + *
  300 + * *******************************/
  301 +static T_JZsdkReturnCode ActivateMAT_LockDevice(char *data, unsigned long *ac_code)
  302 +{
  303 + unsigned long crc = 0xFFFFFFFF;
  304 + int length = strlen(data);
  305 +
  306 +
  307 +
  308 + for (int i = 0; i < length; i++)
  309 + {
  310 + crc ^= (unsigned long)data[i];
  311 +
  312 + for (int j = 0; j < 8; j++) {
  313 + if (crc & 1)
  314 + crc = (crc >> 1) ^ CRC32_POLYNOMIAL_LOCK;
  315 + else
  316 + crc >>= 1;
  317 + }
  318 + }
  319 +
  320 + crc ^= 0xFFFFFFFF;
  321 +
  322 + *ac_code = crc;
  323 +
  324 + printf("反激活 序列号%s crccode:%08X accode:%08X\n", data, crc, *ac_code);
  325 +
  326 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  327 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file ActivateMAT_InAndOut.h
  4 + * ActivateMAT_InAndOut的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef ACTIVATE_MAT_INANDOUT_H
  11 +#define ACTIVATE_MAT_INANDOUT_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +
  23 +
  24 +/* Exported types ------------------------------------------------------------*/
  25 +
  26 +/* Exported functions --------------------------------------------------------*/
  27 +T_JZsdkReturnCode ActivateMAT_ActivateInterface(int SN_status, unsigned char *SerialNumber ,unsigned char *data, int data_length);
  28 + T_JZsdkReturnCode ActivateMAT_DeviceActivateStatus(int SN_Type, int *SN_status);
  29 +
  30 +#ifdef __cplusplus
  31 +}
  32 +#endif
  33 +
  34 +#endif
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdlib.h>
  4 +
  5 +#include "JZsdkLib.h"
  6 +#include "BaseConfig.h"
  7 +#include "DeviceInfo/SerialNumberProc/SerialProc.h"
  8 +#include "JZsdk_Base/JZsdk_Code/JZsdk_SMT_Code.h"
  9 +#include "./FirewareOriginMAT.h"
  10 +
  11 +static int g_OriginRegionNum = ORIGIN_DEFAULT;
  12 +
  13 +/*****************
  14 + *
  15 + * 属地初始化
  16 + *
  17 + *
  18 + * *******************/
  19 +T_JZsdkReturnCode FOMAT_FirewareOriginRegion_Init(int SerialType, unsigned char *SerialNumber, unsigned int SerialNumberLen)
  20 +{
  21 + //没有定义过的序列号,或是没有序列号
  22 + if (SerialType == UNDEFINED_SNM || SerialType == STANDARD_DJI_SNM)
  23 + {
  24 + if (FIRMWARE_ORIGIN == DOMESTIC_VERSION)
  25 + {
  26 + g_OriginRegionNum = ORIGIN_CN;
  27 + }
  28 + else if (FIRMWARE_ORIGIN == OVERSEAS_VERSION)
  29 + {
  30 + g_OriginRegionNum = ORIGIN_EN;
  31 + }
  32 + else
  33 + {
  34 + g_OriginRegionNum = ORIGIN_CN;
  35 + }
  36 +
  37 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  38 + }
  39 +
  40 + //旧类型的
  41 + if (SerialType == STANDARD_JZ_1_SNM)
  42 + {
  43 + if (FIRMWARE_ORIGIN == DOMESTIC_VERSION)
  44 + {
  45 + g_OriginRegionNum = ORIGIN_CN;
  46 + }
  47 + else if (FIRMWARE_ORIGIN == OVERSEAS_VERSION)
  48 + {
  49 + g_OriginRegionNum = ORIGIN_EN;
  50 + }
  51 + else
  52 + {
  53 + g_OriginRegionNum = ORIGIN_CN;
  54 + }
  55 +
  56 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  57 + }
  58 +
  59 + //新序列号类型
  60 + if (SerialType == STANDARD_JZ_2_SNM)
  61 + {
  62 + //检测是否是国内的
  63 + if ((SerialNumber[4] == 'C' || SerialNumber[4] == 'c')
  64 + && (SerialNumber[5] == 'N' || SerialNumber[5] == 'n'))
  65 + {
  66 + g_OriginRegionNum = ORIGIN_CN;
  67 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  68 + }
  69 +
  70 + //检测是否是en通用区的
  71 + if ((SerialNumber[4] == 'E' || SerialNumber[4] == 'e')
  72 + && (SerialNumber[5] == 'N' || SerialNumber[5] == 'n'))
  73 + {
  74 + g_OriginRegionNum = ORIGIN_EN;
  75 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  76 + }
  77 +
  78 + //无匹配序列号
  79 + if (FIRMWARE_ORIGIN == DOMESTIC_VERSION)
  80 + {
  81 + g_OriginRegionNum = ORIGIN_CN;
  82 + }
  83 + else if (FIRMWARE_ORIGIN == OVERSEAS_VERSION)
  84 + {
  85 + g_OriginRegionNum = ORIGIN_EN;
  86 + }
  87 + else
  88 + {
  89 + g_OriginRegionNum = ORIGIN_CN;
  90 + }
  91 + }
  92 +
  93 +}
  94 +
  95 +/*******************
  96 + *
  97 + * 属地获取
  98 + *
  99 + * *****************/
  100 +T_JZsdkReturnCode FOMAT_Get_FirewareOriginRegion()
  101 +{
  102 + return g_OriginRegionNum;
  103 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file FirewareOriginMAT.h
  4 + * FirewareOriginMAT.h 的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef FIREWARE_ORIGIN_MAT_H
  11 +#define FIREWARE_ORIGIN_MAT_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +T_JZsdkReturnCode FOMAT_FirewareOriginRegion_Init(int SerialType, unsigned char *SerialNumber, unsigned int SerialNumberLen);
  23 +T_JZsdkReturnCode FOMAT_Get_FirewareOriginRegion();
  24 +
  25 +
  26 +/* Exported types ------------------------------------------------------------*/
  27 +typedef enum OriginRegionPlace
  28 +{
  29 + ORIGIN_DEFAULT = 0x0000,
  30 + ORIGIN_CN = 0x0001,
  31 + ORIGIN_EN = 0x0002,
  32 +}OriginRegionPlace;
  33 +
  34 +/* Exported functions --------------------------------------------------------*/
  35 +
  36 +
  37 +#ifdef __cplusplus
  38 +}
  39 +#endif
  40 +
  41 +#endif
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdlib.h>
  4 +
  5 +#include "./SerialProc.h"
  6 +#include "./ActivateMAT/ActivateMAT.h"
  7 +#include "JZsdkLib.h"
  8 +#include "JZsdk_Base/JZsdk_Code/JZsdk_SMT_Code.h"
  9 +
  10 +// 负载型号 (4位)+ CN/EN + F 1(默认) + 年(1) + 月(1) + 4位的计数(1-ffff 的16进制) + 2位CRC校验位
  11 +
  12 +
  13 +#define SNM_PATH "/root/num" //序列号存放地址
  14 +#define SERIAL_NUMBER_LENGTH STANDARD_JZ_2_SUM_LENGTH
  15 +
  16 +static int SerialNumberLength; //序列号长度
  17 +static int SerialNumberType = UNDEFINED_SNM; //序列号类型
  18 +static int SerialNumberStatus = SNM_NOT_EXISTS; //激活状态,未存在序列号
  19 +static unsigned char SerialNumber[17]; //序列号
  20 +static unsigned char DJI_SkyPort_SerialNumber[15]; //大疆转接环序列号 默认为空
  21 +
  22 +static T_JZsdkReturnCode SerialMAT_NumberGet();
  23 +
  24 +/******************
  25 + *
  26 + * 序列号管理模块初始化
  27 + *
  28 + * ******************/
  29 +T_JZsdkReturnCode SerialMAT_Init()
  30 +{
  31 + T_JZsdkReturnCode ret;
  32 + // 1、获取序列号与类型
  33 + ret = SerialMAT_NumberGet();
  34 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  35 + {
  36 + JZSDK_LOG_ERROR("序列号获取错误");
  37 + }
  38 +
  39 + // 2、根据上一步获取的情况,确认激活状态
  40 + ret = ActivateMAT_DeviceActivateStatus(SerialNumberType, &SerialNumberStatus);
  41 +
  42 + // 3、进行属地的初始化
  43 + FOMAT_FirewareOriginRegion_Init(SerialNumberType, SerialNumber, SerialNumberLength);
  44 +
  45 + JZSDK_LOG_INFO("序列号模块初始化完成");
  46 +
  47 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  48 +}
  49 +
  50 +/*************
  51 + *
  52 + * 设置大疆序列号
  53 + * 固定长度14位
  54 + * ***********/
  55 +T_JZsdkReturnCode SerialMAT_Set_DJI_SkyPort_SerialNumber(char *str)
  56 +{
  57 + memcpy(DJI_SkyPort_SerialNumber, str, 14);
  58 + DJI_SkyPort_SerialNumber[14] = '\0';
  59 +}
  60 +
  61 +/***************
  62 + *
  63 + * 获取序列号
  64 + *
  65 + * **************/
  66 +T_JZsdkReturnCode SerialMAT_Get_SerialNumber(char *str, int *strlen)
  67 +{
  68 + if (str != NULL)
  69 + {
  70 + memcpy(str, SerialNumber, SerialNumberLength);
  71 + }
  72 +
  73 + if (strlen != NULL)
  74 + {
  75 + *strlen = SerialNumberLength;
  76 + }
  77 +
  78 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  79 +}
  80 +
  81 +/*********
  82 + *
  83 + * 读取,并获取序列号 暂时不开启对外使用
  84 + *
  85 + * *************/
  86 +T_JZsdkReturnCode SerialMAT_Read_SerialNumber(char *str, int *strlen)
  87 +{
  88 + SerialMAT_NumberGet();
  89 + SerialMAT_Get_SerialNumber(str, strlen);
  90 +}
  91 +
  92 +/******************
  93 + *
  94 + * 获取激活状态
  95 + * SNM_NOT_EXISTS = 0x0000, // (未存在序列号)
  96 + SNM_HAVE_ACTIVATED = 0x0001, // 已激活
  97 + SNM_NOT_ACTIVATED = 0x0002, // 未激活
  98 + SNM_ERROR_ACTIVATED = 0x0003, //激活错误状态
  99 + * ******************/
  100 +T_JZsdkReturnCode SerialMAT_Get_SerialNumberStatus()
  101 +{
  102 + return SerialNumberStatus;
  103 +}
  104 +
  105 +/**********************************
  106 + *
  107 + * 判断是否需要激活/反激活,且进行激活或者反激活
  108 + * 输入字符串 及长度
  109 + * 无任何操作发生 返回JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE
  110 + * 有进行了激活或者 反激活 返回JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS
  111 + * *******************************/
  112 +T_JZsdkReturnCode SerialMAT_ActivateInterface(unsigned char *data, int data_length)
  113 +{
  114 + T_JZsdkReturnCode ret;
  115 + ret = ActivateMAT_ActivateInterface(SerialNumberStatus, SerialNumber, data, data_length ) ;
  116 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  117 + {
  118 + return ret;
  119 + }
  120 +
  121 + //操作完后,重新检测激活状态
  122 + ActivateMAT_DeviceActivateStatus(SerialNumberType, &SerialNumberStatus);
  123 +
  124 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  125 +}
  126 +
  127 +
  128 +
  129 +/******************
  130 + *
  131 + * 获取序列号,及序列号类型
  132 + *
  133 + * ******************/
  134 +static T_JZsdkReturnCode SerialMAT_NumberGet()
  135 +{
  136 + FILE *num_file;
  137 + char num_char[128];
  138 + memset(num_char, 0, sizeof(num_char));
  139 + memset(SerialNumber, 0, sizeof(SerialNumber));
  140 + T_JZsdkReturnCode ret;
  141 +
  142 + //1、判断是否拥有大疆环的序列号
  143 + ret = JZsdk_Array_isEmpty(DJI_SkyPort_SerialNumber, sizeof(DJI_SkyPort_SerialNumber));
  144 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  145 + {
  146 + SerialNumberType = STANDARD_DJI_SNM;
  147 +
  148 + //拥有转接环序列号
  149 + memcpy(SerialNumber,DJI_SkyPort_SerialNumber, sizeof(DJI_SkyPort_SerialNumber));
  150 + SerialNumberLength = sizeof(DJI_SkyPort_SerialNumber);
  151 + JZSDK_LOG_INFO("带SkyPort设备,序列号%s",SerialNumber);
  152 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  153 + }
  154 +
  155 + //2、未有转接环序列号,正常读取序列号
  156 + num_file = fopen(SNM_PATH, "rb+");
  157 + if (num_file == NULL)
  158 + {
  159 + SerialNumberType = UNDEFINED_SNM;
  160 + JZSDK_LOG_ERROR("不存在序列号文件");
  161 + memcpy(SerialNumber, "SN none", sizeof("SN none"));
  162 + SerialNumberLength = sizeof("SN none");
  163 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  164 + }
  165 +
  166 + if (fseek(num_file, 0, SEEK_SET) != 0)
  167 + {
  168 + JZSDK_LOG_ERROR("序列号读取时,发生计数错误");
  169 + SerialNumberType = UNDEFINED_SNM;
  170 + memcpy(SerialNumber, "SN ERROR", sizeof("SN ERROR"));
  171 + SerialNumberLength = sizeof("SN ERROR");
  172 + fclose(num_file);
  173 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  174 + }
  175 +
  176 + ret = fread((char *)&num_char, 1, SERIAL_NUMBER_LENGTH, num_file);
  177 + if (ret == 0)
  178 + {
  179 + JZSDK_LOG_ERROR("序列号读取到的长度为0");
  180 + SerialNumberType = UNDEFINED_SNM;
  181 + memcpy(SerialNumber, "SN none", sizeof("SN none"));
  182 + SerialNumberLength = sizeof("SN none");
  183 + fclose(num_file);
  184 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  185 + }
  186 +
  187 + //判断是否是极至标准1的序列号
  188 + else if ( (ret <= 14) && (ret >= 12))
  189 + {
  190 + JZSDK_LOG_INFO("标准一序列号");
  191 +
  192 + SerialNumberType = STANDARD_JZ_1_SNM;
  193 + memcpy(SerialNumber, num_char, ret);
  194 + SerialNumberLength = ret;
  195 + JZSDK_LOG_INFO("序列号%s",SerialNumber);
  196 + }
  197 +
  198 + //判断是否是极至标准2的序列号
  199 + //目前只靠长度判断
  200 + else if(ret == 16)
  201 + {
  202 + JZSDK_LOG_INFO("标准二序列号");
  203 +
  204 + SerialNumberType = STANDARD_JZ_2_SNM;
  205 + memcpy(SerialNumber, num_char, ret);
  206 + SerialNumberLength = ret;
  207 + JZSDK_LOG_INFO("序列号%s",SerialNumber);
  208 + }
  209 +
  210 + else
  211 + {
  212 + JZSDK_LOG_ERROR("序列号长度错误%d",ret);
  213 + memcpy(SerialNumber, "SN ERROR", sizeof("SN ERROR"));
  214 + SerialNumberLength = sizeof("SN ERROR");
  215 + fclose(num_file);
  216 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  217 + }
  218 +
  219 + //检测序列号中是否存在换行符,如果有,替换成结束符
  220 + JZsdk_replace_newlines_with_null_terminator(SerialNumber);
  221 +
  222 + fclose(num_file);
  223 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  224 +}
  225 +
  226 +
  227 +/******************************
  228 + *
  229 + * 设置序列号
  230 + *
  231 + *
  232 + * ************************/
  233 +T_JZsdkReturnCode SerialMAT_SetSerialNumber(unsigned char *SerialNumber, unsigned int SerialNumberLen)
  234 +{
  235 + FILE *num_file = fopen(SNM_PATH, "wb+");
  236 + if (num_file == NULL)
  237 + {
  238 + JZSDK_LOG_ERROR("写入序列号失败");
  239 + return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
  240 + }
  241 +
  242 + if( fwrite(SerialNumber, 1, SerialNumberLen, num_file) != SerialNumberLen)
  243 + {
  244 + JZSDK_LOG_ERROR("写入序列号失败");
  245 + fclose(num_file);
  246 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  247 + }
  248 +
  249 + fclose(num_file);
  250 +
  251 + //刷新序列号
  252 + SerialMAT_NumberGet();
  253 +
  254 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  255 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file Megaphone_InputAndOutput.h
  4 + * Megaphone_InputAndOutput的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef SERIALMAT_INANDOUT_H
  11 +#define SERIALMAT_INANDOUT_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +#include "DeviceInfo/SerialNumberProc/FirewareOriginMAT/FirewareOriginMAT.h"
  16 +
  17 +#ifdef __cplusplus
  18 +extern "C" {
  19 +#endif
  20 +
  21 +/* Exported constants --------------------------------------------------------*/
  22 +/* 常亮定义*/
  23 +
  24 +
  25 +/* Exported types ------------------------------------------------------------*/
  26 +
  27 +/* Exported functions --------------------------------------------------------*/
  28 +T_JZsdkReturnCode SerialMAT_Init();
  29 +T_JZsdkReturnCode SerialMAT_Get_SerialNumberStatus();
  30 +T_JZsdkReturnCode SerialMAT_ActivateInterface(unsigned char *data, int data_length);
  31 +T_JZsdkReturnCode SerialMAT_Set_DJI_SkyPort_SerialNumber(char *str);
  32 +T_JZsdkReturnCode SerialMAT_Get_SerialNumber(char *str, int *strlen);
  33 +T_JZsdkReturnCode SerialMAT_Read_SerialNumber(char *str, int *strlen);
  34 +T_JZsdkReturnCode SerialMAT_SetSerialNumber(unsigned char *SerialNumber, unsigned int SerialNumberLen);
  35 +
  36 +#ifdef __cplusplus
  37 +}
  38 +#endif
  39 +
  40 +#endif
  1 +/**
  2 + ********************************************************************
  3 + * @file GIMBAL_H3.h
  4 + * GIMBAL_H3的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef GIMBAL_H3_H
  11 +#define GIMBAL_H3_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +
  15 +#ifdef __cplusplus
  16 +extern "C" {
  17 +#endif
  18 +
  19 +/* Exported constants --------------------------------------------------------*/
  20 +/* 常亮定义*/
  21 +#include "Gimbal_H3_H150ST/Gimbal_H3_H150ST.h"
  22 +#include "Gimbal_H3_H10/Gimbal_H3_H10.h"
  23 +
  24 +/* Exported types ------------------------------------------------------------*/
  25 +
  26 +/* Exported functions --------------------------------------------------------*/
  27 +
  28 +
  29 +#ifdef __cplusplus
  30 +}
  31 +#endif
  32 +
  33 +#endif
  1 +/* Includes ------------------------------------------------------------------*/
  2 +#include <stdio.h>
  3 +#include <pthread.h>
  4 +#include <stdlib.h>
  5 +
  6 +#include "Gimbal_H3_H10.h"
  7 +#include "version_choose.h"
  8 +
  9 +#if WIRINGPI_STATUS == VERSION_SWITCH_ON
  10 + #include <wiringPi.h>
  11 +#endif
  12 +
  13 +
  14 +/* Private constants ---------------------------------------------------------*/
  15 +#define MOTOR_FILE_PATH "/root/motor"
  16 +#define H3_H10_angle_PWM_MIN 1000
  17 +#define H3_H10_angle_PWM_MAX 2000
  18 +
  19 +#define H3_H10_MOTOR_ADJUSTMENT 5 //云台微调值
  20 +
  21 +/* Private types -------------------------------------------------------------*/
  22 +
  23 +/* Private functions declaration ---------------------------------------------*/
  24 +#define MOTOR 18
  25 +
  26 +static int H3_H10_motor_precise_adjustment_pitch =0;//默认H3微调值为0
  27 +
  28 +static void *Gimbal_H3_H10_Motor_control(void *arg);
  29 +static int H3_H10_Read_MotorAdjustmentPitch(void);
  30 +static void H3_H10_Write_MotorAdjustmentPitch(int MotorAdjustmentPitch);
  31 +
  32 +int Gimbal_H3_H10_CheckStatus_GimbalFineTuning(int *AdjustmentPitch)
  33 +{
  34 + *AdjustmentPitch = H3_H10_motor_precise_adjustment_pitch;
  35 +}
  36 +
  37 +void Gimbal_H3_H10_init_motor(void)
  38 +{
  39 + H3_H10_motor_precise_adjustment_pitch = H3_H10_Read_MotorAdjustmentPitch();//获取微调角度-读文件获取
  40 + Gimbal_H3_H10_set_angle(0);
  41 +}
  42 +
  43 +
  44 +int Gimbal_H3_H10_set_angle(int angle)
  45 +{
  46 + printf("H3_H10_angle:%d\n",angle);
  47 +
  48 + //每43.5对饮20度
  49 + int num = 200 -angle*195/900; //正常
  50 + //int num = 200 -angle*195/900 -150*195/900; //上调15度
  51 +#if WIRINGPI_STATUS == VERSION_SWITCH_ON
  52 +
  53 + pwmWrite(MOTOR,num);
  54 +#endif
  55 +}
  56 +
  57 +//云台微调
  58 +T_JZsdkReturnCode Gimbal_H3_H10_set_PitchFineTuning(int Pitch)
  59 +{
  60 + //微调的角度值 提高Pitch
  61 + H3_H10_motor_precise_adjustment_pitch += Pitch;
  62 +
  63 + //写入本地文件
  64 + H3_H10_Write_MotorAdjustmentPitch(H3_H10_motor_precise_adjustment_pitch);
  65 +
  66 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  67 +
  68 +}
  69 +
  70 +//从文件中读出电机微调值
  71 +static int H3_H10_Read_MotorAdjustmentPitch(void)
  72 +{
  73 + FILE *motor_file;
  74 + int MotorAdjustmentPitch = 0;
  75 + motor_file = fopen(MOTOR_FILE_PATH, "rb+");
  76 + if (motor_file == NULL)
  77 + {
  78 + motor_file = fopen(MOTOR_FILE_PATH, "wb+");
  79 + if (motor_file == NULL)
  80 + {
  81 + return 0;
  82 + }
  83 + }
  84 + else
  85 + {
  86 + int ret = fseek(motor_file, 0, SEEK_SET);
  87 + if (ret != 0) {
  88 + printf("Seek log count file error, ret: %d.\r\n", ret);
  89 + }
  90 +
  91 + ret = fread((unsigned int *) &MotorAdjustmentPitch, 1, sizeof(unsigned int), motor_file);
  92 + if (ret != sizeof(unsigned int)) {
  93 + printf("Read motor weitiao error.\r\n");
  94 + }
  95 + else{
  96 + printf("Read motor weitiao=%d\n",MotorAdjustmentPitch);
  97 + }
  98 + }
  99 + fclose(motor_file);
  100 +
  101 + return MotorAdjustmentPitch;
  102 +}
  103 +
  104 +//写入电机微调值刀文件中
  105 +static void H3_H10_Write_MotorAdjustmentPitch(int MotorAdjustmentPitch)
  106 +{
  107 + //写入文件
  108 + FILE *motor_file;
  109 + motor_file = fopen(MOTOR_FILE_PATH, "wb+");
  110 + int ret = fwrite((unsigned int *) &MotorAdjustmentPitch, 1, sizeof(unsigned int),motor_file);
  111 + if (ret != sizeof(unsigned int)) {
  112 + printf("Write motor weitiao error.\r\n");
  113 + }
  114 + else{
  115 + printf("Write motor weitiao=%d\n",MotorAdjustmentPitch);
  116 + }
  117 +
  118 + fclose(motor_file);
  119 +}
  120 +
  121 +
  1 +/**
  2 + ********************************************************************
  3 + * @file GIMBAL_H3_H10.h
  4 + * H3板子的h10云台头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef GIMBAL_H3_H10_H
  11 +#define GIMBAL_H3_H10_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +
  23 +/* Exported types ------------------------------------------------------------*/
  24 +
  25 +/* Exported functions --------------------------------------------------------*/
  26 +int Gimbal_H3_H10_set_angle(int angle);
  27 +void Gimbal_H3_H10_init_motor(void);
  28 +int Gimbal_H3_H10_CheckStatus_GimbalFineTuning(int *AdjustmentPitch);
  29 +//云台微调
  30 +T_JZsdkReturnCode Gimbal_H3_H10_set_PitchFineTuning(int Pitch);
  31 +
  32 +
  33 +#ifdef __cplusplus
  34 +}
  35 +#endif
  36 +
  37 +#endif
  1 +/* Includes ------------------------------------------------------------------*/
  2 +#include <stdio.h>
  3 +#include <stdlib.h>
  4 +#include <string.h>
  5 +
  6 +#include <pthread.h>
  7 +
  8 +#include "Gimbal_H3_H150ST_UartDeal.h"
  9 +#include "Gimbal_H3_H150ST.h"
  10 +#include "BaseConfig.h"
  11 +
  12 +#include "JZsdk_uart/UartConnection/UartConnection.h"
  13 +#include "JZsdk_TaskManagement/TaskManagement.h"
  14 +
  15 +
  16 +/* Private constants ---------------------------------------------------------*/
  17 +#define MOTOR_FILE_PATH "/root/motor"
  18 +#define H3_H150ST_angle_PWM_MIN 1000
  19 +#define H3_H150ST_angle_PWM_MAX 2000
  20 +
  21 +#define H3_H150ST_MOTOR_ADJUSTMENT 5 //云台微调值
  22 +
  23 +/* Private types -------------------------------------------------------------*/
  24 +
  25 +/* Private functions declaration ---------------------------------------------*/
  26 +
  27 +static int H3_H150ST_motor_precise_adjustment_pitch =0;//默认H3微调值为0
  28 +
  29 +static void *H3_H150ST_Motor_control(void *arg);
  30 +static void H3_H150ST_Write_MotorAdjustmentPitch(int MotorAdjustmentPitch);
  31 +static int H3_H150ST_Read_MotorAdjustmentPitch();
  32 +
  33 +T_JZsdkReturnCode Gimbal_H3_H150ST_init_motor()
  34 +{
  35 + int Uart_fd = 0;
  36 + //1、串口初始化
  37 + Uart_fd = UartConnection_UartEnabled(GIMBAL_UART_NUM, GIMBAL_UART_BITRATE);
  38 +
  39 + //2、串口接收初始化
  40 + Gimbal_H3_H150ST_UartDeal_Receive(Uart_fd);
  41 +
  42 + //从文件中读出电机微调值
  43 + H3_H150ST_motor_precise_adjustment_pitch = H3_H150ST_Read_MotorAdjustmentPitch();
  44 +}
  45 +
  46 +/**********
  47 + *
  48 + * 发送任务函数
  49 + *
  50 + * ***********/
  51 +static void JZsdk_Uart_UartSend_Task(void *data)
  52 +{
  53 + Gimbal_H3_H150ST_UartDeal_UartSend(data, 4);
  54 +}
  55 +
  56 +/****************
  57 + *
  58 + *
  59 + * 发送函数
  60 + *
  61 + * ****************/
  62 +T_JZsdkReturnCode H150ST_UartSend(unsigned char *data, int num)
  63 +{
  64 + unsigned char *str = (unsigned char*)malloc(sizeof(data));
  65 + memcpy(str, data, sizeof(data));
  66 +
  67 + T_JZsdkReturnCode ret = TaskManagement_SubmitTask(JZsdk_Uart_UartSend_Task, (void *)str);
  68 + if (ret != JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS)
  69 + {
  70 + free(str);
  71 + return ret;
  72 + }
  73 +
  74 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  75 +}
  76 +
  77 +
  78 +//设置角度
  79 +int H3_H150ST_Gimbal_SetAngle(int angle)
  80 +{
  81 + //调整为PWM值 1000~2000
  82 + int angle_PWM = 0;
  83 +
  84 + if(angle>=0){
  85 + angle_PWM = H3_H150ST_angle_PWM_MIN;
  86 + }
  87 + else if(angle<=-900){
  88 + angle_PWM = H3_H150ST_angle_PWM_MAX;
  89 + }
  90 + else if(angle<0 && angle>-900)
  91 + {
  92 + angle_PWM= H3_H150ST_angle_PWM_MIN +(-angle)*(H3_H150ST_angle_PWM_MAX-H3_H150ST_angle_PWM_MIN)/900;
  93 + }
  94 +
  95 + char send_angle[]={0x5a,0x00,0x00,0xa5};
  96 + send_angle[1]=(char )(angle_PWM);
  97 + send_angle[2]=(char )(angle_PWM>>8);
  98 +
  99 + H150ST_UartSend(send_angle, 4);
  100 +}
  101 +
  102 +
  103 +
  104 +//H150ST云台范围
  105 +T_JZsdkReturnCode H3_H150ST_SetGimbalRange(int Range)
  106 +{
  107 + uint8_t set_min_motor[]={0x4A,0x00,0x00,0xA4};
  108 + uint8_t set_max_motor[]={0x6A,0x00,0x00,0xA6};
  109 +
  110 + if (Range == 0xFF)
  111 + {
  112 + printf("设置H150ST云台最大值\n");
  113 + Gimbal_H3_H150ST_UartDeal_UartSend(set_max_motor, 4);
  114 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  115 + }
  116 + else if (Range == 0x00)
  117 + {
  118 + printf("设置H150ST云台最小值\n");
  119 + Gimbal_H3_H150ST_UartDeal_UartSend(set_min_motor, 4);
  120 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  121 + }
  122 +
  123 + return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
  124 +}
  125 +
  126 +//云台微调
  127 +T_JZsdkReturnCode Gimbal_H3_H150ST_set_PitchFineTuning(int Pitch)
  128 +{
  129 + //微调的角度值 提高Pitch
  130 + H3_H150ST_motor_precise_adjustment_pitch += Pitch;
  131 +
  132 + //写入本地文件
  133 + H3_H150ST_Write_MotorAdjustmentPitch(H3_H150ST_motor_precise_adjustment_pitch);
  134 +
  135 + return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
  136 +
  137 +}
  138 +
  139 +//微调向上
  140 +void H3_H150ST_gimbal_up(void)
  141 +{
  142 + //微调的pwm值 提高5
  143 + H3_H150ST_motor_precise_adjustment_pitch = H3_H150ST_motor_precise_adjustment_pitch + H3_H150ST_MOTOR_ADJUSTMENT;
  144 +
  145 + //写入本地文件
  146 + H3_H150ST_Write_MotorAdjustmentPitch(H3_H150ST_motor_precise_adjustment_pitch);
  147 +}
  148 +
  149 +//向下微调
  150 +void H3_H150ST_gimbal_down(void)
  151 +{
  152 + //微调的pwm值 减少5
  153 + H3_H150ST_motor_precise_adjustment_pitch = H3_H150ST_motor_precise_adjustment_pitch - H3_H150ST_MOTOR_ADJUSTMENT;
  154 +
  155 + //写入本地文件
  156 + H3_H150ST_Write_MotorAdjustmentPitch(H3_H150ST_motor_precise_adjustment_pitch);
  157 +}
  158 +
  159 +//查询云台微调值
  160 +int Gimbal_H3_H150ST_CheckStatus_GimbalFineTuning(int *AdjustmentPitch)
  161 +{
  162 + *AdjustmentPitch = H3_H150ST_motor_precise_adjustment_pitch;
  163 +}
  164 +
  165 +//从文件中读出电机微调值
  166 +static int H3_H150ST_Read_MotorAdjustmentPitch()
  167 +{
  168 + FILE *motor_file;
  169 + int MotorAdjustmentPitch = 0;
  170 + motor_file = fopen(MOTOR_FILE_PATH, "rb+");
  171 + if (motor_file == NULL)
  172 + {
  173 + motor_file = fopen(MOTOR_FILE_PATH, "wb+");
  174 + if (motor_file == NULL)
  175 + {
  176 + return 0;
  177 + }
  178 + }
  179 + else
  180 + {
  181 + int ret = fseek(motor_file, 0, SEEK_SET);
  182 + if (ret != 0) {
  183 + printf("Seek log count file error, ret: %d.\r\n", ret);
  184 + }
  185 +
  186 + ret = fread((unsigned int *) &MotorAdjustmentPitch, 1, sizeof(unsigned int), motor_file);
  187 + if (ret != sizeof(unsigned int)) {
  188 + printf("Read motor weitiao error.\r\n");
  189 + }
  190 + else{
  191 + printf("Read motor weitiao=%d\n",MotorAdjustmentPitch);
  192 + }
  193 + }
  194 + fclose(motor_file);
  195 +
  196 + return MotorAdjustmentPitch;
  197 +}
  198 +
  199 +//写入电机微调值刀文件中
  200 +static void H3_H150ST_Write_MotorAdjustmentPitch(int MotorAdjustmentPitch)
  201 +{
  202 + //写入文件
  203 + FILE *motor_file;
  204 + motor_file = fopen(MOTOR_FILE_PATH, "wb+");
  205 + int ret = fwrite((unsigned int *) &MotorAdjustmentPitch, 1, sizeof(unsigned int),motor_file);
  206 + if (ret != sizeof(unsigned int)) {
  207 + printf("Write motor weitiao error.\r\n");
  208 + }
  209 + else{
  210 + printf("Write motor weitiao=%d\n",MotorAdjustmentPitch);
  211 + }
  212 +
  213 + fclose(motor_file);
  214 +}
  215 +
  216 +
  1 +/**
  2 + ********************************************************************
  3 + * @file GIMBAL_H3.h
  4 + * GIMBAL_H3的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef GIMBAL_H3_H150ST_H
  11 +#define GIMBAL_H3_H150ST_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +
  23 +/* Exported types ------------------------------------------------------------*/
  24 +
  25 +/* Exported functions --------------------------------------------------------*/
  26 +int H3_H150ST_Gimbal_SetAngle(int angle);
  27 +int Gimbal_H3_H150ST_CheckStatus_GimbalFineTuning(int *AdjustmentPitch);
  28 +T_JZsdkReturnCode Gimbal_H3_H150ST_init_motor();
  29 +T_JZsdkReturnCode H3_H150ST_SetGimbalRange(int Range);
  30 +T_JZsdkReturnCode Gimbal_H3_H150ST_set_PitchFineTuning(int Pitch);
  31 +
  32 +
  33 +#ifdef __cplusplus
  34 +}
  35 +#endif
  36 +
  37 +#endif
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <pthread.h>
  4 +#include <stdlib.h>
  5 +
  6 +#include <fcntl.h>
  7 +#include <unistd.h>
  8 +#include <termios.h>
  9 +#include <sys/time.h>
  10 +
  11 +static void *UartDeal_rece(void *arg);
  12 +static void *UartDeal_send(void *arg);
  13 +static int Gimbal_Uart_fd = 0;
  14 +
  15 +/******************************************************************
  16 +
  17 + 创建一个接收Gimbal的接收线程
  18 +
  19 +******************************************************************/
  20 +int Gimbal_H3_H150ST_UartDeal_Receive(int Uart_fd)
  21 +{
  22 + int ret = 0;
  23 + pthread_t Uart_rece_task;
  24 + Gimbal_Uart_fd = Uart_fd;
  25 +
  26 + pthread_attr_t task_attribute; //线程属性
  27 + pthread_attr_init(&task_attribute); //初始化线程属性
  28 + pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
  29 +
  30 + ret = pthread_create(&Uart_rece_task,&task_attribute,UartDeal_rece,NULL); //串口接收线程
  31 + if(ret != 0)
  32 + {
  33 + printf("创建展架串口接收线程失败!\n");
  34 + }
  35 + else{
  36 + printf("创建展架串口接收线程成功!\n");
  37 + }
  38 +}
  39 +
  40 +static void *UartDeal_rece(void *arg)
  41 +{
  42 + char getbuf[1024];
  43 +
  44 + int ret = 0;
  45 + fd_set fs_read;
  46 + struct timeval tv_timeout;
  47 +
  48 + //FD_ZERO 将指定的文件描述符集清空,在对文件描述符集合进行设置前,必须对其进行初始化
  49 + //如果不清空,由于在系统分配内存空间后,通常并不作清空处理,所以结果是不可知的。
  50 + FD_ZERO(&fs_read);
  51 +
  52 + //FD_SET 用于在文件描述符集合中增加一个新的文件描述符。
  53 + FD_SET(Gimbal_Uart_fd, &fs_read);
  54 +
  55 + //115200 / char 8 位 = 14400 个char数据
  56 + //tv_timeout.tv_sec = 6000;//(10*20/115200+2);
  57 + //tv_timeout.tv_usec = 0;
  58 +
  59 + //2、正常接收
  60 + while(1)
  61 + {
  62 + //检查fs_read套节字是否有数据
  63 + select(Gimbal_Uart_fd+1, &fs_read, NULL, NULL, NULL);
  64 + usleep(10000);
  65 +
  66 + //FD_ISSET 用于测试指定的文件描述符是否在该集合中。
  67 + //Gimbal_Uart_fd 是否在fsread中
  68 + if (FD_ISSET(Gimbal_Uart_fd, &fs_read))
  69 + {
  70 + //1、读取串口内容 ret 接收长度 getbuf 获取的字符
  71 + memset(getbuf,0,sizeof(getbuf)); //清空接收数组
  72 + ret = read(Gimbal_Uart_fd,getbuf,sizeof(getbuf));
  73 + }
  74 + }
  75 +
  76 +}
  77 +
  78 +
  79 +/****************
  80 + *
  81 + *
  82 + * 发送函数
  83 + *
  84 + * ****************/
  85 +int Gimbal_H3_H150ST_UartDeal_UartSend(unsigned char *send, int num)
  86 +{
  87 +
  88 + write(Gimbal_Uart_fd,send, num);
  89 + return 0;
  90 +}
  1 +/**
  2 + ********************************************************************
  3 + * @file Gimbal_UartDeal.h
  4 + * Gimbal_UartDeal的头文件
  5 + *
  6 + *********************************************************************
  7 + */
  8 +
  9 +/* Define to prevent recursive inclusion 避免重定义 -------------------------------------*/
  10 +#ifndef GIMBAL_UART_DEAL_H
  11 +#define GIMBAL_UART_DEAL_H
  12 +
  13 +/* Includes ------------------------------------------------------------------*/
  14 +#include "JZsdk_Base/JZsdk_Code/JZsdk_Code.h"
  15 +
  16 +#ifdef __cplusplus
  17 +extern "C" {
  18 +#endif
  19 +
  20 +/* Exported constants --------------------------------------------------------*/
  21 +/* 常亮定义*/
  22 +
  23 +/* Exported types ------------------------------------------------------------*/
  24 +
  25 +/* Exported functions --------------------------------------------------------*/
  26 +int Gimbal_H3_H150ST_UartDeal_Receive(int Uart_fd);
  27 +int Gimbal_H3_H150ST_UartDeal_UartSend(unsigned char *send, int num);
  28 +
  29 +#ifdef __cplusplus
  30 +}
  31 +#endif
  32 +
  33 +#endif