android手机推送RTMP直播流到nginx服务器纯代码实现

Yasea是一个100%纯Java写的,完全开源的安卓直播推流客户端。它使用MediaCodec系统接口对H.264和AAC进行硬编码,封装FLV格式,推送至RTMP服务器。延迟达到毫秒级别。

特性列表:

  • Android Min API 16 (Android 4.1 Jelly Bean)

  • H.264/AAC硬编码

  • RTMP推流,事件状态回调

  • 手机横竖屏动态切换

  • 前后摄像头热切换

  • 推流过程随时录制MP4,支持暂停和恢复

下面是yasea-master的工程源码截图:

编译之后,生成apk,在android机器上即可安装运行。

在nginx服务器上安装rtmp直播模块:

1、下载nginx-rtmp-module模块

git clone https://github.com/arut/nginx-rtmp-module.git

如果git没有识别,通过下面命令安装git。

yum install -y git

  2、下载nginx模块

假设你之前已经安装好了nginx,且安装目录是/usr/local/nginx。现在在别的目录(这里是/sg)下载一个同样版本的nginx,然后解压。

[root@localhost]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

[root@localhost]# tar -zxvf nginx-1.16.1.tar.gz

 

  3、 进入到解压后的nginx的目录

./configure --prefix=/usr/local/nginx --add-module=/sg/nginx-rtmp-module --with-http_ssl_module

--prefix是nginx的安装目录(以前的),因为我们编译新的nginx最后要拷贝到这个目录。--add-module指定rtmp模块的路径

 

4、执行make命令

make

make 编译之后的nginx二进制文件就在objs目录下,将它覆盖你原来的nginx执行文件。

cp /sg/nginx-1.16.1/objs/nginx  /usr/local/nginx/

5、配置

events {
    worker_connections  1024;
}
rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        application hls {
            live on;
            #record all;
            #record_path /home/live_record;
            #record_max_size 200M;
            hls on;
            hls_path /root/lshTool/nginx-1.16.0/live/hls;
            hls_fragment 2s;
            #publish_notify on; #表示开启鉴权服务
            #on_publish http://127.0.0.1:80/publish; #鉴权地址
        }
    }
}

6、再次启动nginx即可接受手机端的rtmp推流。

7、报错解决

执行添加rtmp模块的时候如果报 ./configure: error: SSL modules require the OpenSSL library.
执行以下命令然后继续添加rtmp模块即可

[root@localhost]# yum -y install openssl openssl-devel

./configure: error: C compiler cc is not found
执行以下命令然后继续添加rtmp模块即可

[root@localhost]# yum install  gcc

[root@localhost]# gcc -v

 

运行效果:

在桌面端利用rtmp播放器播放直播流:

需要源码(网上也可也搜素下载)和指导的朋友可以加我QQ(395702361)。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现这个功能,你可以使用以下步骤: 1. 使用OpenCV库来解码和编码视频流。你可以使用cv::VideoCapture来解码视频文件,并使用cv::VideoWriter来编码视频文件。 2. 使用FFmpeg库来推送RTMP流。你需要使用FFmpeg的API来打开一个RTMP流,并使用avcodec_encode_video2()函数将OpenCV编码后的视频帧推送到流中。 3. 将RTMP推送Nginx服务器。你可以使用RTMP协议将视频流推送Nginx服务器。在Nginx服务器上,你需要配置一个RTMP模块,并使用推流URL将视频流推送服务器上。 以下是一个简单的示例代码,可以实现将OpenCV视频流编码并推送Nginx服务器。 ```cpp #include <opencv2/opencv.hpp> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/opt.h> #include <libavutil/imgutils.h> #include <libavutil/time.h> #include <librtmp/rtmp.h> int main(int argc, char *argv[]) { // OpenCV video capture and video writer cv::VideoCapture cap(argv[1]); cv::Mat frame; cv::VideoWriter writer("output.mp4", cv::VideoWriter::fourcc('M','J','P','G'), 25, cv::Size(640, 480)); // FFmpeg RTMP stream av_register_all(); avcodec_register_all(); AVFormatContext *fmt_ctx = nullptr; AVOutputFormat *out_fmt = nullptr; AVStream *out_stream = nullptr; AVCodec *codec = nullptr; AVCodecContext *codec_ctx = nullptr; AVPacket pkt; int ret = 0; // Open RTMP stream RTMP *rtmp = RTMP_Alloc(); RTMP_Init(rtmp); RTMP_SetupURL(rtmp, "rtmp://localhost/live/mystream"); RTMP_EnableWrite(rtmp); // Connect to RTMP stream if (!RTMP_Connect(rtmp, nullptr)) { if (!RTMP_ConnectStream(rtmp, 0)) { // Create AVFormatContext avformat_alloc_output_context2(&fmt_ctx, nullptr, "flv", "rtmp://localhost/live/mystream"); if (!fmt_ctx) { fprintf(stderr, "Could not create output context\n"); return -1; } // Create video stream out_fmt = fmt_ctx->oformat; codec = avcodec_find_encoder(out_fmt->video_codec); out_stream = avformat_new_stream(fmt_ctx, codec); if (!out_stream) { fprintf(stderr, "Could not create video stream\n"); return -1; } codec_ctx = avcodec_alloc_context3(codec); avcodec_parameters_to_context(codec_ctx, out_stream->codecpar); codec_ctx->width = 640; codec_ctx->height = 480; codec_ctx->time_base = {1, 25}; codec_ctx->framerate = {25, 1}; codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; avcodec_open2(codec_ctx, codec, nullptr); // Write header avformat_write_header(fmt_ctx, nullptr); // Encode and push frames while (cap.read(frame)) { // Encode frame ret = avcodec_send_frame(codec_ctx, av_frame); if (ret < 0) { fprintf(stderr, "Error sending frame to encoder\n"); break; } while (ret >= 0) { ret = avcodec_receive_packet(codec_ctx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error receiving packet from encoder\n"); break; } // Write packet pkt.stream_index = out_stream->index; av_interleaved_write_frame(fmt_ctx, &pkt); av_packet_unref(&pkt); // Push packet to RTMP stream RTMP_Write(rtmp, (char *)pkt.data, pkt.size); } // Write frame to OpenCV video writer writer.write(frame); } // Write trailer av_write_trailer(fmt_ctx); // Close RTMP stream RTMP_Close(rtmp); RTMP_Free(rtmp); // Cleanup avcodec_free_context(&codec_ctx); avformat_free_context(fmt_ctx); } } return 0; } ``` 注意,这只是一个简单的示例代码,还需要进行更多的错误检查和异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wave12_mp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值