Remove video player usage of AttachmentServer.
This commit is contained in:
parent
a5368b7ea9
commit
8a8817f8d3
2 changed files with 30 additions and 104 deletions
|
@ -45,12 +45,9 @@ public class MediaSendVideoFragment extends Fragment implements MediaSendPageFra
|
|||
|
||||
uri = getArguments().getParcelable(KEY_URI);
|
||||
VideoSlide slide = new VideoSlide(requireContext(), uri, 0);
|
||||
try {
|
||||
((VideoPlayer) view).setWindow(requireActivity().getWindow());
|
||||
((VideoPlayer) view).setVideoSource(slide, false);
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Failed to play video.", e);
|
||||
}
|
||||
|
||||
((VideoPlayer) view).setWindow(requireActivity().getWindow());
|
||||
((VideoPlayer) view).setVideoSource(slide, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.thoughtcrime.securesms.video;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import android.util.AttributeSet;
|
||||
|
@ -25,9 +24,6 @@ import android.view.View;
|
|||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.MediaController;
|
||||
import android.widget.Toast;
|
||||
import android.widget.VideoView;
|
||||
|
||||
import com.google.android.exoplayer2.DefaultLoadControl;
|
||||
import com.google.android.exoplayer2.ExoPlayerFactory;
|
||||
|
@ -49,26 +45,19 @@ import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
|
|||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.attachments.AttachmentServer;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
import org.thoughtcrime.securesms.mms.VideoSlide;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
import org.thoughtcrime.securesms.video.exo.AttachmentDataSourceFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class VideoPlayer extends FrameLayout {
|
||||
|
||||
private static final String TAG = VideoPlayer.class.getSimpleName();
|
||||
|
||||
@Nullable private final VideoView videoView;
|
||||
@Nullable private final PlayerView exoView;
|
||||
private final PlayerView exoView;
|
||||
|
||||
@Nullable private SimpleExoPlayer exoPlayer;
|
||||
@Nullable private PlayerControlView exoControls;
|
||||
@Nullable private AttachmentServer attachmentServer;
|
||||
@Nullable private Window window;
|
||||
private SimpleExoPlayer exoPlayer;
|
||||
private PlayerControlView exoControls;
|
||||
private Window window;
|
||||
|
||||
public VideoPlayer(Context context) {
|
||||
this(context, null);
|
||||
|
@ -83,31 +72,34 @@ public class VideoPlayer extends FrameLayout {
|
|||
|
||||
inflate(context, R.layout.video_player, this);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 16) {
|
||||
this.exoView = ViewUtil.findById(this, R.id.video_view);
|
||||
this.videoView = null;
|
||||
this.exoControls = new PlayerControlView(getContext());
|
||||
this.exoControls.setShowTimeoutMs(-1);
|
||||
} else {
|
||||
this.videoView = ViewUtil.findById(this, R.id.video_view);
|
||||
this.exoView = null;
|
||||
initializeVideoViewControls(videoView);
|
||||
}
|
||||
this.exoView = ViewUtil.findById(this, R.id.video_view);
|
||||
this.exoControls = new PlayerControlView(getContext());
|
||||
this.exoControls.setShowTimeoutMs(-1);
|
||||
}
|
||||
|
||||
public void setVideoSource(@NonNull VideoSlide videoSource, boolean autoplay)
|
||||
throws IOException
|
||||
{
|
||||
if (Build.VERSION.SDK_INT >= 16) setExoViewSource(videoSource, autoplay);
|
||||
else setVideoViewSource(videoSource, autoplay);
|
||||
public void setVideoSource(@NonNull VideoSlide videoSource, boolean autoplay) {
|
||||
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
|
||||
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
|
||||
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
|
||||
LoadControl loadControl = new DefaultLoadControl();
|
||||
|
||||
exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
|
||||
exoPlayer.addListener(new ExoPlayerListener(window));
|
||||
exoView.setPlayer(exoPlayer);
|
||||
exoControls.setPlayer(exoPlayer);
|
||||
|
||||
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getContext(), "GenericUserAgent", null);
|
||||
AttachmentDataSourceFactory attachmentDataSourceFactory = new AttachmentDataSourceFactory(getContext(), defaultDataSourceFactory, null);
|
||||
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
|
||||
|
||||
MediaSource mediaSource = new ExtractorMediaSource(videoSource.getUri(), attachmentDataSourceFactory, extractorsFactory, null, null);
|
||||
|
||||
exoPlayer.prepare(mediaSource);
|
||||
exoPlayer.setPlayWhenReady(autoplay);
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
if (this.attachmentServer != null && this.videoView != null) {
|
||||
this.videoView.stopPlayback();
|
||||
} else if (this.exoPlayer != null) {
|
||||
this.exoPlayer.setPlayWhenReady(false);
|
||||
}
|
||||
this.exoPlayer.setPlayWhenReady(false);
|
||||
}
|
||||
|
||||
public void hideControls() {
|
||||
|
@ -124,10 +116,6 @@ public class VideoPlayer extends FrameLayout {
|
|||
}
|
||||
|
||||
public void cleanup() {
|
||||
if (this.attachmentServer != null) {
|
||||
this.attachmentServer.stop();
|
||||
}
|
||||
|
||||
if (this.exoPlayer != null) {
|
||||
this.exoPlayer.release();
|
||||
}
|
||||
|
@ -137,65 +125,6 @@ public class VideoPlayer extends FrameLayout {
|
|||
this.window = window;
|
||||
}
|
||||
|
||||
private void setExoViewSource(@NonNull VideoSlide videoSource, boolean autoplay)
|
||||
throws IOException
|
||||
{
|
||||
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
|
||||
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
|
||||
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
|
||||
LoadControl loadControl = new DefaultLoadControl();
|
||||
|
||||
exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
|
||||
exoPlayer.addListener(new ExoPlayerListener(window));
|
||||
//noinspection ConstantConditions
|
||||
exoView.setPlayer(exoPlayer);
|
||||
//noinspection ConstantConditions
|
||||
exoControls.setPlayer(exoPlayer);
|
||||
|
||||
DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(getContext(), "GenericUserAgent", null);
|
||||
AttachmentDataSourceFactory attachmentDataSourceFactory = new AttachmentDataSourceFactory(getContext(), defaultDataSourceFactory, null);
|
||||
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
|
||||
|
||||
MediaSource mediaSource = new ExtractorMediaSource(videoSource.getUri(), attachmentDataSourceFactory, extractorsFactory, null, null);
|
||||
|
||||
exoPlayer.prepare(mediaSource);
|
||||
exoPlayer.setPlayWhenReady(autoplay);
|
||||
}
|
||||
|
||||
private void setVideoViewSource(@NonNull VideoSlide videoSource, boolean autoplay)
|
||||
throws IOException
|
||||
{
|
||||
if (this.attachmentServer != null) {
|
||||
this.attachmentServer.stop();
|
||||
}
|
||||
|
||||
if (videoSource.getUri() != null && PartAuthority.isLocalUri(videoSource.getUri())) {
|
||||
Log.i(TAG, "Starting video attachment server for part provider Uri...");
|
||||
this.attachmentServer = new AttachmentServer(getContext(), videoSource.asAttachment());
|
||||
this.attachmentServer.start();
|
||||
|
||||
//noinspection ConstantConditions
|
||||
this.videoView.setVideoURI(this.attachmentServer.getUri());
|
||||
} else if (videoSource.getUri() != null) {
|
||||
Log.i(TAG, "Playing video directly from non-local Uri...");
|
||||
//noinspection ConstantConditions
|
||||
this.videoView.setVideoURI(videoSource.getUri());
|
||||
} else {
|
||||
Toast.makeText(getContext(), getContext().getString(R.string.VideoPlayer_error_playing_video), Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (autoplay) this.videoView.start();
|
||||
}
|
||||
|
||||
private void initializeVideoViewControls(@NonNull VideoView videoView) {
|
||||
MediaController mediaController = new MediaController(getContext());
|
||||
mediaController.setAnchorView(videoView);
|
||||
mediaController.setMediaPlayer(videoView);
|
||||
|
||||
videoView.setMediaController(mediaController);
|
||||
}
|
||||
|
||||
private static class ExoPlayerListener extends Player.DefaultEventListener {
|
||||
private final Window window;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue