guiTheoraCtrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef TORQUE_OGGTHEORA
  23. #include "gui/core/guiControl.h"
  24. #include "gui/theora/guiTheoraCtrl.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "console/engineAPI.h"
  29. IMPLEMENT_CONOBJECT( GuiTheoraCtrl );
  30. ConsoleDocClass( GuiTheoraCtrl,
  31. "@brief A control to playing Theora videos.\n\n"
  32. "This control can be used to play videos in the Theora video format. The videos may include audio in Vorbis format. The "
  33. "codecs for both formats are integrated with the engine and no codecs must be present on the user's machine.\n\n"
  34. "@tsexample\n"
  35. "%video = new GuiTheoraCtrl()\n"
  36. "{\n"
  37. " theoraFile = \"videos/intro.ogv\";\n"
  38. " playOnWake = false;\n"
  39. " stopOnSleep = true;\n"
  40. "}\n"
  41. "\n"
  42. "Canvas.setContent( %video );\n"
  43. "%video.play();\n"
  44. "@endtsexample\n\n"
  45. "@see http://www.theora.org\n\n"
  46. "@ingroup GuiImages"
  47. );
  48. ImplementEnumType( GuiTheoraTranscoder,
  49. "Routine to use for converting Theora's Y'CbCr pixel format to RGB color space.\n\n"
  50. "@ingroup GuiImages" )
  51. { OggTheoraDecoder::TRANSCODER_Auto, "Auto", "Automatically detect most appropriate setting." },
  52. { OggTheoraDecoder::TRANSCODER_Generic, "Generic", "Slower but beneric transcoder that can convert all Y'CbCr input formats to RGB or RGBA output." },
  53. { OggTheoraDecoder::TRANSCODER_SSE2420RGBA, "SSE2420RGBA", "Fast SSE2-based transcoder with fixed conversion from 4:2:0 Y'CbCr to RGBA." },
  54. EndImplementEnumType;
  55. //-----------------------------------------------------------------------------
  56. GuiTheoraCtrl::GuiTheoraCtrl()
  57. {
  58. mFilename = StringTable->EmptyString();
  59. mDone = false;
  60. mStopOnSleep = false;
  61. mMatchVideoSize = true;
  62. mPlayOnWake = true;
  63. mRenderDebugInfo = false;
  64. mTranscoder = OggTheoraDecoder::TRANSCODER_Auto;
  65. mLoop = false;
  66. mBackgroundColor.set( 0, 0, 0, 255);
  67. }
  68. //-----------------------------------------------------------------------------
  69. void GuiTheoraCtrl::initPersistFields()
  70. {
  71. addGroup( "Playback");
  72. addField( "theoraFile", TypeStringFilename, Offset( mFilename, GuiTheoraCtrl ),
  73. "Theora video file to play." );
  74. addField( "backgroundColor", TypeColorI, Offset( mBackgroundColor, GuiTheoraCtrl ),
  75. "Fill color when video is not playing." );
  76. addField( "playOnWake", TypeBool, Offset( mPlayOnWake, GuiTheoraCtrl ),
  77. "Whether to start playing video when control is woken up." );
  78. addField( "loop", TypeBool, Offset( mLoop, GuiTheoraCtrl ),
  79. "Loop playback." );
  80. addField( "stopOnSleep", TypeBool, Offset( mStopOnSleep, GuiTheoraCtrl ),
  81. "Whether to stop video when control is set to sleep.\n\n"
  82. "If this is not set to true, the video will be paused when the control is put to sleep. This is because there is no support "
  83. "for seeking in the video stream in the player backend and letting the time source used to synchronize video (either audio "
  84. "or a raw timer) get far ahead of frame decoding will cause possibly very long delays when the control is woken up again." );
  85. addField( "matchVideoSize", TypeBool, Offset( mMatchVideoSize, GuiTheoraCtrl ),
  86. "Whether to automatically match control extents to the video size." );
  87. addField( "renderDebugInfo", TypeBool, Offset( mRenderDebugInfo, GuiTheoraCtrl ),
  88. "If true, displays an overlay on top of the video with useful debugging information." );
  89. addField( "transcoder", TYPEID< OggTheoraDecoder::ETranscoder >(), Offset( mTranscoder, GuiTheoraCtrl ),
  90. "The routine to use for Y'CbCr to RGB conversion." );
  91. endGroup( "Playback" );
  92. Parent::initPersistFields();
  93. }
  94. //-----------------------------------------------------------------------------
  95. void GuiTheoraCtrl::setFile( const String& filename )
  96. {
  97. mDone = false;
  98. mFilename = filename;
  99. mTheoraTexture.setFile( filename );
  100. if( mMatchVideoSize && mTheoraTexture.isReady() )
  101. setExtent( Point2I( mTheoraTexture.getWidth(), mTheoraTexture.getHeight() ) );
  102. OggTheoraDecoder* decoder = mTheoraTexture._getTheora();
  103. if( decoder != NULL )
  104. decoder->setTranscoder( mTranscoder );
  105. }
  106. //-----------------------------------------------------------------------------
  107. void GuiTheoraCtrl::play()
  108. {
  109. if( mFilename == StringTable->EmptyString() )
  110. return;
  111. if( !mTheoraTexture.isPlaying() )
  112. {
  113. mDone = false;
  114. mTheoraTexture.play();
  115. }
  116. }
  117. //-----------------------------------------------------------------------------
  118. void GuiTheoraCtrl::pause()
  119. {
  120. if( !mTheoraTexture.isPlaying() )
  121. {
  122. Con::errorf( "GuiTheoraCtrl::pause - not playing" );
  123. return;
  124. }
  125. mTheoraTexture.pause();
  126. }
  127. //-----------------------------------------------------------------------------
  128. void GuiTheoraCtrl::stop()
  129. {
  130. mTheoraTexture.stop();
  131. mDone = true;
  132. }
  133. //-----------------------------------------------------------------------------
  134. bool GuiTheoraCtrl::onWake()
  135. {
  136. if( !Parent::onWake() )
  137. return false;
  138. if( !mTheoraTexture.isReady() )
  139. setFile( mFilename );
  140. if( mPlayOnWake && !mTheoraTexture.isPlaying() )
  141. play();
  142. return true;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void GuiTheoraCtrl::onSleep()
  146. {
  147. Parent::onSleep();
  148. if( mTheoraTexture.isPlaying() )
  149. {
  150. if( mStopOnSleep )
  151. stop();
  152. else
  153. pause();
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. void GuiTheoraCtrl::onRender(Point2I offset, const RectI &updateRect)
  158. {
  159. const RectI rect(offset, getBounds().extent);
  160. if( mTheoraTexture.isReady() )
  161. {
  162. mTheoraTexture.refresh();
  163. if( mTheoraTexture.isPlaying()
  164. || mTheoraTexture.isPaused() )
  165. {
  166. // Draw the frame.
  167. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  168. drawUtil->clearBitmapModulation();
  169. drawUtil->drawBitmapStretch( mTheoraTexture.getTexture(), rect );
  170. // Draw frame info, if requested.
  171. if( mRenderDebugInfo )
  172. {
  173. String info = String::ToString( "Frame Number: %i | Frame Time: %.2fs | Playback Time: %.2fs | Dropped: %i",
  174. mTheoraTexture.getFrameNumber(),
  175. mTheoraTexture.getFrameTime(),
  176. F32( mTheoraTexture.getPosition() ) / 1000.f,
  177. mTheoraTexture.getNumDroppedFrames() );
  178. drawUtil->setBitmapModulation( mProfile->mFontColors[ 0 ] );
  179. drawUtil->drawText( mProfile->mFont, localToGlobalCoord( Point2I( 0, 0 ) ), info, mProfile->mFontColors );
  180. }
  181. }
  182. else
  183. {
  184. if(mLoop)
  185. {
  186. play();
  187. } else {
  188. mDone = true;
  189. }
  190. }
  191. }
  192. else
  193. GFX->getDrawUtil()->drawRectFill(rect, mBackgroundColor); // black rect
  194. renderChildControls(offset, updateRect);
  195. }
  196. //-----------------------------------------------------------------------------
  197. void GuiTheoraCtrl::inspectPostApply()
  198. {
  199. if( !mTheoraTexture.getFilename().equal( mFilename, String::NoCase ) )
  200. {
  201. stop();
  202. setFile( mFilename );
  203. if( mPlayOnWake && !mTheoraTexture.isPlaying() )
  204. play();
  205. }
  206. if( mMatchVideoSize && mTheoraTexture.isReady() )
  207. setExtent( Point2I( mTheoraTexture.getWidth(), mTheoraTexture.getHeight() ) );
  208. OggTheoraDecoder* decoder = mTheoraTexture._getTheora();
  209. if( decoder != NULL )
  210. decoder->setTranscoder( mTranscoder );
  211. Parent::inspectPostApply();
  212. }
  213. //=============================================================================
  214. // API.
  215. //=============================================================================
  216. // MARK: ---- API ----
  217. //-----------------------------------------------------------------------------
  218. DefineEngineMethod( GuiTheoraCtrl, setFile, void, ( const char* filename ),,
  219. "Set the video file to play. If a video is already playing, playback is stopped and "
  220. "the new video file is loaded.\n\n"
  221. "@param filename The video file to load." )
  222. {
  223. object->setFile( filename );
  224. }
  225. //-----------------------------------------------------------------------------
  226. DefineEngineMethod( GuiTheoraCtrl, play, void, (),,
  227. "Start playing the video. If the video is already playing, the call is ignored." )
  228. {
  229. object->play();
  230. }
  231. //-----------------------------------------------------------------------------
  232. DefineEngineMethod( GuiTheoraCtrl, pause, void, (),,
  233. "Pause playback of the video. If the video is not currently playing, the call is ignored.\n\n"
  234. "While stopped, the control displays the last frame." )
  235. {
  236. object->pause();
  237. }
  238. //-----------------------------------------------------------------------------
  239. DefineEngineMethod( GuiTheoraCtrl, stop, void, (),,
  240. "Stop playback of the video. The next call to play() will then start playback from the beginning of the video.\n\n"
  241. "While stopped, the control renders empty with just the background color." )
  242. {
  243. object->stop();
  244. }
  245. //-----------------------------------------------------------------------------
  246. DefineEngineMethod( GuiTheoraCtrl, getCurrentTime, F32, (),,
  247. "Get the current playback time.\n\n"
  248. "@return The elapsed playback time in seconds." )
  249. {
  250. return object->getCurrentTime();
  251. }
  252. //-----------------------------------------------------------------------------
  253. DefineEngineMethod( GuiTheoraCtrl, isPlaybackDone, bool, (),,
  254. "Test whether the video has finished playing.\n\n"
  255. "@return True if the video has finished playing, false otherwise." )
  256. {
  257. return object->isPlaybackDone();
  258. }
  259. #endif // TORQUE_OGGTHEORA