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
->wxControl::Create(parent
, id
, pos
, size
,
261 m_ctrl
->MacRemoveBordersFromStyle(style
),
264 ctrl
->wxWindow::Create(parent
, id
, pos
, size
,
265 m_ctrl
->MacRemoveBordersFromStyle(style
),
275 //---------------------------------------------------------------------------
276 // wxQTMediaBackend::Load (file version)
278 // 1) Get an FSSpec from the Windows path name
280 // 3) Obtain the movie instance from the movie resource
281 // 4) Close the movie resource
283 //---------------------------------------------------------------------------
284 bool wxQTMediaBackend::Load(const wxString
& fileName
)
293 wxMacFilename2FSSpec( fileName
, &sfFile
);
295 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
298 short movieResID
= 0;
301 err
= NewMovieFromFile (
309 CloseMovieFile (movieResFile
);
316 return ::GetMoviesError() == noErr
;
319 //---------------------------------------------------------------------------
320 // wxQTMediaBackend::Load (URL Version)
322 // 1) Build an escaped URI from location
323 // 2) Create a handle to store the URI string
324 // 3) Put the URI string inside the handle
325 // 4) Make a QuickTime URL data ref from the handle with the URI in it
326 // 5) Clean up the URI string handle
327 // 6) Do some prerolling
329 //---------------------------------------------------------------------------
330 bool wxQTMediaBackend::Load(const wxURI
& location
)
335 wxString theURI
= location
.BuildURI();
339 Handle theHandle
= NewHandleClear(theURI
.length() + 1);
342 BlockMove(theURI
.mb_str(), *theHandle
, theURI
.length() + 1);
344 //create the movie from the handle that refers to the URI
345 err
= NewMovieFromDataRef(&m_movie
, newMovieActive
,
347 URLDataHandlerSubType
);
349 DisposeHandle(theHandle
);
354 //preroll movie for streaming
355 //TODO:Async this using threads?
358 timeNow
= GetMovieTime(m_movie
, NULL
);
359 playRate
= GetMoviePreferredRate(m_movie
);
360 PrePrerollMovie(m_movie
, timeNow
, playRate
, NULL
, NULL
);
361 PrerollMovie(m_movie
, timeNow
, playRate
);
362 SetMovieRate(m_movie
, playRate
);
366 return ::GetMoviesError() == noErr
;
369 //---------------------------------------------------------------------------
370 // wxQTMediaBackend::FinishLoad
372 // 1) Create the movie timer
373 // 2) Get real size of movie for GetBestSize/sizers
374 // 3) See if there is video in the movie, and if so then either
375 // SetMovieGWorld if < 10.2 or use Native CreateMovieControl
376 // 4) Set the movie time scale to something usable so that seeking
377 // etc. will work correctly
378 // 5) Refresh parent window
379 //---------------------------------------------------------------------------
380 void wxQTMediaBackend::FinishLoad()
382 m_timer
= new _wxQTTimer(m_movie
, (wxQTMediaBackend
*) this);
385 //get the real size of the movie
387 ::GetMovieNaturalBoundsRect (m_movie
, &outRect
);
388 wxASSERT(::GetMoviesError() == noErr
);
390 m_bestSize
.x
= outRect
.right
- outRect
.left
;
391 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
393 //reparent movie/*AudioMediaCharacteristic*/
394 if(GetMovieIndTrackType(m_movie
, 1,
395 VisualMediaCharacteristic
,
396 movieTrackCharacteristic
|
397 movieTrackEnabledOnly
) != NULL
)
399 #if wxUSE_CREATEMOVIECONTROL
401 //Native CreateMovieControl QT control (Thanks to Kevin Olliver's
402 //wxQTMovie for some of this).
404 Rect bounds
= wxMacGetBoundsForControl(m_ctrl
,
405 m_ctrl
->GetPosition(),
408 //kMovieControlOptionXXX
409 //HideController - hide the movie controller
410 //LocateTopLeft - movie is pinned to top left rather than centered in the control
411 //EnableEditing - Allows programmatic editing and dragn'drop
412 //HandleEditingHI- Installs event stuff for edit menu - forces EnableEditing also
413 //SetKeysEnabled - Allows keyboard input
414 //ManuallyIdled - app handles movie idling rather than internal timer event loop
415 ::CreateMovieControl(
417 m_ctrl
->MacGetTopLevelWindowRef(), //parent
418 &bounds
, //control bounds
419 m_movie
, //movie handle
420 kMovieControlOptionHideController
421 | kMovieControlOptionLocateTopLeft
422 | kMovieControlOptionSetKeysEnabled
423 // | kMovieControlOptionManuallyIdled
425 GetControlPeer(m_ctrl
)->GetControlRefAddr() );
427 ::EmbedControl(GetControlPeer(m_ctrl
)->GetControlRef(),
428 (ControlRef
) m_ctrl
->GetParent()->GetHandle());
433 SetMovieGWorld(m_movie
,
437 m_ctrl
->MacGetTopLevelWindowRef()
443 //we want millisecond precision
444 ::SetMovieTimeScale(m_movie
, 1000);
445 wxASSERT(::GetMoviesError() == noErr
);
448 //Here, if the parent of the control has a sizer - we
449 //tell it to recalculate the size of this control since
450 //the user opened a seperate media file
452 m_ctrl
->InvalidateBestSize();
453 m_ctrl
->GetParent()->Layout();
454 m_ctrl
->GetParent()->Refresh();
455 m_ctrl
->GetParent()->Update();
458 //---------------------------------------------------------------------------
459 // wxQTMediaBackend::Play
461 // 1) Start the QT movie
462 // 2) Start the movie loading timer
463 //---------------------------------------------------------------------------
464 bool wxQTMediaBackend::Play()
466 ::StartMovie(m_movie
);
467 m_timer
->SetPaused(false);
468 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
469 return ::GetMoviesError() == noErr
;
472 //---------------------------------------------------------------------------
473 // wxQTMediaBackend::Pause
476 // 2) Stop the movie timer
477 //---------------------------------------------------------------------------
478 bool wxQTMediaBackend::Pause()
480 ::StopMovie(m_movie
);
481 m_timer
->SetPaused(true);
483 return ::GetMoviesError() == noErr
;
486 //---------------------------------------------------------------------------
487 // wxQTMediaBackend::Stop
490 // 2) Stop the movie timer
491 // 3) Seek to the beginning of the movie
492 //---------------------------------------------------------------------------
493 bool wxQTMediaBackend::Stop()
495 m_timer
->SetPaused(false);
498 ::StopMovie(m_movie
);
499 if(::GetMoviesError() != noErr
)
502 ::GoToBeginningOfMovie(m_movie
);
503 return ::GetMoviesError() == noErr
;
506 //---------------------------------------------------------------------------
507 // wxQTMediaBackend::GetPlaybackRate
509 // 1) Get the movie playback rate from ::GetMovieRate
510 //---------------------------------------------------------------------------
511 double wxQTMediaBackend::GetPlaybackRate()
513 return ( ((double)::GetMovieRate(m_movie
)) / 0x10000);
516 //---------------------------------------------------------------------------
517 // wxQTMediaBackend::SetPlaybackRate
519 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
520 //---------------------------------------------------------------------------
521 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
523 ::SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
524 return ::GetMoviesError() == noErr
;
527 //---------------------------------------------------------------------------
528 // wxQTMediaBackend::SetPosition
530 // 1) Create a time record struct (TimeRecord) with appropriate values
531 // 2) Pass struct to SetMovieTime
532 //---------------------------------------------------------------------------
533 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
535 TimeRecord theTimeRecord
;
536 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
537 theTimeRecord
.value
.lo
= where
.GetValue();
538 theTimeRecord
.scale
= ::GetMovieTimeScale(m_movie
);
539 theTimeRecord
.base
= ::GetMovieTimeBase(m_movie
);
540 ::SetMovieTime(m_movie
, &theTimeRecord
);
542 if (::GetMoviesError() != noErr
)
548 //---------------------------------------------------------------------------
549 // wxQTMediaBackend::GetPosition
551 // Calls GetMovieTime
552 //---------------------------------------------------------------------------
553 wxLongLong
wxQTMediaBackend::GetPosition()
555 return ::GetMovieTime(m_movie
, NULL
);
558 //---------------------------------------------------------------------------
559 // wxQTMediaBackend::GetDuration
561 // Calls GetMovieDuration
562 //---------------------------------------------------------------------------
563 wxLongLong
wxQTMediaBackend::GetDuration()
565 return ::GetMovieDuration(m_movie
);
568 //---------------------------------------------------------------------------
569 // wxQTMediaBackend::GetState
571 // Determines the current state - the timer keeps track of whether or not
572 // we are paused or stopped (if the timer is running we are playing)
573 //---------------------------------------------------------------------------
574 wxMediaState
wxQTMediaBackend::GetState()
576 if ( !m_timer
|| (m_timer
->IsRunning() == false &&
577 m_timer
->GetPaused() == false) )
578 return wxMEDIASTATE_STOPPED
;
580 if( m_timer
->IsRunning() == true )
581 return wxMEDIASTATE_PLAYING
;
583 return wxMEDIASTATE_PAUSED
;
586 //---------------------------------------------------------------------------
587 // wxQTMediaBackend::Cleanup
589 // Diposes of the movie timer, Control if native, and stops and disposes
591 //---------------------------------------------------------------------------
592 void wxQTMediaBackend::Cleanup()
597 #if wxUSE_CREATEMOVIECONTROL
598 DisposeControl(GetControlPeer(m_ctrl
)->GetControlRef());
602 DisposeMovie(m_movie
);
605 //---------------------------------------------------------------------------
606 // wxQTMediaBackend::GetVideoSize
608 // Returns the actual size of the QT movie
609 //---------------------------------------------------------------------------
610 wxSize
wxQTMediaBackend::GetVideoSize() const
615 //---------------------------------------------------------------------------
616 // wxQTMediaBackend::Move
618 // If not using a native 10.2 QT control performs some emulated window
620 //---------------------------------------------------------------------------
621 void wxQTMediaBackend::Move(int x
, int y
, int w
, int h
)
623 #if !wxUSE_CREATEMOVIECONTROL
628 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
631 Rect theRect
= {y
, x
, y
+h
, x
+w
};
633 ::SetMovieBox(m_movie
, &theRect
);
634 wxASSERT(::GetMoviesError() == noErr
);
640 //in source file that contains stuff you don't directly use
641 #include <wx/html/forcelnk.h>
642 FORCE_LINK_ME(basewxmediabackends
);
644 #endif //wxUSE_MEDIACTRL