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

Add createOffer() and createAnswer() #1320

Merged
merged 4 commits into from
Jan 22, 2025
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
25 changes: 22 additions & 3 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ Initiates the handshake process. Following this call, the local description call
Arguments:

- `pc`: the Peer Connection identifier
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for autodetection.
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for automatic (recommended).

Warning: This function expects the optional type for the local description and not an SDP description. It is not possible to set an existing SDP description.

#### rtcSetRemoteDescription

Expand All @@ -220,7 +222,8 @@ Sets the remote description received from the remote peer by the user's method o
Arguments:

- `pc`: the Peer Connection identifier
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for autodetection.
- `sdp`: the remote description in SDP format
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for automatic (not recommended).

If the remote description is an offer and `disableAutoNegotiation` was not set in `rtcConfiguration`, the library will automatically answer by calling `rtcSetLocalDescription` internally. Otherwise, the user must call it to answer the remote description.

Expand Down Expand Up @@ -296,7 +299,7 @@ Return value: the length of the string copied in buffer (including the terminati

If `buffer` is `NULL`, the description is not copied but the size is still returned.

#### rtcGetRemoteDescription
#### rtcGetRemoteDescriptionType

```
int rtcGetRemoteDescriptionType(int pc, char *buffer, int size)
Expand All @@ -314,6 +317,22 @@ Return value: the length of the string copied in buffer (including the terminati

If `buffer` is `NULL`, the description is not copied but the size is still returned.

#### rtcCreateOffer/rtcCreateAnswer

```
int rtcCreateOffer(int pc, char *buffer, int size)
int rtcCreateAnswer(int pc, char *buffer, int size)
```

Create a local offer or answer description in SDP format. These functions are intended only for specific use cases where the application needs to generate a description without setting it. It is useless to call them before `rtcSetLocalDescription` as it doesn't expect the user to supply a description.

- `pc`: the Peer Connection identifier
- `buffer`: a user-supplied buffer to store the description
- `size`: the size of `buffer`

Return value: the length of the string copied in buffer (including the terminating null character) or a negative error code

If `buffer` is `NULL`, the description is not copied but the size is still returned.

#### rtcGetLocalAddress

Expand Down
3 changes: 2 additions & 1 deletion include/rtc/description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ class RTC_CPP_EXPORT Description {
bool ended() const;

void hintType(Type type);
void setFingerprint(CertificateFingerprint f);
void addIceOption(string option);
void removeIceOption(const string &option);
void setIceAttribute(string ufrag, string pwd);
void setFingerprint(CertificateFingerprint f);

std::vector<string> attributes() const;
void addAttribute(string attr);
Expand Down
6 changes: 5 additions & 1 deletion include/rtc/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
bool getSelectedCandidatePair(Candidate *local, Candidate *remote);

void setLocalDescription(Description::Type type = Description::Type::Unspec, LocalDescriptionInit init = {});
void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});
void setRemoteDescription(Description description);
void addRemoteCandidate(Candidate candidate);
void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});

// For specific use cases only
Description createOffer();
Description createAnswer();

void setMediaHandler(shared_ptr<MediaHandler> handler);
shared_ptr<MediaHandler> getMediaHandler();
Expand Down
6 changes: 5 additions & 1 deletion include/rtc/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ RTC_C_EXPORT int rtcSetIceStateChangeCallback(int pc, rtcIceStateChangeCallbackF
RTC_C_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
RTC_C_EXPORT int rtcSetSignalingStateChangeCallback(int pc, rtcSignalingStateCallbackFunc cb);

RTC_C_EXPORT int rtcSetLocalDescription(int pc, const char *type);
RTC_C_EXPORT int rtcSetLocalDescription(int pc, const char *type); // type may be NULL
RTC_C_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
RTC_C_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);

Expand All @@ -221,6 +221,10 @@ RTC_C_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size);
RTC_C_EXPORT int rtcGetLocalDescriptionType(int pc, char *buffer, int size);
RTC_C_EXPORT int rtcGetRemoteDescriptionType(int pc, char *buffer, int size);

// For specific use cases only
RTC_C_EXPORT int rtcCreateOffer(int pc, char *buffer, int size);
RTC_C_EXPORT int rtcCreateAnswer(int pc, char *buffer, int size);

RTC_C_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
RTC_C_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);

Expand Down
25 changes: 22 additions & 3 deletions pages/content/pages/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ Initiates the handshake process. Following this call, the local description call
Arguments:

- `pc`: the Peer Connection identifier
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for autodetection.
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for automatic (recommended).

Warning: This function expects the optional type for the local description and not an SDP description. It is not possible to set an existing SDP description.

#### rtcSetRemoteDescription

Expand All @@ -223,7 +225,8 @@ Sets the remote description received from the remote peer by the user's method o
Arguments:

- `pc`: the Peer Connection identifier
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for autodetection.
- `sdp`: the remote description in SDP format
- `type` (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for automatic (not recommended).

If the remote description is an offer and `disableAutoNegotiation` was not set in `rtcConfiguration`, the library will automatically answer by calling `rtcSetLocalDescription` internally. Otherwise, the user must call it to answer the remote description.

Expand Down Expand Up @@ -299,7 +302,7 @@ Return value: the length of the string copied in buffer (including the terminati

If `buffer` is `NULL`, the description is not copied but the size is still returned.

#### rtcGetRemoteDescription
#### rtcGetRemoteDescriptionType

```
int rtcGetRemoteDescriptionType(int pc, char *buffer, int size)
Expand All @@ -317,6 +320,22 @@ Return value: the length of the string copied in buffer (including the terminati

If `buffer` is `NULL`, the description is not copied but the size is still returned.

#### rtcCreateOffer/rtcCreateAnswer

```
int rtcCreateOffer(int pc, char *buffer, int size)
int rtcCreateAnswer(int pc, char *buffer, int size)
```

Create a local offer or answer description in SDP format. These functions are intended only for specific use cases where the application needs to generate a description without setting it. It is useless to call them before `rtcSetLocalDescription` as it doesn't expect the user to supply a description.

- `pc`: the Peer Connection identifier
- `buffer`: a user-supplied buffer to store the description
- `size`: the size of `buffer`

Return value: the length of the string copied in buffer (including the terminating null character) or a negative error code

If `buffer` is `NULL`, the description is not copied but the size is still returned.

#### rtcGetLocalAddress

Expand Down
18 changes: 18 additions & 0 deletions src/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,24 @@ int rtcGetRemoteDescriptionType(int pc, char *buffer, int size) {
});
}

