GithubHelp home page GithubHelp logo

media-server-client-js's Introduction

Medooze Media Server Client

This client libray alllows to easily connect to the Medooze Media Server with a simple API.

Features

  • Automatically sets Unified Plan SDP semantics for chrome
  • Automatically creates a new server side Transport object when a new managed PeerConnection is created by the client
  • Performs SDP offer/answer when a new track is added or removed in either client or server side
  • Synchronizes stream and tracks in client and server, creating and deleting them as appropiate
  • Perform SDP mangling for enabling simulcast in Chrome

Demo

You can find a demo in the demo directory. To run it just do:

npm i
npm run-script dist
cd demo
npm i
node index.js <your ip address>

Usage

In order to connect with the server you will need to connect via websocket and open a new transaction manager:

//Connect with websocket
const ws = new WebSocket(url);
	
//Create transaction manager 
const tm = new TransactionManager(ws);
	
//Create managed peer connection
const client = new MediaServerClient(tm);
	
//Start on open
ws.onopen = async ()=>{
  //Create new managed pc 
  pc = await client.createManagedPeerConnection();
};

Server side you will have to be listening for new clients by using the PeerConnectionserver:

const connection = request.accept(protocol);

//Create new transaction manager
const tm = new TransactionManager(connection);
   		
//Create new managed peerconnection server for this
const mngr = endpoint.createPeerConnectionServer(tm,Capabilities);
   
//LIsten for remotelly created peer connections
mngr.on("transport",(transport)=>{
 //Here you will get the transport associated to the PeerConnection created in client.createManagedPeerConnection()
});

Once you have both the managed peerconnection client and the server transport created you can add tracks in either side, for example if you do it in the browser:

//Browser
const sender = await pc.addTrack(track,stream);

Will trigger the incomingtrack event on the server transport.

//Server 
transport.on("incomingtrack",(track,stream)=>{});
  

And vice versa, a track added in the transport

//Crete empty stream
transport.createOutgoingStream(outgoingStreamId);
			
//Create ougoing track
const outgoing = outgoingStream.createTrack("video");

Will trigger the ontrack event on the managed peer connection on the browser:

//Event handler
pc.ontrack = (event) => console.log(event);

Similarily, removing the tracks on browser or stopping the tracks on the server, will trigger the appropiate event on the other side.

API

MediaServerClient

Factory object used to synchronze with the server.

constructor( transactionManager )

Creates a new client object.

  • transactionManager A transaction manager connected with the server (see TransactionManager for more info).

Promise createManagedPeerConnection(options)

Creates a new managed peer connection client object.

Addintionaly the following options can be used:

  • options.forceSDPMunging Do always SDP munging for setting up simulcast on Chrome
  • options.strictW3C Force strict W3C api compatibility (disables hacks)

This will create a new Transport object on the server.

PeerConnectionClient

Promise< RTCSender > addTrack(track,stream,params?)

