UnityVideoDecoderFactory.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "pch.h"
  2. #include <media/engine/internal_decoder_factory.h>
  3. #include <modules/video_coding/include/video_error_codes.h>
  4. #include "Codec/CreateVideoCodecFactory.h"
  5. #include "GraphicsDevice/GraphicsUtility.h"
  6. #include "ProfilerMarkerFactory.h"
  7. #include "ScopedProfiler.h"
  8. #include "UnityVideoDecoderFactory.h"
  9. namespace unity
  10. {
  11. namespace webrtc
  12. {
  13. class UnityVideoDecoder : public VideoDecoder
  14. {
  15. public:
  16. UnityVideoDecoder(std::unique_ptr<VideoDecoder> decoder, ProfilerMarkerFactory* profiler)
  17. : decoder_(std::move(decoder))
  18. , profiler_(profiler)
  19. , marker_(nullptr)
  20. , profilerThread_(nullptr)
  21. {
  22. if (profiler)
  23. marker_ = profiler->CreateMarker(
  24. "UnityVideoDecoder.Decode", kUnityProfilerCategoryOther, kUnityProfilerMarkerFlagDefault, 0);
  25. }
  26. ~UnityVideoDecoder() override { }
  27. int32_t InitDecode(const VideoCodec* codec_settings, int32_t number_of_cores) override
  28. {
  29. int32_t result = decoder_->InitDecode(codec_settings, number_of_cores);
  30. if (result >= WEBRTC_VIDEO_CODEC_OK && !profilerThread_)
  31. {
  32. std::stringstream ss;
  33. ss << "Decoder ";
  34. ss
  35. << (decoder_->GetDecoderInfo().implementation_name.empty()
  36. ? "VideoDecoder"
  37. : decoder_->GetDecoderInfo().implementation_name);
  38. ss << "(" << CodecTypeToPayloadString(codec_settings->codecType) << ")";
  39. profilerThread_ = profiler_->CreateScopedProfilerThread("WebRTC", ss.str().c_str());
  40. }
  41. return result;
  42. }
  43. int32_t Decode(const EncodedImage& input_image, bool missing_frames, int64_t render_time_ms) override
  44. {
  45. int32_t result;
  46. {
  47. std::unique_ptr<const ScopedProfiler> profiler;
  48. if (profiler_)
  49. profiler = profiler_->CreateScopedProfiler(*marker_);
  50. result = decoder_->Decode(input_image, missing_frames, render_time_ms);
  51. }
  52. return result;
  53. }
  54. int32_t RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override
  55. {
  56. return decoder_->RegisterDecodeCompleteCallback(callback);
  57. }
  58. int32_t Release() override { return decoder_->Release(); }
  59. DecoderInfo GetDecoderInfo() const override { return decoder_->GetDecoderInfo(); }
  60. private:
  61. std::unique_ptr<VideoDecoder> decoder_;
  62. ProfilerMarkerFactory* profiler_;
  63. const UnityProfilerMarkerDesc* marker_;
  64. std::unique_ptr<const ScopedProfilerThread> profilerThread_;
  65. };
  66. UnityVideoDecoderFactory::UnityVideoDecoderFactory(IGraphicsDevice* gfxDevice, ProfilerMarkerFactory* profiler)
  67. : profiler_(profiler)
  68. , factories_()
  69. {
  70. const std::vector<std::string> arrayImpl = {
  71. kInternalImpl, kNvCodecImpl, kAndroidMediaCodecImpl, kVideoToolboxImpl
  72. };
  73. for (auto impl : arrayImpl)
  74. {
  75. auto factory = CreateVideoDecoderFactory(impl, gfxDevice, profiler);
  76. if (factory)
  77. factories_.emplace(impl, factory);
  78. }
  79. }
  80. UnityVideoDecoderFactory::~UnityVideoDecoderFactory() = default;
  81. std::vector<webrtc::SdpVideoFormat> UnityVideoDecoderFactory::GetSupportedFormats() const
  82. {
  83. return GetSupportedFormatsInFactories(factories_);
  84. }
  85. std::unique_ptr<webrtc::VideoDecoder>
  86. UnityVideoDecoderFactory::CreateVideoDecoder(const webrtc::SdpVideoFormat& format)
  87. {
  88. VideoDecoderFactory* factory = FindCodecFactory(factories_, format);
  89. auto decoder = factory->CreateVideoDecoder(format);
  90. if (!profiler_)
  91. return decoder;
  92. // Use Unity Profiler for measuring decoding process.
  93. return std::make_unique<UnityVideoDecoder>(std::move(decoder), profiler_);
  94. }
  95. } // namespace webrtc
  96. } // namespace unity