2020-03-06 13:00:06 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2020-03-06 13:00:06 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2020-03-06 13:00:06 +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.
|
2020-03-06 13:00:06 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2020-03-06 13:00:06 +08:00
|
|
|
|
#include "TSDecoder.h"
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
bool TSSegment::isTSPacket(const char *data, size_t len){
|
2020-05-17 18:00:23 +08:00
|
|
|
|
return len == TS_PACKET_SIZE && ((uint8_t*)data)[0] == TS_SYNC_BYTE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 14:25:01 +08:00
|
|
|
|
void TSSegment::setOnSegment(TSSegment::onSegment cb) {
|
|
|
|
|
_onSegment = std::move(cb);
|
2020-03-06 13:00:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t TSSegment::onRecvHeader(const char *data, size_t len) {
|
2020-05-17 18:00:23 +08:00
|
|
|
|
if (!isTSPacket(data, len)) {
|
|
|
|
|
WarnL << "不是ts包:" << (int) (data[0]) << " " << len;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-03-06 13:00:06 +08:00
|
|
|
|
_onSegment(data, len);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
const char *TSSegment::onSearchPacketTail(const char *data, size_t len) {
|
2020-03-06 13:00:06 +08:00
|
|
|
|
if (len < _size + 1) {
|
2020-05-17 18:00:23 +08:00
|
|
|
|
if (len == _size && ((uint8_t *) data)[0] == TS_SYNC_BYTE) {
|
2020-03-06 13:00:06 +08:00
|
|
|
|
return data + _size;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
//下一个包头
|
2020-05-17 18:00:23 +08:00
|
|
|
|
if (((uint8_t *) data)[_size] == TS_SYNC_BYTE) {
|
2020-03-06 13:00:06 +08:00
|
|
|
|
return data + _size;
|
|
|
|
|
}
|
2020-05-17 18:00:23 +08:00
|
|
|
|
auto pos = memchr(data + _size, TS_SYNC_BYTE, len - _size);
|
2020-03-06 13:00:06 +08:00
|
|
|
|
if (pos) {
|
|
|
|
|
return (char *) pos;
|
|
|
|
|
}
|
2021-12-24 14:25:01 +08:00
|
|
|
|
if (remainDataSize() > 4 * _size) {
|
|
|
|
|
//数据这么多都没ts包,全部清空
|
|
|
|
|
return data + len;
|
|
|
|
|
}
|
|
|
|
|
//等待更多数据
|
2020-03-06 13:00:06 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-05-17 18:00:23 +08:00
|
|
|
|
#if defined(ENABLE_HLS)
|
|
|
|
|
#include "mpeg-ts.h"
|
|
|
|
|
TSDecoder::TSDecoder() : _ts_segment() {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
_ts_segment.setOnSegment([this](const char *data, size_t len){
|
2020-03-06 13:00:06 +08:00
|
|
|
|
ts_demuxer_input(_demuxer_ctx,(uint8_t*)data,len);
|
|
|
|
|
});
|
|
|
|
|
_demuxer_ctx = ts_demuxer_create([](void* param, int program, int stream, int codecid, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes){
|
|
|
|
|
TSDecoder *thiz = (TSDecoder*)param;
|
|
|
|
|
if(thiz->_on_decode){
|
|
|
|
|
thiz->_on_decode(stream,codecid,flags,pts,dts,data,bytes);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
},this);
|
2020-11-29 09:38:04 +08:00
|
|
|
|
|
|
|
|
|
ts_demuxer_notify_t notify = {
|
|
|
|
|
[](void *param, int stream, int codecid, const void *extra, int bytes, int finish) {
|
|
|
|
|
TSDecoder *thiz = (TSDecoder *) param;
|
|
|
|
|
if (thiz->_on_stream) {
|
|
|
|
|
thiz->_on_stream(stream, codecid, extra, bytes, finish);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ts_demuxer_set_notify((struct ts_demuxer_t *) _demuxer_ctx, ¬ify, this);
|
2020-03-06 13:00:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSDecoder::~TSDecoder() {
|
|
|
|
|
ts_demuxer_destroy(_demuxer_ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 14:01:10 +08:00
|
|
|
|
ssize_t TSDecoder::input(const uint8_t *data, size_t bytes) {
|
2020-05-17 18:00:23 +08:00
|
|
|
|
if (TSSegment::isTSPacket((char *)data, bytes)) {
|
|
|
|
|
return ts_demuxer_input(_demuxer_ctx, (uint8_t *) data, bytes);
|
2020-03-06 13:00:06 +08:00
|
|
|
|
}
|
|
|
|
|
_ts_segment.input((char*)data,bytes);
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 09:38:04 +08:00
|
|
|
|
void TSDecoder::setOnDecode(Decoder::onDecode cb) {
|
|
|
|
|
_on_decode = std::move(cb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TSDecoder::setOnStream(Decoder::onStream cb) {
|
|
|
|
|
_on_stream = std::move(cb);
|
2020-03-06 13:00:06 +08:00
|
|
|
|
}
|
2020-11-29 09:38:04 +08:00
|
|
|
|
|
2020-05-17 18:00:23 +08:00
|
|
|
|
#endif//defined(ENABLE_HLS)
|
2020-03-06 13:00:06 +08:00
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|