Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Apr 5, 2024
1 parent dd47b69 commit 25833bf
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 180 deletions.
4 changes: 2 additions & 2 deletions src/devices/audioFromFileDevice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ if(NOT SKIP_audioFromFileDevice)
PRIVATE
AudioFromFileDevice.cpp
AudioFromFileDevice.h
AudioFromFileDevice_paramsParser.cpp
AudioFromFileDevice_paramsParser.h
AudioFromFileDevice_ParamsParser.cpp
AudioFromFileDevice_ParamsParser.h
)

target_link_libraries(yarp_audioFromFileDevice
Expand Down
6 changes: 6 additions & 0 deletions src/devices/ffmpeg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ yarp_prepare_plugin(ffmpeg_grabber
INCLUDE FfmpegGrabber.h
EXTRA_CONFIG
WRAPPER=frameGrabber_nws_yarp
GENERATE_PARSER
DEPENDS "YARP_HAS_FFMPEG;FFMPEG_avcodec_FOUND;FFMPEG_avformat_FOUND;FFMPEG_avdevice_FOUND;FFMPEG_avutil_FOUND;FFMPEG_swscale_FOUND"
)

yarp_prepare_plugin(ffmpeg_writer
CATEGORY device
TYPE FfmpegWriter
INCLUDE FfmpegWriter.h
GENERATE_PARSER
DEPENDS "YARP_HAS_FFMPEG;FFMPEG_avcodec_FOUND;FFMPEG_avformat_FOUND;FFMPEG_avdevice_FOUND;FFMPEG_avutil_FOUND;FFMPEG_swscale_FOUND"
)

Expand All @@ -29,6 +31,10 @@ if(NOT SKIP_ffmpeg_grabber OR NOT SKIP_ffmpeg_writer)
FfmpegWriter.h
ffmpeg_api.h
ffmpeg_api.cpp
FfmpegGrabber_ParamsParser.cpp
FfmpegGrabber_ParamsParser.h
FfmpegWriter_ParamsParser.cpp
FfmpegWriter_ParamsParser.h
)

