PageRenderTime 39ms CodeModel.GetById 28ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/cores/dvdplayer/DVDMessage.cpp

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