流#0:0:未知:无(pcm_s16be)

时间:2016-10-28 01:51:56

标签: ffmpeg rtp libav

我正在尝试使用FFmpeg创建一个RTP音频流,代码如下。在我的Windows 10计算机上运行时,我收到以下响应:

Output #0, rtp, to 'rtp://127.0.0.1:8554':
    Stream #0:0: Audio: pcm_s16be, 8000 Hz, mono, s16, 128 kb/s
SDP dump:
=================
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 57.25.101
m=audio 8554 RTP/AVP 96
b=AS:128
a=rtpmap:96 L16/8000/1
ret = 0

但是在Linux(#57~14.04.1-Ubuntu)上,FFmpeg将流视为“未知”:

Output #0, rtp, to 'rtp://127.0.0.1:8554':
    Stream #0:0: Unknown: none (pcm_s16be)
SDP dump:
=================
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 57.57.100
m=application 8554 RTP/AVP 3

有谁知道为什么会这样?任何形式的帮助都将非常感激。

#include <math.h>
extern "C"
{
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libavformat/avformat.h>
}

/*
* Audio encoding example
*/
static void audio_encode_example(const char *filename)
{
  int ret;
  AVCodec *outCodec = NULL;
  AVCodecContext *outCodecCtx = NULL;
  AVFormatContext *outFormatCtx = NULL;
  AVStream * outAudioStream = NULL;
  AVFrame *outFrame = NULL;

  ret = avformat_alloc_output_context2(&outFormatCtx, NULL, "rtp", filename);
  if (!outFormatCtx || ret < 0)
  {
    fprintf(stderr, "Could not allocate output context");
  }

  outFormatCtx->flags |= AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS;
  outFormatCtx->oformat->audio_codec = AV_CODEC_ID_PCM_S16BE;
  // outFormatCtx->audio_codec_id = AV_CODEC_ID_PCM_S16BE;
  // outFormatCtx->oformat->video_codec = AV_CODEC_ID_NONE;
  // outFormatCtx->oformat->data_codec = AV_CODEC_ID_PCM_S16BE;

  /* find the encoder */
  outCodec = avcodec_find_encoder(outFormatCtx->oformat->audio_codec);
  if (!outCodec) {
    fprintf(stderr, "Codec not found\n");
    exit(1);
  }

  outAudioStream = avformat_new_stream(outFormatCtx, outCodec);
  if (!outAudioStream)
  {
    fprintf(stderr, "Cannot add new audio stream\n");
    exit(1);
  }

  outAudioStream->time_base.den = 8000;
  outAudioStream->time_base.num = 1;
  outCodecCtx = outAudioStream->codec;
  outCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;

  /* select other audio parameters supported by the encoder */
  outCodecCtx->sample_rate = 8000;
  outCodecCtx->channel_layout = AV_CH_LAYOUT_MONO;
  outCodecCtx->channels = 1;

  /* open it */
  if (avcodec_open2(outCodecCtx, outCodec, NULL) < 0) {
    fprintf(stderr, "Could not open codec\n");
    exit(1);
  }
  outCodecCtx->frame_size = 372;

  av_dump_format(outFormatCtx, 0, filename, 1);

  char buff[10000] = { 0 };
  ret = av_sdp_create(&outFormatCtx, 1, buff, sizeof(buff));
  printf("SDP dump:\n"
          "=================\n"
          "%s", buff);
  /*
  Running the program returns the following:

    Output #0, rtp, to 'rtp://127.0.0.1:8554':
        Stream #0:0: Unknown: none (pcm_s16be)
    SDP dump:
    =================
    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 57.57.100
    m=application 8554 RTP/AVP 3

  */

  exit(1);
}


int main(int argc, char **argv)
{
  const char *output;

  av_register_all();
  avformat_network_init(); // for network streaming

  audio_encode_example("rtp://127.0.0.1:8554");

  return 0;
}

1 个答案:

答案 0 :(得分:0)

我能够通过忽略av_sdp_create

的输出来使其工作
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 57.25.101
m=audio 8554 RTP/AVP 97
b=AS:256
a=rtpmap:97 L16/8000/1

我忍不住感到av_sdp_create给了我一个无法正常工作的SDP文件,从而引发了我的疯狂追逐。