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

s3src.rs « src « gst-plugin-s3 - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9933e4016a724dd1a9918a485631f0215501bfa9 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (C) 2017 Author: Arun Raghavan <arun@arunraghavan.net>
//
// 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://opensource.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 bytes::Bytes;
use futures::sync::oneshot;
use futures::{Future, Stream};
use rusoto_s3::*;
use tokio::runtime;

use glib::prelude::*;
use glib::subclass;
use glib::subclass::prelude::*;

use gst;
use gst::subclass::prelude::*;

use gst_base;
use gst_base::prelude::*;
use gst_base::subclass::prelude::*;

use crate::s3url::*;
use crate::s3utils;

enum StreamingState {
    Stopped,
    Started {
        url: GstS3Url,
        client: S3Client,
        size: u64,
    },
}

pub struct S3Src {
    url: Mutex<Option<GstS3Url>>,
    state: Mutex<StreamingState>,
    cat: gst::DebugCategory,
    runtime: runtime::Runtime,
    canceller: Mutex<Option<oneshot::Sender<Bytes>>>,
}

static PROPERTIES: [subclass::Property; 1] = [subclass::Property("uri", |name| {
    glib::ParamSpec::string(
        name,
        "URI",
        "The S3 object URI",
        None,
        glib::ParamFlags::READWRITE, /* + GST_PARAM_MUTABLE_READY) */
    )
})];

impl S3Src {
    fn cancel(&self) {
        let mut canceller = self.canceller.lock().unwrap();

        if let Some(_) = canceller.take() {
            /* We don't do anything, the Sender will be dropped, and that will cause the
             * Receiver to be cancelled */
        }
    }

    fn connect(self: &S3Src, url: &GstS3Url) -> Result<S3Client, gst::ErrorMessage> {
        Ok(S3Client::new(url.region.clone()))
    }

    fn set_uri(self: &S3Src, _: &gst_base::BaseSrc, url_str: &str) -> Result<(), glib::Error> {
        let state = self.state.lock().unwrap();

        if let StreamingState::Started { .. } = *state {
            return Err(gst::Error::new(
                gst::URIError::BadState,
                "Cannot set URI on a started s3src",
            ));
        }

        let mut url = self.url.lock().unwrap();

        match parse_s3_url(&url_str) {
            Ok(s3url) => {
                *url = Some(s3url);
                Ok(())
            }
            Err(_) => Err(gst::Error::new(
                gst::URIError::BadUri,
                "Could not parse URI",
            )),
        }
    }

    fn head(
        self: &S3Src,
        src: &gst_base::BaseSrc,
        client: &S3Client,
        url: &GstS3Url,
    ) -> Result<u64, gst::ErrorMessage> {
        let request = HeadObjectRequest {
            bucket: url.bucket.clone(),
            key: url.object.clone(),
            version_id: url.version.clone(),
            ..Default::default()
        };

        let response = client.head_object(request);

        let output = s3utils::wait(
            &self.canceller,
            &self.runtime,
            response.map_err(|err| {
                gst_error_msg!(
                    gst::ResourceError::NotFound,
                    ["Failed to HEAD object: {}", err]
                )
            }),
        )
        .map_err(|err| {
            err.unwrap_or(gst_error_msg!(
                gst::LibraryError::Failed,
                ["Interrupted during start"]
            ))
        })?;

        if let Some(size) = output.content_length {
            gst_info!(
                self.cat,
                obj: src,
                "HEAD success, content length = {}",
                size
            );
            Ok(size as u64)
        } else {
            Err(gst_error_msg!(
                gst::ResourceError::Read,
                ["Failed to get content length"]
            ))
        }
    }

