forked from ZLMediaKit/ZLMediaKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMP4Reader.cpp
More file actions
255 lines (225 loc) · 7.37 KB
/
Copy pathMP4Reader.cpp
File metadata and controls
255 lines (225 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* 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.
*/
#ifdef ENABLE_MP4
#include "MP4Reader.h"
#include "Common/config.h"
#include "Thread/WorkThreadPool.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
MP4Reader::MP4Reader(const string &vhost, const string &app, const string &stream_id, const string &file_path) {
_file_path = file_path;
if (_file_path.empty()) {
GET_CONFIG(string, recordPath, Record::kFilePath);
GET_CONFIG(bool, enableVhost, General::kEnableVhost);
if (enableVhost) {
_file_path = vhost + "/" + app + "/" + stream_id;
} else {
_file_path = app + "/" + stream_id;
}
_file_path = File::absolutePath(_file_path, recordPath);
}
_demuxer = std::make_shared<MP4Demuxer>();
_demuxer->openMP4(_file_path);
if (stream_id.empty()) {
return;
}
ProtocolOption option;
//读取mp4文件并流化时,不重复生成mp4/hls文件
option.enable_mp4 = false;
option.enable_hls = false;
_muxer = std::make_shared<MultiMediaSourceMuxer>(vhost, app, stream_id, _demuxer->getDurationMS() / 1000.0f, option);
auto tracks = _demuxer->getTracks(false);
if (tracks.empty()) {
throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path);
}
for (auto &track : tracks) {
_muxer->addTrack(track);
if (track->getTrackType() == TrackVideo) {
_have_video = true;
}
}
//添加完毕所有track,防止单track情况下最大等待3秒
_muxer->addTrackCompleted();
}
bool MP4Reader::readSample() {
if (_paused) {
//确保暂停时,时间轴不走动
_seek_ticker.resetTime();
return true;
}
bool keyFrame = false;
bool eof = false;
while (!eof && _last_dts < getCurrentStamp()) {
auto frame = _demuxer->readFrame(keyFrame, eof);
if (!frame) {
continue;
}
_last_dts = frame->dts();
if (_muxer) {
_muxer->inputFrame(frame);
}
}
GET_CONFIG(bool, file_repeat, Record::kFileRepeat);
if (eof && (file_repeat || _file_repeat)) {
//需要从头开始看
seekTo(0);
return true;
}
return !eof;
}
bool MP4Reader::readNextSample() {
bool keyFrame = false;
bool eof = false;
auto frame = _demuxer->readFrame(keyFrame, eof);
if (!frame) {
return false;
}
if (_muxer) {
_muxer->inputFrame(frame);
}
setCurrentStamp(frame->dts());
return true;
}
void MP4Reader::stopReadMP4() {
_timer = nullptr;
}
void MP4Reader::startReadMP4(const EventPoller::Ptr &poller_in, uint64_t sample_ms, bool ref_self, bool file_repeat) {
GET_CONFIG(uint32_t, sampleMS, Record::kSampleMS);
auto strong_self = shared_from_this();
if (_muxer) {
_muxer->setMediaListener(strong_self);
//一直读到所有track就绪为止
while (!_muxer->isAllTrackReady() && readNextSample()) {}
}
//未指定线程,那么使用后台线程(读写文件采用后台线程)
auto poller = poller_in ? poller_in : WorkThreadPool::Instance().getPoller();
auto timer_sec = (sample_ms ? sample_ms : sampleMS) / 1000.0f;
//启动定时器
if (ref_self) {
_timer = std::make_shared<Timer>(timer_sec, [strong_self]() {
lock_guard<recursive_mutex> lck(strong_self->_mtx);
return strong_self->readSample();
}, poller);
} else {
weak_ptr<MP4Reader> weak_self = strong_self;
_timer = std::make_shared<Timer>(timer_sec, [weak_self]() {
auto strong_self = weak_self.lock();
if (!strong_self) {
return false;
}
lock_guard<recursive_mutex> lck(strong_self->_mtx);
return strong_self->readSample();
}, poller);
}
_file_repeat = file_repeat;
}
const MP4Demuxer::Ptr &MP4Reader::getDemuxer() const {
return _demuxer;
}
uint32_t MP4Reader::getCurrentStamp() {
return (uint32_t) (_seek_to + !_paused * _speed * _seek_ticker.elapsedTime());
}
void MP4Reader::setCurrentStamp(uint32_t new_stamp) {
auto old_stamp = getCurrentStamp();
_seek_to = new_stamp;
_last_dts = new_stamp;
_seek_ticker.resetTime();
if (old_stamp != new_stamp && _muxer) {
//时间轴未拖动时不操作
_muxer->setTimeStamp(new_stamp);
}
}
bool MP4Reader::seekTo(MediaSource &sender, uint32_t stamp) {
//拖动进度条后应该恢复播放
pause(sender, false);
TraceL << getOriginUrl(sender) << ",stamp:" << stamp;
return seekTo(stamp);
}
bool MP4Reader::pause(MediaSource &sender, bool pause) {
if (_paused == pause) {
return true;
}
//_seek_ticker重新计时,不管是暂停还是seek都不影响总的播放进度
setCurrentStamp(getCurrentStamp());
_paused = pause;
TraceL << getOriginUrl(sender) << ",pause:" << pause;
return true;
}
bool MP4Reader::speed(MediaSource &sender, float speed) {
if (speed < 0.1 && speed > 20) {
WarnL << "播放速度取值范围非法:" << speed;
return false;
}
//设置播放速度后应该恢复播放
pause(sender, false);
if (_speed == speed) {
return true;
}
_speed = speed;
TraceL << getOriginUrl(sender) << ",speed:" << speed;
return true;
}
bool MP4Reader::seekTo(uint32_t stamp_seek) {
lock_guard<recursive_mutex> lck(_mtx);
if (stamp_seek > _demuxer->getDurationMS()) {
//超过文件长度
return false;
}
auto stamp = _demuxer->seekTo(stamp_seek);
if (stamp == -1) {
//seek失败
return false;
}
if (!_have_video) {
//没有视频,不需要搜索关键帧;设置当前时间戳
setCurrentStamp((uint32_t) stamp);
return true;
}
//搜索到下一帧关键帧
bool keyFrame = false;
bool eof = false;
while (!eof) {
auto frame = _demuxer->readFrame(keyFrame, eof);
if (!frame) {
//文件读完了都未找到下一帧关键帧
continue;
}
if (keyFrame || frame->keyFrame() || frame->configFrame()) {
//定位到key帧
if (_muxer) {
_muxer->inputFrame(frame);
}
//设置当前时间戳
setCurrentStamp(frame->dts());
return true;
}
}
return false;
}
bool MP4Reader::close(MediaSource &sender, bool force) {
if (!_muxer || (!force && _muxer->totalReaderCount())) {
return false;
}
_timer.reset();
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
return true;
}
int MP4Reader::totalReaderCount(MediaSource &sender) {
return _muxer ? _muxer->totalReaderCount() : sender.readerCount();
}
MediaOriginType MP4Reader::getOriginType(MediaSource &sender) const {
return MediaOriginType::mp4_vod;
}
string MP4Reader::getOriginUrl(MediaSource &sender) const {
return _file_path;
}
} /* namespace mediakit */
#endif //ENABLE_MP4