赞
踩
SDL(Simple DirectMedia Layer)是一个跨平台开发库(Windows、macOS、Linux、iOS 和 Android等),旨在通过 OpenGL 和 Direct3D 提供对音频、键盘、鼠标、游戏杆和图形硬件的低级访问,开发者只需要编写一套代码既可以支持跨平台的运行。目前SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。本文主要用到的是SDL中音视频控制和基础事件部分。下图可以看出SDL主要是对于不同平台的硬件控制库进行支持,使得上层应用对于底层控制无感,降低了开发的门槛。
粉丝福利, 免费领取C++音视频学习资料包+学习路线大纲、技术视频/代码,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓
SDL将功能分成下列数个子系统,不同的子系统包含特定功能本文关注的几个模块包括音频、视频和事件等子系统:
- - SDL_INIT_TIMER:定时器
- - SDL_INIT_AUDIO:音频
- - SDL_INIT_VIDEO:视频
- - SDL_INIT_JOYSTICK:摇杆
- - SDL_INIT_HAPTIC:触摸屏
- - SDL_INIT_GAMECONTROLLER:游戏控制器
- - SDL_INIT_EVENTS:事件
- - SDL_INIT_EVERYTHING:包含上述所有选项
2.2 SDL视频模块
2.2.1 视频API
视频API三大控件窗口(window)、渲染器(renderer)、纹理(texture)。窗口作为所有界面的载体负责基础的窗口事件的捕获和响应;渲染器则是将图像数据渲染成图像;纹理主要是用来管理图像将图像数据加入到纹理,方便后续加载到渲染器。
- - SDL_Init():初始化SDL系统
- - SDL_CreateWindow():创建窗口SDL_Window
- - SDL_CreateRenderer():创建渲染器SDL_Renderer
- - SDL_CreateTexture():创建纹理SDL_Texture
- - SDL_UpdateTexture():设置纹理的数据
- - SDL_RenderCopy():将纹理的数据拷贝给渲染器
- - SDL_RenderPresent():显示
- - SDL_Delay():工具函数,用于延时
- - SDL_Quit():退出SDL系统
视频基础:窗口、渲染器、纹理和矩形等对象。
- - SDL_Window // 窗口
- - SDL_Renderer // 渲染
- - SDL_Texture // 纹理
- - SDL_Rect // 矩形
2.2.2 音频API
音频API基础流程比较简单,首先是打开音频设备SDL_OpenAudio,并开启音频播放SDL_PauseAudio,利用音频回调函数SDL_AudioCallback将数据写入音频播放缓冲区播放SDL_MixAudio,最后关闭音频播放SDL_CloseAudio。
- - SDL_Init():初始化SDL系统
- - SDL_OpenAudio():打开音频设备
- - SDL_AudioCallback():音频播放回调函数
- - SDL_PauseAudio():开启或者暂停播放
- - SDL_MixAudio():将音频数据写入播放缓冲区
- - SDL_CloseAudio():关闭音频设备
- - SDL_Quit():退出SDL系统
本文介绍的是Linux下的环境搭建,主要通过源码进行编译获取SDL动态库。下载路径:https://www.libsdl.org/download-2.0.php
- mkdir SDL
- cd SDL
- wget https://www.libsdl.org/release/SDL2-2.0.22.tar.gz
- tar -xvf SDL2-2.0.22.tar.gz
- cd SDL2-2.0.22
- mkdir build
- cmake ..
- make -j4
整个编译过程很顺利,最终会得到编译文件如下图,这里会用到其中libSDL2-2.0.so动态库文件和根目录下的include文件夹。
得到SDL2动态库后,接下来就是进行工程构建,创建一个SDL_project工程目录,包含该标准bin、build、include、lib、src目录文件。并且将依赖库拷贝到工程目录下。基本框架就构建好了。
- mkdir SDL_project
- cd SDL_project
- mkdir bin build include lib src
- cp ../SDL2-2.0.22/build/libSDL2-2.0.so* ./lib/
- cp ../SDL2-2.0.22/include/ ./src/ -rf
- touch CMakeLists.txt
- touch ./src/CMakeLists.txt
这里截取部分源码进行描述;
第一初始化基础结构;
创建窗口:SDL_CreateWindow
创建渲染器:SDL_CreateRenderer
创建纹理:SDL_CreateTexture
创建刷新线程:SDL_CreateThread
第二是利用SDL_WaitEvent,响应不同SDL事件,包括视频显示流程:
读取yuv文件
将视频缓冲数据更新到纹理:SDL_UpdateTexture;
清理渲染器:SDL_RenderClear
纹理拷贝到渲染器:SDL_RenderCopy
显示视频画面:SDL_RenderPresent
- //SDL_CreateWindow
- window = SDL_CreateWindow("Simple YUV Player",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- video_width, video_height,
- SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
- if(!window)
- {
- fprintf(stderr, "SDL: could not create window, err:%s\n",SDL_GetError());
- goto _FAIL;
- }
- // SDL_CreateRenderer
- renderer = SDL_CreateRenderer(window, -1, 0);
- // SDL_CreateTexture
- texture = SDL_CreateTexture(renderer,
- pixformat,
- SDL_TEXTUREACCESS_STREAMING,
- video_width,
- video_height);
-
-
- video_buf = (uint8_t*)malloc(yuv_frame_len);
- if(!video_buf)
- {
- fprintf(stderr, "Failed to alloce yuv frame space!\n");
- goto _FAIL;
- }
-
- // open yuv_path
- video_fd = fopen(yuv_path, "rb");
- if( !video_fd )
- {
- fprintf(stderr, "Failed to open yuv file:%s\n", yuv_path);
- goto _FAIL;
- }
-
- // SDL_CreateThread
- timer_thread = SDL_CreateThread(refresh_video_timer,
- NULL,
- NULL);
-
- while (1)
- {
- // SDL_WaitEvent
- SDL_WaitEvent(&event);
-
- if(event.type == SDL_REFRESH_EVENT) // update video frame
- {
- video_buff_len = fread(video_buf, 1, yuv_frame_len, video_fd);
- if(video_buff_len <= 0)
- {
- fprintf(stderr, "Failed to read data from yuv file!\n");
- goto _FAIL;
- }
- // SDL_UpdateTexture
- SDL_UpdateTexture(texture, NULL, video_buf, video_width);
-
- // keep window scale
- rect.x = 0;
- rect.y = 0;
- float w_ratio = win_width * 1.0 /video_width;
- float h_ratio = win_height * 1.0 /video_height;
- rect.w = video_width * w_ratio;
- rect.h = video_height * h_ratio;
-
- // SDL_RenderClear
- SDL_RenderClear(renderer);
- // SDL_RenderCopy
- SDL_RenderCopy(renderer, texture, NULL, &rect);
- // SDL_RenderPresent
- SDL_RenderPresent(renderer);
- }
- else if(event.type == SDL_WINDOWEVENT)
- {
- //If Resize
- SDL_GetWindowSize(window, &win_width, &win_height);
- //printf("SDL_WINDOWEVENT win_width:%d, win_height:%d\n",win_width,
- // win_height );
- }
- else if(event.type == SDL_QUIT) //退出事件
- {
- g_thread_exit = 1;
- }
- else if(event.type == SDL_QUIT_EVENT)
- {
- break;
- }
- }
yuv播放器测试,播放一个yuv文件,可以正常播放画面,调节画面大小。
SDL 音频播放相对视频播放器更为简单:
音频设备初始化并且读取音频数据:
打开音频设备:SDL_OpenAudio
开启音频设备播放
读取pcm文件,写入音频缓冲区
- if(SDL_Init(SDL_INIT_AUDIO))
- {
- fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
- return ret;
- }
-
- //open pcmfile
- audio_fd = fopen(path, "rb");
- if(!audio_fd)
- {
- fprintf(stderr, "Failed to open pcm file!\n");
- goto _FAIL;
- }
-
- g_audio_buf = (uint8_t *)malloc(PCM_BUFFER_SIZE);
-
- // SDL_AudioSpec
- spec.freq = 44100;
- spec.format = AUDIO_S16SYS;
- spec.channels = 2;
- spec.silence = 0;
- spec.samples = 1024;
- spec.callback = fill_audio_pcm;
- spec.userdata = NULL;
-
- if(SDL_OpenAudio(&spec, NULL))
- {
- fprintf(stderr, "Failed to open audio device, %s\n", SDL_GetError());
- goto _FAIL;
- }
-
- //play audio
- SDL_PauseAudio(0);
-
- int data_count = 0;
- while(1)
- {
- // read pcm
- read_buffer_len = fread(g_audio_buf, 1, PCM_BUFFER_SIZE, audio_fd);
- if(read_buffer_len == 0)
- {
- break;
- }
- data_count += read_buffer_len;
- printf("now playing %10d bytes data.\n",data_count);
- g_audio_end = g_audio_buf + read_buffer_len;
- g_audio_pos = g_audio_buf;
- //the main thread wait for a moment
- while(g_audio_pos < g_audio_end)
- {
- SDL_Delay(10);
- }
- }
- printf("play PCM finish\n");
- // SDL_CloseAudio
- SDL_CloseAudio();
回调函数将音频数据写入播放缓冲区:
- void mix_audio_pcm(void *udata, Uint8 *stream, int len)
- {
- SDL_memset(stream, 0, len);
-
- if(g_audio_pos >= g_audio_end)
- {
- return;
- }
-
- int remain_buffer_len = g_audio_end - g_audio_pos;
- len = (len < remain_buffer_len) ? len : remain_buffer_len;
- SDL_MixAudio(stream, g_audio_pos, len, SDL_MIX_MAXVOLUME/8);
- printf("len = %d\n", len);
- g_audio_pos += len;
- }
粉丝福利, 免费领取C++音视频学习资料包+学习路线大纲、技术视频/代码,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs)↓↓↓↓↓↓见下面↓↓文章底部点击免费领取↓↓
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。