target_link_libraries(yarp_ffmpeg
Expand Down
20 changes: 20 additions & 0 deletions src/devices/ffmpeg/FFmpegGrabber_params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
* | | source | string | - | default.avi | No | media file to read from | |
* | | loop | bool | - | true | No | media should loop (default) | |
* | | sync | string | - | image | No | sync on image or audio (if have to choose)? | |
* | | nodelay | bool | - | false | No | media will play in simulated realtime unless this is present | |
* | | pace | bool | - | 1.0 | No | simulated realtime multiplier factor (must be <1 right now) | |
* | | v4l | bool | - | false | No | if present, read from video4linux | |
* | | v4l1 | bool | - | false | No | if present, read from video4linux | |
* | | v4l2 | bool | - | false | No | if present, read from video4linux | |
* | | ieee1394 | bool | - | false | No | if present, read from firewire | |
* | | v4ldevice | string | - | /dev/video0 | No | device name | |
* | | audio | string | - | /dev/dsp | No | optional audio device name | |
* | | audio_rate | int | - | 44100 | No | audio sample rate | |
* | | channels | int | - | 1 | No | number of channels | |
* | | time_base_num | int | - | 1 | No | numerator of basic time unit | |
* | | time_base_den | int | - | 29 | No | denominator of basic time unit | |
* | | channel | int | - | 0 | No | channel identifier | |
* | | standard | string | - | - | No | pal versus ntsc | |
* | | width | int | - | 640 | No | width of image | |
* | | height | int | - | 480 | No | height of image | |
* | | devname | string | - | /dev/dv1394 | No | firewire device name | |
7 changes: 7 additions & 0 deletions src/devices/ffmpeg/FFmpegWriter_params.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* | | width | int | - | 0 | No | width of image (must be even) | |
* | | height | int | - | 0 | No | height of image (must be even) | |
* | | framerate | int | - | 30 | No | baseline images per second | |
* | | audio | bool | - | false | No | should audio be included? | |
* | | channels | int | - | 1 | No | number of audio channels | |
* | | sample_rate | int | - | 44100 | No | audio samples per second | |
* | | out | string | - | movie.avi | No | name of movie to write | |
97 changes: 28 additions & 69 deletions src/devices/ffmpeg/FfmpegGrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,89 +332,69 @@ bool FfmpegGrabber::openV4L(yarp::os::Searchable & config,
*(audio?(&formatParamsAudio):(&formatParamsVideo));

AVInputFormat *iformat;
Value v;
std::string vs;

if (!audio) {
//formatParams.prealloced_context = 1;
v = config.check("v4ldevice",
Value("/dev/video0"),
"device name");
vs = m_v4ldevice;
} else {
v = config.check("audio",
Value("/dev/dsp"),
"optional audio device name");
vs = m_audio;
}
yCInfo(FFMPEGGRABBER, "Device %s",v.asString().c_str());
yCInfo(FFMPEGGRABBER, "Device %s",vs.c_str());

m_uri = v.asString();
m_uri = vs;

if (audio) {
av_dict_set_int(&formatParams,
"sample_rate",
config.check("audio_rate",
Value(44100),
"audio sample rate").asInt32(),
m_audio_rate,
0);
av_dict_set_int(&formatParams,
"channels",
config.check("channels",
Value(1),
"number of channels").asInt32(),
m_channels,
0);
} else {
if (config.check("time_base_num") && config.check("time_base_den")) {
char buf[256];
sprintf(buf, "%d/%d",
config.check("time_base_num",
Value(1),
"numerator of basic time unit").asInt32(),
config.check("time_base_den",
Value(29),
"denominator of basic time unit").asInt32());
m_time_base_num,
m_time_base_den);
av_dict_set(&formatParams, "framerate", buf, 0);
}

if (config.check("channel")) {
av_dict_set_int(&formatParams,
"channel",
config.check("channel",
Value(0),
"channel identifier").asInt32(),
m_channel,
0);
}
if (config.check("standard")) {
av_dict_set(&formatParams,
"standard",
config.check("standard",
Value("-"),
"pal versus ntsc").asString().c_str(),
m_standard.c_str(),
0);
}
av_dict_set_int(&formatParams,
"width",
config.check("width",
Value(640),
"width of image").asInt32(),
m_width,
0);
av_dict_set_int(&formatParams,
"height",
config.check("height",
Value(480),
"height of image").asInt32(),
m_height,
0);
}

std::string videoDevice = (config.check("v4l1") ? "video4linux" : "video4linux2");
std::string videoDevice = (m_v4l1 ? "video4linux" : "video4linux2");
iformat = av_find_input_format(audio ? "audio_device" : videoDevice.c_str());

int result = avformat_open_input(audio ? ppFormatCtx2 : ppFormatCtx,
v.asString().c_str(),
vs.c_str(),
iformat,
&formatParams);

bool ok = (result==0);
if (!ok) {
yCError(FFMPEGGRABBER, "%s: ffmpeg error %d", v.asString().c_str(), result);
yCError(FFMPEGGRABBER, "%s: ffmpeg error %d", vs.c_str(), result);
}

if (ok) {
Expand All @@ -435,15 +415,12 @@ bool FfmpegGrabber::openFirewire(yarp::os::Searchable & config,
AVFormatContext **ppFormatCtx)
{
AVInputFormat *iformat;
std::string devname = config.check("devname",
Value("/dev/dv1394"),
"firewire device name").asString();
iformat = av_find_input_format("dv1394");
yCInfo(FFMPEGGRABBER, "Checking for digital video in %s", devname.c_str());
yCInfo(FFMPEGGRABBER, "Checking for digital video in %s", m_devname.c_str());

m_uri = devname;
m_uri = m_devname;

return avformat_open_input(ppFormatCtx, strdup(devname.c_str()), iformat, nullptr) == 0;
return avformat_open_input(ppFormatCtx, strdup(m_devname.c_str()), iformat, nullptr) == 0;
}


Expand All @@ -457,55 +434,37 @@ bool FfmpegGrabber::openFile(AVFormatContext **ppFormatCtx,

bool FfmpegGrabber::open(yarp::os::Searchable & config)
{
std::string fname =
config.check("source",
Value("default.avi"),
"media file to read from").asString();

if (config.check("loop","media should loop (default)")) {
shouldLoop = true;
}

if (config.check("noloop","media should not loop")) {
shouldLoop = false;
}
if (!this->parseParams(config)) { return false; }

imageSync = false;
std::string sync =
config.check("sync",
Value("image"),
"sync on image or audio (if have to choose)?").asString();
imageSync = (sync=="image");
imageSync = (m_sync=="image");

needRateControl = true; // default for recorded media

if (config.check("nodelay","media will play in simulated realtime unless this is present")) {
if (m_nodelay) {
needRateControl = false;
}

pace = config.check("pace",Value(1.0),
"simulated realtime multiplier factor (must be <1 right now)").asFloat64();

// Register all formats and codecs
av_register_all();
avdevice_register_all();

// Open video file
if (config.check("v4l","if present, read from video4linux") || config.check("v4l1","if present, read from video4linux") || config.check("v4l2","if present, read from video4linux2")) {
if (m_v4l|| m_v4l1 || m_v4l2) {
needRateControl = false; // reading from live media
if (!openV4L(config,&pFormatCtx,&pFormatCtx2)) {
yCError(FFMPEGGRABBER, "Could not open Video4Linux input");
return false;
}
} else if (config.check("ieee1394","if present, read from firewire")) {
} else if (m_ieee1394) {
needRateControl = false; // reading from live media
if (!openFirewire(config,&pFormatCtx)) {
yCError(FFMPEGGRABBER, "Could not open ieee1394 input");
return false;
}
} else {
if (!openFile(&pFormatCtx,fname.c_str())) {
yCError(FFMPEGGRABBER, "Could not open media file %s", fname.c_str());
if (!openFile(&pFormatCtx,m_source.c_str())) {
yCError(FFMPEGGRABBER, "Could not open media file %s", m_source.c_str());
return false; // Couldn't open file
}
}
Expand Down Expand Up @@ -707,7 +666,7 @@ bool FfmpegGrabber::getAudioVisual(yarp::sig::ImageOf<yarp::sig::PixelRgb>& imag
image.resize(0,0);
}
if (needRateControl) {
double now = (SystemClock::nowSystem()-startTime)*pace;
double now = (SystemClock::nowSystem()-startTime)*m_pace;
double delay = time_target-now;
if (delay>0) {
SystemClock::delaySystem(delay);
Expand All @@ -724,7 +683,7 @@ bool FfmpegGrabber::getAudioVisual(yarp::sig::ImageOf<yarp::sig::PixelRgb>& imag
tryAgain = !triedAgain;

if (tryAgain) {
if (!shouldLoop) {
if (!m_loop) {
return false;
}
av_seek_frame(pFormatCtx,-1,0,AVSEEK_FLAG_BACKWARD);
Expand Down
50 changes: 19 additions & 31 deletions src/devices/ffmpeg/FfmpegGrabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,31 @@ extern "C" {
#include <yarp/dev/DeviceDriver.h>

#include "ffmpeg_api.h"
#include "FfmpegGrabber_ParamsParser.h"

/**
* @ingroup dev_impl_media
*
* \brief `ffmpeg_grabber`: An image frame grabber device using ffmpeg to
* capture images from AVI files.
*
* Parameters required by this device are shown in class: FfmpegGrabber_ParamsParser
*/
class FfmpegGrabber :
public yarp::dev::IFrameGrabberImage,
public yarp::dev::IAudioGrabberSound,
public yarp::dev::IAudioVisualGrabber,
public yarp::dev::IAudioVisualStream,
public yarp::dev::DeviceDriver
public yarp::dev::DeviceDriver,
public FfmpegGrabber_ParamsParser
{
public:

FfmpegGrabber() :
system_resource(nullptr),
formatParamsVideo(nullptr),
formatParamsAudio(nullptr),
pFormatCtx(nullptr),
pFormatCtx2(nullptr),
pAudioFormatCtx(nullptr),
active(false),
startTime(0),
_hasAudio(false),
_hasVideo(false),
needRateControl(false),
shouldLoop(true),
pace(1),
imageSync(false),
m_w(0),
m_h(0),
m_channels(0),
m_rate(0),
m_capture(nullptr)
imageSync(false)
{
memset(&packet,0,sizeof(packet));
}
Expand Down Expand Up @@ -121,35 +110,34 @@ class FfmpegGrabber :
}

protected:
void *system_resource;
void *system_resource = nullptr;

AVDictionary* formatParamsVideo;
AVDictionary* formatParamsAudio;
AVFormatContext *pFormatCtx;
AVFormatContext *pFormatCtx2;
AVFormatContext *pAudioFormatCtx;
AVDictionary* formatParamsVideo = nullptr;
AVDictionary* formatParamsAudio = nullptr;
AVFormatContext *pFormatCtx = nullptr;
AVFormatContext *pFormatCtx2 = nullptr;
AVFormatContext *pAudioFormatCtx = nullptr;
AVPacket packet;
bool active;
double startTime;
bool _hasAudio, _hasVideo;
bool _hasAudio = false;
bool _hasVideo = false;
bool needRateControl;
bool shouldLoop;
double pace;
bool imageSync;

/** Uri of the images a grabber produces. */
std::string m_uri;

/** Width of the images a grabber produces. */
int m_w;
int m_w = 0;
/** Height of the images a grabber produces. */
int m_h;
int m_h = 0;

int m_channels;
int m_rate;
int m_channels = 0;
int m_rate = 0;

/** Opaque ffmpeg structure for image capture. */
void * m_capture;
void * m_capture = nullptr;

bool openFirewire(yarp::os::Searchable & config,
AVFormatContext **ppFormatCtx);
Expand Down
Loading

0 comments on commit 25833bf

Please sign in to comment.