FlvMuxer.h 2.91 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
xiongziliang committed
26

27 28
#ifndef ZLMEDIAKIT_FLVMUXER_H
#define ZLMEDIAKIT_FLVMUXER_H
xiongziliang committed
29

30 31
#include "Rtmp/Rtmp.h"
#include "Rtmp/RtmpMediaSource.h"
xiongziliang committed
32
#include "Network/Socket.h"
33
#include "MediaFile/Stamp.h"
xiongziliang committed
34
using namespace toolkit;
xiongziliang committed
35

xiongziliang committed
36
namespace mediakit {
xiongziliang committed
37 38 39

class FlvMuxer{
public:
40
    typedef std::shared_ptr<FlvMuxer> Ptr;
xiongziliang committed
41 42 43 44
    FlvMuxer();
    virtual ~FlvMuxer();
    void stop();
protected:
45
    void start(const EventPoller::Ptr &poller,const RtmpMediaSource::Ptr &media);
xiongziliang committed
46 47 48 49 50 51 52
    virtual void onWrite(const Buffer::Ptr &data) = 0;
    virtual void onDetach() = 0;
    virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
private:
    void onWriteFlvHeader(const RtmpMediaSource::Ptr &media);
    void onWriteRtmp(const RtmpPacket::Ptr &pkt);
    void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t ui32TimeStamp);
53
    void onWriteFlvTag(uint8_t ui8Type, const Buffer::Ptr &buffer, uint32_t ui32TimeStamp);
xiongziliang committed
54 55
private:
    RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
56 57
    //时间戳修整器
    Stamp _stamp[2];
58

xiongziliang committed
59 60 61 62
};

class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
public:
63
    typedef std::shared_ptr<FlvRecorder> Ptr;
xiongziliang committed
64 65
    FlvRecorder();
    virtual ~FlvRecorder();
66 67
    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
68 69 70 71 72 73 74 75 76 77
private:
    virtual void onWrite(const Buffer::Ptr &data) override ;
    virtual void onDetach() override;
    virtual std::shared_ptr<FlvMuxer> getSharedPtr() override;
private:
    std::shared_ptr<FILE> _file;
    recursive_mutex _file_mtx;
};


xiongziliang committed
78
}//namespace mediakit
xiongziliang committed
79

80
#endif //ZLMEDIAKIT_FLVMUXER_H