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