<div dir="ltr">Hi David,<div><br></div><div>As Dragos mentioned, there are examples in FS source tree how to do this, mod_avmd in particular.</div><div>Others are mod_cv, mod_stress, mod_soundtouch, mod_oreka, mod_vmd, mod_vmd, mod_ladspa, mod_snapshot, mod_fsk, mod_video_filter, mod_spandsp.</div><div><br></div><div>You may need to switch_channel_set_private/switch_channel_remove_private.</div><div><br></div><div>Instead of removing/re-adding the bug you can add it just once with switch_core_media_bug_add, and remove it when FS session ends.</div><div>You can then supply the state describing your app's session as the 5th argument to a call:  <div><br></div><div>my_session_t *my_session = alloc_my_session_dynamically_or_use_global_variable_just_about_anything_but_local_stack(not_active) __cannot_fail__;</div><div>switch_core_media_bug_add(session, "my-function", NULL, my_callback, my_session, 0, my_flags, &my_bug) </div><div><br></div><div>In that state you can put whatever you need, including whether your app on given FS session is active at the moment,</div><div>and based on that you can drive your code inside my_callback.</div><div><br></div><div> static switch_bool_t my_callback(switch_media_bug_t * bug, void *user_data, switch_abc_type_t type)</div><div> {</div><div>     my_session = (my_session_t *) user_data;</div><div>     // </div><div>     switch (type) {</div><div>         case SWITCH_ABC_TYPE_INIT:</div><div>             // if it has just been started</div><div>             my_session->active = 1;</div><div>             // maybe do something more if needed when your session is started (when you would (re)add bug otherwise)<br></div><div>             break;</div><div>         case SWITCH_ABC_TYPE_CLOSE:</div><div><div>             my_session->active = 0;</div><div>             // maybe do something more if needed when your session is closed (when you would remove bug otherwise) </div><div>             break;</div></div><div>         case SWITCH_ABC_TYPE_READ_REPLACE:<br></div><div>             // processing frame of data if session is active</div><div>             break;</div><div>         case SWITCH_ABC_TYPE_WRITE_REPLACE:<br></div><div><div>             // processing frame of data if session is active</div><div>             break;</div></div><div>         default:</div><div>             // other types</div><div>             why();</div><div>     }</div><div>     // ...</div><div> }</div><div><br></div><div>Callback will be called every time there is a reason to call it (considering also the flags you've supplied).</div><div>Good luck!</div><div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><font style="background-color:rgb(255,255,255)" size="4" face="arial, helvetica, sans-serif"><img src="https://drive.google.com/a/dataandsignal.com/uc?id=1lUdzdLjEycxXcGhDz-bKqDX8-AIlZuCq&export=download" width="96" height="49"><br></font></div><div><font style="background-color:rgb(255,255,255)" size="4" face="arial, helvetica, sans-serif"><br></font></div><div><font style="background-color:rgb(255,255,255)" size="4" face="arial, helvetica, sans-serif">Piotr Gregor</font></div><div><span style="background-color:rgb(255,255,255)"><font size="2" face="arial, helvetica, sans-serif">Software Engineer</font></span></div><div><span style="font-family:arial,helvetica,sans-serif;font-size:x-small"><br></span></div><div><span style="font-family:arial,helvetica,sans-serif;font-size:x-small">M: (+44) 07483 866 525</span><span style="font-family:arial,helvetica,sans-serif;font-size:x-small">     </span><span style="font-family:arial,helvetica,sans-serif;font-size:x-small">www: </span><a href="http://dataandsignal.com" style="font-family:arial,helvetica,sans-serif;font-size:x-small" target="_blank">dataandsignal.com</a></div><div><br></div><div><br><br><br></div></div></div></div></div></div></div></div></div></div></div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Sep 9, 2019 at 1:19 PM Dragos Oancea <<a href="mailto:dragos@freeswitch.org">dragos@freeswitch.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">It looks to me like it should have worked.<div><br></div><div>Maybe you do something wrong in the callback ? </div><div><br></div><div>Refer to other code that uses media bug, like mod_avmd .  </div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 6, 2019 at 7:03 PM David Horton <<a href="mailto:daveh@drachtio.org" target="_blank">daveh@drachtio.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">(Resending, as original email has not appeared in the mailing list)<br>
<br>
I’m trying to understand the semantics of removing and re-adding a media bug to a channel.<br>
<br>
My scenario is that I need to add a bug to a session, and after some period of processing audio then want to remove it.  <br>
Based on other factors, some time later in the call, I may want to re-add it again.<br>
<br>
I am on Freeswitch v1.8.5<br>
<br>
How I am doing this now is:<br>
<br>
- switch_core_media_bug_add(session, “my-function”, NULL, callback, …)<br>
<br>
some time later<br>
<br>
- switch_core_media_bug_remove(session, &bug);<br>
<br>
some time later<br>
<br>
- switch_core_media_bug_add(session, “my-function”, NULL, callback, …)<br>
<br>
That all works fine, but when the session ends I get a segfault.  See the stack trace below.<br>
<br>
(Note: I also tried using ‘switch_core_media_bug_close’ for the second step, and I don’t get a stack trace in that scenario, but neither does my callback get invoked after I re-add the bug in step #3).<br>
<br>
So, two questions:<br>
<br>
1) What is the recommended way to accomplish what I am doing?<br>
2) Is this a bug in freeswitch that I should report via Confluence?<br>
<br>
Thread 40 "freeswitch" received signal SIGSEGV, Segmentation fault.<br>
[Switching to Thread 0x7fffee766700 (LWP 31704)]<br>
__GI___pthread_rwlock_unlock (rwlock=0x7fffc801acf0) at pthread_rwlock_unlock.c:38<br>
38      pthread_rwlock_unlock.c: No such file or directory.<br>
(gdb) bt<br>
#0  __GI___pthread_rwlock_unlock (rwlock=0x7fffc801acf0) at pthread_rwlock_unlock.c:38<br>
#1  0x00007ffff7658b6e in apr_thread_rwlock_unlock () from /usr/local/freeswitch/lib/libfreeswitch.so.1<br>
#2  0x00007ffff6fc2cf3 in switch_thread_rwlock_unlock (rwlock=0x7fffc801ace8) at src/switch_apr.c:279<br>
#3  0x00007ffff6fe3272 in switch_core_media_bug_remove_all_function (session=0x7fffc801c158, function=0x0)<br>
   at src/switch_core_media_bug.c:1231<br>
