RawMediaReader.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "RawMediaReader.h"
  2. #include <limits.h>
  3. bool IsMyExtension(const wchar_t *filename);
  4. int RawMediaReaderService::CreateRawMediaReader(const wchar_t *filename, ifc_raw_media_reader **out_reader)
  5. {
  6. if (IsMyExtension(filename))
  7. {
  8. CGioFile *file = new CGioFile();
  9. if (!file)
  10. return NErr_OutOfMemory;
  11. if (file->Open(filename, 0) != NErr_Success)
  12. {
  13. delete file;
  14. return NErr_FileNotFound;
  15. }
  16. RawMediaReader *reader = new RawMediaReader(file);
  17. if (!reader)
  18. {
  19. file->Close();
  20. delete file;
  21. return NErr_OutOfMemory;
  22. }
  23. *out_reader = reader;
  24. return NErr_Success;
  25. }
  26. else
  27. {
  28. return NErr_False;
  29. }
  30. }
  31. #define CBCLASS RawMediaReaderService
  32. START_DISPATCH;
  33. CB(CREATERAWMEDIAREADER, CreateRawMediaReader);
  34. END_DISPATCH;
  35. #undef CBCLASS
  36. RawMediaReader::RawMediaReader(CGioFile *file) : file(file)
  37. {}
  38. int RawMediaReader::Read( void *buffer, size_t buffer_size, size_t *bytes_read )
  39. {
  40. if ( buffer_size > INT_MAX )
  41. return NErr_BadParameter;
  42. int file_bytes_read = 0;
  43. int ret = file->Read( buffer, (int)buffer_size, &file_bytes_read );
  44. if ( ret == NErr_Success )
  45. {
  46. *bytes_read = (size_t)file_bytes_read;
  47. if ( !file_bytes_read && file->IsEof() )
  48. return NErr_EndOfFile;
  49. return NErr_Success;
  50. }
  51. else
  52. return NErr_Error;
  53. }
  54. size_t RawMediaReader::Release()
  55. {
  56. file->Close();
  57. delete file;
  58. file = NULL;
  59. delete this;
  60. return 0;
  61. }
  62. #define CBCLASS RawMediaReader
  63. START_DISPATCH;
  64. CB( RELEASE, Release );
  65. CB( RAW_READ, Read );
  66. END_DISPATCH;
  67. #undef CBCLASS