1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
41 #include "wx/mediactrl.h"
48 // uma is for wxMacFSSpec
49 #include "wx/mac/uma.h"
55 #include <QuickTimeComponents.h>
57 #include <QuickTime/QuickTimeComponents.h>
60 //---------------------------------------------------------------------------
61 // Height and Width of movie controller in the movie control (apple samples)
62 //---------------------------------------------------------------------------
66 //===========================================================================
67 // BACKEND DECLARATIONS
68 //===========================================================================
70 //---------------------------------------------------------------------------
72 //---------------------------------------------------------------------------
74 class WXDLLIMPEXP_MEDIA wxQTMediaBackend
: public wxMediaBackendCommonBase
80 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
85 const wxValidator
& validator
,
86 const wxString
& name
);
88 virtual bool Load(const wxString
& fileName
);
89 virtual bool Load(const wxURI
& location
);
95 virtual wxMediaState
GetState();
97 virtual bool SetPosition(wxLongLong where
);
98 virtual wxLongLong
GetPosition();
99 virtual wxLongLong
GetDuration();
101 virtual void Move(int x
, int y
, int w
, int h
);
102 wxSize
GetVideoSize() const;
104 virtual double GetPlaybackRate();
105 virtual bool SetPlaybackRate(double dRate
);
107 virtual double GetVolume();
108 virtual bool SetVolume(double);
113 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags
);
115 virtual wxLongLong
GetDownloadProgress();
116 virtual wxLongLong
GetDownloadTotal();
118 virtual void MacVisibilityChanged();
121 // ------ Implementation from now on --------
126 void DoLoadBestSize();
127 void DoSetControllerVisible(wxMediaCtrlPlayerControls flags
);
129 wxLongLong
GetDataSizeFromStart(TimeValue end
);
131 Boolean
IsQuickTime4Installed();
132 void DoNewMovieController();
134 static pascal void PPRMProc(
135 Movie theMovie
, OSErr theErr
, void* theRefCon
);
137 //TODO: Last param actually long - does this work on 64bit machines?
138 static pascal Boolean
MCFilterProc(MovieController theController
,
139 short action
, void *params
, long refCon
);
141 static pascal OSStatus
WindowEventHandler(
142 EventHandlerCallRef inHandlerCallRef
,
143 EventRef inEvent
, void *inUserData
);
145 wxSize m_bestSize
; // Original movie size
146 Movie m_movie
; // Movie instance
147 bool m_bPlaying
; // Whether media is playing or not
148 class wxTimer
* m_timer
; // Timer for streaming the movie
149 MovieController m_mc
; // MovieController instance
150 wxMediaCtrlPlayerControls m_interfaceflags
; // Saved interface flags
152 // Event handlers and UPPs/Callbacks
153 EventHandlerRef m_windowEventHandler
;
154 EventHandlerUPP m_windowUPP
;
156 MoviePrePrerollCompleteUPP m_preprerollupp
;
157 MCActionFilterWithRefConUPP m_mcactionupp
;
159 GWorldPtr m_movieWorld
; //Offscreen movie GWorld
161 friend class wxQTMediaEvtHandler
;
163 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
)
166 // helper to hijack background erasing for the QT window
167 class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler
: public wxEvtHandler
170 wxQTMediaEvtHandler(wxQTMediaBackend
*qtb
)
174 qtb
->m_ctrl
->Connect(
175 qtb
->m_ctrl
->GetId(), wxEVT_ERASE_BACKGROUND
,
176 wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground
),
180 void OnEraseBackground(wxEraseEvent
& event
);
183 wxQTMediaBackend
*m_qtb
;
185 DECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler
)
188 //===========================================================================
190 //===========================================================================
193 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
197 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
199 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
)
201 //Time between timer calls - this is the Apple recommondation to the TCL
203 #define MOVIE_DELAY 20
205 //---------------------------------------------------------------------------
206 // wxQTMediaLoadTimer
208 // QT, esp. QT for Windows is very picky about how you go about
209 // async loading. If you were to go through a Windows message loop
210 // or a MoviesTask or both and then check the movie load state
211 // it would still return 1000 (loading)... even (pre)prerolling doesn't
212 // help. However, making a load timer like this works
213 //---------------------------------------------------------------------------
214 class wxQTMediaLoadTimer
: public wxTimer
217 wxQTMediaLoadTimer(wxQTMediaBackend
* parent
) :
222 ::MCIdle(m_parent
->m_mc
);
224 // kMovieLoadStatePlayable is not enough on MAC:
225 // it plays, but IsMovieDone might return true (!)
226 // sure we need to wait until kMovieLoadStatePlaythroughOK
227 if (::GetMovieLoadState(m_parent
->m_movie
) >= 20000)
229 m_parent
->FinishLoad();
235 wxQTMediaBackend
*m_parent
; // Backend pointer
238 // --------------------------------------------------------------------------
239 // wxQTMediaPlayTimer - Handle Asyncronous Playing
241 // 1) Checks to see if the movie is done, and if not continues
242 // streaming the movie
243 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
245 // --------------------------------------------------------------------------
246 class wxQTMediaPlayTimer
: public wxTimer
249 wxQTMediaPlayTimer(wxQTMediaBackend
* parent
) :
255 // OK, a little explaining - basically originally
256 // we only called MoviesTask if the movie was actually
257 // playing (not paused or stopped)... this was before
258 // we realized MoviesTask actually handles repainting
259 // of the current frame - so if you were to resize
260 // or something it would previously not redraw that
261 // portion of the movie.
263 // So now we call MoviesTask always so that it repaints
266 ::MCIdle(m_parent
->m_mc
);
269 // Handle the stop event - if the movie has reached
270 // the end, notify our handler
272 if (::IsMovieDone(m_parent
->m_movie
))
274 if ( m_parent
->SendStopEvent() )
277 wxASSERT(::GetMoviesError() == noErr
);
279 m_parent
->QueueFinishEvent();
285 wxQTMediaBackend
* m_parent
; // Backend pointer
289 //---------------------------------------------------------------------------
290 // wxQTMediaBackend Constructor
292 // Sets m_timer to NULL signifying we havn't loaded anything yet
293 //---------------------------------------------------------------------------
294 wxQTMediaBackend::wxQTMediaBackend()
295 : m_movie(NULL
), m_bPlaying(false), m_timer(NULL
)
296 , m_mc(NULL
), m_interfaceflags(wxMEDIACTRLPLAYERCONTROLS_NONE
)
297 , m_preprerollupp(NULL
), m_movieWorld(NULL
)
301 //---------------------------------------------------------------------------
302 // wxQTMediaBackend Destructor
304 // 1) Cleans up the QuickTime movie instance
305 // 2) Decrements the QuickTime reference counter - if this reaches
306 // 0, QuickTime shuts down
307 // 3) Decrements the QuickTime Windows Media Layer reference counter -
308 // if this reaches 0, QuickTime shuts down the Windows Media Layer
309 //---------------------------------------------------------------------------
310 wxQTMediaBackend::~wxQTMediaBackend()
315 // Cleanup for moviecontroller
318 // destroy wxQTMediaEvtHandler we pushed on it
319 m_ctrl
->PopEventHandler(true);
320 RemoveEventHandler(m_windowEventHandler
);
321 DisposeEventHandlerUPP(m_windowUPP
);
323 // Dispose of the movie controller
324 ::DisposeMovieController(m_mc
);
326 DisposeMCActionFilterWithRefConUPP(m_mcactionupp
);
328 // Dispose of offscreen GWorld
329 ::DisposeGWorld(m_movieWorld
);
332 // Note that ExitMovies() is not necessary...
336 //---------------------------------------------------------------------------
337 // wxQTMediaBackend::CreateControl
339 // 1) Intializes QuickTime
340 // 2) Creates the control window
341 //---------------------------------------------------------------------------
342 bool wxQTMediaBackend::CreateControl(
349 const wxValidator
& validator
,
350 const wxString
& name
)
352 if (!IsQuickTime4Installed())
357 wxMediaCtrl
* mediactrl
= (wxMediaCtrl
*)ctrl
;
361 // By default wxWindow(s) is created with a border -
362 // so we need to get rid of those
364 // Since we don't have a child window like most other
365 // backends, we don't need wxCLIP_CHILDREN
367 if ( !mediactrl
->wxControl::Create(
368 parent
, id
, pos
, size
,
369 wxWindow::MacRemoveBordersFromStyle(style
),
376 mediactrl
->SetValidator(validator
);
383 //---------------------------------------------------------------------------
384 // wxQTMediaBackend::IsQuickTime4Installed
386 // Determines whether version 4 of QT is installed
387 // (Pretty much for Classic only)
388 //---------------------------------------------------------------------------
389 Boolean
wxQTMediaBackend::IsQuickTime4Installed()
394 error
= Gestalt(gestaltQuickTime
, &result
);
395 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
398 //---------------------------------------------------------------------------
399 // wxQTMediaBackend::Load (file version)
401 // 1) Get an FSSpec from the Windows path name
403 // 3) Obtain the movie instance from the movie resource
404 // 4) Close the movie resource
406 //---------------------------------------------------------------------------
407 bool wxQTMediaBackend::Load(const wxString
& fileName
)
412 ::ClearMoviesStickyError(); // clear previous errors so
413 // GetMoviesStickyError is useful
419 wxMacFilename2FSSpec( fileName
, &sfFile
);
420 if (OpenMovieFile( &sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
423 short movieResID
= 0;
426 err
= NewMovieFromFile(
435 // check GetMoviesStickyError() because it may not find the
436 // proper codec and play black video and other strange effects,
437 // not to mention mess up the dynamic backend loading scheme
438 // of wxMediaCtrl - so it just does what the QuickTime player does
440 if (err
== noErr
&& ::GetMoviesStickyError() == noErr
)
442 ::CloseMovieFile(movieResFile
);
444 // Create movie controller/control
445 DoNewMovieController();
454 //---------------------------------------------------------------------------
455 // wxQTMediaBackend::Load (URL Version)
457 // 1) Build an escaped URI from location
458 // 2) Create a handle to store the URI string
459 // 3) Put the URI string inside the handle
460 // 4) Make a QuickTime URL data ref from the handle with the URI in it
461 // 5) Clean up the URI string handle
462 // 6) Do some prerolling
464 //---------------------------------------------------------------------------
465 bool wxQTMediaBackend::Load(const wxURI
& location
)
470 ::ClearMoviesStickyError(); // clear previous errors so
471 // GetMoviesStickyError is useful
473 wxString theURI
= location
.BuildURI();
477 const char* theURIString
;
480 wxCharBuffer buf
= wxConvLocal
.cWC2MB(theURI
, theURI
.length(), &len
);
483 theURIString
= theURI
;
484 len
= theURI
.length();
487 Handle theHandle
= ::NewHandleClear(len
+ 1);
490 ::BlockMoveData(theURIString
, *theHandle
, len
+ 1);
492 // create the movie from the handle that refers to the URI
493 err
= ::NewMovieFromDataRef(
495 newMovieActive
| newMovieAsyncOK
/* | newMovieIdleImportOK*/,
497 URLDataHandlerSubType
);
499 ::DisposeHandle(theHandle
);
501 if (err
== noErr
&& ::GetMoviesStickyError() == noErr
)
503 // Movie controller resets prerolling, so we must create first
504 DoNewMovieController();
509 timeNow
= ::GetMovieTime(m_movie
, NULL
);
510 wxASSERT(::GetMoviesError() == noErr
);
512 playRate
= ::GetMoviePreferredRate(m_movie
);
513 wxASSERT(::GetMoviesError() == noErr
);
516 // Note that the callback here is optional,
517 // but without it PrePrerollMovie can be buggy
518 // (see Apple ml). Also, some may wonder
519 // why we need this at all - this is because
520 // Apple docs say QuickTime streamed movies
521 // require it if you don't use a Movie Controller,
522 // which we don't by default.
525 NewMoviePrePrerollCompleteUPP( wxQTMediaBackend::PPRMProc
);
526 ::PrePrerollMovie( m_movie
, timeNow
, playRate
,
527 m_preprerollupp
, (void*)this);
535 //---------------------------------------------------------------------------
536 // wxQTMediaBackend::DoNewMovieController
538 // Attaches movie to moviecontroller or creates moviecontroller
539 // if not created yet
540 //---------------------------------------------------------------------------
541 void wxQTMediaBackend::DoNewMovieController()
545 // Get top level window ref for some mac functions
546 WindowRef wrTLW
= (WindowRef
) m_ctrl
->MacGetTopLevelWindowRef();
548 // MovieController not set up yet, so we need to create a new one.
549 // You have to pass a valid movie to NewMovieController, evidently
550 ::SetMovieGWorld(m_movie
,
551 (CGrafPtr
) GetWindowPort(wrTLW
),
553 wxASSERT(::GetMoviesError() == noErr
);
555 Rect bounds
= wxMacGetBoundsForControl(
557 m_ctrl
->GetPosition(),
560 m_mc
= ::NewMovieController(
562 mcTopLeftMovie
| mcNotVisible
/* | mcWithFrame */ );
563 wxASSERT(::GetMoviesError() == noErr
);
565 ::MCDoAction(m_mc
, 32, (void*)true); // mcActionSetKeysEnabled
566 wxASSERT(::GetMoviesError() == noErr
);
568 // Setup a callback so we can tell when the user presses
569 // play on the player controls
571 NewMCActionFilterWithRefConUPP( wxQTMediaBackend::MCFilterProc
);
572 ::MCSetActionFilterWithRefCon( m_mc
, m_mcactionupp
, (long)this );
573 wxASSERT(::GetMoviesError() == noErr
);
575 // Part of a suggestion from Greg Hazel to repaint movie when idle
576 m_ctrl
->PushEventHandler(new wxQTMediaEvtHandler(this));
578 // Create offscreen GWorld for where to "show" when window is hidden
580 worldRect
.left
= worldRect
.top
= 0;
581 worldRect
.right
= worldRect
.bottom
= 1;
582 ::NewGWorld(&m_movieWorld
, 0, &worldRect
, NULL
, NULL
, 0);
584 // Catch window messages:
585 // if we do not do this and if the user clicks the play
586 // button on the controller, for instance, nothing will happen...
587 EventTypeSpec theWindowEventTypes
[] =
589 { kEventClassMouse
, kEventMouseDown
},
590 { kEventClassMouse
, kEventMouseUp
},
591 { kEventClassMouse
, kEventMouseDragged
},
592 { kEventClassKeyboard
, kEventRawKeyDown
},
593 { kEventClassKeyboard
, kEventRawKeyRepeat
},
594 { kEventClassKeyboard
, kEventRawKeyUp
},
595 { kEventClassWindow
, kEventWindowUpdate
},
596 { kEventClassWindow
, kEventWindowActivated
},
597 { kEventClassWindow
, kEventWindowDeactivated
}
600 NewEventHandlerUPP( wxQTMediaBackend::WindowEventHandler
);
601 InstallWindowEventHandler(
604 GetEventTypeCount( theWindowEventTypes
), theWindowEventTypes
,
606 &m_windowEventHandler
);
610 // MovieController already created:
611 // Just change the movie in it and we're good to go
613 thePoint
.h
= thePoint
.v
= 0;
614 ::MCSetMovie(m_mc
, m_movie
,
615 (WindowRef
)m_ctrl
->MacGetTopLevelWindowRef(),
617 wxASSERT(::GetMoviesError() == noErr
);
621 //---------------------------------------------------------------------------
622 // wxQTMediaBackend::FinishLoad
624 // Performs operations after a movie ready to play/loaded.
625 //---------------------------------------------------------------------------
626 void wxQTMediaBackend::FinishLoad()
628 // Dispose of the PrePrerollMovieUPP if we used it
629 DisposeMoviePrePrerollCompleteUPP(m_preprerollupp
);
631 // get the real size of the movie
634 // show the player controls if the user wants to
635 if (m_interfaceflags
)
636 DoSetControllerVisible(m_interfaceflags
);
638 // we want millisecond precision
639 ::SetMovieTimeScale(m_movie
, 1000);
640 wxASSERT(::GetMoviesError() == noErr
);
642 // start movie progress timer
643 m_timer
= new wxQTMediaPlayTimer(this);
645 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
647 // send loaded event and refresh size
651 //---------------------------------------------------------------------------
652 // wxQTMediaBackend::DoLoadBestSize
654 // Sets the best size of the control from the real size of the movie
655 //---------------------------------------------------------------------------
656 void wxQTMediaBackend::DoLoadBestSize()
658 // get the real size of the movie
660 ::GetMovieNaturalBoundsRect(m_movie
, &outRect
);
661 wxASSERT(::GetMoviesError() == noErr
);
663 // determine best size
664 m_bestSize
.x
= outRect
.right
- outRect
.left
;
665 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
668 //---------------------------------------------------------------------------
669 // wxQTMediaBackend::Play
671 // Start the QT movie
672 // (Apple recommends mcActionPrerollAndPlay but that's QT 4.1+)
673 //---------------------------------------------------------------------------
674 bool wxQTMediaBackend::Play()
676 Fixed fixRate
= (Fixed
) (wxQTMediaBackend::GetPlaybackRate() * 0x10000);
678 fixRate
= ::GetMoviePreferredRate(m_movie
);
680 wxASSERT(fixRate
!= 0);
683 ::MCDoAction( m_mc
, 8 /* mcActionPlay */, (void*) fixRate
);
685 bool result
= (::GetMoviesError() == noErr
);
695 //---------------------------------------------------------------------------
696 // wxQTMediaBackend::Pause
699 //---------------------------------------------------------------------------
700 bool wxQTMediaBackend::DoPause()
702 // Stop the movie A.K.A. ::StopMovie(m_movie);
705 ::MCDoAction( m_mc
, 8 /*mcActionPlay*/, (void *) 0);
707 return ::GetMoviesError() == noErr
;
714 bool wxQTMediaBackend::Pause()
716 bool bSuccess
= DoPause();
718 this->QueuePauseEvent();
723 //---------------------------------------------------------------------------
724 // wxQTMediaBackend::Stop
727 // 2) Seek to the beginning of the movie
728 //---------------------------------------------------------------------------
729 bool wxQTMediaBackend::DoStop()
731 if (!wxQTMediaBackend::DoPause())
734 ::GoToBeginningOfMovie(m_movie
);
735 return ::GetMoviesError() == noErr
;
738 bool wxQTMediaBackend::Stop()
740 bool bSuccess
= DoStop();
747 //---------------------------------------------------------------------------
748 // wxQTMediaBackend::GetPlaybackRate
750 // 1) Get the movie playback rate from ::GetMovieRate
751 //---------------------------------------------------------------------------
752 double wxQTMediaBackend::GetPlaybackRate()
754 return ( ((double)::GetMovieRate(m_movie
)) / 0x10000);
757 //---------------------------------------------------------------------------
758 // wxQTMediaBackend::SetPlaybackRate
760 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
761 //---------------------------------------------------------------------------
762 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
764 ::SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
765 return ::GetMoviesError() == noErr
;
768 //---------------------------------------------------------------------------
769 // wxQTMediaBackend::SetPosition
771 // 1) Create a time record struct (TimeRecord) with appropriate values
772 // 2) Pass struct to SetMovieTime
773 //---------------------------------------------------------------------------
774 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
776 TimeRecord theTimeRecord
;
777 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
778 theTimeRecord
.value
.lo
= where
.GetValue();
779 theTimeRecord
.scale
= ::GetMovieTimeScale(m_movie
);
780 theTimeRecord
.base
= ::GetMovieTimeBase(m_movie
);
781 ::SetMovieTime(m_movie
, &theTimeRecord
);
783 if (::GetMoviesError() != noErr
)
789 //---------------------------------------------------------------------------
790 // wxQTMediaBackend::GetPosition
792 // Calls GetMovieTime
793 //---------------------------------------------------------------------------
794 wxLongLong
wxQTMediaBackend::GetPosition()
796 return ::GetMovieTime(m_movie
, NULL
);
799 //---------------------------------------------------------------------------
800 // wxQTMediaBackend::GetVolume
802 // Gets the volume through GetMovieVolume - which returns a 16 bit short -
804 // +--------+--------+
806 // +--------+--------+
808 // (1) first 8 bits are value before decimal
809 // (2) second 8 bits are value after decimal
811 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
812 // 1 (full gain and sound)
813 //---------------------------------------------------------------------------
814 double wxQTMediaBackend::GetVolume()
816 short sVolume
= ::GetMovieVolume(m_movie
);
818 if (sVolume
& (128 << 8)) //negative - no sound
821 return sVolume
/ 256.0;
824 //---------------------------------------------------------------------------
825 // wxQTMediaBackend::SetVolume
827 // Sets the volume through SetMovieVolume - which takes a 16 bit short -
829 // +--------+--------+
831 // +--------+--------+
833 // (1) first 8 bits are value before decimal
834 // (2) second 8 bits are value after decimal
836 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
837 // 1 (full gain and sound)
838 //---------------------------------------------------------------------------
839 bool wxQTMediaBackend::SetVolume(double dVolume
)
841 ::SetMovieVolume(m_movie
, (short) (dVolume
* 256));
845 //---------------------------------------------------------------------------
846 // wxQTMediaBackend::GetDuration
848 // Calls GetMovieDuration
849 //---------------------------------------------------------------------------
850 wxLongLong
wxQTMediaBackend::GetDuration()
852 return ::GetMovieDuration(m_movie
);
855 //---------------------------------------------------------------------------
856 // wxQTMediaBackend::GetState
858 // Determines the current state - the timer keeps track of whether or not
859 // we are paused or stopped (if the timer is running we are playing)
860 //---------------------------------------------------------------------------
861 wxMediaState
wxQTMediaBackend::GetState()
864 // GetMovieActive/IsMovieDone/SetMovieActive
865 // combo if implemented that way
867 return wxMEDIASTATE_PLAYING
;
868 else if (!m_movie
|| wxQTMediaBackend::GetPosition() == 0)
869 return wxMEDIASTATE_STOPPED
;
871 return wxMEDIASTATE_PAUSED
;
874 //---------------------------------------------------------------------------
875 // wxQTMediaBackend::Cleanup
877 // Diposes of the movie timer, Control if native, and stops and disposes
879 //---------------------------------------------------------------------------
880 void wxQTMediaBackend::Cleanup()
890 // Apple samples with CreateMovieControl typically
891 // install a event handler and do this on the dispose
892 // event, but we do it here for simplicity
893 // (It might keep playing for several seconds after
894 // control destruction if not)
895 wxQTMediaBackend::Pause();
897 // Dispose of control or remove movie from MovieController
899 thePoint
.h
= thePoint
.v
= 0;
900 ::MCSetVisible(m_mc
, false);
901 ::MCSetMovie(m_mc
, NULL
, NULL
, thePoint
);
903 ::DisposeMovie(m_movie
);
907 //---------------------------------------------------------------------------
908 // wxQTMediaBackend::GetVideoSize
910 // Returns the actual size of the QT movie
911 //---------------------------------------------------------------------------
912 wxSize
wxQTMediaBackend::GetVideoSize() const
917 //---------------------------------------------------------------------------
918 // wxQTMediaBackend::Move
920 // Move the movie controller or movie control
921 // (we need to actually move the movie control manually...)
922 // Top 10 things to do with quicktime in March 93's issue
923 // of DEVELOP - very useful
924 // http:// www.mactech.com/articles/develop/issue_13/031-033_QuickTime_column.html
925 // OLD NOTE: Calling MCSetControllerBoundsRect without detaching
926 // supposively resulted in a crash back then. Current code even
927 // with CFM classic runs fine. If there is ever a problem,
928 // take out the if 0 lines below
929 //---------------------------------------------------------------------------
930 void wxQTMediaBackend::Move(int x
, int y
, int w
, int h
)
934 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
935 Rect theRect
= {y
, x
, y
+ h
, x
+ w
};
937 #if 0 // see note above
938 ::MCSetControllerAttached(m_mc
, false);
939 wxASSERT(::GetMoviesError() == noErr
);
942 ::MCSetControllerBoundsRect(m_mc
, &theRect
);
943 wxASSERT(::GetMoviesError() == noErr
);
945 #if 0 // see note above
946 if (m_interfaceflags
)
948 ::MCSetVisible(m_mc
, true);
949 wxASSERT(::GetMoviesError() == noErr
);
955 //---------------------------------------------------------------------------
956 // wxQTMediaBackend::DoSetControllerVisible
958 // Utility function that takes care of showing the moviecontroller
959 // and showing/hiding the particular controls on it
960 //---------------------------------------------------------------------------
961 void wxQTMediaBackend::DoSetControllerVisible(
962 wxMediaCtrlPlayerControls flags
)
964 ::MCSetVisible(m_mc
, true);
966 // Take care of subcontrols
967 if (::GetMoviesError() == noErr
)
970 ::MCDoAction(m_mc
, 39/*mcActionGetFlags*/, (void*)&mcFlags
);
972 if (::GetMoviesError() == noErr
)
974 mcFlags
|= ( //(1<<0)/*mcFlagSuppressMovieFrame*/ |
975 (1 << 3)/*mcFlagsUseWindowPalette*/
976 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_STEP
)
977 ? 0 : (1 << 1)/*mcFlagSuppressStepButtons*/)
978 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_VOLUME
)
979 ? 0 : (1 << 2)/*mcFlagSuppressSpeakerButton*/)
980 //if we take care of repainting ourselves
981 // | (1 << 4) /*mcFlagDontInvalidate*/
984 ::MCDoAction(m_mc
, 38/*mcActionSetFlags*/, (void*)mcFlags
);
988 // Adjust height and width of best size for movie controller
989 // if the user wants it shown
990 m_bestSize
.x
= m_bestSize
.x
> wxMCWIDTH
? m_bestSize
.x
: wxMCWIDTH
;
991 m_bestSize
.y
+= wxMCHEIGHT
;
994 //---------------------------------------------------------------------------
995 // wxQTMediaBackend::ShowPlayerControls
997 // Shows/Hides subcontrols on the media control
998 //---------------------------------------------------------------------------
999 bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags
)
1002 return false; // no movie controller...
1004 bool bSizeChanged
= false;
1006 // if the controller is visible and we want to hide it do so
1007 if (m_interfaceflags
&& !flags
)
1009 bSizeChanged
= true;
1011 ::MCSetVisible(m_mc
, false);
1013 else if (!m_interfaceflags
&& flags
) // show controller if hidden
1015 bSizeChanged
= true;
1016 DoSetControllerVisible(flags
);
1019 // readjust parent sizers
1022 NotifyMovieSizeChanged();
1024 // remember state in case of loading new media
1025 m_interfaceflags
= flags
;
1028 return ::GetMoviesError() == noErr
;
1031 //---------------------------------------------------------------------------
1032 // wxQTMediaBackend::GetDataSizeFromStart
1034 // Calls either GetMovieDataSize or GetMovieDataSize64 with a value
1035 // of 0 for the starting value
1036 //---------------------------------------------------------------------------
1037 wxLongLong
wxQTMediaBackend::GetDataSizeFromStart(TimeValue end
)
1039 #if 0 // old pre-qt4 way
1040 return ::GetMovieDataSize(m_movie
, 0, end
)
1043 ::GetMovieDataSize64(m_movie
, 0, end
, &llDataSize
);
1044 return wxLongLong(llDataSize
.hi
, llDataSize
.lo
);
1048 //---------------------------------------------------------------------------
1049 // wxQTMediaBackend::GetDownloadProgress
1050 //---------------------------------------------------------------------------
1051 wxLongLong
wxQTMediaBackend::GetDownloadProgress()
1053 #if 0 // hackish and slow
1054 Handle hMovie
= NewHandle(0);
1055 PutMovieIntoHandle(m_movie
, hMovie
);
1056 long lSize
= GetHandleSize(hMovie
);
1057 DisposeHandle(hMovie
);
1062 if (::GetMaxLoadedTimeInMovie(m_movie
, &tv
) != noErr
)
1064 wxLogDebug(wxT("GetMaxLoadedTimeInMovie failed"));
1068 return wxQTMediaBackend::GetDataSizeFromStart(tv
);
1072 //---------------------------------------------------------------------------
1073 // wxQTMediaBackend::GetDownloadTotal
1074 //---------------------------------------------------------------------------
1075 wxLongLong
wxQTMediaBackend::GetDownloadTotal()
1077 return wxQTMediaBackend::GetDataSizeFromStart(
1078 ::GetMovieDuration(m_movie
)
1082 //---------------------------------------------------------------------------
1083 // wxQTMediaBackend::MacVisibilityChanged
1085 // The main problem here is that Windows quicktime, for example,
1086 // renders more directly to a HWND. Mac quicktime does not do this
1087 // and instead renders to the port of the WindowRef/WindowPtr on top
1088 // of everything else/all other windows.
1090 // So, for example, if you were to have a CreateTabsControl/wxNotebook
1091 // and change pages, even if you called HIViewSetVisible/SetControlVisibility
1092 // directly the movie will still continue playing on top of everything else
1093 // if you went to a different tab.
1095 // Note that another issue, and why we call MCSetControllerPort instead
1096 // of SetMovieGWorld directly, is that in addition to rendering on
1097 // top of everything else the last created controller steals mouse and
1098 // other input from everything else in the window, including other
1099 // controllers. Setting the port of it releases this behaviour.
1100 //---------------------------------------------------------------------------
1101 void wxQTMediaBackend::MacVisibilityChanged()
1103 if(!m_mc
|| !m_ctrl
->m_bLoaded
)
1104 return; //not initialized yet
1106 if(m_ctrl
->MacIsReallyShown())
1108 //The window is being shown again, so set the GWorld of the
1109 //controller back to the port of the parent WindowRef
1111 (WindowRef
) m_ctrl
->MacGetTopLevelWindowRef();
1113 ::MCSetControllerPort(m_mc
, (CGrafPtr
) GetWindowPort(wrTLW
));
1114 wxASSERT(::GetMoviesError() == noErr
);
1118 //We are being hidden - set the GWorld of the controller
1119 //to the offscreen GWorld
1120 ::MCSetControllerPort(m_mc
, m_movieWorld
);
1121 wxASSERT(::GetMoviesError() == noErr
);
1125 //---------------------------------------------------------------------------
1126 // wxQTMediaBackend::OnEraseBackground
1128 // Suggestion from Greg Hazel to repaint the movie when idle
1130 //---------------------------------------------------------------------------
1131 void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent
& evt
)
1133 // Work around Nasty OSX drawing bug:
1134 // http://lists.apple.com/archives/QuickTime-API/2002/Feb/msg00311.html
1135 WindowRef wrTLW
= (WindowRef
) m_qtb
->m_ctrl
->MacGetTopLevelWindowRef();
1137 RgnHandle region
= ::MCGetControllerBoundsRgn(m_qtb
->m_mc
);
1138 ::MCInvalidate(m_qtb
->m_mc
, wrTLW
, region
);
1139 ::MCIdle(m_qtb
->m_mc
);
1142 //---------------------------------------------------------------------------
1143 // wxQTMediaBackend::PPRMProc (static)
1145 // Called when done PrePrerolling the movie.
1146 // Note that in 99% of the cases this does nothing...
1147 // Anyway we set up the loading timer here to tell us when the movie is done
1148 //---------------------------------------------------------------------------
1149 pascal void wxQTMediaBackend::PPRMProc(
1151 OSErr
WXUNUSED_UNLESS_DEBUG(theErr
),
1154 wxASSERT( theMovie
);
1155 wxASSERT( theRefCon
);
1156 wxASSERT( theErr
== noErr
);
1158 wxQTMediaBackend
* pBE
= (wxQTMediaBackend
*) theRefCon
;
1160 long lTime
= ::GetMovieTime(theMovie
,NULL
);
1161 Fixed rate
= ::GetMoviePreferredRate(theMovie
);
1162 ::PrerollMovie(theMovie
,lTime
,rate
);
1163 pBE
->m_timer
= new wxQTMediaLoadTimer(pBE
);
1164 pBE
->m_timer
->Start(MOVIE_DELAY
);
1167 //---------------------------------------------------------------------------
1168 // wxQTMediaBackend::MCFilterProc (static)
1170 // Callback for when the movie controller recieves a message
1171 //---------------------------------------------------------------------------
1172 pascal Boolean
wxQTMediaBackend::MCFilterProc(
1173 MovieController
WXUNUSED(theController
),
1175 void * WXUNUSED(params
),
1178 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)refCon
;
1183 // don't process idle events
1187 // play button triggered - MC will set movie to opposite state
1188 // of current - playing ? paused : playing
1189 pThis
->m_bPlaying
= !(pThis
->m_bPlaying
);
1199 //---------------------------------------------------------------------------
1200 // wxQTMediaBackend::WindowEventHandler [static]
1202 // Event callback for the top level window of our control that passes
1203 // messages to our moviecontroller so it can receive mouse clicks etc.
1204 //---------------------------------------------------------------------------
1205 pascal OSStatus
wxQTMediaBackend::WindowEventHandler(
1206 EventHandlerCallRef inHandlerCallRef
,
1210 wxQTMediaBackend
* be
= (wxQTMediaBackend
*) inUserData
;
1212 // Only process keyboard messages on this window if it actually
1213 // has focus, otherwise it will steal keystrokes from other windows!
1214 // As well as when it is not loaded properly as it
1215 // will crash in MCIsPlayerEvent
1216 if((GetEventClass(inEvent
) == kEventClassKeyboard
&&
1217 wxWindow::FindFocus() != be
->m_ctrl
)
1218 || !be
->m_ctrl
->m_bLoaded
)
1219 return eventNotHandledErr
;
1221 // Pass the event onto the movie controller
1222 EventRecord theEvent
;
1223 ConvertEventRefToEventRecord( inEvent
, &theEvent
);
1226 // TODO: Apple says MCIsPlayerEvent is depreciated and
1227 // MCClick, MCKey, MCIdle etc. should be used
1228 // (RN: Of course that's what they say about
1229 // CreateMovieControl and HIMovieView as well, LOL!)
1230 err
= ::MCIsPlayerEvent( be
->m_mc
, &theEvent
);
1232 // Pass on to other event handlers if not handled- i.e. wx
1236 return eventNotHandledErr
;
1239 // in source file that contains stuff you don't directly use
1240 #include "wx/html/forcelnk.h"
1241 FORCE_LINK_ME(basewxmediabackends
)
1243 #endif // wxUSE_MEDIACTRL