当前位置: 首页 > news >正文

有哪些做鞋机设备的网站网站开发app开发培训

有哪些做鞋机设备的网站,网站开发app开发培训,win7电脑做网站服务器,wordpress左侧菜单目录 学习课题#xff1a;逐步构建开发播放器【QT5 FFmpeg6 SDL2】 步骤 AudioOutPut模块 1、初始化【分配缓存、读取信息】 2、开始线程工作【从队列读帧-重采样-SDL回调-写入音频播放数据-SDL进行播放】 主要代码 分配缓存 // 对于样本队列 av_audio_…目录 学习课题逐步构建开发播放器【QT5 FFmpeg6 SDL2】 步骤 AudioOutPut模块 1、初始化【分配缓存、读取信息】 2、开始线程工作【从队列读帧-重采样-SDL回调-写入音频播放数据-SDL进行播放】 主要代码 分配缓存 // 对于样本队列 av_audio_fifo_alloc(playSampleFmt, playChannels, spec.samples * 5);// 对于帧的音频字节数据 // 首次计算帧大小并且开辟缓冲区 maxOutSamples (int) av_rescale_rnd(decCtxSamples, playSampleRate, srcSampleRate, AV_ROUND_UP); audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, maxOutSamples, playSampleFmt, 0); audioBuffer (uint8_t *) av_malloc(audioBufferSize); 重采样相关 //配置重采样器参数 swr_alloc_set_opts2(swrContext,srcChannelLayout, playSampleFmt, playSampleRate,srcChannelLayout, AVSampleFormat(srcSampleFmt), srcSampleRate,0, nullptr); //初始化重采样器 swr_init(swrContext);//重采样流程 // 计算重采样后要输出多少样本数delay swr_get_delay(swrContext, sample_rate);out_samples (int) av_rescale_rnd(nb_samples delay,playSampleRate,sample_rate,AV_ROUND_DOWN);// 判断预测的输出样本数是否本次任务的最大样本数if (out_samples maxOutSamples) {// 释放缓冲区重新初始化缓冲区大小av_freep(audioBuffer);audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, out_samples, playSampleFmt, 0);audioBuffer (uint8_t *) av_malloc(audioBufferSize);maxOutSamples out_samples;}playSamples swr_convert(swrContext, audioBuffer, out_samples, (const uint8_t **) frame-data, nb_samples); SDL的音频回调 // SDL音频回调函数提供了一个回调接口可以让我们在音频设备需要数据的时候向里面写入数据 // 从而进行声音播放 // 回调函数示例 函数名自定义放在类中需要加静态(static) void AudioOutPut::AudioCallBackFunc(void *userdata, Uint8 *stream, int len) {//userdata 是在初始化时赋值的有时候会把类中this传进去//stream 是音频流在回调函数中需要把音频数据写入到stream就可以实现声音播放//len是由SDL传入的SDL缓冲区的大小如果这个缓冲未满我们就一直往里填充数据... } 完整模块 AudioOutPut //AudioOutPut.h #include FFmpegHeader.h #include SDL.h #include queue/AVFrameQueue.h #include QDebug #include QObject #include QtGui #include QtWidgets #include threadclass AudioOutPut { private:std::thread *m_thread;bool isStopped true; // 是否已经停止 停止时退出线程bool isPlaying false;// 是否正在播放bool isPause false; // 是否暂停void run();int resampleFrame(AVFrame *frame);int sdlCallBackMode 1;QString url; //视频地址uint8_t *audioBuffer; //存储解码后音频bufferint audioBufferSize 0;//buffer大小int audioBufferIndex 0;SDL_mutex *mtx nullptr;// 队列锁SDL_AudioDeviceID audioDevice;AVAudioFifo *fifo nullptr;//Audio BufferAVFrameQueue *frameQueue; //解码后的帧队列SwrContext *swrContext; //重采样上下文// 解码器上下文AVCodecContext *decCtx; // 音频解码器上下文int srcChannels; // 源通道数AVChannelLayout srcChannelLayout;// 源通道布局enum AVSampleFormat srcSampleFmt;// 源采样格式int srcSampleRate; // 源音频采样率// playerint maxOutSamples; // 最大样本数用于计算缓存区大小int playSamples; // 最终播放的样本数int playSampleRate;// 最终播放的音频采样率enum AVSampleFormat playSampleFmt;int playChannels;// 源通道数 public:AudioOutPut(AVCodecContext *dec_ctx, AVFrameQueue *frame_queue);int init(int mode 1);static void AudioCallBackFunc(void *userdata, Uint8 *stream, int len);//SDL音频回调函数实体普通版void AudioCallBack(Uint8 *stream, int len);//SDL音频回调函数实体队列版void AudioCallBackFromQueue(Uint8 *stream, int len);int start(); };//AudioOutPut.cpp #include AudioOutPut.h AudioOutPut::AudioOutPut(AVCodecContext *dec_ctx, AVFrameQueue *frame_queue): decCtx(dec_ctx), frameQueue(frame_queue) {srcSampleFmt decCtx-sample_fmt;srcSampleRate decCtx-sample_rate;srcChannelLayout decCtx-ch_layout;srcChannels srcChannelLayout.nb_channels; } int AudioOutPut::init(int mode) {sdlCallBackMode mode;// SDL initif (SDL_Init(SDL_INIT_AUDIO) ! 0) {qDebug() SDL_INIT_AUDIO error;return -1;}SDL_AudioSpec wanted_spec, spec;wanted_spec.channels decCtx-ch_layout.nb_channels;wanted_spec.freq decCtx-sample_rate;SDL_AudioFormat sample_type;switch (srcSampleFmt) {case AV_SAMPLE_FMT_FLTP:case AV_SAMPLE_FMT_FLT:sample_type AUDIO_F32SYS;break;case AV_SAMPLE_FMT_U8P:case AV_SAMPLE_FMT_U8:sample_type AUDIO_U8;break;case AV_SAMPLE_FMT_S64P:case AV_SAMPLE_FMT_S64:case AV_SAMPLE_FMT_S32P:case AV_SAMPLE_FMT_S32:sample_type AUDIO_S32SYS;break;case AV_SAMPLE_FMT_S16P:case AV_SAMPLE_FMT_S16:sample_type AUDIO_S16SYS;break;default:sample_type AUDIO_S16SYS;qDebug() 不支持的采样格式:AVSampleFormat( srcSampleFmt );}wanted_spec.format sample_type;wanted_spec.silence 0;wanted_spec.callback AudioCallBackFunc;wanted_spec.userdata this;wanted_spec.samples decCtx-frame_size;int ret;// ret SDL_OpenAudio(wanted_spec, spec);audioDevice SDL_OpenAudioDevice(NULL, 0, wanted_spec, spec, SDL_AUDIO_ALLOW_ANY_CHANGE);if (audioDevice 0) {qDebug() SDL_OpenAudio error;return -1;}playChannels spec.channels;playSampleRate spec.freq;playSampleFmt av_get_packed_sample_fmt(srcSampleFmt);if (mode 1) {fifo av_audio_fifo_alloc(playSampleFmt, playChannels, spec.samples * 5);}ret swr_alloc_set_opts2(swrContext,srcChannelLayout, playSampleFmt, playSampleRate,srcChannelLayout, AVSampleFormat(srcSampleFmt), srcSampleRate,0, nullptr);if (ret ! 0) {qDebug() swr_alloc_set_opts2错误;return -1;}if (!swrContext) {qDebug() 创建音频重采样上下文错误 swr_alloc;return -1;}ret swr_init(swrContext);if (ret 0) {qDebug() 初始化音频重采样上下文错误 swr_init;return -1;}// 解码器上下文保存的帧样本数int decCtxSamples 1024;if (decCtx-frame_size 1024) {decCtxSamples decCtx-frame_size;}// 首次计算帧大小并且开辟缓冲区maxOutSamples (int) av_rescale_rnd(decCtxSamples, playSampleRate, srcSampleRate, AV_ROUND_UP);audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, maxOutSamples, playSampleFmt, 0);audioBuffer (uint8_t *) av_malloc(audioBufferSize);return 1; }void AudioOutPut::AudioCallBackFunc(void *userdata, Uint8 *stream, int len) {AudioOutPut *player (AudioOutPut *) userdata;if (player-sdlCallBackMode 1) {player-AudioCallBackFromQueue(stream, len);} else {player-AudioCallBack(stream, len);} }void AudioOutPut::AudioCallBack(Uint8 *stream, int len) {int len1;// sdl的内部stream可用空间/* len是由SDL传入的SDL缓冲区的大小如果这个缓冲未满我们就一直往里填充数据 */while (len 0) {/* audioBufferIndex 和 audioBufferSize 标示我们自己用来放置解码出来的数据的缓冲区*//* 这些数据待copy到SDL缓冲区 当audioBufferIndex audioBufferSize的时候意味着我*//* 们的缓冲为空没有数据可供copy这时候需要调用audio_decode_frame来解码出更多的桢数据 */if (audioBufferIndex audioBufferSize) {AVFrame *frame frameQueue-pop(10);if (frame) {audioBufferSize resampleFrame(frame);/* audioBufferSize 0 标示没能解码出数据我们默认播放静音 */if (audioBufferSize 0) {/* silence */audioBufferSize 1024;/* 清零静音 */memset(audioBuffer, 0, audioBufferSize);}}audioBufferIndex 0;}/* 当audioBufferIndex audioBufferSize 查看stream可用空间决定一次copy多少数据剩下的下次继续copy */len1 audioBufferSize - audioBufferIndex;// 可用空间if (len1 len) {len1 len;}if (audioBuffer nullptr) return;memcpy(stream, (uint8_t *) audioBuffer audioBufferIndex, len1);len - len1;stream len1;audioBufferIndex len1;} }void AudioOutPut::AudioCallBackFromQueue(Uint8 *stream, int len) {//由于AVAudioFifo非线程安全且是子线程触发此回调所以需要加锁SDL_LockMutex(mtx);//读取队列中的音频数据av_audio_fifo_read(fifo, (void **) stream, playSamples);SDL_UnlockMutex(mtx); } int AudioOutPut::start() {SDL_PauseAudioDevice(audioDevice, 0);// SDL_PauseAudio(0);if (sdlCallBackMode 1) {m_thread new std::thread(AudioOutPut::run, this);if (!m_thread-joinable()) {qDebug() AudioOutPut音频帧处理线程创建失败;return -1;}}isStopped false;isPlaying true;return 0; } void AudioOutPut::run() {AVFrame *frame;while (!isStopped) {frame frameQueue-pop(10);if (frame) {audioBufferSize resampleFrame(frame);while (true) {SDL_LockMutex(mtx);if (av_audio_fifo_space(fifo) playSamples) {av_audio_fifo_write(fifo, (void **) audioBuffer, playSamples);SDL_UnlockMutex(mtx);av_frame_unref(frame);break;}SDL_UnlockMutex(mtx);//队列可用空间不足则延时等待SDL_Delay((double) playSamples / playSampleRate);}}} } int AudioOutPut::resampleFrame(AVFrame *frame) {int64_t delay; // 重采样后延迟int out_samples;// 预测的重采样后的输出样本数int sample_rate;// 帧原采样率int nb_samples; // 帧原样本数sample_rate frame-sample_rate;nb_samples frame-nb_samples;// 计算重采样后要输出多少样本数delay swr_get_delay(swrContext, sample_rate);out_samples (int) av_rescale_rnd(nb_samples delay,playSampleRate,sample_rate,AV_ROUND_DOWN);// 判断预测的输出样本数是否本次任务的最大样本数if (out_samples maxOutSamples) {// 释放缓冲区重新初始化缓冲区大小av_freep(audioBuffer);audioBufferSize av_samples_get_buffer_size(nullptr, srcChannels, out_samples, playSampleFmt, 0);audioBuffer (uint8_t *) av_malloc(audioBufferSize);maxOutSamples out_samples;}playSamples swr_convert(swrContext, audioBuffer, out_samples, (const uint8_t **) frame-data, nb_samples);if (playSamples 0) {return -1;}return av_samples_get_buffer_size(nullptr, srcChannels, playSamples, playSampleFmt, 1); } PlayerMain 添加音频输出代码 AudioOutPut *audioOutPut; audioOutPut new AudioOutPut(audioDecodeThread-dec_ctx, audioFrameQueue); audioOutPut-init(1); audioOutPut-start(); 测试运行结果 如果需要同时执行视频和音频的输出记得要在解复用模块那把限制队列大小的位置把视频队列的大小限制给去掉。 目前只是实现了音频播放和视频渲染显示画面但是可以看到音频和视频是不同步的下一章我们就要让音频和视频同步起来。 播放器开发(六)音频帧处理并用SDL播放结果
http://www.w-s-a.com/news/139046/

