libexplain  1.4.D001
libexplain/buffer/v4l2_buffer.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2011-2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/errno.h>
00020 #include <libexplain/ac/string.h>
00021 #include <libexplain/ac/linux/videodev2.h>
00022 #include <libexplain/ac/sys/ioctl.h>
00023 
00024 #include <libexplain/buffer/int32_t.h>
00025 #include <libexplain/buffer/long.h>
00026 #include <libexplain/buffer/pointer.h>
00027 #include <libexplain/buffer/timeval.h>
00028 #include <libexplain/buffer/v4l2_buf_flags.h>
00029 #include <libexplain/buffer/v4l2_buf_type.h>
00030 #include <libexplain/buffer/v4l2_buffer.h>
00031 #include <libexplain/buffer/v4l2_field.h>
00032 #include <libexplain/buffer/v4l2_memory.h>
00033 #include <libexplain/buffer/v4l2_timecode.h>
00034 #include <libexplain/is_efault.h>
00035 
00036 
00037 #ifdef HAVE_LINUX_VIDEODEV2_H
00038 
00039 void
00040 explain_buffer_v4l2_buffer(explain_string_buffer_t *sb,
00041     const struct v4l2_buffer *data, int extra)
00042 {
00043     if (explain_is_efault_pointer(data, sizeof(*data)))
00044     {
00045         explain_buffer_pointer(sb, data);
00046         return;
00047     }
00048 
00049     explain_string_buffer_puts(sb, "{ index = ");
00050     explain_buffer_uint32_t(sb, data->index);
00051     explain_string_buffer_puts(sb, ", type = ");
00052     explain_buffer_v4l2_buf_type(sb, data->type);
00053     if (extra)
00054     {
00055         explain_string_buffer_puts(sb, ", bytesused = ");
00056         explain_buffer_uint32_t(sb, data->bytesused);
00057         explain_string_buffer_puts(sb, ", flags = ");
00058         explain_buffer_v4l2_buf_flags(sb, data->flags);
00059         explain_string_buffer_puts(sb, ", field = ");
00060         explain_buffer_v4l2_field(sb, data->field);
00061         explain_string_buffer_puts(sb, ", timestamp = ");
00062         explain_buffer_timeval(sb, &data->timestamp);
00063         explain_string_buffer_puts(sb, ", timecode = ");
00064         explain_buffer_v4l2_timecode(sb, &data->timecode);
00065         explain_string_buffer_puts(sb, ", sequence = ");
00066         explain_buffer_uint32_t(sb, data->sequence);
00067     }
00068     explain_string_buffer_puts(sb, ", memory = ");
00069     explain_buffer_v4l2_memory(sb, data->memory);
00070     if (extra)
00071     {
00072         switch (data->memory)
00073         {
00074         case V4L2_MEMORY_MMAP:
00075             explain_string_buffer_puts(sb, ", m.offset = ");
00076             explain_buffer_uint32_t(sb, data->m.offset);
00077             break;
00078 
00079         case V4L2_MEMORY_USERPTR:
00080             explain_string_buffer_puts(sb, ", m.userptr = ");
00081             explain_buffer_ulong(sb, data->m.userptr);
00082             break;
00083 
00084         case V4L2_MEMORY_OVERLAY:
00085         default:
00086             break;
00087         }
00088         explain_string_buffer_puts(sb, ", length = ");
00089         explain_buffer_uint32_t(sb, data->length);
00090 #ifdef LINUX_VIDEODEV2_H_struct_v4l2_buffer_input
00091         explain_string_buffer_puts(sb, ", input = ");
00092         explain_buffer_uint32_t(sb, data->input);
00093 #endif
00094         if (data->reserved)
00095         {
00096             explain_string_buffer_puts(sb, ", reserved = ");
00097             explain_buffer_uint32_t(sb, data->reserved);
00098         }
00099     }
00100     explain_string_buffer_puts(sb, " }");
00101 }
00102 
00103 
00104 int
00105 explain_v4l2_buffer_get_nframes(int fildes, int type)
00106 {
00107     int lo = 0;
00108     int hi = 200;
00109     for (;;)
00110     {
00111         int mid = (lo + hi) / 2;
00112         struct v4l2_buffer qry;
00113         memset(&qry, 0, sizeof(qry));
00114         qry.index = mid;
00115         qry.type = type; /* NOT optional */
00116         if (ioctl(fildes, VIDIOC_QUERYBUF, &qry) >= 0)
00117         {
00118             if (hi <= 0 && lo <= 0)
00119                 return -1;
00120             /* mid < nframes */
00121             lo = mid + 1;
00122             if (lo > hi)
00123                 return lo;
00124         }
00125         else if (errno != EINVAL)
00126         {
00127             return -1;
00128         }
00129         else
00130         {
00131             if (hi <= 0 && lo <= 0)
00132                 return -1;
00133             /* mid >= nframes */
00134             hi = mid - 1;
00135             if (lo >= hi)
00136                 return lo;
00137         }
00138     }
00139 }
00140 
00141 #else
00142 
00143 void
00144 explain_buffer_v4l2_buffer(explain_string_buffer_t *sb,
00145     const struct v4l2_buffer *data, int extra)
00146 {
00147     (void)extra;
00148     explain_buffer_pointer(sb, data);
00149 }
00150 
00151 
00152 int
00153 explain_v4l2_buffer_get_nframes(int fildes, int type)
00154 {
00155     return -1;
00156 }
00157 
00158 #endif
00159 
00160 /* vim: set ts=8 sw=4 et : */