Initial checkin

This commit is contained in:
Philippe Normand 2020-07-01 19:21:13 +01:00
commit e5a3621705
15 changed files with 2596 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

4
.gitmodules vendored Normal file
View file

@ -0,0 +1,4 @@
[submodule "wpe-graphics-overlays"]
path = wpe-graphics-overlays
url = https://github.com/Igalia/wpe-graphics-overlays.git
branch = igalia

1425
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

29
Cargo.toml Normal file
View file

@ -0,0 +1,29 @@
[package]
name = "embedded-gst-wpe-demo"
version = "0.1.0"
authors = ["Philippe Normand <philn@igalia.com>"]
edition = "2018"
[dependencies]
anyhow = "1.0.31"
async-tungstenite = { version = "0.7", features = ["gio-runtime"] }
base64 = "0.11"
futures = "0.3"
gio = "0.8"
glib = "0.9"
gst = { package = "gstreamer", version = "0.15", features = ["v1_10"] }
gst-sdp = { package = "gstreamer-sdp", version = "0.15", features = ["v1_14"] }
gst-webrtc = { package = "gstreamer-webrtc", version = "0.15" }
http = "0.2"
num = "0.2"
rand = "0.7"
serde = "1"
serde_any = "0.5"
serde_derive = "1"
serde_json = "1.0.53"
strfmt = "0.1.6"
url = "2"
structopt = { version = "0.3", default-features = false }
env_logger = "0.7.1"
log = "0.4.8"

BIN
janus-web-app/igalia-bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

21
janus-web-app/index.html Normal file
View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="/webrtc.js"></script>
<style type="text/css">
body {
background-image: url('igalia-bg.png');
background-repeat: no-repeat;
background-size: 80vh;
}
</style>
</head>
<body onload="start()">
<video id="remoteVideo" muted autoplay></video>
<span id="message"/>
</body>
</html>

84
janus-web-app/webrtc.js Normal file
View file

@ -0,0 +1,84 @@
var remoteVideo;
var peerConnection;
var janusConnection;
var sessionId;
var handleId;
const roomId = 1234;
const feedId = 42;
const CONFIG = { audio: false, video: false, iceServers: [ {urls: 'stun:stun.l.google.com:19302'}, ] };
const RECEIVERS = { create_session, create_handle, publish, join_subscriber, ack };
function send(msg) {
janusConnection.send(JSON.stringify(msg));
}
function ack(payload) {}
function start() {
remoteVideo = document.getElementById('remoteVideo');
janusConnection = new WebSocket('wss://' + window.location.hostname + ':8989', 'janus-protocol');
janusConnection.onmessage = function(message) {
var payload = JSON.parse(message.data);
var receiver = RECEIVERS[payload.janus] || RECEIVERS[payload.transaction] || console.log;
receiver(payload);
};
janusConnection.onopen = function(event) {
send({janus: 'create', transaction: 'create_session'});
};
}
function join_broadcast() {
peerConnection = new RTCPeerConnection(CONFIG);
peerConnection.onicecandidate = on_ice_candidate;
peerConnection.ontrack = on_track;
send({janus: 'message', transaction: 'join_subscriber',
body: {request : 'join', ptype: 'subscriber', room: roomId, feed: feedId},
session_id: sessionId, handle_id: handleId});
}
function on_ice_candidate(event) {
send({janus: 'trickle', transaction: 'candidate', candidate: event.candidate,
session_id: sessionId, handle_id: handleId});
}
function on_track(event) {
remoteVideo.srcObject = event.streams[0];
remoteVideo.play();
}
function keepalive() {
send({janus: 'keepalive', transaction: 'keepalive', session_id: sessionId});
}
function create_session(payload) {
sessionId = payload.data.id;
setInterval(keepalive, 30000);
send({janus: 'attach', transaction: 'create_handle', plugin: 'janus.plugin.videoroom', session_id: sessionId});
}
function create_handle(payload) {
handleId = payload.data.id;
join_broadcast();
}
function publish(payload) {
peerConnection.setRemoteDescription(new RTCSessionDescription(payload.jsep));
}
function join_subscriber(payload) {
if (!payload.jsep) {
var container = document.getElementById('message');
if (payload.plugindata.data.error_code == 428) {
container.innerHTML = "GstWPE demo is offline. ";
}
container.innerHTML += payload.plugindata.data.error;
return;
}
peerConnection.setRemoteDescription(new RTCSessionDescription(payload.jsep));
peerConnection.createAnswer().then(function(answer) {
peerConnection.setLocalDescription(answer);
send({janus: 'message', transaction: 'blah', body: {request: 'start'},
jsep: answer, session_id: sessionId, handle_id: handleId});
});
}

