Files
ZLMediaKit/src/Rtmp/FlvMuxer.h

66 lines
2.3 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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.
2018-10-25 10:00:17 +08:00
*/
2018-08-30 19:29:54 +08:00
2018-10-24 18:16:08 +08:00
#ifndef ZLMEDIAKIT_FLVMUXER_H
#define ZLMEDIAKIT_FLVMUXER_H
2018-08-30 19:29:54 +08:00
2018-10-24 18:09:54 +08:00
#include "Rtmp/Rtmp.h"
#include "Rtmp/RtmpMediaSource.h"
#include "Poller/EventPoller.h"
2018-08-30 19:29:54 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-08-30 19:29:54 +08:00
2021-07-15 11:16:11 +08:00
class FlvMuxer {
2018-08-30 19:29:54 +08:00
public:
2021-07-15 11:16:11 +08:00
using Ptr = std::shared_ptr<FlvMuxer>;
FlvMuxer();
2021-07-15 11:16:11 +08:00
virtual ~FlvMuxer() = default;
2018-08-30 19:29:54 +08:00
void stop();
2020-08-30 10:48:34 +08:00
2018-08-30 19:29:54 +08:00
protected:
void start(const toolkit::EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media, uint32_t start_pts = 0);
virtual void onWrite(const toolkit::Buffer::Ptr &data, bool flush) = 0;
2018-08-30 19:29:54 +08:00
virtual void onDetach() = 0;
virtual std::shared_ptr<FlvMuxer> getSharedPtr() = 0;
2020-08-30 10:48:34 +08:00
2018-08-30 19:29:54 +08:00
private:
void onWriteFlvHeader(const RtmpMediaSource::Ptr &src);
2020-08-30 10:48:34 +08:00
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 toolkit::Buffer::Ptr &buffer, uint32_t time_stamp, bool flush);
toolkit::BufferRaw::Ptr obtainBuffer(const void *data, size_t len);
toolkit::BufferRaw::Ptr obtainBuffer();
2020-08-30 10:48:34 +08:00
2018-08-30 19:29:54 +08:00
private:
toolkit::ResourcePool<toolkit::BufferRaw> _packet_pool;
2020-08-30 10:48:34 +08:00
RtmpMediaSource::RingType::RingReader::Ptr _ring_reader;
2018-08-30 19:29:54 +08:00
};
class FlvRecorder : public FlvMuxer , public std::enable_shared_from_this<FlvRecorder>{
public:
2021-07-15 11:16:11 +08:00
using Ptr = std::shared_ptr<FlvRecorder>;
void startRecord(const toolkit::EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media, const std::string &file_path);
void startRecord(const toolkit::EventPoller::Ptr &poller, const std::string &vhost, const std::string &app, const std::string &stream, const std::string &file_path);
2020-08-30 10:48:34 +08:00
2018-08-30 19:29:54 +08:00
private:
virtual void onWrite(const toolkit::Buffer::Ptr &data, bool flush) override ;
2018-08-30 19:29:54 +08:00
virtual void onDetach() override;
virtual std::shared_ptr<FlvMuxer> getSharedPtr() override;
2020-08-30 10:48:34 +08:00
2018-08-30 19:29:54 +08:00
private:
std::shared_ptr<FILE> _file;
std::recursive_mutex _file_mtx;
2018-08-30 19:29:54 +08:00
};
2018-10-24 17:17:55 +08:00
}//namespace mediakit
2018-10-24 18:16:08 +08:00
#endif //ZLMEDIAKIT_FLVMUXER_H