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
|
/* GStreamer
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gst/gst.h>
#include <gst/video/video.h>
#include <stdlib.h>
#include <string>
#include <mutex>
#include "../key-handler.h"
static GMainLoop *loop = nullptr;
static std::mutex input_lock;
static gint par = 1;
static gint fps = 30;
static gboolean set_hdr10 = FALSE;
static GstElement *setter = nullptr;
static gboolean updated = FALSE;
static void
print_keyboard_help (void)
{
static struct
{
const gchar *key_desc;
const gchar *key_help;
} key_controls[] = {
{
"q", "Quit"}, {
"right arrow", "Increase framerate"}, {
"left arrow", "Decrease framerate"}, {
"up arrow", "Increase pixel-aspect-ratio"}, {
"down arrow", "Decrease pixel-aspect-ratio"}, {
"m", "Toggle HDR10 metadata"}, {
"k", "show keyboard shortcuts"}
};
guint i, chars_to_pad, desc_len, max_desc_len = 0;
gst_print ("\n\n%s\n\n", "Keyboard controls:");
for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
desc_len = g_utf8_strlen (key_controls[i].key_desc, -1);
max_desc_len = MAX (max_desc_len, desc_len);
}
++max_desc_len;
for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
chars_to_pad = max_desc_len - g_utf8_strlen (key_controls[i].key_desc, -1);
gst_print ("\t%s", key_controls[i].key_desc);
gst_print ("%-*s: ", chars_to_pad, "");
gst_print ("%s\n", key_controls[i].key_help);
}
gst_print ("\n");
}
static void
keyboard_cb (gchar input, gboolean is_ascii, gpointer user_data)
{
std::lock_guard<std::mutex> lk (input_lock);
if (!is_ascii) {
switch (input) {
case KB_ARROW_UP:
par++;
updated = TRUE;
gst_println ("Increasing pixel-aspect-ratio to %d", par);
break;
case KB_ARROW_DOWN:
if (par == 1)
return;
par--;
updated = TRUE;
gst_println ("Decreasing pixel-aspect-ratio to %d", par);
break;
case KB_ARROW_RIGHT:
fps++;
updated = TRUE;
gst_println ("Increasing framerate to %d", fps);
break;
case KB_ARROW_LEFT:
if (fps == 1)
return;
fps--;
updated = TRUE;
gst_println ("Decreasing framerate to %d", fps);
break;
default:
break;
}
} else {
switch (input) {
case 'k':
case 'K':
print_keyboard_help ();
break;
case 'q':
case 'Q':
g_main_loop_quit (loop);
break;
case 'm':
case 'M':
set_hdr10 = !set_hdr10;
updated = TRUE;
gst_println ("%sable HDR10 metadata", set_hdr10 ? "En" : "Dis");
break;
default:
break;
}
}
if (updated && setter) {
GstPad *pad = gst_element_get_static_pad (setter, "sink");
GstCaps *caps = gst_pad_get_current_caps (pad);
gst_object_unref (pad);
if (!caps)
return;
if (gst_caps_is_any (caps) || gst_caps_is_empty (caps)) {
gst_caps_unref (caps);
return;
}
caps = gst_caps_make_writable (caps);
gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
par, 1, "framerate", GST_TYPE_FRACTION, fps, 1, nullptr);
if (set_hdr10) {
gst_caps_set_simple (caps, "mastering-display-info", G_TYPE_STRING,
"34000:16000:13250:34500:7500:3000:15635:16450:10000000:1",
"content-light-level", G_TYPE_STRING, "1000:400", nullptr);
}
g_object_set (setter, "caps", caps, nullptr);
gst_caps_unref (caps);
}
}
static void
decoder_caps_notify (GstPad * pad, GParamSpec * pspec, gpointer user_data)
{
GstCaps *caps;
gchar *caps_str;
g_object_get (pad, "caps", &caps, nullptr);
if (!caps)
return;
caps_str = gst_caps_to_string (caps);
gst_println ("\nDecoder output caps\n%s\n", caps_str);
g_free (caps_str);
gst_caps_unref (caps);
}
static gboolean
bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
{
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:{
GError *err;
gchar *dbg;
gst_message_parse_error (msg, &err, &dbg);
gst_printerrln ("ERROR %s", err->message);
if (dbg != NULL)
gst_printerrln ("ERROR debug information: %s", dbg);
g_clear_error (&err);
g_free (dbg);
g_main_loop_quit (loop);
break;
}
case GST_MESSAGE_EOS:
gst_println ("Got EOS");
g_main_loop_quit (loop);
break;
default:
break;
}
return TRUE;
}
gint
main (gint argc, gchar ** argv)
{
GstElement *pipeline;
GError *error = nullptr;
GOptionContext *option_ctx;
gchar *decoder_name = nullptr;
gchar *encoder_name = nullptr;
gchar *sink_name = nullptr;
gchar *location = nullptr;
GOptionEntry options[] = {
{"decoder", 0, 0, G_OPTION_ARG_STRING, &decoder_name,
"Video decoder to use"},
{"encoder", 0, 0, G_OPTION_ARG_STRING, &encoder_name,
"Video encoder description. Ignored if \"location\" is set "
"(example: \"x264enc speed-preset=ultrafast\""},
{"videosink", 0, 0, G_OPTION_ARG_STRING, &sink_name,
"Video sink to use"},
{"location", 0, 0, G_OPTION_ARG_STRING, &location, "File location"},
{nullptr}
};
option_ctx =
g_option_context_new ("Video decoder caps update example");
g_option_context_add_main_entries (option_ctx, options, nullptr);
g_option_context_set_help_enabled (option_ctx, TRUE);
if (!g_option_context_parse (option_ctx, &argc, &argv, &error)) {
gst_printerrln ("option parsing failed: %s\n", error->message);
g_clear_error (&error);
exit (1);
}
g_option_context_free (option_ctx);
gst_init (nullptr, nullptr);
if (!decoder_name) {
gst_printerrln ("Decoder must be specified");
exit (1);
}
if (!encoder_name && !location) {
gst_printerrln ("Encoder or file location must be specified");
exit (1);
}
std::string pipeline_desc;
if (location) {
pipeline_desc = "filesrc location=" + std::string (location)
+ " ! parsebin ! capssetter name=setter ! "
+ std::string (decoder_name) + " name=dec";
} else {
pipeline_desc = "videotestsrc ! " + std::string (encoder_name)
+ " ! parsebin ! capssetter name=setter ! "
+ std::string (decoder_name) + " name=dec";
}
if (sink_name) {
pipeline_desc += " ! " + std::string (sink_name);
} else {
pipeline_desc += " ! fakevideosink";
}
gst_println ("Constructing test pipeline \"%s\"", pipeline_desc.c_str());
loop = g_main_loop_new (nullptr, FALSE);
pipeline = gst_parse_launch (pipeline_desc.c_str(), &error);
if (error) {
gst_printerrln ("Could not construct pipeline, error: %s",
error->message);
exit(1);
}
setter = gst_bin_get_by_name (GST_BIN (pipeline), "setter");
if (!setter) {
gst_printerrln ("Could not get capssetter from pipeline");
exit(1);
}
GstElement *dec = gst_bin_get_by_name (GST_BIN (pipeline), "dec");
if (!dec) {
gst_printerrln ("Could not get decoder from pipeline");
exit(1);
}
GstPad *pad = gst_element_get_static_pad (dec, "src");
g_signal_connect (pad, "notify::caps", G_CALLBACK (decoder_caps_notify),
nullptr);
gst_object_unref (pad);
gst_object_unref (dec);
gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_msg, nullptr);
/* run the pipeline */
GstStateChangeReturn ret =
gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
gst_printerrln ("Pipeline doesn't want to playing");
} else {
set_key_handler ((KeyInputCallback) keyboard_cb, nullptr);
gst_println ("Press k to see supported keyboard inputs");
g_main_loop_run (loop);
unset_key_handler ();
}
input_lock.lock();
gst_clear_object (&setter);
input_lock.unlock();
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_bus_remove_watch (GST_ELEMENT_BUS (pipeline));
gst_object_unref (pipeline);
g_main_loop_unref (loop);
g_free (decoder_name);
g_free (encoder_name);
g_free (location);
return 0;
}
|