YARP
Yet Another Robot Platform
DevicePipe.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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 #include "DevicePipe.h"
11 
12 #include <yarp/os/LogComponent.h>
13 #include <yarp/os/Time.h>
14 
16 
17 #include <cstdio>
18 
19 using namespace yarp::os;
20 using namespace yarp::sig;
21 using namespace yarp::dev;
22 
23 namespace {
24 YARP_LOG_COMPONENT(DEVICEPIPE, "yarp.devices.DevicePipe")
25 }
26 
28 {
29  bool ok = open("source",
30  source,
31  config,
32  "device to read from (string or nested properties)");
33  if (!ok) {
34  return false;
35  }
36  ok = open("sink",
37  sink,
38  config,
39  "device to write to (string or nested properties)");
40  if (!ok) {
41  source.close();
42  return false;
43  }
44  return true;
45 }
46 
47 
48 bool DevicePipe::open(const char* key,
49  PolyDriver& poly,
50  yarp::os::Searchable& config,
51  const char* comment)
52 {
53 
54  Value* name;
55  if (config.check(key, name, comment)) {
56  if (name->isString()) {
57  // maybe user isn't doing nested configuration
59  p.setMonitor(config.getMonitor(),
60  name->toString().c_str()); // pass on any monitoring
61  p.fromString(config.toString());
62  p.put("device", name->toString());
63  p.unput("subdevice");
64  p.unput("wrapped");
65  poly.open(p);
66  } else {
67  Bottle subdevice = config.findGroup(key).tail();
68  poly.open(subdevice);
69  }
70  if (!poly.isValid()) {
71  yCInfo(DEVICEPIPE, "cannot make <%s>", name->toString().c_str());
72  return false;
73  }
74  } else {
75  yCInfo(DEVICEPIPE, "\"--%s <name>\" not set", key);
76  return false;
77  }
78  return true;
79 }
80 
81 
83 {
84  yCInfo(DEVICEPIPE, "Devices closing");
85  source.close();
86  sink.close();
87  return true;
88 }
89 
90 
92 {
93  IFrameGrabberImage* imgSource;
94  IAudioGrabberSound* sndSource;
95  IAudioVisualGrabber* imgSndSource;
96  IAudioVisualStream* sourceType;
97 
98  IAudioRender* sndSink;
99  IFrameWriterImage* imgSink;
100  IFrameWriterAudioVisual* imgSndSink;
101  IAudioVisualStream* sinkType;
102 
103  source.view(imgSource);
104  source.view(sndSource);
105  source.view(imgSndSource);
106  source.view(sourceType);
107 
108  sink.view(imgSink);
109  sink.view(sndSink);
110  sink.view(imgSndSink);
111  sink.view(sinkType);
112 
113  if (sourceType != nullptr) {
114  if (!(sourceType->hasAudio() && sourceType->hasVideo())) {
115  imgSndSource = nullptr;
116  }
117  }
118  if (sinkType != nullptr) {
119  if (!(sinkType->hasAudio() && sinkType->hasVideo())) {
120  imgSndSink = nullptr;
121  }
122  }
123 
124 
125  if (imgSndSource != nullptr && imgSndSink != nullptr) {
126  ImageOf<PixelRgb> tmp;
127  Sound tmpSound;
128  imgSndSource->getAudioVisual(tmp, tmpSound);
129  imgSndSink->putAudioVisual(tmp, tmpSound);
130  yCInfo(DEVICEPIPE,
131  "piped %zux%zu image, %zux%zu sound",
132  tmp.width(),
133  tmp.height(),
134  tmpSound.getSamples(),
135  tmpSound.getChannels());
136  } else if (imgSource != nullptr && imgSink != nullptr) {
137  ImageOf<PixelRgb> tmp;
138  imgSource->getImage(tmp);
139  imgSink->putImage(tmp);
140  yCInfo(DEVICEPIPE, "piped %zux%zu image", tmp.width(), tmp.height());
141  } else if (sndSource != nullptr && sndSink != nullptr) {
142  Sound tmp;
143  //the following values have been arbitrarily chosen and may be optimized.
144  //4410 samples correspond to 0.1s with a frequency of 44100hz.
145  sndSource->getSound(tmp, 4410, 4410, 0);
146  sndSink->renderSound(tmp);
147  yCInfo(DEVICEPIPE,
148  "piped %zux%zu sound",
149  tmp.getSamples(),
150  tmp.getChannels());
151  } else {
152  yCInfo(DEVICEPIPE, "Don't know how to pipe between these devices.");
153  yCInfo(DEVICEPIPE, "Piping is very limited at the moment.");
154  yCInfo(DEVICEPIPE, "You're probably better off writing some short custom code.");
155  return false;
156  }
157  return true;
158 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
yarp::dev::IAudioRender::renderSound
virtual bool renderSound(const yarp::sig::Sound &sound)=0
Render a sound using a device (i.e.
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::os::Searchable::findGroup
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
yarp::sig::Sound::getSamples
size_t getSamples() const
Get the number of samples contained in the sound.
Definition: Sound.cpp:404
yarp::os::Searchable::toString
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
yarp::os::Property::fromString
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1046
yarp::dev::PolyDriver::isValid
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:199
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::dev::IFrameWriterAudioVisual::putAudioVisual
virtual bool putAudioVisual(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image, yarp::sig::Sound &sound)=0
Write an image and sound.
yarp::dev::IAudioVisualStream::hasAudio
virtual bool hasAudio()=0
yarp::sig::Sound::getChannels
size_t getChannels() const
Get the number of channels of the sound.
Definition: Sound.cpp:409
yarp::dev::PolyDriver::open
bool open(const std::string &txt)
Construct and configure a device by its common name.
Definition: PolyDriver.cpp:143
yarp::dev::IAudioVisualStream::hasVideo
virtual bool hasVideo()=0
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::IAudioRender
Definition: IAudioRender.h:21
yarp::sig::ImageOf< PixelRgb >
yarp::dev::IFrameWriterImage
Read a YARP-format image to a device.
Definition: FrameGrabberInterfaces.h:343
yarp::os::Value::isString
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:159
yarp::dev::IAudioGrabberSound
Read a YARP-format sound block from a device.
Definition: IAudioGrabberSound.h:26
DevicePipe::updateService
bool updateService() override
Give the service the chance to run for a while.
Definition: DevicePipe.cpp:91
AudioVisualInterfaces.h
DevicePipe::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: DevicePipe.cpp:27
yarp::dev::IFrameGrabberImage
Read a YARP-format image from a device.
Definition: FrameGrabberInterfaces.h:241
yarp::dev::IFrameGrabberImage::getImage
virtual bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image)=0
Get an rgb image from the frame grabber, if required demosaicking/color reconstruction is applied.
yarp::dev::IAudioVisualGrabber
Read a YARP-format image and sound from a device.
Definition: IAudioVisualGrabber.h:27
yarp::dev::IFrameWriterImage::putImage
virtual bool putImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image)=0
Write an image to the device.
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
DevicePipe::close
bool close() override
Close the DeviceDriver.
Definition: DevicePipe.cpp:82
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
yarp::dev::IFrameWriterAudioVisual
Write a YARP-format image and sound to a device.
Definition: IFrameWriterAudioVisual.h:27
LogComponent.h
yarp::dev::IAudioVisualStream
Definition: IAudioVisualStream.h:27
DevicePipe.h
yarp::os::Bottle::tail
Bottle tail() const
Get all but the first element of a bottle.
Definition: Bottle.cpp:391
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::sig::Sound
Class for storing sounds.
Definition: Sound.h:28
yarp::dev::IAudioGrabberSound::getSound
virtual bool getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s)=0
Get a sound from a device.
yarp::dev::IAudioVisualGrabber::getAudioVisual
virtual bool getAudioVisual(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image, yarp::sig::Sound &sound)=0
Get an image and sound.
Time.h
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::os::Property::unput
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
Definition: Property.cpp:1029
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37