Adds a new track to the client and creates a new IncomingStreamTrack (and IncomingStream if needed) server side.

  • track The track to send
  • stream The stream to send
  • params Send params (Optional)
    • codecs An array of desired codecs (eg. ["h264]), this will remove the non present codecs from the local sdp offer (Optional)
    • encodings An array of RTCRtpEncodingParameters which can be used for enabling simulcast. If the encodings array is provided it will be set accordingly on the transcevier (on the sender.setParameters if on Firefox) and if it is not supported, it will mangle the SDP for chrome adding the required ssrcs to enable simulcast (Optional)

void removeTrack(sender)

Stops sending the track, will stop the IncomingStreamTrack server side also.

  • sender The RTCSender returned on the addTrack

Promise< RTCStatsReport > getStats(selector)

Proxy for RTCPeerConnection getStats.

  • selector See RTCPeerConnection getStats

void stop()

Closes local peerconnection and remote transport

attribute EventHandler ontrack

Proxy for the RTCPeerConnection ontrack event handler. As local ids will not match the remote ids anymore in WebRTC, we have extended the event to make them available:

  • remoteStreamId The stream id of the OutgoingStream on the server
  • remoteTrackId The track id of the OutgoingStreamTrack on the server

attribute EventHandler ontrackended

Event handler for the new ontrackended event. The event fired will be an RTCTrackEvent with event name trackended.

attribute EventHandler onstatsended

Proxy for the RTCPeerConnection onstatsended event handler.

Install

Just create a js bundle and link it in your web app.

npm i
npm run-script dist

You will need browserify to be installed globally, you can do it by:

npm i -g browserify

The js file will be located on the dist directory. Note that you will also need to use the Transaction Manager library.

Author

Sergio Garcia Murillo

License

MIT

media-server-client-js's People

Contributors

lucifer1004 avatar murillo128 avatar netizen-ais avatar roboholix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

media-server-client-js's Issues

Adding tracks with Codecs does not work

I've tried running the demo and documented the steps I followed here https://maxrohde.com/2019/07/28/medooze-media-server-demo/

It shows both the local and remote track when either clicking [Add normal track] or [Add simulcast track]. However, when trying to add a track with VP8 or H264, the following exception is thrown and only the local stream is rendered:

Uncaught (in promise) TypeError: Cannot read property 'getType' of undefined
    at PeerConnectionClient.renegotiate (medooze-media-server-client.js:2455)


renegotiate | @ | medooze-media-server-client.js:2455
-- | -- | --
  | async function (async) |   |  
  | renegotiate | @ | medooze-media-server-client.js:2403
  | PeerConnectionClient.pc.onnegotiationneeded | @ | medooze-media-server-client.js:2249

Seems to work fine when manually skipping the logic for overriding the codecs. But I don't have sufficient understanding of this to know if this is safe to do or not.

                //For all transceivers
		for (const transceiver of this.pc.getTransceivers())
		{
			//If we have to override the codec
			if (transceiver.codecs && transceiver.mid)
			{
				//Get local media
				const localMedia = this.localInfo.getMediaById(transceiver.mid);
				//console.log(this.remote.capabilites);
				//Get remote capabilities
				const capabilities = null; // this.remote.capabilities[localMedia.getType()];
				//If got none
				if (!capabilities)
					//Skip
					continue;

I'm running this on Chrome and Ububtu.

errors

when I run the second command 'npm run-script dist' , I get the following error:

===============================

[email protected] dist /Users/lichao/Downloads/media-server-client-js
browserify --standalone MediaServerClient lib/MediaServerClient.js -o dist/medooze-media-server-client.js && cp dist/* demo/www

sh: browserify: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] dist: browserify --standalone MediaServerClient lib/MediaServerClient.js -o dist/medooze-media-server-client.js && cp dist/* demo/www
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] dist script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/lichao/.npm/_logs/2018-11-15T03_09_09_545Z-debug.log

No local ICE candidates

Sorry about limited info, I can only partially debug a remote client, but what I've seen is that peer.localInfo has an empty candidates array, and peer.connectionState / peer.iceConnectionstate are stuck at "new". Any hint

RTCPeerConnection.setRemoteDescription rejection on Chrome 104/Linux

MediaServerClient can't establish connection, either with or without using adapter.js

I can reproduce this using Chrome (dev) Version 104.0.5112.20 (Official Build) beta (64-bit), while the same code works in v.103 or even into a browserstack's (vm?) Windows 11 Version 104.0.5083.0 (Official Build) beta (64-bit)

Console (error):
Uncaught (in promise) DOMException: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=rtpmap:0 pcmu/8000 Duplicate statically assigned payload type with conflicting codec name or clock rate. at PeerConnectionClient.renegotiate (https://.../mmsc.js?r=1656413494:2663:17) at new PeerConnectionClient (https://.../mmsc.js?r=1656413494:2398:8) at MediaServerClient.createManagedPeerConnection (https://.../mmsc.js?r=1656413494:2273:10)

Fill codecPayloadTipe on addTransceivers

To properly enable fixing a codec with simulcast the codecPayloadType of the sendEncodings should be filled when calling addTransceivers.

We can retrieve the codec payloads from the initialization SPP O/A transceivers and do a lookup before adding the simulcast transceiver.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.