1
0

window-basic-auto-config.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #pragma once
  2. #include <QWizard>
  3. #include <QPointer>
  4. #include <QFormLayout>
  5. #include <QWizardPage>
  6. #include <condition_variable>
  7. #include <utility>
  8. #include <thread>
  9. #include <memory>
  10. #include <vector>
  11. #include <string>
  12. #include <mutex>
  13. #include <json11.hpp>
  14. #include "streaming-helpers.hpp"
  15. class Ui_AutoConfigStartPage;
  16. class Ui_AutoConfigVideoPage;
  17. class Ui_AutoConfigStreamPage;
  18. class Ui_AutoConfigTestPage;
  19. class AutoConfigStreamPage;
  20. class Auth;
  21. class AutoConfig : public QWizard {
  22. Q_OBJECT
  23. friend class AutoConfigStartPage;
  24. friend class AutoConfigVideoPage;
  25. friend class AutoConfigStreamPage;
  26. friend class AutoConfigTestPage;
  27. enum class Type {
  28. Invalid,
  29. Streaming,
  30. Recording,
  31. VirtualCam,
  32. };
  33. enum class Service {
  34. Twitch,
  35. YouTube,
  36. Other,
  37. };
  38. enum class Encoder {
  39. x264,
  40. NVENC,
  41. QSV,
  42. AMD,
  43. Stream,
  44. };
  45. enum class Quality {
  46. Stream,
  47. High,
  48. };
  49. enum class FPSType : int {
  50. PreferHighFPS,
  51. PreferHighRes,
  52. UseCurrent,
  53. fps30,
  54. fps60,
  55. };
  56. static inline const char *GetEncoderId(Encoder enc);
  57. AutoConfigStreamPage *streamPage = nullptr;
  58. Service service = Service::Other;
  59. Quality recordingQuality = Quality::Stream;
  60. Encoder recordingEncoder = Encoder::Stream;
  61. Encoder streamingEncoder = Encoder::x264;
  62. Type type = Type::Streaming;
  63. FPSType fpsType = FPSType::PreferHighFPS;
  64. int idealBitrate = 2500;
  65. int baseResolutionCX = 1920;
  66. int baseResolutionCY = 1080;
  67. int idealResolutionCX = 1280;
  68. int idealResolutionCY = 720;
  69. int idealFPSNum = 60;
  70. int idealFPSDen = 1;
  71. std::string serviceName;
  72. std::string serverName;
  73. std::string server;
  74. std::string key;
  75. bool hardwareEncodingAvailable = false;
  76. bool nvencAvailable = false;
  77. bool qsvAvailable = false;
  78. bool vceAvailable = false;
  79. int startingBitrate = 2500;
  80. bool customServer = false;
  81. bool bandwidthTest = false;
  82. bool testRegions = true;
  83. bool twitchAuto = false;
  84. bool regionUS = true;
  85. bool regionEU = true;
  86. bool regionAsia = true;
  87. bool regionOther = true;
  88. bool preferHighFPS = false;
  89. bool preferHardware = false;
  90. int specificFPSNum = 0;
  91. int specificFPSDen = 0;
  92. void TestHardwareEncoding();
  93. bool CanTestServer(const char *server);
  94. virtual void done(int result) override;
  95. void SaveStreamSettings();
  96. void SaveSettings();
  97. public:
  98. AutoConfig(QWidget *parent);
  99. ~AutoConfig();
  100. enum Page {
  101. StartPage,
  102. VideoPage,
  103. StreamPage,
  104. TestPage,
  105. };
  106. };
  107. class AutoConfigStartPage : public QWizardPage {
  108. Q_OBJECT
  109. friend class AutoConfig;
  110. std::unique_ptr<Ui_AutoConfigStartPage> ui;
  111. public:
  112. AutoConfigStartPage(QWidget *parent = nullptr);
  113. ~AutoConfigStartPage();
  114. virtual int nextId() const override;
  115. public slots:
  116. void on_prioritizeStreaming_clicked();
  117. void on_prioritizeRecording_clicked();
  118. void PrioritizeVCam();
  119. };
  120. class AutoConfigVideoPage : public QWizardPage {
  121. Q_OBJECT
  122. friend class AutoConfig;
  123. std::unique_ptr<Ui_AutoConfigVideoPage> ui;
  124. public:
  125. AutoConfigVideoPage(QWidget *parent = nullptr);
  126. ~AutoConfigVideoPage();
  127. virtual int nextId() const override;
  128. virtual bool validatePage() override;
  129. };
  130. class AutoConfigStreamPage : public QWizardPage {
  131. Q_OBJECT
  132. friend class AutoConfig;
  133. enum class Section : int {
  134. Connect,
  135. StreamKey,
  136. };
  137. std::shared_ptr<Auth> auth;
  138. std::unique_ptr<Ui_AutoConfigStreamPage> ui;
  139. bool ready = false;
  140. StreamSettingsUI streamUi;
  141. public:
  142. AutoConfigStreamPage(QWidget *parent = nullptr);
  143. ~AutoConfigStreamPage();
  144. virtual bool isComplete() const override;
  145. virtual int nextId() const override;
  146. virtual bool validatePage() override;
  147. void OnAuthConnected();
  148. void OnOAuthStreamKeyConnected();
  149. public slots:
  150. void on_show_clicked();
  151. void on_connectAccount_clicked();
  152. void on_disconnectAccount_clicked();
  153. void on_useStreamKey_clicked();
  154. void ServiceChanged();
  155. void UpdateCompleted();
  156. void reset_service_ui_fields(std::string &service);
  157. };
  158. class AutoConfigTestPage : public QWizardPage {
  159. Q_OBJECT
  160. friend class AutoConfig;
  161. QPointer<QFormLayout> results;
  162. std::unique_ptr<Ui_AutoConfigTestPage> ui;
  163. std::thread testThread;
  164. std::condition_variable cv;
  165. std::mutex m;
  166. bool cancel = false;
  167. bool started = false;
  168. enum class Stage {
  169. Starting,
  170. BandwidthTest,
  171. StreamEncoder,
  172. RecordingEncoder,
  173. Finished,
  174. };
  175. Stage stage = Stage::Starting;
  176. bool softwareTested = false;
  177. void StartBandwidthStage();
  178. void StartStreamEncoderStage();
  179. void StartRecordingEncoderStage();
  180. void FindIdealHardwareResolution();
  181. bool TestSoftwareEncoding();
  182. void TestBandwidthThread();
  183. void TestStreamEncoderThread();
  184. void TestRecordingEncoderThread();
  185. void FinalizeResults();
  186. struct ServerInfo {
  187. std::string name;
  188. std::string address;
  189. int bitrate = 0;
  190. int ms = -1;
  191. inline ServerInfo() {}
  192. inline ServerInfo(const std::string &name_,
  193. const std::string &address_)
  194. : name(name_), address(address_)
  195. {
  196. }
  197. };
  198. void GetServers(std::vector<ServerInfo> &servers);
  199. public:
  200. AutoConfigTestPage(QWidget *parent = nullptr);
  201. ~AutoConfigTestPage();
  202. virtual void initializePage() override;
  203. virtual void cleanupPage() override;
  204. virtual bool isComplete() const override;
  205. virtual int nextId() const override;
  206. public slots:
  207. void NextStage();
  208. void UpdateMessage(QString message);
  209. void Failure(QString message);
  210. void Progress(int percentage);
  211. };