【发布时间】:2012-04-25 23:45:48
【问题描述】:
过去一周我一直在尝试通过 RTP 实现 H.264 流,使用 x264 作为编码器和 libavformat 来打包和发送流。问题是,据我所知,它工作不正常。
现在我只是编码随机数据 (x264_picture_alloc) 并从 libx264 中提取 NAL 帧。这很简单:
x264_picture_t pic_out;
x264_nal_t* nals;
int num_nals;
int frame_size = x264_encoder_encode(this->encoder, &nals, &num_nals, this->pic_in, &pic_out);
if (frame_size <= 0)
{
return frame_size;
}
// push NALs into the queue
for (int i = 0; i < num_nals; i++)
{
// create a NAL storage unit
NAL nal;
nal.size = nals[i].i_payload;
nal.payload = new uint8_t[nal.size];
memcpy(nal.payload, nals[i].p_payload, nal.size);
// push the storage into the NAL queue
{
// lock and push the NAL to the queue
boost::mutex::scoped_lock lock(this->nal_lock);
this->nal_queue.push(nal);
}
}
nal_queue 用于将帧安全地传递给 Streamer 类,然后将帧发送出去。现在它没有线程化,因为我只是在测试试图让它工作。在对单个帧进行编码之前,我已经确保初始化了编码器。
但我不认为 x264 是问题所在,因为我可以在 NAL 中看到它返回的帧数据。 流式传输数据是使用 libavformat 完成的,它首先在 Streamer 类中初始化:
Streamer::Streamer(Encoder* encoder, string rtp_address, int rtp_port, int width, int height, int fps, int bitrate)
{
this->encoder = encoder;
// initalize the AV context
this->ctx = avformat_alloc_context();
if (!this->ctx)
{
throw runtime_error("Couldn't initalize AVFormat output context");
}
// get the output format
this->fmt = av_guess_format("rtp", NULL, NULL);
if (!this->fmt)
{
throw runtime_error("Unsuitable output format");
}
this->ctx->oformat = this->fmt;
// try to open the RTP stream
snprintf(this->ctx->filename, sizeof(this->ctx->filename), "rtp://%s:%d", rtp_address.c_str(), rtp_port);
if (url_fopen(&(this->ctx->pb), this->ctx->filename, URL_WRONLY) < 0)
{
throw runtime_error("Couldn't open RTP output stream");
}
// add an H.264 stream
this->stream = av_new_stream(this->ctx, 1);
if (!this->stream)
{
throw runtime_error("Couldn't allocate H.264 stream");
}
// initalize codec
AVCodecContext* c = this->stream->codec;
c->codec_id = CODEC_ID_H264;
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->bit_rate = bitrate;
c->width = width;
c->height = height;
c->time_base.den = fps;
c->time_base.num = 1;
// write the header
av_write_header(this->ctx);
}
这就是事情似乎出错的地方。上面的av_write_header 似乎什么也没做;我已经使用wireshark来验证这一点。作为参考,我使用Streamer streamer(&enc, "10.89.6.3", 49990, 800, 600, 30, 40000); 来初始化Streamer 实例,enc 是对以前用于处理x264 的Encoder 对象的引用。
现在,当我想流式传输 NAL 时,我使用这个:
// grab a NAL
NAL nal = this->encoder->nal_pop();
cout << "NAL popped with size " << nal.size << endl;
// initalize a packet
AVPacket p;
av_init_packet(&p);
p.data = nal.payload;
p.size = nal.size;
p.stream_index = this->stream->index;
// send it out
av_write_frame(this->ctx, &p);
此时,我可以看到 RTP 数据出现在网络上,它看起来就像我一直在发送的帧,甚至包括来自 x264 的一点版权 blob。 但是,我用过的任何播放器都无法理解这些数据。 VLC 不再需要 SDP 描述,apparently isn't required。
然后我尝试通过gst-launch播放它:
gst-launch udpsrc port=49990 ! rtph264depay ! decodebin ! xvimagesink
这将等待 UDP 数据,但是当它收到时,我得到:
错误:元素 /GstPipeline:pipeline0/GstRtpH264Depay:rtph264depay0:无 RTP 格式已协商。附加调试信息: gstbasertpdepayload.c(372): gst_base_rtp_depayload_chain (): /GstPipeline:pipeline0/GstRtpH264Depay:rtph264depay0:输入缓冲区 需要在它们上设置 RTP 上限。这通常通过设置来实现 上游源元素的“caps”属性(通常是 udpsrc 或 appsrc),或者在 depayloader 之前放置一个 capsfilter 元素和 在上面设置'caps'属性。另见 http://cgit.freedesktop.org/gstreamer/gst-plugins-good/tree/gst/rtp/README
由于我没有使用 GStreamer 进行流式传输,因此我不太确定 RTP 上限意味着什么。但是,这让我想知道我是否没有通过 RTP 发送足够的信息来描述流。我对视频很陌生,我觉得这里缺少一些关键的东西。有什么提示吗?
【问题讨论】:
标签: c++ h.264 rtp libavformat libx264