]>
Commit | Line | Data |
---|---|---|
1a680109 | 1 | ///////////////////////////////////////////////////////////////////////////// |
987e9419 | 2 | // Name: src/mac/carbon/mediactrl.cpp |
ff4aedc5 | 3 | // Purpose: Built-in Media Backends for Mac |
1a680109 | 4 | // Author: Ryan Norton <wxprojects@comcast.net> |
ff4aedc5 | 5 | // Modified by: |
1a680109 RN |
6 | // Created: 11/07/04 |
7 | // RCS-ID: $Id$ | |
0c81ef7f | 8 | // Copyright: (c) 2004-2006 Ryan Norton |
1a680109 RN |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
bf354396 | 12 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
0c81ef7f SC |
13 | // OK, a casual overseer of this file may wonder why we don't use |
14 | // either CreateMovieControl or HIMovieView... | |
557002cf | 15 | // |
0c81ef7f SC |
16 | // CreateMovieControl |
17 | // 1) Need to dispose and create each time a new movie is loaded | |
18 | // 2) Not that many real advantages | |
19 | // 3) Progressively buggier in higher OSX versions | |
20 | // (see main.c of QTCarbonShell sample for details) | |
21 | // HIMovieView | |
22 | // 1) Crashes on destruction in ALL cases on quite a few systems! | |
23 | // (With the only real "alternative" is to simply not | |
24 | // dispose of it and let it leak...) | |
25 | // 2) Massive refreshing bugs with its movie controller between | |
26 | // movies | |
557002cf | 27 | // |
0c81ef7f SC |
28 | // At one point we had a complete implementation for CreateMovieControl |
29 | // and on my (RN) local copy I had one for HIMovieView - but they | |
30 | // were simply deemed to be too buggy/unuseful. HIMovieView could | |
31 | // have been useful as well because it uses OpenGL contexts instead | |
32 | // of GWorlds. Perhaps someday when someone comes out with some | |
33 | // ingenious workarounds :). | |
bf354396 VZ |
34 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
35 | ||
1a680109 RN |
36 | // For compilers that support precompilation, includes "wx.h". |
37 | #include "wx/wxprec.h" | |
38 | ||
987e9419 WS |
39 | #if wxUSE_MEDIACTRL |
40 | ||
ff4aedc5 | 41 | #include "wx/mediactrl.h" |
1a680109 | 42 | |
987e9419 WS |
43 | #ifndef WX_PRECOMP |
44 | #include "wx/log.h" | |
c0badb70 | 45 | #include "wx/timer.h" |
987e9419 WS |
46 | #endif |
47 | ||
9419b0d6 DS |
48 | // uma is for wxMacFSSpec |
49 | #include "wx/mac/uma.h" | |
9419b0d6 | 50 | |
9419b0d6 | 51 | // standard QT stuff |
0c81ef7f | 52 | #ifndef __DARWIN__ |
9419b0d6 DS |
53 | #include <Movies.h> |
54 | #include <Gestalt.h> | |
55 | #include <QuickTimeComponents.h> | |
56 | #else | |
57 | #include <QuickTime/QuickTimeComponents.h> | |
58 | #endif | |
59 | ||
3b5023b9 | 60 | //--------------------------------------------------------------------------- |
0c81ef7f | 61 | // Height and Width of movie controller in the movie control (apple samples) |
bf354396 VZ |
62 | //--------------------------------------------------------------------------- |
63 | #define wxMCWIDTH 320 | |
64 | #define wxMCHEIGHT 16 | |
65 | ||
ff4aedc5 RN |
66 | //=========================================================================== |
67 | // BACKEND DECLARATIONS | |
68 | //=========================================================================== | |
212945de | 69 | |
ff4aedc5 | 70 | //--------------------------------------------------------------------------- |
ff4aedc5 | 71 | // wxQTMediaBackend |
ff4aedc5 | 72 | //--------------------------------------------------------------------------- |
1a680109 | 73 | |
bf354396 | 74 | class WXDLLIMPEXP_MEDIA wxQTMediaBackend : public wxMediaBackendCommonBase |
ff4aedc5 RN |
75 | { |
76 | public: | |
ff4aedc5 | 77 | wxQTMediaBackend(); |
d3c7fc99 | 78 | virtual ~wxQTMediaBackend(); |
ff4aedc5 | 79 | |
d0ee33f5 | 80 | virtual bool CreateControl(wxControl* ctrl, wxWindow* parent, |
ff4aedc5 | 81 | wxWindowID id, |
d0ee33f5 | 82 | const wxPoint& pos, |
ff4aedc5 | 83 | const wxSize& size, |
d0ee33f5 | 84 | long style, |
ff4aedc5 RN |
85 | const wxValidator& validator, |
86 | const wxString& name); | |
87 | ||
0c81ef7f SC |
88 | virtual bool Load(const wxString& fileName); |
89 | virtual bool Load(const wxURI& location); | |
90 | ||
ff4aedc5 RN |
91 | virtual bool Play(); |
92 | virtual bool Pause(); | |
93 | virtual bool Stop(); | |
94 | ||
ff4aedc5 RN |
95 | virtual wxMediaState GetState(); |
96 | ||
97 | virtual bool SetPosition(wxLongLong where); | |
98 | virtual wxLongLong GetPosition(); | |
99 | virtual wxLongLong GetDuration(); | |
100 | ||
101 | virtual void Move(int x, int y, int w, int h); | |
102 | wxSize GetVideoSize() const; | |
103 | ||
104 | virtual double GetPlaybackRate(); | |
105 | virtual bool SetPlaybackRate(double dRate); | |
106 | ||
c5191fbd VZ |
107 | virtual double GetVolume(); |
108 | virtual bool SetVolume(double); | |
109 | ||
ff4aedc5 RN |
110 | void Cleanup(); |
111 | void FinishLoad(); | |
112 | ||
bf354396 | 113 | virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags); |
9419b0d6 | 114 | |
557002cf VZ |
115 | virtual wxLongLong GetDownloadProgress(); |
116 | virtual wxLongLong GetDownloadTotal(); | |
bf354396 | 117 | |
0c81ef7f SC |
118 | virtual void MacVisibilityChanged(); |
119 | ||
557002cf | 120 | // |
bf354396 | 121 | // ------ Implementation from now on -------- |
557002cf VZ |
122 | // |
123 | bool DoPause(); | |
124 | bool DoStop(); | |
592a247d | 125 | |
bf354396 VZ |
126 | void DoLoadBestSize(); |
127 | void DoSetControllerVisible(wxMediaCtrlPlayerControls flags); | |
128 | ||
557002cf VZ |
129 | wxLongLong GetDataSizeFromStart(TimeValue end); |
130 | ||
0c81ef7f SC |
131 | Boolean IsQuickTime4Installed(); |
132 | void DoNewMovieController(); | |
133 | ||
134 | static pascal void PPRMProc( | |
135 | Movie theMovie, OSErr theErr, void* theRefCon); | |
136 | ||
bf354396 | 137 | //TODO: Last param actually long - does this work on 64bit machines? |
9419b0d6 | 138 | static pascal Boolean MCFilterProc(MovieController theController, |
bf354396 VZ |
139 | short action, void *params, long refCon); |
140 | ||
0c81ef7f SC |
141 | static pascal OSStatus WindowEventHandler( |
142 | EventHandlerCallRef inHandlerCallRef, | |
143 | EventRef inEvent, void *inUserData ); | |
bf354396 VZ |
144 | |
145 | wxSize m_bestSize; // Original movie size | |
bf354396 | 146 | Movie m_movie; // Movie instance |
bf354396 VZ |
147 | bool m_bPlaying; // Whether media is playing or not |
148 | class wxTimer* m_timer; // Timer for streaming the movie | |
149 | MovieController m_mc; // MovieController instance | |
150 | wxMediaCtrlPlayerControls m_interfaceflags; // Saved interface flags | |
9419b0d6 | 151 | |
0c81ef7f | 152 | // Event handlers and UPPs/Callbacks |
987e9419 | 153 | EventHandlerRef m_windowEventHandler; |
0c81ef7f SC |
154 | EventHandlerUPP m_windowUPP; |
155 | ||
557002cf | 156 | MoviePrePrerollCompleteUPP m_preprerollupp; |
557002cf | 157 | MCActionFilterWithRefConUPP m_mcactionupp; |
ff4aedc5 | 158 | |
987e9419 | 159 | GWorldPtr m_movieWorld; //Offscreen movie GWorld |
0c81ef7f | 160 | |
bf354396 | 161 | friend class wxQTMediaEvtHandler; |
9419b0d6 | 162 | |
d0ee33f5 | 163 | DECLARE_DYNAMIC_CLASS(wxQTMediaBackend) |
ff4aedc5 RN |
164 | }; |
165 | ||
bf354396 VZ |
166 | // helper to hijack background erasing for the QT window |
167 | class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler : public wxEvtHandler | |
168 | { | |
169 | public: | |
170 | wxQTMediaEvtHandler(wxQTMediaBackend *qtb) | |
171 | { | |
172 | m_qtb = qtb; | |
173 | ||
9419b0d6 DS |
174 | qtb->m_ctrl->Connect( |
175 | qtb->m_ctrl->GetId(), wxEVT_ERASE_BACKGROUND, | |
bf354396 VZ |
176 | wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground), |
177 | NULL, this); | |
178 | } | |
179 | ||
180 | void OnEraseBackground(wxEraseEvent& event); | |
181 | ||
182 | private: | |
183 | wxQTMediaBackend *m_qtb; | |
184 | ||
185 | DECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler) | |
186 | }; | |
187 | ||
bf354396 VZ |
188 | //=========================================================================== |
189 | // IMPLEMENTATION | |
190 | //=========================================================================== | |
191 | ||
ff4aedc5 RN |
192 | |
193 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
0c81ef7f | 194 | // |
ff4aedc5 | 195 | // wxQTMediaBackend |
0c81ef7f | 196 | // |
ff4aedc5 RN |
197 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
198 | ||
412e0d47 | 199 | IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend, wxMediaBackend) |
1a680109 | 200 | |
bf354396 VZ |
201 | //Time between timer calls - this is the Apple recommondation to the TCL |
202 | //team I believe | |
203 | #define MOVIE_DELAY 20 | |
1a680109 | 204 | |
bf354396 VZ |
205 | //--------------------------------------------------------------------------- |
206 | // wxQTMediaLoadTimer | |
207 | // | |
208 | // QT, esp. QT for Windows is very picky about how you go about | |
209 | // async loading. If you were to go through a Windows message loop | |
210 | // or a MoviesTask or both and then check the movie load state | |
211 | // it would still return 1000 (loading)... even (pre)prerolling doesn't | |
212 | // help. However, making a load timer like this works | |
213 | //--------------------------------------------------------------------------- | |
214 | class wxQTMediaLoadTimer : public wxTimer | |
1a680109 RN |
215 | { |
216 | public: | |
0c81ef7f SC |
217 | wxQTMediaLoadTimer(wxQTMediaBackend* parent) : |
218 | m_parent(parent) {} | |
1a680109 | 219 | |
bf354396 VZ |
220 | void Notify() |
221 | { | |
bf354396 | 222 | ::MCIdle(m_parent->m_mc); |
9419b0d6 DS |
223 | |
224 | // kMovieLoadStatePlayable is not enough on MAC: | |
225 | // it plays, but IsMovieDone might return true (!) | |
226 | // sure we need to wait until kMovieLoadStatePlaythroughOK | |
0c81ef7f | 227 | if (::GetMovieLoadState(m_parent->m_movie) >= 20000) |
9419b0d6 | 228 | { |
bf354396 VZ |
229 | m_parent->FinishLoad(); |
230 | delete this; | |
9419b0d6 | 231 | } |
1a680109 RN |
232 | } |
233 | ||
bf354396 | 234 | protected: |
9419b0d6 | 235 | wxQTMediaBackend *m_parent; // Backend pointer |
bf354396 VZ |
236 | }; |
237 | ||
238 | // -------------------------------------------------------------------------- | |
239 | // wxQTMediaPlayTimer - Handle Asyncronous Playing | |
240 | // | |
241 | // 1) Checks to see if the movie is done, and if not continues | |
242 | // streaming the movie | |
243 | // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of | |
244 | // the movie. | |
245 | // -------------------------------------------------------------------------- | |
246 | class wxQTMediaPlayTimer : public wxTimer | |
247 | { | |
248 | public: | |
0c81ef7f SC |
249 | wxQTMediaPlayTimer(wxQTMediaBackend* parent) : |
250 | m_parent(parent) {} | |
1a680109 RN |
251 | |
252 | void Notify() | |
9419b0d6 | 253 | { |
bf354396 VZ |
254 | // |
255 | // OK, a little explaining - basically originally | |
256 | // we only called MoviesTask if the movie was actually | |
257 | // playing (not paused or stopped)... this was before | |
258 | // we realized MoviesTask actually handles repainting | |
259 | // of the current frame - so if you were to resize | |
260 | // or something it would previously not redraw that | |
261 | // portion of the movie. | |
262 | // | |
263 | // So now we call MoviesTask always so that it repaints | |
264 | // correctly. | |
265 | // | |
266 | ::MCIdle(m_parent->m_mc); | |
ff4aedc5 | 267 | |
0c81ef7f | 268 | // |
bf354396 VZ |
269 | // Handle the stop event - if the movie has reached |
270 | // the end, notify our handler | |
0c81ef7f SC |
271 | // |
272 | if (::IsMovieDone(m_parent->m_movie)) | |
9419b0d6 | 273 | { |
bf354396 VZ |
274 | if ( m_parent->SendStopEvent() ) |
275 | { | |
ff4aedc5 RN |
276 | m_parent->Stop(); |
277 | wxASSERT(::GetMoviesError() == noErr); | |
278 | ||
bf354396 | 279 | m_parent->QueueFinishEvent(); |
1a680109 RN |
280 | } |
281 | } | |
282 | } | |
283 | ||
284 | protected: | |
9419b0d6 | 285 | wxQTMediaBackend* m_parent; // Backend pointer |
1a680109 RN |
286 | }; |
287 | ||
bf354396 | 288 | |
ff4aedc5 | 289 | //--------------------------------------------------------------------------- |
e7b97da3 | 290 | // wxQTMediaBackend Constructor |
ff4aedc5 RN |
291 | // |
292 | // Sets m_timer to NULL signifying we havn't loaded anything yet | |
293 | //--------------------------------------------------------------------------- | |
9419b0d6 | 294 | wxQTMediaBackend::wxQTMediaBackend() |
bf354396 VZ |
295 | : m_movie(NULL), m_bPlaying(false), m_timer(NULL) |
296 | , m_mc(NULL), m_interfaceflags(wxMEDIACTRLPLAYERCONTROLS_NONE) | |
0c81ef7f | 297 | , m_preprerollupp(NULL), m_movieWorld(NULL) |
1a680109 | 298 | { |
1a680109 RN |
299 | } |
300 | ||
ff4aedc5 RN |
301 | //--------------------------------------------------------------------------- |
302 | // wxQTMediaBackend Destructor | |
303 | // | |
304 | // 1) Cleans up the QuickTime movie instance | |
305 | // 2) Decrements the QuickTime reference counter - if this reaches | |
306 | // 0, QuickTime shuts down | |
307 | // 3) Decrements the QuickTime Windows Media Layer reference counter - | |
308 | // if this reaches 0, QuickTime shuts down the Windows Media Layer | |
309 | //--------------------------------------------------------------------------- | |
310 | wxQTMediaBackend::~wxQTMediaBackend() | |
1a680109 | 311 | { |
9419b0d6 | 312 | if (m_movie) |
ff4aedc5 | 313 | Cleanup(); |
1a680109 | 314 | |
bf354396 | 315 | // Cleanup for moviecontroller |
9419b0d6 | 316 | if (m_mc) |
bf354396 VZ |
317 | { |
318 | // destroy wxQTMediaEvtHandler we pushed on it | |
319 | m_ctrl->PopEventHandler(true); | |
0c81ef7f SC |
320 | RemoveEventHandler(m_windowEventHandler); |
321 | DisposeEventHandlerUPP(m_windowUPP); | |
557002cf VZ |
322 | |
323 | // Dispose of the movie controller | |
bf354396 | 324 | ::DisposeMovieController(m_mc); |
0c81ef7f | 325 | m_mc = NULL; |
3e7f4432 | 326 | |
0c81ef7f SC |
327 | // Dispose of offscreen GWorld |
328 | ::DisposeGWorld(m_movieWorld); | |
bf354396 | 329 | } |
bf354396 | 330 | |
9419b0d6 | 331 | // Note that ExitMovies() is not necessary... |
ff4aedc5 | 332 | ExitMovies(); |
1a680109 RN |
333 | } |
334 | ||
ff4aedc5 RN |
335 | //--------------------------------------------------------------------------- |
336 | // wxQTMediaBackend::CreateControl | |
337 | // | |
338 | // 1) Intializes QuickTime | |
339 | // 2) Creates the control window | |
340 | //--------------------------------------------------------------------------- | |
9419b0d6 DS |
341 | bool wxQTMediaBackend::CreateControl( |
342 | wxControl* ctrl, | |
343 | wxWindow* parent, | |
344 | wxWindowID id, | |
345 | const wxPoint& pos, | |
346 | const wxSize& size, | |
347 | long style, | |
348 | const wxValidator& validator, | |
349 | const wxString& name) | |
1a680109 | 350 | { |
bf354396 | 351 | if (!IsQuickTime4Installed()) |
1a680109 | 352 | return false; |
1a680109 | 353 | |
ff4aedc5 RN |
354 | EnterMovies(); |
355 | ||
0c81ef7f SC |
356 | wxMediaCtrl* mediactrl = (wxMediaCtrl*)ctrl; |
357 | ||
358 | // | |
ff4aedc5 RN |
359 | // Create window |
360 | // By default wxWindow(s) is created with a border - | |
361 | // so we need to get rid of those | |
362 | // | |
363 | // Since we don't have a child window like most other | |
364 | // backends, we don't need wxCLIP_CHILDREN | |
0c81ef7f SC |
365 | // |
366 | if ( !mediactrl->wxControl::Create( | |
9419b0d6 DS |
367 | parent, id, pos, size, |
368 | wxWindow::MacRemoveBordersFromStyle(style), | |
369 | validator, name)) | |
370 | { | |
1a680109 | 371 | return false; |
9419b0d6 | 372 | } |
1a680109 | 373 | |
ecd20d4a | 374 | #if wxUSE_VALIDATORS |
0c81ef7f | 375 | mediactrl->SetValidator(validator); |
ecd20d4a RN |
376 | #endif |
377 | ||
0c81ef7f | 378 | m_ctrl = mediactrl; |
1a680109 RN |
379 | return true; |
380 | } | |
381 | ||
bf354396 VZ |
382 | //--------------------------------------------------------------------------- |
383 | // wxQTMediaBackend::IsQuickTime4Installed | |
384 | // | |
9419b0d6 DS |
385 | // Determines whether version 4 of QT is installed |
386 | // (Pretty much for Classic only) | |
bf354396 | 387 | //--------------------------------------------------------------------------- |
bf354396 VZ |
388 | Boolean wxQTMediaBackend::IsQuickTime4Installed() |
389 | { | |
9419b0d6 | 390 | OSErr error; |
bf354396 VZ |
391 | long result; |
392 | ||
9419b0d6 | 393 | error = Gestalt(gestaltQuickTime, &result); |
bf354396 VZ |
394 | return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400); |
395 | } | |
bf354396 | 396 | |
ff4aedc5 RN |
397 | //--------------------------------------------------------------------------- |
398 | // wxQTMediaBackend::Load (file version) | |
399 | // | |
400 | // 1) Get an FSSpec from the Windows path name | |
401 | // 2) Open the movie | |
402 | // 3) Obtain the movie instance from the movie resource | |
3b5023b9 RN |
403 | // 4) Close the movie resource |
404 | // 5) Finish loading | |
ff4aedc5 RN |
405 | //--------------------------------------------------------------------------- |
406 | bool wxQTMediaBackend::Load(const wxString& fileName) | |
1a680109 | 407 | { |
9419b0d6 | 408 | if (m_movie) |
1a680109 RN |
409 | Cleanup(); |
410 | ||
987e9419 | 411 | ::ClearMoviesStickyError(); // clear previous errors so |
0c81ef7f | 412 | // GetMoviesStickyError is useful |
987e9419 | 413 | |
1a680109 RN |
414 | OSErr err = noErr; |
415 | short movieResFile; | |
416 | FSSpec sfFile; | |
d0ee33f5 | 417 | |
9419b0d6 DS |
418 | wxMacFilename2FSSpec( fileName, &sfFile ); |
419 | if (OpenMovieFile( &sfFile, &movieResFile, fsRdPerm ) != noErr) | |
1a680109 RN |
420 | return false; |
421 | ||
422 | short movieResID = 0; | |
423 | Str255 movieName; | |
557002cf | 424 | |
9419b0d6 DS |
425 | err = NewMovieFromFile( |
426 | &m_movie, | |
427 | movieResFile, | |
428 | &movieResID, | |
429 | movieName, | |
430 | newMovieActive, | |
431 | NULL); // wasChanged | |
557002cf | 432 | |
0c81ef7f SC |
433 | // |
434 | // check GetMoviesStickyError() because it may not find the | |
435 | // proper codec and play black video and other strange effects, | |
436 | // not to mention mess up the dynamic backend loading scheme | |
437 | // of wxMediaCtrl - so it just does what the QuickTime player does | |
438 | // | |
439 | if (err == noErr && ::GetMoviesStickyError() == noErr) | |
bf354396 | 440 | { |
9419b0d6 | 441 | ::CloseMovieFile(movieResFile); |
1a680109 | 442 | |
bf354396 | 443 | // Create movie controller/control |
bf354396 | 444 | DoNewMovieController(); |
9419b0d6 DS |
445 | |
446 | FinishLoad(); | |
0c81ef7f | 447 | return true; |
bf354396 | 448 | } |
9419b0d6 | 449 | |
0c81ef7f | 450 | return false; |
bf354396 | 451 | } |
1a680109 | 452 | |
ff4aedc5 | 453 | //--------------------------------------------------------------------------- |
3b5023b9 | 454 | // wxQTMediaBackend::Load (URL Version) |
ff4aedc5 | 455 | // |
3b5023b9 RN |
456 | // 1) Build an escaped URI from location |
457 | // 2) Create a handle to store the URI string | |
458 | // 3) Put the URI string inside the handle | |
459 | // 4) Make a QuickTime URL data ref from the handle with the URI in it | |
460 | // 5) Clean up the URI string handle | |
461 | // 6) Do some prerolling | |
462 | // 7) Finish Loading | |
ff4aedc5 RN |
463 | //--------------------------------------------------------------------------- |
464 | bool wxQTMediaBackend::Load(const wxURI& location) | |
1a680109 | 465 | { |
9419b0d6 | 466 | if (m_movie) |
1a680109 RN |
467 | Cleanup(); |
468 | ||
987e9419 | 469 | ::ClearMoviesStickyError(); // clear previous errors so |
0c81ef7f SC |
470 | // GetMoviesStickyError is useful |
471 | ||
1a680109 | 472 | wxString theURI = location.BuildURI(); |
0c81ef7f | 473 | OSErr err; |
1a680109 | 474 | |
0c81ef7f SC |
475 | size_t len; |
476 | const char* theURIString; | |
477 | ||
478 | #if wxUSE_UNICODE | |
479 | wxCharBuffer buf = wxConvLocal.cWC2MB(theURI, theURI.length(), &len); | |
480 | theURIString = buf; | |
481 | #else | |
482 | theURIString = theURI; | |
483 | len = theURI.length(); | |
484 | #endif | |
1a680109 | 485 | |
0c81ef7f | 486 | Handle theHandle = ::NewHandleClear(len + 1); |
1a680109 RN |
487 | wxASSERT(theHandle); |
488 | ||
0c81ef7f | 489 | ::BlockMoveData(theURIString, *theHandle, len + 1); |
1a680109 | 490 | |
9419b0d6 DS |
491 | // create the movie from the handle that refers to the URI |
492 | err = ::NewMovieFromDataRef( | |
493 | &m_movie, | |
494 | newMovieActive | newMovieAsyncOK /* | newMovieIdleImportOK*/, | |
495 | NULL, theHandle, | |
496 | URLDataHandlerSubType); | |
1a680109 | 497 | |
bf354396 | 498 | ::DisposeHandle(theHandle); |
1a680109 | 499 | |
0c81ef7f | 500 | if (err == noErr && ::GetMoviesStickyError() == noErr) |
bf354396 | 501 | { |
9419b0d6 | 502 | // Movie controller resets prerolling, so we must create first |
bf354396 | 503 | DoNewMovieController(); |
9419b0d6 | 504 | |
bf354396 | 505 | long timeNow; |
9419b0d6 | 506 | Fixed playRate; |
1a680109 | 507 | |
bf354396 VZ |
508 | timeNow = ::GetMovieTime(m_movie, NULL); |
509 | wxASSERT(::GetMoviesError() == noErr); | |
1a680109 | 510 | |
bf354396 VZ |
511 | playRate = ::GetMoviePreferredRate(m_movie); |
512 | wxASSERT(::GetMoviesError() == noErr); | |
513 | ||
0c81ef7f | 514 | // |
bf354396 VZ |
515 | // Note that the callback here is optional, |
516 | // but without it PrePrerollMovie can be buggy | |
517 | // (see Apple ml). Also, some may wonder | |
518 | // why we need this at all - this is because | |
519 | // Apple docs say QuickTime streamed movies | |
520 | // require it if you don't use a Movie Controller, | |
521 | // which we don't by default. | |
522 | // | |
3e7f4432 | 523 | m_preprerollupp = wxQTMediaBackend::PPRMProc; |
987e9419 | 524 | ::PrePrerollMovie( m_movie, timeNow, playRate, |
0c81ef7f | 525 | m_preprerollupp, (void*)this); |
9419b0d6 | 526 | |
0c81ef7f SC |
527 | return true; |
528 | } | |
1a680109 | 529 | |
0c81ef7f | 530 | return false; |
bf354396 | 531 | } |
bf354396 VZ |
532 | |
533 | //--------------------------------------------------------------------------- | |
534 | // wxQTMediaBackend::DoNewMovieController | |
535 | // | |
536 | // Attaches movie to moviecontroller or creates moviecontroller | |
537 | // if not created yet | |
538 | //--------------------------------------------------------------------------- | |
bf354396 VZ |
539 | void wxQTMediaBackend::DoNewMovieController() |
540 | { | |
9419b0d6 | 541 | if (!m_mc) |
bf354396 VZ |
542 | { |
543 | // Get top level window ref for some mac functions | |
544 | WindowRef wrTLW = (WindowRef) m_ctrl->MacGetTopLevelWindowRef(); | |
9419b0d6 DS |
545 | |
546 | // MovieController not set up yet, so we need to create a new one. | |
547 | // You have to pass a valid movie to NewMovieController, evidently | |
bf354396 VZ |
548 | ::SetMovieGWorld(m_movie, |
549 | (CGrafPtr) GetWindowPort(wrTLW), | |
550 | NULL); | |
551 | wxASSERT(::GetMoviesError() == noErr); | |
552 | ||
9419b0d6 DS |
553 | Rect bounds = wxMacGetBoundsForControl( |
554 | m_ctrl, | |
555 | m_ctrl->GetPosition(), | |
556 | m_ctrl->GetSize()); | |
bf354396 | 557 | |
9419b0d6 DS |
558 | m_mc = ::NewMovieController( |
559 | m_movie, &bounds, | |
560 | mcTopLeftMovie | mcNotVisible /* | mcWithFrame */ ); | |
bf354396 | 561 | wxASSERT(::GetMoviesError() == noErr); |
9419b0d6 DS |
562 | |
563 | ::MCDoAction(m_mc, 32, (void*)true); // mcActionSetKeysEnabled | |
bf354396 VZ |
564 | wxASSERT(::GetMoviesError() == noErr); |
565 | ||
566 | // Setup a callback so we can tell when the user presses | |
567 | // play on the player controls | |
3e7f4432 | 568 | m_mcactionupp = wxQTMediaBackend::MCFilterProc; |
9419b0d6 | 569 | ::MCSetActionFilterWithRefCon( m_mc, m_mcactionupp, (long)this ); |
bf354396 VZ |
570 | wxASSERT(::GetMoviesError() == noErr); |
571 | ||
9419b0d6 | 572 | // Part of a suggestion from Greg Hazel to repaint movie when idle |
bf354396 | 573 | m_ctrl->PushEventHandler(new wxQTMediaEvtHandler(this)); |
9419b0d6 | 574 | |
0c81ef7f SC |
575 | // Create offscreen GWorld for where to "show" when window is hidden |
576 | Rect worldRect; | |
577 | worldRect.left = worldRect.top = 0; | |
578 | worldRect.right = worldRect.bottom = 1; | |
579 | ::NewGWorld(&m_movieWorld, 0, &worldRect, NULL, NULL, 0); | |
580 | ||
581 | // Catch window messages: | |
582 | // if we do not do this and if the user clicks the play | |
583 | // button on the controller, for instance, nothing will happen... | |
584 | EventTypeSpec theWindowEventTypes[] = | |
9419b0d6 DS |
585 | { |
586 | { kEventClassMouse, kEventMouseDown }, | |
587 | { kEventClassMouse, kEventMouseUp }, | |
588 | { kEventClassMouse, kEventMouseDragged }, | |
589 | { kEventClassKeyboard, kEventRawKeyDown }, | |
590 | { kEventClassKeyboard, kEventRawKeyRepeat }, | |
591 | { kEventClassKeyboard, kEventRawKeyUp }, | |
592 | { kEventClassWindow, kEventWindowUpdate }, | |
593 | { kEventClassWindow, kEventWindowActivated }, | |
594 | { kEventClassWindow, kEventWindowDeactivated } | |
595 | }; | |
987e9419 | 596 | m_windowUPP = |
0c81ef7f | 597 | NewEventHandlerUPP( wxQTMediaBackend::WindowEventHandler ); |
9419b0d6 DS |
598 | InstallWindowEventHandler( |
599 | wrTLW, | |
0c81ef7f SC |
600 | m_windowUPP, |
601 | GetEventTypeCount( theWindowEventTypes ), theWindowEventTypes, | |
987e9419 | 602 | this, |
0c81ef7f | 603 | &m_windowEventHandler ); |
bf354396 VZ |
604 | } |
605 | else | |
606 | { | |
9419b0d6 | 607 | // MovieController already created: |
bf354396 VZ |
608 | // Just change the movie in it and we're good to go |
609 | Point thePoint; | |
610 | thePoint.h = thePoint.v = 0; | |
9419b0d6 | 611 | ::MCSetMovie(m_mc, m_movie, |
bf354396 | 612 | (WindowRef)m_ctrl->MacGetTopLevelWindowRef(), |
9419b0d6 | 613 | thePoint); |
bf354396 | 614 | wxASSERT(::GetMoviesError() == noErr); |
1a680109 | 615 | } |
bf354396 | 616 | } |
bf354396 VZ |
617 | |
618 | //--------------------------------------------------------------------------- | |
619 | // wxQTMediaBackend::FinishLoad | |
620 | // | |
621 | // Performs operations after a movie ready to play/loaded. | |
622 | //--------------------------------------------------------------------------- | |
623 | void wxQTMediaBackend::FinishLoad() | |
9419b0d6 | 624 | { |
bf354396 VZ |
625 | // get the real size of the movie |
626 | DoLoadBestSize(); | |
627 | ||
9419b0d6 DS |
628 | // show the player controls if the user wants to |
629 | if (m_interfaceflags) | |
bf354396 | 630 | DoSetControllerVisible(m_interfaceflags); |
1a680109 | 631 | |
9419b0d6 | 632 | // we want millisecond precision |
1a680109 | 633 | ::SetMovieTimeScale(m_movie, 1000); |
ff4aedc5 RN |
634 | wxASSERT(::GetMoviesError() == noErr); |
635 | ||
9419b0d6 | 636 | // start movie progress timer |
0c81ef7f | 637 | m_timer = new wxQTMediaPlayTimer(this); |
bf354396 VZ |
638 | wxASSERT(m_timer); |
639 | m_timer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS); | |
640 | ||
9419b0d6 | 641 | // send loaded event and refresh size |
bf354396 VZ |
642 | NotifyMovieLoaded(); |
643 | } | |
644 | ||
645 | //--------------------------------------------------------------------------- | |
646 | // wxQTMediaBackend::DoLoadBestSize | |
647 | // | |
648 | // Sets the best size of the control from the real size of the movie | |
649 | //--------------------------------------------------------------------------- | |
650 | void wxQTMediaBackend::DoLoadBestSize() | |
651 | { | |
9419b0d6 | 652 | // get the real size of the movie |
bf354396 | 653 | Rect outRect; |
9419b0d6 | 654 | ::GetMovieNaturalBoundsRect(m_movie, &outRect); |
bf354396 VZ |
655 | wxASSERT(::GetMoviesError() == noErr); |
656 | ||
9419b0d6 | 657 | // determine best size |
bf354396 VZ |
658 | m_bestSize.x = outRect.right - outRect.left; |
659 | m_bestSize.y = outRect.bottom - outRect.top; | |
1a680109 RN |
660 | } |
661 | ||
ff4aedc5 | 662 | //--------------------------------------------------------------------------- |
e7b97da3 | 663 | // wxQTMediaBackend::Play |
ff4aedc5 | 664 | // |
bf354396 | 665 | // Start the QT movie |
557002cf | 666 | // (Apple recommends mcActionPrerollAndPlay but that's QT 4.1+) |
ff4aedc5 RN |
667 | //--------------------------------------------------------------------------- |
668 | bool wxQTMediaBackend::Play() | |
1a680109 | 669 | { |
bf354396 | 670 | Fixed fixRate = (Fixed) (wxQTMediaBackend::GetPlaybackRate() * 0x10000); |
9419b0d6 | 671 | if (!fixRate) |
bf354396 | 672 | fixRate = ::GetMoviePreferredRate(m_movie); |
9419b0d6 | 673 | |
bf354396 VZ |
674 | wxASSERT(fixRate != 0); |
675 | ||
9419b0d6 DS |
676 | if (!m_bPlaying) |
677 | ::MCDoAction( m_mc, 8 /* mcActionPlay */, (void*) fixRate); | |
557002cf | 678 | |
9419b0d6 DS |
679 | bool result = (::GetMoviesError() == noErr); |
680 | if (result) | |
557002cf | 681 | { |
9419b0d6 | 682 | m_bPlaying = true; |
557002cf | 683 | QueuePlayEvent(); |
557002cf | 684 | } |
9419b0d6 DS |
685 | |
686 | return result; | |
1a680109 RN |
687 | } |
688 | ||
ff4aedc5 | 689 | //--------------------------------------------------------------------------- |
e7b97da3 | 690 | // wxQTMediaBackend::Pause |
ff4aedc5 | 691 | // |
bf354396 | 692 | // Stop the movie |
ff4aedc5 | 693 | //--------------------------------------------------------------------------- |
557002cf | 694 | bool wxQTMediaBackend::DoPause() |
1a680109 | 695 | { |
9419b0d6 DS |
696 | // Stop the movie A.K.A. ::StopMovie(m_movie); |
697 | if (m_bPlaying) | |
bf354396 | 698 | { |
9419b0d6 | 699 | ::MCDoAction( m_mc, 8 /*mcActionPlay*/, (void *) 0); |
bf354396 | 700 | m_bPlaying = false; |
9419b0d6 | 701 | return ::GetMoviesError() == noErr; |
bf354396 | 702 | } |
9419b0d6 DS |
703 | |
704 | // already paused | |
705 | return true; | |
557002cf | 706 | } |
592a247d | 707 | |
557002cf VZ |
708 | bool wxQTMediaBackend::Pause() |
709 | { | |
710 | bool bSuccess = DoPause(); | |
9419b0d6 | 711 | if (bSuccess) |
557002cf | 712 | this->QueuePauseEvent(); |
9419b0d6 DS |
713 | |
714 | return bSuccess; | |
1a680109 RN |
715 | } |
716 | ||
ff4aedc5 | 717 | //--------------------------------------------------------------------------- |
e7b97da3 | 718 | // wxQTMediaBackend::Stop |
ff4aedc5 | 719 | // |
3b5023b9 | 720 | // 1) Stop the movie |
bf354396 | 721 | // 2) Seek to the beginning of the movie |
ff4aedc5 | 722 | //--------------------------------------------------------------------------- |
4ac483eb | 723 | bool wxQTMediaBackend::DoStop() |
1a680109 | 724 | { |
4ac483eb | 725 | if (!wxQTMediaBackend::DoPause()) |
1a680109 | 726 | return false; |
d0ee33f5 | 727 | |
1a680109 RN |
728 | ::GoToBeginningOfMovie(m_movie); |
729 | return ::GetMoviesError() == noErr; | |
730 | } | |
731 | ||
557002cf VZ |
732 | bool wxQTMediaBackend::Stop() |
733 | { | |
734 | bool bSuccess = DoStop(); | |
9419b0d6 | 735 | if (bSuccess) |
557002cf | 736 | QueueStopEvent(); |
9419b0d6 DS |
737 | |
738 | return bSuccess; | |
557002cf VZ |
739 | } |
740 | ||
ff4aedc5 | 741 | //--------------------------------------------------------------------------- |
e7b97da3 | 742 | // wxQTMediaBackend::GetPlaybackRate |
ff4aedc5 | 743 | // |
3b5023b9 | 744 | // 1) Get the movie playback rate from ::GetMovieRate |
ff4aedc5 RN |
745 | //--------------------------------------------------------------------------- |
746 | double wxQTMediaBackend::GetPlaybackRate() | |
1a680109 | 747 | { |
1a680109 RN |
748 | return ( ((double)::GetMovieRate(m_movie)) / 0x10000); |
749 | } | |
750 | ||
ff4aedc5 | 751 | //--------------------------------------------------------------------------- |
e7b97da3 | 752 | // wxQTMediaBackend::SetPlaybackRate |
ff4aedc5 | 753 | // |
3b5023b9 | 754 | // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate |
ff4aedc5 RN |
755 | //--------------------------------------------------------------------------- |
756 | bool wxQTMediaBackend::SetPlaybackRate(double dRate) | |
1a680109 | 757 | { |
1a680109 RN |
758 | ::SetMovieRate(m_movie, (Fixed) (dRate * 0x10000)); |
759 | return ::GetMoviesError() == noErr; | |
760 | } | |
761 | ||
ff4aedc5 | 762 | //--------------------------------------------------------------------------- |
e7b97da3 | 763 | // wxQTMediaBackend::SetPosition |
ff4aedc5 | 764 | // |
3b5023b9 RN |
765 | // 1) Create a time record struct (TimeRecord) with appropriate values |
766 | // 2) Pass struct to SetMovieTime | |
ff4aedc5 RN |
767 | //--------------------------------------------------------------------------- |
768 | bool wxQTMediaBackend::SetPosition(wxLongLong where) | |
1a680109 | 769 | { |
1a680109 RN |
770 | TimeRecord theTimeRecord; |
771 | memset(&theTimeRecord, 0, sizeof(TimeRecord)); | |
ff4aedc5 | 772 | theTimeRecord.value.lo = where.GetValue(); |
1a680109 RN |
773 | theTimeRecord.scale = ::GetMovieTimeScale(m_movie); |
774 | theTimeRecord.base = ::GetMovieTimeBase(m_movie); | |
775 | ::SetMovieTime(m_movie, &theTimeRecord); | |
776 | ||
777 | if (::GetMoviesError() != noErr) | |
778 | return false; | |
779 | ||
780 | return true; | |
781 | } | |
782 | ||
ff4aedc5 | 783 | //--------------------------------------------------------------------------- |
e7b97da3 | 784 | // wxQTMediaBackend::GetPosition |
ff4aedc5 | 785 | // |
3b5023b9 | 786 | // Calls GetMovieTime |
ff4aedc5 RN |
787 | //--------------------------------------------------------------------------- |
788 | wxLongLong wxQTMediaBackend::GetPosition() | |
1a680109 | 789 | { |
1a680109 RN |
790 | return ::GetMovieTime(m_movie, NULL); |
791 | } | |
792 | ||
ff4aedc5 | 793 | //--------------------------------------------------------------------------- |
c5191fbd VZ |
794 | // wxQTMediaBackend::GetVolume |
795 | // | |
796 | // Gets the volume through GetMovieVolume - which returns a 16 bit short - | |
797 | // | |
798 | // +--------+--------+ | |
799 | // + (1) + (2) + | |
800 | // +--------+--------+ | |
801 | // | |
802 | // (1) first 8 bits are value before decimal | |
803 | // (2) second 8 bits are value after decimal | |
804 | // | |
805 | // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to | |
806 | // 1 (full gain and sound) | |
807 | //--------------------------------------------------------------------------- | |
808 | double wxQTMediaBackend::GetVolume() | |
809 | { | |
bf354396 | 810 | short sVolume = ::GetMovieVolume(m_movie); |
c5191fbd | 811 | |
9419b0d6 | 812 | if (sVolume & (128 << 8)) //negative - no sound |
c5191fbd VZ |
813 | return 0.0; |
814 | ||
9419b0d6 | 815 | return sVolume / 256.0; |
c5191fbd VZ |
816 | } |
817 | ||
818 | //--------------------------------------------------------------------------- | |
819 | // wxQTMediaBackend::SetVolume | |
820 | // | |
821 | // Sets the volume through SetMovieVolume - which takes a 16 bit short - | |
822 | // | |
823 | // +--------+--------+ | |
824 | // + (1) + (2) + | |
825 | // +--------+--------+ | |
826 | // | |
827 | // (1) first 8 bits are value before decimal | |
828 | // (2) second 8 bits are value after decimal | |
829 | // | |
830 | // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to | |
831 | // 1 (full gain and sound) | |
832 | //--------------------------------------------------------------------------- | |
833 | bool wxQTMediaBackend::SetVolume(double dVolume) | |
834 | { | |
bf354396 | 835 | ::SetMovieVolume(m_movie, (short) (dVolume * 256)); |
c5191fbd VZ |
836 | return true; |
837 | } | |
9419b0d6 DS |
838 | |
839 | //--------------------------------------------------------------------------- | |
e7b97da3 | 840 | // wxQTMediaBackend::GetDuration |
ff4aedc5 | 841 | // |
3b5023b9 | 842 | // Calls GetMovieDuration |
ff4aedc5 RN |
843 | //--------------------------------------------------------------------------- |
844 | wxLongLong wxQTMediaBackend::GetDuration() | |
1a680109 | 845 | { |
1a680109 RN |
846 | return ::GetMovieDuration(m_movie); |
847 | } | |
848 | ||
ff4aedc5 | 849 | //--------------------------------------------------------------------------- |
e7b97da3 | 850 | // wxQTMediaBackend::GetState |
ff4aedc5 | 851 | // |
3b5023b9 RN |
852 | // Determines the current state - the timer keeps track of whether or not |
853 | // we are paused or stopped (if the timer is running we are playing) | |
ff4aedc5 RN |
854 | //--------------------------------------------------------------------------- |
855 | wxMediaState wxQTMediaBackend::GetState() | |
1a680109 | 856 | { |
bf354396 | 857 | // Could use |
9419b0d6 | 858 | // GetMovieActive/IsMovieDone/SetMovieActive |
bf354396 | 859 | // combo if implemented that way |
9419b0d6 | 860 | if (m_bPlaying) |
1a680109 | 861 | return wxMEDIASTATE_PLAYING; |
0c81ef7f | 862 | else if (!m_movie || wxQTMediaBackend::GetPosition() == 0) |
bf354396 | 863 | return wxMEDIASTATE_STOPPED; |
1a680109 RN |
864 | else |
865 | return wxMEDIASTATE_PAUSED; | |
866 | } | |
867 | ||
ff4aedc5 | 868 | //--------------------------------------------------------------------------- |
e7b97da3 | 869 | // wxQTMediaBackend::Cleanup |
ff4aedc5 | 870 | // |
3b5023b9 RN |
871 | // Diposes of the movie timer, Control if native, and stops and disposes |
872 | // of the QT movie | |
ff4aedc5 RN |
873 | //--------------------------------------------------------------------------- |
874 | void wxQTMediaBackend::Cleanup() | |
1a680109 | 875 | { |
bf354396 | 876 | m_bPlaying = false; |
9419b0d6 | 877 | if (m_timer) |
bf354396 | 878 | { |
9419b0d6 DS |
879 | delete m_timer; |
880 | m_timer = NULL; | |
bf354396 | 881 | } |
d0ee33f5 | 882 | |
9419b0d6 | 883 | // Stop the movie: |
bf354396 VZ |
884 | // Apple samples with CreateMovieControl typically |
885 | // install a event handler and do this on the dispose | |
886 | // event, but we do it here for simplicity | |
887 | // (It might keep playing for several seconds after | |
9419b0d6 | 888 | // control destruction if not) |
bf354396 | 889 | wxQTMediaBackend::Pause(); |
9419b0d6 | 890 | |
bf354396 | 891 | // Dispose of control or remove movie from MovieController |
bf354396 VZ |
892 | Point thePoint; |
893 | thePoint.h = thePoint.v = 0; | |
894 | ::MCSetVisible(m_mc, false); | |
895 | ::MCSetMovie(m_mc, NULL, NULL, thePoint); | |
1a680109 | 896 | |
bf354396 | 897 | ::DisposeMovie(m_movie); |
9419b0d6 | 898 | m_movie = NULL; |
bf354396 VZ |
899 | } |
900 | ||
ff4aedc5 | 901 | //--------------------------------------------------------------------------- |
e7b97da3 | 902 | // wxQTMediaBackend::GetVideoSize |
ff4aedc5 | 903 | // |
3b5023b9 | 904 | // Returns the actual size of the QT movie |
ff4aedc5 RN |
905 | //--------------------------------------------------------------------------- |
906 | wxSize wxQTMediaBackend::GetVideoSize() const | |
1a680109 RN |
907 | { |
908 | return m_bestSize; | |
909 | } | |
910 | ||
ff4aedc5 RN |
911 | //--------------------------------------------------------------------------- |
912 | // wxQTMediaBackend::Move | |
913 | // | |
557002cf VZ |
914 | // Move the movie controller or movie control |
915 | // (we need to actually move the movie control manually...) | |
916 | // Top 10 things to do with quicktime in March 93's issue | |
917 | // of DEVELOP - very useful | |
918 | // http:// www.mactech.com/articles/develop/issue_13/031-033_QuickTime_column.html | |
919 | // OLD NOTE: Calling MCSetControllerBoundsRect without detaching | |
920 | // supposively resulted in a crash back then. Current code even | |
921 | // with CFM classic runs fine. If there is ever a problem, | |
922 | // take out the if 0 lines below | |
ff4aedc5 RN |
923 | //--------------------------------------------------------------------------- |
924 | void wxQTMediaBackend::Move(int x, int y, int w, int h) | |
1a680109 | 925 | { |
9419b0d6 | 926 | if (m_timer) |
1a680109 | 927 | { |
9419b0d6 DS |
928 | m_ctrl->GetParent()->MacWindowToRootWindow(&x, &y); |
929 | Rect theRect = {y, x, y + h, x + w}; | |
930 | ||
0c81ef7f | 931 | #if 0 // see note above |
9419b0d6 DS |
932 | ::MCSetControllerAttached(m_mc, false); |
933 | wxASSERT(::GetMoviesError() == noErr); | |
557002cf | 934 | #endif |
9419b0d6 | 935 | |
bf354396 | 936 | ::MCSetControllerBoundsRect(m_mc, &theRect); |
1a680109 | 937 | wxASSERT(::GetMoviesError() == noErr); |
557002cf VZ |
938 | |
939 | #if 0 // see note above | |
9419b0d6 | 940 | if (m_interfaceflags) |
557002cf | 941 | { |
9419b0d6 DS |
942 | ::MCSetVisible(m_mc, true); |
943 | wxASSERT(::GetMoviesError() == noErr); | |
944 | } | |
557002cf | 945 | #endif |
1a680109 RN |
946 | } |
947 | } | |
948 | ||
bf354396 VZ |
949 | //--------------------------------------------------------------------------- |
950 | // wxQTMediaBackend::DoSetControllerVisible | |
951 | // | |
952 | // Utility function that takes care of showing the moviecontroller | |
953 | // and showing/hiding the particular controls on it | |
954 | //--------------------------------------------------------------------------- | |
0c81ef7f SC |
955 | void wxQTMediaBackend::DoSetControllerVisible( |
956 | wxMediaCtrlPlayerControls flags) | |
9419b0d6 DS |
957 | { |
958 | ::MCSetVisible(m_mc, true); | |
959 | ||
bf354396 | 960 | // Take care of subcontrols |
9419b0d6 | 961 | if (::GetMoviesError() == noErr) |
bf354396 VZ |
962 | { |
963 | long mcFlags = 0; | |
964 | ::MCDoAction(m_mc, 39/*mcActionGetFlags*/, (void*)&mcFlags); | |
9419b0d6 DS |
965 | |
966 | if (::GetMoviesError() == noErr) | |
967 | { | |
bf354396 | 968 | mcFlags |= ( //(1<<0)/*mcFlagSuppressMovieFrame*/ | |
9419b0d6 | 969 | (1 << 3)/*mcFlagsUseWindowPalette*/ |
bf354396 | 970 | | ((flags & wxMEDIACTRLPLAYERCONTROLS_STEP) |
9419b0d6 | 971 | ? 0 : (1 << 1)/*mcFlagSuppressStepButtons*/) |
bf354396 | 972 | | ((flags & wxMEDIACTRLPLAYERCONTROLS_VOLUME) |
9419b0d6 | 973 | ? 0 : (1 << 2)/*mcFlagSuppressSpeakerButton*/) |
0c81ef7f | 974 | //if we take care of repainting ourselves |
987e9419 | 975 | // | (1 << 4) /*mcFlagDontInvalidate*/ |
bf354396 | 976 | ); |
9419b0d6 DS |
977 | |
978 | ::MCDoAction(m_mc, 38/*mcActionSetFlags*/, (void*)mcFlags); | |
bf354396 | 979 | } |
9419b0d6 DS |
980 | } |
981 | ||
982 | // Adjust height and width of best size for movie controller | |
983 | // if the user wants it shown | |
bf354396 VZ |
984 | m_bestSize.x = m_bestSize.x > wxMCWIDTH ? m_bestSize.x : wxMCWIDTH; |
985 | m_bestSize.y += wxMCHEIGHT; | |
986 | } | |
987 | ||
988 | //--------------------------------------------------------------------------- | |
989 | // wxQTMediaBackend::ShowPlayerControls | |
990 | // | |
991 | // Shows/Hides subcontrols on the media control | |
992 | //--------------------------------------------------------------------------- | |
993 | bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags) | |
994 | { | |
9419b0d6 DS |
995 | if (!m_mc) |
996 | return false; // no movie controller... | |
997 | ||
bf354396 | 998 | bool bSizeChanged = false; |
9419b0d6 DS |
999 | |
1000 | // if the controller is visible and we want to hide it do so | |
1001 | if (m_interfaceflags && !flags) | |
bf354396 VZ |
1002 | { |
1003 | bSizeChanged = true; | |
1004 | DoLoadBestSize(); | |
9419b0d6 | 1005 | ::MCSetVisible(m_mc, false); |
bf354396 | 1006 | } |
9419b0d6 | 1007 | else if (!m_interfaceflags && flags) // show controller if hidden |
bf354396 VZ |
1008 | { |
1009 | bSizeChanged = true; | |
1010 | DoSetControllerVisible(flags); | |
1011 | } | |
9419b0d6 DS |
1012 | |
1013 | // readjust parent sizers | |
1014 | if (bSizeChanged) | |
bf354396 | 1015 | { |
9419b0d6 DS |
1016 | NotifyMovieSizeChanged(); |
1017 | ||
1018 | // remember state in case of loading new media | |
bf354396 | 1019 | m_interfaceflags = flags; |
9419b0d6 DS |
1020 | } |
1021 | ||
557002cf VZ |
1022 | return ::GetMoviesError() == noErr; |
1023 | } | |
1024 | ||
1025 | //--------------------------------------------------------------------------- | |
1026 | // wxQTMediaBackend::GetDataSizeFromStart | |
1027 | // | |
1028 | // Calls either GetMovieDataSize or GetMovieDataSize64 with a value | |
1029 | // of 0 for the starting value | |
1030 | //--------------------------------------------------------------------------- | |
1031 | wxLongLong wxQTMediaBackend::GetDataSizeFromStart(TimeValue end) | |
1032 | { | |
1033 | #if 0 // old pre-qt4 way | |
1034 | return ::GetMovieDataSize(m_movie, 0, end) | |
1035 | #else // qt4 way | |
1036 | wide llDataSize; | |
1037 | ::GetMovieDataSize64(m_movie, 0, end, &llDataSize); | |
1038 | return wxLongLong(llDataSize.hi, llDataSize.lo); | |
1039 | #endif | |
1040 | } | |
1041 | ||
1042 | //--------------------------------------------------------------------------- | |
1043 | // wxQTMediaBackend::GetDownloadProgress | |
1044 | //--------------------------------------------------------------------------- | |
1045 | wxLongLong wxQTMediaBackend::GetDownloadProgress() | |
1046 | { | |
1047 | #if 0 // hackish and slow | |
1048 | Handle hMovie = NewHandle(0); | |
1049 | PutMovieIntoHandle(m_movie, hMovie); | |
1050 | long lSize = GetHandleSize(hMovie); | |
1051 | DisposeHandle(hMovie); | |
9419b0d6 | 1052 | |
557002cf VZ |
1053 | return lSize; |
1054 | #else | |
1055 | TimeValue tv; | |
9419b0d6 | 1056 | if (::GetMaxLoadedTimeInMovie(m_movie, &tv) != noErr) |
557002cf VZ |
1057 | { |
1058 | wxLogDebug(wxT("GetMaxLoadedTimeInMovie failed")); | |
1059 | return 0; | |
592a247d | 1060 | } |
9419b0d6 | 1061 | |
557002cf VZ |
1062 | return wxQTMediaBackend::GetDataSizeFromStart(tv); |
1063 | #endif | |
1064 | } | |
592a247d | 1065 | |
557002cf VZ |
1066 | //--------------------------------------------------------------------------- |
1067 | // wxQTMediaBackend::GetDownloadTotal | |
1068 | //--------------------------------------------------------------------------- | |
1069 | wxLongLong wxQTMediaBackend::GetDownloadTotal() | |
1070 | { | |
987e9419 WS |
1071 | return wxQTMediaBackend::GetDataSizeFromStart( |
1072 | ::GetMovieDuration(m_movie) | |
0c81ef7f SC |
1073 | ); |
1074 | } | |
1075 | ||
1076 | //--------------------------------------------------------------------------- | |
1077 | // wxQTMediaBackend::MacVisibilityChanged | |
1078 | // | |
1079 | // The main problem here is that Windows quicktime, for example, | |
1080 | // renders more directly to a HWND. Mac quicktime does not do this | |
1081 | // and instead renders to the port of the WindowRef/WindowPtr on top | |
1082 | // of everything else/all other windows. | |
1083 | // | |
1084 | // So, for example, if you were to have a CreateTabsControl/wxNotebook | |
1085 | // and change pages, even if you called HIViewSetVisible/SetControlVisibility | |
1086 | // directly the movie will still continue playing on top of everything else | |
1087 | // if you went to a different tab. | |
1088 | // | |
1089 | // Note that another issue, and why we call MCSetControllerPort instead | |
1090 | // of SetMovieGWorld directly, is that in addition to rendering on | |
1091 | // top of everything else the last created controller steals mouse and | |
1092 | // other input from everything else in the window, including other | |
1093 | // controllers. Setting the port of it releases this behaviour. | |
1094 | //--------------------------------------------------------------------------- | |
1095 | void wxQTMediaBackend::MacVisibilityChanged() | |
1096 | { | |
1097 | if(!m_mc || !m_ctrl->m_bLoaded) | |
1098 | return; //not initialized yet | |
987e9419 | 1099 | |
0c81ef7f SC |
1100 | if(m_ctrl->MacIsReallyShown()) |
1101 | { | |
1102 | //The window is being shown again, so set the GWorld of the | |
1103 | //controller back to the port of the parent WindowRef | |
987e9419 | 1104 | WindowRef wrTLW = |
0c81ef7f SC |
1105 | (WindowRef) m_ctrl->MacGetTopLevelWindowRef(); |
1106 | ||
1107 | ::MCSetControllerPort(m_mc, (CGrafPtr) GetWindowPort(wrTLW)); | |
1108 | wxASSERT(::GetMoviesError() == noErr); | |
1109 | } | |
1110 | else | |
1111 | { | |
1112 | //We are being hidden - set the GWorld of the controller | |
1113 | //to the offscreen GWorld | |
1114 | ::MCSetControllerPort(m_mc, m_movieWorld); | |
987e9419 | 1115 | wxASSERT(::GetMoviesError() == noErr); |
0c81ef7f | 1116 | } |
bf354396 VZ |
1117 | } |
1118 | ||
1119 | //--------------------------------------------------------------------------- | |
1120 | // wxQTMediaBackend::OnEraseBackground | |
1121 | // | |
1122 | // Suggestion from Greg Hazel to repaint the movie when idle | |
1123 | // (on pause also) | |
1124 | //--------------------------------------------------------------------------- | |
bf354396 VZ |
1125 | void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent& evt) |
1126 | { | |
9419b0d6 | 1127 | // Work around Nasty OSX drawing bug: |
bf354396 | 1128 | // http://lists.apple.com/archives/QuickTime-API/2002/Feb/msg00311.html |
9419b0d6 | 1129 | WindowRef wrTLW = (WindowRef) m_qtb->m_ctrl->MacGetTopLevelWindowRef(); |
bf354396 | 1130 | |
0c81ef7f SC |
1131 | RgnHandle region = ::MCGetControllerBoundsRgn(m_qtb->m_mc); |
1132 | ::MCInvalidate(m_qtb->m_mc, wrTLW, region); | |
1133 | ::MCIdle(m_qtb->m_mc); | |
1134 | } | |
1135 | ||
1136 | //--------------------------------------------------------------------------- | |
1137 | // wxQTMediaBackend::PPRMProc (static) | |
1138 | // | |
1139 | // Called when done PrePrerolling the movie. | |
1140 | // Note that in 99% of the cases this does nothing... | |
1141 | // Anyway we set up the loading timer here to tell us when the movie is done | |
1142 | //--------------------------------------------------------------------------- | |
1143 | pascal void wxQTMediaBackend::PPRMProc( | |
1144 | Movie theMovie, | |
1145 | OSErr WXUNUSED_UNLESS_DEBUG(theErr), | |
1146 | void* theRefCon) | |
1147 | { | |
1148 | wxASSERT( theMovie ); | |
1149 | wxASSERT( theRefCon ); | |
1150 | wxASSERT( theErr == noErr ); | |
1151 | ||
1152 | wxQTMediaBackend* pBE = (wxQTMediaBackend*) theRefCon; | |
1153 | ||
1154 | long lTime = ::GetMovieTime(theMovie,NULL); | |
1155 | Fixed rate = ::GetMoviePreferredRate(theMovie); | |
1156 | ::PrerollMovie(theMovie,lTime,rate); | |
1157 | pBE->m_timer = new wxQTMediaLoadTimer(pBE); | |
1158 | pBE->m_timer->Start(MOVIE_DELAY); | |
bf354396 | 1159 | } |
bf354396 VZ |
1160 | |
1161 | //--------------------------------------------------------------------------- | |
0c81ef7f SC |
1162 | // wxQTMediaBackend::MCFilterProc (static) |
1163 | // | |
1164 | // Callback for when the movie controller recieves a message | |
1165 | //--------------------------------------------------------------------------- | |
1166 | pascal Boolean wxQTMediaBackend::MCFilterProc( | |
1167 | MovieController WXUNUSED(theController), | |
1168 | short action, | |
1169 | void * WXUNUSED(params), | |
1170 | long refCon) | |
1171 | { | |
1172 | wxQTMediaBackend* pThis = (wxQTMediaBackend*)refCon; | |
1173 | ||
1174 | switch (action) | |
1175 | { | |
1176 | case 1: | |
1177 | // don't process idle events | |
1178 | break; | |
1179 | ||
1180 | case 8: | |
1181 | // play button triggered - MC will set movie to opposite state | |
1182 | // of current - playing ? paused : playing | |
1183 | pThis->m_bPlaying = !(pThis->m_bPlaying); | |
1184 | break; | |
1185 | ||
1186 | default: | |
1187 | break; | |
1188 | } | |
1189 | ||
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | //--------------------------------------------------------------------------- | |
1194 | // wxQTMediaBackend::WindowEventHandler [static] | |
bf354396 VZ |
1195 | // |
1196 | // Event callback for the top level window of our control that passes | |
9419b0d6 | 1197 | // messages to our moviecontroller so it can receive mouse clicks etc. |
bf354396 | 1198 | //--------------------------------------------------------------------------- |
0c81ef7f | 1199 | pascal OSStatus wxQTMediaBackend::WindowEventHandler( |
9419b0d6 DS |
1200 | EventHandlerCallRef inHandlerCallRef, |
1201 | EventRef inEvent, | |
1202 | void *inUserData) | |
bf354396 | 1203 | { |
0c81ef7f | 1204 | wxQTMediaBackend* be = (wxQTMediaBackend*) inUserData; |
987e9419 | 1205 | |
0c81ef7f SC |
1206 | // Only process keyboard messages on this window if it actually |
1207 | // has focus, otherwise it will steal keystrokes from other windows! | |
1208 | // As well as when it is not loaded properly as it | |
1209 | // will crash in MCIsPlayerEvent | |
1210 | if((GetEventClass(inEvent) == kEventClassKeyboard && | |
987e9419 | 1211 | wxWindow::FindFocus() != be->m_ctrl) |
0c81ef7f | 1212 | || !be->m_ctrl->m_bLoaded) |
557002cf | 1213 | return eventNotHandledErr; |
9419b0d6 | 1214 | |
0c81ef7f | 1215 | // Pass the event onto the movie controller |
bf354396 VZ |
1216 | EventRecord theEvent; |
1217 | ConvertEventRefToEventRecord( inEvent, &theEvent ); | |
1218 | OSStatus err; | |
592a247d | 1219 | |
987e9419 | 1220 | // TODO: Apple says MCIsPlayerEvent is depreciated and |
0c81ef7f SC |
1221 | // MCClick, MCKey, MCIdle etc. should be used |
1222 | // (RN: Of course that's what they say about | |
1223 | // CreateMovieControl and HIMovieView as well, LOL!) | |
1224 | err = ::MCIsPlayerEvent( be->m_mc, &theEvent ); | |
9419b0d6 | 1225 | |
0c81ef7f | 1226 | // Pass on to other event handlers if not handled- i.e. wx |
9419b0d6 | 1227 | if (err != noErr) |
bf354396 VZ |
1228 | return noErr; |
1229 | else | |
1230 | return eventNotHandledErr; | |
1231 | } | |
ff4aedc5 | 1232 | |
9419b0d6 | 1233 | // in source file that contains stuff you don't directly use |
f572d649 | 1234 | #include "wx/html/forcelnk.h" |
412e0d47 | 1235 | FORCE_LINK_ME(basewxmediabackends) |
ff4aedc5 | 1236 | |
9419b0d6 | 1237 | #endif // wxUSE_MEDIACTRL |