From c05dfb14625cf8929460285ecab622e22dd644f0 Mon Sep 17 00:00:00 2001
From: Pierre Lespagnol <pierre.lespagnol@savoirfairelinux.com>
Date: Thu, 18 Feb 2021 12:09:35 -0500
Subject: [PATCH] media: update the resolution from decoder to receive_thread
 if change

Gitlab: #447

Change-Id: I249fb7034a12f117298187b7b42977760be3ef83
---
 src/media/media_decoder.cpp              | 14 ++++++++++++++
 src/media/media_decoder.h                |  6 ++++++
 src/media/video/video_receive_thread.cpp |  4 ++++
 3 files changed, 24 insertions(+)

diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp
index 894f4bde6d..65fe188882 100644
--- a/src/media/media_decoder.cpp
+++ b/src/media/media_decoder.cpp
@@ -532,6 +532,8 @@ MediaDecoder::prepareDecoderContext()
         return -1;
     }
     avcodec_parameters_to_context(decoderCtx_, avStream_->codecpar);
+    width_ = decoderCtx_->width;
+    height_ = decoderCtx_->height;
     decoderCtx_->framerate = avStream_->avg_frame_rate;
     if (avStream_->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
         if (decoderCtx_->framerate.num == 0 || decoderCtx_->framerate.den == 0)
@@ -579,6 +581,18 @@ MediaDecoder::decode(AVPacket& packet)
                  : std::static_pointer_cast<MediaFrame>(std::make_shared<AudioFrame>());
     auto frame = f->pointer();
     ret = avcodec_receive_frame(decoderCtx_, frame);
+    if (resolutionChangedCallback_) {
+        if (decoderCtx_->width != width_ or decoderCtx_->height != height_) {
+            JAMI_DBG("Resolution changed from %dx%d to %dx%d",
+                width_,
+                height_,
+                decoderCtx_->width,
+                decoderCtx_->height);
+            width_ = decoderCtx_->width;
+            height_ = decoderCtx_->height;
+            resolutionChangedCallback_(width_, height_);
+        }
+    }
     if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
         return DecodeStatus::DecodeError;
     }
diff --git a/src/media/media_decoder.h b/src/media/media_decoder.h
index 826a9ab10b..64c4047b7c 100644
--- a/src/media/media_decoder.h
+++ b/src/media/media_decoder.h
@@ -191,6 +191,8 @@ public:
 
     MediaStream getStream(std::string name = "") const;
 
+    void setResolutionChangedCallback(std::function<void(int, int)> cb) { resolutionChangedCallback_ = std::move(cb); }
+
 private:
     NON_COPYABLE(MediaDecoder);
 
@@ -223,6 +225,10 @@ private:
     int prepareDecoderContext();
     int64_t seekTime_ = -1;
     void resetSeekTime() { seekTime_ = -1; }
+    std::function<void(int, int)> resolutionChangedCallback_;
+
+    int width_;
+    int height_;
 
 protected:
     AVDictionary* options_ = nullptr;
diff --git a/src/media/video/video_receive_thread.cpp b/src/media/video/video_receive_thread.cpp
index 2c865832cd..690377af13 100644
--- a/src/media/video/video_receive_thread.cpp
+++ b/src/media/video/video_receive_thread.cpp
@@ -82,6 +82,10 @@ VideoReceiveThread::setup()
                                             av_buffer_ref(displayMatrix.get()));
         publishFrame(std::static_pointer_cast<VideoFrame>(frame));
     }));
+    videoDecoder_->setResolutionChangedCallback([this] (int width, int height){
+        dstWidth_ = width;
+        dstHeight_ = height;
+    });
 
     dstWidth_ = args_.width;
     dstHeight_ = args_.height;
-- 
GitLab