Skip to content

Commit

Permalink
Make forceH264=true and forceVP9=true just affect codec selection in …
Browse files Browse the repository at this point in the history
…mediasoup-client
  • Loading branch information
ibc committed May 7, 2020
1 parent 1902dd5 commit 581dcc6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 92 deletions.
14 changes: 4 additions & 10 deletions aiortc/src/urlFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ if (hostname === 'test.mediasoup.org')
protooPort = 4444;

export function getProtooUrl(
{ roomId, peerId, forceH264, forceVP8 }:
{ roomId: string; peerId: string; forceH264: boolean; forceVP8: boolean }): string
{ roomId, peerId }:
{ roomId: string; peerId: string; }
): string
{
let url = `wss://${hostname}:${protooPort}/?roomId=${roomId}&peerId=${peerId}`;

if (forceH264)
url = `${url}&forceH264=true`;
else if (forceVP8)
url = `${url}&forceVP8=true`;

return url;
return `wss://${hostname}:${protooPort}/?roomId=${roomId}&peerId=${peerId}`;
}
135 changes: 94 additions & 41 deletions app/lib/RoomClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export default class RoomClient
// @type {Boolean}
this._useDataChannel = datachannel;

// Force H264 codec for sending.
this._forceH264 = Boolean(forceH264);

// Force VP9 codec for sending.
this._forceVP9 = Boolean(forceVP9);

// External video.
// @type {HTMLVideoElement}
this._externalVideo = null;
Expand Down Expand Up @@ -147,7 +153,7 @@ export default class RoomClient

// Protoo URL.
// @type {String}
this._protooUrl = getProtooUrl({ roomId, peerId, forceH264, forceVP9 });
this._protooUrl = getProtooUrl({ roomId, peerId });

// protoo-client Peer instance.
// @type {protooClient.Peer}
Expand Down Expand Up @@ -966,6 +972,34 @@ export default class RoomClient
track = stream.getVideoTracks()[0].clone();
}

let encodings;
let codec;
const codecOptions =
{
videoGoogleStartBitrate : 1000
};

if (this._forceH264)
{
codec = this._mediasoupDevice.rtpCapabilities.codecs
.find((c) => c.mimeType.toLowerCase() === 'video/h264');

if (!codec)
{
throw new Error('desired H264 codec+configuration is not supported');
}
}
else if (this._forceVP9)
{
codec = this._mediasoupDevice.rtpCapabilities.codecs
.find((c) => c.mimeType.toLowerCase() === 'video/vp9');

if (!codec)
{
throw new Error('desired VP9 codec+configuration is not supported');
}
}

if (this._useSimulcast)
{
// If VP9 is the only available video codec then use SVC.
Expand All @@ -974,31 +1008,27 @@ export default class RoomClient
.codecs
.find((c) => c.kind === 'video');

let encodings;

if (firstVideoCodec.mimeType.toLowerCase() === 'video/vp9')
if (
(this._forceVP9 && codec) ||
firstVideoCodec.mimeType.toLowerCase() === 'video/vp9'
)
{
encodings = VIDEO_KSVC_ENCODINGS;
}
else
{
encodings = VIDEO_SIMULCAST_ENCODINGS;

this._webcamProducer = await this._sendTransport.produce(
{
track,
encodings,
codecOptions :
{
videoGoogleStartBitrate : 1000
}
// NOTE: for testing codec selection.
// codec : this._mediasoupDevice.rtpCapabilities.codecs
// .find((codec) => codec.mimeType.toLowerCase() === 'video/h264')
});
}
else
{
this._webcamProducer = await this._sendTransport.produce({ track });
}
}

this._webcamProducer = await this._sendTransport.produce(
{
track,
encodings,
codecOptions,
codec
});

store.dispatch(stateActions.addProducer(
{
id : this._webcamProducer.id,
Expand Down Expand Up @@ -1252,6 +1282,34 @@ export default class RoomClient

track = stream.getVideoTracks()[0];

let encodings;
let codec;
const codecOptions =
{
videoGoogleStartBitrate : 1000
};

if (this._forceH264)
{
codec = this._mediasoupDevice.rtpCapabilities.codecs
.find((c) => c.mimeType.toLowerCase() === 'video/h264');

if (!codec)
{
throw new Error('desired H264 codec+configuration is not supported');
}
}
else if (this._forceVP9)
{
codec = this._mediasoupDevice.rtpCapabilities.codecs
.find((c) => c.mimeType.toLowerCase() === 'video/vp9');

if (!codec)
{
throw new Error('desired VP9 codec+configuration is not supported');
}
}

if (this._useSharingSimulcast)
{
// If VP9 is the only available video codec then use SVC.
Expand All @@ -1260,9 +1318,10 @@ export default class RoomClient
.codecs
.find((c) => c.kind === 'video');

let encodings;

if (firstVideoCodec.mimeType.toLowerCase() === 'video/vp9')
if (
(this._forceVP9 && codec) ||
firstVideoCodec.mimeType.toLowerCase() === 'video/vp9'
)
{
encodings = VIDEO_SVC_ENCODINGS;
}
Expand All @@ -1271,25 +1330,19 @@ export default class RoomClient
encodings = VIDEO_SIMULCAST_ENCODINGS
.map((encoding) => ({ ...encoding, dtx: true }));
}
}

this._shareProducer = await this._sendTransport.produce(
this._shareProducer = await this._sendTransport.produce(
{
track,
encodings,
codecOptions,
codec,
appData :
{
track,
encodings,
codecOptions :
{
videoGoogleStartBitrate : 1000
},
appData :
{
share : true
}
});
}
else
{
this._shareProducer = await this._sendTransport.produce({ track });
}
share : true
}
});

