TSDecoder.cpp 2.92 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * 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.
xiongziliang committed
9
 */
xiongziliang committed
10

xiongziliang committed
11 12 13
#include "TSDecoder.h"
namespace mediakit {

14
bool TSSegment::isTSPacket(const char *data, size_t len){
15 16 17
    return len == TS_PACKET_SIZE && ((uint8_t*)data)[0] == TS_SYNC_BYTE;
}

xiongziliang committed
18 19 20 21
void TSSegment::setOnSegment(const TSSegment::onSegment &cb) {
    _onSegment = cb;
}

22
ssize_t TSSegment::onRecvHeader(const char *data, size_t len) {
23 24 25 26
    if (!isTSPacket(data, len)) {
        WarnL << "不是ts包:" << (int) (data[0]) << " " << len;
        return 0;
    }
xiongziliang committed
27 28 29 30
    _onSegment(data, len);
    return 0;
}

31
const char *TSSegment::onSearchPacketTail(const char *data, size_t len) {
xiongziliang committed
32
    if (len < _size + 1) {
33
        if (len == _size && ((uint8_t *) data)[0] == TS_SYNC_BYTE) {
xiongziliang committed
34 35 36 37 38
            return data + _size;
        }
        return nullptr;
    }
    //下一个包头
39
    if (((uint8_t *) data)[_size] == TS_SYNC_BYTE) {
xiongziliang committed
40 41
        return data + _size;
    }
42
    auto pos = memchr(data + _size, TS_SYNC_BYTE, len - _size);
xiongziliang committed
43 44 45 46 47 48 49 50
    if (pos) {
        return (char *) pos;
    }
    return nullptr;
}

////////////////////////////////////////////////////////////////

51 52 53
#if defined(ENABLE_HLS)
#include "mpeg-ts.h"
TSDecoder::TSDecoder() : _ts_segment() {
54
    _ts_segment.setOnSegment([this](const char *data, size_t len){
xiongziliang committed
55 56 57 58 59 60 61 62 63
        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);
64 65 66 67 68 69 70 71 72 73

    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, &notify, this);
xiongziliang committed
74 75 76 77 78 79
}

TSDecoder::~TSDecoder() {
    ts_demuxer_destroy(_demuxer_ctx);
}

80
size_t TSDecoder::input(const uint8_t *data, size_t bytes) {
81 82
    if (TSSegment::isTSPacket((char *)data, bytes)) {
        return ts_demuxer_input(_demuxer_ctx, (uint8_t *) data, bytes);
xiongziliang committed
83 84 85 86 87
    }
    _ts_segment.input((char*)data,bytes);
    return bytes;
}

88 89 90 91 92 93
void TSDecoder::setOnDecode(Decoder::onDecode cb) {
    _on_decode = std::move(cb);
}

void TSDecoder::setOnStream(Decoder::onStream cb) {
    _on_stream = std::move(cb);
xiongziliang committed
94
}
95

96
#endif//defined(ENABLE_HLS)
xiongziliang committed
97 98

}//namespace mediakit