diff --git a/src/projects/modules/ffmpeg/ffmpeg_conv.h b/src/projects/modules/ffmpeg/ffmpeg_conv.h index 115864a05..e8f171166 100644 --- a/src/projects/modules/ffmpeg/ffmpeg_conv.h +++ b/src/projects/modules/ffmpeg/ffmpeg_conv.h @@ -31,6 +31,7 @@ extern "C" #include #include +#include #include #include #include @@ -796,6 +797,11 @@ namespace ffmpeg } break; + case cmn::MediaType::Data: + { + codecpar->codec_id = AV_CODEC_ID_NONE; + } + break; default: return false; } @@ -808,7 +814,7 @@ namespace ffmpeg return ov::String::FormatString("%s", ::avcodec_get_name(codec_id)); } - static ov::String GetFormatByExtension(ov::String extension, ov::String default_format) + static ov::String GetFormatByExtension(ov::String extension, ov::String default_format = "mpegts") { if (extension == "mp4") { @@ -826,6 +832,23 @@ namespace ffmpeg return default_format; } + static ov::String GetFormatByProtocolType(const info::Push::ProtocolType protocol_type, ov::String default_format = "flv") + { + switch (protocol_type) + { + case info::Push::ProtocolType::RTMP: + return "flv"; + case info::Push::ProtocolType::MPEGTS: + return "mpegts"; + case info::Push::ProtocolType::SRT: + return "mpegts"; + default: + break; + } + + return default_format; + } + static bool IsSupportCodec(ov::String format, cmn::MediaCodecId codec_id) { if (format == "mp4") diff --git a/src/projects/modules/ffmpeg/ffmpeg_writer.cpp b/src/projects/modules/ffmpeg/ffmpeg_writer.cpp index 6c3953850..b58ebbd8f 100644 --- a/src/projects/modules/ffmpeg/ffmpeg_writer.cpp +++ b/src/projects/modules/ffmpeg/ffmpeg_writer.cpp @@ -115,6 +115,7 @@ namespace ffmpeg bool Writer::AddTrack(const std::shared_ptr &media_track) { + // TODO: This code will move to mediarouter. if (media_track->GetCodecId() == cmn::MediaCodecId::Opus && media_track->GetDecoderConfigurationRecord() == nullptr) { @@ -276,6 +277,7 @@ namespace ffmpeg switch (packet->GetBitstreamFormat()) { case cmn::BitstreamFormat::H264_AVCC: + // Do nothing break; case cmn::BitstreamFormat::H264_ANNEXB: { new_data = NalStreamConverter::ConvertAnnexbToXvcc(packet->GetData(), packet->GetFragHeader()); @@ -301,6 +303,9 @@ namespace ffmpeg av_packet.data = (uint8_t *)new_data->GetDataAs(); } break; + case cmn::BitstreamFormat::AMF: + // Do nothing + break; default: // Unsupported bitstream foramt return false; diff --git a/src/projects/publishers/push/push_session.cpp b/src/projects/publishers/push/push_session.cpp index 8b1822fa8..14b764e53 100755 --- a/src/projects/publishers/push/push_session.cpp +++ b/src/projects/publishers/push/push_session.cpp @@ -56,16 +56,16 @@ namespace pub GetPush()->UpdatePushStartTime(); GetPush()->SetState(info::Push::PushState::Connecting); - ov::String rtmp_url; + ov::String dest_url; if (GetPush()->GetStreamKey().IsEmpty()) { - rtmp_url = GetPush()->GetUrl(); + dest_url = GetPush()->GetUrl(); } else { - rtmp_url = ov::String::FormatString("%s/%s", GetPush()->GetUrl().CStr(), GetPush()->GetStreamKey().CStr()); + dest_url = ov::String::FormatString("%s/%s", GetPush()->GetUrl().CStr(), GetPush()->GetStreamKey().CStr()); } - + auto writer = CreateWriter(); if (writer == nullptr) { @@ -75,8 +75,7 @@ namespace pub return false; } - ov::String format = (GetPush()->GetProtocolType() == info::Push::ProtocolType::RTMP) ? "flv" : "mpegts"; - if (writer->SetUrl(rtmp_url, format) == false) + if (writer->SetUrl(dest_url, ffmpeg::Conv::GetFormatByProtocolType(GetPush()->GetProtocolType())) == false) { SetState(SessionState::Error); GetPush()->SetState(info::Push::PushState::Error); @@ -86,8 +85,9 @@ namespace pub for (auto &[track_id, track] : GetStream()->GetTracks()) { - if (track->GetMediaType() != cmn::MediaType::Video && track->GetMediaType() != cmn::MediaType::Audio) + if (IsSupportTrack(track) == false) { + logtw("Could not supported track. track_id:%d, codec_id: %d", track->GetId(), track->GetCodecId()); continue; } @@ -98,14 +98,11 @@ namespace pub continue; } - // RTMP protocol does not supported except for H264 and AAC codec. - if (GetPush()->GetProtocolType() == info::Push::ProtocolType::RTMP) + if (IsSupportCodec(GetPush()->GetProtocolType(), track->GetCodecId()) == false) { - if (!(track->GetCodecId() == cmn::MediaCodecId::H264 || track->GetCodecId() == cmn::MediaCodecId::Aac)) - { - logtw("Could not supported codec. track_id:%d, codec_id: %d", track->GetId(), track->GetCodecId()); - continue; - } + logtw("Could not supported codec. track_id:%d, codec_id: %d", track->GetId(), track->GetCodecId()); + + continue; } bool ret = writer->AddTrack(track); @@ -131,23 +128,6 @@ namespace pub return Session::Start(); } - bool PushSession::IsSelectedTrack(const std::shared_ptr &track) - { - auto selected_track_ids = GetPush()->GetTrackIds(); - auto selected_track_names = GetPush()->GetVariantNames(); - - if (selected_track_ids.size() > 0 || selected_track_names.size() > 0) - { - if ((find(selected_track_ids.begin(), selected_track_ids.end(), track->GetId()) == selected_track_ids.end()) && - (find(selected_track_names.begin(), selected_track_names.end(), track->GetVariantName()) == selected_track_names.end())) - { - return false; - } - } - - return true; - } - bool PushSession::Stop() { auto writer = GetWriter(); @@ -167,9 +147,9 @@ namespace pub GetPush()->SetState(info::Push::PushState::Stopped); GetPush()->IncreaseSequence(); } - - std::unique_lock lock(_writer_mutex); - _writer = nullptr; + + DestoryWriter(); + logtd("PushSession(%d) has stopped", GetId()); } @@ -251,9 +231,78 @@ namespace pub return _writer; } + void PushSession::DestoryWriter() + { + std::lock_guard lock(_writer_mutex); + if (_writer != nullptr) + { + _writer->Stop(); + _writer = nullptr; + } + } + std::shared_ptr PushSession::GetPush() { std::shared_lock lock(_push_mutex); return _push; } + + bool PushSession::IsSelectedTrack(const std::shared_ptr &track) + { + auto selected_track_ids = GetPush()->GetTrackIds(); + auto selected_track_names = GetPush()->GetVariantNames(); + + if (selected_track_ids.size() > 0 || selected_track_names.size() > 0) + { + if ((find(selected_track_ids.begin(), selected_track_ids.end(), track->GetId()) == selected_track_ids.end()) && + (find(selected_track_names.begin(), selected_track_names.end(), track->GetVariantName()) == selected_track_names.end())) + { + return false; + } + } + + return true; + } + + bool PushSession::IsSupportTrack(const std::shared_ptr &track) + { + // If the track is not video, audio, or data, ignore it. + if (track->GetMediaType() == cmn::MediaType::Video || + track->GetMediaType() == cmn::MediaType::Audio || + track->GetMediaType() == cmn::MediaType::Data) + { + return true; + } + + return false; + } + + bool PushSession::IsSupportCodec(const info::Push::ProtocolType protocol_type, cmn::MediaCodecId codec_id) + { + // RTMP protocol does not supported except for H264 and AAC codec. + if (protocol_type == info::Push::ProtocolType::RTMP) + { + if (codec_id == cmn::MediaCodecId::H264 || + codec_id == cmn::MediaCodecId::Aac || + codec_id == cmn::MediaCodecId::None) + { + return true; + } + } + else if (protocol_type == info::Push::ProtocolType::SRT || protocol_type == info::Push::ProtocolType::MPEGTS) + { + if (codec_id == cmn::MediaCodecId::H264 || + codec_id == cmn::MediaCodecId::H265 || + codec_id == cmn::MediaCodecId::Vp8 || + codec_id == cmn::MediaCodecId::Vp9 || + codec_id == cmn::MediaCodecId::Aac || + codec_id == cmn::MediaCodecId::Mp3 || + codec_id == cmn::MediaCodecId::Opus) + { + return true; + } + } + + return false; + } } // namespace pub \ No newline at end of file diff --git a/src/projects/publishers/push/push_session.h b/src/projects/publishers/push/push_session.h index 1648e2005..27a55fd02 100755 --- a/src/projects/publishers/push/push_session.h +++ b/src/projects/publishers/push/push_session.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "base/info/push.h" @@ -41,8 +42,11 @@ namespace pub private: std::shared_ptr CreateWriter(); + void DestoryWriter(); bool IsSelectedTrack(const std::shared_ptr &track); + bool IsSupportTrack(const std::shared_ptr &track); + bool IsSupportCodec(const info::Push::ProtocolType protocol_type, cmn::MediaCodecId codec_id); std::shared_ptr _push = nullptr; std::shared_mutex _push_mutex;