Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Applied EXT-X-KEY for each media segment in the writer. (#37)
Browse files Browse the repository at this point in the history
* Applied EXT-X-KEY for each media segment in the writer.

* Prevented writing unnecessary duplicate EXT-X-KEY tags.
  • Loading branch information
Wopple authored and sunglee413 committed Jun 20, 2016
1 parent 00d62f1 commit bd0de8b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
14 changes: 6 additions & 8 deletions src/main/java/com/iheartradio/m3u8/ExtendedM3uWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.iheartradio.m3u8.data.Playlist;

class ExtendedM3uWriter extends Writer {
private List<IExtTagWriter> mExtTagWriter = new ArrayList<IExtTagWriter>();
private List<SectionWriter> mExtTagWriter = new ArrayList<SectionWriter>();

public ExtendedM3uWriter(OutputStream outputStream, Encoding encoding) {
super(outputStream, encoding);
Expand All @@ -17,7 +18,6 @@ public ExtendedM3uWriter(OutputStream outputStream, Encoding encoding) {
ExtTagWriter.EXTM3U_HANDLER,
ExtTagWriter.EXT_X_VERSION_HANDLER,
MediaPlaylistTagWriter.EXT_X_PLAYLIST_TYPE,
MediaPlaylistTagWriter.EXT_X_KEY,
MediaPlaylistTagWriter.EXT_X_TARGETDURATION,
MediaPlaylistTagWriter.EXT_X_START,
MediaPlaylistTagWriter.EXT_X_MEDIA_SEQUENCE,
Expand All @@ -26,22 +26,20 @@ public ExtendedM3uWriter(OutputStream outputStream, Encoding encoding) {
MediaPlaylistTagWriter.EXT_X_ALLOW_CACHE,
MasterPlaylistTagWriter.EXT_X_STREAM_INF,
MasterPlaylistTagWriter.EXT_X_I_FRAME_STREAM_INF,
MediaPlaylistTagWriter.EXTINF,
MediaPlaylistTagWriter.MEDIA_SEGMENTS,
MediaPlaylistTagWriter.EXT_X_ENDLIST
);
}

private void putWriters(IExtTagWriter... writers) {
private void putWriters(SectionWriter... writers) {
if (writers != null) {
for (IExtTagWriter writer : writers) {
mExtTagWriter.add(writer);
}
Collections.addAll(mExtTagWriter, writers);
}
}

@Override
void doWrite(Playlist playlist) throws IOException, ParseException, PlaylistException {
for (IExtTagWriter singleTagWriter : mExtTagWriter) {
for (SectionWriter singleTagWriter : mExtTagWriter) {
singleTagWriter.write(tagWriter, playlist);
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/iheartradio/m3u8/IExtTagWriter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.iheartradio.m3u8;

import java.io.IOException;

import com.iheartradio.m3u8.data.Playlist;

interface IExtTagWriter {
interface IExtTagWriter extends SectionWriter {
String getTag();

void write(TagWriter tagWriter, Playlist playlist) throws IOException, ParseException;
}
76 changes: 43 additions & 33 deletions src/main/java/com/iheartradio/m3u8/MediaPlaylistTagWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,41 +188,46 @@ public void doWrite(TagWriter tagWriter, Playlist playlist, MediaPlaylist mediaP

// media segment tags

static final IExtTagWriter EXTINF = new MediaPlaylistTagWriter() {
static final SectionWriter MEDIA_SEGMENTS = new SectionWriter() {
@Override
public String getTag() {
return Constants.EXTINF_TAG;
}
public void write(TagWriter tagWriter, Playlist playlist) throws IOException, ParseException {
if (playlist.hasMediaPlaylist()) {
KeyWriter keyWriter = new KeyWriter();

@Override
boolean hasData() {
return true;
}

@Override
public void doWrite(TagWriter tagWriter, Playlist playlist, MediaPlaylist mediaPlaylist) throws IOException ,ParseException {
for (TrackData trackData : mediaPlaylist.getTracks()) {
StringBuilder sb = new StringBuilder();
if (playlist.getCompatibilityVersion() <= 3) {
sb.append(Integer.toString((int)trackData.getTrackInfo().duration));
} else {
sb.append(Float.toString(trackData.getTrackInfo().duration));
}
if (trackData.getTrackInfo().title != null) {
sb.append(Constants.COMMA).append(trackData.getTrackInfo().title);
}
if (trackData.hasDiscontinuity()) {
tagWriter.writeTag(Constants.EXT_X_DISCONTINUITY_TAG);
for (TrackData trackData : playlist.getMediaPlaylist().getTracks()) {
if (trackData.hasDiscontinuity()) {
tagWriter.writeTag(Constants.EXT_X_DISCONTINUITY_TAG);
}

keyWriter.writeTrackData(tagWriter, playlist, trackData);
writeExtinf(tagWriter, playlist, trackData);
tagWriter.writeLine(trackData.getUri());
}
tagWriter.writeTag(getTag(), sb.toString());
tagWriter.writeLine(trackData.getUri());
}
};
}
};

static final ExtTagWriter EXT_X_KEY = new MediaPlaylistTagWriter() {
private static void writeExtinf(TagWriter tagWriter, Playlist playlist, TrackData trackData) throws IOException {
final StringBuilder builder = new StringBuilder();

if (playlist.getCompatibilityVersion() <= 3) {
builder.append(Integer.toString((int) trackData.getTrackInfo().duration));
} else {
builder.append(Float.toString(trackData.getTrackInfo().duration));
}

if (trackData.getTrackInfo().title != null) {
builder.append(Constants.COMMA).append(trackData.getTrackInfo().title);
}

tagWriter.writeTag(Constants.EXTINF_TAG, builder.toString());
}

static class KeyWriter extends MediaPlaylistTagWriter {
private final Map<String, AttributeWriter<EncryptionData>> HANDLERS = new HashMap<String, AttributeWriter<EncryptionData>>();

private EncryptionData mEncryptionData;

{
HANDLERS.put(Constants.METHOD, new AttributeWriter<EncryptionData>() {
@Override
Expand Down Expand Up @@ -299,13 +304,18 @@ boolean hasData() {

@Override
public void doWrite(TagWriter tagWriter, Playlist playlist, MediaPlaylist mediaPlaylist) throws IOException, ParseException {
if (mediaPlaylist.getTracks().size() > 0) {
TrackData td = mediaPlaylist.getTracks().get(0);
if (td.hasEncryptionData()) {
EncryptionData ed = td.getEncryptionData();
writeAttributes(tagWriter, ed, HANDLERS);
writeAttributes(tagWriter, mEncryptionData, HANDLERS);
}

void writeTrackData(TagWriter tagWriter, Playlist playlist, TrackData trackData) throws IOException, ParseException {
if (trackData != null && trackData.hasEncryptionData()) {
final EncryptionData encryptionData = trackData.getEncryptionData();

if (!encryptionData.equals(mEncryptionData)) {
mEncryptionData = encryptionData;
write(tagWriter, playlist);
}
}
}
};
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/iheartradio/m3u8/SectionWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.iheartradio.m3u8;

import com.iheartradio.m3u8.data.Playlist;

import java.io.IOException;

interface SectionWriter {
void write(TagWriter tagWriter, Playlist playlist) throws IOException, ParseException;
}

0 comments on commit bd0de8b

Please sign in to comment.