1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
<?php
/*
Copyright 2001 - 2006 Ampache.org
All Rights Reserved
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* delete_now_playing
* This function checks to see if we should delete the last now playing entry now that it's
* finished streaming the song. Basicly this is an exception for WMP10
* @package General
* @catagory Now Playing
*/
function delete_now_playing($insert_id) {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (stristr($user_agent,"NSPlayer")) {
// Commented out until I can figure out the
// trick to making this work
return true;
}
// Remove the song from the now_playing table
$sql = "DELETE FROM now_playing WHERE id = '$insert_id'";
$db_result = mysql_query($sql, dbh());
} // delete_now_playing
/**
* gc_now_playing
* this is a garbage collection function for now playing this is called every time something
* is streamed
* @package General
* @catagory Now Playing
*/
function gc_now_playing() {
$time = time();
$expire = $time - 3200; // 86400 seconds = 1 day
$session_id = sql_escape($_REQUEST['sid']);
if (strlen($session_id)) {
$session_sql = " OR session = '$session_id'";
}
$sql = "DELETE FROM now_playing WHERE start_time < $expire" . $session_sql;
$db_result = mysql_query($sql, dbh());
} // gc_now_playing
/**
* insert_now_playing
* This function takes care of inserting the now playing data
* we use this function because we need to do thing differently
* depending upon which play is actually streaming
* @package General
* @catagory Now Playing
*/
function insert_now_playing($song_id,$uid,$song_length) {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$time = time();
/* Windows Media Player is evil and it makes multiple requests per song */
if (stristr($user_agent,"Windows-Media-Player")) { return false; }
/* Set the Expire Time */
// If they are using Windows media player
if (stristr($user_agent,"NSPlayer")) {
// WMP does keep the session open so we need to cheat a little here
$session_id = sql_escape($_REQUEST['sid']);
}
/* Set expire time for worst case clean up */
$expire = $time;
$sql = "INSERT INTO now_playing (`song_id`, `user`, `start_time`,`session`)" .
" VALUES ('$song_id', '$uid', '$expire','$session_id')";
$db_result = mysql_query($sql, dbh());
$insert_id = mysql_insert_id(dbh());
return $insert_id;
} // insert_now_playing
/**
* check_lock_songs
* This checks to see if the song is already playing, if it is then it prevents the user
* from streaming it
* @package General
* @catagory Security
*/
function check_lock_songs($song_id) {
$sql = "SELECT song_id FROM now_playing " .
"WHERE song_id = '$song_id'";
$db_results = mysql_query($sql, dbh());
if (mysql_num_rows($db_results)) {
return false;
}
return true;
} // check_lock_songs
/**
* start_downsample
* This is a rather complext function that starts the downsampling of a song and returns the
* opened file handled
* @package General
* @catagory Downsample
* @param $song The Song Object
*/
function start_downsample($song,$now_playing_id=0,$song_name=0) {
/**
* Extra check, for now hardcode it to mp3 but if
* we are transcoding we need to fix the mime type
* and let the user define what file type it's switching
* to.
*/
if (!$song->native_stream()) {
$song->mime = 'audio/mpeg';
$song_name = $song->f_artist_full . " - " . $song->title . ".mp3";
}
/* Check to see if bitrates are set if so let's go ahead and optomize! */
$max_bitrate = conf('max_bit_rate');
$min_bitrate = conf('min_bit_rate');
$time = time();
$dbh = dbh();
$user_sample_rate = $GLOBALS['user']->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.
$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_results);
// Current number of active streams (current is already in now playing)
$active_streams = $results[0];
/* 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 */
debug_event('downsample',"Error: Max bandwidith already allocated. $active_streams Active Streams",'2');
/* 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
// Never go over the users sample rate
if ($sample_rate > $user_sample_rate) { $sample_rate = $user_sample_rate; }
debug_event('downsample',"Downsampled: $active_streams current active streams, downsampling to $sample_rate",'2');
} // end if we've got bitrates
else {
$sample_rate = $user_sample_rate;
}
/* Validate the bitrate */
$sample_rate = validate_bitrate($sample_rate);
/* Never Upsample a song */
if (($sample_rate*1000) > $song->bitrate) {
$sample_rate = validate_bitrate($song->bitrate)/1000;
$sample_ratio = '1';
}
else {
/* Set the Sample Ratio */
$sample_ratio = $sample_rate/($song->bitrate/1000);
}
header("Content-Length: " . $sample_ratio*$song->size);
$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);
$song_file = escapeshellarg($song->file);
/* Replace Variables */
$downsample_command = conf($song->stream_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
$message = "Start Downsample: $downsample_command";
debug_event('downsample',$message,'3');
$fp = @popen($downsample_command, 'rb');
/* We need more than just the handle here */
$return_array['handle'] = $fp;
$return_array['size'] = $sample_ration*$song->size;
return ($return_array);
} // start_downsample
/**
* validate_bitrate
* this function takes a bitrate and returns a valid one
* @package Stream
* @catagory Downsample
*/
function validate_bitrate($bitrate) {
// Setup an array of valid bitrates for Lame (yea yea, others might be different :P)
$valid_rate = array('32','40','56','64','80','96','112','128','160','192','224','256','320');
/* Round to standard bitrates */
$sample_rate = 8*(floor($bitrate/8));
if (in_array($sample_rate,$valid_rate)) {
return $sample_rate;
}
/* See if it's less than the lowest one */
if ($sample_rate < $valid_rate['0']) {
return $valid_rate['0'];
}
/* Check to see if it's over 320 */
if ($sample_rate > 320) {
return '320';
}
foreach ($valid_rate as $key=>$rate) {
$next_key = $key+1;
if ($sample_rate > $rate AND $sample_rate < $valid_rate[$next_key]) {
return $rate;
}
} // end foreach
} // validate_bitrate
?>
|