-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoPlayer.java
More file actions
211 lines (175 loc) · 7.02 KB
/
Copy pathVideoPlayer.java
File metadata and controls
211 lines (175 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.ken.smartguard.video_stream.video_player;
import android.media.MediaCodec;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import com.ken.smartguard.R;
import com.ken.smartguard.video_stream.utils.ScreenUtils;
import com.ken.smartguard.video_stream.video_player.video_decoder.H264Decoder;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.BlockingQueue;
public class VideoPlayer
{
private static final String DEBUG_TAG = "Ken-Video-Display";
private final AppCompatActivity context;
private final H264Decoder videoDecoder;
private final View defaultDisplayContainer;
private final SurfaceView defaultDisplay;
private final View fullscreenOnToggle;
private boolean isDefaultDisplayCreated;
private final View fullscreenDisplayContainer;
private final SurfaceView fullscreenDisplay;
private final View fullscreenOffToggle;
private boolean isFullscreenDisplayCreated;
private final View initialView;
private final View loadingView;
private final View errorView;
private final VideoPlayerCallbacks videoPlayerCallbacks;
private boolean isFullscreenMode;
public VideoPlayer(AppCompatActivity context, VideoPlayerCallbacks videoPlayerCallbacks)
{
this.context = context;
this.videoPlayerCallbacks = videoPlayerCallbacks;
this.defaultDisplayContainer = context.findViewById(R.id.default_player_container);
this.defaultDisplay = context.findViewById(R.id.default_video_display);
this.defaultDisplay.getHolder().addCallback(new VideoDisplayEventHandler(0,
new SurfaceCreatedEventHandler(),
holder -> {isDefaultDisplayCreated = false;}));
this.fullscreenDisplayContainer = context.findViewById(R.id.fullscreen_video_display_holder);
this.fullscreenDisplay = context.findViewById(R.id.fullscreen_video_display);
this.fullscreenOffToggle = context.findViewById(R.id.video_fullscreen_toggle_fullscreen);
fullscreenOnToggle = context.findViewById(R.id.video_fullscreen_toggle);
fullscreenOnToggle.setOnClickListener( view ->
{
/* fullscreenOnToggle onClickListener */
if(videoPlayerCallbacks.onFullscreenRequest() && !isFullscreenMode)
{
//screen switched to fullscreen mode, switch now player also to fullscreen mode
Log.d(DEBUG_TAG, "Switching to fullscreen mode.");
isFullscreenMode = true;
defaultDisplay.setVisibility(View.GONE);
defaultDisplayContainer.setVisibility(View.GONE);
ScreenUtils.enableFullscreenMode(context, true);
}
else
Log.d(DEBUG_TAG, "Fullscreen request is rejected.");
});
this.initialView = context.findViewById(R.id.stream_video_initializing);
this.loadingView = context.findViewById(R.id.default_player_loading);
this.errorView = context.findViewById(R.id.stream_error);
this.videoDecoder = new H264Decoder(defaultDisplay.getHolder(), () ->
{
videoPlayerCallbacks.onVideoLoaded();
Log.d(DEBUG_TAG, "Channel changed.");
}, exception ->
{
if(exception instanceof IOException)
{
Log.e(DEBUG_TAG, "Decoder create error: " + exception.toString());
Log.d(DEBUG_TAG, "Encountered unrecoverable error. Card crashed.");
defaultDisplay.setVisibility(View.GONE);
//set and show message
//streamErrorText.setText(context.getResources().getString(R.string.stream_fatal_error));
//error(); //todo replace with onFatalError.run()
videoPlayerCallbacks.onDecoderFatalError();
}
else if(exception instanceof MediaCodec.CodecException)
{
Log.e(DEBUG_TAG, "Decoder decode error: " + exception.getLocalizedMessage());
throw new RuntimeException("Not implemented"); //todo handle this
}
});
}
/**
* Set player in the initial state.
*/
public void initial()
{
Log.d(DEBUG_TAG, "Initial state.");
this.defaultDisplay.setVisibility(View.GONE);
this.initialView.setVisibility(View.VISIBLE);
this.errorView.setVisibility(View.GONE);
}
/**
* Set player in loading state. E.g. waiting data from data-source.
*/
public void loading()
{
this.loadingView.setVisibility(View.VISIBLE);
}
public void loaded()
{
this.loadingView.setVisibility(View.GONE);
this.initialView.setVisibility(View.GONE);
this.fullscreenOnToggle.setVisibility(View.VISIBLE);
}
private BlockingQueue<ByteBuffer> dataBuffer;
/**
* Start the player in playing mode with a buffer that the data will be written in from the custom data-source, and read out from the H264Decoder
* @param dataBuffer The buffer contains video frames.
*/
public void begin(BlockingQueue<ByteBuffer> dataBuffer)
{
this.defaultDisplay.setVisibility(View.VISIBLE);
this.dataBuffer = dataBuffer;
if(isDefaultDisplayCreated)
startVideoPlaying();
else
defaultDisplay.setVisibility(View.VISIBLE);
}
private void startVideoPlaying()
{
if(dataBuffer != null)
{
videoDecoder.pause();
videoDecoder.enableRendering(true);
videoDecoder.start(dataBuffer);
}
}
/**
* Pause the video player. Triggered normally when activity paused.
*/
public void pause()
{
videoDecoder.pause(); //pause the decoder
}
public void error()
{
videoDecoder.pause();
this.initialView.setVisibility(View.GONE);
this.defaultDisplay.setVisibility(View.GONE);
this.errorView.setVisibility(View.VISIBLE);
}
/**
* Close the video player. Triggered normally when activity destroyed.
*/
public void close()
{
videoDecoder.close();
}
public boolean handleBackKeyPress()
{
return false;
}
private class SurfaceCreatedEventHandler implements VideoDisplayEventHandler.OnSurfaceCreatedCallback
{
@Override
public void accept(SurfaceHolder surfaceHolder)
{
Log.d(DEBUG_TAG, "Default Surface created");
isDefaultDisplayCreated = true;
//todo calculate from current video size
surfaceHolder.setFixedSize(0, 0); //todo surfaceHolder.setFixedSize(currentVideoDisplaySize.width(), currentVideoDisplaySize.height());
startVideoPlaying();
}
}
public interface VideoPlayerCallbacks
{
void onVideoLoaded();
void onDecoderFatalError();
boolean onFullscreenRequest();
}
}