whip-utils.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #define do_log_s(level, format, ...) \
  10. blog(level, "[obs-webrtc] [whip_output: '%s'] " format, \
  11. obs_output_get_name(whipOutput->output), ##__VA_ARGS__)
  12. static uint32_t generate_random_u32()
  13. {
  14. std::random_device rd;
  15. std::mt19937 gen(rd());
  16. std::uniform_int_distribution<uint32_t> dist(1, (UINT32_MAX - 1));
  17. return dist(gen);
  18. }
  19. static std::string trim_string(const std::string &source)
  20. {
  21. std::string ret(source);
  22. ret.erase(0, ret.find_first_not_of(" \n\r\t"));
  23. ret.erase(ret.find_last_not_of(" \n\r\t") + 1);
  24. return ret;
  25. }
  26. static size_t curl_writefunction(char *data, size_t size, size_t nmemb,
  27. void *priv_data)
  28. {
  29. auto read_buffer = static_cast<std::string *>(priv_data);
  30. size_t real_size = size * nmemb;
  31. read_buffer->append(data, real_size);
  32. return real_size;
  33. }
  34. #define LOCATION_HEADER_LENGTH 10
  35. static size_t curl_header_location_function(char *data, size_t size,
  36. size_t nmemb, void *priv_data)
  37. {
  38. auto header_buffer = static_cast<std::vector<std::string> *>(priv_data);
  39. size_t real_size = size * nmemb;
  40. if (real_size < LOCATION_HEADER_LENGTH)
  41. return real_size;
  42. if (!astrcmpi_n(data, "location: ", LOCATION_HEADER_LENGTH)) {
  43. char *val = data + LOCATION_HEADER_LENGTH;
  44. auto header_temp =
  45. std::string(val, real_size - LOCATION_HEADER_LENGTH);
  46. header_temp = trim_string(header_temp);
  47. header_buffer->push_back(header_temp);
  48. }
  49. return real_size;
  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. }