File: cmthandler.vala

package info (click to toggle)
fso-audiod 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,220 kB
  • ctags: 2,357
  • sloc: ansic: 16,763; sh: 11,353; makefile: 546
file content (593 lines) | stat: -rw-r--r-- 18,261 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * Copyright (C) 2011-2012 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *                         Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
extern int snd_pcm_format_set_silence (Alsa.PcmFormat format,void * data, uint samples );


/**
 * @class RingBuffer
 *
 * Provides a ring buffer for alsa frames
 **/
errordomain RingError {
    Overflow,
    Underflow
}

class RingBuffer : FsoFramework.AbstractObject
{
    private uint8[] ring;
    private int ring_head;
    private int ring_tail;
    private int ring_size;
    
    public RingBuffer( int size )
    {
        this.ring = new uint8[size];
        this.ring_head = 0;
        this.ring_tail = 0;
        this.ring_size = size;
    }

    public void write( uint8[] x, int count ) throws RingError
    {
        int new_ring_head = (ring_head + count) % ring_size;
        int free = (ring_size + ring_tail - ring_head) % ring_size;
        if ( free == 0 )
            free = ring_size;
        logger.debug( @"RingBuffer.write: ring_head=$(ring_head), ring_tail=$(ring_tail), count=$(count), free=$(free)");
        if ( count > free )
        {
            throw new RingError.Overflow( @"Buffer is full (free: $free / wanted to write: $count)" );
        }		 

        logger.debug( @"RingBuffer.write: new ring_head would be $(new_ring_head)");
        /* check wraparound */
        if ( new_ring_head == 0 || new_ring_head > ring_head )
        {
            /* check next line for +-1 errors I made! */
            logger.debug( @"RingBuffer.write: does not overlap - chunk = $(count)");
            Memory.copy( &ring[ring_head], x, count );
        }
        else
        {
            logger.debug( @"RingBuffer.write: does overlap - first chunk = $(ring_size - ring_head)");
            /* check next 2 lines for +-1 errors I made! */
            Memory.copy( &ring[ring_head], x, ring_size - ring_head );
            logger.debug( @"RingBuffer.write: second chunk = $(ring_size - ring_head)");
            Memory.copy( &ring[0], &x[ring_size - ring_head], count - (ring_size - ring_head) );
        }
        ring_head = new_ring_head;
    }

    /* we pass the pointer to buffer as a parameter, so you can use existing buffers */
    /* wouldn't want the function to malloc a new buffer each time */
    public void read( uint8[] x, int count ) throws RingError
    {
        int avail = (ring_size + ring_head - ring_tail) % ring_size;

        logger.debug( @"RingBuffer.read: ring_head=$(ring_head), ring_tail=$(ring_tail), count=$(count), avail=$(avail)");
        if ( avail < count )
        {
            throw new RingError.Underflow( @"Buffer has only $avail bytes available ($count requested)" );
        }

        int new_ring_tail = (ring_tail + count) % ring_size;
        logger.debug( @"RingBuffer.read: new_ring_tail would be $(new_ring_tail)");

        if ( new_ring_tail == 0 || new_ring_tail > ring_tail )
        {
            logger.debug( @"RingBuffer.read: does not wrap - chunk = $(count)" );
            Memory.copy( x, &ring[ring_tail], count );
        }
        else
        {
            logger.debug( @"RingBuffer.read: does overwrap - first chunk = $(ring_size - ring_tail)");
            Memory.copy( x, &ring[ring_tail], ring_size - ring_tail );
            logger.debug( @"RingBuffer.read: second chunk = $(count - (ring_size - ring_tail))");
            Memory.copy( &x[ring_size - ring_tail], ring, count - (ring_size - ring_tail) );
        }
        ring_tail = new_ring_tail;
    }

    public void reset()
    {
        ring_tail = ring_head = 0;
    }
    public override string repr()
    {
        return "<RingBuffer>";
    }
}



/**
 * @class CmtHandler
 *
 * Handles Audio via libcmtspeechdata
 **/
public class CmtHandler : FsoFramework.AbstractObject
{
    private CmtSpeech.Connection connection;
    private IOChannel channel;
    private FsoAudio.PcmDevice pcmout = null;
    private FsoAudio.PcmDevice pcmin = null;
    private bool status;
    private const int FCOUNT = 160;
    private const int FRAMESIZE = 2;
    private const int BUFSIZE = 3 * FCOUNT * FRAMESIZE;

