From 00d4b8fdbae06bffff4a3cce51067baf23e5e0fb Mon Sep 17 00:00:00 2001 From: Alf Date: Fri, 25 Sep 2020 22:13:48 -0700 Subject: [PATCH] Add more bindings for AVFormatContext data. Add Overview page in Vue. Add required CORS headers for supporting SharedArrayBuffer in Firefox. --- Makefile | 12 +---- src/main.c | 38 -------------- src/main.cpp | 88 +++++++++++++++++++++++---------- www/src/components/File.vue | 25 +++++----- www/src/components/Overview.vue | 39 +++++++++++++++ www/vue.config.js | 8 +++ 6 files changed, 122 insertions(+), 88 deletions(-) delete mode 100644 src/main.c create mode 100644 www/src/components/Overview.vue diff --git a/Makefile b/Makefile index eb00adc..1763106 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,6 @@ -# 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 \ - --bind \ + emcc --bind \ -L/opt/ffmpeg/lib \ -I/opt/ffmpeg/include/ \ -s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, getValue, setValue, writeAsciiToMemory]" \ diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 9fd0501..0000000 --- a/src/main.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include - -#include -#include -#include - - -void version() { - printf("%s\n", AV_STRINGIFY(LIBAVFORMAT_VERSION)); -} - -void 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("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); -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 094e2c4..bbadfbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,29 +1,40 @@ +#include #include #include - using namespace emscripten; - extern "C" { - #include #include #include - +}; const std::string c_avformat_version() { return AV_STRINGIFY(LIBAVFORMAT_VERSION); } -struct Response { +struct Stream { + int id; + int start_time; + int duration; + int codec_type; +}; +typedef struct Stream Stream; + +struct FileInfoResponse { std::string name; int bit_rate; int duration; + std::string url; + int nb_streams; + int flags; + std::vector streams; }; +typedef struct FileInfoResponse FileInfoResponse; -Response c_openfile() { - FILE *file = fopen("testingfs", "rb"); +FileInfoResponse get_file_info() { + FILE *file = fopen("file", "rb"); if (!file) { printf("cannot open file\n"); } @@ -33,40 +44,65 @@ Response c_openfile() { 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) { + if ((ret = avformat_open_input(&pFormatContext, "file", 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); + // Initialize response struct with format data. + FileInfoResponse r = { + .name = pFormatContext->iformat->name, + .bit_rate = (int)pFormatContext->bit_rate, + .duration = (int)pFormatContext->duration, + .url = pFormatContext->url, + .nb_streams = (int)pFormatContext->nb_streams, + .flags = pFormatContext->flags + }; - Response r; - r.name = pFormatContext->iformat->name; - r.duration = pFormatContext->duration; - r.bit_rate = pFormatContext->bit_rate; + // Get streams data. + AVCodec *pCodec = NULL; + AVCodecParameters *pCodecParameters = NULL; + int video_stream_index = -1; + + // Loop through the streams and print its information. + for (int i = 0; i < pFormatContext->nb_streams; i++) { + AVCodecParameters *pLocalCodecParameters = NULL; + pLocalCodecParameters = pFormatContext->streams[i]->codecpar; + Stream s = { + .id = (int)pFormatContext->streams[i]->id, + .start_time = (int)pFormatContext->streams[i]->start_time, + .duration = (int)pFormatContext->streams[i]->duration, + .codec_type = (int)pLocalCodecParameters->codec_type + }; + r.streams.push_back(s); + } 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) +EMSCRIPTEN_BINDINGS(FileInfoResponse_struct) { + emscripten::value_object("Stream") + .field("id", &Stream::id) + .field("start_time", &Stream::start_time) + .field("duration", &Stream::duration) + .field("codec_type", &Stream::codec_type) ; - function("c_openfile", &c_openfile); + register_vector("Stream"); + emscripten::value_object("FileInfoResponse") + .field("name", &FileInfoResponse::name) + .field("duration", &FileInfoResponse::duration) + .field("bit_rate", &FileInfoResponse::bit_rate) + .field("url", &FileInfoResponse::url) + .field("nb_streams", &FileInfoResponse::nb_streams) + .field("flags", &FileInfoResponse::flags) + .field("streams", &FileInfoResponse::streams) + ; + function("get_file_info", &get_file_info); } \ No newline at end of file diff --git a/www/src/components/File.vue b/www/src/components/File.vue index 0587b43..cdf5d82 100644 --- a/www/src/components/File.vue +++ b/www/src/components/File.vue @@ -20,19 +20,25 @@
Selected file: {{ file ? `${file.name}: ${file.size} bytes` : '' }}
-
- {{ info }} - avformat_version: {{ avformat_version }} + + +
+ +
+
+
- \ No newline at end of file diff --git a/www/vue.config.js b/www/vue.config.js index 2aad78f..3caba08 100644 --- a/www/vue.config.js +++ b/www/vue.config.js @@ -8,4 +8,12 @@ module.exports = { publicPath: process.env.NODE_ENV === 'production' ? '/ffmpeg-webtools/' : '/', + configureWebpack: { + devServer: { + headers: { + 'Cross-Origin-Opener-Policy': 'same-origin', + 'Cross-Origin-Embedder-Policy': 'require-corp', + } + } + }, };