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
65 //for <gdk/gdkx.h>/related for GDK_WINDOW_XWINDOW
66 # include "wx/gtk/win_gtk.h"
69 class WXDLLIMPEXP_MEDIA wxGStreamerMediaBackend
: public wxMediaBackend
73 wxGStreamerMediaBackend();
74 ~wxGStreamerMediaBackend();
76 virtual bool CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
81 const wxValidator
& validator
,
82 const wxString
& name
);
88 virtual bool Load(const wxString
& fileName
);
89 virtual bool Load(const wxURI
& location
);
91 virtual wxMediaState
GetState();
93 virtual bool SetPosition(wxLongLong where
);
94 virtual wxLongLong
GetPosition();
95 virtual wxLongLong
GetDuration();
97 virtual void Move(int x
, int y
, int w
, int h
);
98 wxSize
GetVideoSize() const;
100 virtual double GetPlaybackRate();
101 virtual bool SetPlaybackRate(double dRate
);
105 static void OnFinish(GstElement
*play
, gpointer data
);
106 static void OnError (GstElement
*play
, GstElement
*src
,
107 GError
*err
, gchar
*debug
,
110 GstElement
* m_player
; //GStreamer media element
111 GstElement
* m_audiosink
;
112 GstElement
* m_videosink
;
114 DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend
);
118 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
120 // wxGStreamerMediaBackend
122 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124 IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend
, wxMediaBackend
);
126 wxGStreamerMediaBackend::wxGStreamerMediaBackend()
130 wxGStreamerMediaBackend::~wxGStreamerMediaBackend()
132 gst_element_set_state (m_player
, GST_STATE_NULL
);
133 gst_object_unref (GST_OBJECT (m_player
));
134 gst_object_unref (GST_OBJECT (m_videosink
));
135 gst_object_unref (GST_OBJECT (m_audiosink
));
138 bool wxGStreamerMediaBackend::CreateControl(wxControl
* ctrl
, wxWindow
* parent
,
143 const wxValidator
& validator
,
144 const wxString
& name
)
147 gst_init(NULL
, NULL
);
151 // By default wxWindow(s) is created with a border -
152 // so we need to get rid of those return Load(
154 // Since we don't have a child window like most other
155 // backends, we don't need wxCLIP_CHILDREN
158 ctrl
->wxControl::Create(parent
, id
, pos
, size
,
159 style
, //remove borders???
164 m_player
= gst_element_factory_make ("playbin", "play");
165 m_audiosink
= gst_element_factory_make ("alsasink", "audiosink");
166 m_videosink
= gst_element_factory_make ("xvimagesink", "videosink");
168 g_object_set (G_OBJECT (m_player
),
169 "video-sink", m_videosink
,
170 "audio-sink", m_audiosink
,
173 g_signal_connect (m_player
, "eos", G_CALLBACK (OnError
), this);
174 g_signal_connect (m_player
, "error", G_CALLBACK (OnFinish
), this);
176 if ( ! GST_IS_X_OVERLAY(m_videosink
) )
179 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(m_videosink
),
181 GDK_WINDOW_XWINDOW(ctrl
->GetHandle())
189 void wxGStreamerMediaBackend::OnFinish(GstElement
*play
, gpointer data
)
191 wxGStreamerMediaBackend
* m_parent
= (wxGStreamerMediaBackend
*) data
;
193 wxMediaEvent
theEvent(wxEVT_MEDIA_STOP
,
194 m_parent
->m_ctrl
->GetId());
195 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
197 if(theEvent
.IsAllowed())
199 bool bOk
= m_parent
->Stop();
202 //send the event to our child
203 wxMediaEvent
theEvent(wxEVT_MEDIA_FINISHED
,
204 m_parent
->m_ctrl
->GetId());
205 m_parent
->m_ctrl
->ProcessEvent(theEvent
);
209 void wxGStreamerMediaBackend::OnError(GstElement
*play
,
215 wxLogSysError(wxString::Format(wxT("Error in GStreamer Playback!\nError Message:%s"), wxString(err
->message
).c_str()));
219 bool wxGStreamerMediaBackend::Load(const wxString
& fileName
)
223 wxString( wxT("file://") ) + fileName
228 bool wxGStreamerMediaBackend::Load(const wxURI
& location
)
231 wxString locstring
= location
.BuildURI();
234 if ( GST_STATE(m_player
) > GST_STATE_READY
)
235 gst_element_set_state(m_player
, GST_STATE_READY
);
237 g_object_set (G_OBJECT (m_player
), "uri", locstring
.c_str(), NULL
);
239 gst_x_overlay_expose(GST_X_OVERLAY(m_videosink
));
241 return GST_STATE(m_player
) == GST_STATE_READY
;
244 bool wxGStreamerMediaBackend::Play()
246 if (gst_element_set_state (m_player
, GST_STATE_PLAYING
)
247 != GST_STATE_SUCCESS
)
252 bool wxGStreamerMediaBackend::Pause()
254 if (gst_element_set_state (m_player
, GST_STATE_PAUSED
)
255 != GST_STATE_SUCCESS
)
260 bool wxGStreamerMediaBackend::Stop()
262 if (gst_element_set_state (m_player
,
263 GST_STATE_READY
) != GST_STATE_SUCCESS
)
268 wxMediaState
wxGStreamerMediaBackend::GetState()
270 switch(GST_STATE(m_player
))
272 case GST_STATE_PLAYING
:
273 return wxMEDIASTATE_PLAYING
;
274 case GST_STATE_PAUSED
:
275 return wxMEDIASTATE_PAUSED
;
276 default://case GST_STATE_READY:
277 return wxMEDIASTATE_STOPPED
;
281 bool wxGStreamerMediaBackend::SetPosition(wxLongLong where
)
283 return gst_element_seek (play
, (GstSeekType
) (GST_SEEK_METHOD_SET
|
284 GST_FORMAT_TIME
| GST_SEEK_FLAG_FLUSH
),
285 where
* GST_MSECOND
);
288 wxLongLong
wxGStreamerMediaBackend::GetPosition()
291 GstFormat fmtTime
= GST_FORMAT_TIME
;
293 if (!gst_element_query (play
, GST_QUERY_POSITION
, &fmtTime
, &pos
))
295 return pos
/ GST_MSECOND
;
298 wxLongLong
wxGStreamerMediaBackend::GetDuration()
301 GstFormat fmtTime
= GST_FORMAT_TIME
;
303 if(!gst_element_query(m_player
, GST_QUERY_TOTAL
, &fmtTime
, &length
))
305 return length
/ GST_MSECOND
;
308 void wxGStreamerMediaBackend::Move(int x
, int y
, int w
, int h
)
312 wxSize
wxGStreamerMediaBackend::GetVideoSize() const
315 //TODO: maybe cache size
316 wxSize retSize
= wxSize(0,0);
318 const GList
*list
= NULL
;
319 g_object_get (G_OBJECT (m_player
), "stream-info", &list
, NULL
);
321 for ( ; list
!= NULL
; list
= list
->next
)
323 GObject
*info
= (GObject
*) list
->data
;
329 g_object_get (info
, "type", &type
, NULL
);
330 pspec
= g_object_class_find_property (
331 G_OBJECT_GET_CLASS (info
), "type");
332 val
= g_enum_get_value (G_PARAM_SPEC_ENUM (pspec
)->enum_class
, type
);
334 if (strstr (val
->value_name
, "VIDEO"))
336 g_object_get (info
, "object", &pad
, NULL
);
337 pad
= (GstPad
*) GST_PAD_REALIZE (pad
);
340 GstCaps
* caps
= GST_PAD_CAPS (pad
);
343 const GstStructure
*s
;
344 s
= gst_caps_get_structure (caps
, 0);
347 gst_structure_get_int (s
, "width", &retSize
.x
);
348 gst_structure_get_int (s
, "height", &retSize
.y
);
351 par
= gst_structure_get_value (s
, "pixel-aspect-ratio"));
355 int num
= gst_value_get_fraction_numerator (par
),
356 den
= gst_value_get_fraction_denominator (par
);
358 //TODO: maybe better fraction normalization...
360 retSize
.x
= (int) ((float) num
* retSize
.x
/ den
);
362 retSize
.y
= (int) ((float) den
* retSize
.y
/ num
);
368 double wxGStreamerMediaBackend::GetPlaybackRate()
371 GstClock
* theClock
= gst_element_get_clock(m_player
);
373 return gst_clock_get_speed(theClock
);
376 bool wxGStreamerMediaBackend::SetPlaybackRate(double dRate
)
379 GstClock
* theClock
= gst_element_get_clock(m_player
);
381 return gst_clock_change_speed(theClock
, GetPlaybackRate(), dRate
)==dRate
;
384 #endif //wxUSE_GSTREAMER
386 //in source file that contains stuff you don't directly use
387 #include <wx/html/forcelnk.h>
388 FORCE_LINK_ME(basewxmediabackends
);
390 #endif //wxUSE_MEDIACTRL