    /* alsa parameters */
    private Alsa.PcmFormat format = Alsa.PcmFormat.S16_LE;
    private Alsa.PcmAccess access = Alsa.PcmAccess.RW_INTERLEAVED;

    /* silence buffer */
    private uint8[] silence_buffer = null;

    /* playback Thread */
    private Thread<void *> playbackThread = null;
    private int runPlaybackThread = 0;
    private Mutex playbackMutex = new Mutex();
    private RingBuffer fromModem;

    /* record Thread */
    private Thread<void *> recordThread = null;
    private bool runRecordThread = false;
    private Mutex recordMutex = new Mutex();
    private RingBuffer toModem;
    private int timing; // feed UL to the modem (in ms)

    //
    // Constructor
    //
    public CmtHandler()
    {
        status = false;

        assert( logger.debug( "Initializing cmtspeech" ) );
        CmtSpeech.init();

        assert( logger.debug( "Setting up traces" ) );
        CmtSpeech.trace_toggle( CmtSpeech.TraceType.STATE_CHANGE, true );
        CmtSpeech.trace_toggle( CmtSpeech.TraceType.IO, true );
        CmtSpeech.trace_toggle( CmtSpeech.TraceType.DEBUG, true );

        assert( logger.debug( "Instanciating connection" ) );
        connection = new CmtSpeech.Connection();
        if ( connection == null )
        {
            logger.error( "Can't instanciate connection" );
            return;
        }

        var fd = connection.descriptor();

        if ( fd == -1 )
        {
            logger.error( "Cmtspeech file descriptor invalid" );
        }

        timing = 50;
        fromModem = new RingBuffer( BUFSIZE );
        toModem = new RingBuffer( BUFSIZE );

        assert( logger.debug( "Hooking up fd with main loop" ) );
        channel = new IOChannel.unix_new( fd );
        channel.add_watch( IOCondition.IN | IOCondition.HUP, onInputFromChannel );

        silence_buffer = new uint8[ FCOUNT  * FRAMESIZE ];
        snd_pcm_format_set_silence( format, silence_buffer, FCOUNT );

        logger.info( "Created" );
    }

    //
    // Private API
    //

    private void play_silence( int count )
    {
        Alsa.PcmSignedFrames ret;
        int retries = 3;

        while ( count > 0 && retries > 0 )
        {
            try
            {
                ret = pcmout.writei( silence_buffer, FCOUNT );
                if ( ret == -Posix.EPIPE )
                {
                    pcmout.recover( -Posix.EPIPE, 0 );
                    retries--;
                }
                else
                {
                    count--;
                }
            }
            catch ( FsoAudio.SoundError e )
            {
                logger.error( @"Error: $(e.message)" );
                return;
            }
        }
    }

    private bool feed_to_modem()
    {
        CmtSpeech.FrameBuffer ulbuf = null;

        assert( logger.debug( "feeding from ringbuffer to modem" ) );
        if ( connection.is_active() )
        {
            var ok = connection.ul_buffer_acquire( out ulbuf );
            if ( ok == 0 )
            {
                assert( logger.debug( "protocol state is ACTIVE_DLUL, uploading as well..." ) );
                try
                {
                    recordMutex.lock();
                    toModem.read( (uint8[])ulbuf.payload, ulbuf.pcount );
                    recordMutex.unlock();
                }
                catch ( RingError e )
                {
                    recordMutex.unlock();
                    Memory.copy( ulbuf.payload, silence_buffer, ulbuf.pcount > FCOUNT * FRAMESIZE ? FCOUNT * FRAMESIZE : ulbuf.pcount );
                }
                connection.ul_buffer_release( ulbuf );
            }
        }
        return false;
    }

