Commit a1a6f71b authored by Stefan Westerfeld's avatar Stefan Westerfeld

HLSOutputStream: handle add_stream() errors

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 245439e6
...@@ -54,37 +54,26 @@ HLSOutputStream::HLSOutputStream (int n_channels, int sample_rate, int bit_depth ...@@ -54,37 +54,26 @@ HLSOutputStream::HLSOutputStream (int n_channels, int sample_rate, int bit_depth
} }
/* Add an output stream. */ /* Add an output stream. */
void Error
HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id) HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id)
{ {
/* find the encoder */ /* find the encoder */
*codec = avcodec_find_encoder (codec_id); *codec = avcodec_find_encoder (codec_id);
if (!(*codec)) if (!(*codec))
{ return Error (string_printf ("could not find encoder for '%s'", avcodec_get_name (codec_id)));
fprintf(stderr, "Could not find encoder for '%s'\n", avcodec_get_name (codec_id));
exit(1);
}
m_st = avformat_new_stream (m_fmt_ctx, NULL); m_st = avformat_new_stream (m_fmt_ctx, NULL);
if (!m_st) if (!m_st)
{ return Error ("could not allocate stream");
fprintf (stderr, "Could not allocate stream\n");
exit(1);
}
m_st->id = m_fmt_ctx->nb_streams - 1; m_st->id = m_fmt_ctx->nb_streams - 1;
m_enc = avcodec_alloc_context3 (*codec); m_enc = avcodec_alloc_context3 (*codec);
if (!m_enc) if (!m_enc)
{ return Error ("could not alloc an encoding context");
fprintf (stderr, "Could not alloc an encoding context\n");
exit(1);
}
if ((*codec)->type != AVMEDIA_TYPE_AUDIO) if ((*codec)->type != AVMEDIA_TYPE_AUDIO)
{ return Error ("codec type must be audio");
error ("HLSOutputStream: codec type must be audio");
exit (1);
}
m_enc->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP; m_enc->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
m_enc->bit_rate = 128000; m_enc->bit_rate = 128000;
...@@ -101,10 +90,7 @@ HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id) ...@@ -101,10 +90,7 @@ HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id)
} }
} }
if (!match) if (!match)
{ return Error (string_printf ("no codec support for sample rate %d", m_sample_rate));
error ("HLSOutputStream: unsupported sample rate %d\n", m_sample_rate);
exit (1);
}
} }
m_enc->channels = av_get_channel_layout_nb_channels (m_enc->channel_layout); m_enc->channels = av_get_channel_layout_nb_channels (m_enc->channel_layout);
m_enc->channel_layout = AV_CH_LAYOUT_STEREO; m_enc->channel_layout = AV_CH_LAYOUT_STEREO;
...@@ -123,6 +109,8 @@ HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id) ...@@ -123,6 +109,8 @@ HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id)
/* Some formats want stream headers to be separate. */ /* Some formats want stream headers to be separate. */
if (m_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) if (m_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
m_enc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; m_enc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
return Error::Code::NONE;
} }
...@@ -344,14 +332,14 @@ HLSOutputStream::open (const string& out_filename, size_t cut_aac_frames, size_t ...@@ -344,14 +332,14 @@ HLSOutputStream::open (const string& out_filename, size_t cut_aac_frames, size_t
int ret = avio_open (&m_fmt_ctx->pb, filename.c_str(), AVIO_FLAG_WRITE); int ret = avio_open (&m_fmt_ctx->pb, filename.c_str(), AVIO_FLAG_WRITE);
if (ret < 0) if (ret < 0)
{ return Error (av_err2str (ret));
error ("Could not open output: %s\n", av_err2str (ret));
return Error ("open hls output failed");
}
AVDictionary *opt = nullptr; AVDictionary *opt = nullptr;
AVCodec *audio_codec; AVCodec *audio_codec;
add_stream (&audio_codec, AV_CODEC_ID_AAC); Error err = add_stream (&audio_codec, AV_CODEC_ID_AAC);
if (err)
return err;
open_audio (audio_codec, opt); open_audio (audio_codec, opt);
/* Write the stream header, if any. */ /* Write the stream header, if any. */
......
...@@ -89,7 +89,7 @@ class HLSOutputStream : public AudioOutputStream { ...@@ -89,7 +89,7 @@ class HLSOutputStream : public AudioOutputStream {
AudioBuffer m_audio_buffer; AudioBuffer m_audio_buffer;
size_t m_delete_input_start = 0; size_t m_delete_input_start = 0;
void add_stream (AVCodec **codec, enum AVCodecID codec_id); Error add_stream (AVCodec **codec, enum AVCodecID codec_id);
void open_audio (AVCodec *codec, AVDictionary *opt_arg); void open_audio (AVCodec *codec, AVDictionary *opt_arg);
AVFrame *get_audio_frame(); AVFrame *get_audio_frame();
int write_audio_frame(); int write_audio_frame();
......
...@@ -367,6 +367,11 @@ hls_mark (const string& infile, const string& outfile, const string& bits) ...@@ -367,6 +367,11 @@ hls_mark (const string& infile, const string& outfile, const string& bits)
const size_t keep_aac_frames = size / 1024; const size_t keep_aac_frames = size / 1024;
err = out_stream.open (outfile, cut_aac_frames, keep_aac_frames, pts_start, delete_input_start); err = out_stream.open (outfile, cut_aac_frames, keep_aac_frames, pts_start, delete_input_start);
if (err)
{
error ("audiowmark: error opening HLS output stream %s: %s\n", outfile.c_str(), err.message());
return 1;
}
int zrc = add_stream_watermark (&in_stream, &out_stream, bits, start_pos - prev_size); int zrc = add_stream_watermark (&in_stream, &out_stream, bits, start_pos - prev_size);
if (zrc != 0) if (zrc != 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment