aboutsummaryrefslogtreecommitdiff
path: root/modules/pdp_live~.c
blob: aa8398d3e4a452ccc21c416609387ee8fd4c7d9d (plain)
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
 *   Pure Data Packet module.
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*  This object is a stream decoder object
 *  A lot of this object code is inspired by the excellent ffmpeg.c
 *  Copyright (c) 2000, 2001, 2002 Fabrice Bellard
 *  The rest is written by Yves Degoyon ( ydegoyon@free.fr )                             
 */


#include "pdp.h"
#include "yuv.h"
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <avformat.h>

#define VIDEO_BUFFER_SIZE (1024*1024)
#define MAX_AUDIO_PACKET_SIZE (128 * 1024)
#define MIN_AUDIO_SIZE (64 * 1024)
#define AUDIO_PACKET_SIZE (2*1152)

#define DEFAULT_CHANNELS 1
#define DEFAULT_FRAME_RATE 25
#define DEFAULT_WIDTH 320
#define DEFAULT_HEIGHT 240
#define DEFAULT_PRIORITY 0

static char   *pdp_live_version = "pdp_live~: version 0.1, a video stream decoder ( ydegoyon@free.fr).";

typedef struct pdp_live_struct
{
    t_object x_obj;
    t_float x_f;

    t_int x_packet0;
    t_int x_dropped;

    t_pdp *x_header;
    short int *x_data;
    t_int x_vwidth;
    t_int x_vheight;
    t_int x_vsize;

    t_outlet *x_pdp_out;           // output decoded pdp packets
    t_outlet *x_outlet_left;       // left audio output
    t_outlet *x_outlet_right;      // right audio output
    t_outlet *x_outlet_streaming;  // indicates the action of streaming
    t_outlet *x_outlet_nbframes;   // number of frames emitted

    pthread_t x_connectchild;      // thread used for connecting to a stream
    pthread_t x_decodechild;       // stream decoding thread
    t_int x_usethread;             // flag to activate decoding in a thread
    t_int x_priority;              // priority of decoding thread

    char  *x_url;
    t_int x_streaming;      // streaming flag
    t_int x_nbframes;       // number of frames emitted
    t_int x_framerate;      // framerate
    t_int x_samplerate;     // audio sample rate
    t_int x_audiochannels;  // audio channels
    t_int x_audioon;        // enough audio data to start playing
    struct timeval x_starttime; // streaming starting time
    t_int x_cursec;         // current second
    t_int x_secondcount;    // number of frames received in the current second
    t_int x_nbvideostreams; // number of video streams
    t_int x_nbaudiostreams; // number of audio streams

      /* AV data structures */
    AVFormatContext  *x_avcontext;
    AVFormatParameters x_avparameters; // unused but the call is necessary to allocate structures
    AVPacket x_pkt;                    // packet received on the stream
    AVPicture x_picture_decoded;
    t_int x_newpicture;

      /* audio structures */
    t_int x_audio;           // flag to activate the decoding of audio
    short x_audio_buf[4*MAX_AUDIO_PACKET_SIZE]; /* buffer for audio from stream*/
    short x_audio_in[4*MAX_AUDIO_PACKET_SIZE]; /* buffer for resampled PCM audio */
    t_int x_audioin_position; // writing position for incoming audio
    ReSampleContext *x_audio_resample_ctx; // structures for audio resample

} t_pdp_live;

static void pdp_live_priority(t_pdp_live *x, t_floatarg fpriority )
{
   x->x_priority = (int)fpriority;
}

static void pdp_live_threadify(t_pdp_live *x, t_floatarg fusethread )
{
   if ( ( fusethread == 0 ) || ( fusethread == 1 ) ) 
   {
      x->x_usethread = (int)fusethread;
   }
}

static void pdp_live_audio(t_pdp_live *x, t_floatarg faudio )
{
   if ( ( faudio == 0.0 ) || ( faudio == 1 ) )
   {
      x->x_audio = (int)faudio;
   }
}

