搞了一个晚上没结果,来救助 -- ffmpeg 向srs推流rtmp
百灵工作室 2016-03-22 03:17:52 rtmp用虚拟机搭建SRS,
./srs -c conf/hls.conf
./nginx
....
代码片段,不太好贴,贴出关键部分
。。。。。。
int ret = 0;
if (!memcmp(filename, "rtmp", 4)) {
ret = avformat_alloc_output_context2(&m_pFormatCtx, NULL, "flv", filename);
//m_pFormatCtx->oformat->name = "flv";
//m_pFormatCtx->oformat->extensions = "flv";
//m_pFormatCtx->oformat->mime_type = "application/flv";
m_pFormatCtx->oformat->video_codec = AV_CODEC_ID_H264;
m_pFormatCtx->oformat->audio_codec = AV_CODEC_ID_AAC;
} else {
ret = avformat_alloc_output_context2(&m_pFormatCtx, NULL, "f4v", filename);
}
if ((ret < 0) || (m_pFormatCtx == NULL))
{
//TODO: print messages for errors
return false;
}
//Add video stream
add_stream(m_pFormatCtx->oformat->video_codec);
//Add audio stream
add_stream(m_pFormatCtx->oformat->audio_codec);
//Open codec
if (bNeedEncoder)
{
open_video(m_VideoStream.st->codec->codec, m_videoOptions);
open_audio(m_AudioStream.st->codec->codec, m_audioOptions);
}
if (!(m_pFormatCtx->oformat->flags & AVFMT_NOFILE)) {
ret = avio_open(&m_pFormatCtx->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open output file '%s'", filename);
CleanUp();
return false;
}
}
av_dump_format(m_pFormatCtx, 0, filename, 1);
/* Write the stream header, if any. */
ret = avformat_write_header(m_pFormatCtx, &m_fmtOptions);
if (ret < 0) {
CleanUp();
return false;
}
m_isOpened = true;
return true;
。。。。。。
如果ret = avformat_alloc_output_context2(&m_pFormatCtx, NULL, "flv", filename);
此处使用f4v,则ret = avformat_write_header(m_pFormatCtx, &m_fmtOptions);
始终返回-22
如果使用flv,则ret = avformat_write_header(m_pFormatCtx, &m_fmtOptions);
返回0,ok,但srs服务器日志显示如下
----------------------------------------------------------------------------------------------------
[2016-03-22 15:13:53.990][trace][3545][119] RTMP client ip=192.168.177.1
[2016-03-22 15:13:53.991][trace][3545][119] simple handshake success.
[2016-03-22 15:13:53.991][trace][3545][119] connect app, tcUrl=rtmp://192.168.177.128:1935/live, pageUrl=, swfUrl=, schema=rtmp, vhost=__defaultVhost__, port=1935, app=live, args=null
[2016-03-22 15:13:53.991][trace][3545][119] out chunk size to 60000
[2016-03-22 15:13:54.040][trace][3545][119] client identified, type=fmle-publish, stream_name=stream2, duration=-1.00
[2016-03-22 15:13:54.040][trace][3545][119] source url=/live/stream2, ip=192.168.177.1, cache=1, is_edge=0, source_id=-1[-1]
[2016-03-22 15:13:54.090][trace][3545][119] hls: win=60.00, frag=10.00, prefix=, path=./objs/nginx/html, m3u8=[app]/[stream].m3u8, ts=[app]/[stream]-[seq].ts, aof=2.00, floor=0, clean=1, waitk=1, dispose=0
[2016-03-22 15:13:54.102][trace][3545][119] start publish mr=0/350, p1stpt=20000, pnt=20000, tcp_nodelay=0, rtcid=120
[2016-03-22 15:13:54.142][trace][3545][119] got metadata, width=640, height=480, vcodec=7, acodec=10
[2016-03-22 15:13:54.142][error][3545][119][11] avc decode sequenc header failed. ret=3001(Resource temporarily unavailable)
[2016-03-22 15:13:54.142][error][3545][119][11] source codec demux video failed. ret=3001(Resource temporarily unavailable)
[2016-03-22 15:13:54.142][error][3545][119][11] source process video message failed. ret=3001(Resource temporarily unavailable)
[2016-03-22 15:13:54.142][error][3545][119][11] fmle process publish message failed. ret=3001(Resource temporarily unavailable)
[2016-03-22 15:13:54.142][error][3545][119][11] thread process message failed. ret=3001(Resource temporarily unavailable)
[2016-03-22 15:13:54.142][warn][3545][119][11] thread recv cycle failed, ignored and retry, ret=3001
[2016-03-22 15:13:54.142][error][3545][119][11] recv thread failed. ret=3001(Resource temporarily unavailable)
[2016-03-22 15:13:54.142][trace][3545][119] unpublish drop ts segment, sequence_no=0, uri=stream2-0.ts, duration=0.00, start=193273480080
[2016-03-22 15:13:54.142][trace][3545][119] cleanup when unpublish
[2016-03-22 15:13:54.142][error][3545][119][11] stream service cycle failed. ret=3001(Resource temporarily unavailable)
------------------------------
使用ffmpeg推流是正常的
命令如下
D:\temp\ffmpeg\bin>ffmpeg -re -rtbufsize 8000000 -thread_queue_size 32 -f dshow -i video="HD WebCam" -f dshow -sample_rate 44100 -i aud
io="麦克风 (Realtek High Definition Audio)" -pix_fmt yuv420p -profile main -tune zerolatency -preset ultrafast -vcodec libx264 -r 25 -s
640x480 -vb 512k -acodec libvo_aacenc -ab 256k -f flv -y rtmp://192.168.177.128:1935/live/stream2
srs正常,日志如下:
[2016-03-22 15:07:36.822][trace][3545][105] RTMP client ip=192.168.177.1
[2016-03-22 15:07:36.822][trace][3545][105] srand initialized the random.
[2016-03-22 15:07:36.822][trace][3545][105] srand initialized the random.
[2016-03-22 15:07:36.822][trace][3545][105] simple handshake success.
[2016-03-22 15:07:36.822][trace][3545][105] connect app, tcUrl=rtmp://192.168.177.128:1935/live, pageUrl=, swfUrl=, schema=rtmp, vhost=__defaultVhost__, port=1935, app=live, args=null
[2016-03-22 15:07:36.822][trace][3545][105] out chunk size to 60000
[2016-03-22 15:07:36.875][trace][3545][105] client identified, type=fmle-publish, stream_name=stream2, duration=-1.00
[2016-03-22 15:07:36.898][trace][3545][105] source url=/live/stream2, ip=192.168.177.1, cache=1, is_edge=0, source_id=-1[-1]
[2016-03-22 15:07:36.936][trace][3545][105] hls: win=60.00, frag=10.00, prefix=, path=./objs/nginx/html, m3u8=[app]/[stream].m3u8, ts=[app]/[stream]-[seq].ts, aof=2.00, floor=0, clean=1, waitk=1, dispose=0
[2016-03-22 15:07:36.947][trace][3545][105] start publish mr=0/350, p1stpt=20000, pnt=20000, tcp_nodelay=0, rtcid=108
[2016-03-22 15:07:37.046][trace][3545][105] got metadata, width=640, height=480, vcodec=7, acodec=10
[2016-03-22 15:07:37.046][trace][3545][105] 43B video sh, codec(7, profile=Baseline, level=3, 768x640, 0kbps, 0fps, 0s)
[2016-03-22 15:07:37.046][trace][3545][105] 4B audio sh, codec(10, profile=LC, 2channels, 0kbps, 44100HZ), flv(16bits, 2channels, 44100HZ)
[2016-03-22 15:07:40.092][trace][3545][105] unpublish drop ts segment, sequence_no=0, uri=stream2-0.ts, duration=2147483.53, start=0
[2016-03-22 15:07:40.092][trace][3545][105] cleanup when unpublish
[2016-03-22 15:07:40.092][trace][3545][105] control message(unpublish) accept, retry stream service.
[2016-03-22 15:07:40.092][warn][3545][105][32] client disconnect peer. ret=1004
[2016-03-22 15:08:16.958][trace][3545][110] RTMP client ip=192.168.177.1
求高手给个思路,昨晚搞一晚上,没搞定,特求教