Skip to content

Commit

Permalink
Fixed two possible NullPointerException
Browse files Browse the repository at this point in the history
  • Loading branch information
fyhertz committed May 3, 2013
1 parent b176bae commit 51c55ba
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/net/majorkernelpanic/http/TinyHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ public void stop() {

@Override
public void onCreate() {

super.onCreate();

mContext = getApplicationContext();
Expand Down
2 changes: 1 addition & 1 deletion src/net/majorkernelpanic/spydroid/SpydroidApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ else if (key.equals("notification_enabled")) {

private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
public void onReceive(Context context, Intent intent) {
batteryLevel = intent.getIntExtra("level", 0);
}
};
Expand Down
38 changes: 20 additions & 18 deletions src/net/majorkernelpanic/spydroid/ui/HandsetFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,26 @@ public void update() {
getActivity().runOnUiThread(new Runnable () {
@Override
public void run() {
if (mHttpServer != null && mRtspServer != null) {
if (!mHttpServer.isHttpEnabled() && !mHttpServer.isHttpsEnabled()) {
mDescription1.setVisibility(View.INVISIBLE);
mLine1.setVisibility(View.INVISIBLE);
} else {
mDescription1.setVisibility(View.VISIBLE);
mLine1.setVisibility(View.VISIBLE);
}
if (!mRtspServer.isEnabled()) {
mDescription2.setVisibility(View.INVISIBLE);
mLine2.setVisibility(View.INVISIBLE);
} else {
mDescription2.setVisibility(View.VISIBLE);
mLine2.setVisibility(View.VISIBLE);
}
if (!mHttpServer.isStreaming() && !mRtspServer.isStreaming()) displayIpAddress();
else streamingState(1);
}
if (mDescription1 != null) {
if (mHttpServer != null && mRtspServer != null) {
if (!mHttpServer.isHttpEnabled() && !mHttpServer.isHttpsEnabled()) {
mDescription1.setVisibility(View.INVISIBLE);
mLine1.setVisibility(View.INVISIBLE);
} else {
mDescription1.setVisibility(View.VISIBLE);
mLine1.setVisibility(View.VISIBLE);
}
if (!mRtspServer.isEnabled()) {
mDescription2.setVisibility(View.INVISIBLE);
mLine2.setVisibility(View.INVISIBLE);
} else {
mDescription2.setVisibility(View.VISIBLE);
mLine2.setVisibility(View.VISIBLE);
}
if (!mHttpServer.isStreaming() && !mRtspServer.isStreaming()) displayIpAddress();
else streamingState(1);
}
}
}
});
}
Expand Down
10 changes: 6 additions & 4 deletions src/net/majorkernelpanic/spydroid/ui/PreviewFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ public void update() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if ((mRtspServer != null && mRtspServer.isStreaming()) || (mHttpServer != null && mHttpServer.isStreaming()))
mTextView.setVisibility(View.INVISIBLE);
else
mTextView.setVisibility(View.VISIBLE);
if (mTextView != null) {
if ((mRtspServer != null && mRtspServer.isStreaming()) || (mHttpServer != null && mHttpServer.isStreaming()))
mTextView.setVisibility(View.INVISIBLE);
else
mTextView.setVisibility(View.VISIBLE);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/majorkernelpanic/streaming/MediaStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public int[] getLocalPorts() {
}

/**
* Sets the mode of the {@link MediaStreame}.
* Sets the mode of the {@link MediaStream}.
* If the mode is set to {@link #MODE_STREAMING}, video is forwarded to a UDP socket,
* and if the mode is {@link #MODE_DEFAULT}, video is recorded in a file.
* @param mode Either {@link #MODE_STREAMING} or {@link #MODE_DEFAULT}
Expand Down
2 changes: 1 addition & 1 deletion src/net/majorkernelpanic/streaming/audio/AACStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

/**
* A class for streaming AAC from the microphone of an android device using RTP.
* Call {@link #setDestination(java.net.InetAddress, int)}, {@link #prepare()} & {@link #start()} and that's it !
* Call {@link #setDestinationAddress(java.net.InetAddress)}, {@link #prepare()} & {@link #start()} and that's it !
* Call {@link #stop()} to stop the stream.
* Do not forget to call {@link #release()} when you're done.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/net/majorkernelpanic/streaming/audio/AMRNBStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/**
* A class for streaming AMR-NB from the microphone of an android device using RTP.
* Call {@link #setDestinationAddress(java.net.InetAddress), {@link #prepare()} & {@link #start()} and that's it !
* Call {@link #setDestinationAddress(java.net.InetAddress)}, {@link #prepare()} & {@link #start()} and that's it !
* Call {@link #stop()} to stop the stream.
* Do not forget to call {@link #release()} when you're done.
*/
Expand Down
44 changes: 28 additions & 16 deletions src/net/majorkernelpanic/streaming/rtsp/RtspServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.majorkernelpanic.http.TinyHttpServer;
import net.majorkernelpanic.streaming.Session;
import net.majorkernelpanic.streaming.SessionBuilder;
import android.app.Service;
Expand Down Expand Up @@ -80,10 +79,10 @@ public class RtspServer extends Service {
public final static int MESSAGE_STREAMING_STOPPED = 0X01;

/** Key used in the SharedPreferences to store whether the RTSP server is enabled or not. */
protected String mEnabledKey = "rtsp_enabled";
public final static String KEY_ENABLED = "rtsp_enabled";

/** Key used in the SharedPreferences for the port used by the RTSP server. */
protected String mPortKey = "rtsp_port";
public final static String KEY_PORT = "rtsp_port";

protected SessionBuilder mSessionBuilder;
protected SharedPreferences mSharedPreferences;
Expand Down Expand Up @@ -112,7 +111,7 @@ public interface CallbackListener {
}

/**
* See {@link TinyHttpServer.CallbackListener} to check out what events will be fired once you set up a listener.
* See {@link RtspServer.CallbackListener} to check out what events will be fired once you set up a listener.
* @param listener The listener
*/
public void addCallbackListener(CallbackListener listener) {
Expand Down Expand Up @@ -142,7 +141,7 @@ public int getPort() {
*/
public void setPort(int port) {
Editor editor = mSharedPreferences.edit();
editor.putString(mPortKey, String.valueOf(port));
editor.putString(KEY_PORT, String.valueOf(port));
editor.commit();
}

Expand Down Expand Up @@ -194,8 +193,8 @@ public void onCreate() {

// Let's restore the state of the service
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mPort = Integer.parseInt(mSharedPreferences.getString(mPortKey, String.valueOf(mPort)));
mEnabled = mSharedPreferences.getBoolean(mEnabledKey, mEnabled);
mPort = Integer.parseInt(mSharedPreferences.getString(KEY_PORT, String.valueOf(mPort)));
mEnabled = mSharedPreferences.getBoolean(KEY_ENABLED, mEnabled);

// If the configuration is modified, the server will adjust
mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
Expand All @@ -213,16 +212,16 @@ public void onDestroy() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

if (key.equals(mPortKey)) {
int port = Integer.parseInt(sharedPreferences.getString(mPortKey, String.valueOf(mPort)));
if (key.equals(KEY_PORT)) {
int port = Integer.parseInt(sharedPreferences.getString(KEY_PORT, String.valueOf(mPort)));
if (port != mPort) {
mPort = port;
mRestart = true;
start();
}
}
else if (key.equals(mEnabledKey)) {
mEnabled = sharedPreferences.getBoolean(mEnabledKey, mEnabled);
else if (key.equals(KEY_ENABLED)) {
mEnabled = sharedPreferences.getBoolean(KEY_ENABLED, mEnabled);
start();
}
}
Expand Down Expand Up @@ -260,6 +259,22 @@ protected void postError(Exception exception, int id) {
}
}

/**
* By default the RTSP uses {@link UriParser} to parse the URI requested by the client
* but you can change that behavior by override this method.
* @param uri The uri that the client has requested
* @param client The socket associated to the client
* @return A proper session
*/
protected Session handleRequest(String uri, Socket client) throws IllegalStateException, IOException {
Session session = UriParser.parse(uri);
session.setOrigin(client.getLocalAddress());
if (session.getDestination()==null) {
session.setDestination(client.getInetAddress());
}
return session;
}

class RequestListener extends Thread implements Runnable {

private final ServerSocket mServer;
Expand Down Expand Up @@ -391,12 +406,9 @@ public Response processRequest(Request request) throws IllegalStateException, IO
if (request.method.equalsIgnoreCase("DESCRIBE")) {

// Parse the requested URI and configure the session
mSession = UriParser.parse(request.uri);
mSession = handleRequest(request.uri, mClient);
mSessions.put(mSession, null);
mSession.setOrigin(mClient.getLocalAddress());
if (mSession.getDestination()==null) {
mSession.setDestination(mClient.getInetAddress());
}

String requestContent = mSession.getSessionDescription();
String requestAttributes =
"Content-Base: "+mClient.getLocalAddress().getHostAddress()+":"+mClient.getLocalPort()+"/\r\n" +
Expand Down
2 changes: 1 addition & 1 deletion src/net/majorkernelpanic/streaming/rtsp/UriParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public class UriParser {
* <li>rtsp://xxx.xxx.xxx.xxx:8086?h264=200-20-320-240</li>
* <li>rtsp://xxx.xxx.xxx.xxx:8086?aac</li></ul>
* @param uri The URI
* @param session The Session that will be configured
* @throws IllegalStateException
* @throws IOException
* @return A Session configured according to the URI
*/
public static Session parse(String uri) throws IllegalStateException, IOException {
SessionBuilder builder = SessionBuilder.getInstance().clone();
Expand Down
6 changes: 3 additions & 3 deletions src/net/majorkernelpanic/streaming/video/VideoStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public VideoStream() {

/**
* Don't use this class directly
* @param cameraId Can be either CameraInfo.CAMERA_FACING_BACK or CameraInfo.CAMERA_FACING_FRONT
* @param camera Can be either CameraInfo.CAMERA_FACING_BACK or CameraInfo.CAMERA_FACING_FRONT
*/
public VideoStream(int camera) {
super();
Expand All @@ -65,7 +65,7 @@ public VideoStream(int camera) {
/**
* Sets the camera that will be used to capture video.
* You can call this method at any time and changes will take effect next time you call {@link #prepare()}.
* @param cameraId Can be either CameraInfo.CAMERA_FACING_BACK or CameraInfo.CAMERA_FACING_FRONT
* @param camera Can be either CameraInfo.CAMERA_FACING_BACK or CameraInfo.CAMERA_FACING_FRONT
*/
public void setCamera(int camera) {
CameraInfo cameraInfo = new CameraInfo();
Expand Down Expand Up @@ -196,7 +196,7 @@ public synchronized void stop() {

/**
* Prepare the VideoStream, you can then call {@link #start()}.
* The underlying Camera will be opened and configured whaen you call this method so don't forget to deal with the RuntimeExceptions !
* The underlying Camera will be opened and configured when you call this method so don't forget to deal with the RuntimeExceptions !
* Camera.open, Camera.setParameter, Camera.unlock may throw one !
*/
public void prepare() throws IllegalStateException, IOException {
Expand Down

0 comments on commit 51c55ba

Please sign in to comment.