shithub: libopusenc

Download patch

ref: b718267504c90df4854218b4ffe2d38a4e552da7
parent: cd2f3a7fd28e1ee5a4e0898df938a19654598c26
author: Jean-Marc Valin <[email protected]>
date: Fri May 5 13:11:45 EDT 2017

split the calls for draining and destroying the object

--- a/examples/opusenc_example.c
+++ b/examples/opusenc_example.c
@@ -30,7 +30,8 @@
       ope_write(enc, buf, ret);
     } else break;
   }
-  ope_close_and_free(enc);
+  ope_drain(enc);
+  ope_destroy(enc);
   fclose(fin);
   return 0;
 }
--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -114,8 +114,11 @@
 /** Add/encode any number of int16 samples to the file. */
 OPE_EXPORT int ope_write(OggOpusEnc *enc, const opus_int16 *pcm, int samples_per_channel);
 
-/** Close/finalize the stream. */
-OPE_EXPORT int ope_close_and_free(OggOpusEnc *enc);
+/** Finalizes the stream, but does not deallocate the object. */
+OPE_EXPORT int ope_drain(OggOpusEnc *enc);
+
+/** Deallocates the obect. Make sure to ope_drain() first. */
+OPE_EXPORT void ope_destroy(OggOpusEnc *enc);
 
 /** Ends the stream and create a new stream within the same file. */
 OPE_EXPORT int ope_chain_current(OggOpusEnc *enc);
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -577,7 +577,7 @@
   return OPE_OK;
 }
 
-static void finalize_all_streams(OggOpusEnc *enc) {
+int ope_drain(OggOpusEnc *enc) {
   /* FIXME: Use a better value. */
   int pad_samples = 3000;
   if (!enc->streams->stream_is_init) init_stream(enc);
@@ -589,11 +589,12 @@
   assert(enc->buffer_end <= BUFFER_SAMPLES);
   encode_buffer(enc);
   assert(enc->streams == NULL);
+  return OPE_OK;
 }
 
 /* Close/finalize the stream. */
-int ope_close_and_free(OggOpusEnc *enc) {
-  finalize_all_streams(enc);
+void ope_destroy(OggOpusEnc *enc) {
+  /* FIXME: cleanup non-closed streams if any remain. */
   if (enc->chaining_keyframe) free(enc->chaining_keyframe);
   free(enc->buffer);
 #ifdef USE_OGGP
@@ -602,7 +603,6 @@
   opus_multistream_encoder_destroy(enc->st);
   if (enc->re) speex_resampler_destroy(enc->re);
   free(enc);
-  return OPE_OK;
 }
 
 /* Ends the stream and create a new stream within the same file. */