Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ FFMPEG Media Plugin for unreal engine
===================================

A plugin that let's you use the FFMPEG library as media player
WARNING: Video not work if DisableAudio = false.


## Features:
- Works on Windows and Mac
- Works on Windows, Linux and Mac
- Support for hardware accelerated codecs
- Support for videos with alpha

## How to use

1. The plugin works with Unreal 4.26
1. The plugin works with Unreal 5.4
2. You can clone the repository to `<Game>/Plugins/` or if you want you can use git submodules to your own git repository. Alternatively, you can copy to the `Engine/Plugins/` if you wish to make the plugin available to all of your projects.
> Do not forget to run UE4's `Generate Project Files` to account for these changes!
3. Follow the steps to [play a video tutorial](https://docs.unrealengine.com/en-us/Engine/MediaFramework/HowTo/FileMediaSource) but insead of using the automatic player, choose the FFMPEGPlayer inside the players overrides for the step 5.
Expand Down
2 changes: 0 additions & 2 deletions Source/FFMPEGMedia/Private/FFMPEG/FFMPEGDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ int FFMPEGDecoder::DecodeFrame(AVFrame* frame, AVSubtitle* sub)
av_packet_unref(&pkt);
}
}

return ret;
}

void FFMPEGDecoder::SetDecoderReorderPts(int pts)
Expand Down
2 changes: 1 addition & 1 deletion Source/FFMPEGMedia/Private/Player/FFMPEGMediaAudioSample.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FFFMPEGMediaAudioSample
Channels = InChannels;
Duration = InDuration;
SampleRate = InSampleRate;
Time = InTime;
Time.Time = InTime;

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FFFMPEGMediaBinarySample
Buffer.Append((uint8*)InBuffer, InSize);

Duration = InDuration;
Time = InTime;
Time.Time = InTime;

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class FFFMPEGMediaOverlaySample

Duration = (InDuration < FTimespan::Zero()) ? FTimespan::MaxValue() : InDuration;
Text = FText::FromString(StrippedText);
Time = InTime;
Time.Time = InTime;
Position = InPosition;

return true;
Expand Down
4 changes: 2 additions & 2 deletions Source/FFMPEGMedia/Private/Player/FFMPEGMediaPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ AVFormatContext* FFFMPEGMediaPlayer::ReadContext(const TSharedPtr<FArchive, ESPM
if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
errbuf_ptr = strerror(AVUNERROR(err));
#endif
PlayerTasks.Enqueue([=]()
PlayerTasks.Enqueue([this]()
{
EventSink.ReceiveMediaEvent(EMediaEvent::MediaOpenFailed);
});
Expand All @@ -421,7 +421,7 @@ AVFormatContext* FFFMPEGMediaPlayer::ReadContext(const TSharedPtr<FArchive, ESPM
{
UE_LOG(LogFFMPEGMedia, Error, TEXT("Option %s not found"), UTF8_TO_TCHAR(t->key));

PlayerTasks.Enqueue([=]()
PlayerTasks.Enqueue([this]()
{
EventSink.ReceiveMediaEvent(EMediaEvent::MediaOpenFailed);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class FFFMPEGMediaTextureSample
Dim = InDim;
SampleFormat = EMediaTextureSampleFormat::CharBGRA;
Stride = InStride;
Time = InTime;
Time.Time = InTime;

return true;
}
Expand Down
42 changes: 29 additions & 13 deletions Source/FFMPEGMedia/Private/Player/FFMPEGMediaTracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ bool FFFMPEGMediaTracks::AddStreamToTracks(uint32 StreamIndex, bool IsVideoDevic

if (MediaType != AVMEDIA_TYPE_VIDEO && MediaType != AVMEDIA_TYPE_AUDIO && MediaType != AVMEDIA_TYPE_SUBTITLE)
{
UE_LOG(LogFFMPEGMedia, Verbose, TEXT("Tracks %p: Unsupported major type %s of stream %i"), this,
UE_LOG(LogFFMPEGMedia, Verbose, TEXT("Tracks %p: Unsupported major type %hs of stream %i"), this,
av_get_media_type_string(MediaType), StreamIndex);
OutInfo += TEXT("\tUnsupported stream type\n");

Expand Down Expand Up @@ -2643,7 +2643,7 @@ int FFFMPEGMediaTracks::AudioDecodeFrame(FTimespan& time, FTimespan& duration)
}


void FFFMPEGMediaTracks::RenderAudio()
double FFFMPEGMediaTracks::RenderAudio()
{
int audio_size, len1;

Expand Down Expand Up @@ -2690,6 +2690,8 @@ void FFFMPEGMediaTracks::RenderAudio()
audioClockSerial, audioCallbackTime / 1000000.0);
extclk.SyncToSlave(&audclk);
}

return duration;
}

void FFFMPEGMediaTracks::StartDisplayThread()
Expand Down Expand Up @@ -2754,20 +2756,37 @@ int FFFMPEGMediaTracks::AudioRenderThread()
{
double remaining_time = 0.0;


int64_t startTime = av_gettime_relative();
int64_t nextWakeTime = av_gettime_relative();
while (audioRunning)
{
if (bPrerolled)
{
RenderAudio();
int64_t endTime = av_gettime_relative();
int64_t dif = endTime - startTime;
if (dif < 33333)
double duration = RenderAudio();
if (duration > 0.0)
{
int64_t durationUS = (int64_t)(duration * 1000000.0);
nextWakeTime += durationUS;
int64_t now = av_gettime_relative();
if (nextWakeTime > now)
{
av_usleep(nextWakeTime - now);
}
else
{
if (now - nextWakeTime > 1000000)
nextWakeTime = now;
}
}
else
{
av_usleep(33333 - dif);
av_usleep(5000);
nextWakeTime = av_gettime_relative();
}
startTime = endTime;
}
else
{
av_usleep(10000);
nextWakeTime = av_gettime_relative();
}
}
return 0;
Expand Down Expand Up @@ -3047,9 +3066,6 @@ int FFFMPEGMediaTracks::VideoThread()
return 0;
}
}

av_frame_free(&frame);
return 0;
}


Expand Down
11 changes: 11 additions & 0 deletions Source/FFMPEGMedia/Private/Player/FFMPEGMediaTracks.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
#pragma once


#include <functional>


#include "FFMPEGMediaSettings.h"
#include "FFMPEGMediaPrivate.h"
#include "FFMPEGFrameQueue.h"
#include "FFMPEGClock.h"
#include "IMediaEventSink.h"

extern "C" {
#include "libavutil/avutil.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}



#include "CoreTypes.h"
Expand Down