Skip to content

Commit

Permalink
add boftimecode cont, add persist file to emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Jan 15, 2024
1 parent b0e1834 commit 6e5dbf0
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ include(cmake/fetch_content.cmake)
# in code coverage computation as they are test programs themselves.
set(EXTRA_COVERAGE_EXCLUSION "\'${CMAKE_CURRENT_SOURCE_DIR}/integration/*\'")

project(bofstd VERSION 5.4.0.0)
project(bofstd VERSION 5.4.1.0)
# Some naming conventions either requires lower or upper case.
# And some don't like underscore.
# So already prepare all those variables
Expand Down
104 changes: 104 additions & 0 deletions lib/include/bofstd/bofcircularbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,111 @@
#include <cstring>

BEGIN_BOF_NAMESPACE()
//TODO one day:
/*
#include <iostream>
#include <deque>
#include <mutex>
#include <condition_variable>
template <typename T>
class CircularBuffer {
public:
explicit CircularBuffer(size_t capacity, bool overwrite = false, bool useLock = false)
: capacity_(capacity), overwrite_(overwrite), useLock_(useLock) {}
void push(const T& value) {
if (useLock_) {
std::lock_guard<std::mutex> lock(mutex_);
pushInternal(value);
} else {
pushInternal(value);
}
}
T pop() {
if (useLock_) {
std::lock_guard<std::mutex> lock(mutex_);
return popInternal();
} else {
return popInternal();
}
}
void reset() {
if (useLock_) {
std::lock_guard<std::mutex> lock(mutex_);
resetInternal();
} else {
resetInternal();
}
}
private:
size_t capacity_;
bool overwrite_;
bool useLock_;
std::deque<T> buffer_;
std::mutex mutex_;
std::condition_variable notEmpty_;
void pushInternal(const T& value) {
if (buffer_.size() == capacity_) {
if (overwrite_) {
buffer_.pop_front();
} else {
// Wait until there is space in the buffer
while (buffer_.size() == capacity_) {
notEmpty_.wait(lock);
}
}
}
buffer_.push_back(value);
notEmpty_.notify_one();
}
T popInternal() {
// Wait until there is an element in the buffer
while (buffer_.empty()) {
notEmpty_.wait(lock);
}
T frontValue = buffer_.front();
buffer_.pop_front();
notEmpty_.notify_one();
return frontValue;
}
void resetInternal() {
buffer_.clear();
}
};
int main() {
CircularBuffer<int> circularBuffer(5, true, true);
// Example usage with lock protection
circularBuffer.push(1);
circularBuffer.push(2);
std::cout << "Pop: " << circularBuffer.pop() << std::endl;
circularBuffer.push(3);
circularBuffer.push(4);
circularBuffer.push(5);
circularBuffer.push(6); // Overwriting oldest element
// Without lock protection
CircularBuffer<int> bufferWithoutLock(3, false, false);
bufferWithoutLock.push(10);
std::cout << "Pop: " << bufferWithoutLock.pop() << std::endl;
return 0;
}
*/
#define BOF_CIRCULAR_BUFFER_LOCK(Sts) \
{ \
Sts = mCircularBufferParam_X.MultiThreadAware_B ? Bof_LockMutex(mCircularBufferMtx_X) : BOF_ERR_NO_ERROR; \
Expand Down
5 changes: 4 additions & 1 deletion lib/include/bofstd/boftimecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class BOFSTD_EXPORT BofTimecode

public:
BofTimecode();
BofTimecode(bool _Is50Hz, bool _Drop_B, uint16_t _NbDay_U16, uint8_t _Hour_U8, uint8_t _Minute_U8, uint8_t _Second_U8,
uint8_t _Frame_U8, uint8_t _UserBit0_U8, uint8_t _UserBit1_U8, uint8_t _UserBit2_U8, uint8_t _UserBit3_U8);
BofTimecode(const BofTimecode &) = default; // Copy constructor
BofTimecode(BofTimecode &&) = default; // Move constructor
BofTimecode &operator=(const BofTimecode &) = default; // Copy assignment operator
Expand Down Expand Up @@ -121,7 +123,8 @@ class BOFSTD_EXPORT BofTimecode
BofTimecode operator+(int32_t _NbField_S32);
BOF_TIMECODE ToByteStruct() const;
BOFERR FromByteStruct(const BOF_TIMECODE &_rBofTimeCodeStruct_X);

void GetUserBit(uint8_t &_rUserBit0_U8, uint8_t &_rUserBit1_U8, uint8_t &_rUserBit2_U8, uint8_t &_rUserBit3_U8);
void SetUserBit(uint8_t _UserBit0_U8, uint8_t _UserBit1_U8, uint8_t _UserBit2_U8, uint8_t _UserBit3_U8);
static BOFERR S_ValidateTimecode(const BOF_TIMECODE &_rBofTimeCodeStruct_X);
static BOFERR S_BinToTimeCode(uint64_t _Tc_U64, BOF_TIMECODE &_rTc_X);
static BOFERR S_TimeCodeToBin(const BOF_TIMECODE &_rTc_X, uint64_t &_rTc_U64);
Expand Down
34 changes: 17 additions & 17 deletions lib/src/bofstd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,29 +483,29 @@ BOFERR Bof_Initialize(BOFSTDPARAM &_rStdParam_X)
#endif

#if defined(__EMSCRIPTEN__)
// EM_ASM is a macro to call in-line JavaScript code.
if (GL_BofStdParam_X.pPersistentRootDir_c)
{
EM_ASM({
Module.fs_is_ready = 0; // flag to check when data are synchronized
FS.mkdir(UTF8ToString($0)); // Make a directory other than '/'
FS.mount(IDBFS, {}, UTF8ToString($0)); // Then mount with IDBFS type

FS.syncfs( // Then sync with true
true, function(err) {
Module.print("End persistent file sync..");
assert(!err);
Module.fs_is_ready = 1;
});
},
GL_BofStdParam_X.pPersistentRootDir_c);
}
/*
0 lets the browser decide how often to call your callback, like v-sync. You can pass a positive integer to set a specific frame rate.
true stops execution at this point, your next code that runs will be the loop callback.
*/
if (GL_BofStdParam_X.EmscriptenCallback)
{
// EM_ASM is a macro to call in-line JavaScript code.
if (GL_BofStdParam_X.pPersistentRootDir_c)
{
EM_ASM({
Module.fs_is_ready = 0; // flag to check when data are synchronized
FS.mkdir(UTF8ToString($0)); // Make a directory other than '/'
FS.mount(IDBFS, {}, UTF8ToString($0)); // Then mount with IDBFS type

FS.syncfs( // Then sync with true
true, function(err) {
Module.print("End persistent file sync..");
assert(!err);
Module.fs_is_ready = 1;
});
},
GL_BofStdParam_X.pPersistentRootDir_c);
}
emscripten_set_main_loop_arg(S_BofEmscriptenCallback, GL_BofStdParam_X.pEmscriptenCallbackArg, GL_BofStdParam_X.EmscriptenCallbackFps_U32, false);
}
#endif
Expand Down
36 changes: 36 additions & 0 deletions lib/src/boftimecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ BofTimecode::BofTimecode(bool _Ntsc_B, uint64_t _Ms_U64)
FromMs(_Ntsc_B, _Ms_U64);
mTcValid_B = true;
}
BofTimecode::BofTimecode(bool _Is50Hz, bool _Drop_B, uint16_t _NbDay_U16, uint8_t _Hour_U8, uint8_t _Minute_U8, uint8_t _Second_U8,
uint8_t _Frame_U8, uint8_t _UserBit0_U8, uint8_t _UserBit1_U8, uint8_t _UserBit2_U8, uint8_t _UserBit3_U8)
{
BOFERR Rts_E;
BOF_TIMECODE Tc_X;

Tc_X.TcFlag_U8 = 0;
if (!_Is50Hz) Tc_X.TcFlag_U8 |= BOF_TIMECODE_FLAG_NTSC;
if (_Drop_B) Tc_X.TcFlag_U8 |= BOF_TIMECODE_FLAG_DROP;
Tc_X.NbDay_U16 = _NbDay_U16;
Tc_X.Hour_U8 = _Hour_U8;
Tc_X.Minute_U8 = _Minute_U8;
Tc_X.Second_U8 = _Second_U8;
Tc_X.Frame_U8 = _Frame_U8;
Tc_X.pUserBit_U8[0] = _UserBit0_U8;
Tc_X.pUserBit_U8[1] = _UserBit1_U8;
Tc_X.pUserBit_U8[2] = _UserBit2_U8;
Tc_X.pUserBit_U8[3] = _UserBit3_U8;
Tc_X.NbDay_U16 = _NbDay_U16;
mTcValid_B = (FromByteStruct(Tc_X) == BOF_ERR_NO_ERROR);
}


