Refactored description for media handling

This commit is contained in:
Paul-Louis Ageneau
2020-09-06 19:43:20 +02:00
parent 31aac35d2c
commit 6c3aaf8a77
10 changed files with 548 additions and 318 deletions

View File

@ -1,5 +1,6 @@
/**
* Copyright (c) 2019 Paul-Louis Ageneau
* Copyright (c) 2019-2020 Paul-Louis Ageneau
* Copyright (c) 2020 Staz M
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -24,12 +25,13 @@
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <variant>
#include <vector>
namespace rtc {
class Description {
public:
enum class Type { Unspec = 0, Offer = 1, Answer = 2 };
@ -44,18 +46,12 @@ public:
string typeString() const;
Role role() const;
string roleString() const;
string dataMid() const;
string bundleMid() const;
std::optional<string> fingerprint() const;
std::optional<uint16_t> sctpPort() const;
std::optional<size_t> maxMessageSize() const;
bool ended() const;
void hintType(Type type);
void setDataMid(string mid);
void setFingerprint(string fingerprint);
void setSctpPort(uint16_t port);
void setMaxMessageSize(size_t size);
void addCandidate(Candidate candidate);
void endCandidates();
@ -63,22 +59,68 @@ public:
operator string() const;
string generateSdp(string_view eol) const;
string generateDataSdp(string_view eol) const;
string generateApplicationSdp(string_view eol) const;
class Entry {
public:
Entry(string mline, string mid = "", Direction dir = Direction::Unknown);
virtual ~Entry() = default;
virtual string type() const { return mType; }
virtual string description() const { return mDescription; }
virtual string mid() const { return mMid; }
Direction direction() const;
virtual void parseSdpLine(string_view line);
virtual string generateSdp(string_view eol) const;
protected:
std::vector<string> mAttributes;
Direction mDirection;
private:
string mType;
string mDescription;
string mMid;
};
struct Application : public Entry {
public:
Application(string mid = "data");
Application(const Application &other) = default;
Application(Application &&other) = default;
string description() const override;
Application reciprocate() const;
void setSctpPort(uint16_t port) { mSctpPort = port; }
void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
std::optional<uint16_t> sctpPort() const { return mSctpPort; }
std::optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
virtual void parseSdpLine(string_view line) override;
virtual string generateSdp(string_view eol) const override;
private:
std::optional<uint16_t> mSctpPort;
std::optional<size_t> mMaxMessageSize;
};
// Media (non-data)
class Media {
class Media : public Entry {
public:
Media(string mline, Direction dir = Direction::Unknown, string mid = "media");
Media(string mline, string mid = "media", Direction dir = Direction::SendOnly);
Media(const Media &other) = default;
Media(Media &&other) = default;
virtual ~Media() = default;
string type() const { return mType; }
string description() const { return mDescription; }
string mid() const { return mMid; }
string description() const override;
Media reciprocate() const;
void removeFormat(const string &fmt);
Direction getDirection();
void setDirection(Direction dir);
void addVideoCodec(int payloadType, const string &codec);
void addH264Codec(int payloadType);
void addVP8Codec(int payloadType);
@ -87,19 +129,16 @@ public:
void setBitrate(int bitrate);
int getBitrate() const;
void parseSdpLine(string line);
string generateSdp(string_view eol) const;
bool hasPayloadType(int payloadType) const;
virtual void parseSdpLine(string_view line) override;
virtual string generateSdp(string_view eol) const override;
private:
string mType;
string mDescription;
string mMid;
std::vector<string> mAttributes;
std::vector<string> mAttributesl;
int mBas = -1;
struct RTPMap {
RTPMap(const string &mLine);
RTPMap(string_view mline);
void removeFB(const string &string);
void addFB(const string &string);
@ -119,35 +158,45 @@ public:
std::map<int, RTPMap> mRtpMap;
};
class AudioMedia : public Media {
class Audio : public Media {
public:
AudioMedia(Direction dir, string mid = "audio");
Audio(string mid = "audio", Direction dir = Direction::SendOnly);
};
class VideoMedia : public Media {
class Video : public Media {
public:
VideoMedia(Direction dir, string mid = "video");
Video(string mid = "video", Direction dir = Direction::SendOnly);
};
bool hasMedia() const;
void addMedia(Media media);
bool hasAudioOrVideo() const;
int addMedia(Media media);
int addMedia(Application application);
int addApplication(string mid = "data");
int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
std::variant<Media *, Application *> media(int index);
std::variant<const Media *, const Application *> media(int index) const;
int mediaCount() const;
Application *application();
private:
std::shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
void removeApplication();
Type mType;
// Session-level attributes
Role mRole;
string mSessionId;
string mIceUfrag, mIcePwd;
std::optional<string> mFingerprint;
struct Data {
string mid;
std::optional<uint16_t> sctpPort;
std::optional<size_t> maxMessageSize;
};
Data mData;
std::map<int, Media> mMedia; // by m-line index
// Entries
std::vector<std::shared_ptr<Entry>> mEntries;
std::shared_ptr<Application> mApplication;
// Candidates
std::vector<Candidate> mCandidates;