whip-utils.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <obs.h>
  3. #include <string>
  4. #include <random>
  5. #include <sstream>
  6. #define do_log(level, format, ...) \
  7. blog(level, "[obs-webrtc] [whip_output: '%s'] " format, \
  8. obs_output_get_name(output), ##__VA_ARGS__)
  9. static uint32_t generate_random_u32()
  10. {
  11. std::random_device rd;
  12. std::mt19937 gen(rd());
  13. std::uniform_int_distribution<uint32_t> dist(1, (UINT32_MAX - 1));
  14. return dist(gen);
  15. }
  16. static std::string trim_string(const std::string &source)
  17. {
  18. std::string ret(source);
  19. ret.erase(0, ret.find_first_not_of(" \n\r\t"));
  20. ret.erase(ret.find_last_not_of(" \n\r\t") + 1);
  21. return ret;
  22. }
  23. static std::string value_for_header(const std::string &header,
  24. const std::string &val)
  25. {
  26. if (val.size() <= header.size() ||
  27. astrcmpi_n(header.c_str(), val.c_str(), header.size()) != 0) {
  28. return "";
  29. }
  30. auto delimiter = val.find_first_of(" ");
  31. if (delimiter == std::string::npos) {
  32. return "";
  33. }
  34. return val.substr(delimiter + 1);
  35. }
  36. static size_t curl_writefunction(char *data, size_t size, size_t nmemb,
  37. void *priv_data)
  38. {
  39. auto read_buffer = static_cast<std::string *>(priv_data);
  40. size_t real_size = size * nmemb;
  41. read_buffer->append(data, real_size);
  42. return real_size;
  43. }
  44. static size_t curl_header_function(char *data, size_t size, size_t nmemb,
  45. void *priv_data)
  46. {
  47. auto header_buffer = static_cast<std::vector<std::string> *>(priv_data);
  48. header_buffer->push_back(trim_string(std::string(data, size * nmemb)));
  49. return size * nmemb;
  50. }
  51. static inline std::string generate_user_agent()
  52. {
  53. #ifdef _WIN64
  54. #define OS_NAME "Windows x86_64"
  55. #elif __APPLE__
  56. #define OS_NAME "Mac OS X"
  57. #elif __OpenBSD__
  58. #define OS_NAME "OpenBSD"
  59. #elif __FreeBSD__
  60. #define OS_NAME "FreeBSD"
  61. #elif __linux__ && __LP64__
  62. #define OS_NAME "Linux x86_64"
  63. #else
  64. #define OS_NAME "Linux"
  65. #endif
  66. // Build the user-agent string
  67. std::stringstream ua;
  68. // User agent header prefix
  69. ua << "User-Agent: Mozilla/5.0 ";
  70. // OBS version info
  71. ua << "(OBS-Studio/" << obs_get_version_string() << "; ";
  72. // Operating system version info
  73. ua << OS_NAME << "; " << obs_get_locale() << ")";
  74. return ua.str();
  75. }