Source Address
Https://github.com/979451341/Rtmp
1. Configuring the RTMP Server
I don't say much about this. Two blogs are on Mac and Windows, and you'll follow
Mac to build rtmp server
https://www.jianshu.com/p/6fcec3b9d644
This is on Windows, rtmp Server Setup (Crtmpserver and Nginx)
Https://www.jianshu.com/p/c71cc39f72ec
2. About the IP address of the push-stream output I'll talk.
I am here is the mobile phone hotspot, the computer connected to the mobile phone, the RTMP server's push stream address has localhost, the server on the computer, For the computer this localhost is 127.0.0.1, but for external such as mobile phone, you can not use localhost, but with this computer in this hotspot is the LAN IP address, not 127.0.0.1 This only represents the IP address of the device node, this you need to go to the phone settings --"More--" mobile network sharing-"portable WLAN hotspot-" Management device list, you can see the computer's LAN IP address
3. Talk about Code
Register the component, the second, if not added, you cannot get network information, such as a URL
av_register_all();avformat_network_init();
Get information about the input video, and create an environment for the output URL address
av_dump_format(ictx, 0, inUrl, 0); ret = avformat_alloc_output_context2(&octx, NULL, "flv", outUrl); if (ret < 0) { avError(ret); throw ret; }
Put the input video stream into the output stream you just created
for (i = 0; i < ictx->nb_streams; i++) { //获取输入视频流 AVStream *in_stream = ictx->streams[i]; //为输出上下文添加音视频流(初始化一个音视频流容器) AVStream *out_stream = avformat_new_stream(octx, in_stream->codec->codec); if (!out_stream) { printf("未能成功添加音视频流\n"); ret = AVERROR_UNKNOWN; } if (octx->oformat->flags & AVFMT_GLOBALHEADER) { out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; } ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar); if (ret < 0) { printf("copy 编解码器上下文失败\n"); } out_stream->codecpar->codec_tag = 0;
Out_stream->codec->codec_tag = 0;
}
Open the output URL and write the header data
//打开IO ret = avio_open(&octx->pb, outUrl, AVIO_FLAG_WRITE); if (ret < 0) { avError(ret); throw ret; } logd("avio_open success!"); //写入头部信息 ret = avformat_write_header(octx, 0); if (ret < 0) { avError(ret); throw ret; }
And then start looping the decoding and pushing the stream data
First get a frame of data
ret = Av_read_frame (Ictx, &PKT);
Then give this frame of data configuration parameters, if the original configuration does not have time to configure the time, I am here to mention two concepts
DTS (decoding timestamp) and PTS (display timestamp) are timestamps of the decoder relative to the SCR (System reference) when decoding and displaying the frames, respectively. SCR can be understood as the time when the decoder should start reading data from disk.
if (pkt.pts == AV_NOPTS_VALUE) { //AVRational time_base:时基。通过该值可以把PTS,DTS转化为真正的时间。 AVRational time_base1 = ictx->streams[videoindex]->time_base; int64_t calc_duration = (double) AV_TIME_BASE / av_q2d(ictx->streams[videoindex]->r_frame_rate); //配置参数 pkt.pts = (double) (frame_index * calc_duration) / (double) (av_q2d(time_base1) * AV_TIME_BASE); pkt.dts = pkt.pts; pkt.duration = (double) calc_duration / (double) (av_q2d(time_base1) * AV_TIME_BASE); }
Adjust the playback time, that is, we decoded the video before recording a current time, and then in the loop to push the current time, and then to get a moment, the difference is our video should play time, if the video playback too fast on the process sleep Pkt.dts minus the actual playing time difference
if (pkt.stream_index == videoindex) { AVRational time_base = ictx->streams[videoindex]->time_base; AVRational time_base_q = {1, AV_TIME_BASE}; //计算视频播放时间 int64_t pts_time = av_rescale_q(pkt.dts, time_base, time_base_q); //计算实际视频的播放时间 int64_t now_time = av_gettime() - start_time; AVRational avr = ictx->streams[videoindex]->time_base; cout << avr.num << " " << avr.den << " " << pkt.dts << " " << pkt.pts << " " << pts_time << endl; if (pts_time > now_time) { //睡眠一段时间(目的是让当前视频记录的播放时间与实际时间同步) av_usleep((unsigned int) (pts_time - now_time)); } }
If the delay, the time that the configuration of this frame is recorded should be changed
//计算延时后,重新指定时间戳 pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); pkt.duration = (int) av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
Callback the time parameter of this frame, where the interface is instantiated in Mainactivity, showing the time of playback
int res = FFmpegHandle.setCallback(new PushCallback() { @Override public void videoCallback(final long pts, final long dts, final long duration, final long index) { runOnUiThread(new Runnable() { @Override public void run() { if(pts == -1){ tvPushInfo.setText("播放结束"); return ; } tvPushInfo.setText("播放时间:"+dts/1000+"秒"); } }); } });
The segment code then calls the C language's Setcallback function, obtains an instance of the interface, and the interface's Videocallback function reference, which also calls this function to initialize the time display
//转换为全局变量pushCallback = env->NewGlobalRef(pushCallback1);if (pushCallback == NULL) { return -3;}cls = env->GetObjectClass(pushCallback);if (cls == NULL) { return -1;}mid = env->GetMethodID(cls, "videoCallback", "(JJJJ)V");if (mid == NULL) { return -2;}env->CallVoidMethod(pushCallback, mid, (jlong) 0, (jlong) 0, (jlong) 0, (jlong) 0);
This time we go back to the loop to push the flow of a frame of data when calling the Videocallback function
env->CallVoidMethod(pushCallback, mid, (jlong) pts, (jlong) dts, (jlong) duration, (jlong) index);
It then outputs the data to the output URL and releases the data from that frame.
ret = av_interleaved_write_frame(octx, &pkt); av_packet_unref(&pkt);
Freeing resources
//关闭输出上下文,这个很关键。if (octx != NULL) avio_close(octx->pb);//释放输出封装上下文if (octx != NULL) avformat_free_context(octx);//关闭输入上下文if (ictx != NULL) avformat_close_input(&ictx);octx = NULL;ictx = NULL;env->ReleaseStringUTFChars(path_, path);env->ReleaseStringUTFChars(outUrl_, outUrl);
Last callback time display, say play end
callback(env, -1, -1, -1, -1);
4. About receiving push stream data
I am using VLC, this Mac and Windows have version, file--"OPEN NETWORK, enter the previous output URL can be." It is important to note that you first turn on the app and then use VLC to open the URL.
The effect is as follows
Reference articles
Https://www.jianshu.com/p/dcac5da8f1da
This blogger is really skilled at pushing the stream, and if you want to enter it, you can look at his blog.
Android audio and video in depth 15 FFmpeg implementation based on RTMP protocol push stream (with source download)