Changeset 5738


Ignore:
Timestamp:
Feb 16, 2018 9:37:00 AM (6 years ago)
Author:
nanang
Message:

Misc (#2059): Update Android & Java app samples to avoid call instance deletion in the library callback context (thanks Kai Ludwig for the feedbacks).

Location:
pjproject/trunk/pjsip-apps/src/swig/java
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/swig/java/android/app/src/main/java/org/pjsip/pjsua2/app/MainActivity.java

    r5649 r5738  
    233233        } else if (m.what == MSG_TYPE.CALL_STATE) { 
    234234 
    235             CallInfo ci = (CallInfo) m.obj; 
    236  
    237             /* Forward the message to CallActivity */ 
    238             if (CallActivity.handler_ != null) { 
     235            MyCall call = (MyCall) m.obj; 
     236            CallInfo ci; 
     237            try { 
     238                ci = call.getInfo(); 
     239            } catch (Exception e) { 
     240                ci = null; 
     241            } 
     242 
     243            /* Forward the call info to CallActivity */ 
     244            if (ci != null && CallActivity.handler_ != null) { 
    239245                Message m2 = Message.obtain(CallActivity.handler_, 
    240246                    MSG_TYPE.CALL_STATE, ci); 
    241247                m2.sendToTarget(); 
     248            } 
     249 
     250            if (ci != null && 
     251                ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) 
     252            { 
     253                call.delete(); 
    242254            } 
    243255 
     
    602614            ci = null; 
    603615        } 
    604         Message m = Message.obtain(handler, MSG_TYPE.CALL_STATE, ci); 
     616        Message m = Message.obtain(handler, MSG_TYPE.CALL_STATE, call); 
    605617        m.sendToTarget(); 
    606618 
  • pjproject/trunk/pjsip-apps/src/swig/java/android/app/src/main/java/org/pjsip/pjsua2/app/MyApp.java

    r5649 r5738  
    6262    public void onCallState(OnCallStateParam prm) 
    6363    { 
    64             MyApp.observer.notifyCallState(this); 
    65             try { 
    66                 CallInfo ci = getInfo(); 
    67                 if (ci.getState() ==  
    68                     pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) 
    69                 { 
    70                     MyApp.ep.utilLogWrite(3, "MyCall", this.dump(true, "")); 
    71                     this.delete(); 
    72                 } 
    73             } catch (Exception e) { 
    74                 return; 
    75             } 
     64        try { 
     65            CallInfo ci = getInfo(); 
     66            if (ci.getState() ==  
     67                pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) 
     68            { 
     69                MyApp.ep.utilLogWrite(3, "MyCall", this.dump(true, "")); 
     70            } 
     71        } catch (Exception e) { 
     72        } 
     73         
     74        // Should not delete this call instance (self) in this context, 
     75        // so the observer should manage this call instance deletion 
     76        // out of this callback context. 
     77        MyApp.observer.notifyCallState(this); 
    7678    } 
    7779 
  • pjproject/trunk/pjsip-apps/src/swig/java/sample.java

    r5649 r5738  
    2626class MyObserver implements MyAppObserver { 
    2727        private static MyCall currentCall = null; 
     28        private boolean del_call_scheduled = false; 
     29         
     30        public void check_call_deletion() 
     31        { 
     32                if (del_call_scheduled && currentCall != null) { 
     33                        currentCall.delete(); 
     34                        currentCall = null; 
     35                        del_call_scheduled = false; 
     36                } 
     37        } 
    2838         
    2939        @Override 
     
    5868                        ci = null; 
    5969                } 
    60                 if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) 
    61                         currentCall = null;              
     70                if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) { 
     71                        // Should not delete call instance in this context, 
     72                        // so let's just schedule it, the call will be deleted 
     73                        // in our main worker thread context. 
     74                        del_call_scheduled = true; 
     75                } 
     76                         
    6277        } 
    6378         
     
    86101public class sample { 
    87102        private static MyApp app = new MyApp(); 
    88         private static MyAppObserver observer = new MyObserver(); 
     103        private static MyObserver observer = new MyObserver(); 
    89104        private static MyAccount account = null; 
    90105        private static AccountConfig accCfg = null;              
     
    142157 
    143158                while (!Thread.currentThread().isInterrupted()) { 
     159                        // Handle events 
    144160                        MyApp.ep.libHandleEvents(10); 
     161                         
     162                        // Check if any call instance need to be deleted 
     163                        observer.check_call_deletion(); 
     164                         
    145165                        try {                                            
    146166                                Thread.currentThread().sleep(50); 
    147167                        } catch (InterruptedException ie) {                                              
    148168                                break; 
    149                         }                                        
     169                        }                        
    150170                } 
    151171                app.deinit(); 
  • pjproject/trunk/pjsip-apps/src/swig/java/sample2.java

    r5402 r5738  
    3434class MyObserver implements MyAppObserver { 
    3535        private static MyCall currentCall = null; 
     36        private static boolean del_call_scheduled = false; 
     37         
     38        public void check_call_deletion() 
     39        { 
     40                if (del_call_scheduled && currentCall != null) { 
     41                        currentCall.delete(); 
     42                        currentCall = null; 
     43                        del_call_scheduled = false; 
     44                } 
     45        } 
    3646         
    3747        @Override 
     
    6777                } 
    6878                if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) { 
    69                         currentCall = null;                         
     79                        // Should not delete call instance here, so let's schedule it. 
     80                        // The call will be deleted by our main worker thread. 
     81                        del_call_scheduled = true;                  
    7082                } else if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) { 
    7183                        if (ci.getSetting().getVideoCount() != 0) { 
     
    89101class MyThread extends Thread { 
    90102        private static MyApp app = new MyApp(); 
    91         private static MyAppObserver observer = new MyObserver(); 
     103        private static MyObserver observer = new MyObserver(); 
    92104        private static MyAccount account = null; 
    93105        private static AccountConfig accCfg = null;                   
     
    118130 
    119131                while (!Thread.currentThread().isInterrupted()) { 
     132                        // Handle events 
    120133                        MyApp.ep.libHandleEvents(10); 
     134 
     135                        // Check if any call instance need to be deleted 
     136                        observer.check_call_deletion(); 
    121137                        try { 
    122138                                Thread.currentThread().sleep(50); 
Note: See TracChangeset for help on using the changeset viewer.