From 70af97d0bc0331e49118a2711e430816a7c4d169 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Fri, 18 Oct 2024 08:21:01 -0700 Subject: [PATCH] Add a mutex. --- modules/wmf/media_grabber_callback.cpp | 14 ++++++------ modules/wmf/media_grabber_callback.h | 5 +++-- modules/wmf/video_stream_wmf.cpp | 31 +++++++++++++++++--------- modules/wmf/video_stream_wmf.h | 2 +- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/modules/wmf/media_grabber_callback.cpp b/modules/wmf/media_grabber_callback.cpp index 5b9f38d8b2cb..4ad03ca29421 100644 --- a/modules/wmf/media_grabber_callback.cpp +++ b/modules/wmf/media_grabber_callback.cpp @@ -8,12 +8,12 @@ #include #include -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; @@ -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; @@ -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(); diff --git a/modules/wmf/media_grabber_callback.h b/modules/wmf/media_grabber_callback.h index 1cf8b64ad4fb..44dc10ce7567 100644 --- a/modules/wmf/media_grabber_callback.h +++ b/modules/wmf/media_grabber_callback.h @@ -19,6 +19,7 @@ class VideoStreamPlaybackWMF; class MediaGrabberCallback : public IMFSampleGrabberSinkCallback { long m_cRef = 0; VideoStreamPlaybackWMF *playback; + Mutex& mtx; int width = 0; int height = 0; @@ -26,11 +27,11 @@ class MediaGrabberCallback : public IMFSampleGrabberSinkCallback { 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); diff --git a/modules/wmf/video_stream_wmf.cpp b/modules/wmf/video_stream_wmf.cpp index e1cddc41e37e..55c043c60d9c 100644 --- a/modules/wmf/video_stream_wmf.cpp +++ b/modules/wmf/video_stream_wmf.cpp @@ -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; @@ -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; } @@ -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)); @@ -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 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); } @@ -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() { @@ -484,14 +493,18 @@ void VideoStreamPlaybackWMF::write_frame_done() { } void VideoStreamPlaybackWMF::present() { + if (texture.is_null()) { + Ref 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 img = memnew(Image(stream_info.size.x, stream_info.size.y, 0, Image::FORMAT_RGB8, the_frame.data)); + Ref 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); } @@ -512,8 +525,6 @@ VideoStreamPlaybackWMF::VideoStreamPlaybackWMF() : is_video_paused(false), is_video_seekable(false) { id = counter; counter++; - - texture = Ref(memnew(ImageTexture)); cache_frames.resize(24); } diff --git a/modules/wmf/video_stream_wmf.h b/modules/wmf/video_stream_wmf.h index 5378c56f08c1..60d9ee689a3b 100644 --- a/modules/wmf/video_stream_wmf.h +++ b/modules/wmf/video_stream_wmf.h @@ -28,7 +28,7 @@ class VideoStreamPlaybackWMF : public VideoStreamPlayback { IMFPresentationClock *presentation_clock; MediaGrabberCallback *sample_grabber_callback; - Vector cache_frames; + LocalVector cache_frames; int read_frame_idx = 0; int write_frame_idx = 0;