Commit e3e7495c by xia-chu

新增webrtc播放器datachannel消息广播机制和接口

parent 39dd886e
ZLToolKit @ 5d74e09b
Subproject commit 97f9b9a2ac58353f72f085830690d27833b8ad88 Subproject commit 5d74e09b8c84cccc46036ed2ef1a62f670c423d4
...@@ -816,6 +816,19 @@ void installWebApi() { ...@@ -816,6 +816,19 @@ void installWebApi() {
}); });
}); });
api_regist("/index/api/broadcastMessage", [](API_ARGS_MAP) {
CHECK_SECRET();
CHECK_ARGS("schema", "vhost", "app", "stream", "msg");
auto src = MediaSource::find(allArgs["schema"], allArgs["vhost"], allArgs["app"], allArgs["stream"]);
if (!src) {
throw ApiRetException("can not find the stream", API::NotFound);
}
Any any;
Buffer::Ptr buffer = std::make_shared<BufferLikeString>(allArgs["msg"]);
any.set(std::move(buffer));
src->broadcastMessage(any);
});
//测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs //测试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){ api_regist("/index/api/getMediaInfo",[](API_ARGS_MAP_ASYNC){
CHECK_SECRET(); CHECK_SECRET();
......
...@@ -353,6 +353,8 @@ public: ...@@ -353,6 +353,8 @@ public:
cb(std::list<toolkit::Any>()); cb(std::list<toolkit::Any>());
} }
virtual bool broadcastMessage(const toolkit::Any &data) { return false; }
// 获取媒体源类型 // 获取媒体源类型
MediaOriginType getOriginType() const; MediaOriginType getOriginType() const;
// 获取媒体源url或者文件路径 // 获取媒体源url或者文件路径
......
...@@ -55,9 +55,16 @@ public: ...@@ -55,9 +55,16 @@ public:
void getPlayerList(const std::function<void(const std::list<toolkit::Any> &info_list)> &cb, void getPlayerList(const std::function<void(const std::list<toolkit::Any> &info_list)> &cb,
const std::function<toolkit::Any(toolkit::Any &&info)> &on_change) override { const std::function<toolkit::Any(toolkit::Any &&info)> &on_change) override {
assert(_ring);
_ring->getInfoList(cb, on_change); _ring->getInfoList(cb, on_change);
} }
bool broadcastMessage(const toolkit::Any &data) override {
assert(_ring);
_ring->sendMessage(data);
return true;
}
/** /**
* 获取播放器个数 * 获取播放器个数
*/ */
......
...@@ -71,6 +71,21 @@ void WebRtcPlayer::onStartWebRTC() { ...@@ -71,6 +71,21 @@ void WebRtcPlayer::onStartWebRTC() {
} }
strong_self->onShutdown(SockException(Err_shutdown, "rtsp ring buffer detached")); strong_self->onShutdown(SockException(Err_shutdown, "rtsp ring buffer detached"));
}); });
_reader->setMessageCB([weak_self] (const toolkit::Any &data) {
auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
if (data.is<Buffer>()) {
auto &buffer = data.get<Buffer>();
// PPID 51: 文本string
// PPID 53: 二进制
strong_self->sendDatachannel(0, 51, buffer.data(), buffer.size());
} else {
WarnL << "Send unknown message type to webrtc player: " << data.type_name();
}
});
} }
} }
void WebRtcPlayer::onDestory() { void WebRtcPlayer::onDestory() {
......
...@@ -229,6 +229,19 @@ void WebRtcTransport::OnSctpAssociationMessageReceived( ...@@ -229,6 +229,19 @@ void WebRtcTransport::OnSctpAssociationMessageReceived(
_sctp->SendSctpMessage(params, ppid, msg, len); _sctp->SendSctpMessage(params, ppid, msg, len);
} }
#endif #endif
void WebRtcTransport::sendDatachannel(uint16_t streamId, uint32_t ppid, const char *msg, size_t len) {
#ifdef ENABLE_SCTP
if (_sctp) {
RTC::SctpStreamParameters params;
params.streamId = streamId;
_sctp->SendSctpMessage(params, ppid, (uint8_t *)msg, len);
}
#else
WarnL << "WebRTC datachannel disabled!";
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTuple *tuple) { void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTuple *tuple) {
......
...@@ -112,6 +112,7 @@ public: ...@@ -112,6 +112,7 @@ public:
*/ */
void sendRtpPacket(const char *buf, int len, bool flush, void *ctx = nullptr); void sendRtpPacket(const char *buf, int len, bool flush, void *ctx = nullptr);
void sendRtcpPacket(const char *buf, int len, bool flush, void *ctx = nullptr); void sendRtcpPacket(const char *buf, int len, bool flush, void *ctx = nullptr);
void sendDatachannel(uint16_t streamId, uint32_t ppid, const char *msg, size_t len);
const EventPoller::Ptr& getPoller() const; const EventPoller::Ptr& getPoller() const;
Session::Ptr getSession() const; Session::Ptr getSession() const;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论