|
| 1 | +/* |
| 2 | + * Copyright 2026 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <atomic> |
| 20 | +#include <csignal> |
| 21 | +#include <cstdlib> |
| 22 | +#include <iostream> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace user_timestamped_video { |
| 27 | + |
| 28 | +enum class ParseResult { Ok, Help, Error }; |
| 29 | + |
| 30 | +struct CliOptions { |
| 31 | + std::string url; |
| 32 | + std::string token; |
| 33 | + bool use_user_timestamp = true; |
| 34 | +}; |
| 35 | + |
| 36 | +inline std::atomic<bool> g_running{true}; |
| 37 | + |
| 38 | +inline void handleSignal(int) { g_running.store(false); } |
| 39 | + |
| 40 | +inline bool isRunning() { return g_running.load(std::memory_order_relaxed); } |
| 41 | + |
| 42 | +inline void installSignalHandlers() { |
| 43 | + std::signal(SIGINT, handleSignal); |
| 44 | +#ifdef SIGTERM |
| 45 | + std::signal(SIGTERM, handleSignal); |
| 46 | +#endif |
| 47 | +} |
| 48 | + |
| 49 | +inline std::string getenvOrEmpty(const char *name) { |
| 50 | + const char *value = std::getenv(name); |
| 51 | + return value ? std::string(value) : std::string{}; |
| 52 | +} |
| 53 | + |
| 54 | +inline void printUsage(const char *program) { |
| 55 | + std::cerr << "Usage:\n" |
| 56 | + << " " << program << " <ws-url> <token> " |
| 57 | + << "[--with-user-timestamp|--without-user-timestamp]\n" |
| 58 | + << "or:\n" |
| 59 | + << " LIVEKIT_URL=... LIVEKIT_TOKEN=... " << program |
| 60 | + << " [--with-user-timestamp|--without-user-timestamp]\n"; |
| 61 | +} |
| 62 | + |
| 63 | +inline ParseResult parseArgs(int argc, char *argv[], CliOptions &options) { |
| 64 | + std::vector<std::string> positional; |
| 65 | + options = CliOptions{}; |
| 66 | + |
| 67 | + for (int i = 1; i < argc; ++i) { |
| 68 | + const std::string arg = argv[i]; |
| 69 | + if (arg == "-h" || arg == "--help") { |
| 70 | + return ParseResult::Help; |
| 71 | + } |
| 72 | + if (arg == "--without-user-timestamp") { |
| 73 | + options.use_user_timestamp = false; |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (arg == "--with-user-timestamp") { |
| 77 | + options.use_user_timestamp = true; |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (!arg.empty() && arg[0] == '-') { |
| 81 | + return ParseResult::Error; |
| 82 | + } |
| 83 | + |
| 84 | + positional.push_back(arg); |
| 85 | + } |
| 86 | + |
| 87 | + if (positional.size() > 2) { |
| 88 | + return ParseResult::Error; |
| 89 | + } |
| 90 | + |
| 91 | + options.url = getenvOrEmpty("LIVEKIT_URL"); |
| 92 | + options.token = getenvOrEmpty("LIVEKIT_TOKEN"); |
| 93 | + |
| 94 | + if (positional.size() == 2) { |
| 95 | + options.url = positional[0]; |
| 96 | + options.token = positional[1]; |
| 97 | + } else if (positional.size() == 1) { |
| 98 | + return ParseResult::Error; |
| 99 | + } |
| 100 | + |
| 101 | + return (options.url.empty() || options.token.empty()) ? ParseResult::Error |
| 102 | + : ParseResult::Ok; |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace user_timestamped_video |
0 commit comments