2014-10-15 05:13:14 +08:00
|
|
|
/*
|
|
|
|
* cocos2d-x http://www.cocos2d-x.org
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010-2011 - cocos2d-x community
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/winrt/MediaStreamer.h"
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
#include <Mfidl.h>
|
|
|
|
#include <Mfreadwrite.h>
|
|
|
|
#include <Mfapi.h>
|
|
|
|
|
|
|
|
#include <wrl\wrappers\corewrappers.h>
|
|
|
|
#include <ppltasks.h>
|
|
|
|
|
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
using namespace Windows::Storage;
|
|
|
|
using namespace Windows::Storage::FileProperties;
|
|
|
|
using namespace Windows::Storage::Streams;
|
|
|
|
using namespace Windows::Foundation;
|
2014-03-29 03:53:35 +08:00
|
|
|
using namespace Windows::ApplicationModel;
|
2014-10-15 05:13:14 +08:00
|
|
|
using namespace Concurrency;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
#ifndef MAKEFOURCC
|
|
|
|
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
|
|
|
|
((uint32)(byte)(ch0) | ((uint32)(byte)(ch1) << 8) | \
|
|
|
|
((uint32)(byte)(ch2) << 16) | ((uint32)(byte)(ch3) << 24 ))
|
|
|
|
#endif /* defined(MAKEFOURCC) */
|
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
const int FMT_CHUNK_MAX = 256;
|
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
inline void ThrowIfFailed(HRESULT hr)
|
2014-03-29 03:53:35 +08:00
|
|
|
{
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-10-15 05:13:14 +08:00
|
|
|
// Set a breakpoint on this line to catch DX API errors.
|
2014-03-29 03:53:35 +08:00
|
|
|
throw Platform::Exception::CreateException(hr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
MediaStreamer::MediaStreamer() :
|
|
|
|
m_offset(0)
|
2015-05-16 09:17:52 +08:00
|
|
|
, m_dataLen(0)
|
|
|
|
, m_filename(nullptr)
|
2014-03-29 03:53:35 +08:00
|
|
|
{
|
|
|
|
ZeroMemory(&m_waveFormat, sizeof(m_waveFormat));
|
2014-10-15 05:13:14 +08:00
|
|
|
m_location = Package::Current->InstalledLocation;
|
|
|
|
m_locationPath = Platform::String::Concat(m_location->Path, "\\Assets\\Resources\\");
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamer::~MediaStreamer()
|
|
|
|
{
|
|
|
|
}
|
2015-05-16 09:17:52 +08:00
|
|
|
|
|
|
|
Platform::Array<byte>^ MediaStreamer::ReadData(_In_ Platform::String^ filename)
|
|
|
|
{
|
|
|
|
return ReadData(filename, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Platform::Array<byte>^ MediaStreamer::ReadData(_In_ Platform::String^ filename, uint32 from, uint32 length)
|
2014-10-15 05:13:14 +08:00
|
|
|
{
|
|
|
|
CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
|
|
|
|
extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
|
|
|
|
extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
|
2015-05-24 10:13:12 +08:00
|
|
|
extendedParams.dwFileFlags = from ? FILE_FLAG_RANDOM_ACCESS : FILE_FLAG_SEQUENTIAL_SCAN;
|
2014-10-15 05:13:14 +08:00
|
|
|
extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
|
|
|
|
extendedParams.lpSecurityAttributes = nullptr;
|
|
|
|
extendedParams.hTemplateFile = nullptr;
|
|
|
|
|
|
|
|
Wrappers::FileHandle file(
|
2015-05-16 09:17:52 +08:00
|
|
|
CreateFile2(filename->Data(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams)
|
2014-10-15 05:13:14 +08:00
|
|
|
);
|
2015-05-16 09:17:52 +08:00
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
if (file.Get()==INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
throw ref new Platform::FailureException();
|
|
|
|
}
|
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
FILE_STANDARD_INFO fileInfo = { 0 };
|
|
|
|
if (!GetFileInformationByHandleEx(file.Get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
|
2014-10-15 05:13:14 +08:00
|
|
|
{
|
|
|
|
throw ref new Platform::FailureException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileInfo.EndOfFile.HighPart != 0)
|
|
|
|
{
|
|
|
|
throw ref new Platform::OutOfMemoryException();
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:50:04 +08:00
|
|
|
from += static_cast<unsigned int>(m_offset);
|
2015-05-16 09:17:52 +08:00
|
|
|
length = (length == 0 || from + length > fileInfo.EndOfFile.LowPart) ? fileInfo.EndOfFile.LowPart - from : length;
|
|
|
|
Platform::Array<byte>^ fileData = ref new Platform::Array<byte>(length);
|
2014-10-15 05:13:14 +08:00
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
if (from)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER pos = { 0 };
|
|
|
|
pos.QuadPart = from;
|
|
|
|
if (!SetFilePointerEx(file.Get(), pos, nullptr, FILE_BEGIN))
|
|
|
|
{
|
|
|
|
throw ref new Platform::FailureException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadFile(file.Get(), fileData->Data, fileData->Length, nullptr, nullptr))
|
2014-10-15 05:13:14 +08:00
|
|
|
{
|
|
|
|
throw ref new Platform::FailureException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileData;
|
|
|
|
}
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
void MediaStreamer::Initialize(__in const WCHAR* url, bool lazy)
|
2014-03-29 03:53:35 +08:00
|
|
|
{
|
2015-05-16 09:17:52 +08:00
|
|
|
m_filename = ref new Platform::String(url);
|
2014-10-15 05:13:14 +08:00
|
|
|
WCHAR filePath[MAX_PATH] = {0};
|
2014-03-29 03:53:35 +08:00
|
|
|
if ((wcslen(url) > 1 && url[1] == ':'))
|
|
|
|
{
|
|
|
|
// path start with "x:", is absolute path
|
|
|
|
wcscat_s(filePath, url);
|
|
|
|
}
|
|
|
|
else if (wcslen(url) > 0
|
|
|
|
&& (L'/' == url[0] || L'\\' == url[0]))
|
|
|
|
{
|
|
|
|
// path start with '/' or '\', is absolute path without driver name
|
2014-10-15 05:13:14 +08:00
|
|
|
wcscat_s(filePath, m_locationPath->Data());
|
2014-03-29 03:53:35 +08:00
|
|
|
// remove '/' or '\\'
|
|
|
|
wcscat_s(filePath, (const WCHAR*)url[1]);
|
|
|
|
}else
|
|
|
|
{
|
2014-10-15 05:13:14 +08:00
|
|
|
wcscat_s(filePath, m_locationPath->Data());
|
2014-03-29 03:53:35 +08:00
|
|
|
wcscat_s(filePath, url);
|
|
|
|
}
|
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
Platform::Array<byte>^ data = lazy ? ReadData(ref new Platform::String(filePath), 0, FMT_CHUNK_MAX) : ReadData(ref new Platform::String(filePath));
|
2014-10-15 05:13:14 +08:00
|
|
|
UINT32 length = data->Length;
|
|
|
|
const byte * dataPtr = data->Data;
|
|
|
|
UINT32 offset = 0;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
DWORD riffDataSize = 0;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
auto ReadChunk = [&length, &offset, &dataPtr, &riffDataSize](DWORD fourcc, DWORD& outChunkSize, DWORD& outChunkPos) -> HRESULT
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (offset + sizeof(DWORD) * 2 >= length)
|
|
|
|
{
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read two DWORDs.
|
|
|
|
DWORD chunkType = *reinterpret_cast<const DWORD *>(&dataPtr[offset]);
|
|
|
|
DWORD chunkSize = *reinterpret_cast<const DWORD *>(&dataPtr[offset + sizeof(DWORD)]);
|
|
|
|
offset += sizeof(DWORD) * 2;
|
|
|
|
|
|
|
|
if (chunkType == MAKEFOURCC('R', 'I', 'F', 'F'))
|
|
|
|
{
|
|
|
|
riffDataSize = chunkSize;
|
|
|
|
chunkSize = sizeof(DWORD);
|
|
|
|
outChunkSize = sizeof(DWORD);
|
|
|
|
outChunkPos = offset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outChunkSize = chunkSize;
|
|
|
|
outChunkPos = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += chunkSize;
|
|
|
|
|
|
|
|
if (chunkType == fourcc)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Locate riff chunk, check the file type.
|
|
|
|
DWORD chunkSize = 0;
|
|
|
|
DWORD chunkPos = 0;
|
|
|
|
|
|
|
|
ThrowIfFailed(ReadChunk(MAKEFOURCC('R', 'I', 'F', 'F'), chunkSize, chunkPos));
|
|
|
|
if (*reinterpret_cast<const DWORD *>(&dataPtr[chunkPos]) != MAKEFOURCC('W', 'A', 'V', 'E')) ThrowIfFailed(E_FAIL);
|
|
|
|
|
|
|
|
// Locate 'fmt ' chunk, copy to WAVEFORMATEXTENSIBLE.
|
|
|
|
ThrowIfFailed(ReadChunk(MAKEFOURCC('f', 'm', 't', ' '), chunkSize, chunkPos));
|
|
|
|
ThrowIfFailed((chunkSize <= sizeof(m_waveFormat)) ? S_OK : E_FAIL);
|
|
|
|
CopyMemory(&m_waveFormat, &dataPtr[chunkPos], chunkSize);
|
|
|
|
|
|
|
|
// Locate the 'data' chunk and copy its contents to a buffer.
|
|
|
|
ThrowIfFailed(ReadChunk(MAKEFOURCC('d', 'a', 't', 'a'), chunkSize, chunkPos));
|
2015-05-16 09:17:52 +08:00
|
|
|
m_dataLen = chunkSize;
|
|
|
|
m_offset = chunkPos;
|
2014-10-15 05:13:14 +08:00
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
if (!lazy)
|
|
|
|
{
|
|
|
|
m_data.resize(chunkSize);
|
|
|
|
CopyMemory(m_data.data(), &dataPtr[chunkPos], chunkSize);
|
|
|
|
m_offset = 0;
|
|
|
|
}
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaStreamer::ReadAll(uint8* buffer, uint32 maxBufferSize, uint32* bufferLength)
|
|
|
|
{
|
2015-05-16 09:17:52 +08:00
|
|
|
if (!m_data.size())
|
|
|
|
{
|
2016-06-15 23:50:04 +08:00
|
|
|
ReadChunk(buffer, 0, static_cast<unsigned int>(m_dataLen), bufferLength);
|
2015-05-16 09:17:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-15 23:50:04 +08:00
|
|
|
UINT32 toCopy = static_cast<UINT32>(m_data.size() - m_offset);
|
2015-05-16 09:17:52 +08:00
|
|
|
if (toCopy > maxBufferSize) toCopy = maxBufferSize;
|
|
|
|
|
|
|
|
CopyMemory(buffer, m_data.data(), toCopy);
|
|
|
|
*bufferLength = toCopy;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
m_offset += toCopy;
|
|
|
|
if (m_offset > m_data.size()) m_offset = m_data.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaStreamer::ReadChunk(uint8* buffer, uint32 from, uint32 length, uint32* bytesRead)
|
|
|
|
{
|
|
|
|
Platform::Array<byte>^ data = ReadData(m_filename, from, length);
|
|
|
|
*bytesRead = data->Length;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2015-05-16 09:17:52 +08:00
|
|
|
if (*bytesRead > 0)
|
|
|
|
{
|
|
|
|
CopyMemory(buffer, (byte*)data->Data, data->Length);
|
|
|
|
}
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaStreamer::Restart()
|
|
|
|
{
|
2014-10-15 05:13:14 +08:00
|
|
|
m_offset = 0;
|
2014-06-19 19:45:24 +08:00
|
|
|
}
|