MediaReader.cpp 11.7 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27
 */

#include "MediaReader.h"
xiongziliang committed
28
#include "Common/config.h"
xiongzilaing committed
29
#include "Util/mini.h"
xzl committed
30 31 32 33 34 35 36 37
#include "Http/HttpSession.h"

using namespace ZL::Util;


namespace ZL {
namespace MediaFile {

38
#ifdef ENABLE_MP4V2
39
MediaReader::MediaReader(const string &strVhost,const string &strApp, const string &strId) {
40 41 42
    GET_CONFIG_AND_REGISTER(string,recordPath,Config::Record::kFilePath);

    auto strFileName = recordPath + "/" + strVhost + "/" + strApp + "/" + strId;
xzl committed
43 44 45

	m_hMP4File = MP4Read(strFileName.data());
	if(m_hMP4File == MP4_INVALID_FILE_HANDLE){
46
		throw runtime_error(StrPrinter << "打开MP4文件失败:" << strFileName << endl);
xzl committed
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
	}
	m_video_trId = MP4FindTrackId(m_hMP4File, 0, MP4_VIDEO_TRACK_TYPE, 0);
	if(m_video_trId != MP4_INVALID_TRACK_ID){
		 if(strcmp(MP4GetTrackMediaDataName(m_hMP4File, m_video_trId),"avc1") ==0){
			auto m_video_timescale 		= MP4GetTrackTimeScale(m_hMP4File, m_video_trId);
			auto m_video_duration 		= MP4GetTrackDuration(m_hMP4File, m_video_trId);
			m_video_num_samples     = MP4GetTrackNumberOfSamples(m_hMP4File, m_video_trId);
			m_video_sample_max_size = MP4GetTrackMaxSampleSize(m_hMP4File, m_video_trId);
			m_video_width 			= MP4GetTrackVideoWidth(m_hMP4File, m_video_trId);
			m_video_height 			= MP4GetTrackVideoHeight(m_hMP4File, m_video_trId);
			m_video_framerate       = MP4GetTrackVideoFrameRate(m_hMP4File, m_video_trId);
			m_pcVideoSample = std::shared_ptr<uint8_t> (new uint8_t[m_video_sample_max_size],[](uint8_t *ptr){
				delete [] ptr;
			});
			uint8_t **seqheader;
			uint8_t **pictheader;
			uint32_t *pictheadersize;
			uint32_t *seqheadersize;
			uint32_t ix;
			if(MP4GetTrackH264SeqPictHeaders(m_hMP4File, m_video_trId, &seqheader, &seqheadersize, &pictheader, &pictheadersize)){
				for (ix = 0; seqheadersize[ix] != 0; ix++) {
					m_strSps.assign((char *)(seqheader[ix]), seqheadersize[ix]);
					float framerate;
					getAVCInfo(m_strSps, (int &)m_video_width, (int &)m_video_height, framerate);
					m_video_framerate = framerate;
					m_strSps = string("\x0\x0\x0\x1",4) + m_strSps;
					free(seqheader[ix]);
				}
				free(seqheader);
				free(seqheadersize);
				for (ix = 0; pictheadersize[ix] != 0; ix++) {
					m_strPps.assign("\x0\x0\x0\x1",4);
					m_strPps.append((char *)(pictheader[ix]), pictheadersize[ix]);
					free(pictheader[ix]);
				}
				free(pictheader);
				free(pictheadersize);
			}
			m_video_ms = 1000.0 * m_video_duration / m_video_timescale;
			/*InfoL 	<< "\r\n"
					<< m_video_ms << "\r\n"
					<< m_video_num_samples << "\r\n"
					<< m_video_framerate << "\r\n"
					<< m_video_width << "\r\n"
					<< m_video_height << "\r\n";*/
		} else {
			//如果不是h264,则忽略
			m_video_trId = MP4_INVALID_TRACK_ID;
		}
	}


	m_audio_trId = MP4FindTrackId(m_hMP4File, 0, MP4_AUDIO_TRACK_TYPE, 0);
	if (m_audio_trId != MP4_INVALID_TRACK_ID) {
		if (strcmp(MP4GetTrackMediaDataName(m_hMP4File, m_audio_trId), "mp4a") == 0) {
			m_audio_sample_rate = MP4GetTrackTimeScale(m_hMP4File, m_audio_trId);
			auto m_audio_duration = MP4GetTrackDuration(m_hMP4File, m_audio_trId);
			m_audio_num_samples = MP4GetTrackNumberOfSamples(m_hMP4File,m_audio_trId);
			m_audio_num_channels = MP4GetTrackAudioChannels(m_hMP4File, m_audio_trId);
			m_audio_sample_max_size = MP4GetTrackMaxSampleSize(m_hMP4File,m_audio_trId);
			uint8_t *ppConfig;
			uint32_t pConfigSize;
			if(MP4GetTrackESConfiguration(m_hMP4File,m_audio_trId,&ppConfig,&pConfigSize)){
				m_strAacCfg.assign((char *)ppConfig, pConfigSize);
				makeAdtsHeader(m_strAacCfg, m_adts);
				writeAdtsHeader(m_adts,m_adts.data);
				getAACInfo(m_adts, (int &)m_audio_sample_rate, (int &)m_audio_num_channels);
				free(ppConfig);
			}
			m_audio_ms = 1000.0 * m_audio_duration / m_audio_sample_rate;
			/*InfoL 	<< "\r\n"
					<< m_audio_ms << "\r\n"
					<< m_audio_num_samples << "\r\n"
					<< m_audio_num_channels << "\r\n"
					<< m_audio_sample_rate << "\r\n";*/
		}else{
			m_audio_trId = MP4_INVALID_TRACK_ID;
		}
	}
	if(m_audio_trId == MP4_INVALID_TRACK_ID && m_video_trId == MP4_INVALID_TRACK_ID){
		MP4Close(m_hMP4File);
		m_hMP4File = MP4_INVALID_FILE_HANDLE;
129
		throw runtime_error(StrPrinter << "该MP4文件音视频格式不支持:" << strFileName << endl);
xzl committed
130 131 132
	}

	m_iDuration	= MAX(m_video_ms,m_audio_ms);
133
	m_pChn.reset(new DevChannel(strVhost.data(),strApp.data(),strId.data(),m_iDuration/1000.0,true, false));
xzl committed
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
	if (m_audio_trId != MP4_INVALID_TRACK_ID) {
		AudioInfo info;
		info.iChannel = m_audio_num_channels;
		info.iSampleBit = 16;
		info.iSampleRate = m_audio_sample_rate;
		m_pChn->initAudio(info);
	}

	if (m_video_trId != MP4_INVALID_TRACK_ID) {
		VideoInfo info;
		info.iFrameRate = m_video_framerate;
		info.iWidth = m_video_width;
		info.iHeight = m_video_height;
		m_pChn->initVideo(info);
	}

	if (m_audio_trId != MP4_INVALID_TRACK_ID) {
		m_pChn->inputAAC((char *)m_adts.data, 7, 0);
	}

	if (m_video_trId != MP4_INVALID_TRACK_ID) {
		//m_pChn->initVideo(info);
		m_pChn->inputH264((char *) m_strSps.data(), m_strSps.size(), 0);
		m_pChn->inputH264((char *) m_strPps.data(), m_strPps.size(), 0);
	}
}


MediaReader::~MediaReader() {
	if (m_hMP4File != MP4_INVALID_FILE_HANDLE) {
		MP4Close(m_hMP4File);
		m_hMP4File = MP4_INVALID_FILE_HANDLE;
	}
}


void MediaReader::startReadMP4() {
	auto strongSelf = shared_from_this();
172 173 174
    GET_CONFIG_AND_REGISTER(uint32_t,sampleMS,Config::Record::kSampleMS);

    AsyncTaskThread::Instance().DoTaskDelay(reinterpret_cast<uint64_t>(this), sampleMS, [strongSelf](){
xzl committed
175 176
		return strongSelf->readSample();
	});
177 178 179 180 181 182 183 184
	m_pChn->setListener(strongSelf);
}
 bool MediaReader::seekTo(uint32_t ui32Stamp){
	 seek(ui32Stamp);
	 return true;
}
uint32_t MediaReader::getStamp() {
	 return m_iSeekTime + m_ticker.elapsedTime();
xzl committed
185
}
186 187 188 189
bool MediaReader::shutDown(){
    AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));
    return true;
}
xzl committed
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

