Advertisement
Guest User

PlayQueueInputStream.java

a guest
Mar 19th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. /*
  2.  This file is part of Subsonic.
  3.  
  4.  Subsonic is free software: you can redistribute it and/or modify
  5.  it under the terms of the GNU General Public License as published by
  6.  the Free Software Foundation, either version 3 of the License, or
  7.  (at your option) any later version.
  8.  
  9.  Subsonic is distributed in the hope that it will be useful,
  10.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  GNU General Public License for more details.
  13.  
  14.  You should have received a copy of the GNU General Public License
  15.  along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17.  Copyright 2009 (C) Sindre Mehus
  18.  */
  19. package net.sourceforge.subsonic.io;
  20.  
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.List;
  24. import net.sourceforge.subsonic.Logger;
  25. import net.sourceforge.subsonic.domain.MediaFile;
  26. import net.sourceforge.subsonic.domain.PlayQueue;
  27. import net.sourceforge.subsonic.domain.Player;
  28. import net.sourceforge.subsonic.domain.TransferStatus;
  29. import net.sourceforge.subsonic.domain.VideoTranscodingSettings;
  30. import net.sourceforge.subsonic.service.AudioScrobblerService;
  31. import net.sourceforge.subsonic.service.MediaFileService;
  32. import net.sourceforge.subsonic.service.SearchService;
  33. import net.sourceforge.subsonic.service.TranscodingService;
  34. import net.sourceforge.subsonic.util.FileUtil;
  35.  
  36. /**
  37.  * Implementation of {@link InputStream} which reads from a {@link net.sourceforge.subsonic.domain.PlayQueue}.
  38.  *
  39.  * @author Sindre Mehus
  40.  */
  41. public class PlayQueueInputStream extends InputStream {
  42.  
  43.     private static final Logger LOG = Logger.getLogger(PlayQueueInputStream.class);
  44.  
  45.     private final Player player;
  46.     private final TransferStatus status;
  47.     private final Integer maxBitRate;
  48.     private final String preferredTargetFormat;
  49.     private final VideoTranscodingSettings videoTranscodingSettings;
  50.     private final TranscodingService transcodingService;
  51.     private final AudioScrobblerService audioScrobblerService;
  52.     private final MediaFileService mediaFileService;
  53.     private MediaFile currentFile;
  54.     private InputStream currentInputStream;
  55.     private SearchService searchService;
  56.  
  57.     public PlayQueueInputStream(Player player, TransferStatus status, Integer maxBitRate, String preferredTargetFormat,
  58.                                 VideoTranscodingSettings videoTranscodingSettings, TranscodingService transcodingService,
  59.                                 AudioScrobblerService audioScrobblerService, MediaFileService mediaFileService, SearchService searchService) {
  60.         this.player = player;
  61.         this.status = status;
  62.         this.maxBitRate = maxBitRate;
  63.         this.preferredTargetFormat = preferredTargetFormat;
  64.         this.videoTranscodingSettings = videoTranscodingSettings;
  65.         this.transcodingService = transcodingService;
  66.         this.audioScrobblerService = audioScrobblerService;
  67.         this.mediaFileService = mediaFileService;
  68.         this.searchService = searchService;
  69.     }
  70.  
  71.     @Override
  72.     public int read() throws IOException {
  73.         byte[] b = new byte[1];
  74.         int n = read(b);
  75.         return n == -1 ? -1 : b[0];
  76.     }
  77.  
  78.     @Override
  79.     public int read(byte[] b) throws IOException {
  80.         return read(b, 0, b.length);
  81.     }
  82.  
  83.     @Override
  84.     public int read(byte[] b, int off, int len) throws IOException {
  85.         prepare();
  86.         if (currentInputStream == null || player.getPlayQueue().getStatus() == PlayQueue.Status.STOPPED) {
  87.             return -1;
  88.         }
  89.  
  90.         int n = currentInputStream.read(b, off, len);
  91.  
  92.         // If end of song reached, skip to next song and call read() again.
  93.         if (n == -1) {
  94.             player.getPlayQueue().next();
  95.             close();
  96.             return read(b, off, len);
  97.         } else {
  98.             status.addBytesTransfered(n);
  99.         }
  100.         return n;
  101.     }
  102.  
  103.     private void prepare() throws IOException {
  104.         PlayQueue playQueue = player.getPlayQueue();
  105.  
  106.         // If playlist is in auto-random mode, populate it with new random songs.
  107.         if (playQueue.getIndex() == -1 && playQueue.getRandomSearchCriteria() != null) {
  108.             populateRandomPlaylist(playQueue);
  109.         }
  110.  
  111.         MediaFile result;
  112.         synchronized (playQueue) {
  113.             result = playQueue.getCurrentFile();
  114.         }
  115.         MediaFile file = result;
  116.         if (file == null) {
  117.             close();
  118.         } else if (!file.equals(currentFile)) {
  119.             close();
  120.             LOG.info(player.getUsername() + " listening to \"" + FileUtil.getShortPath(file.getFile()) + "\"");
  121.             mediaFileService.incrementPlayCount(file);
  122.             mediaFileService.updateStatisticsUser(player.getUsername(), file);
  123.  
  124.             if (player.getClientId() == null) {  // Don't scrobble REST players.
  125.                 audioScrobblerService.register(file, player.getUsername(), false, null);
  126.             }
  127.  
  128.             TranscodingService.Parameters parameters = transcodingService.getParameters(file, player, maxBitRate, preferredTargetFormat, videoTranscodingSettings, true);
  129.             currentInputStream = transcodingService.getTranscodedInputStream(parameters);
  130.             currentFile = file;
  131.             status.setFile(currentFile.getFile());
  132.         }
  133.     }
  134.  
  135.     private void populateRandomPlaylist(PlayQueue playQueue) throws IOException {
  136.         List<MediaFile> files = searchService.getRandomSongs(playQueue.getRandomSearchCriteria());
  137.         playQueue.addFiles(false, files);
  138.         LOG.info("Recreated random playlist with " + playQueue.size() + " songs.");
  139.     }
  140.  
  141.     @Override
  142.     public void close() throws IOException {
  143.         try {
  144.             if (currentInputStream != null) {
  145.                 currentInputStream.close();
  146.             }
  147.         } finally {
  148.             if (player.getClientId() == null) {  // Don't scrobble REST players.
  149.                 audioScrobblerService.register(currentFile, player.getUsername(), true, null);
  150.             }
  151.             currentInputStream = null;
  152.             currentFile = null;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement