Ap4AsyncReaderStream.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:2k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "Ap4AsyncReaderStream.h"
  3. AP4_AsyncReaderStream::AP4_AsyncReaderStream(CBaseSplitterFile* pFile) 
  4. : m_refs(1)
  5. , m_pFile(pFile)
  6. {
  7. ASSERT(pFile);
  8. }
  9. AP4_AsyncReaderStream::~AP4_AsyncReaderStream()
  10. {
  11. ASSERT(m_refs == 0);
  12. }
  13. void AP4_AsyncReaderStream::AddReference()
  14. {
  15. ASSERT(m_refs > 0); 
  16. ++m_refs;
  17. }
  18. void AP4_AsyncReaderStream::Release()
  19. {
  20. ASSERT(m_refs > 0); 
  21. if(--m_refs == 0) delete this;
  22. }
  23. AP4_Result AP4_AsyncReaderStream::Read(void* buffer, AP4_Size bytesToRead, AP4_Size* bytesRead)
  24. {
  25. AP4_Size bytesAvail = (AP4_Size)(m_pFile->GetLength() - m_pFile->GetPos());
  26. if(bytesAvail < bytesToRead)
  27. {
  28. if(bytesRead) *bytesRead = bytesAvail;
  29. bytesToRead = bytesAvail;
  30. }
  31. if(bytesAvail == 0)
  32. {
  33. return AP4_ERROR_EOS;
  34. }
  35. if(FAILED(m_pFile->Read((BYTE*)buffer, bytesToRead)))
  36. {
  37. if(bytesRead) *bytesRead = 0;
  38. return AP4_ERROR_READ_FAILED;
  39. }
  40. if(bytesRead) *bytesRead = bytesToRead;
  41. return AP4_SUCCESS;
  42. }
  43. AP4_Result AP4_AsyncReaderStream::Write(const void* buffer, AP4_Size bytesToWrite, AP4_Size* bytesWritten)
  44. {
  45.     return AP4_ERROR_WRITE_FAILED;
  46. }
  47. AP4_Result AP4_AsyncReaderStream::Seek(AP4_Offset offset)
  48. {
  49. m_pFile->Seek(offset);
  50. return m_pFile->GetPos() == offset ? AP4_SUCCESS : AP4_FAILURE;
  51. }
  52. AP4_Result AP4_AsyncReaderStream::Tell(AP4_Offset& offset)
  53. {
  54. offset = (AP4_Offset)m_pFile->GetPos();
  55. return AP4_SUCCESS;
  56. }
  57. AP4_Result AP4_AsyncReaderStream::GetSize(AP4_Size& size)
  58. {
  59. size = (AP4_Size)m_pFile->GetLength();
  60. return AP4_SUCCESS;
  61. }