static t_int pdp_live_decode_packet(t_pdp_live *x)
{
  t_int chunksize=0, length;
  t_int audiosize, sizeout, imagesize, pictureok;
  AVFrame frame;
  uint8_t *pcktptr;
  struct timeval etime;

   if ( !x->x_streaming )
   {
      return -1;
   }

   // read new packet on the stream
   if (av_read_packet(x->x_avcontext, &x->x_pkt) < 0) 
   {
      // post( "pdp_live~ : decoding thread : nothing to decode" );
      return -1;
   }
   // post( "pdp_live~ : read packet ( size=%d )", x->x_pkt.size );

   if (x->x_pkt.stream_index >= x->x_avcontext->nb_streams)
   {
      post("pdp_live~ : stream received out of range !! ");
      return 0;
   }

   length = x->x_pkt.size;
   pcktptr = x->x_pkt.data;
   while (length > 0) 
   {
         switch(x->x_avcontext->streams[x->x_pkt.stream_index]->codec.codec_type) 
         {
             case CODEC_TYPE_AUDIO:
                    if ( !x->x_audio )
                    {
                      av_free_packet(&x->x_pkt);
                      return 0;
                    }
                    chunksize = avcodec_decode_audio(&x->x_avcontext->streams[x->x_pkt.stream_index]->codec, 
                                               &x->x_audio_buf[0], &audiosize,
                                               pcktptr, length);
                    if (chunksize < 0)
                    {
                       post("pdp_live~ : could not decode audio input (ret=%d)", chunksize );
                       av_free_packet(&x->x_pkt);
                       continue;
                    }
                    // some bug in mpeg audio decoder gives
                    // data_size < 0, it seems they are overflows
                    if (audiosize <= 0) 
                    {
                        /* no audio frame */
                        pcktptr += chunksize;
                        length -= chunksize;
                        // post("pdp_live~ : audio overflow in decoder!");
                        continue;
                    }

                    // resample received audio
                    // post( "pdp_live~ : resampling from %dHz-%dch to %dHz-%dch (in position=%d)",
                    //                x->x_avcontext->streams[x->x_pkt.stream_index]->codec.sample_rate,
                    //                x->x_avcontext->streams[x->x_pkt.stream_index]->codec.channels,
                    //                (int)sys_getsr(), 2, x->x_audioin_position );

                    x->x_audiochannels = x->x_avcontext->streams[x->x_pkt.stream_index]->codec.channels;
                    x->x_samplerate = x->x_avcontext->streams[x->x_pkt.stream_index]->codec.sample_rate;
                    if (x->x_audio_resample_ctx) audio_resample_close(x->x_audio_resample_ctx);
                    x->x_audio_resample_ctx =
                          audio_resample_init(DEFAULT_CHANNELS, 
                                         x->x_avcontext->streams[x->x_pkt.stream_index]->codec.channels,
                                         (int)sys_getsr(),
                                         x->x_avcontext->streams[x->x_pkt.stream_index]->codec.sample_rate);

                    sizeout = audio_resample(x->x_audio_resample_ctx,
                                    &x->x_audio_in[x->x_audioin_position],
                                    &x->x_audio_buf[0],
                                    audiosize/(x->x_avcontext->streams[x->x_pkt.stream_index]->codec.channels * sizeof(short)));
                    sizeout = sizeout * DEFAULT_CHANNELS;

                    if ( ( x->x_audioin_position + sizeout ) < 3*MAX_AUDIO_PACKET_SIZE )
                    {
                      x->x_audioin_position = x->x_audioin_position + sizeout;
                    }
                    else
                    {
                      post( "pdp_live~ : audio overflow : packet ignored...");
                    }
                    if ( ( x->x_audioin_position > MIN_AUDIO_SIZE ) && (!x->x_audioon) )
                    {
                       x->x_audioon = 1;
                       // post( "pdp_live~ : audio on" );
                    }
                    break;

             case CODEC_TYPE_VIDEO:

                    imagesize = (x->x_avcontext->streams[x->x_pkt.stream_index]->codec.width * 
                                 x->x_avcontext->streams[x->x_pkt.stream_index]->codec.height * 3) / 2; // yuv planar

                    // do not believe the declared framerate
                    // x->x_framerate = x->x_avcontext->streams[x->x_pkt.stream_index]->codec.frame_rate / 10000;

                    // calculate actual frame rate
                    if ( gettimeofday(&etime, NULL) == -1)
                    {
                       post("pdp_live~ : could not read time" );
                    }
                    if ( ( etime.tv_sec - x->x_starttime.tv_sec ) > 0 )
                    {
                       x->x_framerate = x->x_nbframes / ( etime.tv_sec - x->x_starttime.tv_sec );
                    }
                    if ( x->x_framerate == 0 ) x->x_framerate = 1;
                    // post ("pdp_live~ : frame rate is %d", x->x_framerate );

                    chunksize = avcodec_decode_video(
                                   &x->x_avcontext->streams[x->x_pkt.stream_index]->codec,
                                   &frame, &pictureok, 
                                   pcktptr, length);
                    if ( x->x_avcontext->streams[x->x_pkt.stream_index]->codec.pix_fmt != PIX_FMT_YUV420P )
                    {
                       post( "pdp_live~ : unsupported image format : %d", 
                              x->x_avcontext->streams[x->x_pkt.stream_index]->codec.pix_fmt ); 
                       pictureok = 0;
                    }
                    // post( "pdp_live~ : decoded new frame : type=%d format=%d (w=%d) (h=%d) (linesizes=%d,%d,%d,%d)", 
                    //           frame.pict_type,
                    //           x->x_avcontext->streams[x->x_pkt.stream_index]->codec.pix_fmt, 
                    //           x->x_avcontext->streams[x->x_pkt.stream_index]->codec.width, 
                    //           x->x_avcontext->streams[x->x_pkt.stream_index]->codec.height, 
                    //           frame.linesize[0],
                    //           frame.linesize[1],
                    //           frame.linesize[2],
                    //           frame.linesize[3] );
                    x->x_picture_decoded = *(AVPicture*)&frame;
                    x->x_avcontext->streams[x->x_pkt.stream_index]->quality= frame.quality;
                    if (chunksize < 0) 
                    {
                       av_free_packet(&x->x_pkt);
                       post("pdp_live~ : could not decode video frame (ret=%d)", chunksize );
                       return 0;
                    }
                    if (!pictureok) 
                    {
                       // no picture yet 
                       pcktptr += chunksize;
                       length -= chunksize;
                       continue;
                    }
                    else
                    {  
                        x->x_newpicture=1;
                        x->x_vwidth = x->x_avcontext->streams[x->x_pkt.stream_index]->codec.width;
                        x->x_vheight = x->x_avcontext->streams[x->x_pkt.stream_index]->codec.height;
                        x->x_vsize = x->x_vwidth*x->x_vheight;
                    }
                    break;
         }
         pcktptr += chunksize;
         length -= chunksize;
         if ( !x->x_streaming ) break;
   }
   av_free_packet(&x->x_pkt);

   // post( "pdp_live~ : decoded one packet" );
   return 0;

}

