Skip to content

Commit

Permalink
update audio decoder pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
yezhan10 committed Aug 14, 2024
1 parent afbb4a8 commit 14d2655
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 61 deletions.
6 changes: 3 additions & 3 deletions lib/AudioDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AudioDecoder extends Emitter
super();

//Create decoder
this.decoder = new Native.AudioDecoderFacade();
this.decoder = new Native.AudioDecoderFacadeShared();

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

Expand All @@ -64,7 +64,7 @@ class AudioDecoder extends Emitter
//Get first encoding
const encoding = track.encodings.values().next();
//Set it
this.decoder.SetIncoming(encoding.value.source.toRTPIncomingMediaStream());
track.depacketizer.AddMediaListener(this.decoder.toMediaFrameListener());
//Listen for events
track.once("stopped",this.ontrackstopped);
//Keep attached object
Expand Down
65 changes: 12 additions & 53 deletions src/AudioDecoderFacade.i
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ class AudioDecoderFacade : public AudioDecoderWorker
{
public:
AudioDecoderFacade() = default;
~AudioDecoderFacade()
{
Log("-AudioDecoderFacade::~AudioDecoderFacade()() [incoming:%p,this:%p]\n",incoming.get(),this);
//Remove listener from old stream
if (this->incoming)
this->incoming->RemoveListener(this);
}
~AudioDecoderFacade() = default;

void SetAACConfig(v8::Local<v8::Object> config)
{
Expand All @@ -30,61 +24,26 @@ public:
//Free config
free(data);
}

bool SetIncoming(const RTPIncomingMediaStream::shared& incoming)
{
Log("-AudioDecoderFacade::SetIncoming() [incoming:%p,this:%p]\n",incoming.get(),this);

//TODO: may be a sync issue here with onEnded

//If they are the same
if (this->incoming==incoming)
//DO nothing
return false;
//Remove listener from old stream
if (this->incoming)
this->incoming->RemoveListener(this);

//Store stream and receiver
this->incoming = incoming;
//Double check
if (this->incoming)
//Add us as listeners
this->incoming->AddListener(this);

//OK
return true;
}

int Stop()
{
SetIncoming({});
return AudioDecoderWorker::Stop();
}

virtual void onEnded(RTPIncomingMediaStream* incoming)
{
Log("-AudioDecoderFacade::onEnded() [incoming:%p,this:%p]\n",incoming,this);
//If they are the same
if (this->incoming.get()==incoming)
this->incoming.reset();
AudioDecoderWorker::onEnded(incoming);
}

private:
RTPIncomingMediaStream::shared incoming;
};
%}

%include "RTPIncomingMediaStream.i"

struct AudioDecoderFacade
{
int Start();
void SetAACConfig(v8::Local<v8::Object> config);
void AddAudioOuput(AudioOutput* ouput);
void RemoveAudioOutput(AudioOutput* ouput);
bool SetIncoming(const RTPIncomingMediaStreamShared& incomingSource);
int Stop();

};

SHARED_PTR_BEGIN(AudioDecoderFacade)
{
AudioDecoderFacadeShared()
{
return new std::shared_ptr<AudioDecoderFacade>(new AudioDecoderFacade());
}
SHARED_PTR_TO(MediaFrameListener)
}
SHARED_PTR_END(AudioDecoderFacade)

8 changes: 4 additions & 4 deletions src/audio-codecs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ export class AudioCodecs {
static EnableUltraDebug(flag: boolean): void;
}

export class AudioDecoderFacade {
export class AudioDecoderFacadeShared {

constructor();

Start(): number;

SetAACConfig(config: any): void;
Expand All @@ -172,11 +174,9 @@ export class AudioDecoderFacade {

RemoveAudioOutput(ouput: any): void;

SetIncoming(incomingSource: RTPIncomingMediaStreamShared): boolean;
toMediaFrameListener(): MediaFrameListenerShared;

Stop(): number;

constructor();
}

export class AudioInput {
Expand Down
1 change: 0 additions & 1 deletion src/audio-codecs.i
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
%include "MediaFrame.i"
%include "MediaFrameListenerBridge.i"
%include "Properties.i"
%include "RTPIncomingMediaStream.i"

%include "AudioCodecs.i"
%include "AudioDecoderFacade.i"
Expand Down

0 comments on commit 14d2655

Please sign in to comment.