Skip to content

Commit

Permalink
Refactored clumsy usage of optional in rtcAddTrackEx
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Sep 6, 2023
1 parent 4b70bfe commit 59f3e59
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,75 +1037,71 @@ int rtcAddTrackEx(int pc, const rtcTrackInit *init) {
}

int pt = init->payloadType;
auto profile = init->profile ? std::make_optional(string(init->profile)) : nullopt;

optional<string> profile;
if (init->profile)
profile.emplace(string(init->profile));

optional<Description::Media> optDescription = nullopt;

unique_ptr<Description::Media> description;
switch (init->codec) {
case RTC_CODEC_H264:
case RTC_CODEC_H265:
case RTC_CODEC_VP8:
case RTC_CODEC_VP9: {
auto desc = Description::Video(mid, direction);
auto video = std::make_unique<Description::Video>(mid, direction);
switch (init->codec) {
case RTC_CODEC_H264:
desc.addH264Codec(pt, profile);
video->addH264Codec(pt, profile);
break;
case RTC_CODEC_H265:
desc.addH265Codec(pt, profile);
video->addH265Codec(pt, profile);
break;
case RTC_CODEC_VP8:
desc.addVP8Codec(pt, profile);
video->addVP8Codec(pt, profile);
break;
case RTC_CODEC_VP9:
desc.addVP9Codec(pt, profile);
video->addVP9Codec(pt, profile);
break;
default:
break;
}
optDescription = desc;
description = std::move(video);
break;
}
case RTC_CODEC_OPUS:
case RTC_CODEC_PCMU:
case RTC_CODEC_PCMA:
case RTC_CODEC_AAC: {
auto desc = Description::Audio(mid, direction);
auto audio = std::make_unique<Description::Audio>(mid, direction);
switch (init->codec) {
case RTC_CODEC_OPUS:
desc.addOpusCodec(pt, profile);
audio->addOpusCodec(pt, profile);
break;
case RTC_CODEC_PCMU:
desc.addPCMUCodec(pt, profile);
audio->addPCMUCodec(pt, profile);
break;
case RTC_CODEC_PCMA:
desc.addPCMACodec(pt, profile);
audio->addPCMACodec(pt, profile);
break;
case RTC_CODEC_AAC:
desc.addAacCodec(pt, profile);
audio->addAacCodec(pt, profile);
break;
default:
break;
}
optDescription = desc;
description = std::move(audio);
break;
}
default:
break;
}

if (!optDescription)
if (!description)
throw std::invalid_argument("Unexpected codec");

auto desc = std::move(*optDescription);
desc.addSSRC(init->ssrc, init->name ? std::make_optional(string(init->name)) : nullopt,
init->msid ? std::make_optional(string(init->msid)) : nullopt,
init->trackId ? std::make_optional(string(init->trackId)) : nullopt);
description->addSSRC(init->ssrc,
init->name ? std::make_optional(string(init->name)) : nullopt,
init->msid ? std::make_optional(string(init->msid)) : nullopt,
init->trackId ? std::make_optional(string(init->trackId)) : nullopt);

int tr = emplaceTrack(peerConnection->addTrack(std::move(desc)));
int tr = emplaceTrack(peerConnection->addTrack(std::move(*description)));

if (auto ptr = getUserPointer(pc))
rtcSetUserPointer(tr, *ptr);
Expand Down

0 comments on commit 59f3e59

Please sign in to comment.