@@ -110,7 +110,7 @@ public SurfaceTextureTarget(String name) {
110110 }
111111
112112 @ Override
113- public void setupPorts () {
113+ public synchronized void setupPorts () {
114114 // Make sure we have a SurfaceView
115115 if (mSurfaceTexture == null ) {
116116 throw new RuntimeException ("Null SurfaceTexture passed to SurfaceTextureTarget" );
@@ -158,7 +158,7 @@ public void prepare(FilterContext context) {
158158 }
159159
160160 @ Override
161- public void open (FilterContext context ) {
161+ public synchronized void open (FilterContext context ) {
162162 // Set up SurfaceTexture internals
163163 mSurfaceId = context .getGLEnvironment ().registerSurfaceTexture (
164164 mSurfaceTexture , mScreenWidth , mScreenHeight );
@@ -169,17 +169,42 @@ public void open(FilterContext context) {
169169
170170
171171 @ Override
172- public void close (FilterContext context ) {
172+ public synchronized void close (FilterContext context ) {
173173 if (mSurfaceId > 0 ) {
174174 context .getGLEnvironment ().unregisterSurfaceId (mSurfaceId );
175+ mSurfaceId = -1 ;
176+ // Once the surface is unregistered, remove the surfacetexture reference.
177+ // The surfaceId could not have been valid without a valid surfacetexture.
178+ mSurfaceTexture = null ;
175179 }
176180 }
177181
182+ // This should be called from the client side when the surfacetexture is no longer
183+ // valid. e.g. from onPause() in the application using the filter graph.
184+ public synchronized void disconnect (FilterContext context ) {
185+ if (mLogVerbose ) Log .v (TAG , "disconnect" );
186+ if (mSurfaceTexture == null ) {
187+ Log .d (TAG , "SurfaceTexture is already null. Nothing to disconnect." );
188+ return ;
189+ }
190+ mSurfaceTexture = null ;
191+ // Make sure we unregister the surface as well if a surface was registered.
192+ // There can be a situation where the surface was not registered but the
193+ // surfacetexture was valid. For example, the disconnect can be called before
194+ // the filter was opened. Hence, the surfaceId may not be a valid one here,
195+ // and need to check for its validity.
196+ if (mSurfaceId > 0 ) {
197+ context .getGLEnvironment ().unregisterSurfaceId (mSurfaceId );
198+ mSurfaceId = -1 ;
199+ }
200+ }
178201
179202 @ Override
180- public void process (FilterContext context ) {
181- if (mLogVerbose ) Log .v (TAG , "Starting frame processing" );
182-
203+ public synchronized void process (FilterContext context ) {
204+ // Surface is not registered. Nothing to render into.
205+ if (mSurfaceId <= 0 ) {
206+ return ;
207+ }
183208 GLEnvironment glEnv = context .getGLEnvironment ();
184209
185210 // Get input frame
@@ -197,8 +222,6 @@ public void process(FilterContext context) {
197222
198223 // See if we need to copy to GPU
199224 Frame gpuFrame = null ;
200- if (mLogVerbose ) Log .v ("SurfaceTextureTarget" , "Got input format: " + input .getFormat ());
201-
202225 int target = input .getFormat ().getTarget ();
203226 if (target != FrameFormat .TARGET_GPU ) {
204227 gpuFrame = context .getFrameManager ().duplicateFrameToTarget (input ,
0 commit comments