Decoder.h 1.99 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
 */

#ifndef ZLMEDIAKIT_DECODER_H
#define ZLMEDIAKIT_DECODER_H

#include <stdint.h>
#include <memory>
#include <functional>
17 18
#include "Common/MediaSink.h"

xiongziliang committed
19 20 21 22
namespace mediakit {

class Decoder {
public:
23 24 25
    using Ptr = std::shared_ptr<Decoder>;
    using onDecode = std::function<void(int stream, int codecid, int flags, int64_t pts, int64_t dts, const void *data, size_t bytes)>;
    using onStream = std::function<void(int stream, int codecid, const void *extra, size_t bytes, int finish)>;
26

xia-chu committed
27
    virtual ssize_t input(const uint8_t *data, size_t bytes) = 0;
28 29
    void setOnDecode(onDecode cb);
    void setOnStream(onStream cb);
30

xiongziliang committed
31 32 33
protected:
    Decoder() = default;
    virtual ~Decoder() = default;
34 35 36 37

protected:
    onDecode _on_decode;
    onStream _on_stream;
xiongziliang committed
38 39
};

40 41
class DecoderImp{
public:
42
    typedef enum { decoder_ts = 0, decoder_ps } Type;
43

44
    using Ptr = std::shared_ptr<DecoderImp>;
45 46
    ~DecoderImp() = default;

47
    static Ptr createDecoder(Type type, MediaSinkInterface *sink);
xia-chu committed
48
    ssize_t input(const uint8_t *data, size_t bytes);
49
    void flush();
50 51 52 53 54 55

protected:
    void onTrack(const Track::Ptr &track);
    void onFrame(const Frame::Ptr &frame);

private:
56
    DecoderImp(const Decoder::Ptr &decoder, MediaSinkInterface *sink);
57 58
    void onDecode(int stream, int codecid, int flags, int64_t pts, int64_t dts, const void *data, size_t bytes);
    void onStream(int stream, int codecid, const void *extra, size_t bytes, int finish);
59 60 61

private:
    Decoder::Ptr _decoder;
62
    MediaSinkInterface *_sink;
xia-chu committed
63
    FrameMerger _merger{FrameMerger::none};
64
    Track::Ptr _tracks[TrackMax];
65 66
};

xiongziliang committed
67 68
}//namespace mediakit
#endif //ZLMEDIAKIT_DECODER_H