Skip to content

Commit

Permalink
Seamless Travel
Browse files Browse the repository at this point in the history
- Updated Odin Synth Component to restart automatically after a Seamless Server Travel
- Fixed and improved error reporting of Remove Media from Room
  • Loading branch information
DavidLiebemann committed Jul 1, 2024
1 parent a46ff9f commit 7167e2a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 52 deletions.
5 changes: 3 additions & 2 deletions Odin.uplugin
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"FileVersion": 2,
"Version": 413,
"EngineVersion": "5.4.0",
"Version": 415,
"WhitelistPlatforms": [ "Win64", "Mac", "IOS", "Linux", "LinuxArm64", "Android" ],
"VersionName": "1.8.8",
"VersionName": "1.8.9",
"FriendlyName": "4Players ODIN",
"Description": "Unreal integration plugin to integrate real-time chat technology into your game",
"Category": "Other",
Expand Down
1 change: 1 addition & 0 deletions Source/Odin/Private/OdinMediaSoundGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int32 OdinMediaSoundGenerator::OnGenerateAudio(float* OutAudio, int32 NumSamples
for (IAudioBufferListener* AudioBufferListener : AudioBufferListeners) {
AudioBufferListener->OnGeneratedBuffer(OutAudio, NumSamples, 2);
}

return read;
}

