mediactrl.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 806 行 · 第 1/2 页
CPP
806 行
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::OnVideoCapsReady
//
// Called by gstreamer when the video caps for the media is ready
//---------------------------------------------------------------------------
void wxGStreamerMediaBackend::OnVideoCapsReady(GstPad* pad, GParamSpec* pspec, gpointer data)
{
wxGStreamerMediaBackend::TransCapsToVideoSize((wxGStreamerMediaBackend*) data, pad);
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::DoLoad
//
// 1) Stops/Cleanups the previous instance if there is any
// 2) Creates the gstreamer playbin
// 3) If there is no playbin bail out
// 4) Set up the error and end-of-stream callbacks for our player
// 5) Make our video sink and make sure it supports the x overlay interface
// 6) Make sure the passed URI is valid and tell playbin to load it
// 7) Use the xoverlay extension to tell gstreamer to play in our window
// 8) Get the video size - pause required to set the stream in action
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::DoLoad(const wxString& locstring)
{
//1
Cleanup();
//2
m_player = gst_element_factory_make ("playbin", "play");
//3
if (!m_player)
return false;
//4
g_signal_connect (m_player, "eos", G_CALLBACK (OnFinish), this);
g_signal_connect (m_player, "error", G_CALLBACK (OnError), this);
//5
GstElement* overlay = NULL;
GstElement* videosink;
GstElement* audiosink = NULL; //NULL usually autodetects on gstreamer
#if defined(__WXGTK__) && wxUSE_DYNLIB_CLASS
//use gnome-specific gstreamer extensions
//if synthisis (?) file not found, it
//spits out a warning and uses ximagesink
wxDynamicLibrary gstgconf;
if(gstgconf.Load(gstgconf.CanonicalizeName(wxT("gstgconf-0.8"))))
{
typedef GstElement* (*LPgst_gconf_get_default_video_sink) (void);
typedef GstElement* (*LPgst_gconf_get_default_audio_sink) (void);
LPgst_gconf_get_default_video_sink pGst_gconf_get_default_video_sink =
(LPgst_gconf_get_default_video_sink)
gstgconf.GetSymbol(wxT("gst_gconf_get_default_video_sink"));
LPgst_gconf_get_default_audio_sink pGst_gconf_get_default_audio_sink =
(LPgst_gconf_get_default_audio_sink)
gstgconf.GetSymbol(wxT("gst_gconf_get_default_audio_sink"));
if (pGst_gconf_get_default_video_sink)
{
videosink = (*pGst_gconf_get_default_video_sink) ();
wxASSERT( GST_IS_BIN(videosink) );
overlay = gst_bin_get_by_interface (GST_BIN (videosink),
GST_TYPE_X_OVERLAY);
}
if (pGst_gconf_get_default_audio_sink)
{
audiosink = (*pGst_gconf_get_default_audio_sink) ();
}
gstgconf.Detach();
}
if ( ! GST_IS_X_OVERLAY(overlay) )
{
#endif
wxLogDebug(wxT("Could not load Gnome preferences, reverting to xvimagesink for video for gstreamer"));
videosink = gst_element_factory_make ("xvimagesink", "videosink");
if ( !GST_IS_OBJECT(videosink) )
videosink = gst_element_factory_make ("ximagesink", "videosink");
overlay = videosink;
wxASSERT( GST_IS_X_OVERLAY(overlay) );
if ( ! GST_IS_X_OVERLAY(overlay) )
return false;
#if defined(__WXGTK__) && wxUSE_DYNLIB_CLASS
}
#endif
g_object_set (G_OBJECT (m_player),
"video-sink", videosink,
"audio-sink", audiosink,
NULL);
//6
wxASSERT(gst_uri_protocol_is_valid("file"));
wxASSERT(gst_uri_is_valid(locstring.mb_str()));
g_object_set (G_OBJECT (m_player), "uri", (const char*)locstring.mb_str(), NULL);
//7
#ifdef __WXGTK__
if(!GTK_WIDGET_REALIZED(m_ctrl->m_wxwindow))
{
//Not realized yet - set to connect at realization time
gtk_signal_connect( GTK_OBJECT(m_ctrl->m_wxwindow),
"realize",
GTK_SIGNAL_FUNC(wxGStreamerMediaBackend::OnGTKRealize),
(gpointer) this );
}
else
{
wxYield(); //see realize callback...
GdkWindow *window = GTK_PIZZA(m_ctrl->m_wxwindow)->bin_window;
wxASSERT(window);
#endif
gst_x_overlay_set_xwindow_id( GST_X_OVERLAY(overlay),
#ifdef __WXGTK__
GDK_WINDOW_XWINDOW( window )
#else
ctrl->GetHandle()
#endif
);
#ifdef __WXGTK__
} //end else block
#endif
//8
int nResult = gst_element_set_state (m_player, GST_STATE_PAUSED);
if(nResult != GST_STATE_SUCCESS)
{
wxLogDebug(wxT("Could not set initial state to paused!"));
return false;
}
const GList *list = NULL;
g_object_get (G_OBJECT (m_player), "stream-info", &list, NULL);
bool bVideoFound = false;
for ( ; list != NULL; list = list->next)
{
GObject *info = (GObject *) list->data;
gint type;
GParamSpec *pspec;
GEnumValue *val;
GstPad *pad = NULL;
g_object_get (info, "type", &type, NULL);
pspec = g_object_class_find_property (
G_OBJECT_GET_CLASS (info), "type");
val = g_enum_get_value (G_PARAM_SPEC_ENUM (pspec)->enum_class, type);
if (strstr (val->value_name, "VIDEO"))
{
//Newer gstreamer 0.8+ is SUPPOSED to have "object"...
//but a lot of old plugins still use "pad" :)
pspec = g_object_class_find_property (
G_OBJECT_GET_CLASS (info), "object");
if (!pspec)
g_object_get (info, "pad", &pad, NULL);
else
g_object_get (info, "object", &pad, NULL);
pad = (GstPad *) GST_PAD_REALIZE (pad);
wxASSERT(pad);
if(!wxGStreamerMediaBackend::TransCapsToVideoSize(this, pad));
{
//wait for those caps to get ready
g_signal_connect(
pad,
"notify::caps",
G_CALLBACK(wxGStreamerMediaBackend::OnVideoCapsReady),
this);
}
bVideoFound = true;
break;
}//end if video
else
{
m_videoSize = wxSize(0,0);
PostRecalcSize();
}
}//end searching through info list
if(!bVideoFound)
{
wxLogDebug(wxT("No video found for gstreamer stream"));
}
m_nPausedPos = 0;
//send loaded event
wxMediaEvent theEvent(wxEVT_MEDIA_LOADED,
m_ctrl->GetId());
m_ctrl->AddPendingEvent(theEvent);
return true;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::Play
//
// Sets the stream to a playing state
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::Play()
{
if (gst_element_set_state (m_player, GST_STATE_PLAYING)
!= GST_STATE_SUCCESS)
return false;
return true;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::Pause
//
// Marks where we paused and pauses the stream
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::Pause()
{
m_nPausedPos = GetPosition();
if (gst_element_set_state (m_player, GST_STATE_PAUSED)
!= GST_STATE_SUCCESS)
return false;
return true;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::Stop
//
// Pauses the stream and sets the position to 0
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::Stop()
{
if (gst_element_set_state (m_player,
GST_STATE_PAUSED) != GST_STATE_SUCCESS)
return false;
return wxGStreamerMediaBackend::SetPosition(0);
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::GetState
//
// Gets the state of the stream
//---------------------------------------------------------------------------
wxMediaState wxGStreamerMediaBackend::GetState()
{
switch(GST_STATE(m_player))
{
case GST_STATE_PLAYING:
return wxMEDIASTATE_PLAYING;
case GST_STATE_PAUSED:
if (m_nPausedPos == 0)
return wxMEDIASTATE_STOPPED;
else
return wxMEDIASTATE_PAUSED;
default://case GST_STATE_READY:
return wxMEDIASTATE_STOPPED;
}
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::GetPosition
//
// If paused, returns our marked position - otherwise it queries the
// GStreamer playbin for the position and returns that
//
//TODO:
//TODO: In lue of the last big TODO, when you pause and seek gstreamer
//TODO: doesn't update the position sometimes, so we need to keep track of whether
//TODO: we have paused or not and keep track of the time after the pause
//TODO: and whenever the user seeks while paused
//TODO:
//---------------------------------------------------------------------------
wxLongLong wxGStreamerMediaBackend::GetPosition()
{
if(GetState() != wxMEDIASTATE_PLAYING)
return m_nPausedPos;
else
{
gint64 pos;
GstFormat fmtTime = GST_FORMAT_TIME;
if (!gst_element_query (m_player, GST_QUERY_POSITION, &fmtTime, &pos))
return 0;
return pos / GST_MSECOND ;
}
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::SetPosition
//
// Sets the position of the stream
// Note that GST_MSECOND is 1000000 (GStreamer uses nanoseconds - so
// there is 1000000 nanoseconds in a millisecond)
//
// If paused marks where we seeked to
//---------------------------------------------------------------------------
bool wxGStreamerMediaBackend::SetPosition(wxLongLong where)
{
if( gst_element_seek (m_player, (GstSeekType) (GST_SEEK_METHOD_SET |
GST_FORMAT_TIME | GST_SEEK_FLAG_FLUSH),
where.GetValue() * GST_MSECOND ) )
{
if (GetState() != wxMEDIASTATE_PLAYING)
m_nPausedPos = where;
return true;
}
return false;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::GetDuration
//
// Obtains the total time of our stream
//---------------------------------------------------------------------------
wxLongLong wxGStreamerMediaBackend::GetDuration()
{
gint64 length;
GstFormat fmtTime = GST_FORMAT_TIME;
if(!gst_element_query(m_player, GST_QUERY_TOTAL, &fmtTime, &length))
return 0;
return length / GST_MSECOND ;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::Move
//
// Called when the window is moved - GStreamer takes care of this
// for us so nothing is needed
//---------------------------------------------------------------------------
void wxGStreamerMediaBackend::Move(int x, int y, int w, int h)
{
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::GetVideoSize
//
// Returns our cached video size from Load/OnVideoCapsReady
//---------------------------------------------------------------------------
wxSize wxGStreamerMediaBackend::GetVideoSize() const
{
return m_videoSize;
}
//---------------------------------------------------------------------------
// wxGStreamerMediaBackend::GetPlaybackRate
// wxGStreamerMediaBackend::SetPlaybackRate
//
// Obtains/Sets the playback rate of the stream
//
//TODO: PlaybackRate not currently supported via playbin directly -
//TODO: Ronald S. Bultje noted on gstreamer-devel:
//TODO:
//TODO: Like "play at twice normal speed"? Or "play at 25 fps and 44,1 kHz"? As
//TODO: for the first, yes, we have elements for that, btu they"re not part of
//TODO: playbin. You can create a bin (with a ghost pad) containing the actual
//TODO: video/audiosink and the speed-changing element for this, and set that
//TODO: element as video-sink or audio-sink property in playbin. The
//TODO: audio-element is called "speed", the video-element is called "videodrop"
//TODO: (although that appears to be deprecated in favour of "videorate", which
//TODO: again cannot do this, so this may not work at all in the end). For
//TODO: forcing frame/samplerates, see audioscale and videorate. Audioscale is
//TODO: part of playbin.
//---------------------------------------------------------------------------
double wxGStreamerMediaBackend::GetPlaybackRate()
{
//not currently supported via playbin
return 1.0;
}
bool wxGStreamerMediaBackend::SetPlaybackRate(double dRate)
{
//not currently supported via playbin
return false;
}
#endif //wxUSE_GSTREAMER
//in source file that contains stuff you don't directly use
#include <wx/html/forcelnk.h>
FORCE_LINK_ME(basewxmediabackends);
#endif //wxUSE_MEDIACTRL
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?