1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/carbon/mediactrl.cpp
3 // Purpose: Built-in Media Backends for Mac
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) 2004-2005 Ryan Norton, portions (c) 2004 Kevin Olliver
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //===========================================================================
14 //===========================================================================
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mediactrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 //---------------------------------------------------------------------------
33 //---------------------------------------------------------------------------
34 #include "wx/mediactrl.h"
36 //---------------------------------------------------------------------------
38 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
42 // Whether or not to use OSX 10.2's CreateMovieControl for native QuickTime
43 // control - i.e. native positioning and event handling etc..
44 //---------------------------------------------------------------------------
45 #ifndef wxUSE_CREATEMOVIECONTROL
46 # if defined( __WXMAC_OSX__ ) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
47 # define wxUSE_CREATEMOVIECONTROL 1
49 # define wxUSE_CREATEMOVIECONTROL 0
53 //===========================================================================
54 // BACKEND DECLARATIONS
55 //===========================================================================
57 //---------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
63 //---------------------------------------------------------------------------
65 //---------------------------------------------------------------------------
66 //uma is for wxMacFSSpec
67 #include "wx/mac/uma.h"
71 #include <QuickTimeComponents.h> //Standard QT stuff
73 //Determines whether version 4 of QT is installed (Pretty much for classic only)
74 Boolean
_wxIsQuickTime4Installed (void)
79 error
= Gestalt (gestaltQuickTime
, &result
);
80 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
83 class WXDLLIMPEXP_MEDIA wxQTMediaBackend
: public wxMediaBackend
90 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
95 const wxValidator
& validator
,
96 const wxString
& name
);
102 virtual bool Load(const wxString
& fileName
);
103 virtual bool Load(const wxURI
& location
);
105 virtual wxMediaState
GetState();
107 virtual bool SetPosition(wxLongLong where
);
108 virtual wxLongLong
GetPosition();
109 virtual wxLongLong
GetDuration();
111 virtual void Move(int x
, int y
, int w
, int h
);
112 wxSize
GetVideoSize() const;
114 virtual double GetPlaybackRate();
115 virtual bool SetPlaybackRate(double dRate
);
120 wxSize m_bestSize
; //Original movie size
121 struct MovieType
** m_movie
; //QT Movie handle/instance
122 wxControl
* m_ctrl
; //Parent control
123 bool m_bVideo
; //Whether or not we have video
124 class _wxQTTimer
* m_timer
; //Timer for streaming the movie
126 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
);
130 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
134 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
136 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
);
138 //Time between timer calls
139 #define MOVIE_DELAY 100
141 // --------------------------------------------------------------------------
142 // wxQTTimer - Handle Asyncronous Playing
143 // --------------------------------------------------------------------------
144 class _wxQTTimer
: public wxTimer
147 _wxQTTimer(Movie movie
, wxQTMediaBackend
* parent
) :
148 m_movie(movie
), m_bPaused(false), m_parent(parent
)
156 bool GetPaused() {return m_bPaused
;}
157 void SetPaused(bool bPaused
) {m_bPaused
= bPaused
;}
159 //-----------------------------------------------------------------------
160 // _wxQTTimer::Notify
162 // 1) Checks to see if the movie is done, and if not continues
163 // streaming the movie
164 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
166 //-----------------------------------------------------------------------
171 if(!IsMovieDone(m_movie
))
172 MoviesTask(m_movie
, MOVIE_DELAY
);
175 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
,
176 m_parent
->m_ctrl
->GetId());
177 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
179 if(theEvent
.IsAllowed())
183 wxASSERT(::GetMoviesError() == noErr
);
185 //send the event to our child
186 wxMediaEvent
theEvent(wxEVT_MEDIA_FINISHED
,
187 m_parent
->m_ctrl
->GetId());
188 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
195 Movie m_movie
; //Our movie instance
196 bool m_bPaused
; //Whether we are paused or not
197 wxQTMediaBackend
* m_parent
; //Backend pointer
200 //---------------------------------------------------------------------------
201 // wxQTMediaBackend Constructor
203 // Sets m_timer to NULL signifying we havn't loaded anything yet
204 //---------------------------------------------------------------------------
205 wxQTMediaBackend::wxQTMediaBackend() : m_timer(NULL
)
209 //---------------------------------------------------------------------------
210 // wxQTMediaBackend Destructor
212 // 1) Cleans up the QuickTime movie instance
213 // 2) Decrements the QuickTime reference counter - if this reaches
214 // 0, QuickTime shuts down
215 // 3) Decrements the QuickTime Windows Media Layer reference counter -
216 // if this reaches 0, QuickTime shuts down the Windows Media Layer
217 //---------------------------------------------------------------------------
218 wxQTMediaBackend::~wxQTMediaBackend()
223 //Note that ExitMovies() is not neccessary...
227 //---------------------------------------------------------------------------
228 // wxQTMediaBackend::CreateControl
230 // 1) Intializes QuickTime
231 // 2) Creates the control window
232 //---------------------------------------------------------------------------
233 bool wxQTMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
238 const wxValidator
& validator
,
239 const wxString
& name
)
241 //Don't bother in Native control mode
242 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_2 )
243 if (!_wxIsQuickTime4Installed())
251 // By default wxWindow(s) is created with a border -
252 // so we need to get rid of those
254 // Since we don't have a child window like most other
255 // backends, we don't need wxCLIP_CHILDREN
259 #if wxUSE_CREATEMOVIECONTROL
260 ctrl
->wxWindow::Create(parent
, id
, pos
, size
,
261 m_ctrl
->MacRemoveBordersFromStyle(style
),
264 ctrl
->wxControl::Create(parent
, id
, pos
, size
,
265 m_ctrl
->MacRemoveBordersFromStyle(style
),
272 ctrl
->SetValidator(validator
);
279 //---------------------------------------------------------------------------
280 // wxQTMediaBackend::Load (file version)
282 // 1) Get an FSSpec from the Windows path name
284 // 3) Obtain the movie instance from the movie resource
285 // 4) Close the movie resource
287 //---------------------------------------------------------------------------
288 bool wxQTMediaBackend::Load(const wxString
& fileName
)
297 wxMacFilename2FSSpec( fileName
, &sfFile
);
299 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
302 short movieResID
= 0;
305 err
= NewMovieFromFile (
313 CloseMovieFile (movieResFile
);
320 return ::GetMoviesError() == noErr
;
323 //---------------------------------------------------------------------------
324 // wxQTMediaBackend::Load (URL Version)
326 // 1) Build an escaped URI from location
327 // 2) Create a handle to store the URI string
328 // 3) Put the URI string inside the handle
329 // 4) Make a QuickTime URL data ref from the handle with the URI in it
330 // 5) Clean up the URI string handle
331 // 6) Do some prerolling
333 //---------------------------------------------------------------------------
334 bool wxQTMediaBackend::Load(const wxURI
& location
)
339 wxString theURI
= location
.BuildURI();
343 Handle theHandle
= NewHandleClear(theURI
.length() + 1);
346 BlockMove(theURI
.mb_str(), *theHandle
, theURI
.length() + 1);
348 //create the movie from the handle that refers to the URI
349 err
= NewMovieFromDataRef(&m_movie
, newMovieActive
,
351 URLDataHandlerSubType
);
353 DisposeHandle(theHandle
);
358 //preroll movie for streaming
359 //TODO:Async this using threads?
362 timeNow
= GetMovieTime(m_movie
, NULL
);
363 playRate
= GetMoviePreferredRate(m_movie
);
364 PrePrerollMovie(m_movie
, timeNow
, playRate
, NULL
, NULL
);
365 PrerollMovie(m_movie
, timeNow
, playRate
);
366 SetMovieRate(m_movie
, playRate
);
370 return ::GetMoviesError() == noErr
;
373 //---------------------------------------------------------------------------
374 // wxQTMediaBackend::FinishLoad
376 // 1) Create the movie timer
377 // 2) Get real size of movie for GetBestSize/sizers
378 // 3) See if there is video in the movie, and if so then either
379 // SetMovieGWorld if < 10.2 or use Native CreateMovieControl
380 // 4) Set the movie time scale to something usable so that seeking
381 // etc. will work correctly
382 // 5) Refresh parent window
383 //---------------------------------------------------------------------------
384 void wxQTMediaBackend::FinishLoad()
386 m_timer
= new _wxQTTimer(m_movie
, (wxQTMediaBackend
*) this);
389 //get the real size of the movie
391 ::GetMovieNaturalBoundsRect (m_movie
, &outRect
);
392 wxASSERT(::GetMoviesError() == noErr
);
394 m_bestSize
.x
= outRect
.right
- outRect
.left
;
395 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
397 //reparent movie/*AudioMediaCharacteristic*/
398 if(GetMovieIndTrackType(m_movie
, 1,
399 VisualMediaCharacteristic
,
400 movieTrackCharacteristic
|
401 movieTrackEnabledOnly
) != NULL
)
403 #if wxUSE_CREATEMOVIECONTROL
405 //Native CreateMovieControl QT control (Thanks to Kevin Olliver's
406 //wxQTMovie for some of this).
408 #define GetControlPeer(whatever) ctrl->m_peer
409 wxMediaCtrl
* ctrl
= (wxMediaCtrl
*) m_ctrl
;
410 Rect bounds
= wxMacGetBoundsForControl(m_ctrl
,
411 m_ctrl
->GetPosition(),
414 //Dispose of old control for new one
415 if (GetControlPeer(m_ctrl
) && GetControlPeer(m_ctrl
)->Ok() )
416 GetControlPeer(m_ctrl
)->Dispose();
419 //kMovieControlOptionXXX
420 //HideController - hide the movie controller
421 //LocateTopLeft - movie is pinned to top left rather than centered in the control
422 //EnableEditing - Allows programmatic editing and dragn'drop
423 //HandleEditingHI- Installs event stuff for edit menu - forces EnableEditing also
424 //SetKeysEnabled - Allows keyboard input
425 //ManuallyIdled - app handles movie idling rather than internal timer event loop
426 ::CreateMovieControl(
428 ctrl
->MacGetTopLevelWindowRef(), //parent
429 &bounds
, //control bounds
430 m_movie
, //movie handle
431 kMovieControlOptionHideController
432 | kMovieControlOptionLocateTopLeft
433 | kMovieControlOptionSetKeysEnabled
434 // | kMovieControlOptionManuallyIdled
436 ctrl
->m_peer
->GetControlRefAddr() );
438 ::EmbedControl(ctrl
->m_peer
->GetControlRef(), (ControlRef
)ctrl
->GetParent()->GetHandle());
443 SetMovieGWorld(m_movie
,
447 m_ctrl
->MacGetTopLevelWindowRef()
453 //we want millisecond precision
454 ::SetMovieTimeScale(m_movie
, 1000);
455 wxASSERT(::GetMoviesError() == noErr
);
458 //Here, if the parent of the control has a sizer - we
459 //tell it to recalculate the size of this control since
460 //the user opened a seperate media file
462 m_ctrl
->InvalidateBestSize();
463 m_ctrl
->GetParent()->Layout();
464 m_ctrl
->GetParent()->Refresh();
465 m_ctrl
->GetParent()->Update();
468 //---------------------------------------------------------------------------
469 // wxQTMediaBackend::Play
471 // 1) Start the QT movie
472 // 2) Start the movie loading timer
473 //---------------------------------------------------------------------------
474 bool wxQTMediaBackend::Play()
476 ::StartMovie(m_movie
);
477 m_timer
->SetPaused(false);
478 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
479 return ::GetMoviesError() == noErr
;
482 //---------------------------------------------------------------------------
483 // wxQTMediaBackend::Pause
486 // 2) Stop the movie timer
487 //---------------------------------------------------------------------------
488 bool wxQTMediaBackend::Pause()
490 ::StopMovie(m_movie
);
491 m_timer
->SetPaused(true);
493 return ::GetMoviesError() == noErr
;
496 //---------------------------------------------------------------------------
497 // wxQTMediaBackend::Stop
500 // 2) Stop the movie timer
501 // 3) Seek to the beginning of the movie
502 //---------------------------------------------------------------------------
503 bool wxQTMediaBackend::Stop()
505 m_timer
->SetPaused(false);
508 ::StopMovie(m_movie
);
509 if(::GetMoviesError() != noErr
)
512 ::GoToBeginningOfMovie(m_movie
);
513 return ::GetMoviesError() == noErr
;
516 //---------------------------------------------------------------------------
517 // wxQTMediaBackend::GetPlaybackRate
519 // 1) Get the movie playback rate from ::GetMovieRate
520 //---------------------------------------------------------------------------
521 double wxQTMediaBackend::GetPlaybackRate()
523 return ( ((double)::GetMovieRate(m_movie
)) / 0x10000);
526 //---------------------------------------------------------------------------
527 // wxQTMediaBackend::SetPlaybackRate
529 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
530 //---------------------------------------------------------------------------
531 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
533 ::SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
534 return ::GetMoviesError() == noErr
;
537 //---------------------------------------------------------------------------
538 // wxQTMediaBackend::SetPosition
540 // 1) Create a time record struct (TimeRecord) with appropriate values
541 // 2) Pass struct to SetMovieTime
542 //---------------------------------------------------------------------------
543 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
545 TimeRecord theTimeRecord
;
546 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
547 theTimeRecord
.value
.lo
= where
.GetValue();
548 theTimeRecord
.scale
= ::GetMovieTimeScale(m_movie
);
549 theTimeRecord
.base
= ::GetMovieTimeBase(m_movie
);
550 ::SetMovieTime(m_movie
, &theTimeRecord
);
552 if (::GetMoviesError() != noErr
)
558 //---------------------------------------------------------------------------
559 // wxQTMediaBackend::GetPosition
561 // Calls GetMovieTime
562 //---------------------------------------------------------------------------
563 wxLongLong
wxQTMediaBackend::GetPosition()
565 return ::GetMovieTime(m_movie
, NULL
);
568 //---------------------------------------------------------------------------
569 // wxQTMediaBackend::GetDuration
571 // Calls GetMovieDuration
572 //---------------------------------------------------------------------------
573 wxLongLong
wxQTMediaBackend::GetDuration()
575 return ::GetMovieDuration(m_movie
);
578 //---------------------------------------------------------------------------
579 // wxQTMediaBackend::GetState
581 // Determines the current state - the timer keeps track of whether or not
582 // we are paused or stopped (if the timer is running we are playing)
583 //---------------------------------------------------------------------------
584 wxMediaState
wxQTMediaBackend::GetState()
586 if ( !m_timer
|| (m_timer
->IsRunning() == false &&
587 m_timer
->GetPaused() == false) )
588 return wxMEDIASTATE_STOPPED
;
590 if( m_timer
->IsRunning() == true )
591 return wxMEDIASTATE_PLAYING
;
593 return wxMEDIASTATE_PAUSED
;
596 //---------------------------------------------------------------------------
597 // wxQTMediaBackend::Cleanup
599 // Diposes of the movie timer, Control if native, and stops and disposes
601 //---------------------------------------------------------------------------
602 void wxQTMediaBackend::Cleanup()
607 #if wxUSE_CREATEMOVIECONTROL
608 DisposeControl(((wxMediaCtrl
*)m_ctrl
)->m_peer
->GetControlRef());
612 DisposeMovie(m_movie
);
615 //---------------------------------------------------------------------------
616 // wxQTMediaBackend::GetVideoSize
618 // Returns the actual size of the QT movie
619 //---------------------------------------------------------------------------
620 wxSize
wxQTMediaBackend::GetVideoSize() const
625 //---------------------------------------------------------------------------
626 // wxQTMediaBackend::Move
628 // We need to do this even when using native qt control because
629 // CreateMovieControl is broken in this regard...
630 //---------------------------------------------------------------------------
631 void wxQTMediaBackend::Move(int x
, int y
, int w
, int h
)
633 #if !wxUSE_CREATEMOVIECONTROL
638 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
641 Rect theRect
= {y
, x
, y
+h
, x
+w
};
643 ::SetMovieBox(m_movie
, &theRect
);
644 wxASSERT(::GetMoviesError() == noErr
);
647 if(m_timer
&& m_ctrl
)
649 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
651 ::MoveControl( (ControlRef
) m_ctrl
->GetHandle(), x
, y
);
652 m_ctrl
->GetParent()->Refresh();
653 m_ctrl
->GetParent()->Update();
659 //in source file that contains stuff you don't directly use
660 #include <wx/html/forcelnk.h>
661 FORCE_LINK_ME(basewxmediabackends
);
663 #endif //wxUSE_MEDIACTRL