YARP
Yet Another Robot Platform
FfmpegGrabber.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef FfmpegGrabber_INC
8 #define FfmpegGrabber_INC
9 
10 #include "avpreamble.h"
11 
12 extern "C" {
13 #include <libavcodec/avcodec.h>
14 #include <libavformat/avformat.h>
15 }
16 
17 /*
18  * A YARP frame grabber device driver using ffmpeg to implement
19  * image capture from AVI files.
20  */
21 
24 #include <yarp/dev/DeviceDriver.h>
25 
26 #include "ffmpeg_api.h"
27 
40 {
41 public:
42 
44  system_resource(nullptr),
45  formatParamsVideo(nullptr),
46  formatParamsAudio(nullptr),
47  pFormatCtx(nullptr),
48  pFormatCtx2(nullptr),
49  pAudioFormatCtx(nullptr),
50  active(false),
51  startTime(0),
52  _hasAudio(false),
53  _hasVideo(false),
54  needRateControl(false),
55  shouldLoop(true),
56  pace(1),
57  imageSync(false),
58  m_w(0),
59  m_h(0),
60  m_channels(0),
61  m_rate(0),
62  m_capture(nullptr)
63  {
64  memset(&packet,0,sizeof(packet));
65  }
66 
67  bool open(yarp::os::Searchable & config) override;
68 
69  bool close() override;
70 
72 
73  bool getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override;
74 
75  int height() const override { return m_h; }
76 
77  int width() const override { return m_w; }
78 
80  yarp::sig::Sound& sound) override;
81 
82 
83  bool hasAudio() override {
84  return _hasAudio;
85  }
86 
87  bool hasVideo() override {
88  return _hasVideo;
89  }
90 
91  bool startRecording() override {
92  return true;
93  }
94 
95  bool stopRecording() override {
96  return true;
97  }
98 
99  bool isRecording(bool& recording_enabled) override {
100  return false;
101  }
102 
104  return false;
105  }
106 
108  return false;
109  }
110 
111  bool resetRecordingAudioBuffer() override {
112  return false;
113  }
114 
115  bool setHWGain(double gain) override {
116  return false;
117  }
118 
119  bool setSWGain(double gain) override {
120  return false;
121  }
122 
123 protected:
125 
126  AVDictionary* formatParamsVideo;
127  AVDictionary* formatParamsAudio;
128  AVFormatContext *pFormatCtx;
129  AVFormatContext *pFormatCtx2;
130  AVFormatContext *pAudioFormatCtx;
131  AVPacket packet;
132  bool active;
133  double startTime;
137  double pace;
138  bool imageSync;
139 
141  std::string m_uri;
142 
144  int m_w;
146  int m_h;
147 
149  int m_rate;
150 
152  void * m_capture;
153 
154  bool openFirewire(yarp::os::Searchable & config,
155  AVFormatContext **ppFormatCtx);
156 
157  bool openV4L(yarp::os::Searchable & config,
158  AVFormatContext **ppFormatCtx,
159  AVFormatContext **ppFormatCtx2);
160 
161  bool openFile(AVFormatContext **ppFormatCtx,
162  const char *fname);
163 };
164 
165 #endif
ffmpeg_grabber: An image frame grabber device using ffmpeg to capture images from AVI files.
Definition: FfmpegGrabber.h:40
bool openV4L(yarp::os::Searchable &config, AVFormatContext **ppFormatCtx, AVFormatContext **ppFormatCtx2)
bool hasAudio() override
Definition: FfmpegGrabber.h:83
int m_w
Width of the images a grabber produces.
bool openFile(AVFormatContext **ppFormatCtx, const char *fname)
bool hasVideo() override
Definition: FfmpegGrabber.h:87
int m_h
Height of the images a grabber produces.
void * system_resource
bool openFirewire(yarp::os::Searchable &config, AVFormatContext **ppFormatCtx)
bool setSWGain(double gain) override
Sets a software gain for the grabbed audio.
bool setHWGain(double gain) override
Sets the hardware gain of the grabbing device (if supported by the hardware)
int height() const override
Return the height of each frame.
Definition: FfmpegGrabber.h:75
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
bool stopRecording() override
Stop the recording.
Definition: FfmpegGrabber.h:95
int width() const override
Return the width of each frame.
Definition: FfmpegGrabber.h:77
bool startRecording() override
Start the recording.
Definition: FfmpegGrabber.h:91
bool isRecording(bool &recording_enabled) override
Check if the recording has been enabled (e.g.
Definition: FfmpegGrabber.h:99
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:35
Read a YARP-format sound block from a device.
Read a YARP-format image and sound from a device.
A base class for nested structures that can be searched.
Definition: Searchable.h:66
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition: Sound.h:26