Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] adding missing notifications on RtspServer.stop() #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions src/net/majorkernelpanic/streaming/rtsp/RtspServer.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (C) 2011-2014 GUIGUI Simon, [email protected]
*
*
* This file is part of libstreaming (https://github.com/fyhertz/libstreaming)
*
*
* Spydroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Expand Down Expand Up @@ -51,11 +51,11 @@

/**
* Implementation of a subset of the RTSP protocol (RFC 2326).
*
*
* It allows remote control of an android device cameras & microphone.
* For each connected client, a Session is instantiated.
* The Session will start or stop streams according to what the client wants.
*
*
*/
public class RtspServer extends Service {

Expand All @@ -75,10 +75,10 @@ public class RtspServer extends Service {

/** Streaming started. */
public final static int MESSAGE_STREAMING_STARTED = 0X00;

/** Streaming stopped. */
public final static int MESSAGE_STREAMING_STOPPED = 0X01;

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

Expand All @@ -87,10 +87,10 @@ public class RtspServer extends Service {

protected SessionBuilder mSessionBuilder;
protected SharedPreferences mSharedPreferences;
protected boolean mEnabled = true;
protected boolean mEnabled = true;
protected int mPort = DEFAULT_RTSP_PORT;
protected WeakHashMap<Session,Object> mSessions = new WeakHashMap<Session,Object>(2);

private RequestListener mListenerThread;
private final IBinder mBinder = new LocalBinder();
private boolean mRestart = false;
Expand All @@ -99,7 +99,7 @@ public class RtspServer extends Service {
/** Credentials for Basic Auth */
private String mUsername;
private String mPassword;


public RtspServer() {
}
Expand All @@ -112,7 +112,7 @@ public interface CallbackListener {

/** Called when streaming starts/stops. */
void onMessage(RtspServer server, int message);

}

/**
Expand All @@ -126,7 +126,7 @@ public void addCallbackListener(CallbackListener listener) {
if (cl == listener) return;
}
}
mListeners.add(listener);
mListeners.add(listener);
}
}

Expand All @@ -136,11 +136,11 @@ public void addCallbackListener(CallbackListener listener) {
*/
public void removeCallbackListener(CallbackListener listener) {
synchronized (mListeners) {
mListeners.remove(listener);
mListeners.remove(listener);
}
}

/** Returns the port used by the RTSP server. */
/** Returns the port used by the RTSP server. */
public int getPort() {
return mPort;
}
Expand All @@ -166,9 +166,9 @@ public void setAuthorization(String username, String password)
mPassword = password;
}

/**
* Starts (or restart if needed, if for example the configuration
* of the server has been modified) the RTSP server.
/**
* Starts (or restart if needed, if for example the configuration
* of the server has been modified) the RTSP server.
*/
public void start() {
if (!mEnabled || mRestart) stop();
Expand All @@ -182,9 +182,9 @@ public void start() {
mRestart = false;
}

/**
* Stops the RTSP server but not the Android Service.
* To stop the Android Service you need to call {@link android.content.Context#stopService(Intent)};
/**
* Stops the RTSP server but not the Android Service.
* To stop the Android Service you need to call {@link android.content.Context#stopService(Intent)};
*/
public void stop() {
if (mListenerThread != null) {
Expand All @@ -193,11 +193,12 @@ public void stop() {
for ( Session session : mSessions.keySet() ) {
if ( session != null ) {
if (session.isStreaming()) session.stop();
}
}
}
} catch (Exception e) {
} finally {
mListenerThread = null;
postMessage(MESSAGE_STREAMING_STOPPED);
}
}
}
Expand All @@ -207,11 +208,11 @@ public boolean isStreaming() {
for ( Session session : mSessions.keySet() ) {
if ( session != null ) {
if (session.isStreaming()) return true;
}
}
}
return false;
}

public boolean isEnabled() {
return mEnabled;
}
Expand All @@ -222,11 +223,11 @@ public long getBitrate() {
for ( Session session : mSessions.keySet() ) {
if ( session != null ) {
if (session.isStreaming()) bitrate += session.getBitrate();
}
}
}
return bitrate;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
Expand All @@ -235,7 +236,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onCreate() {

// Let's restore the state of the service
// Let's restore the state of the service
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mPort = Integer.parseInt(mSharedPreferences.getString(KEY_PORT, String.valueOf(mPort)));
mEnabled = mSharedPreferences.getBoolean(KEY_ENABLED, mEnabled);
Expand Down Expand Up @@ -263,7 +264,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
mRestart = true;
start();
}
}
}
else if (key.equals(KEY_ENABLED)) {
mEnabled = sharedPreferences.getBoolean(KEY_ENABLED, mEnabled);
start();
Expand All @@ -289,21 +290,21 @@ protected void postMessage(int id) {
for (CallbackListener cl : mListeners) {
cl.onMessage(this, id);
}
}
}
}
}
}

protected void postError(Exception exception, int id) {
synchronized (mListeners) {
if (mListeners.size() > 0) {
for (CallbackListener cl : mListeners) {
cl.onError(this, exception, 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
Expand All @@ -318,7 +319,7 @@ protected Session handleRequest(String uri, Socket client) throws IllegalStateEx
}
return session;
}

class RequestListener extends Thread implements Runnable {

private final ServerSocket mServer;
Expand Down Expand Up @@ -692,7 +693,7 @@ public void send(OutputStream output) throws IOException {
(seqid>=0?("Cseq: " + seqid + "\r\n"):"") +
"Content-Length: " + content.length() + "\r\n" +
attributes +
"\r\n" +
"\r\n" +
content;

Log.d(TAG,response.replace("\r", ""));
Expand Down