A tool to get a HLS video stream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

237 lines
8.4 KiB

use std::{path::Path, time::Duration};
use anyhow::anyhow;
use futures_util::StreamExt as _;
use http::{header::{CONTENT_TYPE, RANGE}, uri::{Authority, Scheme}, Request, Response, Uri, request::Builder as RequestBuilder};
use http_body_util::BodyDataStream;
use m3u8_rs::{MediaPlaylist, MediaSegment, Playlist};
use reqwest::{redirect::Policy, Body};
use tokio::{fs::File, io::AsyncWriteExt as _, time::timeout};
use tower::{ServiceBuilder, ServiceExt as _};
use tower_http_client::{client::BodyReader, ServiceExt as _};
use tower_reqwest::HttpClientLayer;
use crate::download_error::DownloadError;
use log::{log, Level};
type HttpClient = tower::util::BoxCloneService<Request<Body>, Response<Body>, anyhow::Error>;
#[derive(Clone, Debug)]
pub struct State {
scheme: Scheme,
auth: Authority,
base_path: String,
timeout: Duration,
client: HttpClient,
}
impl State {
pub fn new(uri: &Uri, rate_limit: u64, concurrency_limit: usize) -> anyhow::Result<Self>
{
let scheme = uri.scheme()
. ok_or(anyhow!("Problem scheme in m3u8 uri"))?;
let authority = uri.authority()
. ok_or(anyhow!("Problem authority in m3u8 uri"))?;
let base_path = Path::new(uri.path()).parent()
. ok_or(anyhow!("Path problem"))?
. to_str()
. ok_or(anyhow!("Path problem"))?;
let state = State {
scheme: scheme.clone(),
auth: authority.clone(),
base_path: base_path.to_string(),
timeout: Duration::from_secs(10),
client: ServiceBuilder::new()
// Add some layers.
. buffer(64)
. rate_limit(rate_limit, Duration::from_secs(1))
. concurrency_limit(concurrency_limit)
// Make client compatible with the `tower-http` layers.
. layer(HttpClientLayer)
. service(reqwest::Client::builder()
. redirect(Policy::limited(5))
. build()? )
. map_err(anyhow::Error::msg)
. boxed_clone(),
};
log!(Level::Debug, "-> state: {:?}", state);
Ok(state)
}
pub(super) fn set_timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}
pub(super) async fn get_m3u8_segment_uris(&mut self, path_and_query: &str)
-> anyhow::Result<Vec<Uri>>
{
let uri = Uri::builder()
. scheme(self.scheme.clone())
. authority(self.auth.clone())
. path_and_query(path_and_query)
. build()?;
let mut response = self.request(&uri, 0).await?;
// read body into Vec<u8>
let body: Vec<u8> = BodyReader::new(response.body_mut())
. bytes().await?.to_vec();
match m3u8_rs::parse_playlist(&body) {
Result::Err(e) => Err(anyhow!("m3u8 parse error: {}", e)),
Result::Ok((_, Playlist::MasterPlaylist(_))) =>
Err(anyhow!("Master playlist not supported now")),
Result::Ok((_, Playlist::MediaPlaylist(pl))) => {
Self::write_playlist(&uri, &pl).await?;
pl.segments.iter().map(|s| self.download_uri(s) ).collect()
},
}
}
pub(super) async fn get_m3u8_segment(&mut self, uri: &Uri)
-> Result<(), DownloadError>
{
let mut response = self.request(uri, 0).await
. map_err(|e| DownloadError::new(uri.clone(), Some(e)))?;
// We always need the content-type to be able to decide
let content_type = response.headers()[CONTENT_TYPE].to_str()
. expect("No content-type header found in response");
if content_type != "video/MP2T" {
let message = format!("unexpected content-type: {}", content_type);
log!(Level::Debug, "{}", message);
return Err(DownloadError::new( uri.clone()
, Some(anyhow!(message)) ));
}
// I consider a missing path as fatal... there is absolutely nothing we can do about it
// and we need all files from the playlist.
let path = uri.path();
let filename = Path::new(path)
. file_name()
. expect("no filename in path_and_query");
let mut file = File::create(filename).await
. expect("can not create file for writing");
// read body into file as stream
let mut body_stream = BodyDataStream::new(response.body_mut());
'label: loop {
let data = timeout(self.timeout, body_stream.next()).await
. map_err(|e| DownloadError::new(uri.clone(), Some(e.into())))?;
match data {
None => break 'label,
Some(Err(e)) =>
return Err(DownloadError::new(uri.clone(), Some(e.into()))),
Some(Ok(data)) => {
file.write_all(data.as_ref()).await
. map_err(|e|
DownloadError::new(uri.clone(), Some(e.into())) )?;
},
}
};
Ok(())
}
fn download_uri(&self, segment: &MediaSegment) -> anyhow::Result<Uri>
{
match Uri::try_from(segment.uri.clone()) {
Ok(uri) => {
let scheme = uri.scheme().unwrap_or(&self.scheme);
let auth = uri.authority().unwrap_or(&self.auth);
let path_and_query = uri.path_and_query()
. ok_or(anyhow!("No path in Uri"))?;
Ok(Uri::builder() . scheme(scheme.clone())
. authority(auth.clone())
. path_and_query(path_and_query.clone())
. build()?)
}
Err(_) => {
Ok(Uri::builder() . scheme(self.scheme.clone())
. authority(self.auth.clone())
. path_and_query(self.base_path.clone() + "/" + &segment.uri)
. build()?)
}
}
}
fn uri_relative_path(uri: &Uri) -> anyhow::Result<String>
{
Self::relative_path(uri.path())
}
fn relative_path(path: &str) -> anyhow::Result<String>
{
let filename = Path::new(path)
. file_name()
. ok_or(anyhow!("name error"))?
. to_str()
. ok_or(anyhow!("Error getting filename from uri"))?;
Ok(filename.to_string())
}
async fn request(&mut self, uri: &Uri, from: usize) -> anyhow::Result<Response<Body>>
{
let request = RequestBuilder::new()
. uri(uri)
. header(RANGE, format!("{}-", from))
. body(Body::default())?;
log!(Level::Debug, "{:?}", request);
// Send get request with timeout.
// let send_fut = self.client.get(uri).send()?;
let send_fut = self.client.execute(request);
let response = timeout(self.timeout, send_fut).await??;
anyhow::ensure!( response.status().is_success()
, "resonse status failed: {}"
, response.status() );
log!(Level::Debug, "{:?}", response);
Ok(response)
}
async fn write_playlist(uri: &Uri, playlist: &MediaPlaylist) -> anyhow::Result<()> {
let filename = Path::new(uri.path())
. file_name()
. ok_or(anyhow!("can't extract filename from uri"))?;
let mut file = File::create(filename).await?;
let segments: anyhow::Result<Vec<_>> = playlist.segments.iter().map(|s| {
let mut new_segment = s.clone();
Ok(match Uri::try_from(s.uri.clone()) {
Ok(uri) => {
new_segment.uri = Self::uri_relative_path(&uri)?;
new_segment
}
Err(_) => {
let uri = s.uri.split_once('?')
. map(|(s,_)| s)
. unwrap_or(&s.uri);
new_segment.uri = Self::relative_path(uri)?;
new_segment
}
})
}).collect();
let mut out_pl = playlist.clone();
out_pl.segments = segments?;
let mut file_data = vec![];
out_pl.write_to(&mut file_data)?;
file.write_all(&file_data).await?;
Ok(())
}
}