mediactrl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: mediactrl.h
  3. // Purpose: interface of wxMediaEvent, wxMediaCtrl
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Describes the current state of the media.
  9. @see wxMediaCtrl::GetState()
  10. */
  11. enum wxMediaState
  12. {
  13. /** No media is being currently played. */
  14. wxMEDIASTATE_STOPPED,
  15. /** Current media is paused. */
  16. wxMEDIASTATE_PAUSED,
  17. /** There is media currently playing. */
  18. wxMEDIASTATE_PLAYING
  19. };
  20. enum wxMediaCtrlPlayerControls
  21. {
  22. /** No controls. return wxMediaCtrl to its default state. */
  23. wxMEDIACTRLPLAYERCONTROLS_NONE = 0,
  24. /** Step controls like fastforward, step one frame etc. */
  25. wxMEDIACTRLPLAYERCONTROLS_STEP = 1 << 0,
  26. /** Volume controls like the speaker icon, volume slider, etc. */
  27. wxMEDIACTRLPLAYERCONTROLS_VOLUME = 1 << 1,
  28. /** Default controls for the toolkit. Currently a combination for
  29. @c wxMEDIACTRLPLAYERCONTROLS_STEP and @c wxMEDIACTRLPLAYERCONTROLS_VOLUME. */
  30. wxMEDIACTRLPLAYERCONTROLS_DEFAULT =
  31. wxMEDIACTRLPLAYERCONTROLS_STEP |
  32. wxMEDIACTRLPLAYERCONTROLS_VOLUME
  33. };
  34. /**
  35. @class wxMediaEvent
  36. Event wxMediaCtrl uses.
  37. @beginEventTable{wxMediaEvent}
  38. @event{EVT_MEDIA_LOADED(id\, func)}
  39. Sent when a media has loaded enough data that it can start playing.
  40. @event{EVT_MEDIA_STOP(id\, func)}
  41. Sent when a media has switched to the @c wxMEDIASTATE_STOPPED state.
  42. You may be able to Veto this event to prevent it from stopping,
  43. causing it to continue playing - even if it has reached that end of
  44. the media (note that this may not have the desired effect - if you
  45. want to loop the media, for example, catch the @c EVT_MEDIA_FINISHED
  46. and play there instead).
  47. @event{EVT_MEDIA_FINISHED(id\, func)}
  48. Sent when a media has finished playing in a wxMediaCtrl.
  49. @event{EVT_MEDIA_STATECHANGED(id\, func)}
  50. Sent when a media has switched its state (from any media state).
  51. @event{EVT_MEDIA_PLAY(id\, func)}
  52. Sent when a media has switched to the @c wxMEDIASTATE_PLAYING state.
  53. @event{EVT_MEDIA_PAUSE(id\, func)}
  54. Sent when a media has switched to the @c wxMEDIASTATE_PAUSED state.
  55. @endEventTable
  56. @library{wxmedia}
  57. @category{events}
  58. */
  59. class wxMediaEvent : public wxNotifyEvent
  60. {
  61. public:
  62. /** Default ctor. */
  63. wxMediaEvent(wxEventType commandType = wxEVT_NULL, int winid = 0);
  64. };
  65. /**
  66. @class wxMediaCtrl
  67. wxMediaCtrl is a class for displaying types of media, such as videos, audio
  68. files, natively through native codecs.
  69. wxMediaCtrl uses native backends to render media, for example on Windows
  70. there is a ActiveMovie/DirectShow backend, and on Macintosh there is a
  71. QuickTime backend.
  72. @section mediactrl_rendering_media Rendering media
  73. Depending upon the backend, wxMediaCtrl can render and display pretty much any
  74. kind of media that the native system can - such as an image, mpeg video, or mp3
  75. (without license restrictions - since it relies on native system calls that may
  76. not technically have mp3 decoding available, for example, it falls outside
  77. the realm of licensing restrictions).
  78. For general operation, all you need to do is call Load() to load the file you
  79. want to render, catch the @c EVT_MEDIA_LOADED event, and then call Play()
  80. to show the video/audio of the media in that event.
  81. More complex operations are generally more heavily dependent on the capabilities
  82. of the backend. For example, QuickTime cannot set the playback rate of certain
  83. streaming media - while DirectShow is slightly more flexible in that regard.
  84. @section mediactrl_operation Operation
  85. When wxMediaCtrl plays a file, it plays until the stop position is reached
  86. (currently the end of the file/stream). Right before it hits the end of the stream,
  87. it fires off a @c EVT_MEDIA_STOP event to its parent window, at which point the event
  88. handler can choose to veto the event, preventing the stream from actually stopping.
  89. Example:
  90. @code
  91. //connect to the media event
  92. this->Connect(wxMY_ID, wxEVT_MEDIA_STOP, (wxObjectEventFunction)
  93. (wxEventFunction)(wxMediaEventFunction) &MyFrame::OnMediaStop);
  94. //...
  95. void MyFrame::OnMediaStop(const wxMediaEvent& evt)
  96. {
  97. if(bUserWantsToSeek)
  98. {
  99. m_mediactrl->SetPosition(
  100. m_mediactrl->GetDuration() << 1
  101. );
  102. evt.Veto();
  103. }
  104. }
  105. @endcode
  106. When wxMediaCtrl stops, either by the @c EVT_MEDIA_STOP not being vetoed, or
  107. by manually calling Stop(), where it actually stops is not at the beginning,
  108. rather, but at the beginning of the stream. That is, when it stops and play
  109. is called, playback is guaranteed to start at the beginning of the media.
  110. This is because some streams are not seekable, and when stop is called on
  111. them they return to the beginning, thus wxMediaCtrl tries to keep consistent
  112. for all types of media.
  113. Note that when changing the state of the media through Play() and other methods,
  114. the media may not actually be in the @c wxMEDIASTATE_PLAYING, for example.
  115. If you are relying on the media being in certain state catch the event relevant
  116. to the state. See wxMediaEvent for the kinds of events that you can catch.
  117. @section mediactrl_video_size Video size
  118. By default, wxMediaCtrl will scale the size of the video to the requested
  119. amount passed to either its constructor or Create().
  120. After calling wxMediaCtrl::Load or performing an equivalent operation,
  121. you can subsequently obtain the "real" size of the video (if there is any)
  122. by calling wxMediaCtrl::GetBestSize(). Note that the actual result on the
  123. display will be slightly different when wxMediaCtrl::ShowPlayerControls is
  124. activated and the actual video size will be less than specified due to the
  125. extra controls provided by the native toolkit.
  126. In addition, the backend may modify wxMediaCtrl::GetBestSize() to include
  127. the size of the extra controls - so if you want the real size of the video
  128. just disable wxMediaCtrl::ShowPlayerControls().
  129. The idea with setting wxMediaCtrl::GetBestSize() to the size of the video is
  130. that GetBestSize() is a wxWindow-derived function that is called when sizers
  131. on a window recalculate.
  132. What this means is that if you use sizers by default the video will show in
  133. its original size without any extra assistance needed from the user.
  134. @section mediactrl_player_controls Player controls
  135. Normally, when you use wxMediaCtrl it is just a window for the video to play in.
  136. However, some toolkits have their own media player interface.
  137. For example, QuickTime generally has a bar below the video with a slider.
  138. A special feature available to wxMediaCtrl, you can use the toolkits interface
  139. instead of making your own by using the ShowPlayerControls() function.
  140. There are several options for the flags parameter, with the two general flags
  141. being @c wxMEDIACTRLPLAYERCONTROLS_NONE which turns off the native interface,
  142. and @c wxMEDIACTRLPLAYERCONTROLS_DEFAULT which lets wxMediaCtrl decide what
  143. native controls on the interface.
  144. Be sure to review the caveats outlined in @ref mediactrl_video_size before doing so.
  145. @section mediactrl_choosing_backend Choosing a backend
  146. Generally, you should almost certainly leave this part up to wxMediaCtrl -
  147. but if you need a certain backend for a particular reason, such as QuickTime
  148. for playing .mov files, all you need to do to choose a specific backend is
  149. to pass the name of the backend class to wxMediaCtrl::Create().
  150. The following are valid backend identifiers:
  151. - @b wxMEDIABACKEND_DIRECTSHOW: Use ActiveMovie/DirectShow.
  152. Uses the native ActiveMovie (I.E. DirectShow) control.
  153. Default backend on Windows and supported by nearly all Windows versions,
  154. even some Windows CE versions.
  155. May display a windows media player logo while inactive.
  156. - @b wxMEDIABACKEND_QUICKTIME: Use QuickTime. Mac Only.
  157. WARNING: May not working correctly embedded in a wxNotebook.
  158. - @b wxMEDIABACKEND_GSTREAMER, Use GStreamer. Unix Only.
  159. Requires GStreamer 0.8 along with at the very least the xvimagesink, xoverlay,
  160. and gst-play modules of gstreamer to function.
  161. You need the correct modules to play the relevant files, for example the
  162. mad module to play mp3s, etc.
  163. - @b wxMEDIABACKEND_WMP10, Uses Windows Media Player 10 (Windows only) -
  164. works on mobile machines with Windows Media Player 10 and desktop machines
  165. with either Windows Media Player 9 or 10.
  166. Note that other backends such as wxMEDIABACKEND_MCI can now be found at
  167. wxCode (http://wxcode.sourceforge.net/).
  168. @section mediactrl_creating_backend Creating a backend
  169. Creating a backend for wxMediaCtrl is a rather simple process.
  170. Simply derive from wxMediaBackendCommonBase and implement the methods you want.
  171. The methods in wxMediaBackend correspond to those in wxMediaCtrl except for
  172. wxMediaCtrl::CreateControl which does the actual creation of the control,
  173. in cases where a custom control is not needed you may simply call wxControl::Create().
  174. You need to make sure to use the @c DECLARE_CLASS and @c IMPLEMENT_CLASS macros.
  175. The only real tricky part is that you need to make sure the file in compiled in,
  176. which if there are just backends in there will not happen and you may need to
  177. use a force link hack (see http://www.wxwidgets.org/wiki/index.php/RTTI).
  178. This is a rather simple example of how to create a backend in the
  179. wxActiveXContainer documentation.
  180. @library{wxmedia}
  181. @category{media}
  182. @see wxMediaEvent
  183. */
  184. class wxMediaCtrl : public wxControl
  185. {
  186. public:
  187. /**
  188. Default constructor - you MUST call Create() before calling any
  189. other methods of wxMediaCtrl.
  190. */
  191. wxMediaCtrl();
  192. /**
  193. Constructor that calls Create().
  194. You may prefer to call Create() directly to check to see if
  195. wxMediaCtrl is available on the system.
  196. @param parent
  197. parent of this control. Must not be @NULL.
  198. @param id
  199. id to use for events
  200. @param fileName
  201. If not empty, the path of a file to open.
  202. @param pos
  203. Position to put control at.
  204. @param size
  205. Size to put the control at and to stretch movie to.
  206. @param style
  207. Optional styles.
  208. @param szBackend
  209. Name of backend you want to use, leave blank to make wxMediaCtrl figure it out.
  210. @param validator
  211. validator to use.
  212. @param name
  213. Window name.
  214. */
  215. wxMediaCtrl(wxWindow* parent, wxWindowID id, const wxString& fileName = wxEmptyString,
  216. const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
  217. long style = 0, const wxString& szBackend = wxEmptyString,
  218. const wxValidator& validator = wxDefaultValidator,
  219. const wxString& name = "mediaCtrl");
  220. /**
  221. Creates this control.
  222. Returns @false if it can't load the movie located at @a fileName
  223. or it cannot load one of its native backends.
  224. If you specify a file to open via @a fileName and you don't specify a
  225. backend to use, wxMediaCtrl tries each of its backends until one that
  226. can render the path referred to by @a fileName can be found.
  227. @param parent
  228. parent of this control. Must not be @NULL.
  229. @param id
  230. id to use for events
  231. @param fileName
  232. If not empty, the path of a file to open.
  233. @param pos
  234. Position to put control at.
  235. @param size
  236. Size to put the control at and to stretch movie to.
  237. @param style
  238. Optional styles.
  239. @param szBackend
  240. Name of backend you want to use, leave blank to make wxMediaCtrl figure it out.
  241. @param validator
  242. validator to use.
  243. @param name
  244. Window name.
  245. */
  246. bool Create(wxWindow* parent, wxWindowID id, const wxString& fileName = wxEmptyString,
  247. const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
  248. long style = 0, const wxString& szBackend = wxEmptyString,
  249. const wxValidator& validator = wxDefaultValidator,
  250. const wxString& name = "mediaCtrl");
  251. /**
  252. Obtains the best size relative to the original/natural size of the
  253. video, if there is any.
  254. See @ref mediactrl_video_size for more information.
  255. */
  256. wxSize GetBestSize() const;
  257. /**
  258. Obtains the playback rate, or speed of the media. @c 1.0 represents normal
  259. speed, while @c 2.0 represents twice the normal speed of the media, for
  260. example. Not supported on the GStreamer (Unix) backend.
  261. @return zero on failure.
  262. */
  263. double GetPlaybackRate();
  264. /**
  265. Obtains the state the playback of the media is in.
  266. @beginTable
  267. @row2col{wxMEDIASTATE_STOPPED, The movie has stopped.}
  268. @row2col{wxMEDIASTATE_PAUSED, The movie is paused.}
  269. @row2col{wxMEDIASTATE_PLAYING, The movie is currently playing.}
  270. @endTable
  271. */
  272. wxMediaState GetState();
  273. /**
  274. Gets the volume of the media from a 0.0 to 1.0 range.
  275. @note Due to rounding and other errors the value returned may not be the
  276. exact value sent to SetVolume().
  277. */
  278. double GetVolume();
  279. /**
  280. Obtains the length - the total amount of time the movie has in milliseconds.
  281. */
  282. wxFileOffset Length();
  283. /**
  284. Loads the file that fileName refers to. Returns @false if loading fails.
  285. */
  286. bool Load(const wxString& fileName);
  287. /**
  288. Loads the location that uri refers to. Note that this is very
  289. implementation-dependent, although HTTP URI/URLs are generally
  290. supported, for example. Returns @false if loading fails.
  291. */
  292. bool Load(const wxURI& uri);
  293. /**
  294. Loads the location that @c uri refers to with the proxy @c proxy.
  295. Not implemented on most backends so it should be called with caution.
  296. Returns @false if loading fails.
  297. */
  298. bool Load(const wxURI& uri, const wxURI& proxy);
  299. /**
  300. Same as Load(const wxURI& uri). Kept for wxPython compatibility.
  301. */
  302. bool LoadURI(const wxString& fileName);
  303. /**
  304. Same as Load(const wxURI& uri, const wxURI& proxy).
  305. Kept for wxPython compatibility.
  306. */
  307. bool LoadURIWithProxy(const wxString& fileName, const wxString& proxy);
  308. /**
  309. Pauses playback of the movie.
  310. */
  311. bool Pause();
  312. /**
  313. Resumes playback of the movie.
  314. */
  315. bool Play();
  316. /**
  317. Seeks to a position within the movie.
  318. @todo Document the wxSeekMode parameter @a mode, and perhaps also the
  319. wxFileOffset and wxSeekMode themselves.
  320. */
  321. wxFileOffset Seek(wxFileOffset where, wxSeekMode mode = wxFromStart);
  322. /**
  323. Sets the playback rate, or speed of the media, to that referred by @a dRate.
  324. @c 1.0 represents normal speed, while @c 2.0 represents twice the normal
  325. speed of the media, for example. Not supported on the GStreamer (Unix) backend.
  326. Returns @true if successful.
  327. */
  328. bool SetPlaybackRate(double dRate);
  329. /**
  330. Sets the volume of the media from a 0.0 to 1.0 range to that referred
  331. by @c dVolume. @c 1.0 represents full volume, while @c 0.5
  332. represents half (50 percent) volume, for example.
  333. @note The volume may not be exact due to conversion and rounding errors,
  334. although setting the volume to full or none is always exact.
  335. Returns @true if successful.
  336. */
  337. bool SetVolume(double dVolume);
  338. /**
  339. A special feature to wxMediaCtrl. Applications using native toolkits such as
  340. QuickTime usually have a scrollbar, play button, and more provided to
  341. them by the toolkit. By default wxMediaCtrl does not do this. However, on
  342. the directshow and quicktime backends you can show or hide the native controls
  343. provided by the underlying toolkit at will using ShowPlayerControls(). Simply
  344. calling the function with default parameters tells wxMediaCtrl to use the
  345. default controls provided by the toolkit. The function takes a
  346. wxMediaCtrlPlayerControls enumeration, please see available show modes there.
  347. For more info see @ref mediactrl_player_controls.
  348. Currently only implemented on the QuickTime and DirectShow backends.
  349. The function returns @true on success.
  350. */
  351. bool ShowPlayerControls(wxMediaCtrlPlayerControls flags = wxMEDIACTRLPLAYERCONTROLS_DEFAULT);
  352. /**
  353. Stops the media.
  354. See @ref mediactrl_operation for an overview of how stopping works.
  355. */
  356. bool Stop();
  357. /**
  358. Obtains the current position in time within the movie in milliseconds.
  359. */
  360. wxFileOffset Tell();
  361. };