1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/mediactrl.cpp
3 // Purpose: Built-in Media Backends for Unix
4 // Author: Ryan Norton <wxprojects@comcast.net>
8 // Copyright: (c) 2004-2005 Ryan Norton
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 //---------------------------------------------------------------------------
47 // wxGStreamerMediaBackend
50 //TODO: This is really not the best way to play-stop -
51 //TODO: it should just have one playbin and stick with it the whole
52 //TODO: instance of wxGStreamerMediaBackend - but stopping appears
53 //TODO: to invalidate the playbin object...
56 //---------------------------------------------------------------------------
59 //---------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
63 #include <gst/xoverlay/xoverlay.h>
65 #include <string.h> //strstr
68 #include "wx/msgdlg.h"
71 //for <gdk/gdkx.h>/related for GDK_WINDOW_XWINDOW
72 # include "wx/gtk/win_gtk.h"
73 # include <gtk/gtksignal.h>
74 //# include <gst/gconf/gconf.h> //gstreamer gnome interface
78 class WXDLLIMPEXP_MEDIA wxGStreamerMediaBackend
: public wxMediaBackend
82 wxGStreamerMediaBackend();
83 ~wxGStreamerMediaBackend();
85 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
90 const wxValidator
& validator
,
91 const wxString
& name
);
97 virtual bool Load(const wxString
& fileName
);
98 virtual bool Load(const wxURI
& location
);
100 virtual wxMediaState
GetState();
102 virtual bool SetPosition(wxLongLong where
);
103 virtual wxLongLong
GetPosition();
104 virtual wxLongLong
GetDuration();
106 virtual void Move(int x
, int y
, int w
, int h
);
107 wxSize
GetVideoSize() const;
109 virtual double GetPlaybackRate();
110 virtual bool SetPlaybackRate(double dRate
);
114 static void OnFinish(GstElement
*play
, gpointer data
);
115 static void OnError (GstElement
*play
, GstElement
*src
,
116 GError
*err
, gchar
*debug
,
118 static void OnVideoCapsReady(GstPad
* pad
, GParamSpec
* pspec
, gpointer data
);
120 static bool TransCapsToVideoSize(wxGStreamerMediaBackend
* be
, GstPad
* caps
);
121 void PostRecalcSize();
124 static gint
OnGTKRealize(GtkWidget
* theWidget
, wxGStreamerMediaBackend
* be
);
127 GstElement
* m_player
; //GStreamer media element
132 wxLongLong m_nPausedPos
;
134 DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend
);
138 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
140 // wxGStreamerMediaBackend
142 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
144 IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend
, wxMediaBackend
);
146 //---------------------------------------------------------------------------
147 // wxGStreamerMediaBackend Constructor
149 // Sets m_player to NULL signifying we havn't loaded anything yet
150 //---------------------------------------------------------------------------
151 wxGStreamerMediaBackend
::wxGStreamerMediaBackend() : m_player(NULL
), m_videoSize(0,0)
155 //---------------------------------------------------------------------------
156 // wxGStreamerMediaBackend Destructor
158 // Stops/cleans up memory
159 //---------------------------------------------------------------------------
160 wxGStreamerMediaBackend
::~wxGStreamerMediaBackend()
165 //---------------------------------------------------------------------------
166 // wxGStreamerMediaBackend::OnGTKRealize
168 // If the window wasn't realized when Load was called, this is the
169 // callback for when it is.
171 // 1) Installs GTK idle handler if it doesn't exist
172 // 2) Yeilds to avoid an X11 bug (?)
173 // 3) Tells GStreamer to play the video in our control
174 //---------------------------------------------------------------------------
180 # define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
182 # define DEBUG_MAIN_THREAD
185 #define DEBUG_MAIN_THREAD
188 extern void wxapp_install_idle_handler();
189 extern bool g_isIdle
;
190 extern bool g_mainThreadLocked
;
192 gint wxGStreamerMediaBackend
::OnGTKRealize(GtkWidget
* theWidget
,
193 wxGStreamerMediaBackend
* be
)
198 wxapp_install_idle_handler();
200 wxYield(); //FIXME: X Server gets an error if I don't do this or a messagebox beforehand?!?!??
202 GdkWindow
*window
= GTK_PIZZA(theWidget
)->bin_window
;
205 GstElement
* videosink
;
206 g_object_get (G_OBJECT (be
->m_player
), "video-sink", &videosink
, NULL
);
208 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(videosink
),
209 GDK_WINDOW_XWINDOW( window
)
218 //---------------------------------------------------------------------------
219 // wxGStreamerMediaBackend::Cleanup
221 // Frees the gstreamer interfaces if there were any created
222 //---------------------------------------------------------------------------
223 void wxGStreamerMediaBackend
::Cleanup()
225 if(m_player
&& GST_IS_OBJECT(m_player
))
227 gst_element_set_state (m_player
, GST_STATE_NULL
);
228 gst_object_unref (GST_OBJECT (m_player
));
232 //---------------------------------------------------------------------------
233 // wxGStreamerMediaBackend::CreateControl
235 // Initializes GStreamer and creates the wx side of our media control
236 //---------------------------------------------------------------------------
237 bool wxGStreamerMediaBackend
::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
242 const wxValidator
& validator
,
243 const wxString
& name
)
246 gst_init(NULL
, NULL
);
250 return m_ctrl
->wxControl
::Create(parent
, id
, pos
, size
,
251 style
, //remove borders???
255 //---------------------------------------------------------------------------
256 // wxGStreamerMediaBackend::TransCapsToVideoSize
258 // Gets the size of our video (in wxSize) from a GstPad
259 //---------------------------------------------------------------------------
260 bool wxGStreamerMediaBackend
::TransCapsToVideoSize(wxGStreamerMediaBackend
* be
, GstPad
* pad
)
262 const GstCaps
* caps
= GST_PAD_CAPS (pad
);
266 const GstStructure
*s
;
267 s
= gst_caps_get_structure (caps
, 0);
270 gst_structure_get_int (s
, "width", &be
->m_videoSize
.x
);
271 gst_structure_get_int (s
, "height", &be
->m_videoSize
.y
);
274 par
= gst_structure_get_value (s
, "pixel-aspect-ratio");
278 int num
= gst_value_get_fraction_numerator (par
),
279 den
= gst_value_get_fraction_denominator (par
);
281 //TODO: maybe better fraction normalization...
283 be
->m_videoSize
.x
= (int) ((float) num
* be
->m_videoSize
.x
/ den
);
285 be
->m_videoSize
.y
= (int) ((float) den
* be
->m_videoSize
.y
/ num
);
288 be
->PostRecalcSize();
295 //---------------------------------------------------------------------------
296 // wxGStreamerMediaBackend::PostRecalcSize
298 // Forces parent to recalc its layout if it has sizers to update
299 // to the new video size
300 //---------------------------------------------------------------------------
301 void wxGStreamerMediaBackend
::PostRecalcSize()
303 m_ctrl
->InvalidateBestSize();
304 m_ctrl
->GetParent()->Layout();
305 m_ctrl
->GetParent()->Refresh();
306 m_ctrl
->GetParent()->Update();
309 //---------------------------------------------------------------------------
310 // wxGStreamerMediaBackend::OnFinish
312 // Called by gstreamer when the media is done playing
314 // 1) Send a wxEVT_MEDIA_STOP to the control
315 // 2) If veteod, break out
316 // 3) really stop the media
317 // 4) Send a wxEVT_MEDIA_FINISHED to the control
318 //---------------------------------------------------------------------------
319 void wxGStreamerMediaBackend
::OnFinish(GstElement
*play
, gpointer data
)
321 wxGStreamerMediaBackend
* m_parent
= (wxGStreamerMediaBackend
*) data
;
323 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
,
324 m_parent
->m_ctrl
->GetId());
325 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
327 if(theEvent
.IsAllowed())
329 bool bOk
= m_parent
->Stop();
332 //send the event to our child
333 wxMediaEvent
theEvent(wxEVT_MEDIA_FINISHED
,
334 m_parent
->m_ctrl
->GetId());
335 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
339 //---------------------------------------------------------------------------
340 // wxGStreamerMediaBackend::OnError
342 // Called by gstreamer when an error is encountered playing the media
344 // TODO: Make this better - maybe some more intelligent wxLog stuff
345 //---------------------------------------------------------------------------
346 void wxGStreamerMediaBackend
::OnError(GstElement
*play
,
352 wxMessageBox(wxString
::Format(wxT("Error in wxMediaCtrl!\nError Message:%s"), wxString(err
->message
, wxConvLocal
).c_str()));
356 //---------------------------------------------------------------------------
357 // wxGStreamerMediaBackend::Load (File version)
359 // Just calls the URI version
360 //---------------------------------------------------------------------------
361 bool wxGStreamerMediaBackend
::Load(const wxString
& fileName
)
365 wxString( wxT("file://") ) + fileName
370 //---------------------------------------------------------------------------
371 // wxGStreamerMediaBackend::OnVideoCapsReady
373 // Called by gstreamer when the video caps for the media is ready
374 //---------------------------------------------------------------------------
375 void wxGStreamerMediaBackend
::OnVideoCapsReady(GstPad
* pad
, GParamSpec
* pspec
, gpointer data
)
377 wxGStreamerMediaBackend
::TransCapsToVideoSize((wxGStreamerMediaBackend
*) data
, pad
);
380 //---------------------------------------------------------------------------
381 // wxGStreamerMediaBackend::Load (URI version)
383 // 1) Stops/Cleanups the previous instance if there is any
384 // 2) Creates the gstreamer interfaces - playbin and xvimagesink for video
385 // 3) If there is no playbin bail out
386 // 4) Set up the error and end-of-stream callbacks for our player
387 // 5) Make sure our video sink can support the x overlay interface
388 // 6) Make sure the passed URI is valid and tell playbin to load it
389 // 7) Use the xoverlay extension to tell gstreamer to play in our window
390 // 8) Get the video size - pause required to set the stream in action
391 //---------------------------------------------------------------------------
392 bool wxGStreamerMediaBackend
::Load(const wxURI
& location
)
398 m_player
= gst_element_factory_make ("playbin", "play");
405 g_signal_connect (m_player
, "eos", G_CALLBACK (OnError
), this);
406 g_signal_connect (m_player
, "error", G_CALLBACK (OnFinish
), this);
410 //use gnome-specific gstreamer extensions
411 // GstElement* videosink = gst_gconf_get_default_video_sink();
413 GstElement
* videosink
= gst_element_factory_make ("xvimagesink", "videosink");
414 if ( !GST_IS_OBJECT(videosink
) )
415 videosink
= gst_element_factory_make ("ximagesink", "videosink");
417 wxASSERT( GST_IS_X_OVERLAY(videosink
) );
418 if ( ! GST_IS_X_OVERLAY(videosink
) )
421 g_object_set (G_OBJECT (m_player
),
422 "video-sink", videosink
,
423 // "audio-sink", m_audiosink,
426 wxString locstring
= location
.BuildUnescapedURI();
427 wxASSERT(gst_uri_protocol_is_valid("file"));
428 wxASSERT(gst_uri_is_valid(locstring
.mb_str()));
430 g_object_set (G_OBJECT (m_player
), "uri", (const char*)locstring
.mb_str(), NULL
);
434 if(!GTK_WIDGET_REALIZED(m_ctrl
->m_wxwindow
))
436 //Not realized yet - set to connect at realization time
437 gtk_signal_connect( GTK_OBJECT(m_ctrl
->m_wxwindow
),
439 GTK_SIGNAL_FUNC(wxGStreamerMediaBackend
::OnGTKRealize
),
444 wxYield(); //see realize callback...
445 GdkWindow
*window
= GTK_PIZZA(m_ctrl
->m_wxwindow
)->bin_window
;
450 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(videosink
),
452 GDK_WINDOW_XWINDOW( window
)
463 wxASSERT(gst_element_set_state (m_player
,
464 GST_STATE_PAUSED
) == GST_STATE_SUCCESS
);
466 const GList
*list
= NULL
;
467 g_object_get (G_OBJECT (m_player
), "stream-info", &list
, NULL
);
469 for ( ; list
!= NULL
; list
= list
->next
)
471 GObject
*info
= (GObject
*) list
->data
;
477 g_object_get (info
, "type", &type
, NULL
);
478 pspec
= g_object_class_find_property (
479 G_OBJECT_GET_CLASS (info
), "type");
480 val
= g_enum_get_value (G_PARAM_SPEC_ENUM (pspec
)->enum_class
, type
);
482 if (strstr (val
->value_name
, "VIDEO"))
484 //Newer gstreamer 0.8+ is SUPPOSED to have "object"...
485 //but a lot of old plugins still use "pad" :)
486 pspec
= g_object_class_find_property (
487 G_OBJECT_GET_CLASS (info
), "object");
490 g_object_get (info
, "pad", &pad
, NULL
);
492 g_object_get (info
, "object", &pad
, NULL
);
494 pad
= (GstPad
*) GST_PAD_REALIZE (pad
);
497 if(!wxGStreamerMediaBackend
::TransCapsToVideoSize(this, pad
));
499 //wait for those caps to get ready
503 G_CALLBACK(wxGStreamerMediaBackend
::OnVideoCapsReady
),
509 m_videoSize
= wxSize(0,0);
512 }//end searching through info list
518 //---------------------------------------------------------------------------
519 // wxGStreamerMediaBackend::Play
521 // Sets the stream to a playing state
522 //---------------------------------------------------------------------------
523 bool wxGStreamerMediaBackend
::Play()
525 if (gst_element_set_state (m_player
, GST_STATE_PLAYING
)
526 != GST_STATE_SUCCESS
)
531 //---------------------------------------------------------------------------
532 // wxGStreamerMediaBackend::Pause
534 // Marks where we paused and pauses the stream
535 //---------------------------------------------------------------------------
536 bool wxGStreamerMediaBackend
::Pause()
538 m_nPausedPos
= GetPosition();
539 if (gst_element_set_state (m_player
, GST_STATE_PAUSED
)
540 != GST_STATE_SUCCESS
)
545 //---------------------------------------------------------------------------
546 // wxGStreamerMediaBackend::Stop
548 // Pauses the stream and sets the position to 0
549 //---------------------------------------------------------------------------
550 bool wxGStreamerMediaBackend
::Stop()
552 if (gst_element_set_state (m_player
,
553 GST_STATE_PAUSED
) != GST_STATE_SUCCESS
)
555 return wxGStreamerMediaBackend
::SetPosition(0);
558 //---------------------------------------------------------------------------
559 // wxGStreamerMediaBackend::GetState
561 // Gets the state of the stream
562 //---------------------------------------------------------------------------
563 wxMediaState wxGStreamerMediaBackend
::GetState()
565 switch(GST_STATE(m_player
))
567 case GST_STATE_PLAYING
:
568 return wxMEDIASTATE_PLAYING
;
569 case GST_STATE_PAUSED
:
570 if (m_nPausedPos
== 0)
571 return wxMEDIASTATE_STOPPED
;
573 return wxMEDIASTATE_PAUSED
;
574 default://case GST_STATE_READY:
575 return wxMEDIASTATE_STOPPED
;
579 //---------------------------------------------------------------------------
580 // wxGStreamerMediaBackend::GetPosition
582 // If paused, returns our marked position - otherwise it queries the
583 // GStreamer playbin for the position and returns that
586 //TODO: In lue of the last big TODO, when you pause and seek gstreamer
587 //TODO: doesn't update the position sometimes, so we need to keep track of whether
588 //TODO: we have paused or not and keep track of the time after the pause
589 //TODO: and whenever the user seeks while paused
591 //---------------------------------------------------------------------------
592 wxLongLong wxGStreamerMediaBackend
::GetPosition()
594 if(GetState() != wxMEDIASTATE_PLAYING
)
599 GstFormat fmtTime
= GST_FORMAT_TIME
;
601 if (!gst_element_query (m_player
, GST_QUERY_POSITION
, &fmtTime
, &pos
))
603 return pos
/ GST_MSECOND
;
607 //---------------------------------------------------------------------------
608 // wxGStreamerMediaBackend::SetPosition
610 // Sets the position of the stream
611 // Note that GST_MSECOND is 1000000 (GStreamer uses nanoseconds - so
612 // there is 1000000 nanoseconds in a millisecond)
614 // If paused marks where we seeked to
615 //---------------------------------------------------------------------------
616 bool wxGStreamerMediaBackend
::SetPosition(wxLongLong where
)
618 if( gst_element_seek (m_player
, (GstSeekType
) (GST_SEEK_METHOD_SET
|
619 GST_FORMAT_TIME
| GST_SEEK_FLAG_FLUSH
),
620 where
.GetValue() * GST_MSECOND
) )
622 if (GetState() != wxMEDIASTATE_PLAYING
)
623 m_nPausedPos
= where
;
631 //---------------------------------------------------------------------------
632 // wxGStreamerMediaBackend::GetDuration
634 // Obtains the total time of our stream
635 //---------------------------------------------------------------------------
636 wxLongLong wxGStreamerMediaBackend
::GetDuration()
639 GstFormat fmtTime
= GST_FORMAT_TIME
;
641 if(!gst_element_query(m_player
, GST_QUERY_TOTAL
, &fmtTime
, &length
))
643 return length
/ GST_MSECOND
;
646 //---------------------------------------------------------------------------
647 // wxGStreamerMediaBackend::Move
649 // Called when the window is moved - GStreamer takes care of this
650 // for us so nothing is needed
651 //---------------------------------------------------------------------------
652 void wxGStreamerMediaBackend
::Move(int x
, int y
, int w
, int h
)
656 //---------------------------------------------------------------------------
657 // wxGStreamerMediaBackend::GetVideoSize
659 // Returns our cached video size from Load/OnVideoCapsReady
660 //---------------------------------------------------------------------------
661 wxSize wxGStreamerMediaBackend
::GetVideoSize() const
666 //---------------------------------------------------------------------------
667 // wxGStreamerMediaBackend::GetPlaybackRate
668 // wxGStreamerMediaBackend::SetPlaybackRate
670 // Obtains/Sets the playback rate of the stream
672 //TODO: PlaybackRate not currently supported via playbin directly -
673 //TODO: Ronald S. Bultje noted on gstreamer-devel:
675 //TODO: Like "play at twice normal speed"? Or "play at 25 fps and 44,1 kHz"? As
676 //TODO: for the first, yes, we have elements for that, btu they"re not part of
677 //TODO: playbin. You can create a bin (with a ghost pad) containing the actual
678 //TODO: video/audiosink and the speed-changing element for this, and set that
679 //TODO: element as video-sink or audio-sink property in playbin. The
680 //TODO: audio-element is called "speed", the video-element is called "videodrop"
681 //TODO: (although that appears to be deprecated in favour of "videorate", which
682 //TODO: again cannot do this, so this may not work at all in the end). For
683 //TODO: forcing frame/samplerates, see audioscale and videorate. Audioscale is
684 //TODO: part of playbin.
685 //---------------------------------------------------------------------------
686 double wxGStreamerMediaBackend
::GetPlaybackRate()
688 //not currently supported via playbin
692 bool wxGStreamerMediaBackend
::SetPlaybackRate(double dRate
)
694 //not currently supported via playbin
698 #endif //wxUSE_GSTREAMER
700 //in source file that contains stuff you don't directly use
701 #include <wx/html/forcelnk.h>
702 FORCE_LINK_ME(basewxmediabackends
);
704 #endif //wxUSE_MEDIACTRL