1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mediactrl_qt.cpp
3 // Purpose: QuickTime Media Backend for Windows
4 // Author: Ryan Norton <wxprojects@comcast.net>
5 // Modified by: Robin Dunn (moved QT code from mediactrl.cpp)
9 // Copyright: (c) Ryan Norton
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 //===========================================================================
16 //===========================================================================
18 //---------------------------------------------------------------------------
19 // Pre-compiled header stuff
20 //---------------------------------------------------------------------------
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
31 #include "wx/mediactrl.h"
35 #include "wx/dcclient.h"
37 #include "wx/math.h" // log10 & pow
40 #include "wx/msw/private.h" // user info and wndproc setting/getting
41 #include "wx/dynlib.h"
43 //---------------------------------------------------------------------------
44 // Externals (somewhere in src/msw/app.cpp and src/msw/window.cpp)
45 //---------------------------------------------------------------------------
46 extern "C" WXDLLIMPEXP_BASE HINSTANCE
wxGetInstance(void);
47 extern WXDLLIMPEXP_CORE
const wxChar
*wxCanvasClassName
;
49 LRESULT WXDLLIMPEXP_CORE APIENTRY _EXPORT
wxWndProc(HWND hWnd
, UINT message
,
50 WPARAM wParam
, LPARAM lParam
);
52 //---------------------------------------------------------------------------
53 // Killed MSVC warnings
54 //---------------------------------------------------------------------------
55 //disable "cast truncates constant value" for VARIANT_BOOL values
56 //passed as parameters in VC5 and up
58 #pragma warning (disable:4310)
62 //---------------------------------------------------------------------------
65 // We don't include Quicktime headers here and define all the types
66 // ourselves because looking for the quicktime libaries etc. would
67 // be tricky to do and making this a dependency for the MSVC projects
68 // would be unrealistic.
70 // Thanks to Robert Roebling for the wxDL macro/library idea
71 //---------------------------------------------------------------------------
73 //---------------------------------------------------------------------------
75 //---------------------------------------------------------------------------
76 //#include <qtml.h> // Windoze QT include
77 //#include <QuickTimeComponents.h> // Standard QT stuff
78 #include "wx/dynlib.h"
80 //---------------------------------------------------------------------------
82 //---------------------------------------------------------------------------
83 typedef struct MovieRecord
* Movie
;
84 typedef wxInt16 OSErr
;
85 typedef wxInt32 OSStatus
;
88 typedef unsigned char Str255
[256];
89 #define StringPtr unsigned char*
90 #define newMovieActive 1
91 #define newMovieAsyncOK (1 << 8)
95 #define OSType unsigned long
96 #define CGrafPtr struct GrafPort *
97 #define TimeScale long
98 #define TimeBase struct TimeBaseRecord *
99 typedef struct ComponentInstanceRecord
* ComponentInstance
;
100 #define kMovieLoadStatePlayable 10000
102 #define MovieController ComponentInstance
104 #ifndef URLDataHandlerSubType
105 #if defined(__WATCOMC__) || defined(__MINGW32__)
106 // use magic numbers for compilers which complain about multicharacter integers
107 const OSType URLDataHandlerSubType
= 1970433056;
108 const OSType VisualMediaCharacteristic
= 1702454643;
110 const OSType URLDataHandlerSubType
= 'url ';
111 const OSType VisualMediaCharacteristic
= 'eyes';
119 Str255 name
; // Str63 on mac, Str255 on msw
139 TimeScale scale
; // units per second
161 mcScaleMovieToFit
= 2,
167 //---------------------------------------------------------------------------
169 //---------------------------------------------------------------------------
171 class WXDLLIMPEXP_MEDIA wxQuickTimeLibrary
174 ~wxQuickTimeLibrary()
176 if (m_dll
.IsLoaded())
181 bool IsOk() const {return m_ok
;}
184 wxDynamicLibrary m_dll
;
188 wxDL_VOIDMETHOD_DEFINE( StartMovie
, (Movie m
), (m
) )
189 wxDL_VOIDMETHOD_DEFINE( StopMovie
, (Movie m
), (m
) )
190 wxDL_METHOD_DEFINE( bool, IsMovieDone
, (Movie m
), (m
), false)
191 wxDL_VOIDMETHOD_DEFINE( GoToBeginningOfMovie
, (Movie m
), (m
) )
192 wxDL_METHOD_DEFINE( OSErr
, GetMoviesError
, (), (), -1)
193 wxDL_METHOD_DEFINE( OSErr
, EnterMovies
, (), (), -1)
194 wxDL_VOIDMETHOD_DEFINE( ExitMovies
, (), () )
195 wxDL_METHOD_DEFINE( OSErr
, InitializeQTML
, (long flags
), (flags
), -1)
196 wxDL_VOIDMETHOD_DEFINE( TerminateQTML
, (), () )
198 wxDL_METHOD_DEFINE( OSErr
, NativePathNameToFSSpec
,
199 (char* inName
, FSSpec
* outFile
, long flags
),
200 (inName
, outFile
, flags
), -1)
202 wxDL_METHOD_DEFINE( OSErr
, OpenMovieFile
,
203 (const FSSpec
* fileSpec
, short * resRefNum
, wxInt8 permission
),
204 (fileSpec
, resRefNum
, permission
), -1 )
206 wxDL_METHOD_DEFINE( OSErr
, CloseMovieFile
,
207 (short resRefNum
), (resRefNum
), -1)
209 wxDL_METHOD_DEFINE( OSErr
, NewMovieFromFile
,
210 (Movie
* theMovie
, short resRefNum
, short * resId
,
211 StringPtr resName
, short newMovieFlags
,
212 bool * dataRefWasChanged
),
213 (theMovie
, resRefNum
, resId
, resName
, newMovieFlags
,
214 dataRefWasChanged
), -1)
216 wxDL_VOIDMETHOD_DEFINE( SetMovieRate
, (Movie m
, Fixed rate
), (m
, rate
) )
217 wxDL_METHOD_DEFINE( Fixed
, GetMovieRate
, (Movie m
), (m
), 0)
218 wxDL_VOIDMETHOD_DEFINE( MoviesTask
, (Movie m
, long maxms
), (m
, maxms
) )
219 wxDL_VOIDMETHOD_DEFINE( BlockMove
,
220 (const char* p1
, const char* p2
, long s
), (p1
,p2
,s
) )
221 wxDL_METHOD_DEFINE( Handle
, NewHandleClear
, (long s
), (s
), NULL
)
223 wxDL_METHOD_DEFINE( OSErr
, NewMovieFromDataRef
,
224 (Movie
* m
, short flags
, short * id
,
225 Handle dataRef
, OSType dataRefType
),
226 (m
,flags
,id
,dataRef
,dataRefType
), -1 )
228 wxDL_VOIDMETHOD_DEFINE( DisposeHandle
, (Handle h
), (h
) )
229 wxDL_VOIDMETHOD_DEFINE( GetMovieNaturalBoundsRect
, (Movie m
, Rect
* r
), (m
,r
) )
230 wxDL_METHOD_DEFINE( void*, GetMovieIndTrackType
,
231 (Movie m
, long index
, OSType type
, long flags
),
232 (m
,index
,type
,flags
), NULL
)
233 wxDL_VOIDMETHOD_DEFINE( CreatePortAssociation
,
234 (void* hWnd
, void* junk
, long morejunk
), (hWnd
, junk
, morejunk
) )
235 wxDL_METHOD_DEFINE(void*, GetNativeWindowPort
, (void* hWnd
), (hWnd
), NULL
)
236 wxDL_VOIDMETHOD_DEFINE(SetMovieGWorld
, (Movie m
, CGrafPtr port
, void* whatever
),
237 (m
, port
, whatever
) )
238 wxDL_VOIDMETHOD_DEFINE(DisposeMovie
, (Movie m
), (m
) )
239 wxDL_VOIDMETHOD_DEFINE(SetMovieBox
, (Movie m
, Rect
* r
), (m
,r
))
240 wxDL_VOIDMETHOD_DEFINE(SetMovieTimeScale
, (Movie m
, long s
), (m
,s
))
241 wxDL_METHOD_DEFINE(long, GetMovieDuration
, (Movie m
), (m
), 0)
242 wxDL_METHOD_DEFINE(TimeBase
, GetMovieTimeBase
, (Movie m
), (m
), 0)
243 wxDL_METHOD_DEFINE(TimeScale
, GetMovieTimeScale
, (Movie m
), (m
), 0)
244 wxDL_METHOD_DEFINE(long, GetMovieTime
, (Movie m
, void* cruft
), (m
,cruft
), 0)
245 wxDL_VOIDMETHOD_DEFINE(SetMovieTime
, (Movie m
, TimeRecord
* tr
), (m
,tr
) )
246 wxDL_METHOD_DEFINE(short, GetMovieVolume
, (Movie m
), (m
), 0)
247 wxDL_VOIDMETHOD_DEFINE(SetMovieVolume
, (Movie m
, short sVolume
), (m
,sVolume
) )
248 wxDL_VOIDMETHOD_DEFINE(SetMovieTimeValue
, (Movie m
, long s
), (m
,s
))
249 wxDL_METHOD_DEFINE(ComponentInstance
, NewMovieController
, (Movie m
, const Rect
* mr
, long fl
), (m
,mr
,fl
), 0)
250 wxDL_VOIDMETHOD_DEFINE(DisposeMovieController
, (ComponentInstance ci
), (ci
))
251 wxDL_METHOD_DEFINE(int, MCSetVisible
, (ComponentInstance m
, int b
), (m
, b
), 0)
253 wxDL_VOIDMETHOD_DEFINE(PrePrerollMovie
, (Movie m
, long t
, Fixed r
, WXFARPROC p1
, void* p2
), (m
,t
,r
,p1
,p2
) )
254 wxDL_VOIDMETHOD_DEFINE(PrerollMovie
, (Movie m
, long t
, Fixed r
), (m
,t
,r
) )
255 wxDL_METHOD_DEFINE(Fixed
, GetMoviePreferredRate
, (Movie m
), (m
), 0)
256 wxDL_METHOD_DEFINE(long, GetMovieLoadState
, (Movie m
), (m
), 0)
257 wxDL_METHOD_DEFINE(void*, NewRoutineDescriptor
, (WXFARPROC f
, int l
, void* junk
), (f
, l
, junk
), 0)
258 wxDL_VOIDMETHOD_DEFINE(DisposeRoutineDescriptor
, (void* f
), (f
))
259 wxDL_METHOD_DEFINE(void*, GetCurrentArchitecture
, (), (), 0)
260 wxDL_METHOD_DEFINE(int, MCDoAction
, (ComponentInstance ci
, long f
, void* p
), (ci
,f
,p
), 0)
261 wxDL_VOIDMETHOD_DEFINE(MCSetControllerBoundsRect
, (ComponentInstance ci
, Rect
* r
), (ci
,r
))
262 wxDL_VOIDMETHOD_DEFINE(DestroyPortAssociation
, (CGrafPtr g
), (g
))
263 wxDL_VOIDMETHOD_DEFINE(NativeEventToMacEvent
, (MSG
* p1
, EventRecord
* p2
), (p1
,p2
))
264 wxDL_VOIDMETHOD_DEFINE(MCIsPlayerEvent
, (ComponentInstance ci
, EventRecord
* p2
), (ci
, p2
))
265 wxDL_METHOD_DEFINE(int, MCSetMovie
, (ComponentInstance ci
, Movie m
, void* p1
, Point w
),
267 wxDL_VOIDMETHOD_DEFINE(MCPositionController
,
268 (ComponentInstance ci
, Rect
* r
, void* junk
, void* morejunk
), (ci
,r
,junk
,morejunk
))
269 wxDL_VOIDMETHOD_DEFINE(MCSetActionFilterWithRefCon
,
270 (ComponentInstance ci
, WXFARPROC cb
, void* ref
), (ci
,cb
,ref
))
271 wxDL_VOIDMETHOD_DEFINE(MCGetControllerInfo
, (MovieController mc
, long* flags
), (mc
,flags
))
272 wxDL_VOIDMETHOD_DEFINE(BeginUpdate
, (CGrafPtr port
), (port
))
273 wxDL_VOIDMETHOD_DEFINE(UpdateMovie
, (Movie m
), (m
))
274 wxDL_VOIDMETHOD_DEFINE(EndUpdate
, (CGrafPtr port
), (port
))
275 wxDL_METHOD_DEFINE( OSErr
, GetMoviesStickyError
, (), (), -1)
278 bool wxQuickTimeLibrary::Initialize()
280 // Turn off the wxDynamicLibrary logging as we're prepared to handle the
284 m_ok
= m_dll
.Load(wxT("qtmlClient.dll"));
288 wxDL_METHOD_LOAD( m_dll
, StartMovie
);
289 wxDL_METHOD_LOAD( m_dll
, StopMovie
);
290 wxDL_METHOD_LOAD( m_dll
, IsMovieDone
);
291 wxDL_METHOD_LOAD( m_dll
, GoToBeginningOfMovie
);
292 wxDL_METHOD_LOAD( m_dll
, GetMoviesError
);
293 wxDL_METHOD_LOAD( m_dll
, EnterMovies
);
294 wxDL_METHOD_LOAD( m_dll
, ExitMovies
);
295 wxDL_METHOD_LOAD( m_dll
, InitializeQTML
);
296 wxDL_METHOD_LOAD( m_dll
, TerminateQTML
);
297 wxDL_METHOD_LOAD( m_dll
, NativePathNameToFSSpec
);
298 wxDL_METHOD_LOAD( m_dll
, OpenMovieFile
);
299 wxDL_METHOD_LOAD( m_dll
, CloseMovieFile
);
300 wxDL_METHOD_LOAD( m_dll
, NewMovieFromFile
);
301 wxDL_METHOD_LOAD( m_dll
, GetMovieRate
);
302 wxDL_METHOD_LOAD( m_dll
, SetMovieRate
);
303 wxDL_METHOD_LOAD( m_dll
, MoviesTask
);
304 wxDL_METHOD_LOAD( m_dll
, BlockMove
);
305 wxDL_METHOD_LOAD( m_dll
, NewHandleClear
);
306 wxDL_METHOD_LOAD( m_dll
, NewMovieFromDataRef
);
307 wxDL_METHOD_LOAD( m_dll
, DisposeHandle
);
308 wxDL_METHOD_LOAD( m_dll
, GetMovieNaturalBoundsRect
);
309 wxDL_METHOD_LOAD( m_dll
, GetMovieIndTrackType
);
310 wxDL_METHOD_LOAD( m_dll
, CreatePortAssociation
);
311 wxDL_METHOD_LOAD( m_dll
, DestroyPortAssociation
);
312 wxDL_METHOD_LOAD( m_dll
, GetNativeWindowPort
);
313 wxDL_METHOD_LOAD( m_dll
, SetMovieGWorld
);
314 wxDL_METHOD_LOAD( m_dll
, DisposeMovie
);
315 wxDL_METHOD_LOAD( m_dll
, SetMovieBox
);
316 wxDL_METHOD_LOAD( m_dll
, SetMovieTimeScale
);
317 wxDL_METHOD_LOAD( m_dll
, GetMovieDuration
);
318 wxDL_METHOD_LOAD( m_dll
, GetMovieTimeBase
);
319 wxDL_METHOD_LOAD( m_dll
, GetMovieTimeScale
);
320 wxDL_METHOD_LOAD( m_dll
, GetMovieTime
);
321 wxDL_METHOD_LOAD( m_dll
, SetMovieTime
);
322 wxDL_METHOD_LOAD( m_dll
, GetMovieVolume
);
323 wxDL_METHOD_LOAD( m_dll
, SetMovieVolume
);
324 wxDL_METHOD_LOAD( m_dll
, SetMovieTimeValue
);
325 wxDL_METHOD_LOAD( m_dll
, NewMovieController
);
326 wxDL_METHOD_LOAD( m_dll
, DisposeMovieController
);
327 wxDL_METHOD_LOAD( m_dll
, MCSetVisible
);
328 wxDL_METHOD_LOAD( m_dll
, PrePrerollMovie
);
329 wxDL_METHOD_LOAD( m_dll
, PrerollMovie
);
330 wxDL_METHOD_LOAD( m_dll
, GetMoviePreferredRate
);
331 wxDL_METHOD_LOAD( m_dll
, GetMovieLoadState
);
332 wxDL_METHOD_LOAD( m_dll
, MCDoAction
);
333 wxDL_METHOD_LOAD( m_dll
, MCSetControllerBoundsRect
);
334 wxDL_METHOD_LOAD( m_dll
, NativeEventToMacEvent
);
335 wxDL_METHOD_LOAD( m_dll
, MCIsPlayerEvent
);
336 wxDL_METHOD_LOAD( m_dll
, MCSetMovie
);
337 wxDL_METHOD_LOAD( m_dll
, MCSetActionFilterWithRefCon
);
338 wxDL_METHOD_LOAD( m_dll
, MCGetControllerInfo
);
339 wxDL_METHOD_LOAD( m_dll
, BeginUpdate
);
340 wxDL_METHOD_LOAD( m_dll
, UpdateMovie
);
341 wxDL_METHOD_LOAD( m_dll
, EndUpdate
);
342 wxDL_METHOD_LOAD( m_dll
, GetMoviesStickyError
);
347 class WXDLLIMPEXP_MEDIA wxQTMediaBackend
: public wxMediaBackendCommonBase
351 virtual ~wxQTMediaBackend();
353 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
358 const wxValidator
& validator
,
359 const wxString
& name
);
362 virtual bool Pause();
365 virtual bool Load(const wxURI
& location
,
367 { return wxMediaBackend::Load(location
, proxy
); }
369 virtual bool Load(const wxString
& fileName
);
370 virtual bool Load(const wxURI
& location
);
372 virtual wxMediaState
GetState();
374 virtual bool SetPosition(wxLongLong where
);
375 virtual wxLongLong
GetPosition();
376 virtual wxLongLong
GetDuration();
378 virtual void Move(int x
, int y
, int w
, int h
);
379 wxSize
GetVideoSize() const;
381 virtual double GetPlaybackRate();
382 virtual bool SetPlaybackRate(double dRate
);
384 virtual double GetVolume();
385 virtual bool SetVolume(double);
390 static void PPRMProc (Movie theMovie
, OSErr theErr
, void* theRefCon
);
392 // TODO: Last param actually long - does this work on 64bit machines?
393 static Boolean
MCFilterProc(MovieController theController
,
394 short action
, void *params
, LONG_PTR refCon
);
396 static LRESULT CALLBACK
QTWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
398 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags
);
400 wxSize m_bestSize
; // Original movie size
401 Movie m_movie
; // QT Movie handle/instance
402 bool m_bVideo
; // Whether or not we have video
403 bool m_bPlaying
; // Whether or not movie is playing
404 wxTimer
* m_timer
; // Load or Play timer
405 wxQuickTimeLibrary m_lib
; // DLL to load functions from
406 ComponentInstance m_pMC
; // Movie Controller
407 wxEvtHandler
* m_evthandler
;
409 friend class wxQTMediaEvtHandler
;
411 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
)
414 // helper to hijack background erasing for the QT window
415 class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler
: public wxEvtHandler
418 wxQTMediaEvtHandler(wxQTMediaBackend
*qtb
, WXHWND hwnd
)
423 m_qtb
->m_ctrl
->Connect(m_qtb
->m_ctrl
->GetId(),
424 wxEVT_ERASE_BACKGROUND
,
425 wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground
),
429 void OnEraseBackground(wxEraseEvent
& event
);
432 wxQTMediaBackend
*m_qtb
;
435 wxDECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler
);
439 //===========================================================================
441 //===========================================================================
444 //---------------------------------------------------------------------------
447 // TODO: Use a less kludgy way to pause/get state/set state
448 // FIXME: Greg Hazel reports that sometimes files that cannot be played
449 // with this backend are treated as playable anyway - not verified though.
450 //---------------------------------------------------------------------------
452 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
)
454 // Time between timer calls - this is the Apple recommendation to the TCL
456 #define MOVIE_DELAY 20
458 //---------------------------------------------------------------------------
461 // QT, esp. QT for Windows is very picky about how you go about
462 // async loading. If you were to go through a Windows message loop
463 // or a MoviesTask or both and then check the movie load state
464 // it would still return 1000 (loading)... even (pre)prerolling doesn't
465 // help. However, making a load timer like this works
466 //---------------------------------------------------------------------------
467 class wxQTLoadTimer
: public wxTimer
470 wxQTLoadTimer(Movie movie
, wxQTMediaBackend
* parent
, wxQuickTimeLibrary
* pLib
) :
471 m_movie(movie
), m_parent(parent
), m_pLib(pLib
) {}
475 m_pLib
->MoviesTask(m_movie
, 0);
476 // kMovieLoadStatePlayable
477 if (m_pLib
->GetMovieLoadState(m_movie
) >= 10000)
479 m_parent
->FinishLoad();
485 Movie m_movie
; //Our movie instance
486 wxQTMediaBackend
* m_parent
; //Backend pointer
487 wxQuickTimeLibrary
* m_pLib
; //Interfaces
491 // --------------------------------------------------------------------------
492 // wxQTPlayTimer - Handle Asyncronous Playing
494 // 1) Checks to see if the movie is done, and if not continues
495 // streaming the movie
496 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
498 // --------------------------------------------------------------------------
499 class wxQTPlayTimer
: public wxTimer
502 wxQTPlayTimer(Movie movie
, wxQTMediaBackend
* parent
,
503 wxQuickTimeLibrary
* pLib
) :
504 m_movie(movie
), m_parent(parent
), m_pLib(pLib
) {}
509 // OK, a little explaining - basically originally
510 // we only called MoviesTask if the movie was actually
511 // playing (not paused or stopped)... this was before
512 // we realized MoviesTask actually handles repainting
513 // of the current frame - so if you were to resize
514 // or something it would previously not redraw that
515 // portion of the movie.
517 // So now we call MoviesTask always so that it repaints
520 m_pLib
->MoviesTask(m_movie
, 0);
523 // Handle the stop event - if the movie has reached
524 // the end, notify our handler
526 // m_bPlaying == !(Stopped | Paused)
528 if (m_parent
->m_bPlaying
)
530 if (m_pLib
->IsMovieDone(m_movie
))
532 if ( m_parent
->SendStopEvent() )
535 wxASSERT(m_pLib
->GetMoviesError() == noErr
);
537 m_parent
->QueueFinishEvent();
544 Movie m_movie
; // Our movie instance
545 wxQTMediaBackend
* m_parent
; //Backend pointer
546 wxQuickTimeLibrary
* m_pLib
; //Interfaces
550 //---------------------------------------------------------------------------
551 // wxQTMediaBackend::QTWndProc
553 // Forwards events to the Movie Controller so that it can
554 // redraw itself/process messages etc..
555 //---------------------------------------------------------------------------
556 LRESULT CALLBACK
wxQTMediaBackend::QTWndProc(HWND hWnd
, UINT nMsg
,
557 WPARAM wParam
, LPARAM lParam
)
559 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)wxGetWindowUserData(hWnd
);
569 EventRecord theEvent
;
570 pThis
->m_lib
.NativeEventToMacEvent(&msg
, &theEvent
);
571 pThis
->m_lib
.MCIsPlayerEvent(pThis
->m_pMC
, &theEvent
);
573 return pThis
->m_ctrl
->MSWWindowProc(nMsg
, wParam
, lParam
);
576 //---------------------------------------------------------------------------
577 // wxQTMediaBackend Destructor
579 // Sets m_timer to NULL signifying we havn't loaded anything yet
580 //---------------------------------------------------------------------------
581 wxQTMediaBackend::wxQTMediaBackend()
582 : m_movie(NULL
), m_bPlaying(false), m_timer(NULL
), m_pMC(NULL
)
587 //---------------------------------------------------------------------------
588 // wxQTMediaBackend Destructor
590 // 1) Cleans up the QuickTime movie instance
591 // 2) Decrements the QuickTime reference counter - if this reaches
592 // 0, QuickTime shuts down
593 // 3) Decrements the QuickTime Windows Media Layer reference counter -
594 // if this reaches 0, QuickTime shuts down the Windows Media Layer
595 //---------------------------------------------------------------------------
596 wxQTMediaBackend::~wxQTMediaBackend()
605 m_lib
.DisposeMovieController(m_pMC
);
609 // destroy wxQTMediaEvtHandler we pushed on it
612 m_ctrl
->RemoveEventHandler(m_evthandler
);
616 m_lib
.DestroyPortAssociation(
617 (CGrafPtr
)m_lib
.GetNativeWindowPort(m_ctrl
->GetHWND()));
619 //Note that ExitMovies() is not necessary, but
620 //the docs are fuzzy on whether or not TerminateQTML is
622 m_lib
.TerminateQTML();
626 //---------------------------------------------------------------------------
627 // wxQTMediaBackend::CreateControl
629 // 1) Intializes QuickTime
630 // 2) Creates the control window
631 //---------------------------------------------------------------------------
632 bool wxQTMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
637 const wxValidator
& validator
,
638 const wxString
& name
)
640 if (!m_lib
.Initialize())
643 int nError
= m_lib
.InitializeQTML(0);
644 if (nError
!= noErr
) //-2093 no dll
646 wxFAIL_MSG(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
653 // By default wxWindow(s) is created with a border -
654 // so we need to get rid of those
656 // Since we don't have a child window like most other
657 // backends, we don't need wxCLIP_CHILDREN
658 if ( !ctrl
->wxControl::Create(parent
, id
, pos
, size
,
659 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
,
665 m_ctrl
= wxStaticCast(ctrl
, wxMediaCtrl
);
667 // Create a port association for our window so we
668 // can use it as a WindowRef
669 m_lib
.CreatePortAssociation(m_ctrl
->GetHWND(), NULL
, 0L);
671 // Part of a suggestion from Greg Hazel
672 // to repaint movie when idle
673 m_evthandler
= new wxQTMediaEvtHandler(this, m_ctrl
->GetHWND());
674 m_ctrl
->PushEventHandler(m_evthandler
);
680 //---------------------------------------------------------------------------
681 // wxQTMediaBackend::Load (file version)
683 // 1) Get an FSSpec from the Windows path name
685 // 3) Obtain the movie instance from the movie resource
686 // 4) Close the movie resource
688 //---------------------------------------------------------------------------
689 bool wxQTMediaBackend::Load(const wxString
& fileName
)
694 short movieResFile
= 0; //= 0 because of annoying VC6 warning
697 OSErr err
= m_lib
.NativePathNameToFSSpec(
698 (char*) (const char*) fileName
.mb_str(),
700 bool result
= (err
== noErr
);
704 err
= m_lib
.OpenMovieFile(&sfFile
, &movieResFile
, fsRdPerm
);
705 result
= (err
== noErr
);
710 short movieResID
= 0;
713 err
= m_lib
.NewMovieFromFile(
719 NULL
); // wasChanged
720 result
= (err
== noErr
/*&& m_lib.GetMoviesStickyError() == noErr*/);
722 // check m_lib.GetMoviesStickyError() because it may not find the
723 // proper codec and play black video and other strange effects,
724 // not to mention mess up the dynamic backend loading scheme
725 // of wxMediaCtrl - so it just does what the QuickTime player does
728 m_lib
.CloseMovieFile(movieResFile
);
736 //---------------------------------------------------------------------------
737 // wxQTMediaBackend::PPRMProc (static)
739 // Called when done PrePrerolling the movie.
740 // Note that in 99% of the cases this does nothing...
741 // Anyway we set up the loading timer here to tell us when the movie is done
742 //---------------------------------------------------------------------------
743 void wxQTMediaBackend::PPRMProc (Movie theMovie
,
744 OSErr
WXUNUSED_UNLESS_DEBUG(theErr
),
747 wxASSERT( theMovie
);
748 wxASSERT( theRefCon
);
749 wxASSERT( theErr
== noErr
);
751 wxQTMediaBackend
* pBE
= (wxQTMediaBackend
*) theRefCon
;
753 long lTime
= pBE
->m_lib
.GetMovieTime(theMovie
,NULL
);
754 Fixed rate
= pBE
->m_lib
.GetMoviePreferredRate(theMovie
);
755 pBE
->m_lib
.PrerollMovie(theMovie
, lTime
, rate
);
756 pBE
->m_timer
= new wxQTLoadTimer(pBE
->m_movie
, pBE
, &pBE
->m_lib
);
757 pBE
->m_timer
->Start(MOVIE_DELAY
);
760 //---------------------------------------------------------------------------
761 // wxQTMediaBackend::Load (URL Version)
763 // 1) Build an escaped URI from location
764 // 2) Create a handle to store the URI string
765 // 3) Put the URI string inside the handle
766 // 4) Make a QuickTime URL data ref from the handle with the URI in it
767 // 5) Clean up the URI string handle
768 // 6) Do some prerolling
770 //---------------------------------------------------------------------------
771 bool wxQTMediaBackend::Load(const wxURI
& location
)
776 wxString theURI
= location
.BuildURI();
778 Handle theHandle
= m_lib
.NewHandleClear(theURI
.length() + 1);
781 m_lib
.BlockMove(theURI
.mb_str(), *theHandle
, theURI
.length() + 1);
783 // create the movie from the handle that refers to the URI
784 OSErr err
= m_lib
.NewMovieFromDataRef(&m_movie
, newMovieActive
|
786 /* | newMovieIdleImportOK */,
788 URLDataHandlerSubType
);
790 m_lib
.DisposeHandle(theHandle
);
797 timeNow
= m_lib
.GetMovieTime(m_movie
, NULL
);
798 wxASSERT(m_lib
.GetMoviesError() == noErr
);
800 playRate
= m_lib
.GetMoviePreferredRate(m_movie
);
801 wxASSERT(m_lib
.GetMoviesError() == noErr
);
803 // Note that the callback here is optional,
804 // but without it PrePrerollMovie can be buggy
805 // (see Apple ml). Also, some may wonder
806 // why we need this at all - this is because
807 // Apple docs say QuickTime streamed movies
808 // require it if you don't use a Movie Controller,
809 // which we don't by default.
811 m_lib
.PrePrerollMovie(m_movie
, timeNow
, playRate
,
812 (WXFARPROC
)wxQTMediaBackend::PPRMProc
,
821 //---------------------------------------------------------------------------
822 // wxQTMediaBackend::FinishLoad
824 // 1) Create the movie timer
825 // 2) Get real size of movie for GetBestSize/sizers
826 // 3) Set the movie time scale to something usable so that seeking
827 // etc. will work correctly
828 // 4) Set our Movie Controller to display the movie if it exists,
829 // otherwise set the bounds of the Movie
830 // 5) Refresh parent window
831 //---------------------------------------------------------------------------
832 void wxQTMediaBackend::FinishLoad()
834 // Create the playing/streaming timer
835 m_timer
= new wxQTPlayTimer(m_movie
, (wxQTMediaBackend
*) this, &m_lib
);
838 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
840 // get the real size of the movie
842 memset(&outRect
, 0, sizeof(Rect
)); // suppress annoying VC6 warning
843 m_lib
.GetMovieNaturalBoundsRect (m_movie
, &outRect
);
844 wxASSERT(m_lib
.GetMoviesError() == noErr
);
846 m_bestSize
.x
= outRect
.right
- outRect
.left
;
847 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
849 // Handle the movie GWorld
853 thePoint
.h
= thePoint
.v
= 0;
854 m_lib
.MCSetMovie(m_pMC
, m_movie
,
855 m_lib
.GetNativeWindowPort(m_ctrl
->GetHandle()),
857 m_lib
.MCSetVisible(m_pMC
, true);
862 m_lib
.SetMovieGWorld(m_movie
,
863 (CGrafPtr
) m_lib
.GetNativeWindowPort(m_ctrl
->GetHWND()),
867 // Set the movie to millisecond precision
868 m_lib
.SetMovieTimeScale(m_movie
, 1000);
869 wxASSERT(m_lib
.GetMoviesError() == noErr
);
874 //---------------------------------------------------------------------------
875 // wxQTMediaBackend::Play
877 // 1) Start the QT movie
878 // 2) Start the movie loading timer
880 // NOTE: This will still return success even when
881 // the movie is still loading, and as mentioned in wxQTLoadTimer
882 // I don't know of a way to force this to be sync - so if its
883 // still loading the function will return true but the movie will
884 // still be in the stopped state
885 //---------------------------------------------------------------------------
886 bool wxQTMediaBackend::Play()
888 m_lib
.StartMovie(m_movie
);
891 return m_lib
.GetMoviesError() == noErr
;
894 //---------------------------------------------------------------------------
895 // wxQTMediaBackend::Pause
898 // 2) Stop the movie timer
899 //---------------------------------------------------------------------------
900 bool wxQTMediaBackend::Pause()
903 m_lib
.StopMovie(m_movie
);
905 return m_lib
.GetMoviesError() == noErr
;
908 //---------------------------------------------------------------------------
909 // wxQTMediaBackend::Stop
912 // 2) Stop the movie timer
913 // 3) Seek to the beginning of the movie
914 //---------------------------------------------------------------------------
915 bool wxQTMediaBackend::Stop()
919 m_lib
.StopMovie(m_movie
);
920 if (m_lib
.GetMoviesError() == noErr
)
921 m_lib
.GoToBeginningOfMovie(m_movie
);
923 return m_lib
.GetMoviesError() == noErr
;
926 //---------------------------------------------------------------------------
927 // wxQTMediaBackend::GetPlaybackRate
929 // Get the movie playback rate from ::GetMovieRate
930 //---------------------------------------------------------------------------
931 double wxQTMediaBackend::GetPlaybackRate()
933 return ( ((double)m_lib
.GetMovieRate(m_movie
)) / 0x10000);
936 //---------------------------------------------------------------------------
937 // wxQTMediaBackend::SetPlaybackRate
939 // Convert dRate to Fixed and Set the movie rate through SetMovieRate
940 //---------------------------------------------------------------------------
941 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
943 m_lib
.SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
945 return m_lib
.GetMoviesError() == noErr
;
948 //---------------------------------------------------------------------------
949 // wxQTMediaBackend::SetPosition
951 // 1) Create a time record struct (TimeRecord) with appropriate values
952 // 2) Pass struct to SetMovieTime
953 //---------------------------------------------------------------------------
954 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
956 // NB: For some reason SetMovieTime does not work
957 // correctly with the Quicktime Windows SDK (6)
958 // From Muskelkatermann at the wxForum
959 // http://www.solidsteel.nl/users/wxwidgets/viewtopic.php?t=2957
960 // RN - note that I have not verified this but there
961 // is no harm in calling SetMovieTimeValue instead
963 TimeRecord theTimeRecord
;
964 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
965 theTimeRecord
.value
.lo
= where
.GetLo();
966 theTimeRecord
.scale
= m_lib
.GetMovieTimeScale(m_movie
);
967 theTimeRecord
.base
= m_lib
.GetMovieTimeBase(m_movie
);
968 m_lib
.SetMovieTime(m_movie
, &theTimeRecord
);
970 m_lib
.SetMovieTimeValue(m_movie
, where
.GetLo());
973 return (m_lib
.GetMoviesError() == noErr
);
976 //---------------------------------------------------------------------------
977 // wxQTMediaBackend::GetPosition
979 // 1) Calls GetMovieTime to get the position we are in in the movie
980 // in milliseconds (we called
981 //---------------------------------------------------------------------------
982 wxLongLong
wxQTMediaBackend::GetPosition()
984 return m_lib
.GetMovieTime(m_movie
, NULL
);
987 //---------------------------------------------------------------------------
988 // wxQTMediaBackend::GetVolume
990 // Gets the volume through GetMovieVolume - which returns a 16 bit short -
992 // +--------+--------+
994 // +--------+--------+
996 // (1) first 8 bits are value before decimal
997 // (2) second 8 bits are value after decimal
999 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
1000 // 1 (full gain and sound)
1001 //---------------------------------------------------------------------------
1002 double wxQTMediaBackend::GetVolume()
1004 short sVolume
= m_lib
.GetMovieVolume(m_movie
);
1005 wxASSERT(m_lib
.GetMoviesError() == noErr
);
1007 if (sVolume
& (128 << 8)) //negative - no sound
1010 return sVolume
/ 256.0;
1013 //---------------------------------------------------------------------------
1014 // wxQTMediaBackend::SetVolume
1016 // Sets the volume through SetMovieVolume - which takes a 16 bit short -
1018 // +--------+--------+
1020 // +--------+--------+
1022 // (1) first 8 bits are value before decimal
1023 // (2) second 8 bits are value after decimal
1025 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
1026 // 1 (full gain and sound)
1027 //---------------------------------------------------------------------------
1028 bool wxQTMediaBackend::SetVolume(double dVolume
)
1030 m_lib
.SetMovieVolume(m_movie
, (short) (dVolume
* 256));
1031 return m_lib
.GetMoviesError() == noErr
;
1034 //---------------------------------------------------------------------------
1035 // wxQTMediaBackend::GetDuration
1037 // Calls GetMovieDuration
1038 //---------------------------------------------------------------------------
1039 wxLongLong
wxQTMediaBackend::GetDuration()
1041 return m_lib
.GetMovieDuration(m_movie
);
1044 //---------------------------------------------------------------------------
1045 // wxQTMediaBackend::GetState
1047 // Determines the current state:
1048 // if we are at the beginning, then we are stopped
1049 //---------------------------------------------------------------------------
1050 wxMediaState
wxQTMediaBackend::GetState()
1053 return wxMEDIASTATE_PLAYING
;
1054 else if ( !m_movie
|| wxQTMediaBackend::GetPosition() == 0 )
1055 return wxMEDIASTATE_STOPPED
;
1057 return wxMEDIASTATE_PAUSED
;
1060 //---------------------------------------------------------------------------
1061 // wxQTMediaBackend::Cleanup
1063 // Diposes of the movie timer, Disassociates the Movie Controller with
1064 // movie and hides it if it exists, and stops and disposes
1066 //---------------------------------------------------------------------------
1067 void wxQTMediaBackend::Cleanup()
1073 m_lib
.StopMovie(m_movie
);
1078 thePoint
.h
= thePoint
.v
= 0;
1079 m_lib
.MCSetVisible(m_pMC
, false);
1080 m_lib
.MCSetMovie(m_pMC
, NULL
, NULL
, thePoint
);
1083 m_lib
.DisposeMovie(m_movie
);
1087 //---------------------------------------------------------------------------
1088 // wxQTMediaBackend::ShowPlayerControls
1090 // Creates a movie controller for the Movie if the user wants it
1091 //---------------------------------------------------------------------------
1092 bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags
)
1096 // restore old wndproc
1097 wxSetWindowProc((HWND
)m_ctrl
->GetHWND(), wxWndProc
);
1098 m_lib
.DisposeMovieController(m_pMC
);
1101 // movie controller height
1105 if (flags
&& m_movie
)
1108 wxRect wxrect
= m_ctrl
->GetClientRect();
1110 // make room for controller
1111 if (wxrect
.width
< 320)
1114 rect
.top
= (short)wxrect
.y
;
1115 rect
.left
= (short)wxrect
.x
;
1116 rect
.right
= (short)(rect
.left
+ wxrect
.width
);
1117 rect
.bottom
= (short)(rect
.top
+ wxrect
.height
);
1121 m_pMC
= m_lib
.NewMovieController(m_movie
, &rect
, mcTopLeftMovie
|
1122 // mcScaleMovieToFit |
1125 m_lib
.MCDoAction(m_pMC
, 32, (void*)true); // mcActionSetKeysEnabled
1126 m_lib
.MCSetActionFilterWithRefCon(m_pMC
,
1127 (WXFARPROC
)wxQTMediaBackend::MCFilterProc
, (void*)this);
1128 m_bestSize
.y
+= 16; // movie controller height
1130 // By default the movie controller uses its own colour palette
1131 // for the movie which can be bad on some files, so turn it off.
1132 // Also turn off its frame / border for the movie
1133 // Also take care of a couple of the interface flags here
1135 m_lib
.MCDoAction(m_pMC
, 39/*mcActionGetFlags*/, (void*)&mcFlags
);
1138 // (1<< 0) /*mcFlagSuppressMovieFrame*/ |
1139 (1<< 3) /*mcFlagsUseWindowPalette*/
1140 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_STEP
)
1141 ? 0 : (1<< 1) /*mcFlagSuppressStepButtons*/)
1142 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_VOLUME
)
1143 ? 0 : (1<< 2) /*mcFlagSuppressSpeakerButton*/)
1144 // | (1<< 4) /*mcFlagDontInvalidate*/ // if we take care of repainting ourselves
1147 m_lib
.MCDoAction(m_pMC
, 38/*mcActionSetFlags*/, wxUIntToPtr(mcFlags
));
1149 // intercept the wndproc of our control window
1150 wxSetWindowProc((HWND
)m_ctrl
->GetHWND(), wxQTMediaBackend::QTWndProc
);
1152 // set the user data of our window
1153 wxSetWindowUserData((HWND
)m_ctrl
->GetHWND(), this);
1157 NotifyMovieSizeChanged();
1159 return m_lib
.GetMoviesError() == noErr
;
1162 //---------------------------------------------------------------------------
1163 // wxQTMediaBackend::MCFilterProc (static)
1165 // Callback for when the movie controller recieves a message
1166 //---------------------------------------------------------------------------
1167 Boolean
wxQTMediaBackend::MCFilterProc(MovieController
WXUNUSED(theController
),
1169 void * WXUNUSED(params
),
1172 // NB: potential optimisation
1176 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)refCon
;
1181 // don't process idle events
1185 // play button triggered - MC will set movie to opposite state
1186 // of current - playing ? paused : playing
1188 pThis
->m_bPlaying
= !(pThis
->m_bPlaying
);
1190 // NB: Sometimes it doesn't redraw properly -
1191 // if you click on the button but don't move the mouse
1192 // the button will not change its state until you move
1193 // mcActionDraw and Refresh/Update combo do nothing
1194 // to help this unfortunately
1204 //---------------------------------------------------------------------------
1205 // wxQTMediaBackend::GetVideoSize
1207 // Returns the actual size of the QT movie
1208 //---------------------------------------------------------------------------
1209 wxSize
wxQTMediaBackend::GetVideoSize() const
1214 //---------------------------------------------------------------------------
1215 // wxQTMediaBackend::Move
1217 // Sets the bounds of either the Movie or Movie Controller
1218 //---------------------------------------------------------------------------
1219 void wxQTMediaBackend::Move(int WXUNUSED(x
), int WXUNUSED(y
), int w
, int h
)
1223 // make room for controller
1229 Rect theRect
= {0, 0, (short)h
, (short)w
};
1230 m_lib
.MCSetControllerBoundsRect(m_pMC
, &theRect
);
1234 Rect theRect
= {0, 0, (short)h
, (short)w
};
1235 m_lib
.SetMovieBox(m_movie
, &theRect
);
1238 wxASSERT(m_lib
.GetMoviesError() == noErr
);
1242 //---------------------------------------------------------------------------
1243 // wxQTMediaBackend::OnEraseBackground
1245 // Suggestion from Greg Hazel to repaint the movie when idle
1248 // TODO: We may be repainting too much here - under what exact circumstances
1249 // do we need this? I think Move also repaints correctly for the Movie
1250 // Controller, so in that instance we don't need this either
1251 //---------------------------------------------------------------------------
1252 void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent
& evt
)
1254 wxQuickTimeLibrary
& m_pLib
= m_qtb
->m_lib
;
1258 // repaint movie controller
1259 m_pLib
.MCDoAction(m_qtb
->m_pMC
, 2 /*mcActionDraw*/,
1260 m_pLib
.GetNativeWindowPort(m_hwnd
));
1262 else if ( m_qtb
->m_movie
)
1264 // no movie controller
1265 CGrafPtr port
= (CGrafPtr
)m_pLib
.GetNativeWindowPort(m_hwnd
);
1267 m_pLib
.BeginUpdate(port
);
1268 m_pLib
.UpdateMovie(m_qtb
->m_movie
);
1269 wxASSERT(m_pLib
.GetMoviesError() == noErr
);
1270 m_pLib
.EndUpdate(port
);
1275 // let the system repaint the window
1280 //---------------------------------------------------------------------------
1282 //---------------------------------------------------------------------------
1284 // Allow the user code to use wxFORCE_LINK_MODULE() to ensure that this object
1285 // file is not discarded by the linker.
1286 #include "wx/link.h"
1287 wxFORCE_LINK_THIS_MODULE(wxmediabackend_qt
)
1289 #endif // wxUSE_MEDIACTRL && wxUSE_ACTIVEX