|
|
@ -1,9 +1,10 @@ |
|
|
|
#include <cctype> |
|
|
|
#include <cctype> |
|
|
|
|
|
|
|
|
|
|
|
#include "WebRTCSession.h" |
|
|
|
|
|
|
|
#include "Logging.h" |
|
|
|
#include "Logging.h" |
|
|
|
|
|
|
|
#include "WebRTCSession.h" |
|
|
|
|
|
|
|
|
|
|
|
extern "C" { |
|
|
|
extern "C" |
|
|
|
|
|
|
|
{ |
|
|
|
#include "gst/gst.h" |
|
|
|
#include "gst/gst.h" |
|
|
|
#include "gst/sdp/sdp.h" |
|
|
|
#include "gst/sdp/sdp.h" |
|
|
|
|
|
|
|
|
|
|
@ -13,478 +14,498 @@ extern "C" { |
|
|
|
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(WebRTCSession::State) |
|
|
|
Q_DECLARE_METATYPE(WebRTCSession::State) |
|
|
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
WebRTCSession::WebRTCSession() |
|
|
|
bool isoffering_; |
|
|
|
: QObject() |
|
|
|
std::string localsdp_; |
|
|
|
|
|
|
|
std::vector<mtx::events::msg::CallCandidates::Candidate> localcandidates_; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gboolean newBusMessage(GstBus *bus G_GNUC_UNUSED, GstMessage *msg, gpointer user_data); |
|
|
|
|
|
|
|
GstWebRTCSessionDescription* parseSDP(const std::string &sdp, GstWebRTCSDPType type); |
|
|
|
|
|
|
|
void generateOffer(GstElement *webrtc); |
|
|
|
|
|
|
|
void setLocalDescription(GstPromise *promise, gpointer webrtc); |
|
|
|
|
|
|
|
void addLocalICECandidate(GstElement *webrtc G_GNUC_UNUSED, guint mlineIndex, gchar *candidate, gpointer G_GNUC_UNUSED); |
|
|
|
|
|
|
|
gboolean onICEGatheringCompletion(gpointer timerid); |
|
|
|
|
|
|
|
void iceConnectionStateChanged(GstElement *webrtcbin, GParamSpec *pspec G_GNUC_UNUSED, gpointer user_data G_GNUC_UNUSED); |
|
|
|
|
|
|
|
void createAnswer(GstPromise *promise, gpointer webrtc); |
|
|
|
|
|
|
|
void addDecodeBin(GstElement *webrtc G_GNUC_UNUSED, GstPad *newpad, GstElement *pipe); |
|
|
|
|
|
|
|
void linkNewPad(GstElement *decodebin G_GNUC_UNUSED, GstPad *newpad, GstElement *pipe); |
|
|
|
|
|
|
|
std::string::const_iterator findName(const std::string &sdp, const std::string &name); |
|
|
|
|
|
|
|
int getPayloadType(const std::string &sdp, const std::string &name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WebRTCSession::WebRTCSession() : QObject() |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
qRegisterMetaType<WebRTCSession::State>(); |
|
|
|
qRegisterMetaType<WebRTCSession::State>(); |
|
|
|
connect(this, &WebRTCSession::stateChanged, this, &WebRTCSession::setState); |
|
|
|
connect(this, &WebRTCSession::stateChanged, this, &WebRTCSession::setState); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
bool |
|
|
|
WebRTCSession::init(std::string *errorMessage) |
|
|
|
WebRTCSession::init(std::string *errorMessage) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (initialised_) |
|
|
|
if (initialised_) |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
|
|
GError *error = nullptr; |
|
|
|
GError *error = nullptr; |
|
|
|
if (!gst_init_check(nullptr, nullptr, &error)) { |
|
|
|
if (!gst_init_check(nullptr, nullptr, &error)) { |
|
|
|
std::string strError = std::string("WebRTC: failed to initialise GStreamer: "); |
|
|
|
std::string strError = std::string("WebRTC: failed to initialise GStreamer: "); |
|
|
|
if (error) { |
|
|
|
if (error) { |
|
|
|
strError += error->message; |
|
|
|
strError += error->message; |
|
|
|
g_error_free(error); |
|
|
|
g_error_free(error); |
|
|
|
} |
|
|
|
} |
|
|
|
nhlog::ui()->error(strError); |
|
|
|
nhlog::ui()->error(strError); |
|
|
|
if (errorMessage) |
|
|
|
if (errorMessage) |
|
|
|
*errorMessage = strError; |
|
|
|
*errorMessage = strError; |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
gchar *version = gst_version_string(); |
|
|
|
gchar *version = gst_version_string(); |
|
|
|
std::string gstVersion(version);
|
|
|
|
std::string gstVersion(version); |
|
|
|
g_free(version); |
|
|
|
g_free(version); |
|
|
|
nhlog::ui()->info("WebRTC: initialised " + gstVersion); |
|
|
|
nhlog::ui()->info("WebRTC: initialised " + gstVersion); |
|
|
|
|
|
|
|
|
|
|
|
// GStreamer Plugins:
|
|
|
|
// GStreamer Plugins:
|
|
|
|
// Base: audioconvert, audioresample, opus, playback, volume
|
|
|
|
// Base: audioconvert, audioresample, opus, playback, volume
|
|
|
|
// Good: autodetect, rtpmanager
|
|
|
|
// Good: autodetect, rtpmanager
|
|
|
|
// Bad: dtls, srtp, webrtc
|
|
|
|
// Bad: dtls, srtp, webrtc
|
|
|
|
// libnice [GLib]: nice
|
|
|
|
// libnice [GLib]: nice
|
|
|
|
initialised_ = true; |
|
|
|
initialised_ = true; |
|
|
|
std::string strError = gstVersion + ": Missing plugins: "; |
|
|
|
std::string strError = gstVersion + ": Missing plugins: "; |
|
|
|
const gchar *needed[] = {"audioconvert", "audioresample", "autodetect", "dtls", "nice", |
|
|
|
const gchar *needed[] = {"audioconvert", |
|
|
|
"opus", "playback", "rtpmanager", "srtp", "volume", "webrtc", nullptr}; |
|
|
|
"audioresample", |
|
|
|
GstRegistry *registry = gst_registry_get(); |
|
|
|
"autodetect", |
|
|
|
for (guint i = 0; i < g_strv_length((gchar**)needed); i++) { |
|
|
|
"dtls", |
|
|
|
GstPlugin *plugin = gst_registry_find_plugin(registry, needed[i]); |
|
|
|
"nice", |
|
|
|
if (!plugin) { |
|
|
|
"opus", |
|
|
|
strError += std::string(needed[i]) + " "; |
|
|
|
"playback", |
|
|
|
initialised_ = false; |
|
|
|
"rtpmanager", |
|
|
|
continue; |
|
|
|
"srtp", |
|
|
|
} |
|
|
|
"volume", |
|
|
|
gst_object_unref(plugin); |
|
|
|
"webrtc", |
|
|
|
} |
|
|
|
nullptr}; |
|
|
|
|
|
|
|
GstRegistry *registry = gst_registry_get(); |
|
|
|
if (!initialised_) { |
|
|
|
for (guint i = 0; i < g_strv_length((gchar **)needed); i++) { |
|
|
|
nhlog::ui()->error(strError); |
|
|
|
GstPlugin *plugin = gst_registry_find_plugin(registry, needed[i]); |
|
|
|
if (errorMessage) |
|
|
|
if (!plugin) { |
|
|
|
*errorMessage = strError; |
|
|
|
strError += std::string(needed[i]) + " "; |
|
|
|
} |
|
|
|
initialised_ = false; |
|
|
|
return initialised_; |
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
gst_object_unref(plugin); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!initialised_) { |
|
|
|
|
|
|
|
nhlog::ui()->error(strError); |
|
|
|
|
|
|
|
if (errorMessage) |
|
|
|
|
|
|
|
*errorMessage = strError; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return initialised_; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
namespace { |
|
|
|
WebRTCSession::createOffer() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
isoffering_ = true; |
|
|
|
|
|
|
|
localsdp_.clear(); |
|
|
|
|
|
|
|
localcandidates_.clear(); |
|
|
|
|
|
|
|
return startPipeline(111); // a dynamic opus payload type
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
bool isoffering_; |
|
|
|
WebRTCSession::acceptOffer(const std::string &sdp) |
|
|
|
std::string localsdp_; |
|
|
|
|
|
|
|
std::vector<mtx::events::msg::CallCandidates::Candidate> localcandidates_; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gboolean |
|
|
|
|
|
|
|
newBusMessage(GstBus *bus G_GNUC_UNUSED, GstMessage *msg, gpointer user_data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nhlog::ui()->debug("WebRTC: received offer:\n{}", sdp); |
|
|
|
WebRTCSession *session = static_cast<WebRTCSession *>(user_data); |
|
|
|
if (state_ != State::DISCONNECTED) |
|
|
|
switch (GST_MESSAGE_TYPE(msg)) { |
|
|
|
return false; |
|
|
|
case GST_MESSAGE_EOS: |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: end of stream"); |
|
|
|
isoffering_ = false; |
|
|
|
session->end(); |
|
|
|
localsdp_.clear(); |
|
|
|
break; |
|
|
|
localcandidates_.clear(); |
|
|
|
case GST_MESSAGE_ERROR: |
|
|
|
|
|
|
|
GError *error; |
|
|
|
int opusPayloadType = getPayloadType(sdp, "opus");
|
|
|
|
gchar *debug; |
|
|
|
if (opusPayloadType == -1) |
|
|
|
gst_message_parse_error(msg, &error, &debug); |
|
|
|
return false; |
|
|
|
nhlog::ui()->error( |
|
|
|
|
|
|
|
"WebRTC: error from element {}: {}", GST_OBJECT_NAME(msg->src), error->message); |
|
|
|
GstWebRTCSessionDescription *offer = parseSDP(sdp, GST_WEBRTC_SDP_TYPE_OFFER); |
|
|
|
g_clear_error(&error); |
|
|
|
if (!offer) |
|
|
|
g_free(debug); |
|
|
|
return false; |
|
|
|
session->end(); |
|
|
|
|
|
|
|
break; |
|
|
|
if (!startPipeline(opusPayloadType)) { |
|
|
|
default: |
|
|
|
gst_webrtc_session_description_free(offer); |
|
|
|
break; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return TRUE; |
|
|
|
|
|
|
|
|
|
|
|
// set-remote-description first, then create-answer
|
|
|
|
|
|
|
|
GstPromise *promise = gst_promise_new_with_change_func(createAnswer, webrtc_, nullptr); |
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc_, "set-remote-description", offer, promise); |
|
|
|
|
|
|
|
gst_webrtc_session_description_free(offer); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
GstWebRTCSessionDescription * |
|
|
|
WebRTCSession::startPipeline(int opusPayloadType) |
|
|
|
parseSDP(const std::string &sdp, GstWebRTCSDPType type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state_ != State::DISCONNECTED) |
|
|
|
GstSDPMessage *msg; |
|
|
|
return false; |
|
|
|
gst_sdp_message_new(&msg); |
|
|
|
|
|
|
|
if (gst_sdp_message_parse_buffer((guint8 *)sdp.c_str(), sdp.size(), msg) == GST_SDP_OK) { |
|
|
|
emit stateChanged(State::INITIATING); |
|
|
|
return gst_webrtc_session_description_new(type, msg); |
|
|
|
|
|
|
|
} else { |
|
|
|
if (!createPipeline(opusPayloadType)) |
|
|
|
nhlog::ui()->error("WebRTC: failed to parse remote session description"); |
|
|
|
return false; |
|
|
|
gst_object_unref(msg); |
|
|
|
|
|
|
|
return nullptr; |
|
|
|
webrtc_ = gst_bin_get_by_name(GST_BIN(pipe_), "webrtcbin"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!stunServer_.empty()) { |
|
|
|
|
|
|
|
nhlog::ui()->info("WebRTC: setting STUN server: {}", stunServer_); |
|
|
|
|
|
|
|
g_object_set(webrtc_, "stun-server", stunServer_.c_str(), nullptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto &uri : turnServers_) { |
|
|
|
|
|
|
|
nhlog::ui()->info("WebRTC: setting TURN server: {}", uri); |
|
|
|
|
|
|
|
gboolean udata; |
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc_, "add-turn-server", uri.c_str(), (gpointer)(&udata)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (turnServers_.empty()) |
|
|
|
|
|
|
|
nhlog::ui()->warn("WebRTC: no TURN server provided"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generate the offer when the pipeline goes to PLAYING
|
|
|
|
|
|
|
|
if (isoffering_) |
|
|
|
|
|
|
|
g_signal_connect(webrtc_, "on-negotiation-needed", G_CALLBACK(generateOffer), nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// on-ice-candidate is emitted when a local ICE candidate has been gathered
|
|
|
|
|
|
|
|
g_signal_connect(webrtc_, "on-ice-candidate", G_CALLBACK(addLocalICECandidate), nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// capture ICE failure
|
|
|
|
|
|
|
|
g_signal_connect(webrtc_, "notify::ice-connection-state", |
|
|
|
|
|
|
|
G_CALLBACK(iceConnectionStateChanged), nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// incoming streams trigger pad-added
|
|
|
|
|
|
|
|
gst_element_set_state(pipe_, GST_STATE_READY); |
|
|
|
|
|
|
|
g_signal_connect(webrtc_, "pad-added", G_CALLBACK(addDecodeBin), pipe_); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// webrtcbin lifetime is the same as that of the pipeline
|
|
|
|
|
|
|
|
gst_object_unref(webrtc_); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// start the pipeline
|
|
|
|
|
|
|
|
GstStateChangeReturn ret = gst_element_set_state(pipe_, GST_STATE_PLAYING); |
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: unable to start pipeline"); |
|
|
|
|
|
|
|
end(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipe_)); |
|
|
|
|
|
|
|
gst_bus_add_watch(bus, newBusMessage, this); |
|
|
|
|
|
|
|
gst_object_unref(bus); |
|
|
|
|
|
|
|
emit stateChanged(State::INITIATED); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload=" |
|
|
|
void |
|
|
|
|
|
|
|
setLocalDescription(GstPromise *promise, gpointer webrtc) |
|
|
|
bool |
|
|
|
|
|
|
|
WebRTCSession::createPipeline(int opusPayloadType) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
std::string pipeline("webrtcbin bundle-policy=max-bundle name=webrtcbin " |
|
|
|
const GstStructure *reply = gst_promise_get_reply(promise); |
|
|
|
"autoaudiosrc ! volume name=srclevel ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! " |
|
|
|
gboolean isAnswer = gst_structure_id_has_field(reply, g_quark_from_string("answer")); |
|
|
|
"queue ! " RTP_CAPS_OPUS + std::to_string(opusPayloadType) + " ! webrtcbin."); |
|
|
|
GstWebRTCSessionDescription *gstsdp = nullptr; |
|
|
|
|
|
|
|
gst_structure_get(reply, |
|
|
|
webrtc_ = nullptr; |
|
|
|
isAnswer ? "answer" : "offer", |
|
|
|
GError *error = nullptr; |
|
|
|
GST_TYPE_WEBRTC_SESSION_DESCRIPTION, |
|
|
|
pipe_ = gst_parse_launch(pipeline.c_str(), &error); |
|
|
|
&gstsdp, |
|
|
|
if (error) { |
|
|
|
nullptr); |
|
|
|
nhlog::ui()->error("WebRTC: failed to parse pipeline: {}", error->message); |
|
|
|
gst_promise_unref(promise); |
|
|
|
g_error_free(error); |
|
|
|
g_signal_emit_by_name(webrtc, "set-local-description", gstsdp, nullptr); |
|
|
|
end(); |
|
|
|
|
|
|
|
return false; |
|
|
|
gchar *sdp = gst_sdp_message_as_text(gstsdp->sdp); |
|
|
|
} |
|
|
|
localsdp_ = std::string(sdp); |
|
|
|
return true; |
|
|
|
g_free(sdp); |
|
|
|
|
|
|
|
gst_webrtc_session_description_free(gstsdp); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nhlog::ui()->debug( |
|
|
|
|
|
|
|
"WebRTC: local description set ({}):\n{}", isAnswer ? "answer" : "offer", localsdp_); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
void |
|
|
|
WebRTCSession::acceptAnswer(const std::string &sdp) |
|
|
|
createOffer(GstElement *webrtc) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nhlog::ui()->debug("WebRTC: received answer:\n{}", sdp); |
|
|
|
// create-offer first, then set-local-description
|
|
|
|
if (state_ != State::OFFERSENT) |
|
|
|
GstPromise *promise = |
|
|
|
return false; |
|
|
|
gst_promise_new_with_change_func(setLocalDescription, webrtc, nullptr); |
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc, "create-offer", nullptr, promise); |
|
|
|
GstWebRTCSessionDescription *answer = parseSDP(sdp, GST_WEBRTC_SDP_TYPE_ANSWER); |
|
|
|
|
|
|
|
if (!answer) { |
|
|
|
|
|
|
|
end(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc_, "set-remote-description", answer, nullptr); |
|
|
|
|
|
|
|
gst_webrtc_session_description_free(answer); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
WebRTCSession::acceptICECandidates(const std::vector<mtx::events::msg::CallCandidates::Candidate> &candidates) |
|
|
|
createAnswer(GstPromise *promise, gpointer webrtc) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state_ >= State::INITIATED) { |
|
|
|
// create-answer first, then set-local-description
|
|
|
|
for (const auto &c : candidates) { |
|
|
|
gst_promise_unref(promise); |
|
|
|
nhlog::ui()->debug("WebRTC: remote candidate: (m-line:{}):{}", c.sdpMLineIndex, c.candidate); |
|
|
|
promise = gst_promise_new_with_change_func(setLocalDescription, webrtc, nullptr); |
|
|
|
g_signal_emit_by_name(webrtc_, "add-ice-candidate", c.sdpMLineIndex, c.candidate.c_str()); |
|
|
|
g_signal_emit_by_name(webrtc, "create-answer", nullptr, promise); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
gboolean |
|
|
|
WebRTCSession::toggleMuteAudioSrc(bool &isMuted) |
|
|
|
onICEGatheringCompletion(gpointer timerid) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state_ < State::INITIATED) |
|
|
|
*(guint *)(timerid) = 0; |
|
|
|
return false; |
|
|
|
if (isoffering_) { |
|
|
|
|
|
|
|
emit WebRTCSession::instance().offerCreated(localsdp_, localcandidates_); |
|
|
|
GstElement *srclevel = gst_bin_get_by_name(GST_BIN(pipe_), "srclevel"); |
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::OFFERSENT); |
|
|
|
if (!srclevel) |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
emit WebRTCSession::instance().answerCreated(localsdp_, localcandidates_); |
|
|
|
|
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::ANSWERSENT); |
|
|
|
gboolean muted; |
|
|
|
} |
|
|
|
g_object_get(srclevel, "mute", &muted, nullptr); |
|
|
|
return FALSE; |
|
|
|
g_object_set(srclevel, "mute", !muted, nullptr); |
|
|
|
|
|
|
|
gst_object_unref(srclevel); |
|
|
|
|
|
|
|
isMuted = !muted; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
WebRTCSession::end() |
|
|
|
addLocalICECandidate(GstElement *webrtc G_GNUC_UNUSED, |
|
|
|
|
|
|
|
guint mlineIndex, |
|
|
|
|
|
|
|
gchar *candidate, |
|
|
|
|
|
|
|
gpointer G_GNUC_UNUSED) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nhlog::ui()->debug("WebRTC: ending session"); |
|
|
|
nhlog::ui()->debug("WebRTC: local candidate: (m-line:{}):{}", mlineIndex, candidate); |
|
|
|
if (pipe_) { |
|
|
|
|
|
|
|
gst_element_set_state(pipe_, GST_STATE_NULL); |
|
|
|
|
|
|
|
gst_object_unref(pipe_); |
|
|
|
|
|
|
|
pipe_ = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
webrtc_ = nullptr; |
|
|
|
|
|
|
|
if (state_ != State::DISCONNECTED) |
|
|
|
|
|
|
|
emit stateChanged(State::DISCONNECTED); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
if (WebRTCSession::instance().state() >= WebRTCSession::State::OFFERSENT) { |
|
|
|
|
|
|
|
emit WebRTCSession::instance().newICECandidate( |
|
|
|
|
|
|
|
{"audio", (uint16_t)mlineIndex, candidate}); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string::const_iterator findName(const std::string &sdp, const std::string &name) |
|
|
|
localcandidates_.push_back({"audio", (uint16_t)mlineIndex, candidate}); |
|
|
|
{ |
|
|
|
|
|
|
|
return std::search(sdp.cbegin(), sdp.cend(), name.cbegin(), name.cend(), |
|
|
|
|
|
|
|
[](unsigned char c1, unsigned char c2) {return std::tolower(c1) == std::tolower(c2);}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int getPayloadType(const std::string &sdp, const std::string &name) |
|
|
|
// GStreamer v1.16: webrtcbin's notify::ice-gathering-state triggers
|
|
|
|
{ |
|
|
|
// GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE too early. Fixed in v1.18. Use a 100ms timeout in
|
|
|
|
// eg a=rtpmap:111 opus/48000/2
|
|
|
|
// the meantime
|
|
|
|
auto e = findName(sdp, name); |
|
|
|
static guint timerid = 0; |
|
|
|
if (e == sdp.cend()) { |
|
|
|
if (timerid) |
|
|
|
nhlog::ui()->error("WebRTC: remote offer - " + name + " attribute missing"); |
|
|
|
g_source_remove(timerid); |
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (auto s = sdp.rfind(':', e - sdp.cbegin()); s == std::string::npos) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: remote offer - unable to determine " + name + " payload type"); |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
++s; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return std::stoi(std::string(sdp, s, e - sdp.cbegin() - s)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch(...) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: remote offer - unable to determine " + name + " payload type"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gboolean |
|
|
|
timerid = g_timeout_add(100, onICEGatheringCompletion, &timerid); |
|
|
|
newBusMessage(GstBus *bus G_GNUC_UNUSED, GstMessage *msg, gpointer user_data) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
WebRTCSession *session = (WebRTCSession*)user_data; |
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE(msg)) { |
|
|
|
|
|
|
|
case GST_MESSAGE_EOS: |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: end of stream"); |
|
|
|
|
|
|
|
session->end(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case GST_MESSAGE_ERROR: |
|
|
|
|
|
|
|
GError *error; |
|
|
|
|
|
|
|
gchar *debug; |
|
|
|
|
|
|
|
gst_message_parse_error(msg, &error, &debug); |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: error from element {}: {}", GST_OBJECT_NAME(msg->src), error->message); |
|
|
|
|
|
|
|
g_clear_error(&error); |
|
|
|
|
|
|
|
g_free(debug); |
|
|
|
|
|
|
|
session->end(); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return TRUE; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GstWebRTCSessionDescription* |
|
|
|
void |
|
|
|
parseSDP(const std::string &sdp, GstWebRTCSDPType type) |
|
|
|
iceConnectionStateChanged(GstElement *webrtc, |
|
|
|
|
|
|
|
GParamSpec *pspec G_GNUC_UNUSED, |
|
|
|
|
|
|
|
gpointer user_data G_GNUC_UNUSED) |
|
|
|
{ |
|
|
|
{ |
|
|
|
GstSDPMessage *msg; |
|
|
|
GstWebRTCICEConnectionState newState; |
|
|
|
gst_sdp_message_new(&msg); |
|
|
|
g_object_get(webrtc, "ice-connection-state", &newState, nullptr); |
|
|
|
if (gst_sdp_message_parse_buffer((guint8*)sdp.c_str(), sdp.size(), msg) == GST_SDP_OK) { |
|
|
|
switch (newState) { |
|
|
|
return gst_webrtc_session_description_new(type, msg); |
|
|
|
case GST_WEBRTC_ICE_CONNECTION_STATE_CHECKING: |
|
|
|
} |
|
|
|
nhlog::ui()->debug("WebRTC: GstWebRTCICEConnectionState -> Checking"); |
|
|
|
else { |
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::CONNECTING); |
|
|
|
nhlog::ui()->error("WebRTC: failed to parse remote session description"); |
|
|
|
break; |
|
|
|
gst_object_unref(msg); |
|
|
|
case GST_WEBRTC_ICE_CONNECTION_STATE_FAILED: |
|
|
|
return nullptr; |
|
|
|
nhlog::ui()->error("WebRTC: GstWebRTCICEConnectionState -> Failed"); |
|
|
|
} |
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::ICEFAILED); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
generateOffer(GstElement *webrtc) |
|
|
|
linkNewPad(GstElement *decodebin G_GNUC_UNUSED, GstPad *newpad, GstElement *pipe) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// create-offer first, then set-local-description
|
|
|
|
GstCaps *caps = gst_pad_get_current_caps(newpad); |
|
|
|
GstPromise *promise = gst_promise_new_with_change_func(setLocalDescription, webrtc, nullptr); |
|
|
|
if (!caps) |
|
|
|
g_signal_emit_by_name(webrtc, "create-offer", nullptr, promise); |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const gchar *name = gst_structure_get_name(gst_caps_get_structure(caps, 0)); |
|
|
|
|
|
|
|
gst_caps_unref(caps); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GstPad *queuepad = nullptr; |
|
|
|
|
|
|
|
if (g_str_has_prefix(name, "audio")) { |
|
|
|
|
|
|
|
nhlog::ui()->debug("WebRTC: received incoming audio stream"); |
|
|
|
|
|
|
|
GstElement *queue = gst_element_factory_make("queue", nullptr); |
|
|
|
|
|
|
|
GstElement *convert = gst_element_factory_make("audioconvert", nullptr); |
|
|
|
|
|
|
|
GstElement *resample = gst_element_factory_make("audioresample", nullptr); |
|
|
|
|
|
|
|
GstElement *sink = gst_element_factory_make("autoaudiosink", nullptr); |
|
|
|
|
|
|
|
gst_bin_add_many(GST_BIN(pipe), queue, convert, resample, sink, nullptr); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(queue); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(convert); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(resample); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(sink); |
|
|
|
|
|
|
|
gst_element_link_many(queue, convert, resample, sink, nullptr); |
|
|
|
|
|
|
|
queuepad = gst_element_get_static_pad(queue, "sink"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (queuepad) { |
|
|
|
|
|
|
|
if (GST_PAD_LINK_FAILED(gst_pad_link(newpad, queuepad))) |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: unable to link new pad"); |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
emit WebRTCSession::instance().stateChanged( |
|
|
|
|
|
|
|
WebRTCSession::State::CONNECTED); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
gst_object_unref(queuepad); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
setLocalDescription(GstPromise *promise, gpointer webrtc) |
|
|
|
addDecodeBin(GstElement *webrtc G_GNUC_UNUSED, GstPad *newpad, GstElement *pipe) |
|
|
|
{ |
|
|
|
{ |
|
|
|
const GstStructure *reply = gst_promise_get_reply(promise); |
|
|
|
if (GST_PAD_DIRECTION(newpad) != GST_PAD_SRC) |
|
|
|
gboolean isAnswer = gst_structure_id_has_field(reply, g_quark_from_string("answer")); |
|
|
|
return; |
|
|
|
GstWebRTCSessionDescription *gstsdp = nullptr; |
|
|
|
|
|
|
|
gst_structure_get(reply, isAnswer ? "answer" : "offer", GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &gstsdp, nullptr); |
|
|
|
nhlog::ui()->debug("WebRTC: received incoming stream"); |
|
|
|
gst_promise_unref(promise); |
|
|
|
GstElement *decodebin = gst_element_factory_make("decodebin", nullptr); |
|
|
|
g_signal_emit_by_name(webrtc, "set-local-description", gstsdp, nullptr); |
|
|
|
g_signal_connect(decodebin, "pad-added", G_CALLBACK(linkNewPad), pipe); |
|
|
|
|
|
|
|
gst_bin_add(GST_BIN(pipe), decodebin); |
|
|
|
gchar *sdp = gst_sdp_message_as_text(gstsdp->sdp); |
|
|
|
gst_element_sync_state_with_parent(decodebin); |
|
|
|
localsdp_ = std::string(sdp); |
|
|
|
GstPad *sinkpad = gst_element_get_static_pad(decodebin, "sink"); |
|
|
|
g_free(sdp); |
|
|
|
if (GST_PAD_LINK_FAILED(gst_pad_link(newpad, sinkpad))) |
|
|
|
gst_webrtc_session_description_free(gstsdp); |
|
|
|
nhlog::ui()->error("WebRTC: unable to link new pad"); |
|
|
|
|
|
|
|
gst_object_unref(sinkpad); |
|
|
|
nhlog::ui()->debug("WebRTC: local description set ({}):\n{}", isAnswer ? "answer" : "offer", localsdp_); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
std::string::const_iterator |
|
|
|
addLocalICECandidate(GstElement *webrtc G_GNUC_UNUSED, guint mlineIndex, gchar *candidate, gpointer G_GNUC_UNUSED) |
|
|
|
findName(const std::string &sdp, const std::string &name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nhlog::ui()->debug("WebRTC: local candidate: (m-line:{}):{}", mlineIndex, candidate); |
|
|
|
return std::search( |
|
|
|
|
|
|
|
sdp.cbegin(), |
|
|
|
|
|
|
|
sdp.cend(), |
|
|
|
|
|
|
|
name.cbegin(), |
|
|
|
|
|
|
|
name.cend(), |
|
|
|
|
|
|
|
[](unsigned char c1, unsigned char c2) { return std::tolower(c1) == std::tolower(c2); }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (WebRTCSession::instance().state() >= WebRTCSession::State::OFFERSENT) { |
|
|
|
int |
|
|
|
emit WebRTCSession::instance().newICECandidate({"audio", (uint16_t)mlineIndex, candidate}); |
|
|
|
getPayloadType(const std::string &sdp, const std::string &name) |
|
|
|
return; |
|
|
|
{ |
|
|
|
} |
|
|
|
// eg a=rtpmap:111 opus/48000/2
|
|
|
|
|
|
|
|
auto e = findName(sdp, name); |
|
|
|
|
|
|
|
if (e == sdp.cend()) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: remote offer - " + name + " attribute missing"); |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (auto s = sdp.rfind(':', e - sdp.cbegin()); s == std::string::npos) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: remote offer - unable to determine " + name + |
|
|
|
|
|
|
|
" payload type"); |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
++s; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return std::stoi(std::string(sdp, s, e - sdp.cbegin() - s)); |
|
|
|
|
|
|
|
} catch (...) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: remote offer - unable to determine " + name + |
|
|
|
|
|
|
|
" payload type"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
localcandidates_.push_back({"audio", (uint16_t)mlineIndex, candidate}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GStreamer v1.16: webrtcbin's notify::ice-gathering-state triggers GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE too early
|
|
|
|
bool |
|
|
|
// fixed in v1.18
|
|
|
|
WebRTCSession::createOffer() |
|
|
|
// use a 100ms timeout in the meantime
|
|
|
|
{ |
|
|
|
static guint timerid = 0; |
|
|
|
isoffering_ = true; |
|
|
|
if (timerid) |
|
|
|
localsdp_.clear(); |
|
|
|
g_source_remove(timerid); |
|
|
|
localcandidates_.clear(); |
|
|
|
|
|
|
|
return startPipeline(111); // a dynamic opus payload type
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
timerid = g_timeout_add(100, onICEGatheringCompletion, &timerid); |
|
|
|
bool |
|
|
|
|
|
|
|
WebRTCSession::acceptOffer(const std::string &sdp) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
nhlog::ui()->debug("WebRTC: received offer:\n{}", sdp); |
|
|
|
|
|
|
|
if (state_ != State::DISCONNECTED) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isoffering_ = false; |
|
|
|
|
|
|
|
localsdp_.clear(); |
|
|
|
|
|
|
|
localcandidates_.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int opusPayloadType = getPayloadType(sdp, "opus"); |
|
|
|
|
|
|
|
if (opusPayloadType == -1) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GstWebRTCSessionDescription *offer = parseSDP(sdp, GST_WEBRTC_SDP_TYPE_OFFER); |
|
|
|
|
|
|
|
if (!offer) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!startPipeline(opusPayloadType)) { |
|
|
|
|
|
|
|
gst_webrtc_session_description_free(offer); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set-remote-description first, then create-answer
|
|
|
|
|
|
|
|
GstPromise *promise = gst_promise_new_with_change_func(createAnswer, webrtc_, nullptr); |
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc_, "set-remote-description", offer, promise); |
|
|
|
|
|
|
|
gst_webrtc_session_description_free(offer); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
gboolean |
|
|
|
bool |
|
|
|
onICEGatheringCompletion(gpointer timerid) |
|
|
|
WebRTCSession::acceptAnswer(const std::string &sdp) |
|
|
|
{ |
|
|
|
{ |
|
|
|
*(guint*)(timerid) = 0; |
|
|
|
nhlog::ui()->debug("WebRTC: received answer:\n{}", sdp); |
|
|
|
if (isoffering_) { |
|
|
|
if (state_ != State::OFFERSENT) |
|
|
|
emit WebRTCSession::instance().offerCreated(localsdp_, localcandidates_); |
|
|
|
return false; |
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::OFFERSENT); |
|
|
|
|
|
|
|
} |
|
|
|
GstWebRTCSessionDescription *answer = parseSDP(sdp, GST_WEBRTC_SDP_TYPE_ANSWER); |
|
|
|
else { |
|
|
|
if (!answer) { |
|
|
|
emit WebRTCSession::instance().answerCreated(localsdp_, localcandidates_); |
|
|
|
end(); |
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::ANSWERSENT); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return FALSE; |
|
|
|
|
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc_, "set-remote-description", answer, nullptr); |
|
|
|
|
|
|
|
gst_webrtc_session_description_free(answer); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
iceConnectionStateChanged(GstElement *webrtc, GParamSpec *pspec G_GNUC_UNUSED, gpointer user_data G_GNUC_UNUSED) |
|
|
|
WebRTCSession::acceptICECandidates( |
|
|
|
|
|
|
|
const std::vector<mtx::events::msg::CallCandidates::Candidate> &candidates) |
|
|
|
{ |
|
|
|
{ |
|
|
|
GstWebRTCICEConnectionState newState; |
|
|
|
if (state_ >= State::INITIATED) { |
|
|
|
g_object_get(webrtc, "ice-connection-state", &newState, nullptr); |
|
|
|
for (const auto &c : candidates) { |
|
|
|
switch (newState) { |
|
|
|
nhlog::ui()->debug( |
|
|
|
case GST_WEBRTC_ICE_CONNECTION_STATE_CHECKING: |
|
|
|
"WebRTC: remote candidate: (m-line:{}):{}", c.sdpMLineIndex, c.candidate); |
|
|
|
nhlog::ui()->debug("WebRTC: GstWebRTCICEConnectionState -> Checking"); |
|
|
|
g_signal_emit_by_name( |
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::CONNECTING); |
|
|
|
webrtc_, "add-ice-candidate", c.sdpMLineIndex, c.candidate.c_str()); |
|
|
|
break; |
|
|
|
} |
|
|
|
case GST_WEBRTC_ICE_CONNECTION_STATE_FAILED: |
|
|
|
} |
|
|
|
nhlog::ui()->error("WebRTC: GstWebRTCICEConnectionState -> Failed"); |
|
|
|
|
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::ICEFAILED); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
bool |
|
|
|
createAnswer(GstPromise *promise, gpointer webrtc) |
|
|
|
WebRTCSession::startPipeline(int opusPayloadType) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// create-answer first, then set-local-description
|
|
|
|
if (state_ != State::DISCONNECTED) |
|
|
|
gst_promise_unref(promise); |
|
|
|
return false; |
|
|
|
promise = gst_promise_new_with_change_func(setLocalDescription, webrtc, nullptr); |
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc, "create-answer", nullptr, promise); |
|
|
|
emit stateChanged(State::INITIATING); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!createPipeline(opusPayloadType)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
webrtc_ = gst_bin_get_by_name(GST_BIN(pipe_), "webrtcbin"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!stunServer_.empty()) { |
|
|
|
|
|
|
|
nhlog::ui()->info("WebRTC: setting STUN server: {}", stunServer_); |
|
|
|
|
|
|
|
g_object_set(webrtc_, "stun-server", stunServer_.c_str(), nullptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto &uri : turnServers_) { |
|
|
|
|
|
|
|
nhlog::ui()->info("WebRTC: setting TURN server: {}", uri); |
|
|
|
|
|
|
|
gboolean udata; |
|
|
|
|
|
|
|
g_signal_emit_by_name(webrtc_, "add-turn-server", uri.c_str(), (gpointer)(&udata)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (turnServers_.empty()) |
|
|
|
|
|
|
|
nhlog::ui()->warn("WebRTC: no TURN server provided"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generate the offer when the pipeline goes to PLAYING
|
|
|
|
|
|
|
|
if (isoffering_) |
|
|
|
|
|
|
|
g_signal_connect( |
|
|
|
|
|
|
|
webrtc_, "on-negotiation-needed", G_CALLBACK(::createOffer), nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// on-ice-candidate is emitted when a local ICE candidate has been gathered
|
|
|
|
|
|
|
|
g_signal_connect(webrtc_, "on-ice-candidate", G_CALLBACK(addLocalICECandidate), nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// capture ICE failure
|
|
|
|
|
|
|
|
g_signal_connect( |
|
|
|
|
|
|
|
webrtc_, "notify::ice-connection-state", G_CALLBACK(iceConnectionStateChanged), nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// incoming streams trigger pad-added
|
|
|
|
|
|
|
|
gst_element_set_state(pipe_, GST_STATE_READY); |
|
|
|
|
|
|
|
g_signal_connect(webrtc_, "pad-added", G_CALLBACK(addDecodeBin), pipe_); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// webrtcbin lifetime is the same as that of the pipeline
|
|
|
|
|
|
|
|
gst_object_unref(webrtc_); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// start the pipeline
|
|
|
|
|
|
|
|
GstStateChangeReturn ret = gst_element_set_state(pipe_, GST_STATE_PLAYING); |
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE) { |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: unable to start pipeline"); |
|
|
|
|
|
|
|
end(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipe_)); |
|
|
|
|
|
|
|
gst_bus_add_watch(bus, newBusMessage, this); |
|
|
|
|
|
|
|
gst_object_unref(bus); |
|
|
|
|
|
|
|
emit stateChanged(State::INITIATED); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload=" |
|
|
|
addDecodeBin(GstElement *webrtc G_GNUC_UNUSED, GstPad *newpad, GstElement *pipe) |
|
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
|
|
|
WebRTCSession::createPipeline(int opusPayloadType) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (GST_PAD_DIRECTION(newpad) != GST_PAD_SRC) |
|
|
|
std::string pipeline("webrtcbin bundle-policy=max-bundle name=webrtcbin " |
|
|
|
return; |
|
|
|
"autoaudiosrc ! volume name=srclevel ! audioconvert ! " |
|
|
|
|
|
|
|
"audioresample ! queue ! opusenc ! rtpopuspay ! " |
|
|
|
nhlog::ui()->debug("WebRTC: received incoming stream"); |
|
|
|
"queue ! " RTP_CAPS_OPUS + |
|
|
|
GstElement *decodebin = gst_element_factory_make("decodebin", nullptr); |
|
|
|
std::to_string(opusPayloadType) + " ! webrtcbin."); |
|
|
|
g_signal_connect(decodebin, "pad-added", G_CALLBACK(linkNewPad), pipe); |
|
|
|
|
|
|
|
gst_bin_add(GST_BIN(pipe), decodebin); |
|
|
|
webrtc_ = nullptr; |
|
|
|
gst_element_sync_state_with_parent(decodebin); |
|
|
|
GError *error = nullptr; |
|
|
|
GstPad *sinkpad = gst_element_get_static_pad(decodebin, "sink"); |
|
|
|
pipe_ = gst_parse_launch(pipeline.c_str(), &error); |
|
|
|
if (GST_PAD_LINK_FAILED(gst_pad_link(newpad, sinkpad))) |
|
|
|
if (error) { |
|
|
|
nhlog::ui()->error("WebRTC: unable to link new pad"); |
|
|
|
nhlog::ui()->error("WebRTC: failed to parse pipeline: {}", error->message); |
|
|
|
gst_object_unref(sinkpad); |
|
|
|
g_error_free(error); |
|
|
|
|
|
|
|
end(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
bool |
|
|
|
linkNewPad(GstElement *decodebin G_GNUC_UNUSED, GstPad *newpad, GstElement *pipe) |
|
|
|
WebRTCSession::toggleMuteAudioSrc(bool &isMuted) |
|
|
|
{ |
|
|
|
{ |
|
|
|
GstCaps *caps = gst_pad_get_current_caps(newpad); |
|
|
|
if (state_ < State::INITIATED) |
|
|
|
if (!caps) |
|
|
|
return false; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
GstElement *srclevel = gst_bin_get_by_name(GST_BIN(pipe_), "srclevel"); |
|
|
|
const gchar *name = gst_structure_get_name(gst_caps_get_structure(caps, 0)); |
|
|
|
if (!srclevel) |
|
|
|
gst_caps_unref(caps); |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
GstPad *queuepad = nullptr; |
|
|
|
gboolean muted; |
|
|
|
if (g_str_has_prefix(name, "audio")) { |
|
|
|
g_object_get(srclevel, "mute", &muted, nullptr); |
|
|
|
nhlog::ui()->debug("WebRTC: received incoming audio stream"); |
|
|
|
g_object_set(srclevel, "mute", !muted, nullptr); |
|
|
|
GstElement *queue = gst_element_factory_make("queue", nullptr); |
|
|
|
gst_object_unref(srclevel); |
|
|
|
GstElement *convert = gst_element_factory_make("audioconvert", nullptr); |
|
|
|
isMuted = !muted; |
|
|
|
GstElement *resample = gst_element_factory_make("audioresample", nullptr); |
|
|
|
return true; |
|
|
|
GstElement *sink = gst_element_factory_make("autoaudiosink", nullptr); |
|
|
|
|
|
|
|
gst_bin_add_many(GST_BIN(pipe), queue, convert, resample, sink, nullptr); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(queue); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(convert); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(resample); |
|
|
|
|
|
|
|
gst_element_sync_state_with_parent(sink); |
|
|
|
|
|
|
|
gst_element_link_many(queue, convert, resample, sink, nullptr); |
|
|
|
|
|
|
|
queuepad = gst_element_get_static_pad(queue, "sink"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (queuepad) { |
|
|
|
|
|
|
|
if (GST_PAD_LINK_FAILED(gst_pad_link(newpad, queuepad))) |
|
|
|
|
|
|
|
nhlog::ui()->error("WebRTC: unable to link new pad"); |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
emit WebRTCSession::instance().stateChanged(WebRTCSession::State::CONNECTED); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
gst_object_unref(queuepad); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
|
|
|
WebRTCSession::end() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
nhlog::ui()->debug("WebRTC: ending session"); |
|
|
|
|
|
|
|
if (pipe_) { |
|
|
|
|
|
|
|
gst_element_set_state(pipe_, GST_STATE_NULL); |
|
|
|
|
|
|
|
gst_object_unref(pipe_); |
|
|
|
|
|
|
|
pipe_ = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
webrtc_ = nullptr; |
|
|
|
|
|
|
|
if (state_ != State::DISCONNECTED) |
|
|
|
|
|
|
|
emit stateChanged(State::DISCONNECTED); |
|
|
|
} |
|
|
|
} |
|
|
|