Try main.cpp instead of c.

This commit is contained in:
Alf
2020-09-24 22:50:02 -07:00
parent 711903032c
commit 039388eaf0
3 changed files with 99 additions and 8 deletions

View File

@@ -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: dist/ffmpeg-webtools.js:
mkdir -p dist && \ mkdir -p dist && \
emcc -L/opt/ffmpeg/lib -I/opt/ffmpeg/include/ src/main.c \ emcc \
-s EXPORTED_FUNCTIONS='["_version", "_openfile"]' \ --bind \
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" \ -L/opt/ffmpeg/lib \
-s INITIAL_MEMORY=268435456 \ -I/opt/ffmpeg/include/ \
-lavcodec -lavformat -lavfilter -lavdevice -lswresample -lswscale -lavutil -lm -lx264 -pthread \ -s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, getValue, setValue, writeAsciiToMemory]" \
-o $@ -s INITIAL_MEMORY=268435456 \
-pthread \
-lavcodec -lavformat -lavfilter -lavdevice -lswresample -lswscale -lavutil -lm -lx264 -pthread \
-o www/public/ffmpeg-webtools.js \
src/main.cpp

72
src/main.cpp Normal file
View File

@@ -0,0 +1,72 @@
#include <emscripten.h>
#include <emscripten/bind.h>
using namespace emscripten;
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
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>("Response")
.field("name", &Response::name)
.field("duration", &Response::duration)
.field("bit_rate", &Response::bit_rate)
;
function("c_openfile", &c_openfile);
}

View File

@@ -22,7 +22,10 @@
<div class="mt-3">Selected file: {{ file ? `${file.name}: ${file.size} bytes` : '' }}</div> <div class="mt-3">Selected file: {{ file ? `${file.name}: ${file.size} bytes` : '' }}</div>
<hr /> <hr />
{{ info }} {{ info }}
avformat_version: {{ avformat_version }}
</div> </div>
</div> </div>
</template> </template>
@@ -40,14 +43,17 @@ export default {
} }
}, },
computed: { computed: {
avformat_version() {
// return window.Module.ccall('c_avformat_version', 'string');
return window.Module.c_avformat_version();
},
info() { info() {
const data = this.data; const data = this.data;
console.log('writing data to memfs'); console.log('writing data to memfs');
window.Module.FS.writeFile('testingfs', new Uint8Array(data)); window.Module.FS.writeFile('testingfs', new Uint8Array(data));
console.log('writing data to memfs done'); console.log('writing data to memfs done');
window.Module.ccall('version'); // window.Module.ccall('version');
// Window.Module.ccall('openfile'); // Window.Module.ccall('openfile');
return 'info'; return 'info';
} }