    /* Returns the bytes, Some(error) if one occured, or a None error if interrupted */
    fn get(
        self: &S3Src,
        src: &gst_base::BaseSrc,
        offset: u64,
        length: u64,
    ) -> Result<Bytes, Option<gst::ErrorMessage>> {
        let state = self.state.lock().unwrap();

        let (url, client) = match *state {
            StreamingState::Started {
                ref url,
                ref client,
                ..
            } => (url, client),
            StreamingState::Stopped => {
                return Err(Some(gst_error_msg!(
                    gst::LibraryError::Failed,
                    ["Cannot GET before start()"]
                )));
            }
        };

        let request = GetObjectRequest {
            bucket: url.bucket.clone(),
            key: url.object.clone(),
            range: Some(format!("bytes={}-{}", offset, offset + length - 1)),
            version_id: url.version.clone(),
            ..Default::default()
        };

        gst_debug!(
            self.cat,
            obj: src,
            "Requesting range: {}-{}",
            offset,
            offset + length - 1
        );

        let response = client.get_object(request);

        /* Drop the state lock now that we're done with it and need the next part to be
         * interruptible */
        drop(state);

        let output = s3utils::wait(
            &self.canceller,
            &self.runtime,
            response.map_err(|err| {
                gst_error_msg!(gst::ResourceError::Read, ["Could not read: {}", err])
            }),
        )?;

        gst_debug!(
            self.cat,
            obj: src,
            "Read {} bytes",
            output.content_length.unwrap()
        );

        s3utils::wait(
            &self.canceller,
            &self.runtime,
            output.body.unwrap().concat2().map_err(|err| {
                gst_error_msg!(gst::ResourceError::Read, ["Could not read: {}", err])
            }),
        )
    }
}

impl ObjectSubclass for S3Src {
    const NAME: &'static str = "S3Src";
    type ParentType = gst_base::BaseSrc;
    type Instance = gst::subclass::ElementInstanceStruct<Self>;
    type Class = subclass::simple::ClassStruct<Self>;

    glib_object_subclass!();

    fn new() -> Self {
        Self {
            url: Mutex::new(None),
            state: Mutex::new(StreamingState::Stopped),
            cat: gst::DebugCategory::new(
                "s3src",
                gst::DebugColorFlags::empty(),
                Some("Amazon S3 Source"),
            ),
            runtime: runtime::Builder::new()
                .core_threads(1)
                .name_prefix("gst-s3-tokio")
                .build()
                .unwrap(),
            canceller: Mutex::new(None),
        }
    }

    fn type_init(typ: &mut subclass::InitializingType<Self>) {
        typ.add_interface::<gst::URIHandler>();
    }

    fn class_init(klass: &mut subclass::simple::ClassStruct<Self>) {
        klass.set_metadata(
            "Amazon S3 source",
            "Source/Network",
            "Reads an object from Amazon S3",
            "Arun Raghavan <arun@arunraghavan.net>",
        );

        let caps = gst::Caps::new_any();
        let src_pad_template = gst::PadTemplate::new(
            "src",
            gst::PadDirection::Src,
            gst::PadPresence::Always,
            &caps,
        )
        .unwrap();
        klass.add_pad_template(src_pad_template);

        klass.install_properties(&PROPERTIES);
    }
}

impl ObjectImpl for S3Src {
    glib_object_impl!();

    fn set_property(&self, obj: &glib::Object, id: usize, value: &glib::Value) {
        let prop = &PROPERTIES[id as usize];
        let basesrc = obj.downcast_ref::<gst_base::BaseSrc>().unwrap();

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

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

        match *prop {
            subclass::Property("uri", ..) => {
                let url = match *self.url.lock().unwrap() {
                    Some(ref url) => url.to_string(),
                    None => "".to_string(),
                };

                Ok(url.to_value())
            }
            _ => unimplemented!(),
        }
    }

    fn constructed(&self, obj: &glib::Object) {
        self.parent_constructed(obj);

        let basesrc = obj.downcast_ref::<gst_base::BaseSrc>().unwrap();
        basesrc.set_format(gst::Format::Bytes);
        /* Set a larger default blocksize to make read more efficient */
        basesrc.set_blocksize(262144);
    }
}