int rtcCreateOffer(int pc, char *buffer, int size) {
return wrap([&] {
auto peerConnection = getPeerConnection(pc);

auto desc = peerConnection->createOffer();
return copyAndReturn(string(desc), buffer, size);
});
}

int rtcCreateAnswer(int pc, char *buffer, int size) {
return wrap([&] {
auto peerConnection = getPeerConnection(pc);

auto desc = peerConnection->createAnswer();
return copyAndReturn(string(desc), buffer, size);
});
}

int rtcGetLocalAddress(int pc, char *buffer, int size) {
return wrap([&] {
auto peerConnection = getPeerConnection(pc);
Expand Down
25 changes: 15 additions & 10 deletions src/description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ void Description::hintType(Type type) {
mType = type;
}

void Description::addIceOption(string option) {
if (std::find(mIceOptions.begin(), mIceOptions.end(), option) == mIceOptions.end())
mIceOptions.emplace_back(std::move(option));
}

void Description::removeIceOption(const string &option) {
mIceOptions.erase(std::remove(mIceOptions.begin(), mIceOptions.end(), option),
mIceOptions.end());
}

void Description::setIceAttribute(string ufrag, string pwd) {
mIceUfrag = std::move(ufrag);
mIcePwd = std::move(pwd);
}

void Description::setFingerprint(CertificateFingerprint f) {
if (!f.isValid())
throw std::invalid_argument("Invalid " +
Expand All @@ -227,16 +242,6 @@ void Description::setFingerprint(CertificateFingerprint f) {
mFingerprint = std::move(f);
}

void Description::addIceOption(string option) {
if (std::find(mIceOptions.begin(), mIceOptions.end(), option) == mIceOptions.end())
mIceOptions.emplace_back(std::move(option));
}

void Description::removeIceOption(const string &option) {
mIceOptions.erase(std::remove(mIceOptions.begin(), mIceOptions.end(), option),
mIceOptions.end());
}

std::vector<string> Description::Entry::attributes() const { return mAttributes; }

void Description::Entry::addAttribute(string attr) {
Expand Down
Loading
Loading