Commit e43bf21d authored by Sebastian Dröge's avatar Sebastian Dröge 🍵

Add initialization safety assertions

Ensure that GStreamer is initialized before allowing to call any
functions
parent 992105b4
......@@ -6,6 +6,7 @@ min_cfg_version = "1.8"
target_path = "gstreamer"
work_mode = "normal"
concurrency = "send+sync"
generate_safety_asserts = true
external_libraries = [
"GLib",
......
......@@ -27,6 +27,7 @@ glib_wrapper! {
impl Bin {
pub fn new<'a, P: Into<Option<&'a str>>>(name: P) -> Bin {
assert_initialized_main_thread!();
let name = name.into();
let name = name.to_glib_none();
unsafe {
......
......@@ -21,6 +21,7 @@ glib_wrapper! {
impl Bus {
pub fn new() -> Bus {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_bus_new())
}
......
......@@ -21,6 +21,7 @@ glib_wrapper! {
impl DeviceMonitor {
pub fn new() -> DeviceMonitor {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_device_monitor_new())
}
......
......@@ -27,6 +27,7 @@ glib_wrapper! {
impl DeviceProvider {
pub fn register<'a, P: Into<Option<&'a Plugin>>>(plugin: P, name: &str, rank: u32, type_: glib::types::Type) -> bool {
assert_initialized_main_thread!();
let plugin = plugin.into();
let plugin = plugin.to_glib_none();
unsafe {
......
......@@ -18,12 +18,14 @@ glib_wrapper! {
impl DeviceProviderFactory {
pub fn find(name: &str) -> Option<DeviceProviderFactory> {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_device_provider_factory_find(name.to_glib_none().0))
}
}
pub fn get_by_name(factoryname: &str) -> Option<DeviceProvider> {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_device_provider_factory_get_by_name(factoryname.to_glib_none().0))
}
......
......@@ -42,6 +42,7 @@ glib_wrapper! {
impl Element {
pub fn make_from_uri<'a, P: Into<Option<&'a str>>>(type_: URIType, uri: &str, elementname: P) -> Result<Element, Error> {
assert_initialized_main_thread!();
let elementname = elementname.into();
let elementname = elementname.to_glib_none();
unsafe {
......@@ -52,6 +53,7 @@ impl Element {
}
pub fn register<'a, P: Into<Option<&'a Plugin>>>(plugin: P, name: &str, rank: u32, type_: glib::types::Type) -> bool {
assert_initialized_main_thread!();
let plugin = plugin.into();
let plugin = plugin.to_glib_none();
unsafe {
......@@ -60,12 +62,14 @@ impl Element {
}
pub fn state_change_return_get_name(state_ret: StateChangeReturn) -> Option<String> {
assert_initialized_main_thread!();
unsafe {
from_glib_none(ffi::gst_element_state_change_return_get_name(state_ret.to_glib()))
}
}
pub fn state_get_name(state: State) -> Option<String> {
assert_initialized_main_thread!();
unsafe {
from_glib_none(ffi::gst_element_state_get_name(state.to_glib()))
}
......
......@@ -95,6 +95,7 @@ impl ElementFactory {
}
pub fn find(name: &str) -> Option<ElementFactory> {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_element_factory_find(name.to_glib_none().0))
}
......@@ -109,6 +110,7 @@ impl ElementFactory {
//}
pub fn make<'a, P: Into<Option<&'a str>>>(factoryname: &str, name: P) -> Option<Element> {
assert_initialized_main_thread!();
let name = name.into();
let name = name.to_glib_none();
unsafe {
......
......@@ -35,6 +35,7 @@ impl ToGlib for BufferingMode {
#[doc(hidden)]
impl FromGlib<ffi::GstBufferingMode> for BufferingMode {
fn from_glib(value: ffi::GstBufferingMode) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => BufferingMode::Stream,
1 => BufferingMode::Download,
......@@ -95,6 +96,7 @@ impl ToGlib for CoreError {
#[doc(hidden)]
impl FromGlib<ffi::GstCoreError> for CoreError {
fn from_glib(value: ffi::GstCoreError) -> Self {
skip_assert_initialized!();
match value as i32 {
1 => CoreError::Failed,
2 => CoreError::TooLazy,
......@@ -118,6 +120,7 @@ impl FromGlib<ffi::GstCoreError> for CoreError {
impl ErrorDomain for CoreError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_core_error_quark() }
}
......@@ -126,6 +129,7 @@ impl ErrorDomain for CoreError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
1 => Some(CoreError::Failed),
2 => Some(CoreError::TooLazy),
......@@ -193,6 +197,7 @@ impl ToGlib for FlowReturn {
#[doc(hidden)]
impl FromGlib<ffi::GstFlowReturn> for FlowReturn {
fn from_glib(value: ffi::GstFlowReturn) -> Self {
skip_assert_initialized!();
match value as i32 {
102 => FlowReturn::CustomSuccess2,
101 => FlowReturn::CustomSuccess1,
......@@ -244,6 +249,7 @@ impl ToGlib for Format {
#[doc(hidden)]
impl FromGlib<ffi::GstFormat> for Format {
fn from_glib(value: ffi::GstFormat) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => Format::Undefined,
1 => Format::Default,
......@@ -290,6 +296,7 @@ impl ToGlib for LibraryError {
#[doc(hidden)]
impl FromGlib<ffi::GstLibraryError> for LibraryError {
fn from_glib(value: ffi::GstLibraryError) -> Self {
skip_assert_initialized!();
match value as i32 {
1 => LibraryError::Failed,
2 => LibraryError::TooLazy,
......@@ -305,6 +312,7 @@ impl FromGlib<ffi::GstLibraryError> for LibraryError {
impl ErrorDomain for LibraryError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_library_error_quark() }
}
......@@ -313,6 +321,7 @@ impl ErrorDomain for LibraryError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
1 => Some(LibraryError::Failed),
2 => Some(LibraryError::TooLazy),
......@@ -352,6 +361,7 @@ impl ToGlib for PadDirection {
#[doc(hidden)]
impl FromGlib<ffi::GstPadDirection> for PadDirection {
fn from_glib(value: ffi::GstPadDirection) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => PadDirection::Unknown,
1 => PadDirection::Src,
......@@ -395,6 +405,7 @@ impl ToGlib for PadLinkReturn {
#[doc(hidden)]
impl FromGlib<ffi::GstPadLinkReturn> for PadLinkReturn {
fn from_glib(value: ffi::GstPadLinkReturn) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => PadLinkReturn::Ok,
-1 => PadLinkReturn::WrongHierarchy,
......@@ -444,6 +455,7 @@ impl ToGlib for ParseError {
#[doc(hidden)]
impl FromGlib<ffi::GstParseError> for ParseError {
fn from_glib(value: ffi::GstParseError) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => ParseError::Syntax,
1 => ParseError::NoSuchElement,
......@@ -460,6 +472,7 @@ impl FromGlib<ffi::GstParseError> for ParseError {
impl ErrorDomain for ParseError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_parse_error_quark() }
}
......@@ -468,6 +481,7 @@ impl ErrorDomain for ParseError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
0 => Some(ParseError::Syntax),
1 => Some(ParseError::NoSuchElement),
......@@ -508,6 +522,7 @@ impl ToGlib for PluginError {
#[doc(hidden)]
impl FromGlib<ffi::GstPluginError> for PluginError {
fn from_glib(value: ffi::GstPluginError) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => PluginError::Module,
1 => PluginError::Dependencies,
......@@ -519,6 +534,7 @@ impl FromGlib<ffi::GstPluginError> for PluginError {
impl ErrorDomain for PluginError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_plugin_error_quark() }
}
......@@ -527,6 +543,7 @@ impl ErrorDomain for PluginError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
0 => Some(PluginError::Module),
1 => Some(PluginError::Dependencies),
......@@ -566,6 +583,7 @@ impl ToGlib for ProgressType {
#[doc(hidden)]
impl FromGlib<ffi::GstProgressType> for ProgressType {
fn from_glib(value: ffi::GstProgressType) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => ProgressType::Start,
1 => ProgressType::Continue,
......@@ -629,6 +647,7 @@ impl ToGlib for ResourceError {
#[doc(hidden)]
impl FromGlib<ffi::GstResourceError> for ResourceError {
fn from_glib(value: ffi::GstResourceError) -> Self {
skip_assert_initialized!();
match value as i32 {
1 => ResourceError::Failed,
2 => ResourceError::TooLazy,
......@@ -653,6 +672,7 @@ impl FromGlib<ffi::GstResourceError> for ResourceError {
impl ErrorDomain for ResourceError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_resource_error_quark() }
}
......@@ -661,6 +681,7 @@ impl ErrorDomain for ResourceError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
1 => Some(ResourceError::Failed),
2 => Some(ResourceError::TooLazy),
......@@ -709,6 +730,7 @@ impl ToGlib for SeekType {
#[doc(hidden)]
impl FromGlib<ffi::GstSeekType> for SeekType {
fn from_glib(value: ffi::GstSeekType) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => SeekType::None,
1 => SeekType::Set,
......@@ -748,6 +770,7 @@ impl ToGlib for State {
#[doc(hidden)]
impl FromGlib<ffi::GstState> for State {
fn from_glib(value: ffi::GstState) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => State::VoidPending,
1 => State::Null,
......@@ -791,6 +814,7 @@ impl ToGlib for StateChange {
#[doc(hidden)]
impl FromGlib<ffi::GstStateChange> for StateChange {
fn from_glib(value: ffi::GstStateChange) -> Self {
skip_assert_initialized!();
match value as i32 {
10 => StateChange::NullToReady,
19 => StateChange::ReadyToPaused,
......@@ -831,6 +855,7 @@ impl ToGlib for StateChangeReturn {
#[doc(hidden)]
impl FromGlib<ffi::GstStateChangeReturn> for StateChangeReturn {
fn from_glib(value: ffi::GstStateChangeReturn) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => StateChangeReturn::Failure,
1 => StateChangeReturn::Success,
......@@ -889,6 +914,7 @@ impl ToGlib for StreamError {
#[doc(hidden)]
impl FromGlib<ffi::GstStreamError> for StreamError {
fn from_glib(value: ffi::GstStreamError) -> Self {
skip_assert_initialized!();
match value as i32 {
1 => StreamError::Failed,
2 => StreamError::TooLazy,
......@@ -911,6 +937,7 @@ impl FromGlib<ffi::GstStreamError> for StreamError {
impl ErrorDomain for StreamError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_stream_error_quark() }
}
......@@ -919,6 +946,7 @@ impl ErrorDomain for StreamError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
1 => Some(StreamError::Failed),
2 => Some(StreamError::TooLazy),
......@@ -973,6 +1001,7 @@ impl ToGlib for StreamStatusType {
#[doc(hidden)]
impl FromGlib<ffi::GstStreamStatusType> for StreamStatusType {
fn from_glib(value: ffi::GstStreamStatusType) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => StreamStatusType::Create,
1 => StreamStatusType::Enter,
......@@ -1010,6 +1039,7 @@ impl ToGlib for StructureChangeType {
#[doc(hidden)]
impl FromGlib<ffi::GstStructureChangeType> for StructureChangeType {
fn from_glib(value: ffi::GstStructureChangeType) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => StructureChangeType::Link,
1 => StructureChangeType::Unlink,
......@@ -1046,6 +1076,7 @@ impl ToGlib for URIError {
#[doc(hidden)]
impl FromGlib<ffi::GstURIError> for URIError {
fn from_glib(value: ffi::GstURIError) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => URIError::UnsupportedProtocol,
1 => URIError::BadUri,
......@@ -1058,6 +1089,7 @@ impl FromGlib<ffi::GstURIError> for URIError {
impl ErrorDomain for URIError {
fn domain() -> glib_ffi::GQuark {
skip_assert_initialized!();
unsafe { ffi::gst_uri_error_quark() }
}
......@@ -1066,6 +1098,7 @@ impl ErrorDomain for URIError {
}
fn from(code: i32) -> Option<Self> {
skip_assert_initialized!();
match code {
0 => Some(URIError::UnsupportedProtocol),
1 => Some(URIError::BadUri),
......@@ -1102,6 +1135,7 @@ impl ToGlib for URIType {
#[doc(hidden)]
impl FromGlib<ffi::GstURIType> for URIType {
fn from_glib(value: ffi::GstURIType) -> Self {
skip_assert_initialized!();
match value as i32 {
0 => URIType::Unknown,
1 => URIType::Sink,
......
......@@ -33,6 +33,7 @@ impl ToGlib for SeekFlags {
#[doc(hidden)]
impl FromGlib<ffi::GstSeekFlags> for SeekFlags {
fn from_glib(value: ffi::GstSeekFlags) -> SeekFlags {
skip_assert_initialized!();
SeekFlags::from_bits_truncate(value.bits())
}
}
......@@ -58,6 +59,7 @@ impl ToGlib for StreamFlags {
#[doc(hidden)]
impl FromGlib<ffi::GstStreamFlags> for StreamFlags {
fn from_glib(value: ffi::GstStreamFlags) -> StreamFlags {
skip_assert_initialized!();
StreamFlags::from_bits_truncate(value.bits())
}
}
......@@ -84,6 +86,7 @@ impl ToGlib for StreamType {
#[doc(hidden)]
impl FromGlib<ffi::GstStreamType> for StreamType {
fn from_glib(value: ffi::GstStreamType) -> StreamType {
skip_assert_initialized!();
StreamType::from_bits_truncate(value.bits())
}
}
......
This diff is collapsed.
......@@ -18,6 +18,7 @@ glib_wrapper! {
impl Object {
pub fn check_uniqueness(list: &[Object], name: &str) -> bool {
assert_initialized_main_thread!();
unsafe {
from_glib(ffi::gst_object_check_uniqueness(list.to_glib_none().0, name.to_glib_none().0))
}
......
......@@ -34,6 +34,7 @@ glib_wrapper! {
impl Pad {
pub fn new<'a, P: Into<Option<&'a str>>>(name: P, direction: PadDirection) -> Pad {
assert_initialized_main_thread!();
let name = name.into();
let name = name.to_glib_none();
unsafe {
......@@ -46,6 +47,7 @@ impl Pad {
//}
pub fn new_from_template<'a, P: Into<Option<&'a str>>>(templ: &PadTemplate, name: P) -> Pad {
skip_assert_initialized!();
let name = name.into();
let name = name.to_glib_none();
unsafe {
......@@ -54,6 +56,7 @@ impl Pad {
}
pub fn link_get_name(ret: PadLinkReturn) -> Option<String> {
assert_initialized_main_thread!();
unsafe {
from_glib_none(ffi::gst_pad_link_get_name(ret.to_glib()))
}
......
......@@ -21,6 +21,7 @@ glib_wrapper! {
impl Pipeline {
pub fn new<'a, P: Into<Option<&'a str>>>(name: P) -> Pipeline {
assert_initialized_main_thread!();
let name = name.into();
let name = name.to_glib_none();
unsafe {
......
......@@ -98,18 +98,21 @@ impl Plugin {
}
pub fn list_free(list: &[Plugin]) {
assert_initialized_main_thread!();
unsafe {
ffi::gst_plugin_list_free(list.to_glib_full());
}
}
pub fn load_by_name(name: &str) -> Option<Plugin> {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_plugin_load_by_name(name.to_glib_none().0))
}
}
pub fn load_file(filename: &str) -> Result<Plugin, Error> {
assert_initialized_main_thread!();
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::gst_plugin_load_file(filename.to_glib_none().0, &mut error);
......
......@@ -22,6 +22,7 @@ glib_wrapper! {
impl StreamCollection {
#[cfg(feature = "v1_10")]
pub fn new<'a, P: Into<Option<&'a str>>>(upstream_id: P) -> StreamCollection {
assert_initialized_main_thread!();
let upstream_id = upstream_id.into();
let upstream_id = upstream_id.to_glib_none();
unsafe {
......
......@@ -25,6 +25,17 @@ macro_rules! callback_guard {
)
}
macro_rules! assert_initialized_main_thread {
() => (
assert_eq!(unsafe {ffi::gst_is_initialized()}, ::glib_ffi::GTRUE)
)
}
macro_rules! skip_assert_initialized {
() => (
)
}
pub use glib::{
Cast,
Continue,
......
......@@ -724,6 +724,7 @@ macro_rules! message_builder_generic_impl {
}
pub fn build(mut self) -> Message {
assert_initialized_main_thread!();
unsafe {
let src = self.src.to_glib_none().0;
let msg = $new_fn(&mut self, src);
......
......@@ -24,6 +24,7 @@ pub struct Structure(*mut StructureRef, PhantomData<StructureRef>, bool);
impl Structure {
pub fn new_empty(name: &str) -> Structure {
assert_initialized_main_thread!();
Structure(
unsafe { ffi::gst_structure_new_empty(name.to_glib_none().0) as *mut StructureRef },
PhantomData,
......@@ -32,6 +33,7 @@ impl Structure {
}
pub fn new(name: &str, values: &[(&str, &Value)]) -> Structure {
assert_initialized_main_thread!();
let mut structure = Structure::new_empty(name);
for &(f, v) in values {
......@@ -42,6 +44,7 @@ impl Structure {
}
pub fn from_string(s: &str) -> Option<Structure> {
assert_initialized_main_thread!();
unsafe {
let structure = ffi::gst_structure_from_string(s.to_glib_none().0, ptr::null_mut());
if structure.is_null() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment