14 #include <sys/types.h>
16 #include <sys/ioctl.h>
20 #include <linux/videodev2.h>
22 #include "libmythbase/mythconfig.h"
35 #define LOC QString("MPEGRec[%1](%2): ") \
36 .arg(m_tvrec ? m_tvrec->GetInputId() : -1).arg(m_videodevice)
40 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
45 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
50 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
55 "MPEG-2 PS",
"MPEG-2 TS",
"MPEG-1 VCD",
"PES AV",
56 "",
"PES V",
"",
"PES A",
58 "SVCD",
"DVD-Special 1",
"DVD-Special 2"
63 "Square",
"4:3",
"16:9",
"2.21:1"
92 static int find_index(
const std::array<const int,14> &audio_rate,
int value)
94 for (
uint i = 0; i < audio_rate.size(); i++)
96 if (audio_rate[i] == value)
107 else if (opt ==
"height")
109 else if (opt ==
"mpeg2bitrate")
111 else if (opt ==
"mpeg2maxbitrate")
113 else if (opt ==
"samplerate")
115 else if (opt ==
"mpeg2audbitratel1")
122 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Audiorate(L1): " +
123 QString(
"%1 is invalid").arg(value));
126 else if (opt ==
"mpeg2audbitratel2")
133 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Audiorate(L2): " +
134 QString(
"%1 is invalid").arg(value));
137 else if (opt ==
"mpeg2audbitratel3")
144 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Audiorate(L2): " +
145 QString(
"%1 is invalid").arg(value));
148 else if (opt ==
"mpeg2audvolume")
150 else if (opt.endsWith(
"_mpeg4avgbitrate"))
152 if (opt.startsWith(
"low"))
154 else if (opt.startsWith(
"medium"))
156 else if (opt.startsWith(
"high"))
161 else if (opt.endsWith(
"_mpeg4peakbitrate"))
163 if (opt.startsWith(
"low"))
165 else if (opt.startsWith(
"medium"))
167 else if (opt.startsWith(
"high"))
178 std::string value_ss = value.toStdString();
179 if (opt ==
"mpeg2streamtype")
208 LOG(VB_GENERAL, LOG_ERR,
LOC +
"MPEG2 stream type: " +
209 QString(
"%1 is invalid").arg(value));
212 else if (opt ==
"mpeg2language")
218 LOG(VB_GENERAL, LOG_ERR,
LOC +
"MPEG2 language (stereo) flag " +
219 QString(
"'%1' is invalid").arg(value));
222 else if (opt ==
"mpeg2aspectratio")
237 LOG(VB_GENERAL, LOG_ERR,
LOC +
"MPEG2 Aspect-ratio: " +
238 QString(
"%1 is invalid").arg(value));
241 else if (opt ==
"mpeg2audtype")
243 if (value ==
"Layer I")
244 m_audType = V4L2_MPEG_AUDIO_ENCODING_LAYER_1 + 1;
245 else if (value ==
"Layer II")
246 m_audType = V4L2_MPEG_AUDIO_ENCODING_LAYER_2 + 1;
247 else if (value ==
"Layer III")
248 m_audType = V4L2_MPEG_AUDIO_ENCODING_LAYER_3 + 1;
251 LOG(VB_GENERAL, LOG_ERR,
LOC +
"MPEG2 audio layer: " +
252 QString(
"%1 is invalid").arg(value));
255 else if (opt ==
"audiocodec")
257 if (value ==
"AAC Hardware Encoder")
258 m_audType = V4L2_MPEG_AUDIO_ENCODING_AAC + 1;
259 else if (value ==
"AC3 Hardware Encoder")
260 m_audType = V4L2_MPEG_AUDIO_ENCODING_AC3 + 1;
269 const QString &videodev,
270 const QString &audiodev,
271 const QString &vbidev)
276 if (videodev.startsWith(
"file:", Qt::CaseInsensitive))
280 QString newVideoDev = videodev;
281 if (newVideoDev.startsWith(
"file:", Qt::CaseInsensitive))
282 newVideoDev = newVideoDev.remove(0,5);
344 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"Can't open MPEG File '%1'")
346 m_error =
LOC + QString(
"Can't open MPEG File '%1'")
359 m_chanfd = open(vdevice.constData(), O_RDWR);
362 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Can't open video device. " +
ENO);
369 bool supports_tuner =
false;
370 bool supports_audio =
false;
371 uint32_t capabilities = 0;
375 supports_tuner = ((capabilities & V4L2_CAP_TUNER) != 0U);
376 supports_audio = ((capabilities & V4L2_CAP_AUDIO) != 0U);
385 if (!(capabilities & V4L2_CAP_VIDEO_CAPTURE))
387 LOG(VB_GENERAL, LOG_ERR,
LOC +
"V4L version 1, unsupported");
420 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Can't open video device." +
ENO);
440 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to allocate DRB buffer");
441 m_error =
"Failed to allocate DRB buffer";
451 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Failed to setup DRB buffer");
452 m_error =
"Failed to setup DRB buffer";
460 LOG(VB_RECORD, LOG_INFO,
LOC +
"DRB ready");
474 struct v4l2_format vfmt {};
476 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
478 if (ioctl(chanfd, VIDIOC_G_FMT, &vfmt) < 0)
480 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Error getting format" +
ENO);
488 if (ioctl(chanfd, VIDIOC_S_FMT, &vfmt) < 0)
490 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Error setting format" +
ENO);
501 struct v4l2_tuner vt {};
502 if (ioctl(chanfd, VIDIOC_G_TUNER, &vt) < 0)
504 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Unable to get audio mode" +
ENO);
511 vt.audmode = V4L2_TUNER_MODE_LANG1;
514 vt.audmode = V4L2_TUNER_MODE_LANG2;
517 vt.audmode = V4L2_TUNER_MODE_LANG1_LANG2;
520 vt.audmode = V4L2_TUNER_MODE_LANG1;
527 LOG(VB_GENERAL, LOG_WARNING,
528 "Dual audio mode incompatible with Layer I audio."
529 "\n\t\t\tFalling back to Main Language");
530 vt.audmode = V4L2_TUNER_MODE_LANG1;
534 if (ioctl(chanfd, VIDIOC_S_TUNER, &vt) < 0)
536 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Unable to set audio mode" +
ENO);
546 struct v4l2_queryctrl qctrl {};
547 qctrl.id = V4L2_CID_AUDIO_VOLUME;
548 if ((ioctl(chanfd, VIDIOC_QUERYCTRL, &qctrl) < 0) ||
549 (qctrl.flags & V4L2_CTRL_FLAG_DISABLED))
551 LOG(VB_CHANNEL, LOG_WARNING,
552 LOC +
"Audio volume control not supported.");
557 int range = qctrl.maximum - qctrl.minimum;
558 int value = (int) ((range *
m_audVolume * 0.01F) + qctrl.minimum);
559 int ctrl_volume = std::min(qctrl.maximum, std::max(qctrl.minimum, value));
562 struct v4l2_control ctrl {V4L2_CID_AUDIO_VOLUME, ctrl_volume};
564 if (ioctl(chanfd, VIDIOC_S_CTRL, &ctrl) < 0)
566 LOG(VB_GENERAL, LOG_WARNING,
LOC +
567 "Unable to set recording volume" +
ENO +
"\n\t\t\t" +
568 "If you are using an AverMedia M179 card this is normal.");
583 case 2: st = 2;
break;
586 case 14: st = 10;
break;
587 case 11: st = 11;
break;
588 case 12: st = 12;
break;
589 default: st = 0;
break;
595 LOG(VB_GENERAL, LOG_WARNING,
LOC +
596 QString(
"Stream type '%1'\n\t\t\t"
597 "is not supported by %2 driver, using '%3' instead.")
610 sr = (
m_driver ==
"ivtv") ? 48000 : sr;
614 LOG(VB_GENERAL, LOG_WARNING,
LOC +
615 QString(
"Audio sample rate %1 Hz\n\t\t\t"
616 "is not supported by %2 driver, using %3 Hz instead.")
622 case 32000:
return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
623 case 44100:
return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
625 default:
return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
633 layer = std::max(std::min(layer, 3U), 1U);
635 layer = (
m_driver ==
"ivtv") ? 2 : layer;
639 LOG(VB_GENERAL, LOG_WARNING,
LOC +
640 QString(
"MPEG layer %1 does not work properly\n\t\t\t"
641 "with %2 driver. Using MPEG layer %3 audio instead.")
658 case 0:
return V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
659 case 1:
return V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
660 case 2:
return V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
664 return V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
665 case 10:
return V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
666 case 11:
return V4L2_MPEG_STREAM_TYPE_MPEG1_VCD;
667 case 12:
return V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD;
670 return V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
671 default:
return V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
675 static void add_ext_ctrl(std::vector<struct v4l2_ext_control> &ctrl_list,
676 uint32_t
id, int32_t value)
678 struct v4l2_ext_control tmp_ctrl {};
680 tmp_ctrl.value = value;
681 ctrl_list.push_back(tmp_ctrl);
684 static void set_ctrls(
int fd, std::vector<struct v4l2_ext_control> &ext_ctrls)
686 static QMutex s_controlDescriptionLock;
687 static QMap<uint32_t,QString> s_controlDescription;
689 s_controlDescriptionLock.lock();
690 if (s_controlDescription.isEmpty())
692 s_controlDescription[V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ] =
693 "Audio Sampling Frequency";
694 s_controlDescription[V4L2_CID_MPEG_VIDEO_ASPECT] =
695 "Video Aspect ratio";
696 s_controlDescription[V4L2_CID_MPEG_AUDIO_ENCODING] =
698 s_controlDescription[V4L2_CID_MPEG_AUDIO_L2_BITRATE] =
700 s_controlDescription[V4L2_CID_MPEG_VIDEO_BITRATE_PEAK] =
701 "Video Peak Bitrate";
702 s_controlDescription[V4L2_CID_MPEG_VIDEO_BITRATE] =
703 "Video Average Bitrate";
704 s_controlDescription[V4L2_CID_MPEG_STREAM_TYPE] =
706 s_controlDescription[V4L2_CID_MPEG_VIDEO_BITRATE_MODE] =
709 s_controlDescriptionLock.unlock();
711 for (
auto & ext_ctrl : ext_ctrls)
713 struct v4l2_ext_controls ctrls {};
715 int value = ext_ctrl.value;
717 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
719 ctrls.controls = &ext_ctrl;
721 if (ioctl(fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
723 QMutexLocker locker(&s_controlDescriptionLock);
724 LOG(VB_GENERAL, LOG_ERR, QString(
"mpegrecorder.cpp:set_ctrls(): ") +
725 QString(
"Could not set %1 to %2")
726 .arg(s_controlDescription[ext_ctrl.id]).arg(value) +
734 std::vector<struct v4l2_ext_control> ext_ctrls;
739 if (!
m_driver.startsWith(
"saa7164"))
741 add_ext_ctrl(ext_ctrls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
769 add_ext_ctrl(ext_ctrls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
771 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR :
772 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
778 add_ext_ctrl(ext_ctrls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
787 struct v4l2_audio ain {};
788 ain.index = audioinput;
789 if (ioctl(chanfd, VIDIOC_ENUMAUDIO, &ain) < 0)
791 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"Unable to get audio input.");
795 ain.index = audioinput;
796 if (ioctl(chanfd, VIDIOC_S_AUDIO, &ain) < 0)
798 LOG(VB_GENERAL, LOG_WARNING,
799 LOC +
"Unable to set audio input.");
805 if (
m_driver ==
"hdpvr" && audioinput != 2)
807 struct v4l2_queryctrl qctrl {};
808 qctrl.id = V4L2_CID_MPEG_AUDIO_ENCODING;
810 if (!ioctl(chanfd, VIDIOC_QUERYCTRL, &qctrl))
812 uint audio_enc = std::max(std::min(
m_audType-1, qctrl.maximum), qctrl.minimum);
813 add_ext_ctrl(ext_ctrls, V4L2_CID_MPEG_AUDIO_ENCODING, audio_enc);
817 LOG(VB_GENERAL, LOG_WARNING,
LOC +
818 "Unable to get supported audio codecs." +
ENO);
833 #ifdef V4L2_CAP_SLICED_VBI_CAPTURE
843 struct v4l2_format vbifmt {};
844 vbifmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
846 V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
848 if (ioctl(fd, VIDIOC_S_FMT, &vbifmt) < 0)
853 if (ioctl(fd, VIDIOC_S_FMT, &vbifmt) < 0)
855 LOG(VB_GENERAL, LOG_WARNING,
LOC +
856 "Unable to enable VBI embedding (/dev/vbiX)" +
ENO);
862 LOG(VB_GENERAL, LOG_WARNING,
LOC +
863 "Unable to enable VBI embedding (/dev/videoX)" +
ENO);
868 if (ioctl(fd, VIDIOC_G_FMT, &vbifmt) >= 0)
870 LOG(VB_RECORD, LOG_INFO,
871 LOC + QString(
"VBI service: %1, io size: %2")
872 .arg(vbifmt.fmt.sliced.service_set)
873 .arg(vbifmt.fmt.sliced.io_size));
875 struct v4l2_ext_control vbi_ctrl {};
876 vbi_ctrl.id = V4L2_CID_MPEG_STREAM_VBI_FMT;
877 vbi_ctrl.value = V4L2_MPEG_STREAM_VBI_FMT_IVTV;
879 struct v4l2_ext_controls ctrls {};
880 ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
882 ctrls.controls = &vbi_ctrl;
884 if (ioctl(fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0)
886 LOG(VB_GENERAL, LOG_WARNING,
LOC +
887 "Unable to set VBI embedding format" +
ENO);
895 #endif // V4L2_CAP_SLICED_VBI_CAPTURE
911 m_error =
"Failed to open V4L device";
915 bool has_select =
true;
917 #if defined(__FreeBSD__)
952 bool good_data =
false;
958 long long bytesRead = 0;
962 int dummyBPS = qEnvironmentVariableIntValue(
"DUMMYBPS", &ok) / 8;
965 LOG(VB_GENERAL, LOG_INFO,
966 LOC + QString(
"Throttling dummy recorder to %1 bits per second")
970 struct timeval tv {};
974 elapsedTimer.
start();
977 LOG(VB_RECORD, LOG_INFO,
LOC +
"Initial startup of recorder");
989 if (dummyBPS && bytesRead)
991 elapsed = (elapsedTimer.
elapsed().count() / 1000.0) + 1;
992 while ((bytesRead / elapsed) > dummyBPS)
994 std::this_thread::sleep_for(50ms);
995 elapsed = (elapsedTimer.
elapsed().count() / 1000.0) + 1;
1000 elapsed = (elapsedTimer.
elapsed().count() / 1000.0) + 1;
1003 std::this_thread::sleep_for(50ms);
1004 elapsed = (elapsedTimer.
elapsed().count() / 1000.0) + 1;
1017 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Device error detected");
1037 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Device EOF detected");
1038 m_error =
"Device EOF detected";
1051 (gap_start, gap_end));
1052 LOG(VB_RECORD, LOG_DEBUG,
1053 LOC + QString(
"Inserted gap %1 dur %2")
1055 .arg(gap_start.secsTo(gap_end)));
1076 switch (select(
m_readfd + 1, &rdset,
nullptr,
nullptr, &tv))
1082 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Select error" +
ENO);
1086 LOG(VB_GENERAL, LOG_ERR,
LOC +
"select timeout - "
1087 "driver has stopped responding");
1091 LOG(VB_GENERAL, LOG_ERR,
LOC +
"Close error" +
ENO);
1105 if (len < 0 && !has_select)
1116 m_readfd = open(vdevice.constData(), O_RDONLY);
1126 m_error =
"Failed to read from video file";
1130 else if (len < 0 && errno != EAGAIN)
1132 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"error reading from: %1")
1146 int start_remain = len - remainder;
1147 if (remainder && (start_remain >= remainder))
1148 memcpy(buffer, buffer+start_remain, remainder);
1150 memmove(buffer, buffer+start_remain, remainder);
1159 LOG(VB_RECORD, LOG_INFO,
LOC +
"run finishing up");
1195 const uint pid = tspacket_real.
PID();
1198 if ((
m_driver ==
"hdpvr") && (pid == 0x1001))
1206 const TSPacket &tspacket = (tspacket_fake)
1207 ? *tspacket_fake : tspacket_real;
1211 delete tspacket_fake;
1218 LOG(VB_RECORD, LOG_INFO,
LOC +
"Reset(void)");
1249 LOG(VB_RECORD, LOG_INFO,
LOC +
"PauseAndWait pause");
1265 LOG(VB_RECORD, LOG_INFO,
LOC +
"PauseAndWait unpause");
1286 LOG(VB_RECORD, LOG_INFO,
LOC +
"RestartEncoding");
1312 LOG(VB_RECORD, LOG_INFO,
LOC +
"StartEncoding");
1319 LOG(VB_GENERAL, LOG_ERR,
LOC +
1320 "StartEncoding: Can't open video device." +
ENO);
1321 m_error =
"Failed to start recording";
1329 bool good_res =
true;
1342 for ( ; idx < 50; ++idx)
1350 LOG(VB_GENERAL, LOG_ERR,
LOC +
1351 "StartEncoding: read failing, re-opening device: " +
ENO);
1353 std::this_thread::sleep_for(2ms);
1358 LOG(VB_GENERAL, LOG_ERR,
LOC +
1359 "StartEncoding: Can't open video device." +
ENO);
1360 m_error =
"Failed to start recording";
1366 LOG(VB_GENERAL, LOG_ERR,
LOC +
1367 QString(
"StartEncoding: read failed, retry in %1 msec:")
1368 .arg(100 * idx) +
ENO);
1369 std::this_thread::sleep_for(idx * 100us);
1374 LOG(VB_GENERAL, LOG_ERR,
LOC +
1375 "StartEncoding: read from video device failed." +
ENO);
1376 m_error =
"Failed to start recording";
1383 LOG(VB_RECORD, LOG_WARNING,
LOC +
1384 QString(
"%1 read attempts required to start encoding").arg(idx));
1404 LOG(VB_RECORD, LOG_INFO,
LOC +
"StopEncoding");
1409 struct v4l2_encoder_cmd command {};
1410 command.cmd = V4L2_ENC_CMD_STOP;
1411 command.flags = V4L2_ENC_CMD_STOP_AT_GOP_END;
1416 bool stopped = 0 == ioctl(
m_readfd, VIDIOC_ENCODER_CMD, &command);
1419 LOG(VB_RECORD, LOG_INFO,
LOC +
"Encoding stopped");
1421 else if (errno != ENOTTY && errno != EINVAL)
1427 LOG(VB_GENERAL, LOG_WARNING,
LOC +
"StopEncoding failed" +
ENO);
1433 std::this_thread::sleep_for(20ms);
1449 const QString & reason)
1451 if (maxbitrate == bitrate)
1453 LOG(VB_RECORD, LOG_INFO,
LOC + QString(
"%1 bitrate %2 kbps CBR")
1454 .arg(reason).arg(bitrate));
1458 LOG(VB_RECORD, LOG_INFO,
LOC + QString(
"%1 bitrate %2/%3 kbps VBR")
1459 .arg(reason).arg(bitrate).arg(maxbitrate));
1462 std::vector<struct v4l2_ext_control> ext_ctrls;
1463 add_ext_ctrl(ext_ctrls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1464 (maxbitrate == bitrate) ?
1465 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR :
1466 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
1471 add_ext_ctrl(ext_ctrls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1479 LOG(VB_RECORD, LOG_INFO,
LOC +
"Checking Resolution");
1481 struct v4l2_format vfmt {};
1482 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1484 if (0 == ioctl(
m_chanfd, VIDIOC_G_FMT, &vfmt))
1486 LOG(VB_RECORD, LOG_INFO,
LOC + QString(
"Got Resolution %1x%2")
1487 .arg(vfmt.fmt.pix.width).arg(vfmt.fmt.pix.height));
1488 pix = vfmt.fmt.pix.width * vfmt.fmt.pix.height;
1493 LOG(VB_RECORD, LOG_INFO,
LOC +
"Giving up detecting resolution: " +
ENO);
1504 else if (pix >= 1920*1080)
1518 if (old_max == old_avg)
1520 LOG(VB_RECORD, LOG_INFO,
LOC +
1521 QString(
"Old bitrate %1 CBR").arg(old_avg));
1525 LOG(VB_RECORD, LOG_INFO,
LOC +
1526 QString(
"Old bitrate %1/%2 VBR") .arg(old_avg).arg(old_max));
1537 LOG(VB_VBI, LOG_INFO,
LOC + QString(
"FormatCC(0x%1,0x%2)")
1538 .arg(code1,0,16).arg(code2,0,16));