From daa247f662fa4e2ed56cb2c5722347942b1f7b1c Mon Sep 17 00:00:00 2001
From: philippegorley <philippe.gorley@savoirfairelinux.com>
Date: Thu, 28 Jun 2018 10:25:50 -0400
Subject: [PATCH] media: fix file streaming

File streaming emulates the frame rate and needs the unscaled
timestamps.

Change-Id: If712062d263c7c2110a72056b889ef3a4d1e51df
Reviewed-by: Sebastien Blin <sebastien.blin@savoirfairelinux.com>
---
 src/media/media_decoder.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp
index 52ea3851e2..61604a5bab 100644
--- a/src/media/media_decoder.cpp
+++ b/src/media/media_decoder.cpp
@@ -290,6 +290,7 @@ MediaDecoder::decode(VideoFrame& result)
             }
         }
 #endif
+        auto emulationTimestamp = frame->pts;
         if (frame->pts != AV_NOPTS_VALUE)
             frame->pts = av_rescale_q(frame->pts - avStream_->start_time, avStream_->time_base, decoderCtx_->time_base);
 
@@ -306,8 +307,8 @@ MediaDecoder::decode(VideoFrame& result)
                 rec->recordData(frame, true, true);
         }
 
-        if (emulateRate_ and frame->pts != AV_NOPTS_VALUE) {
-            auto frame_time = getTimeBase()*(frame->pts - avStream_->start_time);
+        if (emulateRate_ and emulationTimestamp != AV_NOPTS_VALUE) {
+            auto frame_time = getTimeBase()*(emulationTimestamp - avStream_->start_time);
             auto target = startTime_ + static_cast<std::int64_t>(frame_time.real() * 1e6);
             auto now = av_gettime();
             if (target > now) {
@@ -359,6 +360,7 @@ MediaDecoder::decode(const AudioFrame& decodedFrame)
     if (frameFinished) {
         av_packet_unref(&inpacket);
 
+        auto emulationTimestamp = frame->pts;
         if (frame->pts != AV_NOPTS_VALUE)
             frame->pts = av_rescale_q(frame->pts, avStream_->time_base, decoderCtx_->time_base);
 
@@ -374,8 +376,8 @@ MediaDecoder::decode(const AudioFrame& decodedFrame)
                 rec->recordData(frame, false, true);
         }
 
-        if (emulateRate_ and frame->pts != AV_NOPTS_VALUE) {
-            auto frame_time = getTimeBase()*(frame->pts - avStream_->start_time);
+        if (emulateRate_ and emulationTimestamp != AV_NOPTS_VALUE) {
+            auto frame_time = getTimeBase()*(emulationTimestamp - avStream_->start_time);
             auto target = startTime_ + static_cast<std::int64_t>(frame_time.real() * 1e6);
             auto now = av_gettime();
             if (target > now) {
-- 
GitLab