mediactrl.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 806 行 · 第 1/2 页
CPP
806 行
/////////////////////////////////////////////////////////////////////////////
// Name: unix/mediactrl.cpp
// Purpose: Built-in Media Backends for Unix
// Author: Ryan Norton <wxprojects@comcast.net>
// Modified by:
// Created: 02/04/05
// RCS-ID: $Id: mediactrl.cpp,v 1.8.2.1 2006/03/09 00:59:48 RD Exp $
// Copyright: (c) 2004-2005 Ryan Norton
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
//===========================================================================
// DECLARATIONS
//===========================================================================
//---------------------------------------------------------------------------
// Pre-compiled header stuff
//---------------------------------------------------------------------------
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "mediactrl.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "wx/mediactrl.h"
//---------------------------------------------------------------------------
// Compilation guard
//---------------------------------------------------------------------------
#if wxUSE_MEDIACTRL
//===========================================================================
// BACKEND DECLARATIONS
//===========================================================================
//---------------------------------------------------------------------------
//
// wxGStreamerMediaBackend
//
//TODO:
//TODO: This is really not the best way to play-stop -
//TODO: it should just have one playbin and stick with it the whole
//TODO: instance of wxGStreamerMediaBackend - but stopping appears
//TODO: to invalidate the playbin object...
//TODO:
//
//---------------------------------------------------------------------------
#if wxUSE_GSTREAMER
//---------------------------------------------------------------------------
// GStreamer Includes
//---------------------------------------------------------------------------
#include <gst/gst.h>
#include <gst/xoverlay/xoverlay.h>
#include <string.h> //strstr
#include "wx/log.h"
#ifdef __WXGTK__
//for <gdk/gdkx.h>/related for GDK_WINDOW_XWINDOW
# include "wx/gtk/win_gtk.h"
# include <gtk/gtksignal.h>
# if wxUSE_DYNLIB_CLASS
# include "wx/dynlib.h"
# endif
//# include <gst/gconf/gconf.h> //gstreamer gnome interface - needs deps
#endif
class WXDLLIMPEXP_MEDIA wxGStreamerMediaBackend : public wxMediaBackend
{
public:
wxGStreamerMediaBackend();
~wxGStreamerMediaBackend();
virtual bool CreateControl(wxControl* ctrl, wxWindow* parent,
wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style,
const wxValidator& validator,
const wxString& name);
virtual bool Play();
virtual bool Pause();
virtual bool Stop();
virtual bool Load(const wxString& fileName);
virtual bool Load(const wxURI& location);
virtual wxMediaState GetState();
virtual bool SetPosition(wxLongLong where);
virtual wxLongLong GetPosition();
virtual wxLongLong GetDuration();
virtual void Move(int x, int y, int w, int h);
wxSize GetVideoSize() const;
virtual double GetPlaybackRate();
virtual bool SetPlaybackRate(double dRate);
void Cleanup();
bool DoLoad(const wxString& locstring);
static void OnFinish(GstElement *play, gpointer data);
static void OnError (GstElement *play, GstElement *src,
GError *err, gchar *debug,
gpointer data);
static void OnVideoCapsReady(GstPad* pad, GParamSpec* pspec, gpointer data);
static bool TransCapsToVideoSize(wxGStreamerMediaBackend* be, GstPad* caps);
void PostRecalcSize();
#ifdef __WXGTK__
static gint OnGTKRealize(GtkWidget* theWidget, wxGStreamerMediaBackend* be);
#endif
GstElement* m_player; //GStreamer media element
wxSize m_videoSize;
wxControl* m_ctrl;
wxLongLong m_nPausedPos;
DECLARE_DYNAMIC_CLASS(wxGStreamerMediaBackend);
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// wxGStreamerMediaBackend
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
IMPLEMENT_DYNAMIC_CLASS(wxGStreamerMediaBackend, wxMediaBackend);
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend Constructor
//
// Sets m_player to NULL signifying we havn't loaded anything yet
//---------------------------------------------------------------------------
wxGStreamerMediaBackend::wxGStreamerMediaBackend() : m_player(NULL), m_videoSize(0,0)
{
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend Destructor
//
// Stops/cleans up memory
//---------------------------------------------------------------------------
wxGStreamerMediaBackend::~wxGStreamerMediaBackend()
{
Cleanup();
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::OnGTKRealize
//
// If the window wasn't realized when Load was called, this is the
// callback for when it is.
//
// 1) Installs GTK idle handler if it doesn't exist
// 2) Yeilds to avoid an X11 bug (?)
// 3) Tells GStreamer to play the video in our control
//---------------------------------------------------------------------------
#ifdef __WXGTK__
#ifdef __WXDEBUG__
#if wxUSE_THREADS
# define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
#else
# define DEBUG_MAIN_THREAD
#endif
#else
#define DEBUG_MAIN_THREAD
#endif // Debug
extern void wxapp_install_idle_handler();
extern bool g_isIdle;
extern bool g_mainThreadLocked;
gint wxGStreamerMediaBackend::OnGTKRealize(GtkWidget* theWidget,
wxGStreamerMediaBackend* be)
{
DEBUG_MAIN_THREAD
if (g_isIdle)
wxapp_install_idle_handler();
wxYield(); //FIXME: X Server gets an error if I don't do this or a messagebox beforehand?!?!??
GdkWindow *window = GTK_PIZZA(theWidget)->bin_window;
wxASSERT(window);
GstElement* videosink;
g_object_get (G_OBJECT (be->m_player), "video-sink", &videosink, NULL);
GstElement* overlay = gst_bin_get_by_interface (GST_BIN (videosink),
GST_TYPE_X_OVERLAY);
gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(overlay),
GDK_WINDOW_XWINDOW( window )
);
return 0;
}
#endif
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::Cleanup
//
// Frees the gstreamer interfaces if there were any created
//---------------------------------------------------------------------------
void wxGStreamerMediaBackend::Cleanup()
{
if(m_player && GST_IS_OBJECT(m_player))
{
gst_element_set_state (m_player, GST_STATE_NULL);
gst_object_unref (GST_OBJECT (m_player));
}
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::CreateControl
//
// Initializes GStreamer and creates the wx side of our media control
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::CreateControl(wxControl* ctrl, wxWindow* parent,
wxWindowID id,
const wxPoint& pos,
const wxSize& size,
long style,
const wxValidator& validator,
const wxString& name)
{
//init gstreamer
gst_init(NULL, NULL);
m_ctrl = ctrl;
return m_ctrl->wxControl::Create(parent, id, pos, size,
style, //remove borders???
validator, name);
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::TransCapsToVideoSize
//
// Gets the size of our video (in wxSize) from a GstPad
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::TransCapsToVideoSize(wxGStreamerMediaBackend* be, GstPad* pad)
{
const GstCaps* caps = GST_PAD_CAPS (pad);
if(caps)
{
const GstStructure *s;
s = gst_caps_get_structure (caps, 0);
wxASSERT(s);
gst_structure_get_int (s, "width", &be->m_videoSize.x);
gst_structure_get_int (s, "height", &be->m_videoSize.y);
wxLogDebug(wxT("Native video size: [%i,%i]"), be->m_videoSize.x, be->m_videoSize.y);
const GValue *par;
par = gst_structure_get_value (s, "pixel-aspect-ratio");
if (par)
{
int num = par->data[0].v_int,
den = par->data[1].v_int;
//TODO: maybe better fraction normalization...
if (num > den)
be->m_videoSize.x = (int) ((float) num * be->m_videoSize.x / den);
else
be->m_videoSize.y = (int) ((float) den * be->m_videoSize.y / num);
}
wxLogDebug(wxT("Adjusted video size: [%i,%i]"), be->m_videoSize.x, be->m_videoSize.y);
be->PostRecalcSize();
return true;
}//end if caps
return false;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::PostRecalcSize
//
// Forces parent to recalc its layout if it has sizers to update
// to the new video size
//---------------------------------------------------------------------------
void wxGStreamerMediaBackend::PostRecalcSize()
{
m_ctrl->InvalidateBestSize();
m_ctrl->GetParent()->Layout();
m_ctrl->GetParent()->Refresh();
m_ctrl->GetParent()->Update();
m_ctrl->SetSize(m_ctrl->GetSize());
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::OnFinish
//
// Called by gstreamer when the media is done playing
//
// 1) Send a wxEVT_MEDIA_STOP to the control
// 2) If veteod, break out
// 3) really stop the media
// 4) Send a wxEVT_MEDIA_FINISHED to the control
//---------------------------------------------------------------------------
void wxGStreamerMediaBackend::OnFinish(GstElement *play, gpointer data)
{
wxGStreamerMediaBackend* m_parent = (wxGStreamerMediaBackend*) data;
wxMediaEvent theEvent(wxEVT_MEDIA_STOP,
m_parent->m_ctrl->GetId());
m_parent->m_ctrl->ProcessEvent(theEvent);
if(theEvent.IsAllowed())
{
bool bOk = m_parent->Stop();
wxASSERT(bOk);
//send the event to our child
wxMediaEvent theEvent(wxEVT_MEDIA_FINISHED,
m_parent->m_ctrl->GetId());
m_parent->m_ctrl->ProcessEvent(theEvent);
}
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::OnError
//
// Called by gstreamer when an error is encountered playing the media
//
// TODO: Make this better - maybe some more intelligent wxLog stuff
//---------------------------------------------------------------------------
void wxGStreamerMediaBackend::OnError(GstElement *play,
GstElement *src,
GError *err,
gchar *debug,
gpointer data)
{
wxLogSysError(
wxString::Format(
wxT("Error in wxMediaCtrl!\nError Message:%s\nDebug:%s\n"),
(const wxChar*)wxConvUTF8.cMB2WX(err->message),
(const wxChar*)wxConvUTF8.cMB2WX(debug)
)
);
}
//-----------------------------------------------------------------------------
// wxGStreamerMediaBackend::Load (File version)
//
// Just calls DoLoad() with a prepended file scheme
//-----------------------------------------------------------------------------
bool wxGStreamerMediaBackend::Load(const wxString& fileName)
{
return DoLoad(wxString( wxT("file://") ) + fileName);
}
//-----------------------------------------------------------------------------
// wxGStreamerMediaBackend::Load (URI version)
//
// In the case of a file URI passes it unencoded -
// also, as of 0.10.3 and earlier GstURI (the uri parser for gstreamer)
// is sort of broken and only accepts uris with at least two slashes
// after the scheme (i.e. file: == not ok, file:// == ok)
//-----------------------------------------------------------------------------
bool wxGStreamerMediaBackend::Load(const wxURI& location)
{
if(location.GetScheme().CmpNoCase(wxT("file")) == 0)
{
wxString uristring = location.BuildUnescapedURI();
//Workaround GstURI leading "//" problem and make sure it leads
//with that
return DoLoad(wxString(wxT("file://")) +
uristring.Right(uristring.Length() - 5)
);
}
else
return DoLoad(location.BuildURI());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?