1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
#pragma once
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <gst/gst.h>
#include <gst/pbutils/missing-plugins.h>
#include <iomanip>
#include <iostream>
#include "gstreamer_utility.h"
#include "utility.h"
// Class to help with gstreamer(mm)
class MediaDecoder : virtual public sigc::trackable {
public:
explicit MediaDecoder(guint timeout = 0) : m_watch_id(0), m_pipeline(nullptr), m_timeout(timeout) {
}
virtual ~MediaDecoder() {
destroy_pipeline();
}
static void static_on_pad_added(GstElement* src, GstPad* pad, void* data) {
MediaDecoder* md = static_cast<MediaDecoder*>(data);
md->on_pad_added(src, pad);
}
static gboolean static_handle_message(GstBus* bus, GstMessage* msg, void* data) {
MediaDecoder* md = static_cast<MediaDecoder*>(data);
return md->on_bus_message(bus, msg);
}
void create_pipeline(const Glib::ustring& uri) {
se_dbg_msg(SE_DBG_PLUGINS, "uri=%s", uri.c_str());
if (m_pipeline)
destroy_pipeline();
m_pipeline = gst_pipeline_new("pipeline");
GstElement* giosrc = gst_element_factory_make("giosrc", NULL);
GstElement* decodebin = gst_element_factory_make("decodebin", "decoder");
g_signal_connect(decodebin, "pad-added", G_CALLBACK(static_on_pad_added), this);
gst_bin_add_many(GST_BIN(m_pipeline), giosrc, decodebin, NULL);
if (!gst_element_link(giosrc, decodebin)) {
g_printerr("Elements could not be linked.\n");
gst_object_unref(m_pipeline);
return;
}
g_object_set(giosrc, "location", uri.c_str(), NULL);
// Bus watching
GstBus* bus = gst_element_get_bus(m_pipeline);
m_watch_id = gst_bus_add_watch(bus, (GstBusFunc)static_handle_message, this);
gst_object_unref(bus);
if (gst_element_set_state(m_pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
se_dbg_msg(SE_DBG_PLUGINS, "Failed to change the state of the pipeline to PLAYING");
}
}
void destroy_pipeline() {
se_dbg(SE_DBG_PLUGINS);
if (m_connection_timeout)
m_connection_timeout.disconnect();
if (m_pipeline) {
g_source_remove(m_watch_id);
gst_element_set_state(m_pipeline, GST_STATE_NULL);
g_object_unref(m_pipeline);
}
m_watch_id = 0;
m_pipeline = nullptr;
}
virtual void on_pad_added(GstElement*, GstPad* newpad) {
se_dbg(SE_DBG_PLUGINS);
GstCaps* caps = gst_pad_query_caps(newpad, NULL);
se_dbg_msg(SE_DBG_PLUGINS, "newpad->caps: %s", GST_PAD_NAME(caps));
GstStructure* structure = gst_caps_get_structure(caps, 0);
if (!structure) {
// FIXME: TODO
// FIXME: unref at the end too
gst_caps_unref(caps);
return;
}
const gchar* structure_name = gst_structure_get_name(structure);
GstElement* sink = create_element(structure_name);
// FIXME: unref sink after ?
if (sink) {
// Add bin to the pipeline
gst_bin_add_many(GST_BIN(m_pipeline), sink, NULL);
// Set the new sink tp PAUSED as well
GstStateChangeReturn retst = gst_element_set_state(sink, GST_STATE_PAUSED);
if (retst == GST_STATE_CHANGE_FAILURE) {
std::cerr << "Could not change state of new sink: " << retst << std::endl;
se_dbg_msg(SE_DBG_PLUGINS, "Could not change the state of new sink");
gst_bin_remove(GST_BIN(m_pipeline), sink);
return;
}
// Get the ghostpad of the sink bin
GstPad* sinkpad = gst_element_get_static_pad(sink, "sink");
GstPadLinkReturn ret = gst_pad_link(newpad, sinkpad);
if (ret != GST_PAD_LINK_OK && ret != GST_PAD_LINK_WAS_LINKED) {
std::cerr << "Linking of pads " << GST_PAD_NAME(newpad) << " and " << GST_PAD_NAME(sinkpad) << " failed." << std::endl;
se_dbg_msg(SE_DBG_PLUGINS, "Linking of pads failed");
} else {
se_dbg_msg(SE_DBG_PLUGINS, "Pads linking with success");
}
} else {
se_dbg_msg(SE_DBG_PLUGINS, "create_element return an NULL sink");
}
}
// BUS MESSAGE
virtual bool on_bus_message(GstBus*, GstMessage* msg) {
se_dbg_msg(SE_DBG_PLUGINS, "type='%s' name='%s'", GST_MESSAGE_TYPE_NAME(msg), GST_OBJECT_NAME(GST_MESSAGE_SRC(msg)));
GstMessageType msg_type = GST_MESSAGE_TYPE(msg);
switch (msg_type) {
case GST_MESSAGE_ELEMENT:
return on_bus_message_element(msg);
case GST_MESSAGE_EOS:
return on_bus_message_eos(msg);
case GST_MESSAGE_ERROR:
return on_bus_message_error(msg);
case GST_MESSAGE_WARNING:
return on_bus_message_warning(msg);
case GST_MESSAGE_STATE_CHANGED:
return on_bus_message_state_changed(msg);
default:
break;
}
return true;
}
virtual bool on_bus_message_error(GstMessage* msg) {
check_missing_plugins();
GError* err = nullptr;
gchar* err_dbg = nullptr;
gst_message_parse_error(msg, &err, &err_dbg);
g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
g_printerr("Debugging information: %s\n", err_dbg ? err_dbg : "none");
// FIXME: dialog
// dialog_error(build_message(_("Media file could not be played.\n%s"), err->message));
g_clear_error(&err);
g_free(err_dbg);
// Critical error, cancel the work.
on_work_cancel();
return true;
}
virtual bool on_bus_message_warning(GstMessage* msg) {
check_missing_plugins();
GError* err = nullptr;
gchar* err_dbg = nullptr;
gst_message_parse_error(msg, &err, &err_dbg);
g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
g_printerr("Debugging information: %s\n", err_dbg ? err_dbg : "none");
// FIXME: dialog
// dialog_error(build_message(_("Media file could not be played.\n%s"), err->message));
g_clear_error(&err);
g_free(err_dbg);
return true;
}
virtual bool on_bus_message_state_changed(GstMessage* msg) {
if (m_timeout > 0)
return on_bus_message_state_changed_timeout(msg);
return true;
}
virtual bool on_bus_message_eos(GstMessage*) {
gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
on_work_finished();
return true;
}
virtual bool on_bus_message_element(GstMessage* msg) {
check_missing_plugin_message(msg);
return true;
}
virtual void on_work_finished() {
// FIXME
}
virtual void on_work_cancel() {
// FIXME
}
virtual GstElement* create_element(const Glib::ustring&) {
return nullptr;
}
virtual bool on_timeout() {
return false;
}
// utility
Glib::ustring time_to_string(gint64 time) {
if (!GST_CLOCK_TIME_IS_VALID(time)) {
return "0:00:000";
}
auto h = time / (GST_SECOND * 60 * 60);
auto m = (time / (GST_SECOND * 60)) % 60;
auto s = (time / GST_SECOND) % 60;
return build_message("%02u:%02u:%03u", h, m, s);
}
protected:
bool on_bus_message_state_changed_timeout(GstMessage* msg) {
se_dbg(SE_DBG_PLUGINS);
// We only update when it is the pipeline object
if (GST_MESSAGE_SRC(msg) != GST_OBJECT(m_pipeline))
return true;
GstState old_state, new_state, pending;
gst_message_parse_state_changed(msg, &old_state, &new_state, &pending);
if (old_state == GST_STATE_PAUSED && new_state == GST_STATE_PLAYING) {
if (!m_connection_timeout) {
m_connection_timeout = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MediaDecoder::on_timeout), m_timeout);
}
} else if (old_state == GST_STATE_PLAYING && new_state == GST_STATE_PAUSED) {
if (m_connection_timeout) {
m_connection_timeout.disconnect();
}
}
return true;
}
void check_missing_plugin_message(GstMessage* msg) {
se_dbg(SE_DBG_PLUGINS);
if (!msg)
return;
if (!gst_is_missing_plugin_message(msg))
return;
gchar* description = gst_missing_plugin_message_get_description(msg);
if (!description)
return;
se_dbg_msg(SE_DBG_PLUGINS, "missing plugin msg '%s'", description);
m_missing_plugins.push_back(description);
g_free(description);
return;
}
bool check_missing_plugins() {
if (m_missing_plugins.empty())
return false;
dialog_missing_plugins(m_missing_plugins);
m_missing_plugins.clear();
return true;
}
// Display a message for missing plugins.
void dialog_missing_plugins(const std::list<Glib::ustring>& list) {
Glib::ustring plugins;
std::list<Glib::ustring>::const_iterator it = list.begin();
std::list<Glib::ustring>::const_iterator end = list.end();
while (it != end) {
plugins += *it;
plugins += "\n";
++it;
}
Glib::ustring msg =
_("GStreamer plugins missing.\n"
"The playback of this movie requires the following decoders which are not installed:");
dialog_error(msg, plugins);
se_dbg_msg(SE_DBG_UTILITY, "%s %s", msg.c_str(), plugins.c_str());
}
protected:
guint m_watch_id;
GstElement* m_pipeline;
// timeout
guint m_timeout;
sigc::connection m_connection_timeout;
std::list<Glib::ustring> m_missing_plugins;
};
|