Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sink.rs « src « gst-plugin-simple - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea70e691f13b5abfaf9547daf9711d309b23f82c (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (C) 2016-2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensink.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::sync::Mutex;

use url::Url;

use glib;
use gst;
use gst::prelude::*;
use gst_base::prelude::*;

use error::*;
use gst_plugin::base_sink::*;
use gst_plugin::element::*;
use gst_plugin::object::*;
use gst_plugin::properties::*;
use gst_plugin::uri_handler::*;

pub use gst_plugin::base_sink::BaseSink;

use UriValidator;

pub trait SinkImpl: Send + 'static {
    fn uri_validator(&self) -> Box<UriValidator>;

    fn start(&mut self, sink: &BaseSink, uri: Url) -> Result<(), gst::ErrorMessage>;
    fn stop(&mut self, sink: &BaseSink) -> Result<(), gst::ErrorMessage>;
    fn render(&mut self, sink: &BaseSink, buffer: &gst::BufferRef) -> Result<(), FlowError>;
}

struct Sink {
    cat: gst::DebugCategory,
    uri: Mutex<(Option<Url>, bool)>,
    uri_validator: Box<UriValidator>,
    imp: Mutex<Box<SinkImpl>>,
}

static PROPERTIES: [Property; 1] = [Property::String(
    "uri",
    "URI",
    "URI to read from",
    None,
    PropertyMutability::ReadWrite,
)];

impl Sink {
    fn new(sink: &BaseSink, sink_info: &SinkInfo) -> Self {
        let sink_impl = (sink_info.create_instance)(sink);

        Self {
            cat: gst::DebugCategory::new(
                "rssink",
                gst::DebugColorFlags::empty(),
                "Rust sink base class",
            ),
            uri: Mutex::new((None, false)),
            uri_validator: sink_impl.uri_validator(),
            imp: Mutex::new(sink_impl),
        }
    }

    fn class_init(klass: &mut BaseSinkClass, sink_info: &SinkInfo) {
        klass.set_metadata(
            &sink_info.long_name,
            &sink_info.classification,
            &sink_info.description,
            &sink_info.author,
        );

        let caps = gst::Caps::new_any();
        let pad_template = gst::PadTemplate::new(
            "sink",
            gst::PadDirection::Sink,
            gst::PadPresence::Always,
            &caps,
        );
        klass.add_pad_template(pad_template);

        klass.install_properties(&PROPERTIES);
    }

    fn init(element: &BaseSink, sink_info: &SinkInfo) -> Box<BaseSinkImpl<BaseSink>> {
        element.set_blocksize(4096);

        let imp = Self::new(element, sink_info);
        Box::new(imp)
    }

    fn get_uri(&self, _element: &glib::Object) -> Option<String> {
        let uri_storage = &self.uri.lock().unwrap();
        uri_storage.0.as_ref().map(|uri| String::from(uri.as_str()))
    }

    fn set_uri(&self, element: &glib::Object, uri_str: Option<String>) -> Result<(), glib::Error> {
        let sink = element.clone().dynamic_cast::<BaseSink>().unwrap();

        let uri_storage = &mut self.uri.lock().unwrap();

        gst_debug!(self.cat, obj: &sink, "Setting URI {:?}", uri_str);

        if uri_storage.1 {
            return Err(
                UriError::new(gst::URIError::BadState, "Already started".to_string()).into(),
            );
        }

        uri_storage.0 = None;

        if let Some(uri_str) = uri_str {
            match Url::parse(uri_str.as_str()) {
                Ok(uri) => {
                    try!((self.uri_validator)(&uri).map_err(|e| e.into()));
                    uri_storage.0 = Some(uri);
                    Ok(())
                }
                Err(err) => Err(UriError::new(
                    gst::URIError::BadUri,
                    format!("Failed to parse URI '{}': {}", uri_str, err),
                ).into()),
            }
        } else {
            Ok(())
        }
    }
}

impl ObjectImpl<BaseSink> for Sink {
    fn set_property(&self, obj: &glib::Object, id: u32, value: &glib::Value) {
        let prop = &PROPERTIES[id as usize];

        match *prop {
            Property::String("uri", ..) => {
                self.set_uri(obj, value.get()).unwrap();
            }
            _ => unimplemented!(),
        }
    }

    fn get_property(&self, obj: &glib::Object, id: u32) -> Result<glib::Value, ()> {
        let prop = &PROPERTIES[id as usize];

        match *prop {
            Property::String("uri", ..) => Ok(self.get_uri(obj).to_value()),
            _ => unimplemented!(),
        }
    }
}

