Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
gamp_av_base.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#include <ctime>
13#include <regex>
14
15#include <jau/debug.hpp>
16#include <jau/environment.hpp>
17#include <jau/string_util.hpp>
18
19#include <gamp/av/PTS.hpp>
20
21//
22//
23//
24
25static std::regex jau_PTS_time_regex() noexcept {
26 try {
27 // [[12:]34:]56.789
28 // - g1: hour 12 (optional)
29 // - g2: minute 34
30 // - g3: second 56
31 // - g4: second-fraction 789 (optional)
32 return
33 std::regex( R"(^\s*)"
34 R"((?:(?:(2[0-3]|1\d|0?\d):)?([1-5]\d|0?\d):([1-5]\d|0?\d)(?:\.(\d+))?)?)"
35 // g1 g2 g3 g4
36 );
37
38 } catch (...) {
39 ERR_PRINT2("Caught unknown exception");
40 return std::regex();
41 }
42}
43
44uint32_t gamp::av::PTS::toMillis(const std::string& timestr) noexcept {
45 static std::regex pattern = jau_PTS_time_regex();
46
47 std::smatch match;
48 try {
49 if( std::regex_search(timestr, match, pattern) ) {
50 constexpr bool DBG_OUT = false;
51 if constexpr ( DBG_OUT ) {
52 std::cout << "XXX: " << timestr << std::endl;
53 std::cout << "XXX: match pos " << match.position() << ", len " << match.length() << ", sz " << match.size() << "\n";
54 for(size_t i=0; i<match.size(); ++i) {
55 const std::string& ms = match[i];
56 std::cout << "- [" << i << "]: '" << ms << "', len " << ms.length() << "\n";
57 }
58 }
59 uint32_t h=0,m=0,s=0;
60 uint64_t ns=0;
61
62 if( match.size() > 1 && match[1].length() > 0 ) {
63 h = std::stoi(match[1]);
64 }
65 if( match.size() > 2 && match[2].length() > 0 ) {
66 m = std::stoi(match[2]);
67 }
68 if( match.size() > 3 && match[3].length() > 0 ) {
69 s = std::stoi(match[3]);
70 if ( match.size() > 4 && 0 < match[4].length() && match[4].length() <= 9 ) {
71 const size_t ns_sdigits = match[4].length();
72 ns = std::stoul(match[4]) * static_cast<uint64_t>(std::pow(10, 9 - ns_sdigits));
73 }
74 }
75 constexpr uint64_t ns_per_ms = 1'000'000UL;
76 constexpr uint64_t ms_per_sec = 1'000UL;
77 constexpr uint64_t ms_per_min = ms_per_sec * 60UL;
78 constexpr uint64_t ms_per_h = ms_per_min * 60UL;
79
80 return h * ms_per_h + m * ms_per_min + s * ms_per_sec +
81 ns / ns_per_ms;
82 }
83 } catch (...) {
84 ERR_PRINT2("Caught unknown exception parsing %s", timestr.c_str());
85 }
86 return 0; // error
87}
88
static uint32_t toMillis(const std::string &datestr) noexcept
Returns milliseconds from given string representation in '[H[H]:]m[m]:s[s][.S*]'.
#define ERR_PRINT2(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
Definition debug.hpp:125
static std::regex jau_PTS_time_regex() noexcept