beeta/backend/src/services/RestreamService.h
doomtube 381e8b79b0
Some checks failed
Build and Push / build-all (push) Has been cancelled
fixes lol
2026-01-11 20:08:40 -05:00

77 lines
2.9 KiB
C++

#pragma once
#include <drogon/HttpClient.h>
#include <drogon/utils/Utilities.h>
#include <functional>
#include <string>
#include <unordered_map>
#include <mutex>
#include <memory>
struct RestreamDestination {
int64_t id;
int64_t realmId;
std::string name;
std::string rtmpUrl;
std::string streamKey;
bool enabled;
bool isConnected;
std::string lastError;
};
class RestreamService {
public:
static RestreamService& getInstance() {
static RestreamService instance;
return instance;
}
// Start pushing stream to a destination
void startPush(const std::string& sourceStreamKey, const RestreamDestination& dest,
std::function<void(bool, const std::string&)> callback);
// Stop pushing stream to a destination
void stopPush(const std::string& sourceStreamKey, int64_t destinationId,
std::function<void(bool)> callback);
// Stop all pushes for a stream
void stopAllPushes(const std::string& sourceStreamKey,
std::function<void(bool)> callback);
// Get push status for a destination
void getPushStatus(const std::string& sourceStreamKey, int64_t destinationId,
std::function<void(bool, bool isConnected, const std::string& error)> callback);
// Start all enabled destinations for a realm when stream goes live
void startAllDestinations(const std::string& streamKey, int64_t realmId);
// Stop all destinations for a realm when stream goes offline
void stopAllDestinations(const std::string& streamKey, int64_t realmId);
// Attempt reconnection for failed destinations (called periodically)
void attemptReconnections(const std::string& streamKey, int64_t realmId);
private:
RestreamService() = default;
~RestreamService() = default;
RestreamService(const RestreamService&) = delete;
RestreamService& operator=(const RestreamService&) = delete;
std::string getBaseUrl();
std::string getPushProxyUrl(); // Openresty proxy for push API (avoids URL encoding issues)
std::string getApiToken();
drogon::HttpClientPtr getClient();
drogon::HttpClientPtr getPushProxyClient(); // Client for push proxy
drogon::HttpRequestPtr createRequest(drogon::HttpMethod method, const std::string& path);
drogon::HttpRequestPtr createJsonRequest(drogon::HttpMethod method, const std::string& path,
const Json::Value& body);
// Generate a unique push ID for tracking
std::string generatePushId(const std::string& streamKey, int64_t destinationId);
// Update destination status in database
void updateDestinationStatus(int64_t destinationId, bool isConnected, const std::string& error);
// Track active pushes: streamKey -> (destinationId -> pushId)
std::unordered_map<std::string, std::unordered_map<int64_t, std::string>> activePushes_;
std::mutex pushMutex_;
};