以下利用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. centos7yum的配置

    首先呢,本人的centos的版本是7.5 这里我主要介绍的是通过挂在系统镜像文件和指定网络epel镜像源. 首先指定本地的光盘当做yum的仓库 1.删除/etc/yum.repos.d的所有文件 2. ...

  2. 真理胜于一切 JAVA模拟表单提交

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  3. SpringBoot 对IBM MQ进行数据监听接收以及数据发送

    一.需求介绍 后端使用Spring Boot2.0框架,要实现IBM MQ的实时数据JMS监听接收处理,并形成回执通过MQ队列发送. 二.引入依赖jar包 <dependency> < ...

  4. Photon Server初识(五) --- 客户端连接服务端

    准备:Unity开开发IDE. 一.新建Unity3D项目 导入包,在资源下新建目录 Plugins .在之前解压的SDK目录 lib中找到 Photon3Unity3D.dll,拖到新建的目前下 二 ...

  5. 【第二季】CH11_ ZYNQ软硬调试高级技巧

    [第二季]CH11_ ZYNQ软硬调试高级技巧 软件和硬件的完美结合才是SOC的优势和长处,那么开发ZYNQ就需要掌握软件和硬件开发的调试技巧,这样才能同时分析软件或者硬件的运行情况,找到问题,最终解 ...

  6. 论文阅读:Adaptive NMS: Refining Pedestrian Detection in a Crowd

    论文阅读:Adaptive NMS: Refining Pedestrian Detection in a Crowd 2019年04月11日 23:08:02 Kivee123 阅读数 836   ...

  7. MySql 8.0.11 客户端连接失败:2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: ....

    近期,换了新笔记本,重新安装了MySql数据库和客户端工具Navicat Premium 12.我是从官网上下载的MySql数据库,版本为8.0.11,链接:https://dev.mysql.com ...

  8. winfrom 保存图片

    private void btnSave_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); ...

  9. C语言 读取配置文件

    配置文件截图: 读取结果截图: 代码转自:http://www.tuicool.com/articles/Zb2iIn 附代码: // ReadConfig.cpp : 定义控制台应用程序的入口点. ...

  10. SP338ROADS题解--最短路变式

    题目链接 https://www.luogu.org/problemnew/show/SP338 分析 联想到不久前做过的一道题\(Full\) \(Tank\),感觉可以用优先队列做,于是写了\(d ...