/xbmc/cores/dvdplayer/DVDInputStreams/DVDInputStreamFile.cpp

http://github.com/xbmc/xbmc · C++ · 161 lines · 107 code · 31 blank · 23 comment · 32 complexity · e6f711f0343e2d959e5357be20fcf384 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "DVDInputStreamFile.h"
  21. #include "filesystem/File.h"
  22. #include "filesystem/IFile.h"
  23. #include "settings/AdvancedSettings.h"
  24. #include "utils/log.h"
  25. #include "utils/URIUtils.h"
  26. using namespace XFILE;
  27. CDVDInputStreamFile::CDVDInputStreamFile() : CDVDInputStream(DVDSTREAM_TYPE_FILE)
  28. {
  29. m_pFile = NULL;
  30. m_eof = true;
  31. }
  32. CDVDInputStreamFile::~CDVDInputStreamFile()
  33. {
  34. Close();
  35. }
  36. bool CDVDInputStreamFile::IsEOF()
  37. {
  38. return !m_pFile || m_eof;
  39. }
  40. bool CDVDInputStreamFile::Open(const char* strFile, const std::string& content)
  41. {
  42. if (!CDVDInputStream::Open(strFile, content))
  43. return false;
  44. m_pFile = new CFile();
  45. if (!m_pFile)
  46. return false;
  47. unsigned int flags = READ_TRUNCATED | READ_BITRATE | READ_CHUNKED;
  48. if ( g_advancedSettings.m_alwaysForceBuffer &&
  49. !URIUtils::IsOnDVD(strFile) &&
  50. !URIUtils::IsBluray(strFile) )
  51. flags |= READ_CACHED;
  52. if (content == "video/mp4" || content == "video/x-msvideo" || content == "video/avi")
  53. flags |= READ_MULTI_STREAM;
  54. // open file in binary mode
  55. if (!m_pFile->Open(strFile, flags))
  56. {
  57. delete m_pFile;
  58. m_pFile = NULL;
  59. return false;
  60. }
  61. if (m_pFile->GetImplemenation() && (content.empty() || content == "application/octet-stream"))
  62. m_content = m_pFile->GetImplemenation()->GetContent();
  63. m_eof = true;
  64. return true;
  65. }
  66. // close file and reset everyting
  67. void CDVDInputStreamFile::Close()
  68. {
  69. if (m_pFile)
  70. {
  71. m_pFile->Close();
  72. delete m_pFile;
  73. }
  74. CDVDInputStream::Close();
  75. m_pFile = NULL;
  76. m_eof = true;
  77. }
  78. int CDVDInputStreamFile::Read(uint8_t* buf, int buf_size)
  79. {
  80. if(!m_pFile) return -1;
  81. unsigned int ret = m_pFile->Read(buf, buf_size);
  82. /* we currently don't support non completing reads */
  83. if( ret == 0 ) m_eof = true;
  84. return (int)(ret & 0xFFFFFFFF);
  85. }
  86. int64_t CDVDInputStreamFile::Seek(int64_t offset, int whence)
  87. {
  88. if(!m_pFile) return -1;
  89. if(whence == SEEK_POSSIBLE)
  90. return m_pFile->IoControl(IOCTRL_SEEK_POSSIBLE, NULL);
  91. int64_t ret = m_pFile->Seek(offset, whence);
  92. /* if we succeed, we are not eof anymore */
  93. if( ret >= 0 ) m_eof = false;
  94. return ret;
  95. }
  96. int64_t CDVDInputStreamFile::GetLength()
  97. {
  98. if (m_pFile)
  99. return m_pFile->GetLength();
  100. return 0;
  101. }
  102. bool CDVDInputStreamFile::GetCacheStatus(XFILE::SCacheStatus *status)
  103. {
  104. if(m_pFile && m_pFile->IoControl(IOCTRL_CACHE_STATUS, status) >= 0)
  105. return true;
  106. else
  107. return false;
  108. }
  109. BitstreamStats CDVDInputStreamFile::GetBitstreamStats() const
  110. {
  111. if (!m_pFile)
  112. return m_stats; // dummy return. defined in CDVDInputStream
  113. if(m_pFile->GetBitstreamStats())
  114. return *m_pFile->GetBitstreamStats();
  115. else
  116. return m_stats;
  117. }
  118. int CDVDInputStreamFile::GetBlockSize()
  119. {
  120. if(m_pFile)
  121. return m_pFile->GetChunkSize();
  122. else
  123. return 0;
  124. }
  125. void CDVDInputStreamFile::SetReadRate(unsigned rate)
  126. {
  127. unsigned maxrate = rate + 1024 * 1024 / 8;
  128. if(m_pFile->IoControl(IOCTRL_CACHE_SETRATE, &maxrate) >= 0)
  129. CLog::Log(LOGDEBUG, "CDVDInputStreamFile::SetReadRate - set cache throttle rate to %u bytes per second", maxrate);
  130. }