mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-10 08:08:34 +00:00
Remove video player usage of AttachmentServer.
This commit is contained in:
parent
a5368b7ea9
commit
8a8817f8d3
@ -45,12 +45,9 @@ public class MediaSendVideoFragment extends Fragment implements MediaSendPageFra
|
|||||||
|
|
||||||
uri = getArguments().getParcelable(KEY_URI);
|
uri = getArguments().getParcelable(KEY_URI);
|
||||||
VideoSlide slide = new VideoSlide(requireContext(), uri, 0);
|
VideoSlide slide = new VideoSlide(requireContext(), uri, 0);
|
||||||
try {
|
|
||||||
((VideoPlayer) view).setWindow(requireActivity().getWindow());
|
((VideoPlayer) view).setWindow(requireActivity().getWindow());
|
||||||
((VideoPlayer) view).setVideoSource(slide, false);
|
((VideoPlayer) view).setVideoSource(slide, false);
|
||||||
} catch (IOException e) {
|
|
||||||
Log.w(TAG, "Failed to play video.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
package org.thoughtcrime.securesms.video;
|
package org.thoughtcrime.securesms.video;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
@ -25,9 +24,6 @@ import android.view.View;
|
|||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.widget.FrameLayout;
|
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.DefaultLoadControl;
|
||||||
import com.google.android.exoplayer2.ExoPlayerFactory;
|
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 com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.R;
|
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.mms.VideoSlide;
|
||||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||||
import org.thoughtcrime.securesms.video.exo.AttachmentDataSourceFactory;
|
import org.thoughtcrime.securesms.video.exo.AttachmentDataSourceFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class VideoPlayer extends FrameLayout {
|
public class VideoPlayer extends FrameLayout {
|
||||||
|
|
||||||
private static final String TAG = VideoPlayer.class.getSimpleName();
|
private static final String TAG = VideoPlayer.class.getSimpleName();
|
||||||
|
|
||||||
@Nullable private final VideoView videoView;
|
private final PlayerView exoView;
|
||||||
@Nullable private final PlayerView exoView;
|
|
||||||
|
|
||||||
@Nullable private SimpleExoPlayer exoPlayer;
|
private SimpleExoPlayer exoPlayer;
|
||||||
@Nullable private PlayerControlView exoControls;
|
private PlayerControlView exoControls;
|
||||||
@Nullable private AttachmentServer attachmentServer;
|
private Window window;
|
||||||
@Nullable private Window window;
|
|
||||||
|
|
||||||
public VideoPlayer(Context context) {
|
public VideoPlayer(Context context) {
|
||||||
this(context, null);
|
this(context, null);
|
||||||
@ -83,31 +72,34 @@ public class VideoPlayer extends FrameLayout {
|
|||||||
|
|
||||||
inflate(context, R.layout.video_player, this);
|
inflate(context, R.layout.video_player, this);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= 16) {
|
this.exoView = ViewUtil.findById(this, R.id.video_view);
|
||||||
this.exoView = ViewUtil.findById(this, R.id.video_view);
|
this.exoControls = new PlayerControlView(getContext());
|
||||||
this.videoView = null;
|
this.exoControls.setShowTimeoutMs(-1);
|
||||||
this.exoControls = new PlayerControlView(getContext());
|
|
||||||
this.exoControls.setShowTimeoutMs(-1);
|
|
||||||
} else {
|
|
||||||
this.videoView = ViewUtil.findById(this, R.id.video_view);
|
|
||||||
this.exoView = null;
|
|
||||||
initializeVideoViewControls(videoView);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVideoSource(@NonNull VideoSlide videoSource, boolean autoplay)
|
public void setVideoSource(@NonNull VideoSlide videoSource, boolean autoplay) {
|
||||||
throws IOException
|
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
|
||||||
{
|
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
|
||||||
if (Build.VERSION.SDK_INT >= 16) setExoViewSource(videoSource, autoplay);
|
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
|
||||||
else setVideoViewSource(videoSource, autoplay);
|
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() {
|
public void pause() {
|
||||||
if (this.attachmentServer != null && this.videoView != null) {
|
this.exoPlayer.setPlayWhenReady(false);
|
||||||
this.videoView.stopPlayback();
|
|
||||||
} else if (this.exoPlayer != null) {
|
|
||||||
this.exoPlayer.setPlayWhenReady(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void hideControls() {
|
public void hideControls() {
|
||||||
@ -124,10 +116,6 @@ public class VideoPlayer extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
if (this.attachmentServer != null) {
|
|
||||||
this.attachmentServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.exoPlayer != null) {
|
if (this.exoPlayer != null) {
|
||||||
this.exoPlayer.release();
|
this.exoPlayer.release();
|
||||||
}
|
}
|
||||||
@ -137,65 +125,6 @@ public class VideoPlayer extends FrameLayout {
|
|||||||
this.window = window;
|
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 static class ExoPlayerListener extends Player.DefaultEventListener {
|
||||||
private final Window window;
|
private final Window window;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user