Skip to content

Commit

Permalink
add hook onOffer and onAnswer
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Mar 29, 2024
1 parent 13e211a commit fa15949
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whip-whep",
"version": "1.1.1",
"name": "@binbat/whip-whep",
"version": "1.1.1-sdp",
"description": "WHIP and WHEP clients",
"scripts": {
"precommit": "npm run-script lint",
Expand Down
24 changes: 5 additions & 19 deletions whep.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class WHEPClient extends EventTarget
//Pending candidadtes
this.candidates = [];
this.endOfcandidates = false;

this.onOffer = offer => offer;
this.onAnswer = answer => answer;
}

async view(pc, url, token)
Expand All @@ -33,24 +36,6 @@ export class WHEPClient extends EventTarget
this.token = token;
this.pc = pc;

//Listen for state change events
pc.onconnectionstatechange = (event) =>
{
switch (pc.connectionState)
{
case "connected":
// The connection has become fully connected
break;
case "disconnected":
case "failed":
// One or more transports has terminated unexpectedly or in an error
break;
case "closed":
// The connection has been closed
break;
}
}

//Listen for candidates
pc.onicecandidate = (event) =>
{
Expand All @@ -73,6 +58,7 @@ export class WHEPClient extends EventTarget
}
//Create SDP offer
const offer = await pc.createOffer();
offer.sdp = this.onOffer(offer.sdp);

//Request headers
const headers = {
Expand Down Expand Up @@ -261,7 +247,7 @@ export class WHEPClient extends EventTarget
//}

//And set remote description
await pc.setRemoteDescription({ type: "answer", sdp: answer });
await pc.setRemoteDescription({ type: "answer", sdp: this.onAnswer(answer) });
}

async restart()
Expand Down
24 changes: 5 additions & 19 deletions whip.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class WHIPClient
//Pending candidadtes
this.candidates = [];
this.endOfcandidates = false;

this.onOffer = offer => offer;
this.onAnswer = answer => answer;
}

async publish(pc, url, token)
Expand All @@ -24,24 +27,6 @@ export class WHIPClient
this.token = token;
this.pc = pc;

//Listen for state change events
pc.onconnectionstatechange = (event) =>
{
switch (pc.connectionState)
{
case "connected":
// The connection has become fully connected
break;
case "disconnected":
case "failed":
// One or more transports has terminated unexpectedly or in an error
break;
case "closed":
// The connection has been closed
break;
}
}

//Listen for candidates
pc.onicecandidate = (event) =>
{
Expand All @@ -64,6 +49,7 @@ export class WHIPClient
}
//Create SDP offer
const offer = await pc.createOffer();
offer.sdp = this.onOffer(offer.sdp);

//Request headers
const headers = {
Expand Down Expand Up @@ -210,7 +196,7 @@ export class WHIPClient
//}

//And set remote description
await pc.setRemoteDescription({ type: "answer", sdp: answer });
await pc.setRemoteDescription({ type: "answer", sdp: this.onAnswer(answer) });
}

async restart()
Expand Down

0 comments on commit fa15949

Please # to comment.