bool MediaReader::readSample(int iTimeInc) {
	TimeTicker();
	lock_guard<recursive_mutex> lck(m_mtx);
	auto bFlag0 = readVideoSample(iTimeInc);//数据没读完
	auto bFlag1 = readAudioSample(iTimeInc);//数据没读完
	auto bFlag2 = m_pChn->readerCount() > 0;//读取者大于0
	if((bFlag0 || bFlag1) && bFlag2){
		m_alive.resetTime();
	}
	//DebugL << "alive ...";
	//3秒延时关闭
	return  m_alive.elapsedTime() <  3 * 1000;
}
inline bool MediaReader::readVideoSample(int iTimeInc) {
	if (m_video_trId != MP4_INVALID_TRACK_ID) {
		auto iNextSample = getVideoSampleId(iTimeInc);
		MP4SampleId iIdx = m_video_current;
		for (iIdx = m_video_current; iIdx < iNextSample; iIdx++) {
			uint8_t *pBytes = m_pcVideoSample.get();
			uint32_t numBytes = m_video_sample_max_size;
			if(MP4ReadSample(m_hMP4File, m_video_trId, iIdx + 1, &pBytes, &numBytes,NULL,NULL,NULL,&m_bSyncSample)){
				if (!iTimeInc) {
					uint32_t iOffset = 0;
					while (iOffset < numBytes) {
						uint32_t iFrameLen;
						memcpy(&iFrameLen,pBytes + iOffset,4);
						iFrameLen = ntohl(iFrameLen);
                        if(iFrameLen + iOffset + 4> numBytes){
                            break;
                        }
						memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4);
						writeH264(pBytes + iOffset, iFrameLen + 4, (double) m_video_ms * iIdx / m_video_num_samples);
						iOffset += (iFrameLen + 4);
					}
				}else if(m_bSyncSample){
					break;
				}
			}else{
				FatalL << "读取视频失败:" << iIdx + 1;
			}
		}
		m_video_current = iIdx;
		return m_video_current < m_video_num_samples;
	}
	return false;
}

