2 * Copyright (C) 2015-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 "VideoSyncTVos.h"
11 #include "cores/VideoPlayer/VideoReferenceClock.h"
12 #include "utils/MathUtils.h"
13 #include "utils/TimeUtils.h"
14 #include "utils/log.h"
15 #include "windowing/GraphicContext.h"
16 #import "windowing/tvos/WinSystemTVOS.h"
18 bool CVideoSyncTVos::Setup()
20 CLog::Log(LOGDEBUG
, "CVideoSyncTVos::{} setting up TVOS", __FUNCTION__
);
22 //init the vblank timestamp
23 m_LastVBlankTime
= CurrentHostCounter();
26 bool setupOk
= InitDisplayLink();
29 m_winSystem
.Register(this);
35 void CVideoSyncTVos::Run(CEvent
& stopEvent
)
37 //because cocoa has a vblank callback, we just keep sleeping until we're asked to stop the thread
38 XbmcThreads::CEventGroup waitGroup
{&stopEvent
, &m_abortEvent
};
42 void CVideoSyncTVos::Cleanup()
44 CLog::Log(LOGDEBUG
, "CVideoSyncTVos::{} cleaning up TVOS", __FUNCTION__
);
46 m_winSystem
.Unregister(this);
49 float CVideoSyncTVos::GetFps()
51 m_fps
= CServiceBroker::GetWinSystem()->GetGfxContext().GetFPS();
52 CLog::Log(LOGDEBUG
, "CVideoSyncTVos::{} Detected refreshrate: {} hertz", __FUNCTION__
, m_fps
);
56 void CVideoSyncTVos::OnResetDisplay()
61 void CVideoSyncTVos::TVosVblankHandler()
63 int64_t nowtime
= CurrentHostCounter();
65 //calculate how many vblanks happened
67 static_cast<double>(nowtime
- m_LastVBlankTime
) / static_cast<double>(CurrentHostFrequency());
68 int NrVBlanks
= MathUtils::round_int(VBlankTime
* static_cast<double>(m_fps
));
70 //save the timestamp of this vblank so we can calculate how many happened next time
71 m_LastVBlankTime
= nowtime
;
73 //update the vblank timestamp, update the clock and send a signal that we got a vblank
74 m_refClock
->UpdateClock(NrVBlanks
, nowtime
);
77 bool CVideoSyncTVos::InitDisplayLink()
80 CLog::Log(LOGDEBUG
, "CVideoSyncTVos: setting up displaylink");
81 if (!m_winSystem
.InitDisplayLink(this))
83 CLog::Log(LOGDEBUG
, "CVideoSyncTVos: InitDisplayLink failed");
89 void CVideoSyncTVos::DeinitDisplayLink()
91 m_winSystem
.DeinitDisplayLink();