Skip to content

Commit 383763d

Browse files
author
Daniel Neto
committed
Refactor resolution handling to include streamer ID for user-specific encoding
1 parent 5f0d67e commit 383763d

File tree

4 files changed

+101
-29
lines changed

4 files changed

+101
-29
lines changed

objects/Format.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,13 @@ public static function getAvailableResolutionsInfo()
562562
* Shows only the resolutions that will actually be encoded for the current user
563563
* @return array Array of resolution info considering user restrictions
564564
*/
565-
public static function getAvailableResolutionsInfoForUser()
565+
public static function getAvailableResolutionsInfoForUser($streamersId)
566566
{
567567
$resolutions = [];
568568
$availableResolutions = Format::getAvailableResolutions();
569569

570570
// Get the final resolutions that will be used for encoding (with user restrictions)
571-
$finalResolutions = self::getSelectedResolutionsWithUserRestrictions();
571+
$finalResolutions = self::getSelectedResolutionsWithUserRestrictions($streamersId);
572572

573573
foreach ($availableResolutions as $key => $resolution) {
574574
// Check if this resolution will actually be encoded for the current user
@@ -582,7 +582,7 @@ public static function getAvailableResolutionsInfoForUser()
582582

583583
// Different color based on user permissions
584584
require_once __DIR__ . '/Login.php';
585-
$userAllowedResolutions = Login::getAllowedResolutions();
585+
$userAllowedResolutions = Login::getAllowedResolutions($streamersId);
586586

587587
if (empty($userAllowedResolutions) || in_array($resolution, $userAllowedResolutions)) {
588588
$label .= "success"; // Green for allowed resolutions
@@ -703,14 +703,14 @@ private static function getFinalResolutions($userAllowedResolutions, $encoderEna
703703
* Get selected resolutions with user group restrictions applied
704704
* @return array Final resolutions to use for encoding
705705
*/
706-
private static function getSelectedResolutionsWithUserRestrictions()
706+
private static function getSelectedResolutionsWithUserRestrictions($streamersId)
707707
{
708708
// Get encoder configured resolutions
709709
$encoderResolutions = self::getSelectedResolutions();
710710

711711
// Get user allowed resolutions from login session
712712
require_once __DIR__ . '/Login.php';
713-
$userAllowedResolutions = Login::getAllowedResolutions();
713+
$userAllowedResolutions = Login::getAllowedResolutions($streamersId);
714714

715715
// Apply user restrictions
716716
$finalResolutions = self::getFinalResolutions($userAllowedResolutions, $encoderResolutions);
@@ -720,7 +720,7 @@ private static function getSelectedResolutionsWithUserRestrictions()
720720
return $finalResolutions;
721721
}
722722

723-
static function loadEncoderConfiguration()
723+
static function loadEncoderConfiguration($streamersId)
724724
{
725725
$availableConfiguration = self::getAvailableConfigurations();
726726

@@ -730,7 +730,7 @@ static function loadEncoderConfiguration()
730730
$videoFramerate = [];
731731

732732
// Use the new method that applies user group restrictions
733-
$selectedResolutions = self::getSelectedResolutionsWithUserRestrictions();
733+
$selectedResolutions = self::getSelectedResolutionsWithUserRestrictions($streamersId);
734734

735735
sort($selectedResolutions);
736736

@@ -764,7 +764,10 @@ private static function getDynamicCommandFromFormat($pathFileName, $encoder_queu
764764
//$audioTracks = self::getAudioTracks($pathFileName);
765765
$advancedCustom = getAdvancedCustomizedObjectData();
766766

767-
$encoderConfig = self::loadEncoderConfiguration();
767+
$encoder = new Encoder($encoder_queue_id);
768+
$streamersId = $encoder->getStreamers_id();
769+
770+
$encoderConfig = self::loadEncoderConfiguration($streamersId);
768771
$resolutions = $encoderConfig['resolutions'];
769772
$bandwidth = $encoderConfig['bandwidth'];
770773
$videoFramerate = $encoderConfig['videoFramerate'];
@@ -833,12 +836,14 @@ private static function getDynamicCommandFromFormat($pathFileName, $encoder_queu
833836
return $command;
834837
}
835838

836-
private static function preProcessDynamicHLS($pathFileName, $destinationFile)
839+
private static function preProcessDynamicHLS($pathFileName, $destinationFile, $encoder_queue_id)
837840
{
841+
$encoder = new Encoder($encoder_queue_id);
842+
$streamersId = $encoder->getStreamers_id();
838843
$height = self::getResolution($pathFileName);
839844
//$audioTracks = self::getAudioTracks($pathFileName);
840845
// TODO: This method should be refactored to use loadEncoderConfiguration instead of getAvailableConfigurations...
841-
$encoderConfig = self::loadEncoderConfiguration();
846+
$encoderConfig = self::loadEncoderConfiguration($streamersId);
842847
$resolutions = $encoderConfig['resolutions'];
843848
$bandwidth = $encoderConfig['bandwidth'];
844849
$audioBitrate = $encoderConfig['audioBitrate'];
@@ -1009,11 +1014,11 @@ private static function exec($format_id, $pathFileName, $destinationFile, $encod
10091014
if (empty($fc) || $format_id == 30) {
10101015
if (empty($global['disableHLSAudioMultitrack'])) {
10111016
_error_log("AVideo-Encoder Format::exec use HLSProcessor");
1012-
$dynamic = HLSProcessor::createHLSWithAudioTracks($pathFileName, $destinationFile);
1017+
$dynamic = HLSProcessor::createHLSWithAudioTracks($pathFileName, $destinationFile, $encoder_queue_id);
10131018
_error_log("AVideo-Encoder Format::exec use HLSProcessor Complete");
10141019
} else {
10151020
_error_log("AVideo-Encoder Format::exec disableHLSAudioMultitrack");
1016-
$dynamic = self::preProcessDynamicHLS($pathFileName, $destinationFile);
1021+
$dynamic = self::preProcessDynamicHLS($pathFileName, $destinationFile, $encoder_queue_id);
10171022
}
10181023
$destinationFile = $dynamic[0];
10191024
$fc = $dynamic[1];

objects/HLSProcessor.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ static function createMP3AndPM4IfNeed($pathFileName, $destinationFile)
2626
}
2727
}
2828

29-
public static function createHLSWithAudioTracks($pathFileName, $destinationFile)
29+
public static function createHLSWithAudioTracks($pathFileName, $destinationFile, $encoder_queue_id)
3030
{
31+
$encoder = new Encoder($encoder_queue_id);
32+
$streamersId = $encoder->getStreamers_id();
3133
// Detect video resolution and audio tracks
3234
$resolution = self::getResolution($pathFileName);
3335
$audioTracks = self::getAudioTracks($pathFileName); // Detect audio tracks
34-
$encoderConfig = Format::loadEncoderConfiguration();
36+
$encoderConfig = Format::loadEncoderConfiguration($streamersId);
3537
$resolutions = $encoderConfig['resolutions'];
3638
$bandwidth = $encoderConfig['bandwidth'];
3739
$videoFramerate = $encoderConfig['videoFramerate'];
@@ -137,22 +139,25 @@ public static function createHLSWithAudioTracks($pathFileName, $destinationFile)
137139

138140
if (empty($resolutionsFound)) {
139141
_error_log("HLSProcessor: createHLSWithAudioTracks Resolution found is empty");
140-
// did not find any resolution, process the default one
141-
$encodingSettings = Format::ENCODING_SETTINGS[480];
142+
// Use the lowest allowed resolution for the user as fallback
143+
$fallbackResolution = !empty($resolutions) ? min($resolutions) : 240;
144+
_error_log("HLSProcessor: createHLSWithAudioTracks Using fallback resolution: {$fallbackResolution}p (user allowed resolutions: " . json_encode($resolutions) . ")");
145+
146+
$encodingSettings = Format::ENCODING_SETTINGS[$fallbackResolution];
142147
$rate = $encodingSettings['maxrate']; // Use the maxrate from ENCODING_SETTINGS
143-
$framerate = isset($videoFramerate[$key]) && $videoFramerate[$key] > 0 ? $videoFramerate[$key] : 30;
144-
$dir = $destinationFile . "res{$resolution}/";
148+
$framerate = isset($videoFramerate[0]) && $videoFramerate[0] > 0 ? $videoFramerate[0] : 30;
149+
$dir = $destinationFile . "res{$fallbackResolution}/";
145150
mkdir($dir);
146151
$outputFile = "{$dir}index.m3u8";
147152

148153
// Add resolution playlist entry to the master playlist
149-
$width = self::getScaledWidth($pathFileName, $resolution);
150-
$masterPlaylist .= "#EXT-X-STREAM-INF:BANDWIDTH=" . ($rate * 1000) . ",RESOLUTION={$width}x{$resolution},AUDIO=\"audio_group\"" . PHP_EOL;
154+
$width = self::getScaledWidth($pathFileName, $fallbackResolution);
155+
$masterPlaylist .= "#EXT-X-STREAM-INF:BANDWIDTH=" . ($rate * 1000) . ",RESOLUTION={$width}x{$fallbackResolution},AUDIO=\"audio_group\"" . PHP_EOL;
151156

152-
$masterPlaylist .= "res{$resolution}/index.m3u8" . PHP_EOL;
157+
$masterPlaylist .= "res{$fallbackResolution}/index.m3u8" . PHP_EOL;
153158

154159
// Append FFmpeg command for this resolution
155-
$ffmpegCommand .= self::getFFmpegCommandForResolution($pathFileName, $resolution, $rate, $framerate, $audioTracks, $keyInfoFile, $outputFile);
160+
$ffmpegCommand .= self::getFFmpegCommandForResolution($pathFileName, $fallbackResolution, $rate, $framerate, $audioTracks, $keyInfoFile, $outputFile);
156161

157162
$resolutionsFound++;
158163
}

objects/Login.php

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,81 @@ static function getStreamerId() {
250250
* Get allowed resolutions for the logged user from login session
251251
* @return array|null Array of allowed resolutions or null if not set
252252
*/
253-
static function getAllowedResolutions() {
253+
static function getAllowedResolutions($streamersId) {
254+
_error_log("Login::getAllowedResolutions - isLogged: " . json_encode(static::isLogged()));
255+
_error_log("Login::getAllowedResolutions - Session login data: " . json_encode($_SESSION['login'] ?? 'no session'));
256+
257+
// First, check if we have a logged user with valid allowedResolutions
258+
if (static::isLogged() && !empty($_SESSION['login']->allowedResolutions) && is_array($_SESSION['login']->allowedResolutions)) {
259+
_error_log("Login::getAllowedResolutions - Found allowedResolutions: " . json_encode($_SESSION['login']->allowedResolutions));
260+
return $_SESSION['login']->allowedResolutions;
261+
}
262+
263+
// If user is not logged in OR allowedResolutions is null/empty, try to re-login using database streamer credentials
254264
if (!static::isLogged()) {
255-
return null;
265+
_error_log("Login::getAllowedResolutions - User not logged in, attempting re-login with database credentials");
266+
} else {
267+
_error_log("Login::getAllowedResolutions - allowedResolutions is null, attempting re-login with database credentials");
256268
}
257269

258-
// Check if allowedResolutions exists in session
259-
if (!empty($_SESSION['login']->allowedResolutions) && is_array($_SESSION['login']->allowedResolutions)) {
260-
return $_SESSION['login']->allowedResolutions;
270+
if (self::retryLoginWithDatabaseCredentials($streamersId)) {
271+
// After re-login, check again for allowedResolutions
272+
if (static::isLogged() && !empty($_SESSION['login']->allowedResolutions) && is_array($_SESSION['login']->allowedResolutions)) {
273+
_error_log("Login::getAllowedResolutions - Found allowedResolutions after re-login: " . json_encode($_SESSION['login']->allowedResolutions));
274+
return $_SESSION['login']->allowedResolutions;
275+
}
261276
}
262277

278+
_error_log("Login::getAllowedResolutions - No allowedResolutions found even after re-login attempt");
263279
return null;
264280
}
265281

266-
}
282+
/**
283+
* Attempt to re-login using database streamer credentials
284+
* @return bool True if re-login was successful, false otherwise
285+
*/
286+
private static function retryLoginWithDatabaseCredentials($streamersId) {
287+
try {
288+
if (!$streamersId) {
289+
_error_log("Login::retryLoginWithDatabaseCredentials - No streamers_id found");
290+
return false;
291+
}
292+
293+
// Load streamer from database
294+
require_once __DIR__ . '/Streamer.php';
295+
$streamer = new Streamer($streamersId);
296+
if (!$streamer->getId()) {
297+
_error_log("Login::retryLoginWithDatabaseCredentials - Streamer not found in database: $streamersId");
298+
return false;
299+
}
300+
301+
$user = $streamer->getUser();
302+
$pass = $streamer->getPass(); // This should be the encoded password
303+
$streamerURL = $streamer->getStreamerURL();
304+
305+
if (empty($user) || empty($pass) || empty($streamerURL)) {
306+
_error_log("Login::retryLoginWithDatabaseCredentials - Missing credentials: user=$user, pass=" . (empty($pass) ? 'empty' : 'set') . ", url=$streamerURL");
307+
return false;
308+
}
309+
310+
_error_log("Login::retryLoginWithDatabaseCredentials - Attempting re-login with user: $user, url: $streamerURL");
311+
312+
// Perform login with encoded password
313+
self::run($user, $pass, $streamerURL, true);
314+
315+
// Check if login was successful and we now have allowedResolutions
316+
if (self::isLogged() && isset($_SESSION['login']->allowedResolutions)) {
317+
_error_log("Login::retryLoginWithDatabaseCredentials - Re-login successful");
318+
return true;
319+
}
320+
321+
_error_log("Login::retryLoginWithDatabaseCredentials - Re-login failed or no allowedResolutions returned");
322+
return false;
323+
324+
} catch (Exception $e) {
325+
_error_log("Login::retryLoginWithDatabaseCredentials - Exception: " . $e->getMessage());
326+
return false;
327+
}
328+
} }
267329

268330
}

view/index_shareVideos.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
<div class="availableResolutionsLabels">
133133
<?php
134134
// Show resolutions that will actually be encoded for this user
135-
$resolutionsInfo = Format::getAvailableResolutionsInfoForUser();
135+
$resolutionsInfo = Format::getAvailableResolutionsInfoForUser(Login::getStreamerId());
136136

137137
if (!empty($resolutionsInfo)) {
138138
foreach ($resolutionsInfo as $value) {

0 commit comments

Comments
 (0)