aboutsummaryrefslogtreecommitdiff
path: root/modules/generic/pdp_rawout.c
blob: e001b37784aab4687188402b352f4278c3d7b1d5 (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
/*
 *   Pure Data Packet module. Raw packet output.
 *   Copyright (c) by Tom Schouten <tom@zwizwa.be>
 *
 *   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 is the most straightforward way to get data out of pdp.
  The internals follow the simple reader/writer pattern
  * writer: runs in pd thread, accepts packets from inlet and stores in queue
  * reader: runs in own thread, reads packets from queue and writes to pipe

  Since there is no communication from reader to writer, we need a watchdog
  timer to check the status of the writer. Mainly to close when necessary.

  To enable audio recording, pdp_rawout will also produce interleaved 16bit
  audio. You will need to instantiate it with [pdp_rawout~ nbchans]



*/

/* TODO:

   make audio buffer smaller (128 bytes writes is too heavy)

*/



#include <string.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include "pdp_pd.h"
#include "pdp_debug.h"
#include "pdp_list.h"
#include "pdp_comm.h"
#include "pdp_post.h"
#include "pdp_packet.h"


#define D if (1)
#define DEF_QUEUESIZE 100
#define PIPE_BLOCKSIZE 4096
#define POLLTIME 20


/* raw input from a unix pipe */

typedef struct rawout_struct
{
    /* pd */
    t_object x_obj;
    t_float  x_f;

    //t_outlet *x_outlet;
    t_outlet *x_sync_outlet;

    /* comm */
    t_pdp_list *x_queue;   // packet queue
    t_clock    *x_clock;   // watchdog timer
    t_float     x_deltime; // watchdog period
    int         x_verbose;
    int         x_packet;    // dsp fillup state
    int         x_countdown; // amount of packet filled up
    short int * x_needle;    // start writing here

    /* thread */
    pthread_mutex_t x_mut;
    pthread_attr_t x_attr;
    pthread_t x_thread;

    /* sync */
    int x_giveup;  // 1-> terminate writer thread
    int x_active;  // 1-> writer thread is launched
    int x_done;    // 1-> writer thread has exited

    /* config */
    t_symbol      *x_pipe;
    int           x_chans;            // nb audio channels
    t_pdp_symbol *x_tmptype;
    int           x_max_queuesize;    // buffer size ( < 0 = infty )

} t_rawout;


static inline void lock(t_rawout *x){pthread_mutex_lock(&x->x_mut);}
static inline void unlock(t_rawout *x){pthread_mutex_unlock(&x->x_mut);}

static void rawout_close(t_rawout *x);

/* READER THREAD: reads from queue, writes to pipe */

static void *rawout_thread(void *y)
{
    int pipefd;
    int packet = -1;
    t_rawout *x = (t_rawout *)y;
    int period_sec;
    int period_usec;
    sigset_t _sigvec; /* signal handling  */

    char me[1024];
    sprintf(me, "pdp_rawout: %s", x->x_pipe->s_name);

    /* ignore pipe signal */
    sigemptyset(&_sigvec);
    sigaddset(&_sigvec,SIGPIPE);
    pthread_sigmask(SIG_BLOCK, &_sigvec, 0);

    //D pdp_post("pipe: %s", x->x_pipe->s_name);
    //D pdp_post("type: %s", x->x_type->s_name);


    /* open pipe */
    if (-1 == (pipefd = open(x->x_pipe->s_name, O_WRONLY|O_NONBLOCK|O_APPEND))){
      if (-1 == (pipefd = open(x->x_pipe->s_name, O_WRONLY|O_CREAT, 0600))){
	    perror(me);
	    goto exit;
	}
    }

    pdp_post("pdp_rawout: opened %s", x->x_pipe->s_name);


    /* main loop (packets) */
    while(1){
	void *data = 0;
	int left = -1;

	/* try again if queue is empty */
	if (!x->x_queue->elements){
	    /* check if we need to stop */
	    if (x->x_giveup){
		goto close;
	    }
	    else {
		usleep(1000.0f); // sleep before polling again
		continue;
	    }
	}
	/* get packet from queue */
	lock(x);
	packet = pdp_list_pop(x->x_queue).w_packet;
	unlock(x);

	/* send packet */
	//t_pdp *h = pdp_packet_header(packet);
	//fprintf(stderr, "users %d\n", h->users);
	
	data = pdp_packet_data(packet);
	left = pdp_packet_data_size(packet);
	//int i; for (i = 0; i<left/2; i++) fprintf(stderr, "%06x ", ((short int *)data)[i]); fprintf(stderr, "\n");


	/* inner loop: pipe reads */
	while(left){

	    fd_set outset;
	    struct timeval tv = {0,10000};

	    /* check if we need to stop */
	    if (x->x_giveup){
		pdp_packet_mark_unused(packet);
		goto close;
	    }

	    /* select, with timeout */
	    FD_ZERO(&outset);
	    FD_SET(pipefd, &outset);
	    if (-1 == select(pipefd+1, NULL, &outset, NULL, &tv)){
		pdp_post("select error");
		goto close;
	    }

	    /* if ready, read, else retry */
	    if (FD_ISSET(pipefd, &outset)){

		int bytes = write(pipefd, data, left);
		/* handle errors */
		if (bytes <= 0){
		    perror(me);
		    if (errno != EAGAIN) goto close;
		}
		/* or update pointers */
		else{
		    data += bytes;
		    left -= bytes;
		    //pdp_post("left %d", left);
		}
	    }
	    else {
		//pdp_post("retrying write");
	    }
	}
		   
	/* discard packet */
	pdp_packet_mark_unused(packet);

    
    }

  close:
    /* close pipe */
    close(pipefd);
	
	
  exit:
    x->x_done = 1;
    return 0;
}


/* DSP INPUT */
#define DSP_ARG(type, name, source) type name = (type)source

static t_int *rawout_perform(t_int *w);
static void rawout_dsp(t_rawout *x, t_signal **sp){
    int nargs = 2 + x->x_chans;
    t_int args[nargs];
    args[0] = (int)x;
    args[1] = (int)sp[0]->s_n;
    float **in = (float **)(args+2);
    int i;
    for (i=0; i<x->x_chans; i++) in[i] = sp[i]->s_vec;
    dsp_addv(rawout_perform, nargs, args);
}

static t_int *rawout_perform(t_int *w)
{
    DSP_ARG(t_rawout*,  x,     w[1]);
    DSP_ARG(t_int,      n,     w[2]);
    DSP_ARG(t_float**,  in,    &w[3]);

    short int *out;
    int i,c,k;
    

    if (x->x_queue->elements >= x->x_max_queuesize){
	// drop
	if (x->x_verbose && x->x_active) pdp_post_n(".");
    }
    else {

	// create packet
	if (x->x_countdown) {
	    out = x->x_needle;
	}
	else {
	    int p = pdp_factory_newpacket(x->x_tmptype);
	    pdp_packet_mark_unused(x->x_packet);
	    // if (-1 == p) pdp_post("pdp_rawout~: can't create packet");
	    x->x_needle = out = (short int *)pdp_packet_data(p);
	    x->x_packet = p;
	    x->x_countdown = pdp_packet_data_size(p) / 2;
	}

	//pdp_post("data size = %d bytes", pdp_packet_data_size(p));

	//memset(out, 0, pdp_packet_data_size(p));


	// convert & interleave
	for (k=0,i=0; i<n; i++){
	    for (c=0; c<x->x_chans; c++,k++){
		float val = (in[c])[i];
		val *= (float)((1<<15)-1);
		out[k] = (short int)(val);
		//out[k] = 0x1234;
		//fprintf(stderr, "(%d,%d,%d) %d\n", c, i, k, (int)out[k]);
	    }
	}

	x->x_needle += k;
	x->x_countdown -= k;
	if (!x->x_countdown){
	    // transfer
	    lock(x);
	    pdp_list_add_back(x->x_queue, a_packet, (t_pdp_word)x->x_packet);
	    x->x_packet = -1;
	    unlock(x);
	}
    }

    return w+3+x->x_chans;
}


/* PACKET INPUT */


static void pdp_in(t_rawout *x, t_symbol *s, t_float f)
{
    /* save packet to pdp queue, if size is smaller than maxsize */
    if (s == S_REGISTER_RO){
	if (x->x_queue->elements < x->x_max_queuesize){
	    int p = (int)f;
	    p = pdp_packet_copy_ro(p);
	    if (p != -1){
		lock(x);
		pdp_list_add_back(x->x_queue, a_packet, (t_pdp_word)p);
		unlock(x);
	    }
	}
	else {
	    //pdp_post("pdp_rawout: dropping packet: (queue full)", x->x_max_queuesize);
	    if (x->x_active && x->x_verbose) pdp_post_n(".");
	}
	    
    }

    /* check if thread is done */
    if (x->x_done) rawout_close(x);

}

/* CONTROL */

//static void rawout_type(t_rawout *x, t_symbol *type)
//{
    //x->x_type = pdp_gensym(type->s_name);
//}

static void clear_queue(t_rawout *x);
static void rawout_open(t_rawout *x, t_symbol *spipe)
{
    /* save pipe name if not empty */
    if (spipe->s_name[0]) {x->x_pipe = spipe;}

    if (x->x_active) {
	pdp_post("already open");
	return;
    }
    /* start thread */
    x->x_giveup = 0;
    x->x_done = 0;
    clear_queue(x);
    pthread_create(&x->x_thread, &x->x_attr, rawout_thread , x);
    x->x_active = 1;
}

static void rawout_stopthread(t_rawout *x){
    if (!x->x_active) return;

    /* stop thread: set giveup + wait */
    x->x_giveup = 1;
    pthread_join(x->x_thread, NULL);
    x->x_active = 0;
    clear_queue(x);
}

static void rawout_close(t_rawout *x)
{
    if (!x->x_active) return;
    rawout_stopthread(x);

    /* notify */
    outlet_bang(x->x_sync_outlet);
    pdp_post("pdp_rawout: closed %s", x->x_pipe->s_name);
    
}

static void rawout_verbose(t_rawout *x, t_float fverbose){
    x->x_verbose = (int)fverbose;
}
static void rawout_tick(t_rawout *x){
    if (x->x_done) rawout_close(x);
    clock_delay(x->x_clock, x->x_deltime);
}

static void clear_queue(t_rawout *x){
    lock(x);
    while(x->x_queue->elements){
	pdp_packet_mark_unused(pdp_list_pop(x->x_queue).w_packet);
    }
    unlock(x);
}

#define MAXINT (0x80 << ((sizeof(int)-1)*8))
static void rawout_queuesize(t_rawout *x, t_float fbufsize){
    int bufsize = (int)fbufsize;
    if (fbufsize < 1) fbufsize = MAXINT;
    x->x_max_queuesize = bufsize;
}

static void rawout_free(t_rawout *x)
{
    // terminate thread
    rawout_stopthread(x);
    
    // cleanup
    clock_unset(x->x_clock);
    clear_queue(x);
    pdp_tree_free(x->x_queue);
    pdp_packet_mark_unused(x->x_packet);
}

t_class *rawout_class;
t_class *rawout_dsp_class;

// shared stuff
static void rawout_init(t_rawout *x, t_symbol *spipe){
    //x->x_outlet = outlet_new(&x->x_obj, &s_anything);
    x->x_sync_outlet = outlet_new(&x->x_obj, &s_anything);
    x->x_queue = pdp_list_new(0);
    x->x_active = 0;
    x->x_giveup = 0;
    x->x_done = 0;
    x->x_packet = -1;
    x->x_countdown = 0;
    x->x_max_queuesize = DEF_QUEUESIZE;
    //x->x_type = pdp_gensym("image/YCrCb/320x240"); //default
    x->x_pipe = gensym("/tmp/pdpraw"); // default
    x->x_deltime = POLLTIME;
    x->x_clock = clock_new(x, (t_method)rawout_tick);
    pthread_attr_init(&x->x_attr);
    pthread_mutex_init(&x->x_mut, NULL);

    /* args */
    //rawout_type(x, type);
    if (spipe && spipe->s_name[0]) x->x_pipe = spipe; 
    rawout_tick(x);
    rawout_verbose(x,0);
}

// [pdp_rawout]
static void *rawout_new(t_symbol *spipe /* , t_symbol *type */)
{
    t_rawout *x;
    /* allocate & init */
    x = (t_rawout *)pd_new(rawout_class);
    rawout_init(x, spipe);
    return (void *)x;

}

// [pdp_rawout~]


// HUH??? why do i get the symbol first, then the float????
// see http://lists.puredata.info/pipermail/pd-dev/2003-09/001618.html
//static void *rawout_dsp_new(t_float fchans, t_symbol *spipe){
static void *rawout_dsp_new(t_symbol *spipe, t_float fchans){
    int chans = (int)fchans;
    if (chans < 1)  chans = 1;
    if (chans > 64) return 0; // this is just a safety measure

    t_rawout *x = (t_rawout *)pd_new(rawout_dsp_class);
    rawout_init(x, spipe);

    // hack: temp packet
    char temp_packet[1024];
    sprintf(temp_packet, "image/grey/256x%d", 8 * chans);
    pdp_post("pdp_rawout: using fake packet %s", temp_packet);
    x->x_tmptype = pdp_gensym(temp_packet);

    // create audio inlets
    x->x_chans = chans;
    while (--chans) inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("signal"), gensym("signal"));
    
    return (void *)x;
}



