AndroidMP4视频文件推送rtmp

2021/10/23 23:41:11

本文主要是介绍AndroidMP4视频文件推送rtmp,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

AndroidMP4视频文件推送rtmp,直播

  • RTMP介绍
    • 传送协议
    • 功能实现
    • 相关代码
  • 配置

RTMP介绍

RTMP是Real Time Messaging Protocol(实时消息传输协议)的首字母缩写。该协议基于TCP,是一个协议族,包括RTMP基本协议及RTMPT/RTMPS/RTMPE等多种变种。RTMP是一种设计用来进行实时数据通信的网络协议,主要用来在Flash/AIR平台和支持RTMP协议的流媒体/交互服务器之间进行音视频和数据通信。支持该协议的软件包括Adobe Media Server/Ultrant Media Server/red5等。RTMP与HTTP一样,都属于TCP/IP四层模型的应用层。RTMP和RTSP协议已经成为直播的主流协议,对于安防监控等场景应用广泛。

传送协议

RTMP(Real Time Messaging Protocol)实时消息传送协议是Adobe Systems公司为Flash播放器和服务器之间音频、视频和数据传输 开发的开放协议。
它有多种变种:
1)RTMP工作在TCP之上,默认使用端口1935;
2)RTMPE在RTMP的基础上增加了加密功能;
3)RTMPT封装在HTTP请求之上,可穿透防火墙;
4)RTMPS类似RTMPT,增加了TLS/SSL的安全功能;

功能实现

分为几个步骤:
1.将本地保存的MP4视频文件,读取进行编码H264.H264格式字节。
2.将编码后的字节帧数组组装
3.通过ffmpeg 进行转化
4.链接RTMP远程地址
5.连接成功后将帧数据ffmpeg写入到RTMP连接中
6.用户就能通过RTMP地址查看视频了

相关代码

package com.onzhou.ffmpeg.streamer;

/**
 * @anchor: andy
 * @date: 2018-10-29
 * @description:
 */
public class NativeStreamer {

    static {
        System.loadLibrary("native-stream");
    }

    public native int startPublish(String mp4Path, String stream);

    public native void stopPublish();

}
package com.onzhou.ffmpeg.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.os.AsyncTaskCompat;
import android.view.View;
import android.widget.TextView;

import com.onzhou.ffmpeg.base.AbsBaseActivity;
import com.onzhou.ffmpeg.streamer.NativeStreamer;
import com.onzhou.ffmpeg.streamer.R;
import com.onzhou.ffmpeg.task.AssertReleaseTask;

import java.io.File;

import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @anchor: andy
 * @date: 2018-10-29
 * @description:
 */
public class NativeStreamActivity extends AbsBaseActivity implements AssertReleaseTask.ReleaseCallback {

    /**
     * 推流地址
     */
    
    private static final String PUBLISH_ADDRESS = "rtmp://192.168.1.102:1935/onzhou/live";

    /**
     * 开始推流按钮
     */
    private TextView mBtnStartPublish, mBtnStopPublish;

    private NativeStreamer nowStreamer;

    private Disposable publishDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stream);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setupViews();
        setupVideo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (publishDisposable != null && !publishDisposable.isDisposed()) {
            publishDisposable.dispose();
            publishDisposable = null;
        }
    }

    private void setupViews() {
        mBtnStartPublish = (TextView) findViewById(R.id.btn_start_publish);
        mBtnStopPublish = (TextView) findViewById(R.id.btn_stop_publish);
    }

    public void setupVideo() {
        AssertReleaseTask videoReleaseTask = new AssertReleaseTask(this, "input.mp4", this);
        AsyncTaskCompat.executeParallel(videoReleaseTask);
    }

    @Override
    public void onReleaseSuccess(String videoPath) {
        mBtnStartPublish.setEnabled(true);
    }

    public void onStartClick(View view) {
        mBtnStartPublish.setEnabled(false);
        mBtnStopPublish.setEnabled(true);
        if (nowStreamer == null) {
            nowStreamer = new NativeStreamer();
        }
        if (publishDisposable == null) {
            publishDisposable = Schedulers.newThread().scheduleDirect(new Runnable() {
                @Override
                public void run() {
                    final File inputVideo = new File(getExternalFilesDir(null), "input.mp4");
                    nowStreamer.startPublish(inputVideo.getAbsolutePath(), PUBLISH_ADDRESS);
                }
            });
        }
    }

    public void onStopClick(View view) {
        if (nowStreamer != null) {
            nowStreamer.stopPublish();
        }
        if (publishDisposable != null) {
            publishDisposable.dispose();
            publishDisposable = null;
        }
        mBtnStartPublish.setEnabled(true);
        mBtnStopPublish.setEnabled(false);
    }
}

github地址:android_rtmp视频文件播放
图片描述
标红的工程

配置

备注:根据自己的CPU架构需要可以配置多款生成动态依赖库,本人使用arm64-v8a
根据自己的CPU架构需要可以配置多款生成动态依赖库,本人使用arm64-v8a



这篇关于AndroidMP4视频文件推送rtmp的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程