Mp4Maker.h 3.33 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 28 29
 */

#ifndef MP4MAKER_H_
#define MP4MAKER_H_

30
#ifdef ENABLE_MP4V2
xzl committed
31 32 33

#include <mutex>
#include <memory>
xiongzilaing committed
34 35
#include <mp4v2/mp4v2.h>
#include "Player/PlayerBase.h"
xzl committed
36
#include "Util/util.h"
xiongzilaing committed
37
#include "Util/logger.h"
xzl committed
38 39
#include "Util/TimeTicker.h"
#include "Util/TimeTicker.h"
40
#include "Common/MediaSink.h"
41
#include "Extension/Track.h"
42

xiongziliang committed
43
using namespace toolkit;
xzl committed
44

xiongziliang committed
45
namespace mediakit {
xzl committed
46

47
class Mp4Info {
xzl committed
48 49 50
public:
	time_t ui64StartedTime; //GMT标准时间,单位秒
	time_t ui64TimeLen;//录像长度,单位秒
xiongziliang committed
51
	off_t ui64FileSize;//文件大小,单位BYTE
xzl committed
52 53 54 55 56 57
	string strFilePath;//文件路径
	string strFileName;//文件名称
	string strFolder;//文件夹路径
	string strUrl;//播放路径
	string strAppName;//应用名称
	string strStreamId;//流ID
58
	string strVhost;//vhost
xzl committed
59
};
60
class Mp4Maker : public MediaSink{
xzl committed
61 62
public:
	typedef std::shared_ptr<Mp4Maker> Ptr;
63 64 65
	Mp4Maker(const string &strPath,
			 const string &strVhost ,
			 const string &strApp,
66
			 const string &strStreamId);
xzl committed
67
	virtual ~Mp4Maker();
68 69 70 71 72 73 74 75 76 77 78 79 80 81
private:
	/**
     * 某Track输出frame,在onAllTrackReady触发后才会调用此方法
     * @param frame
     */
	void onTrackFrame(const Frame::Ptr &frame) override ;

	/**
	 * 所有Track准备好了
	 */
	void onAllTrackReady() override;
private:
    void createFile();
    void closeFile();
82
    void asyncClose();
83

xzl committed
84
	//时间戳:参考频率1000
85
	void inputH264(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp);
xzl committed
86 87
	//时间戳:参考频率1000
	void inputAAC(void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp);
88 89 90

	void inputH264_l(void *pData, uint32_t ui32Length, uint32_t ui64Duration);
    void inputAAC_l(void *pData, uint32_t ui32Length, uint32_t ui64Duration);
xzl committed
91
private:
92 93 94 95 96 97 98
	MP4FileHandle _hMp4 = MP4_INVALID_FILE_HANDLE;
	MP4TrackId _hVideo = MP4_INVALID_TRACK_ID;
	MP4TrackId _hAudio = MP4_INVALID_TRACK_ID;
	string _strPath;
	string _strFile;
	string _strFileTmp;
	Ticker _ticker;
xzl committed
99

100 101
	string _strLastVideo;
	string _strLastAudio;
xzl committed
102

103 104 105
	uint32_t _ui32LastVideoTime = 0;
	uint32_t _ui32LastAudioTime = 0;
	Mp4Info _info;
106 107 108

	bool _haveVideo = false;
	int _audioSampleRate;
xzl committed
109 110
};

xiongziliang committed
111
} /* namespace mediakit */
xzl committed
112

113
#endif ///ENABLE_MP4V2
xzl committed
114 115

#endif /* MP4MAKER_H_ */