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
122 struct MovieType
** m_movie
; //QT Movie handle/instance
126 wxControl
* m_ctrl
; //Parent control
127 bool m_bVideo
; //Whether or not we have video
128 class _wxQTTimer
* m_timer
; //Timer for streaming the movie
130 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend
);
134 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
138 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
140 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend
, wxMediaBackend
);
142 //Time between timer calls
143 #define MOVIE_DELAY 100
145 // --------------------------------------------------------------------------
146 // wxQTTimer - Handle Asyncronous Playing
147 // --------------------------------------------------------------------------
148 class _wxQTTimer
: public wxTimer
151 _wxQTTimer(Movie movie
, wxQTMediaBackend
* parent
) :
152 m_movie(movie
), m_bPaused(false), m_parent(parent
)
160 bool GetPaused() {return m_bPaused
;}
161 void SetPaused(bool bPaused
) {m_bPaused
= bPaused
;}
163 //-----------------------------------------------------------------------
164 // _wxQTTimer::Notify
166 // 1) Checks to see if the movie is done, and if not continues
167 // streaming the movie
168 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
170 //-----------------------------------------------------------------------
175 if(!IsMovieDone(m_movie
))
176 MoviesTask(m_movie
, MOVIE_DELAY
);
179 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
,
180 m_parent
->m_ctrl
->GetId());
181 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
183 if(theEvent
.IsAllowed())
187 wxASSERT(::GetMoviesError() == noErr
);
189 //send the event to our child
190 wxMediaEvent
theEvent(wxEVT_MEDIA_FINISHED
,
191 m_parent
->m_ctrl
->GetId());
192 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
199 Movie m_movie
; //Our movie instance
200 bool m_bPaused
; //Whether we are paused or not
201 wxQTMediaBackend
* m_parent
; //Backend pointer
204 //---------------------------------------------------------------------------
205 // wxQTMediaBackend Constructor
207 // Sets m_timer to NULL signifying we havn't loaded anything yet
208 //---------------------------------------------------------------------------
209 wxQTMediaBackend::wxQTMediaBackend() : m_timer(NULL
)
213 //---------------------------------------------------------------------------
214 // wxQTMediaBackend Destructor
216 // 1) Cleans up the QuickTime movie instance
217 // 2) Decrements the QuickTime reference counter - if this reaches
218 // 0, QuickTime shuts down
219 // 3) Decrements the QuickTime Windows Media Layer reference counter -
220 // if this reaches 0, QuickTime shuts down the Windows Media Layer
221 //---------------------------------------------------------------------------
222 wxQTMediaBackend::~wxQTMediaBackend()
227 //Note that ExitMovies() is not necessary...
231 //---------------------------------------------------------------------------
232 // wxQTMediaBackend::CreateControl
234 // 1) Intializes QuickTime
235 // 2) Creates the control window
236 //---------------------------------------------------------------------------
237 bool wxQTMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
242 const wxValidator
& validator
,
243 const wxString
& name
)
245 //Don't bother in Native control mode
246 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_2 )
247 if (!_wxIsQuickTime4Installed())
255 // By default wxWindow(s) is created with a border -
256 // so we need to get rid of those
258 // Since we don't have a child window like most other
259 // backends, we don't need wxCLIP_CHILDREN
263 #if wxUSE_CREATEMOVIECONTROL
264 ctrl
->wxWindow::Create(parent
, id
, pos
, size
,
265 m_ctrl
->MacRemoveBordersFromStyle(style
),
268 ctrl
->wxControl::Create(parent
, id
, pos
, size
,
269 m_ctrl
->MacRemoveBordersFromStyle(style
),
276 ctrl
->SetValidator(validator
);
283 //---------------------------------------------------------------------------
284 // wxQTMediaBackend::Load (file version)
286 // 1) Get an FSSpec from the Windows path name
288 // 3) Obtain the movie instance from the movie resource
289 // 4) Close the movie resource
291 //---------------------------------------------------------------------------
292 bool wxQTMediaBackend::Load(const wxString
& fileName
)
301 //FIXME:wxMacFilename2FSSpec crashes on empty string -
302 //does it crash on other strings too and should this
303 //"fix" be put in the carbon wxSound?
304 if (fileName
.empty())
307 wxMacFilename2FSSpec( fileName
, &sfFile
);
309 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
312 short movieResID
= 0;
315 err
= NewMovieFromFile (
323 CloseMovieFile (movieResFile
);
330 return ::GetMoviesError() == noErr
;
333 //---------------------------------------------------------------------------
334 // wxQTMediaBackend::Load (URL Version)
336 // 1) Build an escaped URI from location
337 // 2) Create a handle to store the URI string
338 // 3) Put the URI string inside the handle
339 // 4) Make a QuickTime URL data ref from the handle with the URI in it
340 // 5) Clean up the URI string handle
341 // 6) Do some prerolling
343 //---------------------------------------------------------------------------
344 bool wxQTMediaBackend::Load(const wxURI
& location
)
349 wxString theURI
= location
.BuildURI();
353 Handle theHandle
= NewHandleClear(theURI
.length() + 1);
356 BlockMove(theURI
.mb_str(), *theHandle
, theURI
.length() + 1);
358 //create the movie from the handle that refers to the URI
359 err
= NewMovieFromDataRef(&m_movie
, newMovieActive
,
361 URLDataHandlerSubType
);
363 DisposeHandle(theHandle
);
368 //preroll movie for streaming
369 //TODO:Async this using threads?
372 timeNow
= GetMovieTime(m_movie
, NULL
);
373 playRate
= GetMoviePreferredRate(m_movie
);
374 PrePrerollMovie(m_movie
, timeNow
, playRate
, NULL
, NULL
);
375 PrerollMovie(m_movie
, timeNow
, playRate
);
376 SetMovieRate(m_movie
, playRate
);
380 return ::GetMoviesError() == noErr
;
383 //---------------------------------------------------------------------------
384 // wxQTMediaBackend::FinishLoad
386 // 1) Create the movie timer
387 // 2) Get real size of movie for GetBestSize/sizers
388 // 3) See if there is video in the movie, and if so then either
389 // SetMovieGWorld if < 10.2 or use Native CreateMovieControl
390 // 4) Set the movie time scale to something usable so that seeking
391 // etc. will work correctly
392 // 5) Refresh parent window
393 //---------------------------------------------------------------------------
394 void wxQTMediaBackend::FinishLoad()
396 m_timer
= new _wxQTTimer(m_movie
, (wxQTMediaBackend
*) this);
399 //get the real size of the movie
401 ::GetMovieNaturalBoundsRect (m_movie
, &outRect
);
402 wxASSERT(::GetMoviesError() == noErr
);
404 m_bestSize
.x
= outRect
.right
- outRect
.left
;
405 m_bestSize
.y
= outRect
.bottom
- outRect
.top
;
407 //reparent movie/*AudioMediaCharacteristic*/
408 if(GetMovieIndTrackType(m_movie
, 1,
409 VisualMediaCharacteristic
,
410 movieTrackCharacteristic
|
411 movieTrackEnabledOnly
) != NULL
)
413 #if wxUSE_CREATEMOVIECONTROL
415 //Native CreateMovieControl QT control (Thanks to Kevin Olliver's
416 //wxQTMovie for some of this).
418 #define GetControlPeer(whatever) ctrl->m_peer
419 wxMediaCtrl
* ctrl
= (wxMediaCtrl
*) m_ctrl
;
420 Rect bounds
= wxMacGetBoundsForControl(m_ctrl
,
421 m_ctrl
->GetPosition(),
424 //Dispose of old control for new one
425 if (GetControlPeer(m_ctrl
) && GetControlPeer(m_ctrl
)->Ok() )
426 GetControlPeer(m_ctrl
)->Dispose();
429 //kMovieControlOptionXXX
430 //HideController - hide the movie controller
431 //LocateTopLeft - movie is pinned to top left rather than centered in the control
432 //EnableEditing - Allows programmatic editing and dragn'drop
433 //HandleEditingHI- Installs event stuff for edit menu - forces EnableEditing also
434 //SetKeysEnabled - Allows keyboard input
435 //ManuallyIdled - app handles movie idling rather than internal timer event loop
436 ::CreateMovieControl(
438 ctrl
->MacGetTopLevelWindowRef(), //parent
439 &bounds
, //control bounds
440 m_movie
, //movie handle
441 kMovieControlOptionHideController
442 | kMovieControlOptionLocateTopLeft
443 | kMovieControlOptionSetKeysEnabled
444 // | kMovieControlOptionManuallyIdled
446 ctrl
->m_peer
->GetControlRefAddr() );
448 ::EmbedControl(ctrl
->m_peer
->GetControlRef(), (ControlRef
)ctrl
->GetParent()->GetHandle());
453 SetMovieGWorld(m_movie
,
457 m_ctrl
->MacGetTopLevelWindowRef()
463 //we want millisecond precision
464 ::SetMovieTimeScale(m_movie
, 1000);
465 wxASSERT(::GetMoviesError() == noErr
);
468 //Here, if the parent of the control has a sizer - we
469 //tell it to recalculate the size of this control since
470 //the user opened a separate media file
472 m_ctrl
->InvalidateBestSize();
473 m_ctrl
->GetParent()->Layout();
474 m_ctrl
->GetParent()->Refresh();
475 m_ctrl
->GetParent()->Update();
478 //---------------------------------------------------------------------------
479 // wxQTMediaBackend::Play
481 // 1) Start the QT movie
482 // 2) Start the movie loading timer
483 //---------------------------------------------------------------------------
484 bool wxQTMediaBackend::Play()
486 ::StartMovie(m_movie
);
487 m_timer
->SetPaused(false);
488 m_timer
->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
489 return ::GetMoviesError() == noErr
;
492 //---------------------------------------------------------------------------
493 // wxQTMediaBackend::Pause
496 // 2) Stop the movie timer
497 //---------------------------------------------------------------------------
498 bool wxQTMediaBackend::Pause()
500 ::StopMovie(m_movie
);
501 m_timer
->SetPaused(true);
503 return ::GetMoviesError() == noErr
;
506 //---------------------------------------------------------------------------
507 // wxQTMediaBackend::Stop
510 // 2) Stop the movie timer
511 // 3) Seek to the beginning of the movie
512 //---------------------------------------------------------------------------
513 bool wxQTMediaBackend::Stop()
515 m_timer
->SetPaused(false);
518 ::StopMovie(m_movie
);
519 if(::GetMoviesError() != noErr
)
522 ::GoToBeginningOfMovie(m_movie
);
523 return ::GetMoviesError() == noErr
;
526 //---------------------------------------------------------------------------
527 // wxQTMediaBackend::GetPlaybackRate
529 // 1) Get the movie playback rate from ::GetMovieRate
530 //---------------------------------------------------------------------------
531 double wxQTMediaBackend::GetPlaybackRate()
533 return ( ((double)::GetMovieRate(m_movie
)) / 0x10000);
536 //---------------------------------------------------------------------------
537 // wxQTMediaBackend::SetPlaybackRate
539 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
540 //---------------------------------------------------------------------------
541 bool wxQTMediaBackend::SetPlaybackRate(double dRate
)
543 ::SetMovieRate(m_movie
, (Fixed
) (dRate
* 0x10000));
544 return ::GetMoviesError() == noErr
;
547 //---------------------------------------------------------------------------
548 // wxQTMediaBackend::SetPosition
550 // 1) Create a time record struct (TimeRecord) with appropriate values
551 // 2) Pass struct to SetMovieTime
552 //---------------------------------------------------------------------------
553 bool wxQTMediaBackend::SetPosition(wxLongLong where
)
555 TimeRecord theTimeRecord
;
556 memset(&theTimeRecord
, 0, sizeof(TimeRecord
));
557 theTimeRecord
.value
.lo
= where
.GetValue();
558 theTimeRecord
.scale
= ::GetMovieTimeScale(m_movie
);
559 theTimeRecord
.base
= ::GetMovieTimeBase(m_movie
);
560 ::SetMovieTime(m_movie
, &theTimeRecord
);
562 if (::GetMoviesError() != noErr
)
568 //---------------------------------------------------------------------------
569 // wxQTMediaBackend::GetPosition
571 // Calls GetMovieTime
572 //---------------------------------------------------------------------------
573 wxLongLong
wxQTMediaBackend::GetPosition()
575 return ::GetMovieTime(m_movie
, NULL
);
578 //---------------------------------------------------------------------------
579 // wxQTMediaBackend::GetDuration
581 // Calls GetMovieDuration
582 //---------------------------------------------------------------------------
583 wxLongLong
wxQTMediaBackend::GetDuration()
585 return ::GetMovieDuration(m_movie
);
588 //---------------------------------------------------------------------------
589 // wxQTMediaBackend::GetState
591 // Determines the current state - the timer keeps track of whether or not
592 // we are paused or stopped (if the timer is running we are playing)
593 //---------------------------------------------------------------------------
594 wxMediaState
wxQTMediaBackend::GetState()
596 if ( !m_timer
|| (m_timer
->IsRunning() == false &&
597 m_timer
->GetPaused() == false) )
598 return wxMEDIASTATE_STOPPED
;
600 if( m_timer
->IsRunning() == true )
601 return wxMEDIASTATE_PLAYING
;
603 return wxMEDIASTATE_PAUSED
;
606 //---------------------------------------------------------------------------
607 // wxQTMediaBackend::Cleanup
609 // Diposes of the movie timer, Control if native, and stops and disposes
611 //---------------------------------------------------------------------------
612 void wxQTMediaBackend::Cleanup()
617 #if wxUSE_CREATEMOVIECONTROL
618 DisposeControl(((wxMediaCtrl
*)m_ctrl
)->m_peer
->GetControlRef());
622 DisposeMovie(m_movie
);
625 //---------------------------------------------------------------------------
626 // wxQTMediaBackend::GetVideoSize
628 // Returns the actual size of the QT movie
629 //---------------------------------------------------------------------------
630 wxSize
wxQTMediaBackend::GetVideoSize() const
635 //---------------------------------------------------------------------------
636 // wxQTMediaBackend::Move
638 // We need to do this even when using native qt control because
639 // CreateMovieControl is broken in this regard...
640 //---------------------------------------------------------------------------
641 void wxQTMediaBackend::Move(int x
, int y
, int w
, int h
)
643 #if !wxUSE_CREATEMOVIECONTROL
648 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
651 Rect theRect
= {y
, x
, y
+h
, x
+w
};
653 ::SetMovieBox(m_movie
, &theRect
);
654 wxASSERT(::GetMoviesError() == noErr
);
657 if(m_timer
&& m_ctrl
)
659 m_ctrl
->GetParent()->MacWindowToRootWindow(&x
, &y
);
661 ::MoveControl( (ControlRef
) m_ctrl
->GetHandle(), x
, y
);
662 m_ctrl
->GetParent()->Refresh();
663 m_ctrl
->GetParent()->Update();
669 //in source file that contains stuff you don't directly use
670 #include "wx/html/forcelnk.h"
671 FORCE_LINK_ME(basewxmediabackends
);
673 #endif //wxUSE_MEDIACTRL