Expand Down
12 changes: 10 additions & 2 deletions Source/Odin/Private/OdinRoom.AsyncNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,20 @@ void UOdinRoomRemoveMedia::Activate()
FFunctionGraphTask::CreateAndDispatchWhenReady(
[WeakThis]() {
if (UOdinRoomRemoveMedia* This = WeakThis.Get()) {
OdinReturnCode result = -1;
OdinReturnCode result = 1 << 30;
if (!This->Room.IsValid()) {
UE_LOG(Odin, Error, TEXT("OdinRoomRemoveMedia: The Room reference is invalid."))
}

if (!This->CaptureMedia.IsValid()) {
UE_LOG(Odin, Error,
TEXT("OdinRoomRemoveMedia: The CaptureMedia reference is invalid."))
}

if (This->Room.IsValid() && This->CaptureMedia.IsValid()) {
This->Room->UnbindCaptureMedia(This->CaptureMedia.Get());
result = This->CaptureMedia->ResetOdinStream();
}

bool IsError = odin_is_error(result);
if (IsError) {
This->OnError.ExecuteIfBound(result);
Expand Down
79 changes: 44 additions & 35 deletions Source/Odin/Private/OdinSynthComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,59 @@
bool UOdinSynthComponent::Init(int32 &SampleRate)
{
NumChannels = 2;
// We reset the stream handle here, to avoid any kind of delays after re-enabling
ResetOdinStream(StreamHandle);
return true;
}

void UOdinSynthComponent::BeginDestroy()
{
if (this->sound_generator_) {
this->sound_generator_->SetOdinStream(0);
this->playback_media_ = nullptr;
Super::BeginDestroy();
}

void UOdinSynthComponent::OnRegister()
{
Super::OnRegister();
if (nullptr != playback_media_ && 0 != StreamHandle) {
Start();
}
}

this->sound_generator_ = nullptr;
this->playback_media_ = nullptr;
int32 UOdinSynthComponent::OnGenerateAudio(float *OutAudio, int32 NumSamples)
{
if (StreamHandle == 0) {
return 0;
}

Super::BeginDestroy();
auto read = odin_audio_read_data(StreamHandle, OutAudio, NumSamples);
if (odin_is_error(read)) {
return NumSamples;
}
for (IAudioBufferListener *AudioBufferListener : AudioBufferListeners) {
AudioBufferListener->OnGeneratedBuffer(OutAudio, NumSamples, NumChannels);
}
return read;
}

void UOdinSynthComponent::SetOdinStream(OdinMediaStreamHandle NewStreamHandle)
{
ResetOdinStream(NewStreamHandle);
this->StreamHandle = NewStreamHandle;
}

void UOdinSynthComponent::ResetOdinStream(OdinMediaStreamHandle HandleToReset)
{
if (0 != HandleToReset) {
odin_audio_reset(HandleToReset);
}
}

void UOdinSynthComponent::Odin_AssignSynthToMedia(UPARAM(ref) UOdinPlaybackMedia *&media)
{
if (nullptr != media) {
this->playback_media_ = media;
if (sound_generator_) {
sound_generator_->SetOdinStream(media->GetMediaHandle());
}
SetOdinStream(media->GetMediaHandle());
} else {
UE_LOG(Odin, Error,
TEXT("UOdinSynthComponent::Odin_AssignSynthToMedia: Tried to assign null media to "
Expand All @@ -44,7 +75,7 @@ void UOdinSynthComponent::Odin_AssignSynthToMedia(UPARAM(ref) UOdinPlaybackMedia
void UOdinSynthComponent::Reset()
{
if (this->playback_media_ != nullptr) {
odin_audio_reset(this->playback_media_->GetMediaHandle());
ResetOdinStream(this->playback_media_->GetMediaHandle());
}
}

Expand All @@ -56,9 +87,9 @@ void UOdinSynthComponent::AdjustAttenuation(const FSoundAttenuationSettings &InA
bOverrideAttenuation = true;
AttenuationOverrides = InAttenuationSettings;

auto audioComponent = GetAudioComponent();
if (audioComponent) {
audioComponent->AdjustAttenuation(InAttenuationSettings);
auto AudioComponentPointer = GetAudioComponent();
if (AudioComponentPointer) {
AudioComponentPointer->AdjustAttenuation(InAttenuationSettings);
}

Activate(true);
Expand All @@ -67,31 +98,9 @@ void UOdinSynthComponent::AdjustAttenuation(const FSoundAttenuationSettings &InA
void UOdinSynthComponent::AddAudioBufferListener(IAudioBufferListener *InAudioBufferListener)
{
AudioBufferListeners.AddUnique(InAudioBufferListener);
if (nullptr != sound_generator_)
sound_generator_->AddAudioBufferListener(InAudioBufferListener);
}

void UOdinSynthComponent::RemoveAudioBufferListener(IAudioBufferListener *InAudioBufferListener)
{
AudioBufferListeners.Remove(InAudioBufferListener);
if (nullptr != sound_generator_)
sound_generator_->RemoveAudioBufferListener(InAudioBufferListener);
}

#if ENGINE_MAJOR_VERSION >= 5
ISoundGeneratorPtr
UOdinSynthComponent::CreateSoundGenerator(const FSoundGeneratorInitParams &InParams)
#else
ISoundGeneratorPtr UOdinSynthComponent::CreateSoundGenerator(int32 InSampleRate,
int32 InNumChannels)
#endif
{
this->sound_generator_ = MakeShared<OdinMediaSoundGenerator, ESPMode::ThreadSafe>();
if (this->playback_media_ != nullptr) {
sound_generator_->SetOdinStream(this->playback_media_->GetMediaHandle());
for (IAudioBufferListener *AudioBufferListener : AudioBufferListeners) {
sound_generator_->AddAudioBufferListener(AudioBufferListener);
}
}
return sound_generator_;
}
}
2 changes: 1 addition & 1 deletion Source/Odin/Public/OdinRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ class ODIN_API UOdinRoom : public /* USceneComponent */ UObject
UPROPERTY(
BlueprintAssignable, Category = "Odin|Room|Events",
meta = (DisplayName = "onConnectionStateChanged", DeprecatedProperty,
DeprecationMessage = "Deprecated, instead use On Room Connection State Changed."))
DeprecationMessage = "Use \"On Room Connection State Changed\" instead please."))
FOdinRoomConnectionStatChanged_DEPRECATED onConnectionStateChanged_DEPRECATED;

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOdinRoomConnectionStateChanged,
Expand Down
26 changes: 14 additions & 12 deletions Source/Odin/Public/OdinSynthComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,28 @@ class ODIN_API UOdinSynthComponent : public USynthComponent
UFUNCTION(BlueprintCallable, Category = "Odin|Sound")
void AdjustAttenuation(const FSoundAttenuationSettings &InAttenuationSettings);

// We want to hide the non-virtual function in USynthComponent here!
void AddAudioBufferListener(IAudioBufferListener *InAudioBufferListener);
// We want to hide the non-virtual function in USynthComponent here!
void RemoveAudioBufferListener(IAudioBufferListener *InAudioBufferListener);

protected:
bool Init(int32 &SampleRate) override;
void BeginDestroy() override;
virtual bool Init(int32 &SampleRate) override;

protected:
#if ENGINE_MAJOR_VERSION >= 5
virtual ISoundGeneratorPtr
CreateSoundGenerator(const FSoundGeneratorInitParams &InParams) override;
#else
virtual ISoundGeneratorPtr CreateSoundGenerator(int32 InSampleRate,
int32 InNumChannels) override;
#endif
virtual void BeginDestroy() override;
virtual void OnRegister() override;

virtual int32 OnGenerateAudio(float *OutAudio, int32 NumSamples) override;

void SetOdinStream(OdinMediaStreamHandle NewStreamHandle);
void ResetOdinStream(OdinMediaStreamHandle HandleToReset);

private:
UPROPERTY()
UOdinPlaybackMedia *playback_media_ = nullptr;

TSharedPtr<OdinMediaSoundGenerator, ESPMode::ThreadSafe> sound_generator_;
TArray<IAudioBufferListener *> AudioBufferListeners;
// TSharedPtr<OdinMediaSoundGenerator, ESPMode::ThreadSafe> sound_generator_;
TArray<IAudioBufferListener *> AudioBufferListeners;

OdinMediaStreamHandle StreamHandle = 0;
};

0 comments on commit 7167e2a

Please sign in to comment.