Skip to main content

a silhouette of a person's head and shoulders, used as a default avatar

Graphical profiling under Linux

The Oyranos library became quite slower during the last development cycle for 0.9.6 . That is pretty normal, as new features were added and more ideas waited for implementation letting not much room for all details as wanted. The last two weeks, I took a break and mainly searched for bottlenecks inside the code base and wanted to bring performance back to satisfactory levels. One good starting point for optimisations in Oyranos are the speed tests inside the test suite. But that gives only help on starting a few points. What I wished to be easy, is seeing where code paths spend lots of time and perhaps, which line inside the source file takes much computation time.

I knew from old days the oprofile suite. So I installed it on my openSUSE machine, but had not much success to get callgraphs working. The web search for “Linux profiling” brought me to a article on pixel beat and to perf. I found the article very informative and do not want to duplicate it here. The perf tools are impressive. The sample recording needs to run as root. On the other hand the obtained sample information is quite useful. Most tools of perf are text based. So getting to the hot spots is not straight forward for my taste. However the pixel beat site names a few graphical data representations, and has a screenshot of kcachegrind. The last link under misc guides to flame graphs. The flame graphs are amazing representations of what happens inside Oyranos performance wise. They show in a very intuitive way, which code paths take most time. The graphs are zoom able SVG.

Here an example with expensive hash computation and without in oyranos-profiles:

Computation time has much reduced. An other bottleneck was expensive DB access. I talked with Markus about that already some time ago but forgot to implement. The according flame graph reminded me about that open issue. After some optimisation the DB bottleneck is much reduced.

The command to create the data is:

root$ perf record -g my-command

user& perf-flame-graph.sh my-command-graph-title

… with perf-flame-graph.sh somewhere in your path:

#!/bin/sh
path=/path/to/FlameGraph
output=”$1″
if [ "$output" = "" ]; then
output=”perf”
fi

 

perf script | $path/stackcollapse-perf.pl > $TMPDIR/$USER-out.perf-folded
$path/flamegraph.pl $TMPDIR/$USER-out.perf-folded > $TMPDIR/$USER-$output.svg
firefox $TMPDIR/$USER-$output.svg

One needs FlameGraph, a set of perl script, installed and perf. The above script is just a typing abbreviation.

a silhouette of a person's head and shoulders, used as a default avatar

Custorm feeder for NetworkManager

In this article I would like to explain elementary way to create custom SecretAgent for NetworkManager. Program will communicate via DBus with NetworkManager. It would give password prompted by user, when NetworkManager asks for it. Once NetworkManager asks for password, my program will show dialog with name of wireless network and question about password. After giving password, program will send it to NetworkManager, so connection could be established.

Program will be very simple and don’t support all features of NetworkManager. Firstly, it don’t remember passwords prompted by user. Secondly, it don’t return error, when error occurring. It only supports one method of SecretAgent interface – GetSecrets.

Let’s start at beginning

What DBus is?

Dbus is service of modern GNU/Linux distribution. It allows processes to communicate. It supports sending messages process in the same session and registered as system service(or kernel service). We would like to use system service called NetworkManager.

DBus is object-oriented. It supports methods, signals, properties. All these thinks are assembled to interface, interfaces are assembled into paths and paths are grouped to objects(ex. services).

What NetworkManager is?

NetworkManager is system service, which storing connections settings and supports connecting to networks.

For purpose of this article, we will use org.freedekstop.NetworkManager(at this name NetworkManager is spoken), /org/freedesktop/NetworkManager/AgentManager paths and org.freedesktop.NetworkManager.AgentManager. We will use these thinks only for Register method. We will also implements method GetSecrets of org.freedesktop.NetworkManager.SecretAgent interface.

Which will be connected with our project?

Our program will use libgreattao. We will write one single application to rule all desktop environment, virtual console and shell.

Let’s go!

