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
e141eeb8
Commit
e141eeb8
authored
Apr 26, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
同步取消回调
parent
75cc3481
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
142 行增加
和
54 行删除
+142
-54
api/include/mk_player.h
+3
-3
api/source/mk_player.cpp
+139
-51
没有找到文件。
api/include/mk_player.h
查看文件 @
e141eeb8
...
...
@@ -83,7 +83,7 @@ API_EXPORT void API_CALL mk_player_seekto(mk_player ctx, float progress);
/**
* 设置播放器开启播放结果回调函数
* @param ctx 播放器指针
* @param cb 回调函数指针,
不得为null
* @param cb 回调函数指针,
设置null立即取消回调
* @param user_data 用户数据指针
*/
API_EXPORT
void
API_CALL
mk_player_set_on_result
(
mk_player
ctx
,
on_mk_play_event
cb
,
void
*
user_data
);
...
...
@@ -91,7 +91,7 @@ API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event
/**
* 设置播放被异常中断的回调
* @param ctx 播放器指针
* @param cb 回调函数指针,
不得为null
* @param cb 回调函数指针,
设置null立即取消回调
* @param user_data 用户数据指针
*/
API_EXPORT
void
API_CALL
mk_player_set_on_shutdown
(
mk_player
ctx
,
on_mk_play_event
cb
,
void
*
user_data
);
...
...
@@ -100,7 +100,7 @@ API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_eve
* 设置音视频数据回调函数
* 该接口在播放成功事件触发后才有效
* @param ctx 播放器指针
* @param cb 回调函数指针,
不得为null
* @param cb 回调函数指针,
设置null立即取消回调
* @param user_data 用户数据指针
*/
API_EXPORT
void
API_CALL
mk_player_set_on_data
(
mk_player
ctx
,
on_mk_play_data
cb
,
void
*
user_data
);
...
...
api/source/mk_player.cpp
查看文件 @
e141eeb8
...
...
@@ -15,19 +15,123 @@ using namespace std;
using
namespace
toolkit
;
using
namespace
mediakit
;
class
MediaPlayerForC
:
public
std
::
enable_shared_from_this
<
MediaPlayerForC
>
{
public
:
typedef
std
::
shared_ptr
<
MediaPlayerForC
>
Ptr
;
MediaPlayerForC
(){
_player
=
std
::
make_shared
<
MediaPlayer
>
();
}
~
MediaPlayerForC
(){}
MediaPlayer
*
operator
->
(){
return
_player
.
get
();
}
void
setup
(){
weak_ptr
<
MediaPlayerForC
>
weak_self
=
shared_from_this
();
_player
->
setOnPlayResult
([
weak_self
](
const
SockException
&
ex
){
auto
strong_self
=
weak_self
.
lock
();
if
(
strong_self
){
strong_self
->
onEvent
(
false
,
ex
);
}
});
_player
->
setOnShutdown
([
weak_self
](
const
SockException
&
ex
){
auto
strong_self
=
weak_self
.
lock
();
if
(
strong_self
){
strong_self
->
onEvent
(
true
,
ex
);
}
});
}
void
onEvent
(
bool
is_shutdown
,
const
SockException
&
ex
){
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
if
(
is_shutdown
){
//播放中断
if
(
_on_shutdown
){
_on_shutdown
(
_on_shutdown_data
,
ex
.
getErrCode
(),
ex
.
what
());
}
return
;
}
//播放结果
if
(
_on_play
){
_on_play
(
_on_play_data
,
ex
.
getErrCode
(),
ex
.
what
());
}
if
(
ex
){
//播放失败
return
;
}
//播放成功,添加事件回调
weak_ptr
<
MediaPlayerForC
>
weak_self
=
shared_from_this
();
auto
delegate
=
std
::
make_shared
<
FrameWriterInterfaceHelper
>
([
weak_self
](
const
Frame
::
Ptr
&
frame
)
{
auto
strong_self
=
weak_self
.
lock
();
if
(
strong_self
)
{
strong_self
->
onData
(
frame
);
}
});
for
(
auto
&
track
:
_player
->
getTracks
())
{
track
->
addDelegate
(
delegate
);
}
}
void
onData
(
const
Frame
::
Ptr
&
frame
){
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
if
(
_on_data
){
_on_data
(
_on_data_data
,
frame
->
getTrackType
(),
frame
->
getCodecId
(),
frame
->
data
(),
frame
->
size
(),
frame
->
dts
(),
frame
->
pts
());
}
}
void
setOnEvent
(
on_mk_play_event
cb
,
void
*
user_data
,
int
type
)
{
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
if
(
type
==
0
){
_on_play_data
=
user_data
;
_on_play
=
cb
;
}
else
{
_on_shutdown_data
=
user_data
;
_on_shutdown
=
cb
;
}
}
void
setOnData
(
on_mk_play_data
cb
,
void
*
user_data
)
{
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
_on_data_data
=
user_data
;
_on_data
=
cb
;
}
MediaPlayer
::
Ptr
&
getPlayer
(){
return
_player
;
}
private
:
MediaPlayer
::
Ptr
_player
;
recursive_mutex
_mtx
;
on_mk_play_event
_on_play
=
nullptr
;
on_mk_play_data
_on_data
=
nullptr
;
on_mk_play_event
_on_shutdown
=
nullptr
;
void
*
_on_play_data
=
nullptr
;
void
*
_on_shutdown_data
=
nullptr
;
void
*
_on_data_data
=
nullptr
;
};
API_EXPORT
mk_player
API_CALL
mk_player_create
()
{
MediaPlayer
::
Ptr
*
obj
=
new
MediaPlayer
::
Ptr
(
new
MediaPlayer
());
MediaPlayerForC
::
Ptr
*
obj
=
new
MediaPlayerForC
::
Ptr
(
new
MediaPlayerForC
());
(
*
obj
)
->
setup
();
return
obj
;
}
API_EXPORT
void
API_CALL
mk_player_release
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
*
obj
=
(
MediaPlayer
::
Ptr
*
)
ctx
;
MediaPlayer
ForC
::
Ptr
*
obj
=
(
MediaPlayerForC
::
Ptr
*
)
ctx
;
delete
obj
;
}
API_EXPORT
void
API_CALL
mk_player_set_option
(
mk_player
ctx
,
const
char
*
key
,
const
char
*
val
){
assert
(
ctx
&&
key
&&
val
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
MediaPlayerForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
player
=
obj
.
getPlayer
();
string
key_str
(
key
),
val_str
(
val
);
player
->
getPoller
()
->
async
([
key_str
,
val_str
,
player
](){
//切换线程后再操作
...
...
@@ -36,7 +140,8 @@ API_EXPORT void API_CALL mk_player_set_option(mk_player ctx,const char* key,cons
}
API_EXPORT
void
API_CALL
mk_player_play
(
mk_player
ctx
,
const
char
*
url
)
{
assert
(
ctx
&&
url
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
MediaPlayerForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
player
=
obj
.
getPlayer
();
string
url_str
(
url
);
player
->
getPoller
()
->
async
([
url_str
,
player
](){
//切换线程后再操作
...
...
@@ -46,7 +151,8 @@ API_EXPORT void API_CALL mk_player_play(mk_player ctx, const char *url) {
API_EXPORT
void
API_CALL
mk_player_pause
(
mk_player
ctx
,
int
pause
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
MediaPlayerForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
player
=
obj
.
getPlayer
();
player
->
getPoller
()
->
async
([
pause
,
player
](){
//切换线程后再操作
player
->
pause
(
pause
);
...
...
@@ -55,7 +161,8 @@ API_EXPORT void API_CALL mk_player_pause(mk_player ctx, int pause) {
API_EXPORT
void
API_CALL
mk_player_seekto
(
mk_player
ctx
,
float
progress
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
MediaPlayerForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
player
=
obj
.
getPlayer
();
player
->
getPoller
()
->
async
([
progress
,
player
](){
//切换线程后再操作
player
->
seekTo
(
progress
);
...
...
@@ -64,19 +171,8 @@ API_EXPORT void API_CALL mk_player_seekto(mk_player ctx, float progress) {
static
void
mk_player_set_on_event
(
mk_player
ctx
,
on_mk_play_event
cb
,
void
*
user_data
,
int
type
)
{
assert
(
ctx
&&
cb
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
player
->
getPoller
()
->
async
([
cb
,
user_data
,
type
,
player
](){
//切换线程后再操作
if
(
type
==
0
){
player
->
setOnPlayResult
([
cb
,
user_data
](
const
SockException
&
ex
){
cb
(
user_data
,
ex
.
getErrCode
(),
ex
.
what
());
});
}
else
{
player
->
setOnShutdown
([
cb
,
user_data
](
const
SockException
&
ex
){
cb
(
user_data
,
ex
.
getErrCode
(),
ex
.
what
());
});
}
});
MediaPlayerForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
obj
.
setOnEvent
(
cb
,
user_data
,
type
);
}
API_EXPORT
void
API_CALL
mk_player_set_on_result
(
mk_player
ctx
,
on_mk_play_event
cb
,
void
*
user_data
)
{
...
...
@@ -89,88 +185,80 @@ API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_eve
API_EXPORT
void
API_CALL
mk_player_set_on_data
(
mk_player
ctx
,
on_mk_play_data
cb
,
void
*
user_data
)
{
assert
(
ctx
&&
cb
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
player
->
getPoller
()
->
async
([
player
,
cb
,
user_data
](){
//切换线程后再操作
auto
delegate
=
std
::
make_shared
<
FrameWriterInterfaceHelper
>
([
cb
,
user_data
](
const
Frame
::
Ptr
&
frame
){
cb
(
user_data
,
frame
->
getTrackType
(),
frame
->
getCodecId
(),
frame
->
data
(),
frame
->
size
(),
frame
->
dts
(),
frame
->
pts
());
});
for
(
auto
&
track
:
player
->
getTracks
()){
track
->
addDelegate
(
delegate
);
}
});
MediaPlayerForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
obj
.
setOnData
(
cb
,
user_data
);
}
API_EXPORT
int
API_CALL
mk_player_video_codecId
(
mk_player
ctx
){
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
player
->
getTrack
(
TrackVideo
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
obj
->
getTrack
(
TrackVideo
));
return
track
?
track
->
getCodecId
()
:
CodecInvalid
;
}
API_EXPORT
int
API_CALL
mk_player_video_width
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
player
->
getTrack
(
TrackVideo
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
obj
->
getTrack
(
TrackVideo
));
return
track
?
track
->
getVideoWidth
()
:
0
;
}
API_EXPORT
int
API_CALL
mk_player_video_height
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
player
->
getTrack
(
TrackVideo
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
obj
->
getTrack
(
TrackVideo
));
return
track
?
track
->
getVideoHeight
()
:
0
;
}
API_EXPORT
int
API_CALL
mk_player_video_fps
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
player
->
getTrack
(
TrackVideo
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
VideoTrack
>
(
obj
->
getTrack
(
TrackVideo
));
return
track
?
track
->
getVideoFps
()
:
0
;
}
API_EXPORT
int
API_CALL
mk_player_audio_codecId
(
mk_player
ctx
){
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
player
->
getTrack
(
TrackAudio
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
obj
->
getTrack
(
TrackAudio
));
return
track
?
track
->
getCodecId
()
:
CodecInvalid
;
}
API_EXPORT
int
API_CALL
mk_player_audio_samplerate
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
player
->
getTrack
(
TrackAudio
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
obj
->
getTrack
(
TrackAudio
));
return
track
?
track
->
getAudioSampleRate
()
:
0
;
}
API_EXPORT
int
API_CALL
mk_player_audio_bit
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
player
->
getTrack
(
TrackAudio
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
obj
->
getTrack
(
TrackAudio
));
return
track
?
track
->
getAudioSampleBit
()
:
0
;
}
API_EXPORT
int
API_CALL
mk_player_audio_channel
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
player
->
getTrack
(
TrackAudio
));
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
auto
track
=
dynamic_pointer_cast
<
AudioTrack
>
(
obj
->
getTrack
(
TrackAudio
));
return
track
?
track
->
getAudioChannel
()
:
0
;
}
API_EXPORT
float
API_CALL
mk_player_duration
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
return
player
->
getDuration
();
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
return
obj
->
getDuration
();
}
API_EXPORT
float
API_CALL
mk_player_progress
(
mk_player
ctx
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
return
player
->
getProgress
();
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
return
obj
->
getProgress
();
}
API_EXPORT
float
API_CALL
mk_player_loss_rate
(
mk_player
ctx
,
int
track_type
)
{
assert
(
ctx
);
MediaPlayer
::
Ptr
&
player
=
*
((
MediaPlayer
::
Ptr
*
)
ctx
);
return
player
->
getPacketLossRate
((
TrackType
)
track_type
);
MediaPlayer
ForC
&
obj
=
**
((
MediaPlayerForC
::
Ptr
*
)
ctx
);
return
obj
->
getPacketLossRate
((
TrackType
)
track_type
);
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论