#ifdef __cplusplus
extern "C"
{
#endif



#define COMMON(base_class)\
    class_addmethod(base_class, (t_method)rawout_open, gensym("open"), A_DEFSYMBOL, A_NULL);\
    class_addmethod(base_class, (t_method)rawout_close, gensym("close"), A_NULL);\
    class_addmethod(base_class, (t_method)rawout_verbose, gensym("verbose"), A_FLOAT, A_NULL);\
    class_addmethod(base_class, (t_method)rawout_queuesize, gensym("bufsize"), A_FLOAT, A_NULL);

void pdp_rawout_setup(void){

    int i;

    /* PACKETS */

    /* create a standard pd class: [pdp_rawout pipe type] */
    rawout_class = class_new(gensym("pdp_rawout"), (t_newmethod)rawout_new,
   	(t_method)rawout_free, sizeof(t_rawout), 0, A_DEFSYMBOL, A_NULL);

    /* add global message handler */
    class_addmethod(rawout_class, (t_method)pdp_in, gensym("pdp"), A_SYMBOL, A_FLOAT, A_NULL);
    COMMON(rawout_class);

    /* DSP */

    /* create a standard pd class: [pdp_rawout pipe type] */
    rawout_dsp_class = class_new(gensym("pdp_rawout~"), (t_newmethod)rawout_dsp_new,
   	(t_method)rawout_free, sizeof(t_rawout), 0, A_DEFFLOAT, A_DEFSYMBOL, A_NULL);

    /* add signal input */
    CLASS_MAINSIGNALIN(rawout_dsp_class, t_rawout, x_f); 
    class_addmethod(rawout_dsp_class, (t_method)rawout_dsp, gensym("dsp"), 0);   
    COMMON(rawout_dsp_class);

}

    





#ifdef __cplusplus
}
#endif