static void *pdp_decode_stream_from_url(void *tdata)
{
  t_pdp_live *x = (t_pdp_live*)tdata;
  struct sched_param schedprio;
  t_int pmin, pmax;
  struct timespec twait;

    twait.tv_sec = 0; 
    twait.tv_nsec = 10000000; // 10 ms
 
    schedprio.sched_priority = 0;
    if ( sched_setscheduler(0,SCHED_OTHER,&schedprio) == -1)
    {
       post("pdp_live~ : couldn't set scheduler for decoding thread.\n");
    }
    if ( setpriority( PRIO_PROCESS, 0, x->x_priority ) < 0 )
    {
       post("pdp_live~ : couldn't set priority to %d for decoding thread.\n", x->x_priority );
    }
    else
    {
       post("pdp_live~ : priority set to %d for thread %d.\n", x->x_priority, x->x_decodechild );
    }

    if ( ! (x->x_avcontext->iformat->flags & AVFMT_NOHEADER ) )
    {
       if (x->x_avcontext->iformat->read_header(x->x_avcontext, &x->x_avparameters) < 0) 
       {
          post( "pdp_live~ : couldn't read header" );
       }
       post( "pdp_live~ : read header." );
    }

    while ( x->x_streaming )
    {
      while ( x->x_newpicture ) nanosleep( &twait, NULL );
     
      // decode incoming packets
      if ( pdp_live_decode_packet( x ) < 0 )
      {
         nanosleep( &twait, NULL ); // nothing to read, just wait
      }
    }

    post( "pdp_live~ : decoding thread %d exiting....", x->x_decodechild );
    x->x_decodechild = 0;
    pthread_exit(NULL);
}

