Skip to content

Commit

Permalink
Add the ability to set preferred video codec
Browse files Browse the repository at this point in the history
instance.startStreaming(connectionUrl, connectionConfig)

ConnectionConfig.preferredVideoFormat
- type
    - String: Video codec name (eq. H256, VP8)
- If set the specified codec will be preferred if available.
  • Loading branch information
SangwonOh committed Mar 12, 2023
1 parent 469b258 commit 825bd93
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ ovenLivekit.getUserMedia().then(function () {
- When this API is called, the media stream starts to be streamed according to OvenMediaEngine's signaling protocol.

#### `ConnectionConfig`

##### `preferredVideoFormat`
- type
- String: Video codec name (eq. H256, VP8)
- If set the specified codec will be preferred if available.
##### `iceServers`
- type
- [`RTCConfiguration.iceServers`](https://developer.mozilla.org/en-US/docs/Web/API/RTCConfiguration/iceServers)
Expand All @@ -289,6 +294,7 @@ ovenLivekit.getUserMedia().then(function () {
- String: String you want to append to a=fmtp of SDP.
- If set video format is appended to the a=fmtp sections of SDP.


#### `instance.stopStreaming()`
- Close peer connection and websocket associated with OvenMediaEngine.

Expand Down
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ovenlivekit",
"version": "1.0.4",
"version": "1.0.5",
"description": "OvenLiveKit for Web is an open source JavaScript SDK suite for live streaming from web browsers to OvenMediaEngine.",
"main": "dist/OvenLiveKit.min.js",
"scripts": {
Expand Down
47 changes: 44 additions & 3 deletions src/OvenLiveKit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const OvenLiveKit = {};

const version = '1.0.4';
const version = '1.0.5';
const logHeader = 'OvenLiveKit.js :';
const logEventHeader = 'OvenLiveKit.js ====';

Expand Down Expand Up @@ -43,8 +43,9 @@ function getFormatNumber(sdp, format) {

lines[i] = lines[i].toLowerCase();

if (lines[i].indexOf('a=rtpmap') > -1 && lines[i].indexOf(format.toLowerCase()) > -1) {
if (lines[i].indexOf('a=rtpmap') === 0 && lines[i].indexOf(format.toLowerCase()) > -1) {
// parsing "a=rtpmap:100 H264/90000" line
// a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters >]
formatNumber = lines[i].split(' ')[0].split(':')[1];
break;
}
Expand All @@ -53,6 +54,37 @@ function getFormatNumber(sdp, format) {
return formatNumber;
}

function setPreferredVideoFormat(sdp, formatName) {

const formatNumber = getFormatNumber(sdp, formatName);

if (formatNumber === -1) {
return sdp;
}

let newLines = [];
const lines = sdp.split('\r\n');

for (let i = 0; i < lines.length - 1; i++) {

const line = lines[i];

if (line.indexOf('m=video') === 0) {

// m=<media> <port>/<number of ports> <transport> <fmt list>
const others = line.split(' ').slice(0, 3);
const formats = line.split(' ').slice(3);
formats.sort(function (x, y) { return x == formatNumber ? -1 : y == formatNumber ? 1 : 0; });
newLines.push(others.concat(formats).join(' '));
} else {
newLines.push(line);
}

}

return newLines.join('\r\n') + '\r\n';
}

function removeFormat(sdp, formatNumber) {
let newLines = [];
let lines = sdp.split('\r\n');
Expand Down Expand Up @@ -570,7 +602,13 @@ function addMethod(instance) {
offer.sdp = appendFmtp(offer.sdp);
}

if (instance.connectionConfig.preferredVideoFormat) {
offer.sdp = setPreferredVideoFormat(offer.sdp, instance.connectionConfig.preferredVideoFormat)
}


// offer.sdp = appendOrientation(offer.sdp);
console.info(logHeader, 'Modified offer sdp\n\n' + offer.sdp);

peerConnection.setRemoteDescription(new RTCSessionDescription(offer))
.then(function () {
Expand All @@ -583,6 +621,9 @@ function addMethod(instance) {
answer.sdp = appendFmtp(answer.sdp);
}

answer.sdp = setPreferredVideoFormat(answer.sdp, instance.connectionConfig.preferredVideoFormat)
console.info(logHeader, 'Modified answer sdp\n\n' + answer.sdp);

peerConnection.setLocalDescription(answer)
.then(function () {

Expand Down Expand Up @@ -738,7 +779,7 @@ function addMethod(instance) {

instance.startStreaming = function (connectionUrl, connectionConfig) {

console.info(logEventHeader, 'Start Streaming');
console.info(logEventHeader, 'Start Streaming with connectionConfig', connectionConfig);

if (connectionConfig) {

Expand Down

0 comments on commit 825bd93

Please sign in to comment.