diff --git a/inc/VideoDecoder.h b/inc/VideoDecoder.h index cbe8545d..b9c099e9 100644 --- a/inc/VideoDecoder.h +++ b/inc/VideoDecoder.h @@ -20,14 +20,6 @@ #include "SessionSink.h" #include "VideoScaler.h" -#define FOURCC(a, b, c, d) \ - ((static_cast(a)) | (static_cast(b) << 8) | \ - (static_cast(c) << 16) | (static_cast(d) << 24)) - -const uint32_t FOURCC_VP9 = FOURCC('V','P','9', 0); -const uint32_t FOURCC_H265 = FOURCC('H','2','6','5'); -#undef FOURCC - class VideoDecoder : public rtc::VideoSourceInterface, public webrtc::DecodedImageCallback { private: class Frame @@ -35,12 +27,14 @@ class VideoDecoder : public rtc::VideoSourceInterface, publi public: Frame(): m_timestamp_ms(0) {} Frame(const rtc::scoped_refptr & content, uint64_t timestamp_ms, webrtc::VideoFrameType frameType) : m_content(content), m_timestamp_ms(timestamp_ms), m_frameType(frameType) {} - Frame(const cricket::VideoFormat & format) : m_format(format) {} + Frame(const std::string & format, int width, int height) : m_format(format), m_width(width), m_height(height) {} rtc::scoped_refptr m_content; - uint64_t m_timestamp_ms; - webrtc::VideoFrameType m_frameType; - cricket::VideoFormat m_format; + uint64_t m_timestamp_ms; + webrtc::VideoFrameType m_frameType; + std::string m_format; + int m_width; + int m_height; }; public: @@ -134,8 +128,8 @@ class VideoDecoder : public rtc::VideoSourceInterface, publi return frames; } - void postFormat(const cricket::VideoFormat & format) { - Frame frame(format); + void postFormat(const std::string & format, int width, int height) { + Frame frame(format, width, height); { std::unique_lock lock(m_queuemutex); m_queue.push(frame); @@ -195,17 +189,17 @@ class VideoDecoder : public rtc::VideoSourceInterface, publi return (m_decoder.get() != NULL); } - void createDecoder(const cricket::VideoFormat & format) { + void createDecoder(const std::string & format, int width, int height) { webrtc::VideoDecoder::Settings settings; - webrtc::RenderResolution resolution(format.width, format.height); + webrtc::RenderResolution resolution(width, height); settings.set_max_render_resolution(resolution); - if (format.fourcc == cricket::FOURCC_H264) { + if (format == "H264") { m_decoder=m_factory->CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kH264CodecName)); settings.set_codec_type(webrtc::VideoCodecType::kVideoCodecH264); - } else if (format.fourcc == FOURCC_H265) { - m_decoder=m_factory->CreateVideoDecoder(webrtc::SdpVideoFormat("H265")); + } else if (format == "H265") { + m_decoder=m_factory->CreateVideoDecoder(webrtc::SdpVideoFormat(format)); settings.set_codec_type(webrtc::VideoCodecType::kVideoCodecGeneric); - } else if (format.fourcc == FOURCC_VP9) { + } else if (format == "VP9") { m_decoder=m_factory->CreateVideoDecoder(webrtc::SdpVideoFormat(cricket::kVp9CodecName)); settings.set_codec_type(webrtc::VideoCodecType::kVideoCodecVP9); } @@ -232,21 +226,22 @@ class VideoDecoder : public rtc::VideoSourceInterface, publi while (!m_stop) { Frame frame = this->getFrame(); - if (frame.m_format.fourcc != 0) { - cricket::VideoFormat & format = frame.m_format; + if (!frame.m_format.empty()) { if (this->hasDecoder()) { - if ((m_format.width != format.width) || (m_format.height != format.height)) { - RTC_LOG(LS_INFO) << "format changed => set format from " << m_format.ToString() << " to " << format.ToString(); + if ((m_format != frame.m_format) || (m_width != frame.m_width) || (m_height != frame.m_height)) { + RTC_LOG(LS_INFO) << "format changed => set format from " << m_format << " " << m_width << "x" << m_height << " to " << frame.m_format << " " << frame.m_width << "x" << frame.m_height; m_decoder.reset(NULL); } } if (!this->hasDecoder()) { - RTC_LOG(LS_INFO) << "VideoDecoder:DecoderThread set format:" << format.ToString(); - m_format = format; + RTC_LOG(LS_INFO) << "VideoDecoder:DecoderThread set format:" << frame.m_format << " " << frame.m_width << "x" << frame.m_height; + m_format = frame.m_format; + m_width = frame.m_width; + m_height = frame.m_height; - this->createDecoder(format); + this->createDecoder(frame.m_format, frame.m_width, frame.m_height); } } @@ -300,7 +295,10 @@ class VideoDecoder : public rtc::VideoSourceInterface, publi protected: VideoScaler m_scaler; std::unique_ptr& m_factory; - cricket::VideoFormat m_format; + + std::string m_format; + int m_width; + int m_height; std::queue m_queue; std::mutex m_queuemutex; diff --git a/inc/livevideosource.h b/inc/livevideosource.h index 07398de5..556c8648 100644 --- a/inc/livevideosource.h +++ b/inc/livevideosource.h @@ -132,8 +132,7 @@ class LiveVideoSource : public VideoSourceWithDecoder, public T::Callback { int fps = 25; RTC_LOG(LS_INFO) << "LiveVideoSource:onData SPS set format " << sps->width << "x" << sps->height << " fps:" << fps; - cricket::VideoFormat videoFormat(sps->width, sps->height, cricket::VideoFormat::FpsToInterval(fps), cricket::FOURCC_H264); - m_decoder.postFormat(videoFormat); + m_decoder.postFormat(codec, sps->width, sps->height); } } else if (nalu_type == webrtc::H264::NaluType::kPps) @@ -188,8 +187,7 @@ class LiveVideoSource : public VideoSourceWithDecoder, public T::Callback RTC_LOG(LS_VERBOSE) << "LiveVideoSource:onData SPS"; m_cfg.insert(m_cfg.end(), buffer + index.start_offset, buffer + index.payload_size + index.payload_start_offset); - cricket::VideoFormat videoFormat(0, 0, cricket::VideoFormat::FpsToInterval(0), FOURCC_H265); - m_decoder.postFormat(videoFormat); + m_decoder.postFormat(codec, 0, 0); } else if (nalu_type == webrtc::H265::NaluType::kPps) { @@ -265,8 +263,7 @@ class LiveVideoSource : public VideoSourceWithDecoder, public T::Callback } else if (codec == "VP9") { - cricket::VideoFormat videoFormat(0, 0, cricket::VideoFormat::FpsToInterval(0), FOURCC_VP9); - m_decoder.postFormat(videoFormat); + m_decoder.postFormat(codec, 0, 0); webrtc::VideoFrameType frameType = webrtc::VideoFrameType::kVideoFrameKey; rtc::scoped_refptr frame = webrtc::EncodedImageBuffer::Create(buffer, size); diff --git a/inc/rtmpvideosource.h b/inc/rtmpvideosource.h index a7fe52ce..fe537754 100644 --- a/inc/rtmpvideosource.h +++ b/inc/rtmpvideosource.h @@ -120,8 +120,7 @@ class RtmpVideoSource : public VideoSourceWithDecoder RTC_LOG(LS_ERROR) << "sps " << sps->width << "x" << sps->height; int fps = 25; RTC_LOG(LS_INFO) << "RtmpVideoSource:onData SPS set format " << sps->width << "x" << sps->height << " fps:" << fps; - cricket::VideoFormat videoFormat(sps->width, sps->height, cricket::VideoFormat::FpsToInterval(fps), cricket::FOURCC_H264); - m_decoder.postFormat(videoFormat); + m_decoder.postFormat("H264", sps->width, sps->height); m_cfg.insert(m_cfg.end(), H26X_marker, H26X_marker+sizeof(H26X_marker)); m_cfg.insert(m_cfg.end(), &body[13], &body[13 + spssize + 1]);