From 039388eaf0096148002dd6e46ab79bc4b671dd07 Mon Sep 17 00:00:00 2001 From: Alf Date: Thu, 24 Sep 2020 22:50:02 -0700 Subject: [PATCH] Try main.cpp instead of c. --- Makefile | 25 +++++++++---- src/main.cpp | 72 +++++++++++++++++++++++++++++++++++++ www/src/components/File.vue | 10 ++++-- 3 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 src/main.cpp diff --git a/Makefile b/Makefile index 1da52ac..eb00adc 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,21 @@ +# dist/ffmpeg-webtools.js: +# mkdir -p dist && \ +# emcc -L/opt/ffmpeg/lib -I/opt/ffmpeg/include/ src/main.c \ +# -s EXPORTED_FUNCTIONS='["_c_avformat_version", "_openfile", "_addOne"]' \ +# -s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, getValue, setValue, writeAsciiToMemory]" \ +# -s INITIAL_MEMORY=268435456 \ +# -lavcodec -lavformat -lavfilter -lavdevice -lswresample -lswscale -lavutil -lm -lx264 -pthread \ +# -o www/public/ffmpeg-webtools.js + dist/ffmpeg-webtools.js: mkdir -p dist && \ - emcc -L/opt/ffmpeg/lib -I/opt/ffmpeg/include/ src/main.c \ - -s EXPORTED_FUNCTIONS='["_version", "_openfile"]' \ - -s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" \ - -s INITIAL_MEMORY=268435456 \ - -lavcodec -lavformat -lavfilter -lavdevice -lswresample -lswscale -lavutil -lm -lx264 -pthread \ - -o $@ \ No newline at end of file + emcc \ + --bind \ + -L/opt/ffmpeg/lib \ + -I/opt/ffmpeg/include/ \ + -s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, getValue, setValue, writeAsciiToMemory]" \ + -s INITIAL_MEMORY=268435456 \ + -pthread \ + -lavcodec -lavformat -lavfilter -lavdevice -lswresample -lswscale -lavutil -lm -lx264 -pthread \ + -o www/public/ffmpeg-webtools.js \ + src/main.cpp \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..094e2c4 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,72 @@ +#include +#include + + +using namespace emscripten; + + +extern "C" { + +#include +#include +#include + + +const std::string c_avformat_version() { + return AV_STRINGIFY(LIBAVFORMAT_VERSION); +} + +struct Response { + std::string name; + int bit_rate; + int duration; +}; + +Response c_openfile() { + FILE *file = fopen("testingfs", "rb"); + if (!file) { + printf("cannot open file\n"); + } + fclose(file); + + AVFormatContext *pFormatContext = avformat_alloc_context(); + if (!pFormatContext) { + printf("ERROR: could not allocate memory for Format Context\n"); + } + printf("%p\n", pFormatContext); + + printf("opening the input file: %s and loading format (container) header\n", "testingfs"); + + // Open the file and read header. + int ret; + if ((ret = avformat_open_input(&pFormatContext, "testingfs", NULL, NULL)) < 0) { + printf("ERROR: could not open the file. Error: %d\n", ret); + printf("%s", av_err2str(ret)); + } + + printf("format: %s, duration: %lld us, bit_rate: %lld\n", + pFormatContext->iformat->name, + pFormatContext->duration, + pFormatContext->bit_rate); + + Response r; + r.name = pFormatContext->iformat->name; + r.duration = pFormatContext->duration; + r.bit_rate = pFormatContext->bit_rate; + return r; +} +}; + + +EMSCRIPTEN_BINDINGS(my_constant_example) { + function("c_avformat_version", &c_avformat_version); +} + +EMSCRIPTEN_BINDINGS(a_struct) { + emscripten::value_object("Response") + .field("name", &Response::name) + .field("duration", &Response::duration) + .field("bit_rate", &Response::bit_rate) + ; + function("c_openfile", &c_openfile); +} \ No newline at end of file diff --git a/www/src/components/File.vue b/www/src/components/File.vue index 2a5116b..0587b43 100644 --- a/www/src/components/File.vue +++ b/www/src/components/File.vue @@ -22,7 +22,10 @@
Selected file: {{ file ? `${file.name}: ${file.size} bytes` : '' }}

{{ info }} + + avformat_version: {{ avformat_version }} + @@ -40,14 +43,17 @@ export default { } }, computed: { + avformat_version() { + // return window.Module.ccall('c_avformat_version', 'string'); + return window.Module.c_avformat_version(); + }, info() { - const data = this.data; console.log('writing data to memfs'); window.Module.FS.writeFile('testingfs', new Uint8Array(data)); console.log('writing data to memfs done'); - window.Module.ccall('version'); + // window.Module.ccall('version'); // Window.Module.ccall('openfile'); return 'info'; }