2018-12-30 10:03:32 +00:00
|
|
|
// Copyright (C) 2016-2018 Sebastian Dröge <sebastian@centricular.com>
|
2016-05-15 15:54:09 +00:00
|
|
|
//
|
2017-02-16 15:52:27 +00:00
|
|
|
// 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.
|
2019-09-28 12:02:10 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-08-09 11:37:35 +00:00
|
|
|
use std::time::Duration;
|
2018-04-05 08:35:34 +00:00
|
|
|
use std::u64;
|
2019-12-30 14:00:12 +00:00
|
|
|
|
|
|
|
use futures::future;
|
|
|
|
use futures::prelude::*;
|
|
|
|
use reqwest::{Client, Response, StatusCode};
|
2019-06-24 20:11:11 +00:00
|
|
|
use tokio::runtime;
|
2018-04-05 08:35:34 +00:00
|
|
|
use url::Url;
|
2016-05-14 14:04:53 +00:00
|
|
|
|
2020-11-22 15:43:59 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2018-12-30 10:03:32 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
2020-12-20 18:43:45 +00:00
|
|
|
use gst::{gst_debug, gst_error, gst_trace, gst_warning};
|
2018-12-30 10:03:32 +00:00
|
|
|
use gst_base::prelude::*;
|
|
|
|
use gst_base::subclass::prelude::*;
|
2018-05-04 12:16:59 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
const DEFAULT_LOCATION: Option<Url> = None;
|
2019-08-04 18:17:40 +00:00
|
|
|
const DEFAULT_USER_AGENT: &str = concat!(
|
|
|
|
"GStreamer reqwesthttpsrc ",
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
"-",
|
|
|
|
env!("COMMIT_ID")
|
|
|
|
);
|
2019-08-06 19:13:18 +00:00
|
|
|
const DEFAULT_IS_LIVE: bool = false;
|
2019-08-09 11:37:35 +00:00
|
|
|
const DEFAULT_TIMEOUT: u32 = 15;
|
2019-09-30 08:10:33 +00:00
|
|
|
const DEFAULT_COMPRESS: bool = false;
|
2019-10-01 21:10:53 +00:00
|
|
|
const DEFAULT_IRADIO_MODE: bool = true;
|
2019-10-02 05:59:25 +00:00
|
|
|
const DEFAULT_KEEP_ALIVE: bool = true;
|
2016-12-25 11:16:12 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Settings {
|
|
|
|
location: Option<Url>,
|
2019-08-04 18:17:40 +00:00
|
|
|
user_agent: String,
|
2019-08-07 18:58:12 +00:00
|
|
|
user_id: Option<String>,
|
|
|
|
user_pw: Option<String>,
|
2019-08-09 11:37:35 +00:00
|
|
|
timeout: u32,
|
2019-09-30 08:10:33 +00:00
|
|
|
compress: bool,
|
2019-10-01 20:35:55 +00:00
|
|
|
extra_headers: Option<gst::Structure>,
|
|
|
|
cookies: Vec<String>,
|
2019-10-01 21:10:53 +00:00
|
|
|
iradio_mode: bool,
|
2019-10-02 05:59:25 +00:00
|
|
|
keep_alive: bool,
|
2021-04-14 21:58:37 +00:00
|
|
|
// Notes about souphttpsrc compatibility:
|
|
|
|
// Internal representation of no proxy is None,
|
|
|
|
// but externally Some("").
|
|
|
|
// Default is set from env var 'http_proxy'.
|
|
|
|
// Prepends http:// if not protocol specified.
|
|
|
|
proxy: Option<String>,
|
|
|
|
// Nullable fields that behave normally:
|
|
|
|
proxy_id: Option<String>,
|
|
|
|
proxy_pw: Option<String>,
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Settings {
|
|
|
|
location: DEFAULT_LOCATION,
|
2019-08-04 18:17:40 +00:00
|
|
|
user_agent: DEFAULT_USER_AGENT.into(),
|
2019-08-07 18:58:12 +00:00
|
|
|
user_id: None,
|
|
|
|
user_pw: None,
|
2019-08-09 11:37:35 +00:00
|
|
|
timeout: DEFAULT_TIMEOUT,
|
2019-09-30 08:10:33 +00:00
|
|
|
compress: DEFAULT_COMPRESS,
|
2019-10-01 20:35:55 +00:00
|
|
|
extra_headers: None,
|
|
|
|
cookies: Vec::new(),
|
2019-10-01 21:10:53 +00:00
|
|
|
iradio_mode: DEFAULT_IRADIO_MODE,
|
2019-10-02 05:59:25 +00:00
|
|
|
keep_alive: DEFAULT_KEEP_ALIVE,
|
2021-04-14 21:58:37 +00:00
|
|
|
proxy: match proxy_from_str(std::env::var("http_proxy").ok()) {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(_) => None,
|
|
|
|
},
|
|
|
|
proxy_id: None,
|
|
|
|
proxy_pw: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn proxy_from_str(s: Option<String>) -> Result<Option<String>, glib::Error> {
|
|
|
|
match s {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(s) if s.is_empty() => Ok(None),
|
|
|
|
Some(not_empty_str) => {
|
|
|
|
// If no protocol specified, prepend http for compatibility
|
|
|
|
// https://gstreamer.freedesktop.org/documentation/soup/souphttpsrc.html
|
|
|
|
let url_string = if !not_empty_str.contains("://") {
|
|
|
|
format!("http://{}", not_empty_str)
|
|
|
|
} else {
|
|
|
|
not_empty_str
|
|
|
|
};
|
|
|
|
match reqwest::Url::parse(&url_string) {
|
|
|
|
Ok(url) => {
|
|
|
|
// this may urlencode and add trailing /
|
|
|
|
Ok(Some(url.to_string()))
|
|
|
|
}
|
|
|
|
Err(err) => Err(glib::Error::new(
|
|
|
|
gst::URIError::BadUri,
|
|
|
|
format!("Failed to parse URI '{}': {:?}", url_string, err).as_str(),
|
|
|
|
)),
|
|
|
|
}
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 14:30:30 +00:00
|
|
|
const REQWEST_CLIENT_CONTEXT: &str = "gst.reqwest.client";
|
2019-09-28 12:02:10 +00:00
|
|
|
|
2020-11-22 15:43:59 +00:00
|
|
|
#[derive(Clone, Debug, glib::GBoxed)]
|
2020-03-19 09:18:17 +00:00
|
|
|
#[gboxed(type_name = "ReqwestClientContext")]
|
2019-09-28 12:02:10 +00:00
|
|
|
struct ClientContext(Arc<ClientContextInner>);
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ClientContextInner {
|
|
|
|
client: Client,
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:42:07 +00:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2016-08-22 19:25:58 +00:00
|
|
|
#[derive(Debug)]
|
2018-12-30 10:03:32 +00:00
|
|
|
enum State {
|
2016-08-22 19:25:58 +00:00
|
|
|
Stopped,
|
|
|
|
Started {
|
2016-09-01 21:52:28 +00:00
|
|
|
uri: Url,
|
2019-12-30 14:00:12 +00:00
|
|
|
response: Option<Response>,
|
2016-08-22 19:25:58 +00:00
|
|
|
seekable: bool,
|
|
|
|
position: u64,
|
2016-09-01 22:01:25 +00:00
|
|
|
size: Option<u64>,
|
2016-08-22 19:25:58 +00:00
|
|
|
start: u64,
|
2016-09-01 22:01:25 +00:00
|
|
|
stop: Option<u64>,
|
2019-10-01 21:10:53 +00:00
|
|
|
caps: Option<gst::Caps>,
|
|
|
|
tags: Option<gst::TagList>,
|
2016-08-22 19:25:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
State::Stopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[derive(Debug, Default)]
|
2019-06-26 04:40:21 +00:00
|
|
|
pub struct ReqwestHttpSrc {
|
2019-09-28 12:02:10 +00:00
|
|
|
client: Mutex<Option<ClientContext>>,
|
|
|
|
external_client: Mutex<Option<ClientContext>>,
|
2018-12-30 10:03:32 +00:00
|
|
|
settings: Mutex<Settings>,
|
|
|
|
state: Mutex<State>,
|
2019-12-30 14:00:12 +00:00
|
|
|
canceller: Mutex<Option<future::AbortHandle>>,
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:43:59 +00:00
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
2019-10-31 22:34:21 +00:00
|
|
|
"reqwesthttpsrc",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Rust HTTP source"),
|
2020-11-22 15:43:59 +00:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
static RUNTIME: Lazy<runtime::Runtime> = Lazy::new(|| {
|
2021-01-09 10:04:38 +00:00
|
|
|
runtime::Builder::new_multi_thread()
|
2019-12-30 14:00:12 +00:00
|
|
|
.enable_all()
|
2021-01-09 10:04:38 +00:00
|
|
|
.worker_threads(1)
|
2019-12-30 14:00:12 +00:00
|
|
|
.build()
|
2020-11-22 15:43:59 +00:00
|
|
|
.unwrap()
|
|
|
|
});
|
2019-10-31 22:34:21 +00:00
|
|
|
|
2019-06-26 04:40:21 +00:00
|
|
|
impl ReqwestHttpSrc {
|
2019-08-12 22:45:36 +00:00
|
|
|
fn set_location(
|
|
|
|
&self,
|
2020-11-14 17:24:01 +00:00
|
|
|
_element: &super::ReqwestHttpSrc,
|
2019-08-12 22:45:36 +00:00
|
|
|
uri: Option<&str>,
|
|
|
|
) -> Result<(), glib::Error> {
|
2018-12-30 10:03:32 +00:00
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
if let State::Started { .. } = *state {
|
|
|
|
return Err(glib::Error::new(
|
|
|
|
gst::URIError::BadState,
|
2019-06-26 04:40:21 +00:00
|
|
|
"Changing the `location` property on a started `reqwesthttpsrc` is not supported",
|
2018-12-30 10:03:32 +00:00
|
|
|
));
|
2016-07-20 08:28:58 +00:00
|
|
|
}
|
2016-05-14 14:04:53 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
|
2019-08-12 22:45:36 +00:00
|
|
|
if uri.is_none() {
|
|
|
|
settings.location = DEFAULT_LOCATION;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let uri = uri.unwrap();
|
2019-05-25 17:23:55 +00:00
|
|
|
let uri = Url::parse(uri).map_err(|err| {
|
2018-12-30 10:03:32 +00:00
|
|
|
glib::Error::new(
|
|
|
|
gst::URIError::BadUri,
|
2019-10-01 19:34:11 +00:00
|
|
|
format!("Failed to parse URI '{}': {:?}", uri, err).as_str(),
|
2018-12-30 10:03:32 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if uri.scheme() != "http" && uri.scheme() != "https" {
|
|
|
|
return Err(glib::Error::new(
|
|
|
|
gst::URIError::UnsupportedProtocol,
|
2019-10-01 19:34:11 +00:00
|
|
|
format!("Unsupported URI scheme '{}'", uri.scheme()).as_str(),
|
2018-12-30 10:03:32 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.location = Some(uri);
|
|
|
|
|
|
|
|
Ok(())
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
2016-05-15 08:48:54 +00:00
|
|
|
|
2021-04-14 21:58:37 +00:00
|
|
|
/// Set a proxy-related property and perform necessary state checks and modifications to client.
|
|
|
|
fn set_proxy_prop<F>(
|
|
|
|
&self,
|
|
|
|
property_name: &str,
|
|
|
|
desired_value: Option<String>,
|
|
|
|
prop_memory_location: F,
|
|
|
|
) -> Result<(), glib::Error>
|
|
|
|
where
|
|
|
|
F: Fn(&mut Settings) -> &mut Option<String>,
|
|
|
|
{
|
|
|
|
// Proxy props can only be changed when not started.
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
if let State::Started { .. } = *state {
|
|
|
|
return Err(glib::Error::new(
|
|
|
|
gst::URIError::BadState,
|
|
|
|
&format!(
|
|
|
|
"Changing the `{}` property on a started `reqwesthttpsrc` is not supported",
|
|
|
|
property_name
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get memory address of specific variable to change.
|
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let target_variable = prop_memory_location(&mut settings);
|
|
|
|
if &desired_value == target_variable {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the Proxy is changed we need to throw away the old client since it isn't properly
|
|
|
|
// configured with a proxy anymore. Since element is not started, an existing client
|
|
|
|
// without proxy will be used, or a new one with/without proxy will be built on next call
|
|
|
|
// to ensure_client.
|
|
|
|
*self.client.lock().unwrap() = None;
|
|
|
|
*target_variable = desired_value;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn ensure_client(
|
|
|
|
&self,
|
|
|
|
src: &super::ReqwestHttpSrc,
|
2021-04-14 21:58:37 +00:00
|
|
|
proxy: Option<String>,
|
|
|
|
proxy_id: Option<String>,
|
|
|
|
proxy_pw: Option<String>,
|
2020-11-14 17:24:01 +00:00
|
|
|
) -> Result<ClientContext, gst::ErrorMessage> {
|
2019-10-01 19:34:11 +00:00
|
|
|
let mut client_guard = self.client.lock().unwrap();
|
|
|
|
if let Some(ref client) = *client_guard {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Using already configured client");
|
2019-10-01 19:34:11 +00:00
|
|
|
return Ok(client.clone());
|
|
|
|
}
|
|
|
|
|
2021-04-14 21:58:37 +00:00
|
|
|
// Attempt to acquire an existing client context from another element instance
|
|
|
|
// unless using proxy, because proxy is client specific.
|
|
|
|
if proxy.is_none() {
|
|
|
|
let srcpad = src.static_pad("src").unwrap();
|
|
|
|
let mut q = gst::query::Context::new(REQWEST_CLIENT_CONTEXT);
|
|
|
|
if srcpad.peer_query(&mut q) {
|
|
|
|
if let Some(context) = q.context_owned() {
|
|
|
|
src.set_context(&context);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let _ = src.post_message(
|
|
|
|
gst::message::NeedContext::builder(REQWEST_CLIENT_CONTEXT)
|
|
|
|
.src(src)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hopefully now, self.set_context will have been synchronously called
|
|
|
|
if let Some(client) = self.external_client.lock().unwrap().clone() {
|
|
|
|
gst_debug!(CAT, obj: src, "Using shared client");
|
|
|
|
*client_guard = Some(client.clone());
|
|
|
|
|
|
|
|
return Ok(client);
|
2019-10-01 19:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 21:58:37 +00:00
|
|
|
let mut builder = Client::builder().cookie_store(true).gzip(true);
|
|
|
|
|
|
|
|
if let Some(proxy) = &proxy {
|
|
|
|
// Proxy is url-checked on property set but perhaps this might still fail.
|
|
|
|
let mut p = reqwest::Proxy::all(proxy).map_err(|err| {
|
|
|
|
gst::error_msg!(gst::ResourceError::OpenRead, ["Bad proxy URI: {}", err])
|
|
|
|
})?;
|
|
|
|
if let Some(proxy_id) = &proxy_id {
|
|
|
|
let proxy_pw = proxy_pw.as_deref().unwrap_or("");
|
|
|
|
p = p.basic_auth(proxy_id, proxy_pw);
|
|
|
|
}
|
|
|
|
builder = builder.proxy(p);
|
2019-10-01 19:34:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Creating new client");
|
2019-10-01 19:34:11 +00:00
|
|
|
let client = ClientContext(Arc::new(ClientContextInner {
|
2021-04-14 21:58:37 +00:00
|
|
|
client: builder.build().map_err(|err| {
|
|
|
|
gst::error_msg!(
|
|
|
|
gst::ResourceError::OpenRead,
|
|
|
|
["Failed to create Client: {}", err]
|
|
|
|
)
|
|
|
|
})?,
|
2019-10-01 19:34:11 +00:00
|
|
|
}));
|
|
|
|
|
2021-04-14 21:58:37 +00:00
|
|
|
// Share created client with other elements, unless using proxy. Shared client never uses proxy.
|
|
|
|
// The alternative would be different contexts for different proxy settings, or one context with a
|
|
|
|
// map from proxy settings to client, but then, how and when to discard those, retaining reuse benefits?
|
|
|
|
if proxy.is_none() {
|
|
|
|
gst_debug!(CAT, obj: src, "Sharing new client with other elements");
|
|
|
|
let mut context = gst::Context::new(REQWEST_CLIENT_CONTEXT, true);
|
|
|
|
{
|
|
|
|
let context = context.get_mut().unwrap();
|
|
|
|
let s = context.structure_mut();
|
|
|
|
s.set("client", &client);
|
|
|
|
}
|
|
|
|
src.set_context(&context);
|
|
|
|
let _ = src.post_message(gst::message::HaveContext::builder(context).src(src).build());
|
2019-10-01 19:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*client_guard = Some(client.clone());
|
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
2017-07-31 13:36:35 +00:00
|
|
|
fn do_request(
|
|
|
|
&self,
|
2020-11-14 17:24:01 +00:00
|
|
|
src: &super::ReqwestHttpSrc,
|
2017-07-31 13:36:35 +00:00
|
|
|
uri: Url,
|
|
|
|
start: u64,
|
|
|
|
stop: Option<u64>,
|
2019-10-01 19:34:11 +00:00
|
|
|
) -> Result<State, Option<gst::ErrorMessage>> {
|
2020-01-18 10:50:24 +00:00
|
|
|
use hyperx::header::{
|
|
|
|
qitem, AcceptEncoding, AcceptRanges, ByteRangeSpec, Connection, ContentLength,
|
|
|
|
ContentRange, ContentRangeSpec, ContentType, Cookie, Encoding, Range, RangeUnit,
|
|
|
|
TypedHeaders, UserAgent,
|
2019-09-30 08:10:33 +00:00
|
|
|
};
|
2020-01-18 10:50:24 +00:00
|
|
|
use reqwest::header::HeaderMap;
|
2019-09-30 08:10:33 +00:00
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Creating new request for {}", uri);
|
2019-09-28 12:02:10 +00:00
|
|
|
|
2019-08-07 18:58:12 +00:00
|
|
|
let settings = self.settings.lock().unwrap().clone();
|
2018-12-19 12:31:42 +00:00
|
|
|
|
2021-04-14 21:58:37 +00:00
|
|
|
let req = self
|
|
|
|
.ensure_client(src, settings.proxy, settings.proxy_id, settings.proxy_pw)?
|
|
|
|
.0
|
|
|
|
.client
|
|
|
|
.get(uri.clone());
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let mut headers = HeaderMap::new();
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2019-10-02 05:59:25 +00:00
|
|
|
if settings.keep_alive {
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&Connection::keep_alive());
|
2019-10-02 05:59:25 +00:00
|
|
|
} else {
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&Connection::close());
|
2019-10-02 05:59:25 +00:00
|
|
|
}
|
2019-09-28 12:02:10 +00:00
|
|
|
|
2016-11-25 12:42:16 +00:00
|
|
|
match (start != 0, stop) {
|
|
|
|
(false, None) => (),
|
2017-07-31 13:29:11 +00:00
|
|
|
(true, None) => {
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&Range::Bytes(vec![ByteRangeSpec::AllFrom(start)]));
|
2017-07-31 13:29:11 +00:00
|
|
|
}
|
2016-11-25 12:42:16 +00:00
|
|
|
(_, Some(stop)) => {
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&Range::Bytes(vec![ByteRangeSpec::FromTo(start, stop - 1)]));
|
2016-11-25 12:42:16 +00:00
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&UserAgent::new(settings.user_agent));
|
2019-08-04 18:17:40 +00:00
|
|
|
|
2019-09-30 08:10:33 +00:00
|
|
|
if !settings.compress {
|
|
|
|
// Compression is the default
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&AcceptEncoding(vec![qitem(Encoding::Identity)]));
|
2019-09-30 08:10:33 +00:00
|
|
|
};
|
|
|
|
|
2019-10-01 20:35:55 +00:00
|
|
|
if let Some(ref extra_headers) = settings.extra_headers {
|
2020-01-18 10:50:24 +00:00
|
|
|
use reqwest::header::{HeaderName, HeaderValue};
|
2019-12-30 14:00:12 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2019-10-01 20:35:55 +00:00
|
|
|
for (field, value) in extra_headers.iter() {
|
2019-12-30 14:00:12 +00:00
|
|
|
let field = match HeaderName::try_from(field) {
|
|
|
|
Ok(field) => field,
|
|
|
|
Err(err) => {
|
|
|
|
gst_warning!(
|
|
|
|
CAT,
|
|
|
|
obj: src,
|
|
|
|
"Failed to transform extra-header field name '{}' to header name: {}",
|
|
|
|
field,
|
|
|
|
err,
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut append_header = |field: &HeaderName, value: &glib::Value| {
|
|
|
|
let value = match value.transform::<String>() {
|
2021-04-25 12:41:22 +00:00
|
|
|
Ok(value) => value,
|
|
|
|
Err(_) => {
|
2019-10-01 20:35:55 +00:00
|
|
|
gst_warning!(
|
2019-10-31 22:34:21 +00:00
|
|
|
CAT,
|
2019-10-01 20:35:55 +00:00
|
|
|
obj: src,
|
2019-12-30 14:00:12 +00:00
|
|
|
"Failed to transform extra-header '{}' value to string",
|
2019-10-01 20:35:55 +00:00
|
|
|
field
|
|
|
|
);
|
2019-12-30 14:00:12 +00:00
|
|
|
return;
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
2019-12-30 14:00:12 +00:00
|
|
|
};
|
|
|
|
|
2021-04-25 12:41:22 +00:00
|
|
|
let value = value.get::<Option<&str>>().unwrap().unwrap_or("");
|
2019-12-30 14:00:12 +00:00
|
|
|
|
|
|
|
let value = match HeaderValue::from_str(value) {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(_) => {
|
2019-10-01 20:35:55 +00:00
|
|
|
gst_warning!(
|
2019-10-31 22:34:21 +00:00
|
|
|
CAT,
|
2019-10-01 20:35:55 +00:00
|
|
|
obj: src,
|
2019-12-30 14:00:12 +00:00
|
|
|
"Failed to transform extra-header '{}' value to header value",
|
2019-10-01 20:35:55 +00:00
|
|
|
field
|
|
|
|
);
|
2019-12-30 14:00:12 +00:00
|
|
|
return;
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
2019-12-30 14:00:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
headers.append(field.clone(), value);
|
|
|
|
};
|
|
|
|
|
2021-04-25 12:41:22 +00:00
|
|
|
if let Ok(values) = value.get::<gst::Array>() {
|
2019-12-30 14:00:12 +00:00
|
|
|
for value in values.as_slice() {
|
|
|
|
append_header(&field, value);
|
|
|
|
}
|
2021-04-25 12:41:22 +00:00
|
|
|
} else if let Ok(values) = value.get::<gst::List>() {
|
2019-12-30 14:00:12 +00:00
|
|
|
for value in values.as_slice() {
|
|
|
|
append_header(&field, value);
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-30 14:00:12 +00:00
|
|
|
append_header(&field, value);
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !settings.cookies.is_empty() {
|
2020-01-18 10:50:24 +00:00
|
|
|
let mut cookies = Cookie::new();
|
2019-10-01 20:35:55 +00:00
|
|
|
for cookie in settings.cookies {
|
|
|
|
let mut split = cookie.splitn(2, '=');
|
|
|
|
let key = split.next();
|
|
|
|
let value = split.next();
|
|
|
|
if let (Some(key), Some(value)) = (key, value) {
|
2020-01-18 10:50:24 +00:00
|
|
|
cookies.append(String::from(key), String::from(value));
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 10:50:24 +00:00
|
|
|
headers.encode(&cookies);
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:10:53 +00:00
|
|
|
if settings.iradio_mode {
|
2019-12-30 14:00:12 +00:00
|
|
|
headers.append("icy-metadata", "1".parse().unwrap());
|
2019-10-01 21:10:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 12:31:42 +00:00
|
|
|
// Add all headers for the request here
|
2019-12-30 14:00:12 +00:00
|
|
|
let req = req.headers(headers);
|
2018-12-19 12:31:42 +00:00
|
|
|
|
2019-10-01 20:35:55 +00:00
|
|
|
let req = if let Some(ref user_id) = settings.user_id {
|
2019-08-07 18:58:12 +00:00
|
|
|
// HTTP auth available
|
|
|
|
req.basic_auth(user_id, settings.user_pw)
|
|
|
|
} else {
|
|
|
|
req
|
|
|
|
};
|
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Sending new request: {:?}", req);
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let future = async {
|
|
|
|
req.send().await.map_err(|err| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(
|
2019-12-30 14:00:12 +00:00
|
|
|
gst::ResourceError::OpenRead,
|
|
|
|
["Failed to fetch {}: {:?}", uri, err]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let res = self.wait(future);
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let res = match res {
|
2019-10-01 19:34:11 +00:00
|
|
|
Ok(res) => res,
|
|
|
|
Err(Some(err)) => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Error {:?}", err);
|
2019-10-01 19:34:11 +00:00
|
|
|
return Err(Some(err));
|
|
|
|
}
|
|
|
|
Err(None) => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Flushing");
|
2019-10-01 19:34:11 +00:00
|
|
|
return Err(None);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Received response: {:?}", res);
|
2019-10-01 19:34:11 +00:00
|
|
|
|
|
|
|
if !res.status().is_success() {
|
|
|
|
match res.status() {
|
2019-07-04 19:17:22 +00:00
|
|
|
StatusCode::NOT_FOUND => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_error!(CAT, obj: src, "Resource not found");
|
2020-12-20 18:43:45 +00:00
|
|
|
return Err(Some(gst::error_msg!(
|
2019-07-04 19:17:22 +00:00
|
|
|
gst::ResourceError::NotFound,
|
2019-10-01 19:34:11 +00:00
|
|
|
["Resource '{}' not found", uri]
|
|
|
|
)));
|
2019-07-04 19:17:22 +00:00
|
|
|
}
|
|
|
|
StatusCode::UNAUTHORIZED
|
|
|
|
| StatusCode::PAYMENT_REQUIRED
|
|
|
|
| StatusCode::FORBIDDEN
|
|
|
|
| StatusCode::PROXY_AUTHENTICATION_REQUIRED => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_error!(CAT, obj: src, "Not authorized: {}", res.status());
|
2020-12-20 18:43:45 +00:00
|
|
|
return Err(Some(gst::error_msg!(
|
2019-07-04 19:17:22 +00:00
|
|
|
gst::ResourceError::NotAuthorized,
|
2019-10-01 19:34:11 +00:00
|
|
|
["Not Authorized for resource '{}': {}", uri, res.status()]
|
|
|
|
)));
|
2019-07-04 19:17:22 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_error!(CAT, obj: src, "Request failed: {}", res.status());
|
2020-12-20 18:43:45 +00:00
|
|
|
return Err(Some(gst::error_msg!(
|
2019-07-04 19:17:22 +00:00
|
|
|
gst::ResourceError::OpenRead,
|
2019-10-01 19:34:11 +00:00
|
|
|
["Request for '{}' failed: {}", uri, res.status()]
|
|
|
|
)));
|
2019-07-04 19:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let headers = res.headers();
|
2020-01-18 10:50:24 +00:00
|
|
|
let size = headers.decode().map(|ContentLength(cl)| cl + start).ok();
|
|
|
|
|
|
|
|
let accept_byte_ranges = if let Ok(AcceptRanges(ref ranges)) = headers.decode() {
|
|
|
|
ranges.iter().any(|u| *u == RangeUnit::Bytes)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2016-09-01 22:01:25 +00:00
|
|
|
let seekable = size.is_some() && accept_byte_ranges;
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2020-01-18 10:50:24 +00:00
|
|
|
let position = if let Ok(ContentRange(ContentRangeSpec::Bytes {
|
|
|
|
range: Some((range_start, _)),
|
|
|
|
..
|
|
|
|
})) = headers.decode()
|
2017-07-31 13:36:35 +00:00
|
|
|
{
|
2016-08-27 08:16:17 +00:00
|
|
|
range_start
|
|
|
|
} else {
|
2019-07-16 20:57:35 +00:00
|
|
|
0
|
2016-08-27 08:16:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if position != start {
|
2020-12-20 18:43:45 +00:00
|
|
|
return Err(Some(gst::error_msg!(
|
2017-09-16 17:35:01 +00:00
|
|
|
gst::ResourceError::Seek,
|
2017-07-31 13:36:35 +00:00
|
|
|
["Failed to seek to {}: Got {}", start, position]
|
2019-10-01 19:34:11 +00:00
|
|
|
)));
|
2016-05-15 08:48:54 +00:00
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2019-10-14 18:59:49 +00:00
|
|
|
let mut caps = headers
|
2019-12-30 14:00:12 +00:00
|
|
|
.get("icy-metaint")
|
|
|
|
.and_then(|s| s.to_str().ok())
|
2019-10-01 21:10:53 +00:00
|
|
|
.and_then(|s| s.parse::<i32>().ok())
|
|
|
|
.map(|icy_metaint| {
|
|
|
|
gst::Caps::builder("application/x-icy")
|
|
|
|
.field("metadata-interval", &icy_metaint)
|
|
|
|
.build()
|
|
|
|
});
|
|
|
|
|
2020-01-18 10:50:24 +00:00
|
|
|
if let Ok(ContentType(ref content_type)) = headers.decode() {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Got content type {}", content_type);
|
2019-10-14 18:59:49 +00:00
|
|
|
if let Some(ref mut caps) = caps {
|
|
|
|
let caps = caps.get_mut().unwrap();
|
2021-04-20 12:58:11 +00:00
|
|
|
let s = caps.structure_mut(0).unwrap();
|
2019-10-14 18:59:49 +00:00
|
|
|
s.set("content-type", &content_type.as_ref());
|
|
|
|
} else if content_type.type_() == "audio" && content_type.subtype() == "L16" {
|
|
|
|
let channels = content_type
|
2021-04-20 15:28:38 +00:00
|
|
|
.get_param("channels")
|
2019-10-14 18:59:49 +00:00
|
|
|
.and_then(|s| s.as_ref().parse::<i32>().ok())
|
|
|
|
.unwrap_or(2);
|
|
|
|
let rate = content_type
|
2021-04-20 15:28:38 +00:00
|
|
|
.get_param("rate")
|
2019-10-14 18:59:49 +00:00
|
|
|
.and_then(|s| s.as_ref().parse::<i32>().ok())
|
|
|
|
.unwrap_or(44_100);
|
|
|
|
|
|
|
|
caps = Some(
|
|
|
|
gst::Caps::builder("audio/x-unaligned-raw")
|
|
|
|
.field("format", &"S16BE")
|
|
|
|
.field("layout", &"interleaved")
|
|
|
|
.field("channels", &channels)
|
|
|
|
.field("rate", &rate)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:10:53 +00:00
|
|
|
let mut tags = gst::TagList::new();
|
|
|
|
{
|
|
|
|
let tags = tags.get_mut().unwrap();
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
if let Some(ref icy_name) = headers.get("icy-name").and_then(|s| s.to_str().ok()) {
|
2019-10-01 21:10:53 +00:00
|
|
|
tags.add::<gst::tags::Organization>(icy_name, gst::TagMergeMode::Replace);
|
|
|
|
}
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
if let Some(ref icy_genre) = headers.get("icy-genre").and_then(|s| s.to_str().ok()) {
|
2019-10-01 21:10:53 +00:00
|
|
|
tags.add::<gst::tags::Genre>(icy_genre, gst::TagMergeMode::Replace);
|
|
|
|
}
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
if let Some(ref icy_url) = headers.get("icy-url").and_then(|s| s.to_str().ok()) {
|
2019-10-01 21:10:53 +00:00
|
|
|
tags.add::<gst::tags::Location>(icy_url, gst::TagMergeMode::Replace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Request successful");
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
Ok(State::Started {
|
2018-10-11 10:49:10 +00:00
|
|
|
uri,
|
2019-12-30 14:00:12 +00:00
|
|
|
response: Some(res),
|
2018-10-11 10:49:10 +00:00
|
|
|
seekable,
|
2019-07-18 16:46:34 +00:00
|
|
|
position,
|
2018-10-11 10:49:10 +00:00
|
|
|
size,
|
|
|
|
start,
|
|
|
|
stop,
|
2019-10-01 21:10:53 +00:00
|
|
|
caps,
|
|
|
|
tags: if tags.n_tags() > 0 { Some(tags) } else { None },
|
2017-07-31 13:36:35 +00:00
|
|
|
})
|
2016-05-15 08:48:54 +00:00
|
|
|
}
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
fn wait<F, T>(&self, future: F) -> Result<T, Option<gst::ErrorMessage>>
|
2019-06-24 20:11:11 +00:00
|
|
|
where
|
2019-12-30 14:00:12 +00:00
|
|
|
F: Send + Future<Output = Result<T, gst::ErrorMessage>>,
|
|
|
|
T: Send + 'static,
|
2019-06-24 20:11:11 +00:00
|
|
|
{
|
2019-08-09 11:37:35 +00:00
|
|
|
let timeout = self.settings.lock().unwrap().timeout;
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let mut canceller = self.canceller.lock().unwrap();
|
|
|
|
let (abort_handle, abort_registration) = future::AbortHandle::new_pair();
|
|
|
|
canceller.replace(abort_handle);
|
2019-10-01 19:33:43 +00:00
|
|
|
drop(canceller);
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
// Wrap in a timeout
|
|
|
|
let future = async {
|
|
|
|
if timeout == 0 {
|
|
|
|
future.await
|
|
|
|
} else {
|
|
|
|
let res = tokio::time::timeout(Duration::from_secs(timeout.into()), future).await;
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(res) => res,
|
2020-12-20 18:43:45 +00:00
|
|
|
Err(_) => Err(gst::error_msg!(
|
2019-12-30 14:00:12 +00:00
|
|
|
gst::ResourceError::Read,
|
|
|
|
["Request timeout"]
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 11:37:35 +00:00
|
|
|
};
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
// And make abortable
|
|
|
|
let future = async {
|
|
|
|
match future::Abortable::new(future, abort_registration).await {
|
|
|
|
Ok(res) => res.map_err(Some),
|
|
|
|
Err(_) => Err(None),
|
|
|
|
}
|
|
|
|
};
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2021-01-09 10:04:38 +00:00
|
|
|
let res = {
|
|
|
|
let _enter = RUNTIME.enter();
|
|
|
|
futures::executor::block_on(future)
|
|
|
|
};
|
2019-06-24 20:11:11 +00:00
|
|
|
|
|
|
|
/* Clear out the canceller */
|
2019-12-30 14:00:12 +00:00
|
|
|
let _ = self.canceller.lock().unwrap().take();
|
2019-06-24 20:11:11 +00:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 04:40:21 +00:00
|
|
|
impl ObjectImpl for ReqwestHttpSrc {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
|
|
|
vec![
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_string(
|
2021-01-21 18:21:29 +00:00
|
|
|
"location",
|
|
|
|
"Location",
|
|
|
|
"URL to read from",
|
|
|
|
None,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_string(
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-agent",
|
|
|
|
"User-Agent",
|
|
|
|
"Value of the User-Agent HTTP request header field",
|
|
|
|
DEFAULT_USER_AGENT.into(),
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_boolean(
|
2021-01-21 18:21:29 +00:00
|
|
|
"is-live",
|
|
|
|
"Is Live",
|
|
|
|
"Act like a live source",
|
|
|
|
DEFAULT_IS_LIVE,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_string(
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-id",
|
|
|
|
"User-id",
|
|
|
|
"HTTP location URI user id for authentication",
|
|
|
|
None,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_string(
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-pw",
|
|
|
|
"User-pw",
|
|
|
|
"HTTP location URI user password for authentication",
|
|
|
|
None,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_uint(
|
2021-01-21 18:21:29 +00:00
|
|
|
"timeout",
|
|
|
|
"Timeout",
|
|
|
|
"Value in seconds to timeout a blocking I/O (0 = No timeout).",
|
|
|
|
0,
|
|
|
|
3600,
|
|
|
|
DEFAULT_TIMEOUT,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_boolean(
|
2021-01-21 18:21:29 +00:00
|
|
|
"compress",
|
|
|
|
"Compress",
|
|
|
|
"Allow compressed content encodings",
|
|
|
|
DEFAULT_COMPRESS,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_boxed(
|
2021-01-21 18:21:29 +00:00
|
|
|
"extra-headers",
|
|
|
|
"Extra Headers",
|
|
|
|
"Extra headers to append to the HTTP request",
|
|
|
|
gst::Structure::static_type(),
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_boxed(
|
2021-01-21 18:21:29 +00:00
|
|
|
"cookies",
|
|
|
|
"Cookies",
|
|
|
|
"HTTP request cookies",
|
|
|
|
Vec::<String>::static_type(),
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_boolean(
|
2021-01-21 18:21:29 +00:00
|
|
|
"iradio-mode",
|
|
|
|
"I-Radio Mode",
|
|
|
|
"Enable internet radio mode (ask server to send shoutcast/icecast metadata interleaved with the actual stream data",
|
|
|
|
DEFAULT_IRADIO_MODE,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-12 16:12:05 +00:00
|
|
|
glib::ParamSpec::new_boolean(
|
2021-01-21 18:21:29 +00:00
|
|
|
"keep-alive",
|
|
|
|
"Keep Alive",
|
|
|
|
"Use HTTP persistent connections",
|
|
|
|
DEFAULT_KEEP_ALIVE,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-04-14 21:58:37 +00:00
|
|
|
glib::ParamSpec::new_string(
|
|
|
|
"proxy",
|
|
|
|
"Proxy",
|
|
|
|
"HTTP proxy server URI",
|
|
|
|
Some(""),
|
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
|
|
|
),
|
|
|
|
glib::ParamSpec::new_string(
|
|
|
|
"proxy-id",
|
|
|
|
"Proxy-id",
|
|
|
|
"HTTP proxy URI user id for authentication",
|
|
|
|
Some(""),
|
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
|
|
|
),
|
|
|
|
glib::ParamSpec::new_string(
|
|
|
|
"proxy-pw",
|
|
|
|
"Proxy-pw",
|
|
|
|
"HTTP proxy URI user password for authentication",
|
|
|
|
Some(""),
|
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_READY,
|
|
|
|
),
|
2021-01-21 18:21:29 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_property(
|
|
|
|
&self,
|
|
|
|
obj: &Self::Type,
|
|
|
|
_id: usize,
|
|
|
|
value: &glib::Value,
|
|
|
|
pspec: &glib::ParamSpec,
|
|
|
|
) {
|
2021-04-14 21:58:37 +00:00
|
|
|
let res = match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"location" => {
|
2021-04-25 12:41:22 +00:00
|
|
|
let location = value.get::<Option<&str>>().expect("type checked upstream");
|
2021-04-14 21:58:37 +00:00
|
|
|
self.set_location(obj, location)
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-agent" => {
|
2019-08-04 18:17:40 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2019-08-12 22:45:36 +00:00
|
|
|
let user_agent = value
|
2021-04-25 12:41:22 +00:00
|
|
|
.get::<Option<String>>()
|
2019-08-12 22:45:36 +00:00
|
|
|
.expect("type checked upstream")
|
|
|
|
.unwrap_or_else(|| DEFAULT_USER_AGENT.into());
|
2019-08-04 18:17:40 +00:00
|
|
|
settings.user_agent = user_agent;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-08-04 18:17:40 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"is-live" => {
|
2021-04-25 12:41:22 +00:00
|
|
|
let is_live = value.get().expect("type checked upstream");
|
2020-11-14 17:24:01 +00:00
|
|
|
obj.set_live(is_live);
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-08-06 19:13:18 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-id" => {
|
2019-08-07 18:58:12 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2019-08-12 22:45:36 +00:00
|
|
|
let user_id = value.get().expect("type checked upstream");
|
2019-08-07 18:58:12 +00:00
|
|
|
settings.user_id = user_id;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-08-07 18:58:12 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-pw" => {
|
2019-08-07 18:58:12 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2019-08-12 22:45:36 +00:00
|
|
|
let user_pw = value.get().expect("type checked upstream");
|
2019-08-07 18:58:12 +00:00
|
|
|
settings.user_pw = user_pw;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-08-07 18:58:12 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"timeout" => {
|
2019-08-09 11:37:35 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let timeout = value.get().expect("type checked upstream");
|
2019-08-09 11:37:35 +00:00
|
|
|
settings.timeout = timeout;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-08-09 11:37:35 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"compress" => {
|
2019-09-30 08:10:33 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let compress = value.get().expect("type checked upstream");
|
2019-09-30 08:10:33 +00:00
|
|
|
settings.compress = compress;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-09-30 08:10:33 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"extra-headers" => {
|
2019-10-01 20:35:55 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let extra_headers = value.get().expect("type checked upstream");
|
|
|
|
settings.extra_headers = extra_headers;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"cookies" => {
|
2019-10-01 20:35:55 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
settings.cookies = value.get::<Vec<String>>().expect("type checked upstream");
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"iradio-mode" => {
|
2019-10-01 21:10:53 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let iradio_mode = value.get().expect("type checked upstream");
|
2019-10-01 21:10:53 +00:00
|
|
|
settings.iradio_mode = iradio_mode;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
2019-10-01 21:10:53 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"keep-alive" => {
|
2019-10-02 05:59:25 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let keep_alive = value.get().expect("type checked upstream");
|
2019-10-02 05:59:25 +00:00
|
|
|
settings.keep_alive = keep_alive;
|
2021-04-14 21:58:37 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"proxy" => {
|
|
|
|
let proxy = proxy_from_str(
|
|
|
|
value
|
|
|
|
.get::<Option<String>>()
|
|
|
|
.expect("type checked upstream"),
|
|
|
|
);
|
|
|
|
match proxy {
|
|
|
|
Ok(proxy) => self
|
|
|
|
.set_proxy_prop(pspec.name(), proxy, move |settings| &mut settings.proxy),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"proxy-id" => {
|
|
|
|
let proxy_id = value
|
|
|
|
.get::<Option<String>>()
|
|
|
|
.expect("type checked upstream");
|
|
|
|
self.set_proxy_prop(pspec.name(), proxy_id, move |settings| {
|
|
|
|
&mut settings.proxy_id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
"proxy-pw" => {
|
|
|
|
let proxy_pw = value
|
|
|
|
.get::<Option<String>>()
|
|
|
|
.expect("type checked upstream");
|
|
|
|
self.set_proxy_prop(pspec.name(), proxy_pw, move |settings| {
|
|
|
|
&mut settings.proxy_pw
|
|
|
|
})
|
2019-10-02 05:59:25 +00:00
|
|
|
}
|
2018-12-30 10:03:32 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
2021-04-14 21:58:37 +00:00
|
|
|
|
|
|
|
if let Err(err) = res {
|
|
|
|
gst_error!(
|
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Failed to set property `{}`: {:?}",
|
|
|
|
pspec.name(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
2016-08-24 21:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:57:40 +00:00
|
|
|
fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"location" => {
|
2018-12-30 10:03:32 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
let location = settings.location.as_ref().map(Url::to_string);
|
|
|
|
|
2020-11-19 15:55:57 +00:00
|
|
|
location.to_value()
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-agent" => {
|
2019-08-04 18:17:40 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.user_agent.to_value()
|
2019-08-04 18:17:40 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"is-live" => obj.is_live().to_value(),
|
|
|
|
"user-id" => {
|
2019-08-07 18:58:12 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.user_id.to_value()
|
2019-08-07 18:58:12 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"user-pw" => {
|
2019-08-07 18:58:12 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.user_pw.to_value()
|
2019-08-07 18:58:12 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"timeout" => {
|
2019-08-09 11:37:35 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.timeout.to_value()
|
2019-08-09 11:37:35 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"compress" => {
|
2019-09-30 08:10:33 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.compress.to_value()
|
2019-09-30 08:10:33 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"extra-headers" => {
|
2019-10-01 20:35:55 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.extra_headers.to_value()
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"cookies" => {
|
2019-10-01 20:35:55 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.cookies.to_value()
|
2019-10-01 20:35:55 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"iradio-mode" => {
|
2019-10-01 21:10:53 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.iradio_mode.to_value()
|
2019-10-01 21:10:53 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"keep-alive" => {
|
2019-10-02 05:59:25 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.keep_alive.to_value()
|
2019-10-02 05:59:25 +00:00
|
|
|
}
|
2021-04-14 21:58:37 +00:00
|
|
|
// return None values as Some("") for compatibility with souphttpsrc
|
|
|
|
"proxy" => self
|
|
|
|
.settings
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.proxy
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or("")
|
|
|
|
.to_value(),
|
|
|
|
"proxy-id" => self.settings.lock().unwrap().proxy_id.to_value(),
|
|
|
|
"proxy-pw" => self.settings.lock().unwrap().proxy_pw.to_value(),
|
2018-12-30 10:03:32 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 14:04:53 +00:00
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn constructed(&self, obj: &Self::Type) {
|
2018-12-30 10:03:32 +00:00
|
|
|
self.parent_constructed(obj);
|
2020-11-14 17:24:01 +00:00
|
|
|
obj.set_automatic_eos(false);
|
|
|
|
obj.set_format(gst::Format::Bytes);
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 12:02:10 +00:00
|
|
|
impl ElementImpl for ReqwestHttpSrc {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"HTTP Source",
|
|
|
|
"Source/Network/HTTP",
|
|
|
|
"Read stream from an HTTP/HTTPS location",
|
|
|
|
"Sebastian Dröge <sebastian@centricular.com>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
|
|
|
let caps = gst::Caps::new_any();
|
|
|
|
let src_pad_template = gst::PadTemplate::new(
|
|
|
|
"src",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![src_pad_template]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn set_context(&self, element: &Self::Type, context: &gst::Context) {
|
2021-04-12 12:49:54 +00:00
|
|
|
if context.context_type() == REQWEST_CLIENT_CONTEXT {
|
2019-09-28 12:02:10 +00:00
|
|
|
let mut external_client = self.external_client.lock().unwrap();
|
2021-04-12 12:49:54 +00:00
|
|
|
let s = context.structure();
|
2019-09-28 12:02:10 +00:00
|
|
|
*external_client = s
|
2021-04-25 12:41:22 +00:00
|
|
|
.get::<&ClientContext>("client")
|
2019-09-28 12:02:10 +00:00
|
|
|
.map(|c| Some(c.clone()))
|
|
|
|
.unwrap_or(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.parent_set_context(element, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change_state(
|
|
|
|
&self,
|
2020-11-14 17:24:01 +00:00
|
|
|
element: &Self::Type,
|
2019-09-28 12:02:10 +00:00
|
|
|
transition: gst::StateChange,
|
|
|
|
) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
|
2019-11-24 22:00:27 +00:00
|
|
|
if let gst::StateChange::ReadyToNull = transition {
|
|
|
|
*self.client.lock().unwrap() = None;
|
2019-09-28 12:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.parent_change_state(element, transition)
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 14:04:53 +00:00
|
|
|
|
2019-06-26 04:40:21 +00:00
|
|
|
impl BaseSrcImpl for ReqwestHttpSrc {
|
2020-11-14 17:24:01 +00:00
|
|
|
fn is_seekable(&self, _src: &Self::Type) -> bool {
|
2018-12-30 10:03:32 +00:00
|
|
|
match *self.state.lock().unwrap() {
|
|
|
|
State::Started { seekable, .. } => seekable,
|
2016-08-22 19:25:58 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:57:40 +00:00
|
|
|
fn size(&self, _src: &Self::Type) -> Option<u64> {
|
2018-12-30 10:03:32 +00:00
|
|
|
match *self.state.lock().unwrap() {
|
|
|
|
State::Started { size, .. } => size,
|
2016-09-01 22:01:25 +00:00
|
|
|
_ => None,
|
2016-08-22 19:25:58 +00:00
|
|
|
}
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn unlock(&self, _src: &Self::Type) -> Result<(), gst::ErrorMessage> {
|
2019-12-30 14:00:12 +00:00
|
|
|
let canceller = self.canceller.lock().unwrap();
|
|
|
|
if let Some(ref canceller) = *canceller {
|
|
|
|
canceller.abort();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn start(&self, src: &Self::Type) -> Result<(), gst::ErrorMessage> {
|
2018-12-30 10:03:32 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
*state = State::Stopped;
|
|
|
|
|
2019-01-26 15:07:51 +00:00
|
|
|
let uri = self
|
|
|
|
.settings
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.location
|
|
|
|
.as_ref()
|
|
|
|
.ok_or_else(|| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(gst::CoreError::StateChange, ["Can't start without an URI"])
|
2019-01-26 15:07:51 +00:00
|
|
|
})
|
|
|
|
.map(|uri| uri.clone())?;
|
2018-12-30 10:03:32 +00:00
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Starting for URI {}", uri);
|
2019-10-01 19:34:11 +00:00
|
|
|
|
|
|
|
*state = self.do_request(src, uri, 0, None).map_err(|err| {
|
|
|
|
err.unwrap_or_else(|| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(gst::LibraryError::Failed, ["Interrupted during start"])
|
2019-10-01 19:34:11 +00:00
|
|
|
})
|
|
|
|
})?;
|
2019-01-26 15:07:51 +00:00
|
|
|
|
|
|
|
Ok(())
|
2016-08-22 19:25:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn stop(&self, src: &Self::Type) -> Result<(), gst::ErrorMessage> {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Stopping");
|
2018-12-30 10:03:32 +00:00
|
|
|
*self.state.lock().unwrap() = State::Stopped;
|
2016-05-14 14:04:53 +00:00
|
|
|
|
2019-01-26 15:07:51 +00:00
|
|
|
Ok(())
|
2016-05-14 14:04:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn query(&self, element: &Self::Type, query: &mut gst::QueryRef) -> bool {
|
2019-10-01 19:55:25 +00:00
|
|
|
use gst::QueryView;
|
|
|
|
|
|
|
|
match query.view_mut() {
|
|
|
|
QueryView::Scheduling(ref mut q) => {
|
|
|
|
q.set(
|
|
|
|
gst::SchedulingFlags::SEQUENTIAL | gst::SchedulingFlags::BANDWIDTH_LIMITED,
|
|
|
|
1,
|
|
|
|
-1,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
q.add_scheduling_modes(&[gst::PadMode::Push]);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => BaseSrcImplExt::parent_query(self, element, query),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn do_seek(&self, src: &Self::Type, segment: &mut gst::Segment) -> bool {
|
2018-12-30 10:03:32 +00:00
|
|
|
let segment = segment.downcast_mut::<gst::format::Bytes>().unwrap();
|
|
|
|
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
|
|
|
|
let (position, old_stop, uri) = match *state {
|
|
|
|
State::Started {
|
2017-04-12 13:46:11 +00:00
|
|
|
position,
|
|
|
|
stop,
|
|
|
|
ref uri,
|
|
|
|
..
|
|
|
|
} => (position, stop, uri.clone()),
|
2018-12-30 10:03:32 +00:00
|
|
|
State::Stopped => {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::element_error!(src, gst::LibraryError::Failed, ["Not started yet"]);
|
2018-12-30 10:03:32 +00:00
|
|
|
|
|
|
|
return false;
|
2016-09-01 21:52:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
let start = segment.start().expect("No start position given");
|
|
|
|
let stop = segment.stop();
|
2016-08-22 19:25:58 +00:00
|
|
|
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Seeking to {}-{:?}", start, stop);
|
2019-10-01 19:34:11 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
if position == start && old_stop == stop.0 {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "No change to current request");
|
2018-12-30 10:03:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2018-12-30 10:03:32 +00:00
|
|
|
*state = State::Stopped;
|
|
|
|
match self.do_request(src, uri, start, stop.0) {
|
|
|
|
Ok(s) => {
|
|
|
|
*state = s;
|
|
|
|
true
|
|
|
|
}
|
2019-10-01 19:34:11 +00:00
|
|
|
Err(Some(err)) => {
|
2020-06-30 20:57:22 +00:00
|
|
|
src.post_error_message(err);
|
2018-12-30 10:03:32 +00:00
|
|
|
false
|
|
|
|
}
|
2019-10-01 19:34:11 +00:00
|
|
|
Err(None) => false,
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
2016-05-15 08:48:54 +00:00
|
|
|
}
|
2020-03-29 21:29:40 +00:00
|
|
|
}
|
2016-05-15 08:48:54 +00:00
|
|
|
|
2020-03-29 21:29:40 +00:00
|
|
|
impl PushSrcImpl for ReqwestHttpSrc {
|
2020-11-14 17:24:01 +00:00
|
|
|
fn create(&self, src: &Self::Type) -> Result<gst::Buffer, gst::FlowError> {
|
2018-12-30 10:03:32 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
2016-12-27 15:47:39 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let (response, position, caps, tags) = match *state {
|
2018-12-30 10:03:32 +00:00
|
|
|
State::Started {
|
2019-12-30 14:00:12 +00:00
|
|
|
ref mut response,
|
2017-04-12 13:46:11 +00:00
|
|
|
ref mut position,
|
2019-10-01 21:10:53 +00:00
|
|
|
ref mut tags,
|
|
|
|
ref mut caps,
|
2017-04-12 13:46:11 +00:00
|
|
|
..
|
2019-12-30 14:00:12 +00:00
|
|
|
} => (response, position, caps, tags),
|
2018-12-30 10:03:32 +00:00
|
|
|
State::Stopped => {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::element_error!(src, gst::LibraryError::Failed, ["Not started yet"]);
|
2018-12-30 10:03:32 +00:00
|
|
|
|
2019-01-11 23:45:05 +00:00
|
|
|
return Err(gst::FlowError::Error);
|
2016-08-27 08:16:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-29 21:29:40 +00:00
|
|
|
let offset = *position;
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let mut current_response = match response.take() {
|
|
|
|
Some(response) => response,
|
2019-06-24 20:11:11 +00:00
|
|
|
None => {
|
2019-12-30 14:00:12 +00:00
|
|
|
gst_error!(CAT, obj: src, "Don't have a response");
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::element_error!(src, gst::ResourceError::Read, ["Don't have a response"]);
|
2018-12-30 10:03:32 +00:00
|
|
|
|
2019-06-24 20:11:11 +00:00
|
|
|
return Err(gst::FlowError::Error);
|
|
|
|
}
|
2016-09-12 13:00:28 +00:00
|
|
|
};
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2019-10-01 21:10:53 +00:00
|
|
|
let tags = tags.take();
|
|
|
|
let caps = caps.take();
|
2019-06-24 20:11:11 +00:00
|
|
|
drop(state);
|
2019-10-01 21:10:53 +00:00
|
|
|
|
|
|
|
if let Some(caps) = caps {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Setting caps {:?}", caps);
|
2019-10-01 21:10:53 +00:00
|
|
|
src.set_caps(&caps)
|
|
|
|
.map_err(|_| gst::FlowError::NotNegotiated)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(tags) = tags {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Sending iradio tags {:?}", tags);
|
2021-04-20 12:58:11 +00:00
|
|
|
let pad = src.static_pad("src").unwrap();
|
2020-06-24 22:33:57 +00:00
|
|
|
pad.push_event(gst::event::Tag::new(tags));
|
2019-10-01 21:10:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
let future = async {
|
|
|
|
current_response.chunk().await.map_err(move |err| {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::error_msg!(
|
2019-12-30 14:00:12 +00:00
|
|
|
gst::ResourceError::Read,
|
|
|
|
["Failed to read chunk at offset {}: {:?}", offset, err]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let res = self.wait(future);
|
2016-08-27 08:16:17 +00:00
|
|
|
|
2019-10-01 19:34:11 +00:00
|
|
|
let res = match res {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(Some(err)) => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Error {:?}", err);
|
2020-06-30 20:57:22 +00:00
|
|
|
src.post_error_message(err);
|
2019-10-01 19:34:11 +00:00
|
|
|
return Err(gst::FlowError::Error);
|
|
|
|
}
|
|
|
|
Err(None) => {
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "Flushing");
|
2019-10-01 19:34:11 +00:00
|
|
|
return Err(gst::FlowError::Flushing);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-24 20:11:11 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
2019-12-30 14:00:12 +00:00
|
|
|
let (response, position) = match *state {
|
2019-06-24 20:11:11 +00:00
|
|
|
State::Started {
|
2019-12-30 14:00:12 +00:00
|
|
|
ref mut response,
|
2019-06-24 20:11:11 +00:00
|
|
|
ref mut position,
|
|
|
|
..
|
2019-12-30 14:00:12 +00:00
|
|
|
} => (response, position),
|
2019-06-24 20:11:11 +00:00
|
|
|
State::Stopped => {
|
2020-12-20 18:43:45 +00:00
|
|
|
gst::element_error!(src, gst::LibraryError::Failed, ["Not started yet"]);
|
2016-09-01 21:52:28 +00:00
|
|
|
|
2019-06-24 20:11:11 +00:00
|
|
|
return Err(gst::FlowError::Error);
|
|
|
|
}
|
|
|
|
};
|
2016-09-12 13:00:28 +00:00
|
|
|
|
2019-06-24 20:11:11 +00:00
|
|
|
match res {
|
2019-12-30 14:00:12 +00:00
|
|
|
Some(chunk) => {
|
2019-06-24 20:11:11 +00:00
|
|
|
/* do something with the chunk and store the body again in the state */
|
|
|
|
|
2019-10-01 19:34:11 +00:00
|
|
|
gst_trace!(
|
2019-10-31 22:34:21 +00:00
|
|
|
CAT,
|
2019-10-01 19:34:11 +00:00
|
|
|
obj: src,
|
|
|
|
"Chunk of {} bytes received at offset {}",
|
|
|
|
chunk.len(),
|
|
|
|
offset
|
|
|
|
);
|
2019-06-24 20:11:11 +00:00
|
|
|
let size = chunk.len();
|
|
|
|
assert_ne!(chunk.len(), 0);
|
|
|
|
|
|
|
|
*position += size as u64;
|
|
|
|
|
2019-07-18 16:46:34 +00:00
|
|
|
let mut buffer = gst::Buffer::from_slice(chunk);
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2019-12-30 14:00:12 +00:00
|
|
|
*response = Some(current_response);
|
2019-06-24 20:11:11 +00:00
|
|
|
|
2019-07-18 16:46:34 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
|
|
|
buffer.set_offset(offset);
|
2019-10-01 19:34:11 +00:00
|
|
|
buffer.set_offset_end(offset + size as u64);
|
2019-07-18 16:46:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:30:26 +00:00
|
|
|
Ok(buffer)
|
2019-06-24 20:11:11 +00:00
|
|
|
}
|
2019-12-30 14:00:12 +00:00
|
|
|
None => {
|
2019-06-24 20:11:11 +00:00
|
|
|
/* No further data, end of stream */
|
2019-10-31 22:34:21 +00:00
|
|
|
gst_debug!(CAT, obj: src, "End of stream");
|
2019-12-30 14:00:12 +00:00
|
|
|
*response = Some(current_response);
|
2019-07-04 15:30:26 +00:00
|
|
|
Err(gst::FlowError::Eos)
|
2019-06-24 20:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 04:40:21 +00:00
|
|
|
impl URIHandlerImpl for ReqwestHttpSrc {
|
2021-01-21 18:21:29 +00:00
|
|
|
const URI_TYPE: gst::URIType = gst::URIType::Src;
|
|
|
|
|
2021-04-20 12:57:40 +00:00
|
|
|
fn protocols() -> &'static [&'static str] {
|
2021-01-21 18:21:29 +00:00
|
|
|
&["http", "https"]
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:57:40 +00:00
|
|
|
fn uri(&self, _element: &Self::Type) -> Option<String> {
|
2018-12-30 10:03:32 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
|
|
|
|
settings.location.as_ref().map(Url::to_string)
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:24:01 +00:00
|
|
|
fn set_uri(&self, element: &Self::Type, uri: &str) -> Result<(), glib::Error> {
|
2019-08-12 22:45:36 +00:00
|
|
|
self.set_location(&element, Some(uri))
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2019-06-26 04:40:21 +00:00
|
|
|
impl ObjectSubclass for ReqwestHttpSrc {
|
|
|
|
const NAME: &'static str = "ReqwestHttpSrc";
|
2020-11-14 17:24:01 +00:00
|
|
|
type Type = super::ReqwestHttpSrc;
|
2020-03-29 21:29:40 +00:00
|
|
|
type ParentType = gst_base::PushSrc;
|
2021-01-21 18:21:29 +00:00
|
|
|
type Interfaces = (gst::URIHandler,);
|
2018-12-30 10:03:32 +00:00
|
|
|
}
|