<commit_before>#include "audio_manager.h"

#include "base/strings/utf_string_conversions.h"

#include <QtCore/QDebug>
#include <iostream>

static void gst_debug_logcat(GstDebugCategory* category,
  GstDebugLevel level,
  const gchar* file,
  const gchar* function,
  gint line,
  GObject* object,
  GstDebugMessage* message,
  gpointer unused) {
  gchar* tag;

  if (level > gst_debug_category_get_threshold(category))
    return;

  tag = g_strdup_printf("GStreamer+%s", gst_debug_category_get_name(category));

  if (object) {
    gchar* obj;

    if (GST_IS_PAD(object) && GST_OBJECT_NAME(object)) {
      obj = g_strdup_printf("<%s:%s>", GST_DEBUG_PAD_NAME(object));
    } else if (GST_IS_OBJECT(object) && GST_OBJECT_NAME(object)) {
      obj = g_strdup_printf("<%s>", GST_OBJECT_NAME(object));
    } else if (G_IS_OBJECT(object)) {
      obj = g_strdup_printf("<%s@%p>", G_OBJECT_TYPE_NAME(object), object);
    } else {
      obj = g_strdup_printf("<%p>", object);
    }

    qDebug() << file << ":" << line << ":" << function << " " << obj << " " << gst_debug_message_get(message);

    g_free(obj);
  } else {
    qDebug() << file << ":" << line << ":" << function << " " << gst_debug_message_get(message);
  }

  g_free(tag);
}

AudioManager::AudioManager() {
  gst_init(NULL, NULL);

#if defined(OS_POSIX)
  pipeline_ = gst_pipeline_new("audio-player");
  bus_      = gst_pipeline_get_bus(GST_PIPELINE(pipeline_));

  source_   = gst_element_factory_make("filesrc", "file-source");
  decoder_  = gst_element_factory_make("mad", "mp3-decoder");
  sink_     = gst_element_factory_make("autoaudiosink", "audio-output");

  gst_bin_add_many(GST_BIN(pipeline_), source_, decoder_, sink_, NULL);

  gst_element_link_many(source_, decoder_, sink_, NULL);
#elif defined(OS_WIN)
  playbin_ = gst_element_factory_make("playbin", "playbin");

  bus_ = gst_pipeline_get_bus(GST_PIPELINE(playbin_));
#endif
  gst_bus_add_signal_watch(bus_);
  g_signal_connect(bus_, "message", G_CALLBACK(AudioManager::GstBusCallback), this);

  gst_debug_set_active(TRUE);
  gst_debug_set_default_threshold(GST_LEVEL_WARNING);
  gst_debug_remove_log_function(gst_debug_log_default);
  gst_debug_add_log_function((GstLogFunction)gst_debug_logcat, NULL, NULL);

  track_poller_ = new Timer(0, 100, true,
                            std::bind(&AudioManager::TrackPoller, this));
}

AudioManager::~AudioManager() {
  gst_element_set_state(pipeline_, GST_STATE_NULL);
  gst_object_unref(bus_);
  gst_object_unref(GST_OBJECT(pipeline_));
}

void AudioManager::PlayMp3File(base::FilePath& file) {
#if defined(OS_POSIX)
  gst_element_set_state(pipeline_, GST_STATE_READY);
  g_object_set(G_OBJECT(source_), "location", file.value().c_str(), NULL);
  gst_element_set_state(pipeline_, GST_STATE_PLAYING);
#elif defined(OS_WIN)
  gst_element_set_state(playbin_, GST_STATE_READY);
  std::string loc = base::WideToUTF8(file.value());
  g_object_set(G_OBJECT(playbin_), "uri", loc.c_str(), NULL);
  gst_element_set_state(playbin_, GST_STATE_PLAYING);
#endif

  playing_ = 1;
  track_poller_->Start();
}

void AudioManager::TrackPoller() {
  if (playing_) {
#if defined(OS_POSIX)
    gst_element_query_position(pipeline_, GST_FORMAT_TIME, &pos_);
    gst_element_query_duration(pipeline_, GST_FORMAT_TIME, &len_);
#elif defined(OS_WIN)
    // Is this for gstreamer-0.10 ?
    GstFormat fmt = GST_FORMAT_TIME;
    gst_element_query_position(pipeline_, &fmt, &pos_);
    gst_element_query_duration(pipeline_, &fmt, &len_);
#endif
    PlaybackProgressCallback(pos_, len_);
  } else {
    track_poller_->Stop();
  }
}<commit_msg>- Windows: format uri string correctly.<commit_after>#include "audio_manager.h"

#include "base/strings/utf_string_conversions.h"

#include "base/file_util.h"
#include "base/files/file_path.h"

#include <QtCore/QDebug>
#include <iostream>

