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
3921bfb2
Commit
3921bfb2
authored
Oct 24, 2018
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成Rtmp解复用器
parent
de50bf03
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
126 行增加
和
83 行删除
+126
-83
src/Common/Factory.cpp
+16
-1
src/Common/Factory.h
+9
-1
src/RtmpMuxer/AACRtmpCodec.h
+1
-1
src/RtmpMuxer/RtmpDemuxer.cpp
+0
-0
src/RtmpMuxer/RtmpDemuxer.h
+52
-59
src/RtspMuxer/RtspDemuxer.cpp
+31
-5
src/RtspMuxer/RtspDemuxer.h
+17
-16
没有找到文件。
src/Common/Factory.cpp
查看文件 @
3921bfb2
...
...
@@ -3,6 +3,10 @@
//
#include "Factory.h"
#include "RtmpMuxer/H264RtmpCodec.h"
#include "RtmpMuxer/AACRtmpCodec.h"
#include "RtspMuxer/H264RtpCodec.h"
#include "RtspMuxer/AACRtpCodec.h"
namespace
mediakit
{
...
...
@@ -75,7 +79,7 @@ Track::Ptr Factory::getTrackBySdp(const string &sdp) {
}
CodecId
getCodecIdByAmf
(
const
AMFValue
&
val
){
CodecId
Factory
::
getCodecIdByAmf
(
const
AMFValue
&
val
){
if
(
val
.
type
()
==
AMF_STRING
){
auto
str
=
val
.
as_string
();
if
(
str
==
"avc1"
){
...
...
@@ -153,6 +157,17 @@ RtpCodec::Ptr Factory::getRtpDecoderById(CodecId codecId, uint32_t ui32SampleRat
}
}
RtmpCodec
::
Ptr
Factory
::
getRtmpCodecById
(
CodecId
codecId
)
{
switch
(
codecId
){
case
CodecH264
:
return
std
::
make_shared
<
H264RtmpEncoder
>
();
case
CodecAAC
:
return
std
::
make_shared
<
AACRtmpEncoder
>
();
default
:
return
nullptr
;
}
}
}
//namespace mediakit
src/Common/Factory.h
查看文件 @
3921bfb2
...
...
@@ -8,6 +8,8 @@
#include <string>
#include "Player/Track.h"
#include "RtspMuxer/RtspSdp.h"
#include "RtspMuxer/RtpCodec.h"
#include "RtmpMuxer/RtmpCodec.h"
#include "Rtmp/amf.h"
using
namespace
std
;
...
...
@@ -21,7 +23,6 @@ public:
* 根据sdp生成Track对象
*/
static
Track
::
Ptr
getTrackBySdp
(
const
string
&
sdp
);
static
Track
::
Ptr
getTrackByAmf
(
const
AMFValue
&
amf
);
static
Track
::
Ptr
getTrackByCodecId
(
CodecId
codecId
);
...
...
@@ -57,6 +58,13 @@ public:
* @return
*/
static
RtpCodec
::
Ptr
getRtpDecoderById
(
CodecId
codecId
,
uint32_t
ui32SampleRate
);
//////////////////////////////////////////////////////////////////
static
Track
::
Ptr
getTrackByAmf
(
const
AMFValue
&
amf
);
static
CodecId
getCodecIdByAmf
(
const
AMFValue
&
val
);
static
RtmpCodec
::
Ptr
getRtmpCodecById
(
CodecId
codecId
);
};
}
//namespace mediakit
...
...
src/RtmpMuxer/AACRtmpCodec.h
查看文件 @
3921bfb2
...
...
@@ -49,7 +49,7 @@ class AACRtmpEncoder : public AACRtmpDecoder , public ResourcePoolHelper<RtmpPa
public
:
typedef
std
::
shared_ptr
<
AACRtmpEncoder
>
Ptr
;
AACRtmpEncoder
()
;
AACRtmpEncoder
()
{}
~
AACRtmpEncoder
()
{}
/**
...
...
src/RtmpMuxer/RtmpDemuxer.cpp
查看文件 @
3921bfb2
差异被折叠。
点击展开。
src/RtmpMuxer/RtmpDemuxer.h
查看文件 @
3921bfb2
...
...
@@ -32,12 +32,11 @@
#include "Rtmp/amf.h"
#include "Rtmp/Rtmp.h"
#include "Player/Player.h"
#include "Util/TimeTicker.h"
#include "Player/PlayerBase.h"
using
namespace
toolkit
;
#include "Util/TimeTicker.h"
#include "RtmpCodec.h"
#define H264_CODEC_ID 7
#define AAC_CODEC_ID 10
using
namespace
toolkit
;
namespace
mediakit
{
...
...
@@ -47,67 +46,61 @@ public:
RtmpDemuxer
(
const
AMFValue
&
val
);
virtual
~
RtmpDemuxer
();
/**
* 开始解复用
* @param pkt rtmp包
* @return true 代表是i帧
*/
bool
inputRtmp
(
const
RtmpPacket
::
Ptr
&
pkt
);
bool
isInited
()
const
override
{
if
((
_iAudioCodecID
|
_iVideoCodecID
)
==
0
){
//音视频codec_id都未获取到,说明还未初始化成功
return
false
;
}
if
((
_iAudioCodecID
&
_iVideoCodecID
)
==
0
&&
_ticker
.
elapsedTime
()
<
300
){
//音视频codec_id有其一未获取到,且最少分析300ms才能断定没有音频或视频
return
false
;
}
if
(
_iAudioCodecID
&&
!
_strAudioCfg
.
size
())
{
//如果音频是aac但是还未获取aac config ,则未初始化成功
return
false
;
}
if
(
_iVideoCodecID
&&
!
_strSPS
.
size
())
{
//如果视频是h264但是还未获取sps ,则未初始化成功
return
false
;
}
//初始化成功
return
true
;
}
float
getDuration
()
const
override
{
return
_fDuration
;
}
private
:
inline
void
onCheckMedia
(
const
AMFValue
&
obj
);
//返回值:true 代表是i帧第一个rtp包
inline
bool
inputVideo
(
const
RtmpPacket
::
Ptr
&
pkt
);
inline
bool
inputAudio
(
const
RtmpPacket
::
Ptr
&
pkt
);
inline
void
_onGetH264
(
const
char
*
pcData
,
int
iLen
,
uint32_t
ui32TimeStamp
);
inline
void
onGetH264
(
const
char
*
pcData
,
int
iLen
,
uint32_t
ui32TimeStamp
);
inline
void
onGetAAC
(
const
char
*
pcData
,
int
iLen
,
uint32_t
ui32TimeStamp
);
//video
H264Frame
_h264frame
;
//aduio
AACFrame
_adts
;
int
_iSampleRate
=
44100
;
int
_iSampleBit
=
16
;
int
_iChannel
=
1
;
string
_strSPS
;
string
_strPPS
;
string
_strAudioCfg
;
int
_iVideoWidth
=
0
;
int
_iVideoHeight
=
0
;
float
_fVideoFps
=
0
;
//音视频codec_id初始为0代表尚未获取到
int
_iAudioCodecID
=
0
;
int
_iVideoCodecID
=
0
;
float
_fDuration
=
0
;
mutable
Ticker
_ticker
;
function
<
void
(
const
H264Frame
&
frame
)
>
onVideo
;
function
<
void
(
const
AACFrame
&
frame
)
>
onAudio
;
recursive_mutex
_mtxCB
;
/**
* 获取节目总时长
* @return
*/
float
getDuration
()
const
override
;
/**
* 返回是否完成初始化完毕
* 由于在构造该对象时是无法获取sps pps aac_cfg等这些信息,
* 所以要调用inputRtmp后才会获取到这些信息,这时才初始化成功
* @return
*/
bool
isInited
()
const
override
;
/**
* 获取所有可用Track,请在isInited()返回true时调用
* @return
*/
vector
<
Track
::
Ptr
>
getTracks
()
const
override
;
private
:
void
makeVideoTrack
(
const
AMFValue
&
val
);
void
makeAudioTrack
(
const
AMFValue
&
val
);
private
:
float
_fDuration
=
0
;
bool
_tryGetVideoTrack
=
false
;
bool
_tryGetAudioTrack
=
false
;
AudioTrack
::
Ptr
_audioTrack
;
VideoTrack
::
Ptr
_videoTrack
;
RtmpCodec
::
Ptr
_audioRtmpDecoder
;
RtmpCodec
::
Ptr
_videoRtmpDecoder
;
};
}
/* namespace mediakit */
#endif
/* SRC_RTMP_RTMPDEMUXER_H_ */
src/RtspMuxer/RtspDemuxer.cpp
查看文件 @
3921bfb2
...
...
@@ -56,11 +56,11 @@ RtspDemuxer::RtspDemuxer(const string& sdp) {
for
(
int
i
=
0
;
i
<
cnt
;
i
++
)
{
switch
(
tmp
[
i
].
type
)
{
case
TrackVideo
:
{
onGet
VideoTrack
(
tmp
[
i
]);
make
VideoTrack
(
tmp
[
i
]);
}
break
;
case
TrackAudio
:
{
onGet
AudioTrack
(
tmp
[
i
]);
make
AudioTrack
(
tmp
[
i
]);
}
break
;
default
:
...
...
@@ -79,7 +79,10 @@ bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
return
false
;
}
case
TrackAudio
:{
_audioRtpDecoder
->
inputRtp
(
rtp
,
false
);
if
(
_audioRtpDecoder
){
_audioRtpDecoder
->
inputRtp
(
rtp
,
false
);
return
false
;
}
return
false
;
}
default
:
...
...
@@ -88,7 +91,7 @@ bool RtspDemuxer::inputRtp(const RtpPacket::Ptr & rtp) {
}
inline
void
RtspDemuxer
::
onGetAudioTrack
(
const
RtspTrack
&
audio
)
{
void
RtspDemuxer
::
makeAudioTrack
(
const
RtspTrack
&
audio
)
{
//生成Track对象
_audioTrack
=
dynamic_pointer_cast
<
AudioTrack
>
(
Factory
::
getTrackBySdp
(
audio
.
trackSdp
));
if
(
_audioTrack
){
...
...
@@ -97,11 +100,14 @@ inline void RtspDemuxer::onGetAudioTrack(const RtspTrack& audio) {
if
(
_audioRtpDecoder
){
//设置rtp解码器代理,生成的frame写入该Track
_audioRtpDecoder
->
setDelegate
(
_audioTrack
);
}
else
{
//找不到相应的rtp解码器,该track无效
_audioTrack
.
reset
();
}
}
}
inline
void
RtspDemuxer
::
onGetVideoTrack
(
const
RtspTrack
&
video
)
{
void
RtspDemuxer
::
makeVideoTrack
(
const
RtspTrack
&
video
)
{
//生成Track对象
_videoTrack
=
dynamic_pointer_cast
<
VideoTrack
>
(
Factory
::
getTrackBySdp
(
video
.
trackSdp
));
if
(
_videoTrack
){
...
...
@@ -110,6 +116,9 @@ inline void RtspDemuxer::onGetVideoTrack(const RtspTrack& video) {
if
(
_videoRtpDecoder
){
//设置rtp解码器代理,生成的frame写入该Track
_videoRtpDecoder
->
setDelegate
(
_videoTrack
);
}
else
{
//找不到相应的rtp解码器,该track无效
_videoTrack
.
reset
();
}
}
}
...
...
@@ -125,5 +134,22 @@ vector<Track::Ptr> RtspDemuxer::getTracks() const {
return
ret
;
}
bool
RtspDemuxer
::
isInited
()
const
{
bool
ret
=
true
;
if
(
ret
&&
_audioTrack
){
//getTrackType() 等于TrackInvalid时说明该Track还未准备好
ret
=
_audioTrack
->
getTrackType
()
!=
TrackInvalid
;
}
if
(
ret
&&
_videoTrack
){
//getTrackType() 等于TrackInvalid时说明该Track还未准备好
ret
=
_videoTrack
->
getTrackType
()
!=
TrackInvalid
;
}
return
ret
;
}
float
RtspDemuxer
::
getDuration
()
const
{
return
_fDuration
;
}
}
/* namespace mediakit */
src/RtspMuxer/RtspDemuxer.h
查看文件 @
3921bfb2
...
...
@@ -45,12 +45,18 @@ public:
RtspDemuxer
(
const
string
&
sdp
);
virtual
~
RtspDemuxer
(){};
//返回值:true 代表是i帧第一个rtp包
/**
* 开始解复用
* @param rtp rtp包
* @return true 代表是i帧第一个rtp包
*/
bool
inputRtp
(
const
RtpPacket
::
Ptr
&
rtp
);
float
getDuration
()
const
override
{
return
_fDuration
;
}
/**
* 获取节目总时长
* @return
*/
float
getDuration
()
const
override
;
/**
* 返回是否完成初始化完毕
...
...
@@ -58,21 +64,16 @@ public:
* 所以要等待接收到到sps的rtp包后才能完成
* @return
*/
bool
isInited
()
const
override
{
bool
ret
=
true
;
if
(
ret
&&
_audioTrack
){
ret
=
_audioTrack
->
getTrackType
()
!=
TrackInvalid
;
}
if
(
ret
&&
_videoTrack
){
ret
=
_videoTrack
->
getTrackType
()
!=
TrackInvalid
;
}
return
ret
;
}
bool
isInited
()
const
override
;
/**
* 获取所有可用Track,请在isInited()返回true时调用
* @return
*/
vector
<
Track
::
Ptr
>
getTracks
()
const
override
;
private
:
inline
void
onGet
AudioTrack
(
const
RtspTrack
&
audio
);
inline
void
onGet
VideoTrack
(
const
RtspTrack
&
video
);
void
make
AudioTrack
(
const
RtspTrack
&
audio
);
void
make
VideoTrack
(
const
RtspTrack
&
video
);
private
:
float
_fDuration
=
0
;
AudioTrack
::
Ptr
_audioTrack
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论