Commit 5100e1af authored by Stefan Westerfeld's avatar Stefan Westerfeld

HLSOutputStream: more error handling

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent a1a6f71b
...@@ -115,14 +115,14 @@ HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id) ...@@ -115,14 +115,14 @@ HLSOutputStream::add_stream (AVCodec **codec, enum AVCodecID codec_id)
AVFrame * AVFrame *
HLSOutputStream::alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples) HLSOutputStream::alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples, Error& err)
{ {
AVFrame *frame = av_frame_alloc(); AVFrame *frame = av_frame_alloc();
if (!frame) if (!frame)
{ {
fprintf (stderr, "Error allocating an audio frame\n"); err = Error ("error allocating an audio frame");
exit(1); return nullptr;
} }
frame->format = sample_fmt; frame->format = sample_fmt;
...@@ -135,8 +135,8 @@ HLSOutputStream::alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_ ...@@ -135,8 +135,8 @@ HLSOutputStream::alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_
int ret = av_frame_get_buffer (frame, 0); int ret = av_frame_get_buffer (frame, 0);
if (ret < 0) if (ret < 0)
{ {
fprintf (stderr, "Error allocating an audio buffer\n"); err = Error ("Error allocating an audio buffer");
exit(1); return nullptr;
} }
} }
...@@ -144,7 +144,7 @@ HLSOutputStream::alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_ ...@@ -144,7 +144,7 @@ HLSOutputStream::alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_
} }
void Error
HLSOutputStream::open_audio (AVCodec *codec, AVDictionary *opt_arg) HLSOutputStream::open_audio (AVCodec *codec, AVDictionary *opt_arg)
{ {
int nb_samples; int nb_samples;
...@@ -166,8 +166,14 @@ HLSOutputStream::open_audio (AVCodec *codec, AVDictionary *opt_arg) ...@@ -166,8 +166,14 @@ HLSOutputStream::open_audio (AVCodec *codec, AVDictionary *opt_arg)
else else
nb_samples = m_enc->frame_size; nb_samples = m_enc->frame_size;
m_frame = alloc_audio_frame (m_enc->sample_fmt, m_enc->channel_layout, m_enc->sample_rate, nb_samples); Error err;
m_tmp_frame = alloc_audio_frame (AV_SAMPLE_FMT_FLT, m_enc->channel_layout, m_enc->sample_rate, nb_samples); m_frame = alloc_audio_frame (m_enc->sample_fmt, m_enc->channel_layout, m_enc->sample_rate, nb_samples, err);
if (err)
return err;
m_tmp_frame = alloc_audio_frame (AV_SAMPLE_FMT_FLT, m_enc->channel_layout, m_enc->sample_rate, nb_samples, err);
if (err)
return err;
/* copy the stream parameters to the muxer */ /* copy the stream parameters to the muxer */
ret = avcodec_parameters_from_context (m_st->codecpar, m_enc); ret = avcodec_parameters_from_context (m_st->codecpar, m_enc);
...@@ -199,6 +205,7 @@ HLSOutputStream::open_audio (AVCodec *codec, AVDictionary *opt_arg) ...@@ -199,6 +205,7 @@ HLSOutputStream::open_audio (AVCodec *codec, AVDictionary *opt_arg)
fprintf(stderr, "Failed to initialize the resampling context\n"); fprintf(stderr, "Failed to initialize the resampling context\n");
exit(1); exit(1);
} }
return Error::Code::NONE;
} }
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
...@@ -340,7 +347,9 @@ HLSOutputStream::open (const string& out_filename, size_t cut_aac_frames, size_t ...@@ -340,7 +347,9 @@ HLSOutputStream::open (const string& out_filename, size_t cut_aac_frames, size_t
if (err) if (err)
return err; return err;
open_audio (audio_codec, opt); err = open_audio (audio_codec, opt);
if (err)
return err;
/* Write the stream header, if any. */ /* Write the stream header, if any. */
ret = avformat_write_header (m_fmt_ctx, &opt); ret = avformat_write_header (m_fmt_ctx, &opt);
......
...@@ -90,11 +90,11 @@ class HLSOutputStream : public AudioOutputStream { ...@@ -90,11 +90,11 @@ class HLSOutputStream : public AudioOutputStream {
size_t m_delete_input_start = 0; size_t m_delete_input_start = 0;
Error 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); Error open_audio (AVCodec *codec, AVDictionary *opt_arg);
AVFrame *get_audio_frame(); AVFrame *get_audio_frame();
int write_audio_frame(); int write_audio_frame();
void close_stream(); void close_stream();
AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples); AVFrame *alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples, Error& err);
int write_frame (const AVRational *time_base, AVStream *st, AVPacket *pkt); int write_frame (const AVRational *time_base, AVStream *st, AVPacket *pkt);
public: public:
HLSOutputStream (int n_channels, int sample_rate, int bit_depth); HLSOutputStream (int n_channels, int sample_rate, int bit_depth);
......
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