Skip to content

Commit

Permalink
Merge pull request #26 from bakjos/feature/media_player_options
Browse files Browse the repository at this point in the history
feat: Add media player options settings
  • Loading branch information
bakjos authored Mar 17, 2021
2 parents 8c00f8f + 61e81e9 commit 9f6d83d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
30 changes: 17 additions & 13 deletions Source/FFMPEGMedia/Private/Player/FFMPEGMediaPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,23 @@ IMediaView& FFFMPEGMediaPlayer::GetView()
return *this;
}

bool FFFMPEGMediaPlayer::Open(const FString& Url, const IMediaOptions* Options, const FMediaPlayerOptions* PlayerOptions) {
Close();

bool FFFMPEGMediaPlayer::Open(const FString& Url, const IMediaOptions* Options)
{
Close();
if (Url.IsEmpty())
{
return false;
}

if (Url.IsEmpty())
{
return false;
}
const bool Precache = (Options != nullptr) ? Options->GetMediaOption("PrecacheFile", false) : false;

return InitializePlayer(nullptr, Url, Precache, PlayerOptions);
}

const bool Precache = (Options != nullptr) ? Options->GetMediaOption("PrecacheFile", false) : false;

return InitializePlayer(nullptr, Url, Precache);
bool FFFMPEGMediaPlayer::Open(const FString& Url, const IMediaOptions* Options)
{
return Open(Url, Options, nullptr);
}


Expand All @@ -174,7 +178,7 @@ bool FFFMPEGMediaPlayer::Open(const TSharedRef<FArchive, ESPMode::ThreadSafe>& A
return false;
}

return InitializePlayer(Archive, OriginalUrl, false);
return InitializePlayer(Archive, OriginalUrl, false, nullptr);
}


Expand Down Expand Up @@ -229,7 +233,7 @@ void FFFMPEGMediaPlayer::TickInput(FTimespan DeltaTime, FTimespan Timecode)
/* FFFMPEGMediaPlayer implementation
*****************************************************************************/