impl ElementImpl<BaseSink> for Sink {}

impl BaseSinkImpl<BaseSink> for Sink {
    fn start(&self, sink: &BaseSink) -> bool {
        gst_debug!(self.cat, obj: sink, "Starting");

        // Don't keep the URI locked while we call start later
        let uri = match *self.uri.lock().unwrap() {
            (Some(ref uri), ref mut started) => {
                *started = true;
                uri.clone()
            }
            (None, _) => {
                gst_error!(self.cat, obj: sink, "No URI given");
                gst_element_error!(sink, gst::ResourceError::OpenRead, ["No URI given"]);
                return false;
            }
        };

        let sink_impl = &mut self.imp.lock().unwrap();
        match sink_impl.start(sink, uri) {
            Ok(..) => {
                gst_trace!(self.cat, obj: sink, "Started successfully");
                true
            }
            Err(ref msg) => {
                gst_error!(self.cat, obj: sink, "Failed to start: {:?}", msg);

                self.uri.lock().unwrap().1 = false;
                sink.post_error_message(msg);
                false
            }
        }
    }

    fn stop(&self, sink: &BaseSink) -> bool {
        let sink_impl = &mut self.imp.lock().unwrap();

        gst_debug!(self.cat, obj: sink, "Stopping");

        match sink_impl.stop(sink) {
            Ok(..) => {
                gst_trace!(self.cat, obj: sink, "Stopped successfully");
                self.uri.lock().unwrap().1 = false;
                true
            }
            Err(ref msg) => {
                gst_error!(self.cat, obj: sink, "Failed to stop: {:?}", msg);

                sink.post_error_message(msg);
                false
            }
        }
    }

    fn render(&self, sink: &BaseSink, buffer: &gst::BufferRef) -> gst::FlowReturn {
        let sink_impl = &mut self.imp.lock().unwrap();

        gst_trace!(self.cat, obj: sink, "Rendering buffer {:?}", buffer,);

        match sink_impl.render(sink, buffer) {
            Ok(()) => gst::FlowReturn::Ok,
            Err(flow_error) => {
                gst_error!(self.cat, obj: sink, "Failed to render: {:?}", flow_error);
                match flow_error {
                    FlowError::NotNegotiated(ref msg) | FlowError::Error(ref msg) => {
                        sink.post_error_message(msg);
                    }
                    _ => (),
                }
                flow_error.into()
            }
        }
    }
}

impl URIHandlerImpl for Sink {
    fn get_uri(&self, element: &gst::URIHandler) -> Option<String> {
        Sink::get_uri(self, &element.clone().upcast())
    }

    fn set_uri(&self, element: &gst::URIHandler, uri: Option<String>) -> Result<(), glib::Error> {
        Sink::set_uri(self, &element.clone().upcast(), uri)
    }
}

pub struct SinkInfo {
    pub name: String,
    pub long_name: String,
    pub description: String,
    pub classification: String,
    pub author: String,
    pub rank: u32,
    pub create_instance: fn(&BaseSink) -> Box<SinkImpl>,
    pub protocols: Vec<String>,
}

struct SinkStatic {
    name: String,
    sink_info: SinkInfo,
}

impl ImplTypeStatic<BaseSink> for SinkStatic {
    fn get_name(&self) -> &str {
        self.name.as_str()
    }

    fn new(&self, element: &BaseSink) -> Box<BaseSinkImpl<BaseSink>> {
        Sink::init(element, &self.sink_info)
    }

    fn class_init(&self, klass: &mut BaseSinkClass) {
        Sink::class_init(klass, &self.sink_info);
    }

    fn type_init(&self, token: &TypeInitToken, type_: glib::Type) {
        register_uri_handler(token, type_, self);
    }
}

impl URIHandlerImplStatic<BaseSink> for SinkStatic {
    fn get_impl<'a>(&self, imp: &'a Box<BaseSinkImpl<BaseSink>>) -> &'a URIHandlerImpl {
        imp.downcast_ref::<Sink>().unwrap()
    }

    fn get_type(&self) -> gst::URIType {
        gst::URIType::Sink
    }

    fn get_protocols(&self) -> Vec<String> {
        self.sink_info.protocols.clone()
    }
}

pub fn sink_register(plugin: &gst::Plugin, sink_info: SinkInfo) {
    let name = sink_info.name.clone();
    let rank = sink_info.rank;

    let sink_static = SinkStatic {
        name: format!("Sink-{}", name),
        sink_info: sink_info,
    };

    let type_ = register_type(sink_static);
    gst::Element::register(plugin, &name, rank, type_);
}