Skip to content

Commit

Permalink
correct buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
thekvs committed Jul 16, 2018
1 parent 8972c9c commit 6f13bd4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,7 @@ Copy your existing `bookmarks.xml` from [Radio Tray](http://radiotray.sourceforg
Configuration file is located in the same directory as bookmarks file. It has simple XML format and following options are supported:
* `last_station` -- name of the last played station. Automatically updated, you don't need to change it.
* `buffer_size` -- size of the internal gstreamer's buffer.
* `buffer_duration` -- number of seconds to buffer.
* `url_timeout` -- timeout in seconds for fetching playlist.
* `notifications` -- if set to `false` disables desktop notification messages. Default is `true`.

Expand All @@ -45,6 +46,7 @@ Example:
<config>
<option name="last_station" value="Rock 181" />
<option name="buffer_size" value="102400" />
<option name="buffer_duration" value="2" />
<option name="url_timeout" value="5" />
<option name="notifications" value="false" />
</config>
Expand Down
11 changes: 11 additions & 0 deletions src/config.cpp
Expand Up @@ -34,6 +34,11 @@ Config::load_config()
buffer_size = node.node().attribute("value").as_int();
}

node = config.select_node("/config/option[@name='buffer_duration']");
if (not node.node().empty()) {
buffer_duration = node.node().attribute("value").as_int();
}

node = config.select_node("/config/option[@name='url_timeout']");
if (not node.node().empty()) {
url_timeout_ms = node.node().attribute("value").as_float() * 1000; /* 'url_timeout' is in seconds */
Expand Down Expand Up @@ -93,6 +98,12 @@ Config::create_new_config()
option.append_attribute("value").set_value(buffer_size);
}

{
auto option = root.append_child("option");
option.append_attribute("name").set_value("buffer_duration");
option.append_attribute("value").set_value(buffer_duration);
}

{
auto option = root.append_child("option");
option.append_attribute("name").set_value("url_timeout");
Expand Down
1 change: 1 addition & 0 deletions src/config.hpp
Expand Up @@ -28,6 +28,7 @@ class Config

long url_timeout_ms = kDefaultHTTPRequestTimeout;
int buffer_size = kDefaultGStreamerBufferSize;
int64_t buffer_duration = kDefaultBufferDuration;

private:
std::string filename;
Expand Down
4 changes: 3 additions & 1 deletion src/constants.hpp
Expand Up @@ -2,11 +2,13 @@
#define __CONSTANTS_HPP_INCLUDED__

#include <string>
#include <cstdint>

namespace radiotray
{
const long kDefaultHTTPRequestTimeout = 5 * 1000;
const int kDefaultGStreamerBufferSize = 1024 * 100;
const int kDefaultGStreamerBufferSize = 320000;
const int64_t kDefaultBufferDuration = 2;

const char* const kRadioTrayAppDirName = "radiotray";
const char* const kAppDirName = "radiotray-lite";
Expand Down
34 changes: 30 additions & 4 deletions src/player.cpp
Expand Up @@ -13,7 +13,7 @@ Player::init(int argc, char** argv)
LOG(ERROR) << "The PlayBin element could not be created.";
return false;
}
set_buffer_size(config->buffer_size);
set_buffer();

Glib::RefPtr<Gst::Bus> bus = playbin->get_bus();
bus->add_watch(sigc::mem_fun(*this, &Player::on_bus_message));
Expand Down Expand Up @@ -77,9 +77,10 @@ Player::set_stream(const Glib::ustring& url)
}

void
Player::set_buffer_size(int size)
Player::set_buffer()
{
playbin->property_buffer_size() = size;
playbin->property_buffer_size() = config->buffer_size * config->buffer_duration;
playbin->property_buffer_duration() = config->buffer_duration * GST_SECOND;
}

void
Expand All @@ -96,7 +97,7 @@ Player::play_next_stream()
if (gst_uri_is_valid(u.c_str()) != 0) {
LOG(DEBUG) << "Trying to play stream: " << u;

set_buffer_size(config->buffer_size);
set_buffer();
set_stream(u);
start();

Expand Down Expand Up @@ -193,6 +194,31 @@ Player::on_bus_message(const Glib::RefPtr<Gst::Bus>& /*bus*/, const Glib::RefPtr
<< " Old: " << print(old_state) << " New: " << print(new_state)
<< " Source: " << state_changed_msg->get_source()->get_name();
}
} else if (message_type == Gst::MESSAGE_BUFFERING) {
auto buffering_msg = Glib::RefPtr<Gst::MessageBuffering>::cast_static(message);
auto percent = buffering_msg->parse();

if (percent == 100) {
buffering = false;
playbin->set_state(Gst::STATE_PLAYING);
LOG(DEBUG) << "buffering done";
} else {
buffering = true;

Gst::State state, pending;

playbin->get_state(state, pending, Gst::CLOCK_TIME_NONE);
if (state != Gst::STATE_PAUSED) {
playbin->set_state(Gst::STATE_PAUSED);
}

std::stringstream ss;

ss << "buffering " << percent << "%";
em->broadcast_info_changed(current_station, ss.str());

LOG(DEBUG) << "buffering: " << percent;
}
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion src/player.hpp
Expand Up @@ -55,10 +55,11 @@ class Player
MediaStreams::iterator next_stream;

Glib::ustring current_station;
bool buffering = false;

bool on_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message);
void set_stream(const Glib::ustring& url);
void set_buffer_size(int size);
void set_buffer();
void play_next_stream();
};

Expand Down

0 comments on commit 6f13bd4

Please sign in to comment.