static void *pdp_live_connect_to_url(void *tdata)
{
  int i, err;
  t_pdp_live *x = (t_pdp_live*)tdata;
  pthread_attr_t decode_child_attr;

    memset(&x->x_avparameters, 0, sizeof(AVFormatParameters));
    x->x_avparameters.sample_rate = sys_getsr();
    x->x_avparameters.channels = DEFAULT_CHANNELS;
    x->x_avparameters.frame_rate = DEFAULT_FRAME_RATE;
    x->x_avparameters.width = DEFAULT_WIDTH;
    x->x_avparameters.height = DEFAULT_HEIGHT;
    x->x_avparameters.image_format = PIX_FMT_YUV420P;

    post( "pdp_live~ : opening url : %s", x->x_url );
    err = av_open_input_file(&x->x_avcontext, x->x_url, x->x_avcontext->iformat, 0, &x->x_avparameters);
    if (err < 0)
    {
      if ( err == -1 ) post( "pdp_live~ : unknown error" );
      if ( err == -2 ) post( "pdp_live~ : i/o error" );
      if ( err == -3 ) post( "pdp_live~ : number syntax expected in filename" );
      if ( err == -4 ) post( "pdp_live~ : invalid data found" );
      if ( err == -5 ) post( "pdp_live~ : not enough memory" );
      if ( err == -6 ) post( "pdp_live~ : unknown format ( stream not found? )" );
      x->x_connectchild = 0;
      x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
      pthread_exit(NULL);
    }
    /* If not enough info to get the stream parameters, we decode the
       first frames to get it. (used in mpeg case for example) */
    err = av_find_stream_info(x->x_avcontext);
    if (err < 0) 
    {
      post( "pdp_live~ : %s: could not find codec parameters\n", x->x_url);
      x->x_connectchild = 0;
      av_close_input_file(x->x_avcontext);
      x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
      pthread_exit(NULL);
    }

    // post( "pdp_live~ : stream reader : %x", x->x_avcontext->iformat );

    /* copy stream format */
    x->x_nbvideostreams = 0;
    x->x_nbaudiostreams = 0;

    for(i=0;i<x->x_avcontext->nb_streams;i++) 
    {
      AVStream *st;

        if ( x->x_avcontext->streams[i]->codec.codec_type == CODEC_TYPE_UNKNOWN )
        {
           post( "pdp_live~ : stream #%d # type : unknown", i ); 
        }
        if ( x->x_avcontext->streams[i]->codec.codec_type == CODEC_TYPE_AUDIO )
        {
           post( "pdp_live~ : stream #%d # type : audio # id : %d # bitrate : %d", 
                 i, x->x_avcontext->streams[i]->codec.codec_id, x->x_avcontext->streams[i]->codec.bit_rate ); 
           post( "pdp_live~ : sample rate : %d # channels : %d", 
                 x->x_avcontext->streams[i]->codec.sample_rate, x->x_avcontext->streams[i]->codec.channels ); 
           x->x_nbaudiostreams++;
        }
        if ( x->x_avcontext->streams[i]->codec.codec_type == CODEC_TYPE_VIDEO )
        {
           post( "pdp_live~ : stream #%d # type : video # id : %d # bitrate : %d", 
                 i, x->x_avcontext->streams[i]->codec.codec_id, 
                 x->x_avcontext->streams[i]->codec.bit_rate ); 
           post( "pdp_live~ : framerate : %d # width : %d # height : %d", 
                 x->x_avcontext->streams[i]->codec.frame_rate/10000, 
                 x->x_avcontext->streams[i]->codec.width, 
                 x->x_avcontext->streams[i]->codec.height ); 
           x->x_nbvideostreams++;
        }
    }

     /* open each decoder */
    for(i=0;i<x->x_avcontext->nb_streams;i++) 
    {
      AVCodec *codec;
      post("pdp_live~ : opening decoder for stream #%d", i);
      codec = avcodec_find_decoder(x->x_avcontext->streams[i]->codec.codec_id);
      if (!codec) 
      {
          post("pdp_live~ : unsupported codec for output stream #%d\n", i );
          x->x_streaming = 0;
          x->x_connectchild = 0;
          av_close_input_file(x->x_avcontext);
          x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
          pthread_exit(NULL);
      }
      if (avcodec_open(&x->x_avcontext->streams[i]->codec, codec) < 0) 
      {
          post("pdp_live~ : error while opening codec for stream #%d - maybe incorrect parameters such as bit_rate, rate, width or height\n", i);
          x->x_streaming = 0;
          x->x_connectchild = 0;
          av_close_input_file(x->x_avcontext);
          x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
          pthread_exit(NULL);
      }
      else
      {
          post("pdp_live~ : opened decoder for stream #%d", i);
      }
    }

    if ( gettimeofday(&x->x_starttime, NULL) == -1)
    {
       post("pdp_live~ : could not set start time" );
    }
    x->x_streaming = 1;
    x->x_nbframes = 0;

    x->x_connectchild = 0;

    if ( x->x_usethread )
    {
      // launch decoding thread
      if ( pthread_attr_init( &decode_child_attr ) < 0 ) 
      {
         post( "pdp_live~ : could not launch decoding thread" );
         perror( "pthread_attr_init" );
         av_close_input_file(x->x_avcontext);
         x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
         pthread_exit(NULL);
      }
      if ( pthread_create( &x->x_decodechild, &decode_child_attr, pdp_decode_stream_from_url, x ) < 0 ) 
      {
         post( "pdp_live~ : could not launch decoding thread" );
         perror( "pthread_create" );
         av_close_input_file(x->x_avcontext);
         x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
         pthread_exit(NULL);
      }
      else
      {
         post( "pdp_live~ : decoding thread %d launched", (int)x->x_decodechild );
      }
    }
   
    pthread_exit(NULL);
}

