diff options
author | Austin Kirk <askirk@umich.edu> | 2017-06-14 13:24:57 -0400 |
---|---|---|
committer | Austin Kirk <askirk@umich.edu> | 2017-06-14 13:24:57 -0400 |
commit | ae9717334bf605241ac586f55906fc2def681db1 (patch) | |
tree | f8d75edb1388f0c83c1ce92aa9642821e0e9f718 | |
parent | bdaa3c98f254d157144bea7ac3200c480e5b0a76 (diff) | |
download | sdl_android-feature/issue_469_additions.tar.gz |
Removing SdlProxyALM references and opting for SdlSessionfeature/issue_469_additions
- startService now returns a StreamWriterThread
- Devs can pass this in to the VirtualDisplayEncoder in favor of a proxy
- StreamWriterThread constructor takes in an SdlSession, not a proxy
3 files changed, 41 insertions, 79 deletions
diff --git a/sdl_android/src/main/java/com/smartdevicelink/encoder/VirtualDisplayEncoder.java b/sdl_android/src/main/java/com/smartdevicelink/encoder/VirtualDisplayEncoder.java index e6caabd47..5d47b459a 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/encoder/VirtualDisplayEncoder.java +++ b/sdl_android/src/main/java/com/smartdevicelink/encoder/VirtualDisplayEncoder.java @@ -22,10 +22,6 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; -import com.smartdevicelink.SdlConnection.SdlSession; -import com.smartdevicelink.protocol.enums.SessionType; -import com.smartdevicelink.proxy.SdlProxyBase; -import com.smartdevicelink.proxy.interfaces.ISdlServiceListener; import com.smartdevicelink.proxy.rpc.OnTouchEvent; import com.smartdevicelink.proxy.rpc.ScreenParams; import com.smartdevicelink.proxy.rpc.TouchCoord; @@ -53,7 +49,6 @@ public class VirtualDisplayEncoder { private Class<? extends SdlPresentation> presentationClass = null; private StreamWriterThread streamWriterThread = null; private Context mContext; - private SdlProxyBase proxy; private Boolean initPassed = false; private Handler uiHandler = new Handler(Looper.getMainLooper()); private final static int REFRESH_RATE_MS = 100; @@ -71,7 +66,7 @@ public class VirtualDisplayEncoder { * @param screenParams * @throws Exception */ - public void init(Context context, SdlProxyBase proxyALM, Class<? extends SdlPresentation> presentationClass, ScreenParams screenParams) throws Exception { + public void init(Context context, StreamWriterThread streamWriterThread, Class<? extends SdlPresentation> presentationClass, ScreenParams screenParams) throws Exception { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { Log.e(TAG, "API level of 21 required for VirtualDisplayEncoder"); throw new Exception("API level of 21 required"); @@ -85,7 +80,7 @@ public class VirtualDisplayEncoder { mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE); mContext = context; - proxy = proxyALM; + this.streamWriterThread = streamWriterThread; //need to check image resolution for null if(screenParams.getImageResolution().getResolutionHeight() != null){ @@ -125,6 +120,8 @@ public class VirtualDisplayEncoder { synchronized (STREAMING_LOCK) { try { + streamWriterThread.start(); + inputSurface = prepareVideoEncoder(); // Create a virtual display that will output to our encoder. @@ -315,7 +312,17 @@ public class VirtualDisplayEncoder { private void onStreamDataAvailable(byte[] data, int size) { try { - proxy.writeToStream(SessionType.NAV, data, size); + synchronized (streamWriterThread.BUFFER_LOCK){ + streamWriterThread.isWaiting = true; + try { + streamWriterThread.BUFFER_LOCK.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + streamWriterThread.isWaiting = false; + + streamWriterThread.setByteBuffer(data, size); + } } catch (Exception e) { e.printStackTrace(); } @@ -458,29 +465,20 @@ public class VirtualDisplayEncoder { private void setupVideoStreamWriter() { if (streamWriterThread == null) { // Setup VideoStreamWriterThread thread - proxy.setServiceListener(SessionType.NAV, new ISdlServiceListener() { - @Override - public void onServiceStarted(SdlSession session, SessionType type, boolean isEncrypted) { - Log.i(TAG, type.getName() + " service started."); - } - - @Override - public void onServiceEnded(SdlSession session, SessionType type) { - Log.i(TAG, type.getName() + " service ended."); - } - - @Override - public void onServiceError(SdlSession session, SessionType type, String reason) { - Log.i(TAG, type.getName() + " service error - " + reason); - } - }); - - proxy.startService(SessionType.NAV, false); + streamWriterThread.start(); } } private void releaseVideoStreamWriter() { - proxy.endService(SessionType.NAV); + if(streamWriterThread != null){ + streamWriterThread.halt(); + try { + streamWriterThread.interrupt(); + } catch (Exception ex) { + ex.printStackTrace(); + } + streamWriterThread.clearByteBuffer(); + } } public class StreamingParameters { diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java index e99762220..0a584a6cb 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyBase.java @@ -5634,33 +5634,23 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> return sPoliciesURL;
}
- // Helper methods for streaming
+ // For streaming
private Map<SessionType, StreamWriterThread> serviceThreads = new HashMap<>();
- // Developer-facing
- public void startService(SessionType serviceType, boolean isEncrypted) {
+ public StreamWriterThread startService(SessionType serviceType, boolean isEncrypted) {
if(serviceType != null && sdlSession != null){
sdlSession.startService(serviceType, sdlSession.getSessionId(), isEncrypted);
- StreamWriterThread thread = new StreamWriterThread(this, serviceType);
+ StreamWriterThread thread = new StreamWriterThread(this.sdlSession, serviceType);
serviceThreads.put(serviceType, thread);
- thread.start();
+ return thread;
}
+ return null;
}
public void endService(SessionType serviceType) {
if(serviceType != null && sdlSession != null){
sdlSession.endService(serviceType, sdlSession.getSessionId());
- StreamWriterThread thread = serviceThreads.get(serviceType);
- if(thread != null){
- thread.halt();
- try {
- thread.interrupt();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- thread.clearByteBuffer();
- serviceThreads.remove(serviceType);
- }
+ serviceThreads.remove(serviceType);
}
}
@@ -5670,32 +5660,4 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase> }
}
- public void writeToStream(SessionType serviceType, byte[] buf, Integer size){
- StreamWriterThread thread = serviceThreads.get(serviceType);
- if(thread == null){
- return;
- }
-
- synchronized (thread.BUFFER_LOCK){
- thread.isWaiting = true;
- try {
- thread.BUFFER_LOCK.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- thread.isWaiting = false;
-
- thread.setByteBuffer(buf, size);
- }
- }
-
- // For internal use
- public byte getSessionId(){
- return sdlSession.getSessionId();
- }
-
- public void sendStreamPacket(ProtocolMessage pm){
- sdlSession.sendStreamPacket(pm);
- }
-
} // end-class
diff --git a/sdl_android/src/main/java/com/smartdevicelink/streaming/StreamWriterThread.java b/sdl_android/src/main/java/com/smartdevicelink/streaming/StreamWriterThread.java index dcf30502a..12d1c8d50 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/streaming/StreamWriterThread.java +++ b/sdl_android/src/main/java/com/smartdevicelink/streaming/StreamWriterThread.java @@ -1,15 +1,17 @@ package com.smartdevicelink.streaming;
+import com.smartdevicelink.SdlConnection.SdlSession;
import com.smartdevicelink.protocol.ProtocolMessage;
import com.smartdevicelink.protocol.enums.SessionType;
-import com.smartdevicelink.proxy.SdlProxyBase;
+
+import static com.smartdevicelink.trace.enums.Mod.proxy;
public class StreamWriterThread extends Thread {
private Boolean isHalted = false;
private byte[] buf = null;
private Integer size = 0;
private SessionType serviceType;
- private SdlProxyBase proxy;
+ private SdlSession session;
public Boolean isWaiting = false;
private final static Object lock = new Object();
public final static Object BUFFER_LOCK = new Object();
@@ -22,9 +24,9 @@ public class StreamWriterThread extends Thread { private final static int ENC_READ_SIZE = TLS_MAX_RECORD_SIZE - TLS_RECORD_HEADER_SIZE - TLS_RECORD_MES_AUTH_CDE_SIZE - TLS_MAX_RECORD_PADDING_SIZE;
- public StreamWriterThread(SdlProxyBase proxy, SessionType serviceType) {
- this.proxy = proxy;
- this.isServiceProtected = proxy.isServiceTypeProtected(serviceType);
+ public StreamWriterThread(SdlSession sdlSession, SessionType serviceType) {
+ this.session = sdlSession;
+ this.isServiceProtected = session.isServiceProtected(serviceType);
this.serviceType = serviceType;
this.setName(serviceType.getName()+"StreamWriter");
@@ -59,7 +61,7 @@ public class StreamWriterThread extends Thread { private ProtocolMessage createStreamPacket(byte[] byteData) {
ProtocolMessage pm = new ProtocolMessage();
- pm.setSessionID(proxy.getSessionId());
+ pm.setSessionID(session.getSessionId());
pm.setSessionType(serviceType);
pm.setFunctionID(0);
pm.setCorrID(0);
@@ -95,12 +97,12 @@ public class StreamWriterThread extends Thread { for(int i = 0; i < ret.length; i++) {
byte[] byteData = ret[i];
ProtocolMessage pm = createStreamPacket(byteData);
- proxy.sendStreamPacket(pm);
+ session.sendStreamPacket(pm);
}
}
else {
ProtocolMessage pm = createStreamPacket(buf);
- proxy.sendStreamPacket(pm);
+ session.sendStreamPacket(pm);
}
clearByteBuffer();
|