Firstly, we need to initialize libgreattao. To do this, we will call tao_initialize, giving program name(mandatory) as first argument, help text as second, pointer to argument’s count(mandatory) as third, as fourth pointer to array of char’s array(mandatory).

Next step is connecting to system bus of DBus. We will achieve this by step displayed below:


bus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);

But firstly, we need to declare two variables. First is called bus_session and it should been declared in global scope. Second is called error and it should been declared in main function scope.


DBusConnection *bus_connection;

and


DBusError error;

We must, also, register to AgentManager. We can done this by invoking Register method of org.freedesktop.NetworkManager.AgentManager, which can be found at path /org/freedekstop/NetworkManager/AgentManager, which is handled by object org.freedekstop.NetworkManager

  DBusMessage *msg;
  DBusError bus_error;
   
  msg = dbus_message_new_method_call("org.freedesktop.NetworkManager",
      "/org/freedesktop/NetworkManager/AgentManager", "org.freedesktop.NetworkManager.AgentManager",
      "Register"
    );

After creating a message, we can attach an arguments to it. Register catching one argument – agent’s identificator. It has string type and containing from 3 to 255 characters, but without colons. In our application it will looks like below:

const char *service_name = "org.taolib.nmpassword";

Below is displayed, how attach arguments to message:

dbus_message_append_args(msg, DBUS_TYPE_STRING, &service_name, DBUS_TYPE_INVALID); 

DBUS_TYPE_INVALID points, that there’s no more arguments. It’s obligatory!

At we face to face with most important part of DBus based programs – sending messages and waiting for replies:

dbus_connection_send_with_reply_and_block(bus_connection, msg, -1, &bus_error);
 
if (dbus_error_is_set(&bus_error)) {
   
  show_message_and_abort(bus_error.message);
}

I won’t introduce body of show_message_and_abort function. It doesn’t contain DBus related code. You can see, how it works, by looking at sources. Minus one meaning very long. I don’t know how order this function to wait infinity, so I give them maximum(for x86) value as unsigned int. Function dbus_connection_send_with_reply_and_block will waits for reply message omitting everything else(other messages will waits in poll).

Please, don’t forget about initializing error structure, called bus_error. At the end of our function, we will freeing error message:

dbus_error_free(&bus_error);

At now, we need add support for org.freedesktop.NetworkManager.SecretAgent interfacve. First step is give information, we were listening on this interface. Entire duty is closed in add_signal_support(I know name was misleading):

char *buffer;
int length;
DBusError error;
 
length = sizeof("interface=''")
            + strlen(interface);
buffer = (char *) malloc(length);
 
snprintf(buffer, length, "interface='%s'",      
     interface);
dbus_error_init(&error);
 
dbus_bus_add_match(bus_connection, buffer, &error);
 
if (dbus_error_is_set(&error))
{
     show_message(error.message);
}
 
dbus_error_free(&error);
 
free(buffer);

In first step we counting amount of required space for allocating buffer for character array for dbus_bus_add_match function. Sizeof will return size of character array(including character of code 0, so we don’t plus one). In second step we fill array of characters, using snprintg. In next steps we initializing error, invoking dbus_bus_add_match, checking for error occurrence.

In this moment we do most important thing – create a timer. This timer will be calling messages handling function with 100 ms interval.

timer = tao_add_timer(100, (void (*)(void *))nm_dbus_loop, (void*)bus_connection);

Timer like libgreattao windows are pointers of void, because they aren’t suspected to being used by application. Our message handling function is calling nm_dbus_loop. It looks like below:

DBusMessage* msg;
struct password_prompt *prompt;
void *window;
 
  dbus_connection_read_write(bus_connection, 0);
  msg = dbus_connection_pop_message(bus_connection);
   
   
  if (msg) {
    window = NULL;
     
    if (dbus_message_is_method_call(msg, "org.freedesktop.NetworkManager.SecretAgent", "GetSecrets")) {
     
  prepare_return_secret(msg, &window);
    }
     
    if (!window) {
     
  dbus_message_unref(msg);
    }
  }

