Intermediate commit

This commit is contained in:
László Károlyi 2022-01-22 19:21:12 +01:00
parent c731ce1be4
commit cf0f674c1e
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE
8 changed files with 207 additions and 25 deletions

View file

@ -1,9 +1,9 @@
MIT License
MIT+NIGGER License
Copyright (c) <year> <copyright holders>
Copyright (c) <2022> <László Károlyi>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice, this permission notice and the word "NIGGER" shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,5 +1,6 @@
playlist:
path: ./CMakeLists.txt
path_reload_when_touched: .
icecast_server:
address: stream.streamhostt.com
tls: false

View file

@ -9,6 +9,7 @@ int main(int argc, char *argv[]) {
IceGStreamer::Config::_config config;
try {
config = IceGStreamer::Config::load(argc, argv);
IceGStreamer::Streamer::start(argc, argv, config);
} catch (IceGStreamer::Exceptions::PlayerExceptionBase &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
@ -16,7 +17,7 @@ int main(int argc, char *argv[]) {
std::cerr << e.what() << std::endl;
return 1;
}
for (int x = 0; x< 1000000000; x++)
IceGStreamer::Streamer::start(argc, argv, config);
// for (int x = 0; x< 1000000000; x++)
// IceGStreamer::Streamer::start(argc, argv, config);
return 0;
}

View file

@ -51,6 +51,10 @@ inline void check_structure() {
throw IceGStreamer::Exceptions::ConfigOptionMissingException(
"playlist.path");
}
if (!yaml_config["playlist"]["path_reload_when_touched"].IsScalar()) {
throw IceGStreamer::Exceptions::ConfigOptionMissingException(
"playlist.path_reload_when_touched");
}
if (!yaml_config["icecast_server"].IsMap()) {
throw IceGStreamer::Exceptions::ConfigOptionMissingException(
"icecast_server");
@ -89,11 +93,16 @@ inline void check_structure() {
}
inline void fill_contents(_config &config) {
const std::string path = yaml_config["playlist"]["path"].as<std::string>();
// playlist section
const std::string path = yaml_config["playlist"]["path"].as<std::string>();
const std::string path_reload_when_touched =
yaml_config["playlist"]["path_reload_when_touched"].as<std::string>();
if (!is_readable(path))
throw Exceptions::PlaylistPathNotFoundException(path);
throw Exceptions::PlaylistUnreadableException(path);
if (!is_readable(path))
throw Exceptions::PlaylistUnreadableException(path_reload_when_touched);
config.playlist.path = path;
config.playlist.path_reload_when_touched = path_reload_when_touched;
// icecast_server section
config.icecast_server.address =
yaml_config["icecast_server"]["address"].as<std::string>();

View file

@ -6,6 +6,7 @@ namespace Config {
struct _config_playlist {
std::string path{};
std::string path_reload_when_touched{};
};
struct _config_icecastserver {

View file

@ -23,8 +23,7 @@ const char *PlaylistUnreadableException::what() { return error_msg.c_str(); }
// ===== PlaylistUnreadableException END =====
// ===== PlaylistUnreadableException START =====
PlaylistEmptyException::PlaylistEmptyException(
const std::string &path)
PlaylistEmptyException::PlaylistEmptyException(const std::string &path)
: error_msg("Playlist empty: " + path) {}
const char *PlaylistEmptyException::what() { return error_msg.c_str(); }
@ -51,14 +50,15 @@ ConfigOptionMissingException::ConfigOptionMissingException()
: d_ptr_{std::make_unique<Data>()} {};
struct ConfigOptionMissingException::Data {
std::string error_str{"Configuration file item missing or is of bad type:"};
std::string error_str{
"Configuration file item missing in YAML file or is of bad type:"};
std::string missing_item{"<unknown>"};
std::string allocated_message{};
};
// Copy constructor
// ConfigOptionMissingException::ConfigOptionMissingException(
// ConfigOptionMissingException &other)
// const ConfigOptionMissingException &other)
// : d_ptr_(std::make_unique<Data>(*other.d_ptr_)){};
// Move constructor
@ -99,5 +99,19 @@ const char *ConfigOptionMissingException::what() {
}
// ===== ConfigOptionMissingException END =====
// ===== PlayerSetupException START =====
PlayerSetupException::PlayerSetupException(const std::string &error)
: error_msg("Player setup exception: " + error) {}
const char *PlayerSetupException::what() { return error_msg.c_str(); }
// ===== PlayerSetupException END =====
// ===== PlayerBusException START =====
PlayerBusException::PlayerBusException(const std::string &error)
: error_msg("Player bus exception: " + error) {}
const char *PlayerBusException::what() { return error_msg.c_str(); }
// ===== PlayerBusException END =====
} // namespace Exceptions
} // namespace IceGStreamer

View file

@ -73,5 +73,21 @@ public:
~ConfigOptionMissingException();
};
class PlayerSetupException : public PlayerExceptionBase {
const std::string error_msg;
public:
PlayerSetupException(const std::string &error);
virtual const char *what();
};
class PlayerBusException : public PlayerExceptionBase {
const std::string error_msg;
public:
PlayerBusException(const std::string &error);
virtual const char *what();
};
} // namespace Exceptions
} // namespace IceGStreamer

