-
Notifications
You must be signed in to change notification settings - Fork 637
WebRTC Publish & Play JavaScript SDK
Attention: We have migrated our documentation to our new platform, Ant Media Resources. Please follow this link for the latest and up-to-date documentation.
NOTE: We have updated our documentation. This page is outdated. You can access updated version from the sidebar menu.
Ant Media Server provides WebSocket interface in publishing and playing WebRTC streams. In this document, we will show both how to publish and play WebRTC streams by using JavaScript SDK.
Let's see how to make it step by step
- Load the below scripts in head element of the html file
<head>
...
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="js/webrtc_adaptor.js" ></script>
...
</head>
- Create local video element somewhere in the body tag
<video id="localVideo" autoplay muted width="480"></video>
- Initialize the
WebRTCAdaptor
object in script tag
var pc_config = null;
var sdpConstraints = {
OfferToReceiveAudio : false,
OfferToReceiveVideo : false
};
var mediaConstraints = {
video : true,
audio : true
};
var webRTCAdaptor = new WebRTCAdaptor({
websocket_url : "ws://" + location.hostname + ":"+location.port+"/WebRTCAppEE",
mediaConstraints : mediaConstraints,
peerconnection_config : pc_config,
sdp_constraints : sdpConstraints,
localVideoId : "localVideo",
callback : function(info) {
if (info == "initialized")
{
console.log("initialized");
}
else if (info == "publish_started")
{
//stream is being published
console.log("publish started");
}
else if (info == "publish_finished")
{
//stream is finished
console.log("publish finished");
}
else if (info == "screen_share_extension_available")
{
//screen share extension is avaiable
console.log("screen share extension available");
}
else if (info == "screen_share_stopped")
{
//"Stop Sharing" is clicked in chrome screen share dialog
console.log("screen share stopped");
}
},
callbackError : function(error) {
//some of the possible errors, NotFoundError, SecurityError,PermissionDeniedError
console.log("error callback: " + error);
alert(error);
}
});
- Call
publish(streamName)
to Start Publishing
In order to publish WebRTC stream to Ant Media Server, WebRTCAdaptor's publish(streamName)
function should be called.
You can choose the call this function in success callback function when the info parameter is having value "initialized"
if (info == "initialized")
{
// it is called with this parameter when it connects to
// Ant Media Server and everything is ok
console.log("initialized");
webRTCAdaptor.publish("stream1");
}
- Call
stop()
to Stop Publishing
You may want to stop publishing anytime by calling stop function of WebRTCAdaptor
webRTCAdaptor.stop()
Please take a look at the WebRTCAppEE/index.html file in order to see How JavaScript SDK can be used for publishing a stream
- Load the below scripts in head element of the html file
<head>
...
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="js/webrtc_adaptor.js" ></script>
...
</head>
- Create remote video element somewhere in the body tag
<video id="remoteVideo" autoplay controls></video>
- Initialize the
WebRTCAdaptor
object like below in script tag
var pc_config = null;
var sdpConstraints = {
OfferToReceiveAudio : true,
OfferToReceiveVideo : true
};
var mediaConstraints = {
video : true,
audio : true
};
var webRTCAdaptor = new WebRTCAdaptor({
websocket_url : "ws://" + location.hostname + ":"+ location.port +"/WebRTCAppEE",
mediaConstraints : mediaConstraints,
peerconnection_config : pc_config,
sdp_constraints : sdpConstraints,
remoteVideoId : "remoteVideo",
isPlayMode: true,
callback : function(info) {
if (info == "initialized") {
console.log("initialized");
} else if (info == "play_started") {
//play_started
console.log("play started");
} else if (info == "play_finished") {
// play finishedthe stream
console.log("play finished");
}
},
callbackError : function(error) {
//some of the possible errors, NotFoundError, SecurityError,PermissionDeniedError
console.log("error callback: " + error);
alert(error);
}
});
- Call
play(streamName)
to Start Playing
In order to play WebRTC stream to Ant Media Server, WebRTCAdaptor's play(streamName)
function should be called.
You can choose the call this function in success callback function when the info parameter is having value "initialized"
if (info == "initialized")
{
// it is called with this parameter when it connects to
// Ant Media Server and everything is ok
console.log("initialized");
webRTCAdaptor.play("stream1");
}
- Call
stop()
to Stop Playing
You may want to stop play anytime by calling stop function of WebRTCAdaptor
webRTCAdaptor.stop()
Take a look at JavaScript Error Callbacks to handle callbacks gracefully
Please take a look at the WebRTCAppEE/player.html file in order to see How JavaScript SDK can be used for playing a stream
- Introduction
- Quick Start
- Installation
- Publishing Live Streams
- Playing Live Streams
- Conference Call
- Peer to Peer Call
- Adaptive Bitrate(Multi-Bitrate) Streaming
- Data Channel
- Video on Demand Streaming
- Simulcasting to Social Media Channels
- Clustering & Scaling
- Monitor Ant Media Servers with Apache Kafka and Grafana
- WebRTC SDKs
- Security
- Integration with your Project
- Advanced
- WebRTC Load Testing
- TURN Servers
- AWS Wavelength Deployment
- Multi-Tenancy Support
- Monitor Ant Media Server with Datadog
- Clustering in Alibaba
- Playlist
- Kubernetes
- Time based One Time Password
- Kubernetes Autoscaling
- Kubernetes Ingress
- How to Install Ant Media Server on EKS
- Release Tests
- Spaceport Volumetric Video
- WebRTC Viewers Info
- Webhook Authentication for Publishing Streams
- Recording Streams
- How to Update Ant Media Server with Cloudformation
- How to Install Ant Media Server on GKE
- Ant Media Server on Docker Swarm
- Developer Quick Start
- Recording HLS, MP4 and how to recover
- Re-streaming update
- Git Branching
- UML Diagrams