53
src/app.rs Normal file
View file

@ -0,0 +1,53 @@
use crate::janus;
use crate::pipeline::Pipeline;
use anyhow::anyhow;
use gst::prelude::*;
use std::ops;
use std::rc::Rc;
// Our refcounted application struct for containing all the state we have to carry around.
//
// This represents our main application window.
#[derive(Clone)]
pub struct App(Rc<AppInner>);
// Deref into the contained struct to make usage a bit more ergonomic
impl ops::Deref for App {
type Target = AppInner;
fn deref(&self) -> &AppInner {
&*self.0
}
}
pub struct AppInner {
pipeline: Pipeline,
}
impl App {
pub fn new() -> Result<Self, anyhow::Error> {
let pipeline =
Pipeline::new().map_err(|err| anyhow!("Error creating pipeline: {:?}", err))?;
let app = App(Rc::new(AppInner { pipeline }));
Ok(app)
}
pub async fn run(&self) -> Result<(), anyhow::Error> {
self.pipeline.prepare()?;
let bin = self.pipeline.pipeline.clone().upcast::<gst::Bin>();
let mut gw = janus::JanusGateway::new(bin).await?;
self.pipeline.start()?;
gw.run().await?;
Ok(())
}
}
// Make sure to shut down the pipeline when it goes out of scope
// to release any system resources
impl Drop for AppInner {
fn drop(&mut self) {
let _ = self.pipeline.stop();
}
}

618
src/janus.rs Normal file
View file

