forked from guardianproject/IOCipherExample
/
MjpegViewerActivity.java
161 lines (124 loc) · 4.11 KB
/
MjpegViewerActivity.java
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
package info.guardianproject.iocipher.camera.viewer;
import info.guardianproject.iocipher.File;
import info.guardianproject.iocipher.FileInputStream;
import info.guardianproject.iocipher.camera.MediaConstants;
import info.guardianproject.iocipher.camera.encoders.AACHelper;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class MjpegViewerActivity extends Activity {
private static final String TAG = "MjpegActivity";
private MjpegView mv;
private AudioTrack at;
InputStream isAudio = null;
AACHelper aac = null;
boolean useAAC = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//prevent screenshots
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
mv = new MjpegView(this);
setContentView(mv);
final String ioCipherVideoPath = getIntent().getExtras().getString("video");
final String ioCipherAudioPath = getIntent().getExtras().getString("audio");
try {
mv.setDisplayMode(MjpegView.SIZE_BEST_FIT);
//mv.showFps(false);
mv.setFrameDelay(5); //we need to better sync each frame to the audio
File fileAudio = null;
if (ioCipherAudioPath == null)
{
fileAudio = new File(ioCipherVideoPath + ".pcm");
if (!fileAudio.exists())
{
fileAudio = new File(ioCipherVideoPath + ".aac");
useAAC = true;
}
}
else
{
fileAudio = new File(ioCipherAudioPath);
if (getIntent().getExtras().containsKey("useAAC"))
useAAC = getIntent().getExtras().getBoolean("useAAC",false);
else if (ioCipherAudioPath.endsWith(".aac"))
useAAC = true;
}
if (fileAudio.exists())
{
initAudio(fileAudio.getAbsolutePath());
new Thread ()
{
public void run ()
{
try {
playAudio();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
mv.setSource(new MjpegInputStream(new FileInputStream(ioCipherVideoPath)));
}
catch (Exception e) {
Log.e(TAG,"an error occcured init'g video playback",e);
}
}
public void initAudio(String vfsPath) throws Exception {
isAudio = new BufferedInputStream(new FileInputStream(vfsPath));
if (useAAC)
{
aac = new AACHelper();
aac.setDecoder(MediaConstants.sAudioSampleRate, MediaConstants.sAudioChannels, MediaConstants.sAudioBitRate);
}
else
{
int minBufferSize = AudioTrack.getMinBufferSize(MediaConstants.sAudioSampleRate,
MediaConstants.sChannelConfigOut, AudioFormat.ENCODING_PCM_16BIT)*8;
at = new AudioTrack(AudioManager.STREAM_MUSIC, MediaConstants.sAudioSampleRate,
MediaConstants.sChannelConfigOut, AudioFormat.ENCODING_PCM_16BIT,
minBufferSize, AudioTrack.MODE_STREAM);
}
}
public void playAudio () throws IOException
{
if (useAAC)
aac.startPlaying(isAudio);
else
{
try{
byte[] music = null;
music = new byte[512];
at.play();
int i = 0;
while((i = isAudio.read(music)) != -1)
at.write(music, 0, i);
} catch (IOException e) {
e.printStackTrace();
}
at.stop();
at.release();
isAudio.close();
at = null;
}
}
public void onPause() {
super.onPause();
mv.stopPlayback();
if (at!=null)
at.stop();
}
}