    private void * playbackThreadFunc()
    {
        while ( runPlaybackThread > 0 )
        {
            var buf = new uint8[ FCOUNT * FRAMESIZE ];
            Alsa.PcmSignedFrames frames;

            try
            {
                playbackMutex.lock();
                fromModem.read( buf, FCOUNT * FRAMESIZE );
                playbackMutex.unlock();
                frames = pcmout.writei( (uint8[])buf, FCOUNT );
                if ( frames != FCOUNT )
                {
                    logger.debug(@"frames: $((long)frames)");
                }
                else if ( frames == -Posix.EPIPE )
                {
                    pcmout.recover( -Posix.EPIPE, 0 );
                }
            }
            catch ( FsoAudio.SoundError e )
            {
                logger.error( @"Error: $(e.message)" );
            }
            catch ( RingError e )
            {
                playbackMutex.unlock();
                logger.warning( @"RingBuffer error: $(e.message)" );
                play_silence( 1 );
            }
        }

        return null;
    }


    private void * recordThreadFunc()
    {
        while ( runRecordThread == true )
        {
            var buf = new uint8[ FCOUNT * FRAMESIZE ];
            Alsa.PcmSignedFrames frames;

            Timeout.add_full( Priority.HIGH, timing, feed_to_modem );
            try
            {
                frames = pcmin.readi( (uint8[])buf, FCOUNT );
                if ( frames == -Posix.EPIPE )
                {
                    pcmin.prepare();
                }
                recordMutex.lock();
                toModem.write( buf, (int)frames * FRAMESIZE );
                recordMutex.unlock();
            }
            catch ( FsoAudio.SoundError e )
            {
                logger.error( @"SoundError: $(e.message)" );
            }
            catch ( RingError e )
            {
                recordMutex.unlock();
            }
        }

        return null;
    }

    private void alsaSinkSetup()
    {
        int channels = 1;
        int rate = 8000;

        pcmout = new FsoAudio.PcmDevice();
        assert( logger.debug( @"Setup alsa sink for modem audio" ) );
        try
        {
            pcmout.open( "plug:dmix" );
            pcmout.setFormat( access, format, rate, channels );
        }
        catch ( Error e )
        {
            logger.error( @"Error: $(e.message)" );
        }

        /* start the playback thread now
         */
        if ( !Thread.supported() )
        {
            logger.warning( "Cannot run without thread support!" );
        }
        else
        {
            if ( playbackThread == null )
            {
                try
                {
                    playbackThread = new Thread<void *>( "playback", playbackThreadFunc );
                }
                catch ( ThreadError e )
                {
                    logger.debug( @"Error: $(e.message)" );
                    return;
               }
            }
            else
            {
                logger.debug( "Thread already launched" );
            }
            AtomicInt.set(ref runPlaybackThread,1);
        }
    }

    private void alsaSrcSetup()
    {
        int channels = 1;
        int rate = 8000;

        pcmin = new FsoAudio.PcmDevice();
        assert( logger.debug( @"Setup alsa source for modem audio" ) );
        try
        {
            pcmin.open( "plug:dsnoop", Alsa.PcmStream.CAPTURE );
            pcmin.setFormat( access, format, rate, channels );
        }
        catch ( Error e )
        {
            logger.error( @"Error: $(e.message)" );
        }

        /* start the record thread now */
        if ( !Thread.supported() )
        {
            logger.warning( "Cannot run without thread support!" );
        }
        else
        {
            if ( recordThread == null )
            {
                try
                {
                    recordThread = new Thread<void *>( "record", recordThreadFunc );
                }
                catch ( ThreadError e )
                {
                    logger.debug( @"Error: $(e.message)" );
                    return;
               }
            }
            else
            {
                logger.debug( "Thread already launched" );
            }
            runRecordThread = true;
        }

    }

    private void alsaSinkCleanup()
    {
        AtomicInt.set(ref runPlaybackThread,0);
        playbackThread.join();
        playbackThread = null;
        pcmout.close();
        pcmout = null;
        fromModem.reset();
    }

    private void alsaSrcCleanup()
    {
        runRecordThread = false;
        recordThread.join();
        recordThread = null;
        pcmin.close();
        pcmin = null;
        toModem.reset();
    }

    private void handleTimingUpdate( CmtSpeech.Event event )
    {
        CmtSpeech.EventData* msg = (CmtSpeech.EventData*) (&event.msg);
        assert( logger.debug( @"modem UL timing update: msec = $(msg.timing_config_ntf.msec) usec = $(msg.timing_config_ntf.usec)" ) );
        timing = msg.timing_config_ntf.msec;
    }

