Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix media description m-line parsing #1081

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ inline void trim_end(string &str) {
str.end());
}

inline string get_first_line(const string &str) {
string line;
std::istringstream ss(str);
std::getline(ss, line);
return line;
}

inline std::pair<string_view, string_view> parse_pair(string_view attr) {
string_view key, value;
if (size_t separator = attr.find(':'); separator != string::npos) {
Expand Down Expand Up @@ -523,12 +530,15 @@ unsigned int Description::mediaCount() const { return unsigned(mEntries.size());
Description::Entry::Entry(const string &mline, string mid, Direction dir)
: mMid(std::move(mid)), mDirection(dir) {

uint16_t port;
std::istringstream ss(mline);
uint16_t port = 0;
std::istringstream ss(match_prefix(mline, "m=") ? mline.substr(2) : mline);
ss >> mType;
ss >> port;
ss >> mDescription;

if (mType.empty() || mDescription.empty())
throw std::invalid_argument("Invalid media description line");

// RFC 3264: Existing media streams are removed by creating a new SDP with the port number for
// that stream set to zero.
// RFC 8843: If the offerer assigns a zero port value to a bundled "m=" section, but does not
Expand Down Expand Up @@ -866,10 +876,12 @@ void Description::Application::parseSdpLine(string_view line) {
}
}

Description::Media::Media(const string &sdp) : Entry(sdp, "", Direction::Unknown) {
Description::Media::Media(const string &sdp)
: Entry(get_first_line(sdp), "", Direction::Unknown) {
string line;
std::istringstream ss(sdp);
std::getline(ss, line); // discard first line
while (ss) {
string line;
std::getline(ss, line);
trim_end(line);
if (line.empty())
Expand Down
5 changes: 5 additions & 0 deletions test/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ void test_track() {
media.setBitrate(3000);
media.addSSRC(1234, "video-send");

const auto mediaSdp1 = string(media);
const auto mediaSdp2 = string(Description::Media(mediaSdp1));
if (mediaSdp2 != mediaSdp1)
throw runtime_error("Media description parsing test failed");

auto t1 = pc1.addTrack(media);

pc1.setLocalDescription();
Expand Down
Loading