Skip to content

Commit

Permalink
Fetch one IStream reference and keep it until the file is written
Browse files Browse the repository at this point in the history
* before, we repeatedly fetched an IStream reference into an IStreamPtr and soon after released it again
* this led the attached FileWriter to constantly open and close the associated file, hurting performance
* affects roman380#19
* fixes roman380#21
  • Loading branch information
micha137 committed Feb 8, 2019
1 parent 0c794c0 commit 557057f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
41 changes: 30 additions & 11 deletions mp4mux/MuxFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,11 @@ MuxOutput::DecideBufferSize(IMemAllocator * pAlloc, ALLOCATOR_PROPERTIES * pprop
HRESULT
MuxOutput::CompleteConnect(IPin *pReceivePin)
{
// make sure that this is the file writer, supporting
// make sure that pReceivePin is the file writer, supporting
// IStream, or we will not be able to write out the metadata
// at stop time
IStreamPtr pIStream = pReceivePin;
if (pIStream == NULL)
pReceivePin->QueryInterface(__uuidof(IStream), (VOID**)&m_pIStream);
if (m_pIStream == NULL)
{
return E_NOINTERFACE;
}
Expand All @@ -891,6 +891,12 @@ MuxOutput::CompleteConnect(IPin *pReceivePin)
HRESULT
MuxOutput::BreakConnect()
{
if (m_pIStream)
{
m_pIStream->Release();
m_pIStream = NULL;
}

return __super::BreakConnect();
}

Expand All @@ -900,6 +906,11 @@ MuxOutput::Reset()
CAutoLock lock(&m_csWrite);
m_llBytes = 0;
m_bUseIStream = true; // always use IStream, so we don't fail when downstream filter is stopped first
if (m_pIStream)
{
m_pIStream->Release();
m_pIStream = NULL;
}
}

void
Expand Down Expand Up @@ -944,19 +955,24 @@ MuxOutput::Replace(LONGLONG pos, const BYTE* pBuffer, long cBytes)
HRESULT hr = S_OK;
if (m_bUseIStream)
{
IStreamPtr pStream = GetConnected();
if (pStream == NULL)
{

if (m_pIStream == NULL)
{
GetConnected()->QueryInterface(__uuidof(IStream), (VOID**)&m_pIStream);
}

if (m_pIStream == NULL)
{
hr = E_NOINTERFACE;
} else {
LARGE_INTEGER liTo;
liTo.QuadPart = pos;
ULARGE_INTEGER uliUnused;
hr = pStream->Seek(liTo, STREAM_SEEK_SET, &uliUnused);
hr = m_pIStream->Seek(liTo, STREAM_SEEK_SET, &uliUnused);
if (SUCCEEDED(hr))
{
ULONG cActual;
hr = pStream->Write(pBuffer, cBytes, &cActual);
hr = m_pIStream->Write(pBuffer, cBytes, &cActual);
if (SUCCEEDED(hr) && ((long)cActual != cBytes))
{
hr = E_FAIL;
Expand Down Expand Up @@ -998,13 +1014,16 @@ MuxOutput::Replace(LONGLONG pos, const BYTE* pBuffer, long cBytes)
void
MuxOutput::FillSpace()
{
IStreamPtr pStream = GetConnected();
if (pStream != NULL)
if (m_pIStream == NULL)
{
GetConnected()->QueryInterface(__uuidof(IStream), (VOID**)&m_pIStream);
}
if (m_pIStream != NULL)
{
LARGE_INTEGER li0;
li0.QuadPart = 0;
ULARGE_INTEGER uliEnd;
HRESULT hr = pStream->Seek(li0, STREAM_SEEK_END, &uliEnd);
HRESULT hr = m_pIStream->Seek(li0, STREAM_SEEK_END, &uliEnd);
if (SUCCEEDED(hr))
{
if (uliEnd.QuadPart > (ULONGLONG) m_llBytes)
Expand Down
1 change: 1 addition & 0 deletions mp4mux/MuxFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class MuxOutput

private:
Mpeg4Mux* m_pMux;
IStream* m_pIStream;
CCritSec m_csWrite;
bool m_bUseIStream;
LONGLONG m_llBytes;
Expand Down

0 comments on commit 557057f

Please sign in to comment.