Search in sources :

Example 1 with C

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);
}
Also used : MediaCodecAudioRenderer(com.google.android.exoplayer2.audio.MediaCodecAudioRenderer) MetadataRenderer(com.google.android.exoplayer2.metadata.MetadataRenderer) MediaCodecVideoRenderer(com.google.android.exoplayer2.video.MediaCodecVideoRenderer) TextRenderer(com.google.android.exoplayer2.text.TextRenderer)

Example 2 with C

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);
    }
}
Also used : ReusableBufferedOutputStream(com.google.android.exoplayer2.util.ReusableBufferedOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CacheException(com.google.android.exoplayer2.upstream.cache.Cache.CacheException) DataOutputStream(java.io.DataOutputStream) ReusableBufferedOutputStream(com.google.android.exoplayer2.util.ReusableBufferedOutputStream) DataOutputStream(java.io.DataOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Random(java.util.Random) IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 3 with C

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);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline)

Example 4 with C

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));
}
Also used : ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Test(org.junit.Test)

Example 5 with C

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);
}
Also used : Format(com.google.android.exoplayer2.Format) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 BaseUrl (com.google.android.exoplayer2.source.dash.manifest.BaseUrl)6 Format (com.google.android.exoplayer2.Format)5 SeekPoint (com.google.android.exoplayer2.extractor.SeekPoint)5 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)4 IOException (java.io.IOException)4 Nullable (androidx.annotation.Nullable)3 RendererCapabilities (com.google.android.exoplayer2.RendererCapabilities)3 HashMap (java.util.HashMap)3 Metadata (com.google.android.exoplayer2.metadata.Metadata)2 EventStream (com.google.android.exoplayer2.source.dash.manifest.EventStream)2 InterruptedIOException (java.io.InterruptedIOException)2 TargetApi (android.annotation.TargetApi)1 Activity (android.app.Activity)1 ActivityManager (android.app.ActivityManager)1 Application (android.app.Application)1 UiModeManager (android.app.UiModeManager)1 Context (android.content.Context)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1