// Copyright Epic Games, Inc. All Rights Reserved. #include "MediaSource.h" #include "IMediaAssetsModule.h" #include "MediaAssetsPrivate.h" #include "MediaTexture.h" #include "Modules/ModuleManager.h" #include "Misc/Paths.h" #include "StreamMediaSource.h" #if WITH_EDITOR #include "MediaSourceRendererInterface.h" #include "UObject/Package.h" #endif #include UE_INLINE_GENERATED_CPP_BY_NAME(MediaSource) #if WITH_EDITOR void UMediaSource::GenerateThumbnail() { if (MediaSourceRenderer == nullptr) { IMediaAssetsModule* MediaAssetsModule = FModuleManager::LoadModulePtr("MediaAssets"); if (MediaAssetsModule != nullptr) { MediaSourceRenderer = MediaAssetsModule->CreateMediaSourceRenderer(); } } if (MediaSourceRenderer != nullptr) { IMediaSourceRendererInterface* Interface = Cast(MediaSourceRenderer); if (Interface != nullptr) { ThumbnailImage = Interface->Open(this); } } } #endif // WITH_EDITOR void UMediaSource::RegisterSpawnFromFileExtension(const FString& Extension, FMediaSourceSpawnDelegate InDelegate) { TMap& Delegates = GetSpawnFromFileExtensionDelegates(); Delegates.Emplace(Extension, InDelegate); } void UMediaSource::UnregisterSpawnFromFileExtension(const FString& Extension) { TMap& Delegates = GetSpawnFromFileExtensionDelegates(); Delegates.Remove(Extension); } UMediaSource* UMediaSource::SpawnMediaSourceForString(const FString& MediaPath, UObject* Outer) { TObjectPtr MediaSource = nullptr; // Is it a URL? bool bIsUrl = MediaPath.Contains(TEXT("://")); if (bIsUrl) { TObjectPtr StreamMediaSource = NewObject(Outer, NAME_None, RF_Transactional); StreamMediaSource->StreamUrl = MediaPath; MediaSource = StreamMediaSource; } else { // Do we know about this file extension? FString FileExtension = FPaths::GetExtension(MediaPath); TMap& Delegates = GetSpawnFromFileExtensionDelegates(); FMediaSourceSpawnDelegate* Delegate = Delegates.Find(FileExtension); if ((Delegate != nullptr) && (Delegate->IsBound())) { MediaSource = Delegate->Execute(MediaPath, Outer); } } // Validate the media source. if (MediaSource != nullptr) { if (!MediaSource->Validate()) { UE_LOG(LogMediaAssets, Error, TEXT("Failed to validate %s"), *MediaPath); MediaSource = nullptr; } } return MediaSource; } TMap& UMediaSource::GetSpawnFromFileExtensionDelegates() { static TMap Delegates; return Delegates; } void UMediaSource::BeginDestroy() { #if WITH_EDITOR MediaSourceRenderer = nullptr; #endif // WITH_EDITOR Super::BeginDestroy(); } /* IMediaOptions interface *****************************************************************************/ FName UMediaSource::GetDesiredPlayerName() const { return NAME_None; } bool UMediaSource::GetMediaOption(const FName& Key, bool DefaultValue) const { const FVariant* Variant = GetMediaOptionDefault(Key); if ((Variant != nullptr) && (Variant->GetType() == EVariantTypes::Bool)) { return Variant->GetValue(); } else { return DefaultValue; } } double UMediaSource::GetMediaOption(const FName& Key, double DefaultValue) const { const FVariant* Variant = GetMediaOptionDefault(Key); if ((Variant != nullptr) && (Variant->GetType() == EVariantTypes::Double)) { return Variant->GetValue(); } else { return DefaultValue; } } int64 UMediaSource::GetMediaOption(const FName& Key, int64 DefaultValue) const { const FVariant* Variant = GetMediaOptionDefault(Key); if ((Variant != nullptr) && (Variant->GetType() == EVariantTypes::Int64)) { return Variant->GetValue(); } else { return DefaultValue; } } FString UMediaSource::GetMediaOption(const FName& Key, const FString& DefaultValue) const { const FVariant* Variant = GetMediaOptionDefault(Key); if ((Variant != nullptr) && (Variant->GetType() == EVariantTypes::String)) { return Variant->GetValue(); } else { return DefaultValue; } } FText UMediaSource::GetMediaOption(const FName& Key, const FText& DefaultValue) const { return DefaultValue; } TSharedPtr UMediaSource::GetMediaOption(const FName& Key, const TSharedPtr& DefaultValue) const { return DefaultValue; } bool UMediaSource::HasMediaOption(const FName& Key) const { return MediaOptionsMap.Contains(Key); } const UObject* UMediaSource::ToUObject() const { return this; } void UMediaSource::SetMediaOptionBool(const FName& Key, bool Value) { FVariant Variant(Value); SetMediaOption(Key, Variant); } void UMediaSource::SetMediaOptionFloat(const FName& Key, float Value) { SetMediaOptionDouble(Key, (double)Value); } void UMediaSource::SetMediaOptionDouble(const FName& Key, double Value) { FVariant Variant(Value); SetMediaOption(Key, Variant); } void UMediaSource::SetMediaOptionInt64(const FName& Key, int64 Value) { FVariant Variant(Value); SetMediaOption(Key, Variant); } void UMediaSource::SetMediaOptionString(const FName& Key, const FString& Value) { FVariant Variant(Value); SetMediaOption(Key, Variant); } const FVariant* UMediaSource::GetMediaOptionDefault(const FName& Key) const { return MediaOptionsMap.Find(Key); } void UMediaSource::SetMediaOption(const FName& Key, FVariant& Value) { MediaOptionsMap.Emplace(Key, Value); }