shithub: libopusenc

Download patch

ref: 8630085e38ed539e461553503a8cb4efbc10c3f0
parent: e6459b4d783e6f4757054e7efce7ce147aff8c0d
author: Jean-Marc Valin <[email protected]>
date: Mon Apr 24 13:45:20 EDT 2017

Starting implementation (mostly still placeholders)

--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -34,6 +34,9 @@
 
 #include "opus.h"
 
+#define OPE_OK 0
+#define OPE_ERROR_CANNOT_OPEN -10
+
 typedef int (*ope_write_func)(void *user_data, const unsigned char *ptr, int len);
 
 typedef int (*ope_close_func)(void *user_data);
@@ -51,11 +54,11 @@
 
 /** Create a new OggOpus file. */
 OggOpusEnc *ope_create_file(const char *path, const OggOpusComments *comments,
-  int rate, int channels, int family, int *error);
+    int rate, int channels, int family, int *error);
 
 /** Create a new OggOpus file (callback-based). */
-OggOpusEnc *ope_create_callbacks(OpusEncCallbacks *callbacks, const OggOpusComments *comments,
-  void *user_data, int rate, int channels, int family, int *error);
+OggOpusEnc *ope_create_callbacks(const OpusEncCallbacks *callbacks, void *user_data,
+    const OggOpusComments *comments, int rate, int channels, int family, int *error);
 
 /** Add/encode any number of float samples to the file. */
 int ope_write_float(OggOpusEnc *enc, float *pcm, int samples_per_channel);
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -32,17 +32,32 @@
 #endif
 
 #include <stdlib.h>
+#include <stdio.h>
 #include "opusenc.h"
 
+int stdio_write(void *user_data, const unsigned char *ptr, int len) {
+  return fwrite(ptr, 1, len, (FILE*)user_data) != len;
+}
+
+static const OpusEncCallbacks stdio_callbacks = {
+  stdio_write,
+  fclose
+};
+
 /* Create a new OggOpus file. */
 OggOpusEnc *ope_create_file(const char *path, const OggOpusComments *comments,
-  int rate, int channels, int family, int *error) {
-  return NULL;
+    int rate, int channels, int family, int *error) {
+  FILE *file = fopen(path, "wb");
+  if (!file) {
+    if (error) *error = OPE_ERROR_CANNOT_OPEN;
+    return NULL;
+  }
+  return ope_create_callbacks(&stdio_callbacks, file, comments, rate, channels, family, error);
 }
 
 /* Create a new OggOpus file (callback-based). */
-OggOpusEnc *ope_create_callbacks(OpusEncCallbacks *callbacks, const OggOpusComments *comments,
-  void *user_data, int rate, int channels, int family, int *error) {
+OggOpusEnc *ope_create_callbacks(const OpusEncCallbacks *callbacks, void *user_data,
+    const OggOpusComments *comments, int rate, int channels, int family, int *error) {
   return NULL;
 }