ZLMediaKit/src/Rtp/PSDecoder.cpp

84 lines
2.7 KiB
C++
Raw Normal View History

2019-12-05 19:20:12 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-12-06 11:54:10 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2019-12-06 11:54:10 +08:00
*
2020-04-04 20:30:09 +08:00
* 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.
2019-12-06 11:54:10 +08:00
*/
2019-12-05 19:20:12 +08:00
2019-12-06 11:54:10 +08:00
#if defined(ENABLE_RTPPROXY)
2019-12-05 19:20:12 +08:00
#include "PSDecoder.h"
2019-12-06 11:54:10 +08:00
#include "mpeg-ps.h"
using namespace toolkit;
2019-12-06 11:54:10 +08:00
namespace mediakit{
2019-12-05 19:20:12 +08:00
PSDecoder::PSDecoder() {
_ps_demuxer = ps_demuxer_create([](void* param,
int stream,
int codecid,
int flags,
int64_t pts,
int64_t dts,
const void* data,
size_t bytes){
PSDecoder *thiz = (PSDecoder *)param;
2020-03-06 13:00:06 +08:00
if(thiz->_on_decode){
thiz->_on_decode(stream, codecid, flags, pts, dts, data, bytes);
}
2020-10-18 21:33:36 +08:00
return 0;
2019-12-05 19:20:12 +08:00
},this);
ps_demuxer_notify_t notify = {
[](void *param, int stream, int codecid, const void *extra, int bytes, int finish) {
PSDecoder *thiz = (PSDecoder *) param;
if (thiz->_on_stream) {
thiz->_on_stream(stream, codecid, extra, bytes, finish);
}
}
};
ps_demuxer_set_notify((struct ps_demuxer_t *) _ps_demuxer, &notify, this);
2019-12-05 19:20:12 +08:00
}
PSDecoder::~PSDecoder() {
2019-12-06 11:54:10 +08:00
ps_demuxer_destroy((struct ps_demuxer_t*)_ps_demuxer);
2019-12-05 19:20:12 +08:00
}
2021-02-09 14:01:10 +08:00
ssize_t PSDecoder::input(const uint8_t *data, size_t bytes) {
2021-07-18 15:02:48 +08:00
HttpRequestSplitter::input(reinterpret_cast<const char *>(data), bytes);
return bytes;
2019-12-05 19:20:12 +08:00
}
2019-12-06 11:54:10 +08:00
void PSDecoder::setOnDecode(Decoder::onDecode cb) {
_on_decode = std::move(cb);
}
void PSDecoder::setOnStream(Decoder::onStream cb) {
_on_stream = std::move(cb);
2020-03-06 13:00:06 +08:00
}
2019-12-06 11:54:10 +08:00
2021-07-18 15:02:48 +08:00
const char *PSDecoder::onSearchPacketTail(const char *data, size_t len) {
try {
auto ret = ps_demuxer_input(static_cast<struct ps_demuxer_t *>(_ps_demuxer), reinterpret_cast<const uint8_t *>(data), len);
if (ret >= 0) {
//解析成功全部或部分
return data + ret;
}
//解析失败,丢弃所有数据
return data + len;
} catch (std::exception &ex) {
InfoL << "解析 ps 异常: bytes=" << len
<< ", exception=" << ex.what()
<< ", hex=" << hexdump(data, MIN(len, 32));
//触发断言,解析失败,丢弃所有数据
return data + len;
2021-07-18 15:02:48 +08:00
}
}
2020-03-06 13:00:06 +08:00
}//namespace mediakit
2019-12-06 11:54:10 +08:00
#endif//#if defined(ENABLE_RTPPROXY)