|
@@ -31,13 +31,12 @@ int audio_read_thread (void* arg) {
|
|
|
ret = av_read_frame (stream->getFormatContext (), packet);
|
|
ret = av_read_frame (stream->getFormatContext (), packet);
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
if (ret == AVERROR_EOF) {
|
|
|
- // a one-shot stream is done, rewinding/flushing would only race the decoder still draining the queue
|
|
|
|
|
if (!stream->isRepeat ()) {
|
|
if (!stream->isRepeat ()) {
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
avformat_seek_file (stream->getFormatContext (), stream->getAudioStream (), 0, 0, 0, ~AVSEEK_FLAG_FRAME);
|
|
avformat_seek_file (stream->getFormatContext (), stream->getAudioStream (), 0, 0, 0, ~AVSEEK_FLAG_FRAME);
|
|
|
- stream->flushCodec ();
|
|
|
|
|
|
|
+ stream->queueLoopMarker ();
|
|
|
ret = 0;
|
|
ret = 0;
|
|
|
|
|
|
|
|
continue;
|
|
continue;
|
|
@@ -165,10 +164,6 @@ AudioStream::~AudioStream () {
|
|
|
|
|
|
|
|
delete this->m_queue;
|
|
delete this->m_queue;
|
|
|
|
|
|
|
|
- if (this->m_codecMutex != nullptr) {
|
|
|
|
|
- SDL_DestroyMutex (this->m_codecMutex);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
if (this->m_formatContext != nullptr) {
|
|
if (this->m_formatContext != nullptr) {
|
|
|
avformat_free_context (this->m_formatContext);
|
|
avformat_free_context (this->m_formatContext);
|
|
|
}
|
|
}
|
|
@@ -289,8 +284,6 @@ void AudioStream::initialize () {
|
|
|
sLog.exception ("Failed to initialize the resampling context.");
|
|
sLog.exception ("Failed to initialize the resampling context.");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this->m_codecMutex = SDL_CreateMutex ();
|
|
|
|
|
-
|
|
|
|
|
this->m_queue->mutex = SDL_CreateMutex ();
|
|
this->m_queue->mutex = SDL_CreateMutex ();
|
|
|
this->m_queue->cond = SDL_CreateCond ();
|
|
this->m_queue->cond = SDL_CreateCond ();
|
|
|
this->m_queue->wait = SDL_CreateCond ();
|
|
this->m_queue->wait = SDL_CreateCond ();
|
|
@@ -353,10 +346,23 @@ bool AudioStream::doQueue (AVPacket* pkt) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void AudioStream::flushCodec () {
|
|
|
|
|
- SDL_LockMutex (this->m_codecMutex);
|
|
|
|
|
- avcodec_flush_buffers (this->m_context);
|
|
|
|
|
- SDL_UnlockMutex (this->m_codecMutex);
|
|
|
|
|
|
|
+void AudioStream::queueLoopMarker () {
|
|
|
|
|
+ AVPacket* marker = av_packet_alloc ();
|
|
|
|
|
+
|
|
|
|
|
+ if (marker == nullptr) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // empty packet, recognised by its stream index
|
|
|
|
|
+ marker->stream_index = NO_AUDIO_STREAM;
|
|
|
|
|
+
|
|
|
|
|
+ SDL_LockMutex (this->m_queue->mutex);
|
|
|
|
|
+ const bool gotQueued = this->doQueue (marker);
|
|
|
|
|
+ SDL_UnlockMutex (this->m_queue->mutex);
|
|
|
|
|
+
|
|
|
|
|
+ if (!gotQueued) {
|
|
|
|
|
+ av_packet_free (&marker);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void AudioStream::markReaderFinished () {
|
|
void AudioStream::markReaderFinished () {
|
|
@@ -367,45 +373,40 @@ void AudioStream::markReaderFinished () {
|
|
|
SDL_UnlockMutex (this->m_queue->mutex);
|
|
SDL_UnlockMutex (this->m_queue->mutex);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool AudioStream::dequeuePacket () {
|
|
|
|
|
|
|
+AudioStream::Dequeued AudioStream::dequeuePacket () {
|
|
|
MyAVPacketList entry {};
|
|
MyAVPacketList entry {};
|
|
|
- bool gotPacket = false;
|
|
|
|
|
|
|
+ // read before the queue, the reader sets it after queueing its last packet
|
|
|
|
|
+ const bool finished = this->m_readerFinished || !this->m_initialized;
|
|
|
|
|
|
|
|
SDL_LockMutex (this->m_queue->mutex);
|
|
SDL_LockMutex (this->m_queue->mutex);
|
|
|
|
|
|
|
|
- while (this->m_audioContext.getApplicationContext ().state.general.keepRunning) {
|
|
|
|
|
#if FF_API_FIFO_OLD_API
|
|
#if FF_API_FIFO_OLD_API
|
|
|
- int ret = -1;
|
|
|
|
|
|
|
+ int ret = -1;
|
|
|
|
|
|
|
|
- if (av_fifo_size (this->m_queue->packetList) >= static_cast<int> (sizeof (entry))) {
|
|
|
|
|
- ret = av_fifo_generic_read (this->m_queue->packetList, &entry, sizeof (entry), nullptr);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (av_fifo_size (this->m_queue->packetList) >= static_cast<int> (sizeof (entry))) {
|
|
|
|
|
+ ret = av_fifo_generic_read (this->m_queue->packetList, &entry, sizeof (entry), nullptr);
|
|
|
|
|
+ }
|
|
|
#else
|
|
#else
|
|
|
- const int ret = av_fifo_read (this->m_queue->packetList, &entry, 1);
|
|
|
|
|
|
|
+ const int ret = av_fifo_read (this->m_queue->packetList, &entry, 1);
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
- if (ret >= 0) {
|
|
|
|
|
- this->m_queue->nb_packets--;
|
|
|
|
|
- this->m_queue->size -= entry.packet->size + sizeof (entry);
|
|
|
|
|
- this->m_queue->duration -= entry.packet->duration;
|
|
|
|
|
-
|
|
|
|
|
- av_packet_move_ref (this->m_decodePacket, entry.packet);
|
|
|
|
|
- av_packet_free (&entry.packet);
|
|
|
|
|
- gotPacket = true;
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (ret >= 0) {
|
|
|
|
|
+ this->m_queue->nb_packets--;
|
|
|
|
|
+ this->m_queue->size -= entry.packet->size + sizeof (entry);
|
|
|
|
|
+ this->m_queue->duration -= entry.packet->duration;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // the reader is gone and the queue is drained, nobody will ever signal us again
|
|
|
|
|
- if (this->m_readerFinished || !this->m_initialized) {
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ SDL_UnlockMutex (this->m_queue->mutex);
|
|
|
|
|
|
|
|
- SDL_CondWaitTimeout (this->m_queue->cond, this->m_queue->mutex, 20);
|
|
|
|
|
|
|
+ if (ret < 0) {
|
|
|
|
|
+ return finished ? Dequeued::Finished : Dequeued::Empty;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- SDL_UnlockMutex (this->m_queue->mutex);
|
|
|
|
|
|
|
+ av_packet_unref (this->m_decodePacket);
|
|
|
|
|
+ av_packet_move_ref (this->m_decodePacket, entry.packet);
|
|
|
|
|
+ av_packet_free (&entry.packet);
|
|
|
|
|
|
|
|
- return gotPacket;
|
|
|
|
|
|
|
+ return Dequeued::Packet;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
AVCodecContext* AudioStream::getContext () const { return this->m_context; }
|
|
AVCodecContext* AudioStream::getContext () const { return this->m_context; }
|
|
@@ -464,8 +465,6 @@ AVRational AudioStream::getTimeBase () const {
|
|
|
|
|
|
|
|
int64_t AudioStream::getQueueDuration () const { return this->m_queue->duration; }
|
|
int64_t AudioStream::getQueueDuration () const { return this->m_queue->duration; }
|
|
|
|
|
|
|
|
-bool AudioStream::isQueueEmpty () const { return this->m_queue->nb_packets == 0; }
|
|
|
|
|
-
|
|
|
|
|
SDL_mutex* AudioStream::getMutex () const { return this->m_queue->mutex; }
|
|
SDL_mutex* AudioStream::getMutex () const { return this->m_queue->mutex; }
|
|
|
|
|
|
|
|
void AudioStream::stop () {
|
|
void AudioStream::stop () {
|
|
@@ -586,62 +585,84 @@ int AudioStream::resampleAudio (uint8_t* out_buf, const int out_size) {
|
|
|
return resampled_data_size;
|
|
return resampled_data_size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+int AudioStream::flushResampler (uint8_t* out_buf, const int out_size) {
|
|
|
|
|
+ const int channels = this->m_audioContext.getChannels ();
|
|
|
|
|
+ const int samples = swr_get_out_samples (this->m_swrctx, 0);
|
|
|
|
|
+
|
|
|
|
|
+ if (samples <= 0) {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ uint8_t* data = nullptr;
|
|
|
|
|
+
|
|
|
|
|
+ if (av_samples_alloc (&data, nullptr, channels, samples, this->m_audioContext.getFormat (), 1) < 0) {
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const int converted = swr_convert (this->m_swrctx, &data, samples, nullptr, 0);
|
|
|
|
|
+ const int size = converted > 0
|
|
|
|
|
+ ? av_samples_get_buffer_size (nullptr, channels, converted, this->m_audioContext.getFormat (), 1)
|
|
|
|
|
+ : -1;
|
|
|
|
|
+
|
|
|
|
|
+ if (size > 0) {
|
|
|
|
|
+ memcpy (out_buf, data, std::min (size, out_size));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ av_freep (&data);
|
|
|
|
|
+
|
|
|
|
|
+ return size > 0 ? std::min (size, out_size) : -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
int AudioStream::decodeFrame (uint8_t* audioBuffer, const int bufferSize) {
|
|
int AudioStream::decodeFrame (uint8_t* audioBuffer, const int bufferSize) {
|
|
|
- // block until there's any data in the buffers
|
|
|
|
|
|
|
+ // runs on the audio callback, so it never waits
|
|
|
while (this->m_audioContext.getApplicationContext ().state.general.keepRunning) {
|
|
while (this->m_audioContext.getApplicationContext ().state.general.keepRunning) {
|
|
|
- while (this->m_audioPacketSize > 0 && this->m_audioContext.getApplicationContext ().state.general.keepRunning) {
|
|
|
|
|
- int got_frame = 0;
|
|
|
|
|
|
|
+ const int ret = avcodec_receive_frame (this->getContext (), this->m_decodeFrame);
|
|
|
|
|
|
|
|
- SDL_LockMutex (this->m_codecMutex);
|
|
|
|
|
- int ret = avcodec_receive_frame (this->getContext (), this->m_decodeFrame);
|
|
|
|
|
|
|
+ if (ret == 0) {
|
|
|
|
|
+ const int size = this->resampleAudio (audioBuffer, bufferSize);
|
|
|
|
|
|
|
|
- if (ret == 0) {
|
|
|
|
|
- got_frame = 1;
|
|
|
|
|
- }
|
|
|
|
|
- if (ret == AVERROR (EAGAIN)) {
|
|
|
|
|
- ret = 0;
|
|
|
|
|
|
|
+ if (size > 0) {
|
|
|
|
|
+ return size;
|
|
|
}
|
|
}
|
|
|
- if (ret == 0) {
|
|
|
|
|
- ret = avcodec_send_packet (this->getContext (), this->m_decodePacket);
|
|
|
|
|
- }
|
|
|
|
|
- SDL_UnlockMutex (this->m_codecMutex);
|
|
|
|
|
|
|
|
|
|
- if (ret < 0 && ret != AVERROR (EAGAIN)) {
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (this->m_decodePacket->size < 0) {
|
|
|
|
|
- // if error, skip frame
|
|
|
|
|
- this->m_audioPacketSize = 0;
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ if (ret == AVERROR_EOF) {
|
|
|
|
|
+ if (this->m_loopDrain) {
|
|
|
|
|
+ avcodec_flush_buffers (this->getContext ());
|
|
|
|
|
+ this->m_loopDrain = false;
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this->m_audioPacketSize -= this->m_decodePacket->size;
|
|
|
|
|
- int data_size = 0;
|
|
|
|
|
|
|
+ return this->flushResampler (audioBuffer, bufferSize);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (got_frame) {
|
|
|
|
|
- data_size = this->resampleAudio (audioBuffer, bufferSize);
|
|
|
|
|
- }
|
|
|
|
|
- if (data_size <= 0) {
|
|
|
|
|
- // no data found, keep waiting
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- return data_size;
|
|
|
|
|
|
|
+ if (ret != AVERROR (EAGAIN)) {
|
|
|
|
|
+ return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (this->m_decodePacket->data) {
|
|
|
|
|
- av_packet_unref (this->m_decodePacket);
|
|
|
|
|
|
|
+ switch (this->dequeuePacket ()) {
|
|
|
|
|
+ case Dequeued::Empty:
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ case Dequeued::Finished:
|
|
|
|
|
+ avcodec_send_packet (this->getContext (), nullptr);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ case Dequeued::Packet:
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!this->dequeuePacket ()) {
|
|
|
|
|
- this->m_audioPacketSize = 0;
|
|
|
|
|
- return -1;
|
|
|
|
|
|
|
+ if (this->m_decodePacket->stream_index == NO_AUDIO_STREAM) {
|
|
|
|
|
+ avcodec_send_packet (this->getContext (), nullptr);
|
|
|
|
|
+ this->m_loopDrain = true;
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this->m_audioPacketSize = this->m_decodePacket->size;
|
|
|
|
|
|
|
+ avcodec_send_packet (this->getContext (), this->m_decodePacket);
|
|
|
|
|
+ av_packet_unref (this->m_decodePacket);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
AudioContext& AudioStream::getAudioContext () const { return this->m_audioContext; }
|
|
AudioContext& AudioStream::getAudioContext () const { return this->m_audioContext; }
|