浏览代码

switch to websocket as information receiver

Noah Vogt 7 小时之前
父节点
当前提交
fc6a05a6cb
共有 3 个文件被更改,包括 74 次插入20 次删除
  1. 4 2
      CMakeLists.txt
  2. 24 0
      README.md
  3. 46 18
      cd-rec-status.cpp

+ 4 - 2
CMakeLists.txt

@@ -13,12 +13,14 @@ set(CMAKE_PROJECT_NAME cd-rec-status)
 
 add_library(${CMAKE_PROJECT_NAME} MODULE ${SOURCES})
 
-find_package(Qt6 COMPONENTS Widgets Core)
+find_package(Qt6 COMPONENTS Widgets Core WebSockets)
 target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
   OBS::libobs
   OBS::frontend-api
   Qt6::Core
-  Qt6::Widgets)
+  Qt6::Widgets
+  Qt6::WebSockets
+)
 
 set_target_properties(
   ${CMAKE_PROJECT_NAME}

+ 24 - 0
README.md

@@ -3,3 +3,27 @@
 An [OBS](https://github.com/obsproject/obs-studio) plugin to display the status of an ongoing cd recording as a dock.
 
 The [offical obs plugin template](https://github.com/obsproject/obs-plugintemplate) was used to create this plugin.
+
+## How it works
+
+This plugin listens in on a WebSocket server on `ws://localhost:8765` from which it receives messages like:
+```json
+{
+  "recording": true,
+  "cd": 3,
+  "track": 7,
+  "cd_time": "00:12:03",
+  "track_time": "00:01:26"
+}
+```
+It then adds a dock on the obs studio interface titled *CD Rec Status* that displays the state of the (ongoing) cd recording.
+
+
+## Building
+- checkout the [obs studio](https://github.com/obsproject/obs-studio) repository
+- `cd ../../../UI/frontend-plugins/CMakeLists.txt`
+- run `git clone https://github.com/noahvogt/obs-cd-rec-status.git cd-rec-status`
+- add the following line to the `CMakeLists.txt` file: `add_subdirectory(cd-rec-status)`
+- follow the [offical obs build instructions](https://github.com/obsproject/obs-studio/wiki/Install-Instructions#building-obs-studio) for your prefered platform
+
+You now have compiled OBS with the plugin. But you probably don't want to rebuild obs after every new release. So just find the `cd-rec-status.so` file in the build directory and put it into a plugin directory you can point your regular obs installation to. If you are unsure on how to structure the plugin directory, have a look at the releases on this github repository.

+ 46 - 18
cd-rec-status.cpp

@@ -1,40 +1,68 @@
 #include <obs-module.h>
 #include <obs-frontend-api.h>
 #include <QLabel>
-#include <QFile>
 #include <QTimer>
 #include <QVBoxLayout>
 #include <QWidget>
 #include <QDockWidget>
+#include <QWebSocket>
+#include <QJsonDocument>
+#include <QJsonObject>
 
 OBS_DECLARE_MODULE()
-OBS_MODULE_USE_DEFAULT_LOCALE("show_cd_rec_status", "en-US")
+OBS_MODULE_USE_DEFAULT_LOCALE("cd-rec-status", "en-US")
 
 class CDStatusDock : public QWidget {
 public:
-    CDStatusDock(QWidget *parent = nullptr) : QWidget(parent) {
+    CDStatusDock(QWidget *parent = nullptr) : QWidget(parent), socket(new QWebSocket()) {
         QVBoxLayout *layout = new QVBoxLayout(this);
-        statusLabel = new QLabel("Loading...", this);
+        statusLabel = new QLabel("Connecting to server...", this);
         layout->addWidget(statusLabel);
         setLayout(layout);
 
-        QTimer *timer = new QTimer(this);
-        connect(timer, &QTimer::timeout, this, &CDStatusDock::updateStatus);
-        timer->start(1000);
+        connect(socket, &QWebSocket::connected, this, &CDStatusDock::onConnected);
+        connect(socket, &QWebSocket::disconnected, this, &CDStatusDock::onDisconnected);
+        connect(socket, &QWebSocket::textMessageReceived, this, &CDStatusDock::onMessageReceived);
+
+        socket->open(QUrl(QStringLiteral("ws://localhost:8765")));
     }
 
-private:
-    QLabel *statusLabel;
+    ~CDStatusDock() {
+        socket->close();
+        delete socket;
+    }
 
-    void updateStatus() {
-        QFile f("/tmp/cd_status.txt");
-        if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
-            QString text = f.readAll();
-            statusLabel->setText(text);
-        } else {
-            statusLabel->setText("Status file not found");
+private slots:
+    void onConnected() {
+        statusLabel->setText("Connected to status server");
+    }
+
+    void onDisconnected() {
+        statusLabel->setText("Disconnected from server");
+    }
+
+    void onMessageReceived(const QString &message) {
+        QJsonParseError parseError;
+        QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &parseError);
+        if (parseError.error != QJsonParseError::NoError || !doc.isObject()) {
+            statusLabel->setText("Invalid status message");
+            return;
         }
+
+        QJsonObject obj = doc.object();
+        QString statusText = QString("Recording: %1\nCD: %2\nTrack: %3\nElapsed CD Time: %4\nElapsed Track Time: %5")
+                                .arg(obj["recording"].toBool() ? "Yes" : "No")
+                                .arg(obj["cd"].toInt())
+                                .arg(obj["track"].toInt())
+                                .arg(obj["cd_time"].toString())
+                                .arg(obj["track_time"].toString());
+
+        statusLabel->setText(statusText);
     }
+
+private:
+    QLabel *statusLabel;
+    QWebSocket *socket;
 };
 
 static void *create_cd_status_dock(obs_source_t *) {
@@ -55,10 +83,10 @@ bool obs_module_load(void)
 
 MODULE_EXPORT const char *obs_module_description(void)
 {
-	return obs_module_text("Description");
+    return obs_module_text("Description");
 }
 
 MODULE_EXPORT const char *obs_module_name(void)
 {
-	return obs_module_text("CD Rec Status");
+    return obs_module_text("CD Rec Status");
 }