2016-11-25 17:34:00 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
|
|
|
// Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// In addition, as a special exception, the copyright holders give
|
|
|
|
// permission to link the code of portions of this program with the
|
|
|
|
// OpenSSL library under certain conditions as described in each
|
|
|
|
// individual source file, and distribute linked combinations including
|
|
|
|
// the two.
|
|
|
|
//
|
|
|
|
// You must obey the GNU General Public License in all respects for all
|
|
|
|
// of the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
// exception, you may extend this exception to your version of the
|
|
|
|
// file(s), but you are not obligated to do so. If you do not wish to do
|
|
|
|
// so, delete this exception statement from your version. If you delete
|
|
|
|
// this exception statement from all source files in the program, then
|
|
|
|
// also delete it here.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::h264;
|
2019-07-04 17:51:38 -04:00
|
|
|
use cstr::*;
|
2018-12-28 22:53:29 -05:00
|
|
|
use failure::{Error, bail};
|
|
|
|
use ffmpeg;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use log::{debug, info, warn};
|
2019-07-05 00:22:45 -04:00
|
|
|
use std::ffi::CString;
|
2016-11-25 17:34:00 -05:00
|
|
|
use std::result::Result;
|
|
|
|
|
2019-07-25 00:52:55 -04:00
|
|
|
static START: parking_lot::Once = parking_lot::Once::new();
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref FFMPEG: Ffmpeg = Ffmpeg::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Source<'a> {
|
2019-02-14 01:34:19 -05:00
|
|
|
/// A filename, for testing.
|
2016-11-25 17:34:00 -05:00
|
|
|
#[cfg(test)]
|
2019-02-14 01:34:19 -05:00
|
|
|
File(&'a str),
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2019-02-14 01:34:19 -05:00
|
|
|
/// An RTSP stream, for production use.
|
|
|
|
Rtsp {
|
|
|
|
url: &'a str,
|
|
|
|
redacted_url: &'a str
|
|
|
|
},
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
pub trait Opener<S : Stream> : Sync {
|
|
|
|
fn open(&self, src: Source) -> Result<S, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Stream {
|
|
|
|
fn get_extra_data(&self) -> Result<h264::ExtraData, Error>;
|
2018-12-28 22:53:29 -05:00
|
|
|
fn get_next<'p>(&'p mut self) -> Result<ffmpeg::Packet<'p>, ffmpeg::Error>;
|
2016-12-06 21:41:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Ffmpeg {}
|
|
|
|
|
|
|
|
impl Ffmpeg {
|
|
|
|
fn new() -> Ffmpeg {
|
2016-11-25 17:34:00 -05:00
|
|
|
START.call_once(|| {
|
2018-12-28 22:53:29 -05:00
|
|
|
ffmpeg::Ffmpeg::new();
|
2017-09-21 00:06:06 -04:00
|
|
|
//ffmpeg::init().unwrap();
|
|
|
|
//ffmpeg::format::network::init();
|
2016-11-25 17:34:00 -05:00
|
|
|
});
|
2016-12-06 21:41:44 -05:00
|
|
|
Ffmpeg{}
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
impl Opener<FfmpegStream> for Ffmpeg {
|
|
|
|
fn open(&self, src: Source) -> Result<FfmpegStream, Error> {
|
2018-12-28 22:53:29 -05:00
|
|
|
use ffmpeg::InputFormatContext;
|
2017-09-21 00:06:06 -04:00
|
|
|
let (mut input, discard_first) = match src {
|
2016-11-25 17:34:00 -05:00
|
|
|
#[cfg(test)]
|
2017-10-10 00:44:48 -04:00
|
|
|
Source::File(filename) => {
|
2018-12-28 22:53:29 -05:00
|
|
|
let mut open_options = ffmpeg::Dictionary::new();
|
2017-10-10 00:44:48 -04:00
|
|
|
|
|
|
|
// Work around https://github.com/scottlamb/moonfire-nvr/issues/10
|
2019-07-04 17:51:38 -04:00
|
|
|
open_options.set(cstr!("advanced_editlist"), cstr!("false")).unwrap();
|
2017-10-10 00:44:48 -04:00
|
|
|
let url = format!("file:{}", filename);
|
|
|
|
let i = InputFormatContext::open(&CString::new(url.clone()).unwrap(),
|
|
|
|
&mut open_options)?;
|
|
|
|
if !open_options.empty() {
|
|
|
|
warn!("While opening URL {}, some options were not understood: {}",
|
|
|
|
url, open_options);
|
|
|
|
}
|
|
|
|
(i, false)
|
|
|
|
}
|
2019-02-14 01:34:19 -05:00
|
|
|
Source::Rtsp{url, redacted_url} => {
|
2018-12-28 22:53:29 -05:00
|
|
|
let mut open_options = ffmpeg::Dictionary::new();
|
2019-07-04 17:51:38 -04:00
|
|
|
open_options.set(cstr!("rtsp_transport"), cstr!("tcp")).unwrap();
|
|
|
|
open_options.set(cstr!("user-agent"), cstr!("moonfire-nvr")).unwrap();
|
2017-09-21 00:06:06 -04:00
|
|
|
// 10-second socket timeout, in microseconds.
|
2019-07-04 17:51:38 -04:00
|
|
|
open_options.set(cstr!("stimeout"), cstr!("10000000")).unwrap();
|
2019-02-12 01:58:09 -05:00
|
|
|
|
|
|
|
// Moonfire NVR currently only supports video, so receiving audio is wasteful.
|
|
|
|
// It also triggers <https://github.com/scottlamb/moonfire-nvr/issues/36>.
|
2019-07-04 17:51:38 -04:00
|
|
|
open_options.set(cstr!("allowed_media_types"), cstr!("video")).unwrap();
|
2019-02-12 01:58:09 -05:00
|
|
|
|
2017-09-21 00:06:06 -04:00
|
|
|
let i = InputFormatContext::open(&CString::new(url).unwrap(), &mut open_options)?;
|
|
|
|
if !open_options.empty() {
|
|
|
|
warn!("While opening URL {}, some options were not understood: {}",
|
2019-02-14 01:34:19 -05:00
|
|
|
redacted_url, open_options);
|
2017-09-21 00:06:06 -04:00
|
|
|
}
|
|
|
|
(i, true)
|
2016-11-25 17:34:00 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-09-21 00:06:06 -04:00
|
|
|
input.find_stream_info()?;
|
|
|
|
|
2016-11-25 17:34:00 -05:00
|
|
|
// Find the video stream.
|
|
|
|
let mut video_i = None;
|
2017-09-21 00:06:06 -04:00
|
|
|
{
|
|
|
|
let s = input.streams();
|
|
|
|
for i in 0 .. s.len() {
|
2019-12-29 09:35:39 -05:00
|
|
|
if s.get(i).codecpar().codec_type().is_video() {
|
2017-09-21 00:06:06 -04:00
|
|
|
debug!("Video stream index is {}", i);
|
|
|
|
video_i = Some(i);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let video_i = match video_i {
|
|
|
|
Some(i) => i,
|
2018-02-21 01:46:14 -05:00
|
|
|
None => bail!("no video stream"),
|
2016-11-25 17:34:00 -05:00
|
|
|
};
|
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
let mut stream = FfmpegStream{
|
2017-09-21 00:06:06 -04:00
|
|
|
input,
|
|
|
|
video_i,
|
2016-11-25 17:34:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if discard_first {
|
|
|
|
info!("Discarding the first packet to work around https://trac.ffmpeg.org/ticket/5018");
|
|
|
|
stream.get_next()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
pub struct FfmpegStream {
|
2018-12-28 22:53:29 -05:00
|
|
|
input: ffmpeg::InputFormatContext,
|
2016-11-25 17:34:00 -05:00
|
|
|
video_i: usize,
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
impl Stream for FfmpegStream {
|
|
|
|
fn get_extra_data(&self) -> Result<h264::ExtraData, Error> {
|
2017-09-21 00:06:06 -04:00
|
|
|
let video = self.input.streams().get(self.video_i);
|
|
|
|
let tb = video.time_base();
|
|
|
|
if tb.num != 1 || tb.den != 90000 {
|
2018-02-21 01:46:14 -05:00
|
|
|
bail!("video stream has timebase {}/{}; expected 1/90000", tb.num, tb.den);
|
2017-09-21 00:06:06 -04:00
|
|
|
}
|
2019-12-29 09:35:39 -05:00
|
|
|
let codec = video.codecpar();
|
2017-09-21 00:06:06 -04:00
|
|
|
let codec_id = codec.codec_id();
|
|
|
|
if !codec_id.is_h264() {
|
2018-02-21 01:46:14 -05:00
|
|
|
bail!("stream's video codec {:?} is not h264", codec_id);
|
2017-09-21 00:06:06 -04:00
|
|
|
}
|
|
|
|
h264::ExtraData::parse(codec.extradata(), codec.width() as u16, codec.height() as u16)
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:53:29 -05:00
|
|
|
fn get_next<'i>(&'i mut self) -> Result<ffmpeg::Packet<'i>, ffmpeg::Error> {
|
2016-11-25 17:34:00 -05:00
|
|
|
loop {
|
2017-09-21 00:06:06 -04:00
|
|
|
let p = self.input.read_frame()?;
|
|
|
|
if p.stream_index() == self.video_i {
|
|
|
|
return Ok(p);
|
2016-11-25 17:34:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|