/* -LICENSE-START-
** Copyright (c) 2023 Blackmagic Design
**
** Permission is hereby granted, free of charge, to any person or organization
** obtaining a copy of the software and accompanying documentation (the
** "Software") to use, reproduce, display, distribute, sub-license, execute,
** and transmit the Software, and to prepare derivative works of the Software,
** and to permit third-parties to whom the Software is furnished to do so, in
** accordance with:
**
** (1) if the Software is obtained from Blackmagic Design, the End User License
** Agreement for the Software Development Kit (“EULA”) available at
** https://www.blackmagicdesign.com/EULA/DeckLinkSDK; or
**
** (2) if the Software is obtained from any third party, such licensing terms
** as notified by that third party,
**
** and all subject to the following:
**
** (3) the copyright notices in the Software and this entire statement,
** including the above license grant, this restriction and the following
** disclaimer, must be included in all copies of the Software, in whole or in
** part, and all derivative works of the Software, unless such copies or
** derivative works are solely in the form of machine-executable object code
** generated by a source language processor.
**
** (4) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
** OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
**
** A copy of the Software is available free of charge at
** https://www.blackmagicdesign.com/desktopvideo_sdk under the EULA.
**
** -LICENSE-END-
*/

#include "stdafx.h"
#include "mfapi.h"
#include <stdexcept>
#include "DeckLinkMediaBuffer.h"

DeckLinkMediaBuffer::DeckLinkMediaBuffer(unsigned int bufferSize) :
	m_refCount(1),
	m_accessedBuffer(nullptr),
	m_accessedCount(0)
{
	if (MFCreateAlignedMemoryBuffer(bufferSize, MF_64_BYTE_ALIGNMENT, &m_mediaBuffer) != S_OK)
		throw std::runtime_error("Unable to allocate Media buffer");

	// Set the data length of the buffer.
	if (m_mediaBuffer->SetCurrentLength(bufferSize) != S_OK)
		throw std::runtime_error("Unable to set length of Media buffer");
}

HRESULT DeckLinkMediaBuffer::QueryInterface(REFIID riid, void** ppvObject)
{
	if (ppvObject == nullptr)
		return E_POINTER;

	if (riid == IID_IUnknown)
	{
		*ppvObject = this;
		AddRef();
	}
	else if (riid == IID_IDeckLinkVideoBuffer)
	{
		*ppvObject = static_cast<IDeckLinkVideoBuffer*>(this);
		AddRef();
	}
	else if (riid == IID_DeckLinkMediaBuffer)
	{
		*ppvObject = static_cast<DeckLinkMediaBuffer*>(this);
		AddRef();
	}
	else
	{
		*ppvObject = nullptr;
		return E_NOINTERFACE;
	}

	return S_OK;
}

ULONG DeckLinkMediaBuffer::AddRef(void)
{
	return ++m_refCount;
}

ULONG DeckLinkMediaBuffer::Release(void)
{
	ULONG newRefValue = --m_refCount;

	if (newRefValue == 0)
		delete this;

	return newRefValue;
}

HRESULT DeckLinkMediaBuffer::GetBytes(void** buffer)
{
	std::lock_guard<std::mutex> lock(m_mutex);

	if (!buffer)
		return E_POINTER;

	if (!m_accessedCount)
		return E_ACCESSDENIED;

	*buffer = (void*)m_accessedBuffer;
	return S_OK;
}

HRESULT DeckLinkMediaBuffer::StartAccess(BMDBufferAccessFlags)
{
	std::lock_guard<std::mutex> lock(m_mutex);

	// Lock buffer so pointer is accessible to DeckLinkAPI
	HRESULT hr = m_mediaBuffer->Lock(&m_accessedBuffer, nullptr, nullptr);
	if (hr == S_OK)
		m_accessedCount++;

	return hr;
}

HRESULT DeckLinkMediaBuffer::EndAccess(BMDBufferAccessFlags)
{
	std::lock_guard<std::mutex> lock(m_mutex);

	if (!m_accessedCount)
		return E_UNEXPECTED;

	HRESULT hr = m_mediaBuffer->Unlock();
	if (hr == S_OK)
		m_accessedCount--;

	return hr;
}

CComPtr<IMFMediaBuffer>	DeckLinkMediaBuffer::getBuffer()
{
	std::lock_guard<std::mutex> lock(m_mutex);
	return m_mediaBuffer;
}

