DSM-320 flac to wav transcoding solution!

D-Link specific stuff goes here
murrayfleming
Posts: 30
Joined: Sat Feb 11, 2006 1:44 pm

DSM-320 flac to wav transcoding solution!

Postby murrayfleming » Mon Jan 28, 2008 5:49 pm

I've been trying to get flac to wav transcoding working reliably with my DSM-320 for a couple of weeks. As I've finally cracked it, I thought that I should share my experiences here, as there isn't too much documentation out there (probably because Twonky don't seem to provide support for transcoding any more). Sorry if this post is a bit long, but I've been though a bit of a journey....

Firstly thanks to everybody on the forum who have posted about transcoding, as without this information I wouldn't have got anywhere....

So, as previously discussed elsewhere. I simply extracted the contents of the flac-i386_341_225.zip (I'm on Linux) file from the Addons page on the TwonkyVision site into my cgi-bin directory, and I changed my clients.db so that for the DSM-320 the flac line now reads MT:flac audio/x-flac instead of MT:flac audio/flac. Initially this seemed to work fine, I could call up flac files, and they would be transcoded and play on the DSM, and I could move onto the next track in an album or playlist.

However I soon noticed some issues:
1. My other UPNP client, a Roberts Internet Radio, was now being sent transcoded audio, even though it can play flac natively.
2. Total track times on the DSM were always displayed as 9 minutes 54 seconds.
3. After playing a complete song the DSM always failed to move on automatically to the next song in the album/playlist. The http log just showed it being sent the last 16k chuck of data for the song, again and again. The DSM had to be rebooted to recover. Even when the Roberts was receiving trancoded wav files, it didn't share this problem, so this looks like another DSM-320 issue:!:

I could live with the first two but the last problem would make transcoding unworkable for me. :cry:

The first problem was easily solved by setting up the Roberts as an OXX Alto (a device based on the same chipset) in the Clients config page, and then editing the Clients.db entry for OXX Alto so that it included a MT:flac audio/flac line.

My initial attempt to fix the other problems centered on trying to store track times for my flac files in the TwonkyMedia database, as I had noticed that they were missing. I thought that if Twonky knew the track time then this would be sent to the DSM, and everything would be OK. I ended up using a mock iTunes database file to import track times into the Twonky database, however this didn't improve anything, so I had to have another think. :?

I'd noticed that if I played wav files that had already transcoded, that were still in the cache, then I had no problems. This made me look at the source code for the cgi-flac plugin, to see what was going on....

The way transcoding is setup by default is that the cgi-flac plugin starts decoding the flac file, then waits 2 seconds (2000ms), for some of the wav file to be written to the cache, and then it allows Twonky to start streaming the file down to the client, while the rest of the track is still being decoded. This configuration should allow low power servers, like a NAS, to perform the transcoding on the fly, whilst the track is being played back.

However, It appears that the DSM-320 can't properly deal with a transcoded stream that is still being written to, whereas, other devices such as my Roberts radio can. Thanks D-Link!. :evil:

The way I have solved this problem is by changing the cgi-flac plugin to always wait for the file to be completely transcoded to wav, before allowing Twonky to start sending it to the DSM. I run Twonky on my PC, and the average flac file only takes about 2 seconds to decode, so I'm not noticing any more of a time lag before a song starts than I would when using the original plugin. I also always encode my flac files with maximun compression, as this seems to speed up decoding. My PC is fairly new with an AMD dual core processor, but I wouldn't say that it's top of the range. Therefore I think that this solution could work for a lot or people. Unfortunately if you have a NAS, I'm guessing that the decode times would be unacceptable.

The portion of the cgi-flac.c Linux code I modified was at the end of the file:

Code: Select all

       default:
         sleep(2000);
         cat_file(outfile,1/*TRUE*/);
         waitpid(pid,NULL,0);
         free(outfile);
         return 0;
         break;

Becomes:

Code: Select all

       default:
         wait(NULL); //Wait for child process to end
         cat_file(outfile,0/*FALSE*/); //File not growing
         free(outfile);
         return 0;
         break;


If you are on Windows then the code to change would be from :

Code: Select all

   if (CreateProcess (strPath, strStart, NULL,NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
      Sleep(2000);
      // Do not wait until child process exits.
      cat_file(outfile,TRUE);  // growing
      WaitForSingleObject (pi.hProcess, INFINITE);
      //fprintf(stderr, "wait for child...done\n");
      CloseHandle(pi.hProcess);
      CloseHandle(pi.hThread);
      free(outfile);
      return 0;
   }


to:

Code: Select all

   if (CreateProcess (strPath, strStart, NULL,NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
      //Wait until child process exits.
      WaitForSingleObject (pi.hProcess, INFINITE);
      //fprintf(stderr, "wait for child...done\n");
      CloseHandle(pi.hProcess);
      CloseHandle(pi.hThread);
      cat_file(outfile,FALSE);  //not growing
      free(outfile);
      return 0;
   }


On Windows I'd recommend putting the latest version of flac.exe into the cgi-bin directory, as this seems to be faster than than the one in flac-win32.zip from the TwonkyAddons page.

As the cache limit that can be set in the config pages doesn't seem to apply to transcoded wav files, I've added a simple cron job that will delete any files that haven't been accessed in the last hour. It's just a script file in my /etc/cron.hourly directory, containing the following line:

Code: Select all

find /usr/local/mediaserver/twonkymedia.db/cache/*.wav -amin 60 -delete


Just set the correct location of your cache, and remember to make the file executable.


On Windows you could use a batch file run as a Scheduled Task to tidy up the cache, or possibly a "Disk Cleanup" utility.


My flac files now play as reliably as my mp3s. Success! :D

I have compiled new versions of the cgi-flac plugin for the AMD64 architecture on Linux, and also for Windows, so if anyone wants a copy to try out, then drop me a PM with your email.

Finally thanks to TwonkyVision for everything they have done in providing the transcoding plugin architecture, the cg-flac source code, and the editable clients database, that have made this all possible (holding back a tear here). Without Twonky my DSM-320 would have been sent to the scrapheap long ago...

Axeman77
Posts: 2
Joined: Mon Feb 18, 2008 10:19 pm

DSM-320 flac to wav transcoding solution!

Postby Axeman77 » Mon Feb 18, 2008 10:29 pm

Hey Murray, you da man! :D

Thanks, this solution works nicely with my DSM-520 too (running i386 Linux). Previously, there had been various problems with playback, principally track lengths not being detected and tracks being truncated. Now it works nicely.

Thanks!

Axeman77

murrayfleming
Posts: 30
Joined: Sat Feb 11, 2006 1:44 pm

Try ffmpeg instead of flac

Postby murrayfleming » Tue Feb 19, 2008 1:50 pm

Good to hear that you've also got this working. :D

Once thing I've discovered since my original post is that ffmpeg decodes flac files at least twice as fast than flac itself. To decode a file with ffmpeg your just need to type:

Code: Select all

ffmpeg -i title.flac title.wav


So I've just changed my version of the plugin to read

Code: Select all

       case 0:
         execl ("/usr/bin/ffmpeg", "/usr/bin/ffmpeg", "-i", source, outfile, NULL);
         perror("execl");
         _exit(0); // should never been reached



Just relpace "/usr/bin/ffmpeg" with the path to ffmpeg on your distro. Enjoy!
Last edited by murrayfleming on Mon Feb 25, 2008 11:05 pm, edited 2 times in total.

SumnerBoy
Posts: 9
Joined: Fri Feb 22, 2008 9:03 am

Postby SumnerBoy » Fri Feb 22, 2008 9:07 am

Hi - I have downloaded the trial version of Twonky and have it up and running. I can stream mp3s and video from my server to my XBox360.

I also downloaded the flac plugin and unzipped to the /cgi-bin/ folder. I updated my clients.db file so it now reads MT:flac audio/x-flac for all clients.

However I still can't get flacs to play in XBox360. What else am I missing?

Any help would be greatly appreciated!

murrayfleming
Posts: 30
Joined: Sat Feb 11, 2006 1:44 pm

Re: DSM-320 flac to wav transcoding solution!

Postby murrayfleming » Mon Feb 25, 2008 11:21 pm

Don't know if this is the best place to discuss the XBox :wink:

You could look at this thread where XBox transcoding was discussed.

Or start a new thread in the XBox forum. I'd be happy to pitch in there.

SumnerBoy
Posts: 9
Joined: Fri Feb 22, 2008 9:03 am

Re: DSM-320 flac to wav transcoding solution!

Postby SumnerBoy » Tue Feb 26, 2008 3:24 am

Thanks Murray - sorry for jumping into the wrong thread. I have posted to the thread you mentioned.

Axeman77
Posts: 2
Joined: Mon Feb 18, 2008 10:19 pm

Re: DSM-320 flac to wav transcoding solution!

Postby Axeman77 » Sun Mar 16, 2008 3:53 pm

Hi again Murray,

I've made the switch to ffmpeg too. Thanks for sharing your work on this.

Regards,

Axeman77


Return to “D-Link DSM...”

Who is online

Users browsing this forum: No registered users and 2 guests