sexta-feira, 6 de novembro de 2015

Save radio shows in podcasts so you can listen later!

The story behind this project is that, since I moved to Germany I miss some old news shows on the radio that I use to listen in Brazil. Yeah, I know I can listen to them online. But due to the difference on the timezone, sometimes I just don't want to listen to it while I have my lunch, or work. Saving them into a podcast is much easier and gives me the freedom to listen whenever I want.


The idea is pretty simple: Use ffmpeg to save the stream online, copy it to your podcast server and enjoy. Let's work to put everything together:

First thing you're gonna need is some scripts. The first one captures the stream of audio and dumps into a ts file:

otubo@deathstar /opt/ $ cat capture_stream.sh 
#/bin/bash

TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S");
/usr/bin/ffmpeg -i URL -c copy /opt/radio_show_${TIMESTAMP}.ts

Next, you're going to need a script to encode the transport stream into mp3:

otubo@deathstar /opt/ $ cat encode_stream.sh 
#/bin/bash

LAST_TS_FILE="$(ls -1t /opt/|grep ts|head -1)"
NEW_MP3_FILE="$(echo $LAST_TS_FILE|sed -e 's/ts/mp3/g')"
LAST_TS_FILE="/opt/${LAST_TS_FILE}"
NEW_MP3_FILE="/var/www/html/media/${NEW_MP3_FILE}"
/usr/bin/ffmpeg -i "${LAST_TS_FILE}" -acodec mp3 -write_xing 0 "${NEW_MP3_FILE}"
/usr/bin/id3 -t "$(date)" "${NEW_MP3_FILE}"
/usr/bin/id3 -a "News" "${NEW_MP3_FILE}"
wget -O- http://GENERATOR SERVER ADDR/html/pg-cron.php?key=YOUR KEY >/dev/null 2>&1
rm "${LAST_TS_FILE}"

Little gotcha: I had this issue with the iPhone podcast app, according to this ticket, adding the option -write_xing 0 solves the problem.

Note that in the last script, there's the "Podcast Generator" server. That's a really neat and simple to use podcast server made with php5. You can download it here: http://podcastgen.sourceforge.net/. The instructions to install and configure are very easy, and as they say: Newbie-proof.

Let's dig down a little bit on the last script: First you run ffmpeg to convert the transport stream into mp3, then you set the title id3 tag for the title of this podcast "episode", than set the artist id3 tag for the description. After that you call wget to reload your podcast library and update the RSS. And it's done!

Now let's put everything on the crontab:

30 10 * * * /opt/capture_stream.sh
2  12 * * * killall -9 ffmpeg
3  12 * * * /opt/encode_stream.sh

Easy and simple: The show starts at 10:30 (my germany time) and ends at 12h00 (I added 2 more minutes, just in case). Three minutes after the dump is finished I start the encoding script. And that's all! Podcast generator also gives you RSS url to put on your smartphone. Pretty easy :D

RetroPie: Play snes games on your Raspberry Pi!

So I wanted to play some old Super Nintendo games and also share this special retro gaming style with my daughter. So I decided to put it on my Raspberry Pi and have some fun. So let's do it!

The process is pretty easy, but the controllers configuration are a little tricky. Follow this guide to install RetroPie on an SD card and boot up your Raspberry Pi. This documentation explains everything you need to know about configuring wifi, setting up everything to have all up and running.

Now to the controller configuration:
I bought two ordinary snes controllers, they work pretty fine. The configuration is done via the file /opt/retropie/configs/all/retroarch.cfg, find this file and open it with your favorite text editor. Now find the input_player and erase everything related to it and make it look like this:

input_device_p1 = "0"
input_libretro_device_p1 = "0"
input_player2_analog_dpad_mode = "0"
input_player1_joypad_index = "0"

input_player1_b_btn = "2"
input_player1_y_btn = "3"
input_player1_select_btn = "8"
input_player1_start_btn = "9"
input_player1_up_axis = "-1"
input_player1_down_axis = "+1"
input_player1_left_axis = "-0"
input_player1_right_axis = "+0"
input_player1_a_btn = "1"
input_player1_x_btn = "0"
input_player1_l_btn = "4"
input_player1_r_btn = "5"


input_device_p2 = "1"
input_libretro_device_p2 = "0"
input_player2_analog_dpad_mode = "0"
input_player2_joypad_index = "1"

