Debian Codegraph @

components/decoder/wma/wmareader.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2017 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 "wmareader.h"
14
15 using namespace smooth::Threads;
16
17 BoCA::WMAReader::WMAReader()
18 {
19 m_cRef = 1;
20
21 m_fEOF = false;
22 m_qwTime = 0;
23 m_dwAudioOutputNum = 0;
24
25 m_hAsyncEvent = CreateEvent(NIL, FALSE, FALSE, NIL);
26
27 m_pReaderAdvanced = NIL;
28
29 active = False;
30 error = False;
31
32 samplesBuffer = NIL;
33 samplesBufferMutex = NIL;
34 }
35
36 BoCA::WMAReader::~WMAReader()
37 {
38 CloseHandle(m_hAsyncEvent);
39 }
40
41 STDMETHODIMP BoCA::WMAReader::QueryInterface(REFIID riid, void __RPC_FAR * __RPC_FAR *ppvObject)
42 {
43 if (ppvObject == NIL) return E_INVALIDARG;
44
45 if (riid == IID_IUnknown) *ppvObject = (this);
46 else if (riid == IID_IWMStatusCallback) *ppvObject = static_cast<IWMStatusCallback *> (this);
47 else if (riid == IID_IWMReaderCallback) *ppvObject = static_cast<IWMReaderCallback *> (this);
48 else if (riid == IID_IWMReaderCallbackAdvanced) *ppvObject = static_cast<IWMReaderCallbackAdvanced *>(this);
49 else *ppvObject = NIL;
50
51 if (*ppvObject == NIL) return E_NOINTERFACE;
52
53 AddRef();
54
55 return S_OK;
56 }
57
58 STDMETHODIMP_(ULONG) BoCA::WMAReader::AddRef()
59 {
60 return InterlockedIncrement(&m_cRef);
61 }
62
63 STDMETHODIMP_(ULONG) BoCA::WMAReader::Release()
64 {
65 if (InterlockedDecrement(&m_cRef) == 0)
66 {
67 delete this;
68
69 return 0;
70 }
71
72 return m_cRef;
73 }
74
75 STDMETHODIMP BoCA::WMAReader::OnStatus(WMT_STATUS status, HRESULT hr, WMT_ATTR_DATATYPE dwType, BYTE __RPC_FAR *pValue, void __RPC_FAR *pvContext)
76 {
77 /* This switch checks for the important messages sent by the reader object.
78 */
79 switch (status)
80 {
81 /* The reader is finished opening a file.
82 */
83 case WMT_OPENED:
84 SetAsyncEvent(hr);
85
86 break;
87
88 /* The reader is finished closing a file.
89 */
90 case WMT_CLOSED:
91 SetAsyncEvent(hr);
92
93 break;
94
95 /* Playback of the opened file has begun.
96 */
97 case WMT_STARTED:
98 if (pvContext == NIL) m_qwTime = 10000000;
99 else m_qwTime = *(QWORD *) pvContext + 10000000;
100
101 m_pReaderAdvanced->DeliverTime(m_qwTime);
102
103 active = True;
104
105 SetAsyncEvent(hr);
106
107 break;
108
109 /* The previously playing reader has stopped.
110 */
111 case WMT_STOPPED:
112 active = False;
113
114 SetAsyncEvent(hr);
115
116 break;
117
118 case WMT_EOF:
119 active = False;
120
121 m_fEOF = true;
122
123 break;
124
125 /* This class reacts to any errors by changing its state to stopped.
126 */
127 case WMT_ERROR:
128 active = False;
129
130 error = True;
131 errorString = "Unknown error";
132
133 break;
134
135 case WMT_MISSING_CODEC:
136 error = True;
137 errorString = "Missing appropriate codec";
138
139 break;
140
141 case WMT_NO_RIGHTS:
142 case WMT_NO_RIGHTS_EX:
143 error = True;
144 errorString = "Cannot open protected files";
145
146 break;
147
148 /* Do nothing on other events.
149 */
150 default:
151 break;
152 }
153
154 return S_OK;
155 }
156
157 STDMETHODIMP BoCA::WMAReader::OnSample(DWORD dwOutputNum, QWORD cnsSampleTime, QWORD cnsSampleDuration, DWORD dwFlags, INSSBuffer *pSample, void *pvContext)
158 {
159 /* Check the output number of the sample against the stored output number.
160 * Because only the first audio output is stored, all other outputs,
161 * regardless of type, will be ignored.
162 */
163 if (dwOutputNum != m_dwAudioOutputNum) return S_OK;
164
165 BYTE *pData = NIL;
166 DWORD cbData = 0;
167
168 /* Get the sample from the buffer object.
169 */
170 HRESULT hr = pSample->GetBufferAndLength(&pData, &cbData);
171
172 if (hr == S_OK)
173 {
174 while (IsActive() && samplesBuffer->Size() >= 131072) S::System::System::Sleep(1);
175
176 /* Copy the sample to the sample buffer.
177 */
178 if (IsActive())
179 {
180 samplesBufferMutex->Lock();
181
182 samplesBuffer->Resize(samplesBuffer->Size() + cbData);
183
184 memcpy((UnsignedByte *) *samplesBuffer + samplesBuffer->Size() - cbData, pData, cbData);
185
186 samplesBufferMutex->Release();
187 }
188 }
189
190 return hr;
191 }
192
193 STDMETHODIMP BoCA::WMAReader::OnTime(QWORD cnsCurrentTime, void *pvContext)
194 {
195 HRESULT hr = S_OK;
196
197 /* Keep asking for the specific duration of the stream till EOF
198 */
199 if (m_fEOF == false)
200 {
201 m_qwTime += 10000000; // 1 second
202
203 hr = m_pReaderAdvanced->DeliverTime(m_qwTime);
204 }
205
206 return hr;
207 }
208
209 Void BoCA::WMAReader::SetAsyncEvent(HRESULT hrAsync)
210 {
211 SetEvent(m_hAsyncEvent);
212 }
213
214 Bool BoCA::WMAReader::IsActive()
215 {
216 return active;
217 }
218
219 Void BoCA::WMAReader::SetActive(Bool nActive)
220 {
221 active = nActive;
222 }
223
224 Bool BoCA::WMAReader::IsError()
225 {
226 return error;
227 }
228
229 const String &BoCA::WMAReader::GetErrorString()
230 {
231 return errorString;
232 }
233
234 HANDLE BoCA::WMAReader::GetAsyncEventHandle() const
235 {
236 return m_hAsyncEvent;
237 }
238
239 Void BoCA::WMAReader::SetReaderAdvanced(IWMReaderAdvanced *pReaderAdvanced)
240 {
241 m_pReaderAdvanced = pReaderAdvanced;
242 }
243
244 Void BoCA::WMAReader::SetAudioOutputNum(DWORD dwAudioOutputNum)
245 {
246 m_dwAudioOutputNum = dwAudioOutputNum;
247 }
248
249 Void BoCA::WMAReader::SetSamplesBuffer(Buffer<UnsignedByte> *buffer, Mutex *mutex)
250 {
251 samplesBuffer = buffer;
252 samplesBufferMutex = mutex;
253
254 /* Allocate 128kB for the sample buffer.
255 */
256 samplesBuffer->Resize(131072);
257 samplesBuffer->Resize(0);
258 }