Path: blob/master/Documentation/DocBook/v4l/func-read.xml
10821 views
<refentry id="func-read">1<refmeta>2<refentrytitle>V4L2 read()</refentrytitle>3&manvol;4</refmeta>56<refnamediv>7<refname>v4l2-read</refname>8<refpurpose>Read from a V4L2 device</refpurpose>9</refnamediv>1011<refsynopsisdiv>12<funcsynopsis>13<funcsynopsisinfo>#include <unistd.h></funcsynopsisinfo>14<funcprototype>15<funcdef>ssize_t <function>read</function></funcdef>16<paramdef>int <parameter>fd</parameter></paramdef>17<paramdef>void *<parameter>buf</parameter></paramdef>18<paramdef>size_t <parameter>count</parameter></paramdef>19</funcprototype>20</funcsynopsis>21</refsynopsisdiv>2223<refsect1>24<title>Arguments</title>2526<variablelist>27<varlistentry>28<term><parameter>fd</parameter></term>29<listitem>30<para>&fd;</para>31</listitem>32</varlistentry>33<varlistentry>34<term><parameter>buf</parameter></term>35<listitem>36<para></para>37</listitem>38</varlistentry>39<varlistentry>40<term><parameter>count</parameter></term>41<listitem>42<para></para>43</listitem>44</varlistentry>45</variablelist>46</refsect1>4748<refsect1>49<title>Description</title>5051<para><function>read()</function> attempts to read up to52<parameter>count</parameter> bytes from file descriptor53<parameter>fd</parameter> into the buffer starting at54<parameter>buf</parameter>. The layout of the data in the buffer is55discussed in the respective device interface section, see ##. If <parameter>count</parameter> is zero,56<function>read()</function> returns zero and has no other results. If57<parameter>count</parameter> is greater than58<constant>SSIZE_MAX</constant>, the result is unspecified. Regardless59of the <parameter>count</parameter> value each60<function>read()</function> call will provide at most one frame (two61fields) worth of data.</para>6263<para>By default <function>read()</function> blocks until data64becomes available. When the <constant>O_NONBLOCK</constant> flag was65given to the &func-open; function it66returns immediately with an &EAGAIN; when no data is available. The67&func-select; or &func-poll; functions68can always be used to suspend execution until data becomes available. All69drivers supporting the <function>read()</function> function must also70support <function>select()</function> and71<function>poll()</function>.</para>7273<para>Drivers can implement read functionality in different74ways, using a single or multiple buffers and discarding the oldest or75newest frames once the internal buffers are filled.</para>7677<para><function>read()</function> never returns a "snapshot" of a78buffer being filled. Using a single buffer the driver will stop79capturing when the application starts reading the buffer until the80read is finished. Thus only the period of the vertical blanking81interval is available for reading, or the capture rate must fall below82the nominal frame rate of the video standard.</para>8384<para>The behavior of85<function>read()</function> when called during the active picture86period or the vertical blanking separating the top and bottom field87depends on the discarding policy. A driver discarding the oldest88frames keeps capturing into an internal buffer, continuously89overwriting the previously, not read frame, and returns the frame90being received at the time of the <function>read()</function> call as91soon as it is complete.</para>9293<para>A driver discarding the newest frames stops capturing until94the next <function>read()</function> call. The frame being received at95<function>read()</function> time is discarded, returning the following96frame instead. Again this implies a reduction of the capture rate to97one half or less of the nominal frame rate. An example of this model98is the video read mode of the bttv driver, initiating a DMA to user99memory when <function>read()</function> is called and returning when100the DMA finished.</para>101102<para>In the multiple buffer model drivers maintain a ring of103internal buffers, automatically advancing to the next free buffer.104This allows continuous capturing when the application can empty the105buffers fast enough. Again, the behavior when the driver runs out of106free buffers depends on the discarding policy.</para>107108<para>Applications can get and set the number of buffers used109internally by the driver with the &VIDIOC-G-PARM; and &VIDIOC-S-PARM;110ioctls. They are optional, however. The discarding policy is not111reported and cannot be changed. For minimum requirements see <xref112linkend="devices" />.</para>113</refsect1>114115<refsect1>116<title>Return Value</title>117118<para>On success, the number of bytes read is returned. It is not119an error if this number is smaller than the number of bytes requested,120or the amount of data required for one frame. This may happen for121example because <function>read()</function> was interrupted by a122signal. On error, -1 is returned, and the <varname>errno</varname>123variable is set appropriately. In this case the next read will start124at the beginning of a new frame. Possible error codes are:</para>125126<variablelist>127<varlistentry>128<term><errorcode>EAGAIN</errorcode></term>129<listitem>130<para>Non-blocking I/O has been selected using131O_NONBLOCK and no data was immediately available for reading.</para>132</listitem>133</varlistentry>134<varlistentry>135<term><errorcode>EBADF</errorcode></term>136<listitem>137<para><parameter>fd</parameter> is not a valid file138descriptor or is not open for reading, or the process already has the139maximum number of files open.</para>140</listitem>141</varlistentry>142<varlistentry>143<term><errorcode>EBUSY</errorcode></term>144<listitem>145<para>The driver does not support multiple read streams and the146device is already in use.</para>147</listitem>148</varlistentry>149<varlistentry>150<term><errorcode>EFAULT</errorcode></term>151<listitem>152<para><parameter>buf</parameter> references an inaccessible153memory area.</para>154</listitem>155</varlistentry>156<varlistentry>157<term><errorcode>EINTR</errorcode></term>158<listitem>159<para>The call was interrupted by a signal before any160data was read.</para>161</listitem>162</varlistentry>163<varlistentry>164<term><errorcode>EIO</errorcode></term>165<listitem>166<para>I/O error. This indicates some hardware problem or a167failure to communicate with a remote device (USB camera etc.).</para>168</listitem>169</varlistentry>170<varlistentry>171<term><errorcode>EINVAL</errorcode></term>172<listitem>173<para>The <function>read()</function> function is not174supported by this driver, not on this device, or generally not on this175type of device.</para>176</listitem>177</varlistentry>178</variablelist>179</refsect1>180</refentry>181182<!--183Local Variables:184mode: sgml185sgml-parent-document: "v4l2.sgml"186indent-tabs-mode: nil187End:188-->189190191