提交 0f23804e authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Working #54

上级 04ec0b82
...@@ -245,16 +245,6 @@ class RtpReceiver extends EventEmitter ...@@ -245,16 +245,6 @@ class RtpReceiver extends EventEmitter
}); });
} }
isRtpReceiver()
{
return true;
}
isRtpSender()
{
return false;
}
/** /**
* Start receiving media. * Start receiving media.
* *
......
...@@ -137,16 +137,6 @@ class RtpSender extends EventEmitter ...@@ -137,16 +137,6 @@ class RtpSender extends EventEmitter
}); });
} }
isRtpSender()
{
return true;
}
isRtpReceiver()
{
return false;
}
/** /**
* Assign Transport. * Assign Transport.
* *
......
'use strict';
module.exports =
{
RTCSdpType :
{
offer : 'offer',
answer : 'answer'
},
RTCSignalingState :
{
stable : 'stable',
haveLocalOffer : 'have-local-offer',
haveRemoteOffer : 'have-remote-offer'
},
RTCRtpTransceiverDirection :
{
sendrecv : 'sendrecv',
sendonly : 'sendonly',
recvonly : 'recvonly',
inactive : 'inactive'
}
};
'use strict';
const EventEmitter = require('events').EventEmitter;
// const sdpTransform = require('sdp-transform');
// const randomString = require('random-string');
const logger = require('../../logger');
// const extra = require('../../extra');
// const InvalidStateError = require('../../errors').InvalidStateError;
// const RTCDictionaries = require('../RTCDictionaries');
// const RTCSessionDescription = require('../RTCSessionDescription');
// const RTCRtpTransceiver = require('../RTCRtpTransceiver');
// const NEGOTIATION_NEEDED_DELAY = 1000;
class RTCPeerConnectionUnifiedPlan extends EventEmitter
{
constructor(room, peerName, options)
{
super();
this.setMaxListeners(Infinity);
this._logger = logger(`webrtc:RTCPeerConnectionUnifiedPlan:${peerName}`);
this._logger.debug('constructor() [options:%o]', options);
}
}
module.exports = RTCPeerConnectionUnifiedPlan;
'use strict';
const RTCPeerConnectionUnifiedPlan = require('./RTCPeerConnectionUnifiedPlan');
const RTCPeerConnectionPlanB = require('./RTCPeerConnectionPlanB');
class RTCPeerConnection
{
/**
* RTCPeerConnection constructor.
* @param {Room} room
* @param {string} peerName
* @param {object} [options]
* @param {object} [options.transportOptions] - Options for Transport.
* @param {boolean} [options.usePlanB] - Use Plan-B rather than Unified-Plan.
* @param {object} [options.bandwidth]
* @param {number} [options.bandwidth.audio] - Maximum bandwidth for audio streams.
* @param {number} [options.bandwidth.video] - Maximum bandwidth for video streams.
*/
constructor(room, peerName, options)
{
options = options || {};
if (!options.usePlanB)
return new RTCPeerConnectionUnifiedPlan(room, peerName, options);
else
return new RTCPeerConnectionPlanB(room, peerName, options);
}
}
module.exports = RTCPeerConnection;
'use strict';
const RTCDictionaries = require('./RTCDictionaries');
class RTCRtpTransceiver
{
constructor(kind)
{
// kind attribute.
this._kind = kind;
// mid attribute.
this._mid = null;
// RtpReceiver instance.
this._receiver = null;
// RtpSender instance.
this._sender = null;
// stopped attribute.
this._stopped = false;
// direction attribute.
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.inactive;
// Bind events.
this._onReceiverClose = this._onReceiverClose.bind(this);
this._onSenderClose = this._onSenderClose.bind(this);
// TODO: Add more.
}
get kind()
{
return this._kind;
}
get mid()
{
return this._mid;
}
set mid(mid)
{
if (this._stopped)
return;
this._mid = mid;
// TODO: pass it to the receiver and sender.
}
get receiver()
{
return this._receiver;
}
set receiver(receiver)
{
if (this._stopped)
return;
if (this._receiver)
this._unhandleReceiverEvents(this._receiver);
if (receiver)
{
this._receiver = receiver;
this._handleReceiverEvents(receiver);
// Update direction.
switch (this._direction)
{
case RTCDictionaries.RTCRtpTransceiverDirection.inactive:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.recvonly;
break;
case RTCDictionaries.RTCRtpTransceiverDirection.sendonly:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.sendrecv;
break;
}
// TODO: set mid?
}
else
{
this._receiver = null;
// Update direction.
switch (this._direction)
{
case RTCDictionaries.RTCRtpTransceiverDirection.recvonly:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.inactive;
break;
case RTCDictionaries.RTCRtpTransceiverDirection.sendrecv:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.sendonly;
break;
}
}
}
get sender()
{
return this._sender;
}
set sender(sender)
{
if (this._stopped)
return;
if (this._sender)
this._unhandleSenderEvents(this._sender);
if (sender)
{
this._sender = sender;
this._handleSenderEvents(sender);
// Update direction.
switch (this._direction)
{
case RTCDictionaries.RTCRtpTransceiverDirection.inactive:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.sendonly;
break;
case RTCDictionaries.RTCRtpTransceiverDirection.recvonly:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.sendrecv;
break;
}
// TODO: set mid.
}
else
{
this._sender = null;
// Update direction.
switch (this._direction)
{
case RTCDictionaries.RTCRtpTransceiverDirection.sendonly:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.inactive;
break;
case RTCDictionaries.RTCRtpTransceiverDirection.sendrecv:
this._direction = RTCDictionaries.RTCRtpTransceiverDirection.recvonly;
break;
}
}
}
get stopped()
{
return this._stopped;
}
get direction()
{
return this._direction;
}
stop()
{
if (this._stopped)
return;
// Close receiver.
if (this._receiver)
this._receiver.close();
// Close sender.
if (this._sender)
this._sender.close();
// Finally set stopped flag.
this._stopped = true;
}
_handleReceiverEvents(receiver)
{
receiver.on('close', this._onReceiverClose);
}
_unhandleReceiverEvents(receiver)
{
receiver.removeListener('close', this._onReceiverClose);
}
_handleSenderEvents(sender)
{
sender.on('close', this._onSenderClose);
}
_unhandleSenderEvents(sender)
{
sender.removeListener('close', this._onSenderClose);
}
_onReceiverClose()
{
this.receiver = null;
}
_onSenderClose()
{
this.sender = null;
}
}
module.exports = RTCRtpTransceiver;
'use strict'; 'use strict';
const sdpTransform = require('sdp-transform');
const RTCDictionaries = require('./RTCDictionaries');
class RTCSessionDescription class RTCSessionDescription
{ {
static isDescription(obj)
{
return (
obj &&
typeof obj.type === 'string' &&
typeof obj.sdp === 'string'
);
}
/** /**
* RTCSessionDescription constructor. * RTCSessionDescription constructor.
* @param {object} [options] * @param {object} [descriptionInitDict]
* @param {string} [options.type] - "offer" / "answer". * @param {string} [descriptionInitDict.type] - "offer" / "answer".
* @param {string} [options.sdp] - SDP. * @param {string|object} [descriptionInitDict.sdp] - SDP string or sdp-transform object.
*/ */
constructor(descriptionInitDict) constructor(descriptionInitDict)
{ {
let type = descriptionInitDict.type; let type = descriptionInitDict.type;
if (['offer', 'answer'].indexOf(type) === -1) switch (type)
{
case RTCDictionaries.RTCSdpType.offer:
break;
case RTCDictionaries.RTCSdpType.answer:
break;
default:
throw new TypeError(`invalid type "${type}"`); throw new TypeError(`invalid type "${type}"`);
}
let sdp = descriptionInitDict.sdp; let sdp = descriptionInitDict.sdp;
let parsed;
switch (typeof sdp)
{
case 'string':
{
try
{
parsed = sdpTransform.parse(sdp);
}
catch (error)
{
throw new Error(`invalid sdp: ${error}`);
}
break;
}
case 'object':
{
parsed = sdp;
try
{
sdp = sdpTransform.write(parsed);
}
catch (error)
{
throw new Error(`invalid sdp-transform object: ${error}`);
}
break;
}
default:
{
throw new TypeError('sdp must be a string or a sdp-transform object');
}
}
if (typeof sdp !== 'string') // Private attributes.
throw new TypeError('sdp must be a string'); this._type = type;
this._sdp = sdp;
this._parsed = parsed;
}
// Public r/w properties. get type()
this.type = type; {
this.sdp = sdp; return this._type;
}
get sdp()
{
return this._sdp;
}
get parsed()
{
return this._parsed;
}
serialize()
{
return {
type : this._type,
sdp : this._sdp
};
} }
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论