bool MediaStreamer::GetNextBuffer(uint8* buffer, uint32 maxBufferSize, uint32* bufferLength) { Microsoft::WRL::ComPtr<IMFSample> sample; Microsoft::WRL::ComPtr<IMFMediaBuffer> mediaBuffer; BYTE *audioData = nullptr; DWORD sampleBufferLength = 0; DWORD flags = 0; *bufferLength = 0; if (m_reader == nullptr) { return false; } DX::ThrowIfFailed( m_reader->ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, nullptr, &flags, nullptr, &sample) ); if (sample == nullptr) { if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { return true; } else { return false; } } DX::ThrowIfFailed( sample->ConvertToContiguousBuffer(&mediaBuffer) ); DX::ThrowIfFailed( mediaBuffer->Lock(&audioData, nullptr, &sampleBufferLength) ); // Only copy the sample if the remaining buffer is large enough. if (sampleBufferLength <= maxBufferSize) { CopyMemory(buffer, audioData, sampleBufferLength); *bufferLength = sampleBufferLength; } if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { return true; } else { return false; } }
bool MediaStreamer::GetNextBuffer(uint8* buffer, uint32 maxBufferSize, uint32* bufferLength) { Microsoft::WRL::ComPtr<IMFSample> sample; Microsoft::WRL::ComPtr<IMFMediaBuffer> mediaBuffer; BYTE *audioData = nullptr; DWORD sampleBufferLength = 0; DWORD flags = 0; *bufferLength = 0; if (m_reader == nullptr) { return false; } ThrowIfFailed( m_reader->ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, nullptr, &flags, nullptr, &sample) ); if (sample == nullptr) { if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { return true; } else { return false; } } ThrowIfFailed( sample->ConvertToContiguousBuffer(&mediaBuffer) ); ThrowIfFailed( mediaBuffer->Lock(&audioData, nullptr, &sampleBufferLength) ); // If buffer isn't large enough, dump sample if (sampleBufferLength <= maxBufferSize) { CopyMemory(buffer, audioData, sampleBufferLength); *bufferLength = sampleBufferLength; } else { #if defined(COCOS2D_DEBUG) OutputDebugString(L"Sample buffer dumped"); #endif } if (flags & MF_SOURCE_READERF_ENDOFSTREAM) { return true; } else { return false; } }