相关文章:

  • 怎样建网站赚钱贵州seo和网络推广
  • 创建网站的工具站内seo优化
  • 网站特效 站长查询网网站
  • 百度移动端网站网站建设设计思想
  • 青岛建设官方网站南宁制作企业网站
  • 校园网站建设管理工作制度大网站开发费用
  • 做logo赚钱的网站分类网站 模板
  • 网站建设完成报告织梦网站怎么做备份
  • 邯郸市城乡建设管理局网站vimwiki wordpress
  • 如何修改wordpress站名如何制作公司网站
  • 宁波网站建设与推广方案网站有了备案号之后能做什么
  • 汕头手机端建站模板pinterest app下载
  • 网站主机免费宁波网站建设优化诊断
  • 吧网站做软件的软件下载简单的ui界面制作
  • 陕西网站制作公司网页制作与设计代码
  • 做网站行情郑州微信网站开发
  • 河间网站建设制作null wordpress theme
  • h5网站制作网站开发网站建设文翻译工作
  • 网站建设 税种秦皇岛哪有网站优化公司
  • 专业开发网站设计找人做网页需要多少钱
  • 手机购物网站 建站网站建设网站制作网站设计
  • 基于iview的网站开发模板小程序制作需要什么语言
  • 精美网站设计保定建行网站首页登录
  • 网站建设常见问题做网站保存什么格式最好
  • 营销型网站建设与网页设计网站建设 amp 找VX cp5173
  • 新网站该如何做网站优化呢儿童手工
  • 湖北现代城市建设集团网站搜索引擎优化的作用
  • 上海做网站吧开一家软件开发公司需要什么
  • 阿里巴巴网站建设改图片建设厅官方网站河南
  • 邓砚谷电子商务网站建设镇江网