input_player2_b_btn = "2"
input_player2_y_btn = "3"
input_player2_select_btn = "8"
input_player2_start_btn = "9"
input_player2_up_axis = "-1"
input_player2_down_axis = "+1"
input_player2_left_axis = "-0"
input_player2_right_axis = "+0"
input_player2_a_btn = "1"
input_player2_x_btn = "0"
input_player2_l_btn = "4"
input_player2_r_btn = "5"

input_enable_hotkey_btn = "8"

input_exit_emulator_btn = "9"

This is all you're going to need to make those USB controllers work. If you bought a different controller, you can try to configure them using this command:

sudo ./retroarch-joyconfig -j 1 -p 2 >> /opt/retropie/configs/all/retroarch.cfg

This will interactively ask you to push every button in order to map it to the correct values. Always remember to double check your /opt/retropie/configs/all/retroarch.cfg to avoid duplicates, this got me in trouble for quite some time.

After that, you can copy all your ROM files into /home/pi/RetroPie/roms/snes, or if you have ROMs for different console, just copy into the correct folder.

That's it. Enjoy :D

segunda-feira, 8 de junho de 2015

GSM bridge between two raspbx hosts

Disclaimer: I know there's a lot of options on the market for free long distance calls like Skype, Viber, WhatsApp and so on. The goal of this personal project was all along to come up with a proof of concept that actually works, study something different other than my actual job and, why not, have some fun :) BUT, yes, there's one single use case that this setup will be useful: You're in the middle of nowhere, no internet connection and you need to call someone in the other side of the planet but you don't want to pay a long distance call: BINGO!

Disclaimer2: For this tutorial you'll have to be prepared for Asterisk terminologies like trunks and channels. If you're not familiar with this vocabulary, please take a quick look at this documentation.

Description of the environment: The server1 is placed on the Brazilian side. It's a Raspberry Pi model B that runs an asterisk-modded-for-raspberry-pi distro called raspbx. Attached to it there's an USB GSM dongle with a SIM card for the local Brazilian operator. In the same network of my server1 there's a Cisco PAP2t Internet Phone Adapter and, attached to it, a regular land line telephone. The same setup is duplicated on the German side, except that the Raspberry Pi is model B+.

5VpNk+MmEP01rkouqZFky7PHzGZ2ctmqVPmwyRFLjESNJFQIf82v30ZqJNBHxh47yFXxxaIFNLzmPRrsRfA1P74IUqbfeUyzhf8QHxfBHwvf95bBF/hSllNjeVyvGkMiWIyVOsOGvVM0PqB1x2JaWRUl55lkpW2MeFHQSFo2IgQ/2NVeeWZ7LUmiPXaGTUSyofUHi2WKs/DDzv4nZUmqPXshTnhLordE8F2B/hZ+8Fp/mtc50X3hRI8PTXGJ7U9YXi+1p8Ia0TvnuWUQtGrRwz5fGQ4Ly1suYioak7ZlrHgzMQqeIZyCc2ipnvLjV5qpkOpoNc2+Tbxt4RK0sHxPNgiaFpU8achpDBHAIhcy5QkvSPbcWZ9qWKnqAUB6SmWewaMHj/TI5N/G8z+qym8rKFWSCPm7WhCq1z2BFo3tG4PxYIsi7tUAi/E+2ol97VUVmmGrsU7OHE0V34kIa+FswXFCsVbNBwTnhfKcSnGqw5kRyfZ27wQjlbT1OmjhAdEdRxpdw8R22KkgVbmlonZXsoUfZjCkp5jt4TFRjyOmXyoKIAjvV/0O3I606Ft7IT6kTNJNSWpYDqAcdhhxpFRIqoVkAt0hbthAywzKTvCA5YNBYmRAavBX264BGglrAL2rtmB42XxXQsiLBGAYQTYH8cyngZ0bQt8bQrgegfDLDSDUri5SBYueM0nE51QBZ2uqQr2/OFAFdG2qAt9J2CTcr7c26pqzK3ecxe3cgIEVgEIB4XAPRLB6tIEIh0B4ev82kQhvQT29Pm+zIX/Al3M49zlOrYecqlF1wCkPdwCn6gVYiJN6pVrUhXtQtpEouMp30LVB6ZKU9VGBVREfrnLnG+rSYU6CgmKAIWlGy5QXdA4k9JQQidWZqYW2XYOEXhf/ITlrBt5JauGNMNCrj5guhHDIwSvOHP69nzlCvT5nOHR4Q4ZfcOqYhtY9iD1tcHns8PHuxak2tBt3IwJ3snE3GjHPmUT7vodDySqY71CiU/y7T8XNhaMM/YWj754uSJX0/a/OlR4R9qvwRI38n2z9Y5FwtvVr5xPpN76ddXt2mX+P3L62Cfg8WNiy5jIDD7APA4utIO8sY/ATh05Y4OblwMXbkLBZBj/9KGI60H57veh18NEtzC3WS4CDNTAaQtGpRpSRqmKRjYClPF2Nz16vDGEygNBQmTho29nagh7+4nD31kXB620F7baou2g0D1uZPzV90JHXvy5rhHLQUR2rdtrnhW+ofgkVuVrfIcnVSi22lfqqF/sgKW8X/1k5uUtKhD1OaBlxwomhht4fJybyF28kbfw3zpxPj76n9rx0KT0mh3w1PaDY/aDcVO/+LBA8/wQ=
What it does: This is the list of features that this project performs right now:
  1. Someone in Brazil calls my Brazilian number, the dongle1 answers the call, redirects to an IAX2 trunk directly to server2. The server2 takes this incoming call and uses the dongle2 to place a new call to my german cellphone. The other way is still pinned to a single phone -- like, call my dongle2 to reach a single number in Brazilian side -- but improvements are coming.
  2. Someone uses phone1 on Brazilian side to call extension phone2 on German side (and vice-versa) like a regular landline phone.
  3. Someone uses phone1 or phone2 to reach my german cellphone.

