Package: TrackDIDLAdapter
TrackDIDLAdapter
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TrackDIDLAdapter(Track, ResourceServer) |
|
|
|
|
|
||||||||||||||||||||
audioResourceOf(AudioFile) |
|
|
|
|
|
||||||||||||||||||||
durationToString(Duration) |
|
|
|
|
|
||||||||||||||||||||
lambda$audioResourceOf$1(Res, Duration) |
|
|
|
|
|
||||||||||||||||||||
lambda$audioResourceOf$2(Res, Integer) |
|
|
|
|
|
||||||||||||||||||||
lambda$audioResourceOf$3(Res, Integer) |
|
|
|
|
|
||||||||||||||||||||
lambda$audioResourceOf$4(Res, Integer) |
|
|
|
|
|
||||||||||||||||||||
lambda$audioResourceOf$5(Res, Integer) |
|
|
|
|
|
||||||||||||||||||||
lambda$toObject$0(Record) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toObject() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.Immutable;
31: import java.time.Duration;
32: import java.io.IOException;
33: import org.fourthline.cling.support.model.DIDLObject;
34: import org.fourthline.cling.support.model.Protocol;
35: import org.fourthline.cling.support.model.ProtocolInfo;
36: import org.fourthline.cling.support.model.Res;
37: import org.fourthline.cling.support.model.StorageMedium;
38: import org.fourthline.cling.support.model.dlna.DLNAProtocolInfo;
39: import org.fourthline.cling.support.model.item.MusicTrack;
40: import it.tidalwave.role.ui.Displayable;
41: import it.tidalwave.dci.annotation.DciRole;
42: import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
43: import it.tidalwave.bluemarine2.model.audio.AudioFile;
44: import it.tidalwave.bluemarine2.model.audio.Track;
45: import it.tidalwave.bluemarine2.rest.spi.ResourceServer;
46: import lombok.extern.slf4j.Slf4j;
47: import static it.tidalwave.role.ui.Displayable._Displayable_;
48: import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
49: import static it.tidalwave.bluemarine2.model.role.AudioFileSupplier._AudioFileSupplier_;
50:
51: /***********************************************************************************************************************
52: *
53: * The {@link DIDLAdapter} for {@link Track}.
54: *
55: * @stereotype Role
56: *
57: * @author Fabrizio Giudici
58: *
59: **********************************************************************************************************************/
60: @Slf4j
61: @Immutable @DciRole(datumType = Track.class)
62: public class TrackDIDLAdapter extends DIDLAdapterSupport<Track>
63: {
64: public TrackDIDLAdapter (@Nonnull final Track track , @Nonnull final ResourceServer server)
65: {
66: super(track, server);
67: }
68:
69: @Override @Nonnull
70: public DIDLObject toObject()
71: throws IOException
72: {
73: log.debug("toObject() - {}", datum);
74: // parentID not set here
75: final MusicTrack item = setCommonFields(new MusicTrack());
76: final AudioFile audioFile = datum.as(_AudioFileSupplier_).getAudioFile();
77: final Metadata trackMetadata = datum.getMetadata();
78: item.addResource(audioResourceOf(audioFile));
79: trackMetadata.get(TRACK_NUMBER).ifPresent(item::setOriginalTrackNumber);
80:
81: datum.getRecord().flatMap(record -> record.maybeAs(_Displayable_))
82: .map(Displayable::getDisplayName)
83: .ifPresent(item::setAlbum);
84:
85: // trackMetadata.get(DISK_COUNT);
86: // trackMetadata.get(DISK_NUMBER);
87: // trackMetadata.get(COMPOSER);
88: // trackMetadata.get(ARTIST);
89: // item.setContributors(contributors);
90: // item.setCreator(creator);
91: // item.setDate(date);
92: // item.setDescMetadata(descMetadata);
93: // item.setDescription(description);
94: // item.setLanguage(language);
95: // item.setLongDescription(description);
96: item.setStorageMedium(StorageMedium.HDD);
97:
98: // item.setArtists(new PersonWithRole[] { new PersonWithRole("xyz", "AlbumArtist") });
99: // item.setGenres(new String[] { "Classical" });
100: // final Person publisher = new Person("Unknown");
101: // item.setPublishers(new Person[] { publisher });
102:
103: // datum.getDiskNumber();
104:
105: return item;
106: }
107:
108: @Nonnull
109: private Res audioResourceOf (@Nonnull final AudioFile audioFile)
110: {
111: final ProtocolInfo protocolInfo = new DLNAProtocolInfo(Protocol.HTTP_GET, "*", "audio/mpeg", "*"); // FIXME: MIME
112: final Metadata audioFileMetadata = audioFile.getMetadata();
113: final Res resource = new Res(protocolInfo,
114: audioFileMetadata.get(FILE_SIZE).orElse(null),
115: server.absoluteUrl(String.format("rest/audiofile/%s/content", audioFile.getId().stringValue())));
116: audioFileMetadata.get(DURATION).ifPresent(duration -> resource.setDuration(durationToString(duration)));
117: audioFileMetadata.get(BIT_RATE).ifPresent(bitRate -> resource.setBitrate((long)(int)bitRate));
118: audioFileMetadata.get(BITS_PER_SAMPLE).ifPresent(bitPerSample -> resource.setBitsPerSample((long)(int)bitPerSample));
119: audioFileMetadata.get(CHANNELS).ifPresent(channels -> resource.setNrAudioChannels((long)(int)channels));
120: audioFileMetadata.get(SAMPLE_RATE).ifPresent(sampleRate -> resource.setSampleFrequency((long)(int)sampleRate));
121:
122: return resource;
123: }
124:
125: @Nonnull
126: private static String durationToString (@Nonnull final Duration duration)
127: {
128: final int d = (int)duration.getSeconds();
129: final int seconds = d % 60;
130: final int minutes = (d / 60) % 60;
131: final int hours = d / 3600;
132: return String.format("%d:%02d:%02d.000", hours, minutes, seconds);
133: }
134:
135: /*
136: // TODO <desc id="cdudn" nameSpace="urn:schemas-rinconnetworks-com:metadata-1-0/">SA_RINCON5127_42????35</desc>
137:
138: <item id="T:\public_html\Jukebox\mp3\_Presets\10.dpl/0" parentID="T:\public_html\Jukebox\mp3\_Presets\10.dpl" restricted="False">
139: <upnp:albumArtURI">http://eng.linn.co.uk/~joshh/Jukebox/mp3/Amy%20Macdonald/This%20Is%20The%20Life/Folder.jpg</upnp:albumArtURI>
140: <upnp:artworkURI">http://eng.linn.co.uk/~joshh/Jukebox/mp3/Amy%20Macdonald/This%20Is%20The%20Life/Folder.jpg</upnp:artworkURI>
141: <upnp:genre">Pop</upnp:genre>
142: <upnp:artist role="Performer"">Amy Macdonald</upnp:artist>
143: <upnp:artist role="Composer"">Unknown</upnp:artist>
144: <upnp:artist role="AlbumArtist"">Unknown</upnp:artist>
145: <upnp:artist role="Conductor"">Unknown</upnp:artist>
146: <dc:date >Unknown</dc:date>
147: <upnp:originalDiscNumber">0</upnp:originalDiscNumber>
148: <upnp:originalDiscCount">0</upnp:originalDiscCount>
149: </item>
150:
151: */
152: }