1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/cocoa/mediactrl.cpp
3 // Purpose: Built-in Media Backends for Cocoa
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) 2004-2005 Ryan Norton, (c) 2005 David Elliot
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 // BACKEND DECLARATIONS
43 //===========================================================================
45 //---------------------------------------------------------------------------
49 //---------------------------------------------------------------------------
51 //---------------------------------------------------------------------------
53 //---------------------------------------------------------------------------
55 #include <QuickTime/QuickTime.h>
57 #include "wx/cocoa/autorelease.h"
58 #include "wx/cocoa/string.h"
60 #import <AppKit/NSMovie.h>
61 #import <AppKit/NSMovieView.h>
64 class WXDLLIMPEXP_MEDIA wxQTMediaBackend : public wxMediaBackend
71 virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
76 const wxValidator& validator,
77 const wxString& name);
83 virtual bool Load(const wxString& fileName);
84 virtual bool Load(const wxURI& location);
86 virtual wxMediaState GetState();
88 virtual bool SetPosition(wxLongLong where);
89 virtual wxLongLong GetPosition();
90 virtual wxLongLong GetDuration();
92 virtual void Move(int x, int y, int w, int h);
93 wxSize GetVideoSize() const;
95 virtual double GetPlaybackRate();
96 virtual bool SetPlaybackRate(double dRate);
101 wxSize m_bestSize; //Original movie size
102 Movie m_movie; //QT Movie handle/instance
103 NSMovieView* m_movieview; //NSMovieView instance
104 wxControl* m_ctrl; //Parent control
105 bool m_bVideo; //Whether or not we have video
106 class _wxQTTimer* m_timer; //Timer for streaming the movie
108 DECLARE_DYNAMIC_CLASS(wxQTMediaBackend);
112 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
116 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
118 IMPLEMENT_DYNAMIC_CLASS(wxQTMediaBackend, wxMediaBackend);
120 //Time between timer calls
121 #define MOVIE_DELAY 100
123 // --------------------------------------------------------------------------
124 // wxQTTimer - Handle Asyncronous Playing
125 // --------------------------------------------------------------------------
126 class _wxQTTimer : public wxTimer
129 _wxQTTimer(Movie movie, wxQTMediaBackend* parent) :
130 m_movie(movie), m_bPaused(false), m_parent(parent)
138 bool GetPaused() {return m_bPaused;}
139 void SetPaused(bool bPaused) {m_bPaused = bPaused;}
141 //-----------------------------------------------------------------------
142 // _wxQTTimer::Notify
144 // 1) Checks to see if the movie is done, and if not continues
145 // streaming the movie
146 // 2) Sends the wxEVT_MEDIA_STOP event if we have reached the end of
148 //-----------------------------------------------------------------------
153 if(!IsMovieDone(m_movie))
154 MoviesTask(m_movie, MOVIE_DELAY);
157 wxMediaEvent theEvent(wxEVT_MEDIA_STOP,
158 m_parent->m_ctrl->GetId());
159 m_parent->m_ctrl->ProcessEvent(theEvent);
161 if(theEvent.IsAllowed())
165 wxASSERT(::GetMoviesError() == noErr);
167 //send the event to our child
168 wxMediaEvent theEvent(wxEVT_MEDIA_FINISHED,
169 m_parent->m_ctrl->GetId());
170 m_parent->m_ctrl->ProcessEvent(theEvent);
177 Movie m_movie; //Our movie instance
178 bool m_bPaused; //Whether we are paused or not
179 wxQTMediaBackend* m_parent; //Backend pointer
182 //---------------------------------------------------------------------------
183 // wxQTMediaBackend Constructor
185 // Sets m_timer to NULL signifying we havn't loaded anything yet
186 //---------------------------------------------------------------------------
187 wxQTMediaBackend::wxQTMediaBackend() : m_timer(NULL)
191 //---------------------------------------------------------------------------
192 // wxQTMediaBackend Destructor
194 // 1) Cleans up the QuickTime movie instance
195 // 2) Decrements the QuickTime reference counter - if this reaches
196 // 0, QuickTime shuts down
197 // 3) Decrements the QuickTime Windows Media Layer reference counter -
198 // if this reaches 0, QuickTime shuts down the Windows Media Layer
199 //---------------------------------------------------------------------------
200 wxQTMediaBackend::~wxQTMediaBackend()
205 //Note that ExitMovies() is not neccessary...
209 //---------------------------------------------------------------------------
210 // wxQTMediaBackend::CreateControl
212 // 1) Intializes QuickTime
213 // 2) Creates the control window
214 //---------------------------------------------------------------------------
215 bool wxQTMediaBackend::CreateControl(wxControl* inctrl, wxWindow* parent,
220 const wxValidator& validator,
221 const wxString& name)
225 wxMediaCtrl* ctrl = (wxMediaCtrl*) inctrl;
227 //Create the control base
228 wxASSERT(ctrl->CreateBase(parent,wid,pos,size,style, validator, size));
230 //Create the NSMovieView
231 ctrl->SetNSView(NULL);
232 NSView* theView = [[NSMovieView alloc] initWithFrame: ctrl->MakeDefaultNSRect(size)];
233 ctrl->SetNSView(theView);
238 parent->AddChild(ctrl);
239 parent->CocoaAddChild(ctrl);
240 ctrl->SetInitialFrameRect(pos,size);
243 m_movieview = theView;
248 //---------------------------------------------------------------------------
249 // wxQTMediaBackend::Load (file version)
251 // Calls the URI version
252 //---------------------------------------------------------------------------
253 bool wxQTMediaBackend::Load(const wxString& fileName)
257 wxString( wxT("file://") ) + fileName
262 //---------------------------------------------------------------------------
263 // wxQTMediaBackend::Load (URL Version)
265 // 1) Build an escaped URI from location
267 //---------------------------------------------------------------------------
268 bool wxQTMediaBackend::Load(const wxURI& location)
273 wxString theURI = location.BuildURI();
275 [m_movieview setMovie:[[NSMovie alloc] initWithURL: [NSURL URLWithString: wxNSStringWithWxString(theURI)]
276 byReference: YES ] ];
278 m_movie = (Movie) [[m_movieview movie] QTMovie];
280 //preroll movie for streaming
281 //TODO:Async this using threads?
284 timeNow = GetMovieTime(m_movie, NULL);
285 playRate = GetMoviePreferredRate(m_movie);
286 PrePrerollMovie(m_movie, timeNow, playRate, NULL, NULL);
287 PrerollMovie(m_movie, timeNow, playRate);
288 SetMovieRate(m_movie, playRate);
292 return ::GetMoviesError() == noErr;
295 //---------------------------------------------------------------------------
296 // wxQTMediaBackend::FinishLoad
298 // 1) Create the movie timer
299 // 2) Get real size of movie for GetBestSize/sizers
300 // 3) See if there is video in the movie, and if so then either
301 // SetMovieGWorld if < 10.2 or use Native CreateMovieControl
302 // 4) Set the movie time scale to something usable so that seeking
303 // etc. will work correctly
304 // 5) Refresh parent window
305 //---------------------------------------------------------------------------
306 void wxQTMediaBackend::FinishLoad()
308 m_timer = new _wxQTTimer(m_movie, (wxQTMediaBackend*) this);
311 //get the real size of the movie
313 ::GetMovieNaturalBoundsRect (m_movie, &outRect);
314 wxASSERT(::GetMoviesError() == noErr);
316 m_bestSize.x = outRect.right - outRect.left;
317 m_bestSize.y = outRect.bottom - outRect.top;
319 //we want millisecond precision
320 ::SetMovieTimeScale(m_movie, 1000);
321 wxASSERT(::GetMoviesError() == noErr);
324 //Here, if the parent of the control has a sizer - we
325 //tell it to recalculate the size of this control since
326 //the user opened a seperate media file
328 m_ctrl->InvalidateBestSize();
329 m_ctrl->GetParent()->Layout();
330 m_ctrl->GetParent()->Refresh();
331 m_ctrl->GetParent()->Update();
334 //---------------------------------------------------------------------------
335 // wxQTMediaBackend::Play
337 // 1) Start the QT movie
338 // 2) Start the movie loading timer
339 //---------------------------------------------------------------------------
340 bool wxQTMediaBackend::Play()
342 ::StartMovie(m_movie);
343 m_timer->SetPaused(false);
344 m_timer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
345 return ::GetMoviesError() == noErr;
348 //---------------------------------------------------------------------------
349 // wxQTMediaBackend::Pause
352 // 2) Stop the movie timer
353 //---------------------------------------------------------------------------
354 bool wxQTMediaBackend::Pause()
356 ::StopMovie(m_movie);
357 m_timer->SetPaused(true);
359 return ::GetMoviesError() == noErr;
362 //---------------------------------------------------------------------------
363 // wxQTMediaBackend::Stop
366 // 2) Stop the movie timer
367 // 3) Seek to the beginning of the movie
368 //---------------------------------------------------------------------------
369 bool wxQTMediaBackend::Stop()
371 m_timer->SetPaused(false);
374 ::StopMovie(m_movie);
375 if(::GetMoviesError() != noErr)
378 ::GoToBeginningOfMovie(m_movie);
379 return ::GetMoviesError() == noErr;
382 //---------------------------------------------------------------------------
383 // wxQTMediaBackend::GetPlaybackRate
385 // 1) Get the movie playback rate from ::GetMovieRate
386 //---------------------------------------------------------------------------
387 double wxQTMediaBackend::GetPlaybackRate()
389 return ( ((double)::GetMovieRate(m_movie)) / 0x10000);
392 //---------------------------------------------------------------------------
393 // wxQTMediaBackend::SetPlaybackRate
395 // 1) Convert dRate to Fixed and Set the movie rate through SetMovieRate
396 //---------------------------------------------------------------------------
397 bool wxQTMediaBackend::SetPlaybackRate(double dRate)
399 ::SetMovieRate(m_movie, (Fixed) (dRate * 0x10000));
400 return ::GetMoviesError() == noErr;
403 //---------------------------------------------------------------------------
404 // wxQTMediaBackend::SetPosition
406 // 1) Create a time record struct (TimeRecord) with appropriate values
407 // 2) Pass struct to SetMovieTime
408 //---------------------------------------------------------------------------
409 bool wxQTMediaBackend::SetPosition(wxLongLong where)
411 TimeRecord theTimeRecord;
412 memset(&theTimeRecord, 0, sizeof(TimeRecord));
413 theTimeRecord.value.lo = where.GetValue();
414 theTimeRecord.scale = ::GetMovieTimeScale(m_movie);
415 theTimeRecord.base = ::GetMovieTimeBase(m_movie);
416 ::SetMovieTime(m_movie, &theTimeRecord);
418 if (::GetMoviesError() != noErr)
424 //---------------------------------------------------------------------------
425 // wxQTMediaBackend::GetPosition
427 // Calls GetMovieTime
428 //---------------------------------------------------------------------------
429 wxLongLong wxQTMediaBackend::GetPosition()
431 return ::GetMovieTime(m_movie, NULL);
434 //---------------------------------------------------------------------------
435 // wxQTMediaBackend::GetDuration
437 // Calls GetMovieDuration
438 //---------------------------------------------------------------------------
439 wxLongLong wxQTMediaBackend::GetDuration()
441 return ::GetMovieDuration(m_movie);
444 //---------------------------------------------------------------------------
445 // wxQTMediaBackend::GetState
447 // Determines the current state - the timer keeps track of whether or not
448 // we are paused or stopped (if the timer is running we are playing)
449 //---------------------------------------------------------------------------
450 wxMediaState wxQTMediaBackend::GetState()
452 if ( !m_timer || (m_timer->IsRunning() == false &&
453 m_timer->GetPaused() == false) )
454 return wxMEDIASTATE_STOPPED;
456 if( m_timer->IsRunning() == true )
457 return wxMEDIASTATE_PLAYING;
459 return wxMEDIASTATE_PAUSED;
462 //---------------------------------------------------------------------------
463 // wxQTMediaBackend::Cleanup
465 // Diposes of the movie timer, Control if native, and stops and disposes
467 //---------------------------------------------------------------------------
468 void wxQTMediaBackend::Cleanup()
473 [[m_movieview movie] release];
474 [m_movieview setMovie:NULL];
477 //---------------------------------------------------------------------------
478 // wxQTMediaBackend::GetVideoSize
480 // Returns the actual size of the QT movie
481 //---------------------------------------------------------------------------
482 wxSize wxQTMediaBackend::GetVideoSize() const
487 //---------------------------------------------------------------------------
488 // wxQTMediaBackend::Move
490 // Nothin... cocoa takes care of this for us
491 //---------------------------------------------------------------------------
492 void wxQTMediaBackend::Move(int x, int y, int w, int h)
497 //in source file that contains stuff you don't directly use
498 #include <wx/html/forcelnk.h>
499 FORCE_LINK_ME(basewxmediabackends);
501 #endif //wxUSE_MEDIACTRL