Mercurial > mplayer.hg
annotate stream/tvi_v4l2.c @ 36855:5cee3aefde4f
Fix previous sync error
author | jrash |
---|---|
date | Wed, 26 Feb 2014 21:57:48 +0000 |
parents | 389d43c448b3 |
children | 6db1f7a10803 |
rev | line source |
---|---|
10536 | 1 /* |
28106 | 2 * Video 4 Linux 2 input |
3 * | |
4 * copyright (c) 2003 Martin Olschewski <olschewski@zpr.uni-koeln.de> | |
5 * copyright (c) 2003 Jindrich Makovicka <makovick@gmail.com> | |
6 * | |
7 * Some ideas are based on works from | |
8 * Alex Beregszaszi <alex@fsn.hu> | |
9 * Gerd Knorr <kraxel@bytesex.org> | |
10 * | |
11 * This file is part of MPlayer. | |
12 * | |
13 * MPlayer is free software; you can redistribute it and/or modify | |
14 * it under the terms of the GNU General Public License as published by | |
15 * the Free Software Foundation; either version 2 of the License, or | |
16 * (at your option) any later version. | |
17 * | |
18 * MPlayer is distributed in the hope that it will be useful, | |
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 * GNU General Public License for more details. | |
22 * | |
23 * You should have received a copy of the GNU General Public License along | |
24 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
26 */ | |
10536 | 27 |
28 /* | |
29 | |
30 known issues: | |
31 - norm setting isn't consistent with tvi_v4l | |
32 - the same for volume/bass/treble/balance | |
33 | |
34 */ | |
35 | |
36 #include "config.h" | |
37 | |
38 #include <errno.h> | |
39 #include <fcntl.h> | |
40 #include <pthread.h> | |
41 #include <stdio.h> | |
42 #include <string.h> | |
35903 | 43 #include <strings.h> |
10536 | 44 #include <sys/ioctl.h> |
45 #include <sys/mman.h> | |
46 #include <sys/time.h> | |
47 #include <sys/types.h> | |
48 #include <unistd.h> | |
27461
5a30f5bc23a0
Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents:
27390
diff
changeset
|
49 #include <math.h> |
10536 | 50 #ifdef HAVE_SYS_SYSINFO_H |
51 #include <sys/sysinfo.h> | |
52 #endif | |
33163 | 53 #ifdef HAVE_SYS_VIDEOIO_H |
54 #include <sys/videoio.h> | |
55 #else | |
16442 | 56 #include <linux/types.h> |
57 #include <linux/videodev2.h> | |
33163 | 58 #endif |
17012 | 59 #include "mp_msg.h" |
19431
ac69ba536915
Explicitly include libmpcodecs/img_format.h and libvo/fastmemcpy.h.
diego
parents:
19271
diff
changeset
|
60 #include "libmpcodecs/img_format.h" |
29759
d287e2785570
Move teletext specific code from stream into libmpcodecs.
cehoyos
parents:
29263
diff
changeset
|
61 #include "libmpcodecs/dec_teletext.h" |
17012 | 62 #include "libaf/af_format.h" |
10536 | 63 #include "tv.h" |
64 #include "audio_in.h" | |
65 | |
22381
6cabac4d35b5
tv driver loading rework. As a side effect "-tv driver=help" option is
voroshil
parents:
21587
diff
changeset
|
66 #define info tvi_info_v4l2 |
23883 | 67 static tvi_handle_t *tvi_init_v4l2(tv_param_t* tv_param); |
10536 | 68 /* information about this file */ |
25689 | 69 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
|
70 tvi_init_v4l2, |
10536 | 71 "Video 4 Linux 2 input", |
72 "v4l2", | |
73 "Martin Olschewski <olschewski@zpr.uni-koeln.de>", | |
74 "first try, more to come ;-)" | |
75 }; | |
76 | |
77 struct map { | |
78 struct v4l2_buffer buf; | |
79 void *addr; | |
80 size_t len; | |
81 }; | |
82 | |
83 #define BUFFER_COUNT 6 | |
84 | |
23423 | 85 /** video ringbuffer entry */ |
86 typedef struct { | |
87 unsigned char *data; ///< frame contents | |
88 long long timestamp; ///< frame timestamp | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
89 int framesize; ///< actual frame size |
23423 | 90 } video_buffer_entry; |
91 | |
10536 | 92 /* private data */ |
31322
016194f71de3
Fix function pointer types in tvi_functions struct
reimar
parents:
29949
diff
changeset
|
93 typedef struct priv { |
10536 | 94 /* video */ |
23151 | 95 char *video_dev; |
96 int video_fd; | |
23901 | 97 char *vbi_dev; |
98 int vbi_fd; | |
99 int vbi_bufsize; | |
100 int vbi_shutdown; | |
101 pthread_t vbi_grabber_thread; | |
102 void *priv_vbi; | |
10536 | 103 int mp_format; |
23151 | 104 struct v4l2_capability capability; |
10536 | 105 struct v4l2_input input; |
23151 | 106 struct v4l2_format format; |
107 struct v4l2_standard standard; | |
108 struct v4l2_tuner tuner; | |
109 struct map *map; | |
110 int mapcount; | |
111 int frames; | |
10851 | 112 volatile long long first_frame; |
10536 | 113 long long curr_frame; |
114 /* audio video interleaving ;-) */ | |
23151 | 115 volatile int streamon; |
116 pthread_t audio_grabber_thread; | |
117 pthread_mutex_t skew_mutex; | |
10536 | 118 |
119 /* 2nd level video buffers */ | |
120 int first; | |
121 int immediate_mode; | |
122 | |
123 int video_buffer_size_max; | |
124 volatile int video_buffer_size_current; | |
23423 | 125 video_buffer_entry *video_ringbuffer; |
23151 | 126 volatile int video_head; |
127 volatile int video_tail; | |
128 volatile int video_cnt; | |
129 pthread_t video_grabber_thread; | |
10536 | 130 pthread_mutex_t video_buffer_mutex; |
131 | |
132 /* audio */ | |
23151 | 133 char *audio_dev; |
10536 | 134 audio_in_t audio_in; |
135 | |
136 long long audio_start_time; | |
137 int audio_buffer_size; | |
138 int aud_skew_cnt; | |
23151 | 139 unsigned char *audio_ringbuffer; |
140 long long *audio_skew_buffer; | |
141 long long *audio_skew_delta_buffer; | |
142 volatile int audio_head; | |
143 volatile int audio_tail; | |
144 volatile int audio_cnt; | |
10536 | 145 volatile long long audio_skew; |
146 volatile double audio_skew_factor; | |
147 volatile long long audio_skew_measure_time; | |
148 volatile int audio_drop; | |
149 volatile int shutdown; | |
150 | |
25962 | 151 int audio_initialized; |
10536 | 152 double audio_secs_per_block; |
15449 | 153 long long audio_usecs_per_block; |
10536 | 154 long long audio_skew_total; |
10653 | 155 long long audio_skew_delta_total; |
23151 | 156 long audio_recv_blocks_total; |
157 long audio_sent_blocks_total; | |
15449 | 158 pthread_mutex_t audio_mutex; |
159 int audio_insert_null_samples; | |
160 volatile long audio_null_blocks_inserted; | |
161 volatile long long dropped_frames_timeshift; | |
162 long long dropped_frames_compensated; | |
23883 | 163 |
164 tv_param_t *tv_param; | |
10536 | 165 } priv_t; |
166 | |
167 #include "tvi_def.h" | |
168 | |
169 static void *audio_grabber(void *data); | |
170 static void *video_grabber(void *data); | |
171 | |
172 /**********************************************************************\ | |
173 | |
174 Only few of the fourccs are the same in v4l2 and mplayer: | |
175 | |
176 IMGFMT_YVU9 == V4L2_PIX_FMT_YVU410 | |
177 IMGFMT_YV12 == V4L2_PIX_FMT_YVU420 | |
178 IMGFMT_NV12 == V4L2_PIX_FMT_NV12 | |
179 IMGFMT_422P == V4L2_PIX_FMT_YUV422P | |
180 IMGFMT_411P == V4L2_PIX_FMT_YUV411P | |
181 IMGFMT_UYVY == V4L2_PIX_FMT_UYVY | |
182 IMGFMT_Y41P == V4L2_PIX_FMT_Y41P | |
183 | |
184 This may be an useful translation table for some others: | |
185 | |
186 IMGFMT_RGB8 == V4L2_PIX_FMT_RGB332 | |
15449 | 187 IMGFMT_BGR15 == V4L2_PIX_FMT_RGB555 |
188 IMGFMT_BGR16 == V4L2_PIX_FMT_RGB565 | |
10536 | 189 IMGFMT_RGB24 == V4L2_PIX_FMT_RGB24 |
190 IMGFMT_RGB32 == V4L2_PIX_FMT_RGB32 | |
191 IMGFMT_BGR24 == V4L2_PIX_FMT_BGR24 | |
192 IMGFMT_BGR32 == V4L2_PIX_FMT_BGR32 | |
193 IMGFMT_Y800 == V4L2_PIX_FMT_GREY | |
194 IMGFMT_IF09 == V4L2_PIX_FMT_YUV410 | |
195 IMGFMT_I420 == V4L2_PIX_FMT_YUV420 | |
196 IMGFMT_YUY2 == V4L2_PIX_FMT_YUYV | |
197 | |
198 \**********************************************************************/ | |
199 | |
200 /* | |
201 ** Translate a mplayer fourcc to a video4linux2 pixel format. | |
202 */ | |
203 static int fcc_mp2vl(int fcc) | |
204 { | |
205 switch (fcc) { | |
23151 | 206 case IMGFMT_RGB8: return V4L2_PIX_FMT_RGB332; |
207 case IMGFMT_BGR15: return V4L2_PIX_FMT_RGB555; | |
208 case IMGFMT_BGR16: return V4L2_PIX_FMT_RGB565; | |
209 case IMGFMT_RGB24: return V4L2_PIX_FMT_RGB24; | |
210 case IMGFMT_RGB32: return V4L2_PIX_FMT_RGB32; | |
211 case IMGFMT_BGR24: return V4L2_PIX_FMT_BGR24; | |
212 case IMGFMT_BGR32: return V4L2_PIX_FMT_BGR32; | |
213 case IMGFMT_Y800: return V4L2_PIX_FMT_GREY; | |
214 case IMGFMT_IF09: return V4L2_PIX_FMT_YUV410; | |
215 case IMGFMT_I420: return V4L2_PIX_FMT_YUV420; | |
216 case IMGFMT_YUY2: return V4L2_PIX_FMT_YUYV; | |
217 case IMGFMT_YV12: return V4L2_PIX_FMT_YVU420; | |
11657 | 218 case IMGFMT_UYVY: return V4L2_PIX_FMT_UYVY; |
23423 | 219 case IMGFMT_MJPEG: return V4L2_PIX_FMT_MJPEG; |
10536 | 220 } |
221 return fcc; | |
222 } | |
223 | |
224 /* | |
225 ** Translate a video4linux2 fourcc aka pixel format to mplayer. | |
226 */ | |
227 static int fcc_vl2mp(int fcc) | |
228 { | |
229 switch (fcc) { | |
23151 | 230 case V4L2_PIX_FMT_RGB332: return IMGFMT_RGB8; |
231 case V4L2_PIX_FMT_RGB555: return IMGFMT_BGR15; | |
232 case V4L2_PIX_FMT_RGB565: return IMGFMT_BGR16; | |
233 case V4L2_PIX_FMT_RGB24: return IMGFMT_RGB24; | |
234 case V4L2_PIX_FMT_RGB32: return IMGFMT_RGB32; | |
235 case V4L2_PIX_FMT_BGR24: return IMGFMT_BGR24; | |
236 case V4L2_PIX_FMT_BGR32: return IMGFMT_BGR32; | |
237 case V4L2_PIX_FMT_GREY: return IMGFMT_Y800; | |
238 case V4L2_PIX_FMT_YUV410: return IMGFMT_IF09; | |
239 case V4L2_PIX_FMT_YUV420: return IMGFMT_I420; | |
240 case V4L2_PIX_FMT_YVU420: return IMGFMT_YV12; | |
241 case V4L2_PIX_FMT_YUYV: return IMGFMT_YUY2; | |
11657 | 242 case V4L2_PIX_FMT_UYVY: return IMGFMT_UYVY; |
23423 | 243 case V4L2_PIX_FMT_MJPEG: return IMGFMT_MJPEG; |
10536 | 244 } |
245 return fcc; | |
246 } | |
247 | |
248 /* | |
249 ** Translate a video4linux2 fourcc aka pixel format | |
250 ** to a human readable string. | |
251 */ | |
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
|
252 static const char *pixfmt2name(int pixfmt) |
10536 | 253 { |
254 static char unknown[24]; | |
255 | |
256 switch (pixfmt) { | |
23151 | 257 case V4L2_PIX_FMT_RGB332: return "RGB332"; |
258 case V4L2_PIX_FMT_RGB555: return "RGB555"; | |
259 case V4L2_PIX_FMT_RGB565: return "RGB565"; | |
260 case V4L2_PIX_FMT_RGB555X: return "RGB555X"; | |
261 case V4L2_PIX_FMT_RGB565X: return "RGB565X"; | |
262 case V4L2_PIX_FMT_BGR24: return "BGR24"; | |
263 case V4L2_PIX_FMT_RGB24: return "RGB24"; | |
264 case V4L2_PIX_FMT_BGR32: return "BGR32"; | |
265 case V4L2_PIX_FMT_RGB32: return "RGB32"; | |
266 case V4L2_PIX_FMT_GREY: return "GREY"; | |
267 case V4L2_PIX_FMT_YVU410: return "YVU410"; | |
268 case V4L2_PIX_FMT_YVU420: return "YVU420"; | |
269 case V4L2_PIX_FMT_YUYV: return "YUYV"; | |
270 case V4L2_PIX_FMT_UYVY: return "UYVY"; | |
271 /* case V4L2_PIX_FMT_YVU422P: return "YVU422P"; */ | |
272 /* case V4L2_PIX_FMT_YVU411P: return "YVU411P"; */ | |
273 case V4L2_PIX_FMT_YUV422P: return "YUV422P"; | |
274 case V4L2_PIX_FMT_YUV411P: return "YUV411P"; | |
275 case V4L2_PIX_FMT_Y41P: return "Y41P"; | |
276 case V4L2_PIX_FMT_NV12: return "NV12"; | |
277 case V4L2_PIX_FMT_NV21: return "NV21"; | |
278 case V4L2_PIX_FMT_YUV410: return "YUV410"; | |
279 case V4L2_PIX_FMT_YUV420: return "YUV420"; | |
280 case V4L2_PIX_FMT_YYUV: return "YYUV"; | |
281 case V4L2_PIX_FMT_HI240: return "HI240"; | |
282 case V4L2_PIX_FMT_WNVA: return "WNVA"; | |
23423 | 283 case V4L2_PIX_FMT_MJPEG: return "MJPEG"; |
10536 | 284 } |
285 sprintf(unknown, "unknown (0x%x)", pixfmt); | |
286 return unknown; | |
287 } | |
288 | |
289 | |
290 /* | |
291 ** Gives the depth of a video4linux2 fourcc aka pixel format in bits. | |
292 */ | |
293 static int pixfmt2depth(int pixfmt) | |
294 { | |
295 switch (pixfmt) { | |
296 case V4L2_PIX_FMT_RGB332: | |
23151 | 297 return 8; |
10536 | 298 case V4L2_PIX_FMT_RGB555: |
299 case V4L2_PIX_FMT_RGB565: | |
300 case V4L2_PIX_FMT_RGB555X: | |
301 case V4L2_PIX_FMT_RGB565X: | |
23151 | 302 return 16; |
10536 | 303 case V4L2_PIX_FMT_BGR24: |
304 case V4L2_PIX_FMT_RGB24: | |
23151 | 305 return 24; |
10536 | 306 case V4L2_PIX_FMT_BGR32: |
307 case V4L2_PIX_FMT_RGB32: | |
23151 | 308 return 32; |
10536 | 309 case V4L2_PIX_FMT_GREY: |
23151 | 310 return 8; |
10536 | 311 case V4L2_PIX_FMT_YVU410: |
23151 | 312 return 9; |
10536 | 313 case V4L2_PIX_FMT_YVU420: |
23151 | 314 return 12; |
10536 | 315 case V4L2_PIX_FMT_YUYV: |
316 case V4L2_PIX_FMT_UYVY: | |
317 case V4L2_PIX_FMT_YUV422P: | |
318 case V4L2_PIX_FMT_YUV411P: | |
23151 | 319 return 16; |
10536 | 320 case V4L2_PIX_FMT_Y41P: |
321 case V4L2_PIX_FMT_NV12: | |
322 case V4L2_PIX_FMT_NV21: | |
23151 | 323 return 12; |
10536 | 324 case V4L2_PIX_FMT_YUV410: |
23151 | 325 return 9; |
10536 | 326 case V4L2_PIX_FMT_YUV420: |
23151 | 327 return 12; |
10536 | 328 case V4L2_PIX_FMT_YYUV: |
23151 | 329 return 16; |
10536 | 330 case V4L2_PIX_FMT_HI240: |
23151 | 331 return 8; |
10536 | 332 |
333 } | |
334 return 0; | |
335 } | |
336 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
337 static int amode2v4l(int amode) |
10536 | 338 { |
339 switch (amode) { | |
340 case 0: | |
23151 | 341 return V4L2_TUNER_MODE_MONO; |
10536 | 342 case 1: |
23151 | 343 return V4L2_TUNER_MODE_STEREO; |
10536 | 344 case 2: |
23151 | 345 return V4L2_TUNER_MODE_LANG1; |
10536 | 346 case 3: |
23151 | 347 return V4L2_TUNER_MODE_LANG2; |
10536 | 348 default: |
23151 | 349 return -1; |
10536 | 350 } |
351 } | |
352 | |
353 | |
27138
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
354 /* |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
355 ** Get current FPS. |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
356 */ |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
357 static double getfps(priv_t *priv) |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
358 { |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
359 if (priv->tv_param->fps > 0) |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
360 return priv->tv_param->fps; |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
361 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
|
362 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
|
363 return 25.0; |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
364 } |
62916b06a055
Fix division by zero in tvi_v4l2 which occures when capture device
voroshil
parents:
26756
diff
changeset
|
365 |
10536 | 366 // sets and sanitizes audio buffer/block sizes |
367 static void setup_audio_buffer_sizes(priv_t *priv) | |
368 { | |
369 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
|
370 int seconds = priv->video_buffer_size_max/getfps(priv); |
10536 | 371 |
372 if (seconds < 5) seconds = 5; | |
373 if (seconds > 500) seconds = 500; | |
374 | |
375 // make the audio buffer at least as the video buffer capacity (or 5 seconds) long | |
376 priv->audio_buffer_size = 1 + seconds*priv->audio_in.samplerate | |
23151 | 377 *priv->audio_in.channels |
378 *bytes_per_sample/priv->audio_in.blocksize; | |
10536 | 379 if (priv->audio_buffer_size < 256) priv->audio_buffer_size = 256; |
380 | |
381 // make the skew buffer at least 1 second long | |
382 priv->aud_skew_cnt = 1 + 1*priv->audio_in.samplerate | |
23151 | 383 *priv->audio_in.channels |
384 *bytes_per_sample/priv->audio_in.blocksize; | |
10536 | 385 if (priv->aud_skew_cnt < 16) priv->aud_skew_cnt = 16; |
386 | |
387 mp_msg(MSGT_TV, MSGL_V, "Audio capture - buffer %d blocks of %d bytes, skew average from %d meas.\n", | |
23151 | 388 priv->audio_buffer_size, priv->audio_in.blocksize, priv->aud_skew_cnt); |
10536 | 389 } |
390 | |
15464 | 391 static void init_audio(priv_t *priv) |
392 { | |
25962 | 393 if (priv->audio_initialized) return; |
15464 | 394 |
23886 | 395 if (!priv->tv_param->noaudio) { |
27390
9d95dc936e66
Introduce CONFIG_ALSA preprocessor directive for ALSA 0.9 and 1.x.
diego
parents:
27387
diff
changeset
|
396 #ifdef CONFIG_ALSA |
23886 | 397 if (priv->tv_param->alsa) |
23151 | 398 audio_in_init(&priv->audio_in, AUDIO_IN_ALSA); |
399 else | |
400 audio_in_init(&priv->audio_in, AUDIO_IN_OSS); | |
15464 | 401 #else |
23151 | 402 audio_in_init(&priv->audio_in, AUDIO_IN_OSS); |
15464 | 403 #endif |
404 | |
23151 | 405 if (priv->audio_dev) { |
406 audio_in_set_device(&priv->audio_in, priv->audio_dev); | |
407 } | |
15464 | 408 |
23151 | 409 audio_in_set_samplerate(&priv->audio_in, 44100); |
410 if (priv->capability.capabilities & V4L2_CAP_TUNER) { | |
411 if (priv->tuner.audmode == V4L2_TUNER_MODE_STEREO) { | |
412 audio_in_set_channels(&priv->audio_in, 2); | |
413 } else { | |
414 audio_in_set_channels(&priv->audio_in, 1); | |
415 } | |
416 } else { | |
23886 | 417 if (priv->tv_param->forcechan >= 0) { |
418 audio_in_set_channels(&priv->audio_in, priv->tv_param->forcechan); | |
23151 | 419 } else { |
420 audio_in_set_channels(&priv->audio_in, 2); | |
421 } | |
422 } | |
15464 | 423 |
23151 | 424 if (audio_in_setup(&priv->audio_in) < 0) return; |
15464 | 425 |
25962 | 426 priv->audio_initialized = 1; |
15464 | 427 } |
428 } | |
429 | |
10536 | 430 #if 0 |
431 /* | |
432 ** the number of milliseconds elapsed between time0 and time1 | |
433 */ | |
434 static size_t difftv(struct timeval time1, struct timeval time0) | |
435 { | |
23151 | 436 return (time1.tv_sec - time0.tv_sec) * 1000 + |
437 (time1.tv_usec - time0.tv_usec) / 1000; | |
10536 | 438 } |
439 #endif | |
440 | |
441 /* | |
442 ** Get current video capture format. | |
443 */ | |
444 static int getfmt(priv_t *priv) | |
445 { | |
446 int i; | |
447 | |
448 priv->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
449 if ((i = ioctl(priv->video_fd, VIDIOC_G_FMT, &priv->format)) < 0) { | |
23151 | 450 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get format failed: %s\n", |
451 info.short_name, strerror(errno)); | |
10536 | 452 } |
453 return i; | |
454 } | |
455 | |
456 | |
457 /* | |
458 ** Get current video capture standard. | |
459 */ | |
460 static int getstd(priv_t *priv) | |
461 { | |
462 v4l2_std_id id; | |
463 int i=0; | |
464 | |
465 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
|
466 struct v4l2_streamparm parm; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
467 |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
468 parm.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
469 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
|
470 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
|
471 " 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
|
472 priv->standard.index=0; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
473 priv->standard.id=0; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
474 priv->standard.frameperiod=parm.parm.capture.timeperframe; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
475 return 0; |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
476 } |
0917bf2bed6c
Try to get frame rate information through VIDIOC_G_PARM if
voroshil
parents:
27138
diff
changeset
|
477 |
23151 | 478 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl get standard failed: %s\n", |
479 info.short_name, strerror(errno)); | |
480 return -1; | |
10536 | 481 } |
482 do { | |
23151 | 483 priv->standard.index = i++; |
484 if (ioctl(priv->video_fd, VIDIOC_ENUMSTD, &priv->standard) < 0) { | |
485 return -1; | |
486 } | |
10536 | 487 } while (priv->standard.id != id); |
488 return 0; | |
489 } | |
490 | |
491 /***********************************************************************\ | |
23151 | 492 * * |
493 * * | |
494 * Interface to mplayer * | |
495 * * | |
496 * * | |
10536 | 497 \***********************************************************************/ |
498 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
499 static int set_mute(priv_t *priv, int value) |
10536 | 500 { |
501 struct v4l2_control control; | |
502 control.id = V4L2_CID_AUDIO_MUTE; | |
503 control.value = value; | |
504 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, &control) < 0) { | |
23151 | 505 mp_msg(MSGT_TV,MSGL_ERR,"%s: ioctl set mute failed: %s\n", |
506 info.short_name, strerror(errno)); | |
507 return 0; | |
10536 | 508 } |
509 return 1; | |
510 } | |
511 | |
512 /* | |
12860 | 513 ** MPlayer uses values from -100 up to 100 for controls. |
10536 | 514 ** Here they are scaled to what the tv card needs and applied. |
515 */ | |
516 static int set_control(priv_t *priv, struct v4l2_control *control, int val_signed) { | |
23151 | 517 struct v4l2_queryctrl qctrl; |
10536 | 518 qctrl.id = control->id; |
519 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) { | |
23151 | 520 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n", |
521 info.short_name, strerror(errno)); | |
522 return TVI_CONTROL_FALSE; | |
10536 | 523 } |
524 | |
525 if (val_signed) { | |
23151 | 526 if (control->value < 0) { |
527 control->value = qctrl.default_value + control->value * | |
528 (qctrl.default_value - qctrl.minimum) / 100; | |
529 } else { | |
530 control->value = qctrl.default_value + control->value * | |
531 (qctrl.maximum - qctrl.default_value) / 100; | |
532 } | |
10536 | 533 } else { |
23151 | 534 if (control->value < 50) { |
535 control->value = qctrl.default_value + (control->value-50) * | |
536 (qctrl.default_value - qctrl.minimum) / 50; | |
537 } else { | |
538 control->value = qctrl.default_value + (control->value-50) * | |
539 (qctrl.maximum - qctrl.default_value) / 50; | |
540 } | |
10536 | 541 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
542 |
10536 | 543 |
544 if (ioctl(priv->video_fd, VIDIOC_S_CTRL, control) < 0) { | |
23151 | 545 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl set %s %d failed: %s\n", |
546 info.short_name, qctrl.name, control->value, strerror(errno)); | |
547 return TVI_CONTROL_FALSE; | |
10536 | 548 } |
549 mp_msg(MSGT_TV, MSGL_V, "%s: set %s: %d [%d, %d]\n", info.short_name, | |
550 qctrl.name, control->value, qctrl.minimum, qctrl.maximum); | |
551 | |
552 return TVI_CONTROL_TRUE; | |
553 } | |
554 | |
555 | |
556 /* | |
557 ** Scale the control values back to what mplayer needs. | |
558 */ | |
559 static int get_control(priv_t *priv, struct v4l2_control *control, int val_signed) { | |
23151 | 560 struct v4l2_queryctrl qctrl; |
10536 | 561 |
562 qctrl.id = control->id; | |
563 if (ioctl(priv->video_fd, VIDIOC_QUERYCTRL, &qctrl) < 0) { | |
23151 | 564 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query control failed: %s\n", |
565 info.short_name, strerror(errno)); | |
566 return TVI_CONTROL_FALSE; | |
10536 | 567 } |
568 | |
569 if (ioctl(priv->video_fd, VIDIOC_G_CTRL, control) < 0) { | |
23151 | 570 mp_msg(MSGT_TV, MSGL_ERR,"%s: ioctl get %s failed: %s\n", |
571 info.short_name, qctrl.name, strerror(errno)); | |
572 return TVI_CONTROL_FALSE; | |
10536 | 573 } |
574 mp_msg(MSGT_TV, MSGL_V, "%s: get %s: %d [%d, %d]\n", info.short_name, | |
575 qctrl.name, control->value, qctrl.minimum, qctrl.maximum); | |
576 | |
577 if (val_signed) { | |
23151 | 578 if (control->value < qctrl.default_value) { |
579 control->value = (control->value - qctrl.default_value) * 100 / | |
580 (qctrl.default_value - qctrl.minimum); | |
581 } else { | |
582 control->value = (control->value - qctrl.default_value) * 100 / | |
583 (qctrl.maximum - qctrl.default_value); | |
584 } | |
10536 | 585 } else { |
23151 | 586 if (control->value < qctrl.default_value) { |
587 control->value = (control->value - qctrl.default_value) * 50 / | |
588 (qctrl.default_value - qctrl.minimum) + 50; | |
589 } else { | |
590 control->value = (control->value - qctrl.default_value) * 50 / | |
591 (qctrl.maximum - qctrl.default_value) + 50; | |
592 } | |
10536 | 593 } |
594 | |
595 return TVI_CONTROL_TRUE; | |
596 } | |
597 | |
23901 | 598 static int vbi_init(priv_t* priv,char* device) |
599 { | |
600 int vbi_fd=0; | |
601 struct v4l2_capability cap; | |
602 struct v4l2_format fmt; | |
603 int res; | |
604 | |
605 if(!device) | |
606 return TVI_CONTROL_FALSE; | |
607 | |
608 priv->vbi_dev=strdup(device); | |
609 | |
610 vbi_fd=open(priv->vbi_dev,O_RDWR); | |
611 if(vbi_fd<0){ | |
612 mp_msg(MSGT_TV,MSGL_ERR,"vbi: could not open device %s\n",priv->vbi_dev); | |
613 return TVI_CONTROL_FALSE; | |
614 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
615 |
23901 | 616 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
|
617 mp_msg(MSGT_TV,MSGL_ERR,"vbi: Query capabilities failed for %s\n",priv->vbi_dev); |
23901 | 618 close(vbi_fd); |
619 return TVI_CONTROL_FALSE; | |
620 } | |
29949 | 621 if(!(cap.capabilities & V4L2_CAP_VBI_CAPTURE)){ |
23901 | 622 mp_msg(MSGT_TV,MSGL_ERR,"vbi: %s does not support VBI capture\n",priv->vbi_dev); |
623 close(vbi_fd); | |
624 return TVI_CONTROL_FALSE; | |
625 } | |
626 | |
627 memset(&fmt,0,sizeof(struct v4l2_format)); | |
628 fmt.type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
629 if((res=ioctl(vbi_fd,VIDIOC_G_FMT,&fmt))<0){ | |
630 mp_msg(MSGT_TV,MSGL_ERR,"vbi: Query format failed: %x\n",res); | |
631 close(vbi_fd); | |
632 return TVI_CONTROL_FALSE; | |
633 } | |
634 if(fmt.fmt.vbi.sample_format!=V4L2_PIX_FMT_GREY){ | |
635 mp_msg(MSGT_TV,MSGL_ERR,"vbi: format 0x%x is not supported\n",fmt.fmt.vbi.sample_format); | |
636 close(vbi_fd); | |
637 return TVI_CONTROL_FALSE; | |
638 } | |
639 priv->vbi_fd=vbi_fd; | |
640 mp_msg(MSGT_TV,MSGL_DBG3,"vbi: init ok\n"); | |
641 return TVI_CONTROL_TRUE; | |
642 } | |
643 | |
644 static int vbi_get_props(priv_t* priv,tt_stream_props* ptsp) | |
645 { | |
646 struct v4l2_format fmt; | |
647 int res; | |
648 if(!priv || !ptsp) | |
649 return TVI_CONTROL_FALSE; | |
650 | |
651 memset(&fmt,0,sizeof(struct v4l2_format)); | |
652 fmt.type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
653 if((res=ioctl(priv->vbi_fd,VIDIOC_G_FMT,&fmt))<0){ | |
654 mp_msg(MSGT_TV,MSGL_ERR,"vbi_get_props: Query format failed: %x\n",res); | |
655 return TVI_CONTROL_FALSE; | |
656 } | |
657 | |
658 ptsp->interlaced=(fmt.fmt.vbi.flags& V4L2_VBI_INTERLACED?1:0); | |
659 | |
660 ptsp->offset=fmt.fmt.vbi.offset; | |
661 ptsp->sampling_rate=fmt.fmt.vbi.sampling_rate; | |
662 ptsp->samples_per_line=fmt.fmt.vbi.samples_per_line, | |
663 | |
664 ptsp->count[0]=fmt.fmt.vbi.count[0]; | |
665 ptsp->count[1]=fmt.fmt.vbi.count[1]; | |
666 ptsp->bufsize = ptsp->samples_per_line * (ptsp->count[0] + ptsp->count[1]); | |
667 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
668 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 | 669 ptsp->sampling_rate, |
670 ptsp->offset, | |
671 ptsp->samples_per_line, | |
672 ptsp->interlaced?"Yes":"No", | |
673 ptsp->count[0], | |
674 ptsp->count[1]); | |
675 | |
676 return TVI_CONTROL_TRUE; | |
677 } | |
678 | |
679 static void *vbi_grabber(void *data) | |
680 { | |
681 priv_t *priv = (priv_t *) data; | |
682 int bytes,seq,prev_seq; | |
683 unsigned char* buf; | |
684 tt_stream_props tsp; | |
685 | |
686 if(!priv->priv_vbi){ | |
687 mp_msg(MSGT_TV,MSGL_WARN,"vbi: vbi not initialized. stopping thread.\n"); | |
688 return NULL; | |
689 } | |
690 | |
691 if(vbi_get_props(priv,&tsp)!=TVI_CONTROL_TRUE) | |
692 return NULL; | |
693 | |
694 buf=malloc(tsp.bufsize); | |
695 seq=0; | |
696 prev_seq=0; | |
697 mp_msg(MSGT_TV,MSGL_V,"vbi: vbi capture thread started.\n"); | |
698 | |
699 while (!priv->vbi_shutdown){ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
700 bytes=read(priv->vbi_fd,buf,tsp.bufsize); |
24763
5d7f6e5e0847
After receiving EINTR 'read' syscall should be restarted.
voroshil
parents:
24553
diff
changeset
|
701 if(bytes<0 && errno==EINTR) |
5d7f6e5e0847
After receiving EINTR 'read' syscall should be restarted.
voroshil
parents:
24553
diff
changeset
|
702 continue; |
23901 | 703 if (bytes!=tsp.bufsize){ |
23979 | 704 mp_msg(MSGT_TV,MSGL_WARN,"vbi: expecting bytes: %d, got: %d\n",tsp.bufsize,bytes); |
23901 | 705 break; |
706 } | |
707 seq=*(int*)(buf+bytes-4); | |
708 if(seq<=1) continue; | |
709 if (prev_seq && seq!=prev_seq+1){ | |
710 prev_seq=0; | |
711 seq=0; | |
712 } | |
713 prev_seq=seq; | |
714 teletext_control(priv->priv_vbi,TV_VBI_CONTROL_DECODE_PAGE,&buf); | |
715 mp_msg(MSGT_TV,MSGL_DBG3,"grabber: seq:%d\n",seq); | |
716 } | |
717 free(buf); | |
718 return NULL; | |
719 } | |
720 | |
10536 | 721 static int control(priv_t *priv, int cmd, void *arg) |
722 { | |
723 struct v4l2_control control; | |
724 struct v4l2_frequency frequency; | |
725 | |
726 switch(cmd) { | |
727 case TVI_CONTROL_IS_VIDEO: | |
23151 | 728 return priv->capability.capabilities & V4L2_CAP_VIDEO_CAPTURE? |
729 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
|
730 case TVI_CONTROL_IS_AUDIO: |
23886 | 731 if (priv->tv_param->force_audio) return TVI_CONTROL_TRUE; |
10536 | 732 case TVI_CONTROL_IS_TUNER: |
23151 | 733 return priv->capability.capabilities & V4L2_CAP_TUNER? |
734 TVI_CONTROL_TRUE: TVI_CONTROL_FALSE; | |
10536 | 735 case TVI_CONTROL_IMMEDIATE: |
23151 | 736 priv->immediate_mode = 1; |
737 return TVI_CONTROL_TRUE; | |
10536 | 738 case TVI_CONTROL_VID_GET_FPS: |
32668
cb671a73f3a3
Use getfps helper function everywhere, simplifies code and avoids a possible division by 0.
reimar
parents:
32511
diff
changeset
|
739 *(float *)arg = getfps(priv); |
23151 | 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 { | |
33644
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1380 uint64_t bufsize; |
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1381 int cnt; |
10536 | 1382 |
23886 | 1383 if (priv->tv_param->buffer_size >= 0) { |
1384 bufsize = priv->tv_param->buffer_size*1024*1024; | |
10536 | 1385 } else { |
1386 #ifdef HAVE_SYS_SYSINFO_H | |
23151 | 1387 struct sysinfo si; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1388 |
23151 | 1389 sysinfo(&si); |
33644
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1390 bufsize = (si.freeram/2)*si.mem_unit; |
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1391 if ( bufsize < 16*1024*1024) |
c15dabfa2380
Do a proper calculation of free RAM to be used as V4L buffers.
iive
parents:
33163
diff
changeset
|
1392 #endif |
23151 | 1393 bufsize = 16*1024*1024; |
10536 | 1394 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1395 |
23423 | 1396 cnt = bufsize/priv->format.fmt.pix.sizeimage; |
10536 | 1397 if (cnt < 2) cnt = 2; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1398 |
10536 | 1399 return cnt; |
1400 } | |
1401 | |
1402 /* that's the real start, we'got the format parameters (checked with control) */ | |
1403 static int start(priv_t *priv) | |
1404 { | |
1405 struct v4l2_requestbuffers request; | |
24346 | 1406 unsigned int i; |
10536 | 1407 |
1408 /* setup audio parameters */ | |
1409 | |
15464 | 1410 init_audio(priv); |
25962 | 1411 if (!priv->tv_param->noaudio && !priv->audio_initialized) return 0; |
15464 | 1412 |
10536 | 1413 /* we need this to size the audio buffer properly */ |
1414 if (priv->immediate_mode) { | |
23151 | 1415 priv->video_buffer_size_max = 2; |
10536 | 1416 } else { |
23151 | 1417 priv->video_buffer_size_max = get_capture_buffer_size(priv); |
10536 | 1418 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1419 |
23886 | 1420 if (!priv->tv_param->noaudio) { |
23151 | 1421 setup_audio_buffer_sizes(priv); |
1422 priv->audio_skew_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1423 if (!priv->audio_skew_buffer) { | |
1424 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1425 return 0; | |
1426 } | |
1427 priv->audio_skew_delta_buffer = calloc(priv->aud_skew_cnt, sizeof(long long)); | |
1428 if (!priv->audio_skew_delta_buffer) { | |
1429 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate skew buffer: %s\n", strerror(errno)); | |
1430 return 0; | |
1431 } | |
10536 | 1432 |
23151 | 1433 priv->audio_ringbuffer = calloc(priv->audio_in.blocksize, priv->audio_buffer_size); |
1434 if (!priv->audio_ringbuffer) { | |
1435 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate audio buffer: %s\n", strerror(errno)); | |
1436 return 0; | |
1437 } | |
10536 | 1438 |
23151 | 1439 priv->audio_secs_per_block = (double)priv->audio_in.blocksize/(priv->audio_in.samplerate |
1440 *priv->audio_in.channels | |
1441 *priv->audio_in.bytes_per_sample); | |
1442 priv->audio_usecs_per_block = 1e6*priv->audio_secs_per_block; | |
1443 priv->audio_head = 0; | |
1444 priv->audio_tail = 0; | |
1445 priv->audio_cnt = 0; | |
1446 priv->audio_drop = 0; | |
1447 priv->audio_skew = 0; | |
1448 priv->audio_skew_total = 0; | |
1449 priv->audio_skew_delta_total = 0; | |
1450 priv->audio_recv_blocks_total = 0; | |
1451 priv->audio_sent_blocks_total = 0; | |
1452 priv->audio_null_blocks_inserted = 0; | |
1453 priv->audio_insert_null_samples = 0; | |
1454 priv->dropped_frames_timeshift = 0; | |
1455 priv->dropped_frames_compensated = 0; | |
15449 | 1456 |
23151 | 1457 pthread_mutex_init(&priv->skew_mutex, NULL); |
1458 pthread_mutex_init(&priv->audio_mutex, NULL); | |
10536 | 1459 } |
1460 | |
1461 /* setup video parameters */ | |
23886 | 1462 if (!priv->tv_param->noaudio) { |
32668
cb671a73f3a3
Use getfps helper function everywhere, simplifies code and avoids a possible division by 0.
reimar
parents:
32511
diff
changeset
|
1463 if (priv->video_buffer_size_max < 3*getfps(priv)*priv->audio_secs_per_block) { |
23151 | 1464 mp_msg(MSGT_TV, MSGL_ERR, "Video buffer shorter than 3 times audio frame duration.\n" |
1465 "You will probably experience heavy framedrops.\n"); | |
1466 } | |
10536 | 1467 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1468 |
10536 | 1469 { |
23151 | 1470 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
|
1471 |
23151 | 1472 mp_msg(MSGT_TV, MSGL_V, "Using a ring buffer for maximum %d frames, %d MB total size.\n", |
1473 priv->video_buffer_size_max, | |
1474 priv->video_buffer_size_max*priv->format.fmt.pix.height*bytesperline/(1024*1024)); | |
10536 | 1475 } |
1476 | |
23423 | 1477 priv->video_ringbuffer = calloc(priv->video_buffer_size_max, sizeof(video_buffer_entry)); |
10536 | 1478 if (!priv->video_ringbuffer) { |
23151 | 1479 mp_msg(MSGT_TV, MSGL_ERR, "cannot allocate video buffer: %s\n", strerror(errno)); |
1480 return 0; | |
10536 | 1481 } |
23423 | 1482 memset(priv->video_ringbuffer,0,priv->video_buffer_size_max * sizeof(video_buffer_entry)); |
10536 | 1483 |
15449 | 1484 pthread_mutex_init(&priv->video_buffer_mutex, NULL); |
1485 | |
10536 | 1486 priv->video_head = 0; |
1487 priv->video_tail = 0; | |
1488 priv->video_cnt = 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1489 |
10536 | 1490 /* request buffers */ |
1491 if (priv->immediate_mode) { | |
23151 | 1492 request.count = 2; |
10536 | 1493 } else { |
23151 | 1494 request.count = BUFFER_COUNT; |
10536 | 1495 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1496 |
10536 | 1497 request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1498 request.memory = V4L2_MEMORY_MMAP; | |
1499 if (ioctl(priv->video_fd, VIDIOC_REQBUFS, &request) < 0) { | |
23151 | 1500 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl request buffers failed: %s\n", |
1501 info.short_name, strerror(errno)); | |
1502 return 0; | |
10536 | 1503 } |
1504 | |
1505 /* query buffers */ | |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18176
diff
changeset
|
1506 if (!(priv->map = calloc(request.count, sizeof(struct map)))) { |
23151 | 1507 mp_msg(MSGT_TV, MSGL_ERR, "%s: malloc capture buffers failed: %s\n", |
1508 info.short_name, strerror(errno)); | |
1509 return 0; | |
10536 | 1510 } |
1511 | |
1512 /* map and queue buffers */ | |
1513 for (i = 0; i < request.count; i++) { | |
23151 | 1514 memset(&priv->map[i].buf,0,sizeof(priv->map[i].buf)); |
1515 priv->map[i].buf.index = i; | |
1516 priv->map[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1517 priv->map[i].buf.memory = V4L2_MEMORY_MMAP; | |
1518 if (ioctl(priv->video_fd, VIDIOC_QUERYBUF, &(priv->map[i].buf)) < 0) { | |
1519 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s\n", | |
1520 info.short_name, strerror(errno)); | |
1521 free(priv->map); | |
1522 priv->map = NULL; | |
1523 return 0; | |
1524 } | |
1525 priv->map[i].addr = mmap (0, priv->map[i].buf.length, PROT_READ | | |
1526 PROT_WRITE, MAP_SHARED, priv->video_fd, priv->map[i].buf.m.offset); | |
1527 if (priv->map[i].addr == MAP_FAILED) { | |
1528 mp_msg(MSGT_TV, MSGL_ERR, "%s: mmap capture buffer failed: %s\n", | |
1529 info.short_name, strerror(errno)); | |
1530 priv->map[i].len = 0; | |
1531 return 0; | |
1532 } | |
1533 priv->map[i].len = priv->map[i].buf.length; | |
1534 /* count up to make sure this is correct everytime */ | |
1535 priv->mapcount++; | |
10536 | 1536 |
23151 | 1537 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { |
1538 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1539 info.short_name, strerror(errno)); | |
1540 return 0; | |
1541 } | |
10536 | 1542 } |
1543 | |
23901 | 1544 /* start vbi thread */ |
1545 if(priv->priv_vbi){ | |
1546 priv->vbi_shutdown = 0; | |
1547 pthread_create(&priv->vbi_grabber_thread, NULL, vbi_grabber, priv); | |
1548 } | |
10536 | 1549 /* start audio thread */ |
1550 priv->shutdown = 0; | |
1551 priv->audio_skew_measure_time = 0; | |
1552 priv->first_frame = 0; | |
1553 priv->audio_skew = 0; | |
1554 priv->first = 1; | |
1555 | |
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
|
1556 set_mute(priv, 0); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1557 |
10536 | 1558 return 1; |
1559 } | |
1560 | |
1561 // copies a video frame | |
23423 | 1562 static inline void copy_frame(priv_t *priv, video_buffer_entry *dest, unsigned char *source,int len) |
10536 | 1563 { |
23423 | 1564 dest->framesize=len; |
23886 | 1565 if(priv->tv_param->automute>0){ |
23422 | 1566 if (ioctl(priv->video_fd, VIDIOC_G_TUNER, &priv->tuner) >= 0) { |
23886 | 1567 if(priv->tv_param->automute<<8>priv->tuner.signal){ |
23423 | 1568 fill_blank_frame(dest->data,dest->framesize,fcc_vl2mp(priv->format.fmt.pix.pixelformat)); |
23422 | 1569 set_mute(priv,1); |
1570 return; | |
1571 } | |
1572 } | |
1573 set_mute(priv,0); | |
1574 } | |
23423 | 1575 memcpy(dest->data, source, len); |
10536 | 1576 } |
1577 | |
1578 // maximum skew change, in frames | |
1579 #define MAX_SKEW_DELTA 0.6 | |
1580 static void *video_grabber(void *data) | |
1581 { | |
1582 priv_t *priv = (priv_t*)data; | |
15449 | 1583 long long skew, prev_skew, xskew, interval, prev_interval, delta; |
10536 | 1584 int i; |
23423 | 1585 int framesize = priv->format.fmt.pix.sizeimage; |
10536 | 1586 fd_set rdset; |
1587 struct timeval timeout; | |
1588 struct v4l2_buffer buf; | |
1589 | |
1590 xskew = 0; | |
1591 skew = 0; | |
1592 interval = 0; | |
1593 prev_interval = 0; | |
1594 prev_skew = 0; | |
1595 | |
1596 mp_msg(MSGT_TV, MSGL_V, "%s: going to capture\n", info.short_name); | |
1597 if (ioctl(priv->video_fd, VIDIOC_STREAMON, &(priv->format.type)) < 0) { | |
23151 | 1598 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl streamon failed: %s\n", |
1599 info.short_name, strerror(errno)); | |
1600 return 0; | |
10536 | 1601 } |
1602 priv->streamon = 1; | |
1603 | |
23886 | 1604 if (!priv->tv_param->noaudio) { |
23151 | 1605 pthread_create(&priv->audio_grabber_thread, NULL, audio_grabber, priv); |
10536 | 1606 } |
1607 | |
1608 for (priv->frames = 0; !priv->shutdown;) | |
1609 { | |
23151 | 1610 int ret; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1611 |
23151 | 1612 if (priv->immediate_mode) { |
1613 while (priv->video_cnt == priv->video_buffer_size_max) { | |
1614 usleep(10000); | |
1615 if (priv->shutdown) { | |
1616 return NULL; | |
1617 } | |
1618 } | |
1619 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1620 |
23151 | 1621 FD_ZERO (&rdset); |
1622 FD_SET (priv->video_fd, &rdset); | |
10536 | 1623 |
23151 | 1624 timeout.tv_sec = 1; |
1625 timeout.tv_usec = 0; | |
10536 | 1626 |
23151 | 1627 i = select(priv->video_fd + 1, &rdset, NULL, NULL, &timeout); |
1628 if (i < 0) { | |
1629 mp_msg(MSGT_TV, MSGL_ERR, "%s: select failed: %s\n", | |
1630 info.short_name, strerror(errno)); | |
1631 continue; | |
1632 } | |
1633 else if (i == 0) { | |
1634 mp_msg(MSGT_TV, MSGL_ERR, "%s: select timeout\n", info.short_name); | |
1635 continue; | |
1636 } | |
1637 else if (!FD_ISSET(priv->video_fd, &rdset)) { | |
1638 continue; | |
1639 } | |
10536 | 1640 |
23151 | 1641 memset(&buf,0,sizeof(buf)); |
1642 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1643 buf.memory = V4L2_MEMORY_MMAP; | |
1644 ret = ioctl(priv->video_fd, VIDIOC_DQBUF, &buf); | |
10536 | 1645 |
23151 | 1646 if (ret < 0) { |
1647 /* | |
1648 if there's no signal, the buffer might me dequeued | |
1649 so we query all the buffers to see which one we should | |
1650 put back to queue | |
10536 | 1651 |
23151 | 1652 observed with saa7134 0.2.8 |
1653 don't know if is it a bug or (mis)feature | |
1654 */ | |
1655 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl dequeue buffer failed: %s, idx = %d\n", | |
1656 info.short_name, strerror(errno), buf.index); | |
1657 for (i = 0; i < priv->mapcount; i++) { | |
1658 memset(&buf,0,sizeof(buf)); | |
1659 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1660 buf.memory = V4L2_MEMORY_MMAP; | |
1661 buf.index = i; | |
1662 ret = ioctl(priv->video_fd, VIDIOC_QUERYBUF, &buf); | |
1663 if (ret < 0) { | |
1664 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl query buffer failed: %s, idx = %d\n", | |
1665 info.short_name, strerror(errno), buf.index); | |
1666 return 0; | |
1667 } | |
1668 if ((buf.flags & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE)) == V4L2_BUF_FLAG_MAPPED) { | |
1669 if (ioctl(priv->video_fd, VIDIOC_QBUF, &(priv->map[i].buf)) < 0) { | |
1670 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1671 info.short_name, strerror(errno)); | |
1672 return 0; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1673 } |
23151 | 1674 } |
1675 } | |
1676 continue; | |
1677 } | |
10536 | 1678 |
23151 | 1679 /* store the timestamp of the very first frame as reference */ |
1680 if (!priv->frames++) { | |
23886 | 1681 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1682 priv->first_frame = (long long)1e6*buf.timestamp.tv_sec + buf.timestamp.tv_usec; |
23886 | 1683 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1684 } |
1685 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
|
1686 // fprintf(stderr, "idx = %d, ts = %f\n", buf.index, (double)(priv->curr_frame) / 1e6); |
10536 | 1687 |
23151 | 1688 interval = priv->curr_frame - priv->first_frame; |
1689 delta = interval - prev_interval; | |
10536 | 1690 |
23151 | 1691 if (!priv->immediate_mode) { |
1692 // interpolate the skew in time | |
23886 | 1693 if (!priv->tv_param->noaudio) pthread_mutex_lock(&priv->skew_mutex); |
23151 | 1694 xskew = priv->audio_skew + (interval - priv->audio_skew_measure_time)*priv->audio_skew_factor; |
23886 | 1695 if (!priv->tv_param->noaudio) pthread_mutex_unlock(&priv->skew_mutex); |
23151 | 1696 // correct extreme skew changes to avoid (especially) moving backwards in time |
1697 if (xskew - prev_skew > delta*MAX_SKEW_DELTA) { | |
1698 skew = prev_skew + delta*MAX_SKEW_DELTA; | |
1699 } else if (xskew - prev_skew < -delta*MAX_SKEW_DELTA) { | |
1700 skew = prev_skew - delta*MAX_SKEW_DELTA; | |
1701 } else { | |
1702 skew = xskew; | |
1703 } | |
1704 } | |
10536 | 1705 |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1706 mp_msg(MSGT_TV, MSGL_DBG3, "\nfps = %f, interval = %f, a_skew = %f, corr_skew = %f\n", |
23151 | 1707 delta ? (double)1e6/delta : -1, |
1708 (double)1e-6*interval, (double)1e-6*xskew, (double)1e-6*skew); | |
1709 mp_msg(MSGT_TV, MSGL_DBG3, "vcnt = %d, acnt = %d\n", priv->video_cnt, priv->audio_cnt); | |
10536 | 1710 |
23151 | 1711 prev_skew = skew; |
1712 prev_interval = interval; | |
10536 | 1713 |
23151 | 1714 /* allocate a new buffer, if needed */ |
1715 pthread_mutex_lock(&priv->video_buffer_mutex); | |
1716 if (priv->video_buffer_size_current < priv->video_buffer_size_max) { | |
1717 if (priv->video_cnt == priv->video_buffer_size_current) { | |
1718 unsigned char *newbuf = malloc(framesize); | |
1719 if (newbuf) { | |
1720 memmove(priv->video_ringbuffer+priv->video_tail+1, priv->video_ringbuffer+priv->video_tail, | |
23423 | 1721 (priv->video_buffer_size_current-priv->video_tail)*sizeof(video_buffer_entry)); |
1722 priv->video_ringbuffer[priv->video_tail].data = newbuf; | |
23151 | 1723 if ((priv->video_head >= priv->video_tail) && (priv->video_cnt > 0)) priv->video_head++; |
1724 priv->video_buffer_size_current++; | |
1725 } | |
1726 } | |
1727 } | |
1728 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
10536 | 1729 |
23151 | 1730 if (priv->video_cnt == priv->video_buffer_size_current) { |
1731 if (!priv->immediate_mode) { | |
1732 mp_msg(MSGT_TV, MSGL_ERR, "\nvideo buffer full - dropping frame\n"); | |
1733 if (priv->audio_insert_null_samples) { | |
1734 pthread_mutex_lock(&priv->audio_mutex); | |
1735 priv->dropped_frames_timeshift += delta; | |
1736 pthread_mutex_unlock(&priv->audio_mutex); | |
1737 } | |
1738 } | |
1739 } else { | |
1740 if (priv->immediate_mode) { | |
23423 | 1741 priv->video_ringbuffer[priv->video_tail].timestamp = 0; |
23151 | 1742 } else { |
1743 // compensate for audio skew | |
1744 // negative skew => there are more audio samples, increase interval | |
1745 // positive skew => less samples, shorten the interval | |
23423 | 1746 priv->video_ringbuffer[priv->video_tail].timestamp = interval - skew; |
1747 if (priv->audio_insert_null_samples && priv->video_ringbuffer[priv->video_tail].timestamp > 0) { | |
23151 | 1748 pthread_mutex_lock(&priv->audio_mutex); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1749 priv->video_ringbuffer[priv->video_tail].timestamp += |
23151 | 1750 (priv->audio_null_blocks_inserted |
1751 - priv->dropped_frames_timeshift/priv->audio_usecs_per_block) | |
1752 *priv->audio_usecs_per_block; | |
1753 pthread_mutex_unlock(&priv->audio_mutex); | |
1754 } | |
1755 } | |
23423 | 1756 copy_frame(priv, priv->video_ringbuffer+priv->video_tail, priv->map[buf.index].addr,buf.bytesused); |
23151 | 1757 priv->video_tail = (priv->video_tail+1)%priv->video_buffer_size_current; |
1758 priv->video_cnt++; | |
1759 } | |
1760 if (ioctl(priv->video_fd, VIDIOC_QBUF, &buf) < 0) { | |
1761 mp_msg(MSGT_TV, MSGL_ERR, "%s: ioctl queue buffer failed: %s\n", | |
1762 info.short_name, strerror(errno)); | |
1763 return 0; | |
1764 } | |
10536 | 1765 } |
1766 return NULL; | |
1767 } | |
1768 | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1769 #define MAX_LOOP 50 |
10536 | 1770 static double grab_video_frame(priv_t *priv, char *buffer, int len) |
1771 { | |
1772 double interval; | |
16962
bdc218b5a49a
Do not hang forever when the card delivers no new data.
reimar
parents:
16536
diff
changeset
|
1773 int loop_cnt = 0; |
10536 | 1774 |
1775 if (priv->first) { | |
23151 | 1776 pthread_create(&priv->video_grabber_thread, NULL, video_grabber, priv); |
1777 priv->first = 0; | |
10536 | 1778 } |
1779 | |
1780 while (priv->video_cnt == 0) { | |
23151 | 1781 usleep(10000); |
1782 if (loop_cnt++ > MAX_LOOP) return 0; | |
10536 | 1783 } |
1784 | |
1785 pthread_mutex_lock(&priv->video_buffer_mutex); | |
23423 | 1786 interval = (double)priv->video_ringbuffer[priv->video_head].timestamp*1e-6; |
1787 memcpy(buffer, priv->video_ringbuffer[priv->video_head].data, len); | |
10536 | 1788 priv->video_cnt--; |
1789 priv->video_head = (priv->video_head+1)%priv->video_buffer_size_current; | |
1790 pthread_mutex_unlock(&priv->video_buffer_mutex); | |
1791 | |
1792 return interval; | |
1793 } | |
1794 | |
1795 static int get_video_framesize(priv_t *priv) | |
1796 { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1797 /* |
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1798 this routine will be called before grab_video_frame |
23423 | 1799 thus let's return topmost frame's size |
1800 */ | |
1801 if (priv->video_cnt) | |
1802 return priv->video_ringbuffer[priv->video_head].framesize; | |
1803 /* | |
1804 no video frames yet available. i don't know what to do in this case, | |
1805 thus let's return some fallback result (for compressed format this will be | |
1806 maximum allowed frame size. | |
1807 */ | |
10536 | 1808 return priv->format.fmt.pix.sizeimage; |
1809 } | |
1810 | |
10704 | 1811 //#define DOUBLESPEED |
1812 #ifdef DOUBLESPEED | |
10536 | 1813 // for testing purposes only |
1814 static void read_doublespeed(priv_t *priv) | |
1815 { | |
18885 | 1816 char *bufx = calloc(priv->audio_in.blocksize, 2); |
10536 | 1817 short *s; |
1818 short *d; | |
1819 int i; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1820 |
10536 | 1821 audio_in_read_chunk(&priv->audio_in, bufx); |
1822 audio_in_read_chunk(&priv->audio_in, bufx+priv->audio_in.blocksize); | |
1823 | |
1824 s = bufx; | |
1825 d = priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize; | |
1826 for (i = 0; i < priv->audio_in.blocksize/2; i++) { | |
23151 | 1827 *d++ = *s++; |
1828 *s++; | |
10536 | 1829 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1830 |
10536 | 1831 } |
10704 | 1832 #endif |
10536 | 1833 |
1834 static void *audio_grabber(void *data) | |
1835 { | |
1836 priv_t *priv = (priv_t*)data; | |
1837 struct timeval tv; | |
1838 int i, audio_skew_ptr = 0; | |
10653 | 1839 long long current_time, prev_skew = 0, prev_skew_uncorr = 0; |
10852 | 1840 long long start_time_avg; |
10536 | 1841 |
1842 gettimeofday(&tv, NULL); | |
10852 | 1843 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec; |
10536 | 1844 audio_in_start_capture(&priv->audio_in); |
1845 for (i = 0; i < priv->aud_skew_cnt; i++) | |
23151 | 1846 priv->audio_skew_buffer[i] = 0; |
10653 | 1847 for (i = 0; i < priv->aud_skew_cnt; i++) |
23151 | 1848 priv->audio_skew_delta_buffer[i] = 0; |
10536 | 1849 |
1850 for (; !priv->shutdown;) | |
1851 { | |
10704 | 1852 #ifdef DOUBLESPEED |
23151 | 1853 read_doublespeed(priv); |
10704 | 1854 #else |
23151 | 1855 if (audio_in_read_chunk(&priv->audio_in, priv->audio_ringbuffer+priv->audio_tail*priv->audio_in.blocksize) < 0) |
1856 continue; | |
10704 | 1857 #endif |
23151 | 1858 pthread_mutex_lock(&priv->skew_mutex); |
1859 if (priv->first_frame == 0) { | |
1860 // there is no first frame yet (unlikely to happen) | |
1861 gettimeofday(&tv, NULL); | |
1862 start_time_avg = priv->audio_start_time = (long long)1e6*tv.tv_sec + tv.tv_usec; | |
1863 // fprintf(stderr, "warning - first frame not yet available!\n"); | |
1864 pthread_mutex_unlock(&priv->skew_mutex); | |
1865 continue; | |
1866 } | |
1867 pthread_mutex_unlock(&priv->skew_mutex); | |
10536 | 1868 |
23151 | 1869 gettimeofday(&tv, NULL); |
10536 | 1870 |
23151 | 1871 priv->audio_recv_blocks_total++; |
1872 current_time = (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_start_time; | |
10536 | 1873 |
23151 | 1874 if (priv->audio_recv_blocks_total < priv->aud_skew_cnt*2) { |
1875 start_time_avg += (long long)1e6*tv.tv_sec + tv.tv_usec - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1876 priv->audio_start_time = start_time_avg/(priv->audio_recv_blocks_total+1); | |
1877 } | |
10852 | 1878 |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1879 // fprintf(stderr, "spb = %f, bs = %d, skew = %f\n", priv->audio_secs_per_block, priv->audio_in.blocksize, |
23151 | 1880 // (double)(current_time - 1e6*priv->audio_secs_per_block*priv->audio_recv_blocks_total)/1e6); |
10536 | 1881 |
23151 | 1882 // put the current skew into the ring buffer |
1883 priv->audio_skew_total -= priv->audio_skew_buffer[audio_skew_ptr]; | |
1884 priv->audio_skew_buffer[audio_skew_ptr] = current_time | |
1885 - priv->audio_usecs_per_block*priv->audio_recv_blocks_total; | |
1886 priv->audio_skew_total += priv->audio_skew_buffer[audio_skew_ptr]; | |
10536 | 1887 |
23151 | 1888 pthread_mutex_lock(&priv->skew_mutex); |
10704 | 1889 |
23151 | 1890 // skew calculation |
10704 | 1891 |
23151 | 1892 // compute the sliding average of the skews |
1893 if (priv->audio_recv_blocks_total > priv->aud_skew_cnt) { | |
1894 priv->audio_skew = priv->audio_skew_total/priv->aud_skew_cnt; | |
1895 } else { | |
1896 priv->audio_skew = priv->audio_skew_total/priv->audio_recv_blocks_total; | |
1897 } | |
10653 | 1898 |
23151 | 1899 // put the current skew change (skew-prev_skew) into the ring buffer |
1900 priv->audio_skew_delta_total -= priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1901 priv->audio_skew_delta_buffer[audio_skew_ptr] = priv->audio_skew - prev_skew_uncorr; | |
1902 priv->audio_skew_delta_total += priv->audio_skew_delta_buffer[audio_skew_ptr]; | |
1903 prev_skew_uncorr = priv->audio_skew; // remember the _uncorrected_ average value | |
10704 | 1904 |
23151 | 1905 audio_skew_ptr = (audio_skew_ptr+1) % priv->aud_skew_cnt; // rotate the buffer pointer |
10653 | 1906 |
23151 | 1907 // sliding average approximates the value in the middle of the interval |
1908 // so interpolate the skew value further to the current time | |
1909 priv->audio_skew += priv->audio_skew_delta_total/2; | |
10653 | 1910 |
23151 | 1911 // now finally, priv->audio_skew contains fairly good approximation |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1912 // of the current value |
10653 | 1913 |
23151 | 1914 // current skew factor (assuming linearity) |
1915 // used for further interpolation in video_grabber | |
1916 // probably overkill but seems to be necessary for | |
1917 // stress testing by dropping half of the audio frames ;) | |
1918 // especially when using ALSA with large block sizes | |
1919 // where audio_skew remains a long while behind | |
1920 if ((priv->audio_skew_measure_time != 0) && (current_time - priv->audio_skew_measure_time != 0)) { | |
1921 priv->audio_skew_factor = (double)(priv->audio_skew-prev_skew)/(current_time - priv->audio_skew_measure_time); | |
1922 } else { | |
1923 priv->audio_skew_factor = 0.0; | |
1924 } | |
10852 | 1925 |
23151 | 1926 priv->audio_skew_measure_time = current_time; |
1927 prev_skew = priv->audio_skew; | |
1928 priv->audio_skew += priv->audio_start_time - priv->first_frame; | |
1929 pthread_mutex_unlock(&priv->skew_mutex); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29121
diff
changeset
|
1930 |
32352
76f94c00a69f
1000l, %lf is not valid format string for printf (only for scanf) and actually
reimar
parents:
32141
diff
changeset
|
1931 // fprintf(stderr, "audio_skew = %f, delta = %f\n", (double)priv->audio_skew/1e6, (double)priv->audio_skew_delta_total/1e6); |
10851 | 1932 |
23151 | 1933 pthread_mutex_lock(&priv->audio_mutex); |
1934 if ((priv->audio_tail+1) % priv->audio_buffer_size == priv->audio_head) { | |
1935 mp_msg(MSGT_TV, MSGL_ERR, "\ntoo bad - dropping audio frame !\n"); | |
1936 priv->audio_drop++; | |
1937 } else { | |
1938 priv->audio_tail = (priv->audio_tail+1) % priv->audio_buffer_size; | |
1939 priv->audio_cnt++; | |
1940 } | |
1941 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1942 } |
1943 return NULL; | |
1944 } | |
1945 | |
1946 static double grab_audio_frame(priv_t *priv, char *buffer, int len) | |
1947 { | |
1948 mp_dbg(MSGT_TV, MSGL_DBG2, "grab_audio_frame(priv=%p, buffer=%p, len=%d)\n", | |
23151 | 1949 priv, buffer, len); |
10536 | 1950 |
15449 | 1951 // hack: if grab_audio_frame is called first, it means we are used by mplayer |
1952 // => switch to the mode which outputs audio immediately, even if | |
1953 // it should be silence | |
1954 if (priv->first) priv->audio_insert_null_samples = 1; | |
1955 | |
1956 pthread_mutex_lock(&priv->audio_mutex); | |
1957 while (priv->audio_insert_null_samples | |
23151 | 1958 && priv->dropped_frames_timeshift - priv->dropped_frames_compensated >= priv->audio_usecs_per_block) { |
1959 // some frames were dropped - drop the corresponding number of audio blocks | |
1960 if (priv->audio_drop) { | |
1961 priv->audio_drop--; | |
1962 } else { | |
1963 if (priv->audio_head == priv->audio_tail) break; | |
1964 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1965 } | |
1966 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
|
1967 } |
80402283a017
Fix immediatemode with mplayer (ie playing both sound and video)
albeu
parents:
10735
diff
changeset
|
1968 |
10536 | 1969 // compensate for dropped audio frames |
1970 if (priv->audio_drop && (priv->audio_head == priv->audio_tail)) { | |
23151 | 1971 priv->audio_drop--; |
1972 memset(buffer, 0, len); | |
1973 goto out; | |
10536 | 1974 } |
1975 | |
15449 | 1976 if (priv->audio_insert_null_samples && (priv->audio_head == priv->audio_tail)) { |
23151 | 1977 // return silence to avoid desync and stuttering |
1978 memset(buffer, 0, len); | |
1979 priv->audio_null_blocks_inserted++; | |
1980 goto out; | |
15449 | 1981 } |
1982 | |
1983 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1984 while (priv->audio_head == priv->audio_tail) { |
23151 | 1985 // this is mencoder => just wait until some audio is available |
1986 usleep(10000); | |
10536 | 1987 } |
15451 | 1988 pthread_mutex_lock(&priv->audio_mutex); |
10536 | 1989 memcpy(buffer, priv->audio_ringbuffer+priv->audio_head*priv->audio_in.blocksize, len); |
1990 priv->audio_head = (priv->audio_head+1) % priv->audio_buffer_size; | |
1991 priv->audio_cnt--; | |
15449 | 1992 out: |
1993 pthread_mutex_unlock(&priv->audio_mutex); | |
10536 | 1994 priv->audio_sent_blocks_total++; |
1995 return (double)priv->audio_sent_blocks_total*priv->audio_secs_per_block; | |
1996 } | |
1997 | |
1998 static int get_audio_framesize(priv_t *priv) | |
1999 { | |
26756
c43ce7268677
cosmetics: Remove useless parentheses from return statements.
diego
parents:
25962
diff
changeset
|
2000 return priv->audio_in.blocksize; |
10536 | 2001 } |