From 05cc343719f3436393048bc36a88a169f65ae242 Mon Sep 17 00:00:00 2001
From: Eloi BAIL <eloi.bail@savoirfairelinux.com>
Date: Wed, 25 Mar 2015 15:27:37 -0400
Subject: [PATCH] Daemon: set FPS for streamed files

Refs #69334

Change-Id: I2b1e6916b9d7aa314676cc9ef69eca653a3818e2
Signed-off-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/media/media_decoder.cpp     | 11 ++++++++++-
 src/media/media_decoder.h       |  2 ++
 src/media/video/video_input.cpp |  8 +++++---
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp
index c925641467..e24b368a2f 100644
--- a/src/media/media_decoder.cpp
+++ b/src/media/media_decoder.cpp
@@ -250,7 +250,8 @@ int MediaDecoder::setupFromVideoData()
     }
 
     // Get a pointer to the codec context for the video stream
-    decoderCtx_ = inputCtx_->streams[streamIndex_]->codec;
+    avStream_ = inputCtx_->streams[streamIndex_];
+    decoderCtx_ = avStream_->codec;
     if (decoderCtx_ == 0) {
         RING_ERR("Decoder context is NULL");
         return -1;
@@ -428,6 +429,14 @@ int MediaDecoder::getWidth() const
 int MediaDecoder::getHeight() const
 { return decoderCtx_->height; }
 
+int  // TODO : use of float fps is more accurate
+MediaDecoder::getFps() const
+{
+    return (avStream_->avg_frame_rate.den != 0 ?
+            (int)(avStream_->avg_frame_rate.num / avStream_->avg_frame_rate.den)
+            : 0);
+}
+
 int MediaDecoder::getPixelFormat() const
 { return libav_utils::ring_pixel_format(decoderCtx_->pix_fmt); }
 
diff --git a/src/media/media_decoder.h b/src/media/media_decoder.h
index 91f65194f8..0a20a3aa3c 100644
--- a/src/media/media_decoder.h
+++ b/src/media/media_decoder.h
@@ -99,6 +99,7 @@ class MediaDecoder {
 
         int getWidth() const;
         int getHeight() const;
+        int getFps() const;
         int getPixelFormat() const;
 
         void setOptions(const std::map<std::string, std::string>& options);
@@ -109,6 +110,7 @@ class MediaDecoder {
         AVCodec *inputDecoder_ = nullptr;
         AVCodecContext *decoderCtx_ = nullptr;
         AVFormatContext *inputCtx_ = nullptr;
+        AVStream *avStream_ = nullptr;
         std::unique_ptr<Resampler> resampler_;
         int streamIndex_ = -1;
         bool emulateRate_ = false;
diff --git a/src/media/video/video_input.cpp b/src/media/video/video_input.cpp
index a3d5a7b4dc..c11f19524c 100644
--- a/src/media/video/video_input.cpp
+++ b/src/media/video/video_input.cpp
@@ -185,6 +185,11 @@ VideoInput::createDecoder()
     }
     decOpts_.width = decoder_->getWidth();
     decOpts_.height = decoder_->getHeight();
+    decOpts_.framerate = decoder_->getFps();
+    RING_INFO("create decoder with video params : size=%dX%d, fps=%d",
+            decOpts_.width,
+            decOpts_.height,
+            decOpts_.framerate);
     foundDecOpts(decOpts_);
 }
 
@@ -266,9 +271,6 @@ VideoInput::initFile(std::string path)
         decOpts_.framerate = 1;
     } else {
         RING_WARN("Guessing file type for %s", path.c_str());
-        // FIXME: proper parsing of FPS etc. should be done in
-        // MediaDecoder, not here.
-        decOpts_.framerate = 25;
     }
 
     return true;
-- 
GitLab