android调用ffmpeg解析rtsp协议的视频流

文章目录

  • 一、背景
  • 二、解析rtsp数据
    • 1、C层功能代码
    • 2、jni层的定义
    • 3、app层的调用
  • 三、源码下载

一、背景

本demo主要介绍android调用ffmpeg中的接口解析rtsp协议的视频流(不解析音频),得到yuv数据,把yuv转bitmap在android设备上显示,涉及到打开视频、解封装、解码、回调yuv数据。学习记录帖,C语言小白,不足的地方请指正,多谢!

二、解析rtsp数据

1、C层功能代码

Decoder.h


#ifndef DECODERTSP_DECODER_H
#define DECODERTSP_DECODER_H
#include 
#include "include/jniLog.h"
extern "C"
{
   
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
};

using namespace std;
// 定义一个回调函数类型(typedef 用于为已有的数据类型创建一个别名)
typedef std::function<void(uint8_t *buf, int size)> Callback;

// 解析rtsp视频流
int ReadFrameAndDecoder(const char *url,Callback callback);
void DoStop();
#endif //DECODERTSP_DECODER_H

Decoder.cpp


#include "include/Decoder.h"
#include "include/jniLog.h"
bool isStop = false;
int ReadFrameAndDecoder(const char* m_Url,Callback callback){
   
    char url[100] = {
   0};
    strcpy(url,m_Url);
    AVFormatContext *pFormatCtx = avformat_alloc_context();

    AVDictionary *options = NULL;
    av_dict_set(&options, "buffer_size", "1024000", 0);// 设置缓冲区大小
    av_dict_set(&options, "stimeout", "20000000", 0);
    av_dict_set(&options, "max_delay", "30000000", 0);
//    av_dict_set(&options, "rtsp_transport", "tcp", 0); //使用 TCP 传输

    LOGI("ReadFrameAndDecoder:url = %s",url);
    if (avformat_open_input(&pFormatCtx, url, NULL, NULL) != 0)     // 打开视频文件
        return -1;

    if (avformat_find_stream_info(pFormatCtx, NULL) < 0)    // 查找视频流
        return -2;

    //视频解码,需要找到视频对应的AVStream所在pFormatCtx->streams的索引位置
    int video_stream_idx = -1;
    for (int i = 0; i < pFormatCtx->nb_streams; i++

你可能感兴趣的:(c++,android,ndk,android,jni,android集成ffmpeg)