As second argument of dbus_connection_read_write we give 0. What that meaning? It means that this function won’t block. This function are only decorator – you can remove it and I forget about this. This function can be used to detect bus connection broken, because in this case returns FALSE.

We retrieve message(NULL means no message in poll) to check this is not invocation of our DBus method. In next step we check password prompt is created – if not, we remove message. In other case, message are necessary to get network ssid and to create response message.

At now most hard thing. We must read ssid of network and question flags. NetworkManager are sending question flags, which in example inform that keyring can be interactive(communicate with user).

First argument of our DBus method is network settings, second is configuration path(not as a file – it points to path in NetworkManager). Third and fourth arguments are not known to me – I don’t use it. The last argument is flags.

At the beginning of our function we declare some variables:

DBusMessageIter container, item, a, character, b;
int buffer_position;
char buffer[1024];
char ok;
char *key;
struct nm_settings *prompt;
unsigned int flags;

DBusMessageIter are iterators. It remembers on which argument we do last action.

First, what we doing is setting first iterator to first argument. It hast array type, so we will checks elements of this array. We called dbus_message_iter_recurse(&container, &a); In next step we checks elements of current element. It was caused that this table are dictionary and dictionary contains elements of type DBUS_DICT_ENTRY. This kind of elements assembles two elements – key and value. We are searching string 802-11-wireless for assign dict-value to iterator called item. In next step, we are searching for string ssid for setting iterator to value of dictionary. The case is very complicated – ssid aren’t stored as string, but as variant of array of bytes. We need to call recurse twice.

if (strcmp(key, "ssid") == 0) {
     dbus_message_iter_next(&a);
     dbus_message_iter_recurse(&a, &b);
     dbus_message_iter_recurse(&b, &character);
      
     buffer_position = 0;
     do {
   dbus_message_iter_get_basic(&character, &buffer[buffer_position]);
   ++buffer_position;
     } while (dbus_message_iter_next(&character));
   }

Each retreived byte we add to our buffer. In next step (ssid) we checks that ssid was retreived correctyly. If not, we exits from function.

To obtain flags(the fifth argument), we need to call recurse two times. If first org second flag are set, the NetworkManager allowed to communicate with user. In other case, we exits from function.

At the end of function, we create new window and sets window variable to newly created window.

Most interesting thing is, what cause, when user type password. In this case return_secret will be called.

Return_secret function acts as mirror of previous function, because it create message, so it calls dbus_message_iter_init_append instead of dbus_message_iter_init. Both function’s signature’s are identical. Only difference is in behavior – dbus_message_iter_init_append will write arguments instead of reading them. We will also use dbus_message_iter_append_basic – we will use this function to writes strings. We need also dbus_message_iter_open_container, which are mirror function of dbus_message_iter_recurse. Signature in DBus are character’s array. We create signature by calling dbus_message_iter_open_container.

dbus_message_iter_open_container(&container1, DBUS_TYPE_ARRAY,
                     DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
                     DBUS_TYPE_STRING_AS_STRING
                     DBUS_TYPE_ARRAY_AS_STRING
                     DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
                     DBUS_TYPE_STRING_AS_STRING
                     DBUS_TYPE_VARIANT_AS_STRING
                     DBUS_DICT_ENTRY_END_CHAR_AS_STRING
                     DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &container2);

Third argument is part of signature. Our function will return array of dictionaries, which key as string and value as dictionary. Inner dictionary are assembled with string as key and variant as value. We will set content of variant by string. In this way settings in NetworkManager are stored and our application must follow that way.

After filled of container, we must close it. Strings are not assembled type. We will use dbus_message_iter_close_container, which waiting for pointer to initialized iterator and pointer to iterator. First argument are parent iterator and second are child iterator.

As a result of calling our function, we will retrieve:

