Skip to content

Commit

Permalink
增加rtsp server端代码,代码来自git项目spydroid-ipcamera
Browse files Browse the repository at this point in the history
  • Loading branch information
zpf committed Mar 15, 2018
1 parent 29fe63e commit b3a03c2
Show file tree
Hide file tree
Showing 20 changed files with 3,644 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
useLibrary 'org.apache.http.legacy'
buildTypes {
release {
minifyEnabled false
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zpf.androidshow">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.CAMERA" />
Expand All @@ -24,6 +26,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name="com.zpf.androidshow.rtsp.RtspServer"/>
</application>

</manifest>
101 changes: 101 additions & 0 deletions app/src/main/java/com/zpf/androidshow/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package com.zpf.androidshow;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zpf.androidshow.media.h264data;
import com.zpf.androidshow.rtsp.RtspServer;
import com.zpf.androidshow.screen.Constant;
import com.zpf.androidshow.screen.ScreenRecord;

import java.util.Locale;
import java.util.concurrent.ArrayBlockingQueue;

/**
* Created by zpf on 2018/3/6
*
Expand All @@ -29,18 +44,85 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe

private Button start_record,stop_record;

private TextView line2;

private MediaProjectionManager mMediaProjectionManager;

private ScreenRecord mScreenRecord;

private boolean isRecording = false;

private static int yuvqueuesize = 10;
public static ArrayBlockingQueue<h264data> h264Queue = new ArrayBlockingQueue<>(yuvqueuesize);
private RtspServer mRtspServer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitView();
InitMPManager();
displayIpAddress();
}

private ServiceConnection mRtspServiceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mRtspServer = ((RtspServer.LocalBinder)service).getService();
mRtspServer.addCallbackListener(mRtspCallbackListener);
mRtspServer.start();
}

@Override
public void onServiceDisconnected(ComponentName name) {}

};

@Override
protected void onResume(){
super.onResume();


}

private RtspServer.CallbackListener mRtspCallbackListener = new RtspServer.CallbackListener() {

@Override
public void onError(RtspServer server, Exception e, int error) {
// We alert the user that the port is already used by another app.
if (error == RtspServer.ERROR_BIND_FAILED) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Port already in use !")
.setMessage("You need to choose another port for the RTSP server !")
.show();
}
}

@Override
public void onMessage(RtspServer server, int message) {
if (message==RtspServer.MESSAGE_STREAMING_STARTED) {
// if (mAdapter != null && mAdapter.getHandsetFragment() != null)
// mAdapter.getHandsetFragment().update();

} else if (message==RtspServer.MESSAGE_STREAMING_STOPPED) {
// if (mAdapter != null && mAdapter.getHandsetFragment() != null)
// mAdapter.getHandsetFragment().update();
}
}

};


public static void putData(byte[] buffer, int type,long ts) {
if (h264Queue.size() >= 10) {
h264Queue.poll();
}
h264data data = new h264data();
data.data = buffer;
data.type = type;
data.ts = ts;
h264Queue.add(data);
}

/**
Expand All @@ -51,6 +133,7 @@ private void InitView(){
start_record.setOnClickListener(this);
stop_record = findViewById(R.id.stop_record);
stop_record.setOnClickListener(this);
line2 = (TextView)findViewById(R.id.line2);
}

/**
Expand All @@ -68,6 +151,7 @@ private void StartScreenCapture(){
isRecording = true;
Intent captureIntent = mMediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, REQUEST_CODE_A);
bindService(new Intent(this,RtspServer.class), mRtspServiceConnection, Context.BIND_AUTO_CREATE);
}


Expand All @@ -77,6 +161,8 @@ private void StartScreenCapture(){
private void StopScreenCapture(){
isRecording = false;
mScreenRecord.release();
if (mRtspServer != null) mRtspServer.removeCallbackListener(mRtspCallbackListener);
unbindService(mRtspServiceConnection);
}


Expand Down Expand Up @@ -115,4 +201,19 @@ public void onClick(View view) {
break;
}
}


private void displayIpAddress() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ipaddress = null;
if (info!=null && info.getNetworkId()>-1) {
int i = info.getIpAddress();
String ip = String.format(Locale.ENGLISH,"%d.%d.%d.%d", i & 0xff, i >> 8 & 0xff,i >> 16 & 0xff,i >> 24 & 0xff);
line2.setText("rtsp://");
line2.append(ip);
line2.append(":8086");
}

}
}
38 changes: 23 additions & 15 deletions app/src/main/java/com/zpf/androidshow/media/VideoMediaCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.Log;
import android.view.Surface;

import com.zpf.androidshow.MainActivity;
import com.zpf.androidshow.screen.Constant;

import java.io.BufferedOutputStream;
Expand Down Expand Up @@ -48,7 +49,7 @@ private void createfile(){
*
* **/
public VideoMediaCodec(){
createfile();
//createfile();
prepare();
}

Expand Down Expand Up @@ -105,17 +106,24 @@ public void getBuffer(){
if(mBufferInfo.flags == 2){
configbyte = new byte[mBufferInfo.size];
configbyte = outData;
}else if(mBufferInfo.flags == 1){
}
// else{
// MainActivity.putData(outData,2,mBufferInfo.presentationTimeUs*1000L);
// }

else if(mBufferInfo.flags == 1){
byte[] keyframe = new byte[mBufferInfo.size + configbyte.length];
System.arraycopy(configbyte, 0, keyframe, 0, configbyte.length);
System.arraycopy(outData, 0, keyframe, configbyte.length, outData.length);
if(outputStream != null){
outputStream.write(keyframe, 0, keyframe.length);
}
MainActivity.putData(keyframe,1,mBufferInfo.presentationTimeUs*1000L);
// if(outputStream != null){
// outputStream.write(keyframe, 0, keyframe.length);
// }
}else{
if(outputStream != null){
outputStream.write(outData, 0, outData.length);
}
MainActivity.putData(outData,2,mBufferInfo.presentationTimeUs*1000L);
// if(outputStream != null){
// outputStream.write(outData, 0, outData.length);
// }
}
mEncoder.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mEncoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC);
Expand All @@ -132,12 +140,12 @@ public void getBuffer(){
} catch (Exception e){
e.printStackTrace();
}
try {
outputStream.flush();
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
// try {
// outputStream.flush();
// outputStream.close();
// outputStream = null;
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}
Loading

0 comments on commit b3a03c2

Please sign in to comment.