Skip to content

Commit

Permalink
跳过反复初始化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
langhuihui committed Jun 5, 2022
1 parent 8235f46 commit 30e7bec
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ node_modules
demo/.vitepress/dist
yarn.lock
package-lock.json

.DS_Store
Binary file modified demo/public/decoder.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions demo/public/jessibuca.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/public/jessibuca.js.map

Large diffs are not rendered by default.

Binary file modified dist/decoder.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/jessibuca.js

Large diffs are not rendered by default.

Binary file modified src/decoder/decoder.wasm
Binary file not shown.
56 changes: 23 additions & 33 deletions wasm/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ class FFmpeg
bool initialized = false;
FFmpeg(val &&v) : jsObject(forward<val>(v))
{
pkt = av_packet_alloc();
frame = av_frame_alloc();
}
void initCodec(enum AVCodecID id)
{
if (dec_ctx != nullptr)
{
clear();
}
pkt = av_packet_alloc();
codec = avcodec_find_decoder(id);
parser = av_parser_init(codec->id);
dec_ctx = avcodec_alloc_context3(codec);
frame = av_frame_alloc();
}
void initCodec(enum AVCodecID id, string input)
{
Expand All @@ -67,6 +67,8 @@ class FFmpeg
virtual ~FFmpeg()
{
clear();
av_frame_free(&frame);
av_packet_free(&pkt);
}
virtual int decode(string input, u32 timestamp)
{
Expand All @@ -84,25 +86,12 @@ class FFmpeg
return 0;
}
virtual void _decode(u32 timestamp){};
virtual void clear()
void clear()
{
if (parser)
{
av_parser_close(parser);
parser = nullptr;
}
if (dec_ctx)
{
avcodec_free_context(&dec_ctx);
}
if (frame)
{
av_frame_free(&frame);
}
if (pkt)
{
av_packet_free(&pkt);
}
av_parser_close(parser);
parser = nullptr;
avcodec_free_context(&dec_ctx);

codec = nullptr;
initialized = false;
}
Expand Down Expand Up @@ -236,19 +225,20 @@ class FFmpegVideoDecoder : public FFmpeg
if (((int)(data[0]) >> 4) == 1 && data[1] == 0)
{
// emscripten_log(0, "codec = %d", codec_id);
if (!initialized)
jsObject.call<void>("setVideoCodec", codec_id);
switch (codec_id)
{
case 7:
initCodec(AV_CODEC_ID_H264, data.substr(5));
break;
case 12:
initCodec(AV_CODEC_ID_H265, data.substr(5));
break;
default:
emscripten_log(0, "codec not support: %d", codec_id);
return -1;
if (!initialized){
jsObject.call<void>("setVideoCodec", codec_id);
switch (codec_id)
{
case 7:
initCodec(AV_CODEC_ID_H264, data.substr(5));
break;
case 12:
initCodec(AV_CODEC_ID_H265, data.substr(5));
break;
default:
emscripten_log(0, "codec not support: %d", codec_id);
return -1;
}
}
}
else
Expand Down

1 comment on commit 30e7bec

@vercel
Copy link

@vercel vercel bot commented on 30e7bec Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.