/camera/imageviewer

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/imageviewer
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
#include <stdio.h>
#include "ruby.h"

#include "ds_log.h"
#include "cl.h"

#define BUFFER_FRAMES 16

static VALUE module_object;
static VALUE class_object;

struct TCameraLinkReader {
    CameraLink cl;

    VALUE str;
    
    int run_flag;
    int width, height;
    
    pid_t pid;
    int ready;
    int frame_id;
    void *data;
};

typedef struct TCameraLinkReader CameraLinkReader;

static void reader_mark(CameraLink *ctx) {
}

static VALUE reader_allocate(VALUE klass) {
    CameraLink *ctx = malloc(sizeof(CameraLinkReader));
    if (ctx) {
	memset(ctx, 0, sizeof(CameraLinkReader));
    } else {
        rb_raise(rb_eRuntimeError, "Allocation of CameraLink context is failed");
    }
    
    return Data_Wrap_Struct(klass, reader_mark, cl_destroy, ctx);
}


/*
static reader_callback(void *callback, int frame_id, void *data) {
    rb_funcall((VALUE)callback, rb_intern("call"), 2, INT2NUM(frame_id), Qnil);
}
*/

static reader_safe_callback(CameraLink *ctx, int frame_id, void *data) {
    rb_yield_values(2, INT2NUM(frame_id), rb_str_new(data, ctx->width * ctx->height));
}

static reader_callback(CameraLink *ctx, int frame_id, void *data) {
    struct RString *str = (struct RString*)((CameraLinkReader*)ctx)->str;

    char *ptr = str->ptr;
    str->ptr = data;
    rb_yield_values(2, INT2NUM(frame_id), (VALUE)str);
    str->ptr = ptr;

/*
	// This is a bit safer
    memcpy(str->ptr, data, ctx->width * ctx->height);
    rb_yield_values(2, INT2NUM(frame_id), str);
*/

    return 0;
}

static reader_thread_callback(CameraLinkReader *ctx, int frame_id, void *data) {
    while ((ctx->ready)&&(ctx->run_flag)) usleep(100);
    
    ctx->frame_id = frame_id;
    ctx->data = data;
    ctx->ready = 1;
}


static VALUE reader_init(VALUE self) {
    int err;
    CameraLink *ctx;

    Data_Get_Struct (self, CameraLink, ctx);

    err = cl_init(ctx);
    if (err) rb_raise(rb_eRuntimeError, "Initialization of CameraLink context is failed");

//    cl_register_frame_callback(ctx, &reader_callback, (void*)callback);

    return Qnil;
}

static VALUE reader_open(VALUE self, VALUE vWidth, VALUE vHeight) {
    CameraLinkReader *ctx;
    int width = NUM2INT(vWidth);
    int height = NUM2INT(vHeight);
    char tmp[width*height];

    Data_Get_Struct (self, CameraLinkReader, ctx);

    cl_open((CameraLink*)ctx, width, height, 0);
    ctx->str = rb_str_new(tmp, width * height);

    return Qnil;
}

static VALUE reader_set_max_resolution(VALUE self, VALUE vWidth, VALUE vHeight) {
    CameraLink *ctx;

    Data_Get_Struct (self, CameraLink, ctx);
    cl_set_max_resolution((CameraLink*)ctx, NUM2INT(vWidth), NUM2INT(vHeight));

    return Qnil;
}

static VALUE reader_set_resolution(VALUE self, VALUE vWidth, VALUE vHeight) {
    CameraLink *ctx;

    Data_Get_Struct (self, CameraLink, ctx);
    cl_set_resolution((CameraLink*)ctx, NUM2INT(vWidth), NUM2INT(vHeight));

    return Qnil;
}

static VALUE reader_close(VALUE self) {
    CameraLink *ctx;

    Data_Get_Struct (self, CameraLink, ctx);
    cl_close(ctx);

    return Qnil;
}

static int *reader_run_thread(CameraLinkReader *ctx) {
    int err;
    err = cl_run_frames_continuous((CameraLink*)ctx, &ctx->run_flag, reader_thread_callback, ctx);
    return NULL;
}

static VALUE reader_sleep(VALUE delay) {
    return rb_funcall(Qnil, rb_intern("sleep"), 1, delay);
}

static VALUE reader_threaded_run(VALUE self, VALUE run_flag) {
    int err;
    void *res;
    VALUE delay;
    pthread_t thr;

    CameraLinkReader *ctx;

    rb_need_block();
    
    Data_Get_Struct (self, CameraLinkReader, ctx);

    ctx->pid = getpid();
    ctx->run_flag = 1;

    err = pthread_create(&thr, NULL, (void*(*)(void*))reader_run_thread,(void*)ctx);
    if (err) rb_raise(rb_eRuntimeError, "Thread scheduling is failed");
    
    delay = rb_float_new(0.001);	// better implement over socket, signal and exception are not really working

    while (ctx->run_flag) {
	if (ctx->ready) {
	    //printf("In: %i %p\n", ctx->frame_id, ctx->data);
	    reader_callback((CameraLink*)ctx, ctx->frame_id, ctx->data);
	    ctx->ready = 0;
	}
	rb_funcall(Qnil, rb_intern("sleep"), 1, delay);
    }
    
    pthread_join(thr, &res);

    return Qnil;
}

static VALUE reader_run(VALUE self, VALUE run_flag) {
    int err;

    CameraLinkReader *ctx;

    rb_need_block();
    
    Data_Get_Struct (self, CameraLinkReader, ctx);
    
    ctx->run_flag = 1;

    err = cl_run_frames_continuous((CameraLink*)ctx, &ctx->run_flag, reader_callback, ctx);
    if (err) rb_raise(rb_eRuntimeError, "Run is failed");

    return Qnil;
}

static VALUE reader_safe_run(VALUE self, VALUE run_flag) {
    int err;

    CameraLinkReader *ctx;

    rb_need_block();
    
    Data_Get_Struct (self, CameraLinkReader, ctx);

    ctx->run_flag = 1;

    err = cl_run_frames_continuous((CameraLink*)ctx, &ctx->run_flag, reader_safe_callback, ctx);
    if (err) rb_raise(rb_eRuntimeError, "Run is failed");

    return Qnil;
}



static VALUE reader_stop(VALUE self, VALUE run_flag) {
    int err;
    int rflag = 0;

    CameraLinkReader *ctx;

    Data_Get_Struct (self, CameraLinkReader, ctx);

    ctx->run_flag = 0;
    
    return Qnil;
}


void ds_raise_ruby_exception(int err, const char *msg) {
    rb_raise(rb_eRuntimeError, msg);
}

void Init_cameralink() {

    ds_log_configure_exception_handler(G_LOG_LEVEL_CRITICAL, ds_raise_ruby_exception, 0);

    module_object = rb_define_module("CameraLink");
    class_object = rb_define_class_under(module_object, "Reader", rb_cObject);
    rb_define_alloc_func(class_object, reader_allocate);
    rb_define_method(class_object, "initialize", reader_init, 0);
    rb_define_method(class_object, "open", reader_open, 2);
    rb_define_method(class_object, "close", reader_close, 0);
    rb_define_method(class_object, "run", reader_run, 1);
    rb_define_method(class_object, "safe_run", reader_safe_run, 1);
    rb_define_method(class_object, "threaded_run", reader_threaded_run, 1);
    rb_define_method(class_object, "stop", reader_stop, 0);
    rb_define_method(class_object, "set_max_resolution", reader_set_max_resolution, 2);
    rb_define_method(class_object, "set_resolution", reader_set_resolution, 2);
}