|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "directshowioreader.h" |
|
43 |
|
44 #include "directshoweventloop.h" |
|
45 #include "directshowglobal.h" |
|
46 #include "directshowiosource.h" |
|
47 |
|
48 #include <QtCore/qcoreapplication.h> |
|
49 #include <QtCore/qcoreevent.h> |
|
50 #include <QtCore/qiodevice.h> |
|
51 #include <QtCore/qthread.h> |
|
52 |
|
53 class DirectShowSampleRequest |
|
54 { |
|
55 public: |
|
56 DirectShowSampleRequest( |
|
57 IMediaSample *sample, DWORD_PTR userData, LONGLONG position, LONG length, BYTE *buffer) |
|
58 : next(0) |
|
59 , sample(sample) |
|
60 , userData(userData) |
|
61 , position(position) |
|
62 , length(length) |
|
63 , buffer(buffer) |
|
64 , result(S_FALSE) |
|
65 { |
|
66 } |
|
67 |
|
68 DirectShowSampleRequest *remove() { DirectShowSampleRequest *n = next; delete this; return n; } |
|
69 |
|
70 DirectShowSampleRequest *next; |
|
71 IMediaSample *sample; |
|
72 DWORD_PTR userData; |
|
73 LONGLONG position; |
|
74 LONG length; |
|
75 BYTE *buffer; |
|
76 HRESULT result; |
|
77 }; |
|
78 |
|
79 DirectShowIOReader::DirectShowIOReader( |
|
80 QIODevice *device, DirectShowIOSource *source, DirectShowEventLoop *loop) |
|
81 : m_source(source) |
|
82 , m_device(device) |
|
83 , m_loop(loop) |
|
84 , m_pendingHead(0) |
|
85 , m_pendingTail(0) |
|
86 , m_readyHead(0) |
|
87 , m_readyTail(0) |
|
88 , m_synchronousPosition(0) |
|
89 , m_synchronousLength(0) |
|
90 , m_synchronousBytesRead(0) |
|
91 , m_synchronousBuffer(0) |
|
92 , m_synchronousResult(S_OK) |
|
93 , m_totalLength(0) |
|
94 , m_availableLength(0) |
|
95 , m_flushing(false) |
|
96 { |
|
97 moveToThread(device->thread()); |
|
98 |
|
99 connect(device, SIGNAL(readyRead()), this, SLOT(readyRead())); |
|
100 } |
|
101 |
|
102 DirectShowIOReader::~DirectShowIOReader() |
|
103 { |
|
104 flushRequests(); |
|
105 } |
|
106 |
|
107 HRESULT DirectShowIOReader::QueryInterface(REFIID riid, void **ppvObject) |
|
108 { |
|
109 return m_source->QueryInterface(riid, ppvObject); |
|
110 } |
|
111 |
|
112 ULONG DirectShowIOReader::AddRef() |
|
113 { |
|
114 return m_source->AddRef(); |
|
115 } |
|
116 |
|
117 ULONG DirectShowIOReader::Release() |
|
118 { |
|
119 return m_source->Release(); |
|
120 } |
|
121 |
|
122 // IAsyncReader |
|
123 HRESULT DirectShowIOReader::RequestAllocator( |
|
124 IMemAllocator *pPreferred, ALLOCATOR_PROPERTIES *pProps, IMemAllocator **ppActual) |
|
125 { |
|
126 if (!ppActual || !pProps) { |
|
127 return E_POINTER; |
|
128 } else { |
|
129 ALLOCATOR_PROPERTIES actualProperties; |
|
130 |
|
131 if (pProps->cbAlign == 0) |
|
132 pProps->cbAlign = 1; |
|
133 |
|
134 if (pPreferred && pPreferred->SetProperties(pProps, &actualProperties) == S_OK) { |
|
135 pPreferred->AddRef(); |
|
136 |
|
137 *ppActual = pPreferred; |
|
138 |
|
139 m_source->setAllocator(*ppActual); |
|
140 |
|
141 return S_OK; |
|
142 } else { |
|
143 *ppActual = com_new<IMemAllocator>(CLSID_MemoryAllocator, IID_IMemAllocator); |
|
144 |
|
145 if (*ppActual) { |
|
146 if ((*ppActual)->SetProperties(pProps, &actualProperties) != S_OK) { |
|
147 (*ppActual)->Release(); |
|
148 } else { |
|
149 m_source->setAllocator(*ppActual); |
|
150 |
|
151 return S_OK; |
|
152 } |
|
153 } |
|
154 } |
|
155 ppActual = 0; |
|
156 |
|
157 return E_FAIL; |
|
158 } |
|
159 } |
|
160 |
|
161 HRESULT DirectShowIOReader::Request(IMediaSample *pSample, DWORD_PTR dwUser) |
|
162 { |
|
163 QMutexLocker locker(&m_mutex); |
|
164 |
|
165 if (!pSample) { |
|
166 return E_POINTER; |
|
167 } else if (m_flushing) { |
|
168 return VFW_E_WRONG_STATE; |
|
169 } else { |
|
170 REFERENCE_TIME startTime = 0; |
|
171 REFERENCE_TIME endTime = 0; |
|
172 BYTE *buffer; |
|
173 |
|
174 if (pSample->GetTime(&startTime, &endTime) != S_OK |
|
175 || pSample->GetPointer(&buffer) != S_OK) { |
|
176 return VFW_E_SAMPLE_TIME_NOT_SET; |
|
177 } else { |
|
178 LONGLONG position = startTime / 10000000; |
|
179 LONG length = (endTime - startTime) / 10000000; |
|
180 |
|
181 DirectShowSampleRequest *request = new DirectShowSampleRequest( |
|
182 pSample, dwUser, position, length, buffer); |
|
183 |
|
184 if (m_pendingTail) { |
|
185 m_pendingTail->next = request; |
|
186 } else { |
|
187 m_pendingHead = request; |
|
188 |
|
189 m_loop->postEvent(this, new QEvent(QEvent::User)); |
|
190 } |
|
191 m_pendingTail = request; |
|
192 |
|
193 return S_OK; |
|
194 } |
|
195 } |
|
196 } |
|
197 |
|
198 HRESULT DirectShowIOReader::WaitForNext( |
|
199 DWORD dwTimeout, IMediaSample **ppSample, DWORD_PTR *pdwUser) |
|
200 { |
|
201 if (!ppSample || !pdwUser) |
|
202 return E_POINTER; |
|
203 |
|
204 QMutexLocker locker(&m_mutex); |
|
205 |
|
206 do { |
|
207 if (m_readyHead) { |
|
208 DirectShowSampleRequest *request = m_readyHead; |
|
209 |
|
210 *ppSample = request->sample; |
|
211 *pdwUser = request->userData; |
|
212 |
|
213 HRESULT hr = request->result; |
|
214 |
|
215 m_readyHead = request->next; |
|
216 |
|
217 if (!m_readyHead) |
|
218 m_readyTail = 0; |
|
219 |
|
220 delete request; |
|
221 |
|
222 return hr; |
|
223 } else if (m_flushing) { |
|
224 *ppSample = 0; |
|
225 *pdwUser = 0; |
|
226 |
|
227 return VFW_E_WRONG_STATE; |
|
228 } |
|
229 } while (m_wait.wait(&m_mutex, dwTimeout)); |
|
230 |
|
231 *ppSample = 0; |
|
232 *pdwUser = 0; |
|
233 |
|
234 return VFW_E_TIMEOUT; |
|
235 } |
|
236 |
|
237 HRESULT DirectShowIOReader::SyncReadAligned(IMediaSample *pSample) |
|
238 { |
|
239 if (!pSample) { |
|
240 return E_POINTER; |
|
241 } else { |
|
242 REFERENCE_TIME startTime = 0; |
|
243 REFERENCE_TIME endTime = 0; |
|
244 BYTE *buffer; |
|
245 |
|
246 if (pSample->GetTime(&startTime, &endTime) != S_OK |
|
247 || pSample->GetPointer(&buffer) != S_OK) { |
|
248 return VFW_E_SAMPLE_TIME_NOT_SET; |
|
249 } else { |
|
250 LONGLONG position = startTime / 10000000; |
|
251 LONG length = (endTime - startTime) / 10000000; |
|
252 |
|
253 QMutexLocker locker(&m_mutex); |
|
254 |
|
255 if (thread() == QThread::currentThread()) { |
|
256 qint64 bytesRead = 0; |
|
257 |
|
258 HRESULT hr = blockingRead(position, length, buffer, &bytesRead); |
|
259 |
|
260 if (SUCCEEDED(hr)) |
|
261 pSample->SetActualDataLength(bytesRead); |
|
262 |
|
263 return hr; |
|
264 } else { |
|
265 m_synchronousPosition = position; |
|
266 m_synchronousLength = length; |
|
267 m_synchronousBuffer = buffer; |
|
268 |
|
269 m_loop->postEvent(this, new QEvent(QEvent::User)); |
|
270 |
|
271 m_wait.wait(&m_mutex); |
|
272 |
|
273 m_synchronousBuffer = 0; |
|
274 |
|
275 if (SUCCEEDED(m_synchronousResult)) |
|
276 pSample->SetActualDataLength(m_synchronousBytesRead); |
|
277 |
|
278 return m_synchronousResult; |
|
279 } |
|
280 } |
|
281 } |
|
282 } |
|
283 |
|
284 HRESULT DirectShowIOReader::SyncRead(LONGLONG llPosition, LONG lLength, BYTE *pBuffer) |
|
285 { |
|
286 if (!pBuffer) { |
|
287 return E_POINTER; |
|
288 } else { |
|
289 if (thread() == QThread::currentThread()) { |
|
290 qint64 bytesRead; |
|
291 |
|
292 return blockingRead(llPosition, lLength, pBuffer, &bytesRead); |
|
293 } else { |
|
294 QMutexLocker locker(&m_mutex); |
|
295 |
|
296 m_synchronousPosition = llPosition; |
|
297 m_synchronousLength = lLength; |
|
298 m_synchronousBuffer = pBuffer; |
|
299 |
|
300 m_loop->postEvent(this, new QEvent(QEvent::User)); |
|
301 |
|
302 m_wait.wait(&m_mutex); |
|
303 |
|
304 m_synchronousBuffer = 0; |
|
305 |
|
306 return m_synchronousResult; |
|
307 } |
|
308 } |
|
309 } |
|
310 |
|
311 HRESULT DirectShowIOReader::Length(LONGLONG *pTotal, LONGLONG *pAvailable) |
|
312 { |
|
313 if (!pTotal || !pAvailable) { |
|
314 return E_POINTER; |
|
315 } else { |
|
316 QMutexLocker locker(&m_mutex); |
|
317 |
|
318 *pTotal = m_totalLength; |
|
319 *pAvailable = m_availableLength; |
|
320 |
|
321 return S_OK; |
|
322 } |
|
323 } |
|
324 |
|
325 |
|
326 HRESULT DirectShowIOReader::BeginFlush() |
|
327 { |
|
328 QMutexLocker locker(&m_mutex); |
|
329 |
|
330 if (m_flushing) |
|
331 return S_FALSE; |
|
332 |
|
333 m_flushing = true; |
|
334 |
|
335 flushRequests(); |
|
336 |
|
337 m_wait.wakeAll(); |
|
338 |
|
339 return S_OK; |
|
340 } |
|
341 |
|
342 HRESULT DirectShowIOReader::EndFlush() |
|
343 { |
|
344 QMutexLocker locker(&m_mutex); |
|
345 |
|
346 if (!m_flushing) |
|
347 return S_FALSE; |
|
348 |
|
349 m_flushing = false; |
|
350 |
|
351 return S_OK; |
|
352 } |
|
353 |
|
354 void DirectShowIOReader::customEvent(QEvent *event) |
|
355 { |
|
356 if (event->type() == QEvent::User) { |
|
357 readyRead(); |
|
358 } else { |
|
359 QObject::customEvent(event); |
|
360 } |
|
361 } |
|
362 |
|
363 void DirectShowIOReader::readyRead() |
|
364 { |
|
365 QMutexLocker locker(&m_mutex); |
|
366 |
|
367 m_availableLength = m_device->bytesAvailable() + m_device->pos(); |
|
368 m_totalLength = m_device->size(); |
|
369 |
|
370 if (m_synchronousBuffer) { |
|
371 if (nonBlockingRead( |
|
372 m_synchronousPosition, |
|
373 m_synchronousLength, |
|
374 m_synchronousBuffer, |
|
375 &m_synchronousBytesRead, |
|
376 &m_synchronousResult)) { |
|
377 m_wait.wakeAll(); |
|
378 } |
|
379 } else { |
|
380 qint64 bytesRead = 0; |
|
381 |
|
382 while (m_pendingHead && nonBlockingRead( |
|
383 m_pendingHead->position, |
|
384 m_pendingHead->length, |
|
385 m_pendingHead->buffer, |
|
386 &bytesRead, |
|
387 &m_pendingHead->result)) { |
|
388 m_pendingHead->sample->SetActualDataLength(bytesRead); |
|
389 |
|
390 if (m_readyTail) |
|
391 m_readyTail->next = m_pendingHead; |
|
392 m_readyTail = m_pendingHead; |
|
393 |
|
394 m_pendingHead = m_pendingHead->next; |
|
395 |
|
396 m_readyTail->next = 0; |
|
397 |
|
398 if (!m_pendingHead) |
|
399 m_pendingTail = 0; |
|
400 |
|
401 if (!m_readyHead) |
|
402 m_readyHead = m_readyTail; |
|
403 |
|
404 m_wait.wakeAll(); |
|
405 } |
|
406 } |
|
407 } |
|
408 |
|
409 HRESULT DirectShowIOReader::blockingRead( |
|
410 LONGLONG position, LONG length, BYTE *buffer, qint64 *bytesRead) |
|
411 { |
|
412 *bytesRead = 0; |
|
413 |
|
414 if (qint64(position) > m_device->size()) |
|
415 return S_FALSE; |
|
416 |
|
417 const qint64 maxSize = qMin<qint64>(m_device->size(), position + length); |
|
418 |
|
419 while (m_device->bytesAvailable() + m_device->pos() < maxSize) { |
|
420 if (!m_device->waitForReadyRead(-1)) |
|
421 return S_FALSE; |
|
422 } |
|
423 |
|
424 if (m_device->pos() != position && !m_device->seek(position)) |
|
425 return S_FALSE; |
|
426 |
|
427 const qint64 maxBytes = qMin<qint64>(length, m_device->bytesAvailable()); |
|
428 |
|
429 *bytesRead = m_device->read(reinterpret_cast<char *>(buffer), maxBytes); |
|
430 |
|
431 if (*bytesRead != length) { |
|
432 qMemSet(buffer + *bytesRead, 0, length - *bytesRead); |
|
433 |
|
434 return S_FALSE; |
|
435 } else { |
|
436 return S_OK; |
|
437 } |
|
438 } |
|
439 |
|
440 bool DirectShowIOReader::nonBlockingRead( |
|
441 LONGLONG position, LONG length, BYTE *buffer, qint64 *bytesRead, HRESULT *result) |
|
442 { |
|
443 const qint64 maxSize = qMin<qint64>(m_device->size(), position + length); |
|
444 |
|
445 if (position > m_device->size()) { |
|
446 *bytesRead = 0; |
|
447 *result = S_FALSE; |
|
448 |
|
449 return true; |
|
450 } else if (m_device->bytesAvailable() + m_device->pos() >= maxSize) { |
|
451 if (m_device->pos() != position && !m_device->seek(position)) { |
|
452 *bytesRead = 0; |
|
453 *result = S_FALSE; |
|
454 |
|
455 return true; |
|
456 } else { |
|
457 const qint64 maxBytes = qMin<qint64>(length, m_device->bytesAvailable()); |
|
458 |
|
459 *bytesRead = m_device->read(reinterpret_cast<char *>(buffer), maxBytes); |
|
460 |
|
461 if (*bytesRead != length) { |
|
462 qMemSet(buffer + *bytesRead, 0, length - *bytesRead); |
|
463 |
|
464 *result = S_FALSE; |
|
465 } else { |
|
466 *result = S_OK; |
|
467 } |
|
468 |
|
469 return true; |
|
470 } |
|
471 } else { |
|
472 return false; |
|
473 } |
|
474 } |
|
475 |
|
476 void DirectShowIOReader::flushRequests() |
|
477 { |
|
478 while (m_pendingHead) { |
|
479 m_pendingHead->result = VFW_E_WRONG_STATE; |
|
480 |
|
481 if (m_readyTail) |
|
482 m_readyTail->next = m_pendingHead; |
|
483 |
|
484 m_readyTail = m_pendingHead; |
|
485 |
|
486 m_pendingHead = m_pendingHead->next; |
|
487 |
|
488 m_readyTail->next = 0; |
|
489 |
|
490 if (!m_pendingHead) |
|
491 m_pendingTail = 0; |
|
492 |
|
493 if (!m_readyHead) |
|
494 m_readyHead = m_readyTail; |
|
495 } |
|
496 } |