From 0a3df5cbcfc10a0f18e9da311d887eb00fd6fbf1 Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Fri, 15 Jul 2005 06:50:58 +0000 Subject: added on the fly bandwidth management for downsampled users --- lib/stream.lib.php | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 lib/stream.lib.php (limited to 'lib/stream.lib.php') diff --git a/lib/stream.lib.php b/lib/stream.lib.php new file mode 100644 index 00000000..a022b6da --- /dev/null +++ b/lib/stream.lib.php @@ -0,0 +1,249 @@ +prefs['sample_rate']; + $browser = new Browser(); + + if (!$song_name) { + $song_name = $song->f_artist_full . " - " . $song->title . "." . $song->type; + } + + if ($max_bitrate > 1 AND $min_bitrate < $max_bitrate) { + $last_seen_time = $time - 1200; //20 min. + + /********************************** + * Commenting out the following, I'd rather have it slightly less accurate and avoid a 4 table join + // Count the users connected in the last 20 min using downsampling + $sql = "SELECT COUNT(DISTINCT session.username) FROM session,user,user_preference,preferences " . + "WHERE user.username = session.username AND session.expire > '$time' AND user.last_seen > $last_seen_time " . + "AND preferences.name = 'play_type' AND user_preference.preference = preferences.id AND " . + "user_preference.user = user.username AND user_preference.value='downsample'"; + $db_result = mysql_query($sql, $dbh); + $results = mysql_fetch_row($db_result); + + // The current number of connected users + $current_users_count = $results[0]; + ************************************/ + + + $sql = "SELECT COUNT(*) FROM now_playing, user_preference, preferences" . + "WHERE preferences.name = 'play_type' AND user_preference.preference = preferences.id " . + "AND now_playing.user = user_preference.user AND user_preference.value='downsample'"; + $db_results = mysql_query($sql,$dbh); + $results = mysql_fetch_row($db_result); + + // Current number of active streams + 1 (the one we are starting) + $active_streams = $results[0] + 1; + + + /* If only one user, they'll get all available. Otherwise split up equally. */ + $sample_rate = floor($max_bitrate/$active_streams); + + /* If min_bitrate is set, then we'll exit if the bandwidth would need to be split up smaller than the min. */ + if ($min_bitrate > 1 AND ($max_bitrate/$active_streams) < $min_bitrate) { + + /* Log the failure */ + if (conf('debug')) { + log_event($user->username,' downsample ',"Error: Max bandwidith already allocated. $active_streams Active Streams"); + } + + /* Toast the now playing entry, then tell em to try again later */ + delete_now_playing($now_playing_id); + echo "Maximum bandwidth already allocated. Try again later."; + exit(); + + } + else { + $sample_rate = floor($max_bitrate/$active_streams); + } // end else + + /* Round to standard bitrates */ + $sample_rate = 8*(floor($sample_rate/8)); + + // Never go over the users sample rate + if ($sample_rate > $user_sample_rate) { $sample_rate = $user_sample_rate; } + + if (conf('debug')) { + log_event($GLOBALS['user']->username, ' downsample ',"Downsampled: $active_streams current active streams, downsampling to $sample_rate"); + } + + } // end if we've got bitrates + + else { + $sample_rate = $user_sample_rate; + } + + /* Never Upsample a song */ + if (($sample_rate*1000) > $song->bitrate) { + unset($sample_rate); + } + + $sample_ratio = $sample_rate/($song->bitrate/1000); + + $browser->downloadHeaders($song_name, $song->mime, false,$sample_ratio*$song->size); + + /* Get Offset */ + $offset = ( $start*$song->time )/( $sample_ratio*$song->size ); + $offsetmm = floor($offset/60); + $offsetss = floor($offset-$offsetmm*60); + $offset = sprintf("%02d.%02d",$offsetmm,$offsetss); + + /* Get EOF */ + $eofmm = floor($song->time/60); + $eofss = floor($song->time-$eofmm*60); + $eof = sprintf("%02d.%02d",$eofmm,$eofss); + + /* Replace Variables */ + $downsample_command = conf('downsample_cmd'); + $downsample_command = str_replace("%FILE%",$song->file,$downsample_command); + $downsample_command = str_replace("%OFFSET%",$offset,$downsample_command); + $downsample_command = str_replace("%EOF%",$eof,$downsample_command); + $downsample_command = str_replace("%SAMPLE%",$sample_rate,$downsample_command); + + // If we are debugging log this event + if (conf('debug')) { + $message = "Start Downsample: $downsample_command"; + log_event($GLOBALS['user']->username,' downsample ',$message); + } // if debug + + $fp = @popen($downsample_command, 'r'); + + return ($fp); + +} // start_downsample + +?> -- cgit