89 lines
4.0 KiB
C++
89 lines
4.0 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 = "192.168.2.26";
|
|
const auto serverPort = "1990";
|
|
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;
|
|
}
|
|
});
|
|
}
|
|
|
|
std::shared_ptr<ClientTrackData> WebRTCPublisher::addVideo(const std::shared_ptr<rtc::PeerConnection> pc,
|
|
const uint8_t payloadType, const uint32_t ssrc,
|
|
const std::string cname, const std::string msid,
|
|
const std::function<void()> onOpen) {
|
|
auto video = rtc::Description::Video(cname);
|
|
video.addH264Codec(payloadType);
|
|
video.addSSRC(ssrc, cname, msid, cname);
|
|
auto track = pc->addTrack(video);
|
|
// create RTP configuration
|
|
auto rtpConfig =
|
|
make_shared<rtc::RtpPacketizationConfig>(ssrc, cname, payloadType, rtc::H264RtpPacketizer::defaultClockRate);
|
|
// create packetizer
|
|
auto packetizer = make_shared<rtc::H264RtpPacketizer>(rtc::NalUnit::Separator::Length, rtpConfig);
|
|
// create H264 handler
|
|
auto h264Handler = make_shared<rtc::H264PacketizationHandler>(packetizer);
|
|
// add RTCP SR handler
|
|
auto srReporter = make_shared<rtc::RtcpSrReporter>(rtpConfig);
|
|
h264Handler->addToChain(srReporter);
|
|
// add RTCP NACK handler
|
|
auto nackResponder = std::make_shared<rtc::RtcpNackResponder>();
|
|
h264Handler->addToChain(nackResponder);
|
|
// set handler
|
|
track->setMediaHandler(h264Handler);
|
|
track->onOpen(onOpen);
|
|
auto trackData = make_shared<ClientTrackData>(track, srReporter);
|
|
return trackData;
|
|
}
|