SDL2.0学习

http://www.ffmpeg.org/download.html

http://doc.okbase.net/leixiaohua1020/archive/110977.html  //视频

http://blog.csdn.net/leixiaohua1020/article/details/46890259 //音频

安装:

sudo apt-get install mercurial

hg clone https://hg.libsdl.org/SDL SDL2.0

该安装包不包括SDL_image , SDL_ttf, SDL_mixer

SDL播放视频的代码流程如下所示。
初始化: 

SDL_Init(): 初始化SDL。 
SDL_CreateWindow(): 创建窗口(Window)。 
SDL_CreateRenderer(): 基于窗口创建渲染器(Render)。 
SDL_CreateTexture(): 创建纹理(Texture)。 

循环渲染数据: 

SDL_UpdateTexture(): 设置纹理的数据。 
SDL_RenderCopy(): 纹理复制给渲染器。 
SDL_RenderPresent(): 显示。

上篇文章分析了该流程中的第4个函数SDL_CreateTexture()。本文继续分析该流程中的第5个函数SDL_UpdateTexture()。

 1 {
 2     SDL_Window *screen;
 3     SDL_Renderer* sdlRenderer;
 4     SDL_Texture* sdlTexture;
 5     SDL_Rect sdlRect;
 6 
 7     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
 8         printf( "Could not initialize SDL - %s
", SDL_GetError());
 9         return -1; 
10     }
11 
12     screen = SDL_CreateWindow("Simplest ffmpeg player's Window",     SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
13         screen_w, screen_h,
14         SDL_WINDOW_OPENGL);
15 
16     sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
17     //IYUV: Y + U + V  (3 planes)
18     //YV12: Y + V + U  (3 planes)
19     sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);
20 
21 #if 0
22     SDL_UpdateTexture( sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0] );
23 #else
24     SDL_UpdateYUVTexture(sdlTexture, &sdlRect,
25                 pFrameYUV->data[0], pFrameYUV->linesize[0],
26                 pFrameYUV->data[1], pFrameYUV->linesize[1],
27                 pFrameYUV->data[2], pFrameYUV->linesize[2]);
28 #endif
29     SDL_RenderClear( sdlRenderer );
30     SDL_RenderCopy( sdlRenderer, sdlTexture,  NULL, &sdlRect);
31     SDL_RenderPresent( sdlRenderer );
32     SDL_Delay(40);
33 }

音频:

typedef struct SDL_AudioSpec
{
    int freq;                   /**< DSP frequency -- samples per second */
    SDL_AudioFormat format;     /**< Audio data format */
    Uint8 channels;             /**< Number of channels: 1 mono, 2 stereo */
    Uint8 silence;              /**< Audio buffer silence value (calculated) */
    Uint16 samples;             /**< Audio buffer size in samples (power of 2) */
    Uint16 padding;             /**< Necessary for some compile environments */
    Uint32 size;                /**< Audio buffer size in bytes (calculated) */
    SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
    void *userdata;             /**< Userdata passed to callback (ignored for NULL callbacks). */
} SDL_AudioSpec audio;

SDL_OpenAudio(); 打开音频

SDL_PauseAudio(0); play

SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);

  

播放音频的例子

 1 #include <stdio.h>
 2 #include "SDL2/SDL.h"
 3 
 4 //Buffer:
 5 //|-----------|-------------|
 6 //chunk-------pos---len-----|
 7 static  unsigned char *audio_chunk;
 8 static  unsigned int audio_len;
 9 static  unsigned char *audio_pos;
10 
11 /* Audio Callback
12  * The audio function callback takes the following parameters:
13  * stream: A pointer to the audio buffer to be filled
14  * len: The length (in bytes) of the audio buffer
15  *
16  */
17 void  fill_audio(void *udata,unsigned char *stream,int len){
18     //SDL 2.0
19     SDL_memset(stream, 0, len);
20     if(audio_len==0)        /*  Only  play  if  we  have  data  left  */
21         return;
22     len=(len>audio_len?audio_len:len);   /*  Mix  as  much  data  as  possible  */
23 
24     SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
25     audio_pos += len;
26     audio_len -= len;
27 }
28 int main(int argc, char* argv[])
29 {
30     //Init
31     if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
32         printf( "Could not initialize SDL - %s
", SDL_GetError());
33         return -1;
34     }
35     //SDL_AudioSpec
36     SDL_AudioSpec wanted_spec;
37     wanted_spec.freq = 44100;
38     wanted_spec.format = AUDIO_S16SYS;
39     wanted_spec.channels = 2;
40     wanted_spec.silence = 0;
41     wanted_spec.samples = 1024;
42     wanted_spec.callback = fill_audio;
43 
44     if (SDL_OpenAudio(&wanted_spec, NULL)<0){
45         printf("can't open audio.
");
46         return -1;
47     }
48 
49     FILE *fp=fopen("output.pcm","rb+");
50     if(fp==NULL){
51         printf("cannot open this file
");
52         return -1;
53     }
54 
55     int pcm_buffer_size=4096;
56     char *pcm_buffer=(char *)malloc(pcm_buffer_size);
57     int data_count=0;
58     //Play
59     SDL_PauseAudio(0);
60 
61     while(1){
62         if (fread(pcm_buffer, 1, pcm_buffer_size, fp) != pcm_buffer_size){
63             // Loop
64             fseek(fp, 0, SEEK_SET);
65             fread(pcm_buffer, 1, pcm_buffer_size, fp);
66             data_count=0;
67         }
68         printf("Now Playing %10d Bytes data.
",data_count);
69         data_count+=pcm_buffer_size;
70         //Set audio buffer (PCM data)
71         audio_chunk = (unsigned char*) pcm_buffer;
72         //Audio buffer length
73         audio_len =pcm_buffer_size;
74         audio_pos = audio_chunk;
75 
76         while(audio_len>0)//Wait until finish
77             SDL_Delay(1);
78     }
79     free(pcm_buffer);
80     SDL_Quit();
81     return 0;
82   
原文地址:https://www.cnblogs.com/chencesc/p/5759850.html