store.dispatch(stateActions.addProducer(
{
Expand Down
10 changes: 2 additions & 8 deletions app/lib/urlFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ let protooPort = 4443;
if (window.location.hostname === 'test.mediasoup.org')
protooPort = 4444;

export function getProtooUrl({ roomId, peerId, forceH264, forceVP9 })
export function getProtooUrl({ roomId, peerId })
{
const hostname = window.location.hostname;
let url = `wss://${hostname}:${protooPort}/?roomId=${roomId}&peerId=${peerId}`;

if (forceH264)
url = `${url}&forceH264=true`;
else if (forceVP9)
url = `${url}&forceVP9=true`;

return url;
return `wss://${hostname}:${protooPort}/?roomId=${roomId}&peerId=${peerId}`;
}
31 changes: 3 additions & 28 deletions server/lib/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,16 @@ class Room extends EventEmitter
* @param {mediasoup.Worker} mediasoupWorker - The mediasoup Worker in which a new
* mediasoup Router must be created.
* @param {String} roomId - Id of the Room instance.
* @param {Boolean} [forceH264=false] - Whether just H264 must be used in the
* mediasoup Router video codecs.
* @param {Boolean} [forceVP9=false] - Whether just VP9 must be used in the
* mediasoup Router video codecs.
*/
static async create({ mediasoupWorker, roomId, forceH264 = false, forceVP9 = false })
static async create({ mediasoupWorker, roomId })
{
logger.info(
'create() [roomId:%s, forceH264:%s, forceVP9:%s]',
roomId, forceH264, forceVP9);
logger.info('create() [roomId:%s]', roomId);

// Create a protoo Room instance.
const protooRoom = new protoo.Room();

// Router media codecs.
let { mediaCodecs } = config.mediasoup.routerOptions;

// If forceH264 is given, remove all video codecs but H264.
if (forceH264)
{
mediaCodecs = mediaCodecs
.filter((codec) => (
codec.kind === 'audio' ||
codec.mimeType.toLowerCase() === 'video/h264'
));
}
// If forceVP9 is given, remove all video codecs but VP9.
if (forceVP9)
{
mediaCodecs = mediaCodecs
.filter((codec) => (
codec.kind === 'audio' ||
codec.mimeType.toLowerCase() === 'video/vp9'
));
}
const { mediaCodecs } = config.mediasoup.routerOptions;

// Create a mediasoup Router.
const mediasoupRouter = await mediasoupWorker.createRouter({ mediaCodecs });
Expand Down
8 changes: 3 additions & 5 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,6 @@ async function runProtooWebSocketServer()
const u = url.parse(info.request.url, true);
const roomId = u.query['roomId'];
const peerId = u.query['peerId'];
const forceH264 = u.query['forceH264'] === 'true';
const forceVP9 = u.query['forceVP9'] === 'true';

if (!roomId || !peerId)
{
Expand All @@ -421,7 +419,7 @@ async function runProtooWebSocketServer()
// roomId.
queue.push(async () =>
{
const room = await getOrCreateRoom({ roomId, forceH264, forceVP9 });
const room = await getOrCreateRoom({ roomId });

// Accept the protoo WebSocket connection.
const protooWebSocketTransport = accept();
Expand Down Expand Up @@ -453,7 +451,7 @@ function getMediasoupWorker()
/**
* Get a Room instance (or create one if it does not exist).
*/
async function getOrCreateRoom({ roomId, forceH264 = false, forceVP9 = false })
async function getOrCreateRoom({ roomId })
{
let room = rooms.get(roomId);

Expand All @@ -464,7 +462,7 @@ async function getOrCreateRoom({ roomId, forceH264 = false, forceVP9 = false })

const mediasoupWorker = getMediasoupWorker();

room = await Room.create({ mediasoupWorker, roomId, forceH264, forceVP9 });
room = await Room.create({ mediasoupWorker, roomId });

rooms.set(roomId, room);
room.on('close', () => rooms.delete(roomId));
Expand Down

0 comments on commit 581dcc6

Please sign in to comment.