File: streams.c

package info (click to toggle)
muroard 0.1.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 420 kB
  • ctags: 424
  • sloc: ansic: 3,645; sh: 505; makefile: 113
file content (693 lines) | stat: -rw-r--r-- 16,425 bytes parent folder | download | duplicates (4)
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
//streams.c:

/*
 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013
 *
 *  This file is part of �RoarD,
 *  a sound server daemon for using the RoarAudio protocol.
 *  See README for details.
 *
 *  This file is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  or (at your option) any later version as published by
 *  the Free Software Foundation.
 *
 *  RoarAudio 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 software; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "muroard.h"

int muroard_stream_init(void) {
 muroard_memzero(muroard_state_member(stream), sizeof(muroard_state_member(stream))); // unused = 0
 return 0;
}

int muroard_stream_free(void) {
 int i;
 int ret = 0;

 for (i = 0; i < MUROARD_MAX_STREAMS; i++) {
  if ( muroard_state_member(stream)[i].state != STREAM_STATE_UNUSED ) {
   if ( muroard_stream_delete(i) == -1 )
    ret = -1;
  }
 }

 return ret;
}

int muroard_stream_new(int client, int dir, struct muroard_audio_info * info) {
#if defined(MUROARD_FEATURE_UPSCALE_INPUT) || defined(MUROARD_FEATURE_DOWNSCALE_OUTPUT)
 int need_scale = 0;
#define _NEED_SCALE need_scale
#endif
#if defined(MUROARD_FEATURE_UPMIX_INPUT) || defined(MUROARD_FEATURE_DOWNMIX_OUTPUT)
 int need_mix   = 0;
#define _NEED_MIX need_mix
#endif
#ifdef MUROARD_FEATURE_CODECFILTER
 int need_cf    = 0;
#define _NEED_CF need_cf
#endif
 register struct muroard_stream * stream;
 int i;

 //fprintf(stderr, "%s:%i: dir=%i\n", __FILE__, __LINE__, dir);

#ifdef _NEED_SCALE
 if ( info->bits == 8 ) {
  need_scale = 1;
  switch (info->codec) {
   case MUROAR_CODEC_PCM_S_LE:
   case MUROAR_CODEC_PCM_S_BE:
   case MUROAR_CODEC_PCM_S_PDP:
     info->codec = MUROAR_CODEC_PCM_S;
    break;
#ifdef MUROARD_FEATURE_UNSIGNED_8BIT
   case MUROAR_CODEC_PCM_U_LE:
   case MUROAR_CODEC_PCM_U_BE:
   case MUROAR_CODEC_PCM_U_PDP:
     info->codec = MUROAR_CODEC_PCM_U;
    break;
#endif
  }
 } else if ( info->bits != 16 ) {
  return -1;
 }
#else
 if ( info->bits != 16 )
  return -1;
#endif

#ifdef _NEED_MIX
 if ( info->channels == 1 ) {
  need_mix = 1;
 } else if ( info->channels != muroard_state_member(sa_channels) ) {
  return -1;
 }
#else
 if ( info->channels != muroard_state_member(sa_channels) )
  return -1;
#endif

#ifdef _NEED_CF
 switch (info->codec) {
#ifdef MUROARD_FEATURE_CODECFILTER_AU
  case MUROAR_CODEC_AU:
#endif
#ifdef MUROARD_FEATURE_CODECFILTER_RIFF_WAVE
  case MUROAR_CODEC_RIFF_WAVE:
#endif
#ifdef MUROARD_FEATURE_CODECFILTER_OGG_VORBIS
  case MUROAR_CODEC_OGG_VORBIS:
#endif
    _NEED_CF = info->codec;
    info->codec = MUROAR_CODEC_PCM;
   break;
 }
#endif

#ifdef MUROARD_FEATURE_BYTE_SWAP
 if ( info->bits > 16 && info->codec != MUROAR_CODEC_PCM )
  return -1;

 switch (info->codec) {
  case MUROAR_CODEC_PCM_S_BE:
  case MUROAR_CODEC_PCM_S_LE:
  case MUROAR_CODEC_PCM_S_PDP:
#ifdef MUROARD_FEATURE_UNSIGNED_8BIT
  case MUROAR_CODEC_PCM_U_LE:
  case MUROAR_CODEC_PCM_U_BE:
  case MUROAR_CODEC_PCM_U_PDP:
#endif
   break;
  default:
    return -1;
   break;
 }
#else
 if ( info->codec != MUROAR_CODEC_PCM )
  return -1;
#endif

 if ( client_get_stream(client) != -1 )
  return -1;

 for (i = 0; i < MUROARD_MAX_STREAMS; i++) {
  if ( muroard_state_member(stream)[i].state == STREAM_STATE_UNUSED ) {
   stream = &(muroard_state_member(stream)[i]);
   muroard_memzero(stream, sizeof(*stream));
   stream->state    = STREAM_STATE_UNUSED;
   stream->iobuf    = NULL;

#ifdef _NEED_CF
   stream->cf_read  = NULL;
   stream->cf_close = NULL;

   switch (_NEED_CF) {
    case 0: break; // no need to do anything.
#ifdef MUROARD_FEATURE_CODECFILTER_AU
    case MUROAR_CODEC_AU:
      stream->cf_read = muroard_cf_au_read;
     break;
#endif
#ifdef MUROARD_FEATURE_CODECFILTER_RIFF_WAVE
    case MUROAR_CODEC_RIFF_WAVE:
      stream->cf_read = muroard_cf_riff_wave_read;
     break;
#endif
#ifdef MUROARD_FEATURE_CODECFILTER_OGG_VORBIS
    case MUROAR_CODEC_OGG_VORBIS:
      stream->cf_read  = muroard_cf_ogg_vorbis_read;
      stream->cf_close = muroard_cf_ogg_vorbis_close;
     break;
#endif
    default: return -1;
   }
   if ( _NEED_CF ) {
#ifdef _NEED_SCALE
    _NEED_SCALE = 1;
    info->bits = 8;
#endif
#ifdef _NEED_MIX
    _NEED_MIX = 1;
    info->channels = 1;
#endif
   }
#endif

   switch (dir) {
    case MUROAR_PLAY_WAVE:
      stream->datadir = STREAM_DATADIR_IN;
      if ( (stream->iobuf = muroard_malloc(muroard_state_member(abuffer_size))) == NULL ) {
       return -1;
      }
      muroard_memzero(stream->iobuf, muroard_state_member(abuffer_size)); // clean memory to avoid noise
     break;
#ifdef MUROARD_FEATURE_MONITOR
    case MUROAR_MONITOR_WAVE:
       stream->datadir = STREAM_DATADIR_OUT;
      // no neet to set up anything
     break;
#endif
    default:
      return -1;
     break;
   }

#ifdef _NEED_SCALE
   if ( _NEED_SCALE ) {
    switch (stream->datadir) {
#ifdef MUROARD_FEATURE_UPSCALE_INPUT
     case STREAM_DATADIR_IN: break;
#endif
#ifdef MUROARD_FEATURE_DOWNSCALE_OUTPUT
     case STREAM_DATADIR_OUT: break;
#endif
     default:
       if ( stream->iobuf != NULL )
        muroard_free(stream->iobuf);
       return -1;
    }
   }
#endif

#ifdef _NEED_MIX
   if ( _NEED_MIX ) {
    switch (stream->datadir) {
#ifdef MUROARD_FEATURE_UPMIX_INPUT
     case STREAM_DATADIR_IN: break;
#endif
#ifdef MUROARD_FEATURE_DOWNMIX_OUTPUT
     case STREAM_DATADIR_OUT: break;
#endif
     default:
       if ( stream->iobuf != NULL )
        muroard_free(stream->iobuf);
       return -1;
    }
   }
#endif

   //fprintf(stderr, "%s:%i: dir=%i\n", __FILE__, __LINE__, dir);

   stream->sock   = MUROARD_HANDLE_INVALID;
   stream->client = client;
   stream->dir    = dir;

   // stream volume:
#ifdef MUROARD_FEATURE_VOLUME_CONTROL
   stream->volume = 65535;
#endif

   memcpy(&(stream->info), info, sizeof(stream->info));

   if ( client_set_stream(client, i) == -1 ) {
    if ( stream->iobuf != NULL )
     muroard_free(stream->iobuf);
    return -1;
   }

   stream->state = STREAM_STATE_NEW;
   return i;
  }
 }

 return -1;
}

int muroard_stream_delete(int id) {
 register struct muroard_stream * stream;
 int state;
 int ret   = 0;

 if ( id >= MUROARD_MAX_STREAMS || id < 0 )
  return -1;

 stream = &(muroard_state_member(stream)[id]);
 state = stream->state;

 if ( state == STREAM_STATE_UNUSED  ||
      state == STREAM_STATE_CLOSING )
  return 0;

 stream->state = STREAM_STATE_CLOSING;

#ifdef MUROARD_FEATURE_CODECFILTER
 if ( stream->cf_close != NULL )
  stream->cf_close(id, stream);
#endif

 if ( state == STREAM_STATE_EXECED ) {
  if ( stream->client != -1 )
   if ( muroard_client_delete(stream->client) == -1 )
    ret = -1;
 } else {
  muroard_state_member(client)[stream->client].stream = -1;
 }

 if ( stream->sock != MUROARD_HANDLE_INVALID ) {
  muroard_network_close(stream->sock);
 }

 if ( stream->iobuf != NULL )
  muroard_free(stream->iobuf);

 stream->state = STREAM_STATE_UNUSED;

 return ret;
}

int muroard_stream_exec(int id) {
 muroard_handle_t sock;

 if ( (sock = muroard_client_exec(muroard_state_member(stream)[id].client)) == MUROARD_HANDLE_INVALID )
  return -1;

 muroard_state_member(stream)[id].sock  = sock;
 muroard_state_member(stream)[id].state = STREAM_STATE_EXECED;

 return 0;
}

#ifdef MUROARD_FEATURE_REREADWRITE
ssize_t muroard_stream_network_read(int id, void * buf, size_t len) {
 muroard_handle_t fh = muroard_stream_get_sock(id);
 ssize_t done = 0;
 ssize_t ret;

 if ( fh == MUROARD_HANDLE_INVALID || buf == NULL )
  return -1;

 while (len) {
  if ( (ret = muroard_network_read(fh, buf, len)) < 1 )
   return done;

  done += ret;
  buf   = (char *)buf + ret;
  len  -= ret;
 }

 return done;
}
ssize_t muroard_stream_network_write(int id, const void * buf, size_t len) {
 muroard_handle_t fh = muroard_stream_get_sock(id);
 ssize_t done = 0;
 ssize_t ret;

 if ( fh == MUROARD_HANDLE_INVALID || buf == NULL )
  return -1;

 while (len) {
  if ( (ret = muroard_network_write(fh, buf, len)) < 1 )
   return done;

  done += ret;
  buf   = (const char *)buf + ret;
  len  -= ret;
 }

 return done;
}
#endif

int muroard_stream_read(int id) {
 register struct muroard_stream * stream = &(muroard_state_member(stream)[id]);
 ssize_t len;
 size_t readsize = muroard_state_member(abuffer_size);

#ifdef MUROARD_FEATURE_UPSCALE_INPUT
 if ( stream->info.bits == 8 )
  readsize /= 2;
#endif

#ifdef MUROARD_FEATURE_UPMIX_INPUT
 if ( stream->info.channels == 1 && muroard_state_member(sa_channels) != 1 )
  readsize /= muroard_state_member(sa_channels);
#endif

#ifdef MUROARD_FEATURE_CODECFILTER
 if ( stream->cf_read != NULL ) {
  len = stream->cf_read(id, stream, stream->iobuf, readsize);
 } else {
#endif
  len = muroard_stream_network_read(id, stream->iobuf, readsize);
#ifdef MUROARD_FEATURE_CODECFILTER
 }
#endif

 if ( len == (ssize_t) readsize ) {
  // no op here.
 } else if ( len == 0 ) {
  return muroard_stream_delete(id);
 } else if ( len == -1 ) {
  muroard_stream_delete(id);
  return -1;
 } else {
  muroard_memzero(((void*)stream->iobuf)+len, muroard_state_member(abuffer_size) - len);
 }

#ifdef MUROARD_FEATURE_POSITION
 stream->pos += len / (stream->info.bits / 8);
#endif

#ifdef MUROARD_FEATURE_UPSCALE_INPUT
 if ( stream->info.bits == 8 ) {
  muroard_dsp_upscale(stream->iobuf, len);

  len *= 2;
 }
#endif

#ifdef MUROARD_FEATURE_BYTE_SWAP
 if ( muroard_dsp_need_swap_s(stream->info.codec) )
  muroard_dsp_swap16(stream->iobuf, len/2);
#endif

#ifdef MUROARD_FEATURE_UPMIX_INPUT
 if ( stream->info.channels == 1 && muroard_state_member(sa_channels) != 1 )
  muroard_dsp_upmix(stream->iobuf, len/2);
#endif

 return 0;
}

#ifdef MUROARD_FEATURE_MONITOR
int muroard_stream_write(int id, int16_t * buf) {
 register struct muroard_stream * stream = &(muroard_state_member(stream)[id]);
 ssize_t len;
 size_t  writesize = muroard_state_member(abuffer_size);
#if defined(MUROARD_FEATURE_DOWNSCALE_OUTPUT) || defined(MUROARD_FEATURE_DOWNMIX_OUTPUT) || defined(MUROARD_FEATURE_BYTE_SWAP)
 size_t   need_buf = 0;
#define _NEED_BUF need_buf
#endif

 if ( muroard_stream_get_sock(id) == MUROARD_HANDLE_INVALID )
  return -1;

#ifdef MUROARD_FEATURE_DOWNSCALE_OUTPUT
 if ( stream->info.bits == 8 ) {
  writesize /= 2;
  need_buf   = 1;
 }
#endif

#ifdef MUROARD_FEATURE_DOWNMIX_OUTPUT
 if ( stream->info.channels == 1 && muroard_state_member(sa_channels) != 1 ) {
  writesize /= muroard_state_member(sa_channels);
  need_buf   = 1;
 }
#endif

#ifdef MUROARD_FEATURE_BYTE_SWAP
 if ( muroard_dsp_need_swap_s(stream->info.codec) )
  need_buf   = 1;
#endif

#ifdef _NEED_BUF
 if ( _NEED_BUF ) {
  if ( stream->iobuf == NULL ) {
   muroard_state_member(stream)[id].iobuf = muroard_malloc(muroard_state_member(abuffer_size));
   if ( stream->iobuf == NULL )
    return -1;
  }

  memcpy(stream->iobuf, buf, muroard_state_member(abuffer_size));
  buf = muroard_state_member(stream)[id].iobuf;

#ifdef MUROARD_FEATURE_DOWNMIX_OUTPUT
  if ( stream->info.channels == 1 && muroard_state_member(sa_channels) != 1 ) {
   muroard_dsp_downmix(buf, muroard_state_member(abuffer_size)/(2*muroard_state_member(sa_channels)));
  }
#endif

#ifdef MUROARD_FEATURE_BYTE_SWAP
 if ( muroard_dsp_need_swap_s(stream->info.codec) )
  muroard_dsp_swap16(buf, (muroard_state_member(abuffer_size)*stream->info.channels)/(2*muroard_state_member(sa_channels)));
#endif

#ifdef MUROARD_FEATURE_DOWNSCALE_OUTPUT
  if ( stream->info.bits == 8 ) {
   muroard_dsp_downscale(buf, writesize);
  }
#endif
 }
#endif

 len = muroard_stream_network_write(id, buf, writesize);

 if ( len < 1 ) {
  muroard_stream_delete(id);
  return -1;
 }

#ifdef MUROARD_FEATURE_POSITION
 stream->pos += len / (stream->info.bits / 8);
#endif

 return 0;
}
#endif

#ifdef MUROARD_FEATURE_CMD_ATTACH
int muroard_stream_move_client(int id, int client) {
 int old_client;

 if ( muroard_state_member(stream)[id].state == STREAM_STATE_UNUSED )
  return -1;

 // check if the target client will accept the stream.
 if ( client_set_stream_check(client, id) == -1 )
  return -1;

 old_client = muroard_state_member(stream)[id].client;

 // if we are execed we need to close the client, too.
 // we use a little trick:
 // we tell the stream it is closing and close the client.
 // after the client has been closed we reset the stream to NEW.
 if ( muroard_state_member(stream)[id].state == STREAM_STATE_EXECED ) {
  muroard_state_member(stream)[id].state = STREAM_STATE_CLOSING;
  if ( muroard_client_delete(old_client) == -1 ) {
   muroard_state_member(stream)[id].state = STREAM_STATE_NEW;
   muroard_stream_delete(id);
   return -1;
  }
  old_client = -1;
  muroard_state_member(stream)[id].state = STREAM_STATE_NEW;
 }

 if ( old_client != -1 )
  muroard_state_member(client)[old_client].stream = -1;

 // now we have a free client we need to attach to the client like muroard_stream_new() does:
#ifdef MUROARD_FEATURE_INTERNAL_CLIENT
 if ( muroard_state_member(client)[client].state == CLIENT_STATE_INTERNAL ) {
  muroard_state_member(stream)[id].client = -1;
  return 0;
 }
#endif

 muroard_state_member(stream)[id].client = client;
 client_set_stream(client, id); // errors are checked above.

 return 0;
}
#endif

#ifdef MUROARD_FEATURE_CODECFILTER
int muroard_stream_cksupport(struct muroard_audio_info * info, int datadir) {
 if ( info->bits == 8 ) {
  switch (info->codec) {
   case MUROAR_CODEC_PCM_S_LE:
   case MUROAR_CODEC_PCM_S_BE:
   case MUROAR_CODEC_PCM_S_PDP:
#ifdef MUROARD_FEATURE_UNSIGNED_8BIT
   case MUROAR_CODEC_PCM_U_LE:
   case MUROAR_CODEC_PCM_U_BE:
   case MUROAR_CODEC_PCM_U_PDP:
#endif
    break;
   default:
     return 0;
    break;
  }
 } else if ( info->bits == 16 ) {
  switch (info->codec) {
#if MUROAR_CODEC_PCM_S_LE == MUROAR_CODEC_PCM_S
#ifdef MUROARD_FEATURE_BYTE_SWAP
   case MUROAR_CODEC_PCM_S_BE:
#endif
   case MUROAR_CODEC_PCM_S_LE:
   case MUROAR_CODEC_PCM_S_PDP:
    break;
#else
#ifdef MUROARD_FEATURE_BYTE_SWAP
   case MUROAR_CODEC_PCM_S_LE:
   case MUROAR_CODEC_PCM_S_PDP:
#endif
   case MUROAR_CODEC_PCM_S_BE:
    break;
#endif
#ifdef MUROARD_FEATURE_UNSIGNED_16BIT
#if MUROAR_CODEC_PCM_S_LE == MUROAR_CODEC_PCM_S
#ifdef MUROARD_FEATURE_BYTE_SWAP
   case MUROAR_CODEC_PCM_U_BE:
#endif
   case MUROAR_CODEC_PCM_U_LE:
   case MUROAR_CODEC_PCM_U_PDP:
    break;
#else
#ifdef MUROARD_FEATURE_BYTE_SWAP
   case MUROAR_CODEC_PCM_U_LE:
   case MUROAR_CODEC_PCM_U_PDP:
#endif
   case MUROAR_CODEC_PCM_U_BE:
    break;
#endif
#endif
   default:
     return 0;
    break;
  }
 } else {
  return 0;
 }

 if ( info->bits != 16 ) {
  if ( info->bits == 8 ) {
   if ( datadir == STREAM_DATADIR_IN ) {
#ifndef MUROARD_FEATURE_UPSCALE_INPUT
    return 0;
#endif
   } else if ( datadir == STREAM_DATADIR_OUT ) {
#ifndef MUROARD_FEATURE_DOWNSCALE_OUTPUT
    return 0;
#endif
   } else {
    return 0;
   }
  }
 }

 if ( info->channels != muroard_state_member(sa_channels) ) {
  if ( info->channels == 1 ) {
   if ( datadir == STREAM_DATADIR_IN ) {
#ifndef MUROARD_FEATURE_UPMIX_INPUT
    return 0;
#endif
   } else if ( datadir == STREAM_DATADIR_OUT ) {
#ifndef MUROARD_FEATURE_DOWNMIX_OUTPUT
    return 0;
#endif
   } else {
    return 0;
   }
  } else {
   return 0;
  } 
 }

 return 1;
}
#endif

#ifdef MUROARD_FEATURE_SOURCES
int muroard_stream_new_source(const char * filename, int dir, struct muroard_audio_info * info) {
 int stream;
#ifndef __WIN32
 int flags = O_NONBLOCK;
#else
 int flags = 0;
#endif
 int fh;

 switch (dir) {
  case STREAM_DATADIR_IN:
    flags |= O_RDONLY;
   break;
  case STREAM_DATADIR_OUT:
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
    flags |= O_WRONLY|O_CREAT|O_LARGEFILE|O_APPEND;
   break;
  default:
    return -1;
   break;
 }

 fh = open(filename, flags, 0666);
 if ( fh == -1 )
  return -1;

 stream = muroard_stream_new(0, dir, info);
 if ( stream == -1 ) {
  close(fh);
  return -1;
 }

 if ( muroard_stream_move_client(stream, 0) == -1 ) {
  muroard_stream_delete(stream);
  return -1;
 }

 if ( muroard_stream_set_sock(stream, fh) == -1 ) {
  muroard_stream_delete(stream);
  return -1;
 }

 return stream;
}
#endif

//ll