说明:该android代码h263硬编码rtp打包发送服务器,pc端建立sdp文件用vlc播放
1、服务器ip为192.168.1.6
2、android手机ip为192.168.1.17,在小米1s测试通过,像三星等手机不支持h263编码
3、android端修改net.config包下的Config类的url
4、pc端用已有的sdp文件播放,修改ip为手机ip
package com.lee.h263recorder.activity;
import java.io.IOException;
import java.net.InetAddress;
import net.config.Config;
import net.majorkernelpanic.rtp.AbstractPacketizer;
import net.majorkernelpanic.rtp.H263Packetizer;
import android.media.MediaRecorder;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.SurfaceHolder.Callback;
public class Main extends Activity implements Callback{
private static final String TAG = "VideoCamera";
SharedPreferences sharedPreferences;
private final String mediaShare = "media";
//初始化LocalServerSocket LocalSocket
LocalServerSocket lss;
LocalSocket receiver, sender;
protected AbstractPacketizer packetizer = null;
protected boolean streaming = false, modeDefaultWasUsed = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InitSurfaceView();
InitMediaSharePreference();
InitAbstractPacketizer();
}
private void InitAbstractPacketizer() {
try {
packetizer = new H263Packetizer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//初始化SurfaceView
private SurfaceView mSurfaceView;
private void InitSurfaceView() {
mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
SurfaceHolder holder = mSurfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceView.setVisibility(View.VISIBLE);
}
private void InitMediaSharePreference() {
sharedPreferences = this.getSharedPreferences(mediaShare, MODE_PRIVATE);
}
private SurfaceHolder mSurfaceHolder;
private boolean mMediaRecorderRecording = false;
public void surfaceCreated(SurfaceHolder holder) {
mSurfaceHolder = holder;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mSurfaceHolder = holder;
if(!mMediaRecorderRecording) {
InitLocalSocket();
initializeVideo();
startVideoRecording();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
private void InitLocalSocket(){
try {
lss = new LocalServerSocket("H263");
receiver = new LocalSocket();
receiver.connect(new LocalSocketAddress("H263"));
receiver.setReceiveBufferSize(500000);
sender = lss.accept();
sender.setSendBufferSize(500000);
} catch (IOException e) {
Log.e(TAG, e.toString());
this.finish();
return;
}
}
//初始化MediaRecorder
private MediaRecorder mMediaRecorder = null;
private int videoWidth = 640; //352
private int videoHeight = 480; //288
private int videoRate = 24; //15
private boolean initializeVideo(){
if(mSurfaceHolder == null) {
return false;
}
mMediaRecorderRecording = true;
if(mMediaRecorder == null) {
mMediaRecorder = new MediaRecorder();
} else {
mMediaRecorder.reset();
}
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setVideoFrameRate(videoRate);
mMediaRecorder.setVideoSize(videoWidth, videoHeight);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setMaxDuration(0);
mMediaRecorder.setMaxFileSize(Integer.MAX_VALUE);
mMediaRecorder.setOutputFile(sender.getFileDescriptor());
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
releaseMediaRecorder();
}
return true;
}
//释放MediaRecorder资源
private void releaseMediaRecorder(){
if(mMediaRecorder != null) {
if(mMediaRecorderRecording) {
mMediaRecorder.stop();
mMediaRecorderRecording = false;
}
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
}
}
//开始录像,启动线程
private void startVideoRecording() {
try {
setDestination(InetAddress.getByName(Config.host),Config.video_port);
packetizer.setInputStream(receiver.getInputStream());
packetizer.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(mMediaRecorderRecording)
{
releaseMediaRecorder();
try {
lss.close();
receiver.close();
sender.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
}
mMediaRecorderRecording = false;
}
finish();
}
/** The stream will be sent to the address specified by this function **/
public void setDestination(InetAddress dest, int dport) {
this.packetizer.setDestination(dest, dport);
}
/** Set the Time To Live of the underlying RtpSocket
* @throws IOException **/
public void setTimeToLive(int ttl) throws IOException {
this.packetizer.setTimeToLive(ttl);
}
public int getDestinationPort() {
return this.packetizer.getRtpSocket().getPort();
}
public int getLocalPort() {
return this.packetizer.getRtpSocket().getLocalPort();
}
public void setMode(int mode) throws IllegalStateException {
if (!streaming) {
modeDefaultWasUsed = true;
}
else {
throw new IllegalStateException("You can't call setMode() while streaming !");
}
}
public AbstractPacketizer getPacketizer() {
return packetizer;
}
public boolean isStreaming() {
return streaming;
}
}
资源文件列表
android h263╙▓▒α┬δrtp┤≥░ⁿ/h263.sdp , 83
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/.classpath , 364
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/.project , 848
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/AndroidManifest.xml , 1548
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/AndroidManifest.xml , 1548
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes.dex , 469256
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/BuildConfig.class , 367
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/Main.class , 6794
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$attr.class , 373
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$dimen.class , 494
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$drawable.class , 440
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$id.class , 464
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$layout.class , 427
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$menu.class , 421
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$string.class , 505
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R$style.class , 464
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/com/lee/h263recorder/activity/R.class , 783
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/config/Config.class , 481
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/AACADTSPacketizer.class , 2636
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/AbstractPacketizer.class , 2050
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/AMRNBPacketizer.class , 2830
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/H263Packetizer$Statistics.class , 912
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/H263Packetizer.class , 3578
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/H264Packetizer$Statistics.class , 912
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/H264Packetizer.class , 3373
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/rtp/RtpSocket.class , 3140
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/classes/net/majorkernelpanic/streaming/video/VideoQuality.class , 2394
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/dexedLibs/android-support-v4-b6794c347aa204f134b8705585877bd1.jar , 151838
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/H263Recorder.apk , 204019
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/resources.ap_ , 41022
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/res/drawable-hdpi/ic_launcher.png , 5964
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/res/drawable-mdpi/ic_launcher.png , 3112
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/res/drawable-xhdpi/ic_launcher.png , 9355
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/bin/res/drawable-xxhdpi/ic_launcher.png , 17889
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/gen/com/lee/h263recorder/activity/BuildConfig.java , 171
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/gen/com/lee/h263recorder/activity/R.java , 2555
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/ic_launcher-web.png , 51394
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/libs/android-support-v4.jar , 393154
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/proguard-project.txt , 781
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/project.properties , 563
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/drawable-hdpi/ic_launcher.png , 7658
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/drawable-mdpi/ic_launcher.png , 3777
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/drawable-xhdpi/ic_launcher.png , 12516
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/drawable-xxhdpi/ic_launcher.png , 24777
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/layout/main.xml , 452
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/menu/main.xml , 261
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values-sw600dp/dimens.xml , 201
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values-sw720dp-land/dimens.xml , 275
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values-v11/styles.xml , 332
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values-v14/styles.xml , 389
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values/dimens.xml , 218
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values/strings.xml , 227
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/res/values/styles.xml , 695
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/com/lee/h263recorder/activity/Main.java , 6588
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/config/Config.java , 182
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/rtp/AACADTSPacketizer.java , 3701
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/rtp/AbstractPacketizer.java , 2144
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/rtp/AMRNBPacketizer.java , 3481
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/rtp/H263Packetizer.java , 4587
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/rtp/H264Packetizer.java , 5331
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/rtp/RtpSocket.java , 3810
android h263╙▓▒α┬δrtp┤≥░ⁿ/H263Recorder/src/net/majorkernelpanic/streaming/video/VideoQuality.java , 3042
android h263╙▓▒α┬δrtp┤≥░ⁿ/readme.txt , 267
