首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将图像编码到h264和rtp输出:没有支持参数集的SDP文件不播放。

将图像编码到h264和rtp输出:没有支持参数集的SDP文件不播放。
EN

Stack Overflow用户
提问于 2021-09-13 17:01:06
回答 1查看 807关注 0票数 1

tl;dr:我尝试将获取的相机帧编码到h264,通过RTP发送,然后在另一台设备上播放。由ffmpeg为示例视频生成的SDP文件包含我自己的SDP文件所遗漏的信息。我的SDP文件在ffplay中播放,而不是VLC,而两者都播放ffmpeg的SDP文件。我怀疑我的SDP文件中缺少参数集。

最后,我想在VLC中回放这个。

我正在编写将图像编码到h264并输出到RTP服务器(或客户端)的代码。不管怎么说,听的部分)。我为此生成一个SDP文件。

  • 无问题地播放溪流
  • mplayer显示了一个嵌入在较大黑匣子中的绿色框,但我在某个地方看到它只支持RTP上的mpegts,所以不确定。
  • VLC不播放SDP文件。

现在,当我使用一些随机视频并让ffmpeg输出一个SDP文件时,如下所示

代码语言:javascript
运行
复制
ffmpeg     -re     -i some.mp4     -an     -c:v copy -f rtp -sdp_file
video.sdp     "rtp://127.0.0.1:5004"

我可以看到,生成的SDP文件(在ffplay和VLC中都播放)包括base64编码的sprop-parameter-sets字段,并且删除它会导致流不播放。

代码语言:javascript
运行
复制
> cat video.sdp
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 58.76.100
m=video 5004 RTP/AVP 96
b=AS:1034
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;
sprop-parameter-sets=Z2QANKzZQDAA7fiMBagICAoAAAMAAgAAAwDwHjBjLA==,aOvjyyLA;
profile-level-id=640034

另一方面,我自己的SDP文件不包含此信息,VLC挂起10秒,然后停止尝试“未收到数据”。

代码语言:javascript
运行
复制
> cat test.sdp
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 58.76.100
m=video 44499 RTP/AVP 96
b=AS:2000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1

所以我的理论是,我的自定义代码必须以某种方式将这个SPS信息添加到SDP文件中。但是,尽管搜索了几个小时,我还是找不到一种结构化的方法来在AVStreamAVCodecParams上设置数据外字段。我使用的代码大致如下(我确信里面有无关的错误):

代码语言:javascript
运行
复制
// variables
std::vector<std::uint8_t> imgbuf;
AVFormatContext *ofmt_ctx = nullptr;
AVCodec *out_codec = nullptr;
AVStream *out_stream = nullptr;
AVCodecContext *out_codec_ctx = nullptr;
SwsContext *swsctx = nullptr;
cv::Mat canvas_;
unsigned int height_;
unsigned int width_;
unsigned int fps_;
AVFrame *frame_ = nullptr;

AVOutputFormat *format = av_guess_format("rtp", nullptr, nullptr);
const auto url = std::string("rtp://127.0.0.1:5001");
avformat_alloc_output_context2(ofmt_ctx, format, format->name, url.c_str());

out_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
stream = avformat_new_stream(ofmt_ctx, out_codec);
out_codec_ctx = avcodec_alloc_context3(out_codec);

