Mercurial > mplayer.hg
annotate stream/tvi_v4l2.c @ 37127:ca7e13830a05
Allow building of v4l2 without clock_gettime().
Add overly verbose message in case monotone timestamps are required by the kernel.
author | iive |
---|---|
date | Tue, 10 Jun 2014 16:39:04 +0000 |
parents | 6db1f7a10803 |
children |
rev | line source |
---|---|
10536 | 1 /* |
28106 | 2 * Video 4 Linux 2 input |
3 * | |
4 * copyright (c) 2003 Martin Olschewski <olschewski@zpr.uni-koeln.de> | |
5 * copyright (c) 2003 Jindrich Makovicka <makovick@gmail.com> | |
6 * | |
7 * Some ideas are based on works from | |
8 * Alex Beregszaszi <alex@fsn.hu> | |
9 * Gerd Knorr <kraxel@bytesex.org> | |
10 * | |
11 * This file is part of MPlayer. | |
12 * | |
13 * MPlayer is free software; you can redistribute it and/or modify | |
14 * it under the terms of the GNU General Public License as published by | |
15 * the Free Software Foundation; either version 2 of the License, or | |
16 * (at your option) any later version. | |
17 * | |
18 * MPlayer is distributed in the hope that it will be useful, | |
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 * GNU General Public License for more details. | |
22 * | |
23 * You should have received a copy of the GNU General Public License along | |
24 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
26 */ | |
10536 | 27 |
28 /* | |
29 | |
30 known issues: | |
31 - norm setting isn't consistent with tvi_v4l | |
32 - the same for volume/bass/treble/balance | |
33 | |
34 */ | |
35 | |
36 #include "config.h" | |
37 | |
38 #include <errno.h> | |
39 #include <fcntl.h> | |
40 #include <pthread.h> | |
41 #include <stdio.h> | |
42 #include <string.h> | |
35903 | 43 #include <strings.h> |
10536 | 44 #include <sys/ioctl.h> |
45 #include <sys/mman.h> | |
46 #include <sys/time.h> | |
47 #include <sys/types.h> | |
48 #include <unistd.h> | |
27461
5a30f5bc23a0
Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents:
27390
diff
changeset
|
49 #include <math.h> |
10536 | 50 #ifdef HAVE_SYS_SYSINFO_H |
51 #include <sys/sysinfo.h> | |
52 #endif | |
33163 | 53 #ifdef HAVE_SYS_VIDEOIO_H |
54 #include <sys/videoio.h> | |
55 #else | |
16442 | 56 #include <linux/types.h> |
57 #include <linux/videodev2.h> | |
33163 | 58 #endif |
17012 | 59 #include "mp_msg.h" |
19431
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
19271
diff
changeset
|
60 #include "libmpcodecs/img_format.h" |
29759
d287e2785570
Move teletext specific code from stream into libmpcodecs.
cehoyos
parents:
29263
diff
changeset
|
61 #include "libmpcodecs/dec_teletext.h" |
17012 | 62 #include "libaf/af_format.h" |
10536 | 63 #include "tv.h" |
64 #include "audio_in.h" | |
65 | |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
66 // flag introduced in kernel 3.10 |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
67 #ifndef V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
68 #define V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC 0x2000 |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
69 #endif |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
70 |
22381
6cabac4d35b5
tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents:
21587
diff
changeset
|
71 #define info tvi_info_v4l2 |
23883 | 72 static tvi_handle_t *tvi_init_v4l2(tv_param_t* tv_param); |
10536 | 73 /* information about this file */ |
25689 | 74 const tvi_info_t tvi_info_v4l2 = { |
22381
6cabac4d35b5
tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents:
21587
diff
changeset
|
75 tvi_init_v4l2, |
10536 | 76 "Video 4 Linux 2 input", |
77 "v4l2", | |
78 "Martin Olschewski <olschewski@zpr.uni-koeln.de>", | |
79 "first try, more to come ;-)" | |
80 }; | |
81 | |
82 struct map { | |
83 struct v4l2_buffer buf; | |
84 void *addr; | |
85 size_t len; | |
86 }; | |
87 | |
88 #define BUFFER_COUNT 6 | |
89 | |
23423 | 90 /** video ringbuffer entry */ |
91 typedef struct { | |
92 unsigned char *data; ///< frame contents | |
93 long long timestamp; ///< frame timestamp | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
94 int framesize; ///< actual frame size |
23423 | 95 } video_buffer_entry; |
96 | |
10536 | 97 /* private data */ |
31322
016194f71de3
Fix function pointer types in tvi_functions struct
reimar
parents:
29949
diff
changeset
|
98 typedef struct priv { |
10536 | 99 /* video */ |
23151 | 100 char *video_dev; |
101 int video_fd; | |
23901 | 102 char *vbi_dev; |
103 int vbi_fd; | |
104 int vbi_bufsize; | |
105 int vbi_shutdown; | |
106 pthread_t vbi_grabber_thread; | |
107 void *priv_vbi; | |
10536 | 108 int mp_format; |
23151 | 109 struct v4l2_capability capability; |
10536 | 110 struct v4l2_input input; |
23151 | 111 struct v4l2_format format; |
112 struct v4l2_standard standard; | |
113 struct v4l2_tuner tuner; | |
114 struct map *map; | |
115 int mapcount; | |
116 int frames; | |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
117 volatile long long first_frame; ///< number of useconds |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
118 long long curr_frame; ///< usec, using kernel timestamps |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
119 int clk_id; /**< clk_id from clock_gettime |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
120 used in frame timestamps */ |
10536 | 121 /* audio video interleaving ;-) */ |
23151 | 122 volatile int streamon; |
123 pthread_t audio_grabber_thread; | |
124 pthread_mutex_t skew_mutex; | |
10536 | 125 |
126 /* 2nd level video buffers */ | |
127 int first; | |
128 int immediate_mode; | |
129 | |
130 int video_buffer_size_max; | |
131 volatile int video_buffer_size_current; | |
23423 | 132 video_buffer_entry *video_ringbuffer; |
23151 | 133 volatile int video_head; |
134 volatile int video_tail; | |
135 volatile int video_cnt; | |
136 pthread_t video_grabber_thread; | |
10536 | 137 pthread_mutex_t video_buffer_mutex; |
138 | |
139 /* audio */ | |
23151 | 140 char *audio_dev; |
10536 | 141 audio_in_t audio_in; |
142 | |
143 long long audio_start_time; | |
144 int audio_buffer_size; | |
145 int aud_skew_cnt; | |
23151 | 146 unsigned char *audio_ringbuffer; |
147 long long *audio_skew_buffer; | |
148 long long *audio_skew_delta_buffer; | |
149 volatile int audio_head; | |
150 volatile int audio_tail; | |
151 volatile int audio_cnt; | |
10536 | 152 volatile long long audio_skew; |
153 volatile double audio_skew_factor; | |
154 volatile long long audio_skew_measure_time; | |
155 volatile int audio_drop; | |
156 volatile int shutdown; | |
157 | |
25962 | 158 int audio_initialized; |
10536 | 159 double audio_secs_per_block; |
15449 | 160 long long audio_usecs_per_block; |
10536 | 161 long long audio_skew_total; |
10653 | 162 long long audio_skew_delta_total; |
23151 | 163 long audio_recv_blocks_total; |
164 long audio_sent_blocks_total; | |
15449 | 165 pthread_mutex_t audio_mutex; |
166 int audio_insert_null_samples; | |
167 volatile long audio_null_blocks_inserted; | |
168 volatile long long dropped_frames_timeshift; | |
169 long long dropped_frames_compensated; | |
23883 | 170 |
171 tv_param_t *tv_param; | |
10536 | 172 } priv_t; |
173 | |
174 #include "tvi_def.h" | |
175 | |
176 static void *audio_grabber(void *data); | |
177 static void *video_grabber(void *data); | |
178 | |
179 /**********************************************************************\ | |
180 | |
181 Only few of the fourccs are the same in v4l2 and mplayer: | |
182 | |
183 IMGFMT_YVU9 == V4L2_PIX_FMT_YVU410 | |
184 IMGFMT_YV12 == V4L2_PIX_FMT_YVU420 | |
185 IMGFMT_NV12 == V4L2_PIX_FMT_NV12 | |
186 IMGFMT_422P == V4L2_PIX_FMT_YUV422P | |
187 IMGFMT_411P == V4L2_PIX_FMT_YUV411P | |
188 IMGFMT_UYVY == V4L2_PIX_FMT_UYVY | |
189 IMGFMT_Y41P == V4L2_PIX_FMT_Y41P | |
190 | |
191 This may be an useful translation table for some others: | |
192 | |
193 IMGFMT_RGB8 == V4L2_PIX_FMT_RGB332 | |
15449 | 194 IMGFMT_BGR15 == V4L2_PIX_FMT_RGB555 |
195 IMGFMT_BGR16 == V4L2_PIX_FMT_RGB565 | |
10536 | 196 IMGFMT_RGB24 == V4L2_PIX_FMT_RGB24 |
197 IMGFMT_RGB32 == V4L2_PIX_FMT_RGB32 | |
198 IMGFMT_BGR24 == V4L2_PIX_FMT_BGR24 | |
199 IMGFMT_BGR32 == V4L2_PIX_FMT_BGR32 | |
200 IMGFMT_Y800 == V4L2_PIX_FMT_GREY | |
201 IMGFMT_IF09 == V4L2_PIX_FMT_YUV410 | |
202 IMGFMT_I420 == V4L2_PIX_FMT_YUV420 | |
203 IMGFMT_YUY2 == V4L2_PIX_FMT_YUYV | |
204 | |
205 \**********************************************************************/ | |
206 | |
207 /* | |
208 ** Translate a mplayer fourcc to a video4linux2 pixel format. | |
209 */ | |
210 static int fcc_mp2vl(int fcc) | |
211 { | |
212 switch (fcc) { | |
23151 | 213 case IMGFMT_RGB8: return V4L2_PIX_FMT_RGB332; |
214 case IMGFMT_BGR15: return V4L2_PIX_FMT_RGB555; | |
215 case IMGFMT_BGR16: return V4L2_PIX_FMT_RGB565; | |
216 case IMGFMT_RGB24: return V4L2_PIX_FMT_RGB24; | |
217 case IMGFMT_RGB32: return V4L2_PIX_FMT_RGB32; | |
218 case IMGFMT_BGR24: return V4L2_PIX_FMT_BGR24; | |
219 case IMGFMT_BGR32: return V4L2_PIX_FMT_BGR32; | |
220 case IMGFMT_Y800: return V4L2_PIX_FMT_GREY; | |
221 case IMGFMT_IF09: return V4L2_PIX_FMT_YUV410; | |
222 case IMGFMT_I420: return V4L2_PIX_FMT_YUV420; | |
223 case IMGFMT_YUY2: return V4L2_PIX_FMT_YUYV; | |
224 case IMGFMT_YV12: return V4L2_PIX_FMT_YVU420; | |
11657 | 225 case IMGFMT_UYVY: return V4L2_PIX_FMT_UYVY; |
23423 | 226 case IMGFMT_MJPEG: return V4L2_PIX_FMT_MJPEG; |
10536 | 227 } |
228 return fcc; | |
229 } | |
230 | |
231 /* | |
232 ** Translate a video4linux2 fourcc aka pixel format to mplayer. | |
233 */ | |
234 static int fcc_vl2mp(int fcc) | |
235 { | |
236 switch (fcc) { | |
23151 | 237 case V4L2_PIX_FMT_RGB332: return IMGFMT_RGB8; |
238 case V4L2_PIX_FMT_RGB555: return IMGFMT_BGR15; | |
239 case V4L2_PIX_FMT_RGB565: return IMGFMT_BGR16; | |
240 case V4L2_PIX_FMT_RGB24: return IMGFMT_RGB24; | |
241 case V4L2_PIX_FMT_RGB32: return IMGFMT_RGB32; | |
242 case V4L2_PIX_FMT_BGR24: return IMGFMT_BGR24; | |
243 case V4L2_PIX_FMT_BGR32: return IMGFMT_BGR32; | |
244 case V4L2_PIX_FMT_GREY: return IMGFMT_Y800; | |
245 case V4L2_PIX_FMT_YUV410: return IMGFMT_IF09; | |
246 case V4L2_PIX_FMT_YUV420: return IMGFMT_I420; | |
247 case V4L2_PIX_FMT_YVU420: return IMGFMT_YV12; | |
248 case V4L2_PIX_FMT_YUYV: return IMGFMT_YUY2; | |
11657 | 249 case V4L2_PIX_FMT_UYVY: return IMGFMT_UYVY; |
23423 | 250 case V4L2_PIX_FMT_MJPEG: return IMGFMT_MJPEG; |
10536 | 251 } |
252 return fcc; | |
253 } | |
254 | |
255 /* | |
256 ** Translate a video4linux2 fourcc aka pixel format | |
257 ** to a human readable string. | |
258 */ | |
19108
5e767cabf4cd
marks several read-only string parameters and function return-values which can only be used read-only as const. Patch by Stefan Huehner, stefan _AT huener-org
reynaldo
parents:
18958
diff
changeset
|
259 static const char *pixfmt2name(int pixfmt) |
10536 | 260 { |
261 static char unknown[24]; | |
262 | |
263 switch (pixfmt) { | |
23151 | 264 case V4L2_PIX_FMT_RGB332: return "RGB332"; |
265 case V4L2_PIX_FMT_RGB555: return "RGB555"; | |
266 case V4L2_PIX_FMT_RGB565: return "RGB565"; | |
267 case V4L2_PIX_FMT_RGB555X: return "RGB555X"; | |
268 case V4L2_PIX_FMT_RGB565X: return "RGB565X"; | |
269 case V4L2_PIX_FMT_BGR24: return "BGR24"; | |
270 case V4L2_PIX_FMT_RGB24: return "RGB24"; | |
271 case V4L2_PIX_FMT_BGR32: return "BGR32"; | |
272 case V4L2_PIX_FMT_RGB32: return "RGB32"; | |
273 case V4L2_PIX_FMT_GREY: return "GREY"; | |
274 case V4L2_PIX_FMT_YVU410: return "YVU410"; | |
275 case V4L2_PIX_FMT_YVU420: return "YVU420"; | |
276 case V4L2_PIX_FMT_YUYV: return "YUYV"; | |
277 case V4L2_PIX_FMT_UYVY: return "UYVY"; | |
278 /* case V4L2_PIX_FMT_YVU422P: return "YVU422P"; */ | |
279 /* case V4L2_PIX_FMT_YVU411P: return "YVU411P"; */ | |
280 case V4L2_PIX_FMT_YUV422P: return "YUV422P"; | |
281 case V4L2_PIX_FMT_YUV411P: return "YUV411P"; | |
282 case V4L2_PIX_FMT_Y41P: return "Y41P"; | |
283 case V4L2_PIX_FMT_NV12: return "NV12"; | |
284 case V4L2_PIX_FMT_NV21: return "NV21"; | |
285 case V4L2_PIX_FMT_YUV410: return "YUV410"; | |
286 case V4L2_PIX_FMT_YUV420: return "YUV420"; | |
287 case V4L2_PIX_FMT_YYUV: return "YYUV"; | |
288 case V4L2_PIX_FMT_HI240: return "HI240"; | |
289 case V4L2_PIX_FMT_WNVA: return "WNVA"; | |
23423 | 290 case V4L2_PIX_FMT_MJPEG: return "MJPEG"; |
10536 | 291 } |
292 sprintf(unknown, "unknown (0x%x)", pixfmt); | |
293 return unknown; | |
294 } | |
295 | |
296 | |
297 /* | |
298 ** Gives the depth of a video4linux2 fourcc aka pixel format in bits. | |
299 */ | |
300 static int pixfmt2depth(int pixfmt) | |
301 { | |
302 switch (pixfmt) { | |
303 case V4L2_PIX_FMT_RGB332: | |
23151 | 304 return 8; |
10536 | 305 case V4L2_PIX_FMT_RGB555: |
306 case V4L2_PIX_FMT_RGB565: | |
307 case V4L2_PIX_FMT_RGB555X: | |
308 case V4L2_PIX_FMT_RGB565X: | |
23151 | 309 return 16; |
10536 | 310 case V4L2_PIX_FMT_BGR24: |
311 case V4L2_PIX_FMT_RGB24: | |
23151 | 312 return 24; |
10536 | 313 case V4L2_PIX_FMT_BGR32: |
314 case V4L2_PIX_FMT_RGB32: | |
23151 | 315 return 32; |
10536 | 316 case V4L2_PIX_FMT_GREY: |
23151 | 317 return 8; |
10536 | 318 case V4L2_PIX_FMT_YVU410: |
23151 | 319 return 9; |
10536 | 320 case V4L2_PIX_FMT_YVU420: |
23151 | 321 return 12; |
10536 | 322 case V4L2_PIX_FMT_YUYV: |
323 case V4L2_PIX_FMT_UYVY: | |
324 case V4L2_PIX_FMT_YUV422P: | |
325 case V4L2_PIX_FMT_YUV411P: | |
23151 | 326 return 16; |
10536 | 327 case V4L2_PIX_FMT_Y41P: |
328 case V4L2_PIX_FMT_NV12: | |
329 case V4L2_PIX_FMT_NV21: | |
23151 | 330 return 12; |
10536 | 331 case V4L2_PIX_FMT_YUV410: |
23151 | 332 return 9; |
10536 | 333 case V4L2_PIX_FMT_YUV420: |
23151 | 334 return 12; |
10536 | 335 case V4L2_PIX_FMT_YYUV: |
23151 | 336 return 16; |
10536 | 337 case V4L2_PIX_FMT_HI240: |
23151 | 338 return 8; |
10536 | 339 |
340 } | |
341 return 0; | |
342 } | |
343 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
344 static int amode2v4l(int amode) |
10536 | 345 { |
346 switch (amode) { | |
347 case 0: | |
23151 | 348 return V4L2_TUNER_MODE_MONO; |
10536 | 349 case 1: |
23151 | 350 return V4L2_TUNER_MODE_STEREO; |
10536 | 351 case 2: |
23151 | 352 return V4L2_TUNER_MODE_LANG1; |
10536 | 353 case 3: |
23151 | 354 return V4L2_TUNER_MODE_LANG2; |
10536 | 355 default: |
23151 | 356 return -1; |
10536 | 357 } |
358 } | |
359 | |
360 | |
27138
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
361 /* |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
362 ** Get current FPS. |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
363 */ |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
364 static double getfps(priv_t *priv) |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
365 { |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
366 if (priv->tv_param->fps > 0) |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
367 return priv->tv_param->fps; |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
368 if (priv->standard.frameperiod.denominator && priv->standard.frameperiod.numerator) |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
369 return (double)priv->standard.frameperiod.denominator / priv->standard.frameperiod.numerator; |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
370 return 25.0; |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
371 } |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
372 |
10536 | 373 // sets and sanitizes audio buffer/block sizes |
374 static void setup_audio_buffer_sizes(priv_t *priv) | |
375 { | |
376 int bytes_per_sample = priv->audio_in.bytes_per_sample; | |
27138
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
377 int seconds = priv->video_buffer_size_max/getfps(priv); |
10536 | 378 |
379 if (seconds < 5) seconds = 5; | |
380 if (seconds > 500) seconds = 500; | |
381 | |
382 // make the audio buffer at least as the video buffer capacity (or 5 seconds) long | |
383 priv->audio_buffer_size = 1 + seconds*priv->audio_in.samplerate | |
23151 | 384 *priv->audio_in.channels |
385 *bytes_per_sample/priv->audio_in.blocksize; | |
10536 | 386 if (priv->audio_buffer_size < 256) priv->audio_buffer_size = 256; |
387 | |
388 // make the skew buffer at least 1 second long | |
389 priv->aud_skew_cnt = 1 + 1*priv->audio_in.samplerate | |
23151 | 390 *priv->audio_in.channels |
391 *bytes_per_sample/priv->audio_in.blocksize; | |
10536 | 392 if (priv->aud_skew_cnt < 16) priv->aud_skew_cnt = 16; |
393 | |
394 mp_msg(MSGT_TV, MSGL_V, "Audio capture - buffer %d blocks of %d bytes, skew average from %d meas.\n", | |
23151 | 395 priv->audio_buffer_size, priv->audio_in.blocksize, priv->aud_skew_cnt); |
10536 | 396 } |
397 | |
15464 | 398 static void init_audio(priv_t *priv) |
399 { | |
25962 | 400 if (priv->audio_initialized) return; |
15464 | 401 |
23886 | 402 if (!priv->tv_param->noaudio) { |
27390
9d95dc936e66
Introduce CONFIG_ALSA preprocessor directive for ALSA 0.9 and 1.x.
diego
parents:
27387
diff
changeset
|
403 #ifdef CONFIG_ALSA |
23886 | 404 if (priv->tv_param->alsa) |
23151 | 405 audio_in_init(&priv->audio_in, AUDIO_IN_ALSA); |
406 else | |
407 audio_in_init(&priv->audio_in, AUDIO_IN_OSS); | |
15464 | 408 #else |
23151 | 409 audio_in_init(&priv->audio_in, AUDIO_IN_OSS); |
15464 | 410 #endif |
411 | |
23151 | 412 if (priv->audio_dev) { |
413 audio_in_set_device(&priv->audio_in, priv->audio_dev); | |
414 } | |
15464 | 415 |
23151 | 416 audio_in_set_samplerate(&priv->audio_in, 44100); |
417 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
418 if (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) { | |
419 audio_in_set_channels(&priv->audio_in, 2); | |
420 } else { | |
421 audio_in_set_channels(&priv->audio_in, 1); | |
422 } | |
423 } else { | |
23886 | 424 if (priv->tv_param->forcechan >= 0) { |
425 audio_in_set_channels(&priv->audio_in, priv->tv_param->forcechan); | |
23151 | 426 } else { |
427 audio_in_set_channels(&priv->audio_in, 2); | |
428 } | |
429 } | |
15464 | 430 |
23151 | 431 if (audio_in_setup(&priv->audio_in) < 0) return; |
15464 | 432 |
25962 | 433 priv->audio_initialized = 1; |
15464 | 434 } |
435 } | |
436 | |
10536 | 437 #if 0 |
438 /* | |
439 ** the number of milliseconds elapsed between time0 and time1 | |
440 */ | |
441 static size_t difftv(struct timeval time1, struct timeval time0) | |
442 { | |
23151 | 443 return (time1.tv_sec - time0.tv_sec) * 1000 + |
444 (time1.tv_usec - time0.tv_usec) / 1000; | |
10536 | 445 } |
446 #endif | |
447 | |
448 /* | |
449 ** Get current video capture format. | |
450 */ | |
451 static int getfmt(priv_t *priv) | |
452 { | |
453 int i; | |
454 | |
455 priv->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
456 if ((i = ioctl(priv->video_fd, VIDIOC_G_FMT, &priv->format)) < 0) { | |
23151 | 457 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get format failed: %s\n", |
458 info.short_name, strerror(errno)); | |
10536 | 459 } |
460 return i; | |
461 } | |
462 | |
463 | |
464 /* | |
465 ** Get current video capture standard. | |
466 */ | |
467 static int getstd(priv_t *priv) | |
468 { | |
469 v4l2_std_id id; | |
470 int i=0; | |
471 | |
472 if (ioctl(priv->video_fd, VIDIOC_G_STD, &id) < 0) { | |
27139
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
473 struct v4l2_streamparm parm; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
474 |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
475 parm.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
476 if(ioctl(priv->video_fd, VIDIOC_G_PARM, &parm) >= 0) { |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
477 mp_msg(MSGT_TV, MSGL_WARN, "%s: your device driver does not support VIDIOC_G_STD ioctl," |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
478 " VIDIOC_G_PARM was used instead.\n", info.short_name); |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
479 priv->standard.index=0; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
480 priv->standard.id=0; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
481 priv->standard.frameperiod=parm.parm.capture.timeperframe; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
482 return 0; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
483 } |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
484 |
23151 | 485 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get standard failed: %s\n", |
486 info.short_name, strerror(errno)); | |
487 return -1; | |
10536 | 488 } |
489 do { | |
23151 | 490 priv->standard.index = i++; |
491 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) { | |
492 return -1; | |
493 } | |
10536 | 494 } while (priv->standard.id != id); |
495 return 0; | |
496 } | |
497 | |
37127 | 498 #if HAVE_CLOCK_GETTIME |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
499 /* |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
500 ** Gets current timestamp, using specified clock id. |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
501 ** @return number of microseconds. |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
502 */ |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
503 static long long get_curr_timestamp(int clk_id) |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
504 { |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
505 struct timespec ts; |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
506 clock_gettime(clk_id, &ts); |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
507 return (long long)ts.tv_sec * 1000000 + ts.tv_nsec / 1000; |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
508 } |
37127 | 509 #else |
510 /* | |
511 ** Gets current timestamp, using system time. | |
512 ** @return number of microseconds. | |
513 */ | |
514 static long long get_curr_timestamp(int clk_id) | |
515 { | |
516 struct timeval tv; | |
517 gettimeofday(&tv, NULL); | |
518 return (long long)tv.tv_sec * 1000000 + tv.tv_usec; | |
519 } | |
520 #endif | |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
521 |
10536 | 522 /***********************************************************************\ |
23151 | 523 * * |
524 * * | |
525 * Interface to mplayer * | |
526 * * | |
527 * * | |
10536 | 528 \***********************************************************************/ |
529 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
530 static int set_mute(priv_t *priv, int value) |
10536 | 531 { |
532 struct v4l2_control control; | |
533 control.id = V4L2_CID_AUDIO_MUTE; | |
534 control.value = value; | |
535 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, &control) < 0) { | |
23151 | 536 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl set mute failed: %s\n", |
537 info.short_name, strerror(errno)); | |
538 return 0; | |
10536 | 539 } |
540 return 1; | |
541 } | |
542 | |
543 /* | |
12860 | 544 ** MPlayer uses values from -100 up to 100 for controls. |
10536 | 545 ** Here they are scaled to what the tv card needs and applied. |
546 */ | |
547 static int set_control(priv_t *priv, struct v4l2_control *control, int val_signed) { | |
23151 | 548 struct v4l2_queryctrl qctrl; |
10536 | 549 qctrl.id = control->id; |
550 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) { | |
23151 | 551 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n", |
552 info.short_name, strerror(errno)); | |
553 return TVI_CONTROL_FALSE; | |
10536 | 554 } |
555 | |
556 if (val_signed) { | |
23151 | 557 if (control->value < 0) { |
558 control->value = qctrl.default_value + control->value * | |
559 (qctrl.default_value - qctrl.minimum) / 100; | |
560 } else { | |
561 control->value = qctrl.default_value + control->value * | |
562 (qctrl.maximum - qctrl.default_value) / 100; | |
563 } | |
10536 | 564 } else { |
23151 | 565 if (control->value < 50) { |
566 control->value = qctrl.default_value + (control->value-50) * | |
567 (qctrl.default_value - qctrl.minimum) / 50; | |
568 } else { | |
569 control->value = qctrl.default_value + (control->value-50) * | |
570 (qctrl.maximum - qctrl.default_value) / 50; | |
571 } | |
10536 | 572 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
573 |
10536 | 574 |
575 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, control) < 0) { | |
23151 | 576 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl set %s %d failed: %s\n", |
577 info.short_name, qctrl.name, control->value, strerror(errno)); | |
578 return TVI_CONTROL_FALSE; | |
10536 | 579 } |
580 mp_msg(MSGT_TV, MSGL_V, "%s: set %s: %d [%d, %d]\n", info.short_name, | |
581 qctrl.name, control->value, qctrl.minimum, qctrl.maximum); | |
582 | |
583 return TVI_CONTROL_TRUE; | |
584 } | |
585 | |
586 | |
587 /* | |
588 ** Scale the control values back to what mplayer needs. | |
589 */ | |
590 static int get_control(priv_t *priv, struct v4l2_control *control, int val_signed) { | |
23151 | 591 struct v4l2_queryctrl qctrl; |
10536 | 592 |
593 qctrl.id = control->id; | |
594 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) { | |
23151 | 595 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n", |
596 info.short_name, strerror(errno)); | |
597 return TVI_CONTROL_FALSE; | |
10536 | 598 } |
599 | |
600 if (ioctl(priv->video_fd, VIDIOC_G_CTRL, control) < 0) { | |
23151 | 601 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl get %s failed: %s\n", |
602 info.short_name, qctrl.name, strerror(errno)); | |
603 return TVI_CONTROL_FALSE; | |
10536 | 604 } |
605 mp_msg(MSGT_TV, MSGL_V, "%s: get %s: %d [%d, %d]\n", info.short_name, | |
606 qctrl.name, control->value, qctrl.minimum, qctrl.maximum); | |
607 | |
608 if (val_signed) { | |
23151 | 609 if (control->value < qctrl.default_value) { |
610 control->value = (control->value - qctrl.default_value) * 100 / | |
611 (qctrl.default_value - qctrl.minimum); | |
612 } else { | |
613 control->value = (control->value - qctrl.default_value) * 100 / | |
614 (qctrl.maximum - qctrl.default_value); | |
615 } | |
10536 | 616 } else { |
23151 | 617 if (control->value < qctrl.default_value) { |
618 control->value = (control->value - qctrl.default_value) * 50 / | |
619 (qctrl.default_value - qctrl.minimum) + 50; | |
620 } else { | |
621 control->value = (control->value - qctrl.default_value) * 50 / | |
622 (qctrl.maximum - qctrl.default_value) + 50; | |
623 } | |
10536 | 624 } |
625 | |
626 return TVI_CONTROL_TRUE; | |
627 } | |
628 | |
23901 | 629 static int vbi_init(priv_t* priv,char* device) |
630 { | |
631 int vbi_fd=0; | |
632 struct v4l2_capability cap; | |
633 struct v4l2_format fmt; | |
634 int res; | |
635 | |
636 if(!device) | |
637 return TVI_CONTROL_FALSE; | |
638 | |
639 priv->vbi_dev=strdup(device); | |
640 | |
641 vbi_fd=open(priv->vbi_dev,O_RDWR); | |
642 if(vbi_fd<0){ | |
643 mp_msg(MSGT_TV,MSGL_ERR,"vbi: could not open device %s\n",priv->vbi_dev); | |
644 return TVI_CONTROL_FALSE; | |
645 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
646 |
23901 | 647 if(ioctl(vbi_fd,VIDIOC_QUERYCAP,&cap)<0){ |
31834
64ba1daa147a
various spelling fixes, found by the Debian QA tool 'lintian'
siretart
parents:
31322
diff
changeset
|
648 mp_msg(MSGT_TV,MSGL_ERR,"vbi: Query capabilities failed for %s\n",priv->vbi_dev); |
23901 | 649 close(vbi_fd); |
650 return TVI_CONTROL_FALSE; | |
651 } | |
29949 | 652 if(!(cap.capabilities & V4L2_CAP_VBI_CAPTURE)){ |
23901 | 653 mp_msg(MSGT_TV,MSGL_ERR,"vbi: %s does not support VBI capture\n",priv->vbi_dev); |
654 close(vbi_fd); | |
655 return TVI_CONTROL_FALSE; | |
656 } | |
657 | |
658 memset(&fmt,0,sizeof(struct v4l2_format)); | |
659 fmt.type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
660 if((res=ioctl(vbi_fd,VIDIOC_G_FMT,&fmt))<0){ | |
661 mp_msg(MSGT_TV,MSGL_ERR,"vbi: Query format failed: %x\n",res); | |
662 close(vbi_fd); | |
663 return TVI_CONTROL_FALSE; | |
664 } | |
665 if(fmt.fmt.vbi.sample_format!=V4L2_PIX_FMT_GREY){ | |
666 mp_msg(MSGT_TV,MSGL_ERR,"vbi: format 0x%x is not supported\n",fmt.fmt.vbi.sample_format); | |
667 close(vbi_fd); | |
668 return TVI_CONTROL_FALSE; | |
669 } | |
670 priv->vbi_fd=vbi_fd; | |
671 mp_msg(MSGT_TV,MSGL_DBG3,"vbi: init ok\n"); | |
672 return TVI_CONTROL_TRUE; | |
673 } | |
674 | |
675 static int vbi_get_props(priv_t* priv,tt_stream_props* ptsp) | |
676 { | |
677 struct v4l2_format fmt; | |
678 int res; | |
679 if(!priv || !ptsp) | |
680 return TVI_CONTROL_FALSE; | |
681 | |
682 memset(&fmt,0,sizeof(struct v4l2_format)); | |
683 fmt.type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
684 if((res=ioctl(priv->vbi_fd,VIDIOC_G_FMT,&fmt))<0){ | |
685 mp_msg(MSGT_TV,MSGL_ERR,"vbi_get_props: Query format failed: %x\n",res); | |
686 return TVI_CONTROL_FALSE; | |
687 } | |
688 | |
689 ptsp->interlaced=(fmt.fmt.vbi.flags& V4L2_VBI_INTERLACED?1:0); | |
690 | |
691 ptsp->offset=fmt.fmt.vbi.offset; | |
692 ptsp->sampling_rate=fmt.fmt.vbi.sampling_rate; | |
693 ptsp->samples_per_line=fmt.fmt.vbi.samples_per_line, | |
694 | |
695 ptsp->count[0]=fmt.fmt.vbi.count[0]; | |
696 ptsp->count[1]=fmt.fmt.vbi.count[1]; | |
697 ptsp->bufsize = ptsp->samples_per_line * (ptsp->count[0] + ptsp->count[1]); | |
698 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
699 mp_msg(MSGT_TV,MSGL_V,"vbi_get_props: sampling_rate=%d,offset:%d,samples_per_line: %d\n interlaced:%s, count=[%d,%d]\n", |
23901 | 700 ptsp->sampling_rate, |
701 ptsp->offset, | |
702 ptsp->samples_per_line, | |
703 ptsp->interlaced?"Yes":"No", | |
704 ptsp->count[0], | |
705 ptsp->count[1]); | |
706 | |
707 return TVI_CONTROL_TRUE; | |
708 } | |
709 | |
710 static void *vbi_grabber(void *data) | |
711 { | |
712 priv_t *priv = (priv_t *) data; | |
713 int bytes,seq,prev_seq; | |
714 unsigned char* buf; | |
715 tt_stream_props tsp; | |
716 | |
717 if(!priv->priv_vbi){ | |
718 mp_msg(MSGT_TV,MSGL_WARN,"vbi: vbi not initialized. stopping thread.\n"); | |
719 return NULL; | |
720 } | |
721 | |
722 if(vbi_get_props(priv,&tsp)!=TVI_CONTROL_TRUE) | |
723 return NULL; | |
724 | |
725 buf=malloc(tsp.bufsize); | |
726 seq=0; | |
727 prev_seq=0; | |
728 mp_msg(MSGT_TV,MSGL_V,"vbi: vbi capture thread started.\n"); | |
729 | |
730 while (!priv->vbi_shutdown){ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
731 bytes=read(priv->vbi_fd,buf,tsp.bufsize); |
24763
5d7f6e5e0847
After receiving EINTR 'read' syscall should be restarted.
voroshil
parents:
24553
diff
changeset
|
732 if(bytes<0 && errno==EINTR) |
5d7f6e5e0847
After receiving EINTR 'read' syscall should be restarted.
voroshil
parents:
24553
diff
changeset
|
733 continue; |
23901 | 734 if (bytes!=tsp.bufsize){ |
23979 | 735 mp_msg(MSGT_TV,MSGL_WARN,"vbi: expecting bytes: %d, got: %d\n",tsp.bufsize,bytes); |
23901 | 736 break; |
737 } | |
738 seq=*(int*)(buf+bytes-4); | |
739 if(seq<=1) continue; | |
740 if (prev_seq && seq!=prev_seq+1){ | |
741 prev_seq=0; | |
742 seq=0; | |
743 } | |
744 prev_seq=seq; | |
745 teletext_control(priv->priv_vbi,TV_VBI_CONTROL_DECODE_PAGE,&buf); | |
746 mp_msg(MSGT_TV,MSGL_DBG3,"grabber: seq:%d\n",seq); | |
747 } | |
748 free(buf); | |
749 return NULL; | |
750 } | |
751 | |
10536 | 752 static int control(priv_t *priv, int cmd, void *arg) |
753 { | |
754 struct v4l2_control control; | |
755 struct v4l2_frequency frequency; | |
756 | |
757 switch(cmd) { | |
758 case TVI_CONTROL_IS_VIDEO: | |
23151 | 759 return priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE? |
760 TVI_CONTROL_TRUE: TVI_CONTROL_FALSE; | |
17765
7bf483eaa99a
If we have a tuner, use that as a reason we have audio support, and do
aurel
parents:
17626
diff
changeset
|
761 case TVI_CONTROL_IS_AUDIO: |
23886 | 762 if (priv->tv_param->force_audio) return TVI_CONTROL_TRUE; |
10536 | 763 case TVI_CONTROL_IS_TUNER: |
23151 | 764 return priv->capability.capabilities & V4L2_CAP_TUNER? |
765 TVI_CONTROL_TRUE: TVI_CONTROL_FALSE; | |
10536 | 766 case TVI_CONTROL_IMMEDIATE: |
23151 | 767 priv->immediate_mode = 1; |
768 return TVI_CONTROL_TRUE; | |
10536 | 769 case TVI_CONTROL_VID_GET_FPS: |
32668
cb671a73f3a3
Use getfps helper function everywhere, simplifies code and avoids a possible division by 0.
reimar
parents:
32511
diff
changeset
|
770 *(float *)arg = getfps(priv); |
23151 | 771 mp_msg(MSGT_TV, MSGL_V, "%s: get fps: %f\n", info.short_name, |
772 *(float *)arg); | |
773 return TVI_CONTROL_TRUE; | |
10536 | 774 case TVI_CONTROL_VID_GET_BITS: |
23151 | 775 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
776 *(int *)arg = pixfmt2depth(priv->format.fmt.pix.pixelformat); | |
777 mp_msg(MSGT_TV, MSGL_V, "%s: get depth: %d\n", info.short_name, | |
778 *(int *)arg); | |
779 return TVI_CONTROL_TRUE; | |
10536 | 780 case TVI_CONTROL_VID_GET_FORMAT: |
23151 | 781 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
782 *(int *)arg = fcc_vl2mp(priv->format.fmt.pix.pixelformat); | |
783 mp_msg(MSGT_TV, MSGL_V, "%s: get format: %s\n", info.short_name, | |
784 pixfmt2name(priv->format.fmt.pix.pixelformat)); | |
785 return TVI_CONTROL_TRUE; | |
10536 | 786 case TVI_CONTROL_VID_SET_FORMAT: |
23151 | 787 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
788 priv->format.fmt.pix.pixelformat = fcc_mp2vl(*(int *)arg); | |
789 priv->format.fmt.pix.field = V4L2_FIELD_ANY; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
790 |
23151 | 791 priv->mp_format = *(int *)arg; |
792 mp_msg(MSGT_TV, MSGL_V, "%s: set format: %s\n", info.short_name, | |
793 pixfmt2name(priv->format.fmt.pix.pixelformat)); | |
794 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
795 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n", | |
796 info.short_name, strerror(errno)); | |
797 return TVI_CONTROL_FALSE; | |
798 } | |
799 /* according to the v4l2 specs VIDIOC_S_FMT should not fail, inflexible drivers | |
800 might even always return the default parameters -> update the format here*/ | |
801 priv->mp_format = fcc_vl2mp(priv->format.fmt.pix.pixelformat); | |
802 return TVI_CONTROL_TRUE; | |
10536 | 803 case TVI_CONTROL_VID_GET_WIDTH: |
23151 | 804 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
805 *(int *)arg = priv->format.fmt.pix.width; | |
806 mp_msg(MSGT_TV, MSGL_V, "%s: get width: %d\n", info.short_name, | |
807 *(int *)arg); | |
808 return TVI_CONTROL_TRUE; | |
10536 | 809 case TVI_CONTROL_VID_CHK_WIDTH: |
23151 | 810 return TVI_CONTROL_TRUE; |
28940
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
811 case TVI_CONTROL_VID_SET_WIDTH_HEIGHT: |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
812 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
813 priv->format.fmt.pix.width = ((int *)arg)[0]; |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
814 priv->format.fmt.pix.height = ((int *)arg)[1]; |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
815 priv->format.fmt.pix.field = V4L2_FIELD_ANY; |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
816 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
817 return TVI_CONTROL_FALSE; |
7406e7f30d4e
Add TVI_CONTROL_VID_SET_WIDTH_HEIGHT to set width and height together for v4l2,
reimar
parents:
28927
diff
changeset
|
818 return TVI_CONTROL_TRUE; |
10536 | 819 case TVI_CONTROL_VID_SET_WIDTH: |
23151 | 820 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
821 priv->format.fmt.pix.width = *(int *)arg; | |
822 mp_msg(MSGT_TV, MSGL_V, "%s: set width: %d\n", info.short_name, | |
823 *(int *)arg); | |
824 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
825 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set width failed: %s\n", | |
826 info.short_name, strerror(errno)); | |
827 return TVI_CONTROL_FALSE; | |
828 } | |
829 return TVI_CONTROL_TRUE; | |
10536 | 830 case TVI_CONTROL_VID_GET_HEIGHT: |
23151 | 831 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
832 *(int *)arg = priv->format.fmt.pix.height; | |
833 mp_msg(MSGT_TV, MSGL_V, "%s: get height: %d\n", info.short_name, | |
834 *(int *)arg); | |
835 return TVI_CONTROL_TRUE; | |
10536 | 836 case TVI_CONTROL_VID_CHK_HEIGHT: |
23151 | 837 return TVI_CONTROL_TRUE; |
10536 | 838 case TVI_CONTROL_VID_SET_HEIGHT: |
23151 | 839 if (getfmt(priv) < 0) return TVI_CONTROL_FALSE; |
840 priv->format.fmt.pix.height = *(int *)arg; | |
841 priv->format.fmt.pix.field = V4L2_FIELD_ANY; | |
842 mp_msg(MSGT_TV, MSGL_V, "%s: set height: %d\n", info.short_name, | |
843 *(int *)arg); | |
844 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
845 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set height failed: %s\n", | |
846 info.short_name, strerror(errno)); | |
847 return TVI_CONTROL_FALSE; | |
848 } | |
849 return TVI_CONTROL_TRUE; | |
850 case TVI_CONTROL_VID_GET_BRIGHTNESS: | |
851 control.id = V4L2_CID_BRIGHTNESS; | |
852 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
853 *(int *)arg = control.value; | |
854 return TVI_CONTROL_TRUE; | |
855 } | |
856 return TVI_CONTROL_FALSE; | |
857 case TVI_CONTROL_VID_SET_BRIGHTNESS: | |
858 control.id = V4L2_CID_BRIGHTNESS; | |
859 control.value = *(int *)arg; | |
860 return set_control(priv, &control, 1); | |
861 case TVI_CONTROL_VID_GET_HUE: | |
862 control.id = V4L2_CID_HUE; | |
863 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
864 *(int *)arg = control.value; | |
865 return TVI_CONTROL_TRUE; | |
866 } | |
867 return TVI_CONTROL_FALSE; | |
868 case TVI_CONTROL_VID_SET_HUE: | |
869 control.id = V4L2_CID_HUE; | |
870 control.value = *(int *)arg; | |
871 return set_control(priv, &control, 1); | |
872 case TVI_CONTROL_VID_GET_SATURATION: | |
873 control.id = V4L2_CID_SATURATION; | |
874 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
875 *(int *)arg = control.value; | |
876 return TVI_CONTROL_TRUE; | |
877 } | |
878 return TVI_CONTROL_FALSE; | |
879 case TVI_CONTROL_VID_SET_SATURATION: | |
880 control.id = V4L2_CID_SATURATION; | |
881 control.value = *(int *)arg; | |
882 return set_control(priv, &control, 1); | |
24553
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
883 case TVI_CONTROL_VID_GET_GAIN: |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
884 { |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
885 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
886 control.id = V4L2_CID_AUTOGAIN; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
887 if(get_control(priv,&control,0)!=TVI_CONTROL_TRUE) |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
888 return TVI_CONTROL_FALSE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
889 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
890 if(control.value){ //Auto Gain control is enabled |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
891 *(int*)arg=0; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
892 return TVI_CONTROL_TRUE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
893 } |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
894 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
895 //Manual Gain control |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
896 control.id = V4L2_CID_GAIN; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
897 if(get_control(priv,&control,0)!=TVI_CONTROL_TRUE) |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
898 return TVI_CONTROL_FALSE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
899 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
900 *(int*)arg=control.value?control.value:1; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
901 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
902 return TVI_CONTROL_TRUE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
903 } |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
904 case TVI_CONTROL_VID_SET_GAIN: |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
905 { |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
906 //value==0 means automatic gain control |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
907 int value=*(int*)arg; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
908 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
909 if (value < 0 || value>100) |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
910 return TVI_CONTROL_FALSE; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
911 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
912 control.id=value?V4L2_CID_GAIN:V4L2_CID_AUTOGAIN; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
913 control.value=value?value:1; |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
914 |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
915 return set_control(priv,&control,0); |
d6bba2781d01
Implement setting gain control for video devices (usually webcams)
voroshil
parents:
24346
diff
changeset
|
916 } |
23151 | 917 case TVI_CONTROL_VID_GET_CONTRAST: |
918 control.id = V4L2_CID_CONTRAST; | |
919 if (get_control(priv, &control, 1) == TVI_CONTROL_TRUE) { | |
920 *(int *)arg = control.value; | |
921 return TVI_CONTROL_TRUE; | |
922 } | |
923 return TVI_CONTROL_FALSE; | |
924 case TVI_CONTROL_VID_SET_CONTRAST: | |
925 control.id = V4L2_CID_CONTRAST; | |
926 control.value = *(int *)arg; | |
927 return set_control(priv, &control, 1); | |
10536 | 928 case TVI_CONTROL_TUN_GET_FREQ: |
23151 | 929 frequency.tuner = 0; |
930 frequency.type = V4L2_TUNER_ANALOG_TV; | |
931 if (ioctl(priv->video_fd, VIDIOC_G_FREQUENCY, &frequency) < 0) { | |
932 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl get frequency failed: %s\n", | |
933 info.short_name, strerror(errno)); | |
934 return TVI_CONTROL_FALSE; | |
935 } | |
936 *(int *)arg = frequency.frequency; | |
937 return TVI_CONTROL_TRUE; | |
10536 | 938 case TVI_CONTROL_TUN_SET_FREQ: |
939 #if 0 | |
23151 | 940 set_mute(priv, 1); |
941 usleep(100000); // wait to suppress noise during switching | |
10536 | 942 #endif |
23151 | 943 frequency.tuner = 0; |
944 frequency.type = V4L2_TUNER_ANALOG_TV; | |
945 frequency.frequency = *(int *)arg; | |
946 if (ioctl(priv->video_fd, VIDIOC_S_FREQUENCY, &frequency) < 0) { | |
947 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl set frequency failed: %s\n", | |
948 info.short_name, strerror(errno)); | |
949 return TVI_CONTROL_FALSE; | |
950 } | |
10536 | 951 #if 0 |
23151 | 952 usleep(100000); // wait to suppress noise during switching |
953 set_mute(priv, 0); | |
10536 | 954 #endif |
23151 | 955 return TVI_CONTROL_TRUE; |
10536 | 956 case TVI_CONTROL_TUN_GET_TUNER: |
23151 | 957 mp_msg(MSGT_TV, MSGL_V, "%s: get tuner\n",info.short_name); |
958 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { | |
959 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", | |
960 info.short_name, strerror(errno)); | |
961 return TVI_CONTROL_FALSE; | |
962 } | |
963 return TVI_CONTROL_TRUE; | |
10536 | 964 case TVI_CONTROL_TUN_SET_TUNER: |
23151 | 965 mp_msg(MSGT_TV, MSGL_V, "%s: set tuner\n",info.short_name); |
966 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) { | |
967 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n", | |
968 info.short_name, strerror(errno)); | |
969 return TVI_CONTROL_FALSE; | |
970 } | |
971 return TVI_CONTROL_TRUE; | |
10536 | 972 case TVI_CONTROL_TUN_GET_NORM: |
23151 | 973 *(int *)arg = priv->standard.index; |
974 return TVI_CONTROL_TRUE; | |
24105
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
975 case TVI_CONTROL_TUN_GET_SIGNAL: |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
976 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
977 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
978 info.short_name, strerror(errno)); |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
979 return TVI_CONTROL_FALSE; |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
980 } |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
981 *(int*)arg=100*(priv->tuner.signal>>8)/255; |
9e71e0345c35
Automatic TV channels scanning ability for MPlayer.
voroshil
parents:
23979
diff
changeset
|
982 return TVI_CONTROL_TRUE; |
10536 | 983 case TVI_CONTROL_TUN_SET_NORM: |
23151 | 984 priv->standard.index = *(int *)arg; |
985 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) { | |
986 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum norm failed: %s\n", | |
987 info.short_name, strerror(errno)); | |
988 return TVI_CONTROL_FALSE; | |
989 } | |
990 mp_msg(MSGT_TV, MSGL_V, "%s: set norm: %s\n", info.short_name, priv->standard.name); | |
991 if (ioctl(priv->video_fd, VIDIOC_S_STD, &priv->standard.id) < 0) { | |
992 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set norm failed: %s\n", | |
993 info.short_name, strerror(errno)); | |
994 return TVI_CONTROL_FALSE; | |
995 } | |
996 return TVI_CONTROL_TRUE; | |
13978 | 997 case TVI_CONTROL_SPC_GET_NORMID: |
23151 | 998 { |
999 int i; | |
1000 for (i = 0;; i++) { | |
1001 struct v4l2_standard standard; | |
1002 memset(&standard, 0, sizeof(standard)); | |
1003 standard.index = i; | |
1004 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard)) | |
1005 return TVI_CONTROL_FALSE; | |
1006 if (!strcasecmp(standard.name, (char *)arg)) { | |
1007 *(int *)arg = i; | |
1008 return TVI_CONTROL_TRUE; | |
1009 } | |
1010 } | |
1011 return TVI_CONTROL_FALSE; | |
1012 } | |
10536 | 1013 case TVI_CONTROL_SPC_GET_INPUT: |
23151 | 1014 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, (int *)arg) < 0) { |
1015 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n", | |
1016 info.short_name, strerror(errno)); | |
1017 return TVI_CONTROL_FALSE; | |
1018 } | |
1019 return TVI_CONTROL_TRUE; | |
10536 | 1020 case TVI_CONTROL_SPC_SET_INPUT: |
23151 | 1021 mp_msg(MSGT_TV, MSGL_V, "%s: set input: %d\n", info.short_name, *(int *)arg); |
1022 priv->input.index = *(int *)arg; | |
1023 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &priv->input) < 0) { | |
1024 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl enum input failed: %s\n", | |
1025 info.short_name, strerror(errno)); | |
1026 return TVI_CONTROL_FALSE; | |
1027 } | |
1028 if (ioctl(priv->video_fd, VIDIOC_S_INPUT, (int *)arg) < 0) { | |
1029 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set input failed: %s\n", | |
1030 info.short_name, strerror(errno)); | |
1031 return TVI_CONTROL_FALSE; | |
1032 } | |
1033 return TVI_CONTROL_TRUE; | |
10536 | 1034 case TVI_CONTROL_AUD_GET_FORMAT: |
23151 | 1035 init_audio(priv); |
25962 | 1036 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 1037 *(int *)arg = AF_FORMAT_S16_LE; |
1038 mp_msg(MSGT_TV, MSGL_V, "%s: get audio format: %d\n", | |
1039 info.short_name, *(int *)arg); | |
1040 return TVI_CONTROL_TRUE; | |
10536 | 1041 case TVI_CONTROL_AUD_GET_SAMPLERATE: |
23151 | 1042 init_audio(priv); |
25962 | 1043 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 1044 *(int *)arg = priv->audio_in.samplerate; |
1045 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplerate: %d\n", | |
1046 info.short_name, *(int *)arg); | |
1047 return TVI_CONTROL_TRUE; | |
10536 | 1048 case TVI_CONTROL_AUD_GET_SAMPLESIZE: |
23151 | 1049 init_audio(priv); |
25962 | 1050 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 1051 *(int *)arg = priv->audio_in.bytes_per_sample; |
1052 mp_msg(MSGT_TV, MSGL_V, "%s: get audio samplesize: %d\n", | |
1053 info.short_name, *(int *)arg); | |
1054 return TVI_CONTROL_TRUE; | |
10536 | 1055 case TVI_CONTROL_AUD_GET_CHANNELS: |
23151 | 1056 init_audio(priv); |
25962 | 1057 if (!priv->audio_initialized) return TVI_CONTROL_FALSE; |
23151 | 1058 *(int *)arg = priv->audio_in.channels; |
1059 mp_msg(MSGT_TV, MSGL_V, "%s: get audio channels: %d\n", | |
1060 info.short_name, *(int *)arg); | |
1061 return TVI_CONTROL_TRUE; | |
10536 | 1062 case TVI_CONTROL_AUD_SET_SAMPLERATE: |
23151 | 1063 init_audio(priv); |
1064 mp_msg(MSGT_TV, MSGL_V, "%s: set audio samplerate: %d\n", | |
1065 info.short_name, *(int *)arg); | |
1066 if (audio_in_set_samplerate(&priv->audio_in, *(int*)arg) < 0) return TVI_CONTROL_FALSE; | |
1067 // setup_audio_buffer_sizes(priv); | |
1068 return TVI_CONTROL_TRUE; | |
23901 | 1069 case TVI_CONTROL_VBI_INIT: |
1070 { | |
1071 void* ptr; | |
1072 tt_stream_props tsp; | |
1073 | |
1074 if (vbi_init(priv,*(char**)arg)!=TVI_CONTROL_TRUE) | |
1075 return TVI_CONTROL_FALSE; | |
1076 if(vbi_get_props(priv,&tsp)==TVI_CONTROL_TRUE) | |
1077 { | |
1078 ptr=&tsp; | |
29760
1cc8a20520e8
Add MSGT_TELETEXT, rename TVI_CONTROL as VBI_CONTROL and fix some paths
cehoyos
parents:
29759
diff
changeset
|
1079 if(teletext_control(NULL,TV_VBI_CONTROL_START,&ptr)==VBI_CONTROL_TRUE) |
23901 | 1080 priv->priv_vbi=ptr; |
1081 else | |
1082 priv->priv_vbi=NULL; | |
1083 } | |
1084 return TVI_CONTROL_TRUE; | |
1085 } | |
29806 | 1086 case TVI_CONTROL_GET_VBI_PTR: |
1087 *(void **)arg=priv->priv_vbi; | |
1088 return TVI_CONTROL_TRUE; | |
10536 | 1089 } |
1090 mp_msg(MSGT_TV, MSGL_V, "%s: unknown control: %d\n", info.short_name, cmd); | |
26756
c43ce7268677
cosmetics: Remove useless parentheses from return statements.
diego
parents:
25962
diff
changeset
|
1091 return TVI_CONTROL_UNKNOWN; |
10536 | 1092 } |
1093 | |
1094 | |
1095 #define PRIV ((priv_t *) (tvi_handle->priv)) | |
1096 | |
1097 /* handler creator - entry point ! */ | |
23883 | 1098 static tvi_handle_t *tvi_init_v4l2(tv_param_t* tv_param) |
10536 | 1099 { |
1100 tvi_handle_t *tvi_handle; | |
1101 | |
32141
2802b8095bf7
Move TV input new_handle static function to tv.c and make it non-static.
diego
parents:
32090
diff
changeset
|
1102 tvi_handle = tv_new_handle(sizeof(priv_t), &functions); |
10536 | 1103 if (!tvi_handle) { |
23151 | 1104 return NULL; |
10536 | 1105 } |
1106 PRIV->video_fd = -1; | |
1107 | |
23883 | 1108 PRIV->video_dev = strdup(tv_param->device? tv_param->device: "/dev/video0"); |
10536 | 1109 if (!PRIV->video_dev) { |
32090
535ebcd085e4
Move TV input free_handle static function to tv.c and make it non-static.
diego
parents:
31834
diff
changeset
|
1110 tv_free_handle(tvi_handle); |
23151 | 1111 return NULL; |
10536 | 1112 } |
1113 | |
23883 | 1114 if (tv_param->adevice) { |
1115 PRIV->audio_dev = strdup(tv_param->adevice); | |
23151 | 1116 if (!PRIV->audio_dev) { |
1117 free(PRIV->video_dev); | |
32090
535ebcd085e4
Move TV input free_handle static function to tv.c and make it non-static.
diego
parents:
31834
diff
changeset
|
1118 tv_free_handle(tvi_handle); |
23151 | 1119 return NULL; |
1120 } | |
10536 | 1121 } |
1122 | |
23883 | 1123 PRIV->tv_param=tv_param; |
10536 | 1124 return tvi_handle; |
1125 } | |
1126 | |
1127 #undef PRIV | |
1128 | |
1129 | |
1130 static int uninit(priv_t *priv) | |
1131 { | |
1132 int i, frames, dropped = 0; | |
1133 | |
23901 | 1134 priv->vbi_shutdown=1; |
1135 if(priv->vbi_grabber_thread) | |
1136 pthread_join(priv->vbi_grabber_thread, NULL); | |
1137 | |
1138 teletext_control(priv->priv_vbi,TV_VBI_CONTROL_STOP,(void*)1); | |
1139 priv->priv_vbi=NULL; | |
1140 | |
1141 if(priv->vbi_fd){ | |
1142 close(priv->vbi_fd); | |
1143 priv->vbi_fd=0; | |
1144 } | |
1145 | |
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32352
diff
changeset
|
1146 free(priv->vbi_dev); |
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32352
diff
changeset
|
1147 priv->vbi_dev = NULL; |
10536 | 1148 priv->shutdown = 1; |
16185 | 1149 if(priv->video_grabber_thread) |
23151 | 1150 pthread_join(priv->video_grabber_thread, NULL); |
10536 | 1151 pthread_mutex_destroy(&priv->video_buffer_mutex); |
1152 | |
1153 if (priv->streamon) { | |
23151 | 1154 struct v4l2_buffer buf; |
10536 | 1155 |
23151 | 1156 /* get performance */ |
28927
8f7c8a1cb8b7
100l fix calculation of dropped frames, number of frames is time * fps, not time / fps.
reimar
parents:
28106
diff
changeset
|
1157 frames = 1 + lrintf((double)(priv->curr_frame - priv->first_frame) / 1e6 * getfps(priv)); |
23151 | 1158 dropped = frames - priv->frames; |
10536 | 1159 |
23151 | 1160 /* turn off streaming */ |
1161 if (ioctl(priv->video_fd, VIDIOC_STREAMOFF, &(priv->map[0].buf.type)) < 0) { | |
1162 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamoff failed: %s\n", | |
1163 info.short_name, strerror(errno)); | |
1164 } | |
1165 priv->streamon = 0; | |
10536 | 1166 |
23151 | 1167 /* unqueue all remaining buffers */ |
1168 memset(&buf,0,sizeof(buf)); | |
1169 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1170 buf.memory = V4L2_MEMORY_MMAP; | |
1171 while (!ioctl(priv->video_fd, VIDIOC_DQBUF, &buf)); | |
10536 | 1172 } |
1173 | |
1174 /* unmap all buffers */ | |
1175 for (i = 0; i < priv->mapcount; i++) { | |
23151 | 1176 if (munmap(priv->map[i].addr, priv->map[i].len) < 0) { |
1177 mp_msg(MSGT_TV, MSGL_ERR, "%s: munmap capture buffer failed: %s\n", | |
1178 info.short_name, strerror(errno)); | |
1179 } | |
10536 | 1180 } |
1181 | |
1182 /* stop audio thread */ | |
23886 | 1183 if (!priv->tv_param->noaudio && priv->audio_grabber_thread) { |
23151 | 1184 pthread_join(priv->audio_grabber_thread, NULL); |
1185 pthread_mutex_destroy(&priv->skew_mutex); | |
1186 pthread_mutex_destroy(&priv->audio_mutex); | |
10536 | 1187 } |
1188 | |
17626
5627625b0cdb
Don't test the v4l2_input audioset field for audio capabilities but still try changing the mute setting (patch by Jesse Allen < the3dfxdude _at_ gmail.com >)
aurel
parents:
17199
diff
changeset
|
1189 set_mute(priv, 1); |
10536 | 1190 |
1191 /* free memory and close device */ | |
23151 | 1192 free(priv->map); priv->map = NULL; |
10536 | 1193 priv->mapcount = 0; |
23151 | 1194 if(priv->video_fd!=-1)close(priv->video_fd); priv->video_fd = -1; |
1195 free(priv->video_dev); priv->video_dev = NULL; | |
10536 | 1196 |
1197 if (priv->video_ringbuffer) { | |
23151 | 1198 int i; |
1199 for (i = 0; i < priv->video_buffer_size_current; i++) { | |
23423 | 1200 free(priv->video_ringbuffer[i].data); |
23151 | 1201 } |
1202 free(priv->video_ringbuffer); | |
10536 | 1203 } |
23886 | 1204 if (!priv->tv_param->noaudio) { |
32511
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32352
diff
changeset
|
1205 free(priv->audio_ringbuffer); |
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32352
diff
changeset
|
1206 free(priv->audio_skew_buffer); |
b39155e98ac3
Remove some useless NULL pointer checks before invoking free() on the pointer.
diego
parents:
32352
diff
changeset
|
1207 free(priv->audio_skew_delta_buffer); |
25005
1635b2b31bdd
Add missing call to audio_in_uninit in v4l2 tv driver.
voroshil
parents:
24763
diff
changeset
|
1208 |
1635b2b31bdd
Add missing call to audio_in_uninit in v4l2 tv driver.
voroshil
parents:
24763
diff
changeset
|
1209 audio_in_uninit(&priv->audio_in); |
10536 | 1210 } |
1211 | |
1212 /* show some nice statistics ;-) */ | |
1213 mp_msg(MSGT_TV, MSGL_INFO, | |
23151 | 1214 "%s: %d frames successfully processed, %d frames dropped.\n", |
1215 info.short_name, priv->frames, dropped); | |
10536 | 1216 mp_msg(MSGT_TV, MSGL_V, "%s: up to %u video frames buffered.\n", |
23151 | 1217 info.short_name, priv->video_buffer_size_current); |
10536 | 1218 return 1; |
1219 } | |
1220 | |
1221 | |
1222 /* initialisation */ | |
1223 static int init(priv_t *priv) | |
1224 { | |
1225 int i; | |
1226 | |
1227 priv->audio_ringbuffer = NULL; | |
1228 priv->audio_skew_buffer = NULL; | |
10653 | 1229 priv->audio_skew_delta_buffer = NULL; |
10536 | 1230 |
25962 | 1231 priv->audio_initialized = 0; |
15464 | 1232 |
10536 | 1233 /* Open the video device. */ |
1234 priv->video_fd = open(priv->video_dev, O_RDWR); | |
1235 if (priv->video_fd < 0) { | |
23151 | 1236 mp_msg(MSGT_TV, MSGL_ERR, "%s: unable to open '%s': %s\n", |
1237 info.short_name, priv->video_dev, strerror(errno)); | |
1238 uninit(priv); | |
1239 return 0; | |
10536 | 1240 } |
1241 mp_msg(MSGT_TV, MSGL_DBG2, "%s: video fd: %s: %d\n", | |
23151 | 1242 info.short_name, priv->video_dev, priv->video_fd); |
10536 | 1243 |
1244 /* | |
1245 ** Query the video capabilities and current settings | |
1246 ** for further control calls. | |
1247 */ | |
1248 if (ioctl(priv->video_fd, VIDIOC_QUERYCAP, &priv->capability) < 0) { | |
23151 | 1249 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query capabilities failed: %s\n", |
1250 info.short_name, strerror(errno)); | |
1251 uninit(priv); | |
1252 return 0; | |
10536 | 1253 } |
1254 | |
1255 if (!(priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE)) | |
1256 { | |
23151 | 1257 mp_msg(MSGT_TV, MSGL_ERR, "Device %s is not a video capture device.\n", |
1258 priv->video_dev); | |
1259 return 0; | |
10536 | 1260 } |
1261 | |
18073
8ceb31f028ee
make failures during e.g. setting the TV norm non-fatal.
reimar
parents:
17765
diff
changeset
|
1262 if (getfmt(priv) < 0) { |
23151 | 1263 uninit(priv); |
1264 return 0; | |
10536 | 1265 } |
18073
8ceb31f028ee
make failures during e.g. setting the TV norm non-fatal.
reimar
parents:
17765
diff
changeset
|
1266 getstd(priv); |
10536 | 1267 /* |
1268 ** if this device has got a tuner query it's settings | |
1269 ** otherwise set some nice defaults | |
1270 */ | |
1271 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1272 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) < 0) { |
1273 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get tuner failed: %s\n", | |
1274 info.short_name, strerror(errno)); | |
1275 uninit(priv); | |
1276 return 0; | |
1277 } | |
10536 | 1278 } |
1279 mp_msg(MSGT_TV, MSGL_INFO, "Selected device: %s\n", priv->capability.card); | |
1280 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1281 mp_msg(MSGT_TV, MSGL_INFO, " Tuner cap:%s%s%s\n", |
1282 (priv->tuner.capability & V4L2_TUNER_CAP_STEREO) ? " STEREO" : "", | |
1283 (priv->tuner.capability & V4L2_TUNER_CAP_LANG1) ? " LANG1" : "", | |
1284 (priv->tuner.capability & V4L2_TUNER_CAP_LANG2) ? " LANG2" : ""); | |
1285 mp_msg(MSGT_TV, MSGL_INFO, " Tuner rxs:%s%s%s%s\n", | |
1286 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_MONO) ? " MONO" : "", | |
1287 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_STEREO) ? " STEREO" : "", | |
1288 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG1) ? " LANG1" : "", | |
1289 (priv->tuner.rxsubchans & V4L2_TUNER_SUB_LANG2) ? " LANG2" : ""); | |
10536 | 1290 } |
31834
64ba1daa147a
various spelling fixes, found by the Debian QA tool 'lintian'
siretart
parents:
31322
diff
changeset
|
1291 mp_msg(MSGT_TV, MSGL_INFO, " Capabilities:%s%s%s%s%s%s%s%s%s%s%s\n", |
23151 | 1292 priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE? |
1293 " video capture": "", | |
1294 priv->capability.capabilities & V4L2_CAP_VIDEO_OUTPUT? | |
1295 " video output": "", | |
1296 priv->capability.capabilities & V4L2_CAP_VIDEO_OVERLAY? | |
1297 " video overlay": "", | |
1298 priv->capability.capabilities & V4L2_CAP_VBI_CAPTURE? | |
1299 " VBI capture device": "", | |
1300 priv->capability.capabilities & V4L2_CAP_VBI_OUTPUT? | |
1301 " VBI output": "", | |
1302 priv->capability.capabilities & V4L2_CAP_RDS_CAPTURE? | |
1303 " RDS data capture": "", | |
1304 priv->capability.capabilities & V4L2_CAP_TUNER? | |
1305 " tuner": "", | |
1306 priv->capability.capabilities & V4L2_CAP_AUDIO? | |
1307 " audio": "", | |
1308 priv->capability.capabilities & V4L2_CAP_READWRITE? | |
1309 " read/write": "", | |
1310 priv->capability.capabilities & V4L2_CAP_ASYNCIO? | |
1311 " async i/o": "", | |
1312 priv->capability.capabilities & V4L2_CAP_STREAMING? | |
1313 " streaming": ""); | |
10536 | 1314 mp_msg(MSGT_TV, MSGL_INFO, " supported norms:"); |
1315 for (i = 0;; i++) { | |
23151 | 1316 struct v4l2_standard standard; |
1317 memset(&standard, 0, sizeof(standard)); | |
1318 standard.index = i; | |
1319 if (-1 == ioctl(priv->video_fd, VIDIOC_ENUMSTD, &standard)) | |
1320 break; | |
1321 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, standard.name); | |
10536 | 1322 } |
1323 mp_msg(MSGT_TV, MSGL_INFO, "\n inputs:"); | |
1324 for (i = 0; 1; i++) { | |
23151 | 1325 struct v4l2_input input; |
10536 | 1326 |
23151 | 1327 input.index = i; |
1328 if (ioctl(priv->video_fd, VIDIOC_ENUMINPUT, &input) < 0) { | |
1329 break; | |
1330 } | |
1331 mp_msg(MSGT_TV, MSGL_INFO, " %d = %s;", i, input.name); | |
10536 | 1332 } |
29121
bae666342ba9
Make tvi_v4l2 print -1 as "Current input" if the ioctl to read it failed.
reimar
parents:
28940
diff
changeset
|
1333 i = -1; |
10536 | 1334 if (ioctl(priv->video_fd, VIDIOC_G_INPUT, &i) < 0) { |
23151 | 1335 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get input failed: %s\n", |
1336 info.short_name, strerror(errno)); | |
10536 | 1337 } |
1338 mp_msg(MSGT_TV, MSGL_INFO, "\n Current input: %d\n", i); | |
1339 for (i = 0; ; i++) { | |
23151 | 1340 struct v4l2_fmtdesc fmtdesc; |
10536 | 1341 |
23151 | 1342 fmtdesc.index = i; |
1343 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1344 if (ioctl(priv->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) { | |
1345 break; | |
1346 } | |
1347 mp_msg(MSGT_TV, MSGL_V, " Format %-6s (%2d bits, %s): %s\n", | |
1348 pixfmt2name(fmtdesc.pixelformat), pixfmt2depth(fmtdesc.pixelformat), | |
1349 fmtdesc.description, vo_format_name(fcc_vl2mp(fmtdesc.pixelformat))); | |
10536 | 1350 } |
1351 mp_msg(MSGT_TV, MSGL_INFO, " Current format: %s\n", | |
23151 | 1352 pixfmt2name(priv->format.fmt.pix.pixelformat)); |
10536 | 1353 |
1354 /* set some nice defaults */ | |
1355 if (getfmt(priv) < 0) return 0; | |
1356 priv->format.fmt.pix.width = 640; | |
1357 priv->format.fmt.pix.height = 480; | |
1358 if (ioctl(priv->video_fd, VIDIOC_S_FMT, &priv->format) < 0) { | |
23151 | 1359 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set format failed: %s\n", |
1360 info.short_name, strerror(errno)); | |
1361 uninit(priv); | |
1362 return 0; | |
10536 | 1363 } |
1364 | |
23886 | 1365 // if (!(priv->capability.capabilities & V4L2_CAP_AUDIO) && !priv->tv_param->force_audio) priv->tv_param->noaudio = 1; |
10536 | 1366 |
1367 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
23151 | 1368 struct v4l2_control control; |
23886 | 1369 if (priv->tv_param->amode >= 0) { |
23151 | 1370 mp_msg(MSGT_TV, MSGL_V, "%s: setting audio mode\n", info.short_name); |
23886 | 1371 priv->tuner.audmode = amode2v4l(priv->tv_param->amode); |
23151 | 1372 if (ioctl(priv->video_fd, VIDIOC_S_TUNER, &priv->tuner) < 0) { |
1373 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl set tuner failed: %s\n", | |
1374 info.short_name, strerror(errno)); | |
1375 return TVI_CONTROL_FALSE; | |
1376 } | |
1377 } | |
1378 mp_msg(MSGT_TV, MSGL_INFO, "%s: current audio mode is :%s%s%s%s\n", info.short_name, | |
1379 (priv->tuner.audmode == V4L2_TUNER_MODE_MONO) ? " MONO" : "", | |
1380 (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) ? " STEREO" : "", | |
1381 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG1) ? " LANG1" : "", | |
1382 (priv->tuner.audmode == V4L2_TUNER_MODE_LANG2) ? " LANG2" : ""); | |
10536 | 1383 |
23886 | 1384 if (priv->tv_param->volume >= 0) { |
23151 | 1385 control.id = V4L2_CID_AUDIO_VOLUME; |
23886 | 1386 control.value = priv->tv_param->volume; |
23151 | 1387 set_control(priv, &control, 0); |
1388 } | |
23886 | 1389 if (priv->tv_param->bass >= 0) { |
23151 | 1390 control.id = V4L2_CID_AUDIO_BASS; |
23886 | 1391 control.value = priv->tv_param->bass; |
23151 | 1392 set_control(priv, &control, 0); |
1393 } | |
23886 | 1394 if (priv->tv_param->treble >= 0) { |
23151 | 1395 control.id = V4L2_CID_AUDIO_TREBLE; |
23886 | 1396 control.value = priv->tv_param->treble; |
23151 | 1397 set_control(priv, &control, 0); |
1398 } | |
23886 | 1399 if (priv->tv_param->balance >= 0) { |
23151 | 1400 control.id = V4L2_CID_AUDIO_BALANCE; |
23886 | 1401 control.value = priv->tv_param->balance; |
23151 | 1402 set_control(priv, &control, 0); |
1403 } | |
10536 | 1404 } |
1405 | |
1406 return 1; | |
1407 } | |
1408 | |
1409 static int get_capture_buffer_size(priv_t *priv) | |
1410 { | |
33644
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1411 uint64_t bufsize; |
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1412 int cnt; |
10536 | 1413 |
23886 | 1414 if (priv->tv_param->buffer_size >= 0) { |
1415 bufsize = priv->tv_param->buffer_size*1024*1024; | |
10536 | 1416 } else { |
1417 #ifdef HAVE_SYS_SYSINFO_H | |
23151 | 1418 struct sysinfo si; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1419 |
23151 | 1420 sysinfo(&si); |
33644
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1421 bufsize = (si.freeram/2)*si.mem_unit; |
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1422 if ( bufsize < 16*1024*1024) |
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1423 #endif |
23151 | 1424 bufsize = 16*1024*1024; |
10536 | 1425 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1426 |
23423 | 1427 cnt = bufsize/priv->format.fmt.pix.sizeimage; |
10536 | 1428 if (cnt < 2) cnt = 2; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1429 |
10536 | 1430 return cnt; |
1431 } | |
1432 | |
1433 /* that's the real start, we'got the format parameters (checked with control) */ | |
1434 static int start(priv_t *priv) | |
1435 { | |
1436 struct v4l2_requestbuffers request; | |
24346 | 1437 unsigned int i; |
10536 | 1438 |
1439 /* setup audio parameters */ | |
1440 | |
15464 | 1441 init_audio(priv); |
25962 | 1442 if (!priv->tv_param->noaudio && !priv->audio_initialized) return 0; |
15464 | 1443 |
10536 | 1444 /* we need this to size the audio buffer properly */ |
1445 if (priv->immediate_mode) { | |
23151 | 1446 priv->video_buffer_size_max = 2; |
10536 | 1447 } else { |
23151 | 1448 priv->video_buffer_size_max = get_capture_buffer_size(priv); |
10536 | 1449 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1450 |
23886 | 1451 if (!priv->tv_param->noaudio) { |
23151 | 1452 setup_audio_buffer_sizes(priv); |
1453 priv->audio_skew_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1454 if (!priv->audio_skew_buffer) { | |
1455 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1456 return 0; | |
1457 } | |
1458 priv->audio_skew_delta_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1459 if (!priv->audio_skew_delta_buffer) { | |
1460 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1461 return 0; | |
1462 } | |
10536 | 1463 |
23151 | 1464 priv->audio_ringbuffer = calloc(priv->audio_in.blocksize, priv->audio_buffer_size); |
1465 if (!priv->audio_ringbuffer) { | |
1466 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno)); | |
1467 return 0; | |
1468 } | |
10536 | 1469 |
23151 | 1470 priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate |
1471 *priv->audio_in.channels | |
1472 *priv->audio_in.bytes_per_sample); | |
1473 priv->audio_usecs_per_block = 1e6*priv->audio_secs_per_block; | |
1474 priv->audio_head = 0; | |
1475 priv->audio_tail = 0; | |
1476 priv->audio_cnt = 0; | |
1477 priv->audio_drop = 0; | |
1478 priv->audio_skew = 0; | |
1479 priv->audio_skew_total = 0; | |
1480 priv->audio_skew_delta_total = 0; | |
1481 priv->audio_recv_blocks_total = 0; | |
1482 priv->audio_sent_blocks_total = 0; | |
1483 priv->audio_null_blocks_inserted = 0; | |
1484 priv->audio_insert_null_samples = 0; | |
1485 priv->dropped_frames_timeshift = 0; | |
1486 priv->dropped_frames_compensated = 0; | |
15449 | 1487 |
23151 | 1488 pthread_mutex_init(&priv->skew_mutex, NULL); |
1489 pthread_mutex_init(&priv->audio_mutex, NULL); | |
10536 | 1490 } |
1491 | |
1492 /* setup video parameters */ | |
23886 | 1493 if (!priv->tv_param->noaudio) { |
32668
cb671a73f3a3
Use getfps helper function everywhere, simplifies code and avoids a possible division by 0.
reimar
parents:
32511
diff
changeset
|
1494 if (priv->video_buffer_size_max < 3*getfps(priv)*priv->audio_secs_per_block) { |
23151 | 1495 mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n" |
1496 "You will probably experience heavy framedrops.\n"); | |
1497 } | |
10536 | 1498 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1499 |
10536 | 1500 { |
23151 | 1501 int bytesperline = priv->format.fmt.pix.width*pixfmt2depth(priv->format.fmt.pix.pixelformat)/8; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1502 |
23151 | 1503 mp_msg(MSGT_TV, MSGL_V, "Using a ring buffer for maximum %d frames, %d MB total size.\n", |
1504 priv->video_buffer_size_max, | |
1505 priv->video_buffer_size_max*priv->format.fmt.pix.height*bytesperline/(1024*1024)); | |
10536 | 1506 } |
1507 | |
23423 | 1508 priv->video_ringbuffer = calloc(priv->video_buffer_size_max, sizeof(video_buffer_entry)); |
10536 | 1509 if (!priv->video_ringbuffer) { |
23151 | 1510 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno)); |
1511 return 0; | |
10536 | 1512 } |
23423 | 1513 memset(priv->video_ringbuffer,0,priv->video_buffer_size_max * sizeof(video_buffer_entry)); |
10536 | 1514 |
15449 | 1515 pthread_mutex_init(&priv->video_buffer_mutex, NULL); |
1516 | |
10536 | 1517 priv->video_head = 0; |
1518 priv->video_tail = 0; | |
1519 priv->video_cnt = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1520 |
10536 | 1521 /* request buffers */ |
1522 if (priv->immediate_mode) { | |
23151 | 1523 request.count = 2; |
10536 | 1524 } else { |
23151 | 1525 request.count = BUFFER_COUNT; |
10536 | 1526 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1527 |
10536 | 1528 request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1529 request.memory = V4L2_MEMORY_MMAP; | |
1530 if (ioctl(priv->video_fd, VIDIOC_REQBUFS, &request) < 0) { | |
23151 | 1531 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl request buffers failed: %s\n", |
1532 info.short_name, strerror(errno)); | |
1533 return 0; | |
10536 | 1534 } |
1535 | |
1536 /* query buffers */ | |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18176
diff
changeset
|
1537 if (!(priv->map = calloc(request.count, sizeof(struct map)))) { |
23151 | 1538 mp_msg(MSGT_TV, MSGL_ERR, "%s: malloc capture buffers failed: %s\n", |
1539 info.short_name, strerror(errno)); | |
1540 return 0; | |
10536 | 1541 } |
1542 | |
1543 /* map and queue buffers */ | |
1544 for (i = 0; i < request.count; i++) { | |
23151 | 1545 memset(&priv->map[i].buf,0,sizeof(priv->map[i].buf)); |
1546 priv->map[i].buf.index = i; | |
1547 priv->map[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1548 priv->map[i].buf.memory = V4L2_MEMORY_MMAP; | |
1549 if (ioctl(priv->video_fd, VIDIOC_QUERYBUF, &(priv->map[i].buf)) < 0) { | |
1550 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s\n", | |
1551 info.short_name, strerror(errno)); | |
1552 free(priv->map); | |
1553 priv->map = NULL; | |
1554 return 0; | |
1555 } | |
1556 priv->map[i].addr = mmap (0, priv->map[i].buf.length, PROT_READ | | |
1557 PROT_WRITE, MAP_SHARED, priv->video_fd, priv->map[i].buf.m.offset); | |
1558 if (priv->map[i].addr == MAP_FAILED) { | |
1559 mp_msg(MSGT_TV, MSGL_ERR, "%s: mmap capture buffer failed: %s\n", | |
1560 info.short_name, strerror(errno)); | |
1561 priv->map[i].len = 0; | |
1562 return 0; | |
1563 } | |
1564 priv->map[i].len = priv->map[i].buf.length; | |
37127 | 1565 #ifdef HAVE_CLOCK_GETTIME |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1566 priv->clk_id = (priv->map[i].buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1567 ? CLOCK_MONOTONIC : CLOCK_REALTIME; |
37127 | 1568 #else |
1569 if (priv->map[i].buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) | |
1570 mp_msg(MSGT_TV, MSGL_WARN, "MPlayer compiled without clock_gettime() that is needed to handle monotone video timestamps from the kernel. Expect desync.\n"); | |
1571 #endif | |
23151 | 1572 /* count up to make sure this is correct everytime */ |
1573 priv->mapcount++; | |
10536 | 1574 |
23151 | 1575 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { |
1576 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1577 info.short_name, strerror(errno)); | |
1578 return 0; | |
1579 } | |
10536 | 1580 } |
1581 | |
23901 | 1582 /* start vbi thread */ |
1583 if(priv->priv_vbi){ | |
1584 priv->vbi_shutdown = 0; | |
1585 pthread_create(&priv->vbi_grabber_thread, NULL, vbi_grabber, priv); | |
1586 } | |
10536 | 1587 /* start audio thread */ |
1588 priv->shutdown = 0; | |
1589 priv->audio_skew_measure_time = 0; | |
1590 priv->first_frame = 0; | |
1591 priv->audio_skew = 0; | |
1592 priv->first = 1; | |
1593 | |
17626
5627625b0cdb
Don't test the v4l2_input audioset field for audio capabilities but still try changing the mute setting (patch by Jesse Allen < the3dfxdude _at_ gmail.com >)
aurel
parents:
17199
diff
changeset
|
1594 set_mute(priv, 0); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1595 |
10536 | 1596 return 1; |
1597 } | |
1598 | |
1599 // copies a video frame | |
23423 | 1600 static inline void copy_frame(priv_t *priv, video_buffer_entry *dest, unsigned char *source,int len) |
10536 | 1601 { |
23423 | 1602 dest->framesize=len; |
23886 | 1603 if(priv->tv_param->automute>0){ |
23422 | 1604 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) >= 0) { |
23886 | 1605 if(priv->tv_param->automute<<8>priv->tuner.signal){ |
23423 | 1606 fill_blank_frame(dest->data,dest->framesize,fcc_vl2mp(priv->format.fmt.pix.pixelformat)); |
23422 | 1607 set_mute(priv,1); |
1608 return; | |
1609 } | |
1610 } | |
1611 set_mute(priv,0); | |
1612 } | |
23423 | 1613 memcpy(dest->data, source, len); |
10536 | 1614 } |
1615 | |
1616 // maximum skew change, in frames | |
1617 #define MAX_SKEW_DELTA 0.6 | |
1618 static void *video_grabber(void *data) | |
1619 { | |
1620 priv_t *priv = (priv_t*)data; | |
15449 | 1621 long long skew, prev_skew, xskew, interval, prev_interval, delta; |
10536 | 1622 int i; |
23423 | 1623 int framesize = priv->format.fmt.pix.sizeimage; |
10536 | 1624 fd_set rdset; |
1625 struct timeval timeout; | |
1626 struct v4l2_buffer buf; | |
1627 | |
1628 xskew = 0; | |
1629 skew = 0; | |
1630 interval = 0; | |
1631 prev_interval = 0; | |
1632 prev_skew = 0; | |
1633 | |
1634 mp_msg(MSGT_TV, MSGL_V, "%s: going to capture\n", info.short_name); | |
1635 if (ioctl(priv->video_fd, VIDIOC_STREAMON, &(priv->format.type)) < 0) { | |
23151 | 1636 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamon failed: %s\n", |
1637 info.short_name, strerror(errno)); | |
1638 return 0; | |
10536 | 1639 } |
1640 priv->streamon = 1; | |
1641 | |
23886 | 1642 if (!priv->tv_param->noaudio) { |
23151 | 1643 pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv); |
10536 | 1644 } |
1645 | |
1646 for (priv->frames = 0; !priv->shutdown;) | |
1647 { | |
23151 | 1648 int ret; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1649 |
23151 | 1650 if (priv->immediate_mode) { |
1651 while (priv->video_cnt == priv->video_buffer_size_max) { | |
1652 usleep(10000); | |
1653 if (priv->shutdown) { | |
1654 return NULL; | |
1655 } | |
1656 } | |
1657 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1658 |
23151 | 1659 FD_ZERO (&rdset); |
1660 FD_SET (priv->video_fd, &rdset); | |
10536 | 1661 |
23151 | 1662 timeout.tv_sec = 1; |
1663 timeout.tv_usec = 0; | |
10536 | 1664 |
23151 | 1665 i = select(priv->video_fd + 1, &rdset, NULL, NULL, &timeout); |
1666 if (i < 0) { | |
1667 mp_msg(MSGT_TV, MSGL_ERR, "%s: select failed: %s\n", | |
1668 info.short_name, strerror(errno)); | |
1669 continue; | |
1670 } | |
1671 else if (i == 0) { | |
1672 mp_msg(MSGT_TV, MSGL_ERR, "%s: select timeout\n", info.short_name); | |
1673 continue; | |
1674 } | |
1675 else if (!FD_ISSET(priv->video_fd, &rdset)) { | |
1676 continue; | |
1677 } | |
10536 | 1678 |
23151 | 1679 memset(&buf,0,sizeof(buf)); |
1680 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1681 buf.memory = V4L2_MEMORY_MMAP; | |
1682 ret = ioctl(priv->video_fd, VIDIOC_DQBUF, &buf); | |
10536 | 1683 |
23151 | 1684 if (ret < 0) { |
1685 /* | |
1686 if there's no signal, the buffer might me dequeued | |
1687 so we query all the buffers to see which one we should | |
1688 put back to queue | |
10536 | 1689 |
23151 | 1690 observed with saa7134 0.2.8 |
1691 don't know if is it a bug or (mis)feature | |
1692 */ | |
1693 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl dequeue buffer failed: %s, idx = %d\n", | |
1694 info.short_name, strerror(errno), buf.index); | |
1695 for (i = 0; i < priv->mapcount; i++) { | |
1696 memset(&buf,0,sizeof(buf)); | |
1697 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1698 buf.memory = V4L2_MEMORY_MMAP; | |
1699 buf.index = i; | |
1700 ret = ioctl(priv->video_fd, VIDIOC_QUERYBUF, &buf); | |
1701 if (ret < 0) { | |
1702 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s, idx = %d\n", | |
1703 info.short_name, strerror(errno), buf.index); | |
1704 return 0; | |
1705 } | |
1706 if ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE)) == V4L2_BUF_FLAG_MAPPED) { | |
1707 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { | |
1708 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1709 info.short_name, strerror(errno)); | |
1710 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1711 } |
23151 | 1712 } |
1713 } | |
1714 continue; | |
1715 } | |
10536 | 1716 |
23151 | 1717 /* store the timestamp of the very first frame as reference */ |
1718 if (!priv->frames++) { | |
23886 | 1719 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1720 priv->first_frame = (long long)1e6*buf.timestamp.tv_sec + buf.timestamp.tv_usec; |
23886 | 1721 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1722 } |
1723 priv->curr_frame = (long long)buf.timestamp.tv_sec*1e6+buf.timestamp.tv_usec; | |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1724 // fprintf(stderr, "idx = %d, ts = %f\n", buf.index, (double)(priv->curr_frame) / 1e6); |
10536 | 1725 |
23151 | 1726 interval = priv->curr_frame - priv->first_frame; |
1727 delta = interval - prev_interval; | |
10536 | 1728 |
23151 | 1729 if (!priv->immediate_mode) { |
1730 // interpolate the skew in time | |
23886 | 1731 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1732 xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor; |
23886 | 1733 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1734 // correct extreme skew changes to avoid (especially) moving backwards in time |
1735 if (xskew - prev_skew > delta*MAX_SKEW_DELTA) { | |
1736 skew = prev_skew + delta*MAX_SKEW_DELTA; | |
1737 } else if (xskew - prev_skew < -delta*MAX_SKEW_DELTA) { | |
1738 skew = prev_skew - delta*MAX_SKEW_DELTA; | |
1739 } else { | |
1740 skew = xskew; | |
1741 } | |
1742 } | |
10536 | 1743 |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1744 mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %f, interval = %f, a_skew = %f, corr_skew = %f\n", |
23151 | 1745 delta ? (double)1e6/delta : -1, |
1746 (double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew); | |
1747 mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt); | |
10536 | 1748 |
23151 | 1749 prev_skew = skew; |
1750 prev_interval = interval; | |
10536 | 1751 |
23151 | 1752 /* allocate a new buffer, if needed */ |
1753 pthread_mutex_lock(&priv->video_buffer_mutex); | |
1754 if (priv->video_buffer_size_current < priv->video_buffer_size_max) { | |
1755 if (priv->video_cnt == priv->video_buffer_size_current) { | |
1756 unsigned char *newbuf = malloc(framesize); | |
1757 if (newbuf) { | |
1758 memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail, | |
23423 | 1759 (priv->video_buffer_size_current-priv->video_tail)*sizeof(video_buffer_entry)); |
1760 priv->video_ringbuffer[priv->video_tail].data = newbuf; | |
23151 | 1761 if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++; |
1762 priv->video_buffer_size_current++; | |
1763 } | |
1764 } | |
1765 } | |
1766 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
10536 | 1767 |
23151 | 1768 if (priv->video_cnt == priv->video_buffer_size_current) { |
1769 if (!priv->immediate_mode) { | |
1770 mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n"); | |
1771 if (priv->audio_insert_null_samples) { | |
1772 pthread_mutex_lock(&priv->audio_mutex); | |
1773 priv->dropped_frames_timeshift += delta; | |
1774 pthread_mutex_unlock(&priv->audio_mutex); | |
1775 } | |
1776 } | |
1777 } else { | |
1778 if (priv->immediate_mode) { | |
23423 | 1779 priv->video_ringbuffer[priv->video_tail].timestamp = 0; |
23151 | 1780 } else { |
1781 // compensate for audio skew | |
1782 // negative skew => there are more audio samples, increase interval | |
1783 // positive skew => less samples, shorten the interval | |
23423 | 1784 priv->video_ringbuffer[priv->video_tail].timestamp = interval - skew; |
1785 if (priv->audio_insert_null_samples && priv->video_ringbuffer[priv->video_tail].timestamp > 0) { | |
23151 | 1786 pthread_mutex_lock(&priv->audio_mutex); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1787 priv->video_ringbuffer[priv->video_tail].timestamp += |
23151 | 1788 (priv->audio_null_blocks_inserted |
1789 - priv->dropped_frames_timeshift/priv->audio_usecs_per_block) | |
1790 *priv->audio_usecs_per_block; | |
1791 pthread_mutex_unlock(&priv->audio_mutex); | |
1792 } | |
1793 } | |
23423 | 1794 copy_frame(priv, priv->video_ringbuffer+priv->video_tail, priv->map[buf.index].addr,buf.bytesused); |
23151 | 1795 priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current; |
1796 priv->video_cnt++; | |
1797 } | |
1798 if (ioctl(priv->video_fd, VIDIOC_QBUF, &buf) < 0) { | |
1799 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1800 info.short_name, strerror(errno)); | |
1801 return 0; | |
1802 } | |
10536 | 1803 } |
1804 return NULL; | |
1805 } | |
1806 | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1807 #define MAX_LOOP 50 |
10536 | 1808 static double grab_video_frame(priv_t *priv, char *buffer, int len) |
1809 { | |
1810 double interval; | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1811 int loop_cnt = 0; |
10536 | 1812 |
1813 if (priv->first) { | |
23151 | 1814 pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv); |
1815 priv->first = 0; | |
10536 | 1816 } |
1817 | |
1818 while (priv->video_cnt == 0) { | |
23151 | 1819 usleep(10000); |
1820 if (loop_cnt++ > MAX_LOOP) return 0; | |
10536 | 1821 } |
1822 | |
1823 pthread_mutex_lock(&priv->video_buffer_mutex); | |
23423 | 1824 interval = (double)priv->video_ringbuffer[priv->video_head].timestamp*1e-6; |
1825 memcpy(buffer, priv->video_ringbuffer[priv->video_head].data, len); | |
10536 | 1826 priv->video_cnt--; |
1827 priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current; | |
1828 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
1829 | |
1830 return interval; | |
1831 } | |
1832 | |
1833 static int get_video_framesize(priv_t *priv) | |
1834 { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1835 /* |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1836 this routine will be called before grab_video_frame |
23423 | 1837 thus let's return topmost frame's size |
1838 */ | |
1839 if (priv->video_cnt) | |
1840 return priv->video_ringbuffer[priv->video_head].framesize; | |
1841 /* | |
1842 no video frames yet available. i don't know what to do in this case, | |
1843 thus let's return some fallback result (for compressed format this will be | |
1844 maximum allowed frame size. | |
1845 */ | |
10536 | 1846 return priv->format.fmt.pix.sizeimage; |
1847 } | |
1848 | |
10704 | 1849 //#define DOUBLESPEED |
1850 #ifdef DOUBLESPEED | |
10536 | 1851 // for testing purposes only |
1852 static void read_doublespeed(priv_t *priv) | |
1853 { | |
18885 | 1854 char *bufx = calloc(priv->audio_in.blocksize, 2); |
10536 | 1855 short *s; |
1856 short *d; | |
1857 int i; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1858 |
10536 | 1859 audio_in_read_chunk(&priv->audio_in, bufx); |
1860 audio_in_read_chunk(&priv->audio_in, bufx+priv->audio_in.blocksize); | |
1861 | |
1862 s = bufx; | |
1863 d = priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize; | |
1864 for (i = 0; i < priv->audio_in.blocksize/2; i++) { | |
23151 | 1865 *d++ = *s++; |
1866 *s++; | |
10536 | 1867 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1868 |
10536 | 1869 } |
10704 | 1870 #endif |
10536 | 1871 |
1872 static void *audio_grabber(void *data) | |
1873 { | |
1874 priv_t *priv = (priv_t*)data; | |
1875 int i, audio_skew_ptr = 0; | |
10653 | 1876 long long current_time, prev_skew = 0, prev_skew_uncorr = 0; |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1877 long long start_time_avg, curr_timestamp; |
10536 | 1878 |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1879 start_time_avg = priv->audio_start_time = get_curr_timestamp(priv->clk_id); |
10536 | 1880 audio_in_start_capture(&priv->audio_in); |
1881 for (i = 0; i < priv->aud_skew_cnt; i++) | |
23151 | 1882 priv->audio_skew_buffer[i] = 0; |
10653 | 1883 for (i = 0; i < priv->aud_skew_cnt; i++) |
23151 | 1884 priv->audio_skew_delta_buffer[i] = 0; |
10536 | 1885 |
1886 for (; !priv->shutdown;) | |
1887 { | |
10704 | 1888 #ifdef DOUBLESPEED |
23151 | 1889 read_doublespeed(priv); |
10704 | 1890 #else |
23151 | 1891 if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0) |
1892 continue; | |
10704 | 1893 #endif |
23151 | 1894 pthread_mutex_lock(&priv->skew_mutex); |
1895 if (priv->first_frame == 0) { | |
1896 // there is no first frame yet (unlikely to happen) | |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1897 start_time_avg = priv->audio_start_time = get_curr_timestamp(priv->clk_id); |
23151 | 1898 // fprintf(stderr, "warning - first frame not yet available!\n"); |
1899 pthread_mutex_unlock(&priv->skew_mutex); | |
1900 continue; | |
1901 } | |
1902 pthread_mutex_unlock(&priv->skew_mutex); | |
10536 | 1903 |
23151 | 1904 priv->audio_recv_blocks_total++; |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1905 curr_timestamp = get_curr_timestamp(priv->clk_id); |
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1906 current_time = curr_timestamp - priv->audio_start_time; |
10536 | 1907 |
23151 | 1908 if (priv->audio_recv_blocks_total < priv->aud_skew_cnt*2) { |
37126
6db1f7a10803
Use correct type of timestamps when recording from v4l2. Fix #2176
iive
parents:
35903
diff
changeset
|
1909 start_time_avg += curr_timestamp - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; |
23151 | 1910 priv->audio_start_time = start_time_avg/(priv->audio_recv_blocks_total+1); |
1911 } | |
10852 | 1912 |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1913 // fprintf(stderr, "spb = %f, bs = %d, skew = %f\n", priv->audio_secs_per_block, priv->audio_in.blocksize, |
23151 | 1914 // (double)(current_time - 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total)/1e6); |
10536 | 1915 |
23151 | 1916 // put the current skew into the ring buffer |
1917 priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr]; | |
1918 priv->audio_skew_buffer[audio_skew_ptr] = current_time | |
1919 - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1920 priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr]; | |
10536 | 1921 |
23151 | 1922 pthread_mutex_lock(&priv->skew_mutex); |
10704 | 1923 |
23151 | 1924 // skew calculation |
10704 | 1925 |
23151 | 1926 // compute the sliding average of the skews |
1927 if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) { | |
1928 priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt; | |
1929 } else { | |
1930 priv->audio_skew = priv->audio_skew_total/priv->audio_recv_blocks_total; | |
1931 } | |
10653 | 1932 |
23151 | 1933 // put the current skew change (skew-prev_skew) into the ring buffer |
1934 priv->audio_skew_delta_total -= priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1935 priv->audio_skew_delta_buffer[audio_skew_ptr] = priv->audio_skew - prev_skew_uncorr; | |
1936 priv->audio_skew_delta_total += priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1937 prev_skew_uncorr = priv->audio_skew; // remember the _uncorrected_ average value | |
10704 | 1938 |
23151 | 1939 audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt; // rotate the buffer pointer |
10653 | 1940 |
23151 | 1941 // sliding average approximates the value in the middle of the interval |
1942 // so interpolate the skew value further to the current time | |
1943 priv->audio_skew += priv->audio_skew_delta_total/2; | |
10653 | 1944 |
23151 | 1945 // now finally, priv->audio_skew contains fairly good approximation |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1946 // of the current value |
10653 | 1947 |
23151 | 1948 // current skew factor (assuming linearity) |
1949 // used for further interpolation in video_grabber | |
1950 // probably overkill but seems to be necessary for | |
1951 // stress testing by dropping half of the audio frames ;) | |
1952 // especially when using ALSA with large block sizes | |
1953 // where audio_skew remains a long while behind | |
1954 if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) { | |
1955 priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time); | |
1956 } else { | |
1957 priv->audio_skew_factor = 0.0; | |
1958 } | |
10852 | 1959 |
23151 | 1960 priv->audio_skew_measure_time = current_time; |
1961 prev_skew = priv->audio_skew; | |
1962 priv->audio_skew += priv->audio_start_time - priv->first_frame; | |
1963 pthread_mutex_unlock(&priv->skew_mutex); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1964 |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1965 // fprintf(stderr, "audio_skew = %f, delta = %f\n", (double)priv->audio_skew/1e6, (double)priv->audio_skew_delta_total/1e6); |
10851 | 1966 |
23151 | 1967 pthread_mutex_lock(&priv->audio_mutex); |
1968 if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) { | |
1969 mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n"); | |
1970 priv->audio_drop++; | |
1971 } else { | |
1972 priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size; | |
1973 priv->audio_cnt++; | |
1974 } | |
1975 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1976 } |
1977 return NULL; | |
1978 } | |
1979 | |
1980 static double grab_audio_frame(priv_t *priv, char *buffer, int len) | |
1981 { | |
1982 mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n", | |
23151 | 1983 priv, buffer, len); |
10536 | 1984 |
15449 | 1985 // hack: if grab_audio_frame is called first, it means we are used by mplayer |
1986 // => switch to the mode which outputs audio immediately, even if | |
1987 // it should be silence | |
1988 if (priv->first) priv->audio_insert_null_samples = 1; | |
1989 | |
1990 pthread_mutex_lock(&priv->audio_mutex); | |
1991 while (priv->audio_insert_null_samples | |
23151 | 1992 && priv->dropped_frames_timeshift - priv->dropped_frames_compensated >= priv->audio_usecs_per_block) { |
1993 // some frames were dropped - drop the corresponding number of audio blocks | |
1994 if (priv->audio_drop) { | |
1995 priv->audio_drop--; | |
1996 } else { | |
1997 if (priv->audio_head == priv->audio_tail) break; | |
1998 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1999 } | |
2000 priv->dropped_frames_compensated += priv->audio_usecs_per_block; | |
10776
80402283a017
Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents:
10735
diff
changeset
|
2001 } |
80402283a017
Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents:
10735
diff
changeset
|
2002 |
10536 | 2003 // compensate for dropped audio frames |
2004 if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) { | |
23151 | 2005 priv->audio_drop--; |
2006 memset(buffer, 0, len); | |
2007 goto out; | |
10536 | 2008 } |
2009 | |
15449 | 2010 if (priv->audio_insert_null_samples && (priv->audio_head == priv->audio_tail)) { |
23151 | 2011 // return silence to avoid desync and stuttering |
2012 memset(buffer, 0, len); | |
2013 priv->audio_null_blocks_inserted++; | |
2014 goto out; | |
15449 | 2015 } |
2016 | |
2017 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 2018 while (priv->audio_head == priv->audio_tail) { |
23151 | 2019 // this is mencoder => just wait until some audio is available |
2020 usleep(10000); | |
10536 | 2021 } |
15451 | 2022 pthread_mutex_lock(&priv->audio_mutex); |
10536 | 2023 memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len); |
2024 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
2025 priv->audio_cnt--; | |
15449 | 2026 out: |
2027 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 2028 priv->audio_sent_blocks_total++; |
2029 return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block; | |
2030 } | |
2031 | |
2032 static int get_audio_framesize(priv_t *priv) | |
2033 { | |
26756
c43ce7268677
cosmetics: Remove useless parentheses from return statements.
diego
parents:
25962
diff
changeset
|
2034 return priv->audio_in.blocksize; |
10536 | 2035 } |