@ -0,0 +1,618 @@
// GStreamer
//
// Copyright (C) 2018 maxmcd <max.t.mcdonnell@gmail.com>
// Copyright (C) 2019 Sebastian Dröge <sebastian@centricular.com>
// Copyright (C) 2020 Philippe Normand <philn@igalia.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301, USA.
use {
anyhow::{anyhow, bail, Context},
async_tungstenite::{gio::connect_async, tungstenite},
futures::channel::mpsc,
futures::sink::{Sink, SinkExt},
futures::stream::{Stream, StreamExt},
gst::gst_element_error,
gst::prelude::*,
http::Request,
rand::prelude::*,
serde_derive::{Deserialize, Serialize},
serde_json::json,
std::sync::{Arc, Mutex, Weak},
structopt::StructOpt,
tungstenite::Message as WsMessage,
};
#[derive(Debug, StructOpt)]
pub struct Args {
#[structopt(short, long, default_value = "wss://janus.conf.meetecho.com/ws:8989")]
server: String,
#[structopt(short, long, default_value = "1234")]
room_id: u32,
#[structopt(short, long, default_value = "666")]
feed_id: u32,
}
#[derive(Serialize, Deserialize, Debug)]
struct Base {
janus: String,
transaction: Option<String>,
session_id: Option<i64>,
sender: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
struct DataHolder {
id: i64,
}
#[derive(Serialize, Deserialize, Debug)]
struct PluginDataHolder {
videoroom: String,
room: Option<i64>,
#[serde(rename = "current-bitrate")]
current_bitrate: Option<i64>,
description: Option<String>,
id: Option<i64>,
configured: Option<String>,
video_codec: Option<String>,
unpublished: Option<i64>,
}
#[derive(Serialize, Deserialize, Debug)]
struct PluginHolder {
plugin: String,
data: PluginDataHolder,
}
#[derive(Serialize, Deserialize, Debug)]
struct IceHolder {
candidate: String,
#[serde(rename = "sdpMLineIndex")]
sdp_mline_index: u32,
}
#[derive(Serialize, Deserialize, Debug)]
struct JsepHolder {
#[serde(rename = "type")]
type_: String,
sdp: Option<String>,
ice: Option<IceHolder>,
}
#[derive(Serialize, Deserialize, Debug)]
struct JsonReply {
#[serde(flatten)]
base: Base,
data: Option<DataHolder>,
#[serde(rename = "plugindata")]
plugin_data: Option<PluginHolder>,
jsep: Option<JsepHolder>,
}
fn transaction_id() -> String {
thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(30)
.collect()
}
// Strong reference to the state of one peer
#[derive(Debug, Clone)]
struct Peer(Arc<PeerInner>);
// Weak reference to the state of one peer
#[derive(Debug, Clone)]
struct PeerWeak(Weak<PeerInner>);
impl PeerWeak {
// Try upgrading a weak reference to a strong one
fn upgrade(&self) -> Option<Peer> {
self.0.upgrade().map(Peer)
}
}
// To be able to access the Peers's fields directly
impl std::ops::Deref for Peer {
type Target = PeerInner;
fn deref(&self) -> &PeerInner {
&self.0
}
}
#[derive(Clone, Copy, Debug)]
struct ConnectionHandle {
id: i64,
session_id: i64,
}
// Actual peer state
#[derive(Debug)]
struct PeerInner {
handle: ConnectionHandle,
bin: gst::Bin,
webrtcbin: gst::Element,
send_msg_tx: Arc<Mutex<mpsc::UnboundedSender<WsMessage>>>,
}
impl Peer {
// Downgrade the strong reference to a weak reference
fn downgrade(&self) -> PeerWeak {
PeerWeak(Arc::downgrade(&self.0))
}
// Whenever webrtcbin tells us that (re-)negotiation is needed, simply ask
// for a new offer SDP from webrtcbin without any customization and then
// asynchronously send it to the peer via the WebSocket connection
fn on_negotiation_needed(&self) -> Result<(), anyhow::Error> {
info!("starting negotiation with peer");
let peer_clone = self.downgrade();
let promise = gst::Promise::new_with_change_func(move |res| {
let s = res.expect("no answer");
let peer = upgrade_weak!(peer_clone);
if let Err(err) = peer.on_offer_created(&s.to_owned()) {
gst_element_error!(
peer.bin,
gst::LibraryError::Failed,
("Failed to send SDP offer: {:?}", err)
);
}
});
self.webrtcbin
.emit("create-offer", &[&None::<gst::Structure>, &promise])?;
Ok(())
}
// Once webrtcbin has create the offer SDP for us, handle it by sending it to the peer via the
// WebSocket connection
fn on_offer_created(&self, reply: &gst::Structure) -> Result<(), anyhow::Error> {
let offer = reply
.get_value("offer")?
.get::<gst_webrtc::WebRTCSessionDescription>()
.expect("Invalid argument")
.expect("Invalid offer");
self.webrtcbin
.emit("set-local-description", &[&offer, &None::<gst::Promise>])?;
info!("sending SDP offer to peer: {:?}", offer.get_sdp().as_text());
let transaction = transaction_id();
let sdp_data = offer.get_sdp().as_text()?;
let msg = WsMessage::Text(
json!({
"janus": "message",
"transaction": transaction,
"session_id": self.handle.session_id,
"handle_id": self.handle.id,
"body": {
"request": "publish",
"audio": true,
"video": true,
},
"jsep": {
"sdp": sdp_data,
"trickle": true,
"type": "offer"
}
})
.to_string(),
);
self.send_msg_tx
.lock()
.expect("Invalid message sender")
.unbounded_send(msg)
.with_context(|| "Failed to send SDP offer".to_string())?;
Ok(())
}
// Once webrtcbin has create the answer SDP for us, handle it by sending it to the peer via the
// WebSocket connection
fn on_answer_created(&self, reply: &gst::Structure) -> Result<(), anyhow::Error> {
let answer = reply
.get_value("answer")?
.get::<gst_webrtc::WebRTCSessionDescription>()
.expect("Invalid argument")
.expect("Invalid answer");
self.webrtcbin
.emit("set-local-description", &[&answer, &None::<gst::Promise>])?;
info!(
"sending SDP answer to peer: {:?}",
answer.get_sdp().as_text()
);
Ok(())
}
// Handle incoming SDP answers from the peer
fn handle_sdp(&self, type_: &str, sdp: &str) -> Result<(), anyhow::Error> {
if type_ == "answer" {
info!("Received answer:\n{}\n", sdp);
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| anyhow!("Failed to parse SDP answer"))?;
let answer =
gst_webrtc::WebRTCSessionDescription::new(gst_webrtc::WebRTCSDPType::Answer, ret);
self.webrtcbin
.emit("set-remote-description", &[&answer, &None::<gst::Promise>])?;
Ok(())
} else if type_ == "offer" {
info!("Received offer:\n{}\n", sdp);
let ret = gst_sdp::SDPMessage::parse_buffer(sdp.as_bytes())
.map_err(|_| anyhow!("Failed to parse SDP offer"))?;
// And then asynchronously start our pipeline and do the next steps. The
// pipeline needs to be started before we can create an answer
let peer_clone = self.downgrade();
self.bin.call_async(move |_pipeline| {
let peer = upgrade_weak!(peer_clone);
let offer = gst_webrtc::WebRTCSessionDescription::new(
gst_webrtc::WebRTCSDPType::Offer,
ret,
);
peer.0
.webrtcbin
.emit("set-remote-description", &[&offer, &None::<gst::Promise>])
.expect("Unable to set remote description");
let peer_clone = peer.downgrade();
let promise = gst::Promise::new_with_change_func(move |reply| {
let s = reply.expect("No answer");
let peer = upgrade_weak!(peer_clone);
if let Err(err) = peer.on_answer_created(&s.to_owned()) {
gst_element_error!(
peer.bin,
gst::LibraryError::Failed,
("Failed to send SDP answer: {:?}", err)
);
}
});
peer.0
.webrtcbin
.emit("create-answer", &[&None::<gst::Structure>, &promise])
.expect("Unable to create answer");
});
Ok(())
} else {
bail!("Sdp type is not \"answer\" but \"{}\"", type_)
}
}
// Handle incoming ICE candidates from the peer by passing them to webrtcbin
fn handle_ice(&self, sdp_mline_index: u32, candidate: &str) -> Result<(), anyhow::Error> {
info!(
"Received remote ice-candidate {} {}",
sdp_mline_index, candidate
);
self.webrtcbin
.emit("add-ice-candidate", &[&sdp_mline_index, &candidate])?;
Ok(())
}
// Asynchronously send ICE candidates to the peer via the WebSocket connection as a JSON
// message
fn on_ice_candidate(&self, mlineindex: u32, candidate: String) -> Result<(), anyhow::Error> {
let transaction = transaction_id();
info!("Sending ICE {} {}", mlineindex, &candidate);
let msg = WsMessage::Text(
json!({
"janus": "trickle",
"transaction": transaction,
"session_id": self.handle.session_id,
"handle_id": self.handle.id,
"candidate": {
"candidate": candidate,
"sdpMLineIndex": mlineindex
},
})
.to_string(),
);
self.send_msg_tx
.lock()
.expect("Invalid message sender")
.unbounded_send(msg)
.with_context(|| "Failed to send ICE candidate".to_string())?;
Ok(())
}
}
// At least shut down the bin here if it didn't happen so far
impl Drop for PeerInner {
fn drop(&mut self) {
let _ = self.bin.set_state(gst::State::Null);
}
}
type WsStream =
std::pin::Pin<Box<dyn Stream<Item = Result<WsMessage, tungstenite::error::Error>> + Send>>;
type WsSink = std::pin::Pin<Box<dyn Sink<WsMessage, Error = tungstenite::error::Error> + Send>>;
pub struct JanusGateway {
ws_stream: Option<WsStream>,
ws_sink: Option<WsSink>,
handle: ConnectionHandle,
peer: Mutex<Peer>,
send_ws_msg_rx: Option<mpsc::UnboundedReceiver<WsMessage>>,
}
impl JanusGateway {
pub async fn new(pipeline: gst::Bin) -> Result<Self, anyhow::Error> {
let args = Args::from_args();
let request = Request::builder()
.uri(&args.server)
.header("Sec-WebSocket-Protocol", "janus-protocol")
.body(())?;
let (mut ws, _) = connect_async(request).await?;
let transaction = transaction_id();
let msg = WsMessage::Text(
json!({
"janus": "create",
"transaction": transaction,
})
.to_string(),
);
ws.send(msg).await?;
let msg = ws
.next()
.await
.ok_or_else(|| anyhow!("didn't receive anything"))??;
let payload = msg.to_text()?;
let json_msg: JsonReply = serde_json::from_str(payload)?;
assert_eq!(json_msg.base.janus, "success");
assert_eq!(json_msg.base.transaction, Some(transaction));
let session_id = json_msg.data.expect("no session id").id;
let transaction = transaction_id();
let msg = WsMessage::Text(
json!({
"janus": "attach",
"transaction": transaction,
"plugin": "janus.plugin.videoroom",
"session_id": session_id,
})
.to_string(),
);
ws.send(msg).await?;
let msg = ws
.next()
.await
.ok_or_else(|| anyhow!("didn't receive anything"))??;
let payload = msg.to_text()?;
let json_msg: JsonReply = serde_json::from_str(payload)?;
assert_eq!(json_msg.base.janus, "success");
assert_eq!(json_msg.base.transaction, Some(transaction));
let handle = json_msg.data.expect("no session id").id;
let transaction = transaction_id();
let msg = WsMessage::Text(
json!({
"janus": "message",
"transaction": transaction,
"session_id": session_id,
"handle_id": handle,
"body": {
"request": "join",
"ptype": "publisher",
"room": args.room_id,
"id": args.feed_id,
},
})
.to_string(),
);
ws.send(msg).await?;
let webrtcbin = pipeline
.get_by_name("webrtcbin")
.expect("can't find webrtcbin");
if let Ok(transceiver) = webrtcbin.emit("get-transceiver", &[&0.to_value()]) {
if let Some(t) = transceiver {
if let Ok(obj) = t.get::<glib::Object>() {
obj.expect("Invalid transceiver")
.set_property("do-nack", &true.to_value())?;
}
}
}
let (send_ws_msg_tx, send_ws_msg_rx) = mpsc::unbounded::<WsMessage>();
let connection_handle = ConnectionHandle {
id: handle,
session_id,
};
let peer = Peer(Arc::new(PeerInner {
handle: connection_handle,
bin: pipeline,
webrtcbin,
send_msg_tx: Arc::new(Mutex::new(send_ws_msg_tx)),
}));
// Connect to on-negotiation-needed to handle sending an Offer
let peer_clone = peer.downgrade();
peer.webrtcbin
.connect("on-negotiation-needed", false, move |_| {
let peer = upgrade_weak!(peer_clone, None);
if let Err(err) = peer.on_negotiation_needed() {
gst_element_error!(
peer.bin,
gst::LibraryError::Failed,
("Failed to negotiate: {:?}", err)
);
}
None
})?;
// Whenever there is a new ICE candidate, send it to the peer
let peer_clone = peer.downgrade();
peer.webrtcbin
.connect("on-ice-candidate", false, move |values| {
let mlineindex = values[1]
.get::<u32>()
.expect("Invalid argument")
.expect("Invalid type");
let candidate = values[2]
.get::<String>()
.expect("Invalid argument")
.expect("Invalid type");
let peer = upgrade_weak!(peer_clone, None);
if let Err(err) = peer.on_ice_candidate(mlineindex, candidate) {
gst_element_error!(
peer.bin,
gst::LibraryError::Failed,
("Failed to send ICE candidate: {:?}", err)
);
}
None
})?;
// Split the websocket into the Sink and Stream
let (ws_sink, ws_stream) = ws.split();
Ok(Self {
ws_stream: Some(ws_stream.boxed()),
ws_sink: Some(Box::pin(ws_sink)),
handle: connection_handle,
peer: Mutex::new(peer),
send_ws_msg_rx: Some(send_ws_msg_rx),
})
}
pub async fn run(&mut self) -> Result<(), anyhow::Error> {
if let Some(ws_stream) = self.ws_stream.take() {
// Fuse the Stream, required for the select macro
let mut ws_stream = ws_stream.fuse();
// Channel for outgoing WebSocket messages from other threads
let send_ws_msg_rx = self
.send_ws_msg_rx
.take()
.expect("Invalid message receiver");
let mut send_ws_msg_rx = send_ws_msg_rx.fuse();
let timer = glib::interval_stream(10_000);
let mut timer_fuse = timer.fuse();
let mut sink = self.ws_sink.take().expect("Invalid websocket sink");
loop {
let ws_msg = futures::select! {
// Handle the WebSocket messages here
ws_msg = ws_stream.select_next_some() => {
match ws_msg? {
WsMessage::Close(_) => {
info!("peer disconnected");
break
},
WsMessage::Ping(data) => Some(WsMessage::Pong(data)),
WsMessage::Pong(_) => None,
WsMessage::Binary(_) => None,
WsMessage::Text(text) => {
if let Err(err) = self.handle_websocket_message(&text) {
error!("Failed to parse message: {} ... error: {}", &text, err);
}
None
},
}
},
// Handle WebSocket messages we created asynchronously
// to send them out now
ws_msg = send_ws_msg_rx.select_next_some() => Some(ws_msg),
// Handle keepalive ticks, fired every 10 seconds
ws_msg = timer_fuse.select_next_some() => {
let transaction = transaction_id();
Some(WsMessage::Text(
json!({
"janus": "keepalive",
"transaction": transaction,
"handle_id": self.handle.id,
"session_id": self.handle.session_id,
}).to_string(),
))
},
// Once we're done, break the loop and return
complete => break,
};
// If there's a message to send out, do so now
if let Some(ws_msg) = ws_msg {
sink.send(ws_msg).await?;
}
}
}
Ok(())
}
fn handle_jsep(&self, jsep: &JsepHolder) -> Result<(), anyhow::Error> {
if let Some(sdp) = &jsep.sdp {
assert_eq!(jsep.type_, "answer");
let peer = self.peer.lock().expect("Invalid peer");
return peer.handle_sdp(&jsep.type_, &sdp);
} else if let Some(ice) = &jsep.ice {
let peer = self.peer.lock().expect("Invalid peer");
return peer.handle_ice(ice.sdp_mline_index, &ice.candidate);
}
Ok(())
}
// Handle WebSocket messages, both our own as well as WebSocket protocol messages
fn handle_websocket_message(&self, msg: &str) -> Result<(), anyhow::Error> {
trace!("Incoming raw message: {}", msg);
let json_msg: JsonReply = serde_json::from_str(msg)?;
let payload_type = &json_msg.base.janus;
if payload_type == "ack" {
trace!(
"Ack transaction {:#?}, sessionId {:#?}",
json_msg.base.transaction,
json_msg.base.session_id
);
} else {
debug!("Incoming JSON WebSocket message: {:#?}", json_msg);
}
if payload_type == "event" {
if let Some(_plugin_data) = json_msg.plugin_data {
if let Some(jsep) = json_msg.jsep {
return self.handle_jsep(&jsep);
}
}
}
Ok(())
}
}

