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
    FlvMuxer();
    virtual ~FlvMuxer();
    void stop();
xiongziliang committed
28

xiongziliang committed
29
protected:
xiongziliang committed
30
    void start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media);
xiongziliang committed
31
    virtual void onWrite(const Buffer::Ptr &data, bool flush) = 0;
xiongziliang committed
32 33
    virtual void onDetach() = 0;
    virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
xiongziliang committed
34

xiongziliang committed
35 36
private:
    void onWriteFlvHeader(const RtmpMediaSource::Ptr &media);
xiongziliang committed
37 38 39 40
    void onWriteRtmp(const RtmpPacket::Ptr &pkt, bool flush);
    void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp, bool flush);
    void onWriteFlvTag(uint8_t type, const Buffer::Ptr &buffer, uint32_t time_stamp, bool flush);

xiongziliang committed
41
private:
42 43
    //时间戳修整器
    Stamp _stamp[2];
xiongziliang committed
44
    RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
xiongziliang committed
45 46 47 48
};

class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
public:
49
    typedef std::shared_ptr<FlvRecorder> Ptr;
xiongziliang committed
50 51
    FlvRecorder();
    virtual ~FlvRecorder();
xiongziliang committed
52 53 54
    void startRecord(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media, const string &file_path);
    void startRecord(const EventPoller::Ptr &poller, const string &vhost, const string &app, const string &stream, const string &file_path);

xiongziliang committed
55
private:
xiongziliang committed
56
    virtual void onWrite(const Buffer::Ptr &data, bool flush) override ;
xiongziliang committed
57 58
    virtual void onDetach() override;
    virtual std::shared_ptr<FlvMuxer> getSharedPtr() override;
xiongziliang committed
59

xiongziliang committed
60 61 62 63 64 65
private:
    std::shared_ptr<FILE> _file;
    recursive_mutex _file_mtx;
};


xiongziliang committed
66
}//namespace mediakit
67
#endif //ZLMEDIAKIT_FLVMUXER_H