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