Skip to content

Commit

Permalink
Video encoder timing test (#110)
Browse files Browse the repository at this point in the history
Test that verifies video encoder timing, which uses VideoEncoder to encode several frames to a video file and verifies the duration using the Video class with a new Duration() function.

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>
Co-authored-by: Ian Chen <ichen@osrfoundation.org>
  • Loading branch information
jennuine and iche033 authored Oct 29, 2020
1 parent 6eeff9c commit f59b60a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
9 changes: 9 additions & 0 deletions av/include/ignition/common/Video.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ namespace ignition
/// \return the height
public: int Height() const;

/// \brief Convenience type alias for duration
/// where 1000000 is the same as AV_TIME_BASE fractional seconds
public:
using Length = std::chrono::duration<int64_t, std::ratio<1, 1000000>>;

/// \brief Get the duration of the video
/// \return the duration
public: Length Duration() const;

/// \brief Get the next frame of the video.
/// \param[out] _img Image in which the frame is stored
/// \return false on error
Expand Down
6 changes: 6 additions & 0 deletions av/src/Video.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,9 @@ int Video::Height() const
{
return this->dataPtr->codecCtx->height;
}

/////////////////////////////////////////////////
Video::Length Video::Duration() const
{
return Video::Length(this->dataPtr->formatCtx->duration);
}
3 changes: 2 additions & 1 deletion test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ list(REMOVE_ITEM tests mesh.cc)

ign_build_tests(
TYPE INTEGRATION
SOURCES ${tests})
SOURCES ${tests}
LIB_DEPS ${PROJECT_LIBRARY_TARGET_NAME}-av)

if(TARGET INTEGRATION_plugin)
# We add this dependency to make sure that DummyPlugins gets generated
Expand Down
62 changes: 62 additions & 0 deletions test/integration/encoder_timing.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <array>
#include "ignition/common/VideoEncoder.hh"
#include "ignition/common/Video.hh"
#include "test_config.h"
#include "test/util.hh"

using namespace ignition;
using namespace common;

const unsigned int kSize = 10;
const std::array<unsigned char, kSize*kSize> kFrame = {};

// set to 720ms because video duration missing additional 18 frames
// which may be due to how video encoding works
const std::chrono::milliseconds kTol(720);

void durationTest(VideoEncoder &_vidEncoder, Video &_video,
const int &_fps, const int &_seconds)
{
_vidEncoder.Start("mp4", "", kSize, kSize, _fps, 0);

int frameCount = 0;
while (frameCount != _fps*_seconds)
{
if (_vidEncoder.AddFrame(kFrame.data(), kSize, kSize))
++frameCount;
}

_vidEncoder.Stop();
_video.Load(common::joinPaths(common::cwd(), "/TMP_RECORDING.mp4"));

EXPECT_NEAR(std::chrono::duration_cast<std::chrono::milliseconds>(
_video.Duration()).count(),
_seconds*1000,
kTol.count());
}

TEST(EncoderTimingTest, Duration)
{
VideoEncoder vidEncoder;
Video video;

durationTest(vidEncoder, video, 50, 1);
durationTest(vidEncoder, video, 30, 2);
durationTest(vidEncoder, video, 25, 5);
}

0 comments on commit f59b60a

Please sign in to comment.