[{"802-11-wireless" => [{"security" => variant("802-11-wireless-security")}],  "802-11-wireless-security" => [{"key-mgmt" => variant("wpa-psk"), "psk" => variant(our_password)}]

I must mentoin, that variant are used to storing variables of different kind.

Please, remember about downloading latest version of libgreattao. Instructions about how do this, you can find on SourceForge.

Code of application you can download from:
My home site

In preparation are applet for connecting with networks. It also uses libgreattao. Currently libgreatao doesn’t support tray.

the avatar of Klaas Freitag

Incremental Sync in ownCloud

[caption id=“attachment_548” align=“alignright” width=“240”]KLAASNautilus Shell, David Bygott[/caption] Incremental Sync is probably the feature that most people ask, or even sometimes cry for. Recently there was another wave of discussion about ownCloud is doing incremental sync or not. I will try again (as in this issue) to explain why we decided to slowing that feature. Slowing means that it will be done later, not never, as it was stated. It is just that we think that other things benefit the whole idea of ownCloud more. That has plain technical reasons. Let’s dive a bit into.

RSync is great

Nobody will object here. In a nutshell, this is how rsync works: There is a file on the client and on the server. The idea is to not transfer the entire file from one side to the other if either side changes, but only the parts that have changed.

The original rsync does that by chopping the file to blocks of a given size and calculating a checksum of each of the blocks. The list of checksums is sent to the server and - here’s the trick - the server looks at its version of the file and for each of the checksum in the list, it seeks if it finds the same block in the file. That will often not be at the same position in the file, but maybe somewhere else. That is done for each block, and finally the server will work out the information of which parts of the file are existing and which are not and have to be sent by the client.

By way of this clever algorithm, we will just have to transmit a very small fraction of the changed file, because most content did not change. And that is what we want! Yeah!

Mission accomplished? No, not really. While there is basically nothing wrong with the idea in general, there is a severe architectural downside. The rsync algorithm depends on a strong server component which, for each file, searches around and calculates checksums. In an environment where we potentially have a lot of clients connecting to one server that would create a huge load on it which we need to avoid. So what if instead of putting the burden on the server’s shoulder, we could make the clients take the responsibility?

And guess what, there has been somebody thinking about that before and he says:

Use ZSync for this!

ZSync basically turns the idea of rsync upside down and shifts the calculation of checksums away from the server and onto the clients. That means that with zsync, the server can keep a static list of checksums for every block specific to a version of a file. The list can for example be computed along the upload of the file to the server. From that point it does not change, as long as the file does not change. That means less computation work for the server, and maybe this job can also put into the client.

So far that sounds cool (even though some questions remain) and sounds like something that can help us.

Unfortunately, the approach does not work very well for compressed files. The reason is that if a file gets compressed, even if only a couple of bytes in the original file change, the compression algorithm usually changes a lot all over the entire file. As a result, the zsync algorithm can only compute a comparably large diff. Given the cost of computation that can turn inefficient quickly.

“But who uses compressed files?” you might argue. The problem is that almost each and every of the files in everyday life are stored compressed. This is for example true for Microsoft Office files and the Open Document files produced by LibreOffice and Apache OpenOffice. They are really renamed ZIP containers, that hold the documents with all its embedded files, etc.

Now of course you will reply that zsync has an improved algorithm for compressed files. Yes, true, that is a great thing. However, it involves that the compressed file gets uncompressed to be worked on by zsync. Afterwards it is compressed again. And that is the problem: As common compressors do not leave a hint behind _how_ the file was compressed, it is not possible to reliably recreate a file that is equivalent to the original one. How will apps react on a file that has changed its compression scheme?

Results

As said above, yes, we will at one point of time implement something along the zsync algorithm. The explanations above should show however, that at the current state of ownCloud, other features will improve ownClouds performance, stability and convenience more. And that is the important thing for us, more than pleasing the loudest barking dogs. Here is a rough outline of how I would move on on this, open for your suggestions and critique: The zsync algorithm is designed to improve downloads. We need it for both up- and downloads, and it needs to be thought through if that is also possible. For the server side functionality, there are a couple of open questions which carefully have to be investigated. Preferably an app can be written that provides the handling of the zsync checksum lists. That has to be clarified and discussed, and that will take a while. But as outlined above, this idea is only clever for a limited amount of file types. So what I would suggest first is that we get an idea of the file types users usually store in their ownCloud, so that we can do a validated estimate on how this feature helps. I will follow up on this first step.

Thanks for reading this long blog post. Thanks Danimo for lectorate.

a silhouette of a person's head and shoulders, used as a default avatar

Ya está disponible qRAE para Windows y Mac OS X

qrae_plataformas

 
qRAE 0.4.2 es la primera versión que está disponible para los sistemas operativos Windows y Mac OS X. Está en fase experimental, sobre todo en Mac. La versión de Windows viene sin instalador; es un archivo comprimido en plan portable.

En relación a las anteriores versiones, viene con pequeñas mejoras y autocompletado de términos previamente consultados.

Os podéis bajar esta versión en la zona de descargas.

Nota: Si usas ONO, puede que no resuelva lema.rae.es. Como solución temporal, cambia tus DNS (ej: OpenDNS). Gracias a Eduardo Bohoyo por depurar el problema.

a silhouette of a person's head and shoulders, used as a default avatar

Linux audio library smackdown part3: SDL

How many of you remember Loki Games/Entertaiment/Software? Hands up  now! I’m still waiting.. wuhuu hands up now! Loki.. Loki! Oh still no hands.. what a pity (Damn how old Am I?). Ok I Admit! It was before Steam, before Internet was this huge fast beast for watching videos, telling how you are doing right now and sharing photos.

Loki only shipped CD’s for installing your application and there wasn’t hot fixes waiting when you got CD from post or yes there where but I had to wait and wait for 200 MB blob to download for ages with my 57600 modem. All the time I thought that I’ll boot Windows for playing Castle Wolfestein. It was long before before Linux have any gaming community what so ever. There weren’t Firefox or Chrome available and Google was just starting to own our lifes.  So it was dark days of late ’90 (we had electricity thanks for asking).

Loki was founded on 1998 and  it got on bankruptcy on 2001 after IT bubble blowed. Loki changed Linux in good way. One thing that they left behind was called Simple Direct Layer (SDL1). SDL layer sits top on Xorg that makes creating games more ease. You can port your game to Windows and Mac OS X with little effort. Current version of SDL is SDL2.

Good thing about SDL is that it abstracts drawing to screen in Windows, Mac OS X and X-windows. SDL1 was all about pixel buffers but SDL2 is all about surfaces and acceleration. It support many more OSes but that’s not the what we are looking at today. So how do you playing audio out of SDL1/2?. Good thing is audio interface stayed same through conversion from SDL1 to SDL2. They only added few bugs and float point audio in SDL2.  SDL1 have recording but I didn’t manage to make it work so if anyone with more patience than me can lead me to correct path I would be happy puppy. Here is SDL Github location: https://github.com/illuusio/linux-audio-example/tree/master/sdl

Simple Direct Layer API

Supported outputs: Alsa, Oss, Pulseaudio, Mac OS X, Windows, iOS, Android
License: SDL1 GNU Lesser General Public License 2.1 and SDL2 ZLib license

SDL API is like name says very simple and it’s very popular toolkit. SDL can be found on every bigger Linux distribution out of box (at least SDL1 and SDL2 is fast spreading). SDL can be used for graphics, input and audio. SDL API is actually almost dead simple. Support for audio outputs if very good on Linux and things are like if it’s working on Linux it works on Mac OS X and Windows the same.  Playing audio was so easy that I get frustrated when I tried to make recording example. I even read the source code how it should be done and yes It doesn’t work with SDL2 that what I learner but I couldn’t get it working in SDL1 nether.
SDL audio API is fully and only callback based. You give your callback function and it just get called when SDL feels audio should be played. After that you feed the correct type output as much callback asks and then you wait another callback to feed more. Simple and very efficient.
If you compare callback system to Pulseaudio with SDL you can’t get no information what ALSA, Pulseaudio or OSS is doing. You just feed and they just get played out of speakers. If audio is that what makes your game rock then SDL could be what you want.

Sound card or chip
↑
ALSA kernel drv
↑
ALSA or Pulseaudio or OSS C API
↑
SDL 1/2 audio C API

SDL audio for whom.

It’s for people who don’t need much or don’t have interested how audio get played. People who likes to get audio out with ease and fast. Currently SDL development is under Steam umbrella so they have some commercial interest with it. SDL1 was licensed with LGPL+2.0 and SDL2 is under Zlib license which mostly same like than MIT-license. SDL is also mostly rock solid, sanely licensed (SDL2) and used by many many Indie (The UnReal World RPG if I have to mention one Indie game that I know well) and not so Indie games. Biggest problem with whole SDL is lack of documentation. There is wiki but if there is no question you crawler through web and test everything you find along if it solves your problem. Development is also little bit slow and fixing bugs are not getting fixed in fast but if you can live with that it’s all yours!

Summary: Very simple to get audio out of speaker. Not for high-end or surround sound but mostly for games or playing videos this is excellent! Learning curve is non exist or  there is small: you got to understand C well to work with SDL. Works with IOS and Android so if you are on cross platform this can help you out.

a silhouette of a person's head and shoulders, used as a default avatar

Using Icecream to speed up the LibreOffice Android build

Lots of us is using Icecream distributed compiler for the LibreOffice development. It is usually the first thing we set up at a hackfest, to speed up the build for everybody (given that it only seldom occurs that all the people build at the same time).

The LibreOffice FOSDEM HackFest is no exception; Markus even brought a MacMini with a Linux VM to provide even more building power.

The problem was that I decided to hack on Android-related stuff; and the setup for Icecream was something that I had on my TODO, never achieved, and now it became really paiful. There was a small problem that was confusing me for a while, but finally I have a how-to!
  1. Make sure you have a recent enough repo that contains this fix
  2. Create cross-compile package for your environment like this (adapt the paths of course):
    /usr/lib/icecc/icecc-create-env --gcc /local/libreoffice/android-ndk-r10d/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc /local/libreoffice/android-ndk-r10d/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++
  3. icecc-create-env will create a tarball with a hash instead of a readable name, rename it to something sensible, let's say
    /local/libreoffice/android/arm-linux-androideabi-4.9.tar.gz
  4. Find out how exactly is the compiler called. Just do ./autogen.sh > .log 2>& .log, and search for arm-linux-androideabi-gcc and arm-linux-androideabi-g++. You will need it with all the options that are there, it is quite a long command line!
  5. Modify your autogen.input to start like (we'll:
    CC=icecc [the_command_line_with_options_for_arm-linux-androideabi-gcc]
    CXX=icecc [the_command_line_with_options_for_arm-linux-androideabi-g++]
    --with-parallelism=10
    --enable-icecream
Now every time you start a new shell, just do:

export ICECC_VERSION=/local/libreoffice/android/arm-linux-androideabi-4.9.tar.gz

start the build:

./autogen.sh
make

and enjoy the lightning fast compilation for Android! :-)

Update: Since this commit, it is not necessary to set the ICECC_VERSION in every env any more, the one from the ./autogen.sh time is preserved - it has to be in sync with CC/CXX setting anyway. In other words, you can just add ICECC_VERSION to autogen.input the same way you have added CC/CXX there:

CC=icecc [the_command_line_with_options_for_arm-linux-androideabi-gcc]
CXX=icecc [the_command_line_with_options_for_arm-linux-androideabi-g++]
ICECC_VERSION=/path/to/the/tarball
--with-parallelism=10
--enable-icecream
...your other options...
the avatar of Andrew Wafaa

Sorry for being 50 Shades of Wrong

I was at the wonderful FOSDEM this weekend just gone and met up with many fine folk (as always) unfortunately there were also many people I didn’t manage to catch up with. On the Sunday I attended a talk on The Tumbleweed Factory given by openSUSE’s very own Release Manager, Coolo. I made a statement about this talk, and have been challenged on that statement - I’m glad that I was challenged by one of our community members, because they were quite right to do so.

a silhouette of a person's head and shoulders, used as a default avatar

a silhouette of a person's head and shoulders, used as a default avatar

the avatar of Alberto Garcia

Vídeo esteroscópico casero

Una fotografía/vídeo estereoscópica es aquella técnica que permite reproducir la sensación de relieve y volumen de los objetos en quien contempla la imagen. La técnica básicamente consiste en hacer llegar a cada ojo del espectador dos imágenes ligeramente diferentes de tal manera que al procesarlas el cerebro recree la sensación de profundidad de la escena original.
Para conseguir esto hay multitud de técnicas más o menos exitosas, desde un simple GIF animado realizado con una sola cámara que muestra alternativamente el ojo izquierdo/derecho, hasta modernas técnicas cinematográficas de imagen a todo color, a gran resolución de imágenes perfectamente visibles con gafas especiales o sin ellas.
La técnica más primitiva consistiría por tanto en grabar dos imágenes al mismo tiempo y proyectarlas en paralelo para hacer llegar mediante un dispositivo a cada ojo la imagen que le corresponde. Sin embargo hacer llegar dos imágenes con calidad fotográfica y diferentes a cada ojo no es fácil (ni barato si se aspira a cierta calidad). Con el abaratamiento de la electrónica los cascos de realidad virtual se han vuelto razonablemente accesibles en los últimos años, pero aún siguen siendo raros y poca gente dispone de ellos habitualmente, pero lo que sí tiene mucha gente hoy en día es un smartphone, y aquí es donde entra el ingenio de algún ingeniero de Google ¿porqué no usar la pantalla del teléfono móvil y transformarla en un casco de realidad virtual? solo hace falta un poco de cartón, dos lentes y una tijeras y ya tenemos unas Google Cardboard.

Las Google Cardboard son básicamente un recortable de cartón dentro del cual instalamos el teléfono móvil que proyecta un vídeo partido en dos frames, uno para cada ojo (además de un ingenioso sistema de imán para transmitir “clicks” ar una aplicación en nuestro Android). Google distribuye los susodichos planos/instrucciones para construir tu propio casco de realidad virtual en casa y en Ebay las podéis comprar por 3-4 euros. Una vez tenemos nuestro casco de realidad virtual ya solo falta generar nuestro vídeo estereoscópico para el casco.

Vídeo estereoscópico

Como ya he contado muchas veces tengo en casa varias cámaras tipo CCTV. Desde que conseguí mis Google Cardboard llevo queriendo experimentar a juntar dos de estas cámaras y producir mis propias películas 3D caseras, y eso es lo que hice estos días pasados.
La conexión del hardware no tiene más truco, se cogen dos cámaras conectadas al ordenador con idéntico objetivo (6 mm en mi caso) se sitúan en paralelo (teóricamente con una separación de 63 mm entre el centro de ambos objetivos, que es la separación media entre pupilas) y se graban dos vídeos al mismo tiempo de todo lo que se quiera.Grabadora estereoscópica

Problemas de calidad

La calidad de estas cámaras no es para tirar cohetes como os podéis imaginar, dan para un vídeo de 640×480 px y poco más. No lo que no me esperaba es que dos cámaras idénticas con idéntico objetivo y ubicadas en el mismo sitio dieran imágenes tan sorprendentemente distintas, no ya en nitidez/saturación/contraste (estos miniobjetivos si se les aprieta mucho la rosca del enfoque podemos hacer saltar hacer saltar las lentes por los aires con mucha facilidad. Ya he tenido que montar los cristalitos de algunos de estos objetivos varias veces.)… sino pura y simplemente en encuadre. Me explico: si situamos las dos cámaras una al lado de otra y grabamos a una persona a 2 metros con un encuadre tipo busto/retrato, si la cámara A la encuadra bien, la cámara B la encuadra de los ojos para abajo.
Salida de vídeo a distintas alturas
Esto es, como si una de las cámaras tuviera un ligero cabeceo hacia abajo y viera una porción de espacio por debajo de lo que veía la otra. Deduje que esto es muy probablemente producido por ligeras variaciones en la posición del sensor y en el cono de luz que proyecta el objetivo. Una variación de 1-2 mm produce un desplazamiento enorme en la imagen capturada (y no vale con “levantar” ó “agachar” algunas de las cámaras porque al cambiar la altura del centro del objetivo se produce un cambio en las perspectiva/paralelaje que desbarata cualquier intento de 3d).
Estuve probando diversas cámaras pero el resultado era siempre el mismo, no tengo dos cámaras que situadas de forma contigua graben lo mismo a la misma altura.

Después de diversas pruebas, incluida el intento de reajustas la posición del sensor interno de las cámaras opté por un solución salomónica: recortar la salida de ambas cámaras al área compartida por ambas, de tal manera que el fotograma válido por ambas cámaras pasaba de 640×480 a 640×332, se desechaba una franja de 148 en ambas cámaras, en una por arriba y la otra por abajo. Bueno, siempre me han gustado las panorámicas.

Software FFMPEG

Para la grabación y composición de la panorámica de vídeos he usado ffmpeg, un potente editor/conversor de vídeo en linea de comandos pero que ha traído otro pequeño problema: al tomar dos dispositivos y codificarlos a mp4 para producir la composición final producía un ligerísimo retardo (apenas 4-5 frames) en uno de los vídeos de manera que cualquier movimiento en la imagen se producía un poco antes en el ojo izquierdo que en el derecho. Aunque era un retardo muy pequeño, había que fijarse mucho para descubrirlo pero en cambio producía un efecto muy incomodo de falta de sincronía. Nada especialmente grabe que no se pudiera arreglar con la opción itsoffset desde el propio FFMPEG.

Estas son algunos de las líneas de comandos ffmpeg utilizadas para producir el vídeo estereoscópico:

Comando básico para reproducir un dispositivo conectado a la capturadora del ordenador
ffplay -s 640x480 -f v4l2 /device/video4

Capturar, componer y mostrar en tiempo real dos dispositivos de vídeo
ffmpeg -s 640x480 -f v4l2 -i /dev/video4 -s 640x480 -f v4l2 -i /dev/video8 -filter_complex "[0]pad=iw*2:ih[a];[a]overlay=w" -b 2200k -f mpegts - | ffplay -

Retardar ligeramente el “ojo derecho”
ffmpeg -s 640x480 -f v4l2 -i /dev/video4 -s 640x480 -f v4l2 -itsoffset 0:0:00.08 -i /dev/video8 -filter_complex "[0]pad=iw*2:ih[a];[a]overlay=w" -b 2200k -f mpegts - | ffplay -

Recortar (crop) fotogramas, corregir luminancia en ojo derecho y guardar a mp4
ffmpeg -y -s 640x480 -f v4l2 -i /dev/video4 -s 640x480 -f v4l2 -itsoffset 0:0:00.08 -i /dev/video8 -filter_complex "[0]crop=640:340:0:140[a];[1]mp=eq2=0.8:1:0:2[b];[a]pad=iw*2:ih[a];[a][b]overlay=w" video_final.mp4

Todavía quiero hacer algunas pruebas, desplazando cámaras cambiando objetivos, etc.. pero ya se puede ver algo. Si tenéis las Google Cardboard o similar podéis ver el vídeo en grande en esta dirección.