-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from vzhn/feature/play_on_repeat
Feature/play on repeat
- Loading branch information
Showing
6 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/me/vzhilin/bstreamer/server/streaming/RepeatedSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package me.vzhilin.bstreamer.server.streaming; | ||
|
||
import me.vzhilin.bstreamer.server.streaming.base.PullSource; | ||
import me.vzhilin.bstreamer.server.streaming.file.MediaPacket; | ||
import me.vzhilin.bstreamer.server.streaming.file.SourceDescription; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Supplier; | ||
|
||
public class RepeatedSource implements PullSource { | ||
private final Supplier<PullSource> supplier; | ||
private PullSource delegate = null; | ||
private long lastDts; | ||
private long lastPts; | ||
private long ptsOffset = 0; | ||
private long dtsOffset = 0; | ||
|
||
public RepeatedSource(Supplier<PullSource> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
private PullSource ensureHasDelegate() { | ||
if (delegate == null) { | ||
delegate = supplier.get(); | ||
} | ||
return delegate; | ||
} | ||
@Override | ||
public SourceDescription getDesc() { | ||
ensureHasDelegate(); | ||
return delegate.getDesc(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
ensureHasDelegate(); | ||
|
||
if (!delegate.hasNext()) { | ||
ptsOffset += lastPts; | ||
dtsOffset += lastDts; | ||
try { | ||
delegate.close(); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
delegate = supplier.get(); | ||
} | ||
|
||
return delegate.hasNext(); | ||
} | ||
|
||
@Override | ||
public MediaPacket next() { | ||
MediaPacket p = delegate.next(); | ||
lastDts = p.getDts(); | ||
lastPts = p.getPts(); | ||
return new MediaPacket(p.getPts() + ptsOffset, p.getDts() + dtsOffset, p.isKey(), p.getPayload()); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (delegate != null) { | ||
delegate.close(); | ||
delegate = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters