]> git.saurik.com Git - wxWidgets.git/blame - src/unix/mediactrl.cpp
Add code for parsing globs file in /usr/local as well
[wxWidgets.git] / src / unix / mediactrl.cpp
CommitLineData
ddc90a8d 1/////////////////////////////////////////////////////////////////////////////
7ec69821 2// Name: src/unix/mediactrl.cpp
557002cf 3// Purpose: GStreamer backend for Unix
ddc90a8d
RN
4// Author: Ryan Norton <wxprojects@comcast.net>
5// Modified by:
6// Created: 02/04/05
7// RCS-ID: $Id$
8// Copyright: (c) 2004-2005 Ryan Norton
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
ddc90a8d
RN
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
ddc90a8d
RN
15#if wxUSE_MEDIACTRL
16
e4db172a
WS
17#include "wx/mediactrl.h"
18
0c5c0375
RN
19#if wxUSE_GSTREAMER
20
557002cf 21#include <gst/gst.h> // main gstreamer header
0c5c0375 22
557002cf
VZ
23// xoverlay/video stuff, gst-gconf for 0.8
24#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
25# include <gst/interfaces/xoverlay.h>
26#else
27# include <gst/xoverlay/xoverlay.h>
28# include <gst/gconf/gconf.h> // gstreamer glib configuration
29#endif
0c5c0375 30
e4db172a
WS
31#ifndef WX_PRECOMP
32 #include "wx/log.h" // wxLogDebug/wxLogSysError/wxLogTrace
670f9935 33 #include "wx/app.h" // wxTheApp->argc, wxTheApp->argv
c0badb70 34 #include "wx/timer.h" // wxTimer
e4db172a
WS
35#endif
36
557002cf 37#include "wx/thread.h" // wxMutex/wxMutexLocker
0c5c0375
RN
38
39#ifdef __WXGTK__
08f53168 40 #include <gtk/gtk.h>
e8a5d614 41# include <gdk/gdkx.h> // for GDK_WINDOW_XWINDOW
557002cf
VZ
42#endif
43
44//-----------------------------------------------------------------------------
45// Discussion of internals
46//-----------------------------------------------------------------------------
47
48/*
49 This is the GStreamer backend for unix. Currently we require 0.8 or
50 0.10. Here we use the "playbin" GstElement for ease of use.
51
52 Note that now we compare state change functions to GST_STATE_FAILURE
53 now rather than GST_STATE_SUCCESS as newer gstreamer versions return
54 non-success values for returns that are otherwise successful but not
55 immediate.
56
57 Also this probably doesn't work with anything other than wxGTK at the
58 moment but with a tad bit of work it could theorectically work in
59 straight wxX11 et al.
60
61 One last note is that resuming from pausing/seeking can result
62 in erratic video playback (GStreamer-based bug, happens in totem as well)
63 - this is better in 0.10, however. One thing that might make it worse
64 here is that we don't preserve the aspect ratio of the video and stretch
65 it to the whole window.
66
67 Note that there are some things used here that could be undocumented -
68 for reference see the media player Kiss and Totem as well as some
69 other sources. There was a backend for a kde media player as well
70 that attempted thread-safety...
71
72 Then there is the issue of m_asynclock. This serves several purposes:
73 1) It prevents the C callbacks from sending wx state change events
74 so that we don't get duplicate ones in 0.8
75 2) It makes the sync and async handlers in 0.10 not drop any
76 messages so that while we are polling it we get the messages in
77 SyncStateChange instead of the queue.
78 3) Keeps the pausing in Stop() synchronous
79
80 RN: Note that I've tried to follow the wxGTK conventions here as close
81 as possible. In the implementation the C Callbacks come first, then
82 the internal functions, then the public ones. Set your vi to 80
83 characters people :).
84*/
85
86//=============================================================================
87// Declarations
88//=============================================================================
89
90//-----------------------------------------------------------------------------
91// GStreamer (most version compatability) macros
92//-----------------------------------------------------------------------------
93
94// In 0.9 there was a HUGE change to GstQuery and the
95// gst_element_query function changed dramatically and split off
96// into two seperate ones
97#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR <= 8
98# define wxGst_element_query_duration(e, f, p) \
99 gst_element_query(e, GST_QUERY_TOTAL, f, p)
100# define wxGst_element_query_position(e, f, p) \
101 gst_element_query(e, GST_QUERY_POSITION, f, p)
102#elif GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR == 9
103// However, the actual 0.9 version has a slightly different definition
104// and instead of gst_element_query_duration it has two parameters to
105// gst_element_query_position instead
106# define wxGst_element_query_duration(e, f, p) \
107 gst_element_query_position(e, f, 0, p)
108# define wxGst_element_query_position(e, f, p) \
109 gst_element_query_position(e, f, p, 0)
110#else
111# define wxGst_element_query_duration \
112 gst_element_query_duration
113# define wxGst_element_query_position \
114 gst_element_query_position
115#endif
116
117// Other 0.10 macros
118#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
119# define GST_STATE_FAILURE GST_STATE_CHANGE_FAILURE
120# define GST_STATE_SUCCESS GST_STATE_CHANGE_SUCCESS
121# define GstElementState GstState
122# define gst_gconf_get_default_video_sink() \
123 gst_element_factory_make ("gconfvideosink", "video-sink");
124# define gst_gconf_get_default_audio_sink() \
125 gst_element_factory_make ("gconfaudiosink", "audio-sink");
0c5c0375
RN
126#endif
127
557002cf
VZ
128// Max wait time for element state waiting - GST_CLOCK_TIME_NONE for inf
129#define wxGSTREAMER_TIMEOUT (100 * GST_MSECOND) // Max 100 milliseconds
130
557002cf
VZ
131//-----------------------------------------------------------------------------
132// wxLogTrace mask string
133//-----------------------------------------------------------------------------
134#define wxTRACE_GStreamer wxT("GStreamer")
135
136//-----------------------------------------------------------------------------
137//
138// wxGStreamerMediaBackend
139//
140//-----------------------------------------------------------------------------
141class WXDLLIMPEXP_MEDIA
142 wxGStreamerMediaBackend : public wxMediaBackendCommonBase
0c5c0375
RN
143{
144public:
145
146 wxGStreamerMediaBackend();
d3c7fc99 147 virtual ~wxGStreamerMediaBackend();
0c5c0375
RN
148
149 virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
150 wxWindowID id,
151 const wxPoint& pos,
152 const wxSize& size,
153 long style,
154 const wxValidator& validator,
155 const wxString& name);
156
157 virtual bool Play();
158 virtual bool Pause();
159 virtual bool Stop();
160
161 virtual bool Load(const wxString& fileName);
162 virtual bool Load(const wxURI& location);
163
164 virtual wxMediaState GetState();
165
166 virtual bool SetPosition(wxLongLong where);
167 virtual wxLongLong GetPosition();
168 virtual wxLongLong GetDuration();
169
170 virtual void Move(int x, int y, int w, int h);
171 wxSize GetVideoSize() const;
172
173 virtual double GetPlaybackRate();
174 virtual bool SetPlaybackRate(double dRate);
175
557002cf
VZ
176 virtual wxLongLong GetDownloadProgress();
177 virtual wxLongLong GetDownloadTotal();
178
179 virtual bool SetVolume(double dVolume);
180 virtual double GetVolume();
181
182 //------------implementation from now on-----------------------------------
e17b4db3 183 bool DoLoad(const wxString& locstring);
557002cf
VZ
184 wxMediaCtrl* GetControl() { return m_ctrl; } // for C Callbacks
185 void HandleStateChange(GstElementState oldstate, GstElementState newstate);
186 bool QueryVideoSizeFromElement(GstElement* element);
187 bool QueryVideoSizeFromPad(GstPad* caps);
188 void SetupXOverlay();
189 bool SyncStateChange(GstElement* element, GstElementState state,
190 gint64 llTimeout = wxGSTREAMER_TIMEOUT);
191 bool TryAudioSink(GstElement* audiosink);
192 bool TryVideoSink(GstElement* videosink);
193
194 GstElement* m_playbin; // GStreamer media element
195 wxSize m_videoSize; // Cached actual video size
196 double m_dRate; // Current playback rate -
197 // see GetPlaybackRate for notes
198 wxLongLong m_llPausedPos; // Paused position - see Pause()
199 GstXOverlay* m_xoverlay; // X Overlay that contains the GST video
200 wxMutex m_asynclock; // See "discussion of internals"
201 class wxGStreamerMediaEventHandler* m_eventHandler; // see below
202
203 friend class wxGStreamerMediaEventHandler;
204 friend class wxGStreamerLoadWaitTimer;
205 DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend);
206};
7ec69821 207
557002cf
VZ
208//-----------------------------------------------------------------------------
209// wxGStreamerMediaEventHandler
210//
211// OK, this will take an explanation - basically gstreamer callbacks
212// are issued in a seperate thread, and in this thread we may not set
213// the state of the playbin, so we need to send a wx event in that
214// callback so that we set the state of the media and other stuff
215// like GUI calls.
216//-----------------------------------------------------------------------------
217class wxGStreamerMediaEventHandler : public wxEvtHandler
218{
219 public:
220 wxGStreamerMediaEventHandler(wxGStreamerMediaBackend* be) : m_be(be)
221 {
222 this->Connect(wxID_ANY, wxEVT_MEDIA_FINISHED,
223 wxMediaEventHandler(wxGStreamerMediaEventHandler::OnMediaFinish));
224 }
7ec69821 225
557002cf 226 void OnMediaFinish(wxMediaEvent& event);
0c5c0375 227
557002cf 228 wxGStreamerMediaBackend* m_be;
0c5c0375
RN
229};
230
557002cf
VZ
231//=============================================================================
232// Implementation
233//=============================================================================
0c5c0375 234
412e0d47 235IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend, wxMediaBackend)
0c5c0375 236
557002cf 237//-----------------------------------------------------------------------------
dae87f93 238//
557002cf 239// C Callbacks
dae87f93 240//
557002cf 241//-----------------------------------------------------------------------------
1e22656e 242
557002cf
VZ
243//-----------------------------------------------------------------------------
244// "expose_event" from m_ctrl->m_wxwindow
dae87f93 245//
557002cf
VZ
246// Handle GTK expose event from our window - here we hopefully
247// redraw the video in the case of pausing and other instances...
248// (Returns TRUE to pass to other handlers, FALSE if not)
dae87f93 249//
557002cf
VZ
250// TODO: Do a DEBUG_MAIN_THREAD/install_idle_handler here?
251//-----------------------------------------------------------------------------
1e22656e 252#ifdef __WXGTK__
557002cf
VZ
253extern "C" {
254static gboolean gtk_window_expose_callback(GtkWidget *widget,
255 GdkEventExpose *event,
256 wxGStreamerMediaBackend *be)
257{
258 if(event->count > 0)
259 return FALSE;
1e22656e 260
ce7b001c 261 GdkWindow *window = widget->window;
1e22656e 262
557002cf
VZ
263 // I've seen this reccommended somewhere...
264 // TODO: Is this needed? Maybe it is just cruft...
265 // gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(be->m_xoverlay),
266 // GDK_WINDOW_XWINDOW( window ) );
1e22656e 267
557002cf
VZ
268 // If we have actual video.....
269 if(!(be->m_videoSize.x==0&&be->m_videoSize.y==0) &&
270 GST_STATE(be->m_playbin) >= GST_STATE_PAUSED)
271 {
272 // GST Doesn't redraw automatically while paused
273 // Plus, the video sometimes doesn't redraw when it looses focus
274 // or is painted over so we just tell it to redraw...
275 gst_x_overlay_expose(be->m_xoverlay);
276 }
277 else
278 {
279 // draw a black background like some other backends do....
280 gdk_draw_rectangle (window, widget->style->black_gc, TRUE, 0, 0,
281 widget->allocation.width,
282 widget->allocation.height);
283 }
1e22656e 284
557002cf
VZ
285 return FALSE;
286}
287}
288#endif // wxGTK
289
290//-----------------------------------------------------------------------------
291// "realize" from m_ctrl->m_wxwindow
292//
293// If the window wasn't realized when Load was called, this is the
294// callback for when it is - the purpose of which is to tell
295// GStreamer to play the video in our control
296//-----------------------------------------------------------------------------
297#ifdef __WXGTK__
298extern "C" {
ce7b001c 299static gint gtk_window_realize_callback(GtkWidget* widget,
557002cf 300 wxGStreamerMediaBackend* be)
1e22656e 301{
ce7b001c
RR
302 gdk_flush();
303
304 GdkWindow *window = widget->window;
1e22656e 305 wxASSERT(window);
7ec69821 306
557002cf 307 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(be->m_xoverlay),
a4945572
RN
308 GDK_WINDOW_XWINDOW( window )
309 );
557002cf
VZ
310 g_signal_connect (be->GetControl()->m_wxwindow,
311 "expose_event",
312 G_CALLBACK(gtk_window_expose_callback), be);
1e22656e
RN
313 return 0;
314}
557002cf
VZ
315}
316#endif // wxGTK
1e22656e 317
557002cf
VZ
318//-----------------------------------------------------------------------------
319// "state-change" from m_playbin/GST_MESSAGE_STATE_CHANGE
320//
321// Called by gstreamer when the state changes - here we
322// send the appropriate corresponding wx event.
dae87f93 323//
557002cf
VZ
324// 0.8 only as HandleStateChange does this in both versions
325//-----------------------------------------------------------------------------
326#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR < 10
327extern "C" {
328static void gst_state_change_callback(GstElement *play,
329 GstElementState oldstate,
330 GstElementState newstate,
331 wxGStreamerMediaBackend* be)
1e22656e 332{
557002cf 333 if(be->m_asynclock.TryLock() == wxMUTEX_NO_ERROR)
1e22656e 334 {
557002cf
VZ
335 be->HandleStateChange(oldstate, newstate);
336 be->m_asynclock.Unlock();
1e22656e 337 }
0c5c0375 338}
557002cf
VZ
339}
340#endif // <0.10
0c5c0375 341
557002cf
VZ
342//-----------------------------------------------------------------------------
343// "eos" from m_playbin/GST_MESSAGE_EOS
dae87f93 344//
557002cf
VZ
345// Called by gstreamer when the media is done playing ("end of stream")
346//-----------------------------------------------------------------------------
347extern "C" {
ea88b5fa 348static void gst_finish_callback(GstElement *WXUNUSED(play),
557002cf 349 wxGStreamerMediaBackend* be)
0c5c0375 350{
557002cf
VZ
351 wxLogTrace(wxTRACE_GStreamer, wxT("gst_finish_callback"));
352 wxMediaEvent event(wxEVT_MEDIA_FINISHED);
353 be->m_eventHandler->AddPendingEvent(event);
354}
1e22656e 355}
0c5c0375 356
557002cf
VZ
357//-----------------------------------------------------------------------------
358// "error" from m_playbin/GST_MESSAGE_ERROR
dae87f93 359//
557002cf
VZ
360// Called by gstreamer when an error is encountered playing the media -
361// We call wxLogTrace in addition wxLogSysError so that we can get it
362// on the command line as well for those who want extra traces.
363//-----------------------------------------------------------------------------
364extern "C" {
ea88b5fa
VZ
365static void gst_error_callback(GstElement *WXUNUSED(play),
366 GstElement *WXUNUSED(src),
557002cf
VZ
367 GError *err,
368 gchar *debug,
ea88b5fa 369 wxGStreamerMediaBackend* WXUNUSED(be))
1e22656e 370{
557002cf
VZ
371 wxString sError;
372 sError.Printf(wxT("gst_error_callback\n")
373 wxT("Error Message:%s\nDebug:%s\n"),
374 (const wxChar*)wxConvUTF8.cMB2WX(err->message),
375 (const wxChar*)wxConvUTF8.cMB2WX(debug));
376 wxLogTrace(wxTRACE_GStreamer, sError);
377 wxLogSysError(sError);
378}
1e22656e
RN
379}
380
557002cf
VZ
381//-----------------------------------------------------------------------------
382// "notify::caps" from the videopad inside "stream-info" of m_playbin
383//
384// Called by gstreamer when the video caps for the media is ready - currently
385// we use the caps to get the natural size of the video
dae87f93 386//
557002cf
VZ
387// (Undocumented?)
388//-----------------------------------------------------------------------------
389extern "C" {
390static void gst_notify_caps_callback(GstPad* pad,
ea88b5fa 391 GParamSpec* WXUNUSED(pspec),
557002cf 392 wxGStreamerMediaBackend* be)
1e22656e 393{
557002cf
VZ
394 wxLogTrace(wxTRACE_GStreamer, wxT("gst_notify_caps_callback"));
395 be->QueryVideoSizeFromPad(pad);
396}
0c5c0375
RN
397}
398
557002cf
VZ
399//-----------------------------------------------------------------------------
400// "notify::stream-info" from m_playbin
401//
402// Run through the stuff in "stream-info" of m_playbin for a valid
403// video pad, and then attempt to query the video size from it - if not
404// set up an event to do so when ready.
dae87f93 405//
557002cf
VZ
406// Currently unused - now we just query it directly using
407// QueryVideoSizeFromElement.
dae87f93 408//
557002cf
VZ
409// (Undocumented?)
410//-----------------------------------------------------------------------------
411#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
412extern "C" {
ea88b5fa
VZ
413static void gst_notify_stream_info_callback(GstElement* WXUNUSED(element),
414 GParamSpec* WXUNUSED(pspec),
557002cf 415 wxGStreamerMediaBackend* be)
0c5c0375 416{
557002cf
VZ
417 wxLogTrace(wxTRACE_GStreamer, wxT("gst_notify_stream_info_callback"));
418 be->QueryVideoSizeFromElement(be->m_playbin);
419}
420}
421#endif
0c5c0375 422
557002cf
VZ
423//-----------------------------------------------------------------------------
424// "desired-size-changed" from m_xoverlay
425//
426// 0.8-specific this provides us with the video size when it changes -
427// even though we get the caps as well this seems to come before the
428// caps notification does...
429//
430// Note it will return 16,16 for an early-bird value or for audio
431//-----------------------------------------------------------------------------
432#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR < 10
433extern "C" {
434static void gst_desired_size_changed_callback(GstElement * play,
435 guint width, guint height,
436 wxGStreamerMediaBackend* be)
437{
438 if(!(width == 16 && height == 16))
0c5c0375 439 {
557002cf
VZ
440 be->m_videoSize.x = width;
441 be->m_videoSize.y = height;
0c5c0375 442 }
557002cf
VZ
443 else
444 be->QueryVideoSizeFromElement(be->m_playbin);
445}
0c5c0375 446}
557002cf 447#endif
0c5c0375 448
557002cf
VZ
449//-----------------------------------------------------------------------------
450// gst_bus_async_callback [static]
451// gst_bus_sync_callback [static]
dae87f93 452//
557002cf
VZ
453// Called by m_playbin for notifications such as end-of-stream in 0.10 -
454// in previous versions g_signal notifications were used. Because everything
455// in centered in one switch statement though it reminds one of old WinAPI
456// stuff.
dae87f93 457//
557002cf
VZ
458// gst_bus_sync_callback is that sync version that is called on the main GUI
459// thread before the async version that we use to set the xwindow id of the
460// XOverlay (NB: This isn't currently used - see CreateControl()).
461//-----------------------------------------------------------------------------
462#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
463extern "C" {
ea88b5fa 464static gboolean gst_bus_async_callback(GstBus* WXUNUSED(bus),
557002cf
VZ
465 GstMessage* message,
466 wxGStreamerMediaBackend* be)
0c5c0375 467{
557002cf
VZ
468 if(((GstElement*)GST_MESSAGE_SRC(message)) != be->m_playbin)
469 return TRUE;
470 if(be->m_asynclock.TryLock() != wxMUTEX_NO_ERROR)
471 return TRUE;
0c5c0375 472
557002cf
VZ
473 switch(GST_MESSAGE_TYPE(message))
474 {
475 case GST_MESSAGE_STATE_CHANGED:
476 {
477 GstState oldstate, newstate, pendingstate;
478 gst_message_parse_state_changed(message, &oldstate,
479 &newstate, &pendingstate);
480 be->HandleStateChange(oldstate, newstate);
481 break;
482 }
483 case GST_MESSAGE_EOS:
484 {
485 gst_finish_callback(NULL, be);
486 break;
487 }
488 case GST_MESSAGE_ERROR:
489 {
490 GError* error;
491 gchar* debug;
492 gst_message_parse_error(message, &error, &debug);
493 gst_error_callback(NULL, NULL, error, debug, be);
494 break;
495 }
496 default:
497 break;
498 }
0c5c0375 499
557002cf
VZ
500 be->m_asynclock.Unlock();
501 return FALSE; // remove the message from Z queue
502}
503
504static GstBusSyncReply gst_bus_sync_callback(GstBus* bus,
505 GstMessage* message,
506 wxGStreamerMediaBackend* be)
0c5c0375 507{
557002cf
VZ
508 // Pass a non-xwindowid-setting event on to the async handler where it
509 // belongs
510 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT ||
511 !gst_structure_has_name (message->structure, "prepare-xwindow-id"))
512 {
513 //
514 // NB: Unfortunately, the async callback can be quite
515 // buggy at times and often doesn't get called at all,
516 // so here we are processing it right here in the calling
517 // thread instead of the GUI one...
518 //
519 if(gst_bus_async_callback(bus, message, be))
520 return GST_BUS_PASS;
521 else
522 return GST_BUS_DROP;
523 }
524
525 wxLogTrace(wxTRACE_GStreamer, wxT("Got prepare-xwindow-id"));
526 be->SetupXOverlay();
527 return GST_BUS_DROP; // We handled this message - drop from the queue
528}
0c5c0375 529}
557002cf 530#endif
0c5c0375 531
557002cf
VZ
532//-----------------------------------------------------------------------------
533//
534// Private (although not in the C++ sense) methods
dae87f93 535//
557002cf
VZ
536//-----------------------------------------------------------------------------
537
538//-----------------------------------------------------------------------------
539// wxGStreamerMediaBackend::HandleStateChange
540//
541// Handles a state change event from our C Callback for "state-change" or
542// the async queue in 0.10. (Mostly this is here to avoid locking the
543// the mutex twice...)
544//-----------------------------------------------------------------------------
545void wxGStreamerMediaBackend::HandleStateChange(GstElementState oldstate,
546 GstElementState newstate)
1e22656e 547{
557002cf
VZ
548 switch(newstate)
549 {
550 case GST_STATE_PLAYING:
551 wxLogTrace(wxTRACE_GStreamer, wxT("Play event"));
552 QueuePlayEvent();
553 break;
554 case GST_STATE_PAUSED:
555 // For some reason .10 sends a lot of oldstate == newstate
556 // messages - most likely for pending ones - also
557 // !<GST_STATE_PAUSED as we are only concerned
558 if(oldstate < GST_STATE_PAUSED || oldstate == newstate)
559 break;
560 if(wxGStreamerMediaBackend::GetPosition() != 0)
561 {
562 wxLogTrace(wxTRACE_GStreamer, wxT("Pause event"));
563 QueuePauseEvent();
564 }
565 else
566 {
567 wxLogTrace(wxTRACE_GStreamer, wxT("Stop event"));
568 QueueStopEvent();
569 }
570 break;
571 default: // GST_STATE_NULL etc.
572 break;
573 }
1e22656e
RN
574}
575
557002cf
VZ
576//-----------------------------------------------------------------------------
577// wxGStreamerMediaBackend::QueryVideoSizeFromElement
dae87f93 578//
557002cf
VZ
579// Run through the stuff in "stream-info" of element for a valid
580// video pad, and then attempt to query the video size from it - if not
581// set up an event to do so when ready. Return true
582// if we got a valid video pad.
583//-----------------------------------------------------------------------------
584bool wxGStreamerMediaBackend::QueryVideoSizeFromElement(GstElement* element)
0c5c0375 585{
557002cf
VZ
586 const GList *list = NULL;
587 g_object_get (G_OBJECT (element), "stream-info", &list, NULL);
1e22656e 588
557002cf
VZ
589 for ( ; list != NULL; list = list->next)
590 {
591 GObject *info = (GObject *) list->data;
592 gint type;
593 GParamSpec *pspec;
594 GEnumValue *val;
595 GstPad *pad = NULL;
7ec69821 596
557002cf
VZ
597 g_object_get (info, "type", &type, NULL);
598 pspec = g_object_class_find_property (
599 G_OBJECT_GET_CLASS (info), "type");
600 val = g_enum_get_value (G_PARAM_SPEC_ENUM (pspec)->enum_class, type);
0c5c0375 601
557002cf
VZ
602 if (!strncasecmp(val->value_name, "video", 5) ||
603 !strncmp(val->value_name, "GST_STREAM_TYPE_VIDEO", 21))
604 {
605 // Newer gstreamer 0.8+ plugins are SUPPOSED to have "object"...
606 // but a lot of old plugins still use "pad" :)
607 pspec = g_object_class_find_property (
608 G_OBJECT_GET_CLASS (info), "object");
a4945572 609
557002cf
VZ
610 if (!pspec)
611 g_object_get (info, "pad", &pad, NULL);
612 else
613 g_object_get (info, "object", &pad, NULL);
a4945572 614
557002cf
VZ
615#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR <= 8
616 // Killed in 0.9, presumely because events and such
617 // should be pushed on pads regardless of whether they
618 // are currently linked
619 pad = (GstPad *) GST_PAD_REALIZE (pad);
620 wxASSERT(pad);
621#endif
a4945572 622
557002cf
VZ
623 if(!QueryVideoSizeFromPad(pad))
624 {
625 // wait for those caps to get ready
626 g_signal_connect(
627 pad,
628 "notify::caps",
629 G_CALLBACK(gst_notify_caps_callback),
630 this);
631 }
632 break;
633 }// end if video
634 }// end searching through info list
7ec69821 635
557002cf
VZ
636 // no video (or extremely delayed stream-info)
637 if(list == NULL)
638 {
639 m_videoSize = wxSize(0,0);
640 return false;
a4945572 641 }
7ec69821 642
557002cf
VZ
643 return true;
644}
645
646//-----------------------------------------------------------------------------
647// wxGStreamerMediaBackend::QueryVideoSizeFromPad
648//
649// Gets the size of our video (in wxSize) from a GstPad
650//-----------------------------------------------------------------------------
651bool wxGStreamerMediaBackend::QueryVideoSizeFromPad(GstPad* pad)
652{
653 const GstCaps* caps = GST_PAD_CAPS(pad);
654 if ( caps )
7ec69821 655 {
557002cf
VZ
656 const GstStructure *s = gst_caps_get_structure (caps, 0);
657 wxASSERT(s);
7ec69821 658
557002cf
VZ
659 gst_structure_get_int (s, "width", &m_videoSize.x);
660 gst_structure_get_int (s, "height", &m_videoSize.y);
7ec69821 661
557002cf
VZ
662 const GValue *par;
663 par = gst_structure_get_value (s, "pixel-aspect-ratio");
664
665 if (par)
666 {
667 wxLogTrace(wxTRACE_GStreamer,
668 wxT("pixel-aspect-ratio found in pad"));
669 int num = par->data[0].v_int,
670 den = par->data[1].v_int;
b4a345a6 671
557002cf
VZ
672 // TODO: maybe better fraction normalization...
673 if (num > den)
674 m_videoSize.x = (int) ((float) num * m_videoSize.x / den);
675 else
676 m_videoSize.y = (int) ((float) den * m_videoSize.y / num);
677 }
a4945572 678
557002cf
VZ
679 wxLogTrace(wxTRACE_GStreamer, wxT("Adjusted video size: [%i,%i]"),
680 m_videoSize.x, m_videoSize.y);
681 return true;
682 } // end if caps
0c5c0375 683
557002cf
VZ
684 return false; // not ready/massive failure
685}
7ec69821 686
557002cf
VZ
687//-----------------------------------------------------------------------------
688// wxGStreamerMediaBackend::SetupXOverlay
689//
690// Attempts to set the XWindow id of our GstXOverlay to tell it which
691// window to play video in.
692//-----------------------------------------------------------------------------
693void wxGStreamerMediaBackend::SetupXOverlay()
694{
695 // Use the xoverlay extension to tell gstreamer to play in our window
1e22656e
RN
696#ifdef __WXGTK__
697 if(!GTK_WIDGET_REALIZED(m_ctrl->m_wxwindow))
698 {
557002cf 699 // Not realized yet - set to connect at realization time
18e98904
MR
700 g_signal_connect (m_ctrl->m_wxwindow,
701 "realize",
557002cf 702 G_CALLBACK (gtk_window_realize_callback),
18e98904 703 this);
7ec69821 704 }
1e22656e
RN
705 else
706 {
ce7b001c
RR
707 gdk_flush();
708
709 GdkWindow *window = m_ctrl->m_wxwindow->window;
1e22656e
RN
710 wxASSERT(window);
711#endif
0c5c0375 712
557002cf 713 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(m_xoverlay),
1e22656e
RN
714#ifdef __WXGTK__
715 GDK_WINDOW_XWINDOW( window )
716#else
717 ctrl->GetHandle()
718#endif
719 );
720
7ec69821 721#ifdef __WXGTK__
557002cf
VZ
722 g_signal_connect (m_ctrl->m_wxwindow,
723 // m_ctrl->m_wxwindow/*m_ctrl->m_widget*/,
724 "expose_event",
725 G_CALLBACK(gtk_window_expose_callback), this);
726 } // end if GtkPizza realized
727#endif
728}
729
730//-----------------------------------------------------------------------------
731// wxGStreamerMediaBackend::SyncStateChange
732//
733// This function is rather complex - basically the idea is that we
734// poll the GstBus of m_playbin until it has reached desiredstate, an error
735// is reached, or there are no more messages left in the GstBus queue.
736//
737// Returns true if there are no messages left in the queue or
738// the current state reaches the disired state.
739//
740// PRECONDITION: Assumes m_asynclock is Lock()ed
741//-----------------------------------------------------------------------------
742#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
743bool wxGStreamerMediaBackend::SyncStateChange(GstElement* element,
744 GstElementState desiredstate,
745 gint64 llTimeout)
746{
747 GstBus* bus = gst_element_get_bus(element);
748 GstMessage* message;
749 bool bBreak = false,
750 bSuccess = false;
751 gint64 llTimeWaited = 0;
752
753 do
754 {
755#if 1
756 // NB: The GStreamer gst_bus_poll is unfortunately broken and
757 // throws silly critical internal errors (for instance
758 // "message != NULL" when the whole point of it is to
759 // poll for the message in the first place!) so we implement
760 // our own "waiting mechinism"
761 if(gst_bus_have_pending(bus) == FALSE)
762 {
763 if(llTimeWaited >= llTimeout)
764 return true; // Reached timeout... assume success
765 llTimeWaited += 10*GST_MSECOND;
766 wxMilliSleep(10);
767 continue;
768 }
769
770 message = gst_bus_pop(bus);
771#else
772 message = gst_bus_poll(bus, (GstMessageType)
773 (GST_MESSAGE_STATE_CHANGED |
774 GST_MESSAGE_ERROR |
775 GST_MESSAGE_EOS), llTimeout);
776 if(!message)
777 return true;
7ec69821 778#endif
557002cf
VZ
779 if(((GstElement*)GST_MESSAGE_SRC(message)) == element)
780 {
781 switch(GST_MESSAGE_TYPE(message))
782 {
783 case GST_MESSAGE_STATE_CHANGED:
784 {
785 GstState oldstate, newstate, pendingstate;
786 gst_message_parse_state_changed(message, &oldstate,
787 &newstate, &pendingstate);
788 if(newstate == desiredstate)
789 {
790 bSuccess = bBreak = true;
791 }
792 break;
793 }
794 case GST_MESSAGE_ERROR:
795 {
796 GError* error;
797 gchar* debug;
798 gst_message_parse_error(message, &error, &debug);
799 gst_error_callback(NULL, NULL, error, debug, this);
800 bBreak = true;
801 break;
802 }
803 case GST_MESSAGE_EOS:
804 wxLogSysError(wxT("Reached end of stream prematurely"));
805 bBreak = true;
806 break;
807 default:
808 break; // not handled
809 }
810 }
811
812 gst_message_unref(message);
813 }while(!bBreak);
7ec69821 814
557002cf
VZ
815 return bSuccess;
816}
817#else // 0.8 implementation
818bool wxGStreamerMediaBackend::SyncStateChange(GstElement* element,
819 GstElementState desiredstate,
820 gint64 llTimeout)
821{
822 gint64 llTimeWaited = 0;
823 while(GST_STATE(element) != desiredstate)
7ec69821 824 {
557002cf
VZ
825 if(llTimeWaited >= llTimeout)
826 break;
827 llTimeWaited += 10*GST_MSECOND;
828 wxMilliSleep(10);
829 }
830
831 return llTimeWaited != llTimeout;
832}
833#endif
834
835//-----------------------------------------------------------------------------
836// wxGStreamerMediaBackend::TryAudioSink
837// wxGStreamerMediaBackend::TryVideoSink
838//
839// Uses various means to determine whether a passed in video/audio sink
840// if suitable for us - if it is not we return false and unref the
841// inappropriate sink.
842//-----------------------------------------------------------------------------
843bool wxGStreamerMediaBackend::TryAudioSink(GstElement* audiosink)
844{
845 if( !GST_IS_ELEMENT(audiosink) )
846 {
847 if(G_IS_OBJECT(audiosink))
848 g_object_unref(audiosink);
7ec69821
WS
849 return false;
850 }
a4945572 851
557002cf
VZ
852 return true;
853}
1e22656e 854
557002cf
VZ
855bool wxGStreamerMediaBackend::TryVideoSink(GstElement* videosink)
856{
857 // Check if the video sink either is an xoverlay or might contain one...
858 if( !GST_IS_BIN(videosink) && !GST_IS_X_OVERLAY(videosink) )
859 {
860 if(G_IS_OBJECT(videosink))
861 g_object_unref(videosink);
862 return false;
863 }
c5191fbd 864
557002cf
VZ
865 // Make our video sink and make sure it supports the x overlay interface
866 // the x overlay enables us to put the video in our control window
867 // (i.e. we NEED it!) - also connect to the natural video size change event
868 if( GST_IS_BIN(videosink) )
869 m_xoverlay = (GstXOverlay*)
870 gst_bin_get_by_interface (GST_BIN (videosink),
871 GST_TYPE_X_OVERLAY);
872 else
873 m_xoverlay = (GstXOverlay*) videosink;
874
875 if ( !GST_IS_X_OVERLAY(m_xoverlay) )
1e22656e 876 {
557002cf
VZ
877 g_object_unref(videosink);
878 return false;
879 }
1e22656e 880
557002cf
VZ
881 return true;
882}
1e22656e 883
557002cf
VZ
884//-----------------------------------------------------------------------------
885// wxGStreamerMediaEventHandler::OnMediaFinish
886//
887// Called when the media is about to stop
888//-----------------------------------------------------------------------------
b286fd73 889void wxGStreamerMediaEventHandler::OnMediaFinish(wxMediaEvent& WXUNUSED(event))
557002cf
VZ
890{
891 // (RN - I have no idea why I thought this was good behaviour....
892 // maybe it made sense for streaming/nonseeking data but
893 // generally it seems like a really bad idea) -
894 if(m_be->SendStopEvent())
895 {
896 // Stop the media (we need to set it back to paused
897 // so that people can get the duration et al.
898 // and send the finish event (luckily we can "Sync" it out... LOL!)
899 // (We don't check return values here because we can't really do
900 // anything...)
901 wxMutexLocker lock(m_be->m_asynclock);
902
903 // Set element to ready+sync it
904 gst_element_set_state (m_be->m_playbin, GST_STATE_READY);
905 m_be->SyncStateChange(m_be->m_playbin, GST_STATE_READY);
906
907 // Now set it to paused + update pause pos to 0 and
908 // Sync that as well (note that we don't call Stop() here
909 // due to mutex issues)
910 gst_element_set_state (m_be->m_playbin, GST_STATE_PAUSED);
911 m_be->SyncStateChange(m_be->m_playbin, GST_STATE_PAUSED);
912 m_be->m_llPausedPos = 0;
913
914 // Finally, queue the finish event
915 m_be->QueueFinishEvent();
916 }
917}
7ec69821 918
557002cf
VZ
919//-----------------------------------------------------------------------------
920//
921// Public methods
922//
923//-----------------------------------------------------------------------------
7ec69821 924
557002cf
VZ
925//-----------------------------------------------------------------------------
926// wxGStreamerMediaBackend Constructor
927//
928// Sets m_playbin to NULL signifying we havn't loaded anything yet
929//-----------------------------------------------------------------------------
930wxGStreamerMediaBackend::wxGStreamerMediaBackend()
820162a6
VZ
931 : m_playbin(NULL),
932 m_eventHandler(NULL)
557002cf
VZ
933{
934}
935
936//-----------------------------------------------------------------------------
937// wxGStreamerMediaBackend Destructor
938//
939// Stops/cleans up memory
940//
941// NB: This could trigger a critical warning but doing a SyncStateChange
942// here is just going to slow down quitting of the app, which is bad.
943//-----------------------------------------------------------------------------
944wxGStreamerMediaBackend::~wxGStreamerMediaBackend()
945{
946 // Dispose of the main player and related objects
947 if(m_playbin)
948 {
949 wxASSERT( GST_IS_OBJECT(m_playbin) );
950 gst_element_set_state (m_playbin, GST_STATE_NULL);
951 gst_object_unref (GST_OBJECT (m_playbin));
952 delete m_eventHandler;
953 }
954}
955
956//-----------------------------------------------------------------------------
957// wxGStreamerMediaBackend::CreateControl
958//
959// Initializes GStreamer and creates the wx side of our media control
960//-----------------------------------------------------------------------------
961bool wxGStreamerMediaBackend::CreateControl(wxControl* ctrl, wxWindow* parent,
962 wxWindowID id,
963 const wxPoint& pos,
964 const wxSize& size,
965 long style,
966 const wxValidator& validator,
967 const wxString& name)
968{
969 //
970 //init gstreamer
971 //
e17b4db3
RD
972
973 //Convert arguments to unicode if enabled
27ef4b9b
MR
974#if wxUSE_UNICODE
975 int i;
976 char **argvGST = new char*[wxTheApp->argc + 1];
977 for ( i = 0; i < wxTheApp->argc; i++ )
978 {
0da55f82 979#if wxUSE_UNICODE_WCHAR
27ef4b9b 980 argvGST[i] = wxStrdupA(wxConvUTF8.cWX2MB(wxTheApp->argv[i]));
0da55f82
RR
981#else
982 argvGST[i] = wxStrdupA(wxTheApp->argv[i].utf8_str());
983#endif
27ef4b9b
MR
984 }
985
986 argvGST[wxTheApp->argc] = NULL;
987
988 int argcGST = wxTheApp->argc;
e17b4db3
RD
989#else
990#define argcGST wxTheApp->argc
991#define argvGST wxTheApp->argv
992#endif
27ef4b9b 993
e17b4db3
RD
994 //Really init gstreamer
995 gboolean bInited;
996 GError* error = NULL;
997#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
998 bInited = gst_init_check(&argcGST, &argvGST, &error);
999#else
1000 bInited = gst_init_check(&argcGST, &argvGST);
1001#endif
27ef4b9b 1002
e17b4db3
RD
1003 // Cleanup arguments for unicode case
1004#if wxUSE_UNICODE
27ef4b9b
MR
1005 for ( i = 0; i < argcGST; i++ )
1006 {
1007 free(argvGST[i]);
1008 }
1009
1010 delete [] argvGST;
27ef4b9b 1011#endif
557002cf 1012
e17b4db3
RD
1013 if(!bInited) //gst_init_check fail?
1014 {
1015 if(error)
1016 {
1017 wxLogSysError(wxT("Could not initialize GStreamer\n")
e4db172a 1018 wxT("Error Message:%s"),
e17b4db3
RD
1019 (const wxChar*) wxConvUTF8.cMB2WX(error->message)
1020 );
1021 g_error_free(error);
1022 }
1023 else
1024 wxLogSysError(wxT("Could not initialize GStreamer"));
1025
1026 return false;
1027 }
1028
557002cf
VZ
1029 //
1030 // wxControl creation
1031 //
1032 m_ctrl = wxStaticCast(ctrl, wxMediaCtrl);
1033
1034#ifdef __WXGTK__
1035 // We handle our own GTK expose events
e4db172a 1036 m_ctrl->m_noExpose = true;
557002cf
VZ
1037#endif
1038
1039 if( !m_ctrl->wxControl::Create(parent, id, pos, size,
1040 style, // TODO: remove borders???
1041 validator, name) )
1042 {
1043 wxFAIL_MSG(wxT("Could not create wxControl!!!"));
1044 return false;
1045 }
1046
1047#ifdef __WXGTK__
1048 // Turn off double-buffering so that
1049 // so it doesn't draw over the video and cause sporadic
1050 // disappearances of the video
1051 gtk_widget_set_double_buffered(m_ctrl->m_wxwindow, FALSE);
557002cf
VZ
1052#endif
1053
1054 // don't erase the background of our control window
1055 // so that resizing is a bit smoother
1056 m_ctrl->SetBackgroundStyle(wxBG_STYLE_CUSTOM);
1057
1058 // Create our playbin object
1059 m_playbin = gst_element_factory_make ("playbin", "play");
1060 if (!GST_IS_ELEMENT(m_playbin))
820162a6 1061 {
557002cf
VZ
1062 if(G_IS_OBJECT(m_playbin))
1063 g_object_unref(m_playbin);
1064 wxLogSysError(wxT("Got an invalid playbin"));
1065 return false;
820162a6 1066 }
1e22656e 1067
557002cf
VZ
1068#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR < 10
1069 // Connect the glib events/callbacks we want to our playbin
1070 g_signal_connect(m_playbin, "eos",
1071 G_CALLBACK(gst_finish_callback), this);
1072 g_signal_connect(m_playbin, "error",
1073 G_CALLBACK(gst_error_callback), this);
1074 g_signal_connect(m_playbin, "state-change",
1075 G_CALLBACK(gst_state_change_callback), this);
1076#else
1077 // GStreamer 0.10+ uses GstBus for this now, connect to the sync
1078 // handler as well so we can set the X window id of our xoverlay
1079 gst_bus_add_watch (gst_element_get_bus(m_playbin),
1080 (GstBusFunc) gst_bus_async_callback, this);
1081 gst_bus_set_sync_handler(gst_element_get_bus(m_playbin),
1082 (GstBusSyncHandler) gst_bus_sync_callback, this);
1083 g_signal_connect(m_playbin, "notify::stream-info",
1084 G_CALLBACK(gst_notify_stream_info_callback), this);
1085#endif
1086
1087 // Get the audio sink
1088 GstElement* audiosink = gst_gconf_get_default_audio_sink();
1089 if( !TryAudioSink(audiosink) )
1090 {
1091 // fallback to autodetection, then alsa, then oss as a stopgap
1092 audiosink = gst_element_factory_make ("autoaudiosink", "audio-sink");
1093 if( !TryAudioSink(audiosink) )
1094 {
1095 audiosink = gst_element_factory_make ("alsasink", "alsa-output");
1096 if( !TryAudioSink(audiosink) )
1e22656e 1097 {
557002cf
VZ
1098 audiosink = gst_element_factory_make ("osssink", "play_audio");
1099 if( !TryAudioSink(audiosink) )
1100 {
1101 wxLogSysError(wxT("Could not find a valid audiosink"));
820162a6 1102 return false;
557002cf 1103 }
1e22656e 1104 }
557002cf
VZ
1105 }
1106 }
c5191fbd 1107
557002cf
VZ
1108 // Setup video sink - first try gconf, then auto, then xvimage and
1109 // then finally plain ximage
1110 GstElement* videosink = gst_gconf_get_default_video_sink();
1111 if( !TryVideoSink(videosink) )
1112 {
1113 videosink = gst_element_factory_make ("autovideosink", "video-sink");
1114 if( !TryVideoSink(videosink) )
1e22656e 1115 {
557002cf
VZ
1116 videosink = gst_element_factory_make ("xvimagesink", "video-sink");
1117 if( !TryVideoSink(videosink) )
1118 {
1119 // finally, do a final fallback to ximagesink
1120 videosink =
1121 gst_element_factory_make ("ximagesink", "video-sink");
1122 if( !TryVideoSink(videosink) )
1123 {
1124 g_object_unref(audiosink);
1125 wxLogSysError(wxT("Could not find a suitable video sink"));
1126 return false;
820162a6 1127 }
557002cf 1128 }
1e22656e 1129 }
557002cf
VZ
1130 }
1131
1132#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR < 10
1133 // Not on 0.10... called when video size changes
1134 g_signal_connect(m_xoverlay, "desired-size-changed",
1135 G_CALLBACK(gst_desired_size_changed_callback), this);
1136#endif
1137 // Tell GStreamer which window to draw to in 0.8 - 0.10
1138 // sometimes needs this too...
1139 SetupXOverlay();
1140
1141 // Now that we know (or, rather think) our video and audio sink
1142 // are valid set our playbin to use them
1143 g_object_set (G_OBJECT (m_playbin),
1144 "video-sink", videosink,
1145 "audio-sink", audiosink,
1146 NULL);
1147
1148 m_eventHandler = new wxGStreamerMediaEventHandler(this);
1149 return true;
1150}
1e22656e 1151
557002cf
VZ
1152//-----------------------------------------------------------------------------
1153// wxGStreamerMediaBackend::Load (File version)
1154//
e17b4db3 1155// Just calls DoLoad() with a prepended file scheme
557002cf
VZ
1156//-----------------------------------------------------------------------------
1157bool wxGStreamerMediaBackend::Load(const wxString& fileName)
1158{
e17b4db3 1159 return DoLoad(wxString( wxT("file://") ) + fileName);
557002cf
VZ
1160}
1161
1162//-----------------------------------------------------------------------------
1163// wxGStreamerMediaBackend::Load (URI version)
1164//
e17b4db3
RD
1165// In the case of a file URI passes it unencoded -
1166// also, as of 0.10.3 and earlier GstURI (the uri parser for gstreamer)
1167// is sort of broken and only accepts uris with at least two slashes
1168// after the scheme (i.e. file: == not ok, file:// == ok)
1169//-----------------------------------------------------------------------------
1170bool wxGStreamerMediaBackend::Load(const wxURI& location)
1171{
1172 if(location.GetScheme().CmpNoCase(wxT("file")) == 0)
1173 {
1174 wxString uristring = location.BuildUnescapedURI();
1175
1176 //Workaround GstURI leading "//" problem and make sure it leads
1177 //with that
e4db172a
WS
1178 return DoLoad(wxString(wxT("file://")) +
1179 uristring.Right(uristring.length() - 5)
e17b4db3
RD
1180 );
1181 }
e4db172a 1182 else
e17b4db3
RD
1183 return DoLoad(location.BuildURI());
1184}
1185
1186//-----------------------------------------------------------------------------
1187// wxGStreamerMediaBackend::DoLoad
1188//
557002cf
VZ
1189// Loads the media
1190// 1) Reset member variables and set playbin back to ready state
1191// 2) Check URI for validity and then tell the playbin to load it
1192// 3) Set the playbin to the pause state
1193//
1194// NB: Even after this function is over with we probably don't have the
1195// video size or duration - no amount of clever hacking is going to get
1196// around that, unfortunately.
1197//-----------------------------------------------------------------------------
e17b4db3 1198bool wxGStreamerMediaBackend::DoLoad(const wxString& locstring)
557002cf
VZ
1199{
1200 wxMutexLocker lock(m_asynclock); // lock state events and async callbacks
1201
1202 // Reset positions & rate
1203 m_llPausedPos = 0;
1204 m_dRate = 1.0;
1205 m_videoSize = wxSize(0,0);
1206
1207 // Set playbin to ready to stop the current media...
1208 if( gst_element_set_state (m_playbin,
1209 GST_STATE_READY) == GST_STATE_FAILURE ||
1210 !SyncStateChange(m_playbin, GST_STATE_READY))
c5191fbd 1211 {
557002cf
VZ
1212 wxLogSysError(wxT("wxGStreamerMediaBackend::Load - ")
1213 wxT("Could not set initial state to ready"));
1214 return false;
c5191fbd 1215 }
c5191fbd 1216
92f20fe3
VZ
1217 // free current media resources
1218 gst_element_set_state (m_playbin, GST_STATE_NULL);
1219
557002cf
VZ
1220 // Make sure the passed URI is valid and tell playbin to load it
1221 // non-file uris are encoded
557002cf
VZ
1222 wxASSERT(gst_uri_protocol_is_valid("file"));
1223 wxASSERT(gst_uri_is_valid(locstring.mb_str()));
1224
1225 g_object_set (G_OBJECT (m_playbin), "uri",
1226 (const char*)locstring.mb_str(), NULL);
1227
1228 // Try to pause media as gstreamer won't let us query attributes
1229 // such as video size unless it is paused or playing
1230 if( gst_element_set_state (m_playbin,
1231 GST_STATE_PAUSED) == GST_STATE_FAILURE ||
1232 !SyncStateChange(m_playbin, GST_STATE_PAUSED))
1233 {
1234 return false; // no real error message needed here as this is
1235 // generic failure 99% of the time (i.e. no
1236 // source etc.) and has an error message
1237 }
1238
1239
1240 NotifyMovieLoaded(); // Notify the user - all we can do for now
1e22656e 1241 return true;
0c5c0375
RN
1242}
1243
557002cf
VZ
1244
1245//-----------------------------------------------------------------------------
dae87f93
RN
1246// wxGStreamerMediaBackend::Play
1247//
1248// Sets the stream to a playing state
557002cf
VZ
1249//
1250// THREAD-UNSAFE in 0.8, maybe in 0.10 as well
1251//-----------------------------------------------------------------------------
0c5c0375
RN
1252bool wxGStreamerMediaBackend::Play()
1253{
557002cf
VZ
1254 if (gst_element_set_state (m_playbin,
1255 GST_STATE_PLAYING) == GST_STATE_FAILURE)
0c5c0375
RN
1256 return false;
1257 return true;
1258}
1259
557002cf 1260//-----------------------------------------------------------------------------
dae87f93
RN
1261// wxGStreamerMediaBackend::Pause
1262//
1263// Marks where we paused and pauses the stream
557002cf
VZ
1264//
1265// THREAD-UNSAFE in 0.8, maybe in 0.10 as well
1266//-----------------------------------------------------------------------------
0c5c0375
RN
1267bool wxGStreamerMediaBackend::Pause()
1268{
557002cf
VZ
1269 m_llPausedPos = wxGStreamerMediaBackend::GetPosition();
1270 if (gst_element_set_state (m_playbin,
1271 GST_STATE_PAUSED) == GST_STATE_FAILURE)
0c5c0375
RN
1272 return false;
1273 return true;
1274}
1275
557002cf 1276//-----------------------------------------------------------------------------
dae87f93
RN
1277// wxGStreamerMediaBackend::Stop
1278//
557002cf
VZ
1279// Pauses the stream and sets the position to 0. Note that this is
1280// synchronous (!) pausing.
1281//
1282// Due to the mutex locking this is probably thread-safe actually.
1283//-----------------------------------------------------------------------------
0c5c0375
RN
1284bool wxGStreamerMediaBackend::Stop()
1285{
557002cf
VZ
1286 { // begin state lock
1287 wxMutexLocker lock(m_asynclock);
1288 if(gst_element_set_state (m_playbin,
1289 GST_STATE_PAUSED) == GST_STATE_FAILURE ||
1290 !SyncStateChange(m_playbin, GST_STATE_PAUSED))
1291 {
1292 wxLogSysError(wxT("Could not set state to paused for Stop()"));
1293 return false;
1294 }
1295 } // end state lock
1296
1297 bool bSeekedOK = wxGStreamerMediaBackend::SetPosition(0);
1298
1299 if(!bSeekedOK)
1300 {
1301 wxLogSysError(wxT("Could not seek to initial position in Stop()"));
0c5c0375 1302 return false;
557002cf
VZ
1303 }
1304
1305 QueueStopEvent(); // Success
1306 return true;
0c5c0375
RN
1307}
1308
557002cf 1309//-----------------------------------------------------------------------------
dae87f93
RN
1310// wxGStreamerMediaBackend::GetState
1311//
557002cf
VZ
1312// Gets the state of the media
1313//-----------------------------------------------------------------------------
0c5c0375
RN
1314wxMediaState wxGStreamerMediaBackend::GetState()
1315{
557002cf 1316 switch(GST_STATE(m_playbin))
0c5c0375
RN
1317 {
1318 case GST_STATE_PLAYING:
1319 return wxMEDIASTATE_PLAYING;
1320 case GST_STATE_PAUSED:
557002cf 1321 if (m_llPausedPos == 0)
1e22656e
RN
1322 return wxMEDIASTATE_STOPPED;
1323 else
1324 return wxMEDIASTATE_PAUSED;
0c5c0375
RN
1325 default://case GST_STATE_READY:
1326 return wxMEDIASTATE_STOPPED;
1327 }
1328}
1329
557002cf 1330//-----------------------------------------------------------------------------
dae87f93
RN
1331// wxGStreamerMediaBackend::GetPosition
1332//
7ec69821 1333// If paused, returns our marked position - otherwise it queries the
dae87f93
RN
1334// GStreamer playbin for the position and returns that
1335//
557002cf
VZ
1336// NB:
1337// NB: At least in 0.8, when you pause and seek gstreamer
1338// NB: doesn't update the position sometimes, so we need to keep track of
1339// NB: whether we have paused or not and keep track of the time after the
1340// NB: pause and whenever the user seeks while paused
1341// NB:
e4db172a 1342//
557002cf
VZ
1343// THREAD-UNSAFE, at least if not paused. Requires media to be at least paused.
1344//-----------------------------------------------------------------------------
0c5c0375
RN
1345wxLongLong wxGStreamerMediaBackend::GetPosition()
1346{
1e22656e 1347 if(GetState() != wxMEDIASTATE_PLAYING)
557002cf 1348 return m_llPausedPos;
1e22656e
RN
1349 else
1350 {
1351 gint64 pos;
1352 GstFormat fmtTime = GST_FORMAT_TIME;
7ec69821 1353
557002cf
VZ
1354 if (!wxGst_element_query_position(m_playbin, &fmtTime, &pos) ||
1355 fmtTime != GST_FORMAT_TIME || pos == -1)
1e22656e
RN
1356 return 0;
1357 return pos / GST_MSECOND ;
1358 }
0c5c0375
RN
1359}
1360
557002cf 1361//-----------------------------------------------------------------------------
dae87f93
RN
1362// wxGStreamerMediaBackend::SetPosition
1363//
1364// Sets the position of the stream
1365// Note that GST_MSECOND is 1000000 (GStreamer uses nanoseconds - so
1366// there is 1000000 nanoseconds in a millisecond)
1367//
557002cf
VZ
1368// If we are paused we update the cached pause position.
1369//
1370// This is also an exceedingly ugly function due to the three implementations
1371// (or, rather two plus one implementation without a seek function).
1372//
1373// This is asynchronous and thread-safe on both 0.8 and 0.10.
1374//
1375// NB: This fires both a stop and play event if the media was previously
1376// playing... which in some ways makes sense. And yes, this makes the video
1377// go all haywire at times - a gstreamer bug...
1378//-----------------------------------------------------------------------------
dae87f93
RN
1379bool wxGStreamerMediaBackend::SetPosition(wxLongLong where)
1380{
557002cf
VZ
1381#if GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR == 8 \
1382 && GST_VERSION_MICRO == 0
1383 // 0.8.0 has no gst_element_seek according to official docs!!!
1384 wxLogSysError(wxT("GStreamer 0.8.0 does not have gst_element_seek")
1385 wxT(" according to official docs"));
1386 return false;
1387#else // != 0.8.0
1388
1389# if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
1390 gst_element_seek (m_playbin, m_dRate, GST_FORMAT_TIME,
1391 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
1392 GST_SEEK_TYPE_SET, where.GetValue() * GST_MSECOND,
1393 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE );
1394# else
1395 // NB: Some gstreamer versions return false basically all the time
1396 // here - even totem doesn't bother to check the return value here
1397 // so I guess we'll just assume it worked -
1398 // TODO: maybe check the gst error callback???
1399 gst_element_seek (m_playbin, (GstSeekType) (GST_SEEK_METHOD_SET |
dae87f93 1400 GST_FORMAT_TIME | GST_SEEK_FLAG_FLUSH),
557002cf
VZ
1401 where.GetValue() * GST_MSECOND );
1402
1403# endif // GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
7ec69821 1404
557002cf
VZ
1405 {
1406 m_llPausedPos = where;
dae87f93 1407 return true;
7ec69821 1408 }
557002cf
VZ
1409 return true;
1410#endif //== 0.8.0
dae87f93
RN
1411}
1412
557002cf 1413//-----------------------------------------------------------------------------
dae87f93
RN
1414// wxGStreamerMediaBackend::GetDuration
1415//
1416// Obtains the total time of our stream
557002cf
VZ
1417// THREAD-UNSAFE, requires media to be paused or playing
1418//-----------------------------------------------------------------------------
0c5c0375
RN
1419wxLongLong wxGStreamerMediaBackend::GetDuration()
1420{
1421 gint64 length;
1422 GstFormat fmtTime = GST_FORMAT_TIME;
1423
557002cf
VZ
1424 if(!wxGst_element_query_duration(m_playbin, &fmtTime, &length) ||
1425 fmtTime != GST_FORMAT_TIME || length == -1)
0c5c0375
RN
1426 return 0;
1427 return length / GST_MSECOND ;
1428}
1429
557002cf 1430//-----------------------------------------------------------------------------
dae87f93
RN
1431// wxGStreamerMediaBackend::Move
1432//
1433// Called when the window is moved - GStreamer takes care of this
1434// for us so nothing is needed
557002cf 1435//-----------------------------------------------------------------------------
ea88b5fa
VZ
1436void wxGStreamerMediaBackend::Move(int WXUNUSED(x),
1437 int WXUNUSED(y),
1438 int WXUNUSED(w),
1439 int WXUNUSED(h))
0c5c0375
RN
1440{
1441}
1442
557002cf 1443//-----------------------------------------------------------------------------
dae87f93
RN
1444// wxGStreamerMediaBackend::GetVideoSize
1445//
557002cf
VZ
1446// Returns our cached video size from Load/gst_notify_caps_callback
1447// gst_x_overlay_get_desired_size also does this in 0.8...
1448//-----------------------------------------------------------------------------
0c5c0375 1449wxSize wxGStreamerMediaBackend::GetVideoSize() const
7ec69821 1450{
1e22656e 1451 return m_videoSize;
0c5c0375
RN
1452}
1453
557002cf 1454//-----------------------------------------------------------------------------
dae87f93
RN
1455// wxGStreamerMediaBackend::GetPlaybackRate
1456// wxGStreamerMediaBackend::SetPlaybackRate
1e22656e 1457//
dae87f93 1458// Obtains/Sets the playback rate of the stream
1e22656e 1459//
dae87f93
RN
1460//TODO: PlaybackRate not currently supported via playbin directly -
1461//TODO: Ronald S. Bultje noted on gstreamer-devel:
1462//TODO:
1463//TODO: Like "play at twice normal speed"? Or "play at 25 fps and 44,1 kHz"? As
1464//TODO: for the first, yes, we have elements for that, btu they"re not part of
1465//TODO: playbin. You can create a bin (with a ghost pad) containing the actual
1466//TODO: video/audiosink and the speed-changing element for this, and set that
1467//TODO: element as video-sink or audio-sink property in playbin. The
1468//TODO: audio-element is called "speed", the video-element is called "videodrop"
1469//TODO: (although that appears to be deprecated in favour of "videorate", which
1470//TODO: again cannot do this, so this may not work at all in the end). For
1471//TODO: forcing frame/samplerates, see audioscale and videorate. Audioscale is
1472//TODO: part of playbin.
557002cf
VZ
1473//
1474// In 0.10 GStreamer has new gst_element_seek API that might
1475// support this - and I've got an attempt to do so but it is untested
1476// but it would appear to work...
1477//-----------------------------------------------------------------------------
0c5c0375
RN
1478double wxGStreamerMediaBackend::GetPlaybackRate()
1479{
557002cf
VZ
1480 return m_dRate; // Could use GST_QUERY_RATE but the API doesn't seem
1481 // final on that yet and there may not be any actual
1482 // plugins that support it...
0c5c0375
RN
1483}
1484
1485bool wxGStreamerMediaBackend::SetPlaybackRate(double dRate)
1486{
557002cf
VZ
1487#if GST_VERSION_MAJOR > 0 || GST_VERSION_MINOR >= 10
1488#if 0 // not tested enough
1489 if( gst_element_seek (m_playbin, dRate, GST_FORMAT_TIME,
1490 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT),
1491 GST_SEEK_TYPE_CUR, 0,
1492 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE ) )
1493 {
1494 m_dRate = dRate;
1495 return true;
1496 }
ea88b5fa
VZ
1497#else
1498 wxUnusedVar(dRate);
557002cf
VZ
1499#endif
1500#endif
1501
1502 // failure
1503 return false;
1504}
1505
1506//-----------------------------------------------------------------------------
1507// wxGStreamerMediaBackend::GetDownloadProgress
1508//
1509// Not really outwardly possible - have been suggested that one could
1510// get the information from the component that "downloads"
1511//-----------------------------------------------------------------------------
1512wxLongLong wxGStreamerMediaBackend::GetDownloadProgress()
1513{
1514 return 0;
1515}
1516
1517//-----------------------------------------------------------------------------
1518// wxGStreamerMediaBackend::GetDownloadTotal
1519//
1520// TODO: Cache this?
1521// NB: The length changes every call for some reason due to
1522// GStreamer implementation issues
1523// THREAD-UNSAFE, requires media to be paused or playing
1524//-----------------------------------------------------------------------------
1525wxLongLong wxGStreamerMediaBackend::GetDownloadTotal()
1526{
1527 gint64 length;
1528 GstFormat fmtBytes = GST_FORMAT_BYTES;
1529
1530 if (!wxGst_element_query_duration(m_playbin, &fmtBytes, &length) ||
1531 fmtBytes != GST_FORMAT_BYTES || length == -1)
1532 return 0;
1533 return length;
1534}
1535
1536//-----------------------------------------------------------------------------
1537// wxGStreamerMediaBackend::SetVolume
1538// wxGStreamerMediaBackend::GetVolume
1539//
1540// Sets/Gets the volume through the playbin object.
1541// Note that this requires a relatively recent gst-plugins so we
1542// check at runtime to see whether it is available or not otherwise
1543// GST spits out an error on the command line
1544//-----------------------------------------------------------------------------
1545bool wxGStreamerMediaBackend::SetVolume(double dVolume)
1546{
1547 if(g_object_class_find_property(
1548 G_OBJECT_GET_CLASS(G_OBJECT(m_playbin)),
1549 "volume") != NULL)
1550 {
1551 g_object_set(G_OBJECT(m_playbin), "volume", dVolume, NULL);
1552 return true;
1553 }
1554 else
1555 {
1556 wxLogTrace(wxTRACE_GStreamer,
1557 wxT("SetVolume: volume prop not found - 0.8.5 of ")
1558 wxT("gst-plugins probably needed"));
1e22656e 1559 return false;
557002cf
VZ
1560 }
1561}
1562
1563double wxGStreamerMediaBackend::GetVolume()
1564{
1565 double dVolume = 1.0;
1566
1567 if(g_object_class_find_property(
1568 G_OBJECT_GET_CLASS(G_OBJECT(m_playbin)),
1569 "volume") != NULL)
1570 {
1571 g_object_get(G_OBJECT(m_playbin), "volume", &dVolume, NULL);
1572 }
1573 else
1574 {
1575 wxLogTrace(wxTRACE_GStreamer,
1576 wxT("GetVolume: volume prop not found - 0.8.5 of ")
1577 wxT("gst-plugins probably needed"));
1578 }
1579
1580 return dVolume;
0c5c0375
RN
1581}
1582
1583#endif //wxUSE_GSTREAMER
1584
557002cf 1585// Force link into main library so this backend can be loaded
7ec69821 1586#include "wx/html/forcelnk.h"
412e0d47 1587FORCE_LINK_ME(basewxmediabackends)
ddc90a8d
RN
1588
1589#endif //wxUSE_MEDIACTRL