static void pdp_live_disconnect(t_pdp_live *x)
{
 t_int ret, i, count=0;
 struct timespec twait;

   twait.tv_sec = 0; 
   twait.tv_nsec = 100000000; // 100 ms

   if (!x->x_streaming)
   {
     post("pdp_live~ : close request but no stream is played ... ignored" );
     return;
   }

   if ( x->x_streaming )
   {
     x->x_streaming = 0;
     x->x_newpicture = 0;
     post("pdp_live~ : waiting for the end of decoding thread..." );
     while ( x->x_decodechild && ( count < 100 ) ) 
     {
       count++;
       sleep( 1 );
     }
     if ( x->x_decodechild )
     {
       post("pdp_live~ : zombie thread, i guess" );
     }
     post("pdp_live~ : closing input file..." );
     av_close_input_file(x->x_avcontext);
     x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
   }

   outlet_float( x->x_outlet_streaming, x->x_streaming );
   x->x_nbframes = 0;
   outlet_float( x->x_outlet_nbframes, x->x_nbframes );

   if (x->x_audio_resample_ctx) 
   {
     audio_resample_close(x->x_audio_resample_ctx);
     x->x_audio_resample_ctx = NULL;
   }

}

static void pdp_live_connect(t_pdp_live *x, t_symbol *s)
{
  t_int ret, i;
  pthread_attr_t connect_child_attr;

   if ( ( x->x_streaming ) || ( x->x_connectchild != 0 ) )
   {
     post("pdp_live~ : connection request but a connection is pending ... disconnecting" );
     pdp_live_disconnect(x);
   }

   if ( x->x_url ) free( x->x_url );
   x->x_url = (char*) malloc( strlen( s->s_name ) + 1 );
   strcpy( x->x_url, s->s_name );

   // launch connection thread
   if ( pthread_attr_init( &connect_child_attr ) < 0 ) {
       post( "pdp_live~ : could not launch connection thread" );
       perror( "pthread_attr_init" );
       return;
   }
   if ( pthread_attr_setdetachstate( &connect_child_attr, PTHREAD_CREATE_DETACHED ) < 0 ) {
       post( "pdp_live~ : could not launch connection thread" );
       perror( "pthread_attr_setdetachstate" );
       return;
   }
   if ( pthread_create( &x->x_connectchild, &connect_child_attr, pdp_live_connect_to_url, x ) < 0 ) {
       post( "pdp_live~ : could not launch connection thread" );
       perror( "pthread_create" );
       return;
   }
   else
   {
       post( "pdp_live~ : connection thread %d launched", (int)x->x_connectchild );
   }
   
   return;
}

    /* decode the stream to fill up buffers */
