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