From 4a2b2e013bb8a72bdfac58dd3dacb96ff9c0c206 Mon Sep 17 00:00:00 2001 From: Sanchay Sinha <100801@ittiam.com> Date: Fri, 29 Nov 2024 14:58:27 +0530 Subject: [PATCH] Change in accumulated time logic in loopback and also Adding of accumulated time in output json log --- .../com/facebook/encapp/BufferTranscoder.java | 33 +++++++++---------- .../com/facebook/encapp/MainActivity.java | 12 +++++-- .../com/facebook/encapp/utils/Statistics.java | 7 +++- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/facebook/encapp/BufferTranscoder.java b/app/src/main/java/com/facebook/encapp/BufferTranscoder.java index c169b7e..dc12ad0 100644 --- a/app/src/main/java/com/facebook/encapp/BufferTranscoder.java +++ b/app/src/main/java/com/facebook/encapp/BufferTranscoder.java @@ -80,6 +80,7 @@ public static native int x264Encode(byte[] yuvByteArray, byte[] outputBuffer, in int encodedequeuinput = 0; int encodedequeuoutput = 0; long mLoopback = 0; + long finalAccumulatedTime = 0; /*Flag indicates dequeue decoded o/p from decoder */ boolean decOutputExtractDone = false; @@ -478,7 +479,8 @@ public String start() { inpPlaneStride = null; downscaleByteBuffArr = null; downscalePlaneStride = null; - return String.valueOf(mLoopback); + + return "{ \"loopback\" : " + mLoopback + ",\"accumulatedvalue\" : " + finalAccumulatedTime + "}"; } public void writeToBuffer(@NonNull MediaCodec codec, int index, boolean encoder) { @@ -779,7 +781,13 @@ void bufferTranscoding(int trackIndex) throws IOException { // End of stream reached; perform loopback lastPresentationTimeUs += clipDurationUs; - accumulatedDurationUs += clipDurationUs; + + long loopback_finished = Calendar.getInstance().getTimeInMillis(); + long elapsed_time = (loopback_finished - starttime) * 1000; + + starttime = loopback_finished; + + accumulatedDurationUs += elapsed_time; // **Note** : Run loopback till it exceeds totalDuration. // And Don't do accumulatedDurationUs += clipDurationUs; // Need to test with multiple test in pbtxt. @@ -788,22 +796,13 @@ void bufferTranscoding(int trackIndex) throws IOException { if (accumulatedDurationUs >= totalDurationUs) { Log.d(TAG, "Reached End"); Log.d(TAG, "accumulatedDurationUs : " + accumulatedDurationUs); - long loopback_finished = Calendar.getInstance().getTimeInMillis(); - long elapsed_time = (loopback_finished - starttime) * 1000; + finalAccumulatedTime = accumulatedDurationUs; - Log.d(TAG, "elapsed_time : " + elapsed_time); - - if(elapsed_time < totalDurationUs) { - accumulatedDurationUs = elapsed_time; - continue; - } - else { - // End of stream - send EOS flag - mDecoder.queueInputBuffer(inputBufferIndex, 0, 0, 0L, - MediaCodec.BUFFER_FLAG_END_OF_STREAM); - decOutputExtractDone = true; - break; - } + // End of stream - send EOS flag + mDecoder.queueInputBuffer(inputBufferIndex, 0, 0, 0L, + MediaCodec.BUFFER_FLAG_END_OF_STREAM); + decOutputExtractDone = true; + break; } else { continue; diff --git a/app/src/main/java/com/facebook/encapp/MainActivity.java b/app/src/main/java/com/facebook/encapp/MainActivity.java index a88a849..246ae5e 100644 --- a/app/src/main/java/com/facebook/encapp/MainActivity.java +++ b/app/src/main/java/com/facebook/encapp/MainActivity.java @@ -71,6 +71,7 @@ import android.media.MediaCodec; import org.json.JSONException; +import org.json.JSONObject; public class MainActivity extends AppCompatActivity { private final static String TAG = "encapp.main"; @@ -102,6 +103,7 @@ public class MainActivity extends AppCompatActivity { int startbattery; int endbattery; long mLoopback = 0; + long accumulatedtime = 0; Encoder coder; String fullFilename=""; private static List VIDEO_ENCODED_EXTENSIONS = Arrays.asList("mp4", "webm", "mkv"); @@ -964,10 +966,14 @@ public void run() { status = coder.start(); boolean status_flag = false; try { - mLoopback = Long.parseLong(status); +// mLoopback = Long.parseLong(status); +// status_flag = true; + JSONObject response = new JSONObject(status); status_flag = true; + mLoopback = response.getLong("loopback"); + accumulatedtime = response.getLong("accumulatedvalue"); } - catch (NumberFormatException e) { + catch (JSONException e) { Log.d(TAG, "NumberFormatException Happened : " + e.getMessage()); } if (status_flag) { @@ -993,7 +999,7 @@ public void run() { stats.setStatus(status); Log.d(TAG, "Write stats for " + stats.getId() + " to " + fullFilename); endbattery = getChargeCounter(); - stats.LoopbackData(mLoopback); + stats.LoopbackData(mLoopback, accumulatedtime); stats.BatteryTest(startbattery,endbattery,voltage); try { Log.d(TAG, "Write stats for " + stats.getId() + " to " + fullFilename); diff --git a/app/src/main/java/com/facebook/encapp/utils/Statistics.java b/app/src/main/java/com/facebook/encapp/utils/Statistics.java index f77fae3..e903184 100644 --- a/app/src/main/java/com/facebook/encapp/utils/Statistics.java +++ b/app/src/main/java/com/facebook/encapp/utils/Statistics.java @@ -29,6 +29,7 @@ import java.util.Locale; import java.util.Set; import java.util.UUID; +import java.util.concurrent.TimeUnit; public class Statistics { final static String TAG = "encapp.statistics"; @@ -60,6 +61,7 @@ public class Statistics { int mbatteryDifference; double mtotalEnergyConsumption; long mloopbacks = 0; + long finalaccumulated = 0; double menergy_consumption_per_loopback = 0.0; private static List MEDIAFORMAT_KEY_STRING_LIST = Arrays.asList( @@ -286,8 +288,9 @@ public void BatteryTest(int startbattery,int endbattery,double Voltage) { } } - public void LoopbackData(long no_of_loopbacks) { + public void LoopbackData(long no_of_loopbacks, long accumulatedtime) { mloopbacks = no_of_loopbacks; + finalaccumulated = accumulatedtime; } public void setEncoderMediaFormat(MediaFormat format) { @@ -528,6 +531,8 @@ public void writeJSON(Writer writer) throws IOException, JSONException { JSONObject loopbackData = new JSONObject(); loopbackData.put("Number_of_Loopbacks", mloopbacks); loopbackData.put("Energy_Consumption_per_loopback", menergy_consumption_per_loopback); + String accumulatedInMins = String.valueOf(TimeUnit.MINUTES.convert(finalaccumulated, TimeUnit.MICROSECONDS)); + loopbackData.put("Total_time_taken_to_finish_transcoding", accumulatedInMins+" mins"); json.put("Loopback_data", loopbackData); // GPU info