// then, for each incoming image:
while (receive_image) {
  static bool first_time = true;
  if (first_time) {
    // discover necessary params such as image dimensions from the first
    // received image
    first_time = false;
    height_ = image.rows;
    width_ = image.cols;

    codec_ctx->codec_tag = 0;
    codec_ctx->bit_rate = 2e6;
    // does nothing, unfortunately
    codec_ctx->thread_count = 1;
    codec_ctx->codec_id = AV_CODEC_ID_H264;
    codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
    codec_ctx->width = width_;
    codec_ctx->height = height_;
    codec_ctx->gop_size = 6;
    codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
    codec_ctx->framerate = fps_;
    codec_ctx->time_base = av_inv_q(fps_);

    avcodec_parameters_from_context(stream, out_codec_ctx);

    // this stuff is empty: is that the problem?
    stream->codecpar->extradata = codec_ctx->extradata;
    stream->codecpar->extradata_size = codec_ctx->extradata_size;

    AVDictionary *codec_options = nullptr;
    av_dict_set(&codec_options, "profile", "high", 0);
    av_dict_set(&codec_options, "preset", "ultrafast", 0);
    av_dict_set(&codec_options, "tune", "zerolatency", 0);

    // open video encoder
    avcodec_open2(codec_ctx, codec, &codec_options);

    stream->time_base.num = 1;
    stream->time_base.den = fps_;
    avio_open(&(ofmt_ctx->pb), ofmt_ctx->filename, AVIO_FLAG_WRITE);

    /* Write a file for VLC */
    char buf[200000];
    AVFormatContext *ac[] = {ofmt_ctx};
    av_sdp_create(ac, 1, buf, 20000);
    printf("sdp:\n%s\n", buf);
    FILE *fsdp = fopen("test.sdp", "w");
    fprintf(fsdp, "%s", buf);
    fclose(fsdp);

    swsctx = sws_getContext(width_, height_, AV_PIX_FMT_BGR24, width_, height_,
                            out_codec_ctx->pix_fmt, SWS_BICUBIC, nullptr,
                            nullptr, nullptr);
  }

  if (!frame_) {
    frame_ = av_frame_alloc();

    std::uint8_t *framebuf = new uint8_t[av_image_get_buffer_size(
        codec_ctx->pix_fmt, width_, height_, 1)];
    av_image_fill_arrays(frame_->data, frame_->linesize, framebuf,
                         codec_ctx->pix_fmt, width, height, 1);
    frame_->width = width_;
    frame_->height = height_;
    frame_->format = static_cast<int>(codec_ctx->pix_fmt);
    success = avformat_write_header(ofmt_ctx, nullptr);
  }
  if (imgbuf.empty()) {
    imgbuf.resize(height_ * width_ * 3 + 16);
    canvas_ = cv::Mat(height_, width_, CV_8UC3, imgbuf.data(), width_ * 3);
  } else {
    image.copyTo(canvas_);
  }

  const int stride[] = {static_cast<int>(image.step[0])};

  sws_scale(swsctx, &canvas_.data, stride, 0, canvas_.rows, frame_->data,
            frame_->linesize);
  frame_->pts += av_rescale_q(1, out_codec_ctx->time_base, stream->time_base);

  AVPacket pkt = {0};
  avcodec_send_frame(out_codec_ctx, frame_);
  avcodec_receive_packet(out_codec_ctx, &pkt);
  av_interleaved_write_frame(ofmt_ctx, &pkt);
}

有人能在这里提些建议吗?

--

更新

设置时

代码语言:javascript
运行
复制
this->out_codec_ctx->flags |=AV_CODEC_FLAG_GLOBAL_HEADER;

数据外数据实际上存在于编解码器上下文中,但我不得不在avcodec_open2()之后移动avcodec_open2(),因为在打开编解码器之前,数据外数据是空的。我现在在SDP文件中获得sprop-parameter-sets,但是VLC仍然不播放它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-14 08:27:21

在我的例子中,解决方案是端口号(?)。显然,VLC无法从我所使用的端口44499接收,但是5004就像ffmpeg示例一样有效。我不知道这是一个MacOS特性,还是也转移到linux。

我试过几个端口:

  • 5001不工作
  • 5002工程
  • 5003不工作
  • 5004作品
  • 5005不工作
  • 5006工程
  • 44498作品

因此,VLC要接收RTP数据包,端口号必须是偶数吗?瓦特?

其解释似乎是live555丢弃了端口号:https://github.com/rgaufman/live555/blob/master/liveMedia/MediaSession.cpp#L696的lsb。

因此,即使是端口也没有改变。这是RFC推荐的或强制要求的。

对于UDP和类似的协议,RTP应该使用偶数的目的端口号,相应的RTCP流应该使用下一个更高的(奇数)目标端口号。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69166542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档