Skip to content

Commit

Permalink
DIOS-5052 attach VideoDecoder to depacketizer instead of source (#9)
Browse files Browse the repository at this point in the history
* attach VideoDecoder to depacketizer instead of source

* Update lib/VideoDecoder.js

Co-authored-by: Sergio Garcia Murillo <sergio.garcia.murillo@gmail.com>

* another typo

* anooooother typo

* rename VideoDecoderFacade.i to VideoDecoderWorker.i

* Fixes

* Update to new VideoDecoder apis

---------

Co-authored-by: Sergio Garcia Murillo <sergio.garcia.murillo@gmail.com>
  • Loading branch information
mildsunrise and murillo128 authored May 31, 2024
1 parent 340b270 commit 6f500e1
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 214 deletions.
10 changes: 4 additions & 6 deletions lib/VideoDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class VideoDecoder extends Emitter
super();

//Create decoder
this.decoder = new Native.VideoDecoderFacade();
this.decoder = new Native.VideoDecoderWorkerShared();

//Start decoder
this.decoder.Start();
Expand All @@ -32,7 +32,7 @@ class VideoDecoder extends Emitter
if (this.attached)
{
//Detach native decoder
this.decoder.SetIncoming(null);
this.attached.depacketizer.RemoveMediaListener(this.decoder.toMediaFrameListener());
//remove listener
this.attached.off("stopped",this.ontrackstopped);

Expand All @@ -49,10 +49,8 @@ class VideoDecoder extends Emitter
//Check if valid object
if (track)
{
//Get first encoding
const encoding = track.encodings.values().next();
//Set it
this.decoder.SetIncoming(encoding.value.source.toRTPIncomingMediaStream());
//Set incoming
track.depacketizer.AddMediaListener(this.decoder.toMediaFrameListener());
//Listen for events
track.once("stopped",this.ontrackstopped);
//Keep attached object
Expand Down
19 changes: 12 additions & 7 deletions src/ThumbnailGenerator.i
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public:
const uint8_t* data = (const uint8_t*)uint8array->Buffer()->GetBackingStore()->Data() + uint8array->ByteOffset();
const uint32_t size = uint8array->ByteLength();



//Get codec for
VideoCodec::Type codec = VideoCodec::GetCodecForName(decoderName);

Expand All @@ -49,7 +51,7 @@ public:
});

//Generate thumbanail in a different thread
auto thread = new std::thread([=,cloned=persistent](){
auto thread = new std::thread([=, videoFrame = std::make_shared<VideoFrame>(codec, std::make_shared<Buffer>(data,size)), cloned=persistent](){
//Create encoder
Properties properties;
std::unique_ptr<VideoEncoder> encoder(VideoCodecFactory::CreateEncoder(encoderCodec,properties));
Expand Down Expand Up @@ -81,9 +83,9 @@ public:
//Call method
MakeCallback(cloned,"reject",i,argv);
});

//Decode frame
if(!decoder->Decode(data, size))
if(!decoder->Decode(videoFrame))
return VideoCodecsModule::Async([=,cloned=cloned](){
Nan::HandleScope scope;
int i = 0;
Expand All @@ -92,9 +94,12 @@ public:
//Call method
MakeCallback(cloned,"reject",i,argv);
});


//Get frame
auto videoBuffer = decoder->GetFrame();

//Check decoded frame
if (!decoder->GetWidth() || !decoder->GetHeight() || !decoder->GetFrame())
if (!videoBuffer)
return VideoCodecsModule::Async([=,cloned2=cloned](){
Nan::HandleScope scope;
int i = 0;
Expand All @@ -105,7 +110,7 @@ public:
});

//Set decoded size
if (!encoder->SetSize(decoder->GetWidth(),decoder->GetHeight()))
if (!encoder->SetSize(videoBuffer->GetWidth(),videoBuffer->GetHeight()))
return VideoCodecsModule::Async([=,cloned2=cloned](){
Nan::HandleScope scope;
int i = 0;
Expand All @@ -116,7 +121,7 @@ public:
});

//Encoder jpeb
auto frame = encoder->EncodeFrame(decoder->GetFrame());
auto frame = encoder->EncodeFrame(videoBuffer);

//Check
if (!frame)
Expand Down
57 changes: 0 additions & 57 deletions src/VideoDecoderFacade.i

This file was deleted.

19 changes: 19 additions & 0 deletions src/VideoDecoderWorker.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%include "MediaFrame.i"

struct VideoDecoderWorker : public MediaFrameListener
{
int Start();
void AddVideoOutput(VideoOutput* ouput);
void RemoveVideoOutput(VideoOutput* ouput);
int Stop();
};

SHARED_PTR_BEGIN(VideoDecoderWorker)
{
VideoDecoderWorkerShared()
{
return new std::shared_ptr<VideoDecoderWorker>(new VideoDecoderWorker());
}
SHARED_PTR_TO(MediaFrameListener)
}
SHARED_PTR_END(VideoDecoderWorker)
2 changes: 1 addition & 1 deletion src/video-codecs.i
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ bool MakeCallback(const std::shared_ptr<Persistent<v8::Object>>& persistent, con


%include "VideoEncoderFacade.i"
%include "VideoDecoderFacade.i"
%include "VideoDecoderWorker.i"
%include "ThumbnailGenerator.i"


Expand Down
Loading

0 comments on commit 6f500e1

Please sign in to comment.