MP4.cpp 7.67 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
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.
xiongziliang committed
9 10 11 12 13 14 15
 */

#ifdef ENABLE_MP4
#include "MP4.h"
#include "Util/File.h"
#include "Util/logger.h"
#include "Common/config.h"
xiongziliang committed
16 17
#include "fmp4-writer.h"

xiongziliang committed
18 19 20
using namespace toolkit;
namespace mediakit {

21 22
/////////////////////////////////////////////////mp4_writer_t/////////////////////////////////////////////////

xiongziliang committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
struct mp4_writer_t {
    int is_fmp4;
    union {
        fmp4_writer_t *fmp4;
        mov_writer_t *mov;
    } u;
};

mp4_writer_t* mp4_writer_create(int is_fmp4, const struct mov_buffer_t *buffer, void* param, int flags){
    mp4_writer_t *mp4 = (mp4_writer_t *) malloc(sizeof(mp4_writer_t));
    mp4->is_fmp4 = is_fmp4;
    if (is_fmp4) {
        mp4->u.fmp4 = fmp4_writer_create(buffer, param, flags);
    } else {
        mp4->u.mov = mov_writer_create(buffer, param, flags);
    }
    return mp4;
}

void mp4_writer_destroy(mp4_writer_t* mp4){
    if (mp4->is_fmp4) {
        fmp4_writer_destroy(mp4->u.fmp4);
    } else {
        mov_writer_destroy(mp4->u.mov);
    }
    free(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){
    if (mp4->is_fmp4) {
        return fmp4_writer_add_audio(mp4->u.fmp4, object, channel_count, bits_per_sample, sample_rate, extra_data, extra_data_size);
    } else {
        return mov_writer_add_audio(mp4->u.mov, object, channel_count, bits_per_sample, sample_rate, extra_data, 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){
    if (mp4->is_fmp4) {
        return fmp4_writer_add_video(mp4->u.fmp4, object, width, height, extra_data, extra_data_size);
    } else {
        return mov_writer_add_video(mp4->u.mov, object, width, height, extra_data, extra_data_size);
    }
}

int mp4_writer_add_subtitle(mp4_writer_t* mp4, uint8_t object, const void* extra_data, size_t extra_data_size){
    if (mp4->is_fmp4) {
        return fmp4_writer_add_subtitle(mp4->u.fmp4, object, extra_data, extra_data_size);
    } else {
        return mov_writer_add_subtitle(mp4->u.mov, object, extra_data, 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){
    if (mp4->is_fmp4) {
        return fmp4_writer_write(mp4->u.fmp4, track, data, bytes, pts, dts, flags);
    } else {
        return mov_writer_write(mp4->u.mov, track, data, bytes, pts, dts, 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){
    if (mp4->is_fmp4) {
        return fmp4_writer_write_l(mp4->u.fmp4, track, data, bytes, pts, dts, flags, add_nalu_size);
    } else {
        return mov_writer_write_l(mp4->u.mov, track, data, bytes, pts, dts, flags, add_nalu_size);
    }
}

int mp4_writer_save_segment(mp4_writer_t* mp4){
    if (mp4->is_fmp4) {
        return fmp4_writer_save_segment(mp4->u.fmp4);
    } else {
        return -1;
    }
}

int mp4_writer_init_segment(mp4_writer_t* mp4){
    if (mp4->is_fmp4) {
        return fmp4_writer_init_segment(mp4->u.fmp4);
    } else {
        return -1;
    }
}

107 108
/////////////////////////////////////////////////MP4FileIO/////////////////////////////////////////////////

xiongziliang committed
109
static struct mov_buffer_t s_io = {
xiongziliang committed
110
        [](void *ctx, void *data, uint64_t bytes) {
111
            MP4FileIO *thiz = (MP4FileIO *) ctx;
xiongziliang committed
112
            return thiz->onRead(data, bytes);
xiongziliang committed
113
        },
xiongziliang committed
114
        [](void *ctx, const void *data, uint64_t bytes) {
115
            MP4FileIO *thiz = (MP4FileIO *) ctx;
xiongziliang committed
116
            return thiz->onWrite(data, bytes);
xiongziliang committed
117
        },
xiongziliang committed
118
        [](void *ctx, uint64_t offset) {
119
            MP4FileIO *thiz = (MP4FileIO *) ctx;
xiongziliang committed
120 121
            return thiz->onSeek(offset);
        },
xiongziliang committed
122
        [](void *ctx) {
123
            MP4FileIO *thiz = (MP4FileIO *) ctx;
xiongziliang committed
124 125 126 127
            return thiz->onTell();
        }
};

128
MP4FileIO::Writer MP4FileIO::createWriter(int flags, bool is_fmp4){
xiongziliang committed
129
    Writer writer;
130 131 132
    Ptr self = shared_from_this();
    //保存自己的强引用,防止提前释放
    writer.reset(mp4_writer_create(is_fmp4, &s_io,this, flags),[self](mp4_writer_t *ptr){
xiongziliang committed
133
        if(ptr){
xiongziliang committed
134
            mp4_writer_destroy(ptr);
xiongziliang committed
135 136 137 138 139 140 141 142
        }
    });
    if(!writer){
        throw std::runtime_error("写入mp4文件失败!");
    }
    return writer;
}

143
MP4FileIO::Reader MP4FileIO::createReader(){
xiongziliang committed
144
    Reader reader;
145 146 147
    Ptr self = shared_from_this();
    //保存自己的强引用,防止提前释放
    reader.reset(mov_reader_create(&s_io,this),[self](mov_reader_t *ptr){
xiongziliang committed
148 149 150 151 152 153 154 155 156 157
        if(ptr){
            mov_reader_destroy(ptr);
        }
    });
    if(!reader){
        throw std::runtime_error("读取mp4文件失败!");
    }
    return reader;
}

158 159
/////////////////////////////////////////////////////MP4FileDisk/////////////////////////////////////////////////////////

xiongziliang committed
160 161
#if defined(_WIN32) || defined(_WIN64)
    #define fseek64 _fseeki64
162
    #define ftell64 _ftelli64
xiongziliang committed
163
#else
164 165
    #define fseek64 fseek
    #define ftell64 ftell
xiongziliang committed
166 167
#endif

168
void MP4FileDisk::openFile(const char *file, const char *mode) {
xiongziliang committed
169
    //创建文件
xiongziliang committed
170
    auto fp = File::create_file(file, mode);
xiongziliang committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    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) {
191
        fflush(fp);
xiongziliang committed
192 193 194 195
        fclose(fp);
    });
}

196
void MP4FileDisk::closeFile() {
xiongziliang committed
197 198 199
    _file = nullptr;
}

200
int MP4FileDisk::onRead(void *data, uint64_t bytes) {
xiongziliang committed
201 202 203 204 205 206
    if (bytes == fread(data, 1, bytes, _file.get())){
        return 0;
    }
    return 0 != ferror(_file.get()) ? ferror(_file.get()) : -1 /*EOF*/;
}

207
int MP4FileDisk::onWrite(const void *data, uint64_t bytes) {
xiongziliang committed
208 209 210
    return bytes == fwrite(data, 1, bytes, _file.get()) ? 0 : ferror(_file.get());
}

211
int MP4FileDisk::onSeek(uint64_t offset) {
xiongziliang committed
212 213 214
    return fseek64(_file.get(), offset, SEEK_SET);
}

215
uint64_t MP4FileDisk::onTell() {
xiongziliang committed
216 217 218
    return ftell64(_file.get());
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/////////////////////////////////////////////////////MP4FileMemory/////////////////////////////////////////////////////////

string MP4FileMemory::getAndClearMemory(){
    string ret;
    ret.swap(_memory);
    _offset = 0;
    return ret;
}

uint64_t MP4FileMemory::fileSize() const{
    return _memory.size();
}

uint64_t MP4FileMemory::onTell(){
    return _offset;
}

int MP4FileMemory::onSeek(uint64_t offset){
    if (offset > _memory.size()) {
        return -1;
    }
    _offset = offset;
    return 0;
}

int MP4FileMemory::onRead(void *data, uint64_t bytes){
    if (_offset >= _memory.size()) {
        //EOF
        return -1;
    }
    bytes = MIN(bytes, _memory.size() - _offset);
    memcpy(data, _memory.data(), bytes);
    _offset += bytes;
    return 0;
}

int MP4FileMemory::onWrite(const void *data, uint64_t bytes){
    if (_offset + bytes > _memory.size()) {
        //需要扩容
        _memory.resize(_offset + bytes);
    }
    memcpy((uint8_t *) _memory.data() + _offset, data, bytes);
    _offset += bytes;
    return 0;
}

xiongziliang committed
265 266
}//namespace mediakit
#endif //NABLE_MP4RECORD