2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-04-03 20:46:55 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-04-03 20:46:55 +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.
|
2020-04-03 20:46:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2023-07-02 12:02:33 +08:00
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
#include "MP4.h"
|
|
|
|
|
#include "Util/File.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Common/config.h"
|
2020-09-20 19:44:20 +08:00
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
using namespace toolkit;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
static struct mov_buffer_t s_io = {
|
2020-09-20 19:44:20 +08:00
|
|
|
|
[](void *ctx, void *data, uint64_t bytes) {
|
2020-09-20 19:45:04 +08:00
|
|
|
|
MP4FileIO *thiz = (MP4FileIO *) ctx;
|
2020-09-20 19:44:20 +08:00
|
|
|
|
return thiz->onRead(data, bytes);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
},
|
2020-09-20 19:44:20 +08:00
|
|
|
|
[](void *ctx, const void *data, uint64_t bytes) {
|
2020-09-20 19:45:04 +08:00
|
|
|
|
MP4FileIO *thiz = (MP4FileIO *) ctx;
|
2020-09-20 19:44:20 +08:00
|
|
|
|
return thiz->onWrite(data, bytes);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
},
|
2021-10-22 14:16:39 +08:00
|
|
|
|
[](void *ctx, int64_t offset) {
|
2020-09-20 19:45:04 +08:00
|
|
|
|
MP4FileIO *thiz = (MP4FileIO *) ctx;
|
2020-04-03 20:46:55 +08:00
|
|
|
|
return thiz->onSeek(offset);
|
|
|
|
|
},
|
2020-09-20 19:44:20 +08:00
|
|
|
|
[](void *ctx) {
|
2020-09-20 19:45:04 +08:00
|
|
|
|
MP4FileIO *thiz = (MP4FileIO *) ctx;
|
2021-10-22 14:16:39 +08:00
|
|
|
|
return (int64_t)thiz->onTell();
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-20 19:45:04 +08:00
|
|
|
|
MP4FileIO::Writer MP4FileIO::createWriter(int flags, bool is_fmp4){
|
2020-04-03 20:46:55 +08:00
|
|
|
|
Writer writer;
|
2020-09-20 19:45:04 +08:00
|
|
|
|
Ptr self = shared_from_this();
|
|
|
|
|
//保存自己的强引用,防止提前释放
|
|
|
|
|
writer.reset(mp4_writer_create(is_fmp4, &s_io,this, flags),[self](mp4_writer_t *ptr){
|
2020-04-03 20:46:55 +08:00
|
|
|
|
if(ptr){
|
2020-09-20 19:44:20 +08:00
|
|
|
|
mp4_writer_destroy(ptr);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if(!writer){
|
|
|
|
|
throw std::runtime_error("写入mp4文件失败!");
|
|
|
|
|
}
|
|
|
|
|
return writer;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:04 +08:00
|
|
|
|
MP4FileIO::Reader MP4FileIO::createReader(){
|
2020-04-03 20:46:55 +08:00
|
|
|
|
Reader reader;
|
2020-09-20 19:45:04 +08:00
|
|
|
|
Ptr self = shared_from_this();
|
|
|
|
|
//保存自己的强引用,防止提前释放
|
|
|
|
|
reader.reset(mov_reader_create(&s_io,this),[self](mov_reader_t *ptr){
|
2020-04-03 20:46:55 +08:00
|
|
|
|
if(ptr){
|
|
|
|
|
mov_reader_destroy(ptr);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if(!reader){
|
|
|
|
|
throw std::runtime_error("读取mp4文件失败!");
|
|
|
|
|
}
|
|
|
|
|
return reader;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:04 +08:00
|
|
|
|
/////////////////////////////////////////////////////MP4FileDisk/////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
#define fseek64 _fseeki64
|
2020-09-20 19:45:04 +08:00
|
|
|
|
#define ftell64 _ftelli64
|
2020-04-03 20:46:55 +08:00
|
|
|
|
#else
|
2020-09-20 19:45:04 +08:00
|
|
|
|
#define fseek64 fseek
|
|
|
|
|
#define ftell64 ftell
|
2020-04-03 20:46:55 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-09-20 19:45:04 +08:00
|
|
|
|
void MP4FileDisk::openFile(const char *file, const char *mode) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
//创建文件
|
2020-04-24 12:39:22 +08:00
|
|
|
|
auto fp = File::create_file(file, mode);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
if(!fp){
|
|
|
|
|
throw std::runtime_error(string("打开文件失败:") + file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GET_CONFIG(uint32_t,mp4BufSize,Record::kFileBufSize);
|
|
|
|
|
|
|
|
|
|
//新建文件io缓存
|
|
|
|
|
std::shared_ptr<char> file_buf(new char[mp4BufSize],[](char *ptr){
|
|
|
|
|
if(ptr){
|
|
|
|
|
delete [] ptr;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(file_buf){
|
|
|
|
|
//设置文件io缓存
|
|
|
|
|
setvbuf(fp, file_buf.get(), _IOFBF, mp4BufSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//创建智能指针
|
|
|
|
|
_file.reset(fp,[file_buf](FILE *fp) {
|
2020-07-16 10:40:30 +08:00
|
|
|
|
fflush(fp);
|
2020-04-03 20:46:55 +08:00
|
|
|
|
fclose(fp);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:04 +08:00
|
|
|
|
void MP4FileDisk::closeFile() {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
_file = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
int MP4FileDisk::onRead(void *data, size_t bytes) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
if (bytes == fread(data, 1, bytes, _file.get())){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 0 != ferror(_file.get()) ? ferror(_file.get()) : -1 /*EOF*/;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
int MP4FileDisk::onWrite(const void *data, size_t bytes) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
return bytes == fwrite(data, 1, bytes, _file.get()) ? 0 : ferror(_file.get());
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 20:35:55 +08:00
|
|
|
|
int MP4FileDisk::onSeek(uint64_t offset) {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
return fseek64(_file.get(), offset, SEEK_SET);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 20:35:55 +08:00
|
|
|
|
uint64_t MP4FileDisk::onTell() {
|
2020-04-03 20:46:55 +08:00
|
|
|
|
return ftell64(_file.get());
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 19:45:37 +08:00
|
|
|
|
/////////////////////////////////////////////////////MP4FileMemory/////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
string MP4FileMemory::getAndClearMemory(){
|
|
|
|
|
string ret;
|
|
|
|
|
ret.swap(_memory);
|
|
|
|
|
_offset = 0;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t MP4FileMemory::fileSize() const{
|
2020-09-20 19:45:37 +08:00
|
|
|
|
return _memory.size();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 20:35:55 +08:00
|
|
|
|
uint64_t MP4FileMemory::onTell(){
|
2020-09-20 19:45:37 +08:00
|
|
|
|
return _offset;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 20:35:55 +08:00
|
|
|
|
int MP4FileMemory::onSeek(uint64_t offset){
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (offset > _memory.size()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
_offset = offset;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
int MP4FileMemory::onRead(void *data, size_t bytes){
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_offset >= _memory.size()) {
|
|
|
|
|
//EOF
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
bytes = MIN(bytes, _memory.size() - _offset);
|
|
|
|
|
memcpy(data, _memory.data(), bytes);
|
|
|
|
|
_offset += bytes;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
int MP4FileMemory::onWrite(const void *data, size_t bytes){
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_offset + bytes > _memory.size()) {
|
|
|
|
|
//需要扩容
|
|
|
|
|
_memory.resize(_offset + bytes);
|
|
|
|
|
}
|
|
|
|
|
memcpy((uint8_t *) _memory.data() + _offset, data, bytes);
|
|
|
|
|
_offset += bytes;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 20:46:55 +08:00
|
|
|
|
}//namespace mediakit
|
2023-12-09 16:23:51 +08:00
|
|
|
|
#endif // defined(ENABLE_MP4)
|