tutorials: Set up a runloop on macOS

Fixes https://github.com/sdroege/gstreamer-rs/pull/62
Fixes https://github.com/sdroege/gstreamer-rs/issues/58
This commit is contained in:
Kornel 2017-11-12 13:53:50 +00:00 committed by Sebastian Dröge
parent d4bd1c2d76
commit 10151b9f0d
7 changed files with 83 additions and 8 deletions

View file

@ -1,7 +1,10 @@
extern crate gstreamer as gst;
use gst::prelude::*;
fn main() {
#[path = "../tutorials-common.rs"]
mod tutorials_common;
fn tutorial_main() {
// Initialize GStreamer
gst::init().unwrap();
@ -38,3 +41,9 @@ fn main() {
let ret = pipeline.set_state(gst::State::Null);
assert_ne!(ret, gst::StateChangeReturn::Failure);
}
fn main() {
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
tutorials_common::run(tutorial_main);
}

View file

@ -1,8 +1,10 @@
extern crate gstreamer as gst;
use gst::prelude::*;
fn main() {
#[path = "../tutorials-common.rs"]
mod tutorials_common;
fn tutorial_main() {
// Initialize GStreamer
gst::init().unwrap();
@ -56,3 +58,10 @@ fn main() {
"Unable to set the pipeline to the Null state."
);
}
fn main() {
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
tutorials_common::run(tutorial_main);
}

View file

@ -1,8 +1,10 @@
extern crate gstreamer as gst;
use gst::prelude::*;
fn main() {
#[path = "../tutorials-common.rs"]
mod tutorials_common;
fn tutorial_main() {
// Initialize GStreamer
gst::init().unwrap();
@ -116,3 +118,9 @@ fn main() {
"Unable to set the pipeline to the Null state."
);
}
fn main() {
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
tutorials_common::run(tutorial_main);
}

View file

@ -4,6 +4,9 @@ use std::io::Write;
use gst::prelude::*;
use gst::MessageView;
#[path = "../tutorials-common.rs"]
mod tutorials_common;
struct CustomData {
playbin: gst::Element, // Our one and only element
playing: bool, // Are we in the PLAYING state?
@ -13,7 +16,7 @@ struct CustomData {
duration: gst::ClockTime, // How long does this media last, in nanoseconds
}
fn main() {
fn tutorial_main() {
// Initialize GStreamer
gst::init().unwrap();
@ -147,3 +150,9 @@ fn handle_message(custom_data: &mut CustomData, msg: &gst::GstRc<gst::MessageRef
_ => (),
}
}
fn main() {
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
tutorials_common::run(tutorial_main);
}

View file

@ -2,6 +2,8 @@ extern crate gstreamer as gst;
use gst::prelude::*;
use gst::MessageView;
#[path = "../tutorials-common.rs"]
mod tutorials_common;
fn print_caps(caps: &gst::Caps, prefix: &str) {
if caps.is_any() {
@ -76,7 +78,7 @@ fn print_pad_capabilities(element: &gst::Element, pad_name: &str) {
}
}
fn main() {
fn tutorial_main() {
// Initialize GStreamer
gst::init().unwrap();
@ -155,3 +157,9 @@ fn main() {
let ret = pipeline.set_state(gst::State::Null);
assert_ne!(ret, gst::StateChangeReturn::Failure);
}
fn main() {
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
tutorials_common::run(tutorial_main);
}

View file

@ -1,7 +1,10 @@
extern crate gstreamer as gst;
use gst::prelude::*;
fn main() {
#[path = "../tutorials-common.rs"]
mod tutorials_common;
fn tutorial_main() {
// Initialize GStreamer
if let Err(err) = gst::init() {
eprintln!("Failed to initialize Gst: {}", err);
@ -92,3 +95,9 @@ fn main() {
.into_result()
.expect("Unable to set the pipeline to the Null state.");
}
fn main() {
// tutorials_common::run is only required to set up the application environment on macOS
// (but not necessary in normal Cocoa applications where this is set up automatically)
tutorials_common::run(tutorial_main);
}

View file

@ -0,0 +1,23 @@
/// macOS has a specific requirement that there must be a run loop running
/// on the main thread in order to open windows and use OpenGL.
#[cfg(target_os = "macos")]
#[link(name = "foundation", kind = "framework")]
extern "C" {
fn CFRunLoopRun();
}
/// On macOS this launches the callback function on a thread.
/// On other platforms it's just executed immediately.
#[cfg(not(target_os = "macos"))]
pub fn run<F: FnOnce() + Send + 'static>(main: F) {
main();
}
#[cfg(target_os = "macos")]
pub fn run<F: FnOnce() + Send + 'static>(main: F) {
::std::thread::spawn(main);
unsafe {
CFRunLoopRun();
}
}