use of com.google.android.exoplayer2.C in project ExoPlayer by google.
the class SimpleExoPlayer method setAudioStreamType.
/**
* Sets the stream type for audio playback (see {@link C.StreamType} and
* {@link android.media.AudioTrack#AudioTrack(int, int, int, int, int, int)}). If the stream type
* is not set, audio renderers use {@link C#STREAM_TYPE_DEFAULT}.
* <p>
* Note that when the stream type changes, the AudioTrack must be reinitialized, which can
* introduce a brief gap in audio output. Note also that tracks in the same audio session must
* share the same routing, so a new audio session id will be generated.
*
* @param audioStreamType The stream type for audio playback.
*/
public void setAudioStreamType(@C.StreamType int audioStreamType) {
this.audioStreamType = audioStreamType;
ExoPlayerMessage[] messages = new ExoPlayerMessage[audioRendererCount];
int count = 0;
for (Renderer renderer : renderers) {
if (renderer.getTrackType() == C.TRACK_TYPE_AUDIO) {
messages[count++] = new ExoPlayerMessage(renderer, C.MSG_SET_STREAM_TYPE, audioStreamType);
}
}
player.sendMessages(messages);
}
use of com.google.android.exoplayer2.C in project ExoPlayer by google.
the class CachedContentIndex method writeFile.
private void writeFile() throws CacheException {
DataOutputStream output = null;
try {
OutputStream outputStream = atomicFile.startWrite();
if (bufferedOutputStream == null) {
bufferedOutputStream = new ReusableBufferedOutputStream(outputStream);
} else {
bufferedOutputStream.reset(outputStream);
}
output = new DataOutputStream(bufferedOutputStream);
output.writeInt(VERSION);
int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0;
output.writeInt(flags);
if (cipher != null) {
byte[] initializationVector = new byte[16];
new Random().nextBytes(initializationVector);
output.write(initializationVector);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector);
try {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
// Should never happen.
throw new IllegalStateException(e);
}
output.flush();
output = new DataOutputStream(new CipherOutputStream(bufferedOutputStream, cipher));
}
output.writeInt(keyToContent.size());
int hashCode = 0;
for (CachedContent cachedContent : keyToContent.values()) {
cachedContent.writeToStream(output);
hashCode += cachedContent.headerHashCode();
}
output.writeInt(hashCode);
atomicFile.endWrite(output);
// Avoid calling close twice. Duplicate CipherOutputStream.close calls did
// not used to be no-ops: https://android-review.googlesource.com/#/c/272799/
output = null;
} catch (IOException e) {
throw new CacheException(e);
} finally {
Util.closeQuietly(output);
}
}
use of com.google.android.exoplayer2.C in project ExoPlayer by google.
the class ServerSideAdInsertionUtil method getStreamPositionUs.
/**
* Returns the position in the underlying server-side inserted ads stream for the current playback
* position in the {@link Player}.
*
* @param player The {@link Player}.
* @param adPlaybackState The {@link AdPlaybackState} defining the ad groups.
* @return The position in the underlying server-side inserted ads stream, in microseconds, or
* {@link C#TIME_UNSET} if it can't be determined.
*/
public static long getStreamPositionUs(Player player, AdPlaybackState adPlaybackState) {
Timeline timeline = player.getCurrentTimeline();
if (timeline.isEmpty()) {
return C.TIME_UNSET;
}
Timeline.Period period = timeline.getPeriod(player.getCurrentPeriodIndex(), new Timeline.Period());
if (!Util.areEqual(period.getAdsId(), adPlaybackState.adsId)) {
return C.TIME_UNSET;
}
if (player.isPlayingAd()) {
int adGroupIndex = player.getCurrentAdGroupIndex();
int adIndexInAdGroup = player.getCurrentAdIndexInAdGroup();
long adPositionUs = Util.msToUs(player.getCurrentPosition());
return getStreamPositionUsForAd(adPositionUs, adGroupIndex, adIndexInAdGroup, adPlaybackState);
}
long periodPositionUs = Util.msToUs(player.getCurrentPosition()) - period.getPositionInWindowUs();
return getStreamPositionUsForContent(periodPositionUs, /* nextAdGroupIndex= */
C.INDEX_UNSET, adPlaybackState);
}
use of com.google.android.exoplayer2.C in project ExoPlayer by google.
the class ServerSideAdInsertionUtilTest method addAdGroupToAdPlaybackState_insertsCorrectAdGroupData.
@Test
public void addAdGroupToAdPlaybackState_insertsCorrectAdGroupData() {
AdPlaybackState state = new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 1, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2);
// stream: 0-- content --4300-- ad1 --4500-- content
// content timeline: 0-4300 - [ad1] - 4700-end
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
4300, /* contentResumeOffsetUs= */
400, /* adDurationsUs...= */
200);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 4300, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
200));
// stream: 0-- content --2100-- ad1 --2400-- content --4300-- ad2 --4500-- content
// content timeline: 0-2100 - [ad1] - 2100-4000 - [ad2] - 4400-end
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
2100, /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
300);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 2100, 4000, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdCount(/* adGroupIndex= */
3, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
3, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
3, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
300).withAdDurationsUs(/* adGroupIndex= */
3, /* adDurationsUs...= */
200));
// stream: 0-- ad1 --100-- content --2100-- ad2 --2400-- content --4300-- ad3 --4500-- content
// content timeline: 0 - [ad1] - 50-2050 -[ad2] - 2050-3950 - [ad3] - 4350-end
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
0, /* contentResumeOffsetUs= */
50, /* adDurationsUs...= */
100);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 0, 2050, 3950, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdCount(/* adGroupIndex= */
3, /* adCount= */
1).withAdCount(/* adGroupIndex= */
4, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
3, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
4, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
50).withContentResumeOffsetUs(/* adGroupIndex= */
4, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
100).withAdDurationsUs(/* adGroupIndex= */
3, /* adDurationsUs...= */
300).withAdDurationsUs(/* adGroupIndex= */
4, /* adDurationsUs...= */
200));
// stream: 0-- ad1 --100-- c --2100-- ad2 --2400-- c --4300-- ad3 --4500-- c --5000-- ad4 --6000
// content timeline: 0 - [ad1] - 50-2050 -[ad2] - 2050-3950 - [ad3] - 4350-4850 - [ad4] - 4850
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
5000, /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
1000);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 0, 2050, 3950, 4850, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdCount(/* adGroupIndex= */
3, /* adCount= */
1).withAdCount(/* adGroupIndex= */
4, /* adCount= */
1).withAdCount(/* adGroupIndex= */
5, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
3, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
4, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
5, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
50).withContentResumeOffsetUs(/* adGroupIndex= */
4, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
100).withAdDurationsUs(/* adGroupIndex= */
3, /* adDurationsUs...= */
300).withAdDurationsUs(/* adGroupIndex= */
4, /* adDurationsUs...= */
200).withAdDurationsUs(/* adGroupIndex= */
5, /* adDurationsUs...= */
1000));
}
use of com.google.android.exoplayer2.C in project ExoPlayer by google.
the class DefaultTrackSelectorTest method selectTracksSelectTrackWithSelectionFlag.
/**
* Tests that track selector will select audio track with {@link C#SELECTION_FLAG_DEFAULT} given
* default values of {@link Parameters}.
*/
@Test
public void selectTracksSelectTrackWithSelectionFlag() throws Exception {
Format.Builder formatBuilder = AUDIO_FORMAT.buildUpon();
Format audioFormat = formatBuilder.setSelectionFlags(0).build();
Format formatWithSelectionFlag = formatBuilder.setSelectionFlags(C.SELECTION_FLAG_DEFAULT).build();
TrackGroupArray trackGroups = wrapFormats(audioFormat, formatWithSelectionFlag);
TrackSelectorResult result = trackSelector.selectTracks(new RendererCapabilities[] { ALL_AUDIO_FORMAT_SUPPORTED_RENDERER_CAPABILITIES }, trackGroups, periodId, TIMELINE);
assertFixedSelection(result.selections[0], trackGroups, formatWithSelectionFlag);
}
Aggregations