static t_int *pdp_live_perform(t_int *w)
{
  t_float *out1   = (t_float *)(w[1]);       // left audio inlet
  t_float *out2   = (t_float *)(w[2]);       // right audio inlet 
  t_pdp_live *x = (t_pdp_live *)(w[3]);
  int n = (int)(w[4]);                      // number of samples 
  short int *pY, *pU, *pV; 
  uint8_t *psY, *psU, *psV; 
  t_int pixRGB, px, py;
  short sampleL, sampleR;
  struct timeval etime;
  t_int sn;

    // decode a packet if not in thread mode
    if ( !x->x_usethread )
    {
      pdp_live_decode_packet( x );
    }

    // just read the buffer
    if ( x->x_audioon )
    {
      sn=0;
      n=n*DEFAULT_CHANNELS;
      while (n--) 
      {
        sampleL=x->x_audio_in[ sn++ ];
        *(out1) = ((t_float)sampleL)/32768.0;
        if ( DEFAULT_CHANNELS == 1 )
        {
          *(out2) = *(out1);
        }
        if ( DEFAULT_CHANNELS == 2 )
        {
          sampleR=x->x_audio_in[ sn++ ];
          *(out2) = ((t_float)sampleR)/32768.0;
        }
        out1++;
        out2++;
      }
      x->x_audioin_position-=sn;
      memcpy( &x->x_audio_in[0], &x->x_audio_in[sn], 4*MAX_AUDIO_PACKET_SIZE-sn );
      // post( "pdp_live~ : audio in position : %d", x->x_audioin_position );
      if ( x->x_audioin_position <= sn )
      {
         x->x_audioon = 0;
         // post( "pdp_live~ : audio off" );
      }
    }
    else
    {
      // post("pdp_live~ : no available audio" );
      while (n--)
      {
        *(out1++) = 0.0;
        *(out2++) = 0.0;
      }
    }	

    // check if the framerate has been exceeded
    if ( gettimeofday(&etime, NULL) == -1)
    {
       post("pdp_live~ : could not read time" );
    }
    if ( etime.tv_sec != x->x_cursec )
    {
       x->x_cursec = etime.tv_sec;
       x->x_secondcount = 0;
    }
    if ( x->x_secondcount >= x->x_framerate )
    {
       // return (w+5);
    }

    // output image if there's a new one decoded
    if ( x->x_newpicture )
    {
       // create a new pdp packet from PIX_FMT_YUV420P image format
       x->x_packet0 = pdp_packet_new_image_YCrCb( x->x_vwidth, x->x_vheight );
       x->x_header = pdp_packet_header(x->x_packet0);
       x->x_data = (short int *)pdp_packet_data(x->x_packet0);

       pY = x->x_data;
       pV = x->x_data+x->x_vsize;
       pU = x->x_data+x->x_vsize+(x->x_vsize>>2);

       psY = x->x_picture_decoded.data[0];
       psU = x->x_picture_decoded.data[1];
       psV = x->x_picture_decoded.data[2];

       for ( py=0; py<x->x_vheight; py++)
       {
         for ( px=0; px<x->x_vwidth; px++)
         {
           *(pY) = ( *(psY+px) << 7 ); 
           *(pV) = ( ((*(psV+(px>>1)))-128) << 8 ); 
           *(pU) = ( ((*(psU+(px>>1)))-128) << 8 ); 
           pY++;
           if ( (px%2==0) && (py%2==0) ) 
           {
              pV++; pU++;
           }
         }
         psY += x->x_picture_decoded.linesize[0];
         if ( py%2==0 ) psU += x->x_picture_decoded.linesize[1];
         if ( py%2==0 ) psV += x->x_picture_decoded.linesize[2];
       }
         
       pdp_packet_pass_if_valid(x->x_pdp_out, &x->x_packet0);

       // update streaming status
       outlet_float( x->x_outlet_streaming, x->x_streaming );
       x->x_nbframes++;
       x->x_secondcount++;
       outlet_float( x->x_outlet_nbframes, x->x_nbframes );

       x->x_newpicture = 0;
    }

    return (w+5);
}

