以下利用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. Python基础总结之第七天开始【总结字符串、列表、元组的常用方法】(新手可相互督促)

    前面的笔记说,python中的一切数据类型都是对象 我们在细化下就是:对象可由两部分组成:对象数据和对象方法 针对不同类型的数据对象,有不同的操作对象的方法. 那么我们开始看下字符串对象的常用方法: ...

  2. 【转】mysql分库分表,数据库分库分表思路

    原文:https://www.cnblogs.com/butterfly100/p/9034281.html 同类参考:[转]数据库的分库分表基本思想 数据库分库分表思路   一. 数据切分 关系型数 ...

  3. shiro小记

    今天主要看了Shiro的认证,授权功能初步了解了一下,其他的功能用的不多,之后再看. 先说一下Shiro的三个核心概念: 1.Subject: 代表当前正在执行操作的用户,但Subject代表的可以是 ...

  4. Photon Server 实现注册与登录(一) --- Hibernate整合到项目中

    本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...

  5. VS2017生成一个简单的DLL文件 和 LIB文件——C语言

    下面我们将用两种不同的姿势来用VS2017生成dll文件(动态库文件)和lib文件(静态库文件),这里以C语言为例,用最简单的例子,来让读者了解如何生成dll文件(动态库文件) 生成动态库文件 姿势一 ...

  6. configure,make和make install关系

    linux编译安装中configure.make和make install各自的作用 ./configure是用来检测你的安装平台的目标特征的.configure根据给定的参数和系统环境会生成Make ...

  7. Mybatis 多个参数传入的多种方法

    ist<XXXBean> getXXXBeanList(HashMap map); <select id="getXXXBeanList" parameterTy ...

  8. 微信小程序data-传参

    <view bindtap="change" data-index="{{index}}">哈哈</view> change(event ...

  9. STM32工程模版

    STM32工程模版,看过来 ST库源码去官方下载 创建工程目录 doc:存放说明文档 lib:存放库文件 listing:存放编译产生的中间文件 output:存放生成的文件 project:存放工程 ...

  10. VScode 配置为 LaTeX 编辑器(IDE)

    VScode 配置为 LaTeX IDE 在Windows中,配置VScode作为LaTeX的编辑器(IDE),并使用SumatraPDF预览PDF文件.主要是LaTeX Workshop扩展的设置, ...