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);
48 extern WXDLLIMPEXP_CORE wxChar
*wxCanvasClassName
;
50 extern WXDLLIMPEXP_CORE
const wxChar
*wxCanvasClassName
;
53 LRESULT WXDLLIMPEXP_CORE APIENTRY _EXPORT
wxWndProc(HWND hWnd
, UINT message
,
54 WPARAM wParam
, LPARAM lParam
);
56 //---------------------------------------------------------------------------
57 // Killed MSVC warnings
58 //---------------------------------------------------------------------------
59 //disable "cast truncates constant value" for VARIANT_BOOL values
60 //passed as parameters in VC5 and up
62 #pragma warning (disable:4310)
66 //---------------------------------------------------------------------------
69 // We don't include Quicktime headers here and define all the types
70 // ourselves because looking for the quicktime libaries etc. would
71 // be tricky to do and making this a dependency for the MSVC projects
72 // would be unrealistic.
74 // Thanks to Robert Roebling for the wxDL macro/library idea
75 //---------------------------------------------------------------------------
77 //---------------------------------------------------------------------------
79 //---------------------------------------------------------------------------
80 //#include <qtml.h> // Windoze QT include
81 //#include <QuickTimeComponents.h> // Standard QT stuff
82 #include "wx/dynlib.h"
84 //---------------------------------------------------------------------------
86 //---------------------------------------------------------------------------
87 typedef struct MovieRecord
* Movie
;
88 typedef wxInt16 OSErr
;
89 typedef wxInt32 OSStatus
;
92 typedef unsigned char Str255
[256];
93 #define StringPtr unsigned char*
94 #define newMovieActive 1
95 #define newMovieAsyncOK (1 << 8)
99 #define OSType unsigned long
100 #define CGrafPtr struct GrafPort *
101 #define TimeScale long
102 #define TimeBase struct TimeBaseRecord *
103 typedef struct ComponentInstanceRecord
* ComponentInstance
;
104 #define kMovieLoadStatePlayable 10000
106 #define MovieController ComponentInstance
108 #ifndef URLDataHandlerSubType
109 #if defined(__WATCOMC__) || defined(__MINGW32__)
110 // use magic numbers for compilers which complain about multicharacter integers
111 const OSType URLDataHandlerSubType
= 1970433056;
112 const OSType VisualMediaCharacteristic
= 1702454643;
114 const OSType URLDataHandlerSubType
= 'url ';
115 const OSType VisualMediaCharacteristic
= 'eyes';
123 Str255 name
; // Str63 on mac, Str255 on msw
143 TimeScale scale
; // units per second
165 mcScaleMovieToFit
= 2,
171 //---------------------------------------------------------------------------
173 //---------------------------------------------------------------------------
174 #define wxDL_METHOD_DEFINE( rettype, name, args, shortargs, defret ) \
175 typedef rettype (* name ## Type) args ; \
176 name ## Type pfn_ ## name; \
178 { if (m_ok) return pfn_ ## name shortargs ; return defret; }
180 #define wxDL_VOIDMETHOD_DEFINE( name, args, shortargs ) \
181 typedef void (* name ## Type) args ; \
182 name ## Type pfn_ ## name; \
184 { if (m_ok) pfn_ ## name shortargs ; }
186 #define wxDL_METHOD_LOAD( lib, name, success ) \
187 pfn_ ## name = (name ## Type) lib.GetSymbol( wxT(#name), &success ); \
188 if (!success) return false
191 class WXDLLIMPEXP_MEDIA wxQuickTimeLibrary
194 ~wxQuickTimeLibrary()
196 if (m_dll
.IsLoaded())
201 bool IsOk() const {return m_ok
;}
204 wxDynamicLibrary m_dll
;
208 wxDL_VOIDMETHOD_DEFINE( StartMovie
, (Movie m
), (m
) );
209 wxDL_VOIDMETHOD_DEFINE( StopMovie
, (Movie m
), (m
) );
210 wxDL_METHOD_DEFINE( bool, IsMovieDone
, (Movie m
), (m
), false);
211 wxDL_VOIDMETHOD_DEFINE( GoToBeginningOfMovie
, (Movie m
), (m
) );
212 wxDL_METHOD_DEFINE( OSErr
, GetMoviesError
, (), (), -1);
213 wxDL_METHOD_DEFINE( OSErr
, EnterMovies
, (), (), -1);
214 wxDL_VOIDMETHOD_DEFINE( ExitMovies
, (), () );
215 wxDL_METHOD_DEFINE( OSErr
, InitializeQTML
, (long flags
), (flags
), -1);
216 wxDL_VOIDMETHOD_DEFINE( TerminateQTML
, (), () );
218 wxDL_METHOD_DEFINE( OSErr
, NativePathNameToFSSpec
,
219 (char* inName
, FSSpec
* outFile
, long flags
),
220 (inName
, outFile
, flags
), -1);
222 wxDL_METHOD_DEFINE( OSErr
, OpenMovieFile
,
223 (const FSSpec
* fileSpec
, short * resRefNum
, wxInt8 permission
),
224 (fileSpec
, resRefNum
, permission
), -1 );
226 wxDL_METHOD_DEFINE( OSErr
, CloseMovieFile
,
227 (short resRefNum
), (resRefNum
), -1);
229 wxDL_METHOD_DEFINE( OSErr
, NewMovieFromFile
,
230 (Movie
* theMovie
, short resRefNum
, short * resId
,
231 StringPtr resName
, short newMovieFlags
,
232 bool * dataRefWasChanged
),
233 (theMovie
, resRefNum
, resId
, resName
, newMovieFlags
,
234 dataRefWasChanged
), -1);
236 wxDL_VOIDMETHOD_DEFINE( SetMovieRate
, (Movie m
, Fixed rate
), (m
, rate
) );
237 wxDL_METHOD_DEFINE( Fixed
, GetMovieRate
, (Movie m
), (m
), 0);
238 wxDL_VOIDMETHOD_DEFINE( MoviesTask
, (Movie m
, long maxms
), (m
, maxms
) );
239 wxDL_VOIDMETHOD_DEFINE( BlockMove
,
240 (const char* p1
, const char* p2
, long s
), (p1
,p2
,s
) );
241 wxDL_METHOD_DEFINE( Handle
, NewHandleClear
, (long s
), (s
), NULL
);
243 wxDL_METHOD_DEFINE( OSErr
, NewMovieFromDataRef
,
244 (Movie
* m
, short flags
, short * id
,
245 Handle dataRef
, OSType dataRefType
),
246 (m
,flags
,id
,dataRef
,dataRefType
), -1 );
248 wxDL_VOIDMETHOD_DEFINE( DisposeHandle
, (Handle h
), (h
) );
249 wxDL_VOIDMETHOD_DEFINE( GetMovieNaturalBoundsRect
, (Movie m
, Rect
* r
), (m
,r
) );
250 wxDL_METHOD_DEFINE( void*, GetMovieIndTrackType
,
251 (Movie m
, long index
, OSType type
, long flags
),
252 (m
,index
,type
,flags
), NULL
);
253 wxDL_VOIDMETHOD_DEFINE( CreatePortAssociation
,
254 (void* hWnd
, void* junk
, long morejunk
), (hWnd
, junk
, morejunk
) );
255 wxDL_METHOD_DEFINE(void*, GetNativeWindowPort
, (void* hWnd
), (hWnd
), NULL
);
256 wxDL_VOIDMETHOD_DEFINE(SetMovieGWorld
, (Movie m
, CGrafPtr port
, void* whatever
),
257 (m
, port
, whatever
) );
258 wxDL_VOIDMETHOD_DEFINE(DisposeMovie
, (Movie m
), (m
) );
259 wxDL_VOIDMETHOD_DEFINE(SetMovieBox
, (Movie m
, Rect
* r
), (m
,r
));
260 wxDL_VOIDMETHOD_DEFINE(SetMovieTimeScale
, (Movie m
, long s
), (m
,s
));
261 wxDL_METHOD_DEFINE(long, GetMovieDuration
, (Movie m
), (m
), 0);
262 wxDL_METHOD_DEFINE(TimeBase
, GetMovieTimeBase
, (Movie m
), (m
), 0);
263 wxDL_METHOD_DEFINE(TimeScale
, GetMovieTimeScale
, (Movie m
), (m
), 0);
264 wxDL_METHOD_DEFINE(long, GetMovieTime
, (Movie m
, void* cruft
), (m
,cruft
), 0);
265 wxDL_VOIDMETHOD_DEFINE(SetMovieTime
, (Movie m
, TimeRecord
* tr
), (m
,tr
) );
266 wxDL_METHOD_DEFINE(short, GetMovieVolume
, (Movie m
), (m
), 0);
267 wxDL_VOIDMETHOD_DEFINE(SetMovieVolume
, (Movie m
, short sVolume
), (m
,sVolume
) );
268 wxDL_VOIDMETHOD_DEFINE(SetMovieTimeValue
, (Movie m
, long s
), (m
,s
));
269 wxDL_METHOD_DEFINE(ComponentInstance
, NewMovieController
, (Movie m
, const Rect
* mr
, long fl
), (m
,mr
,fl
), 0);
270 wxDL_VOIDMETHOD_DEFINE(DisposeMovieController
, (ComponentInstance ci
), (ci
));
271 wxDL_METHOD_DEFINE(int, MCSetVisible
, (ComponentInstance m
, int b
), (m
, b
), 0);
273 wxDL_VOIDMETHOD_DEFINE(PrePrerollMovie
, (Movie m
, long t
, Fixed r
, WXFARPROC p1
, void* p2
), (m
,t
,r
,p1
,p2
) );
274 wxDL_VOIDMETHOD_DEFINE(PrerollMovie
, (Movie m
, long t
, Fixed r
), (m
,t
,r
) );
275 wxDL_METHOD_DEFINE(Fixed
, GetMoviePreferredRate
, (Movie m
), (m
), 0);
276 wxDL_METHOD_DEFINE(long, GetMovieLoadState
, (Movie m
), (m
), 0);
277 wxDL_METHOD_DEFINE(void*, NewRoutineDescriptor
, (WXFARPROC f
, int l
, void* junk
), (f
, l
, junk
), 0);
278 wxDL_VOIDMETHOD_DEFINE(DisposeRoutineDescriptor
, (void* f
), (f
));
279 wxDL_METHOD_DEFINE(void*, GetCurrentArchitecture
, (), (), 0);
280 wxDL_METHOD_DEFINE(int, MCDoAction
, (ComponentInstance ci
, long f
, void* p
), (ci
,f
,p
), 0);
281 wxDL_VOIDMETHOD_DEFINE(MCSetControllerBoundsRect
, (ComponentInstance ci
, Rect
* r
), (ci
,r
));
282 wxDL_VOIDMETHOD_DEFINE(DestroyPortAssociation
, (CGrafPtr g
), (g
));
283 wxDL_VOIDMETHOD_DEFINE(NativeEventToMacEvent
, (MSG
* p1
, EventRecord
* p2
), (p1
,p2
));
284 wxDL_VOIDMETHOD_DEFINE(MCIsPlayerEvent
, (ComponentInstance ci
, EventRecord
* p2
), (ci
, p2
));
285 wxDL_METHOD_DEFINE(int, MCSetMovie
, (ComponentInstance ci
, Movie m
, void* p1
, Point w
),
287 wxDL_VOIDMETHOD_DEFINE(MCPositionController
,
288 (ComponentInstance ci
, Rect
* r
, void* junk
, void* morejunk
), (ci
,r
,junk
,morejunk
));
289 wxDL_VOIDMETHOD_DEFINE(MCSetActionFilterWithRefCon
,
290 (ComponentInstance ci
, WXFARPROC cb
, void* ref
), (ci
,cb
,ref
));
291 wxDL_VOIDMETHOD_DEFINE(MCGetControllerInfo
, (MovieController mc
, long* flags
), (mc
,flags
));
292 wxDL_VOIDMETHOD_DEFINE(BeginUpdate
, (CGrafPtr port
), (port
));
293 wxDL_VOIDMETHOD_DEFINE(UpdateMovie
, (Movie m
), (m
));
294 wxDL_VOIDMETHOD_DEFINE(EndUpdate
, (CGrafPtr port
), (port
));
295 wxDL_METHOD_DEFINE( OSErr
, GetMoviesStickyError
, (), (), -1);
298 bool wxQuickTimeLibrary::Initialize()
302 // Turn off the wxDynamicLibrary logging as we're prepared to handle the
306 if (!m_dll
.Load(wxT("qtmlClient.dll")))
311 wxDL_METHOD_LOAD( m_dll
, StartMovie
, m_ok
);
312 wxDL_METHOD_LOAD( m_dll
, StopMovie
, m_ok
);
313 wxDL_METHOD_LOAD( m_dll
, IsMovieDone
, m_ok
);
314 wxDL_METHOD_LOAD( m_dll
, GoToBeginningOfMovie
, m_ok
);
315 wxDL_METHOD_LOAD( m_dll
, GetMoviesError
, m_ok
);
316 wxDL_METHOD_LOAD( m_dll
, EnterMovies
, m_ok
);
317 wxDL_METHOD_LOAD( m_dll
, ExitMovies
, m_ok
);
318 wxDL_METHOD_LOAD( m_dll
, InitializeQTML
, m_ok
);
319 wxDL_METHOD_LOAD( m_dll
, TerminateQTML
, m_ok
);
320 wxDL_METHOD_LOAD( m_dll
, NativePathNameToFSSpec
, m_ok
);
321 wxDL_METHOD_LOAD( m_dll
, OpenMovieFile
, m_ok
);
322 wxDL_METHOD_LOAD( m_dll
, CloseMovieFile
, m_ok
);
323 wxDL_METHOD_LOAD( m_dll
, NewMovieFromFile
, m_ok
);
324 wxDL_METHOD_LOAD( m_dll
, GetMovieRate
, m_ok
);
325 wxDL_METHOD_LOAD( m_dll
, SetMovieRate
, m_ok
);
326 wxDL_METHOD_LOAD( m_dll
, MoviesTask
, m_ok
);
327 wxDL_METHOD_LOAD( m_dll
, BlockMove
, m_ok
);
328 wxDL_METHOD_LOAD( m_dll
, NewHandleClear
, m_ok
);
329 wxDL_METHOD_LOAD( m_dll
, NewMovieFromDataRef
, m_ok
);
330 wxDL_METHOD_LOAD( m_dll
, DisposeHandle
, m_ok
);
331 wxDL_METHOD_LOAD( m_dll
, GetMovieNaturalBoundsRect
, m_ok
);
332 wxDL_METHOD_LOAD( m_dll
, GetMovieIndTrackType
, m_ok
);
333 wxDL_METHOD_LOAD( m_dll
, CreatePortAssociation
, m_ok
);
334 wxDL_METHOD_LOAD( m_dll
, DestroyPortAssociation
, m_ok
);
335 wxDL_METHOD_LOAD( m_dll
, GetNativeWindowPort
, m_ok
);
336 wxDL_METHOD_LOAD( m_dll
, SetMovieGWorld
, m_ok
);
337 wxDL_METHOD_LOAD( m_dll
, DisposeMovie
, m_ok
);
338 wxDL_METHOD_LOAD( m_dll
, SetMovieBox
, m_ok
);
339 wxDL_METHOD_LOAD( m_dll
, SetMovieTimeScale
, m_ok
);
340 wxDL_METHOD_LOAD( m_dll
, GetMovieDuration
, m_ok
);
341 wxDL_METHOD_LOAD( m_dll
, GetMovieTimeBase
, m_ok
);
342 wxDL_METHOD_LOAD( m_dll
, GetMovieTimeScale
, m_ok
);
343 wxDL_METHOD_LOAD( m_dll
, GetMovieTime
, m_ok
);
344 wxDL_METHOD_LOAD( m_dll
, SetMovieTime
, m_ok
);
345 wxDL_METHOD_LOAD( m_dll
, GetMovieVolume
, m_ok
);
346 wxDL_METHOD_LOAD( m_dll
, SetMovieVolume
, m_ok
);
347 wxDL_METHOD_LOAD( m_dll
, SetMovieTimeValue
, m_ok
);
348 wxDL_METHOD_LOAD( m_dll
, NewMovieController
, m_ok
);
349 wxDL_METHOD_LOAD( m_dll
, DisposeMovieController
, m_ok
);
350 wxDL_METHOD_LOAD( m_dll
, MCSetVisible
, m_ok
);
351 wxDL_METHOD_LOAD( m_dll
, PrePrerollMovie
, m_ok
);
352 wxDL_METHOD_LOAD( m_dll
, PrerollMovie
, m_ok
);
353 wxDL_METHOD_LOAD( m_dll
, GetMoviePreferredRate
, m_ok
);
354 wxDL_METHOD_LOAD( m_dll
, GetMovieLoadState
, m_ok
);
355 wxDL_METHOD_LOAD( m_dll
, MCDoAction
, m_ok
);
356 wxDL_METHOD_LOAD( m_dll
, MCSetControllerBoundsRect
, m_ok
);
357 wxDL_METHOD_LOAD( m_dll
, NativeEventToMacEvent
, m_ok
);
358 wxDL_METHOD_LOAD( m_dll
, MCIsPlayerEvent
, m_ok
);
359 wxDL_METHOD_LOAD( m_dll
, MCSetMovie
, m_ok
);
360 wxDL_METHOD_LOAD( m_dll
, MCSetActionFilterWithRefCon
, m_ok
);
361 wxDL_METHOD_LOAD( m_dll
, MCGetControllerInfo
, m_ok
);
362 wxDL_METHOD_LOAD( m_dll
, BeginUpdate
, m_ok
);
363 wxDL_METHOD_LOAD( m_dll
, UpdateMovie
, m_ok
);
364 wxDL_METHOD_LOAD( m_dll
, EndUpdate
, m_ok
);
365 wxDL_METHOD_LOAD( m_dll
, GetMoviesStickyError
, m_ok
);
372 class WXDLLIMPEXP_MEDIA wxQTMediaBackend
: public wxMediaBackendCommonBase
376 virtual ~wxQTMediaBackend();
378 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
383 const wxValidator
& validator
,
384 const wxString
& name
);
387 virtual bool Pause();
390 virtual bool Load(const wxURI
& location
,
392 { return wxMediaBackend::Load(location
, proxy
); }
394 virtual bool Load(const wxString
& fileName
);
395 virtual bool Load(const wxURI
& location
);
397 virtual wxMediaState
GetState();
399 virtual bool SetPosition(wxLongLong where
);
400 virtual wxLongLong
GetPosition();
401 virtual wxLongLong
GetDuration();
403 virtual void Move(int x
, int y
, int w
, int h
);
404 wxSize
GetVideoSize() const;
406 virtual double GetPlaybackRate();
407 virtual bool SetPlaybackRate(double dRate
);
409 virtual double GetVolume();
410 virtual bool SetVolume(double);
415 static void PPRMProc (Movie theMovie
, OSErr theErr
, void* theRefCon
);
417 // TODO: Last param actually long - does this work on 64bit machines?
418 static Boolean
MCFilterProc(MovieController theController
,
419 short action
, void *params
, LONG_PTR refCon
);
421 static LRESULT CALLBACK
QTWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
423 virtual bool ShowPlayerControls(wxMediaCtrlPlayerControls flags
);
425 wxSize m_bestSize
; // Original movie size
426 Movie m_movie
; // QT Movie handle/instance
427 bool m_bVideo
; // Whether or not we have video
428 bool m_bPlaying
; // Whether or not movie is playing
429 wxTimer
* m_timer
; // Load or Play timer
430 wxQuickTimeLibrary m_lib
; // DLL to load functions from
431 ComponentInstance m_pMC
; // Movie Controller
433 friend class wxQTMediaEvtHandler
;
435 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
)
438 // helper to hijack background erasing for the QT window
439 class WXDLLIMPEXP_MEDIA wxQTMediaEvtHandler
: public wxEvtHandler
442 wxQTMediaEvtHandler(wxQTMediaBackend
*qtb
, WXHWND hwnd
)
447 m_qtb
->m_ctrl
->Connect(m_qtb
->m_ctrl
->GetId(),
448 wxEVT_ERASE_BACKGROUND
,
449 wxEraseEventHandler(wxQTMediaEvtHandler::OnEraseBackground
),
453 void OnEraseBackground(wxEraseEvent
& event
);
456 wxQTMediaBackend
*m_qtb
;
459 DECLARE_NO_COPY_CLASS(wxQTMediaEvtHandler
)
463 //===========================================================================
465 //===========================================================================
468 //---------------------------------------------------------------------------
471 // TODO: Use a less kludgy way to pause/get state/set state
472 // FIXME: Greg Hazel reports that sometimes files that cannot be played
473 // with this backend are treated as playable anyway - not verified though.
474 //---------------------------------------------------------------------------
476 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
)
478 // Time between timer calls - this is the Apple recommendation to the TCL
480 #define MOVIE_DELAY 20
482 //---------------------------------------------------------------------------
485 // QT, esp. QT for Windows is very picky about how you go about
486 // async loading. If you were to go through a Windows message loop
487 // or a MoviesTask or both and then check the movie load state
488 // it would still return 1000 (loading)... even (pre)prerolling doesn't
489 // help. However, making a load timer like this works
490 //---------------------------------------------------------------------------
491 class wxQTLoadTimer
: public wxTimer
494 wxQTLoadTimer(Movie movie
, wxQTMediaBackend
* parent
, wxQuickTimeLibrary
* pLib
) :
495 m_movie(movie
), m_parent(parent
), m_pLib(pLib
) {}
499 m_pLib
->MoviesTask(m_movie
, 0);
500 // kMovieLoadStatePlayable
501 if (m_pLib
->GetMovieLoadState(m_movie
) >= 10000)
503 m_parent
->FinishLoad();
509 Movie m_movie
; //Our movie instance
510 wxQTMediaBackend
* m_parent
; //Backend pointer
511 wxQuickTimeLibrary
* m_pLib
; //Interfaces
515 // --------------------------------------------------------------------------
516 // wxQTPlayTimer - Handle Asyncronous Playing
518 // 1) Checks to see if the movie is done, and if not continues
519 // streaming the movie
520 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
522 // --------------------------------------------------------------------------
523 class wxQTPlayTimer
: public wxTimer
526 wxQTPlayTimer(Movie movie
, wxQTMediaBackend
* parent
,
527 wxQuickTimeLibrary
* pLib
) :
528 m_movie(movie
), m_parent(parent
), m_pLib(pLib
) {}
533 // OK, a little explaining - basically originally
534 // we only called MoviesTask if the movie was actually
535 // playing (not paused or stopped)... this was before
536 // we realized MoviesTask actually handles repainting
537 // of the current frame - so if you were to resize
538 // or something it would previously not redraw that
539 // portion of the movie.
541 // So now we call MoviesTask always so that it repaints
544 m_pLib
->MoviesTask(m_movie
, 0);
547 // Handle the stop event - if the movie has reached
548 // the end, notify our handler
550 // m_bPlaying == !(Stopped | Paused)
552 if (m_parent
->m_bPlaying
)
554 if (m_pLib
->IsMovieDone(m_movie
))
556 if ( m_parent
->SendStopEvent() )
559 wxASSERT(m_pLib
->GetMoviesError() == noErr
);
561 m_parent
->QueueFinishEvent();
568 Movie m_movie
; // Our movie instance
569 wxQTMediaBackend
* m_parent
; //Backend pointer
570 wxQuickTimeLibrary
* m_pLib
; //Interfaces
574 //---------------------------------------------------------------------------
575 // wxQTMediaBackend::QTWndProc
577 // Forwards events to the Movie Controller so that it can
578 // redraw itself/process messages etc..
579 //---------------------------------------------------------------------------
580 LRESULT CALLBACK
wxQTMediaBackend::QTWndProc(HWND hWnd
, UINT nMsg
,
581 WPARAM wParam
, LPARAM lParam
)
583 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)wxGetWindowUserData(hWnd
);
593 EventRecord theEvent
;
594 pThis
->m_lib
.NativeEventToMacEvent(&msg
, &theEvent
);
595 pThis
->m_lib
.MCIsPlayerEvent(pThis
->m_pMC
, &theEvent
);
597 return pThis
->m_ctrl
->MSWWindowProc(nMsg
, wParam
, lParam
);
600 //---------------------------------------------------------------------------
601 // wxQTMediaBackend Destructor
603 // Sets m_timer to NULL signifying we havn't loaded anything yet
604 //---------------------------------------------------------------------------
605 wxQTMediaBackend::wxQTMediaBackend()
606 : m_movie(NULL
), m_bPlaying(false), m_timer(NULL
), m_pMC(NULL
)
610 //---------------------------------------------------------------------------
611 // wxQTMediaBackend Destructor
613 // 1) Cleans up the QuickTime movie instance
614 // 2) Decrements the QuickTime reference counter - if this reaches
615 // 0, QuickTime shuts down
616 // 3) Decrements the QuickTime Windows Media Layer reference counter -
617 // if this reaches 0, QuickTime shuts down the Windows Media Layer
618 //---------------------------------------------------------------------------
619 wxQTMediaBackend::~wxQTMediaBackend()
628 m_lib
.DisposeMovieController(m_pMC
);
632 // destroy wxQTMediaEvtHandler we pushed on it
633 m_ctrl
->PopEventHandler(true);
635 m_lib
.DestroyPortAssociation(
636 (CGrafPtr
)m_lib
.GetNativeWindowPort(m_ctrl
->GetHWND()));
638 //Note that ExitMovies() is not necessary, but
639 //the docs are fuzzy on whether or not TerminateQTML is
641 m_lib
.TerminateQTML();
645 //---------------------------------------------------------------------------
646 // wxQTMediaBackend::CreateControl
648 // 1) Intializes QuickTime
649 // 2) Creates the control window
650 //---------------------------------------------------------------------------
651 bool wxQTMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
656 const wxValidator
& validator
,
657 const wxString
& name
)
659 if (!m_lib
.Initialize())
662 int nError
= m_lib
.InitializeQTML(0);
663 if (nError
!= noErr
) //-2093 no dll
665 wxFAIL_MSG(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
672 // By default wxWindow(s) is created with a border -
673 // so we need to get rid of those
675 // Since we don't have a child window like most other
676 // backends, we don't need wxCLIP_CHILDREN
677 if ( !ctrl
->wxControl::Create(parent
, id
, pos
, size
,
678 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
,
684 m_ctrl
= wxStaticCast(ctrl
, wxMediaCtrl
);
686 // Create a port association for our window so we
687 // can use it as a WindowRef
688 m_lib
.CreatePortAssociation(m_ctrl
->GetHWND(), NULL
, 0L);
690 // Part of a suggestion from Greg Hazel
691 // to repaint movie when idle
692 m_ctrl
->PushEventHandler(new wxQTMediaEvtHandler(this, m_ctrl
->GetHWND()));
698 //---------------------------------------------------------------------------
699 // wxQTMediaBackend::Load (file version)
701 // 1) Get an FSSpec from the Windows path name
703 // 3) Obtain the movie instance from the movie resource
704 // 4) Close the movie resource
706 //---------------------------------------------------------------------------
707 bool wxQTMediaBackend::Load(const wxString
& fileName
)
714 short movieResFile
= 0; //= 0 because of annoying VC6 warning
717 err
= m_lib
.NativePathNameToFSSpec(
718 (char*) (const char*) fileName
.mb_str(),
720 result
= (err
== noErr
);
724 err
= m_lib
.OpenMovieFile(&sfFile
, &movieResFile
, fsRdPerm
);
725 result
= (err
== noErr
);
730 short movieResID
= 0;
733 err
= m_lib
.NewMovieFromFile(
739 NULL
); // wasChanged
740 result
= (err
== noErr
/*&& m_lib.GetMoviesStickyError() == noErr*/);
742 // check m_lib.GetMoviesStickyError() because it may not find the
743 // proper codec and play black video and other strange effects,
744 // not to mention mess up the dynamic backend loading scheme
745 // of wxMediaCtrl - so it just does what the QuickTime player does
748 m_lib
.CloseMovieFile(movieResFile
);
756 //---------------------------------------------------------------------------
757 // wxQTMediaBackend::PPRMProc (static)
759 // Called when done PrePrerolling the movie.
760 // Note that in 99% of the cases this does nothing...
761 // Anyway we set up the loading timer here to tell us when the movie is done
762 //---------------------------------------------------------------------------
763 void wxQTMediaBackend::PPRMProc (Movie theMovie
,
764 OSErr
WXUNUSED_UNLESS_DEBUG(theErr
),
767 wxASSERT( theMovie
);
768 wxASSERT( theRefCon
);
769 wxASSERT( theErr
== noErr
);
771 wxQTMediaBackend
* pBE
= (wxQTMediaBackend
*) theRefCon
;
773 long lTime
= pBE
->m_lib
.GetMovieTime(theMovie
,NULL
);
774 Fixed rate
= pBE
->m_lib
.GetMoviePreferredRate(theMovie
);
775 pBE
->m_lib
.PrerollMovie(theMovie
, lTime
, rate
);
776 pBE
->m_timer
= new wxQTLoadTimer(pBE
->m_movie
, pBE
, &pBE
->m_lib
);
777 pBE
->m_timer
->Start(MOVIE_DELAY
);
780 //---------------------------------------------------------------------------
781 // wxQTMediaBackend::Load (URL Version)
783 // 1) Build an escaped URI from location
784 // 2) Create a handle to store the URI string
785 // 3) Put the URI string inside the handle
786 // 4) Make a QuickTime URL data ref from the handle with the URI in it
787 // 5) Clean up the URI string handle
788 // 6) Do some prerolling
790 //---------------------------------------------------------------------------
791 bool wxQTMediaBackend::Load(const wxURI
& location
)
796 wxString theURI
= location
.BuildURI();
798 Handle theHandle
= m_lib
.NewHandleClear(theURI
.length() + 1);
801 m_lib
.BlockMove(theURI
.mb_str(), *theHandle
, theURI
.length() + 1);
803 // create the movie from the handle that refers to the URI
804 OSErr err
= m_lib
.NewMovieFromDataRef(&m_movie
, newMovieActive
|
806 /* | newMovieIdleImportOK */,
808 URLDataHandlerSubType
);
810 m_lib
.DisposeHandle(theHandle
);
817 timeNow
= m_lib
.GetMovieTime(m_movie
, NULL
);
818 wxASSERT(m_lib
.GetMoviesError() == noErr
);
820 playRate
= m_lib
.GetMoviePreferredRate(m_movie
);
821 wxASSERT(m_lib
.GetMoviesError() == noErr
);
823 // Note that the callback here is optional,
824 // but without it PrePrerollMovie can be buggy
825 // (see Apple ml). Also, some may wonder
826 // why we need this at all - this is because
827 // Apple docs say QuickTime streamed movies
828 // require it if you don't use a Movie Controller,
829 // which we don't by default.
831 m_lib
.PrePrerollMovie(m_movie
, timeNow
, playRate
,
832 (WXFARPROC
)wxQTMediaBackend::PPRMProc
,
841 //---------------------------------------------------------------------------
842 // wxQTMediaBackend::FinishLoad
844 // 1) Create the movie timer
845 // 2) Get real size of movie for GetBestSize/sizers
846 // 3) Set the movie time scale to something usable so that seeking
847 // etc. will work correctly
848 // 4) Set our Movie Controller to display the movie if it exists,
849 // otherwise set the bounds of the Movie
850 // 5) Refresh parent window
851 //---------------------------------------------------------------------------
852 void wxQTMediaBackend::FinishLoad()
854 // Create the playing/streaming timer
855 m_timer
= new wxQTPlayTimer(m_movie
, (wxQTMediaBackend
*) this, &m_lib
);
858 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
860 // get the real size of the movie
862 memset(&outRect
, 0, sizeof(Rect
)); // suppress annoying VC6 warning
863 m_lib
.GetMovieNaturalBoundsRect (m_movie
, &outRect
);
864 wxASSERT(m_lib
.GetMoviesError() == noErr
);
866 m_bestSize
.x
= outRect
.right
- outRect
.left
;
867 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
869 // Handle the movie GWorld
873 thePoint
.h
= thePoint
.v
= 0;
874 m_lib
.MCSetMovie(m_pMC
, m_movie
,
875 m_lib
.GetNativeWindowPort(m_ctrl
->GetHandle()),
877 m_lib
.MCSetVisible(m_pMC
, true);
882 m_lib
.SetMovieGWorld(m_movie
,
883 (CGrafPtr
) m_lib
.GetNativeWindowPort(m_ctrl
->GetHWND()),
887 // Set the movie to millisecond precision
888 m_lib
.SetMovieTimeScale(m_movie
, 1000);
889 wxASSERT(m_lib
.GetMoviesError() == noErr
);
894 //---------------------------------------------------------------------------
895 // wxQTMediaBackend::Play
897 // 1) Start the QT movie
898 // 2) Start the movie loading timer
900 // NOTE: This will still return success even when
901 // the movie is still loading, and as mentioned in wxQTLoadTimer
902 // I don't know of a way to force this to be sync - so if its
903 // still loading the function will return true but the movie will
904 // still be in the stopped state
905 //---------------------------------------------------------------------------
906 bool wxQTMediaBackend::Play()
908 m_lib
.StartMovie(m_movie
);
911 return m_lib
.GetMoviesError() == noErr
;
914 //---------------------------------------------------------------------------
915 // wxQTMediaBackend::Pause
918 // 2) Stop the movie timer
919 //---------------------------------------------------------------------------
920 bool wxQTMediaBackend::Pause()
923 m_lib
.StopMovie(m_movie
);
925 return m_lib
.GetMoviesError() == noErr
;
928 //---------------------------------------------------------------------------
929 // wxQTMediaBackend::Stop
932 // 2) Stop the movie timer
933 // 3) Seek to the beginning of the movie
934 //---------------------------------------------------------------------------
935 bool wxQTMediaBackend::Stop()
939 m_lib
.StopMovie(m_movie
);
940 if (m_lib
.GetMoviesError() == noErr
)
941 m_lib
.GoToBeginningOfMovie(m_movie
);
943 return m_lib
.GetMoviesError() == noErr
;
946 //---------------------------------------------------------------------------
947 // wxQTMediaBackend::GetPlaybackRate
949 // Get the movie playback rate from ::GetMovieRate
950 //---------------------------------------------------------------------------
951 double wxQTMediaBackend::GetPlaybackRate()
953 return ( ((double)m_lib
.GetMovieRate(m_movie
)) / 0x10000);
956 //---------------------------------------------------------------------------
957 // wxQTMediaBackend::SetPlaybackRate
959 // Convert dRate to Fixed and Set the movie rate through SetMovieRate
960 //---------------------------------------------------------------------------
961 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
963 m_lib
.SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
965 return m_lib
.GetMoviesError() == noErr
;
968 //---------------------------------------------------------------------------
969 // wxQTMediaBackend::SetPosition
971 // 1) Create a time record struct (TimeRecord) with appropriate values
972 // 2) Pass struct to SetMovieTime
973 //---------------------------------------------------------------------------
974 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
976 // NB: For some reason SetMovieTime does not work
977 // correctly with the Quicktime Windows SDK (6)
978 // From Muskelkatermann at the wxForum
979 // http://www.solidsteel.nl/users/wxwidgets/viewtopic.php?t=2957
980 // RN - note that I have not verified this but there
981 // is no harm in calling SetMovieTimeValue instead
983 TimeRecord theTimeRecord
;
984 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
985 theTimeRecord
.value
.lo
= where
.GetLo();
986 theTimeRecord
.scale
= m_lib
.GetMovieTimeScale(m_movie
);
987 theTimeRecord
.base
= m_lib
.GetMovieTimeBase(m_movie
);
988 m_lib
.SetMovieTime(m_movie
, &theTimeRecord
);
990 m_lib
.SetMovieTimeValue(m_movie
, where
.GetLo());
993 return (m_lib
.GetMoviesError() == noErr
);
996 //---------------------------------------------------------------------------
997 // wxQTMediaBackend::GetPosition
999 // 1) Calls GetMovieTime to get the position we are in in the movie
1000 // in milliseconds (we called
1001 //---------------------------------------------------------------------------
1002 wxLongLong
wxQTMediaBackend::GetPosition()
1004 return m_lib
.GetMovieTime(m_movie
, NULL
);
1007 //---------------------------------------------------------------------------
1008 // wxQTMediaBackend::GetVolume
1010 // Gets the volume through GetMovieVolume - which returns a 16 bit short -
1012 // +--------+--------+
1014 // +--------+--------+
1016 // (1) first 8 bits are value before decimal
1017 // (2) second 8 bits are value after decimal
1019 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
1020 // 1 (full gain and sound)
1021 //---------------------------------------------------------------------------
1022 double wxQTMediaBackend::GetVolume()
1024 short sVolume
= m_lib
.GetMovieVolume(m_movie
);
1025 wxASSERT(m_lib
.GetMoviesError() == noErr
);
1027 if (sVolume
& (128 << 8)) //negative - no sound
1030 return sVolume
/ 256.0;
1033 //---------------------------------------------------------------------------
1034 // wxQTMediaBackend::SetVolume
1036 // Sets the volume through SetMovieVolume - which takes a 16 bit short -
1038 // +--------+--------+
1040 // +--------+--------+
1042 // (1) first 8 bits are value before decimal
1043 // (2) second 8 bits are value after decimal
1045 // Volume ranges from -1.0 (gain but no sound), 0 (no sound and no gain) to
1046 // 1 (full gain and sound)
1047 //---------------------------------------------------------------------------
1048 bool wxQTMediaBackend::SetVolume(double dVolume
)
1050 m_lib
.SetMovieVolume(m_movie
, (short) (dVolume
* 256));
1051 return m_lib
.GetMoviesError() == noErr
;
1054 //---------------------------------------------------------------------------
1055 // wxQTMediaBackend::GetDuration
1057 // Calls GetMovieDuration
1058 //---------------------------------------------------------------------------
1059 wxLongLong
wxQTMediaBackend::GetDuration()
1061 return m_lib
.GetMovieDuration(m_movie
);
1064 //---------------------------------------------------------------------------
1065 // wxQTMediaBackend::GetState
1067 // Determines the current state:
1068 // if we are at the beginning, then we are stopped
1069 //---------------------------------------------------------------------------
1070 wxMediaState
wxQTMediaBackend::GetState()
1073 return wxMEDIASTATE_PLAYING
;
1074 else if ( !m_movie
|| wxQTMediaBackend::GetPosition() == 0 )
1075 return wxMEDIASTATE_STOPPED
;
1077 return wxMEDIASTATE_PAUSED
;
1080 //---------------------------------------------------------------------------
1081 // wxQTMediaBackend::Cleanup
1083 // Diposes of the movie timer, Disassociates the Movie Controller with
1084 // movie and hides it if it exists, and stops and disposes
1086 //---------------------------------------------------------------------------
1087 void wxQTMediaBackend::Cleanup()
1097 m_lib
.StopMovie(m_movie
);
1102 thePoint
.h
= thePoint
.v
= 0;
1103 m_lib
.MCSetVisible(m_pMC
, false);
1104 m_lib
.MCSetMovie(m_pMC
, NULL
, NULL
, thePoint
);
1107 m_lib
.DisposeMovie(m_movie
);
1111 //---------------------------------------------------------------------------
1112 // wxQTMediaBackend::ShowPlayerControls
1114 // Creates a movie controller for the Movie if the user wants it
1115 //---------------------------------------------------------------------------
1116 bool wxQTMediaBackend::ShowPlayerControls(wxMediaCtrlPlayerControls flags
)
1120 // restore old wndproc
1121 wxSetWindowProc((HWND
)m_ctrl
->GetHWND(), wxWndProc
);
1122 m_lib
.DisposeMovieController(m_pMC
);
1125 // movie controller height
1129 if (flags
&& m_movie
)
1132 wxRect wxrect
= m_ctrl
->GetClientRect();
1134 // make room for controller
1135 if (wxrect
.width
< 320)
1138 rect
.top
= (short)wxrect
.y
;
1139 rect
.left
= (short)wxrect
.x
;
1140 rect
.right
= (short)(rect
.left
+ wxrect
.width
);
1141 rect
.bottom
= (short)(rect
.top
+ wxrect
.height
);
1145 m_pMC
= m_lib
.NewMovieController(m_movie
, &rect
, mcTopLeftMovie
|
1146 // mcScaleMovieToFit |
1149 m_lib
.MCDoAction(m_pMC
, 32, (void*)true); // mcActionSetKeysEnabled
1150 m_lib
.MCSetActionFilterWithRefCon(m_pMC
,
1151 (WXFARPROC
)wxQTMediaBackend::MCFilterProc
, (void*)this);
1152 m_bestSize
.y
+= 16; // movie controller height
1154 // By default the movie controller uses its own colour palette
1155 // for the movie which can be bad on some files, so turn it off.
1156 // Also turn off its frame / border for the movie
1157 // Also take care of a couple of the interface flags here
1159 m_lib
.MCDoAction(m_pMC
, 39/*mcActionGetFlags*/, (void*)&mcFlags
);
1162 // (1<< 0) /*mcFlagSuppressMovieFrame*/ |
1163 (1<< 3) /*mcFlagsUseWindowPalette*/
1164 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_STEP
)
1165 ? 0 : (1<< 1) /*mcFlagSuppressStepButtons*/)
1166 | ((flags
& wxMEDIACTRLPLAYERCONTROLS_VOLUME
)
1167 ? 0 : (1<< 2) /*mcFlagSuppressSpeakerButton*/)
1168 // | (1<< 4) /*mcFlagDontInvalidate*/ // if we take care of repainting ourselves
1171 m_lib
.MCDoAction(m_pMC
, 38/*mcActionSetFlags*/, (void*)mcFlags
);
1173 // intercept the wndproc of our control window
1174 wxSetWindowProc((HWND
)m_ctrl
->GetHWND(), wxQTMediaBackend::QTWndProc
);
1176 // set the user data of our window
1177 wxSetWindowUserData((HWND
)m_ctrl
->GetHWND(), this);
1181 NotifyMovieSizeChanged();
1183 return m_lib
.GetMoviesError() == noErr
;
1186 //---------------------------------------------------------------------------
1187 // wxQTMediaBackend::MCFilterProc (static)
1189 // Callback for when the movie controller recieves a message
1190 //---------------------------------------------------------------------------
1191 Boolean
wxQTMediaBackend::MCFilterProc(MovieController
WXUNUSED(theController
),
1193 void * WXUNUSED(params
),
1196 // NB: potential optimisation
1200 wxQTMediaBackend
* pThis
= (wxQTMediaBackend
*)refCon
;
1205 // don't process idle events
1209 // play button triggered - MC will set movie to opposite state
1210 // of current - playing ? paused : playing
1212 pThis
->m_bPlaying
= !(pThis
->m_bPlaying
);
1214 // NB: Sometimes it doesn't redraw properly -
1215 // if you click on the button but don't move the mouse
1216 // the button will not change its state until you move
1217 // mcActionDraw and Refresh/Update combo do nothing
1218 // to help this unfortunately
1228 //---------------------------------------------------------------------------
1229 // wxQTMediaBackend::GetVideoSize
1231 // Returns the actual size of the QT movie
1232 //---------------------------------------------------------------------------
1233 wxSize
wxQTMediaBackend::GetVideoSize() const
1238 //---------------------------------------------------------------------------
1239 // wxQTMediaBackend::Move
1241 // Sets the bounds of either the Movie or Movie Controller
1242 //---------------------------------------------------------------------------
1243 void wxQTMediaBackend::Move(int WXUNUSED(x
), int WXUNUSED(y
), int w
, int h
)
1247 // make room for controller
1253 Rect theRect
= {0, 0, (short)h
, (short)w
};
1254 m_lib
.MCSetControllerBoundsRect(m_pMC
, &theRect
);
1258 Rect theRect
= {0, 0, (short)h
, (short)w
};
1259 m_lib
.SetMovieBox(m_movie
, &theRect
);
1262 wxASSERT(m_lib
.GetMoviesError() == noErr
);
1266 //---------------------------------------------------------------------------
1267 // wxQTMediaBackend::OnEraseBackground
1269 // Suggestion from Greg Hazel to repaint the movie when idle
1272 // TODO: We may be repainting too much here - under what exact circumstances
1273 // do we need this? I think Move also repaints correctly for the Movie
1274 // Controller, so in that instance we don't need this either
1275 //---------------------------------------------------------------------------
1276 void wxQTMediaEvtHandler::OnEraseBackground(wxEraseEvent
& evt
)
1278 wxQuickTimeLibrary
& m_pLib
= m_qtb
->m_lib
;
1282 // repaint movie controller
1283 m_pLib
.MCDoAction(m_qtb
->m_pMC
, 2 /*mcActionDraw*/,
1284 m_pLib
.GetNativeWindowPort(m_hwnd
));
1286 else if ( m_qtb
->m_movie
)
1288 // no movie controller
1289 CGrafPtr port
= (CGrafPtr
)m_pLib
.GetNativeWindowPort(m_hwnd
);
1291 m_pLib
.BeginUpdate(port
);
1292 m_pLib
.UpdateMovie(m_qtb
->m_movie
);
1293 wxASSERT(m_pLib
.GetMoviesError() == noErr
);
1294 m_pLib
.EndUpdate(port
);
1299 // let the system repaint the window
1304 //---------------------------------------------------------------------------
1306 //---------------------------------------------------------------------------
1308 // in source file that contains stuff you don't directly use
1309 #include "wx/html/forcelnk.h"
1310 FORCE_LINK_ME(wxmediabackend_qt
)
1312 #endif // wxUSE_MEDIACTRL && wxUSE_ACTIVEX