Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
mpromonet committed Aug 20, 2023
1 parent 165377a commit 4c989c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
54 changes: 26 additions & 28 deletions inc/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@
#include "SessionSink.h"
#include "VideoScaler.h"

#define FOURCC(a, b, c, d) \
((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \
(static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(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<webrtc::VideoFrame>, public webrtc::DecodedImageCallback {
private:
class Frame
{
public:
Frame(): m_timestamp_ms(0) {}
Frame(const rtc::scoped_refptr<webrtc::EncodedImageBuffer> & 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<webrtc::EncodedImageBuffer> 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:
Expand Down Expand Up @@ -134,8 +128,8 @@ class VideoDecoder : public rtc::VideoSourceInterface<webrtc::VideoFrame>, 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<std::mutex> lock(m_queuemutex);
m_queue.push(frame);
Expand Down Expand Up @@ -195,17 +189,17 @@ class VideoDecoder : public rtc::VideoSourceInterface<webrtc::VideoFrame>, 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);
}
Expand All @@ -232,21 +226,22 @@ class VideoDecoder : public rtc::VideoSourceInterface<webrtc::VideoFrame>, 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);
}
}

Expand Down Expand Up @@ -300,7 +295,10 @@ class VideoDecoder : public rtc::VideoSourceInterface<webrtc::VideoFrame>, publi
protected:
VideoScaler m_scaler;
std::unique_ptr<webrtc::VideoDecoderFactory>& m_factory;
cricket::VideoFormat m_format;

std::string m_format;
int m_width;
int m_height;

std::queue<Frame> m_queue;
std::mutex m_queuemutex;
Expand Down
9 changes: 3 additions & 6 deletions inc/livevideosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<webrtc::EncodedImageBuffer> frame = webrtc::EncodedImageBuffer::Create(buffer, size);
Expand Down
3 changes: 1 addition & 2 deletions inc/rtmpvideosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 4c989c5

Please sign in to comment.