Skip to content

Commit 99eab75

Browse files
author
bretello
committed
Added settings for HTTP audio stream, based on anthonygclark's code (notandy#123)
1 parent 88b2aa7 commit 99eab75

File tree

2 files changed

+148
-12
lines changed

2 files changed

+148
-12
lines changed

htdocs/index.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,22 @@ <h6><a href="http://www.musicpd.org/libs/libmpdclient/">libMPDClient</a> <small>
232232
</div>
233233
<div class="form-group col-md-3">
234234
<label class="control-label" for="mpdport">MPD Port</label>
235-
<input type="text" class="form-control" id="mpdport" />
235+
<input type="number" class="form-control" id="mpdport" />
236+
</div>
237+
</div>
238+
<div class="row" id="audiostreamcheckbox">
239+
<div class="form-group col-md-3">
240+
<input type="checkbox" name="audiostreamenabled" id="audiostreamenabled"> HTTP Audio Stream<br>
241+
</div>
242+
</div >
243+
<div class="row" id="audiostreamsettings" hidden>
244+
<div class="form-group col-md-9">
245+
<label class="control-label" for="audiostreamhost">Audio stream Host/IP</label>
246+
<input type="text" class="form-control" id="audiostreamhost" />
247+
</div>
248+
<div class="form-group col-md-3">
249+
<label class="control-label" for="audiostreamport">Stream Port</label>
250+
<input type="number" class="form-control" id="audiostreamport" />
236251
</div>
237252
</div>
238253
<div class="row">

htdocs/js/mpd.js

Lines changed: 132 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var isTouch = Modernizr.touch ? 1 : 0;
3232
var filter = undefined;
3333
var dirble_api_token = "";
3434
var dirble_stations = false;
35+
var hostname;
36+
var httpAudioStreamEnabled;
37+
var httpAudioStream;
3538