View file

@ -1,4 +1,5 @@
#include "streamer.h"
#include "config.h"
#include "exceptions.h"
#include <fstream>
#include <gstreamer-1.0/gst/gst.h>
@ -9,6 +10,157 @@
namespace IceGStreamer {
namespace Streamer {
class PlaylistHandler {
private:
uint32_t file_seekpoint{0};
IceGStreamer::Config::_config_playlist &config;
public:
std::string current_track{};
PlaylistHandler(IceGStreamer::Config::_config_playlist &config)
: config(config) {}
void load_next_track() {}
};
class StreamerInternals {
private:
IceGStreamer::Config::_config &config;
PlaylistHandler playlist_handler;
GstElement *pipeline{}, *filesrc{}, *decodebin{}, *convert{}, *sink{};
GstBus *bus;
GstMessage *msg;
GMainLoop *main_loop;
public:
StreamerInternals(IceGStreamer::Config::_config &config)
: config(config), playlist_handler(PlaylistHandler(config.playlist)) {}
static void cb_decodebin_pad_added(
GstElement &object, GstPad *pad, StreamerInternals &instance) {
gst_pad_link(pad, gst_element_get_static_pad(instance.convert, "sink"));
std::cout << "cb_decodebin_pad_added, " << pad->object.name << ", "
<< object.object.name << std::endl;
}
static void cb_decodebin_pad_removed(
GstElement &object, GstPad *pad, StreamerInternals &instance) {
// gst_pad_link(pad, gst_element_get_static_pad(instance.convert, "sink"));
std::cout << "cb_decodebin_pad_removed, " << pad->object.name << ", "
<< object.object.name << std::endl;
}
static void
cb_decodebin_drained(GstElement &object, StreamerInternals &instance) {
std::cout << "cb_decodebin_drained, " << object.object.name << std::endl;
GstStateChangeReturn ret =
gst_element_set_state(instance.pipeline, GST_STATE_READY);
// g_object_set(
// instance.filesrc, "location", "/home/karolyi/Downloads/test.mp3",
// nullptr);
ret = gst_element_set_state(instance.pipeline, GST_STATE_PLAYING);
}
static void
cb_bus_message(GstBus &bus, GstMessage &msg, StreamerInternals &instance) {
printf("BUS MESSAGE: %d\n", msg.type);
switch (msg.type) {
case GST_MESSAGE_STATE_CHANGED: {
printf(
"BUS MESSAGE: CHANGED: %d, %d, %d\n",
instance.pipeline->current_state, instance.pipeline->next_state,
instance.pipeline->pending_state);
break;
}
case GST_MESSAGE_ERROR: {
GError *err;
gchar *debug;
gst_message_parse_error(&msg, &err, &debug);
gst_element_set_state(instance.pipeline, GST_STATE_READY);
g_main_loop_quit(instance.main_loop);
const std::string str_message{std::string(err->message)};
const std::string str_debug{debug};
g_error_free(err);
g_free(debug);
throw Exceptions::PlayerBusException(str_message + ": " + str_debug);
break;
}
case GST_MESSAGE_EOS: {
gst_element_set_state(instance.pipeline, GST_STATE_READY);
g_main_loop_quit(instance.main_loop);
break;
}
default:
break;
}
}
void start(int &argc, char *argv[]) {
gst_init(&argc, &argv);
filesrc = gst_element_factory_make("filesrc", "icegstreamer-filesrc");
decodebin = gst_element_factory_make("decodebin", "icegstreamer-decodebin");
convert = gst_element_factory_make("audioconvert", "icegstreamer-convert");
sink = gst_element_factory_make("autoaudiosink", "icegstreamer-sink");
pipeline = gst_pipeline_new("icegstreamer-pipeline");
if (!pipeline || !filesrc || !decodebin || !convert || !sink)
throw Exceptions::PlayerSetupException("Couldn't set up pipeline");
gst_bin_add_many(
GST_BIN(pipeline), filesrc, decodebin, convert, sink, nullptr);
if (!gst_element_link_many(filesrc, decodebin, nullptr))
throw Exceptions::PlayerSetupException("Can't link pipeline elements");
if (!gst_element_link_many(convert, sink, nullptr))
throw Exceptions::PlayerSetupException("Can't link pipeline elements");
g_signal_connect(
decodebin, "pad-added", G_CALLBACK(cb_decodebin_pad_added), this);
g_signal_connect(
decodebin, "pad-removed", G_CALLBACK(cb_decodebin_pad_removed), this);
g_signal_connect(
decodebin, "drained", G_CALLBACK(cb_decodebin_drained), this);
g_object_set(
filesrc, "location", "/home/karolyi/Downloads/IMA FIRIN MAH LAZER.mp3",
nullptr);
const GstStateChangeReturn ret =
gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
throw Exceptions::PlayerSetupException("Unable to set the playing state");
}
bus = gst_element_get_bus(pipeline);
main_loop = g_main_loop_new(nullptr, false);
gst_bus_add_signal_watch(bus);
g_signal_connect(bus, "message", G_CALLBACK(cb_bus_message), this);
g_main_loop_run(main_loop);
// msg = gst_bus_timed_pop_filtered(
// bus, GST_CLOCK_TIME_NONE,
// static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
// printf("ret is %i\n", ret);
// printf("msg is %i\n", msg->type);
// GError *err;
// gchar *debug_info;
// gst_message_parse_error(msg, &err, &debug_info);
// g_printerr(
// "Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src),
// err->message);
// g_printerr("Debugging information: %s\n", debug_info ? debug_info :
// "none");
// // playlist_handler = PlaylistHandler(config.playlist);
}
~StreamerInternals() {
// if (sink)
// gst_object_unref(sink);
// if (filesrc)
// gst_object_unref(filesrc);
if (main_loop)
g_main_loop_unref(main_loop);
if (bus)
gst_object_unref(bus);
if (pipeline) {
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
}
}
};
typedef std::list<std::string> _playlistType;
inline void load_playlist(std::string &path, _playlistType &playlist) {
@ -25,20 +177,8 @@ inline void load_playlist(std::string &path, _playlistType &playlist) {
}
void start(int &argc, char *argv[], IceGStreamer::Config::_config &config) {
_playlistType playlist{};
gst_init(&argc, &argv);
try {
load_playlist(config.playlist.path, playlist);
} catch (Exceptions::PlayerExceptionBase &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return;
}
// GstElement *pipe{gst_pipeline_new("my-pipeline")};
// gst_object_unref(pipe);
typedef std::unique_ptr<GstElement, decltype(&gst_object_unref)> _pipe_uptr;
_pipe_uptr pipeline(gst_pipeline_new("my-pipeline"), &gst_object_unref);
// std::cout << "ENDE " << (GstElement *)pipeline.get() << std::endl;
// std::cout << "ENDE " << pipeline.get() << std::endl;
StreamerInternals streamer_class = StreamerInternals(config);
streamer_class.start(argc, argv);
};
} // namespace Streamer