Ok, so LET'S DO IT!!

I'm gonna skip the PAP2t configuration because I think it's too much. This is only needed if you want to use land line telephones, it's not required for the GSM bridge. If anyone is interested in doing that, please leave a comment and I'll write a new post only for that configuration.

Important note: All the configuration explained from now on is identical on both servers.
  1. Install raspbx on your RPi's SD card. Also take a time to read through the raspbx documentation, which is very useful
  2. Use the script install-dongle (built-in inside raspbx) to install your dongle on your RPi. Important notes regarding problems I got:
    1. There's a list of GSM dongles that are tested with voice, SMS and USSD. I bought the Huawei E160;
    2. I had to unlock the voice feature with DC-Unlocker for Windows;
    3. Plug your dongle and only after that plug your RPi power. If you try to plug your dongle while the RPi turned on, you may experience your RPi to reboot due to power consumption failures;
    4. If your RPi starts rebooting it's because it can't handle the gsm dongle power consumption, in this case, use a proper powered USB hub. I bought the D-Link Dub-H7
    5. Find out the IMEI and the IMSI code of your dongle and set the correct values under /etc/asterisk/dongle.conf because /dev/ttyUSB1 or /dev/ttyUSB2 may change;
  3. Before you start configuring raspbx itself, it's important that you have both sides with network configured properly with static IPs and (in my case) DynDNS. I didn't want to open too many ports on the routers on both sides so I just setup an OpenVPN vpn; 
  4. Setup a new custom trunk for your dongle under Connectivity  Trunks  Add Custom Trunk and set these values:

    Trunk Name: to-my-cellphone
    Outbound CallerID: Your SIM card number with country code (with a plus sign at the beginning)
    Custom Dial String: dongle/dongle0/$OUTNUM$

    Note: This is the trunk that will actually place the inbound calls to your personal telephone.

  5. Setup a new IAX2 trunk under Connectivity  Trunks  Add IAX2 Trunk and set these values:

    General Settings:
    Trunk Name: Something that would remember the incoming connection from the other server
    Outbound CallerID: Your personal phone number

    Outgoing Settings:
    Trunk Name: (same as above, I also used the same name and it's ok)

    host=static IP under vpn of the remote server
    username=any username
    secret=any password
    type=friend
    trunk=yes
    qualify=yes
    qualifyfreqok=25000
    tos=0x18

    Note: The IAX2 trunk is responsible for redirecting calls from this server to the other.

  6. Setup a new inbound route under Connectivity  Inbound Routes → Add Incoming Route:

    DID Number: Your SIM card number with country code (with a plus sign at the beginning)
    CallerID Number: Your personal cell phone number
    Set Destinations: Trunk and your recently created IAX2 trunk.

    Note: This is the route responsible to redirect an inbound call on your local dongle over the IAX2 trunk and then to your remote server.
If you noticed any omitted fields in the above configurations, it's because I didn't fill with anything or it's not important. I also did some extra work on Claning the SIM card memory for SMS every once in a while, I set a static IP for the OpenVPN so each server can always see each other, an additional service to check my credit balance every month and so and so forth. If you also want details like that please let me know in the comments :-) 

That's it! You did it! If you need more help setting up anything else please let me know in the comments of this post. Good luck and have fun! :-)