Skip to content

Commit 8cae5f5

Browse files
committed
encode: fix encoder flushing
'quit' changed meaning to 'immediately quit' during a past commit, causing encoders to not be flushed.
1 parent 70c0a4c commit 8cae5f5

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/cyanrip_encode.c

+18-5
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,26 @@ int cyanrip_send_pcm_to_encoders(cyanrip_ctx *ctx, cyanrip_enc_ctx **enc_ctx,
578578
return ret;
579579
}
580580

581+
void cyanrip_immediate_stop_encoding(cyanrip_ctx *ctx, cyanrip_track *t)
582+
{
583+
for (int i = 0; i < ctx->settings.outputs_num; i++) {
584+
cyanrip_enc_ctx *s = t->enc_ctx[i];
585+
if (s)
586+
atomic_store(&s->quit, 1);
587+
}
588+
}
589+
581590
int cyanrip_reset_encoding(cyanrip_ctx *ctx, cyanrip_track *t)
582591
{
592+
cyanrip_immediate_stop_encoding(ctx, t);
593+
583594
for (int i = 0; i < ctx->settings.outputs_num; i++) {
584595
cyanrip_enc_ctx *s = t->enc_ctx[i];
585596

586-
atomic_store(&s->quit, 1);
587-
pthread_mutex_unlock(&s->lock);
588-
s->mutex_held = 0;
597+
if (s->mutex_held) {
598+
pthread_mutex_unlock(&s->lock);
599+
s->mutex_held = 0;
600+
}
589601
}
590602

591603
for (int i = 0; i < ctx->settings.outputs_num; i++)
@@ -808,13 +820,12 @@ int cyanrip_end_track_encoding(cyanrip_enc_ctx **s)
808820

809821
ctx = *s;
810822

811-
atomic_store(&ctx->quit, 1);
812-
813823
if (ctx->mutex_held) {
814824
pthread_mutex_unlock(&ctx->lock);
815825
ctx->mutex_held = 0;
816826
}
817827

828+
/* Send EOF, needed in case we haven't sent it yet and the user cancels. */
818829
cr_frame_fifo_push(ctx->fifo, NULL);
819830
pthread_join(ctx->thread, NULL);
820831
pthread_mutex_destroy(&ctx->lock);
@@ -831,6 +842,8 @@ int cyanrip_end_track_encoding(cyanrip_enc_ctx **s)
831842
av_buffer_unref(&ctx->packet_fifo);
832843
av_packet_free(&ctx->cover_art_pkt);
833844

845+
atomic_store(&ctx->quit, 0);
846+
834847
int status = ctx->status;
835848
av_freep(s);
836849
return status;

src/cyanrip_encode.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int cyanrip_send_pcm_to_encoders(cyanrip_ctx *ctx, cyanrip_enc_ctx **enc_ctx,
3737
const uint8_t *data, int bytes,
3838
int calc_global_peak);
3939

40+
void cyanrip_immediate_stop_encoding(cyanrip_ctx *ctx, cyanrip_track *t);
4041
int cyanrip_reset_encoding(cyanrip_ctx *ctx, cyanrip_track *t);
4142
int cyanrip_finalize_encoding(cyanrip_ctx *ctx, cyanrip_track *t);
4243

src/cyanrip_main.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ const cyanrip_out_fmt crip_fmt_info[] = {
5858

5959
static void free_track(cyanrip_ctx *ctx, cyanrip_track *t)
6060
{
61+
if (quit_now)
62+
cyanrip_immediate_stop_encoding(ctx, t);
63+
6164
for (int i = 0; i < ctx->settings.outputs_num; i++)
6265
cyanrip_end_track_encoding(&t->enc_ctx[i]);
6366

@@ -818,7 +821,7 @@ repeat_ripping:;
818821
av_free(last_checksums);
819822

820823
t->total_repeats = total_repeats;
821-
if (!ret) {
824+
if (!quit_now && !ret) {
822825
cyanrip_finalize_encoding(ctx, t);
823826
if (ctx->settings.enable_replaygain)
824827
crip_replaygain_meta_track(ctx, t);

0 commit comments

Comments
 (0)