Commit ff5f8c27 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 c2c7ad1d
...@@ -235,7 +235,7 @@ HLSOutputStream::write_frame (const AVRational *time_base, AVStream *st, AVPacke ...@@ -235,7 +235,7 @@ HLSOutputStream::write_frame (const AVRational *time_base, AVStream *st, AVPacke
* return 1 when encoding is finished, 0 otherwise * return 1 when encoding is finished, 0 otherwise
*/ */
int int
HLSOutputStream::write_audio_frame() HLSOutputStream::write_audio_frame (Error& err)
{ {
AVPacket pkt = { 0 }; // data and size must be 0; AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame; AVFrame *frame;
...@@ -260,7 +260,10 @@ HLSOutputStream::write_audio_frame() ...@@ -260,7 +260,10 @@ HLSOutputStream::write_audio_frame()
*/ */
ret = av_frame_make_writable (m_frame); ret = av_frame_make_writable (m_frame);
if (ret < 0) if (ret < 0)
exit(1); {
err = Error ("error making frame writable");
return 1;
}
/* convert to destination format */ /* convert to destination format */
ret = swr_convert (m_swr_ctx, ret = swr_convert (m_swr_ctx,
...@@ -268,8 +271,8 @@ HLSOutputStream::write_audio_frame() ...@@ -268,8 +271,8 @@ HLSOutputStream::write_audio_frame()
(const uint8_t **)frame->data, frame->nb_samples); (const uint8_t **)frame->data, frame->nb_samples);
if (ret < 0) if (ret < 0)
{ {
fprintf (stderr, "Error while converting\n"); err = Error ("error while converting");
exit(1); return 1;
} }
frame = m_frame; frame = m_frame;
...@@ -280,8 +283,8 @@ HLSOutputStream::write_audio_frame() ...@@ -280,8 +283,8 @@ HLSOutputStream::write_audio_frame()
ret = avcodec_encode_audio2 (m_enc, &pkt, frame, &got_packet); ret = avcodec_encode_audio2 (m_enc, &pkt, frame, &got_packet);
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret)); err = Error (string_printf ("error encoding audio frame: %s", av_err2str (ret)));
exit(1); return 1;
} }
if (got_packet) if (got_packet)
...@@ -295,9 +298,8 @@ HLSOutputStream::write_audio_frame() ...@@ -295,9 +298,8 @@ HLSOutputStream::write_audio_frame()
ret = write_frame (&m_enc->time_base, m_st, &pkt); ret = write_frame (&m_enc->time_base, m_st, &pkt);
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, "Error while writing audio frame: %s\n", err = Error (string_printf ("error while writing audio frame: %s", av_err2str (ret)));
av_err2str(ret)); return 1;
exit(1);
} }
m_keep_aac_frames--; m_keep_aac_frames--;
} }
...@@ -363,7 +365,10 @@ HLSOutputStream::open (const string& out_filename, size_t cut_aac_frames, size_t ...@@ -363,7 +365,10 @@ HLSOutputStream::open (const string& out_filename, size_t cut_aac_frames, size_t
Error Error
HLSOutputStream::close() HLSOutputStream::close()
{ {
write(); // drain Error err;
while (write_audio_frame (err) == 0);
if (err)
return err;
av_write_trailer (m_fmt_ctx); av_write_trailer (m_fmt_ctx);
...@@ -379,12 +384,6 @@ HLSOutputStream::close() ...@@ -379,12 +384,6 @@ HLSOutputStream::close()
return Error::Code::NONE; return Error::Code::NONE;
} }
void
HLSOutputStream::write()
{
while (write_audio_frame() == 0);
}
Error Error
HLSOutputStream::write_frames (const std::vector<float>& frames) HLSOutputStream::write_frames (const std::vector<float>& frames)
{ {
...@@ -401,9 +400,12 @@ HLSOutputStream::write_frames (const std::vector<float>& frames) ...@@ -401,9 +400,12 @@ HLSOutputStream::write_frames (const std::vector<float>& frames)
m_delete_input_start -= delete_input; m_delete_input_start -= delete_input;
} }
Error err;
while (m_audio_buffer.can_read_frames() >= 1024) while (m_audio_buffer.can_read_frames() >= 1024)
{ {
write_audio_frame(); write_audio_frame (err);
if (err)
return err;
} }
return Error::Code::NONE; return Error::Code::NONE;
} }
......
...@@ -92,7 +92,7 @@ class HLSOutputStream : public AudioOutputStream { ...@@ -92,7 +92,7 @@ class HLSOutputStream : public AudioOutputStream {
Error add_stream (AVCodec **codec, enum AVCodecID codec_id); Error add_stream (AVCodec **codec, enum AVCodecID codec_id);
Error 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 (Error& err);
void close_stream(); void close_stream();
AVFrame *alloc_audio_frame (AVSampleFormat sample_fmt, uint64_t channel_layout, int sample_rate, int nb_samples, Error& err); 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);
...@@ -104,7 +104,6 @@ public: ...@@ -104,7 +104,6 @@ public:
int sample_rate() const override; int sample_rate() const override;
int n_channels() const override; int n_channels() const override;
Error write_frames (const std::vector<float>& frames) override; Error write_frames (const std::vector<float>& frames) override;
void write();
Error close(); Error close();
}; };
......
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