Add support for logging via slog

This commit is contained in:
Sebastian Dröge 2016-12-25 12:16:12 +01:00
parent 89b0220e26
commit 8ee1f72184
22 changed files with 309 additions and 101 deletions

View file

@ -8,6 +8,7 @@ license = "LGPL-2.1+"
[dependencies]
url = "1.1"
gst-plugin = { path="../gst-plugin" }
slog = "1.3"
[lib]
name = "gstrsfile"

View file

@ -25,6 +25,10 @@ use std::convert::From;
use gst_plugin::error::*;
use gst_plugin::sink::*;
use gst_plugin::buffer::*;
use gst_plugin::utils::*;
use gst_plugin::log::*;
use slog::*;
#[derive(Debug)]
enum StreamingState {
@ -35,15 +39,23 @@ enum StreamingState {
#[derive(Debug)]
pub struct FileSink {
streaming_state: StreamingState,
logger: Logger,
}
impl FileSink {
pub fn new() -> FileSink {
FileSink { streaming_state: StreamingState::Stopped }
pub fn new(element: Element) -> FileSink {
FileSink {
streaming_state: StreamingState::Stopped,
logger: Logger::root(GstDebugDrain::new(Some(&element),
"rsfilesink",
0,
"Rust file sink"),
None),
}
}
pub fn new_boxed() -> Box<Sink> {
Box::new(FileSink::new())
pub fn new_boxed(element: Element) -> Box<Sink> {
Box::new(FileSink::new(element))
}
}

View file

@ -23,6 +23,10 @@ use url::Url;
use gst_plugin::error::*;
use gst_plugin::source::*;
use gst_plugin::buffer::*;
use gst_plugin::log::*;
use gst_plugin::utils::*;
use slog::*;
#[derive(Debug)]
enum StreamingState {
@ -33,15 +37,23 @@ enum StreamingState {
#[derive(Debug)]
pub struct FileSrc {
streaming_state: StreamingState,
logger: Logger,
}
impl FileSrc {
pub fn new() -> FileSrc {
FileSrc { streaming_state: StreamingState::Stopped }
pub fn new(element: Element) -> FileSrc {
FileSrc {
streaming_state: StreamingState::Stopped,
logger: Logger::root(GstDebugDrain::new(Some(&element),
"rsfilesrc",
0,
"Rust file source"),
None),
}
}
pub fn new_boxed() -> Box<Source> {
Box::new(FileSrc::new())
pub fn new_boxed(element: Element) -> Box<Source> {
Box::new(FileSrc::new(element))
}
}

View file

@ -19,6 +19,8 @@
extern crate url;
#[macro_use]
extern crate slog;
#[macro_use]
extern crate gst_plugin;
use gst_plugin::plugin::*;

View file

@ -1,71 +0,0 @@
// Copyright (C) 2016 Sebastian Dröge <sebastian@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.
#![crate_type="cdylib"]
extern crate url;
#[macro_use]
extern crate gst_plugin;
use gst_plugin::plugin::*;
use gst_plugin::source::*;
use gst_plugin::sink::*;
mod filesrc;
mod filesink;
use filesrc::FileSrc;
use filesink::FileSink;
fn plugin_init(plugin: &Plugin) -> bool {
source_register(plugin,
&SourceInfo {
name: "rsfilesrc",
long_name: "File Source",
description: "Reads local files",
classification: "Source/File",
author: "Sebastian Dröge <sebastian@centricular.com>",
rank: 256 + 100,
create_instance: FileSrc::new_boxed,
protocols: "file",
push_only: false,
});
sink_register(plugin,
&SinkInfo {
name: "rsfilesink",
long_name: "File Sink",
description: "Writes to local files",
classification: "Sink/File",
author: "Luis de Bethencourt <luisbg@osg.samsung.com>",
rank: 256 + 100,
create_instance: FileSink::new_boxed,
protocols: "file",
});
true
}
plugin_define!(b"rsfile\0",
b"Rust File Plugin\0",
plugin_init,
b"1.0\0",
b"LGPL\0",
b"rsfile\0",
b"rsfile\0",
b"https://github.com/sdroege/rsplugin\0",
b"2016-12-08\0");

View file

@ -8,6 +8,7 @@ license = "LGPL-2.1+"
[dependencies]
url = "1.1"
gst-plugin = { path="../gst-plugin" }
slog = "1.3"
nom = "2.0"
flavors = {git = "https://github.com/sdroege/flavors.git"}

View file

@ -27,6 +27,10 @@ use gst_plugin::demuxer::*;
use gst_plugin::buffer::*;
use gst_plugin::adapter::*;
use gst_plugin::utils;
use gst_plugin::utils::Element;
use gst_plugin::log::*;
use slog::*;
const AUDIO_STREAM_ID: u32 = 0;
const VIDEO_STREAM_ID: u32 = 1;
@ -383,6 +387,7 @@ impl Metadata {
#[derive(Debug)]
pub struct FlvDemux {
logger: Logger,
state: State,
adapter: Adapter,
// Only in >= State::Streaming
@ -390,16 +395,21 @@ pub struct FlvDemux {
}
impl FlvDemux {
pub fn new() -> FlvDemux {
pub fn new(element: Element) -> FlvDemux {
FlvDemux {
logger: Logger::root(GstDebugDrain::new(Some(&element),
"rsflvdemux",
0,
"Rust FLV demuxer"),
None),
state: State::Stopped,
adapter: Adapter::new(),
streaming_state: None,
}
}
pub fn new_boxed() -> Box<Demuxer> {
Box::new(FlvDemux::new())
pub fn new_boxed(element: Element) -> Box<Demuxer> {
Box::new(FlvDemux::new(element))
}
fn handle_script_tag(&mut self,

View file

@ -21,6 +21,8 @@ extern crate url;
#[macro_use]
extern crate gst_plugin;
#[macro_use]
extern crate slog;
#[macro_use]
extern crate nom;
extern crate flavors;

View file

@ -9,6 +9,7 @@ license = "LGPL-2.1+"
url = "1.1"
gst-plugin = { path="../gst-plugin" }
reqwest = "0.2"
slog = "1.3"
[lib]
name = "gstrshttp"

View file

@ -25,6 +25,10 @@ use reqwest::header::{ContentLength, ContentRange, ContentRangeSpec, Range, Byte
use gst_plugin::error::*;
use gst_plugin::source::*;
use gst_plugin::buffer::*;
use gst_plugin::utils::*;
use gst_plugin::log::*;
use slog::*;
#[derive(Debug)]
enum StreamingState {
@ -43,19 +47,25 @@ enum StreamingState {
#[derive(Debug)]
pub struct HttpSrc {
streaming_state: StreamingState,
logger: Logger,
client: Client,
}
impl HttpSrc {
pub fn new() -> HttpSrc {
pub fn new(element: Element) -> HttpSrc {
HttpSrc {
streaming_state: StreamingState::Stopped,
logger: Logger::root(GstDebugDrain::new(Some(&element),
"rshttpsink",
0,
"Rust http sink"),
None),
client: Client::new().unwrap(),
}
}
pub fn new_boxed() -> Box<Source> {
Box::new(HttpSrc::new())
pub fn new_boxed(element: Element) -> Box<Source> {
Box::new(HttpSrc::new(element))
}
fn do_request(&self,

View file

@ -21,6 +21,8 @@ extern crate url;
#[macro_use]
extern crate gst_plugin;
extern crate reqwest;
#[macro_use]
extern crate slog;
use gst_plugin::plugin::*;
use gst_plugin::source::*;

View file

@ -10,6 +10,7 @@ license = "LGPL-2.1+"
libc = "0.2"
url = "1.1"
bitflags = "0.7"
slog = "1.3"
[build-dependencies]
gcc = "0.3"

View file

@ -23,7 +23,7 @@ fn main() {
let gstbase = pkg_config::probe_library("gstreamer-base-1.0").unwrap();
let includes = [gstreamer.include_paths, gstbase.include_paths];
let files = ["src/error.c", "src/buffer.c", "src/source.c", "src/sink.c", "src/demuxer.c"];
let files = ["src/error.c", "src/buffer.c", "src/log.c", "src/source.c", "src/sink.c", "src/demuxer.c"];
let mut config = gcc::Config::new();
config.include("src");

View file

@ -343,9 +343,10 @@ impl DemuxerWrapper {
#[no_mangle]
pub extern "C" fn demuxer_new(demuxer: *mut c_void,
create_instance: fn() -> Box<Demuxer>)
create_instance: fn(Element) -> Box<Demuxer>)
-> *mut DemuxerWrapper {
Box::into_raw(Box::new(DemuxerWrapper::new(demuxer, create_instance())))
let instance = create_instance(unsafe { Element::new(demuxer) });
Box::into_raw(Box::new(DemuxerWrapper::new(demuxer, instance)))
}
#[no_mangle]
@ -451,7 +452,7 @@ pub struct DemuxerInfo<'a> {
pub classification: &'a str,
pub author: &'a str,
pub rank: i32,
pub create_instance: fn() -> Box<Demuxer>,
pub create_instance: fn(Element) -> Box<Demuxer>,
pub input_formats: &'a str,
pub output_formats: &'a str,
}
@ -480,7 +481,7 @@ pub fn demuxer_register(plugin: &Plugin, demuxer_info: &DemuxerInfo) {
let coutput_formats = CString::new(demuxer_info.output_formats).unwrap();
unsafe {
gst_rs_demuxer_register(plugin.to_raw(),
gst_rs_demuxer_register(plugin.as_ptr(),
cname.as_ptr(),
clong_name.as_ptr(),
cdescription.as_ptr(),

View file

@ -26,4 +26,3 @@ gst_rs_element_error (GstElement * element, GQuark error_domain,
gst_element_message_full (element, GST_MESSAGE_ERROR, error_domain,
error_code, g_strdup (message), g_strdup (debug), file, function, line);
}

View file

@ -20,6 +20,8 @@ extern crate libc;
extern crate url;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate slog;
#[macro_use]
pub mod utils;
@ -32,4 +34,4 @@ pub mod plugin;
pub mod source;
pub mod sink;
pub mod demuxer;
pub mod log;

28
gst-plugin/src/log.c Normal file
View file

@ -0,0 +1,28 @@
/* Copyright (C) 2016 Sebastian Dröge <sebastian@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.
*/
#include <gst/gst.h>
void
gst_rs_debug_log (GstDebugCategory * category,
GstDebugLevel level,
const gchar * file,
const gchar * function, gint line, GObject * object, const gchar * message)
{
gst_debug_log (category, level, file, function, line, object, "%s", message);
}

151
gst-plugin/src/log.rs Normal file
View file

@ -0,0 +1,151 @@
// Copyright (C) 2016 Sebastian Dröge <sebastian@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.
use std::os::raw::c_void;
use libc::c_char;
use std::ffi::CString;
use slog::{Drain, Record, OwnedKeyValueList, Never, Level};
use std::fmt;
use std::ptr;
use utils::Element;
#[derive(Debug)]
pub struct GstDebugDrain {
category: *const c_void,
element: *const c_void,
}
impl GstDebugDrain {
pub fn new(element: Option<&Element>,
name: &str,
color: u32,
description: &str)
-> GstDebugDrain {
extern "C" {
fn _gst_debug_category_new(name: *const c_char,
color: u32,
description: *const c_char)
-> *const c_void;
}
let name_cstr = CString::new(name.as_bytes()).unwrap();
let description_cstr = CString::new(description.as_bytes()).unwrap();
// Gets the category if it exists already
let category = unsafe {
_gst_debug_category_new(name_cstr.as_ptr(), color, description_cstr.as_ptr())
};
let element = match element {
Some(element) => unsafe { element.as_ptr() },
None => ptr::null(),
};
let drain = GstDebugDrain {
category: category,
element: ptr::null(),
};
extern "C" {
fn g_weak_ref_set(weak_ref: &*const c_void, obj: *const c_void);
}
if !element.is_null() {
unsafe {
g_weak_ref_set(&drain.element, element);
}
}
drain
}
}
impl Drop for GstDebugDrain {
fn drop(&mut self) {
extern "C" {
fn g_weak_ref_clear(weak_ref: &*const c_void);
}
if !self.element.is_null() {
unsafe {
g_weak_ref_clear(&self.element);
}
}
}
}
impl Drain for GstDebugDrain {
type Error = Never;
fn log(&self, record: &Record, _: &OwnedKeyValueList) -> Result<(), Never> {
extern "C" {
fn gst_rs_debug_log(category: *const c_void,
level: u32,
file: *const c_char,
function: *const c_char,
line: u32,
object: *const c_void,
message: *const c_char);
}
let file_cstr = CString::new(record.file().as_bytes()).unwrap();
// TODO: Probably want to include module?
let function_cstr = CString::new(record.function().as_bytes()).unwrap();
let message_cstr = CString::new(fmt::format(record.msg()).as_bytes()).unwrap();
let level = match record.level() {
Level::Critical | Level::Error => 1,
Level::Warning => 2,
Level::Info => 4,
Level::Debug => 5,
Level::Trace => 7,
};
extern "C" {
fn g_weak_ref_get(weak_ref: &*const c_void) -> *const c_void;
fn gst_object_unref(obj: *const c_void);
}
unsafe {
let element = if self.element.is_null() {
ptr::null()
} else {
g_weak_ref_get(&self.element)
};
gst_rs_debug_log(self.category,
level,
file_cstr.as_ptr(),
function_cstr.as_ptr(),
record.line(),
element,
message_cstr.as_ptr());
if !element.is_null() {
gst_object_unref(element);
}
}
Ok(())
}
}
unsafe impl Sync for GstDebugDrain {}
unsafe impl Send for GstDebugDrain {}

View file

@ -25,7 +25,7 @@ impl Plugin {
Plugin(plugin)
}
pub unsafe fn to_raw(&self) -> *const c_void {
pub unsafe fn as_ptr(&self) -> *const c_void {
self.0
}
}

View file

@ -177,9 +177,10 @@ impl SinkWrapper {
#[no_mangle]
pub extern "C" fn sink_new(sink: *mut c_void,
create_instance: fn() -> Box<Sink>)
create_instance: fn(Element) -> Box<Sink>)
-> *mut SinkWrapper {
Box::into_raw(Box::new(SinkWrapper::new(sink, create_instance())))
let instance = create_instance(unsafe { Element::new(sink) });
Box::into_raw(Box::new(SinkWrapper::new(sink, instance)))
}
#[no_mangle]
@ -258,7 +259,7 @@ pub struct SinkInfo<'a> {
pub classification: &'a str,
pub author: &'a str,
pub rank: i32,
pub create_instance: fn() -> Box<Sink>,
pub create_instance: fn(Element) -> Box<Sink>,
pub protocols: &'a str,
}
@ -284,7 +285,7 @@ pub fn sink_register(plugin: &Plugin, sink_info: &SinkInfo) {
let cprotocols = CString::new(sink_info.protocols).unwrap();
unsafe {
gst_rs_sink_register(plugin.to_raw(),
gst_rs_sink_register(plugin.as_ptr(),
cname.as_ptr(),
clong_name.as_ptr(),
cdescription.as_ptr(),

View file

@ -201,9 +201,11 @@ impl SourceWrapper {
#[no_mangle]
pub extern "C" fn source_new(source: *mut c_void,
create_instance: fn() -> Box<Source>)
create_instance: fn(Element) -> Box<Source>)
-> *mut SourceWrapper {
Box::into_raw(Box::new(SourceWrapper::new(source, create_instance())))
let instance = create_instance(unsafe { Element::new(source) });
Box::into_raw(Box::new(SourceWrapper::new(source, instance)))
}
#[no_mangle]
@ -311,7 +313,7 @@ pub struct SourceInfo<'a> {
pub classification: &'a str,
pub author: &'a str,
pub rank: i32,
pub create_instance: fn() -> Box<Source>,
pub create_instance: fn(Element) -> Box<Source>,
pub protocols: &'a str,
pub push_only: bool,
}
@ -340,7 +342,7 @@ pub fn source_register(plugin: &Plugin, source_info: &SourceInfo) {
let cprotocols = CString::new(source_info.protocols).unwrap();
unsafe {
gst_rs_source_register(plugin.to_raw(),
gst_rs_source_register(plugin.as_ptr(),
cname.as_ptr(),
clong_name.as_ptr(),
cdescription.as_ptr(),

View file

@ -17,6 +17,7 @@
//
//
use libc::c_char;
use std::os::raw::c_void;
use std::ffi::CString;
use std::i32;
@ -48,6 +49,46 @@ impl GBoolean {
}
}
pub struct Element(*const c_void);
impl Element {
pub unsafe fn new(element: *const c_void) -> Element {
extern "C" {
fn gst_object_ref(object: *const c_void) -> *const c_void;
}
if element.is_null() {
panic!("NULL not allowed");
}
gst_object_ref(element);
Element(element)
}
pub unsafe fn as_ptr(&self) -> *const c_void {
self.0
}
}
impl Drop for Element {
fn drop(&mut self) {
extern "C" {
fn gst_object_unref(object: *const c_void);
}
unsafe {
gst_object_unref(self.0);
}
}
}
impl Clone for Element {
fn clone(&self) -> Self {
unsafe { Element::new(self.0) }
}
}
#[no_mangle]
pub unsafe extern "C" fn cstring_drop(ptr: *mut c_char) {
let _ = CString::from_raw(ptr);