<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-11-07 16:31 GMT+01:00 Lorenzo Miniero <span dir="ltr"><<a href="mailto:lminiero@gmail.com" target="_blank">lminiero@gmail.com</a>></span>:<br><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"><div class="gmail_extra"><div class="gmail_quote"><div><div class="gmail-h5">2016-11-07 16:17 GMT+01:00 Joshua Colp <span dir="ltr"><<a href="mailto:jcolp@digium.com" target="_blank">jcolp@digium.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-m_-1272311461936949868HOEnZb"><div class="gmail-m_-1272311461936949868h5">Lorenzo Miniero wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Hi all,<br>
<br>
apologies if this has been discussed before, but I couldn't find<br>
anything in the recent months on this group so I thought I'd write anyway.<br>
<br>
As a few others, I believe, I have been trying to find a way to make<br>
codec modules more aware of what's happening on the wire. In particular,<br>
the motivation for that comes from an attempt to make the open source<br>
Opus codec module more responsive and adaptive to changes in the<br>
network, taking advantage of the functionality the library provides in<br>
that respect (e.g., dynamic bitrate adaptation). The best approach to do<br>
that would obviously be providing codec modules with info on the RTCP<br>
feedback loop, e.g., in terms of losses the recipient has experienced,<br>
so that you can, for instance, change the bitrate in the encoder.<br>
Unfortunately, as of now there doesn't seem to be any way to make this<br>
possible, at least not in an easy way, in Asterisk out of the box.<br>
I've been investigating a few ways to do that, and have come up with<br>
some possible approaches, that I first wanted to discuss with you guys<br>
though, first of all to make sure I'm actually on the right path, and<br>
then to evaluate whether or not any of those can actually be integrated<br>
within the Asterisk code base (as I do believe such a feedback loop<br>
would be beneficial to other codec modules as well, and not only Opus).<br>
If you're interested in some more motivation behind this, you can read<br>
my discussion with Alexander Traud in a comment to his fork to the<br>
asterisk-opus repo here: <a href="https://github.com/traud/asterisk-opus/issues/3" rel="noreferrer" target="_blank">https://github.com/traud/aster<wbr>isk-opus/issues/3</a><br>
<br>
<br>
For the sake of completeness, Alexander himself thought of a possible<br>
approach for integrating this feedback in a comment to another post:<br>
<a href="https://github.com/seanbright/asterisk-opus/issues/25#issuecomment-249420010" rel="noreferrer" target="_blank">https://github.com/seanbright/<wbr>asterisk-opus/issues/25#issuec<wbr>omment-249420010</a><br>
where the idea is to pass a reference of the ast_rtp_instance into the<br>
codec module itself. While this could possibly do the trick, I don't<br>
believe this would be a viable option, as it would break the<br>
architecture and module relationships, but I thought I'd mention it anyway.<br>
<br>
One possible option that I had thought about was extending ast_frame to<br>
convey RTCP feedback to modules, along with media to translate. This<br>
would allow such feedback to take the same "path" as media packets,<br>
meaning codec modules wouldn't need to be aware of any additional<br>
core-related feature, but only that sometimes they might receive control<br>
data instead of media to translate. Anyway, this could probably be<br>
problematic to integrate with the translator's architecture, and would<br>
probably need "cooperation" from channel modules as well, so may be a<br>
bit overkill and bug-prone.<br>
</blockquote>
<br></div></div>
I don't think the ast_frame needs to be extended, but merely a new frame type added that has a payload defined for this purpose. Right now reading RTCP data returns a null frame. It can be changed to return this new frame with the data. The ast_read function can recognize this frame type and invoke what ever logic may be required. This can include giving this data to the translation path (implementations can be changed to explicitly define that they support it). This also does not alter the threading model and gives a guarantee that the codec won't have to protect itself, like would need to be done for a stasis message.<div><div class="gmail-m_-1272311461936949868h5"><br></div></div></blockquote><div><br></div><div><br></div></div></div><div>Yes, a new ast_frame type is what I assumed would be a way as well, but as you point out it would indeed require some refactoring here and there to make the parties, and the translator core in particular, aware of that. Anyway, I guess this is fair and definitely makes sense and fits the architecture.</div><div><br></div><div>I'll try to experiment with this approach as well and come back with some proof of concept code that shows this working.</div><div><div class="gmail-h5"><div><br></div></div></div></div></div></div></blockquote><div><br></div><div><br></div><div>Hi all,</div><div><br></div><div>I just completed a tentative patch that implements what we discussed. You can find it attached (assuming mailman doesn't strip it), and I used the Asterisk 14.1.2 as a basis if you want to try it. Can you give a look to see if this is in line with what we discussed, and if as an approach it is in line with the architecture philosophy of Asterisk itself?</div><div><br></div><div>To summarize, this is what I did:</div><div><br></div><div><ol><li>I added a new ast_frame frametype called AST_FRAME_RTCP (I guess we could re-use AST_FRAME_CONTROL with something like an AST_CONTROL_RTCP subclass, but that's semantics);</li><li>I also added a new callback that translators can implement, called "feedback", that takes a pvt and a frame as parameters just as "framein"; as a proof of concept, I implemented a placeholder method for codec_speex that prints the incoming feedback; </li><li>when the RTP stack receives a SR/RR, ast_rtcp_read returns a frame containing a copy of the ast_rtp_rtcp_report object instead of a null frame;<br></li><li>when __ast_read gets an AST_FRAME_RTCP frame, it checks if a "write" translator exists (as we want to notify the encoder, not the decoder) via ast_channel_writetrans(chan) and calls ast_translate over it to pass over the frame;<br></li><li>when ast_translate gets an AST_FRAME_RTCP frame, it checks if the "feedback" callback exists for the specified translator, and if it does it sends the frame there;</li><li>the feedback callback in the translator can be used by the codec to parse the ast_rtp_rtcp_report object and handle it accordingly.</li></ol></div><div> </div><div>Not sure if this is exactly what you meant or envisaged but, while probably ugly, it seems to work fine, and does not affect codec modules not interested in or aware of the feature.</div><div><br></div><div>That said, there are probably a few things to think about. For one, the feedback is "routed" by __ast_read automatically, without involving channel modules themselves. Not sure if this is good or not, but I thought it made sense to have something that worked automatically no matter the channel, and without requiring any update on their code either. Nevertheless, there may be reasons I'm not aware of for having this go through channel modules anyway, so let me know if that's indeed an issue.</div><div><br></div><div>Another potential issue in the patch in its current form is that I'm not sure if codec translator chaining can happen in calls and if we should care. If, for instance, there's a slin->slin16->codecX because codecX only has "native" slin16->codecX support but we're bridging to slin, then this approach may need be extended, namely to make sure the feedback gets to all the pieces, or at least to the one that really matters (the last one?) In fact, I guess ast_channel_writetrans(chan) would pass the feedback to slin->slin16, and not to slin16->codecX which is where the real deal should happen instead. Not exactly sure on how we could handle this: at the moment, the feedback callback doesn't expect the module to return anything, something we might change to see if we can have this feedback crawl up. Alternatively this could be done automatically within translate.c itself, who would be aware of such a chain. Again, not even sure this is an issue but I thought I'd mention it.</div><div><br></div><div>To conclude, the patch doesn't currently do anything with the feedback. I guess that at this stage that's beyond the point, as the main result I wanted to achieve was putting up some sort of feedback mechanism in the first place. Should you believe I'm on the right way I'll work a bit on the codec stuff itself (and speex here is a good candidate as it does have many knobs one can play with).</div><div><br></div><div>As a side note, I anticipated the code is ugly in its current form, and I haven't checked if there can be leaks as it is. I've been away from Asterisk coding for a while, so fresh eyes can probably help spot those, if any! :-)</div><div><br></div><div>Hope this helps, looking forward to your thoughts!<br>Lorenzo</div><div><br></div><div><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"><div class="gmail_extra"><div class="gmail_quote"><div><div class="gmail-h5"><div></div><div><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><div class="gmail-m_-1272311461936949868h5">
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Another possible approach, and possibly the way to go, is to make use of<br>
the Stasis message bus, something I was not aware of until I watched<br>
Matt's excellent presentation at a conference recently. I saw how the<br>
RTP engine in Asterisk does publish RTCP feecback on the bus, and that<br>
you can subscribe to that as other events (as the HEP integration does,<br>
for instance) from other parts of the code. I tried doing the same<br>
within the Opus codec implementation, and apart from some quirks (e.g.,<br>
weird fields in report blocks, like negative source_ssrc) it seemed to<br>
do exactly what I was looking for. The only problem, though, is that<br>
while a Stasis event contains a whole lot of info, codec modules are<br>
pretty much clueless and have no way of matching a specific event<br>
related to a specific call to a translator context they're handling. In<br>
fact, AFAIK codec modules have no visibility at all of the channel a<br>
translator is associated with, or of other identifiers it could rely on.<br>
Thinking about this I did find a way to implement some kind of loose<br>
mapping by extending the ast_frame structure with two new properties,<br>
"ssrc" and "themssrc": basically, anytime an RTP packet is received, the<br>
RTP engine copies the local and remote SSRC to the frame before passing<br>
it to the core. When the first packet gets to the codec module, it can<br>
keep track of them and save them locally to its own internal struct. So,<br>
when a new event comes later from Stasis (e.g., an RTCP RR), it can look<br>
at the SSRC it relates to and match it with the SSRCs it is aware of, so<br>
that it knows it's related to a specific translator context and react<br>
accordingly. While this seems effective, it has a few issues, though.<br>
For instance, there's no way to assume remote SSRCs will be unique,<br>
which means this could result in either missing or misleading feedback<br>
in some cases, or even that they'll stay the same for the whole call.<br>
Another aspect I haven't considered is the possible overhead, although I<br>
don't think crawling a JSON object should take much resources. Besides,<br>
I still haven't understood how Asterisk hashtables work, so this part is<br>
still just theory :-)<br>
</blockquote>
<br></div></div>
Personally I'm not a huge fan of using the stasis messages for exactly the reason you've given in your follow-up email. There's no obvious reliable data to key off of and it also requires synchronization between the thread handling the message and the thread doing the transcoding. This changes the threading model for transcoding enough to be of a concern to me and also introduces another lock.<span><br>
<br></span></blockquote><div><br></div><div><br></div></div></div><div>I had thought about synchronization and locking may be easily avoided if you just keep track of what needs to be changed when receiving the message, and only enforce it when you get the next packet you need to transcode. But yes, it does make things more complicated threading-wise, especially considering that even just keeping track of things may require atomic operations to be safe.</div><div><br></div><div>Thanks,</div><div>Lorenzo</div><span class="gmail-"><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
That's basically it. Do you have any feeling/feedback on this? Is this<br>
actually worth investigating, and do you believe I'm on the right track?<br>
Any suggestion on how to make the mapping more effective in the codec<br>
module? I'm of course willing to contribute to such an effort, if it's<br>
deemed worthwhile.<br>
</blockquote>
<br></span>
Cheers,<span class="gmail-m_-1272311461936949868HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Joshua Colp<br>
Digium, Inc. | Senior Software Developer<br>
445 Jan Davis Drive NW - Huntsville, AL 35806 - US<br>
Check us out at: <a href="http://www.digium.com" rel="noreferrer" target="_blank">www.digium.com</a> & <a href="http://www.asterisk.org" rel="noreferrer" target="_blank">www.asterisk.org</a><br>
<br>
-- <br>
______________________________<wbr>______________________________<wbr>_________<br>
-- Bandwidth and Colocation Provided by <a href="http://www.api-digital.com" rel="noreferrer" target="_blank">http://www.api-digital.com</a> --<br>
<br>
asterisk-dev mailing list<br>
To UNSUBSCRIBE or update options visit:<br>
<a href="http://lists.digium.com/mailman/listinfo/asterisk-dev" rel="noreferrer" target="_blank">http://lists.digium.com/mailma<wbr>n/listinfo/asterisk-dev</a><br>
</font></span></blockquote></span></div><br></div></div>
</blockquote></div><br></div></div>