FFmpegPlayer/WebRTCPublisher.cpp
2024-01-18 22:22:26 +08:00

61 lines
2.5 KiB
C++

#include "WebRTCPublisher.h"
#include "BoostLog.h"
#include "NetworkUtility.h"
#include <boost/json/object.hpp>
#include <boost/json/serialize.hpp>
WebRTCPublisher::WebRTCPublisher(boost::asio::io_context &ioContext) : m_ioContext(ioContext) {
m_configuration.iceServers.emplace_back("mystunserver.org:3478");
m_peer = std::make_shared<rtc::PeerConnection>(m_configuration);
m_peer->onLocalDescription([&, this](rtc::Description sdp) {
LOG(info) << "Send the SDP to the remote peer: " << std::string(sdp);
const auto serverIp = "172.16.7.16";
const auto serverPort = "443";
const auto streamUrl = "/live/livestream";
std::ostringstream oss;
oss << "https://" << serverIp << ":" << serverPort << "/rtc/v1/publish/";
boost::json::object requestJson;
requestJson["api"] = oss.str();
requestJson["streamurl"] = streamUrl;
requestJson["sdp"] = std::string(sdp);
auto request = boost::json::serialize(requestJson);
LOG(info) << "body: " << request;
boost::system::error_code error;
Http::Client https(m_ioContext, Http::SSL);
https.loadRootCertificates(error);
auto reply = https.post(serverIp, serverPort, "/rtc/v1/publish/", request, error);
LOG(info) << "reply " << reply;
// Send the SDP to the remote peer
// MY_SEND_DESCRIPTION_TO_REMOTE(std::string(sdp));
});
m_peer->onLocalCandidate([](rtc::Candidate candidate) {
// Send the candidate to the remote peer
// MY_SEND_CANDIDATE_TO_REMOTE(candidate.candidate(), candidate.mid());
});
// MY_ON_RECV_DESCRIPTION_FROM_REMOTE([&pc](std::string sdp) { pc.setRemoteDescription(rtc::Description(sdp)); });
// MY_ON_RECV_CANDIDATE_FROM_REMOTE(
// [&pc](std::string candidate, std::string mid) { pc.addRemoteCandidate(rtc::Candidate(candidate, mid)); });
m_peer->onStateChange([](rtc::PeerConnection::State state) { LOG(info) << "State: " << (int)state; });
m_peer->onGatheringStateChange(
[](rtc::PeerConnection::GatheringState state) { LOG(info) << "Gathering state: " << (int)state; });
m_dataChannel = m_peer->createDataChannel("test");
m_dataChannel->onOpen([]() { LOG(info) << "Open" << std::endl; });
m_dataChannel->onMessage([](std::variant<rtc::binary, rtc::string> message) {
if (std::holds_alternative<rtc::string>(message)) {
LOG(info) << "Received: " << get<rtc::string>(message) << std::endl;
}
});
}