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