Merge pull request #4955 from Memphiz/osxfixoptical2
[vuplus_xbmc] / xbmc / cores / DummyVideoPlayer.cpp
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 "system.h"
23 #include "DummyVideoPlayer.h"
24 #include "guilib/GUIFontManager.h"
25 #include "guilib/GUITextLayout.h"
26 #include "guilib/GUIFont.h" // for XBFONT_* defines
27 #include "Application.h"
28 #include "settings/AdvancedSettings.h"
29 #include "windowing/WindowingFactory.h"
30 #include "utils/log.h"
31 #include "utils/TimeUtils.h"
32 #include "utils/StringUtils.h"
33
34 CDummyVideoPlayer::CDummyVideoPlayer(IPlayerCallback& callback)
35     : IPlayer(callback),
36       CThread("DummyVideoPlayer")
37 {
38   m_paused = false;
39   m_clock = 0;
40   m_lastTime = 0;
41   m_speed = 1;
42 }
43
44 CDummyVideoPlayer::~CDummyVideoPlayer()
45 {
46   CloseFile();
47 }
48
49 bool CDummyVideoPlayer::OpenFile(const CFileItem& file, const CPlayerOptions &options)
50 {
51   try
52   {
53     Create();
54     if( options.starttime > 0 )
55       SeekTime( (int64_t)(options.starttime * 1000) );
56     return true;
57   }
58   catch(...)
59   {
60     CLog::Log(LOGERROR,"%s - Exception thrown on open", __FUNCTION__);
61     return false;
62   }
63 }
64
65 bool CDummyVideoPlayer::CloseFile()
66 {
67   StopThread();
68   return true;
69 }
70
71 bool CDummyVideoPlayer::IsPlaying() const
72 {
73   return !m_bStop;
74 }
75
76 void CDummyVideoPlayer::Process()
77 {
78   m_clock = 0;
79   m_lastTime = XbmcThreads::SystemClockMillis();
80
81   m_callback.OnPlayBackStarted();
82   while (!m_bStop)
83   {
84     if (!m_paused)
85       m_clock += (XbmcThreads::SystemClockMillis() - m_lastTime)*m_speed;
86     m_lastTime = XbmcThreads::SystemClockMillis();
87     Sleep(0);
88     g_graphicsContext.Lock();
89     if (g_graphicsContext.IsFullScreenVideo())
90     {
91 #ifdef HAS_DX   
92       g_Windowing.Get3DDevice()->BeginScene();
93 #endif
94       g_graphicsContext.Clear();
95       g_graphicsContext.SetRenderingResolution(g_graphicsContext.GetResInfo(), false);
96       Render();
97       g_application.RenderNoPresent();
98 #ifdef HAS_DX     
99       g_Windowing.Get3DDevice()->EndScene();
100 #endif      
101     }
102     g_graphicsContext.Unlock();
103   }
104   if (m_bStop)
105     m_callback.OnPlayBackEnded();
106 }
107
108 void CDummyVideoPlayer::Pause()
109 {
110   if (m_paused)
111     m_callback.OnPlayBackResumed();
112   else
113           m_callback.OnPlayBackPaused();
114   m_paused = !m_paused;
115 }
116
117 bool CDummyVideoPlayer::IsPaused() const
118 {
119   return m_paused;
120 }
121
122 bool CDummyVideoPlayer::HasVideo() const
123 {
124   return true;
125 }
126
127 bool CDummyVideoPlayer::HasAudio() const
128 {
129   return true;
130 }
131
132 void CDummyVideoPlayer::SwitchToNextLanguage()
133 {
134 }
135
136 void CDummyVideoPlayer::ToggleSubtitles()
137 {
138 }
139
140 bool CDummyVideoPlayer::CanSeek()
141 {
142   return GetTotalTime() > 0;
143 }
144
145 void CDummyVideoPlayer::Seek(bool bPlus, bool bLargeStep, bool bChapterOverride)
146 {
147   if (g_advancedSettings.m_videoUseTimeSeeking && GetTotalTime() > 2000*g_advancedSettings.m_videoTimeSeekForwardBig)
148   {
149     int seek = 0;
150     if (bLargeStep)
151       seek = bPlus ? g_advancedSettings.m_videoTimeSeekForwardBig : g_advancedSettings.m_videoTimeSeekBackwardBig;
152     else
153       seek = bPlus ? g_advancedSettings.m_videoTimeSeekForward : g_advancedSettings.m_videoTimeSeekBackward;
154     // do the seek
155     SeekTime(GetTime() + seek * 1000);
156   }
157   else
158   {
159     float percent = GetPercentage();
160     if (bLargeStep)
161       percent += bPlus ? g_advancedSettings.m_videoPercentSeekForwardBig : g_advancedSettings.m_videoPercentSeekBackwardBig;
162     else
163       percent += bPlus ? g_advancedSettings.m_videoPercentSeekForward : g_advancedSettings.m_videoPercentSeekBackward;
164
165     if (percent >= 0 && percent <= 100)
166     {
167       // should be modified to seektime
168       SeekPercentage(percent);
169     }
170   }
171 }
172
173 void CDummyVideoPlayer::GetAudioInfo(CStdString& strAudioInfo)
174 {
175   strAudioInfo = "DummyVideoPlayer - nothing to see here";
176 }
177
178 void CDummyVideoPlayer::GetVideoInfo(CStdString& strVideoInfo)
179 {
180   strVideoInfo = "DummyVideoPlayer - nothing to see here";
181 }
182
183 void CDummyVideoPlayer::GetGeneralInfo(CStdString& strGeneralInfo)
184 {
185   strGeneralInfo = "DummyVideoPlayer - what are you still looking for?";
186 }
187
188 void CDummyVideoPlayer::SwitchToNextAudioLanguage()
189 {
190 }
191
192 void CDummyVideoPlayer::SeekPercentage(float iPercent)
193 {
194   int64_t iTime = (int64_t)(GetTotalTime() * iPercent / 100);
195   SeekTime(iTime);
196 }
197
198 float CDummyVideoPlayer::GetPercentage()
199 {
200   int64_t iTotalTime = GetTotalTime();
201
202   if (iTotalTime != 0)
203   {
204     return GetTime() * 100 / (float)iTotalTime;
205   }
206
207   return 0.0f;
208 }
209
210 //This is how much audio is delayed to video, we count the oposite in the dvdplayer
211 void CDummyVideoPlayer::SetAVDelay(float fValue)
212 {
213 }
214
215 float CDummyVideoPlayer::GetAVDelay()
216 {
217   return 0.0f;
218 }
219
220 void CDummyVideoPlayer::SetSubTitleDelay(float fValue)
221 {
222 }
223
224 float CDummyVideoPlayer::GetSubTitleDelay()
225 {
226   return 0.0;
227 }
228
229 void CDummyVideoPlayer::SeekTime(int64_t iTime)
230 {
231   int seekOffset = (int)(iTime - m_clock);
232   m_clock = iTime;
233   m_callback.OnPlayBackSeek((int)iTime, seekOffset);
234 }
235
236 // return the time in milliseconds
237 int64_t CDummyVideoPlayer::GetTime()
238 {
239   return m_clock;
240 }
241
242 // return length in milliseconds
243 int64_t CDummyVideoPlayer::GetTotalTime()
244 {
245   return 1000000;
246 }
247
248 void CDummyVideoPlayer::ToFFRW(int iSpeed)
249 {
250   m_speed = iSpeed;
251   m_callback.OnPlayBackSpeedChanged(iSpeed);
252 }
253
254 void CDummyVideoPlayer::ShowOSD(bool bOnoff)
255 {
256 }
257
258 CStdString CDummyVideoPlayer::GetPlayerState()
259 {
260   return "";
261 }
262
263 bool CDummyVideoPlayer::SetPlayerState(CStdString state)
264 {
265   return true;
266 }
267
268 void CDummyVideoPlayer::Render()
269 {
270   const CRect vw = g_graphicsContext.GetViewWindow();
271 #ifdef HAS_DX
272   D3DVIEWPORT9 newviewport;
273   D3DVIEWPORT9 oldviewport;
274   g_Windowing.Get3DDevice()->GetViewport(&oldviewport);
275   newviewport.MinZ = 0.0f;
276   newviewport.MaxZ = 1.0f;
277   newviewport.X = (DWORD)vw.x1;
278   newviewport.Y = (DWORD)vw.y1;
279   newviewport.Width = (DWORD)vw.Width();
280   newviewport.Height = (DWORD)vw.Height();
281   g_graphicsContext.SetClipRegion(vw.x1, vw.y1, vw.Width(), vw.Height());
282 #else
283   g_graphicsContext.SetViewPort(vw.x1, vw.y1, vw.Width(), vw.Height());
284 #endif 
285   CGUIFont *font = g_fontManager.GetFont("font13");
286   if (font)
287   {
288     // minor issue: The font rendering here is performed in screen coords
289     // so shouldn't really be scaled
290     int mins = (int)(m_clock / 60000);
291     int secs = (int)((m_clock / 1000) % 60);
292     int ms = (int)(m_clock % 1000);
293     CStdString currentTime = StringUtils::Format("Video goes here %02i:%02i:%03i", mins, secs, ms);
294     float posX = (vw.x1 + vw.x2) * 0.5f;
295     float posY = (vw.y1 + vw.y2) * 0.5f;
296     CGUITextLayout::DrawText(font, posX, posY, 0xffffffff, 0, currentTime, XBFONT_CENTER_X | XBFONT_CENTER_Y);
297   }
298 #ifdef HAS_DX
299   g_graphicsContext.RestoreClipRegion();
300 #else
301   g_graphicsContext.RestoreViewPort();
302 #endif
303 }