mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-25 20:27:34 +08:00
增加获取媒体流播放器列表功能
This commit is contained in:
parent
679c79802f
commit
04aa3ef41f
@ -741,6 +741,32 @@ void installWebApi() {
|
||||
val["online"] = (bool) (MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"]));
|
||||
});
|
||||
|
||||
api_regist("/index/api/getMediaPlayerList",[](API_ARGS_MAP_ASYNC){
|
||||
CHECK_SECRET();
|
||||
CHECK_ARGS("schema", "vhost", "app", "stream");
|
||||
auto src = MediaSource::find(allArgs["schema"], allArgs["vhost"], allArgs["app"], allArgs["stream"]);
|
||||
if (!src) {
|
||||
throw ApiRetException("can not find the stream", API::NotFound);
|
||||
}
|
||||
src->getPlayerList(
|
||||
[=](const std::list<std::shared_ptr<void>> &info_list) mutable {
|
||||
val["code"] = API::Success;
|
||||
auto &data = val["data"];
|
||||
for (auto &info : info_list) {
|
||||
auto obj = reinterpret_pointer_cast<Value>(info);
|
||||
data.append(std::move(*obj));
|
||||
}
|
||||
invoker(200, headerOut, val.toStyledString());
|
||||
},
|
||||
[](std::shared_ptr<void> &&info) -> std::shared_ptr<void> {
|
||||
auto obj = std::make_shared<Value>();
|
||||
auto session = reinterpret_pointer_cast<Session>(info);
|
||||
(*obj)["peer_ip"] = session->get_peer_ip();
|
||||
(*obj)["peer_port"] = session->get_peer_port();
|
||||
return obj;
|
||||
});
|
||||
});
|
||||
|
||||
//测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs
|
||||
api_regist("/index/api/getMediaInfo",[](API_ARGS_MAP_ASYNC){
|
||||
CHECK_SECRET();
|
||||
|
@ -192,52 +192,6 @@ public:
|
||||
std::string _param_strs;
|
||||
};
|
||||
|
||||
class BytesSpeed {
|
||||
public:
|
||||
BytesSpeed() = default;
|
||||
~BytesSpeed() = default;
|
||||
|
||||
/**
|
||||
* 添加统计字节
|
||||
*/
|
||||
BytesSpeed& operator += (size_t bytes) {
|
||||
_bytes += bytes;
|
||||
if (_bytes > 1024 * 1024) {
|
||||
//数据大于1MB就计算一次网速
|
||||
computeSpeed();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取速度,单位bytes/s
|
||||
*/
|
||||
int getSpeed() {
|
||||
if (_ticker.elapsedTime() < 1000) {
|
||||
//获取频率小于1秒,那么返回上次计算结果
|
||||
return _speed;
|
||||
}
|
||||
return computeSpeed();
|
||||
}
|
||||
|
||||
private:
|
||||
int computeSpeed() {
|
||||
auto elapsed = _ticker.elapsedTime();
|
||||
if (!elapsed) {
|
||||
return _speed;
|
||||
}
|
||||
_speed = (int)(_bytes * 1000 / elapsed);
|
||||
_ticker.resetTime();
|
||||
_bytes = 0;
|
||||
return _speed;
|
||||
}
|
||||
|
||||
private:
|
||||
int _speed = 0;
|
||||
size_t _bytes = 0;
|
||||
toolkit::Ticker _ticker;
|
||||
};
|
||||
|
||||
/**
|
||||
* 媒体源,任何rtsp/rtmp的直播流都源自该对象
|
||||
*/
|
||||
@ -293,6 +247,12 @@ public:
|
||||
virtual int readerCount() = 0;
|
||||
// 观看者个数,包括(hls/rtsp/rtmp)
|
||||
virtual int totalReaderCount();
|
||||
// 获取播放器列表
|
||||
virtual void getPlayerList(const std::function<void(const std::list<std::shared_ptr<void>> &info_list)> &cb,
|
||||
const std::function<std::shared_ptr<void>(std::shared_ptr<void> &&info)> &on_change) {
|
||||
assert(cb);
|
||||
cb(std::list<std::shared_ptr<void>>());
|
||||
}
|
||||
|
||||
// 获取媒体源类型
|
||||
MediaOriginType getOriginType() const;
|
||||
@ -350,7 +310,7 @@ private:
|
||||
void emitEvent(bool regist);
|
||||
|
||||
protected:
|
||||
BytesSpeed _speed[TrackMax];
|
||||
toolkit::BytesSpeed _speed[TrackMax];
|
||||
|
||||
private:
|
||||
std::atomic_flag _owned { false };
|
||||
|
@ -51,6 +51,11 @@ public:
|
||||
return _ring;
|
||||
}
|
||||
|
||||
void getPlayerList(const std::function<void(const std::list<std::shared_ptr<void>> &info_list)> &cb,
|
||||
const std::function<std::shared_ptr<void>(std::shared_ptr<void> &&info)> &on_change) override {
|
||||
_ring->getInfoList(cb, on_change);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取fmp4 init segment
|
||||
*/
|
||||
|
@ -285,6 +285,9 @@ bool HttpSession::checkLiveStreamFMP4(const function<void()> &cb){
|
||||
}
|
||||
strong_self->shutdown(SockException(Err_shutdown, "fmp4 ring buffer detached"));
|
||||
});
|
||||
|
||||
_fmp4_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); });
|
||||
|
||||
_fmp4_reader->setReadCB([weak_self](const FMP4MediaSource::RingDataType &fmp4_list) {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (!strong_self) {
|
||||
@ -326,6 +329,9 @@ bool HttpSession::checkLiveStreamTS(const function<void()> &cb){
|
||||
}
|
||||
strong_self->shutdown(SockException(Err_shutdown,"ts ring buffer detached"));
|
||||
});
|
||||
|
||||
_ts_reader->setGetInfoCB([weak_self]() { return weak_self.lock(); });
|
||||
|
||||
_ts_reader->setReadCB([weak_self](const TSMediaSource::RingDataType &ts_list) {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (!strong_self) {
|
||||
|
@ -54,6 +54,9 @@ void FlvMuxer::start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr
|
||||
});
|
||||
|
||||
bool check = start_pts > 0;
|
||||
|
||||
_ring_reader->setGetInfoCB([weakSelf]() { return weakSelf.lock(); });
|
||||
|
||||
_ring_reader->setReadCB([weakSelf, start_pts, check](const RtmpMediaSource::RingDataType &pkt) mutable {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (!strongSelf) {
|
||||
|
@ -69,6 +69,11 @@ public:
|
||||
return _ring;
|
||||
}
|
||||
|
||||
void getPlayerList(const std::function<void(const std::list<std::shared_ptr<void>> &info_list)> &cb,
|
||||
const std::function<std::shared_ptr<void>(std::shared_ptr<void> &&info)> &on_change) override {
|
||||
_ring->getInfoList(cb, on_change);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取播放器个数
|
||||
* @return
|
||||
|
@ -308,6 +308,7 @@ void RtmpSession::sendPlayResponse(const string &err, const RtmpMediaSource::Ptr
|
||||
src->pause(false);
|
||||
_ring_reader = src->getRing()->attach(getPoller());
|
||||
weak_ptr<RtmpSession> weakSelf = dynamic_pointer_cast<RtmpSession>(shared_from_this());
|
||||
_ring_reader->setGetInfoCB([weakSelf]() { return weakSelf.lock(); });
|
||||
_ring_reader->setReadCB([weakSelf](const RtmpMediaSource::RingDataType &pkt) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (!strongSelf) {
|
||||
|
@ -65,6 +65,11 @@ public:
|
||||
return _ring;
|
||||
}
|
||||
|
||||
void getPlayerList(const std::function<void(const std::list<std::shared_ptr<void>> &info_list)> &cb,
|
||||
const std::function<std::shared_ptr<void>(std::shared_ptr<void> &&info)> &on_change) override {
|
||||
_ring->getInfoList(cb, on_change);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取播放器个数
|
||||
*/
|
||||
|
@ -836,6 +836,9 @@ void RtspSession::handleReq_Play(const Parser &parser) {
|
||||
if (!_play_reader && _rtp_type != Rtsp::RTP_MULTICAST) {
|
||||
weak_ptr<RtspSession> weakSelf = dynamic_pointer_cast<RtspSession>(shared_from_this());
|
||||
_play_reader = play_src->getRing()->attach(getPoller(), useGOP);
|
||||
|
||||
_play_reader->setGetInfoCB([weakSelf]() { return weakSelf.lock(); });
|
||||
|
||||
_play_reader->setDetachCB([weakSelf]() {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (!strongSelf) {
|
||||
|
@ -51,6 +51,11 @@ public:
|
||||
return _ring;
|
||||
}
|
||||
|
||||
void getPlayerList(const std::function<void(const std::list<std::shared_ptr<void>> &info_list)> &cb,
|
||||
const std::function<std::shared_ptr<void>(std::shared_ptr<void> &&info)> &on_change) override {
|
||||
_ring->getInfoList(cb, on_change);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取播放器个数
|
||||
*/
|
||||
|
@ -234,6 +234,8 @@ void SrtTransportImp::doPlay() {
|
||||
}
|
||||
strong_self->onShutdown(SockException(Err_shutdown));
|
||||
});
|
||||
weak_ptr<Session> weak_session = strong_self->getSession();
|
||||
strong_self->_ts_reader->setGetInfoCB([weak_session]() { return weak_session.lock(); });
|
||||
strong_self->_ts_reader->setReadCB([weak_self](const TSMediaSource::RingDataType &ts_list) {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (!strong_self) {
|
||||
|
@ -39,6 +39,8 @@ void WebRtcPlayer::onStartWebRTC() {
|
||||
_play_src->pause(false);
|
||||
_reader = _play_src->getRing()->attach(getPoller(), true);
|
||||
weak_ptr<WebRtcPlayer> weak_self = static_pointer_cast<WebRtcPlayer>(shared_from_this());
|
||||
weak_ptr<Session> weak_session = getSession();
|
||||
_reader->setGetInfoCB([weak_session]() { return weak_session.lock(); });
|
||||
_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt) {
|
||||
auto strongSelf = weak_self.lock();
|
||||
if (!strongSelf) {
|
||||
|
Loading…
Reference in New Issue
Block a user