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