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

11 12
#ifndef ZLMEDIAKIT_FLVMUXER_H
#define ZLMEDIAKIT_FLVMUXER_H
xiongziliang committed
13

14 15
#include "Rtmp/Rtmp.h"
#include "Rtmp/RtmpMediaSource.h"
xiongziliang committed
16
#include "Network/Socket.h"
xiongziliang committed
17
#include "Common/Stamp.h"
xiongziliang committed
18
using namespace toolkit;
xiongziliang committed
19

xiongziliang committed
20
namespace mediakit {
xiongziliang committed
21 22 23

class FlvMuxer{
public:
24
    typedef std::shared_ptr<FlvMuxer> Ptr;
xiongziliang committed
25 26 27 28
    FlvMuxer();
    virtual ~FlvMuxer();
    void stop();
protected:
29
    void start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media);
xiongziliang committed
30
    virtual void onWrite(const Buffer::Ptr &data, bool flush) = 0;
xiongziliang committed
31 32 33 34
    virtual void onDetach() = 0;
    virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
private:
    void onWriteFlvHeader(const RtmpMediaSource::Ptr &media);
xiongziliang committed
35 36 37
    void onWriteRtmp(const RtmpPacket::Ptr &pkt,bool flush);
    void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp, bool flush);
    void onWriteFlvTag(uint8_t ui8Type, const Buffer::Ptr &buffer, uint32_t ui32TimeStamp, bool flush);
xiongziliang committed
38 39
private:
    RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
40 41
    //时间戳修整器
    Stamp _stamp[2];
42

xiongziliang committed
43 44 45 46
};

class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
public:
47
    typedef std::shared_ptr<FlvRecorder> Ptr;
xiongziliang committed
48 49
    FlvRecorder();
    virtual ~FlvRecorder();
50 51
    void startRecord(const EventPoller::Ptr &poller,const string &vhost,const string &app,const string &stream,const string &file_path);
    void startRecord(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media,const string &file_path);
xiongziliang committed
52
private:
xiongziliang committed
53
    virtual void onWrite(const Buffer::Ptr &data, bool flush) override ;
xiongziliang committed
54 55 56 57 58 59 60 61
    virtual void onDetach() override;
    virtual std::shared_ptr<FlvMuxer> getSharedPtr() override;
private:
    std::shared_ptr<FILE> _file;
    recursive_mutex _file_mtx;
};


xiongziliang committed
62
}//namespace mediakit
xiongziliang committed
63

64
#endif //ZLMEDIAKIT_FLVMUXER_H