Commit a79f608e authored by Stefan Westerfeld's avatar Stefan Westerfeld

HLSOutputStream: clean up encoder result codes

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent b10c33a1
...@@ -237,15 +237,14 @@ HLSOutputStream::write_frame (const AVRational *time_base, AVStream *st, AVPacke ...@@ -237,15 +237,14 @@ HLSOutputStream::write_frame (const AVRational *time_base, AVStream *st, AVPacke
/* /*
* encode one audio frame and send it to the muxer * encode one audio frame and send it to the muxer
* return 1 when encoding is finished, 0 otherwise * returns EncResult: OK, ERROR, DONE
*/ */
int HLSOutputStream::EncResult
HLSOutputStream::write_audio_frame (Error& err) 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;
int ret; int ret;
int got_packet;
av_init_packet (&pkt); av_init_packet (&pkt);
...@@ -267,7 +266,7 @@ HLSOutputStream::write_audio_frame (Error& err) ...@@ -267,7 +266,7 @@ HLSOutputStream::write_audio_frame (Error& err)
if (ret < 0) if (ret < 0)
{ {
err = Error ("error making frame writable"); err = Error ("error making frame writable");
return 1; return EncResult::ERROR;
} }
/* convert to destination format */ /* convert to destination format */
...@@ -277,7 +276,7 @@ HLSOutputStream::write_audio_frame (Error& err) ...@@ -277,7 +276,7 @@ HLSOutputStream::write_audio_frame (Error& err)
if (ret < 0) if (ret < 0)
{ {
err = Error ("error while converting"); err = Error ("error while converting");
return 1; return EncResult::ERROR;
} }
frame = m_frame; frame = m_frame;
...@@ -288,30 +287,28 @@ HLSOutputStream::write_audio_frame (Error& err) ...@@ -288,30 +287,28 @@ HLSOutputStream::write_audio_frame (Error& err)
ret = avcodec_send_frame (m_enc, frame); ret = avcodec_send_frame (m_enc, frame);
if (ret == AVERROR_EOF) if (ret == AVERROR_EOF)
{ {
/* encoder has nothing more to do */ return EncResult::DONE; // encoder has nothing more to do
return 1;
} }
else if (ret < 0) else if (ret < 0)
{ {
err = Error (string_printf ("error encoding audio frame: %s", av_err2str (ret))); err = Error (string_printf ("error encoding audio frame: %s", av_err2str (ret)));
return 1; return EncResult::ERROR;
} }
for (;;) for (;;)
{ {
ret = avcodec_receive_packet (m_enc, &pkt); ret = avcodec_receive_packet (m_enc, &pkt);
if (ret == AVERROR (EAGAIN)) if (ret == AVERROR (EAGAIN))
{ {
/* encoder needs more data to produce something */ return EncResult::OK; // encoder needs more data to produce something
return 0;
} }
else if (ret == AVERROR_EOF) else if (ret == AVERROR_EOF)
{ {
return 1; /* done */ return EncResult::DONE;
} }
else if (ret < 0) else if (ret < 0)
{ {
err = Error (string_printf ("error while encoding audio frame: %s", av_err2str (ret))); err = Error (string_printf ("error while encoding audio frame: %s", av_err2str (ret)));
return 1; return EncResult::ERROR;
} }
/* one packet available */ /* one packet available */
...@@ -325,11 +322,10 @@ HLSOutputStream::write_audio_frame (Error& err) ...@@ -325,11 +322,10 @@ HLSOutputStream::write_audio_frame (Error& err)
if (ret < 0) if (ret < 0)
{ {
err = Error (string_printf ("error while writing audio frame: %s", av_err2str (ret))); err = Error (string_printf ("error while writing audio frame: %s", av_err2str (ret)));
return 1; return EncResult::ERROR;
} }
m_keep_aac_frames--; m_keep_aac_frames--;
} }
return 0; /* not done yet */
} }
} }
...@@ -400,7 +396,7 @@ HLSOutputStream::close() ...@@ -400,7 +396,7 @@ HLSOutputStream::close()
m_state = State::CLOSED; m_state = State::CLOSED;
Error err; Error err;
while (write_audio_frame (err) == 0); while (write_audio_frame (err) == EncResult::OK);
if (err) if (err)
return err; return err;
......
...@@ -99,9 +99,15 @@ class HLSOutputStream : public AudioOutputStream { ...@@ -99,9 +99,15 @@ 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 (Error& err); enum class EncResult {
OK,
ERROR,
DONE
};
EncResult 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);
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