提交 74e30699 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Work in codec capabilities

上级 8d05d4c9
...@@ -140,9 +140,9 @@ tap.test('room.getCapabilities() must retrieve current room capabilities', { tim ...@@ -140,9 +140,9 @@ tap.test('room.getCapabilities() must retrieve current room capabilities', { tim
.then((room) => .then((room) =>
{ {
// TODO: REMOVE // TODO: REMOVE
// console.log(JSON.stringify(room.getCapabilities('audio'), null, '\t')); console.log(JSON.stringify(room.getCapabilities('audio'), null, '\t'));
// console.log(JSON.stringify(room.getCapabilities('video'), null, '\t')); console.log(JSON.stringify(room.getCapabilities('video'), null, '\t'));
// console.log(JSON.stringify(room.getCapabilities('depth'), null, '\t')); console.log(JSON.stringify(room.getCapabilities('depth'), null, '\t'));
t.ok(room.getCapabilities('audio'), 'room.getCapabilities("audio") returns an object'); t.ok(room.getCapabilities('audio'), 'room.getCapabilities("audio") returns an object');
t.ok(room.getCapabilities('video'), 'room.getCapabilities("video") returns an object'); t.ok(room.getCapabilities('video'), 'room.getCapabilities("video") returns an object');
......
#ifndef MS_RTC_CUSTOM_PARAMETERS_H
#define MS_RTC_CUSTOM_PARAMETERS_H
#include "common.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <json/json.h>
namespace RTC
{
class CustomParameterValue
{
private:
enum class Type
{
BOOLEAN = 1,
INTEGER,
DOUBLE,
STRING,
ARRAY_POSITIVE_INTEGER
};
public:
CustomParameterValue() {};
CustomParameterValue(bool booleanValue) :
type(Type::BOOLEAN),
booleanValue(booleanValue)
{}
CustomParameterValue(int32_t integerValue) :
type(Type::INTEGER),
integerValue(integerValue)
{}
CustomParameterValue(double doubleValue) :
type(Type::DOUBLE),
doubleValue(doubleValue)
{}
CustomParameterValue(std::string& stringValue) :
type(Type::STRING),
stringValue(stringValue)
{}
CustomParameterValue(std::vector<uint32_t>& arrayPositiveInteger) :
type(Type::ARRAY_POSITIVE_INTEGER),
arrayPositiveInteger(arrayPositiveInteger)
{}
Json::Value toJson()
{
switch (this->type)
{
case Type::BOOLEAN:
{
return this->booleanValue;
}
case Type::INTEGER:
{
return (Json::Int)this->integerValue;
}
case Type::DOUBLE:
{
return this->doubleValue;
}
case Type::STRING:
{
return this->stringValue;
}
case Type::ARRAY_POSITIVE_INTEGER:
{
Json::Value array(Json::arrayValue);
for (auto& entry : this->arrayPositiveInteger)
array.append((Json::UInt)entry);
return array;
}
default:
return Json::nullValue;
}
}
bool IsBoolean()
{
return this->type == Type::BOOLEAN;
}
bool IsInteger()
{
return this->type == Type::INTEGER;
}
bool IsPositiveInteger()
{
return this->type == Type::INTEGER && this->integerValue >= 0;
}
bool IsDouble()
{
return this->type == Type::DOUBLE;
}
bool IsPositiveDouble()
{
return this->type == Type::DOUBLE && this->doubleValue >= 0;
}
bool IsString()
{
return this->type == Type::STRING;
}
bool IsArrayPositiveInteger()
{
return this->type == Type::ARRAY_POSITIVE_INTEGER;
}
private:
Type type;
public:
bool booleanValue = false;
int32_t integerValue = 0;
double doubleValue = 0.0;
std::string stringValue;
std::vector<uint32_t> arrayPositiveInteger;
};
typedef std::unordered_map<std::string, CustomParameterValue> CustomParameters;
void FillCustomParameters(CustomParameters& parameters, Json::Value& data);
}
#endif
#ifndef MS_RTC_PARAMETERS_H
#define MS_RTC_PARAMETERS_H
#include "common.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <json/json.h>
namespace RTC
{
class Parameters
{
public:
class Value
{
public:
enum class Type
{
BOOLEAN = 1,
INTEGER,
DOUBLE,
STRING,
ARRAY_OF_INTEGERS
};
public:
Value() {};
Value(bool booleanValue) :
type(Type::BOOLEAN),
booleanValue(booleanValue)
{}
Value(int32_t integerValue) :
type(Type::INTEGER),
integerValue(integerValue)
{}
Value(double doubleValue) :
type(Type::DOUBLE),
doubleValue(doubleValue)
{}
Value(std::string& stringValue) :
type(Type::STRING),
stringValue(stringValue)
{}
Value(std::vector<int32_t>& arrayOfIntegers) :
type(Type::ARRAY_OF_INTEGERS),
arrayOfIntegers(arrayOfIntegers)
{}
public:
Type type;
bool booleanValue = false;
int32_t integerValue = 0;
double doubleValue = 0.0;
std::string stringValue;
std::vector<int32_t> arrayOfIntegers;
};
public:
Parameters() {};
Json::Value toJson();
void Set(Json::Value& data);
bool HasBoolean(std::string& key);
bool HasInteger(std::string& key);
bool HasDouble(std::string& key);
bool HasString(std::string& key);
bool HasArrayOfIntegers(std::string& key);
bool IncludesInteger(std::string& key, int32_t integer);
bool GetBoolean(std::string& key);
int32_t GetInteger(std::string& key);
double GetDouble(std::string& key);
std::string& GetString(std::string& key);
std::vector<int32_t>& GetArrayOfIntegers(std::string& key);
void SetBoolean(std::string& key, bool booleanValue);
void SetInteger(std::string& key, int32_t integerValue);
void SetDouble(std::string& key, double doubleValue);
void SetString(std::string& key, std::string& stringValue);
void SetArrayOfIntegers(std::string& key, std::vector<int32_t>& arrayOfIntegers);
private:
std::unordered_map<std::string, Value> mapKeyValues;
};
/* Inline methods. */
inline
bool Parameters::HasBoolean(std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
return this->mapKeyValues[key].type == Value::Type::BOOLEAN;
}
inline
bool Parameters::HasInteger(std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
return this->mapKeyValues[key].type == Value::Type::INTEGER;
}
inline
bool Parameters::HasDouble(std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
return this->mapKeyValues[key].type == Value::Type::DOUBLE;
}
inline
bool Parameters::HasString(std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
return this->mapKeyValues[key].type == Value::Type::STRING;
}
inline
bool Parameters::HasArrayOfIntegers(std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
return this->mapKeyValues[key].type == Value::Type::ARRAY_OF_INTEGERS;
}
inline
bool Parameters::IncludesInteger(std::string& key, int32_t integer)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
auto& array = this->mapKeyValues[key].arrayOfIntegers;
return std::find(array.begin(), array.end(), integer) != array.end();
}
inline
void Parameters::SetBoolean(std::string& key, bool booleanValue)
{
this->mapKeyValues[key] = Value(booleanValue);
}
inline
void Parameters::SetInteger(std::string& key, int32_t integerValue)
{
this->mapKeyValues[key] = Value(integerValue);
}
inline
void Parameters::SetDouble(std::string& key, double doubleValue)
{
this->mapKeyValues[key] = Value(doubleValue);
}
inline
void Parameters::SetString(std::string& key, std::string& stringValue)
{
this->mapKeyValues[key] = Value(stringValue);
}
inline
void Parameters::SetArrayOfIntegers(std::string& key, std::vector<int32_t>& arrayOfIntegers)
{
this->mapKeyValues[key] = Value(arrayOfIntegers);
}
}
#endif
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define MS_RTC_RTP_DICTIONARIES_H #define MS_RTC_RTP_DICTIONARIES_H
#include "common.h" #include "common.h"
#include "RTC/CustomParameters.h" #include "RTC/Parameters.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
...@@ -120,7 +120,7 @@ namespace RTC ...@@ -120,7 +120,7 @@ namespace RTC
uint32_t maxptime = 0; uint32_t maxptime = 0;
uint32_t ptime = 0; uint32_t ptime = 0;
uint32_t numChannels = 1; uint32_t numChannels = 1;
RTC::CustomParameters parameters; RTC::Parameters parameters;
}; };
class RTCRtpCodecRtxParameters class RTCRtpCodecRtxParameters
...@@ -223,7 +223,7 @@ namespace RTC ...@@ -223,7 +223,7 @@ namespace RTC
std::string uri; std::string uri;
uint16_t id = 0; uint16_t id = 0;
bool encrypt = false; bool encrypt = false;
RTC::CustomParameters parameters; RTC::Parameters parameters;
}; };
class RtcpParameters class RtcpParameters
...@@ -271,6 +271,7 @@ namespace RTC ...@@ -271,6 +271,7 @@ namespace RTC
Json::Value toJson(); Json::Value toJson();
bool MatchesCodec(RtpCodec& codec); bool MatchesCodec(RtpCodec& codec);
void Reduce(RtpCodec& codec);
public: public:
Media::Kind kind = Media::Kind::ALL; Media::Kind kind = Media::Kind::ALL;
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include "common.h" #include "common.h"
#include <string> #include <string>
#include <algorithm> // std::transform()
#include <cstring> // std::memcmp(), std::memcpy() #include <cstring> // std::memcmp(), std::memcpy()
#include <openssl/hmac.h> #include <openssl/hmac.h>
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define MS_COMMON_H #define MS_COMMON_H
#include <memory> // std::addressof() #include <memory> // std::addressof()
#include <algorithm> // std::transform(), std::find()
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdint> // uint8_t, etc #include <cstdint> // uint8_t, etc
#include <cinttypes> // PRIu64, etc #include <cinttypes> // PRIu64, etc
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
'src/Channel/Notifier.cpp', 'src/Channel/Notifier.cpp',
'src/Channel/Request.cpp', 'src/Channel/Request.cpp',
'src/Channel/UnixStreamSocket.cpp', 'src/Channel/UnixStreamSocket.cpp',
'src/RTC/CustomParameters.cpp',
'src/RTC/DtlsTransport.cpp', 'src/RTC/DtlsTransport.cpp',
'src/RTC/IceCandidate.cpp', 'src/RTC/IceCandidate.cpp',
'src/RTC/IceServer.cpp', 'src/RTC/IceServer.cpp',
...@@ -45,6 +44,7 @@ ...@@ -45,6 +44,7 @@
'src/RTC/TransportTuple.cpp', 'src/RTC/TransportTuple.cpp',
'src/RTC/UdpSocket.cpp', 'src/RTC/UdpSocket.cpp',
'src/RTC/RtpDictionaries/Media.cpp', 'src/RTC/RtpDictionaries/Media.cpp',
'src/RTC/RtpDictionaries/Parameters.cpp',
'src/RTC/RtpDictionaries/RTCRtpCodecRtxParameters.cpp', 'src/RTC/RtpDictionaries/RTCRtpCodecRtxParameters.cpp',
'src/RTC/RtpDictionaries/RtcpFeedback.cpp', 'src/RTC/RtpDictionaries/RtcpFeedback.cpp',
'src/RTC/RtpDictionaries/RtcpParameters.cpp', 'src/RTC/RtpDictionaries/RtcpParameters.cpp',
...@@ -84,10 +84,10 @@ ...@@ -84,10 +84,10 @@
'include/Channel/Notifier.h', 'include/Channel/Notifier.h',
'include/Channel/Request.h', 'include/Channel/Request.h',
'include/Channel/UnixStreamSocket.h', 'include/Channel/UnixStreamSocket.h',
'include/RTC/CustomParameters.h',
'include/RTC/DtlsTransport.h', 'include/RTC/DtlsTransport.h',
'include/RTC/IceCandidate.h', 'include/RTC/IceCandidate.h',
'include/RTC/IceServer.h', 'include/RTC/IceServer.h',
'include/RTC/Parameters.h',
'include/RTC/Peer.h', 'include/RTC/Peer.h',
'include/RTC/Room.h', 'include/RTC/Room.h',
'include/RTC/RtcpPacket.h', 'include/RTC/RtcpPacket.h',
......
#define MS_CLASS "RTC::CustomParameters"
#include "RTC/CustomParameters.h"
#include "Logger.h"
namespace RTC
{
/* Global methods. */
void FillCustomParameters(CustomParameters& parameters, Json::Value& data)
{
MS_TRACE();
MS_ASSERT(data.isObject(), "data is not a JSON object");
for (Json::Value::iterator it = data.begin(); it != data.end(); ++it)
{
std::string key = it.key().asString();
Json::Value value = (*it);
switch (value.type())
{
case Json::booleanValue:
{
bool booleanValue = value.asBool();
parameters[key] = CustomParameterValue(booleanValue);
break;
}
case Json::intValue:
{
int32_t signedIntegerValue = (int32_t)value.asInt();
parameters[key] = CustomParameterValue(signedIntegerValue);
break;
}
case Json::realValue:
{
double doubleValue = value.asDouble();
parameters[key] = CustomParameterValue(doubleValue);
break;
}
case Json::stringValue:
{
std::string stringValue = value.asString();
parameters[key] = CustomParameterValue(stringValue);
break;
}
case Json::arrayValue:
{
std::vector<uint32_t> arrayPositiveInteger;
bool isValid = true;
for (Json::UInt i = 0; i < value.size() && isValid; i++)
{
auto& entry = value[i];
if (entry.isUInt())
arrayPositiveInteger.push_back((uint32_t)entry.asUInt());
else
isValid = false;
}
if (isValid)
parameters[key] = CustomParameterValue(arrayPositiveInteger);
break;
}
default:
; // Just ignore other value types.
}
}
}
}
...@@ -411,9 +411,8 @@ namespace RTC ...@@ -411,9 +411,8 @@ namespace RTC
if (it == Room::supportedRtpCapabilities.codecs.end()) if (it == Room::supportedRtpCapabilities.codecs.end())
MS_THROW_ERROR("no matching codec capability found"); MS_THROW_ERROR("no matching codec capability found");
// TODO: Reduce the capabilities of the found (and copied) codec (for // Reduce the capabilities of the matching capability codec.
// example set the H264 packetizationMode parameter to a single value). codecCapability.Reduce(roomMediaCodec);
// NOTE: Maybe a RtpCodecCapability.Reduce(codec) method.
// Set unique PT. // Set unique PT.
......
#define MS_CLASS "RTC::Parameters"
#include "RTC/Parameters.h"
#include "Logger.h"
namespace RTC
{
/* Instance methods. */
Json::Value Parameters::toJson()
{
MS_TRACE();
Json::Value json(Json::objectValue);
for (auto& kv : this->mapKeyValues)
{
auto& key = kv.first;
auto& value = kv.second;
switch (value.type)
{
case Value::Type::BOOLEAN:
{
json[key] = value.booleanValue;
break;
}
case Value::Type::INTEGER:
{
json[key] = (Json::Int)value.integerValue;
break;
}
case Value::Type::DOUBLE:
{
json[key] = value.doubleValue;
break;
}
case Value::Type::STRING:
{
json[key] = value.stringValue;
break;
}
case Value::Type::ARRAY_OF_INTEGERS:
{
Json::Value array(Json::arrayValue);
for (auto& entry : value.arrayOfIntegers)
array.append((Json::Int)entry);
json[key] = array;
break;
}
}
}
return json;
}
void Parameters::Set(Json::Value& data)
{
MS_TRACE();
MS_ASSERT(data.isObject(), "data is not a JSON object");
for (Json::Value::iterator it = data.begin(); it != data.end(); ++it)
{
std::string key = it.key().asString();
Json::Value value = (*it);
switch (value.type())
{
case Json::booleanValue:
{
bool booleanValue = value.asBool();
this->mapKeyValues[key] = Value(booleanValue);
break;
}
case Json::intValue:
{
int32_t integerValue = (int32_t)value.asInt();
this->mapKeyValues[key] = Value(integerValue);
break;
}
case Json::realValue:
{
double doubleValue = value.asDouble();
this->mapKeyValues[key] = Value(doubleValue);
break;
}
case Json::stringValue:
{
std::string stringValue = value.asString();
this->mapKeyValues[key] = Value(stringValue);
break;
}
case Json::arrayValue:
{
std::vector<int32_t> arrayOfIntegers;
bool isValid = true;
for (Json::UInt i = 0; i < value.size() && isValid; i++)
{
auto& entry = value[i];
if (entry.isInt())
arrayOfIntegers.push_back((int32_t)entry.asInt());
else
isValid = false;
}
if (isValid)
this->mapKeyValues[key] = Value(arrayOfIntegers);
break;
}
default:
; // Just ignore other value types.
}
}
}
bool Parameters::GetBoolean(std::string& key)
{
MS_TRACE();
MS_ASSERT(this->mapKeyValues.find(key) != this->mapKeyValues.end(),
"key does not exist [key:%s]", key.c_str());
return this->mapKeyValues[key].booleanValue;
}
int32_t Parameters::GetInteger(std::string& key)
{
MS_TRACE();
MS_ASSERT(this->mapKeyValues.find(key) != this->mapKeyValues.end(),
"key does not exist [key:%s]", key.c_str());
return this->mapKeyValues[key].integerValue;
}
double Parameters::GetDouble(std::string& key)
{
MS_TRACE();
MS_ASSERT(this->mapKeyValues.find(key) != this->mapKeyValues.end(),
"key does not exist [key:%s]", key.c_str());
return this->mapKeyValues[key].doubleValue;
}
std::string& Parameters::GetString(std::string& key)
{
MS_TRACE();
MS_ASSERT(this->mapKeyValues.find(key) != this->mapKeyValues.end(),
"key does not exist [key:%s]", key.c_str());
return this->mapKeyValues[key].stringValue;
}
std::vector<int32_t>& Parameters::GetArrayOfIntegers(std::string& key)
{
MS_TRACE();
MS_ASSERT(this->mapKeyValues.find(key) != this->mapKeyValues.end(),
"key does not exist [key:%s]", key.c_str());
return this->mapKeyValues[key].arrayOfIntegers;
}
}
...@@ -52,6 +52,8 @@ namespace RTC ...@@ -52,6 +52,8 @@ namespace RTC
// `parameters` is optional. // `parameters` is optional.
if (data[k_parameters].isObject()) if (data[k_parameters].isObject())
RTC::FillCustomParameters(this->parameters, data[k_parameters]); this->parameters.Set(data[k_parameters]);
// TODO: Check per MIME parameters and set default values.
} }
} }
...@@ -64,6 +64,8 @@ namespace RTC ...@@ -64,6 +64,8 @@ namespace RTC
// `svcMultiStreamSupport` is optional. // `svcMultiStreamSupport` is optional.
if (data[k_svcMultiStreamSupport].isBool()) if (data[k_svcMultiStreamSupport].isBool())
this->svcMultiStreamSupport = data[k_svcMultiStreamSupport].asBool(); this->svcMultiStreamSupport = data[k_svcMultiStreamSupport].asBool();
// TODO: Check per MIME parameters and set default values.
} }
Json::Value RtpCodecCapability::toJson() Json::Value RtpCodecCapability::toJson()
...@@ -118,17 +120,7 @@ namespace RTC ...@@ -118,17 +120,7 @@ namespace RTC
} }
// Add `parameters`. // Add `parameters`.
Json::Value json_parameters(Json::objectValue); json[k_parameters] = this->parameters.toJson();
for (auto& kv : this->parameters)
{
const std::string& key = kv.first;
auto& parameterValue = kv.second;
json_parameters[key] = parameterValue.toJson();
}
json[k_parameters] = json_parameters;
// Add `maxTemporalLayers` (if set). // Add `maxTemporalLayers` (if set).
if (this->maxTemporalLayers) if (this->maxTemporalLayers)
...@@ -184,4 +176,29 @@ namespace RTC ...@@ -184,4 +176,29 @@ namespace RTC
return true; return true;
} }
// NOTE: This method assumes that MatchesCodec() has been called before
// with same codec and returned true.
void RtpCodecCapability::Reduce(RtpCodec& codec)
{
MS_TRACE();
static std::string k_packetizationMode = "packetizationMode";
switch (codec.mime.subtype)
{
case RTC::RtpCodecMime::Subtype::H264:
{
std::vector<int32_t> packetizationMode;
packetizationMode.push_back(codec.parameters.GetInteger(k_packetizationMode));
this->parameters.SetArrayOfIntegers(k_packetizationMode, packetizationMode);
break;
}
default:
;
}
}
} }
...@@ -96,17 +96,7 @@ namespace RTC ...@@ -96,17 +96,7 @@ namespace RTC
} }
// Add `parameters`. // Add `parameters`.
Json::Value json_parameters(Json::objectValue); json[k_parameters] = this->parameters.toJson();
for (auto& kv : this->parameters)
{
const std::string& key = kv.first;
auto& parameterValue = kv.second;
json_parameters[key] = parameterValue.toJson();
}
json[k_parameters] = json_parameters;
return json; return json;
} }
......
...@@ -40,7 +40,7 @@ namespace RTC ...@@ -40,7 +40,7 @@ namespace RTC
// `parameters` is optional. // `parameters` is optional.
if (data[k_parameters].isObject()) if (data[k_parameters].isObject())
RTC::FillCustomParameters(this->parameters, data[k_parameters]); this->parameters.Set(data[k_parameters]);
} }
Json::Value RtpHeaderExtensionParameters::toJson() Json::Value RtpHeaderExtensionParameters::toJson()
...@@ -64,17 +64,7 @@ namespace RTC ...@@ -64,17 +64,7 @@ namespace RTC
json[k_encrypt] = this->encrypt; json[k_encrypt] = this->encrypt;
// Add `parameters`. // Add `parameters`.
Json::Value json_parameters(Json::objectValue); json[k_parameters] = this->parameters.toJson();
for (auto& kv : this->parameters)
{
const std::string& key = kv.first;
auto& parameterValue = kv.second;
json_parameters[key] = parameterValue.toJson();
}
json[k_parameters] = json_parameters;
return json; return json;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论