提交 4f5df6fa authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Breaking things!

上级 a3d2de61
......@@ -66,7 +66,7 @@ module.exports =
'max-len': [ 2, 86,
{
tabWidth: 2,
comments: 86,
comments: 90,
ignoreUrls: false,
ignoreStrings: false,
ignoreTemplateLiterals: false,
......
# TODO in mediasoup v2 (server-side)
* Remove `gulp rtpcapabilities`.
* Should accept `peer.createTransport()` by passing a `transportId` number. The same for `Producer`.
* May have to react on DTLS ALERT CLOSE in the server and make it "really" close the Transport and notify the client. Bufff... I don't like this...
......
......@@ -23,13 +23,13 @@ const workerFiles =
];
const nodeTests =
[
'test/test-mediasoup.js',
// 'test/test-mediasoup.js',
'test/test-Server.js',
'test/test-Room.js',
'test/test-Peer.js',
'test/test-Transport.js',
'test/test-Producer.js',
'test/test-utils.js'
// 'test/test-Room.js',
// 'test/test-Peer.js',
// 'test/test-Transport.js',
// 'test/test-Producer.js',
// 'test/test-utils.js'
];
const workerCompilationDatabaseTemplate = 'worker/compile_commands_template.json';
const workerHeaderFilterRegex =
......@@ -38,18 +38,6 @@ const workerHeaderFilterRegex =
'|handles/*.hpp|Channel/*.hpp|RTC/**/*.hpp)';
const numCpus = os.cpus().length;
gulp.task('rtpcapabilities', () =>
{
const supportedRtpCapabilities = require('./lib/supportedRtpCapabilities');
return gulp.src('worker/src/RTC/Room.cpp')
// Let's generate valid syntax as expected by clang-format rules.
.pipe(replace(/(const std::string supportedRtpCapabilities =).*\r?\n.*/,
`$1\n\t\t\t R"(${JSON.stringify(supportedRtpCapabilities)})";`))
.pipe(gulp.dest('worker/src/RTC/'))
.pipe(touch());
});
gulp.task('lint:node', () =>
{
return gulp.src(nodeFiles)
......
......@@ -22,7 +22,7 @@ class Room extends EventEmitter
this._internal = internal;
// Room data.
// - .capabilities
// - .rtpCapabilities
this._data = data;
// Channel instance.
......@@ -157,9 +157,9 @@ class Room extends EventEmitter
return this._closed;
}
get capabilities()
get rtpCapabilities()
{
return this._data.capabilities;
return this._data.rtpCapabilities;
}
/**
......@@ -280,10 +280,7 @@ class Room extends EventEmitter
// Store the Peer instance and remove it when closed.
this._peers.set(peerName, peer);
peer.on('close', () =>
{
this._peers.delete(peerName);
});
peer.on('close', () => this._peers.delete(peerName));
// Listen for new Producers so we can associate new Consumers.
peer.on('newproducer', (producer) =>
......@@ -343,7 +340,7 @@ class Room extends EventEmitter
{
const response =
{
rtpCapabilities : this.capabilities,
rtpCapabilities : this.rtpCapabilities,
mandatoryCodecPayloadTypes : [] // TODO
};
......
......@@ -5,8 +5,9 @@ const path = require('path');
const EventEmitter = require('events').EventEmitter;
const check = require('check-types');
const Logger = require('./Logger');
const utils = require('./utils');
const errors = require('./errors');
const utils = require('./utils');
const ortc = require('./ortc');
const Worker = require('./Worker');
const DEFAULT_NUM_WORKERS = Object.keys(os.cpus()).length;
......@@ -46,7 +47,7 @@ class Server extends EventEmitter
this._closed = false;
// Clone options.
options = utils.clone(options);
options = utils.cloneObject(options);
// Update numWorkers (if given).
if (check.integer(options.numWorkers) && check.positive(options.numWorkers))
......@@ -229,6 +230,7 @@ class Server extends EventEmitter
return Promise.all(promises);
}
// TODO: REMOVE
/**
* Create a Room instance.
*
......@@ -252,6 +254,28 @@ class Server extends EventEmitter
});
}
/**
* Create a Room instance.
*
* @return {Room}
*/
Room(mediaCodecs)
{
logger.debug('Room()');
if (this._closed)
throw new errors.InvalidStateError('Server closed');
// This may throw.
const rtpCapabilities = ortc.generateRoomRtpCapabilities(mediaCodecs);
const worker = this._getRandomWorker();
const room = worker.Room({ rtpCapabilities });
this.emit('newroom', room);
return room;
}
_addWorker(worker)
{
// Store the Worker instance and remove it when closed.
......
......@@ -181,6 +181,7 @@ class Worker extends EventEmitter
});
}
// TODO: REMOVE
createRoom(options)
{
logger.debug('createRoom() [options:%o]', options);
......@@ -211,6 +212,40 @@ class Worker extends EventEmitter
throw error;
});
}
/**
* Create a Room instance.
*
* @return {Room}
*/
Room(data)
{
logger.debug('Room()');
const internal =
{
roomId : utils.randomNumber()
};
const room = new Room(internal, data, this._channel);
// Store the Room instance and remove it when closed.
this._rooms.add(room);
room.on('close', () => this._rooms.delete(room));
this._channel.request('worker.createRoom', internal)
.then(() =>
{
logger.debug('"worker.createRoom" request succeeded');
})
.catch((error) =>
{
logger.error('"worker.createRoom" request failed: %s', error);
room.close(error, true);
});
return room;
}
}
module.exports = Worker;
'use strict';
const supportedRtpCapabilities = require('./supportedRtpCapabilities');
const DYNAMIC_PAYLOAD_TYPES =
[
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 96, 97, 98, 99, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71
];
/**
* Generate RTP capabilities for the room based on the given media codecs and
* mediasoup supported RTP capabilities. It may throw.
*
* @param {array<RoomMediaCodec>]} mediaCodecs
*
* @return {RTCRtpCapabilities}
*/
exports.generateRoomRtpCapabilities = function(mediaCodecs)
{
if (!Array.isArray(mediaCodecs))
throw new TypeError('mediaCodecs must be an Array');
const supportedCodecs = supportedRtpCapabilities.codecs;
const caps =
{
codecs : [],
headerExtensions : supportedRtpCapabilities.headerExtensions,
fecMechanisms : supportedRtpCapabilities.fecMechanisms
};
let dynamicPayloadTypeIdx = 0;
for (const mediaCodec of mediaCodecs)
{
if (typeof mediaCodecs !== 'object')
throw new TypeError('mediaCodec must be an Object');
const matchedSupportedCodec = supportedCodecs
.find((supportedCodec) =>
{
return matchCodecCapabilities(mediaCodec, supportedCodec);
});
if (!matchedSupportedCodec)
throw new Error(`RoomMediaCodec not supported [name:${mediaCodec.name}]`);
// Clone the supported codec.
const codec = Object.assign({}, matchedSupportedCodec);
// Normalize channels.
if (codec.kind !== 'audio')
delete codec.channels;
else if (!codec.channels)
codec.channels = 1;
// Merge the media codec parameters.
codec.parameters =
Object.assign({}, codec.parameters, mediaCodec.parameters);
// Ensure rtcpFeedback is an Array.
codec.rtcpFeedback = codec.rtcpFeedback || [];
// Assign a payload type.
if (typeof codec.preferredPayloadType !== 'number')
{
const pt = DYNAMIC_PAYLOAD_TYPES[dynamicPayloadTypeIdx++];
if (!pt)
throw new Error('cannot allocate more dynamic codec payload types');
codec.preferredPayloadType = pt;
}
// Append to the codec list.
caps.codecs.push(codec);
// Add a RTX video codec if video.
if (codec.kind === 'video')
{
const pt = DYNAMIC_PAYLOAD_TYPES[dynamicPayloadTypeIdx++];
if (!pt)
throw new Error('cannot allocate more dynamic codec payload types');
const rtxCodec =
{
kind : codec.kind,
name : 'rtx',
mimeType : `${codec.kind}/rtx`,
preferredPayloadType : pt,
clockRate : codec.clockRate,
parameters :
{
apt : codec.preferredPayloadType
}
};
// Append to the codec list.
caps.codecs.push(rtxCodec);
}
}
console.log(caps);
return caps;
};
function matchCodecCapabilities(codecA, codecB)
{
if (codecA.kind !== codecB.kind)
return false;
if (codecA.name.toLowerCase() !== codecB.name.toLowerCase())
return false;
if (codecA.clockRate !== codecB.clockRate)
return false;
if (
(codecA.channels > 1 || codecB.channels > 1) &&
codecA.channels !== codecB.channels)
{
return false;
}
// TODO: H264 parameters.
return true;
}
......@@ -6,123 +6,134 @@ const supportedRtpCapabilities =
[
{
kind : 'audio',
name : 'audio/opus',
name : 'opus',
mimeType : 'audio/opus',
clockRate : 48000,
numChannels : 2,
rtcpFeedback : []
channels : 2
},
{
kind : 'audio',
name : 'audio/PCMU',
name : 'PCMU',
mimeType : 'audio/PCMU',
preferredPayloadType : 0,
clockRate : 8000,
rtcpFeedback : []
},
{
kind : 'audio',
name : 'audio/PCMA',
name : 'PCMA',
mimeType : 'audio/PCMA',
preferredPayloadType : 8,
clockRate : 8000,
rtcpFeedback : []
},
{
kind : 'audio',
name : 'audio/ISAC',
name : 'ISAC',
mimeType : 'audio/ISAC',
clockRate : 32000,
rtcpFeedback : []
},
{
kind : 'audio',
name : 'audio/ISAC',
clockRate : 16000,
rtcpFeedback : []
name : 'ISAC',
mimeType : 'audio/ISAC',
clockRate : 16000
},
{
kind : 'audio',
name : 'audio/G722',
clockRate : 8000,
rtcpFeedback : []
name : 'G722',
mimeType : 'audio/G722',
preferredPayloadType : 9,
clockRate : 8000
},
{
kind : 'audio',
name : 'audio/iLBC',
clockRate : 8000,
rtcpFeedback : []
name : 'iLBC',
mimeType : 'audio/iLBC',
clockRate : 8000
},
{
kind : 'audio',
name : 'audio/SILK',
clockRate : 24000,
rtcpFeedback : []
name : 'SILK',
mimeType : 'audio/SILK',
clockRate : 24000
},
{
kind : 'audio',
name : 'audio/SILK',
clockRate : 16000,
rtcpFeedback : []
name : 'SILK',
mimeType : 'audio/SILK',
clockRate : 16000
},
{
kind : 'audio',
name : 'audio/SILK',
clockRate : 12000,
rtcpFeedback : []
name : 'SILK',
mimeType : 'audio/SILK',
clockRate : 12000
},
{
kind : 'audio',
name : 'audio/SILK',
clockRate : 8000,
rtcpFeedback : []
name : 'SILK',
mimeType : 'audio/SILK',
clockRate : 8000
},
{
kind : 'audio',
name : 'audio/CN',
clockRate : 32000,
rtcpFeedback : []
name : 'CN',
mimeType : 'audio/CN',
preferredPayloadType : 13,
clockRate : 32000
},
{
kind : 'audio',
name : 'audio/CN',
clockRate : 16000,
rtcpFeedback : []
name : 'CN',
mimeType : 'audio/CN',
preferredPayloadType : 13,
clockRate : 16000
},
{
kind : 'audio',
name : 'audio/CN',
clockRate : 8000,
rtcpFeedback : []
name : 'CN',
mimeType : 'audio/CN',
preferredPayloadType : 13,
clockRate : 8000
},
{
kind : 'audio',
name : 'audio/CN',
clockRate : 32000,
rtcpFeedback : []
name : 'CN',
mimeType : 'audio/CN',
preferredPayloadType : 13,
clockRate : 32000
},
{
kind : 'audio',
name : 'audio/telephone-event',
clockRate : 48000,
rtcpFeedback : []
name : 'telephone-event',
mimeType : 'audio/telephone-event',
clockRate : 48000
},
{
kind : 'audio',
name : 'audio/telephone-event',
clockRate : 32000,
rtcpFeedback : []
name : 'telephone-event',
mimeType : 'audio/telephone-event',
clockRate : 32000
},
{
kind : 'audio',
name : 'audio/telephone-event',
clockRate : 16000,
rtcpFeedback : []
name : 'telephone-event',
mimeType : 'audio/telephone-event',
clockRate : 16000
},
{
kind : 'audio',
name : 'audio/telephone-event',
clockRate : 8000,
rtcpFeedback : []
name : 'telephone-event',
mimeType : 'audio/telephone-event',
clockRate : 8000
},
{
kind : 'video',
name : 'video/VP8',
name : 'VP8',
mimeType : 'video/VP8',
clockRate : 90000,
rtcpFeedback :
[
......@@ -139,7 +150,8 @@ const supportedRtpCapabilities =
},
{
kind : 'video',
name : 'video/VP9',
name : 'VP9',
mimeType : 'video/VP9',
clockRate : 90000,
rtcpFeedback :
[
......@@ -156,7 +168,8 @@ const supportedRtpCapabilities =
},
{
kind : 'video',
name : 'video/H264',
name : 'H264',
mimeType : 'video/H264',
clockRate : 90000,
parameters :
{
......@@ -177,7 +190,8 @@ const supportedRtpCapabilities =
},
{
kind : 'video',
name : 'video/H264',
name : 'H264',
mimeType : 'video/H264',
clockRate : 90000,
parameters :
{
......@@ -198,7 +212,8 @@ const supportedRtpCapabilities =
},
{
kind : 'video',
name : 'video/H265',
name : 'H265',
mimeType : 'video/H265',
clockRate : 90000,
rtcpFeedback :
[
......
......@@ -11,20 +11,17 @@ const randomNumberGenerator = randomNumber.generator(
integer : true
});
module.exports =
exports.randomString = function(length)
{
randomString(length)
{
return randomatic('a', length || 8);
},
};
randomNumber : randomNumberGenerator,
exports.randomNumber = randomNumberGenerator;
clone(obj)
{
if (!obj || typeof obj !== 'object')
exports.cloneObject = function(obj)
{
if (typeof obj !== 'object')
return {};
else
return clone(obj, false);
}
};
......@@ -2,139 +2,227 @@
const tap = require('tap');
const mediasoup = require('../');
const roomOptions = require('./data/options').roomOptions;
// const roomOptions = require('./data/options').roomOptions;
tap.test(
'server.updateSettings() with no options must succeed', { timeout: 2000 }, (t) =>
'server.Room() with valid media codecs', { timeout: 2000 }, (t) =>
{
const server = mediasoup.Server();
t.tearDown(() => server.close());
server.updateSettings()
.then(() =>
{
t.pass('server.updateSettings() succeeded');
t.end();
})
.catch((error) => t.fail(`server.updateSettings() failed: ${error}`));
});
tap.test(
'server.updateSettings() with valid options must succeed',
{ timeout: 2000 }, (t) =>
{
const server = mediasoup.Server();
t.tearDown(() => server.close());
server.updateSettings({ logLevel: 'warn' })
.then(() =>
{
t.pass('server.updateSettings() succeeded');
t.end();
})
.catch((error) => t.fail(`server.updateSettings() failed: ${error}`));
});
tap.test(
'server.updateSettings() with invalid options must fail', { timeout: 2000 }, (t) =>
{
const server = mediasoup.Server();
t.tearDown(() => server.close());
server.updateSettings({ logLevel: 'chicken' })
.then(() => t.fail('server.updateSettings() succeeded'))
.catch((error) =>
{
t.pass(`server.updateSettings() failed: ${error}`);
t.end();
});
});
tap.test(
'server.updateSettings() in a closed server must fail', { timeout: 2000 }, (t) =>
{
const server = mediasoup.Server();
t.tearDown(() => server.close());
server.on('close', () =>
{
server.updateSettings({ logLevel: 'error' })
.then(() => t.fail('server.updateSettings() succeeded'))
.catch((error) =>
{
t.pass(`server.updateSettings() failed: ${error}`);
t.type(
error, mediasoup.errors.InvalidStateError,
'server.updateSettings() must reject with InvalidStateError');
t.end();
});
const mediaCodecs =
[
{
kind : 'audio',
name : 'opus',
clockRate : 48000,
channels : 2
},
{
kind : 'audio',
name : 'PCMU',
clockRate : 8000
},
{
kind : 'video',
name : 'VP8',
clockRate : 90000,
parameters :
{
foo : 123
}
}
];
const expectedCodecs =
[
{
kind : 'audio',
name : 'opus',
mimeType : 'audio/opus',
preferredPayloadType : 100,
clockRate : 48000,
channels : 2,
parameters : {}
},
{
kind : 'audio',
name : 'PCMU',
mimeType : 'audio/PCMU',
preferredPayloadType : 0,
clockRate : 8000,
channels : 1,
parameters : {}
},
{
kind : 'video',
name : 'VP8',
mimeType : 'video/VP8',
preferredPayloadType : 101,
clockRate : 90000,
parameters :
{
foo : 123
}
},
{
kind : 'video',
name : 'rtx',
mimeType : 'video/rtx',
preferredPayloadType : 102,
clockRate : 90000,
parameters :
{
apt : 101
}
}
];
const room = server.Room(mediaCodecs);
const reducedCodecs = room.rtpCapabilities.codecs
.map((codec) =>
{
delete codec.rtcpFeedback;
return codec;
});
server.close();
});
tap.test(
'server.createRoom() must succeed', { _timeout: 2000 }, (t) =>
{
const server = mediasoup.Server();
t.tearDown(() => server.close());
server.createRoom(roomOptions)
.then((room) =>
{
t.pass('server.createRoom() succeeded');
room.on('close', (error) =>
{
t.error(error, 'room must close cleanly');
t.same(reducedCodecs, expectedCodecs, 'room codecs match');
t.end();
});
// Wait a bit so Channel requests receive response
setTimeout(() => room.close(), 50);
})
.catch((error) => t.fail(`server.createRoom() failed: ${error}`));
});
tap.test(
'server.createRoom() in a closed server must fail', { timeout: 2000 }, (t) =>
{
const server = mediasoup.Server();
server.close();
server.createRoom(roomOptions)
.then(() => t.fail('server.createRoom() succeeded'))
.catch((error) =>
{
t.pass(`server.createRoom() failed: ${error}`);
t.type(
error, mediasoup.errors.InvalidStateError,
'server.createRoom() must reject with InvalidStateError');
t.end();
});
});
tap.test(
'server.dump() must succeed', { timeout: 2000 }, (t) =>
{
const server = mediasoup.Server({ numWorkers: 2 });
t.tearDown(() => server.close());
server.dump()
.then((data) =>
{
t.pass('server.dump() succeeded');
t.equal(
Object.keys(data.workers).length, 2,
'server.dump() must retrieve two workers');
t.end();
})
.catch((error) => t.fail(`server.dump() failed: ${error}`));
});
// tap.test(
// 'server.updateSettings() with no options must succeed', { timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server();
// t.tearDown(() => server.close());
// server.updateSettings()
// .then(() =>
// {
// t.pass('server.updateSettings() succeeded');
// t.end();
// })
// .catch((error) => t.fail(`server.updateSettings() failed: ${error}`));
// });
// tap.test(
// 'server.updateSettings() with valid options must succeed',
// { timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server();
// t.tearDown(() => server.close());
// server.updateSettings({ logLevel: 'warn' })
// .then(() =>
// {
// t.pass('server.updateSettings() succeeded');
// t.end();
// })
// .catch((error) => t.fail(`server.updateSettings() failed: ${error}`));
// });
// tap.test(
// 'server.updateSettings() with invalid options must fail', { timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server();
// t.tearDown(() => server.close());
// server.updateSettings({ logLevel: 'chicken' })
// .then(() => t.fail('server.updateSettings() succeeded'))
// .catch((error) =>
// {
// t.pass(`server.updateSettings() failed: ${error}`);
// t.end();
// });
// });
// tap.test(
// 'server.updateSettings() in a closed server must fail', { timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server();
// t.tearDown(() => server.close());
// server.on('close', () =>
// {
// server.updateSettings({ logLevel: 'error' })
// .then(() => t.fail('server.updateSettings() succeeded'))
// .catch((error) =>
// {
// t.pass(`server.updateSettings() failed: ${error}`);
// t.type(
// error, mediasoup.errors.InvalidStateError,
// 'server.updateSettings() must reject with InvalidStateError');
// t.end();
// });
// });
// server.close();
// });
// tap.test(
// 'server.createRoom() must succeed', { _timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server();
// t.tearDown(() => server.close());
// server.createRoom(roomOptions)
// .then((room) =>
// {
// t.pass('server.createRoom() succeeded');
// room.on('close', (error) =>
// {
// t.error(error, 'room must close cleanly');
// t.end();
// });
// // Wait a bit so Channel requests receive response
// setTimeout(() => room.close(), 50);
// })
// .catch((error) => t.fail(`server.createRoom() failed: ${error}`));
// });
// tap.test(
// 'server.createRoom() in a closed server must fail', { timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server();
// server.close();
// server.createRoom(roomOptions)
// .then(() => t.fail('server.createRoom() succeeded'))
// .catch((error) =>
// {
// t.pass(`server.createRoom() failed: ${error}`);
// t.type(
// error, mediasoup.errors.InvalidStateError,
// 'server.createRoom() must reject with InvalidStateError');
// t.end();
// });
// });
// tap.test(
// 'server.dump() must succeed', { timeout: 2000 }, (t) =>
// {
// const server = mediasoup.Server({ numWorkers: 2 });
// t.tearDown(() => server.close());
// server.dump()
// .then((data) =>
// {
// t.pass('server.dump() succeeded');
// t.equal(
// Object.keys(data.workers).length, 2,
// 'server.dump() must retrieve two workers');
// t.end();
// })
// .catch((error) => t.fail(`server.dump() failed: ${error}`));
// });
......@@ -30,13 +30,6 @@ namespace RTC
virtual void OnRoomClosed(RTC::Room* room) = 0;
};
public:
static void ClassInit();
private:
static RTC::RtpCapabilities supportedRtpCapabilities;
static std::vector<uint8_t> availablePayloadTypes;
public:
Room(Listener* listener, Channel::Notifier* notifier, uint32_t roomId, Json::Value& data);
......
......@@ -11,13 +11,6 @@
namespace RTC
{
enum class Scope : uint8_t
{
ROOM_CAPABILITY = 1,
PEER_CAPABILITY,
RECEIVE
};
class Media
{
public:
......@@ -38,7 +31,7 @@ namespace RTC
static std::map<Kind, Json::StaticString> kind2Json;
};
class RtpCodecMime
class RtpCodecMimeType
{
public:
enum class Type : uint8_t
......@@ -84,19 +77,24 @@ namespace RTC
static std::map<Subtype, std::string> subtype2String;
public:
RtpCodecMime(){};
RtpCodecMimeType(){};
bool operator==(const RtpCodecMime& other) const
bool operator==(const RtpCodecMimeType& other) const
{
return this->type == other.type && this->subtype == other.subtype;
}
bool operator!=(const RtpCodecMime& other) const
bool operator!=(const RtpCodecMimeType& other) const
{
return !(*this == other);
}
void SetName(const std::string& name);
void SetMimeType(const std::string& mimeType);
const std::string& ToString() const
{
return this->mimeType;
}
const std::string& GetName() const
{
......@@ -121,6 +119,9 @@ namespace RTC
public:
Type type{ Type::UNSET };
Subtype subtype{ Subtype::UNSET };
private:
std::string mimeType;
std::string name;
};
......@@ -156,31 +157,27 @@ namespace RTC
std::string parameter;
};
class RtpCodecParameters
class RtpCodecCapability
{
public:
RtpCodecParameters(){};
RtpCodecParameters(Json::Value& data, RTC::Scope scope);
RtpCodecCapability(){};
RtpCodecCapability(Json::Value& data);
Json::Value ToJson() const;
bool Matches(RtpCodecParameters& codec, bool checkPayloadType = false);
bool Matches(RtpCodecCapability& codec, bool checkPayloadType = false);
void ReduceRtcpFeedback(std::vector<RtcpFeedback>& supportedRtcpFeedback);
private:
void CheckCodec();
private:
RTC::Scope scope{ RTC::Scope::ROOM_CAPABILITY };
public:
Media::Kind kind{ Media::Kind::ALL };
RtpCodecMime mime;
RtpCodecMimeType mime;
uint8_t payloadType{ 0 };
bool hasPayloadType{ false };
uint32_t clockRate{ 0 };
uint32_t maxptime{ 0 };
uint32_t ptime{ 0 };
uint32_t numChannels{ 1 };
uint32_t channels{ 1 };
RTC::Parameters parameters;
std::vector<RtcpFeedback> rtcpFeedback;
};
......@@ -204,21 +201,46 @@ namespace RTC
{
public:
RtpCapabilities(){};
RtpCapabilities(Json::Value& data, RTC::Scope scope);
RtpCapabilities(Json::Value& data);
Json::Value ToJson() const;
void ReduceHeaderExtensions(std::vector<RtpHeaderExtension>& supportedHeaderExtensions);
void ReduceFecMechanisms(std::vector<std::string>& supportedFecMechanisms);
private:
void ValidateCodecs(RTC::Scope scope);
void ValidateCodecs();
public:
std::vector<RtpCodecParameters> codecs;
std::vector<RtpCodecCapability> codecs;
std::vector<RtpHeaderExtension> headerExtensions;
std::vector<std::string> fecMechanisms;
};
class RtpCodecParameters
{
public:
RtpCodecParameters(){};
RtpCodecParameters(Json::Value& data);
Json::Value ToJson() const;
bool Matches(RtpCodecParameters& codec, bool checkPayloadType = false);
void ReduceRtcpFeedback(std::vector<RtcpFeedback>& supportedRtcpFeedback);
private:
void CheckCodec();
public:
Media::Kind kind{ Media::Kind::ALL };
RtpCodecMimeType mime;
uint8_t payloadType{ 0 };
uint32_t clockRate{ 0 };
uint32_t maxptime{ 0 };
uint32_t ptime{ 0 };
uint32_t channels{ 1 };
RTC::Parameters parameters;
std::vector<RtcpFeedback> rtcpFeedback;
};
class RtpFecParameters
{
public:
......
......@@ -17,7 +17,7 @@ namespace RTC
uint32_t ssrc{ 0 };
uint8_t payloadType{ 0 };
RTC::RtpCodecMime mime;
RTC::RtpCodecMimeType mime;
uint32_t clockRate{ 0 };
bool useNack{ false };
bool usePli{ false };
......
......@@ -46,8 +46,7 @@
'src/RTC/RtpDictionaries/Parameters.cpp',
'src/RTC/RtpDictionaries/RtcpFeedback.cpp',
'src/RTC/RtpDictionaries/RtcpParameters.cpp',
'src/RTC/RtpDictionaries/RtpCapabilities.cpp',
'src/RTC/RtpDictionaries/RtpCodecMime.cpp',
'src/RTC/RtpDictionaries/RtpCodecMimeType.cpp',
'src/RTC/RtpDictionaries/RtpCodecParameters.cpp',
'src/RTC/RtpDictionaries/RtpEncodingParameters.cpp',
'src/RTC/RtpDictionaries/RtpFecParameters.cpp',
......
......@@ -469,12 +469,9 @@ namespace RTC
{
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
if (codec.hasPayloadType)
{
this->rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
}
}
}
void Consumer::RetransmitRtpPacket(RTC::RtpPacket* packet)
{
......
......@@ -179,7 +179,7 @@ namespace RTC
try
{
this->capabilities = RTC::RtpCapabilities(request->data, RTC::Scope::PEER_CAPABILITY);
this->capabilities = RTC::RtpCapabilities(request->data);
}
catch (const MediaSoupError& error)
{
......
......@@ -466,16 +466,12 @@ namespace RTC
return;
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
if (codec.hasPayloadType)
{
auto rtpStream = this->rtpStreams[ssrc];
rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
this->rtxStreamMap[encoding.rtx.ssrc] = rtpStream;
}
}
}
void Producer::ClearRtpStreams()
{
......
......@@ -30,7 +30,7 @@ namespace RTC
{
// NOTE: These lines are auto-generated from data/supportedCapabilities.js.
const std::string supportedRtpCapabilities =
R"({"codecs":[{"kind":"audio","name":"audio/opus","clockRate":48000,"numChannels":2,"rtcpFeedback":[]},{"kind":"audio","name":"audio/PCMU","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/PCMA","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/ISAC","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/ISAC","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/G722","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/iLBC","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/SILK","clockRate":24000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/SILK","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/SILK","clockRate":12000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/SILK","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/CN","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/CN","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/CN","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/CN","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/telephone-event","clockRate":48000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/telephone-event","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/telephone-event","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"audio/telephone-event","clockRate":8000,"rtcpFeedback":[]},{"kind":"video","name":"video/VP8","clockRate":90000,"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"video/VP9","clockRate":90000,"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"video/H264","clockRate":90000,"parameters":{"packetizationMode":0},"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"video/H264","clockRate":90000,"parameters":{"packetizationMode":1},"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"video/H265","clockRate":90000,"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]}],"headerExtensions":[{"kind":"audio","uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","preferredId":1,"preferredEncrypt":false},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:toffset","preferredId":2,"preferredEncrypt":false},{"kind":"","uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","preferredId":3,"preferredEncrypt":false},{"kind":"video","uri":"urn:3gpp:video-orientation","preferredId":4,"preferredEncrypt":false},{"kind":"","uri":"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id","preferredId":5,"preferredEncrypt":false}],"fecMechanisms":[]})";
R"({"codecs":[{"kind":"audio","name":"opus","mimeType":"audio/opus","clockRate":48000,"channels":2,"rtcpFeedback":[]},{"kind":"audio","name":"PCMU","mimeType":"audio/PCMU","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"PCMA","mimeType":"audio/PCMA","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"ISAC","mimeType":"audio/ISAC","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"ISAC","mimeType":"audio/ISAC","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"G722","mimeType":"audio/G722","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"LBC","mimeType":"audio/iLBC","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"SILK","mimeType":"audio/SILK","clockRate":24000,"rtcpFeedback":[]},{"kind":"audio","name":"SILK","mimeType":"audio/SILK","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"SILK","mimeType":"audio/SILK","clockRate":12000,"rtcpFeedback":[]},{"kind":"audio","name":"SILK","mimeType":"audio/SILK","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"CN","mimeType":"audio/CN","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"CN","mimeType":"audio/CN","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"CN","mimeType":"audio/CN","clockRate":8000,"rtcpFeedback":[]},{"kind":"audio","name":"CN","mimeType":"audio/CN","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"telephone-event","mimeType":"audio/telephone-event","clockRate":48000,"rtcpFeedback":[]},{"kind":"audio","name":"telephone-event","mimeType":"audio/telephone-event","clockRate":32000,"rtcpFeedback":[]},{"kind":"audio","name":"telephone-event","mimeType":"audio/telephone-event","clockRate":16000,"rtcpFeedback":[]},{"kind":"audio","name":"telephone-event","mimeType":"audio/telephone-event","clockRate":8000,"rtcpFeedback":[]},{"kind":"video","name":"VP8","mimeType":"video/VP8","clockRate":90000,"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"VP9","mimeType":"video/VP9","clockRate":90000,"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"H264","mimeType":"video/H264","clockRate":90000,"parameters":{"packetizationMode":0},"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"H264","mimeType":"video/H264","clockRate":90000,"parameters":{"packetizationMode":1},"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]},{"kind":"video","name":"H265","mimeType":"video/H265","clockRate":90000,"rtcpFeedback":[{"type":"nack"},{"type":"nack","parameter":"pli"},{"type":"nack","parameter":"sli"},{"type":"nack","parameter":"rpsi"},{"type":"nack","parameter":"app"},{"type":"ccm","parameter":"fir"},{"type":"ack","parameter":"rpsi"},{"type":"ack","parameter":"app"},{"type":"goog-remb"}]}],"headerExtensions":[{"kind":"audio","uri":"urn:ietf:params:rtp-hdrext:ssrc-audio-level","preferredId":1,"preferredEncrypt":false},{"kind":"video","uri":"urn:ietf:params:rtp-hdrext:toffset","preferredId":2,"preferredEncrypt":false},{"kind":"","uri":"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time","preferredId":3,"preferredEncrypt":false},{"kind":"video","uri":"urn:3gpp:video-orientation","preferredId":4,"preferredEncrypt":false},{"kind":"","uri":"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id","preferredId":5,"preferredEncrypt":false}],"fecMechanisms":[]})";
Json::CharReaderBuilder builder;
Json::Value settings = Json::nullValue;
......@@ -59,7 +59,7 @@ namespace RTC
try
{
Room::supportedRtpCapabilities = RTC::RtpCapabilities(json, RTC::Scope::ROOM_CAPABILITY);
Room::supportedRtpCapabilities = RTC::RtpCapabilities(json);
}
catch (const MediaSoupError& error)
{
......@@ -85,7 +85,7 @@ namespace RTC
for (auto& jsonMediaCodec : jsonMediaCodecs)
{
RTC::RtpCodecParameters mediaCodec(jsonMediaCodec, RTC::Scope::ROOM_CAPABILITY);
RTC::RtpCodecParameters mediaCodec(jsonMediaCodec);
// Ignore feature codecs.
if (mediaCodec.mime.IsFeatureCodec())
......@@ -507,7 +507,7 @@ namespace RTC
static const std::string videoRtx = "video/rtx";
rtxCodec.kind = RTC::Media::Kind::VIDEO;
rtxCodec.mime.SetName(videoRtx);
rtxCodec.mime.SetMimeType(videoRtx);
rtxCodec.clockRate = mediaCodec.clockRate;
rtxCodec.parameters.SetInteger(associatedPayloadType, mediaCodec.payloadType);
......
......@@ -10,7 +10,7 @@ namespace RTC
{
/* Instance methods. */
RtpCapabilities::RtpCapabilities(Json::Value& data, RTC::Scope scope)
RtpCapabilities::RtpCapabilities(Json::Value& data)
{
MS_TRACE();
......@@ -25,7 +25,7 @@ namespace RTC
for (auto& jsonCodec : jsonCodecs)
{
RtpCodecParameters codec(jsonCodec, scope);
RtpCodecCapability codec(jsonCodec);
// Append to the codecs vector.
this->codecs.push_back(codec);
......@@ -64,7 +64,7 @@ namespace RTC
// Validate RTP capabilities.
ValidateCodecs(scope);
ValidateCodecs();
}
Json::Value RtpCapabilities::ToJson() const
......@@ -155,12 +155,10 @@ namespace RTC
this->fecMechanisms = updatedFecMechanisms;
}
inline void RtpCapabilities::ValidateCodecs(RTC::Scope scope)
inline void RtpCapabilities::ValidateCodecs()
{
MS_TRACE();
if (scope == RTC::Scope::PEER_CAPABILITY)
{
// Payload types must be unique.
std::unordered_set<uint8_t> payloadTypes;
......@@ -173,5 +171,4 @@ namespace RTC
payloadTypes.insert(codec.payloadType);
}
}
}
} // namespace RTC
#define MS_CLASS "RTC::RtpCodecCapability"
// #define MS_LOG_DEV
#include "Logger.hpp"
#include "MediaSoupError.hpp"
#include "RTC/RtpDictionaries.hpp"
namespace RTC
{
/* Instance methods. */
RtpCodecCapability::RtpCodecCapability(Json::Value& data)
{
MS_TRACE();
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringMimeType{ "mimeType" };
static const Json::StaticString JsonStringPayloadType{ "payloadType" };
static const Json::StaticString JsonStringClockRate{ "clockRate" };
static const Json::StaticString JsonStringMaxptime{ "maxptime" };
static const Json::StaticString JsonStringPtime{ "ptime" };
static const Json::StaticString JsonStringNumChannels{ "channels" };
static const Json::StaticString JsonStringParameters{ "parameters" };
static const Json::StaticString JsonStringRtcpFeedback{ "rtcpFeedback" };
if (!data.isObject())
MS_THROW_ERROR("RtpCodecCapability is not an object");
// `kind` is mandatory.
if (!data[JsonStringKind].isString())
MS_THROW_ERROR("missing RtpCodecCapability.kind");
std::string kind = data[JsonStringKind].asString();
// NOTE: This may throw.
this->kind = RTC::Media::GetKind(kind);
// `mimeType` is mandatory.
if (!data[JsonStringMimeType].isString())
MS_THROW_ERROR("missing RtpCodec.mimeType");
std::string mimeType = data[JsonStringMimeType].asString();
// Set MIME field.
// NOTE: This may throw.
this->mime.SetMimeType(mimeType);
if (!data[JsonStringPayloadType].isUInt())
MS_THROW_ERROR("missing RtpCodecCapability.payloadType");
this->payloadType = static_cast<uint8_t>(data[JsonStringPayloadType].asUInt());
// `clockRate` is mandatory.
if (!data[JsonStringClockRate].isUInt())
MS_THROW_ERROR("missing RtpCodecCapability.clockRate");
this->clockRate = uint32_t{ data[JsonStringClockRate].asUInt() };
// `maxptime` is optional.
if (data[JsonStringMaxptime].isUInt())
this->maxptime = uint32_t{ data[JsonStringMaxptime].asUInt() };
// `ptime` is optional.
if (data[JsonStringPtime].isUInt())
this->ptime = uint32_t{ data[JsonStringPtime].asUInt() };
// `channels` is optional.
if (data[JsonStringNumChannels].isUInt())
this->channels = uint32_t{ data[JsonStringNumChannels].asUInt() };
// `parameters` is optional.
if (data[JsonStringParameters].isObject())
this->parameters.Set(data[JsonStringParameters]);
// `rtcpFeedback` is optional.
if (data[JsonStringRtcpFeedback].isArray())
{
auto& jsonRtcpFeedback = data[JsonStringRtcpFeedback];
for (auto& i : jsonRtcpFeedback)
{
RTC::RtcpFeedback rtcpFeedback(i);
// Append to the rtcpFeedback vector.
this->rtcpFeedback.push_back(rtcpFeedback);
}
}
// Check codec.
CheckCodec();
}
Json::Value RtpCodecCapability::ToJson() const
{
MS_TRACE();
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringMimeType{ "mimeType" };
static const Json::StaticString JsonStringName{ "name" };
static const Json::StaticString JsonStringPayloadType{ "payloadType" };
static const Json::StaticString JsonStringClockRate{ "clockRate" };
static const Json::StaticString JsonStringMaxptime{ "maxptime" };
static const Json::StaticString JsonStringPtime{ "ptime" };
static const Json::StaticString JsonStringNumChannels{ "channels" };
static const Json::StaticString JsonStringParameters{ "parameters" };
static const Json::StaticString JsonStringRtcpFeedback{ "rtcpFeedback" };
Json::Value json(Json::objectValue);
// Add `kind`.
json[JsonStringKind] = RTC::Media::GetJsonString(this->kind);
// Add `mimeType`.
json[JsonStringMimeType] = this->mime.ToString();
// Add `name`.
json[JsonStringName] = this->mime.GetName();
// Add `payloadType`.
json[JsonStringPayloadType] = Json::UInt{ this->payloadType };
// Add `clockRate`.
json[JsonStringClockRate] = Json::UInt{ this->clockRate };
// Add `maxptime`.
if (this->maxptime != 0u)
json[JsonStringMaxptime] = Json::UInt{ this->maxptime };
// Add `ptime`.
if (this->ptime != 0u)
json[JsonStringPtime] = Json::UInt{ this->ptime };
// Add `channels`.
if (this->channels > 1)
json[JsonStringNumChannels] = Json::UInt{ this->channels };
// Add `parameters`.
json[JsonStringParameters] = this->parameters.ToJson();
// Add `rtcpFeedback`.
json[JsonStringRtcpFeedback] = Json::arrayValue;
for (auto& entry : this->rtcpFeedback)
{
json[JsonStringRtcpFeedback].append(entry.ToJson());
}
return json;
}
bool RtpCodecCapability::Matches(RtpCodecCapability& codec, bool checkPayloadType)
{
MS_TRACE();
static std::string jsonStringPacketizationMode{ "packetizationMode" };
// MIME must match.
if (this->mime != codec.mime)
return false;
if (checkPayloadType)
{
if (this->payloadType != codec.payloadType)
return false;
}
// Clock rate must match.
if (this->clockRate != codec.clockRate)
return false;
// Per kind checks.
switch (this->kind)
{
case RTC::Media::Kind::AUDIO:
{
// Num channels must match.
if (this->channels != codec.channels)
return false;
break;
}
default:;
}
// Per MIME checks.
switch (this->mime.subtype)
{
case RTC::RtpCodecMimeType::Subtype::H264:
{
int32_t packetizationMode = this->parameters.GetInteger(jsonStringPacketizationMode);
int32_t givenPacketizationMode = codec.parameters.GetInteger(jsonStringPacketizationMode);
if (packetizationMode != givenPacketizationMode)
return false;
break;
}
default:;
}
return true;
}
void RtpCodecCapability::ReduceRtcpFeedback(std::vector<RTC::RtcpFeedback>& supportedRtcpFeedback)
{
MS_TRACE();
std::vector<RTC::RtcpFeedback> updatedRtcpFeedback;
for (auto& rtcpFeedbackItem : this->rtcpFeedback)
{
for (auto& supportedRtcpFeedbackItem : supportedRtcpFeedback)
{
if (rtcpFeedbackItem.type == supportedRtcpFeedbackItem.type &&
rtcpFeedbackItem.parameter == supportedRtcpFeedbackItem.parameter)
{
updatedRtcpFeedback.push_back(supportedRtcpFeedbackItem);
break;
}
}
}
this->rtcpFeedback = updatedRtcpFeedback;
}
inline void RtpCodecCapability::CheckCodec()
{
MS_TRACE();
static std::string jsonStringApt{ "apt" };
static std::string jsonStringPacketizationMode{ "packetizationMode" };
// Check per MIME parameters and set default values.
switch (this->mime.subtype)
{
case RTC::RtpCodecMimeType::Subtype::RTX:
{
// A RTX codec must have 'apt' parameter.
if (!this->parameters.HasInteger(jsonStringApt))
MS_THROW_ERROR("missing apt parameter in RTX RtpCodecCapability");
break;
}
case RTC::RtpCodecMimeType::Subtype::OPUS:
{
// Opus default channels is 2.
if (this->channels < 2)
this->channels = 2;
break;
}
case RTC::RtpCodecMimeType::Subtype::H264:
{
// H264 default packetizationMode is 0.
if (!this->parameters.HasInteger(jsonStringPacketizationMode))
this->parameters.SetInteger(jsonStringPacketizationMode, 0);
break;
}
default:;
}
}
} // namespace RTC
#define MS_CLASS "RTC::RtpCodecMime"
// #define MS_LOG_DEV
#include "Logger.hpp"
#include "MediaSoupError.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp"
namespace RTC
{
/* Class variables. */
// clang-format off
std::unordered_map<std::string, RtpCodecMime::Type> RtpCodecMime::string2Type =
{
{ "audio", RtpCodecMime::Type::AUDIO },
{ "video", RtpCodecMime::Type::VIDEO }
};
std::map<RtpCodecMime::Type, std::string> RtpCodecMime::type2String =
{
{ RtpCodecMime::Type::AUDIO, "audio" },
{ RtpCodecMime::Type::VIDEO, "video" }
};
std::unordered_map<std::string, RtpCodecMime::Subtype> RtpCodecMime::string2Subtype =
{
// Audio codecs:
{ "opus", RtpCodecMime::Subtype::OPUS },
{ "pcma", RtpCodecMime::Subtype::PCMA },
{ "pcmu", RtpCodecMime::Subtype::PCMU },
{ "isac", RtpCodecMime::Subtype::ISAC },
{ "g722", RtpCodecMime::Subtype::G722 },
{ "ilbc", RtpCodecMime::Subtype::ILBC },
{ "silk", RtpCodecMime::Subtype::SILK },
// Video codecs:
{ "vp8", RtpCodecMime::Subtype::VP8 },
{ "vp9", RtpCodecMime::Subtype::VP9 },
{ "h264", RtpCodecMime::Subtype::H264 },
{ "x-h264uc", RtpCodecMime::Subtype::X_H264UC },
{ "h265", RtpCodecMime::Subtype::H265 },
// Complementary codecs:
{ "cn", RtpCodecMime::Subtype::CN },
{ "telephone-event", RtpCodecMime::Subtype::TELEPHONE_EVENT },
// Feature codecs:
{ "rtx", RtpCodecMime::Subtype::RTX },
{ "ulpfec", RtpCodecMime::Subtype::ULPFEC },
{ "flexfec", RtpCodecMime::Subtype::FLEXFEC },
{ "x-ulpfecuc", RtpCodecMime::Subtype::X_ULPFECUC },
{ "red", RtpCodecMime::Subtype::RED }
};
std::map<RtpCodecMime::Subtype, std::string> RtpCodecMime::subtype2String =
{
// Audio codecs:
{ RtpCodecMime::Subtype::OPUS, "opus" },
{ RtpCodecMime::Subtype::PCMA, "PCMA" },
{ RtpCodecMime::Subtype::PCMU, "PCMU" },
{ RtpCodecMime::Subtype::ISAC, "ISAC" },
{ RtpCodecMime::Subtype::G722, "G722" },
{ RtpCodecMime::Subtype::ILBC, "iLBC" },
{ RtpCodecMime::Subtype::SILK, "SILK" },
// Video codecs:
{ RtpCodecMime::Subtype::VP8, "VP8" },
{ RtpCodecMime::Subtype::VP9, "VP9" },
{ RtpCodecMime::Subtype::H264, "H264" },
{ RtpCodecMime::Subtype::X_H264UC, "X-H264UC" },
{ RtpCodecMime::Subtype::H265, "H265" },
// Complementary codecs:
{ RtpCodecMime::Subtype::CN, "CN" },
{ RtpCodecMime::Subtype::TELEPHONE_EVENT, "telephone-event" },
// Feature codecs:
{ RtpCodecMime::Subtype::RTX, "rtx" },
{ RtpCodecMime::Subtype::ULPFEC, "ulpfec" },
{ RtpCodecMime::Subtype::FLEXFEC, "flexfec" },
{ RtpCodecMime::Subtype::X_ULPFECUC, "x-ulpfecuc" },
{ RtpCodecMime::Subtype::RED, "red" }
};
// clang-format on
/* Instance methods. */
void RtpCodecMime::SetName(const std::string& name)
{
MS_TRACE();
auto slashPos = name.find('/');
if (slashPos == std::string::npos || slashPos == 0 || slashPos == name.length() - 1)
MS_THROW_ERROR("wrong codec MIME");
std::string type = name.substr(0, slashPos);
std::string subtype = name.substr(slashPos + 1);
// Force lowcase names.
Utils::String::ToLowerCase(type);
Utils::String::ToLowerCase(subtype);
// Set MIME type.
{
auto it = RtpCodecMime::string2Type.find(type);
if (it != RtpCodecMime::string2Type.end())
this->type = it->second;
else
MS_THROW_ERROR("unknown codec MIME type '%s'", type.c_str());
}
// Set MIME subtype.
{
auto it = RtpCodecMime::string2Subtype.find(subtype);
if (it != RtpCodecMime::string2Subtype.end())
this->subtype = it->second;
else
MS_THROW_ERROR("unknown codec MIME subtype '%s'", subtype.c_str());
}
// Set name.
this->name = type2String[this->type] + "/" + subtype2String[this->subtype];
}
} // namespace RTC
#define MS_CLASS "RTC::RtpCodecMimeType"
// #define MS_LOG_DEV
#include "Logger.hpp"
#include "MediaSoupError.hpp"
#include "Utils.hpp"
#include "RTC/RtpDictionaries.hpp"
namespace RTC
{
/* Class variables. */
// clang-format off
std::unordered_map<std::string, RtpCodecMimeType::Type> RtpCodecMimeType::string2Type =
{
{ "audio", RtpCodecMimeType::Type::AUDIO },
{ "video", RtpCodecMimeType::Type::VIDEO }
};
std::map<RtpCodecMimeType::Type, std::string> RtpCodecMimeType::type2String =
{
{ RtpCodecMimeType::Type::AUDIO, "audio" },
{ RtpCodecMimeType::Type::VIDEO, "video" }
};
std::unordered_map<std::string, RtpCodecMimeType::Subtype> RtpCodecMimeType::string2Subtype =
{
// Audio codecs:
{ "opus", RtpCodecMimeType::Subtype::OPUS },
{ "pcma", RtpCodecMimeType::Subtype::PCMA },
{ "pcmu", RtpCodecMimeType::Subtype::PCMU },
{ "isac", RtpCodecMimeType::Subtype::ISAC },
{ "g722", RtpCodecMimeType::Subtype::G722 },
{ "ilbc", RtpCodecMimeType::Subtype::ILBC },
{ "silk", RtpCodecMimeType::Subtype::SILK },
// Video codecs:
{ "vp8", RtpCodecMimeType::Subtype::VP8 },
{ "vp9", RtpCodecMimeType::Subtype::VP9 },
{ "h264", RtpCodecMimeType::Subtype::H264 },
{ "x-h264uc", RtpCodecMimeType::Subtype::X_H264UC },
{ "h265", RtpCodecMimeType::Subtype::H265 },
// Complementary codecs:
{ "cn", RtpCodecMimeType::Subtype::CN },
{ "telephone-event", RtpCodecMimeType::Subtype::TELEPHONE_EVENT },
// Feature codecs:
{ "rtx", RtpCodecMimeType::Subtype::RTX },
{ "ulpfec", RtpCodecMimeType::Subtype::ULPFEC },
{ "flexfec", RtpCodecMimeType::Subtype::FLEXFEC },
{ "x-ulpfecuc", RtpCodecMimeType::Subtype::X_ULPFECUC },
{ "red", RtpCodecMimeType::Subtype::RED }
};
std::map<RtpCodecMimeType::Subtype, std::string> RtpCodecMimeType::subtype2String =
{
// Audio codecs:
{ RtpCodecMimeType::Subtype::OPUS, "opus" },
{ RtpCodecMimeType::Subtype::PCMA, "PCMA" },
{ RtpCodecMimeType::Subtype::PCMU, "PCMU" },
{ RtpCodecMimeType::Subtype::ISAC, "ISAC" },
{ RtpCodecMimeType::Subtype::G722, "G722" },
{ RtpCodecMimeType::Subtype::ILBC, "iLBC" },
{ RtpCodecMimeType::Subtype::SILK, "SILK" },
// Video codecs:
{ RtpCodecMimeType::Subtype::VP8, "VP8" },
{ RtpCodecMimeType::Subtype::VP9, "VP9" },
{ RtpCodecMimeType::Subtype::H264, "H264" },
{ RtpCodecMimeType::Subtype::X_H264UC, "X-H264UC" },
{ RtpCodecMimeType::Subtype::H265, "H265" },
// Complementary codecs:
{ RtpCodecMimeType::Subtype::CN, "CN" },
{ RtpCodecMimeType::Subtype::TELEPHONE_EVENT, "telephone-event" },
// Feature codecs:
{ RtpCodecMimeType::Subtype::RTX, "rtx" },
{ RtpCodecMimeType::Subtype::ULPFEC, "ulpfec" },
{ RtpCodecMimeType::Subtype::FLEXFEC, "flexfec" },
{ RtpCodecMimeType::Subtype::X_ULPFECUC, "x-ulpfecuc" },
{ RtpCodecMimeType::Subtype::RED, "red" }
};
// clang-format on
/* Instance methods. */
void RtpCodecMimeType::SetMimeType(const std::string& mimeType)
{
MS_TRACE();
auto slashPos = mimeType.find('/');
if (
slashPos == std::string::npos ||
slashPos == 0 || slashPos == mimeType.length() - 1)
{
MS_THROW_ERROR("wrong codec MIME");
}
std::string type = mimeType.substr(0, slashPos);
std::string subtype = mimeType.substr(slashPos + 1);
// Force lowcase names.
Utils::String::ToLowerCase(type);
Utils::String::ToLowerCase(subtype);
// Set MIME type.
{
auto it = RtpCodecMimeType::string2Type.find(type);
if (it != RtpCodecMimeType::string2Type.end())
this->type = it->second;
else
MS_THROW_ERROR("unknown codec MIME type '%s'", type.c_str());
}
// Set MIME subtype.
{
auto it = RtpCodecMimeType::string2Subtype.find(subtype);
if (it != RtpCodecMimeType::string2Subtype.end())
this->subtype = it->second;
else
MS_THROW_ERROR("unknown codec MIME subtype '%s'", subtype.c_str());
}
// Set mimeType and name.
this->mimeType = type2String[this->type] + "/" + subtype2String[this->subtype];
this->name = subtype;
}
} // namespace RTC
......@@ -9,25 +9,23 @@ namespace RTC
{
/* Instance methods. */
RtpCodecParameters::RtpCodecParameters(Json::Value& data, RTC::Scope scope) : scope(scope)
RtpCodecParameters::RtpCodecParameters(Json::Value& data)
{
MS_TRACE();
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringName{ "name" };
static const Json::StaticString JsonStringMimeType{ "mimeType" };
static const Json::StaticString JsonStringPayloadType{ "payloadType" };
static const Json::StaticString JsonStringClockRate{ "clockRate" };
static const Json::StaticString JsonStringMaxptime{ "maxptime" };
static const Json::StaticString JsonStringPtime{ "ptime" };
static const Json::StaticString JsonStringNumChannels{ "numChannels" };
static const Json::StaticString JsonStringNumChannels{ "channels" };
static const Json::StaticString JsonStringParameters{ "parameters" };
static const Json::StaticString JsonStringRtcpFeedback{ "rtcpFeedback" };
if (!data.isObject())
MS_THROW_ERROR("RtpCodecParameters is not an object");
if (this->scope == RTC::Scope::ROOM_CAPABILITY || this->scope == RTC::Scope::PEER_CAPABILITY)
{
// `kind` is mandatory.
if (!data[JsonStringKind].isString())
MS_THROW_ERROR("missing RtpCodecParameters.kind");
......@@ -36,38 +34,21 @@ namespace RTC
// NOTE: This may throw.
this->kind = RTC::Media::GetKind(kind);
}
// `name` is mandatory.
if (!data[JsonStringName].isString())
MS_THROW_ERROR("missing RtpCodec.name");
// `mimeType` is mandatory.
if (!data[JsonStringMimeType].isString())
MS_THROW_ERROR("missing RtpCodec.mimeType");
std::string name = data[JsonStringName].asString();
std::string mimeType = data[JsonStringMimeType].asString();
// Set MIME field.
// NOTE: This may throw.
this->mime.SetName(name);
if (data[JsonStringPayloadType].isUInt())
{
this->payloadType = static_cast<uint8_t>(data[JsonStringPayloadType].asUInt());
this->hasPayloadType = true;
}
if (this->scope == RTC::Scope::PEER_CAPABILITY)
{
if (!this->hasPayloadType)
MS_THROW_ERROR("missing RtpCodecParameters.payloadType");
}
this->mime.SetMimeType(mimeType);
if (this->scope == RTC::Scope::RECEIVE)
{
if (!data[JsonStringPayloadType].isUInt())
MS_THROW_ERROR("missing RtpCodecParameters.payloadType");
this->payloadType = static_cast<uint8_t>(data[JsonStringPayloadType].asUInt());
this->hasPayloadType = true;
}
// `clockRate` is mandatory.
if (!data[JsonStringClockRate].isUInt())
......@@ -83,9 +64,9 @@ namespace RTC
if (data[JsonStringPtime].isUInt())
this->ptime = uint32_t{ data[JsonStringPtime].asUInt() };
// `numChannels` is optional.
// `channels` is optional.
if (data[JsonStringNumChannels].isUInt())
this->numChannels = uint32_t{ data[JsonStringNumChannels].asUInt() };
this->channels = uint32_t{ data[JsonStringNumChannels].asUInt() };
// `parameters` is optional.
if (data[JsonStringParameters].isObject())
......@@ -114,31 +95,29 @@ namespace RTC
MS_TRACE();
static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringMimeType{ "mimeType" };
static const Json::StaticString JsonStringName{ "name" };
static const Json::StaticString JsonStringPayloadType{ "payloadType" };
static const Json::StaticString JsonStringClockRate{ "clockRate" };
static const Json::StaticString JsonStringMaxptime{ "maxptime" };
static const Json::StaticString JsonStringPtime{ "ptime" };
static const Json::StaticString JsonStringNumChannels{ "numChannels" };
static const Json::StaticString JsonStringNumChannels{ "channels" };
static const Json::StaticString JsonStringParameters{ "parameters" };
static const Json::StaticString JsonStringRtcpFeedback{ "rtcpFeedback" };
Json::Value json(Json::objectValue);
if (this->scope == RTC::Scope::ROOM_CAPABILITY || this->scope == RTC::Scope::PEER_CAPABILITY)
{
// Add `kind`.
json[JsonStringKind] = RTC::Media::GetJsonString(this->kind);
}
// Add `mimeType`.
json[JsonStringMimeType] = this->mime.ToString();
// Add `name`.
json[JsonStringName] = this->mime.GetName();
if (this->hasPayloadType)
{
// Add `payloadType`.
json[JsonStringPayloadType] = Json::UInt{ this->payloadType };
}
// Add `clockRate`.
json[JsonStringClockRate] = Json::UInt{ this->clockRate };
......@@ -151,9 +130,9 @@ namespace RTC
if (this->ptime != 0u)
json[JsonStringPtime] = Json::UInt{ this->ptime };
// Add `numChannels`.
if (this->numChannels > 1)
json[JsonStringNumChannels] = Json::UInt{ this->numChannels };
// Add `channels`.
if (this->channels > 1)
json[JsonStringNumChannels] = Json::UInt{ this->channels };
// Add `parameters`.
json[JsonStringParameters] = this->parameters.ToJson();
......@@ -195,7 +174,7 @@ namespace RTC
case RTC::Media::Kind::AUDIO:
{
// Num channels must match.
if (this->numChannels != codec.numChannels)
if (this->channels != codec.channels)
return false;
break;
......@@ -207,7 +186,7 @@ namespace RTC
// Per MIME checks.
switch (this->mime.subtype)
{
case RTC::RtpCodecMime::Subtype::H264:
case RTC::RtpCodecMimeType::Subtype::H264:
{
int32_t packetizationMode = this->parameters.GetInteger(jsonStringPacketizationMode);
int32_t givenPacketizationMode = codec.parameters.GetInteger(jsonStringPacketizationMode);
......@@ -257,7 +236,7 @@ namespace RTC
// Check per MIME parameters and set default values.
switch (this->mime.subtype)
{
case RTC::RtpCodecMime::Subtype::RTX:
case RTC::RtpCodecMimeType::Subtype::RTX:
{
// A RTX codec must have 'apt' parameter.
if (!this->parameters.HasInteger(jsonStringApt))
......@@ -266,16 +245,16 @@ namespace RTC
break;
}
case RTC::RtpCodecMime::Subtype::OPUS:
case RTC::RtpCodecMimeType::Subtype::OPUS:
{
// Opus default numChannels is 2.
if (this->numChannels < 2)
this->numChannels = 2;
// Opus default channels is 2.
if (this->channels < 2)
this->channels = 2;
break;
}
case RTC::RtpCodecMime::Subtype::H264:
case RTC::RtpCodecMimeType::Subtype::H264:
{
// H264 default packetizationMode is 0.
if (!this->parameters.HasInteger(jsonStringPacketizationMode))
......
......@@ -37,7 +37,7 @@ namespace RTC
for (auto& jsonCodec : jsonCodecs)
{
RTC::RtpCodecParameters codec(jsonCodec, RTC::Scope::RECEIVE);
RTC::RtpCodecParameters codec(jsonCodec);
// Append to the codecs vector.
this->codecs.push_back(codec);
......@@ -184,7 +184,7 @@ namespace RTC
MS_WARN_DEV(
"no matching peer codec capability found [payloadType:%" PRIu8 ", mime:%s]",
codec.payloadType,
codec.mime.GetName().c_str());
codec.mime.ToString().c_str());
removedCodecPayloadTypes.push_back(codec.payloadType);
it = this->codecs.erase(it);
......@@ -317,7 +317,7 @@ namespace RTC
switch (codec.mime.subtype)
{
// A RTX codec must have 'apt' parameter pointing to a non RTX codec.
case RTC::RtpCodecMime::Subtype::RTX:
case RTC::RtpCodecMimeType::Subtype::RTX:
{
// NOTE: RtpCodecParameters already asserted that there is 'apt' parameter.
int32_t apt = codec.parameters.GetInteger(jsonStringApt);
......@@ -329,11 +329,11 @@ namespace RTC
if (static_cast<int32_t>(codec.payloadType) == apt)
{
if (codec.mime.subtype == RTC::RtpCodecMime::Subtype::RTX)
if (codec.mime.subtype == RTC::RtpCodecMimeType::Subtype::RTX)
MS_THROW_ERROR("apt in RTX codec points to a RTX codec");
else if (codec.mime.subtype == RTC::RtpCodecMime::Subtype::ULPFEC)
else if (codec.mime.subtype == RTC::RtpCodecMimeType::Subtype::ULPFEC)
MS_THROW_ERROR("apt in RTX codec points to a ULPFEC codec");
else if (codec.mime.subtype == RTC::RtpCodecMime::Subtype::FLEXFEC)
else if (codec.mime.subtype == RTC::RtpCodecMimeType::Subtype::FLEXFEC)
MS_THROW_ERROR("apt in RTX codec points to a FLEXFEC codec");
else
break;
......
......@@ -187,7 +187,7 @@ namespace RTC
json[JsonStringSsrc] = Json::UInt{ this->ssrc };
json[JsonStringPayloadType] = Json::UInt{ this->payloadType };
json[JsonStringMime] = this->mime.name;
json[JsonStringMime] = this->mime.ToString();
json[JsonStringClockRate] = Json::UInt{ this->clockRate };
json[JsonStringUseNack] = this->useNack;
json[JsonStringUsePli] = this->usePli;
......
......@@ -226,7 +226,7 @@ namespace RTC
this->nackGenerator.reset(new RTC::NackGenerator(this));
// Request a full frame so dropped video packets don't cause lag.
if (this->params.mime.type == RTC::RtpCodecMime::Type::VIDEO)
if (this->params.mime.type == RTC::RtpCodecMimeType::Type::VIDEO)
{
MS_DEBUG_TAG(rtx, "stream initialized, triggering PLI [ssrc:%" PRIu32 "]", this->params.ssrc);
......
......@@ -118,7 +118,6 @@ void init()
RTC::TcpServer::ClassInit();
RTC::DtlsTransport::ClassInit();
RTC::SrtpSession::ClassInit();
RTC::Room::ClassInit();
}
void ignoreSignals()
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论