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
4db67aaa
Commit
4db67aaa
authored
May 17, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
单线程提供线程安全
parent
2a8b6d2b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
35 行增加
和
29 行删除
+35
-29
tests/test_pusher.cpp
+16
-15
tests/test_pusherMp4.cpp
+19
-14
没有找到文件。
tests/test_pusher.cpp
查看文件 @
4db67aaa
...
...
@@ -40,28 +40,29 @@ using namespace mediakit;
//推流器,保持强引用
MediaPusher
::
Ptr
pusher
;
Timer
::
Ptr
g_timer
;
//声明函数
void
rePushDelay
(
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
url
);
void
rePushDelay
(
const
EventPoller
::
Ptr
&
poller
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
url
);
//创建推流器并开始推流
void
createPusher
(
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
url
)
{
void
createPusher
(
const
EventPoller
::
Ptr
&
poller
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
url
)
{
//创建推流器并绑定一个MediaSource
pusher
.
reset
(
new
MediaPusher
(
schema
,
vhost
,
app
,
stream
));
pusher
.
reset
(
new
MediaPusher
(
schema
,
vhost
,
app
,
stream
,
poller
));
//可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
// (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
//设置推流中断处理逻辑
pusher
->
setOnShutdown
([
schema
,
vhost
,
app
,
stream
,
url
](
const
SockException
&
ex
)
{
pusher
->
setOnShutdown
([
poller
,
schema
,
vhost
,
app
,
stream
,
url
](
const
SockException
&
ex
)
{
WarnL
<<
"Server connection is closed:"
<<
ex
.
getErrCode
()
<<
" "
<<
ex
.
what
();
//重试
rePushDelay
(
schema
,
vhost
,
app
,
stream
,
url
);
rePushDelay
(
poller
,
schema
,
vhost
,
app
,
stream
,
url
);
});
//设置发布结果处理逻辑
pusher
->
setOnPublished
([
schema
,
vhost
,
app
,
stream
,
url
](
const
SockException
&
ex
)
{
pusher
->
setOnPublished
([
poller
,
schema
,
vhost
,
app
,
stream
,
url
](
const
SockException
&
ex
)
{
if
(
ex
)
{
WarnL
<<
"Publish fail:"
<<
ex
.
getErrCode
()
<<
" "
<<
ex
.
what
();
//如果发布失败,就重试
rePushDelay
(
schema
,
vhost
,
app
,
stream
,
url
);
rePushDelay
(
poller
,
schema
,
vhost
,
app
,
stream
,
url
);
}
else
{
InfoL
<<
"Publish success,Please play with player:"
<<
url
;
}
...
...
@@ -69,16 +70,15 @@ void createPusher(const string &schema,const string &vhost,const string &app, co
pusher
->
publish
(
url
);
}
Timer
::
Ptr
g_timer
;
//推流失败或断开延迟2秒后重试推流
void
rePushDelay
(
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
url
)
{
g_timer
=
std
::
make_shared
<
Timer
>
(
2
,[
schema
,
vhost
,
app
,
stream
,
url
]()
{
void
rePushDelay
(
const
EventPoller
::
Ptr
&
poller
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
url
)
{
g_timer
=
std
::
make_shared
<
Timer
>
(
2
,[
poller
,
schema
,
vhost
,
app
,
stream
,
url
]()
{
InfoL
<<
"Re-Publishing..."
;
//重新推流
createPusher
(
schema
,
vhost
,
app
,
stream
,
url
);
createPusher
(
poller
,
schema
,
vhost
,
app
,
stream
,
url
);
//此任务不重复
return
false
;
},
nullpt
r
);
},
polle
r
);
}
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
...
...
@@ -86,18 +86,19 @@ int domain(const string &playUrl, const string &pushUrl) {
//设置日志
Logger
::
Instance
().
add
(
std
::
make_shared
<
ConsoleChannel
>
());
Logger
::
Instance
().
setWriter
(
std
::
make_shared
<
AsyncLogWriter
>
());
auto
poller
=
EventPollerPool
::
Instance
().
getPoller
();
//拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
//你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
PlayerProxy
::
Ptr
player
(
new
PlayerProxy
(
DEFAULT_VHOST
,
"app"
,
"stream"
));
PlayerProxy
::
Ptr
player
(
new
PlayerProxy
(
DEFAULT_VHOST
,
"app"
,
"stream"
,
false
,
false
,
-
1
,
poller
));
player
->
play
(
playUrl
.
data
());
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
NoticeCenter
::
Instance
().
addListener
(
nullptr
,
Broadcast
::
kBroadcastMediaChanged
,
[
pushUrl
](
BroadcastMediaChangedArgs
)
{
[
pushUrl
,
poller
](
BroadcastMediaChangedArgs
)
{
//媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
if
(
bRegist
&&
pushUrl
.
find
(
schema
)
==
0
){
createPusher
(
schema
,
vhost
,
app
,
stream
,
pushUrl
);
createPusher
(
poller
,
schema
,
vhost
,
app
,
stream
,
pushUrl
);
}
});
...
...
tests/test_pusherMp4.cpp
查看文件 @
4db67aaa
...
...
@@ -41,10 +41,13 @@ using namespace mediakit;
//推流器,保持强引用
MediaPusher
::
Ptr
pusher
;
Timer
::
Ptr
g_timer
;
//声明函数
//推流失败或断开延迟2秒后重试推流
void
rePushDelay
(
const
string
&
schema
,
void
rePushDelay
(
const
EventPoller
::
Ptr
&
poller
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
...
...
@@ -52,7 +55,8 @@ void rePushDelay(const string &schema,
const
string
&
url
)
;
//创建推流器并开始推流
void
createPusher
(
const
string
&
schema
,
void
createPusher
(
const
EventPoller
::
Ptr
&
poller
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
...
...
@@ -67,22 +71,22 @@ void createPusher(const string &schema,
}
//创建推流器并绑定一个MediaSource
pusher
.
reset
(
new
MediaPusher
(
src
));
pusher
.
reset
(
new
MediaPusher
(
src
,
poller
));
//可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
// (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
//设置推流中断处理逻辑
pusher
->
setOnShutdown
([
schema
,
vhost
,
app
,
stream
,
filePath
,
url
](
const
SockException
&
ex
)
{
pusher
->
setOnShutdown
([
poller
,
schema
,
vhost
,
app
,
stream
,
filePath
,
url
](
const
SockException
&
ex
)
{
WarnL
<<
"Server connection is closed:"
<<
ex
.
getErrCode
()
<<
" "
<<
ex
.
what
();
//重新推流
rePushDelay
(
schema
,
vhost
,
app
,
stream
,
filePath
,
url
);
rePushDelay
(
poller
,
schema
,
vhost
,
app
,
stream
,
filePath
,
url
);
});
//设置发布结果处理逻辑
pusher
->
setOnPublished
([
schema
,
vhost
,
app
,
stream
,
filePath
,
url
](
const
SockException
&
ex
)
{
pusher
->
setOnPublished
([
poller
,
schema
,
vhost
,
app
,
stream
,
filePath
,
url
](
const
SockException
&
ex
)
{
if
(
ex
)
{
WarnL
<<
"Publish fail:"
<<
ex
.
getErrCode
()
<<
" "
<<
ex
.
what
();
//如果发布失败,就重试
rePushDelay
(
schema
,
vhost
,
app
,
stream
,
filePath
,
url
);
rePushDelay
(
poller
,
schema
,
vhost
,
app
,
stream
,
filePath
,
url
);
}
else
{
InfoL
<<
"Publish success,Please play with player:"
<<
url
;
}
...
...
@@ -90,21 +94,21 @@ void createPusher(const string &schema,
pusher
->
publish
(
url
);
}
Timer
::
Ptr
g_timer
;
//推流失败或断开延迟2秒后重试推流
void
rePushDelay
(
const
string
&
schema
,
void
rePushDelay
(
const
EventPoller
::
Ptr
&
poller
,
const
string
&
schema
,
const
string
&
vhost
,
const
string
&
app
,
const
string
&
stream
,
const
string
&
filePath
,
const
string
&
url
)
{
g_timer
=
std
::
make_shared
<
Timer
>
(
2
,[
schema
,
vhost
,
app
,
stream
,
filePath
,
url
]()
{
g_timer
=
std
::
make_shared
<
Timer
>
(
2
,[
poller
,
schema
,
vhost
,
app
,
stream
,
filePath
,
url
]()
{
InfoL
<<
"Re-Publishing..."
;
//重新推流
createPusher
(
schema
,
vhost
,
app
,
stream
,
filePath
,
url
);
createPusher
(
poller
,
schema
,
vhost
,
app
,
stream
,
filePath
,
url
);
//此任务不重复
return
false
;
},
nullpt
r
);
},
polle
r
);
}
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
...
...
@@ -112,8 +116,9 @@ int domain(const string & filePath,const string & pushUrl){
//设置日志
Logger
::
Instance
().
add
(
std
::
make_shared
<
ConsoleChannel
>
());
Logger
::
Instance
().
setWriter
(
std
::
make_shared
<
AsyncLogWriter
>
());
//vhost/app/stream可以随便自己填,现在不限制app应用名了
createPusher
(
FindField
(
pushUrl
.
data
(),
nullptr
,
"://"
),
DEFAULT_VHOST
,
"live"
,
"stream"
,
filePath
,
pushUrl
);
auto
poller
=
EventPollerPool
::
Instance
().
getPoller
();
//vhost/app/stream可以随便自己填,现在不限制app应用名了
createPusher
(
poller
,
FindField
(
pushUrl
.
data
(),
nullptr
,
"://"
),
DEFAULT_VHOST
,
"live"
,
"stream"
,
filePath
,
pushUrl
);
//设置退出信号处理函数
static
semaphore
sem
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论