1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/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"
39 #include "wx/mediactrl.h"
41 // uma is for wxMacFSSpec
42 #include "wx/mac/uma.h"
49 #include <QuickTimeComponents.h>
51 #include <QuickTime/QuickTimeComponents.h>
56 //---------------------------------------------------------------------------
57 // Height and Width of movie controller in the movie control (apple samples)
58 //---------------------------------------------------------------------------
62 //===========================================================================
63 // BACKEND DECLARATIONS
64 //===========================================================================
66 //---------------------------------------------------------------------------
68 //---------------------------------------------------------------------------
70 class WXDLLIMPEXP_MEDIA wxQTMediaBackend
: public wxMediaBackendCommonBase
76 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
81 const wxValidator
& validator
,
82 const wxString
& name
);
84 virtual bool Load(const wxString
& fileName
);
85 virtual bool Load(const wxURI
& location
);
91 virtual wxMediaState
GetState();
93 virtual bool SetPosition(wxLongLong where
);
94 virtual wxLongLong
GetPosition();
95 virtual wxLongLong
GetDuration();
97 virtual void Move(int x
, int y
, int w
, int h
);
98 wxSize
GetVideoSize() const;
100 virtual double GetPlaybackRate();
101 virtual bool SetPlaybackRate(double dRate
);
103 virtual double GetVolume();
104 virtual bool SetVolume(double);
109 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags
);
111 virtual wxLongLong
GetDownloadProgress();
112 virtual wxLongLong
GetDownloadTotal();
114 virtual void MacVisibilityChanged();
117 // ------ Implementation from now on --------
122 void DoLoadBestSize();
123 void DoSetControllerVisible(wxMediaCtrlPlayerControls flags
);
125 wxLongLong
GetDataSizeFromStart(TimeValue end
);
127 Boolean
IsQuickTime4Installed();
128 void DoNewMovieController();
130 static pascal void PPRMProc(
131 Movie theMovie
, OSErr theErr
, void* theRefCon
);
133 //TODO: Last param actually long - does this work on 64bit machines?
134 static pascal Boolean
MCFilterProc(MovieController theController
,
135 short action
, void *params
, long refCon
);
137 static pascal OSStatus
WindowEventHandler(
138 EventHandlerCallRef inHandlerCallRef
,
139 EventRef inEvent
, void *inUserData
);
141 wxSize m_bestSize
; // Original movie size
142 Movie m_movie
; // Movie instance
143 bool m_bPlaying
; // Whether media is playing or not
144 class wxTimer
* m_timer
; // Timer for streaming the movie
145 MovieController m_mc
; // MovieController instance
146 wxMediaCtrlPlayerControls m_interfaceflags
; // Saved interface flags
148 // Event handlers and UPPs/Callbacks
149 EventHandlerRef m_windowEventHandler
;
150 EventHandlerUPP m_windowUPP
;
152 MoviePrePrerollCompleteUPP m_preprerollupp
;
153 MCActionFilterWithRefConUPP m_mcactionupp
;
155 GWorldPtr m_movieWorld
; //Offscreen movie GWorld
157 friend class wxQTMediaEvtHandler
;
159 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
)
162 // helper to hijack background erasing for the QT window
163 class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler
: public wxEvtHandler
166 wxQTMediaEvtHandler(wxQTMediaBackend
*qtb
)
170 qtb
->m_ctrl
->Connect(
171 qtb
->m_ctrl
->GetId(), wxEVT_ERASE_BACKGROUND
,
172 wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground
),
176 void OnEraseBackground(wxEraseEvent
& event
);
179 wxQTMediaBackend
*m_qtb
;
181 DECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler
)
184 //===========================================================================
186 //===========================================================================
189 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
193 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
195 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
)
197 //Time between timer calls - this is the Apple recommondation to the TCL
199 #define MOVIE_DELAY 20
201 //---------------------------------------------------------------------------
202 // wxQTMediaLoadTimer
204 // QT, esp. QT for Windows is very picky about how you go about
205 // async loading. If you were to go through a Windows message loop
206 // or a MoviesTask or both and then check the movie load state
207 // it would still return 1000 (loading)... even (pre)prerolling doesn't
208 // help. However, making a load timer like this works
209 //---------------------------------------------------------------------------
210 class wxQTMediaLoadTimer
: public wxTimer
213 wxQTMediaLoadTimer(wxQTMediaBackend
* parent
) :
218 ::MCIdle(m_parent
->m_mc
);
220 // kMovieLoadStatePlayable is not enough on MAC:
221 // it plays, but IsMovieDone might return true (!)
222 // sure we need to wait until kMovieLoadStatePlaythroughOK
223 if (::GetMovieLoadState(m_parent
->m_movie
) >= 20000)
225 m_parent
->FinishLoad();
231 wxQTMediaBackend
*m_parent
; // Backend pointer
234 // --------------------------------------------------------------------------
235 // wxQTMediaPlayTimer - Handle Asyncronous Playing
237 // 1) Checks to see if the movie is done, and if not continues
238 // streaming the movie
239 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
241 // --------------------------------------------------------------------------
242 class wxQTMediaPlayTimer
: public wxTimer
245 wxQTMediaPlayTimer(wxQTMediaBackend
* parent
) :
251 // OK, a little explaining - basically originally
252 // we only called MoviesTask if the movie was actually
253 // playing (not paused or stopped)... this was before
254 // we realized MoviesTask actually handles repainting
255 // of the current frame - so if you were to resize
256 // or something it would previously not redraw that
257 // portion of the movie.
259 // So now we call MoviesTask always so that it repaints
262 ::MCIdle(m_parent
->m_mc
);
265 // Handle the stop event - if the movie has reached
266 // the end, notify our handler
268 if (::IsMovieDone(m_parent
->m_movie
))
270 if ( m_parent
->SendStopEvent() )
273 wxASSERT(::GetMoviesError() == noErr
);
275 m_parent
->QueueFinishEvent();
281 wxQTMediaBackend
* m_parent
; // Backend pointer
285 //---------------------------------------------------------------------------
286 // wxQTMediaBackend Constructor
288 // Sets m_timer to NULL signifying we havn't loaded anything yet
289 //---------------------------------------------------------------------------
290 wxQTMediaBackend::wxQTMediaBackend()
291 : m_movie(NULL
), m_bPlaying(false), m_timer(NULL
)
292 , m_mc(NULL
), m_interfaceflags(wxMEDIACTRLPLAYERCONTROLS_NONE
)
293 , m_preprerollupp(NULL
), m_movieWorld(NULL
)
297 //---------------------------------------------------------------------------
298 // wxQTMediaBackend Destructor
300 // 1) Cleans up the QuickTime movie instance
301 // 2) Decrements the QuickTime reference counter - if this reaches
302 // 0, QuickTime shuts down
303 // 3) Decrements the QuickTime Windows Media Layer reference counter -
304 // if this reaches 0, QuickTime shuts down the Windows Media Layer
305 //---------------------------------------------------------------------------
306 wxQTMediaBackend::~wxQTMediaBackend()
311 // Cleanup for moviecontroller
314 // destroy wxQTMediaEvtHandler we pushed on it
315 m_ctrl
->PopEventHandler(true);
316 RemoveEventHandler(m_windowEventHandler
);
317 DisposeEventHandlerUPP(m_windowUPP
);
319 // Dispose of the movie controller
320 ::DisposeMovieController(m_mc
);
322 DisposeMCActionFilterWithRefConUPP(m_mcactionupp
);
324 // Dispose of offscreen GWorld
325 ::DisposeGWorld(m_movieWorld
);
328 // Note that ExitMovies() is not necessary...
332 //---------------------------------------------------------------------------
333 // wxQTMediaBackend::CreateControl
335 // 1) Intializes QuickTime
336 // 2) Creates the control window
337 //---------------------------------------------------------------------------
338 bool wxQTMediaBackend::CreateControl(
345 const wxValidator
& validator
,
346 const wxString
& name
)
348 if (!IsQuickTime4Installed())
353 wxMediaCtrl
* mediactrl
= (wxMediaCtrl
*)ctrl
;
357 // By default wxWindow(s) is created with a border -
358 // so we need to get rid of those
360 // Since we don't have a child window like most other
361 // backends, we don't need wxCLIP_CHILDREN
363 if ( !mediactrl
->wxControl::Create(
364 parent
, id
, pos
, size
,
365 wxWindow::MacRemoveBordersFromStyle(style
),
372 mediactrl
->SetValidator(validator
);
379 //---------------------------------------------------------------------------
380 // wxQTMediaBackend::IsQuickTime4Installed
382 // Determines whether version 4 of QT is installed
383 // (Pretty much for Classic only)
384 //---------------------------------------------------------------------------
385 Boolean
wxQTMediaBackend::IsQuickTime4Installed()
390 error
= Gestalt(gestaltQuickTime
, &result
);
391 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
394 //---------------------------------------------------------------------------
395 // wxQTMediaBackend::Load (file version)
397 // 1) Get an FSSpec from the Windows path name
399 // 3) Obtain the movie instance from the movie resource
400 // 4) Close the movie resource
402 //---------------------------------------------------------------------------
403 bool wxQTMediaBackend::Load(const wxString
& fileName
)
408 ::ClearMoviesStickyError(); // clear previous errors so
409 // GetMoviesStickyError is useful
415 wxMacFilename2FSSpec( fileName
, &sfFile
);
416 if (OpenMovieFile( &sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
419 short movieResID
= 0;
422 err
= NewMovieFromFile(
431 // check GetMoviesStickyError() because it may not find the
432 // proper codec and play black video and other strange effects,
433 // not to mention mess up the dynamic backend loading scheme
434 // of wxMediaCtrl - so it just does what the QuickTime player does
436 if (err
== noErr
&& ::GetMoviesStickyError() == noErr
)
438 ::CloseMovieFile(movieResFile
);
440 // Create movie controller/control
441 DoNewMovieController();
450 //---------------------------------------------------------------------------
451 // wxQTMediaBackend::Load (URL Version)
453 // 1) Build an escaped URI from location
454 // 2) Create a handle to store the URI string
455 // 3) Put the URI string inside the handle
456 // 4) Make a QuickTime URL data ref from the handle with the URI in it
457 // 5) Clean up the URI string handle
458 // 6) Do some prerolling
460 //---------------------------------------------------------------------------
461 bool wxQTMediaBackend::Load(const wxURI
& location
)
466 ::ClearMoviesStickyError(); // clear previous errors so
467 // GetMoviesStickyError is useful
469 wxString theURI
= location
.BuildURI();
473 const char* theURIString
;
476 wxCharBuffer buf
= wxConvLocal
.cWC2MB(theURI
, theURI
.length(), &len
);
479 theURIString
= theURI
;
480 len
= theURI
.length();
483 Handle theHandle
= ::NewHandleClear(len
+ 1);
486 ::BlockMoveData(theURIString
, *theHandle
, len
+ 1);
488 // create the movie from the handle that refers to the URI
489 err
= ::NewMovieFromDataRef(
491 newMovieActive
| newMovieAsyncOK
/* | newMovieIdleImportOK*/,
493 URLDataHandlerSubType
);
495 ::DisposeHandle(theHandle
);
497 if (err
== noErr
&& ::GetMoviesStickyError() == noErr
)
499 // Movie controller resets prerolling, so we must create first
500 DoNewMovieController();
505 timeNow
= ::GetMovieTime(m_movie
, NULL
);
506 wxASSERT(::GetMoviesError() == noErr
);
508 playRate
= ::GetMoviePreferredRate(m_movie
);
509 wxASSERT(::GetMoviesError() == noErr
);
512 // Note that the callback here is optional,
513 // but without it PrePrerollMovie can be buggy
514 // (see Apple ml). Also, some may wonder
515 // why we need this at all - this is because
516 // Apple docs say QuickTime streamed movies
517 // require it if you don't use a Movie Controller,
518 // which we don't by default.
521 NewMoviePrePrerollCompleteUPP( wxQTMediaBackend::PPRMProc
);
522 ::PrePrerollMovie( m_movie
, timeNow
, playRate
,
523 m_preprerollupp
, (void*)this);
531 //---------------------------------------------------------------------------
532 // wxQTMediaBackend::DoNewMovieController
534 // Attaches movie to moviecontroller or creates moviecontroller
535 // if not created yet
536 //---------------------------------------------------------------------------
537 void wxQTMediaBackend::DoNewMovieController()
541 // Get top level window ref for some mac functions
542 WindowRef wrTLW
= (WindowRef
) m_ctrl
->MacGetTopLevelWindowRef();
544 // MovieController not set up yet, so we need to create a new one.
545 // You have to pass a valid movie to NewMovieController, evidently
546 ::SetMovieGWorld(m_movie
,
547 (CGrafPtr
) GetWindowPort(wrTLW
),
549 wxASSERT(::GetMoviesError() == noErr
);
551 Rect bounds
= wxMacGetBoundsForControl(
553 m_ctrl
->GetPosition(),
556 m_mc
= ::NewMovieController(
558 mcTopLeftMovie
| mcNotVisible
/* | mcWithFrame */ );
559 wxASSERT(::GetMoviesError() == noErr
);
561 ::MCDoAction(m_mc
, 32, (void*)true); // mcActionSetKeysEnabled
562 wxASSERT(::GetMoviesError() == noErr
);
564 // Setup a callback so we can tell when the user presses
565 // play on the player controls
567 NewMCActionFilterWithRefConUPP( wxQTMediaBackend::MCFilterProc
);
568 ::MCSetActionFilterWithRefCon( m_mc
, m_mcactionupp
, (long)this );
569 wxASSERT(::GetMoviesError() == noErr
);
571 // Part of a suggestion from Greg Hazel to repaint movie when idle
572 m_ctrl
->PushEventHandler(new wxQTMediaEvtHandler(this));
574 // Create offscreen GWorld for where to "show" when window is hidden
576 worldRect
.left
= worldRect
.top
= 0;
577 worldRect
.right
= worldRect
.bottom
= 1;
578 ::NewGWorld(&m_movieWorld
, 0, &worldRect
, NULL
, NULL
, 0);
580 // Catch window messages:
581 // if we do not do this and if the user clicks the play
582 // button on the controller, for instance, nothing will happen...
583 EventTypeSpec theWindowEventTypes
[] =
585 { kEventClassMouse
, kEventMouseDown
},
586 { kEventClassMouse
, kEventMouseUp
},
587 { kEventClassMouse
, kEventMouseDragged
},
588 { kEventClassKeyboard
, kEventRawKeyDown
},
589 { kEventClassKeyboard
, kEventRawKeyRepeat
},
590 { kEventClassKeyboard
, kEventRawKeyUp
},
591 { kEventClassWindow
, kEventWindowUpdate
},
592 { kEventClassWindow
, kEventWindowActivated
},
593 { kEventClassWindow
, kEventWindowDeactivated
}
596 NewEventHandlerUPP( wxQTMediaBackend::WindowEventHandler
);
597 InstallWindowEventHandler(
600 GetEventTypeCount( theWindowEventTypes
), theWindowEventTypes
,
602 &m_windowEventHandler
);
606 // MovieController already created:
607 // Just change the movie in it and we're good to go
609 thePoint
.h
= thePoint
.v
= 0;
610 ::MCSetMovie(m_mc
, m_movie
,
611 (WindowRef
)m_ctrl
->MacGetTopLevelWindowRef(),
613 wxASSERT(::GetMoviesError() == noErr
);
617 //---------------------------------------------------------------------------
618 // wxQTMediaBackend::FinishLoad
620 // Performs operations after a movie ready to play/loaded.
621 //---------------------------------------------------------------------------
622 void wxQTMediaBackend::FinishLoad()
624 // Dispose of the PrePrerollMovieUPP if we used it
625 DisposeMoviePrePrerollCompleteUPP(m_preprerollupp
);
627 // get the real size of the movie
630 // show the player controls if the user wants to
631 if (m_interfaceflags
)
632 DoSetControllerVisible(m_interfaceflags
);
634 // we want millisecond precision
635 ::SetMovieTimeScale(m_movie
, 1000);
636 wxASSERT(::GetMoviesError() == noErr
);
638 // start movie progress timer
639 m_timer
= new wxQTMediaPlayTimer(this);
641 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
643 // send loaded event and refresh size
647 //---------------------------------------------------------------------------
648 // wxQTMediaBackend::DoLoadBestSize
650 // Sets the best size of the control from the real size of the movie
651 //---------------------------------------------------------------------------
652 void wxQTMediaBackend::DoLoadBestSize()
654 // get the real size of the movie
656 ::GetMovieNaturalBoundsRect(m_movie
, &outRect
);
657 wxASSERT(::GetMoviesError() == noErr
);
659 // determine best size
660 m_bestSize
.x
= outRect
.right
- outRect
.left
;
661 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
664 //---------------------------------------------------------------------------
665 // wxQTMediaBackend::Play
667 // Start the QT movie
668 // (Apple recommends mcActionPrerollAndPlay but that's QT 4.1+)
669 //---------------------------------------------------------------------------
670 bool wxQTMediaBackend::Play()
672 Fixed fixRate
= (Fixed
) (wxQTMediaBackend::GetPlaybackRate() * 0x10000);
674 fixRate
= ::GetMoviePreferredRate(m_movie
);
676 wxASSERT(fixRate
!= 0);
679 ::MCDoAction( m_mc
, 8 /* mcActionPlay */, (void*) fixRate
);
681 bool result
= (::GetMoviesError() == noErr
);
691 //---------------------------------------------------------------------------
692 // wxQTMediaBackend::Pause
695 //---------------------------------------------------------------------------
696 bool wxQTMediaBackend::DoPause()
698 // Stop the movie A.K.A. ::StopMovie(m_movie);
701 ::MCDoAction( m_mc
, 8 /*mcActionPlay*/, (void *) 0);
703 return ::GetMoviesError() == noErr
;
710 bool wxQTMediaBackend::Pause()
712 bool bSuccess
= DoPause();
714 this->QueuePauseEvent();
719 //---------------------------------------------------------------------------
720 // wxQTMediaBackend::Stop
723 // 2) Seek to the beginning of the movie
724 //---------------------------------------------------------------------------
725 bool wxQTMediaBackend::DoStop()
727 if (!wxQTMediaBackend::DoPause())
730 ::GoToBeginningOfMovie(m_movie
);
731 return ::GetMoviesError() == noErr
;
734 bool wxQTMediaBackend::Stop()
736 bool bSuccess
= DoStop();
743 //---------------------------------------------------------------------------
744 // wxQTMediaBackend::GetPlaybackRate
746 // 1) Get the movie playback rate from ::GetMovieRate
747 //---------------------------------------------------------------------------
748 double wxQTMediaBackend::GetPlaybackRate()
750 return ( ((double)::GetMovieRate(m_movie
)) / 0x10000);
753 //---------------------------------------------------------------------------
754 // wxQTMediaBackend::SetPlaybackRate
756 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
757 //---------------------------------------------------------------------------
758 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
760 ::SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
761 return ::GetMoviesError() == noErr
;
764 //---------------------------------------------------------------------------
765 // wxQTMediaBackend::SetPosition
767 // 1) Create a time record struct (TimeRecord) with appropriate values
768 // 2) Pass struct to SetMovieTime
769 //---------------------------------------------------------------------------
770 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
772 TimeRecord theTimeRecord
;
773 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
774 theTimeRecord
.value
.lo
= where
.GetValue();
775 theTimeRecord
.scale
= ::GetMovieTimeScale(m_movie
);
776 theTimeRecord
.base
= ::GetMovieTimeBase(m_movie
);
777 ::SetMovieTime(m_movie
, &theTimeRecord
);
779 if (::GetMoviesError() != noErr
)
785 //---------------------------------------------------------------------------
786 // wxQTMediaBackend::GetPosition
788 // Calls GetMovieTime
789 //---------------------------------------------------------------------------
790 wxLongLong
wxQTMediaBackend::GetPosition()
792 return ::GetMovieTime(m_movie
, NULL
);
795 //---------------------------------------------------------------------------
796 // wxQTMediaBackend::GetVolume
798 // Gets the volume through GetMovieVolume - which returns a 16 bit short -
800 // +--------+--------+
802 // +--------+--------+
804 // (1) first 8 bits are value before decimal
805 // (2) second 8 bits are value after decimal
807 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
808 // 1 (full gain and sound)
809 //---------------------------------------------------------------------------
810 double wxQTMediaBackend::GetVolume()
812 short sVolume
= ::GetMovieVolume(m_movie
);
814 if (sVolume
& (128 << 8)) //negative - no sound
817 return sVolume
/ 256.0;
820 //---------------------------------------------------------------------------
821 // wxQTMediaBackend::SetVolume
823 // Sets the volume through SetMovieVolume - which takes a 16 bit short -
825 // +--------+--------+
827 // +--------+--------+
829 // (1) first 8 bits are value before decimal
830 // (2) second 8 bits are value after decimal
832 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
833 // 1 (full gain and sound)
834 //---------------------------------------------------------------------------
835 bool wxQTMediaBackend::SetVolume(double dVolume
)
837 ::SetMovieVolume(m_movie
, (short) (dVolume
* 256));
841 //---------------------------------------------------------------------------
842 // wxQTMediaBackend::GetDuration
844 // Calls GetMovieDuration
845 //---------------------------------------------------------------------------
846 wxLongLong
wxQTMediaBackend::GetDuration()
848 return ::GetMovieDuration(m_movie
);
851 //---------------------------------------------------------------------------
852 // wxQTMediaBackend::GetState
854 // Determines the current state - the timer keeps track of whether or not
855 // we are paused or stopped (if the timer is running we are playing)
856 //---------------------------------------------------------------------------
857 wxMediaState
wxQTMediaBackend::GetState()
860 // GetMovieActive/IsMovieDone/SetMovieActive
861 // combo if implemented that way
863 return wxMEDIASTATE_PLAYING
;
864 else if (!m_movie
|| wxQTMediaBackend::GetPosition() == 0)
865 return wxMEDIASTATE_STOPPED
;
867 return wxMEDIASTATE_PAUSED
;
870 //---------------------------------------------------------------------------
871 // wxQTMediaBackend::Cleanup
873 // Diposes of the movie timer, Control if native, and stops and disposes
875 //---------------------------------------------------------------------------
876 void wxQTMediaBackend::Cleanup()
886 // Apple samples with CreateMovieControl typically
887 // install a event handler and do this on the dispose
888 // event, but we do it here for simplicity
889 // (It might keep playing for several seconds after
890 // control destruction if not)
891 wxQTMediaBackend::Pause();
893 // Dispose of control or remove movie from MovieController
895 thePoint
.h
= thePoint
.v
= 0;
896 ::MCSetVisible(m_mc
, false);
897 ::MCSetMovie(m_mc
, NULL
, NULL
, thePoint
);
899 ::DisposeMovie(m_movie
);
903 //---------------------------------------------------------------------------
904 // wxQTMediaBackend::GetVideoSize
906 // Returns the actual size of the QT movie
907 //---------------------------------------------------------------------------
908 wxSize
wxQTMediaBackend::GetVideoSize() const
913 //---------------------------------------------------------------------------
914 // wxQTMediaBackend::Move
916 // Move the movie controller or movie control
917 // (we need to actually move the movie control manually...)
918 // Top 10 things to do with quicktime in March 93's issue
919 // of DEVELOP - very useful
920 // http:// www.mactech.com/articles/develop/issue_13/031-033_QuickTime_column.html
921 // OLD NOTE: Calling MCSetControllerBoundsRect without detaching
922 // supposively resulted in a crash back then. Current code even
923 // with CFM classic runs fine. If there is ever a problem,
924 // take out the if 0 lines below
925 //---------------------------------------------------------------------------
926 void wxQTMediaBackend::Move(int x
, int y
, int w
, int h
)
930 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
931 Rect theRect
= {y
, x
, y
+ h
, x
+ w
};
933 #if 0 // see note above
934 ::MCSetControllerAttached(m_mc
, false);
935 wxASSERT(::GetMoviesError() == noErr
);
938 ::MCSetControllerBoundsRect(m_mc
, &theRect
);
939 wxASSERT(::GetMoviesError() == noErr
);
941 #if 0 // see note above
942 if (m_interfaceflags
)
944 ::MCSetVisible(m_mc
, true);
945 wxASSERT(::GetMoviesError() == noErr
);
951 //---------------------------------------------------------------------------
952 // wxQTMediaBackend::DoSetControllerVisible
954 // Utility function that takes care of showing the moviecontroller
955 // and showing/hiding the particular controls on it
956 //---------------------------------------------------------------------------
957 void wxQTMediaBackend::DoSetControllerVisible(
958 wxMediaCtrlPlayerControls flags
)
960 ::MCSetVisible(m_mc
, true);
962 // Take care of subcontrols
963 if (::GetMoviesError() == noErr
)
966 ::MCDoAction(m_mc
, 39/*mcActionGetFlags*/, (void*)&mcFlags
);
968 if (::GetMoviesError() == noErr
)
970 mcFlags
|= ( //(1<<0)/*mcFlagSuppressMovieFrame*/ |
971 (1 << 3)/*mcFlagsUseWindowPalette*/
972 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_STEP
)
973 ? 0 : (1 << 1)/*mcFlagSuppressStepButtons*/)
974 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_VOLUME
)
975 ? 0 : (1 << 2)/*mcFlagSuppressSpeakerButton*/)
976 //if we take care of repainting ourselves
977 // | (1 << 4) /*mcFlagDontInvalidate*/
980 ::MCDoAction(m_mc
, 38/*mcActionSetFlags*/, (void*)mcFlags
);
984 // Adjust height and width of best size for movie controller
985 // if the user wants it shown
986 m_bestSize
.x
= m_bestSize
.x
> wxMCWIDTH
? m_bestSize
.x
: wxMCWIDTH
;
987 m_bestSize
.y
+= wxMCHEIGHT
;
990 //---------------------------------------------------------------------------
991 // wxQTMediaBackend::ShowPlayerControls
993 // Shows/Hides subcontrols on the media control
994 //---------------------------------------------------------------------------
995 bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags
)
998 return false; // no movie controller...
1000 bool bSizeChanged
= false;
1002 // if the controller is visible and we want to hide it do so
1003 if (m_interfaceflags
&& !flags
)
1005 bSizeChanged
= true;
1007 ::MCSetVisible(m_mc
, false);
1009 else if (!m_interfaceflags
&& flags
) // show controller if hidden
1011 bSizeChanged
= true;
1012 DoSetControllerVisible(flags
);
1015 // readjust parent sizers
1018 NotifyMovieSizeChanged();
1020 // remember state in case of loading new media
1021 m_interfaceflags
= flags
;
1024 return ::GetMoviesError() == noErr
;
1027 //---------------------------------------------------------------------------
1028 // wxQTMediaBackend::GetDataSizeFromStart
1030 // Calls either GetMovieDataSize or GetMovieDataSize64 with a value
1031 // of 0 for the starting value
1032 //---------------------------------------------------------------------------
1033 wxLongLong
wxQTMediaBackend::GetDataSizeFromStart(TimeValue end
)
1035 #if 0 // old pre-qt4 way
1036 return ::GetMovieDataSize(m_movie
, 0, end
)
1039 ::GetMovieDataSize64(m_movie
, 0, end
, &llDataSize
);
1040 return wxLongLong(llDataSize
.hi
, llDataSize
.lo
);
1044 //---------------------------------------------------------------------------
1045 // wxQTMediaBackend::GetDownloadProgress
1046 //---------------------------------------------------------------------------
1047 wxLongLong
wxQTMediaBackend::GetDownloadProgress()
1049 #if 0 // hackish and slow
1050 Handle hMovie
= NewHandle(0);
1051 PutMovieIntoHandle(m_movie
, hMovie
);
1052 long lSize
= GetHandleSize(hMovie
);
1053 DisposeHandle(hMovie
);
1058 if (::GetMaxLoadedTimeInMovie(m_movie
, &tv
) != noErr
)
1060 wxLogDebug(wxT("GetMaxLoadedTimeInMovie failed"));
1064 return wxQTMediaBackend::GetDataSizeFromStart(tv
);
1068 //---------------------------------------------------------------------------
1069 // wxQTMediaBackend::GetDownloadTotal
1070 //---------------------------------------------------------------------------
1071 wxLongLong
wxQTMediaBackend::GetDownloadTotal()
1073 return wxQTMediaBackend::GetDataSizeFromStart(
1074 ::GetMovieDuration(m_movie
)
1078 //---------------------------------------------------------------------------
1079 // wxQTMediaBackend::MacVisibilityChanged
1081 // The main problem here is that Windows quicktime, for example,
1082 // renders more directly to a HWND. Mac quicktime does not do this
1083 // and instead renders to the port of the WindowRef/WindowPtr on top
1084 // of everything else/all other windows.
1086 // So, for example, if you were to have a CreateTabsControl/wxNotebook
1087 // and change pages, even if you called HIViewSetVisible/SetControlVisibility
1088 // directly the movie will still continue playing on top of everything else
1089 // if you went to a different tab.
1091 // Note that another issue, and why we call MCSetControllerPort instead
1092 // of SetMovieGWorld directly, is that in addition to rendering on
1093 // top of everything else the last created controller steals mouse and
1094 // other input from everything else in the window, including other
1095 // controllers. Setting the port of it releases this behaviour.
1096 //---------------------------------------------------------------------------
1097 void wxQTMediaBackend::MacVisibilityChanged()
1099 if(!m_mc
|| !m_ctrl
->m_bLoaded
)
1100 return; //not initialized yet
1102 if(m_ctrl
->MacIsReallyShown())
1104 //The window is being shown again, so set the GWorld of the
1105 //controller back to the port of the parent WindowRef
1107 (WindowRef
) m_ctrl
->MacGetTopLevelWindowRef();
1109 ::MCSetControllerPort(m_mc
, (CGrafPtr
) GetWindowPort(wrTLW
));
1110 wxASSERT(::GetMoviesError() == noErr
);
1114 //We are being hidden - set the GWorld of the controller
1115 //to the offscreen GWorld
1116 ::MCSetControllerPort(m_mc
, m_movieWorld
);
1117 wxASSERT(::GetMoviesError() == noErr
);
1121 //---------------------------------------------------------------------------
1122 // wxQTMediaBackend::OnEraseBackground
1124 // Suggestion from Greg Hazel to repaint the movie when idle
1126 //---------------------------------------------------------------------------
1127 void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent
& evt
)
1129 // Work around Nasty OSX drawing bug:
1130 // http://lists.apple.com/archives/QuickTime-API/2002/Feb/msg00311.html
1131 WindowRef wrTLW
= (WindowRef
) m_qtb
->m_ctrl
->MacGetTopLevelWindowRef();
1133 RgnHandle region
= ::MCGetControllerBoundsRgn(m_qtb
->m_mc
);
1134 ::MCInvalidate(m_qtb
->m_mc
, wrTLW
, region
);
1135 ::MCIdle(m_qtb
->m_mc
);
1138 //---------------------------------------------------------------------------
1139 // wxQTMediaBackend::PPRMProc (static)
1141 // Called when done PrePrerolling the movie.
1142 // Note that in 99% of the cases this does nothing...
1143 // Anyway we set up the loading timer here to tell us when the movie is done
1144 //---------------------------------------------------------------------------
1145 pascal void wxQTMediaBackend::PPRMProc(
1147 OSErr
WXUNUSED_UNLESS_DEBUG(theErr
),
1150 wxASSERT( theMovie
);
1151 wxASSERT( theRefCon
);
1152 wxASSERT( theErr
== noErr
);
1154 wxQTMediaBackend
* pBE
= (wxQTMediaBackend
*) theRefCon
;
1156 long lTime
= ::GetMovieTime(theMovie
,NULL
);
1157 Fixed rate
= ::GetMoviePreferredRate(theMovie
);
1158 ::PrerollMovie(theMovie
,lTime
,rate
);
1159 pBE
->m_timer
= new wxQTMediaLoadTimer(pBE
);
1160 pBE
->m_timer
->Start(MOVIE_DELAY
);
1163 //---------------------------------------------------------------------------
1164 // wxQTMediaBackend::MCFilterProc (static)
1166 // Callback for when the movie controller recieves a message
1167 //---------------------------------------------------------------------------
1168 pascal Boolean
wxQTMediaBackend::MCFilterProc(
1169 MovieController
WXUNUSED(theController
),
1171 void * WXUNUSED(params
),
1174 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)refCon
;
1179 // don't process idle events
1183 // play button triggered - MC will set movie to opposite state
1184 // of current - playing ? paused : playing
1185 pThis
->m_bPlaying
= !(pThis
->m_bPlaying
);
1195 //---------------------------------------------------------------------------
1196 // wxQTMediaBackend::WindowEventHandler [static]
1198 // Event callback for the top level window of our control that passes
1199 // messages to our moviecontroller so it can receive mouse clicks etc.
1200 //---------------------------------------------------------------------------
1201 pascal OSStatus
wxQTMediaBackend::WindowEventHandler(
1202 EventHandlerCallRef inHandlerCallRef
,
1206 wxQTMediaBackend
* be
= (wxQTMediaBackend
*) inUserData
;
1208 // Only process keyboard messages on this window if it actually
1209 // has focus, otherwise it will steal keystrokes from other windows!
1210 // As well as when it is not loaded properly as it
1211 // will crash in MCIsPlayerEvent
1212 if((GetEventClass(inEvent
) == kEventClassKeyboard
&&
1213 wxWindow::FindFocus() != be
->m_ctrl
)
1214 || !be
->m_ctrl
->m_bLoaded
)
1215 return eventNotHandledErr
;
1217 // Pass the event onto the movie controller
1218 EventRecord theEvent
;
1219 ConvertEventRefToEventRecord( inEvent
, &theEvent
);
1222 // TODO: Apple says MCIsPlayerEvent is depreciated and
1223 // MCClick, MCKey, MCIdle etc. should be used
1224 // (RN: Of course that's what they say about
1225 // CreateMovieControl and HIMovieView as well, LOL!)
1226 err
= ::MCIsPlayerEvent( be
->m_mc
, &theEvent
);
1228 // Pass on to other event handlers if not handled- i.e. wx
1232 return eventNotHandledErr
;
1235 // in source file that contains stuff you don't directly use
1236 #include "wx/html/forcelnk.h"
1237 FORCE_LINK_ME(basewxmediabackends
)
1239 #endif // wxUSE_MEDIACTRL