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
f85de2e1
Commit
f85de2e1
authored
Aug 05, 2022
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
精简复用rtp encoder创建相关代码
parent
d3ac2962
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
92 行增加
和
110 行删除
+92
-110
src/Extension/Factory.cpp
+45
-44
src/Extension/Factory.h
+9
-0
src/Rtp/RawEncoder.cpp
+26
-54
src/Rtp/RawEncoder.h
+12
-11
src/Rtp/RtpCache.h
+0
-1
没有找到文件。
src/Extension/Factory.cpp
查看文件 @
f85de2e1
...
...
@@ -95,69 +95,70 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
}
}
Track
::
Ptr
Factory
::
getTrackByAbstractTrack
(
const
Track
::
Ptr
&
track
)
{
auto
codec
=
track
->
getCodecId
();
switch
(
codec
)
{
case
CodecG711A
:
case
CodecG711U
:
{
auto
audio_track
=
dynamic_pointer_cast
<
AudioTrackImp
>
(
track
);
return
std
::
make_shared
<
G711Track
>
(
codec
,
audio_track
->
getAudioSampleRate
(),
audio_track
->
getAudioChannel
(),
16
);
}
case
CodecL16
:
{
auto
audio_track
=
dynamic_pointer_cast
<
AudioTrackImp
>
(
track
);
return
std
::
make_shared
<
L16Track
>
(
audio_track
->
getAudioSampleRate
(),
audio_track
->
getAudioChannel
());
}
case
CodecAAC
:
return
std
::
make_shared
<
AACTrack
>
();
case
CodecOpus
:
return
std
::
make_shared
<
OpusTrack
>
();
case
CodecH265
:
return
std
::
make_shared
<
H265Track
>
();
case
CodecH264
:
return
std
::
make_shared
<
H264Track
>
();
case
CodecG711A
:
case
CodecG711U
:
{
auto
audio_track
=
dynamic_pointer_cast
<
AudioTrackImp
>
(
track
);
return
std
::
make_shared
<
G711Track
>
(
codec
,
audio_track
->
getAudioSampleRate
(),
audio_track
->
getAudioChannel
(),
16
);
}
case
CodecL16
:
{
auto
audio_track
=
dynamic_pointer_cast
<
AudioTrackImp
>
(
track
);
return
std
::
make_shared
<
L16Track
>
(
audio_track
->
getAudioSampleRate
(),
audio_track
->
getAudioChannel
());
}
case
CodecAAC
:
return
std
::
make_shared
<
AACTrack
>
();
case
CodecOpus
:
return
std
::
make_shared
<
OpusTrack
>
();
case
CodecH265
:
return
std
::
make_shared
<
H265Track
>
();
case
CodecH264
:
return
std
::
make_shared
<
H264Track
>
();
default
:
{
//其他codec不支持
WarnL
<<
"暂不支持该该编码类型创建Track:"
<<
track
->
getCodecName
();
return
nullptr
;
default
:
{
//其他codec不支持
WarnL
<<
"暂不支持该该编码类型创建Track:"
<<
track
->
getCodecName
();
return
nullptr
;
}
}
}
RtpCodec
::
Ptr
Factory
::
getRtpEncoderByCodecId
(
CodecId
codec_id
,
uint32_t
sample_rate
,
uint8_t
pt
,
uint32_t
ssrc
)
{
GET_CONFIG
(
uint32_t
,
audio_mtu
,
Rtp
::
kAudioMtuSize
);
GET_CONFIG
(
uint32_t
,
video_mtu
,
Rtp
::
kVideoMtuSize
);
auto
type
=
getTrackType
(
codec_id
);
auto
mtu
=
type
==
TrackVideo
?
video_mtu
:
audio_mtu
;
auto
interleaved
=
type
*
2
;
switch
(
codec_id
)
{
case
CodecH264
:
return
std
::
make_shared
<
H264RtpEncoder
>
(
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecH265
:
return
std
::
make_shared
<
H265RtpEncoder
>
(
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecAAC
:
return
std
::
make_shared
<
AACRtpEncoder
>
(
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecL16
:
case
CodecOpus
:
return
std
::
make_shared
<
CommonRtpEncoder
>
(
codec_id
,
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecG711A
:
case
CodecG711U
:
{
if
(
pt
==
Rtsp
::
PT_PCMA
||
pt
==
Rtsp
::
PT_PCMU
)
{
return
std
::
make_shared
<
G711RtpEncoder
>
(
codec_id
,
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
,
1
);
}
return
std
::
make_shared
<
CommonRtpEncoder
>
(
codec_id
,
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
}
default
:
WarnL
<<
"暂不支持该CodecId:"
<<
codec_id
;
return
nullptr
;
}
}
RtpCodec
::
Ptr
Factory
::
getRtpEncoderBySdp
(
const
Sdp
::
Ptr
&
sdp
)
{
GET_CONFIG
(
uint32_t
,
audio_mtu
,
Rtp
::
kAudioMtuSize
);
GET_CONFIG
(
uint32_t
,
video_mtu
,
Rtp
::
kVideoMtuSize
);
// ssrc不冲突即可,可以为任意的32位整形
static
atomic
<
uint32_t
>
s_ssrc
(
0
);
uint32_t
ssrc
=
s_ssrc
++
;
if
(
!
ssrc
)
{
//ssrc不能为0
if
(
!
ssrc
)
{
//
ssrc不能为0
ssrc
=
1
;
}
if
(
sdp
->
getTrackType
()
==
TrackVideo
)
{
if
(
sdp
->
getTrackType
()
==
TrackVideo
)
{
//视频的ssrc是偶数,方便调试
ssrc
=
2
*
ssrc
;
}
else
{
}
else
{
//音频ssrc是奇数
ssrc
=
2
*
ssrc
+
1
;
}
auto
mtu
=
(
sdp
->
getTrackType
()
==
TrackVideo
?
video_mtu
:
audio_mtu
);
auto
sample_rate
=
sdp
->
getSampleRate
();
auto
pt
=
sdp
->
getPayloadType
();
auto
interleaved
=
sdp
->
getTrackType
()
*
2
;
auto
codec_id
=
sdp
->
getCodecId
();
switch
(
codec_id
){
case
CodecH264
:
return
std
::
make_shared
<
H264RtpEncoder
>
(
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecH265
:
return
std
::
make_shared
<
H265RtpEncoder
>
(
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecAAC
:
return
std
::
make_shared
<
AACRtpEncoder
>
(
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecL16
:
case
CodecOpus
:
return
std
::
make_shared
<
CommonRtpEncoder
>
(
codec_id
,
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
case
CodecG711A
:
case
CodecG711U
:
{
if
(
pt
==
Rtsp
::
PT_PCMA
||
pt
==
Rtsp
::
PT_PCMU
)
{
return
std
::
make_shared
<
G711RtpEncoder
>
(
codec_id
,
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
,
1
);
}
return
std
::
make_shared
<
CommonRtpEncoder
>
(
codec_id
,
ssrc
,
mtu
,
sample_rate
,
pt
,
interleaved
);
}
default
:
WarnL
<<
"暂不支持该CodecId:"
<<
codec_id
;
return
nullptr
;
}
return
getRtpEncoderByCodecId
(
sdp
->
getCodecId
(),
sdp
->
getSampleRate
(),
sdp
->
getPayloadType
(),
ssrc
);
}
RtpCodec
::
Ptr
Factory
::
getRtpDecoderByTrack
(
const
Track
::
Ptr
&
track
)
{
...
...
src/Extension/Factory.h
查看文件 @
f85de2e1
...
...
@@ -39,6 +39,15 @@ public:
static
RtpCodec
::
Ptr
getRtpEncoderBySdp
(
const
Sdp
::
Ptr
&
sdp
);
/**
* 根据codec id生成rtp编码器
* @param codec_id 编码id
* @param sample_rate 采样率,视频固定为90000
* @param pt rtp payload type
* @param ssrc rtp ssrc
*/
static
RtpCodec
::
Ptr
getRtpEncoderByCodecId
(
CodecId
codec_id
,
uint32_t
sample_rate
,
uint8_t
pt
,
uint32_t
ssrc
);
/**
* 根据Track生成Rtp解包器
*/
static
RtpCodec
::
Ptr
getRtpDecoderByTrack
(
const
Track
::
Ptr
&
track
);
...
...
src/Rtp/RawEncoder.cpp
查看文件 @
f85de2e1
...
...
@@ -11,92 +11,64 @@
#if defined(ENABLE_RTPPROXY)
#include "RawEncoder.h"
#include "Extension/H264Rtp.h"
#include "Extension/AACRtp.h"
#include "Extension/H265Rtp.h"
#include "Extension/CommonRtp.h"
#include "Extension/G711Rtp.h"
#include "Extension/Factory.h"
#include "Rtsp/RtspMuxer.h"
using
namespace
toolkit
;
namespace
mediakit
{
namespace
mediakit
{
RawEncoderImp
::
RawEncoderImp
(
uint32_t
ssrc
,
uint8_t
payload_type
,
bool
sendAudio
)
:
_ssrc
(
ssrc
),
_payload_type
(
payload_type
),
_sendAudio
(
sendAudio
)
{
}
RawEncoderImp
::
RawEncoderImp
(
uint32_t
ssrc
,
uint8_t
payload_type
,
bool
send_audio
)
:
_ssrc
(
ssrc
)
,
_payload_type
(
payload_type
)
,
_send_audio
(
send_audio
)
{}
RawEncoderImp
::~
RawEncoderImp
()
{
InfoL
<<
this
<<
" "
<<
printSSRC
(
_ssrc
);
}
bool
RawEncoderImp
::
addTrack
(
const
Track
::
Ptr
&
track
){
if
(
_sendAudio
&&
track
->
getTrackType
()
==
TrackType
::
TrackAudio
&&
!
_rtp_encoder
){
// audio
bool
RawEncoderImp
::
addTrack
(
const
Track
::
Ptr
&
track
)
{
if
(
_send_audio
&&
track
->
getTrackType
()
==
TrackType
::
TrackAudio
&&
!
_rtp_encoder
)
{
// audio
_rtp_encoder
=
createRtpEncoder
(
track
);
_rtp_encoder
->
setRtpRing
(
std
::
make_shared
<
RtpRing
::
RingType
>
());
_rtp_encoder
->
getRtpRing
()
->
setDelegate
(
std
::
make_shared
<
RingDelegateHelper
>
([
this
](
RtpPacket
::
Ptr
rtp
,
bool
is_key
){
onRTP
(
std
::
move
(
rtp
),
true
);
}));
_rtp_encoder
->
getRtpRing
()
->
setDelegate
(
std
::
make_shared
<
RingDelegateHelper
>
(
[
this
](
RtpPacket
::
Ptr
rtp
,
bool
is_key
)
{
onRTP
(
std
::
move
(
rtp
),
true
);
}));
return
true
;
}
if
(
!
_sendAudio
&&
track
->
getTrackType
()
==
TrackType
::
TrackVideo
&&
!
_rtp_encoder
){
_rtp_encoder
=
createRtpEncoder
(
track
);
_rtp_encoder
->
setRtpRing
(
std
::
make_shared
<
RtpRing
::
RingType
>
());
_rtp_encoder
->
getRtpRing
()
->
setDelegate
(
std
::
make_shared
<
RingDelegateHelper
>
([
this
](
RtpPacket
::
Ptr
rtp
,
bool
is_key
){
onRTP
(
std
::
move
(
rtp
),
is_key
);
}));
if
(
!
_send_audio
&&
track
->
getTrackType
()
==
TrackType
::
TrackVideo
&&
!
_rtp_encoder
)
{
_rtp_encoder
=
createRtpEncoder
(
track
);
_rtp_encoder
->
setRtpRing
(
std
::
make_shared
<
RtpRing
::
RingType
>
());
_rtp_encoder
->
getRtpRing
()
->
setDelegate
(
std
::
make_shared
<
RingDelegateHelper
>
(
[
this
](
RtpPacket
::
Ptr
rtp
,
bool
is_key
)
{
onRTP
(
std
::
move
(
rtp
),
is_key
);
}));
return
true
;
}
return
true
;
}
void
RawEncoderImp
::
resetTracks
(){
void
RawEncoderImp
::
resetTracks
()
{
return
;
}
bool
RawEncoderImp
::
inputFrame
(
const
Frame
::
Ptr
&
frame
){
if
(
frame
->
getTrackType
()
==
TrackType
::
TrackAudio
&&
_sendAudio
&&
_rtp_encoder
){
bool
RawEncoderImp
::
inputFrame
(
const
Frame
::
Ptr
&
frame
)
{
if
(
frame
->
getTrackType
()
==
TrackType
::
TrackAudio
&&
_send_audio
&&
_rtp_encoder
)
{
_rtp_encoder
->
inputFrame
(
frame
);
}
if
(
frame
->
getTrackType
()
==
TrackType
::
TrackVideo
&&
!
_sendAudio
&&
_rtp_encoder
)
{
_rtp_encoder
->
inputFrame
(
frame
);
if
(
frame
->
getTrackType
()
==
TrackType
::
TrackVideo
&&
!
_send_audio
&&
_rtp_encoder
)
{
_rtp_encoder
->
inputFrame
(
frame
);
}
return
true
;
}
RtpCodec
::
Ptr
RawEncoderImp
::
createRtpEncoder
(
const
Track
::
Ptr
&
track
){
GET_CONFIG
(
uint32_t
,
audio_mtu
,
Rtp
::
kAudioMtuSize
);
GET_CONFIG
(
uint32_t
,
video_mtu
,
Rtp
::
kVideoMtuSize
);
auto
codec_id
=
track
->
getCodecId
();
RtpCodec
::
Ptr
RawEncoderImp
::
createRtpEncoder
(
const
Track
::
Ptr
&
track
)
{
uint32_t
sample_rate
=
90000
;
int
channels
=
1
;
auto
mtu
=
(
track
->
getTrackType
()
==
TrackVideo
?
video_mtu
:
audio_mtu
);
if
(
track
->
getTrackType
()
==
TrackType
::
TrackAudio
){
AudioTrack
::
Ptr
audioTrack
=
std
::
dynamic_pointer_cast
<
AudioTrack
>
(
track
);
sample_rate
=
audioTrack
->
getAudioSampleRate
();
channels
=
audioTrack
->
getAudioChannel
();
}
switch
(
codec_id
){
case
CodecH264
:
return
std
::
make_shared
<
H264RtpEncoder
>
(
_ssrc
,
mtu
,
sample_rate
,
_payload_type
,
0
);
case
CodecH265
:
return
std
::
make_shared
<
H265RtpEncoder
>
(
_ssrc
,
mtu
,
sample_rate
,
_payload_type
,
0
);
case
CodecAAC
:
return
std
::
make_shared
<
AACRtpEncoder
>
(
_ssrc
,
mtu
,
sample_rate
,
_payload_type
,
0
);
case
CodecL16
:
case
CodecOpus
:
return
std
::
make_shared
<
CommonRtpEncoder
>
(
codec_id
,
_ssrc
,
mtu
,
sample_rate
,
_payload_type
,
0
);
case
CodecG711A
:
case
CodecG711U
:
{
if
(
_payload_type
==
Rtsp
::
PT_PCMA
||
_payload_type
==
Rtsp
::
PT_PCMU
)
{
return
std
::
make_shared
<
G711RtpEncoder
>
(
codec_id
,
_ssrc
,
mtu
,
sample_rate
,
_payload_type
,
0
,
channels
);
}
return
std
::
make_shared
<
CommonRtpEncoder
>
(
codec_id
,
_ssrc
,
mtu
,
sample_rate
,
_payload_type
,
0
);
}
default
:
WarnL
<<
"暂不支持该CodecId:"
<<
codec_id
;
return
nullptr
;
if
(
track
->
getTrackType
()
==
TrackType
::
TrackAudio
)
{
sample_rate
=
std
::
static_pointer_cast
<
AudioTrack
>
(
track
)
->
getAudioSampleRate
();
}
return
Factory
::
getRtpEncoderByCodecId
(
track
->
getCodecId
(),
sample_rate
,
_payload_type
,
_ssrc
);
}
}
//
namespace mediakit
}
//
namespace mediakit
#endif
//
defined(ENABLE_RTPPROXY)
#endif
//
defined(ENABLE_RTPPROXY)
src/Rtp/RawEncoder.h
查看文件 @
f85de2e1
...
...
@@ -13,16 +13,15 @@
#if defined(ENABLE_RTPPROXY)
#include "Common/MediaSink.h"
#include "Common/Stamp.h"
#include "Extension/CommonRtp.h"
namespace
mediakit
{
namespace
mediakit
{
class
RawEncoderImp
:
public
MediaSinkInterface
{
class
RawEncoderImp
:
public
MediaSinkInterface
{
public
:
RawEncoderImp
(
uint32_t
ssrc
,
uint8_t
payload_type
=
96
,
bool
send
A
udio
=
true
);
RawEncoderImp
(
uint32_t
ssrc
,
uint8_t
payload_type
=
96
,
bool
send
_a
udio
=
true
);
~
RawEncoderImp
()
override
;
/**
...
...
@@ -30,7 +29,6 @@ public:
*/
bool
addTrack
(
const
Track
::
Ptr
&
track
)
override
;
/**
* 重置音视频轨道
*/
...
...
@@ -42,17 +40,20 @@ public:
bool
inputFrame
(
const
Frame
::
Ptr
&
frame
)
override
;
protected
:
//rtp打包后回调
//
rtp打包后回调
virtual
void
onRTP
(
toolkit
::
Buffer
::
Ptr
rtp
,
bool
is_key
=
false
)
=
0
;
private
:
RtpCodec
::
Ptr
createRtpEncoder
(
const
Track
::
Ptr
&
track
);
uint32_t
_ssrc
;
private
:
bool
_send_audio
;
uint8_t
_payload_type
;
bool
_sendAudio
;
uint32_t
_ssrc
;
RtpCodec
::
Ptr
_rtp_encoder
;
};
}
//
namespace mediakit
}
//
namespace mediakit
#endif //ENABLE_RTPPROXY
#endif //ZLMEDIAKIT_RAWENCODER_H
#endif //
ENABLE_RTPPROXY
#endif //
ZLMEDIAKIT_RAWENCODER_H
src/Rtp/RtpCache.h
查看文件 @
f85de2e1
...
...
@@ -50,7 +50,6 @@ protected:
void
onRTP
(
toolkit
::
Buffer
::
Ptr
rtp
,
bool
is_key
=
false
)
override
;
};
class
RtpCacheRaw
:
public
RtpCache
,
public
RawEncoderImp
{
public
:
RtpCacheRaw
(
onFlushed
cb
,
uint32_t
ssrc
,
uint8_t
payload_type
=
96
,
bool
sendAudio
=
true
)
:
RtpCache
(
std
::
move
(
cb
)),
RawEncoderImp
(
ssrc
,
payload_type
,
sendAudio
)
{};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论