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 // This won't compile/work without a little work yet...
50 // Uses nanoseconds...
51 //---------------------------------------------------------------------------
54 //---------------------------------------------------------------------------
56 //---------------------------------------------------------------------------
58 #include <gst/xoverlay/xoverlay.h>
60 #include <string.h> //strstr
63 #include "wx/msgdlg.h"
66 //for <gdk/gdkx.h>/related for GDK_WINDOW_XWINDOW
67 # include "wx/gtk/win_gtk.h"
68 # include <gtk/gtksignal.h>
72 //FIXME: This is really not the best way to play-stop -
73 //FIXME: it should just have one playbin and stick with it the whole
74 //FIXME: instance of wxGStreamerMediaBackend - but stopping appears
75 //FIXME: to invalidate the playbin object...
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
128 GstElement
* m_audiosink
;
129 GstElement
* m_videosink
;
135 //FIXME: In lue of the last big FIXME, when you pause and seek gstreamer
136 //FIXME: doesn't update the position sometimes, so we need to keep track of whether
137 //FIXME: we have paused or not and keep track of the time after the pause
138 //FIXME: and whenever the user seeks while paused
140 wxLongLong m_nPausedPos
;
142 DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend
);
146 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
148 // wxGStreamerMediaBackend
150 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
152 IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend
, wxMediaBackend
);
154 wxGStreamerMediaBackend::wxGStreamerMediaBackend() : m_player(NULL
), m_videoSize(0,0)
158 wxGStreamerMediaBackend::~wxGStreamerMediaBackend()
168 # define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
170 # define DEBUG_MAIN_THREAD
173 #define DEBUG_MAIN_THREAD
176 extern void wxapp_install_idle_handler();
177 extern bool g_isIdle
;
178 extern bool g_mainThreadLocked
;
180 gint
wxGStreamerMediaBackend::OnGTKRealize(GtkWidget
* theWidget
,
181 wxGStreamerMediaBackend
* be
)
186 wxapp_install_idle_handler();
188 wxYield(); //X Server gets an error if I don't do this or a messagebox beforehand?!?!??
190 GdkWindow
*window
= GTK_PIZZA(theWidget
)->bin_window
;
193 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(be
->m_videosink
),
194 GDK_WINDOW_XWINDOW( window
)
203 void wxGStreamerMediaBackend::Cleanup()
205 if(m_player
&& GST_IS_OBJECT(m_player
))
207 // wxASSERT(GST_IS_OBJECT(m_audiosink));
208 // wxASSERT(GST_IS_OBJECT(m_videosink));
210 gst_element_set_state (m_player
, GST_STATE_NULL
);
211 gst_object_unref (GST_OBJECT (m_player
));
212 //gst_object_unref (GST_OBJECT (m_videosink));
213 //gst_object_unref (GST_OBJECT (m_audiosink));
217 bool wxGStreamerMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
222 const wxValidator
& validator
,
223 const wxString
& name
)
226 gst_init(NULL
, NULL
);
230 return m_ctrl
->wxControl::Create(parent
, id
, pos
, size
,
231 style
, //remove borders???
235 bool wxGStreamerMediaBackend::TransCapsToVideoSize(wxGStreamerMediaBackend
* be
, GstPad
* pad
)
237 const GstCaps
* caps
= GST_PAD_CAPS (pad
);
241 const GstStructure
*s
;
242 s
= gst_caps_get_structure (caps
, 0);
245 gst_structure_get_int (s
, "width", &be
->m_videoSize
.x
);
246 gst_structure_get_int (s
, "height", &be
->m_videoSize
.y
);
249 par
= gst_structure_get_value (s
, "pixel-aspect-ratio");
253 int num
= gst_value_get_fraction_numerator (par
),
254 den
= gst_value_get_fraction_denominator (par
);
256 //TODO: maybe better fraction normalization...
258 be
->m_videoSize
.x
= (int) ((float) num
* be
->m_videoSize
.x
/ den
);
260 be
->m_videoSize
.y
= (int) ((float) den
* be
->m_videoSize
.y
/ num
);
263 be
->PostRecalcSize();
270 //forces parent to recalc its layout if it has sizers to update
271 //to the new video size
272 void wxGStreamerMediaBackend::PostRecalcSize()
274 m_ctrl
->InvalidateBestSize();
275 m_ctrl
->GetParent()->Layout();
276 m_ctrl
->GetParent()->Refresh();
277 m_ctrl
->GetParent()->Update();
280 void wxGStreamerMediaBackend::OnFinish(GstElement
*play
, gpointer data
)
282 wxGStreamerMediaBackend
* m_parent
= (wxGStreamerMediaBackend
*) data
;
284 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
,
285 m_parent
->m_ctrl
->GetId());
286 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
288 if(theEvent
.IsAllowed())
290 bool bOk
= m_parent
->Stop();
293 //send the event to our child
294 wxMediaEvent
theEvent(wxEVT_MEDIA_FINISHED
,
295 m_parent
->m_ctrl
->GetId());
296 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
300 void wxGStreamerMediaBackend::OnError(GstElement
*play
,
306 wxMessageBox(wxString::Format(wxT("Error in wxMediaCtrl!\nError Message:%s"), wxString(err
->message
, wxConvLocal
).c_str()));
310 bool wxGStreamerMediaBackend::Load(const wxString
& fileName
)
314 wxString( wxT("file://") ) + fileName
319 void wxGStreamerMediaBackend::OnVideoCapsReady(GstPad
* pad
, GParamSpec
* pspec
, gpointer data
)
321 wxGStreamerMediaBackend::TransCapsToVideoSize((wxGStreamerMediaBackend
*) data
, pad
);
324 bool wxGStreamerMediaBackend::Load(const wxURI
& location
)
328 m_player
= gst_element_factory_make ("playbin", "play");
329 m_audiosink
= gst_element_factory_make ("alsasink", "audiosink");
330 m_videosink
= gst_element_factory_make ("xvimagesink", "videosink");
332 //no playbin -- outta here :)
337 if (GST_IS_OBJECT(m_audiosink
) == false)
340 m_audiosink
= gst_element_factory_make ("osssink", "audiosink");
341 wxASSERT_MSG(GST_IS_OBJECT(m_audiosink
), wxT("WARNING: Alsa and OSS drivers for gstreamer not found - audio will be unavailable for wxMediaCtrl"));
345 wxASSERT_MSG(GST_IS_OBJECT(m_videosink
), wxT("WARNING: No X video driver for gstreamer not found - video will be unavailable for wxMediaCtrl"));
347 g_object_set (G_OBJECT (m_player
),
348 "video-sink", m_videosink
,
349 "audio-sink", m_audiosink
,
352 g_signal_connect (m_player
, "eos", G_CALLBACK (OnError
), this);
353 g_signal_connect (m_player
, "error", G_CALLBACK (OnFinish
), this);
355 wxASSERT( GST_IS_X_OVERLAY(m_videosink
) );
356 if ( ! GST_IS_X_OVERLAY(m_videosink
) )
359 wxString locstring
= location
.BuildUnescapedURI();
360 wxASSERT(gst_uri_protocol_is_valid("file"));
361 wxASSERT(gst_uri_is_valid(locstring
.mb_str()));
363 g_object_set (G_OBJECT (m_player
), "uri", (const char*)locstring
.mb_str(), NULL
);
366 if(!GTK_WIDGET_REALIZED(m_ctrl
->m_wxwindow
))
368 //Not realized yet - set to connect at realization time
369 gtk_signal_connect( GTK_OBJECT(m_ctrl
->m_wxwindow
),
371 GTK_SIGNAL_FUNC(wxGStreamerMediaBackend::OnGTKRealize
),
376 wxYield(); //see realize callback...
377 GdkWindow
*window
= GTK_PIZZA(m_ctrl
->m_wxwindow
)->bin_window
;
382 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(m_videosink
),
384 GDK_WINDOW_XWINDOW( window
)
394 wxASSERT(gst_element_set_state (m_player
,
395 GST_STATE_PAUSED
) == GST_STATE_SUCCESS
);
397 const GList
*list
= NULL
;
398 g_object_get (G_OBJECT (m_player
), "stream-info", &list
, NULL
);
400 for ( ; list
!= NULL
; list
= list
->next
)
402 GObject
*info
= (GObject
*) list
->data
;
408 g_object_get (info
, "type", &type
, NULL
);
409 pspec
= g_object_class_find_property (
410 G_OBJECT_GET_CLASS (info
), "type");
411 val
= g_enum_get_value (G_PARAM_SPEC_ENUM (pspec
)->enum_class
, type
);
413 if (strstr (val
->value_name
, "VIDEO"))
415 //Newer gstreamer 0.8+ is SUPPOSED to have "object"...
416 //but a lot of old plugins still use "pad" :)
417 pspec
= g_object_class_find_property (
418 G_OBJECT_GET_CLASS (info
), "object");
421 g_object_get (info
, "pad", &pad
, NULL
);
423 g_object_get (info
, "object", &pad
, NULL
);
425 pad
= (GstPad
*) GST_PAD_REALIZE (pad
);
428 if(!wxGStreamerMediaBackend::TransCapsToVideoSize(this, pad
));
430 //wait for those caps to get ready
434 G_CALLBACK(wxGStreamerMediaBackend::OnVideoCapsReady
),
440 m_videoSize
= wxSize(0,0);
443 }//end searching through info list
449 bool wxGStreamerMediaBackend::Play()
451 if (gst_element_set_state (m_player
, GST_STATE_PLAYING
)
452 != GST_STATE_SUCCESS
)
457 bool wxGStreamerMediaBackend::Pause()
459 m_nPausedPos
= GetPosition();
460 if (gst_element_set_state (m_player
, GST_STATE_PAUSED
)
461 != GST_STATE_SUCCESS
)
466 bool wxGStreamerMediaBackend::Stop()
468 //FIXME: GStreamer won't update the position for getposition for some reason
469 //until it is played again...
470 if (gst_element_set_state (m_player
,
471 GST_STATE_PAUSED
) != GST_STATE_SUCCESS
)
473 return wxGStreamerMediaBackend::SetPosition(0);
476 wxMediaState
wxGStreamerMediaBackend::GetState()
478 switch(GST_STATE(m_player
))
480 case GST_STATE_PLAYING
:
481 return wxMEDIASTATE_PLAYING
;
482 case GST_STATE_PAUSED
:
483 if (m_nPausedPos
== 0)
484 return wxMEDIASTATE_STOPPED
;
486 return wxMEDIASTATE_PAUSED
;
487 default://case GST_STATE_READY:
488 return wxMEDIASTATE_STOPPED
;
492 bool wxGStreamerMediaBackend::SetPosition(wxLongLong where
)
494 if( gst_element_seek (m_player
, (GstSeekType
) (GST_SEEK_METHOD_SET
|
495 GST_FORMAT_TIME
| GST_SEEK_FLAG_FLUSH
),
496 where
.GetValue() * GST_MSECOND
) )
498 if (GetState() != wxMEDIASTATE_PLAYING
)
499 m_nPausedPos
= where
;
507 wxLongLong
wxGStreamerMediaBackend::GetPosition()
509 if(GetState() != wxMEDIASTATE_PLAYING
)
514 GstFormat fmtTime
= GST_FORMAT_TIME
;
516 if (!gst_element_query (m_player
, GST_QUERY_POSITION
, &fmtTime
, &pos
))
518 return pos
/ GST_MSECOND
;
522 wxLongLong
wxGStreamerMediaBackend::GetDuration()
525 GstFormat fmtTime
= GST_FORMAT_TIME
;
527 if(!gst_element_query(m_player
, GST_QUERY_TOTAL
, &fmtTime
, &length
))
529 return length
/ GST_MSECOND
;
532 void wxGStreamerMediaBackend::Move(int x
, int y
, int w
, int h
)
536 wxSize
wxGStreamerMediaBackend::GetVideoSize() const
542 //PlaybackRate not currently supported via playbin directly -
543 // Ronald S. Bultje noted on gstreamer-devel:
545 // Like "play at twice normal speed"? Or "play at 25 fps and 44,1 kHz"? As
546 // for the first, yes, we have elements for that, btu they"re not part of
547 // playbin. You can create a bin (with a ghost pad) containing the actual
548 // video/audiosink and the speed-changing element for this, and set that
549 // element as video-sink or audio-sink property in playbin. The
550 // audio-element is called "speed", the video-element is called "videodrop"
551 // (although that appears to be deprecated in favour of "videorate", which
552 // again cannot do this, so this may not work at all in the end). For
553 // forcing frame/samplerates, see audioscale and videorate. Audioscale is
557 double wxGStreamerMediaBackend::GetPlaybackRate()
559 //not currently supported via playbin
563 bool wxGStreamerMediaBackend::SetPlaybackRate(double dRate
)
565 //not currently supported via playbin
569 #endif //wxUSE_GSTREAMER
571 //in source file that contains stuff you don't directly use
572 #include <wx/html/forcelnk.h>
573 FORCE_LINK_ME(basewxmediabackends
);
575 #endif //wxUSE_MEDIACTRL