/xbmc/cores/dvdplayer/DVDMessage.cpp

http://github.com/xbmc/xbmc · C++ · 121 lines · 81 code · 14 blank · 26 comment · 13 complexity · 487390285bf525c75928943045678bee 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 "threads/SystemClock.h"
  21. #include "DVDMessage.h"
  22. #include "DVDDemuxers/DVDDemuxUtils.h"
  23. #include "DVDStreamInfo.h"
  24. #include "utils/TimeUtils.h"
  25. #include "utils/log.h"
  26. #include "threads/CriticalSection.h"
  27. #include "threads/Condition.h"
  28. #include "threads/SystemClock.h"
  29. #include "utils/MathUtils.h"
  30. class CDVDMsgGeneralSynchronizePriv
  31. {
  32. public:
  33. CDVDMsgGeneralSynchronizePriv(unsigned int timeout, unsigned int sources)
  34. : sources(sources ? sources : SYNCSOURCE_ALL)
  35. , reached(0)
  36. , timeout(timeout)
  37. {}
  38. unsigned int sources;
  39. unsigned int reached;
  40. CCriticalSection section;
  41. XbmcThreads::ConditionVariable condition;
  42. XbmcThreads::EndTime timeout;
  43. };
  44. /**
  45. * CDVDMsgGeneralSynchronize --- GENERAL_SYNCRONIZR
  46. */
  47. CDVDMsgGeneralSynchronize::CDVDMsgGeneralSynchronize(unsigned int timeout, unsigned int sources) : CDVDMsg(GENERAL_SYNCHRONIZE)
  48. , m_p(new CDVDMsgGeneralSynchronizePriv(timeout, sources))
  49. {
  50. }
  51. CDVDMsgGeneralSynchronize::~CDVDMsgGeneralSynchronize()
  52. {
  53. delete m_p;
  54. }
  55. bool CDVDMsgGeneralSynchronize::Wait(unsigned int milliseconds, unsigned int source)
  56. {
  57. if(source == 0)
  58. source = SYNCSOURCE_OWNER;
  59. /* if we are not requested to wait on this object just return, reference count will be decremented */
  60. if (!(m_p->sources & source))
  61. return true;
  62. CSingleLock lock(m_p->section);
  63. XbmcThreads::EndTime timeout(milliseconds);
  64. m_p->reached |= source & m_p->sources;
  65. while( (long)MathUtils::bitcount(m_p->reached) < GetNrOfReferences() )
  66. {
  67. milliseconds = std::min(m_p->timeout.MillisLeft(), timeout.MillisLeft());
  68. if(m_p->condition.wait(lock, milliseconds))
  69. continue;
  70. if(m_p->timeout.IsTimePast())
  71. return true; /* global timeout, we are done */
  72. if(timeout.IsTimePast())
  73. return false; /* request timeout, should be retried */
  74. }
  75. return true;
  76. }
  77. void CDVDMsgGeneralSynchronize::Wait(volatile bool *abort, unsigned int source)
  78. {
  79. while(!Wait(100, source))
  80. {
  81. if(abort && *abort)
  82. return;
  83. }
  84. }
  85. long CDVDMsgGeneralSynchronize::Release()
  86. {
  87. CSingleLock lock(m_p->section);
  88. long count = --m_refs;
  89. m_p->condition.notifyAll();
  90. lock.Leave();
  91. if (count == 0)
  92. delete this;
  93. return count;
  94. }
  95. /**
  96. * CDVDMsgDemuxerPacket --- DEMUXER_PACKET
  97. */
  98. CDVDMsgDemuxerPacket::CDVDMsgDemuxerPacket(DemuxPacket* packet, bool drop) : CDVDMsg(DEMUXER_PACKET)
  99. {
  100. m_packet = packet;
  101. m_drop = drop;
  102. }
  103. CDVDMsgDemuxerPacket::~CDVDMsgDemuxerPacket()
  104. {
  105. if (m_packet)
  106. CDVDDemuxUtils::FreeDemuxPacket(m_packet);
  107. }