Properly cleanup even when ope_drain() isn't called
authorJean-Marc Valin <jmvalin@jmvalin.ca>
Sat, 6 May 2017 01:04:55 +0000 (21:04 -0400)
committerJean-Marc Valin <jmvalin@jmvalin.ca>
Sat, 6 May 2017 01:04:55 +0000 (21:04 -0400)
COPYING
src/opusenc.c

diff --git a/COPYING b/COPYING
index 7b53d66..029b03d 100644 (file)
--- a/COPYING
+++ b/COPYING
@@ -1,4 +1,5 @@
 Copyright (c) 1994-2013 Xiph.Org Foundation and contributors
+Copyright (c) 2017 Jean-Marc Valin
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
index 9443e85..650a814 100644 (file)
@@ -596,6 +596,8 @@ OPE_EXPORT int ope_get_page(OggOpusEnc *enc, unsigned char **page, int *len, int
 }
 
 int ope_drain(OggOpusEnc *enc) {
+  /* Check if it's already been drained. */
+  if (enc->streams == NULL) return OPE_TOO_LATE;
   /* FIXME: Use a better value. */
   int pad_samples = 3000;
   if (!enc->streams->stream_is_init) init_stream(enc);
@@ -611,7 +613,14 @@ int ope_drain(OggOpusEnc *enc) {
 }
 
 void ope_destroy(OggOpusEnc *enc) {
-  /* FIXME: cleanup non-closed streams if any remain. */
+  EncStream *stream;
+  stream = enc->streams;
+  while (stream != NULL) {
+    EncStream *tmp = stream;
+    stream = stream->next;
+    if (tmp->close_at_end) enc->callbacks.close(tmp->user_data);
+    stream_destroy(tmp);
+  }
   if (enc->chaining_keyframe) free(enc->chaining_keyframe);
   free(enc->buffer);
 #ifdef USE_OGGP