@ -41,6 +41,7 @@ import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMethod ;
import javax.annotation.Nullable ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
@ -52,8 +53,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern ;
/ * *
* A controller which streams the content of a { @link PlayQueue } to a remote
* { @link Player } .
* A controller which streams the content of a { @link PlayQueue } to a remote { @link Player } .
*
* @author Sindre Mehus
* /
@ -94,7 +94,8 @@ public class StreamController {
try {
if ( ! ( authentication instanceof JWTAuthenticationToken ) & & ! user . isStreamRole ( ) ) {
response . sendError ( HttpServletResponse . SC_FORBIDDEN , "Streaming is forbidden for user " + user . getUsername ( ) ) ;
response . sendError ( HttpServletResponse . SC_FORBIDDEN ,
"Streaming is forbidden for user " + user . getUsername ( ) ) ;
return ;
}
@ -132,7 +133,8 @@ public class StreamController {
if ( isSingleFile ) {
if ( ! ( authentication instanceof JWTAuthenticationToken ) & & ! securityService . isFolderAccessAllowed ( file , user . getUsername ( ) ) ) {
if ( ! ( authentication instanceof JWTAuthenticationToken ) & & ! securityService . isFolderAccessAllowed ( file ,
user . getUsername ( ) ) ) {
response . sendError ( HttpServletResponse . SC_FORBIDDEN ,
"Access to file " + file . getId ( ) + " is forbidden for user " + user . getUsername ( ) ) ;
return ;
@ -151,28 +153,48 @@ public class StreamController {
playQueue . addFiles ( true , file ) ;
player . setPlayQueue ( playQueue ) ;
if ( settingsService . isEnableSeek ( ) & & ! file . isVideo ( ) ) {
response . setIntHeader ( "ETag" , file . getId ( ) ) ;
response . setHeader ( "Accept-Ranges" , "bytes" ) ;
}
TranscodingService . Parameters parameters = transcodingService . getParameters ( file , player , maxBitRate , preferredTargetFormat , null ) ;
long fileLength = getFileLength ( parameters ) ;
TranscodingService . Parameters parameters = transcodingService . getParameters ( file , player , maxBitRate ,
preferredTargetFormat , null ) ;
boolean isConversion = parameters . isDownsample ( ) | | parameters . isTranscode ( ) ;
boolean estimateContentLength = ServletRequestUtils . getBooleanParameter ( request , "estimateContentLength" , false ) ;
boolean estimateContentLength = ServletRequestUtils . getBooleanParameter ( request ,
"estimateContentLength" , false ) ;
boolean isHls = ServletRequestUtils . getBooleanParameter ( request , "hls" , false ) ;
// Wrangle response length and ranges.
//
// Support ranges as long as we're not transcoding; video is always assumed to transcode
if ( isConversion | | file . isVideo ( ) ) {
// Use chunked transfer; do not accept range requests
response . setStatus ( HttpServletResponse . SC_OK ) ;
response . setHeader ( "Accept-Ranges" , "none" ) ;
} else {
// Not transcoding, partial content permitted because we know the final size
long contentLength ;
// If range was requested, respond in kind
range = getRange ( request , file ) ;
if ( settingsService . isEnableSeek ( ) & & range ! = null & & ! file . isVideo ( ) ) {
LOG . info ( "{}: Got HTTP range: {}" , request . getRemoteAddr ( ) , range ) ;
if ( range ! = null ) {
response . setStatus ( HttpServletResponse . SC_PARTIAL_CONTENT ) ;
Util . setContentLength ( response , range . isClosed ( ) ? range . size ( ) : fileLength - range . getFirstBytePos ( ) ) ;
long lastBytePos = range . getLastBytePos ( ) ! = null ? range . getLastBytePos ( ) : fileLength - 1 ;
response . setHeader ( "Content-Range" , "bytes " + range . getFirstBytePos ( ) + "-" + lastBytePos + "/" + fileLength ) ;
} else if ( ! isHls & & ( ! isConversion | | estimateContentLength ) ) {
Util . setContentLength ( response , fileLength ) ;
response . setHeader ( "Accept-Ranges" , "bytes" ) ;
// Both ends are inclusive
long startByte = range . getFirstBytePos ( ) ;
long endByte = range . isClosed ( ) ? range . getLastBytePos ( ) : file . getFileSize ( ) - 1 ;
response . setHeader ( "Content-Range" ,
String . format ( "bytes %d-%d/%d" , startByte , endByte , file . getFileSize ( ) ) ) ;
contentLength = endByte + 1 - startByte ;
} else {
// No range was requested, give back the whole file
response . setStatus ( HttpServletResponse . SC_OK ) ;
contentLength = file . getFileSize ( ) ;
}
response . setIntHeader ( "ETag" , file . getId ( ) ) ;
Util . setContentLength ( response , contentLength ) ;
}
// Set content type of response
if ( isHls ) {
response . setContentType ( StringUtil . getMimeType ( "ts" ) ) ; // HLS is always MPEG TS.
} else {
@ -187,6 +209,7 @@ public class StreamController {
}
}
// All headers are set, stop if that's all the client requested.
if ( request . getMethod ( ) . equals ( "HEAD" ) ) {
return ;
}
@ -202,32 +225,14 @@ public class StreamController {
status = statusService . createStreamStatus ( player ) ;
in = new PlayQueueInputStream ( player , status , maxBitRate , preferredTargetFormat , videoTranscodingSettings , transcodingService ,
audioScrobblerService , mediaFileService , searchService ) ;
OutputStream out = RangeOutputStream . wrap ( response . getOutputStream ( ) , range ) ;
// Enabled SHOUTcast, if requested.
boolean isShoutCastRequested = "1" . equals ( request . getHeader ( "icy-metadata" ) ) ;
if ( isShoutCastRequested & & ! isSingleFile ) {
response . setHeader ( "icy-metaint" , "" + ShoutCastOutputStream . META_DATA_INTERVAL ) ;
response . setHeader ( "icy-notice1" , "This stream is served using Airsonic" ) ;
response . setHeader ( "icy-notice2" , "Airsonic - Free media streamer" ) ;
response . setHeader ( "icy-name" , "Airsonic" ) ;
response . setHeader ( "icy-genre" , "Mixed" ) ;
response . setHeader ( "icy-url" , "https://airsonic.github.io/" ) ;
out = new ShoutCastOutputStream ( out , player . getPlayQueue ( ) , settingsService ) ;
}
in = new PlayQueueInputStream ( player , status , maxBitRate , preferredTargetFormat , videoTranscodingSettings ,
transcodingService , audioScrobblerService , mediaFileService , searchService ) ;
try ( OutputStream out = makeOutputStream ( request , response , range , isSingleFile , player , settingsService ) ) {
final int BUFFER_SIZE = 2048 ;
byte [ ] buf = new byte [ BUFFER_SIZE ] ;
while ( true ) {
// Check if stream has been terminated.
if ( status . terminated ( ) ) {
return ;
}
while ( ! status . terminated ( ) ) {
if ( player . getPlayQueue ( ) . getStatus ( ) = = PlayQueue . Status . STOPPED ) {
if ( isPodcast | | isSingleFile ) {
break ;
@ -248,6 +253,7 @@ public class StreamController {
}
}
}
}
} catch ( IOException e ) {
// This happens often and outside of the control of the server, so
@ -257,7 +263,10 @@ public class StreamController {
shouldCatch | = Util . isInstanceOfClassName ( e , "org.apache.catalina.connector.ClientAbortException" ) ;
shouldCatch | = Util . isInstanceOfClassName ( e , "org.eclipse.jetty.io.EofException" ) ;
if ( shouldCatch ) {
LOG . info ( "{}: Client unexpectedly closed connection while loading {} ({})" , request . getRemoteAddr ( ) , Util . getAnonymizedURLForRequest ( request ) , e . getCause ( ) . toString ( ) ) ;
LOG . info ( "{}: Client unexpectedly closed connection while loading {} ({})" ,
request . getRemoteAddr ( ) ,
Util . getAnonymizedURLForRequest ( request ) ,
e . getCause ( ) . toString ( ) ) ;
return ;
}
@ -274,6 +283,31 @@ public class StreamController {
return ;
}
/ * *
* Construct an appropriate output stream based on the request .
* < p >
* This is responsible for limiting the output to the given range ( if not null ) and injecting Shoutcast metadata
* into the stream if requested .
* /
private OutputStream makeOutputStream ( HttpServletRequest request , HttpServletResponse response , HttpRange range ,
boolean isSingleFile , Player player , SettingsService settingsService )
throws IOException {
OutputStream out = RangeOutputStream . wrap ( response . getOutputStream ( ) , range ) ;
// Enabled SHOUTcast, if requested.
boolean isShoutCastRequested = "1" . equals ( request . getHeader ( "icy-metadata" ) ) ;
if ( isShoutCastRequested & & ! isSingleFile ) {
response . setHeader ( "icy-metaint" , "" + ShoutCastOutputStream . META_DATA_INTERVAL ) ;
response . setHeader ( "icy-notice1" , "This stream is served using Airsonic" ) ;
response . setHeader ( "icy-notice2" , "Airsonic - Free media streamer" ) ;
response . setHeader ( "icy-name" , "Airsonic" ) ;
response . setHeader ( "icy-genre" , "Mixed" ) ;
response . setHeader ( "icy-url" , "https://airsonic.github.io/" ) ;
out = new ShoutCastOutputStream ( out , player . getPlayQueue ( ) , settingsService ) ;
}
return out ;
}
private void setContentDuration ( HttpServletResponse response , MediaFile file ) {
if ( file . getDurationSeconds ( ) ! = null ) {
response . setHeader ( "X-Content-Duration" , String . format ( "%.1f" , file . getDurationSeconds ( ) . doubleValue ( ) ) ) ;
@ -314,6 +348,7 @@ public class StreamController {
return duration * ( long ) maxBitRate * 1000L / 8L ;
}
@Nullable
private HttpRange getRange ( HttpServletRequest request , MediaFile file ) {
// First, look for "Range" HTTP header.
@ -325,13 +360,11 @@ public class StreamController {
// Second, look for "offsetSeconds" request parameter.
String offsetSeconds = request . getParameter ( "offsetSeconds" ) ;
range = parseAndConvertOffsetSeconds ( offsetSeconds , file ) ;
if ( range ! = null ) {
return range ;
}
return null ;
}
@Nullable
private HttpRange parseAndConvertOffsetSeconds ( String offsetSeconds , MediaFile file ) {
if ( offsetSeconds = = null ) {
return null ;
@ -355,12 +388,14 @@ public class StreamController {
}
}
private VideoTranscodingSettings createVideoTranscodingSettings ( MediaFile file , HttpServletRequest request ) throws ServletRequestBindingException {
private VideoTranscodingSettings createVideoTranscodingSettings ( MediaFile file , HttpServletRequest request )
throws ServletRequestBindingException {
Integer existingWidth = file . getWidth ( ) ;
Integer existingHeight = file . getHeight ( ) ;
Integer maxBitRate = ServletRequestUtils . getIntParameter ( request , "maxBitRate" ) ;
int timeOffset = ServletRequestUtils . getIntParameter ( request , "timeOffset" , 0 ) ;
int defaultDuration = file . getDurationSeconds ( ) = = null ? Integer . MAX_VALUE : file . getDurationSeconds ( ) - timeOffset ;
int defaultDuration = file . getDurationSeconds ( ) = = null ? Integer . MAX_VALUE :
file . getDurationSeconds ( ) - timeOffset ;
int duration = ServletRequestUtils . getIntParameter ( request , "duration" , defaultDuration ) ;
boolean hls = ServletRequestUtils . getBooleanParameter ( request , "hls" , false ) ;