BofTimecode::BofTimecode(const BOF_TIMECODE &_rBofTimeCodeStruct_X)
{
Expand Down Expand Up @@ -101,6 +123,20 @@ BofTimecode::BofTimecode(const char *_pTc_c)
}
}
}
void BofTimecode::GetUserBit(uint8_t &_rUserBit0_U8, uint8_t &_rUserBit1_U8, uint8_t &_rUserBit2_U8, uint8_t &_rUserBit3_U8)
{
_rUserBit0_U8 = mTc_X.pUserBit_U8[0];
_rUserBit1_U8 = mTc_X.pUserBit_U8[1];
_rUserBit2_U8 = mTc_X.pUserBit_U8[2];
_rUserBit3_U8 = mTc_X.pUserBit_U8[3];
}
void BofTimecode::SetUserBit(uint8_t _UserBit0_U8, uint8_t _UserBit1_U8, uint8_t _UserBit2_U8, uint8_t _UserBit3_U8)
{
mTc_X.pUserBit_U8[0]=_UserBit0_U8;
mTc_X.pUserBit_U8[1]=_UserBit1_U8;
mTc_X.pUserBit_U8[2]=_UserBit2_U8;
mTc_X.pUserBit_U8[3]=_UserBit3_U8;
}
/*
DROP FRAME TIMECODE
Expand Down
2 changes: 1 addition & 1 deletion tests/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int main(int argc, char *argv[])
// "Threading_Test.*:BofThreadPool_Test.*:Timecode_Test.*:Uart_Test.*:Uri_Test.*";

//::testing::GTEST_FLAG(filter) = "DateTime_Test.StringDateTime:SocketTcp_Test.TcpClientTest:DateTime_Test.ValidateDateTime:SocketOs_Test.SocketAddress";
//::testing::GTEST_FLAG(filter) = "SocketOs_Test.SocketAddress";
//::testing::GTEST_FLAG(filter) = "Timecode_Test.*";
//::testing::GTEST_FLAG(filter) = "XmlParser_Test.*:JsonParser_Test.*:JsonWriter_Test.*:Pipe_Test.*";
//::testing::GTEST_FLAG(filter) = "JsonParser_Test.*:XmlWriter_Test.*";
//::testing::GTEST_FLAG(filter) = "CmdLineParser_Test.*:Uri_Test.*";
Expand Down
22 changes: 22 additions & 0 deletions tests/src/ut_timecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ TEST(Timecode_Test, Construct)
int64_t DifMs_S64;
BofDateTime DateTime(26, 5, 2018, 8, 16, 32, 47 * 1000);
BofTimecode Tc1;
uint8_t UserBit0_U8, UserBit1_U8, UserBit2_U8, UserBit3_U8;

EXPECT_FALSE(Tc1.IsNtsc());
EXPECT_FALSE(Tc1.IsOddField());
Expand Down Expand Up @@ -122,6 +123,27 @@ TEST(Timecode_Test, Construct)
EXPECT_TRUE(Tc1.IsNtsc());
EXPECT_TRUE(Tc1.IsOddField());
EXPECT_EQ(Tc1.FrameTime(), 1000.0f / 30.0f);


TcInMs_U64 = (static_cast<uint64_t>(367) * static_cast<uint64_t>(24 * 60 * 60 * 1000)) + static_cast<uint64_t>(1 * 60 * 60 * 1000) + static_cast<uint64_t>(2 * 60 * 1000) + static_cast<uint64_t>(3 * 1000) + static_cast<uint64_t>(16.6667f * 9);
BofTimecode Tc4(true, false, 145, 8, 16, 32, 2,1,2,3,4);
EXPECT_FALSE(Tc4.IsNtsc());
EXPECT_EQ(Tc4.FrameTime(), 1000.0f / 25.0f);
EXPECT_EQ(Tc4.FieldTime(), 1000.0f / 50.0f);
EXPECT_STREQ(Tc4.ToString(true, "", true, "", true).c_str(), "1970-05-26 08:16:32:02 @1000/25");
Tc4.GetUserBit(UserBit0_U8, UserBit1_U8, UserBit2_U8, UserBit3_U8);
EXPECT_EQ(UserBit0_U8, 1);
EXPECT_EQ(UserBit1_U8, 2);
EXPECT_EQ(UserBit2_U8, 3);
EXPECT_EQ(UserBit3_U8, 4);

Tc4.SetUserBit(255,254,253,252);
Tc4.GetUserBit(UserBit0_U8, UserBit1_U8, UserBit2_U8, UserBit3_U8);
EXPECT_EQ(UserBit0_U8, 255);
EXPECT_EQ(UserBit1_U8, 254);
EXPECT_EQ(UserBit2_U8, 253);
EXPECT_EQ(UserBit3_U8, 252);

}
TEST(Timecode_Test, Operator)
{
Expand Down
2 changes: 1 addition & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bofstd",
"version": "5.4.0.0",
"version": "5.4.1.0",
"description": "The onbings general purpose C++ Multiplatform library",
"dependencies": [
{
Expand Down

0 comments on commit 6e5dbf0

Please sign in to comment.