Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
slashblog committed Jul 24, 2024
1 parent 6357aa1 commit a9ab052
Show file tree
Hide file tree
Showing 11 changed files with 890 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@
# Go workspace file
go.work
go.work.sum

bin/**
build/**
config/config.yaml


30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# builds service executable
.PHONY: build
build:
go build -x -v -o ./bin/gowebrtc pkg/main.go

clean:
rm -rvf bin build

debian: build
mkdir -p build/debian/gowebrtc/DEBIAN \
build/debian/gowebrtc/usr/local/bin \
build/debian/gowebrtc/etc/gowebrtc/ \
build/debian/gowebrtc/lib/systemd/system \
build/debian/gowebrtc/usr/share/gowebrtc
cp -v config/config.yaml.sample build/debian/gowebrtc/etc/gowebrtc
cp -v bin/gowebrtc build/debian/gowebrtc/usr/local/bin
cp -v gowebrtc.service build/debian/gowebrtc/lib/systemd/system
cp -av html build/debian/gowebrtc/usr/share/gowebrtc
cp -v debian.control build/debian/gowebrtc/DEBIAN/control
dpkg-deb --build build/debian/gowebrtc

debian-install: debian
sudo dpkg -i build/debian/gowebrtc.deb

debian-uninstall:
sudo dpkg -r gowebrtc

.PHONY: test
test: build
@./bin/gowebrtc server
8 changes: 8 additions & 0 deletions config/config.yaml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
port: 8080
url: /stream
image_width: 640
image_height: 480
framerate: 30
log_file: /var/log/gowebrtc.log
audio_device: alsasrc device=plughw:CARD=I930,DEV=0
video_device: autovideosrc
6 changes: 6 additions & 0 deletions debian.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Package: gowebrtc
Version: 0.1.0
Maintainer: Neeraj Jakhar
Architecture: all
Description: Service to setup webrtc streaming
Depends: libgstreamer1.0-dev, libgstreamer-plugins-base1.0-dev, gstreamer1.0-plugins-good, gstreamer1.0-alsa
63 changes: 63 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module github.com/homebackend/go-webrtc

go 1.22.5

require (
github.com/akamensky/argparse v1.4.0
github.com/gin-gonic/gin v1.10.0
github.com/go-gst/go-gst v1.1.0
github.com/homebackend/go-homebackend-common v0.0.0-20231117105846-e72d04db4335
github.com/pion/webrtc/v3 v3.2.49
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-gst/go-glib v1.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pion/datachannel v1.5.8 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/ice/v2 v2.3.29 // indirect
github.com/pion/interceptor v0.1.29 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.12 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.14 // indirect
github.com/pion/rtp v1.8.7 // indirect
github.com/pion/sctp v1.8.19 // indirect
github.com/pion/sdp/v3 v3.0.9 // indirect
github.com/pion/srtp/v2 v2.0.20 // indirect
github.com/pion/stun v0.6.1 // indirect
github.com/pion/transport/v2 v2.2.4 // indirect
github.com/pion/turn/v2 v2.1.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
206 changes: 206 additions & 0 deletions go.sum

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions gowebrtc.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Service to setup webrtc streaming
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/gowebrtc server
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
30 changes: 30 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>WebRTC</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>

</head>
<body>
<button id="start-session" onclick="window.startSession()"> Start Session </button>
<button id="stop-session" onclick="window.stopSession()" disabled> Stop Session </button>
<button id="reload-session" onclick="location.reload()" disabled> Reload Page </button>
<br />

<br />

Video<br />
<div id="remoteVideos">
<video class="video-player" id="video-player" autoplay playsinline></video>
</div> <br />

Logs<br />
<div id="div"></div>


</body>
<script src='main.js'></script>
</html>
4 changes: 4 additions & 0 deletions html/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
textarea {
width: 500px;
min-height: 75px;
}
77 changes: 77 additions & 0 deletions html/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
remoteStream = new MediaStream()
document.getElementById('video-player').srcObject = remoteStream

let pc = new RTCPeerConnection({
iceServers: [{
urls: 'stun:stun.l.google.com:19302'
}]
})
let log = msg => {
document.getElementById('div').innerHTML += msg + '<br>'
}

pc.ontrack = function(event) {
event.streams[0].getTracks().forEach((track) => {
remoteStream.addTrack(track)
})
}

pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
pc.onicecandidate = event => {
if (event.candidate) {
console.log('Updated candidate: ' + btoa(JSON.stringify(pc.localDescription)));
}
}

// Offer to receive 1 audio, and 1 video track
pc.addTransceiver('video', {
'direction': 'sendrecv'
})
pc.addTransceiver('audio', {
'direction': 'sendrecv'
})

pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)

window.startSession = () => {
document.getElementById('start-session').disabled = true;
let localSessionDescription = btoa(JSON.stringify(pc.localDescription));
console.log('Local: ' + localSessionDescription);

fetch('/stream', {
method: 'POST',
body: JSON.stringify({"sdp": localSessionDescription}),
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
if (!response.ok) {
alert('Failed to call /stream api');
throw new Error('Error calling api');
}

return response.json();
}).then(data => {
console.log('Remote: ' + data);
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(data.sdp))));
document.getElementById('stop-session').disabled = false;
}).catch(error => {
alert('Error' + error);
});
}

window.stopSession = () => {
document.getElementById('stop-session').disabled = true;
document.getElementById('reload-session').disabled = false;
fetch('/stream', {
method: 'DELETE',
}).then(response => {
if (!response.ok) {
alert('Failed to call /stream api');
throw new Error('Error calling api');
}
}).catch(error => {
alert('Error' + error);
});
}

Loading

0 comments on commit a9ab052

Please sign in to comment.