Content of file TrackDIDLAdapter.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>TrackDIDLAdapter.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: Application :: JavaFX</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-upnp-mediaserver</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl</a> > <span class="el_source">TrackDIDLAdapter.java</span></div><h1>TrackDIDLAdapter.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* blueMarine II: Semantic Media Centre
* http://tidalwave.it/projects/bluemarine2
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/bluemarine2-src
* git clone https://github.com/tidalwave-it/bluemarine2-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.time.Duration;
import java.io.IOException;
import org.fourthline.cling.support.model.DIDLObject;
import org.fourthline.cling.support.model.Protocol;
import org.fourthline.cling.support.model.ProtocolInfo;
import org.fourthline.cling.support.model.Res;
import org.fourthline.cling.support.model.StorageMedium;
import org.fourthline.cling.support.model.dlna.DLNAProtocolInfo;
import org.fourthline.cling.support.model.item.MusicTrack;
import it.tidalwave.role.ui.Displayable;
import it.tidalwave.dci.annotation.DciRole;
import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
import it.tidalwave.bluemarine2.model.audio.AudioFile;
import it.tidalwave.bluemarine2.model.audio.Track;
import it.tidalwave.bluemarine2.rest.spi.ResourceServer;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.role.ui.Displayable._Displayable_;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
import static it.tidalwave.bluemarine2.model.role.AudioFileSupplier._AudioFileSupplier_;
/***********************************************************************************************************************
*
* The {@link DIDLAdapter} for {@link Track}.
*
* @stereotype Role
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L60">@Slf4j</span>
@Immutable @DciRole(datumType = Track.class)
public class TrackDIDLAdapter extends DIDLAdapterSupport<Track>
{
public TrackDIDLAdapter (@Nonnull final Track track , @Nonnull final ResourceServer server)
{
<span class="fc" id="L66"> super(track, server);</span>
<span class="fc" id="L67"> }</span>
@Override @Nonnull
public DIDLObject toObject()
throws IOException
{
<span class="fc" id="L73"> log.debug("toObject() - {}", datum);</span>
// parentID not set here
<span class="fc" id="L75"> final MusicTrack item = setCommonFields(new MusicTrack());</span>
<span class="fc" id="L76"> final AudioFile audioFile = datum.as(_AudioFileSupplier_).getAudioFile();</span>
<span class="fc" id="L77"> final Metadata trackMetadata = datum.getMetadata();</span>
<span class="fc" id="L78"> item.addResource(audioResourceOf(audioFile));</span>
<span class="fc" id="L79"> trackMetadata.get(TRACK_NUMBER).ifPresent(item::setOriginalTrackNumber);</span>
<span class="fc" id="L81"> datum.getRecord().flatMap(record -> record.maybeAs(_Displayable_))</span>
<span class="fc" id="L82"> .map(Displayable::getDisplayName)</span>
<span class="fc" id="L83"> .ifPresent(item::setAlbum);</span>
// trackMetadata.get(DISK_COUNT);
// trackMetadata.get(DISK_NUMBER);
// trackMetadata.get(COMPOSER);
// trackMetadata.get(ARTIST);
// item.setContributors(contributors);
// item.setCreator(creator);
// item.setDate(date);
// item.setDescMetadata(descMetadata);
// item.setDescription(description);
// item.setLanguage(language);
// item.setLongDescription(description);
<span class="fc" id="L96"> item.setStorageMedium(StorageMedium.HDD);</span>
// item.setArtists(new PersonWithRole[] { new PersonWithRole("xyz", "AlbumArtist") });
// item.setGenres(new String[] { "Classical" });
// final Person publisher = new Person("Unknown");
// item.setPublishers(new Person[] { publisher });
// datum.getDiskNumber();
<span class="fc" id="L105"> return item;</span>
}
@Nonnull
private Res audioResourceOf (@Nonnull final AudioFile audioFile)
{
<span class="fc" id="L111"> final ProtocolInfo protocolInfo = new DLNAProtocolInfo(Protocol.HTTP_GET, "*", "audio/mpeg", "*"); // FIXME: MIME</span>
<span class="fc" id="L112"> final Metadata audioFileMetadata = audioFile.getMetadata();</span>
<span class="fc" id="L113"> final Res resource = new Res(protocolInfo,</span>
<span class="fc" id="L114"> audioFileMetadata.get(FILE_SIZE).orElse(null),</span>
<span class="fc" id="L115"> server.absoluteUrl(String.format("rest/audiofile/%s/content", audioFile.getId().stringValue())));</span>
<span class="fc" id="L116"> audioFileMetadata.get(DURATION).ifPresent(duration -> resource.setDuration(durationToString(duration)));</span>
<span class="fc" id="L117"> audioFileMetadata.get(BIT_RATE).ifPresent(bitRate -> resource.setBitrate((long)(int)bitRate));</span>
<span class="fc" id="L118"> audioFileMetadata.get(BITS_PER_SAMPLE).ifPresent(bitPerSample -> resource.setBitsPerSample((long)(int)bitPerSample));</span>
<span class="pc" id="L119"> audioFileMetadata.get(CHANNELS).ifPresent(channels -> resource.setNrAudioChannels((long)(int)channels));</span>
<span class="fc" id="L120"> audioFileMetadata.get(SAMPLE_RATE).ifPresent(sampleRate -> resource.setSampleFrequency((long)(int)sampleRate));</span>
<span class="fc" id="L122"> return resource;</span>
}
@Nonnull
private static String durationToString (@Nonnull final Duration duration)
{
<span class="fc" id="L128"> final int d = (int)duration.getSeconds();</span>
<span class="fc" id="L129"> final int seconds = d % 60;</span>
<span class="fc" id="L130"> final int minutes = (d / 60) % 60;</span>
<span class="fc" id="L131"> final int hours = d / 3600;</span>
<span class="fc" id="L132"> return String.format("%d:%02d:%02d.000", hours, minutes, seconds);</span>
}
/*
// TODO <desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON5127_42????35</desc>
<item id="T:\public_html\Jukebox\mp3\_Presets\10.dpl/0" parentID="T:\public_html\Jukebox\mp3\_Presets\10.dpl" restricted="False">
<upnp:albumArtURI">http://eng.linn.co.uk/~joshh/Jukebox/mp3/Amy%20Macdonald/This%20Is%20The%20Life/Folder.jpg</upnp:albumArtURI>
<upnp:artworkURI">http://eng.linn.co.uk/~joshh/Jukebox/mp3/Amy%20Macdonald/This%20Is%20The%20Life/Folder.jpg</upnp:artworkURI>
<upnp:genre">Pop</upnp:genre>
<upnp:artist role="Performer"">Amy Macdonald</upnp:artist>
<upnp:artist role="Composer"">Unknown</upnp:artist>
<upnp:artist role="AlbumArtist"">Unknown</upnp:artist>
<upnp:artist role="Conductor"">Unknown</upnp:artist>
<dc:date >Unknown</dc:date>
<upnp:originalDiscNumber">0</upnp:originalDiscNumber>
<upnp:originalDiscCount">0</upnp:originalDiscCount>
</item>
*/
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>