14
src/macros.rs Normal file
View file

@ -0,0 +1,14 @@
// Macro for upgrading a weak reference or returning the given value
//
// This works for glib/gtk objects as well as anything else providing an upgrade method
macro_rules! upgrade_weak {
($x:ident, $r:expr) => {{
match $x.upgrade() {
Some(o) => o,
None => return $r,
}
}};
($x:ident) => {
upgrade_weak!($x, ())
};
}

27
src/main.rs Normal file
View file

@ -0,0 +1,27 @@
#![recursion_limit = "256"]
#[macro_use]
mod macros;
mod app;
mod janus;
mod pipeline;
mod settings;
mod utils;
use crate::app::App;
#[macro_use]
extern crate log;
pub const APPLICATION_NAME: &str = "com.igalia.gstwpe.broadcast.demo";
async fn async_main() -> Result<(), anyhow::Error> {
gst::init()?;
let app = App::new()?;
app.run().await
}
fn main() -> Result<(), anyhow::Error> {
env_logger::init();
let main_context = glib::MainContext::default();
main_context.block_on(async_main())
}

213
src/pipeline.rs Normal file
View file

@ -0,0 +1,213 @@
use crate::settings::VideoResolution;
use crate::utils;
use gst::{self, prelude::*};
use std::error;
use std::ops;
use std::rc::{Rc, Weak};
// Our refcounted pipeline struct for containing all the media state we have to carry around.
#[derive(Clone)]
pub struct Pipeline(Rc<PipelineInner>);
// Deref into the contained struct to make usage a bit more ergonomic
impl ops::Deref for Pipeline {
type Target = PipelineInner;
fn deref(&self) -> &PipelineInner {
&*self.0
}
}
pub struct PipelineInner {
pub pipeline: gst::Pipeline,
}
// Weak reference to our pipeline struct
//
// Weak references are important to prevent reference cycles. Reference cycles are cases where
// struct A references directly or indirectly struct B, and struct B references struct A again
// while both are using reference counting.
pub struct PipelineWeak(Weak<PipelineInner>);
impl PipelineWeak {
pub fn upgrade(&self) -> Option<Pipeline> {
self.0.upgrade().map(Pipeline)
}
}
impl Pipeline {
pub fn new() -> Result<Self, Box<dyn error::Error>> {
let settings = utils::load_settings();
let (width, height) = match settings.video_resolution {
VideoResolution::V480P => (640, 480),
VideoResolution::V720P => (1280, 720),
VideoResolution::V1080P => (1920, 1080),
};
let pipeline = gst::parse_launch(&format!(
"webrtcbin name=webrtcbin stun-server=stun://stun2.l.google.com:19302 \
glvideomixerelement name=mixer sink_1::zorder=0 sink_1::height={height} sink_1::width={width} \
! tee name=video-tee ! queue ! gtkglsink enable-last-sample=0 name=sink qos=0 \
wpesrc location=http://127.0.0.1:3000 name=wpesrc draw-background=0 \
! capsfilter name=wpecaps caps=\"video/x-raw(memory:GLMemory),width={width},height={height},pixel-aspect-ratio=(fraction)1/1\" ! glcolorconvert ! queue ! mixer. \
v4l2src name=videosrc ! capsfilter name=camcaps caps=\"image/jpeg,width={width},height={height},framerate=30/1\" ! queue ! jpegparse ! queue ! jpegdec ! videoconvert ! queue ! glupload ! glcolorconvert
! queue ! mixer. \
", width=width, height=height)
)?;
// Upcast to a gst::Pipeline as the above function could've also returned an arbitrary
// gst::Element if a different string was passed
let pipeline = pipeline
.downcast::<gst::Pipeline>()
.expect("Couldn't downcast pipeline");
// Request that the pipeline forwards us all messages, even those that it would otherwise
// aggregate first
pipeline.set_property_message_forward(true);
let pipeline = Pipeline(Rc::new(PipelineInner { pipeline }));
// Install a message handler on the pipeline's bus to catch errors
let bus = pipeline.pipeline.get_bus().expect("Pipeline had no bus");
// GStreamer is thread-safe and it is possible to attach bus watches from any thread, which
// are then nonetheless called from the main thread. So by default, add_watch() requires
// the passed closure to be Send. We want to pass non-Send values into the closure though.
//
// As we are on the main thread and the closure will be called on the main thread, this
// is actually perfectly fine and safe to do and we can use add_watch_local().
// add_watch_local() would panic if we were not calling it from the main thread.
let pipeline_weak = pipeline.downgrade();
bus.add_watch_local(move |_bus, msg| {
let pipeline = upgrade_weak!(pipeline_weak, glib::Continue(false));
pipeline.on_pipeline_message(msg);
glib::Continue(true)
})
.expect("Unable to add bus watch");
Ok(pipeline)
}
// Downgrade to a weak reference
pub fn downgrade(&self) -> PipelineWeak {
PipelineWeak(Rc::downgrade(&self.0))
}
pub fn prepare(&self) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
let settings = utils::load_settings();
let webrtc_codec = settings.webrtc_codec_params();
let bin_description = &format!(
"queue name=webrtc-vqueue ! gldownload ! videoconvert ! {encoder} ! {payloader} ! queue ! capsfilter name=webrtc-vsink caps=\"application/x-rtp,media=video,encoding-name={encoding_name},payload=96\"",
encoder=webrtc_codec.encoder, payloader=webrtc_codec.payloader,
encoding_name=webrtc_codec.encoding_name
);
let bin = gst::parse_bin_from_description(bin_description, false).unwrap();
bin.set_name("webrtc-vbin").unwrap();
let video_queue = bin
.get_by_name("webrtc-vqueue")
.expect("No webrtc-vqueue found");
let video_tee = self
.pipeline
.get_by_name("video-tee")
.expect("No video-tee found");
self.pipeline
.add(&bin)
.expect("Failed to add recording bin");
let srcpad = video_tee
.get_request_pad("src_%u")
.expect("Failed to request new pad from tee");
let sinkpad = video_queue
.get_static_pad("sink")
.expect("Failed to get sink pad from recording bin");
if let Ok(video_ghost_pad) = gst::GhostPad::new(Some("video_sink"), &sinkpad) {
bin.add_pad(&video_ghost_pad).unwrap();
srcpad.link(&video_ghost_pad).unwrap();
}
let webrtcbin = self.pipeline.get_by_name("webrtcbin").unwrap();
let sinkpad2 = webrtcbin.get_request_pad("sink_%u").unwrap();
let vsink = bin
.get_by_name("webrtc-vsink")
.expect("No webrtc-vqueue found");
let srcpad = vsink.get_static_pad("src").unwrap();
if let Ok(webrtc_ghost_pad) = gst::GhostPad::new(Some("webrtc_video_src"), &srcpad) {
bin.add_pad(&webrtc_ghost_pad).unwrap();
webrtc_ghost_pad.link(&sinkpad2).unwrap();
}
self.pipeline.set_state(gst::State::Ready)
}
pub fn start(&self) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
// This has no effect if called multiple times
self.pipeline.set_state(gst::State::Playing)
}
pub fn stop(&self) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
// This has no effect if called multiple times
self.pipeline.set_state(gst::State::Null)
}
// Here we handle all message we get from the GStreamer pipeline. These are notifications sent
// from GStreamer, including errors that happend at runtime.
//
// This is always called from the main application thread by construction.
fn on_pipeline_message(&self, msg: &gst::MessageRef) {
use gst::MessageView;
// A message can contain various kinds of information but
// here we are only interested in errors so far
match msg.view() {
MessageView::Error(err) => {
panic!(
"Error from {:?}: {} ({:?})",
err.get_src().map(|s| s.get_path_string()),
err.get_error(),
err.get_debug()
);
}
MessageView::Application(msg) => match msg.get_structure() {
// Here we can send ourselves messages from any thread and show them to the user in
// the UI in case something goes wrong
Some(s) if s.get_name() == "warning" => {
let text = s
.get::<&str>("text")
.expect("Warning message without text")
.unwrap();
panic!("{}", text);
}
_ => (),
},
MessageView::StateChanged(state_changed) => {
if let Some(element) = msg.get_src() {
if element == self.pipeline {
let bin_ref = element.downcast_ref::<gst::Bin>().unwrap();
let filename = format!(
"gst-wpe-broadcast-demo-{:#?}_to_{:#?}",
state_changed.get_old(),
state_changed.get_current()
);
bin_ref.debug_to_dot_file_with_ts(gst::DebugGraphDetails::all(), filename);
}
}
}
MessageView::AsyncDone(_) => {
if let Some(element) = msg.get_src() {
let bin_ref = element.downcast_ref::<gst::Bin>().unwrap();
bin_ref.debug_to_dot_file_with_ts(
gst::DebugGraphDetails::all(),
"gst-wpe-broadcast-demo-async-done",
);
}
}
_ => (),
};
}
}

