|
| 1 | +package xyz.gianlu.librespot.dacp; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | +import org.jetbrains.annotations.Range; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import java.io.Closeable; |
| 10 | +import java.io.File; |
| 11 | +import java.io.FileOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.util.Base64; |
| 15 | + |
| 16 | +/** |
| 17 | + * Metadata pipe implementation following the Shairport Sync format (https://github.com/mikebrady/shairport-sync-metadata-reader). |
| 18 | + * |
| 19 | + * @author devgianlu |
| 20 | + */ |
| 21 | +public final class DacpMetadataPipe implements Closeable { |
| 22 | + private static final String TYPE_SSNC = "73736e63"; |
| 23 | + private static final String TYPE_CORE = "636f7265"; |
| 24 | + private static final String CODE_ASAR = "61736172"; |
| 25 | + private static final String CODE_ASAL = "6173616c"; |
| 26 | + private static final String CODE_MINM = "6d696e6d"; |
| 27 | + private static final String CODE_PVOL = "70766f6c"; |
| 28 | + private static final String CODE_PRGR = "70726772"; |
| 29 | + private static final String CODE_PICT = "50494354"; |
| 30 | + private static final String CODE_PFLS = "70666C73"; |
| 31 | + private static final Logger LOGGER = LoggerFactory.getLogger(DacpMetadataPipe.class); |
| 32 | + private final File file; |
| 33 | + private FileOutputStream out; |
| 34 | + |
| 35 | + public DacpMetadataPipe(@NotNull File file) { |
| 36 | + this.file = file; |
| 37 | + } |
| 38 | + |
| 39 | + private void safeSend(@NotNull String type, @NotNull String code) { |
| 40 | + safeSend(type, code, (String) null); |
| 41 | + } |
| 42 | + |
| 43 | + private void safeSend(@NotNull String type, @NotNull String code, @Nullable String payload) { |
| 44 | + safeSend(type, code, payload == null ? null : payload.getBytes(StandardCharsets.UTF_8)); |
| 45 | + } |
| 46 | + |
| 47 | + private void safeSend(@NotNull String type, @NotNull String code, @Nullable byte[] payload) { |
| 48 | + try { |
| 49 | + send(type, code, payload); |
| 50 | + } catch (IOException ex) { |
| 51 | + LOGGER.error("Failed sending metadata through pipe!", ex); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private synchronized void send(@NotNull String type, @NotNull String code, @Nullable byte[] payload) throws IOException { |
| 56 | + if (out == null) out = new FileOutputStream(file); |
| 57 | + |
| 58 | + if (payload != null && payload.length > 0) { |
| 59 | + out.write(String.format("<item><type>%s</type><code>%s</code><length>%d</length>\n<data encoding=\"base64\">%s</data></item>\n", type, code, |
| 60 | + payload.length, new String(Base64.getEncoder().encode(payload), StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8)); |
| 61 | + } else { |
| 62 | + out.write(String.format("<item><type>%s</type><code>%s</code><length>0</length></item>\n", type, code).getBytes(StandardCharsets.UTF_8)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void sendImage(byte[] image) { |
| 67 | + if (image == null) { |
| 68 | + LOGGER.warn("No image found in metadata."); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + safeSend(TYPE_SSNC, CODE_PICT, image); |
| 73 | + } |
| 74 | + |
| 75 | + public void sendProgress(float currentTime, float duration, float sampleRate) { |
| 76 | + safeSend(TYPE_SSNC, CODE_PRGR, String.format("1/%.0f/%.0f", currentTime * sampleRate / 1000 + 1, duration * sampleRate / 1000 + 1)); |
| 77 | + } |
| 78 | + |
| 79 | + public void sendTrackInfo(String name, String albumName, String artist) { |
| 80 | + safeSend(TYPE_CORE, CODE_MINM, name); |
| 81 | + safeSend(TYPE_CORE, CODE_ASAL, albumName); |
| 82 | + safeSend(TYPE_CORE, CODE_ASAR, artist); |
| 83 | + } |
| 84 | + |
| 85 | + public void sendVolume(@Range(from = 0, to = 1) float value) { |
| 86 | + float xmlValue; |
| 87 | + if (value == 0) xmlValue = -144.0f; |
| 88 | + else xmlValue = (value - 1) * 30.0f; |
| 89 | + String volData = String.format("%.2f,0.00,0.00,0.00", xmlValue); |
| 90 | + safeSend(TYPE_SSNC, CODE_PVOL, volData); |
| 91 | + } |
| 92 | + |
| 93 | + public void sendPipeFlush() { |
| 94 | + safeSend(TYPE_CORE, CODE_PFLS); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void close() throws IOException { |
| 99 | + if (out != null) out.close(); |
| 100 | + } |
| 101 | +} |
0 commit comments