RawReader.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef WIN32_LEAN_AND_MEAN
  2. #define WIN32_LEAN_AND_MEAN
  3. #endif
  4. #include "RawReader.h"
  5. #include "main.h"
  6. #include <limits.h>
  7. #include <shlwapi.h>
  8. #include "../nu/AutoWide.h"
  9. #include <new>
  10. #include <strsafe.h>
  11. #include "nswasabi/ReferenceCounted.h"
  12. static bool IsMyExtension(const wchar_t *filename)
  13. {
  14. const wchar_t *extension = PathFindExtensionW(filename);
  15. if (extension && *extension)
  16. {
  17. wchar_t exts[128] = {0};
  18. GetPrivateProfileStringW(L"in_flac", L"extensions", DEFAULT_EXTENSIONSW, exts, 128, winampINI);
  19. extension++;
  20. wchar_t *b = exts;
  21. wchar_t *c;
  22. do
  23. {
  24. wchar_t d[20] = {0};
  25. StringCchCopyW(d, 15, b);
  26. if ((c = wcschr(b, L';')))
  27. {
  28. if ((c-b)<15)
  29. d[c - b] = 0;
  30. }
  31. if (!lstrcmpiW(extension, d))
  32. return true;
  33. b = c + 1;
  34. }
  35. while (c);
  36. }
  37. return false;
  38. }
  39. int RawMediaReaderService::CreateRawMediaReader(const wchar_t *filename, ifc_raw_media_reader **out_reader)
  40. {
  41. if (IsMyExtension(filename))
  42. {
  43. nx_file_t file;
  44. ReferenceCountedNXString filename_nx;
  45. ReferenceCountedNXURI filename_uri;
  46. NXStringCreateWithUTF16(&filename_nx, filename);
  47. NXURICreateWithNXString(&filename_uri, filename_nx);
  48. int ret = NXFileOpenFile(&file, filename_uri, nx_file_FILE_read_binary);
  49. if (ret != NErr_Success)
  50. return ret;
  51. RawMediaReader *reader = new (std::nothrow) RawMediaReader();
  52. if (!reader)
  53. {
  54. NXFileRelease(file);
  55. return NErr_OutOfMemory;
  56. }
  57. ret = reader->Initialize(file);
  58. NXFileRelease(file);
  59. if (ret != NErr_Success)
  60. {
  61. delete reader;
  62. return ret;
  63. }
  64. *out_reader = reader;
  65. return NErr_Success;
  66. }
  67. else
  68. {
  69. return NErr_False;
  70. }
  71. }
  72. #define CBCLASS RawMediaReaderService
  73. START_DISPATCH;
  74. CB(CREATERAWMEDIAREADER, CreateRawMediaReader);
  75. END_DISPATCH;
  76. #undef CBCLASS
  77. static void OnError(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  78. {
  79. //client_data=client_data; // dummy line so i can set a breakpoint
  80. }
  81. static void OnMetadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  82. {
  83. }
  84. static FLAC__StreamDecoderWriteStatus OnAudio(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
  85. {
  86. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  87. }
  88. RawMediaReader::RawMediaReader()
  89. {
  90. decoder=0;
  91. file=0;
  92. }
  93. RawMediaReader::~RawMediaReader()
  94. {
  95. if (decoder)
  96. FLAC__stream_decoder_delete(decoder);
  97. NXFileRelease(file);
  98. }
  99. int RawMediaReader::Initialize(nx_file_t file)
  100. {
  101. this->file = NXFileRetain(file);
  102. decoder = FLAC__stream_decoder_new();
  103. if (!decoder)
  104. return NErr_FailedCreate;
  105. FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
  106. state.SetFile(file);
  107. state.SetObject(this);
  108. FLAC__stream_decoder_set_md5_checking(decoder, true);
  109. if(FLAC__stream_decoder_init_stream(
  110. decoder,
  111. FLAC_NXFile_Read,
  112. FLAC_NXFile_Seek,
  113. FLAC_NXFile_Tell,
  114. FLAC_NXFile_Length,
  115. FLAC_NXFile_EOF,
  116. OnAudio,
  117. OnMetadata,
  118. OnError,
  119. &state
  120. ) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
  121. {
  122. FLAC__stream_decoder_delete(decoder);
  123. decoder=0;
  124. return NErr_Error;
  125. }
  126. FLAC__stream_decoder_process_until_end_of_metadata(decoder);
  127. uint64_t position;
  128. FLAC__stream_decoder_get_decode_position(decoder, &position);
  129. FLAC__stream_decoder_delete(decoder);
  130. decoder=0;
  131. NXFileSeek(file, position);
  132. return NErr_Success;
  133. }
  134. int RawMediaReader::Read(void *buffer, size_t buffer_size, size_t *bytes_read)
  135. {
  136. return NXFileRead(file, buffer, buffer_size, bytes_read);
  137. }
  138. size_t RawMediaReader::Release()
  139. {
  140. delete this;
  141. return 0;
  142. }
  143. #define CBCLASS RawMediaReader
  144. START_DISPATCH;
  145. CB(RELEASE, Release);
  146. CB(RAW_READ, Read);
  147. END_DISPATCH;
  148. #undef CBCLASS