static void pdp_live_dsp(t_pdp_live *x, t_signal **sp)
{
    dsp_add(pdp_live_perform, 4, sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
}

static void pdp_live_free(t_pdp_live *x)
{
  int i;

    if ( x->x_streaming )
    {
       pdp_live_disconnect(x);
       sleep(3);
    }
    post( "pdp_live~ : freeing object" );
    pdp_packet_mark_unused(x->x_packet0);
    if (x->x_audio_resample_ctx) 
    {
      audio_resample_close(x->x_audio_resample_ctx);
      x->x_audio_resample_ctx = NULL;
    }
    av_free_static();
}

t_class *pdp_live_class;

void *pdp_live_new(void)
{
    int i;

    t_pdp_live *x = (t_pdp_live *)pd_new(pdp_live_class);

    x->x_pdp_out = outlet_new(&x->x_obj, &s_anything);

    x->x_outlet_left = outlet_new(&x->x_obj, &s_signal);
    x->x_outlet_right = outlet_new(&x->x_obj, &s_signal);

    x->x_outlet_streaming = outlet_new(&x->x_obj, &s_float);
    x->x_outlet_nbframes = outlet_new(&x->x_obj, &s_float);

    x->x_packet0 = -1;
    x->x_connectchild = 0;
    x->x_decodechild = 0;
    x->x_usethread = 1;
    x->x_priority = DEFAULT_PRIORITY;
    x->x_nbframes = 0;
    x->x_framerate = DEFAULT_FRAME_RATE;
    x->x_samplerate = 0;
    x->x_audiochannels = 0;
    x->x_cursec = 0;
    x->x_secondcount = 0;
    x->x_audio_resample_ctx = NULL;
    x->x_nbvideostreams = 0;
    x->x_audioin_position = 0;
    x->x_newpicture = 0;

    x->x_avcontext = av_mallocz(sizeof(AVFormatContext));
    if ( !x->x_avcontext )
    {
       post( "pdp_live~ : severe error : could not allocate video structures." );
       return NULL;
    }

    // activate codecs
    av_register_all();

    memset( &x->x_audio_buf[0], 0x0, 4*MAX_AUDIO_PACKET_SIZE*sizeof(short) );
    memset( &x->x_audio_in[0], 0x0, 4*MAX_AUDIO_PACKET_SIZE*sizeof(short) );

    return (void *)x;
}


#ifdef __cplusplus
extern "C"
{
#endif


void pdp_live_tilde_setup(void)
{
    post( pdp_live_version );
    pdp_live_class = class_new(gensym("pdp_live~"), (t_newmethod)pdp_live_new,
    	(t_method)pdp_live_free, sizeof(t_pdp_live), 0, A_NULL);

    class_addmethod(pdp_live_class, (t_method)pdp_live_dsp, gensym("dsp"), 0);
    class_addmethod(pdp_live_class, (t_method)pdp_live_connect, gensym("connect"), A_SYMBOL, A_NULL);
    class_addmethod(pdp_live_class, (t_method)pdp_live_disconnect, gensym("disconnect"), A_NULL);
    class_addmethod(pdp_live_class, (t_method)pdp_live_priority, gensym("priority"), A_FLOAT, A_NULL);
    class_addmethod(pdp_live_class, (t_method)pdp_live_audio, gensym("audio"), A_FLOAT, A_NULL);

}

#ifdef __cplusplus
}
#endif