    private void handleDataEvent()
    {
        assert( logger.debug( @"handleDataEvent during protocol state $(connection.protocol_state())" ) );

        CmtSpeech.FrameBuffer dlbuf = null;

        var ok = connection.dl_buffer_acquire( out dlbuf );
        if ( ok == 0 )
        {
            assert( logger.debug( @"received DL packet w/ $(dlbuf.count) bytes (payload is $(dlbuf.pcount))" ) );

            try
            {
                playbackMutex.lock();
                fromModem.write( (uint8[])dlbuf.payload, dlbuf.pcount );
                playbackMutex.unlock();
            }
            catch ( RingError e )
            {
                playbackMutex.unlock();
                logger.warning( @"RingBuffer error: $(e.message)" );
            }

            connection.dl_buffer_release( dlbuf );
        }

    }

    private void handleControlEvent()
    {
        assert( logger.debug( @"handleControlEvent during protocol state $(connection.protocol_state())" ) );

        CmtSpeech.Event event = CmtSpeech.Event();
        CmtSpeech.Transition transition = 0;

        connection.read_event( ref event );

        assert( logger.debug( @"read event, type is $(event.msg_type)" ) );
        transition = connection.event_to_state_transition( event );

        switch ( transition )
        {
            case CmtSpeech.Transition.INVALID:
                assert( logger.debug( "ERROR: invalid state transition") );
                break;

            case CmtSpeech.Transition.6_TIMING_UPDATE:
            case CmtSpeech.Transition.7_TIMING_UPDATE:
                handleTimingUpdate( event );
                break;

            case CmtSpeech.Transition.3_DL_START:
                alsaSinkSetup();
                break;

            case CmtSpeech.Transition.12_UL_START:
                alsaSrcSetup();
                break;

            case CmtSpeech.Transition.4_DLUL_STOP:
                alsaSinkCleanup();
                alsaSrcCleanup();
                break;

            case CmtSpeech.Transition.1_CONNECTED:
            case CmtSpeech.Transition.2_DISCONNECTED:
            case CmtSpeech.Transition.5_PARAM_UPDATE:
            case CmtSpeech.Transition.10_RESET:
            case CmtSpeech.Transition.11_UL_STOP:
                assert( logger.debug( @"State transition ok, new state is $transition" ) );
                break;

            default:
                assert_not_reached();
                break;
        }
    }

    private bool onInputFromChannel( IOChannel source, IOCondition condition )
    {
        //the following line is commented to work arround a vala 0.12.1 bug
        //with the use of to_string() on an enum, which results in a segmentation fault
        //assert( logger.debug( @"onInputFromChannel, condition = $condition" ) );

        assert( condition == IOCondition.HUP || condition == IOCondition.IN );

        if ( condition == IOCondition.HUP )
        {
            logger.warning( "HUP! Will no longer handle input from cmtspeechdata" );
            return false;
        }

        CmtSpeech.EventType flags = 0;
        var ok = connection.check_pending( out flags );
        if ( ok < 0 )
        {
            assert( logger.debug( "Error while checking for pending events..." ) );
        }
        else if ( ok == 0 )
        {
            assert( logger.debug( "D'oh, cmt speech readable, but no events pending..." ) );
        }
        else
        {
            assert( logger.debug( "Connection reports pending events with flags 0x%0X".printf( flags ) ) );

            if ( ( flags & CmtSpeech.EventType.DL_DATA ) == CmtSpeech.EventType.DL_DATA )
            {
                handleDataEvent();
            }
            else if ( ( flags & CmtSpeech.EventType.CONTROL ) == CmtSpeech.EventType.CONTROL )
            {
                handleControlEvent();
            }
            else
            {
                assert( logger.debug( "Event no DL_DATA nor CONTROL, ignoring" ) );
            }
        }

        return true;
    }

    //
    // Public API
    //

    public override string repr()
    {
        CmtSpeech.State state = ( connection != null ) ? connection.protocol_state() : 0;
        return @"<$state>";
    }

    public void setAudioStatus( bool enabled )
    {
        if ( enabled == status )
        {
            assert( logger.debug( @"Status already $status" ) );
            return;
        }

        assert( logger.debug( @"Setting call status to $enabled" ) );
        connection.state_change_call_status( enabled );
        status = enabled;
    }
}
// vim:ts=4:sw=4:expandtab