mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 17:29:32 +01:00
Create a SignalDataSource class for all of our ExoPlayer needs.
Also fixes an issue around GIF playback within a conversation.
This commit is contained in:
@@ -27,7 +27,6 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.android.exoplayer2.MediaItem;
|
||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||
import com.google.android.exoplayer2.PlaybackException;
|
||||
import com.google.android.exoplayer2.Player;
|
||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
||||
@@ -36,12 +35,13 @@ import com.google.android.exoplayer2.source.MediaSourceFactory;
|
||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
|
||||
import com.google.android.exoplayer2.ui.PlayerControlView;
|
||||
import com.google.android.exoplayer2.ui.PlayerView;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.mms.VideoSlide;
|
||||
import org.thoughtcrime.securesms.video.exo.AttachmentDataSourceFactory;
|
||||
import org.thoughtcrime.securesms.video.exo.SignalDataSource;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -86,9 +86,8 @@ public class VideoPlayer extends FrameLayout {
|
||||
Context context = getContext();
|
||||
|
||||
if (exoPlayer == null) {
|
||||
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(context, "GenericUserAgent", null);
|
||||
AttachmentDataSourceFactory attachmentDataSourceFactory = new AttachmentDataSourceFactory(context, defaultDataSourceFactory, null);
|
||||
MediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(attachmentDataSourceFactory);
|
||||
DataSource.Factory attachmentDataSourceFactory = new SignalDataSource.Factory(context, null, null);
|
||||
MediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(attachmentDataSourceFactory);
|
||||
|
||||
exoPlayer = new SimpleExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build();
|
||||
exoPlayer.addListener(new ExoPlayerListener(this, window, playerStateCallback, playerPositionDiscontinuityCallback));
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
package org.thoughtcrime.securesms.video.exo;
|
||||
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSource;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
import org.thoughtcrime.securesms.providers.BlobProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class AttachmentDataSource implements DataSource {
|
||||
|
||||
private final DefaultDataSource defaultDataSource;
|
||||
private final PartDataSource partDataSource;
|
||||
private final BlobDataSource blobDataSource;
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
public AttachmentDataSource(DefaultDataSource defaultDataSource,
|
||||
PartDataSource partDataSource,
|
||||
BlobDataSource blobDataSource)
|
||||
{
|
||||
this.defaultDataSource = defaultDataSource;
|
||||
this.partDataSource = partDataSource;
|
||||
this.blobDataSource = blobDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTransferListener(@NonNull TransferListener transferListener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long open(DataSpec dataSpec) throws IOException {
|
||||
if (BlobProvider.isAuthority(dataSpec.uri)) dataSource = blobDataSource;
|
||||
else if (PartAuthority.isLocalUri(dataSpec.uri)) dataSource = partDataSource;
|
||||
else dataSource = defaultDataSource;
|
||||
|
||||
return dataSource.open(dataSpec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(@NonNull byte[] buffer, int offset, int readLength) throws IOException {
|
||||
return dataSource.read(buffer, offset, readLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri getUri() {
|
||||
return dataSource.getUri();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Map<String, List<String>> getResponseHeaders() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package org.thoughtcrime.securesms.video.exo;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
|
||||
public class AttachmentDataSourceFactory implements DataSource.Factory {
|
||||
|
||||
private final Context context;
|
||||
|
||||
private final DefaultDataSourceFactory defaultDataSourceFactory;
|
||||
private final TransferListener listener;
|
||||
|
||||
public AttachmentDataSourceFactory(@NonNull Context context,
|
||||
@NonNull DefaultDataSourceFactory defaultDataSourceFactory,
|
||||
@Nullable TransferListener listener)
|
||||
{
|
||||
this.context = context;
|
||||
this.defaultDataSourceFactory = defaultDataSourceFactory;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull AttachmentDataSource createDataSource() {
|
||||
return new AttachmentDataSource(defaultDataSourceFactory.createDataSource(),
|
||||
new PartDataSource(context, listener),
|
||||
new BlobDataSource(context, listener));
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class BlobDataSource implements DataSource {
|
||||
class BlobDataSource implements DataSource {
|
||||
|
||||
private final @NonNull Context context;
|
||||
private final @Nullable TransferListener listener;
|
||||
|
||||
@@ -27,7 +27,7 @@ import okhttp3.OkHttpClient;
|
||||
/**
|
||||
* DataSource which utilizes ChunkedDataFetcher to download video content via Signal content proxy.
|
||||
*/
|
||||
public class ChunkedDataSource implements DataSource {
|
||||
class ChunkedDataSource implements DataSource {
|
||||
|
||||
private final OkHttpClient okHttpClient;
|
||||
private final TransferListener transferListener;
|
||||
@@ -138,5 +138,4 @@ public class ChunkedDataSource implements DataSource {
|
||||
}
|
||||
cacheEntry = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package org.thoughtcrime.securesms.video.exo;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public class ChunkedDataSourceFactory implements DataSource.Factory {
|
||||
|
||||
private final OkHttpClient okHttpClient;
|
||||
private final TransferListener listener;
|
||||
|
||||
public ChunkedDataSourceFactory(@NonNull OkHttpClient okHttpClient, @Nullable TransferListener listener) {
|
||||
this.okHttpClient = okHttpClient;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public @NonNull DataSource createDataSource() {
|
||||
return new ChunkedDataSource(okHttpClient, listener);
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PartDataSource implements DataSource {
|
||||
class PartDataSource implements DataSource {
|
||||
|
||||
private final @NonNull Context context;
|
||||
private final @Nullable TransferListener listener;
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package org.thoughtcrime.securesms.video.exo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSource;
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
import org.thoughtcrime.securesms.providers.BlobProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
/**
|
||||
* Go-to {@link DataSource} that handles all of our various types of video sources.
|
||||
* Will defer to other {@link DataSource}s depending on the URI.
|
||||
*/
|
||||
public class SignalDataSource implements DataSource {
|
||||
|
||||
private final DefaultDataSource defaultDataSource;
|
||||
private final PartDataSource partDataSource;
|
||||
private final BlobDataSource blobDataSource;
|
||||
private final ChunkedDataSource chunkedDataSource;
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
public SignalDataSource(@NonNull DefaultDataSource defaultDataSource,
|
||||
@NonNull PartDataSource partDataSource,
|
||||
@NonNull BlobDataSource blobDataSource,
|
||||
@Nullable ChunkedDataSource chunkedDataSource)
|
||||
{
|
||||
this.defaultDataSource = defaultDataSource;
|
||||
this.partDataSource = partDataSource;
|
||||
this.blobDataSource = blobDataSource;
|
||||
this.chunkedDataSource = chunkedDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTransferListener(@NonNull TransferListener transferListener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long open(DataSpec dataSpec) throws IOException {
|
||||
if (BlobProvider.isAuthority(dataSpec.uri)) {
|
||||
dataSource = blobDataSource;
|
||||
} else if (PartAuthority.isLocalUri(dataSpec.uri)) {
|
||||
dataSource = partDataSource;
|
||||
} else if (chunkedDataSource != null && isRemoteUri(dataSpec.uri)) {
|
||||
dataSource = chunkedDataSource;
|
||||
} else {
|
||||
dataSource = defaultDataSource;
|
||||
}
|
||||
|
||||
return dataSource.open(dataSpec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(@NonNull byte[] buffer, int offset, int readLength) throws IOException {
|
||||
return dataSource.read(buffer, offset, readLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Uri getUri() {
|
||||
return dataSource.getUri();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Map<String, List<String>> getResponseHeaders() {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
private static boolean isRemoteUri(@Nullable Uri uri) {
|
||||
if (uri != null) {
|
||||
String scheme = uri.getScheme();
|
||||
return "http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Factory implements DataSource.Factory {
|
||||
private final Context context;
|
||||
private final OkHttpClient okHttpClient;
|
||||
private final TransferListener listener;
|
||||
|
||||
public Factory(@NonNull Context context,
|
||||
@Nullable OkHttpClient okHttpClient,
|
||||
@Nullable TransferListener listener)
|
||||
{
|
||||
this.context = context;
|
||||
this.okHttpClient = okHttpClient;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull SignalDataSource createDataSource() {
|
||||
return new SignalDataSource(new DefaultDataSourceFactory(context, "GenericUserAgent", null).createDataSource(),
|
||||
new PartDataSource(context, listener),
|
||||
new BlobDataSource(context, listener),
|
||||
okHttpClient != null ? new ChunkedDataSource(okHttpClient, listener) : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import com.google.android.exoplayer2.offline.StreamKey;
|
||||
import com.google.android.exoplayer2.source.MediaSource;
|
||||
import com.google.android.exoplayer2.source.MediaSourceFactory;
|
||||
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
||||
import com.google.android.exoplayer2.upstream.HttpDataSource;
|
||||
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
|
||||
@@ -24,17 +25,16 @@ import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class is responsible for creating a MediaSource object for a given Uri, using AttachmentDataSourceFactory
|
||||
* This class is responsible for creating a MediaSource object for a given Uri, using {@link SignalDataSource.Factory}.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public final class AttachmentMediaSourceFactory implements MediaSourceFactory {
|
||||
public final class SignalMediaSourceFactory implements MediaSourceFactory {
|
||||
|
||||
private final ProgressiveMediaSource.Factory progressiveMediaSourceFactory;
|
||||
|
||||
public AttachmentMediaSourceFactory(@NonNull Context context) {
|
||||
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(context, "GenericUserAgent", null);
|
||||
AttachmentDataSourceFactory attachmentDataSourceFactory = new AttachmentDataSourceFactory(context, defaultDataSourceFactory, null);
|
||||
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory().setConstantBitrateSeekingEnabled(true);
|
||||
public SignalMediaSourceFactory(@NonNull Context context) {
|
||||
DataSource.Factory attachmentDataSourceFactory = new SignalDataSource.Factory(context, null, null);
|
||||
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory().setConstantBitrateSeekingEnabled(true);
|
||||
|
||||
progressiveMediaSourceFactory = new ProgressiveMediaSource.Factory(attachmentDataSourceFactory, extractorsFactory);
|
||||
}
|
||||
Reference in New Issue
Block a user