<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][16110] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=16110">16110</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-12-31 14:08:57 -0600 (Thu, 31 Dec 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>nut 'n honey</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_conferencemod_conferencec">freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodapplicationsmod_conferencemod_conferencec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c (16109 => 16110)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c        2009-12-31 19:41:47 UTC (rev 16109)
+++ freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c        2009-12-31 20:08:57 UTC (rev 16110)
</span><span class="lines">@@ -175,7 +175,8 @@
</span><span class="cx">         CFLAG_ANSWERED = (1 << 5),
</span><span class="cx">         CFLAG_BRIDGE_TO = (1 << 6),
</span><span class="cx">         CFLAG_WAIT_MOD = (1 << 7),
</span><del>-        CFLAG_VID_FLOOR = (1 << 8)
</del><ins>+        CFLAG_VID_FLOOR = (1 << 8),
+        CFLAG_WASTE_BANDWIDTH = (1 << 9)
</ins><span class="cx"> } conf_flag_t;
</span><span class="cx">
</span><span class="cx"> typedef enum {
</span><span class="lines">@@ -297,6 +298,7 @@
</span><span class="cx">         uint32_t eflags;
</span><span class="cx">         uint32_t verbose_events;
</span><span class="cx">         int end_count;
</span><ins>+        uint32_t relationship_total;
</ins><span class="cx"> } conference_obj_t;
</span><span class="cx">
</span><span class="cx"> /* Relationship with another member */
</span><span class="lines">@@ -571,7 +573,11 @@
</span><span class="cx">
</span><span class="cx">         rel->id = id;
</span><span class="cx">
</span><ins>+
</ins><span class="cx">         lock_member(member);
</span><ins>+        switch_mutex_lock(member->conference->member_mutex);
+        member->conference->relationship_total++;
+        switch_mutex_unlock(member->conference->member_mutex);
</ins><span class="cx">         rel->next = member->relationships;
</span><span class="cx">         member->relationships = rel;
</span><span class="cx">         unlock_member(member);
</span><span class="lines">@@ -599,6 +605,11 @@
</span><span class="cx">                         } else {
</span><span class="cx">                                 member->relationships = rel->next;
</span><span class="cx">                         }
</span><ins>+
+                        switch_mutex_lock(member->conference->member_mutex);
+                        member->conference->relationship_total--;
+                        switch_mutex_unlock(member->conference->member_mutex);
+
</ins><span class="cx">                 }
</span><span class="cx">                 last = rel;
</span><span class="cx">         }
</span><span class="lines">@@ -970,6 +981,9 @@
</span><span class="cx">         switch_event_t *event;
</span><span class="cx">         uint8_t *file_frame;
</span><span class="cx">         uint8_t *async_file_frame;
</span><ins>+        int16_t *bptr;
+        int x;
+        int32_t z = 0;
</ins><span class="cx">
</span><span class="cx">         file_frame = switch_core_alloc(conference->pool, SWITCH_RECOMMENDED_BUFFER_SIZE);
</span><span class="cx">         async_file_frame = switch_core_alloc(conference->pool, SWITCH_RECOMMENDED_BUFFER_SIZE);
</span><span class="lines">@@ -1111,7 +1125,7 @@
</span><span class="cx">
</span><span class="cx">                                                 for (x = 0; x < file_sample_len; x++) {
</span><span class="cx">                                                         int32_t z;
</span><del>-                                                        int16_t *bptr, *muxed;
</del><ins>+                                                        int16_t *muxed;
</ins><span class="cx">
</span><span class="cx">                                                         muxed = (int16_t *) file_frame;
</span><span class="cx">                                                         bptr = (int16_t *) async_file_frame;
</span><span class="lines">@@ -1127,99 +1141,183 @@
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                if (ready || has_file_data) {
-                        /* Build a muxed frame for every member that contains the mixed audio of everyone else */
</del><ins>+                if (switch_test_flag(conference, CFLAG_WASTE_BANDWIDTH) && !has_file_data) {
+                        file_sample_len = bytes / 2;
</ins><span class="cx">
</span><del>-                        for (omember = conference->members; omember; omember = omember->next) {
</del><ins>+                        if (conference->comfort_noise_level) {
+                                switch_generate_sln_silence((int16_t *) file_frame, file_sample_len, conference->comfort_noise_level);
+                        } else {
+                                memset(file_frame, 255, bytes);
+                        }
+                        has_file_data = 1;
+                }
+
+                if (!conference->relationship_total) {
+                        /* If there are no relationships meaning (user x can specificly not speal to and/or hear user y), use a more efficient muxing technique. */
+                        if (ready || has_file_data) {
+                                /* Use more bits in the main_frame to preserve the exact sum of the audio samples. */
+                                int main_frame[SWITCH_RECOMMENDED_BUFFER_SIZE / 2] = { 0 };
+                                int16_t write_frame[SWITCH_RECOMMENDED_BUFFER_SIZE / 2] = { 0 };
+
+
+                                /* Init the main frame with file data if there is any. */
+                                bptr = (int16_t *) file_frame;
</ins><span class="cx">                                 if (has_file_data && file_sample_len) {
</span><del>-                                        uint32_t sample_bytes = file_sample_len * 2;
-                                        memcpy(omember->mux_frame, file_frame, sample_bytes);
-                                        if (sample_bytes < bytes) {
-                                                if (conference->comfort_noise_level) {
-                                                        switch_generate_sln_silence((int16_t *) omember->mux_frame + sample_bytes,
-                                                                                                                (bytes - sample_bytes) / 2, conference->comfort_noise_level);
</del><ins>+                                        for (x = 0; x < bytes / 2; x++) {
+                                                if (x <= file_sample_len) {
+                                                        main_frame[x] = (int32_t) bptr[x];
</ins><span class="cx">                                                 } else {
</span><del>-                                                        memset(omember->mux_frame + sample_bytes, 255, bytes - sample_bytes);
</del><ins>+                                                        main_frame[x] = 255;
</ins><span class="cx">                                                 }
</span><span class="cx">                                         }
</span><del>-                                } else {
-                                        if (conference->comfort_noise_level) {
-                                                switch_generate_sln_silence((int16_t *) omember->mux_frame, bytes / 2, conference->comfort_noise_level);
-                                        } else {
-                                                memset(omember->mux_frame, 255, bytes);
</del><ins>+                                }
+
+                                /* Copy audio from every member known to be producing audio into the main frame. */
+                                for (omember = conference->members; omember; omember = omember->next) {
+                                        if (!(switch_test_flag(omember, MFLAG_RUNNING) && switch_test_flag(omember, MFLAG_HAS_AUDIO))) {
+                                                continue;
</ins><span class="cx">                                         }
</span><ins>+                                        bptr = (int16_t *) omember->frame;
+                                        for (x = 0; x < omember->read / 2; x++) {
+                                                main_frame[x] += (int32_t) bptr[x];
+                                        }
</ins><span class="cx">                                 }
</span><del>-                                for (imember = conference->members; imember; imember = imember->next) {
-                                        uint32_t x;
-                                        int16_t *bptr, *muxed;
</del><span class="cx">
</span><del>-                                        if (imember == omember || !imember->read) {
-                                                /* Don't add audio from yourself or if you didn't read any */
</del><ins>+                                /* Create write frame once per member who is not deaf for each sample in the main frame
+                                 check if our audio is involved and if so, subtract it from the sample so we don't hear ourselves.
+                                 Since main frame was 32 bit int, we did not lose any detail, now that we have to convert to 16 bit we can
+                                 cut it off at the min and max range if need be and write the frame to the output buffer.
+                                */
+                                for (omember = conference->members; omember; omember = omember->next) {
+                                        switch_size_t ok = 1;
+
+                                        if (!switch_test_flag(omember, MFLAG_RUNNING)) {
</ins><span class="cx">                                                 continue;
</span><span class="cx">                                         }
</span><span class="cx">
</span><del>-                                        /* If they are not supposed to talk to us then don't let them */
-                                        if (omember->relationships) {
-                                                conference_relationship_t *rel;
</del><ins>+                                        if (!switch_test_flag(omember, MFLAG_CAN_HEAR) && !switch_test_flag(omember, MFLAG_WASTE_BANDWIDTH)
+                                                && !switch_test_flag(conference, CFLAG_WASTE_BANDWIDTH)) {
+                                                continue;
+                                        }
</ins><span class="cx">
</span><del>-                                                if ((rel = member_get_relationship(omember, imember))) {
-                                                        if (!switch_test_flag(rel, RFLAG_CAN_HEAR)) {
-                                                                continue;
</del><ins>+                                        bptr = (int16_t *) omember->frame;
+                                        for (x = 0; x < bytes / 2; x++) {
+                                                z = main_frame[x];
+                                                /* bptr[x] represents my own contribution to this audio sample */
+                                                if (switch_test_flag(omember, MFLAG_HAS_AUDIO) && x <= omember->read / 2) {
+                                                        z -= (int32_t) bptr[x];
+                                                }
+                                                /* Now we can convert to 16 bit.*/
+                                                switch_normalize_to_16bit(z);
+                                                write_frame[x] = (int16_t) z;
+                                        }
+                                
+                                        switch_mutex_lock(omember->audio_out_mutex);
+                                        ok = switch_buffer_write(omember->mux_buffer, write_frame, bytes);
+                                        switch_mutex_unlock(omember->audio_out_mutex);
+                                
+                                        if (!ok) {
+                                                goto end;
+                                        }
+                                }
+
+                        }
+                } else {
+                        if (ready || has_file_data) {
+                                /* Build a muxed frame for every member that contains the mixed audio of everyone else */
+
+                                for (omember = conference->members; omember; omember = omember->next) {
+                                        if (has_file_data && file_sample_len) {
+                                                uint32_t sample_bytes = file_sample_len * 2;
+                                                memcpy(omember->mux_frame, file_frame, sample_bytes);
+                                                if (sample_bytes < bytes) {
+                                                        if (conference->comfort_noise_level) {
+                                                                switch_generate_sln_silence((int16_t *) omember->mux_frame + sample_bytes,
+                                                                                                                        (bytes - sample_bytes) / 2, conference->comfort_noise_level);
+                                                        } else {
+                                                                memset(omember->mux_frame + sample_bytes, 255, bytes - sample_bytes);
</ins><span class="cx">                                                         }
</span><span class="cx">                                                 }
</span><ins>+                                        } else {
+                                                if (conference->comfort_noise_level) {
+                                                        switch_generate_sln_silence((int16_t *) omember->mux_frame, bytes / 2, conference->comfort_noise_level);
+                                                } else {
+                                                        memset(omember->mux_frame, 255, bytes);
+                                                }
</ins><span class="cx">                                         }
</span><ins>+                                        for (imember = conference->members; imember; imember = imember->next) {
+                                                uint32_t x;
+                                                int16_t *muxed;
</ins><span class="cx">
</span><del>-                                        /* If we are not supposed to hear them then don't let it happen */
-                                        if (imember->relationships) {
-                                                conference_relationship_t *rel;
</del><ins>+                                                if (imember == omember || !imember->read) {
+                                                        /* Don't add audio from yourself or if you didn't read any */
+                                                        continue;
+                                                }
</ins><span class="cx">
</span><del>-                                                if ((rel = member_get_relationship(imember, omember))) {
-                                                        if (!switch_test_flag(rel, RFLAG_CAN_SPEAK)) {
-                                                                continue;
</del><ins>+                                                /* If they are not supposed to talk to us then don't let them */
+                                                if (omember->relationships) {
+                                                        conference_relationship_t *rel;
+
+                                                        if ((rel = member_get_relationship(omember, imember))) {
+                                                                if (!switch_test_flag(rel, RFLAG_CAN_HEAR)) {
+                                                                        continue;
+                                                                }
</ins><span class="cx">                                                         }
</span><span class="cx">                                                 }
</span><del>-                                        }
</del><ins>+
+                                                /* If we are not supposed to hear them then don't let it happen */
+                                                if (imember->relationships) {
+                                                        conference_relationship_t *rel;
+
+                                                        if ((rel = member_get_relationship(imember, omember))) {
+                                                                if (!switch_test_flag(rel, RFLAG_CAN_SPEAK)) {
+                                                                        continue;
+                                                                }
+                                                        }
+                                                }
</ins><span class="cx"> #if 0
</span><del>-                                        if (nt && conference->not_talking_buf_len && !switch_test_flag(omember, MFLAG_HAS_AUDIO)) {
-                                                memcpy(omember->mux_frame, conference->not_talking_buf, conference->not_talking_buf_len);
-                                                continue;
-                                        }
</del><ins>+                                                if (nt && conference->not_talking_buf_len && !switch_test_flag(omember, MFLAG_HAS_AUDIO)) {
+                                                        memcpy(omember->mux_frame, conference->not_talking_buf, conference->not_talking_buf_len);
+                                                        continue;
+                                                }
</ins><span class="cx"> #endif
</span><del>-                                        bptr = (int16_t *) imember->frame;
-                                        muxed = (int16_t *) omember->mux_frame;
</del><ins>+                                                bptr = (int16_t *) imember->frame;
+                                                muxed = (int16_t *) omember->mux_frame;
</ins><span class="cx">
</span><del>-                                        for (x = 0; x < imember->read / 2; x++) {
-                                                int32_t z = muxed[x] + bptr[x];
-                                                switch_normalize_to_16bit(z);
-                                                muxed[x] = (int16_t) z;
-                                        }
</del><ins>+                                                for (x = 0; x < imember->read / 2; x++) {
+                                                        int32_t z = muxed[x] + bptr[x];
+                                                        switch_normalize_to_16bit(z);
+                                                        muxed[x] = (int16_t) z;
+                                                }
</ins><span class="cx"> #if 0
</span><del>-                                        if (total - ready > 1) {
-                                                conference->not_talking_buf_len = imember->read;
-                                                if (!conference->not_talking_buf) {
-                                                        conference->not_talking_buf = switch_core_alloc(conference->pool, imember->read + 128);
</del><ins>+                                                if (total - ready > 1) {
+                                                        conference->not_talking_buf_len = imember->read;
+                                                        if (!conference->not_talking_buf) {
+                                                                conference->not_talking_buf = switch_core_alloc(conference->pool, imember->read + 128);
+                                                        }
+                                                        memcpy(conference->not_talking_buf, muxed, conference->not_talking_buf_len);
+                                                        nt++;
</ins><span class="cx">                                                 }
</span><del>-                                                memcpy(conference->not_talking_buf, muxed, conference->not_talking_buf_len);
-                                                nt++;
</del><ins>+#endif
</ins><span class="cx">                                         }
</span><del>-#endif
</del><span class="cx">                                 }
</span><del>-                        }
-                        if (bytes) {
-                                /* Go back and write each member his dedicated copy of the audio frame that does not contain his own audio. */
-                                for (imember = conference->members; imember; imember = imember->next) {
-                                        if (switch_test_flag(imember, MFLAG_RUNNING)) {
-                                                switch_size_t ok = 1;
-                                                switch_mutex_lock(imember->audio_out_mutex);
-                                                ok = switch_buffer_write(imember->mux_buffer, imember->mux_frame, bytes);
-                                                switch_mutex_unlock(imember->audio_out_mutex);
-                                                if (!ok) {
-                                                        goto end;
</del><ins>+                                if (bytes) {
+                                        /* Go back and write each member his dedicated copy of the audio frame that does not contain his own audio. */
+                                        for (imember = conference->members; imember; imember = imember->next) {
+                                                if (switch_test_flag(imember, MFLAG_RUNNING)) {
+                                                        switch_size_t ok = 1;
+                                                        switch_mutex_lock(imember->audio_out_mutex);
+                                                        ok = switch_buffer_write(imember->mux_buffer, imember->mux_frame, bytes);
+                                                        switch_mutex_unlock(imember->audio_out_mutex);
+                                                        if (!ok) {
+                                                                goto end;
+                                                        }
</ins><span class="cx">                                                 }
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+
</ins><span class="cx">                 if (conference->async_fnode && conference->async_fnode->done) {
</span><span class="cx">                         switch_memory_pool_t *pool;
</span><span class="cx">                         switch_core_file_close(&conference->async_fnode->fh);
</span><span class="lines">@@ -2367,7 +2465,7 @@
</span><span class="cx">                                 }
</span><span class="cx">
</span><span class="cx">                                 switch_mutex_unlock(member->audio_out_mutex);
</span><del>-                        } else {
</del><ins>+                        } else if (!switch_test_flag(member->conference, CFLAG_WASTE_BANDWIDTH)) {
</ins><span class="cx">                                 if (switch_test_flag(member, MFLAG_WASTE_BANDWIDTH)) {
</span><span class="cx">                                         if (member->conference->comfort_noise_level) {
</span><span class="cx">                                                 switch_generate_sln_silence(write_frame.data, samples, member->conference->comfort_noise_level);
</span><span class="lines">@@ -4800,6 +4898,8 @@
</span><span class="cx">                                 *f |= CFLAG_WAIT_MOD;
</span><span class="cx">                         } else if (!strcasecmp(argv[i], "video-floor-only")) {
</span><span class="cx">                                 *f |= CFLAG_VID_FLOOR;
</span><ins>+                        } else if (!strcasecmp(argv[i], "waste-bandwidth")) {
+                                *f |= CFLAG_WASTE_BANDWIDTH;
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>