bool FFFMPEGMediaPlayer::InitializePlayer(const TSharedPtr<FArchive, ESPMode::ThreadSafe>& Archive, const FString& Url, bool Precache)
bool FFFMPEGMediaPlayer::InitializePlayer(const TSharedPtr<FArchive, ESPMode::ThreadSafe>& Archive, const FString& Url, bool Precache, const FMediaPlayerOptions* PlayerOptions )
{
UE_LOG(LogFFMPEGMedia, Verbose, TEXT("Player %llx: Initializing %s (archive = %s, precache = %s)"), this, *Url, Archive.IsValid() ? TEXT("yes") : TEXT("no"), Precache ? TEXT("yes") : TEXT("no"));

Expand All @@ -243,15 +247,15 @@ bool FFFMPEGMediaPlayer::InitializePlayer(const TSharedPtr<FArchive, ESPMode::Th
const EAsyncExecution Execution = Precache ? EAsyncExecution::Thread : EAsyncExecution::ThreadPool;


TFunction <void()> Task = [Archive, Url, Precache, TracksPtr = TWeakPtr<FFFMPEGMediaTracks, ESPMode::ThreadSafe>(Tracks), ThisPtr=this]()
TFunction <void()> Task = [Archive, Url, Precache, PlayerOptions, TracksPtr = TWeakPtr<FFFMPEGMediaTracks, ESPMode::ThreadSafe>(Tracks), ThisPtr=this]()
{
TSharedPtr<FFFMPEGMediaTracks, ESPMode::ThreadSafe> PinnedTracks = TracksPtr.Pin();

if (PinnedTracks.IsValid() )
{
AVFormatContext* context = ThisPtr->ReadContext(Archive, Url, Precache);
if (context) {
PinnedTracks->Initialize(context, Url);
PinnedTracks->Initialize(context, Url, PlayerOptions);
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion Source/FFMPEGMedia/Private/Player/FFMPEGMediaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class FFFMPEGMediaPlayer
virtual IMediaTracks& GetTracks() override;
virtual FString GetUrl() const override;
virtual IMediaView& GetView() override;
virtual bool Open(const FString& Url, const IMediaOptions* Options, const FMediaPlayerOptions* PlayerOptions) override;
virtual bool Open(const FString& Url, const IMediaOptions* Options) override;
virtual bool Open(const TSharedRef<FArchive, ESPMode::ThreadSafe>& Archive, const FString& OriginalUrl, const IMediaOptions* Options) override;
virtual void TickFetch(FTimespan DeltaTime, FTimespan Timecode) override;
Expand All @@ -76,7 +77,7 @@ class FFFMPEGMediaPlayer
* @param Precache Whether to precache media into RAM if InURL is a local file.
* @return true on success, false otherwise.
*/
bool InitializePlayer(const TSharedPtr<FArchive, ESPMode::ThreadSafe>& Archive, const FString& Url, bool Precache);
bool InitializePlayer(const TSharedPtr<FArchive, ESPMode::ThreadSafe>& Archive, const FString& Url, bool Precache, const FMediaPlayerOptions* PlayerOptions);



Expand Down
19 changes: 15 additions & 4 deletions Source/FFMPEGMedia/Private/Player/FFMPEGMediaTracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "UObject/Class.h"
#include "IMediaBinarySample.h"
#include "IMediaEventSink.h"
#include "MediaPlayerOptions.h"


#if WITH_ENGINE
Expand Down Expand Up @@ -233,10 +234,16 @@ void FFFMPEGMediaTracks::GetFlags(bool& OutMediaSourceChanged, bool& OutSelectio
}


void FFFMPEGMediaTracks::Initialize(AVFormatContext* ic, const FString& Url)
void FFFMPEGMediaTracks::Initialize(AVFormatContext* ic, const FString& Url, const FMediaPlayerOptions* PlayerOptions )
{
Shutdown();


FMediaPlayerTrackOptions TrackOptions;
if (PlayerOptions != nullptr)
{
TrackOptions = PlayerOptions->Tracks;
}


UE_LOG(LogFFMPEGMedia, Verbose, TEXT("Tracks: %p: Initializing (media source %p)"), this, ic);

Expand Down Expand Up @@ -287,7 +294,7 @@ void FFFMPEGMediaTracks::Initialize(AVFormatContext* ic, const FString& Url)

for (int i = 0; i < (int)ic->nb_streams; i++) {
AVStream *st = ic->streams[i];
bool streamAdded = AddStreamToTracks(i, false, Info);
bool streamAdded = AddStreamToTracks(i, false, TrackOptions, Info);
AllStreamsAdded &= streamAdded;
if ( streamAdded) {
totalStreams++;
Expand Down Expand Up @@ -1129,7 +1136,7 @@ bool FFFMPEGMediaTracks::SetRate(float Rate) {
/* FFFMPEGMediaTracks implementation
*****************************************************************************/

bool FFFMPEGMediaTracks::AddStreamToTracks(uint32 StreamIndex, bool IsVideoDevice, FString& OutInfo)
bool FFFMPEGMediaTracks::AddStreamToTracks(uint32 StreamIndex, bool IsVideoDevice, const FMediaPlayerTrackOptions& TrackOptions, FString& OutInfo)
{
OutInfo += FString::Printf(TEXT("Stream %i\n"), StreamIndex);

Expand Down Expand Up @@ -1276,6 +1283,10 @@ bool FFFMPEGMediaTracks::AddStreamToTracks(uint32 StreamIndex, bool IsVideoDevic


Track->StreamIndex = StreamIndex;

if (MediaType == AVMEDIA_TYPE_SUBTITLE && TrackOptions.Caption == INDEX_NONE) {
return false;
}

return true;
}
Expand Down
5 changes: 3 additions & 2 deletions Source/FFMPEGMedia/Private/Player/FFMPEGMediaTracks.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Math/IntPoint.h"
#include "MediaSampleQueue.h"
#include "Templates/SharedPointer.h"
#include "MediaPlayerOptions.h"

#include "HAL/RunnableThread.h"

Expand Down Expand Up @@ -144,7 +145,7 @@ class FFFMPEGMediaTracks
* @param Url The media source URL.
* @see IsInitialized, Shutdown
*/
void Initialize(AVFormatContext *ic, const FString& Url);
void Initialize(AVFormatContext *ic, const FString& Url, const FMediaPlayerOptions* PlayerOptions );

/**
* Reinitialize the track collection
Expand Down Expand Up @@ -241,7 +242,7 @@ class FFFMPEGMediaTracks
* @return true on success, false otherwise.
* @see AddTrackToTopology
*/
bool AddStreamToTracks(uint32 StreamIndex, bool IsVideoDevice, FString& OutInfo);
bool AddStreamToTracks(uint32 StreamIndex, bool IsVideoDevice, const FMediaPlayerTrackOptions& TrackOptions, FString& OutInfo);

/**
* Add the given track to the specified playback topology.
Expand Down

0 comments on commit 9f6d83d

Please sign in to comment.