impl ElementImpl for S3Src {
    // No overrides
}

impl URIHandlerImpl for S3Src {
    fn get_uri(&self, _: &gst::URIHandler) -> Option<String> {
        self.url.lock().unwrap().as_ref().map(|s| s.to_string())
    }

    fn set_uri(&self, element: &gst::URIHandler, uri: &str) -> Result<(), glib::Error> {
        let basesrc = element.dynamic_cast_ref::<gst_base::BaseSrc>().unwrap();
        self.set_uri(basesrc, uri)
    }

    fn get_uri_type() -> gst::URIType {
        gst::URIType::Src
    }

    fn get_protocols() -> Vec<String> {
        vec!["s3".to_string()]
    }
}

impl BaseSrcImpl for S3Src {
    fn is_seekable(&self, _: &gst_base::BaseSrc) -> bool {
        true
    }

    fn get_size(&self, _: &gst_base::BaseSrc) -> Option<u64> {
        match *self.state.lock().unwrap() {
            StreamingState::Stopped => None,
            StreamingState::Started { size, .. } => Some(size),
        }
    }

    fn start(&self, src: &gst_base::BaseSrc) -> Result<(), gst::ErrorMessage> {
        let state = self.state.lock().unwrap();

        if let StreamingState::Started { .. } = *state {
            unreachable!("S3Src is already started");
        }

        /* Drop the lock as self.head() needs it */
        drop(state);

        let s3url = match *self.url.lock().unwrap() {
            Some(ref url) => url.clone(),
            None => {
                return Err(gst_error_msg!(
                    gst::ResourceError::Settings,
                    ["Cannot start without a URL being set"]
                ));
            }
        };

        let s3client = self.connect(&s3url)?;

        let size = self.head(src, &s3client, &s3url)?;

        let mut state = self.state.lock().unwrap();

        *state = StreamingState::Started {
            url: s3url,
            client: s3client,
            size: size,
        };

        Ok(())
    }

    fn stop(&self, _: &gst_base::BaseSrc) -> Result<(), gst::ErrorMessage> {
        let mut state = self.state.lock().unwrap();

        if let StreamingState::Stopped = *state {
            unreachable!("Cannot stop before start");
        }

        *state = StreamingState::Stopped;

        Ok(())
    }

    fn query(&self, src: &gst_base::BaseSrc, query: &mut gst::QueryRef) -> bool {
        match query.view_mut() {
            gst::QueryView::Scheduling(ref mut q) => {
                q.set(
                    gst::SchedulingFlags::SEQUENTIAL | gst::SchedulingFlags::BANDWIDTH_LIMITED,
                    1,
                    -1,
                    0,
                );
                q.add_scheduling_modes(&[gst::PadMode::Push, gst::PadMode::Pull]);
                return true;
            }
            _ => (),
        }

        BaseSrcImplExt::parent_query(self, src, query)
    }

    fn create(
        &self,
        src: &gst_base::BaseSrc,
        offset: u64,
        length: u32,
    ) -> Result<gst::Buffer, gst::FlowError> {
        // FIXME: sanity check on offset and length
        let data = self.get(src, offset, u64::from(length));

        match data {
            /* Got data */
            Ok(bytes) => Ok(gst::Buffer::from_slice(bytes)),
            /* Interrupted */
            Err(None) => Err(gst::FlowError::Flushing),
            /* Actual Error */
            Err(Some(err)) => {
                gst_error!(self.cat, obj: src, "Could not GET: {}", err);
                Err(gst::FlowError::Error)
            }
        }
    }

    /* FIXME: implement */
    fn do_seek(&self, _: &gst_base::BaseSrc, _: &mut gst::Segment) -> bool {
        true
    }

    fn unlock(&self, _: &gst_base::BaseSrc) -> Result<(), gst::ErrorMessage> {
        self.cancel();
        Ok(())
    }
}

pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
    gst::Element::register(Some(plugin), "s3src", gst::Rank::Primary, S3Src::get_type())
}