remove superflous comments
[wxWidgets.git] / src / unix / mediactrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/mediactrl.cpp
3 // Purpose: Built-in Media Backends for Unix
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
12 //===========================================================================
13 // DECLARATIONS
14 //===========================================================================
15
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mediactrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 //---------------------------------------------------------------------------
32 // Includes
33 //---------------------------------------------------------------------------
34 #include "wx/mediactrl.h"
35
36 //---------------------------------------------------------------------------
37 // Compilation guard
38 //---------------------------------------------------------------------------
39 #if wxUSE_MEDIACTRL
40
41 //===========================================================================
42 // BACKEND DECLARATIONS
43 //===========================================================================
44
45 //---------------------------------------------------------------------------
46 //
47 // wxGStreamerMediaBackend
48 //
49 // Uses nanoseconds...
50 //---------------------------------------------------------------------------
51 #if wxUSE_GSTREAMER
52
53 //---------------------------------------------------------------------------
54 // GStreamer Includes
55 //---------------------------------------------------------------------------
56 #include <gst/gst.h>
57 #include <gst/xoverlay/xoverlay.h>
58
59 #include <string.h> //strstr
60
61 #include "wx/log.h"
62 #include "wx/msgdlg.h"
63
64 #ifdef __WXGTK__
65 //for <gdk/gdkx.h>/related for GDK_WINDOW_XWINDOW
66 # include "wx/gtk/win_gtk.h"
67 # include <gtk/gtksignal.h>
68 #endif
69
70 //FIXME:
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...
75 //FIXME:
76
77 class WXDLLIMPEXP_MEDIA wxGStreamerMediaBackend : public wxMediaBackend
78 {
79 public:
80
81 wxGStreamerMediaBackend();
82 ~wxGStreamerMediaBackend();
83
84 virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
85 wxWindowID id,
86 const wxPoint& pos,
87 const wxSize& size,
88 long style,
89 const wxValidator& validator,
90 const wxString& name);
91
92 virtual bool Play();
93 virtual bool Pause();
94 virtual bool Stop();
95
96 virtual bool Load(const wxString& fileName);
97 virtual bool Load(const wxURI& location);
98
99 virtual wxMediaState GetState();
100
101 virtual bool SetPosition(wxLongLong where);
102 virtual wxLongLong GetPosition();
103 virtual wxLongLong GetDuration();
104
105 virtual void Move(int x, int y, int w, int h);
106 wxSize GetVideoSize() const;
107
108 virtual double GetPlaybackRate();
109 virtual bool SetPlaybackRate(double dRate);
110
111 void Cleanup();
112
113 static void OnFinish(GstElement *play, gpointer data);
114 static void OnError (GstElement *play, GstElement *src,
115 GError *err, gchar *debug,
116 gpointer data);
117 static void OnVideoCapsReady(GstPad* pad, GParamSpec* pspec, gpointer data);
118
119 static bool TransCapsToVideoSize(wxGStreamerMediaBackend* be, GstPad* caps);
120 void PostRecalcSize();
121
122 #ifdef __WXGTK__
123 static gint OnGTKRealize(GtkWidget* theWidget, wxGStreamerMediaBackend* be);
124 #endif
125
126 GstElement* m_player; //GStreamer media element
127 GstElement* m_audiosink;
128 GstElement* m_videosink;
129
130 wxSize m_videoSize;
131 wxControl* m_ctrl;
132
133 //FIXME:
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
138 //FIXME:
139 wxLongLong m_nPausedPos;
140
141 DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend);
142 };
143
144
145 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
146 //
147 // wxGStreamerMediaBackend
148 //
149 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
150
151 IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend, wxMediaBackend);
152
153 wxGStreamerMediaBackend::wxGStreamerMediaBackend() : m_player(NULL), m_videoSize(0,0)
154 {
155 }
156
157 wxGStreamerMediaBackend::~wxGStreamerMediaBackend()
158 {
159 Cleanup();
160 }
161
162 #ifdef __WXGTK__
163
164 #ifdef __WXDEBUG__
165
166 #if wxUSE_THREADS
167 # define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
168 #else
169 # define DEBUG_MAIN_THREAD
170 #endif
171 #else
172 #define DEBUG_MAIN_THREAD
173 #endif // Debug
174
175 extern void wxapp_install_idle_handler();
176 extern bool g_isIdle;
177 extern bool g_mainThreadLocked;
178
179 gint wxGStreamerMediaBackend::OnGTKRealize(GtkWidget* theWidget,
180 wxGStreamerMediaBackend* be)
181 {
182 DEBUG_MAIN_THREAD
183
184 if (g_isIdle)
185 wxapp_install_idle_handler();
186
187 wxYield(); //X Server gets an error if I don't do this or a messagebox beforehand?!?!??
188
189 GdkWindow *window = GTK_PIZZA(theWidget)->bin_window;
190 wxASSERT(window);
191
192 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(be->m_videosink),
193 GDK_WINDOW_XWINDOW( window )
194 );
195
196 return 0;
197 }
198
199
200 #endif
201
202 void wxGStreamerMediaBackend::Cleanup()
203 {
204 if(m_player && GST_IS_OBJECT(m_player))
205 {
206 // wxASSERT(GST_IS_OBJECT(m_audiosink));
207 // wxASSERT(GST_IS_OBJECT(m_videosink));
208
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));
213 }
214 }
215
216 bool wxGStreamerMediaBackend::CreateControl(wxControl* ctrl, wxWindow* parent,
217 wxWindowID id,
218 const wxPoint& pos,
219 const wxSize& size,
220 long style,
221 const wxValidator& validator,
222 const wxString& name)
223 {
224 //init gstreamer
225 gst_init(NULL, NULL);
226
227 m_ctrl = ctrl;
228
229 return m_ctrl->wxControl::Create(parent, id, pos, size,
230 style, //remove borders???
231 validator, name);
232 }
233
234 bool wxGStreamerMediaBackend::TransCapsToVideoSize(wxGStreamerMediaBackend* be, GstPad* pad)
235 {
236 const GstCaps* caps = GST_PAD_CAPS (pad);
237 if(caps)
238 {
239
240 const GstStructure *s;
241 s = gst_caps_get_structure (caps, 0);
242 wxASSERT(s);
243
244 gst_structure_get_int (s, "width", &be->m_videoSize.x);
245 gst_structure_get_int (s, "height", &be->m_videoSize.y);
246
247 const GValue *par;
248 par = gst_structure_get_value (s, "pixel-aspect-ratio");
249
250 if (par)
251 {
252 int num = gst_value_get_fraction_numerator (par),
253 den = gst_value_get_fraction_denominator (par);
254
255 //TODO: maybe better fraction normalization...
256 if (num > den)
257 be->m_videoSize.x = (int) ((float) num * be->m_videoSize.x / den);
258 else
259 be->m_videoSize.y = (int) ((float) den * be->m_videoSize.y / num);
260 }
261
262 be->PostRecalcSize();
263 return true;
264 }//end if caps
265
266 return false;
267 }
268
269 //forces parent to recalc its layout if it has sizers to update
270 //to the new video size
271 void wxGStreamerMediaBackend::PostRecalcSize()
272 {
273 m_ctrl->InvalidateBestSize();
274 m_ctrl->GetParent()->Layout();
275 m_ctrl->GetParent()->Refresh();
276 m_ctrl->GetParent()->Update();
277 }
278
279 void wxGStreamerMediaBackend::OnFinish(GstElement *play, gpointer data)
280 {
281 wxGStreamerMediaBackend* m_parent = (wxGStreamerMediaBackend*) data;
282
283 wxMediaEvent theEvent(wxEVT_MEDIA_STOP,
284 m_parent->m_ctrl->GetId());
285 m_parent->m_ctrl->ProcessEvent(theEvent);
286
287 if(theEvent.IsAllowed())
288 {
289 bool bOk = m_parent->Stop();
290 wxASSERT(bOk);
291
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);
296 }
297 }
298
299 void wxGStreamerMediaBackend::OnError(GstElement *play,
300 GstElement *src,
301 GError *err,
302 gchar *debug,
303 gpointer data)
304 {
305 wxMessageBox(wxString::Format(wxT("Error in wxMediaCtrl!\nError Message:%s"), wxString(err->message, wxConvLocal).c_str()));
306 }
307
308
309 bool wxGStreamerMediaBackend::Load(const wxString& fileName)
310 {
311 return Load(
312 wxURI(
313 wxString( wxT("file://") ) + fileName
314 )
315 );
316 }
317
318 void wxGStreamerMediaBackend::OnVideoCapsReady(GstPad* pad, GParamSpec* pspec, gpointer data)
319 {
320 wxGStreamerMediaBackend::TransCapsToVideoSize((wxGStreamerMediaBackend*) data, pad);
321 }
322
323 bool wxGStreamerMediaBackend::Load(const wxURI& location)
324 {
325 Cleanup();
326
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");
330
331 //no playbin -- outta here :)
332 if (!m_player)
333 return false;
334
335 //have alsa?
336 if (GST_IS_OBJECT(m_audiosink) == false)
337 {
338 //nope, try OSS
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"));
341 }
342
343
344 wxASSERT_MSG(GST_IS_OBJECT(m_videosink), wxT("WARNING: No X video driver for gstreamer not found - video will be unavailable for wxMediaCtrl"));
345
346 g_object_set (G_OBJECT (m_player),
347 "video-sink", m_videosink,
348 "audio-sink", m_audiosink,
349 NULL);
350
351 g_signal_connect (m_player, "eos", G_CALLBACK (OnError), this);
352 g_signal_connect (m_player, "error", G_CALLBACK (OnFinish), this);
353
354 wxASSERT( GST_IS_X_OVERLAY(m_videosink) );
355 if ( ! GST_IS_X_OVERLAY(m_videosink) )
356 return false;
357
358 wxString locstring = location.BuildUnescapedURI();
359 wxASSERT(gst_uri_protocol_is_valid("file"));
360 wxASSERT(gst_uri_is_valid(locstring.mb_str()));
361
362 g_object_set (G_OBJECT (m_player), "uri", (const char*)locstring.mb_str(), NULL);
363
364 #ifdef __WXGTK__
365 if(!GTK_WIDGET_REALIZED(m_ctrl->m_wxwindow))
366 {
367 //Not realized yet - set to connect at realization time
368 gtk_signal_connect( GTK_OBJECT(m_ctrl->m_wxwindow),
369 "realize",
370 GTK_SIGNAL_FUNC(wxGStreamerMediaBackend::OnGTKRealize),
371 (gpointer) this );
372 }
373 else
374 {
375 wxYield(); //see realize callback...
376 GdkWindow *window = GTK_PIZZA(m_ctrl->m_wxwindow)->bin_window;
377 wxASSERT(window);
378 #endif
379
380
381 gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(m_videosink),
382 #ifdef __WXGTK__
383 GDK_WINDOW_XWINDOW( window )
384 #else
385 ctrl->GetHandle()
386 #endif
387 );
388
389 #ifdef __WXGTK__
390 } //end else block
391 #endif
392
393 wxASSERT(gst_element_set_state (m_player,
394 GST_STATE_PAUSED) == GST_STATE_SUCCESS);
395
396 const GList *list = NULL;
397 g_object_get (G_OBJECT (m_player), "stream-info", &list, NULL);
398
399 for ( ; list != NULL; list = list->next)
400 {
401 GObject *info = (GObject *) list->data;
402 gint type;
403 GParamSpec *pspec;
404 GEnumValue *val;
405 GstPad *pad = NULL;
406
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);
411
412 if (strstr (val->value_name, "VIDEO"))
413 {
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");
418
419 if (!pspec)
420 g_object_get (info, "pad", &pad, NULL);
421 else
422 g_object_get (info, "object", &pad, NULL);
423
424 pad = (GstPad *) GST_PAD_REALIZE (pad);
425 wxASSERT(pad);
426
427 if(!wxGStreamerMediaBackend::TransCapsToVideoSize(this, pad));
428 {
429 //wait for those caps to get ready
430 g_signal_connect(
431 pad,
432 "notify::caps",
433 G_CALLBACK(wxGStreamerMediaBackend::OnVideoCapsReady),
434 this);
435 }
436 }//end if video
437 else
438 {
439 m_videoSize = wxSize(0,0);
440 PostRecalcSize();
441 }
442 }//end searching through info list
443
444 m_nPausedPos = 0;
445 return true;
446 }
447
448 bool wxGStreamerMediaBackend::Play()
449 {
450 if (gst_element_set_state (m_player, GST_STATE_PLAYING)
451 != GST_STATE_SUCCESS)
452 return false;
453 return true;
454 }
455
456 bool wxGStreamerMediaBackend::Pause()
457 {
458 m_nPausedPos = GetPosition();
459 if (gst_element_set_state (m_player, GST_STATE_PAUSED)
460 != GST_STATE_SUCCESS)
461 return false;
462 return true;
463 }
464
465 bool wxGStreamerMediaBackend::Stop()
466 {
467 if (gst_element_set_state (m_player,
468 GST_STATE_PAUSED) != GST_STATE_SUCCESS)
469 return false;
470 return wxGStreamerMediaBackend::SetPosition(0);
471 }
472
473 wxMediaState wxGStreamerMediaBackend::GetState()
474 {
475 switch(GST_STATE(m_player))
476 {
477 case GST_STATE_PLAYING:
478 return wxMEDIASTATE_PLAYING;
479 case GST_STATE_PAUSED:
480 if (m_nPausedPos == 0)
481 return wxMEDIASTATE_STOPPED;
482 else
483 return wxMEDIASTATE_PAUSED;
484 default://case GST_STATE_READY:
485 return wxMEDIASTATE_STOPPED;
486 }
487 }
488
489 bool wxGStreamerMediaBackend::SetPosition(wxLongLong where)
490 {
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 ) )
494 {
495 if (GetState() != wxMEDIASTATE_PLAYING)
496 m_nPausedPos = where;
497
498 return true;
499 }
500
501 return false;
502 }
503
504 wxLongLong wxGStreamerMediaBackend::GetPosition()
505 {
506 if(GetState() != wxMEDIASTATE_PLAYING)
507 return m_nPausedPos;
508 else
509 {
510 gint64 pos;
511 GstFormat fmtTime = GST_FORMAT_TIME;
512
513 if (!gst_element_query (m_player, GST_QUERY_POSITION, &fmtTime, &pos))
514 return 0;
515 return pos / GST_MSECOND ;
516 }
517 }
518
519 wxLongLong wxGStreamerMediaBackend::GetDuration()
520 {
521 gint64 length;
522 GstFormat fmtTime = GST_FORMAT_TIME;
523
524 if(!gst_element_query(m_player, GST_QUERY_TOTAL, &fmtTime, &length))
525 return 0;
526 return length / GST_MSECOND ;
527 }
528
529 void wxGStreamerMediaBackend::Move(int x, int y, int w, int h)
530 {
531 }
532
533 wxSize wxGStreamerMediaBackend::GetVideoSize() const
534 {
535 return m_videoSize;
536 }
537
538 //
539 //PlaybackRate not currently supported via playbin directly -
540 // Ronald S. Bultje noted on gstreamer-devel:
541 //
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
551 // part of playbin.
552 //
553
554 double wxGStreamerMediaBackend::GetPlaybackRate()
555 {
556 //not currently supported via playbin
557 return 1.0;
558 }
559
560 bool wxGStreamerMediaBackend::SetPlaybackRate(double dRate)
561 {
562 //not currently supported via playbin
563 return false;
564 }
565
566 #endif //wxUSE_GSTREAMER
567
568 //in source file that contains stuff you don't directly use
569 #include <wx/html/forcelnk.h>
570 FORCE_LINK_ME(basewxmediabackends);
571
572 #endif //wxUSE_MEDIACTRL
573
574
575
576
577