mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-28 05:29:24 +08:00
优化与完善rtmp协议相关代码
rtmp相关常量由宏改为枚举 明确rtmp包一些字段赋值含义
This commit is contained in:
@@ -10,10 +10,10 @@
|
||||
|
||||
#include "Rtmp.h"
|
||||
#include "Extension/Factory.h"
|
||||
namespace mediakit{
|
||||
|
||||
TitleMeta::TitleMeta(float dur_sec, size_t fileSize, const std::map<std::string, std::string> &header)
|
||||
{
|
||||
namespace mediakit {
|
||||
|
||||
TitleMeta::TitleMeta(float dur_sec, size_t fileSize, const std::map<std::string, std::string> &header) {
|
||||
_metadata.set("duration", dur_sec);
|
||||
_metadata.set("fileSize", (int)fileSize);
|
||||
_metadata.set("title", std::string("Streamed by ") + kServerName);
|
||||
@@ -22,14 +22,14 @@ TitleMeta::TitleMeta(float dur_sec, size_t fileSize, const std::map<std::string,
|
||||
}
|
||||
}
|
||||
|
||||
VideoMeta::VideoMeta(const VideoTrack::Ptr &video){
|
||||
if(video->getVideoWidth() > 0 ){
|
||||
VideoMeta::VideoMeta(const VideoTrack::Ptr &video) {
|
||||
if (video->getVideoWidth() > 0) {
|
||||
_metadata.set("width", video->getVideoWidth());
|
||||
}
|
||||
if(video->getVideoHeight() > 0 ){
|
||||
if (video->getVideoHeight() > 0) {
|
||||
_metadata.set("height", video->getVideoHeight());
|
||||
}
|
||||
if(video->getVideoFps() > 0 ){
|
||||
if (video->getVideoFps() > 0) {
|
||||
_metadata.set("framerate", video->getVideoFps());
|
||||
}
|
||||
if (video->getBitRate()) {
|
||||
@@ -39,26 +39,26 @@ VideoMeta::VideoMeta(const VideoTrack::Ptr &video){
|
||||
_metadata.set("videocodecid", Factory::getAmfByCodecId(_codecId));
|
||||
}
|
||||
|
||||
AudioMeta::AudioMeta(const AudioTrack::Ptr &audio){
|
||||
AudioMeta::AudioMeta(const AudioTrack::Ptr &audio) {
|
||||
if (audio->getBitRate()) {
|
||||
_metadata.set("audiodatarate", audio->getBitRate() / 1024);
|
||||
}
|
||||
if(audio->getAudioSampleRate() > 0){
|
||||
if (audio->getAudioSampleRate() > 0) {
|
||||
_metadata.set("audiosamplerate", audio->getAudioSampleRate());
|
||||
}
|
||||
if(audio->getAudioSampleBit() > 0){
|
||||
if (audio->getAudioSampleBit() > 0) {
|
||||
_metadata.set("audiosamplesize", audio->getAudioSampleBit());
|
||||
}
|
||||
if(audio->getAudioChannel() > 0){
|
||||
if (audio->getAudioChannel() > 0) {
|
||||
_metadata.set("stereo", audio->getAudioChannel() > 1);
|
||||
}
|
||||
_codecId = audio->getCodecId();
|
||||
_metadata.set("audiocodecid", Factory::getAmfByCodecId(_codecId));
|
||||
}
|
||||
|
||||
uint8_t getAudioRtmpFlags(const Track::Ptr &track){
|
||||
switch (track->getTrackType()){
|
||||
case TrackAudio : {
|
||||
uint8_t getAudioRtmpFlags(const Track::Ptr &track) {
|
||||
switch (track->getTrackType()) {
|
||||
case TrackAudio: {
|
||||
auto audioTrack = std::dynamic_pointer_cast<AudioTrack>(track);
|
||||
if (!audioTrack) {
|
||||
WarnL << "获取AudioTrack失败";
|
||||
@@ -68,21 +68,21 @@ uint8_t getAudioRtmpFlags(const Track::Ptr &track){
|
||||
auto iChannel = audioTrack->getAudioChannel();
|
||||
auto iSampleBit = audioTrack->getAudioSampleBit();
|
||||
|
||||
uint8_t flvAudioType ;
|
||||
switch (track->getCodecId()){
|
||||
case CodecG711A : flvAudioType = FLV_CODEC_G711A; break;
|
||||
case CodecG711U : flvAudioType = FLV_CODEC_G711U; break;
|
||||
case CodecOpus : {
|
||||
flvAudioType = FLV_CODEC_OPUS;
|
||||
//opus不通过flags获取音频相关信息
|
||||
uint8_t flvAudioType;
|
||||
switch (track->getCodecId()) {
|
||||
case CodecG711A: flvAudioType = (uint8_t)RtmpAudioCodec::g711a; break;
|
||||
case CodecG711U: flvAudioType = (uint8_t)RtmpAudioCodec::g711u; break;
|
||||
case CodecOpus: {
|
||||
flvAudioType = (uint8_t)RtmpAudioCodec::opus;
|
||||
// opus不通过flags获取音频相关信息
|
||||
iSampleRate = 44100;
|
||||
iSampleBit = 16;
|
||||
iChannel = 2;
|
||||
break;
|
||||
}
|
||||
case CodecAAC : {
|
||||
flvAudioType = FLV_CODEC_AAC;
|
||||
//aac不通过flags获取音频相关信息
|
||||
case CodecAAC: {
|
||||
flvAudioType = (uint8_t)RtmpAudioCodec::aac;
|
||||
// aac不通过flags获取音频相关信息
|
||||
iSampleRate = 44100;
|
||||
iSampleBit = 16;
|
||||
iChannel = 2;
|
||||
@@ -93,23 +93,15 @@ uint8_t getAudioRtmpFlags(const Track::Ptr &track){
|
||||
|
||||
uint8_t flvSampleRate;
|
||||
switch (iSampleRate) {
|
||||
case 44100:
|
||||
flvSampleRate = 3;
|
||||
break;
|
||||
case 22050:
|
||||
flvSampleRate = 2;
|
||||
break;
|
||||
case 11025:
|
||||
flvSampleRate = 1;
|
||||
break;
|
||||
case 44100: flvSampleRate = 3; break;
|
||||
case 22050: flvSampleRate = 2; break;
|
||||
case 11025: flvSampleRate = 1; break;
|
||||
case 16000: // nellymoser only
|
||||
case 8000: // nellymoser only
|
||||
case 5512: // not MP3
|
||||
flvSampleRate = 0;
|
||||
break;
|
||||
default:
|
||||
WarnL << "FLV does not support sample rate " << iSampleRate << " ,choose from (44100, 22050, 11025)";
|
||||
return 0;
|
||||
default: WarnL << "FLV does not support sample rate " << iSampleRate << " ,choose from (44100, 22050, 11025)"; return 0;
|
||||
}
|
||||
|
||||
uint8_t flvStereoOrMono = (iChannel > 1);
|
||||
@@ -117,32 +109,28 @@ uint8_t getAudioRtmpFlags(const Track::Ptr &track){
|
||||
return (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
|
||||
}
|
||||
|
||||
default : return 0;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Metadata::addTrack(AMFValue &metadata, const Track::Ptr &track) {
|
||||
Metadata::Ptr new_metadata;
|
||||
switch (track->getTrackType()) {
|
||||
case TrackVideo: {
|
||||
new_metadata = std::make_shared<VideoMeta>(std::dynamic_pointer_cast<VideoTrack>(track));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TrackAudio: {
|
||||
new_metadata = std::make_shared<AudioMeta>(std::dynamic_pointer_cast<AudioTrack>(track));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
|
||||
new_metadata->getMetadata().object_for_each([&](const std::string &key, const AMFValue &value) {
|
||||
metadata.set(key, value);
|
||||
});
|
||||
new_metadata->getMetadata().object_for_each([&](const std::string &key, const AMFValue &value) { metadata.set(key, value); });
|
||||
}
|
||||
|
||||
RtmpPacket::Ptr RtmpPacket::create(){
|
||||
RtmpPacket::Ptr RtmpPacket::create() {
|
||||
#if 0
|
||||
static ResourcePool<RtmpPacket> packet_pool;
|
||||
static onceToken token([]() {
|
||||
@@ -156,8 +144,7 @@ RtmpPacket::Ptr RtmpPacket::create(){
|
||||
#endif
|
||||
}
|
||||
|
||||
void RtmpPacket::clear()
|
||||
{
|
||||
void RtmpPacket::clear() {
|
||||
is_abs_stamp = false;
|
||||
time_stamp = 0;
|
||||
ts_field = 0;
|
||||
@@ -165,36 +152,42 @@ void RtmpPacket::clear()
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
bool RtmpPacket::isVideoKeyFrame() const
|
||||
{
|
||||
return type_id == MSG_VIDEO && (uint8_t)buffer[0] >> 4 == FLV_KEY_FRAME && (uint8_t)buffer[1] == 1;
|
||||
bool RtmpPacket::isVideoKeyFrame() const {
|
||||
if (type_id != MSG_VIDEO) {
|
||||
return false;
|
||||
}
|
||||
RtmpPacketInfo info;
|
||||
if (CodecInvalid == parseVideoRtmpPacket((uint8_t *)data(), size(), &info)) {
|
||||
return false;
|
||||
}
|
||||
if (info.is_enhanced) {
|
||||
return info.video.frame_type == RtmpFrameType::key_frame && info.video.pkt_type == RtmpPacketType::PacketTypeCodedFramesX;
|
||||
}
|
||||
return info.video.frame_type == RtmpFrameType::key_frame && info.video.h264_pkt_type == RtmpH264PacketType::h264_nalu;
|
||||
}
|
||||
|
||||
bool RtmpPacket::isCfgFrame() const
|
||||
{
|
||||
bool RtmpPacket::isCfgFrame() const {
|
||||
switch (type_id) {
|
||||
case MSG_VIDEO: return buffer[1] == 0;
|
||||
case MSG_AUDIO: {
|
||||
switch (getMediaType()) {
|
||||
case FLV_CODEC_AAC: return buffer[1] == 0;
|
||||
default: return false;
|
||||
case MSG_VIDEO: return (RtmpH264PacketType)buffer[1] == RtmpH264PacketType::h264_config_header;
|
||||
case MSG_AUDIO: {
|
||||
switch ((RtmpAudioCodec)getRtmpCodecId()) {
|
||||
case RtmpAudioCodec::aac: return (RtmpAACPacketType)buffer[1] == RtmpAACPacketType::aac_config_header;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
default: return false;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
int RtmpPacket::getMediaType() const
|
||||
{
|
||||
int RtmpPacket::getRtmpCodecId() const {
|
||||
switch (type_id) {
|
||||
case MSG_VIDEO: return (uint8_t)buffer[0] & 0x0F;
|
||||
case MSG_AUDIO: return (uint8_t)buffer[0] >> 4;
|
||||
default: return 0;
|
||||
case MSG_VIDEO: return (uint8_t)buffer[0] & 0x0F;
|
||||
case MSG_AUDIO: return (uint8_t)buffer[0] >> 4;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int RtmpPacket::getAudioSampleRate() const
|
||||
{
|
||||
int RtmpPacket::getAudioSampleRate() const {
|
||||
if (type_id != MSG_AUDIO) {
|
||||
return 0;
|
||||
}
|
||||
@@ -203,8 +196,7 @@ int RtmpPacket::getAudioSampleRate() const
|
||||
return sampleRate[flvSampleRate];
|
||||
}
|
||||
|
||||
int RtmpPacket::getAudioSampleBit() const
|
||||
{
|
||||
int RtmpPacket::getAudioSampleBit() const {
|
||||
if (type_id != MSG_AUDIO) {
|
||||
return 0;
|
||||
}
|
||||
@@ -213,8 +205,7 @@ int RtmpPacket::getAudioSampleBit() const
|
||||
return sampleBit[flvSampleBit];
|
||||
}
|
||||
|
||||
int RtmpPacket::getAudioChannel() const
|
||||
{
|
||||
int RtmpPacket::getAudioChannel() const {
|
||||
if (type_id != MSG_AUDIO) {
|
||||
return 0;
|
||||
}
|
||||
@@ -223,8 +214,7 @@ int RtmpPacket::getAudioChannel() const
|
||||
return channel[flvStereoOrMono];
|
||||
}
|
||||
|
||||
RtmpPacket & RtmpPacket::operator=(const RtmpPacket &that)
|
||||
{
|
||||
RtmpPacket &RtmpPacket::operator=(const RtmpPacket &that) {
|
||||
is_abs_stamp = that.is_abs_stamp;
|
||||
stream_index = that.stream_index;
|
||||
body_size = that.body_size;
|
||||
@@ -234,25 +224,20 @@ RtmpPacket & RtmpPacket::operator=(const RtmpPacket &that)
|
||||
return *this;
|
||||
}
|
||||
|
||||
RtmpHandshake::RtmpHandshake(uint32_t _time, uint8_t *_random /*= nullptr*/)
|
||||
{
|
||||
RtmpHandshake::RtmpHandshake(uint32_t _time, uint8_t *_random /*= nullptr*/) {
|
||||
_time = htonl(_time);
|
||||
memcpy(time_stamp, &_time, 4);
|
||||
if (!_random) {
|
||||
random_generate((char *)random, sizeof(random));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
memcpy(random, _random, sizeof(random));
|
||||
}
|
||||
}
|
||||
|
||||
void RtmpHandshake::random_generate(char *bytes, int size)
|
||||
{
|
||||
static char cdata[] = { 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x2d, 0x72,
|
||||
0x74, 0x6d, 0x70, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||
0x2d, 0x77, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x2d, 0x77, 0x69,
|
||||
0x6e, 0x74, 0x65, 0x72, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
|
||||
0x40, 0x31, 0x32, 0x36, 0x2e, 0x63, 0x6f, 0x6d };
|
||||
void RtmpHandshake::random_generate(char *bytes, int size) {
|
||||
static char cdata[] = { 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x2d, 0x72, 0x74, 0x6d, 0x70, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2d, 0x77, 0x69, 0x6e,
|
||||
0x6c, 0x69, 0x6e, 0x2d, 0x77, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x40, 0x31, 0x32, 0x36, 0x2e, 0x63,
|
||||
0x6f, 0x6d };
|
||||
for (int i = 0; i < size; i++) {
|
||||
bytes[i] = cdata[rand() % (sizeof(cdata) - 1)];
|
||||
}
|
||||
@@ -286,9 +271,9 @@ CodecId parseVideoRtmpPacket(const uint8_t *data, size_t size, RtmpPacketInfo *i
|
||||
// IsExHeader == 0
|
||||
info->is_enhanced = false;
|
||||
info->video.frame_type = (RtmpFrameType)(data[0] >> 4);
|
||||
info->video.rtmp_codec = (RtmpVideoCodec)(data[0] & 0x0f);
|
||||
auto rtmp_codec = (RtmpVideoCodec)(data[0] & 0x0f);
|
||||
|
||||
switch (info->video.rtmp_codec) {
|
||||
switch (rtmp_codec) {
|
||||
case RtmpVideoCodec::h264: {
|
||||
CHECK(size >= 1, "Invalid rtmp buffer size: ", size);
|
||||
info->codec = CodecH264;
|
||||
@@ -301,14 +286,14 @@ CodecId parseVideoRtmpPacket(const uint8_t *data, size_t size, RtmpPacketInfo *i
|
||||
info->video.h264_pkt_type = (RtmpH264PacketType)data[1];
|
||||
break;
|
||||
}
|
||||
default: WarnL << "Rtmp video codec not supported: " << (int)info->video.rtmp_codec; break;
|
||||
default: WarnL << "Rtmp video codec not supported: " << (int)rtmp_codec; break;
|
||||
}
|
||||
}
|
||||
return info->codec;
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
} // namespace mediakit
|
||||
|
||||
namespace toolkit {
|
||||
StatisticImp(mediakit::RtmpPacket);
|
||||
StatisticImp(mediakit::RtmpPacket);
|
||||
}
|
||||
Reference in New Issue
Block a user