| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2019 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include "mfbytestream.h" |
| 14 | |
| 15 | using namespace smooth::IO; |
| 16 | |
| 17 | BoCA::MFByteStream::MFByteStream(Driver *driver) |
| 18 | { |
| 19 | refCount = 1; |
| 20 | |
| 21 | this->driver = driver; |
| 22 | |
| 23 | bytesRead = 0; |
| 24 | bytesWritten = 0; |
| 25 | } |
| 26 | |
| 27 | BoCA::MFByteStream::~MFByteStream() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | ULONG STDMETHODCALLTYPE BoCA::MFByteStream::AddRef() |
| 32 | { |
| 33 | return ++refCount; |
| 34 | } |
| 35 | |
| 36 | ULONG STDMETHODCALLTYPE BoCA::MFByteStream::Release() |
| 37 | { |
| 38 | if (--refCount == 0) |
| 39 | { |
| 40 | delete this; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | return refCount; |
| 46 | } |
| 47 | |
| 48 | STDMETHODIMP BoCA::MFByteStream::QueryInterface(REFIID riid, void **ppvObject) |
| 49 | { |
| 50 | if (riid == IID_IUnknown) *ppvObject = (IUnknown *) this; |
| 51 | else if (riid == IID_IMFByteStream) *ppvObject = (IMFByteStream *) this; |
| 52 | else return E_NOINTERFACE; |
| 53 | |
| 54 | AddRef(); |
| 55 | |
| 56 | return S_OK; |
| 57 | } |
| 58 | |
| 59 | STDMETHODIMP BoCA::MFByteStream::GetCapabilities(DWORD *pdwCapabilities) |
| 60 | { |
| 61 | *pdwCapabilities = MFBYTESTREAM_IS_READABLE | MFBYTESTREAM_IS_WRITABLE | MFBYTESTREAM_IS_SEEKABLE; |
| 62 | |
| 63 | return S_OK; |
| 64 | } |
| 65 | |
| 66 | STDMETHODIMP BoCA::MFByteStream::Read(BYTE *pb, ULONG cb, ULONG *pcbRead) |
| 67 | { |
| 68 | *pcbRead = driver->ReadData(pb, cb); |
| 69 | |
| 70 | return S_OK; |
| 71 | } |
| 72 | |
| 73 | STDMETHODIMP BoCA::MFByteStream::Write(const BYTE *pb, ULONG cb, ULONG *pcbWritten) |
| 74 | { |
| 75 | *pcbWritten = driver->WriteData(pb, cb); |
| 76 | |
| 77 | return S_OK; |
| 78 | } |
| 79 | |
| 80 | STDMETHODIMP BoCA::MFByteStream::Seek(MFBYTESTREAM_SEEK_ORIGIN SeekOrigin, LONGLONG qwSeekOffset, DWORD dwSeekFlags, QWORD *pqwCurrentPosition) |
| 81 | { |
| 82 | if (SeekOrigin == msoBegin) *pqwCurrentPosition = driver->Seek(qwSeekOffset); |
| 83 | else *pqwCurrentPosition = driver->Seek(driver->GetPos() + qwSeekOffset); |
| 84 | |
| 85 | return S_OK; |
| 86 | } |
| 87 | |
| 88 | STDMETHODIMP BoCA::MFByteStream::GetCurrentPosition(QWORD *pqwPosition) |
| 89 | { |
| 90 | *pqwPosition = driver->GetPos(); |
| 91 | |
| 92 | return S_OK; |
| 93 | } |
| 94 | |
| 95 | STDMETHODIMP BoCA::MFByteStream::SetCurrentPosition(QWORD qwPosition) |
| 96 | { |
| 97 | driver->Seek(qwPosition); |
| 98 | |
| 99 | return S_OK; |
| 100 | } |
| 101 | |
| 102 | STDMETHODIMP BoCA::MFByteStream::GetLength(QWORD *pqwLength) |
| 103 | { |
| 104 | *pqwLength = driver->GetSize(); |
| 105 | |
| 106 | return S_OK; |
| 107 | } |
| 108 | |
| 109 | STDMETHODIMP BoCA::MFByteStream::SetLength(QWORD qwLength) |
| 110 | { |
| 111 | if (!driver->Truncate(qwLength)) return E_FAIL; |
| 112 | |
| 113 | return S_OK; |
| 114 | } |
| 115 | |
| 116 | STDMETHODIMP BoCA::MFByteStream::IsEndOfStream(BOOL *pfEndOfStream) |
| 117 | { |
| 118 | Int64 position = driver->GetPos(); |
| 119 | Int64 size = driver->GetSize(); |
| 120 | |
| 121 | *pfEndOfStream = (position >= size); |
| 122 | |
| 123 | return S_OK; |
| 124 | } |
| 125 | |
| 126 | STDMETHODIMP BoCA::MFByteStream::Flush() |
| 127 | { |
| 128 | if (!driver->Flush()) return E_FAIL; |
| 129 | |
| 130 | return S_OK; |
| 131 | } |
| 132 | |
| 133 | STDMETHODIMP BoCA::MFByteStream::Close() |
| 134 | { |
| 135 | if (!driver->Close()) return E_FAIL; |
| 136 | |
| 137 | return S_OK; |
| 138 | } |
| 139 | |
| 140 | STDMETHODIMP BoCA::MFByteStream::BeginRead(BYTE *pb, ULONG cb, IMFAsyncCallback *pCallback, IUnknown *punkState) |
| 141 | { |
| 142 | bytesRead = driver->ReadData(pb, cb); |
| 143 | |
| 144 | IMFAsyncResult *asyncResult = NULL; |
| 145 | |
| 146 | ex_MFCreateAsyncResult(NULL, pCallback, punkState, &asyncResult); |
| 147 | ex_MFInvokeCallback(asyncResult); |
| 148 | |
| 149 | asyncResult->Release(); |
| 150 | |
| 151 | return S_OK; |
| 152 | } |
| 153 | |
| 154 | STDMETHODIMP BoCA::MFByteStream::EndRead(IMFAsyncResult *pResult, ULONG *pcbRead) |
| 155 | { |
| 156 | *pcbRead = bytesRead; |
| 157 | |
| 158 | return S_OK; |
| 159 | } |
| 160 | |
| 161 | STDMETHODIMP BoCA::MFByteStream::BeginWrite(const BYTE *pb, ULONG cb, IMFAsyncCallback *pCallback, IUnknown *punkState) |
| 162 | { |
| 163 | bytesWritten = driver->WriteData(pb, cb); |
| 164 | |
| 165 | IMFAsyncResult *asyncResult = NULL; |
| 166 | |
| 167 | ex_MFCreateAsyncResult(NULL, pCallback, punkState, &asyncResult); |
| 168 | ex_MFInvokeCallback(asyncResult); |
| 169 | |
| 170 | asyncResult->Release(); |
| 171 | |
| 172 | return S_OK; |
| 173 | } |
| 174 | |
| 175 | STDMETHODIMP BoCA::MFByteStream::EndWrite(IMFAsyncResult *pResult, ULONG *pcbWritten) |
| 176 | { |
| 177 | *pcbWritten = bytesWritten; |
| 178 | |
| 179 | return S_OK; |
| 180 | } |