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
#include "unit_video.h"

#include "gate/io/videosources.h"
#include "gate/files.h"
#include "gate/results.h"
#include "gate/comparers.h"
#include "gate/threading.h"
#include "gate/graphics/imageformats.h"

#if !defined(GATE_SYS_DOS)


/****************
*  listsources  *
****************/

static gate_result_t unit_video_listsources_callback(gate_video_source_t* source, void* param)
{
    gate_result_t result;
    gate_video_format_t format_buffer[64];
    gate_size_t formats_count = sizeof(format_buffer) / sizeof(format_buffer[0]);
    gate_console_t* con = (gate_console_t*)param;
    gate_stream_t* stream = (gate_stream_t*)con;
    gate_size_t index;
    gate_video_format_t const* ptr_format;

    char const* id = gate_video_source_get_id(source);
    char const* name = gate_video_source_get_name(source);
    formats_count = gate_video_source_get_supported_formats(source, format_buffer, formats_count);

    result = gate_stream_print(
        stream,
        GP_CSTR, "ID=", GP_CSTR, id, GP_NEWLINE,
        GP_CSTR, "\tName:\t", GP_CSTR, name, GP_NEWLINE,
        GP_CSTR, "\tSupported Formats:", GP_NEWLINE,
        GP_END
    );
    for (index = 0; index != formats_count; ++index)
    {
        ptr_format = &format_buffer[index];
        result = gate_stream_print(
            stream,
            GP_CSTR, "\t\t#", GP_UI32, ((gate_uint32_t)index), GP_NEWLINE,
            GP_CSTR, "\t\t\tEncoding:\t", GP_UI32, ptr_format->encoding_id, GP_NEWLINE,
            GP_CSTR, "\t\t\tCompression:\t", GP_UI32, ptr_format->compression, GP_NEWLINE,
            GP_CSTR, "\t\t\tFrame resolution:\t",
            GP_UI16, ptr_format->width, GP_CSTR, "x",
            GP_UI16, ptr_format->height, GP_CSTR, "x",
            GP_UI16, ptr_format->bits_per_pixel, GP_NEWLINE,
            GP_CSTR, "\t\t\tFrames per second:\t", GP_R32, ptr_format->frames_per_second, GP_NEWLINE,
            GP_END
        );
    }
    return result;
}

static gate_result_t unit_video_listsources(gate_console_t* con, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t result;
    gate_delegate_t callback;

    GATE_DELEGATE_BIND_FUNC(gate_video_sources_enum_callback, &callback, unit_video_listsources_callback);

    result = gate_video_sources_enum(&callback, con);

    return result;
}


/**************
 *  snapshot  *
 **************/

typedef struct unit_video_snapshot_class
{
    gate_string_t	source_id;
    gate_string_t	file;
    gate_uint32_t	width;
    gate_uint32_t	height;
    gate_real32_t	fps;
} unit_video_snapshot_t;

static unit_video_snapshot_t snapshot_params = GATE_INIT_EMPTY;

static gatecli_paramter_t unit_video_snapshot_param_defs[] = {
    { "--source-id",	GATE_TYPE_STRING, &snapshot_params.source_id, false, "ID of video source" },
    { "--file",			GATE_TYPE_STRING, &snapshot_params.file, true, "image output file" },
    { "--width",		GATE_TYPE_UI32, &snapshot_params.width, true, "width of image in pixels" },
    { "--height",		GATE_TYPE_UI32, &snapshot_params.height, true, "height of image in pixels" },
    { "--fps",			GATE_TYPE_R32, &snapshot_params.fps, true, "frames per second" }
};

static gate_result_t unit_video_snapshot(gate_console_t* con, char const* const* params, gate_size_t paramcount, gate_uint32_t flags)
{
    gate_result_t result = GATE_RESULT_OK;
    char source_id[256];
    gate_video_format_t formats[64];
    gate_size_t format_count = sizeof(formats) / sizeof(formats[0]);
    gate_video_source_t* source = NULL;
    gate_uint32_t const* ptr_width = NULL;
    gate_uint32_t const* ptr_height = NULL;
    gate_real32_t const* ptr_fps = NULL;
    gate_video_format_t const* ptr_format = NULL;
    gate_video_frame_t frame;
    do
    {
        gate_mem_clear(&frame, sizeof(frame));

        if (snapshot_params.width != 0) ptr_width = &snapshot_params.width;
        if (snapshot_params.height != 0) ptr_height = &snapshot_params.height;
        if (snapshot_params.fps != 0) ptr_fps = &snapshot_params.fps;


        GATE_STRING_TO_BUFFER(&snapshot_params.source_id, source_id);
        result = gate_video_source_by_id(source_id, &source);
        if (GATE_FAILED(result))
        {
            gate_console_println_err(con, gate_result_text(result));
            gate_console_println_err(con, "Failed to connect to requested video source");
            break;
        }

        format_count = gate_video_source_get_supported_formats(source, formats, format_count);

        ptr_format = gate_video_format_choose(formats, format_count, ptr_width, ptr_height, NULL, ptr_fps);
        if (ptr_format == NULL)
        {
            gate_console_println_err(con, "Failed to choose video frame format");
            break;
        }

        result = gate_video_source_open(source, ptr_format);
        if (GATE_FAILED(result))
        {
            gate_console_println_err(con, gate_result_text(result));
            gate_console_println_err(con, "Failed to connect to requested video source");
            break;
        }

        gate_thread_sleep(1000);

        result = gate_video_source_read(source, &frame);
        if (GATE_FAILED(result))
        {
            gate_console_println_err(con, gate_result_text(result));
            gate_console_println_err(con, "Failed to read frame from video source");
            break;
        }
        result = gate_imageformat_save_file(&snapshot_params.file, &frame.image, 0);
        gate_video_frame_release(&frame);
    } while (0);

    if (source != NULL)
    {
        gate_video_source_close(source);
        gate_object_release(source);
    }
    gate_video_frame_release(&frame);
    return result;
}











static gatecli_function_t video_functions[] =
{
    GATECLI_FUNCTION("listsources",		&unit_video_listsources, "Lists all installed video devices"),
    GATECLI_FUNCTION_PARAMS("snapshot",	&unit_video_snapshot, unit_video_snapshot_param_defs, "Connects a video source and saves a snapshot image"),
};


static gatecli_unit_t global_unit_video =
{
    "video",
    "Functions to access audio sources",
    &video_functions[0],
    sizeof(video_functions) / sizeof(video_functions[0])
};

gatecli_unit_t* unit_video()
{
    return &global_unit_video;
}

#endif