mirror of
https://github.com/mii443/libdatachannel.git
synced 2025-08-22 15:15:28 +00:00
Renamed createTrack() to addTrack() and added addDataChannel()
This commit is contained in:
@ -65,7 +65,7 @@ int main() {
|
|||||||
media.setBitrate(
|
media.setBitrate(
|
||||||
3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
|
3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
|
||||||
|
|
||||||
auto track = pc->createTrack(media);
|
auto track = pc->addTrack(media);
|
||||||
|
|
||||||
auto session = std::make_shared<rtc::RtcpSession>();
|
auto session = std::make_shared<rtc::RtcpSession>();
|
||||||
track->setRtcpHandler(session);
|
track->setRtcpHandler(session);
|
||||||
|
@ -86,6 +86,10 @@ public:
|
|||||||
void setRemoteDescription(Description description);
|
void setRemoteDescription(Description description);
|
||||||
void addRemoteCandidate(Candidate candidate);
|
void addRemoteCandidate(Candidate candidate);
|
||||||
|
|
||||||
|
std::shared_ptr<DataChannel> addDataChannel(string label, string protocol = "",
|
||||||
|
Reliability reliability = {});
|
||||||
|
|
||||||
|
// Equivalent to calling addDataChannel() and setLocalDescription()
|
||||||
std::shared_ptr<DataChannel> createDataChannel(string label, string protocol = "",
|
std::shared_ptr<DataChannel> createDataChannel(string label, string protocol = "",
|
||||||
Reliability reliability = {});
|
Reliability reliability = {});
|
||||||
|
|
||||||
@ -102,7 +106,7 @@ public:
|
|||||||
std::optional<std::chrono::milliseconds> rtt();
|
std::optional<std::chrono::milliseconds> rtt();
|
||||||
|
|
||||||
// Track media support requires compiling with libSRTP
|
// Track media support requires compiling with libSRTP
|
||||||
std::shared_ptr<Track> createTrack(Description::Media description);
|
std::shared_ptr<Track> addTrack(Description::Media description);
|
||||||
void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
|
void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
|
||||||
|
|
||||||
// libnice only
|
// libnice only
|
||||||
|
@ -119,6 +119,10 @@ RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
|
|||||||
|
|
||||||
// DataChannel
|
// DataChannel
|
||||||
RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
|
RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
|
||||||
|
RTC_EXPORT int rtcAddDataChannel(int pc, const char *label); // returns dc id
|
||||||
|
RTC_EXPORT int rtcAddDataChannelExt(int pc, const char *label, const char *protocol,
|
||||||
|
const rtcReliability *reliability); // returns dc id
|
||||||
|
// Equivalent to calling rtcAddDataChannel() and rtcSetLocalDescription()
|
||||||
RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
|
RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
|
||||||
RTC_EXPORT int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
|
RTC_EXPORT int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
|
||||||
const rtcReliability *reliability); // returns dc id
|
const rtcReliability *reliability); // returns dc id
|
||||||
@ -130,7 +134,7 @@ RTC_EXPORT int rtcGetDataChannelReliability(int dc, rtcReliability *reliability)
|
|||||||
|
|
||||||
// Track
|
// Track
|
||||||
RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
|
RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
|
||||||
RTC_EXPORT int rtcCreateTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
|
RTC_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
|
||||||
RTC_EXPORT int rtcDeleteTrack(int tr);
|
RTC_EXPORT int rtcDeleteTrack(int tr);
|
||||||
|
|
||||||
RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
|
RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
|
||||||
|
@ -85,8 +85,9 @@ std::optional<Description> PeerConnection::remoteDescription() const {
|
|||||||
void PeerConnection::setLocalDescription() {
|
void PeerConnection::setLocalDescription() {
|
||||||
PLOG_VERBOSE << "Setting local description";
|
PLOG_VERBOSE << "Setting local description";
|
||||||
|
|
||||||
if (std::atomic_load(&mIceTransport))
|
if (std::atomic_load(&mIceTransport)) {
|
||||||
throw std::logic_error("Local description is already set");
|
PLOG_DEBUG << "Local description is already set";
|
||||||
|
}
|
||||||
|
|
||||||
// RFC 5763: The endpoint that is the offerer MUST use the setup attribute value of
|
// RFC 5763: The endpoint that is the offerer MUST use the setup attribute value of
|
||||||
// setup:actpass.
|
// setup:actpass.
|
||||||
@ -197,8 +198,8 @@ std::optional<string> PeerConnection::remoteAddress() const {
|
|||||||
return iceTransport ? iceTransport->getRemoteAddress() : nullopt;
|
return iceTransport ? iceTransport->getRemoteAddress() : nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<DataChannel> PeerConnection::createDataChannel(string label, string protocol,
|
shared_ptr<DataChannel> PeerConnection::addDataChannel(string label, string protocol,
|
||||||
Reliability reliability) {
|
Reliability reliability) {
|
||||||
if (auto local = localDescription(); local && !local->hasApplication()) {
|
if (auto local = localDescription(); local && !local->hasApplication()) {
|
||||||
PLOG_ERROR << "The PeerConnection was negociated without DataChannel support.";
|
PLOG_ERROR << "The PeerConnection was negociated without DataChannel support.";
|
||||||
throw std::runtime_error("No DataChannel support on the PeerConnection");
|
throw std::runtime_error("No DataChannel support on the PeerConnection");
|
||||||
@ -214,18 +215,17 @@ shared_ptr<DataChannel> PeerConnection::createDataChannel(string label, string p
|
|||||||
auto channel =
|
auto channel =
|
||||||
emplaceDataChannel(role, std::move(label), std::move(protocol), std::move(reliability));
|
emplaceDataChannel(role, std::move(label), std::move(protocol), std::move(reliability));
|
||||||
|
|
||||||
if (!iceTransport) {
|
if (auto transport = std::atomic_load(&mSctpTransport))
|
||||||
// RFC 5763: The endpoint that is the offerer MUST use the setup attribute value of
|
if (transport->state() == SctpTransport::State::Connected)
|
||||||
// setup:actpass.
|
channel->open(transport);
|
||||||
// See https://tools.ietf.org/html/rfc5763#section-5
|
|
||||||
iceTransport = initIceTransport(Description::Role::ActPass);
|
return channel;
|
||||||
processLocalDescription(iceTransport->getLocalDescription(Description::Type::Offer));
|
}
|
||||||
iceTransport->gatherLocalCandidates();
|
|
||||||
} else {
|
shared_ptr<DataChannel> PeerConnection::createDataChannel(string label, string protocol,
|
||||||
if (auto transport = std::atomic_load(&mSctpTransport))
|
Reliability reliability) {
|
||||||
if (transport->state() == SctpTransport::State::Connected)
|
auto channel = addDataChannel(label, protocol, reliability);
|
||||||
channel->open(transport);
|
setLocalDescription();
|
||||||
}
|
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +255,7 @@ bool PeerConnection::hasMedia() const {
|
|||||||
return local && local->hasAudioOrVideo();
|
return local && local->hasAudioOrVideo();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Track> PeerConnection::createTrack(Description::Media description) {
|
std::shared_ptr<Track> PeerConnection::addTrack(Description::Media description) {
|
||||||
if (localDescription())
|
if (localDescription())
|
||||||
throw std::logic_error("Tracks must be created before local description");
|
throw std::logic_error("Tracks must be created before local description");
|
||||||
|
|
||||||
|
25
src/rtc.cpp
25
src/rtc.cpp
@ -268,12 +268,12 @@ int rtcDeletePeerConnection(int pc) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
int rtcCreateDataChannel(int pc, const char *label) {
|
int rtcAddDataChannel(int pc, const char *label) {
|
||||||
return rtcCreateDataChannelExt(pc, label, nullptr, nullptr);
|
return rtcAddDataChannelExt(pc, label, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
|
int rtcAddDataChannelExt(int pc, const char *label, const char *protocol,
|
||||||
const rtcReliability *reliability) {
|
const rtcReliability *reliability) {
|
||||||
return WRAP({
|
return WRAP({
|
||||||
Reliability r = {};
|
Reliability r = {};
|
||||||
if (reliability) {
|
if (reliability) {
|
||||||
@ -291,7 +291,7 @@ int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto peerConnection = getPeerConnection(pc);
|
auto peerConnection = getPeerConnection(pc);
|
||||||
int dc = emplaceDataChannel(peerConnection->createDataChannel(
|
int dc = emplaceDataChannel(peerConnection->addDataChannel(
|
||||||
string(label ? label : ""), string(protocol ? protocol : ""), r));
|
string(label ? label : ""), string(protocol ? protocol : ""), r));
|
||||||
if (auto ptr = getUserPointer(pc))
|
if (auto ptr = getUserPointer(pc))
|
||||||
rtcSetUserPointer(dc, *ptr);
|
rtcSetUserPointer(dc, *ptr);
|
||||||
@ -299,6 +299,17 @@ int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int rtcCreateDataChannel(int pc, const char *label) {
|
||||||
|
return rtcCreateDataChannelExt(pc, label, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
|
||||||
|
const rtcReliability *reliability) {
|
||||||
|
int dc = rtcAddDataChannelExt(pc, label, protocol, reliability);
|
||||||
|
rtcSetLocalDescription(pc);
|
||||||
|
return dc;
|
||||||
|
}
|
||||||
|
|
||||||
int rtcDeleteDataChannel(int dc) {
|
int rtcDeleteDataChannel(int dc) {
|
||||||
return WRAP({
|
return WRAP({
|
||||||
auto dataChannel = getDataChannel(dc);
|
auto dataChannel = getDataChannel(dc);
|
||||||
@ -313,13 +324,13 @@ int rtcDeleteDataChannel(int dc) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
int rtcCreateTrack(int pc, const char *mediaDescriptionSdp) {
|
int rtcAddTrack(int pc, const char *mediaDescriptionSdp) {
|
||||||
if (!mediaDescriptionSdp)
|
if (!mediaDescriptionSdp)
|
||||||
throw std::invalid_argument("Unexpected null pointer for track media description");
|
throw std::invalid_argument("Unexpected null pointer for track media description");
|
||||||
|
|
||||||
auto peerConnection = getPeerConnection(pc);
|
auto peerConnection = getPeerConnection(pc);
|
||||||
Description::Media media{string(mediaDescriptionSdp)};
|
Description::Media media{string(mediaDescriptionSdp)};
|
||||||
int tr = emplaceTrack(peerConnection->createTrack(std::move(media)));
|
int tr = emplaceTrack(peerConnection->addTrack(std::move(media)));
|
||||||
if (auto ptr = getUserPointer(pc))
|
if (auto ptr = getUserPointer(pc))
|
||||||
rtcSetUserPointer(tr, *ptr);
|
rtcSetUserPointer(tr, *ptr);
|
||||||
return tr;
|
return tr;
|
||||||
|
@ -151,7 +151,7 @@ int test_capi_track_main() {
|
|||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
// Peer 1: Create track
|
// Peer 1: Create track
|
||||||
peer1->tr = rtcCreateTrack(peer1->pc, mediaDescription);
|
peer1->tr = rtcAddTrack(peer1->pc, mediaDescription);
|
||||||
rtcSetOpenCallback(peer1->tr, openCallback);
|
rtcSetOpenCallback(peer1->tr, openCallback);
|
||||||
rtcSetClosedCallback(peer1->tr, closedCallback);
|
rtcSetClosedCallback(peer1->tr, closedCallback);
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ void test_track() {
|
|||||||
std::atomic_store(&t2, t);
|
std::atomic_store(&t2, t);
|
||||||
});
|
});
|
||||||
|
|
||||||
auto t1 = pc1->createTrack(Description::Video("test"));
|
auto t1 = pc1->addTrack(Description::Video("test"));
|
||||||
|
|
||||||
pc1->setLocalDescription();
|
pc1->setLocalDescription();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user