3639
var app = $.sammy(function() {
3740

@@ -170,6 +173,7 @@ var app = $.sammy(function() {
170173
});
171174

172175
$(document).ready(function(){
176+
checkSettings();
173177
webSocketConnect();
174178
$("#volumeslider").slider(0);
175179
$("#volumeslider").on('slider.newValue', function(evt,data){
@@ -199,6 +203,64 @@ $(document).ready(function(){
199203
add_filter();
200204
});
201205

206+
function checkSettings()
207+
{
208+
httpAudioStreamEnabled = $.cookie("httpAudioStreamEnabled");
209+
if ( typeof httpAudioStreamEnabled == "undefined" ){
210+
// Fall back to default value (disabled)
211+
httpAudioStreamEnabled = false;
212+
$.cookie("httpAudioStreamEnabled", httpAudioStreamEnabled);
213+
} else if ( httpAudioStreamEnabled ){
214+
var host;
215+
var port;
216+
217+
try // Try retrieving stream host and port from cookies, fall back to default value if undefined
218+
{
219+
var src = $.cookie("httpAudioStreamSrc").split("://")[1];
220+
host = src.split(":")[0];
221+
port = src.split(":")[1];
222+
if (typeof host == "undefined" || host.length == 0){
223+
host = window.location.hostname;
224+
}
225+
if (typeof port == "undefined" || (port < 0 || port > 65535)){
226+
port = 5443
227+
}
228+
} catch(err) // Fallback to default values
229+
{
230+
host = window.location.hostname;
231+
port = 5443;
232+
}
233+
234+
if (httpAudioStreamEnabled){
235+
$('#audiostreamhost').val(host);
236+
$('#audiostreamport').val(port);
237+
}
238+
}
239+
}
240+
241+
function createHTTPAudioStream()
242+
{
243+
/* This creates the HTTP audio stream element
244+
* I'm not sure if this is portable. Seems to work in new Chrome and FF.
245+
* This also assumes the ympd server is the same as the stream.
246+
*
247+
* The httpAudioStreamPort can be added in the settings. Ideally,
248+
* this should be autodetected.
249+
*/
250+
251+
httpAudioStream = document.createElement('audio');
252+
httpAudioStream.id = "audiostream";
253+
var src = $.cookie("httpAudioStreamSrc");
254+
if ( src ){
255+
httpAudioStream.src = src;
256+
} else {
257+
// Default values
258+
httpAudioStream.src = window.location.protocol + "//" + window.location.hostname + ":5443";
259+
// Save these values to a cookie
260+
$.cookie("httpAudioStreamSrc", httpAudioStream.src);
261+
}
262+
console.log('Created audio stream: ' + httpAudioStream.src);
263+
}
202264

203265
function webSocketConnect() {
204266
if (typeof MozWebSocket != "undefined") {
@@ -216,8 +278,20 @@ function webSocketConnect() {
216278
}).show();
217279

218280
app.run();
281+
219282
/* emit initial request for output names */
220283
socket.send("MPD_API_GET_OUTPUTS");
284+
285+
/* add the http stream */
286+
if (httpAudioStreamEnabled) { createHTTPAudioStream(); }
287+
288+
/* Populate the form values.
289+
* Without this, e.g., mpdhost.value. does not have a value
290+
* until you enter the Settings modal. I'm not sure if this is intended.
291+
*
292+
* This can be removed since I'm not using it (Anthony Clark)
293+
*/
294+
getHost();
221295
}
222296

223297
socket.onmessage = function got_packet(msg) {
@@ -238,7 +312,7 @@ function webSocketConnect() {
238312

239313
$('#salamisandwich > tbody').append(
240314
"<tr trackid=\"" + obj.data[song].id + "\"><td>" + (obj.data[song].pos + 1) + "</td>" +
241-
"<td>"+ obj.data[song].title +"</td>" +
315+
"<td>"+ obj.data[song].title +"</td>" +
242316
"<td>"+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +
243317
"</td><td></td></tr>");
244318
}
@@ -367,8 +441,8 @@ function webSocketConnect() {
367441
$('#next').removeClass('hide');
368442
} else {
369443
$('#salamisandwich > tbody').append(
370-
"<tr><td><span class=\"glyphicon glyphicon-remove\"></span></td>" +
371-
"<td>Too many results, please refine your search!</td>" +
444+
"<tr><td><span class=\"glyphicon glyphicon-remove\"></span></td>" +
445+
"<td>Too many results, please refine your search!</td>" +
372446
"<td></td><td></td></tr>"
373447
);
374448
}
@@ -400,7 +474,7 @@ function webSocketConnect() {
400474
} else {
401475
$('#salamisandwich > tbody > tr').on({
402476
mouseenter: function() {
403-
if($(this).is(".dir"))
477+
if($(this).is(".dir"))
404478
appendClickableIcon($(this).children().last(), 'MPD_API_ADD_TRACK', 'plus');
405479
else if($(this).is(".song"))
406480
appendClickableIcon($(this).children().last(), 'MPD_API_ADD_PLAY_TRACK', 'play');
@@ -460,7 +534,7 @@ function webSocketConnect() {
460534
$('#progressbar').slider(progress);
461535

462536
$('#counter')
463-
.text(elapsed_minutes + ":" +
537+
.text(elapsed_minutes + ":" +
464538
(elapsed_seconds < 10 ? '0' : '') + elapsed_seconds + " / " +
465539
total_minutes + ":" + (total_seconds < 10 ? '0' : '') + total_seconds);
466540

@@ -552,7 +626,7 @@ function webSocketConnect() {
552626
message:{html: notification},
553627
type: "info",
554628
}).show();
555-
629+
556630
break;
557631
case "mpdhost":
558632
$('#mpdhost').val(obj.data.host);
@@ -584,7 +658,7 @@ function webSocketConnect() {
584658
console.log("disconnected");
585659
$('.top-right').notify({
586660
message:{text:"Connection to ympd lost, retrying in 3 seconds "},
587-
type: "danger",
661+
type: "danger",
588662
onClose: function () {
589663
webSocketConnect();
590664
}
@@ -657,9 +731,11 @@ var updatePlayIcon = function(state)
657731
} else if(state == 2) { // pause
658732
$("#play-icon").addClass("glyphicon-pause");
659733
$('#track-icon').addClass("glyphicon-play");
734+
if (httpAudioStreamEnabled) { httpAudioStream.play(); }
660735
} else { // play
661736
$("#play-icon").addClass("glyphicon-play");
662737
$('#track-icon').addClass("glyphicon-pause");
738+
if (httpAudioStreamEnabled) { httpAudioStream.pause(); }
663739
}
664740
}
665741

@@ -671,10 +747,12 @@ function updateDB() {
671747
}
672748

673749
function clickPlay() {
674-
if($('#track-icon').hasClass('glyphicon-stop'))
750+
if($('#track-icon').hasClass('glyphicon-stop')) {
675751
socket.send('MPD_API_SET_PLAY');
676-
else
752+
}
753+
else {
677754
socket.send('MPD_API_SET_PAUSE');
755+
}
678756
}
679757

680758
function trash(tr) {
@@ -732,7 +810,7 @@ $('#btnrepeat').on('click', function (e) {
732810

733811
function toggleoutput(button, id) {
734812
socket.send("MPD_API_TOGGLE_OUTPUT,"+id+"," + ($(button).hasClass('active') ? 0 : 1));
735-
}
813+
}
736814

737815
$('#trashmode').children("button").on('click', function(e) {
738816
$('#trashmode').children("button").removeClass("active");
@@ -764,9 +842,14 @@ function getHost() {
764842
confirmSettings();
765843
}
766844
}
767-
845+
768846
$('#mpdhost').keypress(onEnter);
769847
$('#mpdport').keypress(onEnter);
848+
if (httpAudioStreamEnabled && !$('#audiostreamenabled').is(":checked")){
849+
$('#audiostreamenabled').click();
850+
}
851+
$('#audiostreamhost').keypress(onEnter);
852+
$('#audiostreamport').keypress(onEnter);
770853
$('#mpd_pw').keypress(onEnter);
771854
$('#mpd_pw_con').keypress(onEnter);
772855
}
@@ -784,6 +867,14 @@ $('#search').submit(function () {
784867
return false;
785868
});
786869

870+
$('#audiostreamenabled').click(function(){
871+
$('#audiostreamsettings').toggle();
872+
if ($('#audiostreamenabled').is(":checked") && !$("#audiostreamhost").attr("value")){
873+
$("#audiostreamhost").attr("placeholder", window.location.hostname);
874+
$("#audiostreamport").attr("placeholder", 5443);
875+
}
876+
});
877+
787878
$('.page-btn').on('click', function (e) {
788879

789880
switch ($(this).text()) {
@@ -843,6 +934,36 @@ function confirmSettings() {
843934
socket.send('MPD_API_SET_MPDPASS,'+$('#mpd_pw').val());
844935
}
845936
socket.send('MPD_API_SET_MPDHOST,'+$('#mpdport').val()+','+$('#mpdhost').val());
937+
938+
httpAudioStreamEnabled = $('#audiostreamenabled').is(":checked");
939+
$.cookie("httpAudioStreamEnabled", httpAudioStreamEnabled);
940+
if (httpAudioStreamEnabled)
941+
{
942+
var host=$('#audiostreamhost').val();
943+
var port=$('#audiostreamport').val();
944+
945+
// Fallback to default values if there are bad values
946+
if (typeof host == "undefined" || host.length == 0){
947+
host = window.location.hostname;
948+
console.log("[HTTP Audio Streaming] fallback to defalt host: " + host);
949+
}
950+
if (typeof port == "undefined" || (port < 0 || port > 65535) || port.length == 0) {
951+
port=5443;
952+
console.log("[HTTP Audio Streaming] fallback to default port: " + port);
953+
}
954+
955+
// Set audio source
956+
var audioSrc = window.location.protocol + "//" + host + ':' + port;
957+
// Save streaming URL in a cookie
958+
$.cookie("httpAudioStreamSrc", audioSrc);
959+
// Set the streaming URL, create it if it does not exist
960+
if (httpAudioStream){
961+
httpAudioStream.src = audioSrc;
962+
} else {
963+
createHTTPAudioStream();
964+
}
965+
}
966+
846967
$('#settings').modal('hide');
847968
}
848969

0 commit comments

Comments
 (0)