YARP
Yet Another Robot Platform
FfmpegGrabber.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef FfmpegGrabber_INC
11 #define FfmpegGrabber_INC
12 
13 #include "avpreamble.h"
14 
15 extern "C" {
16 #include <libavcodec/avcodec.h>
17 #include <libavformat/avformat.h>
18 }
19 
20 /*
21  * A YARP frame grabber device driver using ffmpeg to implement
22  * image capture from AVI files.
23  */
24 
26 #include <yarp/dev/DeviceDriver.h>
27 
28 #include "ffmpeg_api.h"
29 
42 {
43 public:
44 
46  system_resource(nullptr),
47  formatParamsVideo(nullptr),
48  formatParamsAudio(nullptr),
49  pFormatCtx(nullptr),
50  pFormatCtx2(nullptr),
51  pAudioFormatCtx(nullptr),
52  active(false),
53  startTime(0),
54  _hasAudio(false),
55  _hasVideo(false),
56  needRateControl(false),
57  shouldLoop(true),
58  pace(1),
59  imageSync(false),
60  m_w(0),
61  m_h(0),
62  m_channels(0),
63  m_rate(0),
64  m_capture(nullptr)
65  {
66  memset(&packet,0,sizeof(packet));
67  }
68 
69  bool open(yarp::os::Searchable & config) override;
70 
71  bool close() override;
72 
74 
75  bool getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override;
76 
77  int height() const override { return m_h; }
78 
79  int width() const override { return m_w; }
80 
82  yarp::sig::Sound& sound) override;
83 
84 
85  bool hasAudio() override {
86  return _hasAudio;
87  }
88 
89  bool hasVideo() override {
90  return _hasVideo;
91  }
92 
93  bool startRecording() override {
94  return true;
95  }
96 
97  bool stopRecording() override {
98  return true;
99  }
100 
102  return false;
103  }
104 
106  return false;
107  }
108 
109  bool resetRecordingAudioBuffer() override {
110  return false;
111  }
112 
113 protected:
115 
116  AVDictionary* formatParamsVideo;
117  AVDictionary* formatParamsAudio;
118  AVFormatContext *pFormatCtx;
119  AVFormatContext *pFormatCtx2;
120  AVFormatContext *pAudioFormatCtx;
121  AVPacket packet;
122  bool active;
123  double startTime;
127  double pace;
128  bool imageSync;
129 
131  std::string m_uri;
132 
134  int m_w;
136  int m_h;
137 
139  int m_rate;
140 
142  void * m_capture;
143 
144  bool openFirewire(yarp::os::Searchable & config,
145  AVFormatContext **ppFormatCtx);
146 
147  bool openV4L(yarp::os::Searchable & config,
148  AVFormatContext **ppFormatCtx,
149  AVFormatContext **ppFormatCtx2);
150 
151  bool openFile(AVFormatContext **ppFormatCtx,
152  const char *fname);
153 };
154 
155 #endif
ffmpeg_grabber: An image frame grabber device using ffmpeg to capture images from AVI files.
Definition: FfmpegGrabber.h:42
bool openV4L(yarp::os::Searchable &config, AVFormatContext **ppFormatCtx, AVFormatContext **ppFormatCtx2)
bool hasAudio() override
Definition: FfmpegGrabber.h:85
int m_w
Width of the images a grabber produces.
bool openFile(AVFormatContext **ppFormatCtx, const char *fname)
bool hasVideo() override
Definition: FfmpegGrabber.h:89
int m_h
Height of the images a grabber produces.
void * system_resource
bool openFirewire(yarp::os::Searchable &config, AVFormatContext **ppFormatCtx)
int height() const override
Return the height of each frame.
Definition: FfmpegGrabber.h:77
AVFormatContext * pAudioFormatCtx
bool resetRecordingAudioBuffer() override
AVPacket packet
bool getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override
Get a sound from a device.
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
Get an rgb image from the frame grabber, if required demosaicking/color reconstruction is applied.
bool stopRecording() override
Stop the recording.
Definition: FfmpegGrabber.h:97
int width() const override
Return the width of each frame.
Definition: FfmpegGrabber.h:79
bool startRecording() override
Start the recording.
Definition: FfmpegGrabber.h:93
bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &) override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
AVDictionary * formatParamsAudio
bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &) override
AVFormatContext * pFormatCtx2
std::string m_uri
Uri of the images a grabber produces.
virtual bool getAudioVisual(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image, yarp::sig::Sound &sound) override
Get an image and sound.
AVFormatContext * pFormatCtx
AVDictionary * formatParamsVideo
void * m_capture
Opaque ffmpeg structure for image capture.
bool close() override
Close the DeviceDriver.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
Read a YARP-format sound block from a device.
Read a YARP-format image and sound from a device.
Read a YARP-format image from a device.
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Class for storing sounds.
Definition: Sound.h:28