TSDecoder.h 1.67 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 10 11 12 13 14 15 16 17 18 19 20
 */

#ifndef ZLMEDIAKIT_TSDECODER_H
#define ZLMEDIAKIT_TSDECODER_H

#include "Util/logger.h"
#include "Http/HttpRequestSplitter.h"
#include "Decoder.h"

using namespace toolkit;
namespace mediakit {

21 22 23 24
#define TS_PACKET_SIZE		188
#define TS_SYNC_BYTE        0x47

//TS包分割器,用于split一个一个的ts包
xiongziliang committed
25 26
class TSSegment : public HttpRequestSplitter {
public:
27 28
    typedef std::function<void(const char *data,size_t len)> onSegment;
    TSSegment(size_t size = TS_PACKET_SIZE) : _size(size){}
xiongziliang committed
29 30
    ~TSSegment(){}
    void setOnSegment(const onSegment &cb);
31 32
    static bool isTSPacket(const char *data, size_t len);

xiongziliang committed
33
protected:
34
    ssize_t onRecvHeader(const char *data, size_t len) override ;
35 36
    const char *onSearchPacketTail(const char *data, size_t len) override ;

xiongziliang committed
37
private:
38
    size_t _size;
xiongziliang committed
39 40 41
    onSegment _onSegment;
};

42
#if defined(ENABLE_HLS)
xiongziliang committed
43 44 45 46 47
//ts解析器
class TSDecoder : public Decoder {
public:
    TSDecoder();
    ~TSDecoder();
xia-chu committed
48
    ssize_t input(const uint8_t* data, size_t bytes) override ;
49 50 51
    void setOnDecode(onDecode cb) override;
    void setOnStream(onStream cb) override;

xiongziliang committed
52 53 54 55
private:
    TSSegment _ts_segment;
    struct ts_demuxer_t* _demuxer_ctx = nullptr;
    onDecode _on_decode;
56
    onStream _on_stream;
xiongziliang committed
57
};
58
#endif//defined(ENABLE_HLS)
xiongziliang committed
59 60 61

}//namespace mediakit
#endif //ZLMEDIAKIT_TSDECODER_H