#4  0x00007ffff700a6f3 in switch_core_session_hangup_state (session=0x7fffc801c158, force=SWITCH_TRUE)<br>
   at src/switch_core_state_machine.c:838<br>
#5  0x00007ffff7007178 in switch_core_session_run (session=0x7fffc801c158)<br>
   at src/switch_core_state_machine.c:615<br>
#6  0x00007ffff700069e in switch_core_session_thread (thread=0x7fffc8041410, obj=0x7fffc801c158)<br>
   at src/switch_core_session.c:1698<br>
#7  0x00007ffff7000a8d in switch_core_session_thread_pool_worker (thread=0x7fffc8041410, obj=0x7fffc80412a0)<br>
   at src/switch_core_session.c:1761<br>
#8  0x00007ffff7660e1f in dummy_worker () from /usr/local/freeswitch/lib/libfreeswitch.so.1<br>
#9  0x00007ffff63554a4 in start_thread (arg=0x7fffee766700) at pthread_create.c:456<br>
#10 0x00007ffff5992d0f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97<br>
_________________________________________________________________________<br>
<br>
The FreeSWITCH project is sponsored by SignalWire <a href="https://signalwire.com" rel="noreferrer" target="_blank">https://signalwire.com</a><br>
Enhance your FreeSWITCH install with disruptive priced SMS and PSTN services.<br>
Build your next product on our scalable cloud platform.<br>
<br>
Join our online community to chat in real time <a href="https://signalwire.community" rel="noreferrer" target="_blank">https://signalwire.community</a><br>
<br>
Professional FreeSWITCH Services<br>
<a href="mailto:sales@freeswitch.com" target="_blank">sales@freeswitch.com</a><br>
<a href="https://freeswitch.com" rel="noreferrer" target="_blank">https://freeswitch.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="https://freeswitch.com/oss" rel="noreferrer" target="_blank">https://freeswitch.com/oss</a><br>
<a href="https://freeswitch.org/confluence" rel="noreferrer" target="_blank">https://freeswitch.org/confluence</a><br>
<a href="https://cluecon.com" rel="noreferrer" target="_blank">https://cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" rel="noreferrer" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" rel="noreferrer" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="https://freeswitch.com" rel="noreferrer" target="_blank">https://freeswitch.com</a></blockquote></div>
_________________________________________________________________________<br>
<br>
The FreeSWITCH project is sponsored by SignalWire <a href="https://signalwire.com" rel="noreferrer" target="_blank">https://signalwire.com</a><br>
Enhance your FreeSWITCH install with disruptive priced SMS and PSTN services.<br>
Build your next product on our scalable cloud platform.<br>
<br>
Join our online community to chat in real time <a href="https://signalwire.community" rel="noreferrer" target="_blank">https://signalwire.community</a><br>
<br>
Professional FreeSWITCH Services<br>
<a href="mailto:sales@freeswitch.com" target="_blank">sales@freeswitch.com</a><br>
<a href="https://freeswitch.com" rel="noreferrer" target="_blank">https://freeswitch.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="https://freeswitch.com/oss" rel="noreferrer" target="_blank">https://freeswitch.com/oss</a><br>
<a href="https://freeswitch.org/confluence" rel="noreferrer" target="_blank">https://freeswitch.org/confluence</a><br>
<a href="https://cluecon.com" rel="noreferrer" target="_blank">https://cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" rel="noreferrer" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" rel="noreferrer" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="https://freeswitch.com" rel="noreferrer" target="_blank">https://freeswitch.com</a></blockquote></div>