From 3ad1fe4924f2d936d58cb36a499b88582e807aac Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 30 Apr 2020 12:52:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81265=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/{H264Decoder.h => FFMpegDecoder.h} | 23 +++++++++++++++++------ tests/test_player.cpp | 20 ++++++++------------ 2 files changed, 25 insertions(+), 18 deletions(-) rename tests/{H264Decoder.h => FFMpegDecoder.h} (78%) diff --git a/tests/H264Decoder.h b/tests/FFMpegDecoder.h similarity index 78% rename from tests/H264Decoder.h rename to tests/FFMpegDecoder.h index 31dd8229..465a87de 100644 --- a/tests/H264Decoder.h +++ b/tests/FFMpegDecoder.h @@ -13,6 +13,7 @@ #include #include #include +#include "Extension/Frame.h" #ifdef __cplusplus extern "C" { #endif @@ -27,14 +28,24 @@ using namespace std; namespace mediakit { -class H264Decoder -{ +class FFMpegDecoder{ public: - H264Decoder(void){ + FFMpegDecoder(int codec_id){ + auto ff_codec_id = AV_CODEC_ID_H264; + switch (codec_id){ + case CodecH264: + ff_codec_id = AV_CODEC_ID_H264; + break; + case CodecH265: + ff_codec_id = AV_CODEC_ID_H265; + break; + default: + throw std::invalid_argument("不支持该编码格式"); + } avcodec_register_all(); - AVCodec *pCodec = avcodec_find_decoder(AV_CODEC_ID_H264); + AVCodec *pCodec = avcodec_find_decoder(ff_codec_id); if (!pCodec) { - throw std::runtime_error("未找到H264解码器"); + throw std::runtime_error("未找到解码器"); } m_pContext.reset(avcodec_alloc_context3(pCodec), [](AVCodecContext *pCtx) { avcodec_close(pCtx); @@ -57,7 +68,7 @@ public: throw std::runtime_error("创建帧缓存失败"); } } - virtual ~H264Decoder(void){} + virtual ~FFMpegDecoder(void){} bool inputVideo(unsigned char* data,unsigned int dataSize,uint32_t ui32Stamp,AVFrame **ppFrame){ AVPacket pkt; av_init_packet(&pkt); diff --git a/tests/test_player.cpp b/tests/test_player.cpp index ed15f8b5..2954a43c 100644 --- a/tests/test_player.cpp +++ b/tests/test_player.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. * * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). @@ -12,13 +12,12 @@ #include "Util/util.h" #include "Util/logger.h" #include -#include "Poller/EventPoller.h" #include "Rtsp/UDPServer.h" #include "Player/MediaPlayer.h" #include "Util/onceToken.h" -#include "H264Decoder.h" +#include "FFMpegDecoder.h" #include "YuvDisplayer.h" -#include "Network/sockutil.h" +#include "Extension/H265.h" using namespace std; using namespace toolkit; @@ -111,8 +110,8 @@ int main(int argc, char *argv[]) { } auto viedoTrack = strongPlayer->getTrack(TrackVideo); - if (!viedoTrack || viedoTrack->getCodecId() != CodecH264) { - WarnL << "没有视频或者视频不是264编码!"; + if (!viedoTrack) { + WarnL << "没有视频!"; return; } @@ -123,24 +122,21 @@ int main(int argc, char *argv[]) { auto &displayer = (*storage)["displayer"]; auto &merger = (*storage)["merger"]; if(!decoder){ - decoder.set(); + decoder.set(frame->getCodecId()); } if(!displayer){ displayer.set(nullptr,url); } if(!merger){ merger.set(); - }; - + } merger.get().inputFrame(frame,[&](uint32_t dts,uint32_t pts,const Buffer::Ptr &buffer){ AVFrame *pFrame = nullptr; - bool flag = decoder.get().inputVideo((unsigned char *) buffer->data(), buffer->size(), dts, &pFrame); + bool flag = decoder.get().inputVideo((unsigned char *) buffer->data(), buffer->size(), dts, &pFrame); if (flag) { displayer.get().displayYUV(pFrame); } }); - - return true; }); }));