1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
extern crate gstreamer as gst;
use gst::prelude::*;

extern crate glib;

use std::env;

#[path = "../examples-common.rs"]
mod examples_common;

fn example_main() {
    gst::init().unwrap();

    let args: Vec<_> = env::args().collect();
    let uri: &str = if args.len() == 2 {
        args[1].as_ref()
    } else {
        println!("Usage: toc file_path");
        std::process::exit(-1)
    };

    let pipeline = gst::Pipeline::new(None);
    let src = gst::ElementFactory::make("filesrc", None).unwrap();
    let decodebin = gst::ElementFactory::make("decodebin", None).unwrap();

    src.set_property("location", &glib::Value::from(uri))
        .unwrap();

    pipeline.add_many(&[&src, &decodebin]).unwrap();
    gst::Element::link_many(&[&src, &decodebin]).unwrap();

    // Need to move a new reference into the closure
    let pipeline_weak = pipeline.downgrade();
    decodebin.connect_pad_added(move |_, src_pad| {
        let pipeline = match pipeline_weak.upgrade() {
            Some(pipeline) => pipeline,
            None => return,
        };
        let queue = gst::ElementFactory::make("queue", None).unwrap();
        let sink = gst::ElementFactory::make("fakesink", None).unwrap();

        let elements = &[&queue, &sink];
        pipeline.add_many(elements).unwrap();
        gst::Element::link_many(elements).unwrap();

        for e in elements {
            e.sync_state_with_parent().unwrap();
        }

        let sink_pad = queue.get_static_pad("sink").unwrap();
        assert_eq!(src_pad.link(&sink_pad), gst::PadLinkReturn::Ok);
    });

    assert_ne!(
        pipeline.set_state(gst::State::Paused),
        gst::StateChangeReturn::Failure
    );

    let bus = pipeline.get_bus().unwrap();

    while let Some(msg) = bus.timed_pop(gst::CLOCK_TIME_NONE) {
        use gst::MessageView;

        match msg.view() {
            MessageView::Eos(_) | MessageView::AsyncDone(_) => break,
            MessageView::Error(err) => {
                println!(
                    "Error from {:?}: {} ({:?})",
                    err.get_src().map(|s| s.get_path_string()),
                    err.get_error(),
                    err.get_debug()
                );
                break;
            }
            MessageView::Toc(msg_toc) => {
                let (toc, updated) = msg_toc.get_toc();
                println!(
                    "\nReceived toc: {:?} - updated: {}",
                    toc.get_scope(),
                    updated
                );
                if let Some(tags) = toc.get_tags() {
                    println!("- tags: {}", tags.to_string());
                }
                for toc_entry in toc.get_entries() {
                    println!(
                        "\t{:?} - {}",
                        toc_entry.get_entry_type(),
                        toc_entry.get_uid()
                    );
                    if let Some((start, stop)) = toc_entry.get_start_stop_times() {
                        println!("\t- start: {}, stop: {}", start, stop);
                    }
                    if let Some(tags) = toc_entry.get_tags() {
                        println!("\t- tags: {}", tags.to_string());
                    }
                    for toc_sub_entry in toc_entry.get_sub_entries() {
                        println!(
                            "\n\t\t{:?} - {}",
                            toc_sub_entry.get_entry_type(),
                            toc_sub_entry.get_uid()
                        );
                        if let Some((start, stop)) = toc_sub_entry.get_start_stop_times() {
                            println!("\t\t- start: {}, stop: {}", start, stop);
                        }
                        if let Some(tags) = toc_sub_entry.get_tags() {
                            println!("\t\t- tags: {:?}", tags.to_string());
                        }
                    }
                }
            }
            _ => (),
        }
    }

    assert_ne!(
        pipeline.set_state(gst::State::Null),
        gst::StateChangeReturn::Failure
    );
}

fn main() {
    // tutorials_common::run is only required to set up the application environent on macOS
    // (but not necessary in normal Cocoa applications where this is set up autmatically)
    examples_common::run(example_main);
}