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 "WebRtcPlayer.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/config.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
|
|
|
|
WebRtcPlayer::Ptr WebRtcPlayer::create(const EventPoller::Ptr &poller,
|
|
|
|
|
const RtspMediaSource::Ptr &src,
|
2022-11-27 12:43:16 +08:00
|
|
|
|
const MediaInfo &info,
|
2022-11-30 18:24:35 +08:00
|
|
|
|
bool preferred_tcp) {
|
|
|
|
|
WebRtcPlayer::Ptr ret(new WebRtcPlayer(poller, src, info, preferred_tcp), [](WebRtcPlayer *ptr) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
ptr->onDestory();
|
|
|
|
|
delete ptr;
|
|
|
|
|
});
|
|
|
|
|
ret->onCreate();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebRtcPlayer::WebRtcPlayer(const EventPoller::Ptr &poller,
|
|
|
|
|
const RtspMediaSource::Ptr &src,
|
2022-11-27 12:43:16 +08:00
|
|
|
|
const MediaInfo &info,
|
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;
|
|
|
|
|
_play_src = src;
|
2023-02-20 16:23:29 +08:00
|
|
|
|
CHECK(src);
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPlayer::onStartWebRTC() {
|
2023-02-20 16:23:29 +08:00
|
|
|
|
auto playSrc = _play_src.lock();
|
|
|
|
|
if(!playSrc){
|
|
|
|
|
onShutdown(SockException(Err_shutdown, "rtsp media source was shutdown"));
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2021-10-15 16:27:17 +08:00
|
|
|
|
WebRtcTransportImp::onStartWebRTC();
|
|
|
|
|
if (canSendRtp()) {
|
2023-02-20 16:23:29 +08:00
|
|
|
|
playSrc->pause(false);
|
|
|
|
|
_reader = playSrc->getRing()->attach(getPoller(), true);
|
2021-10-15 16:27:17 +08:00
|
|
|
|
weak_ptr<WebRtcPlayer> weak_self = static_pointer_cast<WebRtcPlayer>(shared_from_this());
|
2023-04-28 22:03:16 +08:00
|
|
|
|
weak_ptr<Session> weak_session = static_pointer_cast<Session>(getSession());
|
2023-09-02 12:06:35 +08:00
|
|
|
|
_reader->setGetInfoCB([weak_session]() {
|
|
|
|
|
Any ret;
|
|
|
|
|
ret.set(static_pointer_cast<SockInfo>(weak_session.lock()));
|
|
|
|
|
return ret;
|
|
|
|
|
});
|
2021-10-15 16:27:17 +08:00
|
|
|
|
_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt) {
|
2022-08-30 21:24:25 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
pkt->for_each([&](const RtpPacket::Ptr &rtp) {
|
2022-01-24 11:57:24 +08:00
|
|
|
|
//TraceL<<"send track type:"<<rtp->type<<" ts:"<<rtp->getStamp()<<" ntp:"<<rtp->ntp_stamp<<" size:"<<rtp->getPayloadSize()<<" i:"<<i;
|
2022-08-30 21:24:25 +08:00
|
|
|
|
strong_self->onSendRtp(rtp, ++i == pkt->size());
|
2021-10-15 16:27:17 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
_reader->setDetachCB([weak_self]() {
|
2022-08-30 21:24:25 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
2021-10-15 16:27:17 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-30 21:24:25 +08:00
|
|
|
|
strong_self->onShutdown(SockException(Err_shutdown, "rtsp ring buffer detached"));
|
2021-10-15 16:27:17 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void WebRtcPlayer::onDestory() {
|
|
|
|
|
auto duration = getDuration();
|
|
|
|
|
auto bytes_usage = getBytesUsage();
|
|
|
|
|
//流量统计事件广播
|
|
|
|
|
GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
|
|
|
|
|
if (_reader && 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-09-02 10:52:07 +08:00
|
|
|
|
NOTICE_EMIT(BroadcastFlowReportArgs, Broadcast::kBroadcastFlowReport, _media_info, bytes_usage, duration, true, *getSession());
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-03 11:18:21 +08:00
|
|
|
|
WebRtcTransportImp::onDestory();
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebRtcPlayer::onRtcConfigure(RtcConfigure &configure) const {
|
2023-02-20 16:23:29 +08:00
|
|
|
|
auto playSrc = _play_src.lock();
|
|
|
|
|
if(!playSrc){
|
|
|
|
|
return ;
|
|
|
|
|
}
|
2021-10-15 16:27:17 +08:00
|
|
|
|
WebRtcTransportImp::onRtcConfigure(configure);
|
|
|
|
|
//这是播放
|
|
|
|
|
configure.audio.direction = configure.video.direction = RtpDirection::sendonly;
|
2023-02-20 16:23:29 +08:00
|
|
|
|
configure.setPlayRtspInfo(playSrc->getSdp());
|
2021-10-15 16:27:17 +08:00
|
|
|
|
}
|
2022-09-18 21:03:05 +08:00
|
|
|
|
|
|
|
|
|
}// namespace mediakit
|