Sample Code - Straas/Straas-web-document GitHub Wiki
We provide various sample codes which categorized by programming language.
<?php
$ch = curl_init();
$upload_url = 'UPLOAD URL HERE';
curl_setopt($ch, CURLOPT_URL, $upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$path = 'FILE PATH HERE';
$file = fopen($path, 'rb');
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
$headers = array();
$headers[] = "Content-Type: video/mp4";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
echo $result;
}
curl_close ($ch);
<html>
<head>
<title>StraaS WebRTC Broadcasting Sample</title>
<script type="text/javascript" src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script type="text/javascript" src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
const streamKey = 'replace_with_your_stream_key_here';
const peerConnConfig = {
iceServers: [
// replace with TURN server credential info returned from CMS API
{
urls: ['turn:1.2.3.4?transport=tcp'],
username: '1509677133:foobar',
credential: 'foobarcredential'
}
],
iceTransportPolicy: 'relay',
};
const gumConstraints = {
audio: true,
video: {
width: { min: 1280 },
height: { min: 720 },
}
};
const videoBitrateKbps = 1000;
let peerConn = new RTCPeerConnection(
peerConnConfig,
{
optional: [{'DtlsSrtpKeyAgreement': false}]
}
);
let iceCandidates = '';
let offerSDP;
// RTCPeerConnection event handlers
peerConn.onicecandidate = function (event) {
if (event.candidate) {
iceCandidates += ('a=' + event.candidate.candidate + '\r\n');
return;
}
// ICE candidates gathering has finished
let remoteCandidates;
callStartBroadcastingAPI()
.then((response) => {
let remoteDesc = new RTCSessionDescription({type: 'answer', sdp: response.data.sdp});
remoteCandidates = response.data.remoteCandidates;
return peerConn.setRemoteDescription(remoteDesc);
})
.then(() => {
for (let candidate of remoteCandidates) {
peerConn.addIceCandidate(new RTCIceCandidate({candidate: candidate}));
}
});
};
peerConn.oniceconnectionstatechange = function (event) {
if (!peerConn) {
return;
}
if (peerConn.iceConnectionState === 'failed' ||
peerConn.iceConnectionState === 'closed') {
// peer connection encountered some errors.
terminate();
}
};
// Button click handlers
function broadcast() {
return navigator.mediaDevices.getUserMedia(gumConstraints)
.then((mediaStream) => {
peerConn.addStream(mediaStream);
return peerConn.createOffer();
})
.then((offer) => {
offer.sdp = setVideoBitrate(offer.sdp, videoBitrateKbps);
offerSDP = offer.sdp;
return peerConn.setLocalDescription(offer);
});
}
function terminate() {
if (!peerConn) {
return;
}
for (const stream of peerConn.getLocalStreams()) {
for (const track of stream.getTracks()) {
track.stop();
}
}
peerConn.close();
peerConn = null;
return callStopBroadcastingAPI();
}
// SDP modification
function setVideoBitrate(sdp, bitrateKbps) {
return sdp.replace(/a=mid:video\r\n/g, 'a=mid:video\r\nb=AS:' + bitrateKbps + '\r\n');
}
// StraaS WebRTC Broadcasting APIs
function callStartBroadcastingAPI() {
const config = {
method: 'post',
url: 'https://rtc-middleware.straas.io/broadcasts/webrtc',
headers: {'Content-Type': 'application/json'},
data: {
stream_key: streamKey,
sdp: offerSDP + iceCandidates,
}
};
return axios(config);
}
function callStopBroadcastingAPI() {
const config = {
method: 'delete',
url: 'https://rtc-middleware.straas.io/broadcasts/webrtc',
headers: {'Content-Type': 'application/json'},
data: {
stream_key: streamKey,
}
};
return axios(config);
}
</script>
</head>
<body>
<div>
<button onclick="broadcast()">Broadcast</button>
<button onclick="terminate()">Terminate</button>
</div>
</body>
</html>