以下利用SDL播放网络流,需要自己配置运行环境,包括SDL和FFmpeg

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
/*
#include "stdafx.h" #include <iostream> #define SDL_MAIN_HANDLED #include "SDL.h" int main()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
system("pause");
return 0;
}
*/
#include "stdafx.h" #ifdef __cplusplus
extern "C" {
#endif #include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include "SDL.h" #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h> #ifdef __cplusplus
}
#endif int ffplayer()
{
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
//char filepath[]="F:\\Work\\ffmpegdemo\\Debug\\Wildlife.wmv";
char rtspUrl[] = "rtmp://58.200.131.2:1935/livetv/cctv1"; //char rtspUrl[] = "rtmp://192.168.1.253/live/41";
av_register_all();//注册组件
avformat_network_init();//支持网络流
pFormatCtx = avformat_alloc_context();//初始化AVFormatContext
if (avformat_open_input(&pFormatCtx,/*filepath*/rtspUrl, NULL, NULL) != ) {//打开文件或网络流
printf("无法打开文件\n");
return -;
}
if (avformat_find_stream_info(pFormatCtx, NULL)<)//查找流信息
{
printf("Couldn't find stream information.\n");
return -;
}
videoindex = -;
for (i = ; i<pFormatCtx->nb_streams; i++) //获取视频流ID
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
break;
}
if (videoindex == -)
{
printf("Didn't find a video stream.\n");
return -;
}
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);//查找解码器
if (pCodec == NULL)
{
printf("Codec not found.\n");
return -;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL)<)//打开解码器
{
printf("Could not open codec.\n");
return -;
}
AVFrame *pFrame, *pFrameYUV;
pFrame = av_frame_alloc();//存储解码后AVFrame
pFrameYUV = av_frame_alloc();//存储转换后AVFrame
uint8_t *out_buffer;
out_buffer = new uint8_t[avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];//分配AVFrame所需内存
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);//填充AVFrame //------------SDL初始化--------
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -;
}
SDL_Window *screen = SDL_CreateWindow("RTSP Client Demo",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
, ,
SDL_WINDOW_RESIZABLE/* SDL_WINDOW_HIDDEN*/ | SDL_WINDOW_OPENGL);
if (!screen) {
printf("SDL: could not set video mode - exiting\n");
return -;
}
SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -, );
SDL_Texture* sdlTexture = SDL_CreateTexture(
sdlRenderer,
SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING,
pCodecCtx->width,
pCodecCtx->height); SDL_Rect rect;
SDL_Rect dstRect;
dstRect.x = ;
dstRect.y = ;
dstRect.w = ;
dstRect.h = ;
//-----------------------------
int ret, got_picture;
static struct SwsContext *img_convert_ctx = NULL;
int y_size = pCodecCtx->width * pCodecCtx->height; SDL_Event event;
AVPacket *packet = (AVPacket *)malloc(sizeof(AVPacket));//存储解码前数据包AVPacket
av_new_packet(packet, y_size);
//输出一下信息-----------------------------
printf("文件信息-----------------------------------------\n");
//av_dump_format(pFormatCtx,0,filepath,0);
printf("-------------------------------------------------\n");
//------------------------------
while ()//循环获取压缩数据包AVPacket
{
if (av_read_frame(pFormatCtx, packet) >= )
{
if (packet->stream_index == videoindex)
{
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//解码。输入为AVPacket,输出为AVFrame
if (ret < )
{
printf("解码错误\n");
return -;
}
if (got_picture)
{
//像素格式转换。pFrame转换为pFrameYUV。
//if(img_convert_ctx == NULL)
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, , pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
sws_freeContext(img_convert_ctx);
//------------SDL显示--------
rect.x = ;
rect.y = ;
rect.w = pCodecCtx->width;
rect.h = pCodecCtx->height; SDL_UpdateTexture(sdlTexture, &rect, pFrameYUV->data[], pFrameYUV->linesize[]);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, &rect, &dstRect);
SDL_RenderPresent(sdlRenderer);
//延时20ms
SDL_Delay();
//------------SDL-----------
}
}
}
av_free_packet(packet);
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
SDL_Quit();
exit();
break;
default:
break;
}
}
//sws_freeContext(img_convert_ctx);
SDL_DestroyTexture(sdlTexture);
delete[] out_buffer;
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx); return ;
} int _tmain(int argc, _TCHAR* argv[])
{
printf("hello\n"); ffplayer(); system("pause");
return ;
}

SDL 小例子的更多相关文章

  1. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  2. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  3. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  4. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  5. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  6. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  7. 【zTree】 zTree使用的 小例子

    使用zTree树不是第一次了  但是 还是翻阅着之前做的 对照着 使用起来比较方便  这里就把小例子列出来   总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先  在 ...

  8. js小例子(标签页)

    运用js写的一个小例子,实现点击不同的标签出现不同的内容: <!DOCTYPE html> <html> <head> <meta chaset=" ...

  9. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

随机推荐

  1. 基于DBMS_METADATA.GET_DDL函数批量导出索引的创建语句

    /* 首先要说的DBMS_METADATA.GET_DDL是个好函数呀!新项目不知道哪个缺心眼建的同构库,只是见了表结构,并没有健非主键外的索引,领导让追加一版,以前只是会用视图拼sql创建,今天有学 ...

  2. 1.3.1 Lock接口及其实现

    1.锁的本质 2.Lock接口使用ReentrenLock 3.读写锁使用 4.读写锁实现 Lock接口方法 有点意思的是lockInterruptibly(), 只要没有获取到锁就会一直等待,直到某 ...

  3. MyBatis使用小结

  4. 洛谷P2178 [NOI2015]品酒大会 后缀数组+单调栈

    P2178 [NOI2015]品酒大会 题目链接 https://www.luogu.org/problemnew/show/P2178 题目描述 一年一度的"幻影阁夏日品酒大会" ...

  5. PHP身份证验证

    /** * 身份证号码验证(真正要调用的方法) * @param $id_card 身份证号码 */function validation_filter_id_card($id_card){ if ( ...

  6. Python可修改和不可修改类型变量(mutuable and immutuable)

    通俗的讲,可修改可以理解为可以在数据所在内存地址直接修改,而不可修改则意味着一旦修改便是创建新的数据对象,而不是在原来的对象内存地址修改 1,Mutuable object [sourcecode l ...

  7. 分享一些JVM常见的面试题(转)

    出处:  分享一些JVM常见的面试题 前言: 在遨游了一番 Java Web 的世界之后,发现了自己的一些缺失,所以就着一篇深度好文:知名互联网公司校招 Java 开发岗面试知识点解析 ,来好好的对 ...

  8. FGL内置函数大全

    内置功能摘要: 内置的功能是什么?内置的功能列表支持的功能列表键代码表另请参阅:效用函数,变量,函数,操作符,内置类. ---------------------------------------- ...

  9. XSS跨站攻击靶场-通关笔记

    XSS攻击是Web攻击中最常见的攻击手法之一,XSS中文名跨站脚本攻击,该攻击是指攻击者在网页中嵌入恶意的客户端脚本,通常是使用JS编写的恶意代码,当正常用户访问被嵌入代码的页面时,恶意代码将会在用户 ...

  10. Linux/CentOS 配置Mysql-server过程和遇到错误解决方法

    第一步:下载mysql-server 方法1.wget url(你所要下载的链接,可以从mysq官网查找)到当前目录下 方法2.到mysql官网下载包之后通过xftp传到linux 第二步:解压tar ...