C API usage. How to achieve some tasks

Hello everyone.

I am trying to learn asterisk C API. Great and very useful software, by the way.

I am hacking the codebase trying to figure out how to play a sequence of commands in asterisk from C/C++ (exclusively). This must be done in C/C++ since this is an exercise for training.

I am creating an application module that will call my application in the dialplan (in extensions.conf) as this:

exten => _XX.,1,MyApp()

MyApp will execute all the logic for the call in the C/C++ module.

What I have for now is something like this (only shown relevant parts):

namespace {

int on_call_dialed(struct ast_channel * c,
const char * val) {
    if (ast_answer(c))
        ast_log(LOG_ERROR, "Error answering call\n");
    //Must filter dialed number
    //Must play sound.
    //Must write cdr
    //Must hangup
    return 0;

} //anonymous namespace

static ast_module_load_result load_module(void) {
    if (ast_register_application(
            "Test application",
            "Test application")) {
            ast_log(LOG_ERROR, "Could not load MyApp app module\n");
            return AST_MODULE_LOAD_FAILURE;
    ast_log(LOG_NOTICE, "Loaded MyApp app module\n");


My questions are:

1.- I need to filter the dialed phone number. How can I get the dialed number in on_call_dialed?
The function is being invoked when dialing, I tested.
2.- If 2 or more calls enter at nearly the same time in the same context, 2 instances of the same context will be created or the second call will be queued or refused, making my application slower under heavy load?
2.- I need to play a sound. I am aware of the Playback application. How could I use it from my C++ module without going to extensions.conf?
3.- Any idea of how to write to the cdr from C++ module?
3.- The callback invocation (I mean on_call_dialed) : is it fully asynchronous? What are the thread safety guarantees in general or any direction you could give me if it’s not asynchronous by default, so that I know how to protect relevant data structures to make everything more concurrent?