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
49 // Uses nanoseconds...
50 //---------------------------------------------------------------------------
53 //---------------------------------------------------------------------------
55 //---------------------------------------------------------------------------
57 #include <gst/xoverlay/xoverlay.h>
59 #include <string.h> //strstr
62 #include "wx/msgdlg.h"
65 //for <gdk/gdkx.h>/related for GDK_WINDOW_XWINDOW
66 # include "wx/gtk/win_gtk.h"
67 # include <gtk/gtksignal.h>
71 //FIXME: This is really not the best way to play-stop -
72 //FIXME: it should just have one playbin and stick with it the whole
73 //FIXME: instance of wxGStreamerMediaBackend - but stopping appears
74 //FIXME: to invalidate the playbin object...
77 class WXDLLIMPEXP_MEDIA wxGStreamerMediaBackend
: public wxMediaBackend
81 wxGStreamerMediaBackend();
82 ~wxGStreamerMediaBackend();
84 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
89 const wxValidator
& validator
,
90 const wxString
& name
);
96 virtual bool Load(const wxString
& fileName
);
97 virtual bool Load(const wxURI
& location
);
99 virtual wxMediaState
GetState();
101 virtual bool SetPosition(wxLongLong where
);
102 virtual wxLongLong
GetPosition();
103 virtual wxLongLong
GetDuration();
105 virtual void Move(int x
, int y
, int w
, int h
);
106 wxSize
GetVideoSize() const;
108 virtual double GetPlaybackRate();
109 virtual bool SetPlaybackRate(double dRate
);
113 static void OnFinish(GstElement
*play
, gpointer data
);
114 static void OnError (GstElement
*play
, GstElement
*src
,
115 GError
*err
, gchar
*debug
,
117 static void OnVideoCapsReady(GstPad
* pad
, GParamSpec
* pspec
, gpointer data
);
119 static bool TransCapsToVideoSize(wxGStreamerMediaBackend
* be
, GstPad
* caps
);
120 void PostRecalcSize();
123 static gint
OnGTKRealize(GtkWidget
* theWidget
, wxGStreamerMediaBackend
* be
);
126 GstElement
* m_player
; //GStreamer media element
127 GstElement
* m_audiosink
;
128 GstElement
* m_videosink
;
134 //FIXME: In lue of the last big FIXME, when you pause and seek gstreamer
135 //FIXME: doesn't update the position sometimes, so we need to keep track of whether
136 //FIXME: we have paused or not and keep track of the time after the pause
137 //FIXME: and whenever the user seeks while paused
139 wxLongLong m_nPausedPos
;
141 DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend
);
145 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
147 // wxGStreamerMediaBackend
149 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
151 IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend
, wxMediaBackend
);
153 wxGStreamerMediaBackend::wxGStreamerMediaBackend() : m_player(NULL
), m_videoSize(0,0)
157 wxGStreamerMediaBackend::~wxGStreamerMediaBackend()
167 # define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
169 # define DEBUG_MAIN_THREAD
172 #define DEBUG_MAIN_THREAD
175 extern void wxapp_install_idle_handler();
176 extern bool g_isIdle
;
177 extern bool g_mainThreadLocked
;
179 gint
wxGStreamerMediaBackend::OnGTKRealize(GtkWidget
* theWidget
,
180 wxGStreamerMediaBackend
* be
)
185 wxapp_install_idle_handler();
187 wxYield(); //X Server gets an error if I don't do this or a messagebox beforehand?!?!??
189 GdkWindow
*window
= GTK_PIZZA(theWidget
)->bin_window
;
192 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(be
->m_videosink
),
193 GDK_WINDOW_XWINDOW( window
)
202 void wxGStreamerMediaBackend::Cleanup()
204 if(m_player
&& GST_IS_OBJECT(m_player
))
206 // wxASSERT(GST_IS_OBJECT(m_audiosink));
207 // wxASSERT(GST_IS_OBJECT(m_videosink));
209 gst_element_set_state (m_player
, GST_STATE_NULL
);
210 gst_object_unref (GST_OBJECT (m_player
));
211 //gst_object_unref (GST_OBJECT (m_videosink));
212 //gst_object_unref (GST_OBJECT (m_audiosink));
216 bool wxGStreamerMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
221 const wxValidator
& validator
,
222 const wxString
& name
)
225 gst_init(NULL
, NULL
);
229 return m_ctrl
->wxControl::Create(parent
, id
, pos
, size
,
230 style
, //remove borders???
234 bool wxGStreamerMediaBackend::TransCapsToVideoSize(wxGStreamerMediaBackend
* be
, GstPad
* pad
)
236 const GstCaps
* caps
= GST_PAD_CAPS (pad
);
240 const GstStructure
*s
;
241 s
= gst_caps_get_structure (caps
, 0);
244 gst_structure_get_int (s
, "width", &be
->m_videoSize
.x
);
245 gst_structure_get_int (s
, "height", &be
->m_videoSize
.y
);
248 par
= gst_structure_get_value (s
, "pixel-aspect-ratio");
252 int num
= gst_value_get_fraction_numerator (par
),
253 den
= gst_value_get_fraction_denominator (par
);
255 //TODO: maybe better fraction normalization...
257 be
->m_videoSize
.x
= (int) ((float) num
* be
->m_videoSize
.x
/ den
);
259 be
->m_videoSize
.y
= (int) ((float) den
* be
->m_videoSize
.y
/ num
);
262 be
->PostRecalcSize();
269 //forces parent to recalc its layout if it has sizers to update
270 //to the new video size
271 void wxGStreamerMediaBackend::PostRecalcSize()
273 m_ctrl
->InvalidateBestSize();
274 m_ctrl
->GetParent()->Layout();
275 m_ctrl
->GetParent()->Refresh();
276 m_ctrl
->GetParent()->Update();
279 void wxGStreamerMediaBackend::OnFinish(GstElement
*play
, gpointer data
)
281 wxGStreamerMediaBackend
* m_parent
= (wxGStreamerMediaBackend
*) data
;
283 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
,
284 m_parent
->m_ctrl
->GetId());
285 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
287 if(theEvent
.IsAllowed())
289 bool bOk
= m_parent
->Stop();
292 //send the event to our child
293 wxMediaEvent
theEvent(wxEVT_MEDIA_FINISHED
,
294 m_parent
->m_ctrl
->GetId());
295 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
299 void wxGStreamerMediaBackend::OnError(GstElement
*play
,
305 wxMessageBox(wxString::Format(wxT("Error in wxMediaCtrl!\nError Message:%s"), wxString(err
->message
, wxConvLocal
).c_str()));
309 bool wxGStreamerMediaBackend::Load(const wxString
& fileName
)
313 wxString( wxT("file://") ) + fileName
318 void wxGStreamerMediaBackend::OnVideoCapsReady(GstPad
* pad
, GParamSpec
* pspec
, gpointer data
)
320 wxGStreamerMediaBackend::TransCapsToVideoSize((wxGStreamerMediaBackend
*) data
, pad
);
323 bool wxGStreamerMediaBackend::Load(const wxURI
& location
)
327 m_player
= gst_element_factory_make ("playbin", "play");
328 m_audiosink
= gst_element_factory_make ("alsasink", "audiosink");
329 m_videosink
= gst_element_factory_make ("xvimagesink", "videosink");
331 //no playbin -- outta here :)
336 if (GST_IS_OBJECT(m_audiosink
) == false)
339 m_audiosink
= gst_element_factory_make ("osssink", "audiosink");
340 wxASSERT_MSG(GST_IS_OBJECT(m_audiosink
), wxT("WARNING: Alsa and OSS drivers for gstreamer not found - audio will be unavailable for wxMediaCtrl"));
344 wxASSERT_MSG(GST_IS_OBJECT(m_videosink
), wxT("WARNING: No X video driver for gstreamer not found - video will be unavailable for wxMediaCtrl"));
346 g_object_set (G_OBJECT (m_player
),
347 "video-sink", m_videosink
,
348 "audio-sink", m_audiosink
,
351 g_signal_connect (m_player
, "eos", G_CALLBACK (OnError
), this);
352 g_signal_connect (m_player
, "error", G_CALLBACK (OnFinish
), this);
354 wxASSERT( GST_IS_X_OVERLAY(m_videosink
) );
355 if ( ! GST_IS_X_OVERLAY(m_videosink
) )
358 wxString locstring
= location
.BuildUnescapedURI();
359 wxASSERT(gst_uri_protocol_is_valid("file"));
360 wxASSERT(gst_uri_is_valid(locstring
.mb_str()));
362 g_object_set (G_OBJECT (m_player
), "uri", (const char*)locstring
.mb_str(), NULL
);
365 if(!GTK_WIDGET_REALIZED(m_ctrl
->m_wxwindow
))
367 //Not realized yet - set to connect at realization time
368 gtk_signal_connect( GTK_OBJECT(m_ctrl
->m_wxwindow
),
370 GTK_SIGNAL_FUNC(wxGStreamerMediaBackend::OnGTKRealize
),
375 wxYield(); //see realize callback...
376 GdkWindow
*window
= GTK_PIZZA(m_ctrl
->m_wxwindow
)->bin_window
;
381 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(m_videosink
),
383 GDK_WINDOW_XWINDOW( window
)
393 wxASSERT(gst_element_set_state (m_player
,
394 GST_STATE_PAUSED
) == GST_STATE_SUCCESS
);
396 const GList
*list
= NULL
;
397 g_object_get (G_OBJECT (m_player
), "stream-info", &list
, NULL
);
399 for ( ; list
!= NULL
; list
= list
->next
)
401 GObject
*info
= (GObject
*) list
->data
;
407 g_object_get (info
, "type", &type
, NULL
);
408 pspec
= g_object_class_find_property (
409 G_OBJECT_GET_CLASS (info
), "type");
410 val
= g_enum_get_value (G_PARAM_SPEC_ENUM (pspec
)->enum_class
, type
);
412 if (strstr (val
->value_name
, "VIDEO"))
414 //Newer gstreamer 0.8+ is SUPPOSED to have "object"...
415 //but a lot of old plugins still use "pad" :)
416 pspec
= g_object_class_find_property (
417 G_OBJECT_GET_CLASS (info
), "object");
420 g_object_get (info
, "pad", &pad
, NULL
);
422 g_object_get (info
, "object", &pad
, NULL
);
424 pad
= (GstPad
*) GST_PAD_REALIZE (pad
);
427 if(!wxGStreamerMediaBackend::TransCapsToVideoSize(this, pad
));
429 //wait for those caps to get ready
433 G_CALLBACK(wxGStreamerMediaBackend::OnVideoCapsReady
),
439 m_videoSize
= wxSize(0,0);
442 }//end searching through info list
448 bool wxGStreamerMediaBackend::Play()
450 if (gst_element_set_state (m_player
, GST_STATE_PLAYING
)
451 != GST_STATE_SUCCESS
)
456 bool wxGStreamerMediaBackend::Pause()
458 m_nPausedPos
= GetPosition();
459 if (gst_element_set_state (m_player
, GST_STATE_PAUSED
)
460 != GST_STATE_SUCCESS
)
465 bool wxGStreamerMediaBackend::Stop()
467 if (gst_element_set_state (m_player
,
468 GST_STATE_PAUSED
) != GST_STATE_SUCCESS
)
470 return wxGStreamerMediaBackend::SetPosition(0);
473 wxMediaState
wxGStreamerMediaBackend::GetState()
475 switch(GST_STATE(m_player
))
477 case GST_STATE_PLAYING
:
478 return wxMEDIASTATE_PLAYING
;
479 case GST_STATE_PAUSED
:
480 if (m_nPausedPos
== 0)
481 return wxMEDIASTATE_STOPPED
;
483 return wxMEDIASTATE_PAUSED
;
484 default://case GST_STATE_READY:
485 return wxMEDIASTATE_STOPPED
;
489 bool wxGStreamerMediaBackend::SetPosition(wxLongLong where
)
491 if( gst_element_seek (m_player
, (GstSeekType
) (GST_SEEK_METHOD_SET
|
492 GST_FORMAT_TIME
| GST_SEEK_FLAG_FLUSH
),
493 where
.GetValue() * GST_MSECOND
) )
495 if (GetState() != wxMEDIASTATE_PLAYING
)
496 m_nPausedPos
= where
;
504 wxLongLong
wxGStreamerMediaBackend::GetPosition()
506 if(GetState() != wxMEDIASTATE_PLAYING
)
511 GstFormat fmtTime
= GST_FORMAT_TIME
;
513 if (!gst_element_query (m_player
, GST_QUERY_POSITION
, &fmtTime
, &pos
))
515 return pos
/ GST_MSECOND
;
519 wxLongLong
wxGStreamerMediaBackend::GetDuration()
522 GstFormat fmtTime
= GST_FORMAT_TIME
;
524 if(!gst_element_query(m_player
, GST_QUERY_TOTAL
, &fmtTime
, &length
))
526 return length
/ GST_MSECOND
;
529 void wxGStreamerMediaBackend::Move(int x
, int y
, int w
, int h
)
533 wxSize
wxGStreamerMediaBackend::GetVideoSize() const
539 //PlaybackRate not currently supported via playbin directly -
540 // Ronald S. Bultje noted on gstreamer-devel:
542 // Like "play at twice normal speed"? Or "play at 25 fps and 44,1 kHz"? As
543 // for the first, yes, we have elements for that, btu they"re not part of
544 // playbin. You can create a bin (with a ghost pad) containing the actual
545 // video/audiosink and the speed-changing element for this, and set that
546 // element as video-sink or audio-sink property in playbin. The
547 // audio-element is called "speed", the video-element is called "videodrop"
548 // (although that appears to be deprecated in favour of "videorate", which
549 // again cannot do this, so this may not work at all in the end). For
550 // forcing frame/samplerates, see audioscale and videorate. Audioscale is
554 double wxGStreamerMediaBackend::GetPlaybackRate()
556 //not currently supported via playbin
560 bool wxGStreamerMediaBackend::SetPlaybackRate(double dRate
)
562 //not currently supported via playbin
566 #endif //wxUSE_GSTREAMER
568 //in source file that contains stuff you don't directly use
569 #include <wx/html/forcelnk.h>
570 FORCE_LINK_ME(basewxmediabackends
);
572 #endif //wxUSE_MEDIACTRL