79
src/settings.rs Normal file
View file

@ -0,0 +1,79 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Deserialize)]
pub enum VideoResolution {
V480P,
V720P,
V1080P,
}
impl Default for VideoResolution {
fn default() -> Self {
VideoResolution::V720P
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Serialize, Deserialize)]
pub enum WebRTCCodec {
VP8,
VP9,
H264,
}
impl Default for WebRTCCodec {
fn default() -> Self {
WebRTCCodec::VP8
}
}
#[derive(Debug)]
pub struct VideoParameter {
pub encoder: &'static str,
pub encoding_name: &'static str,
pub payloader: &'static str,
}
const VP8_PARAM: VideoParameter = VideoParameter {
encoder: "vp8enc target-bitrate=400000 threads=4 overshoot=25 undershoot=100 deadline=33000 keyframe-max-dist=1",
encoding_name: "VP8",
payloader: "rtpvp8pay picture-id-mode=2"
};
const VP9_PARAM: VideoParameter = VideoParameter {
encoder: "vp9enc target-bitrate=128000 undershoot=100 deadline=33000 keyframe-max-dist=1",
encoding_name: "VP9",
payloader: "rtpvp9pay picture-id-mode=2",
};
const H264_PARAM: VideoParameter = VideoParameter {
//encoder: "x264enc tune=zerolatency",
encoder: "vaapih264enc",
encoding_name: "H264",
payloader: "rtph264pay",
};
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Settings {
pub video_resolution: VideoResolution,
pub webrtc_codec: WebRTCCodec,
}
impl Default for Settings {
fn default() -> Settings {
Settings {
//h264_encoder: "video/x-raw,format=NV12 ! vaapih264enc bitrate=20000 keyframe-period=60 ! video/x-h264,profile=main".to_string(),
video_resolution: VideoResolution::default(),
webrtc_codec: WebRTCCodec::default(),
}
}
}
impl Settings {
pub fn webrtc_codec_params(&self) -> VideoParameter {
match self.webrtc_codec {
WebRTCCodec::VP8 => VP8_PARAM,
WebRTCCodec::VP9 => VP9_PARAM,
WebRTCCodec::H264 => H264_PARAM,
}
}
}

27
src/utils.rs Normal file
View file

@ -0,0 +1,27 @@
use std::path::PathBuf;
use crate::settings::Settings;
use crate::APPLICATION_NAME;
// Get the default path for the settings file
pub fn get_settings_file_path() -> PathBuf {
let mut path = glib::get_user_config_dir().unwrap_or_else(|| PathBuf::from("."));
path.push(APPLICATION_NAME);
path.push("settings.toml");
path
}
// Load the current settings
pub fn load_settings() -> Settings {
let s = get_settings_file_path();
if s.exists() && s.is_file() {
match serde_any::from_file::<Settings, _>(&s) {
Ok(s) => s,
Err(e) => {
panic!("Error while opening '{}': {}", s.display(), e);
}
}
} else {
Settings::default()
}
}

1
wpe-graphics-overlays Submodule

@ -0,0 +1 @@
Subproject commit 1e23f781adef05d6d2f291d9bb67c28f9bb9b2f1