Skip to content

Commit

Permalink
Add a mutex.
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Oct 18, 2024
1 parent 712f998 commit 70af97d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
14 changes: 7 additions & 7 deletions modules/wmf/media_grabber_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include <cstdio>
#include <new>

MediaGrabberCallback::MediaGrabberCallback(VideoStreamPlaybackWMF *p_playback) :
m_cRef(1), playback(p_playback) {
MediaGrabberCallback::MediaGrabberCallback(VideoStreamPlaybackWMF *p_playback, Mutex& p_mtx) :
m_cRef(1), playback(p_playback), mtx(p_mtx) {
}

HRESULT MediaGrabberCallback::CreateInstance(MediaGrabberCallback **ppCB, VideoStreamPlaybackWMF *p_playback) {
*ppCB = new (std::nothrow) MediaGrabberCallback(p_playback);
HRESULT MediaGrabberCallback::CreateInstance(MediaGrabberCallback **ppCB, VideoStreamPlaybackWMF *p_playback, Mutex& p_mtx) {
*ppCB = new (std::nothrow) MediaGrabberCallback(p_playback, p_mtx);

if (ppCB == nullptr) {
return E_OUTOFMEMORY;
Expand Down Expand Up @@ -153,7 +153,7 @@ STDMETHODIMP MediaGrabberCallback::OnProcessSample(REFGUID guidMajorMediaType,
DWORD outDataLen;
pOutputBuffer->Lock(&outData, NULL, &outDataLen);

//mtx.lock();
mtx.lock();
{
FrameData *frame = playback->get_next_writable_frame();
frame->sample_time = llSampleTime / 10000;
Expand All @@ -180,9 +180,9 @@ STDMETHODIMP MediaGrabberCallback::OnProcessSample(REFGUID guidMajorMediaType,
rgb_buffer[i + 10] = outData[i + 10];
rgb_buffer[i + 11] = outData[i + 9];
}
memcpy(rgb_buffer, outData, outDataLen);
// memcpy(rgb_buffer, outData, outDataLen);
}
//mtx.unlock();
mtx.unlock();

pOutputBuffer->Unlock();

Expand Down
5 changes: 3 additions & 2 deletions modules/wmf/media_grabber_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ class VideoStreamPlaybackWMF;
class MediaGrabberCallback : public IMFSampleGrabberSinkCallback {
long m_cRef = 0;
VideoStreamPlaybackWMF *playback;
Mutex& mtx;
int width = 0;
int height = 0;

IMFTransform *m_pColorTransform = nullptr;
IMFSample *m_pSample = nullptr;
IMFSample *m_pOutSample = nullptr;

MediaGrabberCallback(VideoStreamPlaybackWMF *playback);
MediaGrabberCallback(VideoStreamPlaybackWMF *playback, Mutex& p_mtx);

public:
virtual ~MediaGrabberCallback() {}
static HRESULT CreateInstance(MediaGrabberCallback **ppCB, VideoStreamPlaybackWMF *playback);
static HRESULT CreateInstance(MediaGrabberCallback **ppCB, VideoStreamPlaybackWMF *playback, Mutex& p_mtx);

// IUnknown methods
STDMETHODIMP QueryInterface(REFIID iid, void **ppv);
Expand Down
31 changes: 21 additions & 10 deletions modules/wmf/video_stream_wmf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ HRESULT AddOutputNode(IMFTopology *pTopology,
return hr;
}


HRESULT AddColourConversionNode(IMFTopology *pTopology,
IMFMediaType *inputType,
IMFTransform **ppColorTransform) {
HRESULT hr = S_OK;

IMFTransform *colorTransform;
CoCreateInstance(CLSID_CColorConvertDMO, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&colorTransform));

IMFMediaType *pInType = nullptr;

UINT32 uWidth, uHeight;
MFGetAttributeSize(inputType, MF_MT_FRAME_SIZE, &uWidth, &uHeight);
UINT32 interlaceMode;
Expand All @@ -79,11 +83,19 @@ HRESULT AddColourConversionNode(IMFTopology *pTopology,
CHECK_HR(pInType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
CHECK_HR(pInType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12));
CHECK_HR(MFSetAttributeSize(pInType, MF_MT_FRAME_SIZE, uWidth, uHeight));

HRESULT hr_in = (colorTransform->SetInputType(0, pInType, 0));
CHECK_HR(hr_in);
IMFMediaType *pOutType = nullptr;
CHECK_HR(MFCreateMediaType(&pOutType));

hr = pOutType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
hr = pOutType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB24);
CHECK_HR(MFSetAttributeSize(pOutType, MF_MT_FRAME_SIZE, uWidth, uHeight));

HRESULT hr_out = (colorTransform->SetOutputType(0, pOutType, 0));
CHECK_HR(hr_out);

*ppColorTransform = colorTransform;
return hr;
}
Expand Down Expand Up @@ -363,7 +375,7 @@ void VideoStreamPlaybackWMF::set_file(const String &p_file) {
CHECK_HR(CreateMediaSource(p_file, &media_source));
CHECK_HR(MFCreateMediaSession(nullptr, &media_session));

CHECK_HR(MediaGrabberCallback::CreateInstance(&sample_grabber_callback, this));
CHECK_HR(MediaGrabberCallback::CreateInstance(&sample_grabber_callback, this, mtx));
CHECK_HR(CreateTopology(media_source, sample_grabber_callback, &topology, &stream_info));

CHECK_HR(media_session->SetTopology(0, topology));
Expand Down Expand Up @@ -392,12 +404,9 @@ void VideoStreamPlaybackWMF::set_file(const String &p_file) {
const int rgb24_frame_size = stream_info.size.x * stream_info.size.y * 3;
cache_frames.resize(24);
for (int i = 0; i < cache_frames.size(); ++i) {
cache_frames.write[i].data.resize(rgb24_frame_size);
cache_frames[i].data.resize(rgb24_frame_size);
}
read_frame_idx = write_frame_idx = 0;

Ref<Image> img = memnew(Image(stream_info.size.x, stream_info.size.y, 0, Image::FORMAT_RGBA8, frame_data)); //zero copy image creation
texture->create_from_image(img);
} else {
SafeRelease(media_session);
}
Expand Down Expand Up @@ -456,7 +465,7 @@ void VideoStreamPlaybackWMF::set_audio_track(int p_idx) {
}

FrameData *VideoStreamPlaybackWMF::get_next_writable_frame() {
return &cache_frames.write[write_frame_idx];
return &cache_frames[write_frame_idx];
}

void VideoStreamPlaybackWMF::write_frame_done() {
Expand Down Expand Up @@ -484,14 +493,18 @@ void VideoStreamPlaybackWMF::write_frame_done() {
}

void VideoStreamPlaybackWMF::present() {
if (texture.is_null()) {
Ref<Image> img = memnew(Image(stream_info.size.x, stream_info.size.y, 0, Image::FORMAT_RGB8));
texture = ImageTexture::create_from_image(img);
}
if (read_frame_idx == write_frame_idx) {
return;
}
mtx.lock();
FrameData &the_frame = cache_frames.write[read_frame_idx];
FrameData &the_frame = cache_frames[read_frame_idx];
read_frame_idx = (read_frame_idx + 1) % cache_frames.size();
mtx.unlock();
Ref<Image> img = memnew(Image(stream_info.size.x, stream_info.size.y, 0, Image::FORMAT_RGB8, the_frame.data));
Ref<Image> img = memnew(Image(stream_info.size.x, stream_info.size.y, 0, Image::FORMAT_RGB8, the_frame.data)); //zero copy image creation
texture->update(img);
}

Expand All @@ -512,8 +525,6 @@ VideoStreamPlaybackWMF::VideoStreamPlaybackWMF() :
is_video_paused(false), is_video_seekable(false) {
id = counter;
counter++;

texture = Ref<ImageTexture>(memnew(ImageTexture));
cache_frames.resize(24);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/wmf/video_stream_wmf.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class VideoStreamPlaybackWMF : public VideoStreamPlayback {
IMFPresentationClock *presentation_clock;
MediaGrabberCallback *sample_grabber_callback;

Vector<FrameData> cache_frames;
LocalVector<FrameData> cache_frames;
int read_frame_idx = 0;
int write_frame_idx = 0;

Expand Down

0 comments on commit 70af97d

Please sign in to comment.