Older/MediaServer/Record/MP4Demuxer.cpp
amass 9de3af15eb
All checks were successful
Deploy / PullDocker (push) Successful in 12s
Deploy / Build (push) Successful in 1m51s
add ZLMediaKit code for learning.
2024-09-28 23:55:00 +08:00

194 lines
5.7 KiB
C++

/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like 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.
*/
#ifdef ENABLE_MP4
#include "MP4Demuxer.h"
#include "Util/logger.h"
#include "Extension/Factory.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
MP4Demuxer::~MP4Demuxer() {
closeMP4();
}
void MP4Demuxer::openMP4(const string &file) {
closeMP4();
_mp4_file = std::make_shared<MP4FileDisk>();
_mp4_file->openFile(file.data(), "rb+");
_mov_reader = _mp4_file->createReader();
getAllTracks();
_duration_ms = mov_reader_getduration(_mov_reader.get());
}
void MP4Demuxer::closeMP4() {
_mov_reader.reset();
_mp4_file.reset();
}
int MP4Demuxer::getAllTracks() {
static mov_reader_trackinfo_t s_on_track = {
[](void *param, uint32_t track, uint8_t object, int width, int height, const void *extra, size_t bytes) {
//onvideo
MP4Demuxer *thiz = (MP4Demuxer *)param;
thiz->onVideoTrack(track,object,width,height,extra,bytes);
},
[](void *param, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void *extra, size_t bytes) {
//onaudio
MP4Demuxer *thiz = (MP4Demuxer *)param;
thiz->onAudioTrack(track,object,channel_count,bit_per_sample,sample_rate,extra,bytes);
},
[](void *param, uint32_t track, uint8_t object, const void *extra, size_t bytes) {
//onsubtitle, do nothing
}
};
return mov_reader_getinfo(_mov_reader.get(),&s_on_track,this);
}
void MP4Demuxer::onVideoTrack(uint32_t track, uint8_t object, int width, int height, const void *extra, size_t bytes) {
auto video = Factory::getTrackByCodecId(getCodecByMovId(object));
if (!video) {
return;
}
video->setIndex(track);
_tracks.emplace(track, video);
if (extra && bytes) {
video->setExtraData((uint8_t *)extra, bytes);
}
}
void MP4Demuxer::onAudioTrack(uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void *extra, size_t bytes) {
auto audio = Factory::getTrackByCodecId(getCodecByMovId(object), sample_rate, channel_count, bit_per_sample / channel_count);
if (!audio) {
return;
}
audio->setIndex(track);
_tracks.emplace(track, audio);
if (extra && bytes) {
audio->setExtraData((uint8_t *)extra, bytes);
}
}
int64_t MP4Demuxer::seekTo(int64_t stamp_ms) {
if(0 != mov_reader_seek(_mov_reader.get(),&stamp_ms)){
return -1;
}
return stamp_ms;
}
struct Context {
Context(MP4Demuxer *ptr) : thiz(ptr) {}
MP4Demuxer *thiz;
int flags = 0;
int64_t pts = 0;
int64_t dts = 0;
uint32_t track_id = 0;
BufferRaw::Ptr buffer;
};
Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) {
keyFrame = false;
eof = false;
static mov_reader_onread2 mov_onalloc = [](void *param, uint32_t track_id, size_t bytes, int64_t pts, int64_t dts, int flags) -> void * {
Context *ctx = (Context *) param;
ctx->pts = pts;
ctx->dts = dts;
ctx->flags = flags;
ctx->track_id = track_id;
ctx->buffer = ctx->thiz->_buffer_pool.obtain2();
ctx->buffer->setCapacity(bytes + 1);
ctx->buffer->setSize(bytes);
return ctx->buffer->data();
};
Context ctx(this);
auto ret = mov_reader_read2(_mov_reader.get(), mov_onalloc, &ctx);
switch (ret) {
case 0 : {
eof = true;
return nullptr;
}
case 1 : {
keyFrame = ctx.flags & MOV_AV_FLAG_KEYFREAME;
return makeFrame(ctx.track_id, ctx.buffer, ctx.pts, ctx.dts);
}
default : {
eof = true;
WarnL << "读取mp4文件数据失败:" << ret;
return nullptr;
}
}
}
Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int64_t pts, int64_t dts) {
auto it = _tracks.find(track_id);
if (it == _tracks.end()) {
return nullptr;
}
Frame::Ptr ret;
auto codec = it->second->getCodecId();
switch (codec) {
case CodecH264:
case CodecH265: {
auto bytes = buf->size();
auto data = buf->data();
auto offset = 0u;
while (offset < bytes) {
uint32_t frame_len;
memcpy(&frame_len, data + offset, 4);
frame_len = ntohl(frame_len);
if (frame_len + offset + 4 > bytes) {
return nullptr;
}
memcpy(data + offset, "\x00\x00\x00\x01", 4);
offset += (frame_len + 4);
}
ret = Factory::getFrameFromBuffer(codec, std::move(buf), dts, pts);
break;
}
default: {
ret = Factory::getFrameFromBuffer(codec, std::move(buf), dts, pts);
break;
}
}
if (ret) {
ret->setIndex(track_id);
it->second->inputFrame(ret);
}
return ret;
}
vector<Track::Ptr> MP4Demuxer::getTracks(bool ready) const {
vector<Track::Ptr> ret;
for (auto &pr : _tracks) {
if (ready && !pr.second->ready()) {
continue;
}
ret.push_back(pr.second);
}
return ret;
}
uint64_t MP4Demuxer::getDurationMS() const {
return _duration_ms;
}
}//namespace mediakit
#endif// ENABLE_MP4