2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "DVDMessage.h"
11 #include "DVDDemuxers/DVDDemuxUtils.h"
12 #include "threads/Condition.h"
13 #include "threads/CriticalSection.h"
14 #include "threads/SystemClock.h"
15 #include "utils/MathUtils.h"
16 #include "utils/log.h"
21 using namespace std::chrono_literals
;
23 class CDVDMsgGeneralSynchronizePriv
26 CDVDMsgGeneralSynchronizePriv(std::chrono::milliseconds timeout
, unsigned int sources
)
27 : sources(sources
), m_timer(timeout
)
30 unsigned int reached
= 0;
31 CCriticalSection section
;
32 XbmcThreads::ConditionVariable condition
;
33 XbmcThreads::EndTime
<> m_timer
;
37 * CDVDMsgGeneralSynchronize --- GENERAL_SYNCRONIZR
39 CDVDMsgGeneralSynchronize::CDVDMsgGeneralSynchronize(std::chrono::milliseconds timeout
,
41 : CDVDMsg(GENERAL_SYNCHRONIZE
), m_p(new CDVDMsgGeneralSynchronizePriv(timeout
, sources
))
45 CDVDMsgGeneralSynchronize::~CDVDMsgGeneralSynchronize()
47 m_p
->condition
.notifyAll();
52 bool CDVDMsgGeneralSynchronize::Wait(std::chrono::milliseconds timeout
, unsigned int source
)
54 std::unique_lock
<CCriticalSection
> lock(m_p
->section
);
56 XbmcThreads::EndTime
<> timer
{timeout
};
58 m_p
->reached
|= (source
& m_p
->sources
);
59 if ((m_p
->sources
& SYNCSOURCE_ANY
) && source
)
60 m_p
->reached
|= SYNCSOURCE_ANY
;
62 m_p
->condition
.notifyAll();
64 while (m_p
->reached
!= m_p
->sources
)
66 timeout
= std::min(m_p
->m_timer
.GetTimeLeft(), timer
.GetTimeLeft());
67 if (m_p
->condition
.wait(lock
, timeout
))
70 if (m_p
->m_timer
.IsTimePast())
72 CLog::Log(LOGDEBUG
, "CDVDMsgGeneralSynchronize - global timeout");
73 return true; // global timeout, we are done
75 if (timer
.IsTimePast())
77 return false; /* request timeout, should be retried */
83 void CDVDMsgGeneralSynchronize::Wait(std::atomic
<bool>& abort
, unsigned int source
)
85 while (!Wait(100ms
, source
) && !abort
)
90 * CDVDMsgDemuxerPacket --- DEMUXER_PACKET
92 CDVDMsgDemuxerPacket::CDVDMsgDemuxerPacket(DemuxPacket
* packet
, bool drop
) : CDVDMsg(DEMUXER_PACKET
)
98 CDVDMsgDemuxerPacket::~CDVDMsgDemuxerPacket()
101 CDVDDemuxUtils::FreeDemuxPacket(m_packet
);
104 unsigned int CDVDMsgDemuxerPacket::GetPacketSize()
107 return m_packet
->iSize
;