MP4Reader.cpp 4.84 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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.
xzl committed
9 10
 */

xiongziliang committed
11
#ifdef ENABLE_MP4
xiongziliang committed
12
#include "MP4Reader.h"
xiongziliang committed
13
#include "Common/config.h"
14
#include "Thread/WorkThreadPool.h"
xiongziliang committed
15 16
using namespace toolkit;
namespace mediakit {
xzl committed
17

xiongziliang committed
18
MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &strId,const string &filePath ) {
19
    _poller = WorkThreadPool::Instance().getPoller();
20 21
    _file_path = filePath;
    if(_file_path.empty()){
22
        GET_CONFIG(string,recordPath,Record::kFilePath);
23 24
        GET_CONFIG(bool,enableVhost,General::kEnableVhost);
        if(enableVhost){
25
            _file_path = strVhost + "/" + strApp + "/" + strId;
26
        }else{
27
            _file_path = strApp + "/" + strId;
28
        }
29
        _file_path = File::absolutePath(_file_path,recordPath);
30 31
    }

32
    _demuxer = std::make_shared<MP4Demuxer>();
33
    _demuxer->openMP4(_file_path);
34
    _mediaMuxer.reset(new MultiMediaSourceMuxer(strVhost, strApp, strId, _demuxer->getDurationMS() / 1000.0f, true, true, false, false));
35 36
    auto tracks = _demuxer->getTracks(false);
    if(tracks.empty()){
37
        throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path);
38 39
    }
    for(auto &track : tracks){
xiongziliang committed
40 41 42
        _mediaMuxer->addTrack(track);
        if(track->getTrackType() == TrackVideo){
            _have_video = true;
43
        }
xiongziliang committed
44
    }
xiongziliang committed
45 46 47
    //添加完毕所有track,防止单track情况下最大等待3秒
    _mediaMuxer->addTrackCompleted();
}
xzl committed
48

xiongziliang committed
49
bool MP4Reader::readSample() {
50
    bool keyFrame = false;
xiongziliang committed
51
    bool eof = false;
52 53
    while (!eof) {
        auto frame = _demuxer->readFrame(keyFrame, eof);
xiongziliang committed
54
        if (!frame) {
55
            continue;
xiongziliang committed
56 57
        }
        _mediaMuxer->inputFrame(frame);
xiongziliang committed
58
        if (frame->dts() > getCurrentStamp()) {
xiongziliang committed
59
            break;
60 61 62
        }
    }

xiongziliang committed
63 64 65
    GET_CONFIG(bool, fileRepeat, Record::kFileRepeat);
    if (eof && fileRepeat) {
        //需要从头开始看
xiongziliang committed
66
        seekTo(0);
67
        return true;
68
    }
xiongziliang committed
69 70

    return !eof;
xzl committed
71 72
}

xiongziliang committed
73
void MP4Reader::startReadMP4() {
xiongziliang committed
74
    GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
75
    auto strongSelf = shared_from_this();
76
    _mediaMuxer->setMediaListener(strongSelf);
77

xiongziliang committed
78 79 80
    //先获取关键帧
    seekTo(0);
    //读sampleMS毫秒的数据用于产生MediaSource
xiongziliang committed
81
    setCurrentStamp(getCurrentStamp() + sampleMS);
xiongziliang committed
82 83 84 85 86 87
    readSample();

    //启动定时器
    _timer = std::make_shared<Timer>(sampleMS / 1000.0f, [strongSelf]() {
        lock_guard<recursive_mutex> lck(strongSelf->_mtx);
        return strongSelf->readSample();
88
    }, _poller);
89
}
xiongziliang committed
90

xiongziliang committed
91
uint32_t MP4Reader::getCurrentStamp() {
92
    return (uint32_t)(_seek_to + _seek_ticker.elapsedTime());
93
}
xiongziliang committed
94

xiongziliang committed
95
void MP4Reader::setCurrentStamp(uint32_t ui32Stamp){
xiongziliang committed
96 97
    _seek_to = ui32Stamp;
    _seek_ticker.resetTime();
xiongziliang committed
98
    _mediaMuxer->setTimeStamp(ui32Stamp);
99
}
xzl committed
100

xiongziliang committed
101 102
bool MP4Reader::seekTo(MediaSource &sender,uint32_t ui32Stamp){
    return seekTo(ui32Stamp);
103 104
}

xiongziliang committed
105
bool MP4Reader::seekTo(uint32_t ui32Stamp){
106
    lock_guard<recursive_mutex> lck(_mtx);
xiongziliang committed
107 108 109
    if (ui32Stamp > _demuxer->getDurationMS()) {
        //超过文件长度
        return false;
110
    }
xiongziliang committed
111 112 113 114
    auto stamp = _demuxer->seekTo(ui32Stamp);
    if(stamp == -1){
        //seek失败
        return false;
115
    }
xzl committed
116

xiongziliang committed
117 118
    if(!_have_video){
        //没有视频,不需要搜索关键帧
xiongziliang committed
119
        //设置当前时间戳
120
        setCurrentStamp((uint32_t)stamp);
xiongziliang committed
121 122 123
        return true;
    }
    //搜索到下一帧关键帧
124
    bool keyFrame = false;
125 126 127
    bool eof = false;
    while (!eof) {
        auto frame = _demuxer->readFrame(keyFrame, eof);
xiongziliang committed
128
        if(!frame){
xiongziliang committed
129
            //文件读完了都未找到下一帧关键帧
130
            continue;
xiongziliang committed
131
        }
132
        if(keyFrame || frame->keyFrame() || frame->configFrame()){
xiongziliang committed
133 134
            //定位到key帧
            _mediaMuxer->inputFrame(frame);
xiongziliang committed
135 136
            //设置当前时间戳
            setCurrentStamp(frame->dts());
xiongziliang committed
137
            return true;
138 139
        }
    }
140
    return false;
xzl committed
141 142
}

xiongziliang committed
143 144 145
bool MP4Reader::close(MediaSource &sender,bool force){
    if(!_mediaMuxer || (!force && _mediaMuxer->totalReaderCount())){
        return false;
146
    }
xiongziliang committed
147 148 149
    _timer.reset();
    WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
    return true;
xzl committed
150 151
}

xiongziliang committed
152 153
int MP4Reader::totalReaderCount(MediaSource &sender) {
    return _mediaMuxer ? _mediaMuxer->totalReaderCount() : sender.readerCount();
xzl committed
154 155
}

156 157 158 159 160 161 162 163
MediaOriginType MP4Reader::getOriginType(MediaSource &sender) const {
    return MediaOriginType::mp4_vod;
}

string MP4Reader::getOriginUrl(MediaSource &sender) const {
    return _file_path;
}

xiongziliang committed
164
} /* namespace mediakit */
165
#endif //ENABLE_MP4