Skip to content

Commit

Permalink
fix: Some type fixes and bug fixes to unified track creation refactor (
Browse files Browse the repository at this point in the history
  • Loading branch information
mildsunrise authored Nov 11, 2024
1 parent 400f69d commit f7259d1
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 104 deletions.
11 changes: 6 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export type Refresher = import("./build/types/Refresher");
export type SDPManager = import("./build/types/SDPManager");
export type SDPManagerPlanB = import("./build/types/SDPManagerPlanB");
export type SDPManagerUnified = import( "./build/types/SDPManagerUnified");
//export type Streamer = import("./build/types/Streamer");
//export type StreamerSession = import("./build/types/StreamerSession");
export type Streamer = import("./build/types/Streamer");
export type StreamerSession = import("./build/types/StreamerSession");
export type Transponder = import("./build/types/Transponder");
export type Transport = import("./build/types/Transport");

export type {
MediaStats as IncomingMediaStats, TrackStats as IncomingTrackStats,
MediaStats as IncomingMediaStats, TrackStats as IncomingTrackStats, Encoding,
ActiveEncodingInfo, ActiveLayersInfo, EncodingStats, LayerStats, PacketWaitTime,
} from "./build/types/IncomingStreamTrack";

Expand All @@ -34,9 +34,10 @@ export type {
ReceiverReport,
} from "./build/types/OutgoingStreamTrack";

export type { CreateTransportOptions, RawTxOptions, PeerInfo, ParsedPeerInfo } from "./build/types/Endpoint";
export type { CreateTransportOptions, CreateOfferParameters, RawTxOptions, PeerInfo, ParsedPeerInfo } from "./build/types/Endpoint";
export type { Frame, FrameType } from "./build/types/IncomingStreamTrackReader";
export type { RecorderParams } from "./build/types/Recorder";
export type { SDPState } from "./build/types/SDPManager";
export type { LayerSelection, SetTargetBitrateOptions } from "./build/types/Transponder";
export type { LayerSelection, SetTargetBitrateOptions, TargetBitrateInfo, TargetBitrateValue } from "./build/types/Transponder";
export type { DTLSState, ICEStats, TransportStats, TransportDumpOptions, CreateStreamOptions, CreateStreamTrackOptions, SSRCs } from "./build/types/Transport";
export type { StreamerSessionOptions } from "./build/types/StreamerSession";
5 changes: 2 additions & 3 deletions lib/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const {
DTLSInfo,
ICEInfo,
StreamInfo,
StreamInfoLike,
TrackInfo,
Setup,
} = require("semantic-sdp");
Expand Down Expand Up @@ -57,7 +56,7 @@ const parseInt = /** @type {(x: number) => number} */ (global.parseInt);
/**
* @typedef {Object} CreateOfferParameters
* @property {boolean} [unified] - Generate unified plan like media ids
* @property {Array<StreamInfoLike>} [streams] - Add the stream infos to the generated offer
* @property {Array<SemanticSDP.StreamInfoLike>} [streams] - Add the stream infos to the generated offer
*/


Expand Down Expand Up @@ -372,7 +371,7 @@ class Endpoint extends Emitter
if (!mirroredStream)
{
//Create new stream
mirroredStream = new IncomingStream(incomingStream.transport,incomingStream.receiver, new StreamInfo(incomingStream.getId()));
mirroredStream = new IncomingStream(incomingStream.getId(), incomingStream.transport);

//Add to map and mirror set
this.mirrored.streams.set(incomingStream,mirroredStream);
Expand Down
10 changes: 4 additions & 6 deletions lib/IncomingStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const {
DTLSInfo,
ICEInfo,
StreamInfo,
StreamInfoLike,
TrackInfo,
TrackInfoLike
} = require("semantic-sdp");

/**
Expand All @@ -40,7 +38,7 @@ class IncomingStream extends Emitter
*/
constructor(
/** @type {string} */ id,
/** @type {Transport} */ transport
/** @type {import("./Transport") | null} */ transport
)
{
//Init emitter
Expand Down Expand Up @@ -77,7 +75,7 @@ class IncomingStream extends Emitter
if (this.counter===0)
this.emit("detached",this);
};
this.onTrackStopped = (incomingStreamTrack) => {
this.onTrackStopped = (/** @type {IncomingStreamTrack} */ incomingStreamTrack) => {
//Remove from map
this.tracks.delete(incomingStreamTrack.getId());
}
Expand Down Expand Up @@ -308,7 +306,7 @@ class IncomingStream extends Emitter
/**
* Create new track from a TrackInfo object and add it to this stream
* @param {SemanticSDP.TrackType} media Media type
* @param {TrackInfoLike} params Track info
* @param {SemanticSDP.TrackInfoLike} [params] Track info
* @returns {IncomingStreamTrack}
*/
createTrack(media, params)
Expand Down Expand Up @@ -372,7 +370,7 @@ class IncomingStream extends Emitter
super.stop();

//Remove transport reference, so destructor is called on GC
this.transport = null;
/** @type {any} */ (this.transport) = null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/MediaServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ MediaServer.createIncomingStreamSimulcastAdapter = function(
/** @type {string} */ mediaId)
{
//Create transport-less stream
const incomingStream = new IncomingStream(null,null,{id: streamId,tracks:[]});
const incomingStream = new IncomingStream(streamId,null);
//Create track
const incomingStreamTrack = MediaServer.createIncomingStreamTrackSimulcastAdapter(trackId, mediaId);
//Add track to stream
Expand Down
9 changes: 4 additions & 5 deletions lib/OutgoingStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class OutgoingStream extends Emitter
*/
constructor(
/** @type {string} */ id,
/** @type {Transport} */ transport,
/** @type {import("./Transport")} */ transport,
)
{
//Init emitter
Expand Down Expand Up @@ -286,8 +286,8 @@ class OutgoingStream extends Emitter
/**
* Create new track from a TrackInfo object and add it to this stream
* @param {SemanticSDP.TrackType} media Media type
* @param {SemanticSDP.TrackInfoLike } params Params plain object or TrackInfo object
* @returns The new outgoing stream
* @param {SemanticSDP.TrackInfoLike} params Params plain object or TrackInfo object
* @returns {OutgoingStreamTrack}
*/
createTrack(media, params)
{
Expand Down Expand Up @@ -320,8 +320,7 @@ class OutgoingStream extends Emitter
super.stop();

//Remove transport reference, so destructor is called on GC
//@ts-expect-error
this.transport = null;
/** @type {any} */ (this.transport) = null;
}
};

Expand Down
28 changes: 12 additions & 16 deletions lib/Streamer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
const Emitter = require("medooze-event-emitter");
const StreamerSession = require("./StreamerSession");
const { MediaInfo } = require("semantic-sdp");

/**
* @typedef {Object} StreamerEvents
* @property {(self: Streamer) => void} stopped
*/

/**
* An streamer allows to send and receive plain RTP over udp sockets.
* This allows both to bridge legacy enpoints or integrate streaming/broadcasting services.
* @extends {Emitter<StreamerEvents>}
*/
class Streamer extends Emitter
{
/**
* @ignore
* @hideconstructor
* @param {string} [ip]
* private constructor
*/
constructor(ip)
Expand All @@ -19,19 +28,14 @@ class Streamer extends Emitter
//Store ip address of the endpoint
this.ip = ip;
//Sessions set
/** @type {Set<StreamerSession>} */
this.sessions = new Set();
}

/**
* Creates a new streaming session from a media description
* @param {MediaInfo} media - Media codec description info
* @param {Object} params - Network parameters [Optional]
* @param {Object} params.local - Local parameters
* @param {Number} params.local.port - receiving port
* @param {Object} params.remote - Remote parameters
* @param {String} params.remote.ip - Sending ip address
* @param {Number} params.remote.port - Sending port
* @param {Number} params.noRTCP - Disable sending rtcp
* @param {StreamerSession.StreamerSessionOptions} [params] - Network parameters
* @returns {StreamerSession} The new streaming session
*/
createSession(media,params)
Expand Down Expand Up @@ -60,15 +64,7 @@ class Streamer extends Emitter
for (let session of this.sessions.values())
//stop
session.stop();

/**
* Streamer stopped event
*
* @name stopped
* @memberof Streamer
* @kind event
* @argument {Streamer} streamer
*/

this.emit("stopped",this);

//Stop emitter
Expand Down
36 changes: 29 additions & 7 deletions lib/StreamerSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,56 @@ const SharedPointer = require("./SharedPointer");
const Emitter = require("medooze-event-emitter");
const IncomingStreamTrack = require("./IncomingStreamTrack");
const OutgoingStreamTrack = require("./OutgoingStreamTrack");
const Utils = require("./Utils");
const SemanticSDP = require("semantic-sdp");
const LFSR = require('lfsr');
const {
TrackInfo,
MediaInfo,
} = require("semantic-sdp");

//@ts-expect-error
const parseInt = /** @type {(x: number) => number} */ (global.parseInt);


/**
* @typedef {Object} StreamerSessionOptions
* @property {{ port: number }} [local] Local parameters
* @property {{ ip: string, port: number }} [remote] Remote parameters
* @property {number} [noRTCP] Disable sending rtcp
*/

/**
* @typedef {Object} StreamerSessionEvents
* @property {(self: StreamerSession) => void} stopped
*/

/**
* Represent the connection between a local udp port and a remote one. It sends and/or receive plain RTP data.
* @extends {Emitter<StreamerSessionEvents>}
*/
class StreamerSession extends Emitter
{
/**
* @ignore
* @hideconstructor
* @param {MediaInfo} media
* @param {StreamerSessionOptions} [params]
* private constructor
*/
constructor(/** @type {SemanticSDP.MediaInfo} */ media, params)
constructor(media, params)
{
//Init emitter
super();

//Create new sequence generator
this.lfsr = new LFSR();

const mediaType = media.getType();
if (mediaType === "application")
throw new Error("application media not supported");

//Create session
this.session = new Native.RTPSessionFacadeShared(media.getType().toLowerCase()==="audio" ? 0 : 1);
this.session = SharedPointer(new Native.RTPSessionFacadeShared(Utils.mediaToFrameType(mediaType)));
//Set local params
if (params && params.local && params.local.port)
//Set it
Expand Down Expand Up @@ -79,8 +102,8 @@ class StreamerSession extends Emitter
this.session.Init(properties);

//Create incoming and outgoing tracks
this.incoming = new IncomingStreamTrack(media.getType(), media.getType(), "", this.session.GetTimeService(), this.session.toRTPReceiver(), {'':SharedPointer(this.session.GetIncomingSourceGroup())});
this.outgoing = new OutgoingStreamTrack(media.getType(), media.getType(), "", this.session.toRTPSender(), SharedPointer(this.session.GetOutgoingSourceGroup()));
this.incoming = new IncomingStreamTrack(mediaType, media.getType(), "", this.session.GetTimeService(), SharedPointer(this.session.toRTPReceiver()), {'':SharedPointer(this.session.GetIncomingSourceGroup())});
this.outgoing = new OutgoingStreamTrack(mediaType, media.getType(), "", this.session.toRTPSender(), SharedPointer(this.session.GetOutgoingSourceGroup()));

//Try to get h264 codec
const h264 = media.getCodec("h264");
Expand Down Expand Up @@ -159,8 +182,7 @@ class StreamerSession extends Emitter
super.stop();

//Remove transport reference, so destructor is called on GC
//@ts-expect-error
this.session = null;
/** @type {any} */ (this).session = null;
}

}
Expand Down
Loading

0 comments on commit f7259d1

Please sign in to comment.