Skip to content

Commit

Permalink
feat: add setLogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
ForeverSc committed Aug 13, 2024
1 parent 9b2c33b commit 8f4cf02
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 147 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ WEB_DEMUXER_ARGS = \
-O3 \
-s EXPORT_ES6=1 \
-s INVOKE_RUN=0 \
-s ENVIRONMENT=worker \
-s EXPORTED_RUNTIME_METHODS=cwrap,getValue,UTF8ToString \
-s EXPORTED_FUNCTIONS=_free \
-s ENVIRONMENT=worker \
-s ASYNCIFY \
-s WASM_BIGINT \
-s ALLOW_MEMORY_GROWTH=1

clean:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ async function seek(file, time) {
- [Seek Video Frame](https://foreversc.github.io/web-demuxer/#example-seek)[code](https://github.com/ForeverSc/web-demuxer/blob/main/index.html#L96)
- [Play Video](https://foreversc.github.io/web-demuxer/#example-play)[code](https://github.com/ForeverSc/web-demuxer/blob/main/index.html#L123)

Here is the translation of the provided TypeScript API documentation into English:

## API
```typescript
new WebDemuxer(options: WebDemuxerOptions)
Expand Down
6 changes: 1 addition & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ <h3>Get All Video Packets</h3>

await demuxer.load(file);

const videoDecoderConfig = await demuxer.getVideoDecoderConfig().catch(e => {
console.error('get video decoder config error:', e);
});
const videoDecoderConfig = await demuxer.getVideoDecoderConfig();
const videoChunk = await demuxer.seekEncodedVideoChunk(seekTime);

console.log("getVideoStream", await demuxer.getVideoStream())

const decoder = new VideoDecoder({
output: (frame) => {
const scale = Math.min(canvas.width / frame.displayWidth, canvas.height / frame.displayHeight);
Expand Down
5 changes: 5 additions & 0 deletions lib/web-demuxer/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,18 @@ function genSendAVPacket(messageId) {
}
}

function setAVLogLevel(level) {
Module.set_av_log_level(level);
}

// ============ Module Register ============
Module.getAVStream = getAVStream;
Module.getAVStreams = getAVStreams;
Module.getMediaInfo = getMediaInfo;
Module.getAVPacket = getAVPacket;
Module.getAVPackets = getAVPackets;
Module.readAVPacket = readAVPacket;
Module.setAVLogLevel = setAVLogLevel;

Module.onRuntimeInitialized = () => {
self.postMessage({ type: "WASMRuntimeInitialized" });
Expand Down
7 changes: 5 additions & 2 deletions lib/web-demuxer/web_demuxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ void gen_web_stream(WebAVStream &web_stream, AVStream *stream, AVFormatContext *

WebAVStream get_av_stream(std::string filename, int type, int wanted_stream_nb)
{
// av_log_set_level(AV_LOG_QUIET);

AVFormatContext *fmt_ctx = NULL;
int ret;

Expand Down Expand Up @@ -600,6 +598,10 @@ int read_av_packet(std::string filename, double start, double end, int type, int
return 1;
}

void set_av_log_level(int level) {
av_log_set_level(level);
}

EMSCRIPTEN_BINDINGS(web_demuxer)
{
value_object<Tag>("Tag")
Expand Down Expand Up @@ -667,6 +669,7 @@ EMSCRIPTEN_BINDINGS(web_demuxer)
function("get_av_packet", &get_av_packet, return_value_policy::take_ownership());
function("get_av_packets", &get_av_packets, return_value_policy::take_ownership());
function("read_av_packet", &read_av_packet);
function("set_av_log_level", &set_av_log_level);

register_vector<uint8_t>("vector<uint8_t>");
register_vector<Tag>("vector<Tag>");
Expand Down
232 changes: 125 additions & 107 deletions src/ffmpeg.worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WebAVPacket, WebAVStream } from "./types";
import { FFMpegWorkerMessageType, GetAVPacketMessageData, GetAVPacketsMessageData, GetAVStreamMessageData, GetAVStreamsMessageData, GetMediaInfoMessageData, LoadWASMMessageData, ReadAVPacketMessageData, SetAVLogLevelMessageData, WebAVPacket, WebAVStream } from "./types";

let Module: any; // TODO: rm any

Expand All @@ -7,114 +7,28 @@ self.postMessage({
});

self.addEventListener("message", async function (e) {
const { type, data = {}, msgId } = e.data;
const { type, data, msgId } = e.data

try {
if (type === "LoadWASM") {
const { wasmLoaderPath } = data || {}

const ModuleLoader = await import(/* @vite-ignore */wasmLoaderPath);
Module = await ModuleLoader.default();
} else if (type === "GetAVStream") {
const {
file,
streamType,
streamIndex,
} = data;
const result = Module.getAVStream(file, streamType, streamIndex);

self.postMessage(
{
type,
msgId,
result,
},
[result.extradata.buffer],
);
} else if (type === 'GetAVStreams') {
const {
file,
} = data;
const result = Module.getAVStreams(file);

self.postMessage(
{
type,
msgId,
result,
},
result.map((stream: WebAVStream) => stream.extradata.buffer)
);
} else if (type === "GetMediaInfo") {
const {
file,
} = data;
const result = Module.getMediaInfo(file);

self.postMessage({
type,
msgId,
result,
}, result.streams.map((stream: WebAVStream) => stream.extradata.buffer));
} else if (type === "GetAVPacket") {
const {
file,
time,
streamType,
streamIndex,
} = data;
const result = Module.getAVPacket(
file,
time,
streamType,
streamIndex,
);

self.postMessage(
{
type,
msgId,
result,
},
[result.data.buffer],
);
} else if (type === 'GetAVPackets') {
const {
file,
time,
} = data;
const result = Module.getAVPackets(file, time);

self.postMessage(
{
type,
msgId,
result,
},
result.map((packet: WebAVPacket) => packet.data.buffer),
);
} else if (type === "ReadAVPacket") {
const {
file,
start,
end,
streamType,
streamIndex,
} = data;
const result = await Module.readAVPacket(
msgId,
file,
start,
end,
streamType,
streamIndex,
);

self.postMessage({
type,
msgId,
result,
});
switch (type) {
case "LoadWASM":
return handleLoadWASM(data);
case "GetAVStream":
return handleGetAVStream(data, msgId);
case "GetAVStreams":
return handleGetAVStreams(data, msgId);
case "GetMediaInfo":
return handleGetMediaInfo(data, msgId);
case "GetAVPacket":
return handleGetAVPacket(data, msgId);
case "GetAVPackets":
return handleGetAVPackets(data, msgId);
case "ReadAVPacket":
return handleReadAVPacket(data, msgId);
case "SetAVLogLevel":
return handleSetAVLogLevel(data, msgId);
default:
return;
}
} catch (e) {
self.postMessage({
Expand All @@ -124,3 +38,107 @@ self.addEventListener("message", async function (e) {
});
}
});

async function handleLoadWASM(data: LoadWASMMessageData) {
const { wasmLoaderPath } = data || {};
const ModuleLoader = await import(/* @vite-ignore */wasmLoaderPath);
Module = await ModuleLoader.default();
}

function handleGetAVStream(data: GetAVStreamMessageData, msgId: number) {
const { file, streamType, streamIndex } = data;
const result = Module.getAVStream(file, streamType, streamIndex);

self.postMessage(
{
type: FFMpegWorkerMessageType.GetAVStream,
msgId,
result,
},
[result.extradata.buffer],
);
}

function handleGetAVStreams(data: GetAVStreamsMessageData, msgId: number) {
const { file } = data;
const result = Module.getAVStreams(file);

self.postMessage(
{
type: FFMpegWorkerMessageType.GetAVStreams,
msgId,
result,
},
result.map((stream: WebAVStream) => stream.extradata.buffer),
);
}

function handleGetMediaInfo(data: GetMediaInfoMessageData, msgId: number) {
const { file } = data;
const result = Module.getMediaInfo(file);

self.postMessage(
{
type: FFMpegWorkerMessageType.GetMediaInfo,
msgId,
result,
},
result.streams.map((stream: WebAVStream) => stream.extradata.buffer)
);
}

function handleGetAVPacket(data: GetAVPacketMessageData, msgId: number) {
const { file, time, streamType, streamIndex } = data;
const result = Module.getAVPacket(file, time, streamType, streamIndex);

self.postMessage(
{
type: FFMpegWorkerMessageType.GetAVPacket,
msgId,
result,
},
[result.data.buffer],
);
}

function handleGetAVPackets(data: GetAVPacketsMessageData, msgId: number) {
const { file, time } = data;
const result = Module.getAVPackets(file, time);

self.postMessage(
{
type: FFMpegWorkerMessageType.GetAVPackets,
msgId,
result,
},
result.map((packet: WebAVPacket) => packet.data.buffer),
);
}

async function handleReadAVPacket(data: ReadAVPacketMessageData, msgId: number) {
const { file, start, end, streamType, streamIndex } = data;
const result = await Module.readAVPacket(
msgId,
file,
start,
end,
streamType,
streamIndex,
);

self.postMessage({
type: FFMpegWorkerMessageType.ReadAVPacket,
msgId,
result,
});
}

function handleSetAVLogLevel(data: SetAVLogLevelMessageData, msgId: number) {
const { level } = data

Module.setAVLogLevel(level);
self.postMessage({
type: "SetAVLogLevel",
msgId,
})
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { WebDemuxer } from "./web-demuxer";

export type { WebAVStream, WebAVPacket } from './types';
export { AVMediaType } from './types';
export { WebDemuxer } from "./web-demuxer";
export { AVMediaType, AVLogLevel } from './types';
export { WebDemuxer };
export default WebDemuxer;
43 changes: 43 additions & 0 deletions src/types/avutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,46 @@ export enum AVMediaType {
AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse
AVMEDIA_TYPE_NB,
}

export enum AVLogLevel {
/**
* Print no output.
*/
AV_LOG_QUIET = -8,
/**
* Something went really wrong and we will crash now.
*/
AV_LOG_PANIC = 0,
/**
* Something went wrong and recovery is not possible.
* For example, no header was found for a format which depends
* on headers or an illegal combination of parameters is used.
*/
AV_LOG_FATAL = 8,
/**
* Something went wrong and cannot losslessly be recovered.
* However, not all future data is affected.
*/
AV_LOG_ERROR = 16,
/**
* Something somehow does not look correct. This may or may not
* lead to problems. An example would be the use of '-vstrict -2'.
*/
AV_LOG_WARNING = 24,
/**
* Standard information.
*/
AV_LOG_INFO = 32,
/**
* Detailed information.
*/
AV_LOG_VERBOSE = 40,
/**
* Stuff which is only useful for libav* developers.
*/
AV_LOG_DEBUG = 48,
/**
* Extremely verbose debugging, useful for libav* development.
*/
AV_LOG_TRACE = 56,
}
Loading

0 comments on commit 8f4cf02

Please sign in to comment.