Skip to content

Commit

Permalink
Important modification of VideoStream, I had some problem with a SIII…
Browse files Browse the repository at this point in the history
… and the MediaRecorder API, this solved the problem.
  • Loading branch information
fyhertz committed Oct 21, 2013
1 parent 90a4de7 commit 05dd8f7
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 119 deletions.
5 changes: 2 additions & 3 deletions src/net/majorkernelpanic/streaming/audio/AACStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;

import net.majorkernelpanic.streaming.rtp.AACADTSPacketizer;
Expand Down Expand Up @@ -289,10 +288,10 @@ private void testADTS() throws IllegalStateException, IOException {
break;
}
}
// If he did, we force a reasonable one: 24 kHz
// If he did, we force a reasonable one: 16 kHz
if (i>12) {
Log.e(TAG,"Not a valid sampling rate: "+mQuality.samplingRate);
mQuality.samplingRate = 24000;
mQuality.samplingRate = 16000;
}

if (mSettings!=null) {
Expand Down
37 changes: 26 additions & 11 deletions src/net/majorkernelpanic/streaming/rtsp/RtspClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class RtspClient {
private String mPath;
private String mSessionID;
private String mAuthorization;
private String mRtspServerName;
private Session mSession;
private BufferedReader mBufferedReader;
private OutputStream mOutputStream;
Expand Down Expand Up @@ -187,6 +188,8 @@ private void sendRequestAnnounce() throws IllegalStateException, SocketException
mOutputStream.write(request.getBytes("UTF-8"));
Response response = Response.parseResponse(mBufferedReader);

mRtspServerName = response.headers.get("server");

try {
Matcher m = Response.rexegSession.matcher(response.headers.get("session"));
m.find();
Expand Down Expand Up @@ -243,10 +246,8 @@ private void sendRequestSetup() throws IllegalStateException, SocketException, I
Stream stream = mSession.getTrack(i);
if (stream != null) {
String request = "SETUP rtsp://"+mHost+":"+mPort+mPath+"/trackID="+i+" RTSP/1.0\r\n" +
"CSeq: " + (++mCSeq) + "\r\n" +
"Transport: RTP/AVP/UDP;unicast;client_port="+(5000+2*i)+"-"+(5000+2*i+1)+";mode=receive\r\n" +
"Session: " + mSessionID + "\r\n" +
"Authorization: " + mAuthorization+ "\r\n\r\n";
addHeaders();

Log.i(TAG,request.substring(0, request.indexOf("\r\n")));

Expand All @@ -272,9 +273,7 @@ private void sendRequestSetup() throws IllegalStateException, SocketException, I
private void sendRequestRecord() throws IllegalStateException, SocketException, IOException {
String request = "RECORD rtsp://"+mHost+":"+mPort+mPath+" RTSP/1.0\r\n" +
"Range: npt=0.000-" +
"CSeq: " + (++mCSeq) + "\r\n" +
"Session: " + mSessionID + "\r\n" +
"Authorization: " + mAuthorization+ "\r\n\r\n";
addHeaders();
Log.i(TAG,request.substring(0, request.indexOf("\r\n")));
mOutputStream.write(request.getBytes("UTF-8"));
Response.parseResponse(mBufferedReader);
Expand All @@ -284,15 +283,31 @@ private void sendRequestRecord() throws IllegalStateException, SocketException,
* Forges and sends the TEARDOWN request
*/
private void sendRequestTeardown() throws IOException {
String request = "TEARDOWN rtsp://"+mHost+":"+mPort+mPath+" RTSP/1.0\r\n" +
"CSeq: " + (++mCSeq) + "\r\n" +
"Session: " + mSessionID + "\r\n" +
"Authorization: " + mAuthorization+ "\r\n";
String request = "TEARDOWN rtsp://"+mHost+":"+mPort+mPath+" RTSP/1.0\r\n" + addHeaders();
Log.i(TAG,request.substring(0, request.indexOf("\r\n")));
mOutputStream.write(request.getBytes("UTF-8"));
Response.parseResponse(mBufferedReader);
}

private String addHeaders() {
String delimiter = "\r\n";
/*if (mRtspServerName != null && mRtspServerName.contains("Wowza")) {
Log.e(TAG,mRtspServerName);
// On certain versions of Wowza it appears that this is necessary
try {
Pattern regex = Pattern.compile("build(\\d+)",Pattern.CASE_INSENSITIVE);
Matcher matcher = regex.matcher(mRtspServerName);
if (Integer.parseInt(matcher.group(1))<5334) {
delimiter = "\r\n\r\n";
}
} catch (Exception ignore) {ignore.printStackTrace();}
}*/
return "CSeq: " + (++mCSeq) + "\r\n" +
"Content-Length: 0\r\n" +
"Session: " + mSessionID + "\r\n" +
"Authorization: " + mAuthorization+ delimiter;
}

final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

private static String bytesToHex(byte[] bytes) {
Expand Down Expand Up @@ -346,13 +361,13 @@ public static Response parseResponse(BufferedReader input) throws IOException, I

// Parsing headers of the request
while ( (line = input.readLine()) != null && line.length()>3 ) {
//Log.e(TAG,line);
matcher = rexegHeader.matcher(line);
matcher.find();
response.headers.put(matcher.group(1).toLowerCase(Locale.US),matcher.group(2));
}
if (line==null) throw new SocketException("Connection lost");

// It's not an error, it's just easier to follow what's happening in logcat with the request in red
Log.d(TAG, "Response from server: "+response.status);

return response;
Expand Down
21 changes: 13 additions & 8 deletions src/net/majorkernelpanic/streaming/video/H264Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,17 @@ private MP4Config testH264() throws IllegalStateException, IOException {
boolean savedFlashState = mFlashState;
mFlashState = false;

// Opens the camera if needed
if (mCamera == null) {
mCameraOpenedManually = false;
createCamera();

// Stops the preview if needed
if (mPreviewStarted) {
lockCamera();
try {
mCamera.stopPreview();
} catch (Exception e) {}
mPreviewStarted = false;
}

// Will start the preview if not already started !
startPreview();


try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
Expand Down Expand Up @@ -184,7 +187,9 @@ public void onInfo(MediaRecorder mr, int what, int extra) {
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mMediaRecorder.stop();
try {
mMediaRecorder.stop();
} catch (Exception e) {}
mMediaRecorder.release();
mMediaRecorder = null;
lockCamera();
Expand Down
Loading

0 comments on commit 05dd8f7

Please sign in to comment.