static void gst_debug_logcat(GstDebugCategory* category,
  GstDebugLevel level,
  const gchar* file,
  const gchar* function,
  gint line,
  GObject* object,
  GstDebugMessage* message,
  gpointer unused) {
  gchar* tag;

  if (level > gst_debug_category_get_threshold(category))
    return;

  tag = g_strdup_printf("GStreamer+%s", gst_debug_category_get_name(category));

  if (object) {
    gchar* obj;

    if (GST_IS_PAD(object) && GST_OBJECT_NAME(object)) {
      obj = g_strdup_printf("<%s:%s>", GST_DEBUG_PAD_NAME(object));
    } else if (GST_IS_OBJECT(object) && GST_OBJECT_NAME(object)) {
      obj = g_strdup_printf("<%s>", GST_OBJECT_NAME(object));
    } else if (G_IS_OBJECT(object)) {
      obj = g_strdup_printf("<%s@%p>", G_OBJECT_TYPE_NAME(object), object);
    } else {
      obj = g_strdup_printf("<%p>", object);
    }

    qDebug() << file << ":" << line << ":" << function << " " << obj << " " << gst_debug_message_get(message);

    g_free(obj);
  } else {
    qDebug() << file << ":" << line << ":" << function << " " << gst_debug_message_get(message);
  }

  g_free(tag);
}

AudioManager::AudioManager() {
  gst_init(NULL, NULL);

#if defined(OS_POSIX)
  pipeline_ = gst_pipeline_new("audio-player");
  bus_      = gst_pipeline_get_bus(GST_PIPELINE(pipeline_));

  source_   = gst_element_factory_make("filesrc", "file-source");
  decoder_  = gst_element_factory_make("mad", "mp3-decoder");
  sink_     = gst_element_factory_make("autoaudiosink", "audio-output");

  gst_bin_add_many(GST_BIN(pipeline_), source_, decoder_, sink_, NULL);

  gst_element_link_many(source_, decoder_, sink_, NULL);
#elif defined(OS_WIN)
  playbin_ = gst_element_factory_make("playbin", "playbin");

  bus_ = gst_pipeline_get_bus(GST_PIPELINE(playbin_));
#endif
  gst_bus_add_signal_watch(bus_);
  g_signal_connect(bus_, "message", G_CALLBACK(AudioManager::GstBusCallback), this);

  gst_debug_set_active(TRUE);
  gst_debug_set_default_threshold(GST_LEVEL_WARNING);
  gst_debug_remove_log_function(gst_debug_log_default);
  gst_debug_add_log_function((GstLogFunction)gst_debug_logcat, NULL, NULL);

  track_poller_ = new Timer(0, 100, true,
                            std::bind(&AudioManager::TrackPoller, this));
}

AudioManager::~AudioManager() {
  gst_element_set_state(pipeline_, GST_STATE_NULL);
  gst_object_unref(bus_);
  gst_object_unref(GST_OBJECT(pipeline_));
}

void AudioManager::PlayMp3File(base::FilePath& file) {
#if defined(OS_POSIX)
  gst_element_set_state(pipeline_, GST_STATE_READY);
  g_object_set(G_OBJECT(source_), "location", file.value().c_str(), NULL);
  gst_element_set_state(pipeline_, GST_STATE_PLAYING);
#elif defined(OS_WIN)
  gst_element_set_state(playbin_, GST_STATE_READY);
  base::FilePath abs_loc = base::MakeAbsoluteFilePath(file);
  std::string loc = base::WideToUTF8(abs_loc.value());
  loc.insert(0, "file:///");
  g_object_set(G_OBJECT(playbin_), "uri", loc.c_str(), NULL);
  gst_element_set_state(playbin_, GST_STATE_PLAYING);
#endif

  playing_ = 1;
  track_poller_->Start();
}

void AudioManager::TrackPoller() {
  if (playing_) {
#if defined(OS_POSIX)
    gst_element_query_position(pipeline_, GST_FORMAT_TIME, &pos_);
    gst_element_query_duration(pipeline_, GST_FORMAT_TIME, &len_);
#elif defined(OS_WIN)
    // Is this for gstreamer-0.10 ?
    GstFormat fmt = GST_FORMAT_TIME;
    gst_element_query_position(pipeline_, &fmt, &pos_);
    gst_element_query_duration(pipeline_, &fmt, &len_);
#endif
    PlaybackProgressCallback(pos_, len_);
  } else {
    track_poller_->Stop();
  }
}
{'fraction_non_alphanumeric': 0.14105348900352974, 'fraction_numerical': 0.0031224545207711105, 'mean_word_length': 5.625, 'pattern_counts': {'":': 8, '<': 79, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7517404', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2747, 'n_words': 412}