summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/util/ChapterUtils.java
blob: ac81491192ddc965ce26eaf2389900088e85de92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package de.danoeh.antennapod.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.IOUtils;

import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.Chapter;
import de.danoeh.antennapod.util.comparator.ChapterStartTimeComparator;
import de.danoeh.antennapod.util.id3reader.ChapterReader;
import de.danoeh.antennapod.util.id3reader.ID3ReaderException;
import de.danoeh.antennapod.util.playback.Playable;
import de.danoeh.antennapod.util.vorbiscommentreader.VorbisCommentChapterReader;
import de.danoeh.antennapod.util.vorbiscommentreader.VorbisCommentReaderException;

/** Utility class for getting chapter data from media files. */
public class ChapterUtils {
	private static final String TAG = "ChapterUtils";

	private ChapterUtils() {
	}

	/**
	 * Uses the download URL of a media object of a feeditem to read its ID3
	 * chapters.
	 */
	public static void readID3ChaptersFromPlayableStreamUrl(Playable p) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
		if (p != null && p.getStreamUrl() != null) {
			InputStream in = null;
			try {
				URL url = new URL(p.getStreamUrl());
				ChapterReader reader = new ChapterReader();

				in = url.openStream();
				reader.readInputStream(in);
				List<Chapter> chapters = reader.getChapters();

				if (chapters != null) {
					Collections
							.sort(chapters, new ChapterStartTimeComparator());
					processChapters(chapters, p);
					if (chaptersValid(chapters)) {
						p.setChapters(chapters);
						Log.i(TAG, "Chapters loaded");
					} else {
						Log.e(TAG, "Chapter data was invalid");
					}
				} else {
					Log.i(TAG, "ChapterReader could not find any ID3 chapters");
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ID3ReaderException e) {
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} else {
			Log.e(TAG,
					"Unable to read ID3 chapters: media or download URL was null");
		}
	}

	/**
	 * Uses the file URL of a media object of a feeditem to read its ID3
	 * chapters.
	 */
	public static void readID3ChaptersFromPlayableFileUrl(Playable p) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
		if (p != null && p.localFileAvailable() && p.getLocalMediaUrl() != null) {
			File source = new File(p.getLocalMediaUrl());
			if (source.exists()) {
				ChapterReader reader = new ChapterReader();
				InputStream in = null;

				try {
					in = new BufferedInputStream(new FileInputStream(source));
					reader.readInputStream(in);
					List<Chapter> chapters = reader.getChapters();

					if (chapters != null) {
						Collections.sort(chapters,
								new ChapterStartTimeComparator());
						processChapters(chapters, p);
						if (chaptersValid(chapters)) {
							p.setChapters(chapters);
							Log.i(TAG, "Chapters loaded");
						} else {
							Log.e(TAG, "Chapter data was invalid");
						}
					} else {
						Log.i(TAG,
								"ChapterReader could not find any ID3 chapters");
					}
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ID3ReaderException e) {
					e.printStackTrace();
				} finally {
					if (in != null) {
						try {
							in.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				Log.e(TAG, "Unable to read id3 chapters: Source doesn't exist");
			}
		}
	}

	public static void readOggChaptersFromPlayableStreamUrl(Playable media) {
		if (media != null && media.streamAvailable()) {
			InputStream input = null;
			try {
				URL url = new URL(media.getStreamUrl());
				input = url.openStream();
				if (input != null) {
					readOggChaptersFromInputStream(media, input);
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				IOUtils.closeQuietly(input);
			}
		}
	}

	public static void readOggChaptersFromPlayableFileUrl(Playable media) {
		if (media != null && media.getLocalMediaUrl() != null) {
			File source = new File(media.getLocalMediaUrl());
			if (source.exists()) {
				InputStream input = null;
				try {
					input = new BufferedInputStream(new FileInputStream(source));
					readOggChaptersFromInputStream(media, input);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} finally {
					IOUtils.closeQuietly(input);
				}
			}
		}
	}

	private static void readOggChaptersFromInputStream(Playable p,
			InputStream input) {
		if (AppConfig.DEBUG)
			Log.d(TAG,
					"Trying to read chapters from item with title "
							+ p.getEpisodeTitle());
		try {
			VorbisCommentChapterReader reader = new VorbisCommentChapterReader();
			reader.readInputStream(input);
			List<Chapter> chapters = reader.getChapters();
			if (chapters != null) {
				Collections.sort(chapters, new ChapterStartTimeComparator());
				processChapters(chapters, p);
				if (chaptersValid(chapters)) {
					p.setChapters(chapters);
					Log.i(TAG, "Chapters loaded");
				} else {
					Log.e(TAG, "Chapter data was invalid");
				}
			} else {
				Log.i(TAG,
						"ChapterReader could not find any Ogg vorbis chapters");
			}
		} catch (VorbisCommentReaderException e) {
			e.printStackTrace();
		}
	}

	/** Makes sure that chapter does a title and an item attribute. */
	private static void processChapters(List<Chapter> chapters, Playable p) {
		for (int i = 0; i < chapters.size(); i++) {
			Chapter c = chapters.get(i);
			if (c.getTitle() == null) {
				c.setTitle(Integer.toString(i));
			}
		}
	}

	private static boolean chaptersValid(List<Chapter> chapters) {
		if (chapters.isEmpty()) {
			return false;
		}
		for (Chapter c : chapters) {
			if (c.getTitle() == null) {
				return false;
			}
			if (c.getStart() < 0) {
				return false;
			}
		}
		return true;
	}

	/** Calls getCurrentChapter with current position. */
	public static Chapter getCurrentChapter(Playable media) {
		if (media.getChapters() != null) {
			List<Chapter> chapters = media.getChapters();
			Chapter current = null;
			if (chapters != null) {
				current = chapters.get(0);
				for (Chapter sc : chapters) {
					if (sc.getStart() > media.getPosition()) {
						break;
					} else {
						current = sc;
					}
				}
			}
			return current;
		} else {
			return null;
		}
	}

	public static void loadChaptersFromStreamUrl(Playable media) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Starting chapterLoader thread");
		ChapterUtils.readID3ChaptersFromPlayableStreamUrl(media);
		if (media.getChapters() == null) {
			ChapterUtils.readOggChaptersFromPlayableStreamUrl(media);
		}

		if (AppConfig.DEBUG)
			Log.d(TAG, "ChapterLoaderThread has finished");
	}
	
	public static void loadChaptersFromFileUrl(Playable media) {
		if (media.localFileAvailable()) {
			ChapterUtils.readID3ChaptersFromPlayableFileUrl(media);
			if (media.getChapters() == null) {
				ChapterUtils.readOggChaptersFromPlayableFileUrl(media);
			}
		} else {
			Log.e(TAG, "Could not load chapters from file url: local file not available");
		}
	}
}