1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/mediactrl.cpp
3 // Purpose: Built-in Media Backends for Mac
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) 2004-2006 Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 // OK, a casual overseer of this file may wonder why we don't use
14 // either CreateMovieControl or HIMovieView...
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)
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
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 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 // For compilers that support precompilation, includes "wx.h".
37 #include "wx/wxprec.h"
41 #include "wx/mediactrl.h"
49 #define USE_QUICKTIME 1
51 #define USE_QUICKTIME 0
56 #include "wx/osx/private.h"
57 #include <QuickTime/QuickTimeComponents.h>
59 //---------------------------------------------------------------------------
60 // Height and Width of movie controller in the movie control (apple samples)
61 //---------------------------------------------------------------------------
65 //===========================================================================
66 // BACKEND DECLARATIONS
67 //===========================================================================
69 //---------------------------------------------------------------------------
71 //---------------------------------------------------------------------------
73 class WXDLLIMPEXP_MEDIA wxQTMediaBackend
: public wxMediaBackendCommonBase
77 virtual ~wxQTMediaBackend();
79 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
84 const wxValidator
& validator
,
85 const wxString
& name
);
87 virtual bool Load(const wxString
& fileName
);
88 virtual bool Load(const wxURI
& location
);
94 virtual wxMediaState
GetState();
96 virtual bool SetPosition(wxLongLong where
);
97 virtual wxLongLong
GetPosition();
98 virtual wxLongLong
GetDuration();
100 virtual void Move(int x
, int y
, int w
, int h
);
101 wxSize
GetVideoSize() const;
103 virtual double GetPlaybackRate();
104 virtual bool SetPlaybackRate(double dRate
);
106 virtual double GetVolume();
107 virtual bool SetVolume(double);
112 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags
);
114 virtual wxLongLong
GetDownloadProgress();
115 virtual wxLongLong
GetDownloadTotal();
117 virtual void MacVisibilityChanged();
120 // ------ Implementation from now on --------
125 void DoLoadBestSize();
126 void DoSetControllerVisible(wxMediaCtrlPlayerControls flags
);
128 wxLongLong
GetDataSizeFromStart(TimeValue end
);
130 Boolean
IsQuickTime4Installed();
131 void DoNewMovieController();
133 static pascal void PPRMProc(
134 Movie theMovie
, OSErr theErr
, void* theRefCon
);
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
);
140 static pascal OSStatus
WindowEventHandler(
141 EventHandlerCallRef inHandlerCallRef
,
142 EventRef inEvent
, void *inUserData
);
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
151 // Event handlers and UPPs/Callbacks
152 EventHandlerRef m_windowEventHandler
;
153 EventHandlerUPP m_windowUPP
;
155 MoviePrePrerollCompleteUPP m_preprerollupp
;
156 MCActionFilterWithRefConUPP m_mcactionupp
;
158 GWorldPtr m_movieWorld
; //Offscreen movie GWorld
160 friend class wxQTMediaEvtHandler
;
162 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
)
165 // helper to hijack background erasing for the QT window
166 class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler
: public wxEvtHandler
169 wxQTMediaEvtHandler(wxQTMediaBackend
*qtb
)
173 qtb
->m_ctrl
->Connect(
174 qtb
->m_ctrl
->GetId(), wxEVT_ERASE_BACKGROUND
,
175 wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground
),
179 void OnEraseBackground(wxEraseEvent
& event
);
182 wxQTMediaBackend
*m_qtb
;
184 wxDECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler
);
187 //===========================================================================
189 //===========================================================================
192 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
196 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
198 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
)
200 //Time between timer calls - this is the Apple recommondation to the TCL
202 #define MOVIE_DELAY 20
204 //---------------------------------------------------------------------------
205 // wxQTMediaLoadTimer
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
216 wxQTMediaLoadTimer(wxQTMediaBackend
* parent
) :
221 ::MCIdle(m_parent
->m_mc
);
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)
228 m_parent
->FinishLoad();
234 wxQTMediaBackend
*m_parent
; // Backend pointer
237 // --------------------------------------------------------------------------
238 // wxQTMediaPlayTimer - Handle Asyncronous Playing
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
244 // --------------------------------------------------------------------------
245 class wxQTMediaPlayTimer
: public wxTimer
248 wxQTMediaPlayTimer(wxQTMediaBackend
* parent
) :
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.
262 // So now we call MoviesTask always so that it repaints
265 ::MCIdle(m_parent
->m_mc
);
268 // Handle the stop event - if the movie has reached
269 // the end, notify our handler
271 if (::IsMovieDone(m_parent
->m_movie
))
273 if ( m_parent
->SendStopEvent() )
276 wxASSERT(::GetMoviesError() == noErr
);
278 m_parent
->QueueFinishEvent();
284 wxQTMediaBackend
* m_parent
; // Backend pointer
288 //---------------------------------------------------------------------------
289 // wxQTMediaBackend Constructor
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
)
300 //---------------------------------------------------------------------------
301 // wxQTMediaBackend Destructor
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()
314 // Cleanup for moviecontroller
317 // destroy wxQTMediaEvtHandler we pushed on it
318 m_ctrl
->PopEventHandler(true);
319 RemoveEventHandler(m_windowEventHandler
);
320 DisposeEventHandlerUPP(m_windowUPP
);
322 // Dispose of the movie controller
323 ::DisposeMovieController(m_mc
);
326 // Dispose of offscreen GWorld
327 ::DisposeGWorld(m_movieWorld
);
330 // Note that ExitMovies() is not necessary...
334 //---------------------------------------------------------------------------
335 // wxQTMediaBackend::CreateControl
337 // 1) Intializes QuickTime
338 // 2) Creates the control window
339 //---------------------------------------------------------------------------
340 bool wxQTMediaBackend::CreateControl(
347 const wxValidator
& validator
,
348 const wxString
& name
)
350 if (!IsQuickTime4Installed())
355 wxMediaCtrl
* mediactrl
= (wxMediaCtrl
*)ctrl
;
359 // By default wxWindow(s) is created with a border -
360 // so we need to get rid of those
362 // Since we don't have a child window like most other
363 // backends, we don't need wxCLIP_CHILDREN
365 if ( !mediactrl
->wxControl::Create(
366 parent
, id
, pos
, size
,
367 wxWindow::MacRemoveBordersFromStyle(style
),
374 mediactrl
->SetValidator(validator
);
381 //---------------------------------------------------------------------------
382 // wxQTMediaBackend::IsQuickTime4Installed
384 // Determines whether version 4 of QT is installed
385 // (Pretty much for Classic only)
386 //---------------------------------------------------------------------------
387 Boolean
wxQTMediaBackend::IsQuickTime4Installed()
392 error
= Gestalt(gestaltQuickTime
, &result
);
393 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
396 //---------------------------------------------------------------------------
397 // wxQTMediaBackend::Load (file version)
399 // 1) Get an FSSpec from the Windows path name
401 // 3) Obtain the movie instance from the movie resource
402 // 4) Close the movie resource
404 //---------------------------------------------------------------------------
405 bool wxQTMediaBackend::Load(const wxString
& fileName
)
410 ::ClearMoviesStickyError(); // clear previous errors so
411 // GetMoviesStickyError is useful
417 wxMacFilename2FSSpec( fileName
, &sfFile
);
418 if (OpenMovieFile( &sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
421 short movieResID
= 0;
424 err
= NewMovieFromFile(
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
)
436 ::CloseMovieFile(movieResFile
);
438 // Create movie controller/control
439 DoNewMovieController();
448 //---------------------------------------------------------------------------
449 // wxQTMediaBackend::Load (URL Version)
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
458 //---------------------------------------------------------------------------
459 bool wxQTMediaBackend::Load(const wxURI
& location
)
464 ::ClearMoviesStickyError(); // clear previous errors so
465 // GetMoviesStickyError is useful
467 wxString theURI
= location
.BuildURI();
471 const char* theURIString
;
474 wxCharBuffer buf
= wxConvLocal
.cWC2MB(theURI
.wc_str(), theURI
.length(), &len
);
477 theURIString
= theURI
;
478 len
= theURI
.length();
481 Handle theHandle
= ::NewHandleClear(len
+ 1);
484 ::BlockMoveData(theURIString
, *theHandle
, len
+ 1);
486 // create the movie from the handle that refers to the URI
487 err
= ::NewMovieFromDataRef(
489 newMovieActive
| newMovieAsyncOK
/* | newMovieIdleImportOK*/,
491 URLDataHandlerSubType
);
493 ::DisposeHandle(theHandle
);
495 if (err
== noErr
&& ::GetMoviesStickyError() == noErr
)
497 // Movie controller resets prerolling, so we must create first
498 DoNewMovieController();
503 timeNow
= ::GetMovieTime(m_movie
, NULL
);
504 wxASSERT(::GetMoviesError() == noErr
);
506 playRate
= ::GetMoviePreferredRate(m_movie
);
507 wxASSERT(::GetMoviesError() == noErr
);
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.
518 m_preprerollupp
= wxQTMediaBackend::PPRMProc
;
519 ::PrePrerollMovie( m_movie
, timeNow
, playRate
,
520 m_preprerollupp
, (void*)this);
528 //---------------------------------------------------------------------------
529 // wxQTMediaBackend::DoNewMovieController
531 // Attaches movie to moviecontroller or creates moviecontroller
532 // if not created yet
533 //---------------------------------------------------------------------------
534 void wxQTMediaBackend::DoNewMovieController()
538 // Get top level window ref for some mac functions
539 WindowRef wrTLW
= (WindowRef
) m_ctrl
->MacGetTopLevelWindowRef();
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
),
546 wxASSERT(::GetMoviesError() == noErr
);
548 Rect bounds
= wxMacGetBoundsForControl(
550 m_ctrl
->GetPosition(),
553 m_mc
= ::NewMovieController(
555 mcTopLeftMovie
| mcNotVisible
/* | mcWithFrame */ );
556 wxASSERT(::GetMoviesError() == noErr
);
558 ::MCDoAction(m_mc
, 32, (void*)true); // mcActionSetKeysEnabled
559 wxASSERT(::GetMoviesError() == noErr
);
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
);
567 // Part of a suggestion from Greg Hazel to repaint movie when idle
568 m_ctrl
->PushEventHandler(new wxQTMediaEvtHandler(this));
570 // Create offscreen GWorld for where to "show" when window is hidden
572 worldRect
.left
= worldRect
.top
= 0;
573 worldRect
.right
= worldRect
.bottom
= 1;
574 ::NewGWorld(&m_movieWorld
, 0, &worldRect
, NULL
, NULL
, 0);
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
[] =
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
}
592 NewEventHandlerUPP( wxQTMediaBackend::WindowEventHandler
);
593 InstallWindowEventHandler(
596 GetEventTypeCount( theWindowEventTypes
), theWindowEventTypes
,
598 &m_windowEventHandler
);
602 // MovieController already created:
603 // Just change the movie in it and we're good to go
605 thePoint
.h
= thePoint
.v
= 0;
606 ::MCSetMovie(m_mc
, m_movie
,
607 (WindowRef
)m_ctrl
->MacGetTopLevelWindowRef(),
609 wxASSERT(::GetMoviesError() == noErr
);
613 //---------------------------------------------------------------------------
614 // wxQTMediaBackend::FinishLoad
616 // Performs operations after a movie ready to play/loaded.
617 //---------------------------------------------------------------------------
618 void wxQTMediaBackend::FinishLoad()
620 // get the real size of the movie
623 // show the player controls if the user wants to
624 if (m_interfaceflags
)
625 DoSetControllerVisible(m_interfaceflags
);
627 // we want millisecond precision
628 ::SetMovieTimeScale(m_movie
, 1000);
629 wxASSERT(::GetMoviesError() == noErr
);
631 // start movie progress timer
632 m_timer
= new wxQTMediaPlayTimer(this);
634 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
636 // send loaded event and refresh size
640 //---------------------------------------------------------------------------
641 // wxQTMediaBackend::DoLoadBestSize
643 // Sets the best size of the control from the real size of the movie
644 //---------------------------------------------------------------------------
645 void wxQTMediaBackend::DoLoadBestSize()
647 // get the real size of the movie
649 ::GetMovieNaturalBoundsRect(m_movie
, &outRect
);
650 wxASSERT(::GetMoviesError() == noErr
);
652 // determine best size
653 m_bestSize
.x
= outRect
.right
- outRect
.left
;
654 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
657 //---------------------------------------------------------------------------
658 // wxQTMediaBackend::Play
660 // Start the QT movie
661 // (Apple recommends mcActionPrerollAndPlay but that's QT 4.1+)
662 //---------------------------------------------------------------------------
663 bool wxQTMediaBackend::Play()
665 Fixed fixRate
= (Fixed
) (wxQTMediaBackend::GetPlaybackRate() * 0x10000);
667 fixRate
= ::GetMoviePreferredRate(m_movie
);
669 wxASSERT(fixRate
!= 0);
672 ::MCDoAction( m_mc
, 8 /* mcActionPlay */, (void*) fixRate
);
674 bool result
= (::GetMoviesError() == noErr
);
684 //---------------------------------------------------------------------------
685 // wxQTMediaBackend::Pause
688 //---------------------------------------------------------------------------
689 bool wxQTMediaBackend::DoPause()
691 // Stop the movie A.K.A. ::StopMovie(m_movie);
694 ::MCDoAction( m_mc
, 8 /*mcActionPlay*/, (void *) 0);
696 return ::GetMoviesError() == noErr
;
703 bool wxQTMediaBackend::Pause()
705 bool bSuccess
= DoPause();
707 this->QueuePauseEvent();
712 //---------------------------------------------------------------------------
713 // wxQTMediaBackend::Stop
716 // 2) Seek to the beginning of the movie
717 //---------------------------------------------------------------------------
718 bool wxQTMediaBackend::DoStop()
720 if (!wxQTMediaBackend::DoPause())
723 ::GoToBeginningOfMovie(m_movie
);
724 return ::GetMoviesError() == noErr
;
727 bool wxQTMediaBackend::Stop()
729 bool bSuccess
= DoStop();
736 //---------------------------------------------------------------------------
737 // wxQTMediaBackend::GetPlaybackRate
739 // 1) Get the movie playback rate from ::GetMovieRate
740 //---------------------------------------------------------------------------
741 double wxQTMediaBackend::GetPlaybackRate()
743 return ( ((double)::GetMovieRate(m_movie
)) / 0x10000);
746 //---------------------------------------------------------------------------
747 // wxQTMediaBackend::SetPlaybackRate
749 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
750 //---------------------------------------------------------------------------
751 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
753 ::SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
754 return ::GetMoviesError() == noErr
;
757 //---------------------------------------------------------------------------
758 // wxQTMediaBackend::SetPosition
760 // 1) Create a time record struct (TimeRecord) with appropriate values
761 // 2) Pass struct to SetMovieTime
762 //---------------------------------------------------------------------------
763 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
765 TimeRecord theTimeRecord
;
766 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
767 theTimeRecord
.value
.lo
= where
.GetLo();
768 theTimeRecord
.value
.hi
= where
.GetHi();
769 theTimeRecord
.scale
= ::GetMovieTimeScale(m_movie
);
770 theTimeRecord
.base
= ::GetMovieTimeBase(m_movie
);
771 ::SetMovieTime(m_movie
, &theTimeRecord
);
773 if (::GetMoviesError() != noErr
)
779 //---------------------------------------------------------------------------
780 // wxQTMediaBackend::GetPosition
782 // Calls GetMovieTime
783 //---------------------------------------------------------------------------
784 wxLongLong
wxQTMediaBackend::GetPosition()
786 return ::GetMovieTime(m_movie
, NULL
);
789 //---------------------------------------------------------------------------
790 // wxQTMediaBackend::GetVolume
792 // Gets the volume through GetMovieVolume - which returns a 16 bit short -
794 // +--------+--------+
796 // +--------+--------+
798 // (1) first 8 bits are value before decimal
799 // (2) second 8 bits are value after decimal
801 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
802 // 1 (full gain and sound)
803 //---------------------------------------------------------------------------
804 double wxQTMediaBackend::GetVolume()
806 short sVolume
= ::GetMovieVolume(m_movie
);
808 if (sVolume
& (128 << 8)) //negative - no sound
811 return sVolume
/ 256.0;
814 //---------------------------------------------------------------------------
815 // wxQTMediaBackend::SetVolume
817 // Sets the volume through SetMovieVolume - which takes a 16 bit short -
819 // +--------+--------+
821 // +--------+--------+
823 // (1) first 8 bits are value before decimal
824 // (2) second 8 bits are value after decimal
826 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
827 // 1 (full gain and sound)
828 //---------------------------------------------------------------------------
829 bool wxQTMediaBackend::SetVolume(double dVolume
)
831 ::SetMovieVolume(m_movie
, (short) (dVolume
* 256));
835 //---------------------------------------------------------------------------
836 // wxQTMediaBackend::GetDuration
838 // Calls GetMovieDuration
839 //---------------------------------------------------------------------------
840 wxLongLong
wxQTMediaBackend::GetDuration()
842 return ::GetMovieDuration(m_movie
);
845 //---------------------------------------------------------------------------
846 // wxQTMediaBackend::GetState
848 // Determines the current state - the timer keeps track of whether or not
849 // we are paused or stopped (if the timer is running we are playing)
850 //---------------------------------------------------------------------------
851 wxMediaState
wxQTMediaBackend::GetState()
854 // GetMovieActive/IsMovieDone/SetMovieActive
855 // combo if implemented that way
857 return wxMEDIASTATE_PLAYING
;
858 else if (!m_movie
|| wxQTMediaBackend::GetPosition() == 0)
859 return wxMEDIASTATE_STOPPED
;
861 return wxMEDIASTATE_PAUSED
;
864 //---------------------------------------------------------------------------
865 // wxQTMediaBackend::Cleanup
867 // Diposes of the movie timer, Control if native, and stops and disposes
869 //---------------------------------------------------------------------------
870 void wxQTMediaBackend::Cleanup()
880 // Apple samples with CreateMovieControl typically
881 // install a event handler and do this on the dispose
882 // event, but we do it here for simplicity
883 // (It might keep playing for several seconds after
884 // control destruction if not)
885 wxQTMediaBackend::Pause();
887 // Dispose of control or remove movie from MovieController
889 thePoint
.h
= thePoint
.v
= 0;
890 ::MCSetVisible(m_mc
, false);
891 ::MCSetMovie(m_mc
, NULL
, NULL
, thePoint
);
893 ::DisposeMovie(m_movie
);
897 //---------------------------------------------------------------------------
898 // wxQTMediaBackend::GetVideoSize
900 // Returns the actual size of the QT movie
901 //---------------------------------------------------------------------------
902 wxSize
wxQTMediaBackend::GetVideoSize() const
907 //---------------------------------------------------------------------------
908 // wxQTMediaBackend::Move
910 // Move the movie controller or movie control
911 // (we need to actually move the movie control manually...)
912 // Top 10 things to do with quicktime in March 93's issue
913 // of DEVELOP - very useful
914 // http:// www.mactech.com/articles/develop/issue_13/031-033_QuickTime_column.html
915 // OLD NOTE: Calling MCSetControllerBoundsRect without detaching
916 // supposively resulted in a crash back then. Current code even
917 // with CFM classic runs fine. If there is ever a problem,
918 // take out the if 0 lines below
919 //---------------------------------------------------------------------------
920 void wxQTMediaBackend::Move(int x
, int y
, int w
, int h
)
924 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
925 Rect theRect
= {y
, x
, y
+ h
, x
+ w
};
927 #if 0 // see note above
928 ::MCSetControllerAttached(m_mc
, false);
929 wxASSERT(::GetMoviesError() == noErr
);
932 ::MCSetControllerBoundsRect(m_mc
, &theRect
);
933 wxASSERT(::GetMoviesError() == noErr
);
935 #if 0 // see note above
936 if (m_interfaceflags
)
938 ::MCSetVisible(m_mc
, true);
939 wxASSERT(::GetMoviesError() == noErr
);
945 //---------------------------------------------------------------------------
946 // wxQTMediaBackend::DoSetControllerVisible
948 // Utility function that takes care of showing the moviecontroller
949 // and showing/hiding the particular controls on it
950 //---------------------------------------------------------------------------
951 void wxQTMediaBackend::DoSetControllerVisible(
952 wxMediaCtrlPlayerControls flags
)
954 ::MCSetVisible(m_mc
, true);
956 // Take care of subcontrols
957 if (::GetMoviesError() == noErr
)
960 ::MCDoAction(m_mc
, 39/*mcActionGetFlags*/, (void*)&mcFlags
);
962 if (::GetMoviesError() == noErr
)
964 mcFlags
|= ( //(1<<0)/*mcFlagSuppressMovieFrame*/ |
965 (1 << 3)/*mcFlagsUseWindowPalette*/
966 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_STEP
)
967 ? 0 : (1 << 1)/*mcFlagSuppressStepButtons*/)
968 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_VOLUME
)
969 ? 0 : (1 << 2)/*mcFlagSuppressSpeakerButton*/)
970 //if we take care of repainting ourselves
971 // | (1 << 4) /*mcFlagDontInvalidate*/
974 ::MCDoAction(m_mc
, 38/*mcActionSetFlags*/, (void*)mcFlags
);
978 // Adjust height and width of best size for movie controller
979 // if the user wants it shown
980 m_bestSize
.x
= m_bestSize
.x
> wxMCWIDTH
? m_bestSize
.x
: wxMCWIDTH
;
981 m_bestSize
.y
+= wxMCHEIGHT
;
984 //---------------------------------------------------------------------------
985 // wxQTMediaBackend::ShowPlayerControls
987 // Shows/Hides subcontrols on the media control
988 //---------------------------------------------------------------------------
989 bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags
)
992 return false; // no movie controller...
994 bool bSizeChanged
= false;
996 // if the controller is visible and we want to hide it do so
997 if (m_interfaceflags
&& !flags
)
1001 ::MCSetVisible(m_mc
, false);
1003 else if (!m_interfaceflags
&& flags
) // show controller if hidden
1005 bSizeChanged
= true;
1006 DoSetControllerVisible(flags
);
1009 // readjust parent sizers
1012 NotifyMovieSizeChanged();
1014 // remember state in case of loading new media
1015 m_interfaceflags
= flags
;
1018 return ::GetMoviesError() == noErr
;
1021 //---------------------------------------------------------------------------
1022 // wxQTMediaBackend::GetDataSizeFromStart
1024 // Calls either GetMovieDataSize or GetMovieDataSize64 with a value
1025 // of 0 for the starting value
1026 //---------------------------------------------------------------------------
1027 wxLongLong
wxQTMediaBackend::GetDataSizeFromStart(TimeValue end
)
1029 #if 0 // old pre-qt4 way
1030 return ::GetMovieDataSize(m_movie
, 0, end
)
1033 ::GetMovieDataSize64(m_movie
, 0, end
, &llDataSize
);
1034 return wxLongLong(llDataSize
.hi
, llDataSize
.lo
);
1038 //---------------------------------------------------------------------------
1039 // wxQTMediaBackend::GetDownloadProgress
1040 //---------------------------------------------------------------------------
1041 wxLongLong
wxQTMediaBackend::GetDownloadProgress()
1043 #if 0 // hackish and slow
1044 Handle hMovie
= NewHandle(0);
1045 PutMovieIntoHandle(m_movie
, hMovie
);
1046 long lSize
= GetHandleSize(hMovie
);
1047 DisposeHandle(hMovie
);
1052 if (::GetMaxLoadedTimeInMovie(m_movie
, &tv
) != noErr
)
1054 wxLogDebug(wxT("GetMaxLoadedTimeInMovie failed"));
1058 return wxQTMediaBackend::GetDataSizeFromStart(tv
);
1062 //---------------------------------------------------------------------------
1063 // wxQTMediaBackend::GetDownloadTotal
1064 //---------------------------------------------------------------------------
1065 wxLongLong
wxQTMediaBackend::GetDownloadTotal()
1067 return wxQTMediaBackend::GetDataSizeFromStart(
1068 ::GetMovieDuration(m_movie
)
1072 //---------------------------------------------------------------------------
1073 // wxQTMediaBackend::MacVisibilityChanged
1075 // The main problem here is that Windows quicktime, for example,
1076 // renders more directly to a HWND. Mac quicktime does not do this
1077 // and instead renders to the port of the WindowRef/WindowPtr on top
1078 // of everything else/all other windows.
1080 // So, for example, if you were to have a CreateTabsControl/wxNotebook
1081 // and change pages, even if you called HIViewSetVisible/SetControlVisibility
1082 // directly the movie will still continue playing on top of everything else
1083 // if you went to a different tab.
1085 // Note that another issue, and why we call MCSetControllerPort instead
1086 // of SetMovieGWorld directly, is that in addition to rendering on
1087 // top of everything else the last created controller steals mouse and
1088 // other input from everything else in the window, including other
1089 // controllers. Setting the port of it releases this behaviour.
1090 //---------------------------------------------------------------------------
1091 void wxQTMediaBackend::MacVisibilityChanged()
1093 if(!m_mc
|| !m_ctrl
->m_bLoaded
)
1094 return; //not initialized yet
1096 if(m_ctrl
->IsShownOnScreen())
1098 //The window is being shown again, so set the GWorld of the
1099 //controller back to the port of the parent WindowRef
1101 (WindowRef
) m_ctrl
->MacGetTopLevelWindowRef();
1103 ::MCSetControllerPort(m_mc
, (CGrafPtr
) GetWindowPort(wrTLW
));
1104 wxASSERT(::GetMoviesError() == noErr
);
1108 //We are being hidden - set the GWorld of the controller
1109 //to the offscreen GWorld
1110 ::MCSetControllerPort(m_mc
, m_movieWorld
);
1111 wxASSERT(::GetMoviesError() == noErr
);
1115 //---------------------------------------------------------------------------
1116 // wxQTMediaBackend::OnEraseBackground
1118 // Suggestion from Greg Hazel to repaint the movie when idle
1120 //---------------------------------------------------------------------------
1121 void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent
& WXUNUSED(evt
))
1123 // Work around Nasty OSX drawing bug:
1124 // http://lists.apple.com/archives/QuickTime-API/2002/Feb/msg00311.html
1125 WindowRef wrTLW
= (WindowRef
) m_qtb
->m_ctrl
->MacGetTopLevelWindowRef();
1127 RgnHandle region
= ::MCGetControllerBoundsRgn(m_qtb
->m_mc
);
1128 ::MCInvalidate(m_qtb
->m_mc
, wrTLW
, region
);
1129 ::MCIdle(m_qtb
->m_mc
);
1132 //---------------------------------------------------------------------------
1133 // wxQTMediaBackend::PPRMProc (static)
1135 // Called when done PrePrerolling the movie.
1136 // Note that in 99% of the cases this does nothing...
1137 // Anyway we set up the loading timer here to tell us when the movie is done
1138 //---------------------------------------------------------------------------
1139 pascal void wxQTMediaBackend::PPRMProc(
1141 OSErr
WXUNUSED_UNLESS_DEBUG(theErr
),
1144 wxASSERT( theMovie
);
1145 wxASSERT( theRefCon
);
1146 wxASSERT( theErr
== noErr
);
1148 wxQTMediaBackend
* pBE
= (wxQTMediaBackend
*) theRefCon
;
1150 long lTime
= ::GetMovieTime(theMovie
,NULL
);
1151 Fixed rate
= ::GetMoviePreferredRate(theMovie
);
1152 ::PrerollMovie(theMovie
,lTime
,rate
);
1153 pBE
->m_timer
= new wxQTMediaLoadTimer(pBE
);
1154 pBE
->m_timer
->Start(MOVIE_DELAY
);
1157 //---------------------------------------------------------------------------
1158 // wxQTMediaBackend::MCFilterProc (static)
1160 // Callback for when the movie controller recieves a message
1161 //---------------------------------------------------------------------------
1162 pascal Boolean
wxQTMediaBackend::MCFilterProc(
1163 MovieController
WXUNUSED(theController
),
1165 void * WXUNUSED(params
),
1168 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)refCon
;
1173 // don't process idle events
1177 // play button triggered - MC will set movie to opposite state
1178 // of current - playing ? paused : playing
1179 pThis
->m_bPlaying
= !(pThis
->m_bPlaying
);
1189 //---------------------------------------------------------------------------
1190 // wxQTMediaBackend::WindowEventHandler [static]
1192 // Event callback for the top level window of our control that passes
1193 // messages to our moviecontroller so it can receive mouse clicks etc.
1194 //---------------------------------------------------------------------------
1195 pascal OSStatus
wxQTMediaBackend::WindowEventHandler(
1196 EventHandlerCallRef
WXUNUSED(inHandlerCallRef
),
1200 wxQTMediaBackend
* be
= (wxQTMediaBackend
*) inUserData
;
1202 // Only process keyboard messages on this window if it actually
1203 // has focus, otherwise it will steal keystrokes from other windows!
1204 // As well as when it is not loaded properly as it
1205 // will crash in MCIsPlayerEvent
1206 if((GetEventClass(inEvent
) == kEventClassKeyboard
&&
1207 wxWindow::FindFocus() != be
->m_ctrl
)
1208 || !be
->m_ctrl
->m_bLoaded
)
1209 return eventNotHandledErr
;
1211 // Pass the event onto the movie controller
1212 EventRecord theEvent
;
1213 ConvertEventRefToEventRecord( inEvent
, &theEvent
);
1216 // TODO: Apple says MCIsPlayerEvent is depreciated and
1217 // MCClick, MCKey, MCIdle etc. should be used
1218 // (RN: Of course that's what they say about
1219 // CreateMovieControl and HIMovieView as well, LOL!)
1220 err
= ::MCIsPlayerEvent( be
->m_mc
, &theEvent
);
1222 // Pass on to other event handlers if not handled- i.e. wx
1226 return eventNotHandledErr
;
1231 // in source file that contains stuff you don't directly use
1232 #include "wx/html/forcelnk.h"
1233 FORCE_LINK_ME(basewxmediabackends
)
1235 #endif // wxUSE_MEDIACTRL