aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ecore_audio/ecore_audio_obj_out.c
blob: 0414d668b557ea0cc262cde274189a2b26f5e051 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_FEATURES_H
#include <features.h>
#endif
#include <ctype.h>
#include <errno.h>

#include "ecore_audio_private.h"

#define MY_CLASS ECORE_AUDIO_OUT_CLASS
#define MY_CLASS_NAME "Ecore_Audio_Out"

static Eina_Bool _write_cb(void *data)
{
  Eo *eo_obj = data;
  Eo *in;

  Ecore_Audio_Output *out_obj = eo_data_scope_get(eo_obj, ECORE_AUDIO_OUT_CLASS);
  Ecore_Audio_Object *ea_obj = eo_data_scope_get(eo_obj, ECORE_AUDIO_CLASS);

  ssize_t written, bread = 0;
  float buf[1024];

  if (!ea_obj->vio || !ea_obj->vio->vio->write)
    return EINA_FALSE;

  /* FIXME: Multiple inputs */
  in = eina_list_data_get(out_obj->inputs);

  eo_do(in, bread = ecore_audio_obj_in_read(buf, 4*1024));

  if (bread == 0) {
      ea_obj->paused = EINA_TRUE;
      out_obj->write_idler = NULL;
      return EINA_FALSE;
  }
  written = ea_obj->vio->vio->write(ea_obj->vio->data, eo_obj, buf, bread);

  if (written != bread)
    ERR("Short write");

  return EINA_TRUE;
}

EOLIAN static Eina_Bool
_ecore_audio_out_input_attach(Eo *eo_obj, Ecore_Audio_Output *obj, Eo *input)
{
  Ecore_Audio_Input *in;

  Ecore_Audio_Object *ea_obj = eo_data_scope_get(eo_obj, ECORE_AUDIO_CLASS);

  in = eo_data_scope_get(input, ECORE_AUDIO_IN_CLASS);

  if (in->output == eo_obj)
    return EINA_FALSE;

  if (in->output) eo_do(in->output, ecore_audio_obj_out_input_detach(input));
  in->output = eo_obj;

  /* TODO: Send event */

  obj->inputs = eina_list_append(obj->inputs, input);

  if (obj->need_writer &&
     ea_obj->vio && ea_obj->vio->vio->write)
    obj->write_idler = ecore_idler_add(_write_cb, eo_obj);


  return EINA_TRUE;
}

EOLIAN static Eina_Bool
_ecore_audio_out_input_detach(Eo *eo_obj, Ecore_Audio_Output *obj, Eo *input)
{
  Ecore_Audio_Input *in;

  in = eo_data_scope_get(input, ECORE_AUDIO_IN_CLASS);

  if (in->output != eo_obj)
    return EINA_FALSE;

  in->output = NULL;

  /* TODO: Check type is input
   * Get private data
   * Send event */

  obj->inputs = eina_list_remove(obj->inputs, input);

  return EINA_TRUE;
}

EOLIAN static Eina_List *
_ecore_audio_out_inputs_get(Eo *eo_obj EINA_UNUSED, Ecore_Audio_Output *obj)
{
   return obj->inputs;
}

static void _free_vio(Ecore_Audio_Object *ea_obj)
{
  if (ea_obj->vio->free_func)
    ea_obj->vio->free_func(ea_obj->vio->data);

  free(ea_obj->vio);
  ea_obj->vio = NULL;
}

EOLIAN static void
_ecore_audio_out_ecore_audio_vio_set(Eo *eo_obj, Ecore_Audio_Output *_pd EINA_UNUSED, Ecore_Audio_Vio *vio, void *data, eo_key_data_free_func free_func)
{
  Ecore_Audio_Object *ea_obj = eo_data_scope_get(eo_obj, ECORE_AUDIO_CLASS);

  if (ea_obj->vio)
    _free_vio(ea_obj);

  if (!vio)
    return;

  ea_obj->vio = calloc(1, sizeof(Ecore_Audio_Vio_Internal));
  ea_obj->vio->vio = vio;
  ea_obj->vio->data = data;
  ea_obj->vio->free_func = free_func;
}

EOLIAN static Eo *
_ecore_audio_out_eo_base_constructor(Eo *eo_obj, Ecore_Audio_Output *obj)
{
  eo_obj = eo_do_super_ret(eo_obj, MY_CLASS, eo_obj, eo_constructor());

  obj->need_writer = EINA_TRUE;

  return eo_obj;
}

EOLIAN static void
_ecore_audio_out_eo_base_destructor(Eo *eo_obj, Ecore_Audio_Output *obj)
{
  Eina_List *cur, *tmp;
  Eo *in;

  EINA_LIST_FOREACH_SAFE(obj->inputs, cur, tmp, in) {
      eo_do(eo_obj, ecore_audio_obj_out_input_detach(in));
  }

  eo_do_super(eo_obj, MY_CLASS, eo_destructor());
}

#include "ecore_audio_out.eo.c"