2022-06-18 14:34:58 +08:00
|
|
|
|
/*
|
2021-10-15 16:27:17 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "WebRtcPusher.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/config.h"
|
2023-05-11 20:47:40 +08:00
|
|
|
|
#include "Rtsp/RtspMediaSourceImp.h"
|
2021-10-15 16:27:17 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
2022-09-18 21:03:05 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2021-10-15 16:27:17 +08:00
|
|
|
|
WebRtcPusher::Ptr WebRtcPusher::create(const EventPoller::Ptr &poller,
|
2023-05-11 20:47:40 +08:00
|
|
|
|
const RtspMediaSource::Ptr &src,
|
2022-01-10 17:43:28 +08:00
|
|
|
|
const std::shared_ptr<void> &ownership,
|
2022-06-04 11:06:35 +08:00
|
|
|
|
const MediaInfo &info,
|
2022-11-27 12:43:16 +08:00
|
|
|
|
const ProtocolOption &option,
|
2022-11-30 18:24:35 +08:00
|
|
|
|
bool preferred_tcp) {
|
|
|
|
|
WebRtcPusher::Ptr ret(new WebRtcPusher(poller, src, ownership, info, option,preferred_tcp), [](WebRtcPusher *ptr) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
ptr->onDestory();
|
|
|
|
|
delete ptr;
|
|
|
|
|
});
|
|
|
|
|
ret->onCreate();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller,
|
2023-05-11 20:47:40 +08:00
|
|
|
|
const RtspMediaSource::Ptr &src,
|
2022-01-10 17:43:28 +08:00
|
|
|
|
const std::shared_ptr<void> &ownership,
|
2022-06-04 11:06:35 +08:00
|
|
|
|
const MediaInfo &info,
|
2022-11-27 12:43:16 +08:00
|
|
|
|
const ProtocolOption &option,
|
2022-11-30 18:24:35 +08:00
|
|
|
|
bool preferred_tcp) : WebRtcTransportImp(poller,preferred_tcp) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
_media_info = info;
|
|
|
|
|
_push_src = src;
|
2022-01-10 17:43:28 +08:00
|
|
|
|
_push_src_ownership = ownership;
|
2022-06-04 11:06:35 +08:00
|
|
|
|
_continue_push_ms = option.continue_push_ms;
|
2021-10-15 16:27:17 +08:00
|
|
|
|
CHECK(_push_src);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 20:36:47 +08:00
|
|
|
|
bool WebRtcPusher::close(MediaSource &sender) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
//此回调在其他线程触发
|
2022-09-18 20:36:47 +08:00
|
|
|
|
string err = StrPrinter << "close media: " << sender.getUrl();
|
2021-10-15 16:27:17 +08:00
|
|
|
|
weak_ptr<WebRtcPusher> weak_self = static_pointer_cast<WebRtcPusher>(shared_from_this());
|
|
|
|
|
getPoller()->async([weak_self, err]() {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (strong_self) {
|
|
|
|
|
strong_self->onShutdown(SockException(Err_shutdown, err));
|
2022-02-09 14:31:40 +08:00
|
|
|
|
//主动关闭推流,那么不延时注销
|
|
|
|
|
strong_self->_push_src = nullptr;
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int WebRtcPusher::totalReaderCount(MediaSource &sender) {
|
2023-07-08 21:33:07 +08:00
|
|
|
|
auto total_count = _push_src ? _push_src->totalReaderCount() : 0;
|
|
|
|
|
if (_simulcast) {
|
2023-07-29 13:04:24 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
2023-07-08 21:33:07 +08:00
|
|
|
|
for (auto &src : _push_src_sim) {
|
|
|
|
|
total_count += src.second->totalReaderCount();
|
|
|
|
|
}
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
2023-07-08 21:33:07 +08:00
|
|
|
|
return total_count;
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MediaOriginType WebRtcPusher::getOriginType(MediaSource &sender) const {
|
|
|
|
|
return MediaOriginType::rtc_push;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string WebRtcPusher::getOriginUrl(MediaSource &sender) const {
|
2023-05-25 16:23:24 +08:00
|
|
|
|
return _media_info.full_url;
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<SockInfo> WebRtcPusher::getOriginSock(MediaSource &sender) const {
|
|
|
|
|
return static_pointer_cast<SockInfo>(getSession());
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 17:53:20 +08:00
|
|
|
|
toolkit::EventPoller::Ptr WebRtcPusher::getOwnerPoller(MediaSource &sender) {
|
|
|
|
|
return getPoller();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 16:27:17 +08:00
|
|
|
|
void WebRtcPusher::onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) {
|
|
|
|
|
if (!_simulcast) {
|
|
|
|
|
assert(_push_src);
|
|
|
|
|
_push_src->onWrite(rtp, false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rtp->type == TrackAudio) {
|
|
|
|
|
//音频
|
2022-01-10 17:43:28 +08:00
|
|
|
|
for (auto &pr : _push_src_sim) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
pr.second->onWrite(rtp, false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//视频
|
2023-07-29 13:04:24 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(_mtx);
|
2022-01-10 17:43:28 +08:00
|
|
|
|
auto &src = _push_src_sim[rid];
|
2021-10-15 16:27:17 +08:00
|
|
|
|
if (!src) {
|
2023-06-17 10:28:01 +08:00
|
|
|
|
const auto& stream = _push_src->getMediaTuple().stream;
|
|
|
|
|
auto src_imp = _push_src->clone(rid.empty() ? stream : stream + '_' + rid);
|
2022-01-10 17:43:28 +08:00
|
|
|
|
_push_src_sim_ownership[rid] = src_imp->getOwnership();
|
2021-10-15 16:27:17 +08:00
|
|
|
|
src_imp->setListener(static_pointer_cast<WebRtcPusher>(shared_from_this()));
|
|
|
|
|
src = src_imp;
|
|
|
|
|
}
|
|
|
|
|
src->onWrite(std::move(rtp), false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPusher::onStartWebRTC() {
|
|
|
|
|
WebRtcTransportImp::onStartWebRTC();
|
|
|
|
|
_simulcast = _answer_sdp->supportSimulcast();
|
|
|
|
|
if (canRecvRtp()) {
|
|
|
|
|
_push_src->setSdp(_answer_sdp->toRtspSdp());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPusher::onDestory() {
|
|
|
|
|
auto duration = getDuration();
|
|
|
|
|
auto bytes_usage = getBytesUsage();
|
|
|
|
|
//流量统计事件广播
|
|
|
|
|
GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
|
|
|
|
|
|
|
|
|
|
if (getSession()) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
WarnL << "RTC推流器(" << _media_info.shortUrl() << ")结束推流,耗时(s):" << duration;
|
2021-10-15 16:27:17 +08:00
|
|
|
|
if (bytes_usage >= iFlowThreshold * 1024) {
|
2023-03-03 11:18:21 +08:00
|
|
|
|
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _media_info, bytes_usage, duration, false, static_cast<SockInfo &>(*getSession()));
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-10 17:43:28 +08:00
|
|
|
|
|
2022-06-04 11:06:35 +08:00
|
|
|
|
if (_push_src && _continue_push_ms) {
|
2022-01-10 17:43:28 +08:00
|
|
|
|
//取消所有权
|
|
|
|
|
_push_src_ownership = nullptr;
|
|
|
|
|
//延时10秒注销流
|
|
|
|
|
auto push_src = std::move(_push_src);
|
2022-06-04 11:06:35 +08:00
|
|
|
|
getPoller()->doDelayTask(_continue_push_ms, [push_src]() { return 0; });
|
2022-01-10 17:43:28 +08:00
|
|
|
|
}
|
2023-03-03 11:18:21 +08:00
|
|
|
|
WebRtcTransportImp::onDestory();
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPusher::onRtcConfigure(RtcConfigure &configure) const {
|
|
|
|
|
WebRtcTransportImp::onRtcConfigure(configure);
|
|
|
|
|
//这只是推流
|
|
|
|
|
configure.audio.direction = configure.video.direction = RtpDirection::recvonly;
|
2022-06-11 12:31:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-22 10:46:17 +08:00
|
|
|
|
float WebRtcPusher::getLossRate(MediaSource &sender,TrackType type) {
|
2022-06-11 12:31:06 +08:00
|
|
|
|
return WebRtcTransportImp::getLossRate(type);
|
2022-09-07 19:10:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPusher::OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport) {
|
|
|
|
|
//主动关闭推流,那么不等待重推
|
|
|
|
|
_push_src = nullptr;
|
|
|
|
|
WebRtcTransportImp::OnDtlsTransportClosed(dtlsTransport);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-22 10:46:17 +08:00
|
|
|
|
void WebRtcPusher::onRtcpBye() {
|
2022-09-07 19:10:06 +08:00
|
|
|
|
WebRtcTransportImp::onRtcpBye();
|
2022-09-18 21:03:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-22 10:46:17 +08:00
|
|
|
|
void WebRtcPusher::onShutdown(const SockException &ex) {
|
|
|
|
|
_push_src = nullptr;
|
|
|
|
|
WebRtcTransportImp::onShutdown(ex);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 21:03:05 +08:00
|
|
|
|
}// namespace mediakit
|