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