2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2019-02-27 11:57:17 -05:00
# pragma once
# include "CoreMinimal.h"
2023-02-11 12:39:56 -05:00
# include "ISubmixBufferListener.h"
2019-02-27 11:57:17 -05:00
# include "Logging/LogMacros.h"
# include "AudioMixerDevice.h"
# include "RHI.h"
# include "RHIResources.h"
2021-04-29 19:32:06 -04:00
# include "HAL/Thread.h"
THIRD_PARTY_INCLUDES_START
# pragma warning(push)
2022-01-11 18:10:09 -05:00
# pragma warning(disable: 4596 6319 6323)
2021-04-29 19:32:06 -04:00
# pragma warning(pop)
THIRD_PARTY_INCLUDES_END
# include "AudioEncoder.h"
# include "VideoEncoder.h"
# include "MediaPacket.h"
2023-08-16 14:28:25 -04:00
# include "Templates/SharedPointer.h"
# include "Templates/SharedPointerInternals.h"
2021-04-29 19:32:06 -04:00
# include "VideoEncoderInput.h"
2019-03-07 11:25:32 -05:00
class SWindow ;
2019-02-27 11:57:17 -05:00
class IGameplayMediaEncoderListener
{
public :
2024-03-14 20:30:26 -04:00
PRAGMA_DISABLE_DEPRECATION_WARNINGS
2021-04-29 19:32:06 -04:00
virtual void OnMediaSample ( const AVEncoder : : FMediaPacket & Sample ) = 0 ;
2024-03-14 20:30:26 -04:00
PRAGMA_ENABLE_DEPRECATION_WARNINGS
2019-02-27 11:57:17 -05:00
} ;
2024-03-14 20:30:26 -04:00
PRAGMA_DISABLE_DEPRECATION_WARNINGS
class UE_DEPRECATED ( 5.4 , " GameplayMediaEncoder will move to a plugin in a future Unreal Engine release. " ) FGameplayMediaEncoder final : public ISubmixBufferListener , public AVEncoder : : IAudioEncoderListener
PRAGMA_ENABLE_DEPRECATION_WARNINGS
2019-02-27 11:57:17 -05:00
{
public :
/**
* Get the singleton
*/
2023-06-17 18:13:06 -04:00
static GAMEPLAYMEDIAENCODER_API FGameplayMediaEncoder * Get ( ) ;
2019-02-27 11:57:17 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API ~ FGameplayMediaEncoder ( ) ;
2019-02-27 11:57:17 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API bool RegisterListener ( IGameplayMediaEncoderListener * Listener ) ;
GAMEPLAYMEDIAENCODER_API void UnregisterListener ( IGameplayMediaEncoderListener * Listener ) ;
2019-02-27 11:57:17 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API void SetVideoBitrate ( uint32 Bitrate ) ;
GAMEPLAYMEDIAENCODER_API void SetVideoFramerate ( uint32 Framerate ) ;
2019-02-27 11:57:17 -05:00
2021-04-29 19:32:06 -04:00
///**
// * Returns the audio codec name and configuration
// */
//TPair<FString, AVEncoder::FAudioConfig> GetAudioConfig() const;
//TPair<FString, AVEncoder::FVideoConfig> GetVideoConfig() const;
2019-11-26 17:28:51 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API bool Initialize ( ) ;
GAMEPLAYMEDIAENCODER_API void Shutdown ( ) ;
GAMEPLAYMEDIAENCODER_API bool Start ( ) ;
GAMEPLAYMEDIAENCODER_API void Stop ( ) ;
2019-02-27 11:57:17 -05:00
static void InitializeCmd ( )
{
// We call Get(), so it creates the singleton
Get ( ) - > Initialize ( ) ;
}
static void ShutdownCmd ( )
{
// We call Get(), so it creates the singleton
Get ( ) - > Shutdown ( ) ;
}
static void StartCmd ( )
{
// We call Get(), so it creates the singleton
Get ( ) - > Start ( ) ;
}
static void StopCmd ( )
{
// We call Get(), so it creates the singleton
Get ( ) - > Stop ( ) ;
}
2024-03-14 20:30:26 -04:00
PRAGMA_DISABLE_DEPRECATION_WARNINGS
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API AVEncoder : : FAudioConfig GetAudioConfig ( ) const ;
2021-04-29 19:32:06 -04:00
AVEncoder : : FVideoConfig GetVideoConfig ( ) const { return VideoConfig ; }
2024-03-14 20:30:26 -04:00
PRAGMA_ENABLE_DEPRECATION_WARNINGS
2021-04-29 19:32:06 -04:00
2019-02-27 11:57:17 -05:00
private :
2023-08-16 14:28:25 -04:00
template < typename ObjectType , ESPMode Mode >
friend class SharedPointerInternals : : TIntrusiveReferenceController ;
2019-02-27 11:57:17 -05:00
2019-11-26 17:28:51 -05:00
// Private to control how our single instance is created
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API FGameplayMediaEncoder ( ) ;
2019-11-26 17:28:51 -05:00
// Returns how long it has been recording for.
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API FTimespan GetMediaTimestamp ( ) const ;
2019-02-27 11:57:17 -05:00
// Back buffer capture
2024-02-22 11:38:35 -05:00
GAMEPLAYMEDIAENCODER_API void OnFrameBufferReady ( SWindow & SlateWindow , const FTextureRHIRef & FrameBuffer ) ;
2023-08-16 14:28:25 -04:00
2019-02-27 11:57:17 -05:00
// ISubmixBufferListener interface
2023-08-16 14:28:25 -04:00
GAMEPLAYMEDIAENCODER_API const FString & GetListenerName ( ) const override ;
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API void OnNewSubmixBuffer ( const USoundSubmix * OwningSubmix , float * AudioData , int32 NumSamples , int32 NumChannels , const int32 SampleRate , double AudioClock ) override ;
2019-02-27 11:57:17 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API void ProcessAudioFrame ( const float * AudioData , int32 NumSamples , int32 NumChannels , int32 SampleRate ) ;
2024-02-22 11:38:35 -05:00
GAMEPLAYMEDIAENCODER_API void ProcessVideoFrame ( const FTextureRHIRef & FrameBuffer ) ;
2019-02-27 11:57:17 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API void UpdateVideoConfig ( ) ;
2019-02-27 11:57:17 -05:00
2024-03-14 20:30:26 -04:00
PRAGMA_DISABLE_DEPRECATION_WARNINGS
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API void OnEncodedAudioFrame ( const AVEncoder : : FMediaPacket & Packet ) override ;
GAMEPLAYMEDIAENCODER_API void OnEncodedVideoFrame ( uint32 LayerIndex , const TSharedPtr < AVEncoder : : FVideoEncoderInputFrame > Frame , const AVEncoder : : FCodecPacket & Packet ) ;
2019-11-26 17:28:51 -05:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API TSharedPtr < AVEncoder : : FVideoEncoderInputFrame > ObtainInputFrame ( ) ;
2024-03-14 20:30:26 -04:00
PRAGMA_ENABLE_DEPRECATION_WARNINGS
2024-02-22 11:38:35 -05:00
GAMEPLAYMEDIAENCODER_API void CopyTexture ( const FTextureRHIRef & SourceTexture , FTextureRHIRef & DestinationTexture ) const ;
2021-06-23 17:51:32 -04:00
2023-06-17 18:13:06 -04:00
GAMEPLAYMEDIAENCODER_API void FloatToPCM16 ( float const * floatSamples , int32 numSamples , TArray < int16 > & out ) const ;
2019-11-26 17:28:51 -05:00
2019-02-27 11:57:17 -05:00
FCriticalSection ListenersCS ;
TArray < IGameplayMediaEncoderListener * > Listeners ;
FCriticalSection AudioProcessingCS ;
FCriticalSection VideoProcessingCS ;
2021-04-29 19:32:06 -04:00
2024-03-14 20:30:26 -04:00
PRAGMA_DISABLE_DEPRECATION_WARNINGS
2019-11-26 17:28:51 -05:00
TUniquePtr < AVEncoder : : FAudioEncoder > AudioEncoder ;
2021-04-29 19:32:06 -04:00
AVEncoder : : FVideoConfig VideoConfig ;
2019-11-26 17:28:51 -05:00
TUniquePtr < AVEncoder : : FVideoEncoder > VideoEncoder ;
2021-04-29 19:32:06 -04:00
TSharedPtr < AVEncoder : : FVideoEncoderInput > VideoEncoderInput ;
2024-03-14 20:30:26 -04:00
PRAGMA_ENABLE_DEPRECATION_WARNINGS
2019-02-27 11:57:17 -05:00
uint64 NumCapturedFrames = 0 ;
FTimespan StartTime = 0 ;
2019-11-26 17:28:51 -05:00
2019-09-26 07:27:04 -04:00
// Instead of using the AudioClock parameter ISubmixBufferListener::OnNewSubmixBuffer gives us, we calculate our own, by
// advancing it as we receive more data.
// This is so that we can adjust the clock if things get out of sync, such as if we break into the debugger.
double AudioClock = 0 ;
2019-09-17 07:30:41 -04:00
2019-02-27 11:57:17 -05:00
FTimespan LastVideoInputTimestamp = 0 ;
2019-11-26 17:28:51 -05:00
bool bAudioFormatChecked = false ;
bool bDoFrameSkipping = false ;
friend class FGameplayMediaEncoderModule ;
2023-08-16 14:28:25 -04:00
static GAMEPLAYMEDIAENCODER_API TSharedPtr < FGameplayMediaEncoder , ESPMode : : ThreadSafe > Singleton ;
2019-02-27 11:57:17 -05:00
// live streaming: quality adaptation to available uplink b/w
2019-02-27 16:24:46 -05:00
TAtomic < uint32 > NewVideoBitrate { 0 } ;
2019-02-27 11:57:17 -05:00
FThreadSafeBool bChangeBitrate = false ;
2019-02-27 16:24:46 -05:00
TAtomic < uint32 > NewVideoFramerate { 0 } ;
2019-02-27 11:57:17 -05:00
FThreadSafeBool bChangeFramerate = false ;
2019-11-26 17:28:51 -05:00
2021-04-29 19:32:06 -04:00
TArray < int16 > PCM16 ;
2024-03-13 21:56:03 -04:00
PRAGMA_DISABLE_DEPRECATION_WARNINGS
2024-02-22 11:38:35 -05:00
TMap < TSharedPtr < AVEncoder : : FVideoEncoderInputFrame > , FTextureRHIRef > BackBuffers ;
2024-03-13 21:56:03 -04:00
PRAGMA_ENABLE_DEPRECATION_WARNINGS
2024-07-29 13:35:24 -04:00
bool bIsInDestructor = false ;
2019-02-27 11:57:17 -05:00
} ;
2024-03-14 20:30:26 -04:00