inline bool MediaReader::readAudioSample(int iTimeInc) {
	if (m_audio_trId != MP4_INVALID_TRACK_ID) {
		auto iNextSample = getAudioSampleId(iTimeInc);
		for (auto i = m_audio_current; i < iNextSample; i++) {
			uint32_t numBytes = m_audio_sample_max_size;
			uint8_t *pBytes = m_adts.data + 7;
			if(MP4ReadSample(m_hMP4File, m_audio_trId, i + 1, &pBytes, &numBytes)){
				if (!iTimeInc) {
					m_adts.aac_frame_length = 7 + numBytes;
					writeAdtsHeader(m_adts, m_adts.data);
					writeAAC(m_adts.data, m_adts.aac_frame_length, (double) m_audio_ms * i / m_audio_num_samples);
				}
			}else{
				FatalL << "读取音频失败:" << i+ 1;
			}
		}
		m_audio_current = iNextSample;
		return m_audio_current < m_audio_num_samples;
	}
	return false;
}

inline void MediaReader::writeH264(uint8_t *pucData,int iLen,uint32_t uiStamp) {
	m_pChn->inputH264((char *)pucData, iLen, uiStamp);
}

inline void MediaReader::writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp) {
	m_pChn->inputAAC((char *)pucData, iLen, uiStamp);
}

inline MP4SampleId MediaReader::getVideoSampleId(int iTimeInc ) {
	MP4SampleId video_current = (double)m_video_num_samples *  (m_iSeekTime + m_ticker.elapsedTime() + iTimeInc) / m_video_ms;
	video_current = MAX(0,MIN(m_video_num_samples, video_current));
	return video_current;

}

inline MP4SampleId MediaReader::getAudioSampleId(int iTimeInc) {
	MP4SampleId audio_current = (double)m_audio_num_samples * (m_iSeekTime + m_ticker.elapsedTime() + iTimeInc)  / m_audio_ms ;
	audio_current = MAX(0,MIN(m_audio_num_samples,audio_current));
	return audio_current;
}
inline void MediaReader::setSeekTime(int iSeekTime){
	m_iSeekTime = MAX(0, MIN(iSeekTime,m_iDuration));
	m_ticker.resetTime();
	if (m_audio_trId != MP4_INVALID_TRACK_ID) {
		m_audio_current = getAudioSampleId();
	}
	if (m_video_trId != MP4_INVALID_TRACK_ID) {
		m_video_current = getVideoSampleId();
	}
}

inline uint32_t MediaReader::getVideoCurrentTime(){
	return (double)m_video_current * m_video_ms /m_video_num_samples;
}
void MediaReader::seek(int iSeekTime,bool bReStart){
	lock_guard<recursive_mutex> lck(m_mtx);
	if(iSeekTime == 0 || m_video_trId == MP4_INVALID_TRACK_ID){
		setSeekTime(iSeekTime);
	}else{
		setSeekTime(iSeekTime - 5000);
		//在之后的10秒查找关键帧
		readVideoSample(10000);
		if (m_bSyncSample) {
			//找到关键帧
			auto iIdr =  m_video_current;
			setSeekTime(getVideoCurrentTime());
			m_video_current = iIdr;
		}else{
			//未找到关键帧
			setSeekTime(iSeekTime);
		}
	}
	m_pChn->updateTimeStamp(m_iSeekTime);

	if(bReStart){
		AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));
		startReadMP4();
	}
}

320
#endif //ENABLE_MP4V2
xzl committed
321 322 323



324
MediaSource::Ptr MediaReader::onMakeMediaSource(const string &strSchema,const string &strVhost,const string &strApp, const string &strId){
325
#ifdef ENABLE_MP4V2
326 327 328
    GET_CONFIG_AND_REGISTER(string,appName,Config::Record::kAppName);

    if (strApp != appName) {
xzl committed
329 330 331
		return nullptr;
	}
	try {
332
		MediaReader::Ptr pReader(new MediaReader(strVhost,strApp, strId));
xzl committed
333
		pReader->startReadMP4();
334
		return MediaSource::find(strSchema,strVhost,strApp, strId, false);
xzl committed
335 336 337 338 339 340
	} catch (std::exception &ex) {
		WarnL << ex.what();
		return nullptr;
	}
#else
	return nullptr;
341
#endif //ENABLE_MP4V2
xzl committed
342 343 344 345 346 347
}



} /* namespace MediaFile */
} /* namespace ZL */