PageRenderTime 27ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/src/3rdparty/phonon/ds9/qaudiocdreader.cpp

https://bitbucket.org/kasimling/qt
C++ | 312 lines | 224 code | 62 blank | 26 comment | 20 complexity | 13709abb4145a408d7279f50c72ad204 MD5 | raw file
  1. /* This file is part of the KDE project.
  2. Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
  3. This library is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 2.1 or 3 of the License.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include "qaudiocdreader.h"
  14. #include <dshow.h>
  15. #include <initguid.h>
  16. #include <winioctl.h> // needed for FILE_DEVICE_CD_ROM etc
  17. #define IOCTL_CDROM_READ_TOC CTL_CODE(FILE_DEVICE_CD_ROM, 0x0000, METHOD_BUFFERED, FILE_READ_ACCESS)
  18. #define IOCTL_CDROM_RAW_READ CTL_CODE(FILE_DEVICE_CD_ROM, 0x000F, METHOD_OUT_DIRECT, FILE_READ_ACCESS)
  19. QT_BEGIN_NAMESPACE
  20. #ifndef QT_NO_PHONON_MEDIACONTROLLER
  21. namespace Phonon
  22. {
  23. namespace DS9
  24. {
  25. // {CA46BFE1-D55B-4adf-B803-BC2B9AD57824}
  26. DEFINE_GUID(IID_ITitleInterface,
  27. 0xca46bfe1, 0xd55b, 0x4adf, 0xb8, 0x3, 0xbc, 0x2b, 0x9a, 0xd5, 0x78, 0x24);
  28. struct TRACK_DATA {
  29. UCHAR Reserved;
  30. UCHAR Control : 4;
  31. UCHAR Adr : 4;
  32. UCHAR TrackNumber;
  33. UCHAR Reserved1;
  34. UCHAR Address[4];
  35. };
  36. struct CDROM_TOC {
  37. UCHAR Length[2];
  38. UCHAR FirstTrack;
  39. UCHAR LastTrack;
  40. TRACK_DATA TrackData[100];
  41. };
  42. struct WaveStructure
  43. {
  44. WaveStructure();
  45. char riff[4];
  46. qint32 chunksize;
  47. char wave[4];
  48. char fmt[4];
  49. const qint32 chunksize2;
  50. const quint16 formatTag;
  51. const quint16 nChannels;
  52. const quint32 nSamplesPerSec;
  53. const quint32 nAvgBytesPerSec;
  54. const quint16 nBlockAlign;
  55. const quint16 bitsPerSample;
  56. char data[4];
  57. qint32 dataLength;
  58. };
  59. enum TRACK_MODE_TYPE {
  60. YellowMode2,
  61. XAForm2,
  62. CDDA
  63. };
  64. struct RAW_READ_INFO {
  65. LARGE_INTEGER DiskOffset;
  66. ULONG SectorCount;
  67. TRACK_MODE_TYPE TrackMode;
  68. };
  69. class QAudioCDReader : public QAsyncReader, public ITitleInterface
  70. {
  71. public:
  72. QAudioCDReader(QBaseFilter *parent, QChar drive = QChar());
  73. ~QAudioCDReader();
  74. //reimplementation from IUnknown
  75. STDMETHODIMP_(ULONG) AddRef();
  76. STDMETHODIMP_(ULONG) Release();
  77. STDMETHODIMP Length(LONGLONG *,LONGLONG *);
  78. STDMETHODIMP QueryInterface(REFIID iid, void** out);
  79. QList<qint64> titles() const;
  80. protected:
  81. HRESULT read(LONGLONG pos, LONG length, BYTE *buffer, LONG *actual);
  82. private:
  83. HANDLE m_cddrive;
  84. CDROM_TOC m_toc;
  85. WaveStructure m_waveHeader;
  86. qint64 m_trackAddress;
  87. };
  88. #define SECTOR_SIZE 2352
  89. #define NB_SECTORS_READ 20
  90. static const AM_MEDIA_TYPE audioCDMediaType = { MEDIATYPE_Stream, MEDIASUBTYPE_WAVE, TRUE, FALSE, 1, GUID_NULL, 0, 0, 0};
  91. int addressToSectors(UCHAR address[4])
  92. {
  93. return ((address[0] * 60 + address[1]) * 60 + address[2]) * 75 + address[3] - 150;
  94. }
  95. WaveStructure::WaveStructure() : chunksize(0), chunksize2(16),
  96. formatTag(WAVE_FORMAT_PCM), nChannels(2), nSamplesPerSec(44100), nAvgBytesPerSec(176400), nBlockAlign(4), bitsPerSample(16),
  97. dataLength(0)
  98. {
  99. qMemCopy(riff, "RIFF", 4);
  100. qMemCopy(wave, "WAVE", 4);
  101. qMemCopy(fmt, "fmt ", 4);
  102. qMemCopy(data, "data", 4);
  103. }
  104. QAudioCDReader::QAudioCDReader(QBaseFilter *parent, QChar drive) : QAsyncReader(parent, QVector<AM_MEDIA_TYPE>() << audioCDMediaType)
  105. {
  106. //now open the cd-drive
  107. QString path;
  108. if (drive.isNull()) {
  109. path = QString::fromLatin1("\\\\.\\Cdrom0");
  110. } else {
  111. path = QString::fromLatin1("\\\\.\\%1:").arg(drive);
  112. }
  113. m_cddrive = ::CreateFile((const wchar_t *)path.utf16(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  114. qMemSet(&m_toc, 0, sizeof(CDROM_TOC));
  115. //read the TOC
  116. DWORD bytesRead = 0;
  117. bool tocRead = ::DeviceIoControl(m_cddrive, IOCTL_CDROM_READ_TOC, 0, 0, &m_toc, sizeof(CDROM_TOC), &bytesRead, 0);
  118. if (!tocRead) {
  119. qWarning("unable to load the TOC from the CD");
  120. return;
  121. }
  122. m_trackAddress = addressToSectors(m_toc.TrackData[0].Address);
  123. const qint32 nbSectorsToRead = (addressToSectors(m_toc.TrackData[m_toc.LastTrack + 1 - m_toc.FirstTrack].Address)
  124. - m_trackAddress);
  125. const qint32 dataLength = nbSectorsToRead * SECTOR_SIZE;
  126. m_waveHeader.chunksize = 4 + (8 + m_waveHeader.chunksize2) + (8 + dataLength);
  127. m_waveHeader.dataLength = dataLength;
  128. }
  129. QAudioCDReader::~QAudioCDReader()
  130. {
  131. ::CloseHandle(m_cddrive);
  132. }
  133. STDMETHODIMP_(ULONG) QAudioCDReader::AddRef()
  134. {
  135. return QAsyncReader::AddRef();
  136. }
  137. STDMETHODIMP_(ULONG) QAudioCDReader::Release()
  138. {
  139. return QAsyncReader::Release();
  140. }
  141. STDMETHODIMP QAudioCDReader::Length(LONGLONG *total,LONGLONG *available)
  142. {
  143. const LONGLONG length = sizeof(WaveStructure) + m_waveHeader.dataLength;
  144. if (total) {
  145. *total = length;
  146. }
  147. if (available) {
  148. *available = length;
  149. }
  150. return S_OK;
  151. }
  152. STDMETHODIMP QAudioCDReader::QueryInterface(REFIID iid, void** out)
  153. {
  154. if (!out) {
  155. return E_POINTER;
  156. }
  157. if (iid == IID_ITitleInterface) {
  158. //we reroute that to the pin
  159. *out = static_cast<ITitleInterface*>(this);
  160. AddRef();
  161. return S_OK;
  162. } else {
  163. return QAsyncReader::QueryInterface(iid, out);
  164. }
  165. }
  166. HRESULT QAudioCDReader::read(LONGLONG pos, LONG length, BYTE *buffer, LONG *actual)
  167. {
  168. LONG nbRead = 0;
  169. if (actual) {
  170. *actual = 0;
  171. }
  172. if (pos < sizeof(WaveStructure)) {
  173. //we first copy the content of the structure
  174. nbRead = qMin(LONG(sizeof(WaveStructure) - pos), length);
  175. qMemCopy(buffer, reinterpret_cast<char*>(&m_waveHeader) + pos, nbRead);
  176. }
  177. const LONGLONG posInTrack = pos - sizeof(WaveStructure) + nbRead;
  178. const int bytesLeft = qMin(m_waveHeader.dataLength - posInTrack, LONGLONG(length - nbRead));
  179. if (bytesLeft > 0) {
  180. //we need to read again
  181. const int surplus = posInTrack % SECTOR_SIZE; //how many bytes too much at the beginning
  182. const int firstSector = posInTrack / SECTOR_SIZE,
  183. lastSector = (posInTrack + length - 1) / SECTOR_SIZE;
  184. const int sectorsNeeded = lastSector - firstSector + 1;
  185. int sectorsRead = 0;
  186. QByteArray ba(sectorsNeeded * SECTOR_SIZE, 0);
  187. RAW_READ_INFO ReadInfo;
  188. ReadInfo.TrackMode = CDDA; // Always use CDDA (numerical: 2)
  189. ReadInfo.DiskOffset.QuadPart = (m_trackAddress + firstSector) * 2048;
  190. ReadInfo.SectorCount = qMin(sectorsNeeded - sectorsRead, NB_SECTORS_READ);
  191. while (ReadInfo.SectorCount) {
  192. DWORD dummy = 0;
  193. if (::DeviceIoControl( m_cddrive, IOCTL_CDROM_RAW_READ,
  194. &ReadInfo, sizeof(ReadInfo),
  195. ba.data() + sectorsRead * SECTOR_SIZE,
  196. ReadInfo.SectorCount * SECTOR_SIZE,
  197. &dummy, NULL ) )
  198. {
  199. ReadInfo.DiskOffset.QuadPart += ReadInfo.SectorCount * 2048;
  200. sectorsRead += ReadInfo.SectorCount;
  201. ReadInfo.SectorCount = qMin(sectorsNeeded - sectorsRead, NB_SECTORS_READ);
  202. }else {
  203. qWarning("an error occurred while reading from the media");
  204. return S_FALSE;
  205. }
  206. }
  207. //consume bytes on the buffer
  208. qMemCopy(buffer + nbRead, ba.data() + surplus, bytesLeft);
  209. //at this point we have all we need in the buffer
  210. nbRead += bytesLeft;
  211. }
  212. if (actual) {
  213. *actual = nbRead;
  214. }
  215. return nbRead == length ? S_OK : S_FALSE;
  216. }
  217. QList<qint64> QAudioCDReader::titles() const
  218. {
  219. QList<qint64> ret;
  220. ret << 0;
  221. for(int i = m_toc.FirstTrack; i <= m_toc.LastTrack ; ++i) {
  222. const uchar *address = m_toc.TrackData[i].Address;
  223. ret << ((address[0] * 60 + address[1]) * 60 + address[2]) * 1000 + address[3]*1000/75 - 2000;
  224. }
  225. return ret;
  226. }
  227. QAudioCDPlayer::QAudioCDPlayer() : QBaseFilter(CLSID_NULL)
  228. {
  229. new QAudioCDReader(this);
  230. }
  231. QAudioCDPlayer::~QAudioCDPlayer()
  232. {
  233. }
  234. STDMETHODIMP QAudioCDPlayer::QueryInterface(REFIID iid, void** out)
  235. {
  236. if (iid == IID_ITitleInterface) {
  237. //we reroute that to the pin
  238. return pins().first()->QueryInterface(iid, out);
  239. } else {
  240. return QBaseFilter::QueryInterface(iid, out);
  241. }
  242. }
  243. }
  244. }
  245. #endif //QT_NO_PHONON_MEDIACONTROLLER
  246. QT_END_NAMESPACE