Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
24222858
Commit
24222858
authored
Oct 20, 2021
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改mp4录制相关代码风格
parent
7c016ad1
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
47 行增加
和
57 行删除
+47
-57
src/Record/MP4Recorder.cpp
+37
-44
src/Record/MP4Recorder.h
+10
-13
没有找到文件。
src/Record/MP4Recorder.cpp
查看文件 @
24222858
...
...
@@ -20,17 +20,13 @@ using namespace toolkit;
namespace
mediakit
{
MP4Recorder
::
MP4Recorder
(
const
string
&
strPath
,
const
string
&
strVhost
,
const
string
&
strApp
,
const
string
&
strStreamId
,
size_t
max_second
)
{
_strPath
=
strPath
;
MP4Recorder
::
MP4Recorder
(
const
string
&
path
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream_id
,
size_t
max_second
)
{
_folder_path
=
path
;
/////record 业务逻辑//////
_info
.
app
=
strA
pp
;
_info
.
stream
=
str
StreamI
d
;
_info
.
vhost
=
strV
host
;
_info
.
folder
=
strP
ath
;
_info
.
app
=
a
pp
;
_info
.
stream
=
str
eam_i
d
;
_info
.
vhost
=
v
host
;
_info
.
folder
=
p
ath
;
GET_CONFIG
(
size_t
,
recordSec
,
Record
::
kFileSecond
);
_max_second
=
max_second
?
max_second
:
recordSec
;
}
...
...
@@ -40,31 +36,27 @@ MP4Recorder::~MP4Recorder() {
void
MP4Recorder
::
createFile
()
{
closeFile
();
auto
strD
ate
=
getTimeStr
(
"%Y-%m-%d"
);
auto
strT
ime
=
getTimeStr
(
"%H-%M-%S"
);
auto
strFileTmp
=
_strPath
+
strDate
+
"/."
+
strT
ime
+
".mp4"
;
auto
strFile
=
_strPath
+
strDate
+
"/"
+
strT
ime
+
".mp4"
;
auto
d
ate
=
getTimeStr
(
"%Y-%m-%d"
);
auto
t
ime
=
getTimeStr
(
"%H-%M-%S"
);
auto
full_path_tmp
=
_folder_path
+
date
+
"/."
+
t
ime
+
".mp4"
;
auto
full_path
=
_folder_path
+
date
+
"/"
+
t
ime
+
".mp4"
;
/////record 业务逻辑//////
_info
.
start_time
=
::
time
(
NULL
);
_info
.
file_name
=
strTime
+
".mp4"
;
_info
.
file_path
=
strFile
;
GET_CONFIG
(
string
,
appName
,
Record
::
kAppName
);
_info
.
url
=
appName
+
"/"
+
_info
.
app
+
"/"
+
_info
.
stream
+
"/"
+
strDate
+
"/"
+
strTime
+
".mp4"
;
_info
.
file_name
=
time
+
".mp4"
;
_info
.
file_path
=
full_path
;
GET_CONFIG
(
string
,
appName
,
Record
::
kAppName
);
_info
.
url
=
appName
+
"/"
+
_info
.
app
+
"/"
+
_info
.
stream
+
"/"
+
date
+
"/"
+
time
+
".mp4"
;
try
{
_muxer
=
std
::
make_shared
<
MP4Muxer
>
();
_muxer
->
openMP4
(
strFileT
mp
);
_muxer
->
openMP4
(
full_path_t
mp
);
for
(
auto
&
track
:
_tracks
)
{
//添加track
_muxer
->
addTrack
(
track
);
}
_
strFileTmp
=
strFileT
mp
;
_
strFile
=
strFile
;
_
full_path_tmp
=
full_path_t
mp
;
_
full_path
=
full_path
;
}
catch
(
std
::
exception
&
ex
)
{
WarnL
<<
ex
.
what
();
}
...
...
@@ -72,29 +64,29 @@ void MP4Recorder::createFile() {
void
MP4Recorder
::
asyncClose
()
{
auto
muxer
=
_muxer
;
auto
strFileTmp
=
_strFileT
mp
;
auto
strFile
=
_strFile
;
auto
full_path_tmp
=
_full_path_t
mp
;
auto
full_path
=
_full_path
;
auto
info
=
_info
;
WorkThreadPool
::
Instance
().
getExecutor
()
->
async
([
muxer
,
strFileTmp
,
strFile
,
info
]()
mutable
{
WorkThreadPool
::
Instance
().
getExecutor
()
->
async
([
muxer
,
full_path_tmp
,
full_path
,
info
]()
mutable
{
//获取文件录制时间,放在关闭mp4之前是为了忽略关闭mp4执行时间
info
.
time_len
=
(
float
)(
::
time
(
NULL
)
-
info
.
start_time
);
info
.
time_len
=
(
float
)
(
::
time
(
NULL
)
-
info
.
start_time
);
//关闭mp4非常耗时,所以要放在后台线程执行
muxer
->
closeMP4
();
//获取文件大小
struct
stat
fileData
;
stat
(
strFileT
mp
.
data
(),
&
fileData
);
stat
(
full_path_t
mp
.
data
(),
&
fileData
);
info
.
file_size
=
fileData
.
st_size
;
if
(
fileData
.
st_size
<
1024
)
{
//录像文件太小,删除之
File
::
delete_file
(
strFileT
mp
.
data
());
File
::
delete_file
(
full_path_t
mp
.
data
());
return
;
}
//临时文件名改成正式文件名,防止mp4未完成时被访问
rename
(
strFileTmp
.
data
(),
strFile
.
data
());
rename
(
full_path_tmp
.
data
(),
full_path
.
data
());
/////record 业务逻辑//////
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastRecordMP4
,
info
);
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastRecordMP4
,
info
);
});
}
...
...
@@ -106,17 +98,18 @@ void MP4Recorder::closeFile() {
}
bool
MP4Recorder
::
inputFrame
(
const
Frame
::
Ptr
&
frame
)
{
if
(
_baseSec
==
0
)
{
_baseSec
=
frame
->
dts
();
if
(
_last_dts
==
0
)
{
//极少情况下dts时间戳可能回退
_last_dts
=
frame
->
dts
();
}
auto
duration
=
frame
->
dts
()
-
_
baseSec
;
if
(
!
_muxer
||
((
duration
>
_max_second
*
1000
)
&&
(
!
_have
Video
||
(
_haveV
ideo
&&
frame
->
keyFrame
()))))
{
auto
duration
=
frame
->
dts
()
-
_
last_dts
;
if
(
!
_muxer
||
((
duration
>
_max_second
*
1000
)
&&
(
!
_have
_video
||
(
_have_v
ideo
&&
frame
->
keyFrame
()))))
{
//成立条件
//1、_muxer为空
//2、到了切片时间,并且只有音频
//3、到了切片时间,有视频并且遇到视频的关键帧
_
baseSec
=
0
;
_
last_dts
=
0
;
createFile
();
}
...
...
@@ -127,11 +120,11 @@ bool MP4Recorder::inputFrame(const Frame::Ptr &frame) {
return
false
;
}
bool
MP4Recorder
::
addTrack
(
const
Track
::
Ptr
&
track
)
{
bool
MP4Recorder
::
addTrack
(
const
Track
::
Ptr
&
track
)
{
//保存所有的track,为创建MP4MuxerFile做准备
_tracks
.
emplace_back
(
track
);
if
(
track
->
getTrackType
()
==
TrackVideo
)
{
_have
V
ideo
=
true
;
if
(
track
->
getTrackType
()
==
TrackVideo
)
{
_have
_v
ideo
=
true
;
}
return
true
;
}
...
...
@@ -139,10 +132,9 @@ bool MP4Recorder::addTrack(const Track::Ptr & track){
void
MP4Recorder
::
resetTracks
()
{
closeFile
();
_tracks
.
clear
();
_have
V
ideo
=
false
;
_have
_v
ideo
=
false
;
}
}
/* namespace mediakit */
#endif //ENABLE_MP4
#endif //ENABLE_MP4
\ No newline at end of file
src/Record/MP4Recorder.h
查看文件 @
24222858
...
...
@@ -26,16 +26,12 @@ using namespace toolkit;
namespace
mediakit
{
#ifdef ENABLE_MP4
class
MP4Recorder
:
public
MediaSinkInterface
{
class
MP4Recorder
:
public
MediaSinkInterface
{
public
:
typedef
std
::
shared_ptr
<
MP4Recorder
>
Ptr
;
using
Ptr
=
std
::
shared_ptr
<
MP4Recorder
>
;
MP4Recorder
(
const
string
&
strPath
,
const
string
&
strVhost
,
const
string
&
strApp
,
const
string
&
strStreamId
,
size_t
max_second
);
virtual
~
MP4Recorder
();
MP4Recorder
(
const
string
&
path
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream_id
,
size_t
max_second
);
~
MP4Recorder
()
override
;
/**
* 重置所有Track
...
...
@@ -56,16 +52,17 @@ private:
void
createFile
();
void
closeFile
();
void
asyncClose
();
private
:
bool
_have
V
ideo
=
false
;
bool
_have
_v
ideo
=
false
;
size_t
_max_second
;
string
_
strP
ath
;
string
_
strFile
;
string
_
strFileT
mp
;
string
_
folder_p
ath
;
string
_
full_path
;
string
_
full_path_t
mp
;
RecordInfo
_info
;
MP4Muxer
::
Ptr
_muxer
;
list
<
Track
::
Ptr
>
_tracks
;
uint64_t
_baseSec
=
0
;
uint64_t
_last_dts
=
0
;
};
#endif ///ENABLE_MP4
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论