You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
340 lines
12 KiB
Java
340 lines
12 KiB
Java
package net.ossrs.yasea;
|
|
|
|
import android.app.Activity;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.ActivityInfo;
|
|
import android.content.res.Configuration;
|
|
import android.os.Bundle;
|
|
import android.os.Environment;
|
|
import android.util.Log;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
import android.view.SurfaceView;
|
|
import android.view.View;
|
|
import android.view.WindowManager;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.Toast;
|
|
|
|
import net.ossrs.yasea.rtmp.RtmpPublisher;
|
|
|
|
import java.util.Random;
|
|
|
|
public class MainActivity extends Activity {
|
|
private static final String TAG = "Yasea";
|
|
|
|
Button btnPublish = null;
|
|
Button btnSwitchCamera = null;
|
|
Button btnRecord = null;
|
|
Button btnSwitchEncoder = null;
|
|
|
|
private String mNotifyMsg;
|
|
private SharedPreferences sp;
|
|
private String rtmpUrl = "rtmp://ossrs.net/" + getRandomAlphaString(3) + '/' + getRandomAlphaDigitString(5);
|
|
private String recPath = Environment.getExternalStorageDirectory().getPath() + "/test.mp4";
|
|
|
|
private SrsPublisher mPublisher = new SrsPublisher();
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
// response screen rotation event
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
|
|
|
|
// restore data.
|
|
sp = getSharedPreferences("Yasea", MODE_PRIVATE);
|
|
rtmpUrl = sp.getString("rtmpUrl", rtmpUrl);
|
|
|
|
// initialize url.
|
|
final EditText efu = (EditText) findViewById(R.id.url);
|
|
efu.setText(rtmpUrl);
|
|
|
|
btnPublish = (Button) findViewById(R.id.publish);
|
|
btnSwitchCamera = (Button) findViewById(R.id.swCam);
|
|
btnRecord = (Button) findViewById(R.id.record);
|
|
btnSwitchEncoder = (Button) findViewById(R.id.swEnc);
|
|
|
|
mPublisher.setSurfaceView((SurfaceView) findViewById(R.id.preview));
|
|
|
|
btnPublish.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (btnPublish.getText().toString().contentEquals("publish")) {
|
|
rtmpUrl = efu.getText().toString();
|
|
Log.i(TAG, String.format("RTMP URL changed to %s", rtmpUrl));
|
|
SharedPreferences.Editor editor = sp.edit();
|
|
editor.putString("rtmpUrl", rtmpUrl);
|
|
editor.commit();
|
|
|
|
mPublisher.setPreviewResolution(1280, 720);
|
|
mPublisher.setOutputResolution(384, 640);
|
|
mPublisher.setVideoHDMode();
|
|
mPublisher.startPublish(rtmpUrl);
|
|
|
|
if (btnSwitchEncoder.getText().toString().contentEquals("soft enc")) {
|
|
Toast.makeText(getApplicationContext(), "Use hard encoder", Toast.LENGTH_SHORT).show();
|
|
} else {
|
|
Toast.makeText(getApplicationContext(), "Use soft encoder", Toast.LENGTH_SHORT).show();
|
|
}
|
|
btnPublish.setText("stop");
|
|
btnSwitchEncoder.setEnabled(false);
|
|
} else if (btnPublish.getText().toString().contentEquals("stop")) {
|
|
mPublisher.stopPublish();
|
|
|
|
btnPublish.setText("publish");
|
|
btnRecord.setText("record");
|
|
btnSwitchEncoder.setEnabled(true);
|
|
}
|
|
}
|
|
});
|
|
|
|
btnSwitchCamera.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (mPublisher.getNumberOfCameras() > 0) {
|
|
mPublisher.switchCameraFace((mPublisher.getCamraId() + 1) % mPublisher.getNumberOfCameras());
|
|
}
|
|
}
|
|
});
|
|
|
|
btnRecord.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (btnRecord.getText().toString().contentEquals("record")) {
|
|
mPublisher.startRecord(recPath);
|
|
|
|
btnRecord.setText("pause");
|
|
} else if (btnRecord.getText().toString().contentEquals("pause")) {
|
|
mPublisher.pauseRecord();
|
|
btnRecord.setText("resume");
|
|
} else if (btnRecord.getText().toString().contentEquals("resume")) {
|
|
mPublisher.resumeRecord();
|
|
btnRecord.setText("pause");
|
|
}
|
|
}
|
|
});
|
|
|
|
btnSwitchEncoder.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (btnSwitchEncoder.getText().toString().contentEquals("soft enc")) {
|
|
mPublisher.swithToSoftEncoder();
|
|
btnSwitchEncoder.setText("hard enc");
|
|
} else if (btnSwitchEncoder.getText().toString().contentEquals("hard enc")) {
|
|
mPublisher.swithToHardEncoder();
|
|
btnSwitchEncoder.setText("soft enc");
|
|
}
|
|
}
|
|
});
|
|
|
|
mPublisher.setPublishEventHandler(new RtmpPublisher.EventHandler() {
|
|
@Override
|
|
public void onRtmpConnecting(String msg) {
|
|
mNotifyMsg = msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRtmpConnected(String msg) {
|
|
mNotifyMsg = msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRtmpVideoStreaming(String msg) {
|
|
}
|
|
|
|
@Override
|
|
public void onRtmpAudioStreaming(String msg) {
|
|
}
|
|
|
|
@Override
|
|
public void onRtmpStopped(String msg) {
|
|
mNotifyMsg = msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRtmpDisconnected(String msg) {
|
|
mNotifyMsg = msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRtmpOutputFps(final double fps) {
|
|
Log.i(TAG, String.format("Output Fps: %f", fps));
|
|
}
|
|
});
|
|
|
|
mPublisher.setRecordEventHandler(new SrsMp4Muxer.EventHandler() {
|
|
@Override
|
|
public void onRecordPause(String msg) {
|
|
mNotifyMsg = msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRecordResume(String msg) {
|
|
mNotifyMsg = msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRecordStarted(String msg) {
|
|
mNotifyMsg = "Recording file: " + msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onRecordFinished(String msg) {
|
|
mNotifyMsg = "MP4 file saved: " + msg;
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
|
@Override
|
|
public void uncaughtException(Thread thread, Throwable ex) {
|
|
mNotifyMsg = ex.getMessage();
|
|
runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(getApplicationContext(), mNotifyMsg, Toast.LENGTH_LONG).show();
|
|
mPublisher.stopPublish();
|
|
mPublisher.stopRecord();
|
|
btnPublish.setText("publish");
|
|
btnRecord.setText("record");
|
|
btnSwitchEncoder.setEnabled(true);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
// Handle action bar item clicks here. The action bar will
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
int id = item.getItemId();
|
|
|
|
//noinspection SimplifiableIfStatement
|
|
if (id == R.id.action_settings) {
|
|
return true;
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
final Button btn = (Button) findViewById(R.id.publish);
|
|
btn.setEnabled(true);
|
|
mPublisher.resumeRecord();
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
super.onPause();
|
|
mPublisher.pauseRecord();
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
mPublisher.stopPublish();
|
|
mPublisher.stopRecord();
|
|
}
|
|
|
|
@Override
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
super.onConfigurationChanged(newConfig);
|
|
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
|
|
mPublisher.setPreviewRotation(90);
|
|
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
|
mPublisher.setPreviewRotation(0);
|
|
}
|
|
mPublisher.stopEncode();
|
|
mPublisher.stopRecord();
|
|
btnRecord.setText("record");
|
|
mPublisher.setScreenOrientation(newConfig.orientation);
|
|
if (btnPublish.getText().toString().contentEquals("stop")) {
|
|
mPublisher.startEncode();
|
|
}
|
|
}
|
|
|
|
private static String getRandomAlphaString(int length) {
|
|
String base = "abcdefghijklmnopqrstuvwxyz";
|
|
Random random = new Random();
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int i = 0; i < length; i++) {
|
|
int number = random.nextInt(base.length());
|
|
sb.append(base.charAt(number));
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
private static String getRandomAlphaDigitString(int length) {
|
|
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
Random random = new Random();
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int i = 0; i < length; i++) {
|
|
int number = random.nextInt(base.length());
|
|
sb.append(base.charAt(number));
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|