ZLMediaKit/src/Record/MP4.h

150 lines
4.3 KiB
C++
Raw Normal View History

/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2020-04-03 20:46:55 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* 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.
2020-04-03 20:46:55 +08:00
*/
#ifndef ZLMEDIAKIT_MP4_H
#define ZLMEDIAKIT_MP4_H
#ifdef ENABLE_MP4
#include <memory>
#include <string>
#include "mov-writer.h"
#include "mov-reader.h"
#include "mpeg4-hevc.h"
#include "mpeg4-avc.h"
#include "mpeg4-aac.h"
#include "mov-buffer.h"
#include "mov-format.h"
using namespace std;
namespace mediakit {
2020-09-20 19:45:04 +08:00
//以下是fmp4/mov的通用接口简单包装了ireader/media-server的接口
2020-09-20 19:44:20 +08:00
typedef struct mp4_writer_t mp4_writer_t;
mp4_writer_t* mp4_writer_create(int is_fmp4, const struct mov_buffer_t *buffer, void* param, int flags);
void mp4_writer_destroy(mp4_writer_t* mp4);
int mp4_writer_add_audio(mp4_writer_t* mp4, uint8_t object, int channel_count, int bits_per_sample, int sample_rate, const void* extra_data, size_t extra_data_size);
int mp4_writer_add_video(mp4_writer_t* mp4, uint8_t object, int width, int height, const void* extra_data, size_t extra_data_size);
int mp4_writer_add_subtitle(mp4_writer_t* mp4, uint8_t object, const void* extra_data, size_t extra_data_size);
int mp4_writer_write(mp4_writer_t* mp4, int track, const void* data, size_t bytes, int64_t pts, int64_t dts, int flags);
int mp4_writer_write_l(mp4_writer_t* mp4, int track, const void* data, size_t bytes, int64_t pts, int64_t dts, int flags, int add_nalu_size);
int mp4_writer_save_segment(mp4_writer_t* mp4);
int mp4_writer_init_segment(mp4_writer_t* mp4);
2020-09-20 19:45:04 +08:00
//mp4文件IO的抽象接口类
class MP4FileIO : public std::enable_shared_from_this<MP4FileIO> {
2020-04-03 20:46:55 +08:00
public:
2020-09-20 19:45:04 +08:00
using Ptr = std::shared_ptr<MP4FileIO>;
using Writer = std::shared_ptr<mp4_writer_t>;
using Reader = std::shared_ptr<mov_reader_t>;
MP4FileIO() = default;
virtual ~MP4FileIO() = default;
/**
* mp4复用器
* @param flags 0MOV_FLAG_FASTSTARTMOV_FLAG_SEGMENT
* @param is_fmp4 fmp4还是普通mp4
* @return mp4复用器
*/
virtual Writer createWriter(int flags, bool is_fmp4 = false);
/**
* mp4解复用器
* @return mp4解复用器
*/
virtual Reader createReader();
/**
*
*/
virtual uint64_t onTell() = 0;
/**
* seek至文件某处
* @param offset
* @return (0)
*/
virtual int onSeek(uint64_t offset) = 0;
/**
*
* @param data
* @param bytes
* @return (0)
*/
virtual int onRead(void *data, uint64_t bytes) = 0;
/**
*
* @param data
* @param bytes
* @return (0)
*/
virtual int onWrite(const void *data, uint64_t bytes) = 0;
};
//磁盘MP4文件类
class MP4FileDisk : public MP4FileIO {
public:
using Ptr = std::shared_ptr<MP4FileDisk>;
MP4FileDisk() = default;
~MP4FileDisk() override = default;
/**
*
* @param file
* @param mode fopen的方式
*/
void openFile(const char *file, const char *mode);
/**
*
*/
2020-04-03 20:46:55 +08:00
void closeFile();
2020-09-20 19:45:04 +08:00
protected:
uint64_t onTell() override;
int onSeek(uint64_t offset) override;
int onRead(void *data, uint64_t bytes) override;
int onWrite(const void *data, uint64_t bytes) override;
2020-04-03 20:46:55 +08:00
private:
std::shared_ptr<FILE> _file;
};
class MP4FileMemory : public MP4FileIO{
public:
using Ptr = std::shared_ptr<MP4FileMemory>;
MP4FileMemory() = default;
~MP4FileMemory() override = default;
/**
*
*/
uint64_t fileSize() const;
/**
*
*/
string getAndClearMemory();
protected:
uint64_t onTell() override;
int onSeek(uint64_t offset) override;
int onRead(void *data, uint64_t bytes) override;
int onWrite(const void *data, uint64_t bytes) override;
private:
uint64_t _offset = 0;
string _memory;
};
2020-04-03 20:46:55 +08:00
}//namespace mediakit
#endif //NABLE_MP4RECORD
#endif //ZLMEDIAKIT_MP4_H