Phonon

App Developer Week -- Phonon: Multimedia in Qt -- apachelogger -- Fri, Apr 15th, 2011

   1 [18:02] <dpm> You'll know him not only for his good looks and legendary coding skills, but also for having rocked this AppDeveloper Week: there he is - KDE/Kubuntu developer apachelogger talking about how to write a multimedia app in virtually no time!
   2 [18:03] <dpm> apachelogger, let's roll!
   3 [18:03] <apachelogger> :D
   4 [18:03] <apachelogger> thanks dpm
   5 [18:03] <apachelogger> bonjour, ahoy and welcome everyone!
   6 [18:04] <apachelogger> ...for the third time this week ;)
   7 [18:04] <apachelogger> Today's special on the menu: Phonon, the most awesome terrific super cool magical unicorn multimedia abstraction library ever!
   8 [18:05] <apachelogger> First let's get dependencies sorted out as we will have a bit of coding later on.
   9 [18:05] <apachelogger> If you like GStreamer you might want to use this command:
  10 [18:05] <apachelogger> sudo apt-get install phonon-backend-gstreamer libphonon-dev qt4-qmake libqt4-dev build-essential
  11 [18:05] <apachelogger> If you like VLC you might however want this one:
  12 [18:05] <apachelogger> sudo apt-get install phonon-backend-vlc libphonon-dev qt4-qmake libqt4-dev build-essential
  13 [18:06] <apachelogger> If you have a very old version of Ubuntu OR like xine:
  14 [18:06] <apachelogger> sudo apt-get install phonon-backend-xine libphonon-dev qt4-qmake libqt4-dev build-essential
  15 [18:06] <apachelogger> At any rate a simple text editor will suffice for the editing part ... it is all nicely prepared for copy and paste :D
  16 [18:07] <apachelogger> Should there be any questions, please just drop your QUESTION: in #ubuntu-classroom-chat at any point.
  17 [18:07] <apachelogger> But enough of that nonesense.
  18 [18:07] <apachelogger> Let us talk about Phonon \o/
  19 [18:07] <apachelogger> Phonon is a multimedia abstraction library powered by pluggable backend components, adapting the Phonon API to specific multimedia frameworks/libraries.
  20 [18:08] <apachelogger> ^ I actually made that description up just 5 minutes ago, though it captures Phonon pretty well, plus it got buzz word factor ;)
  21 [18:09] <apachelogger> Primarily Phonon is an abstraction layer between multimedia consuming applications (such as music players, games or sound notification systems) and a specific multimedia library (GStreamer, VLC, Xine, Helix, QuickTime, DirectShow, Microsoft Media Foundation, etc.)
  22 [18:10] <apachelogger> Possibly the fact that I listed 7 of such libraries and yet could not resist appending an 'etc' might give you an idea of why exactly abstraction makes sense.
  23 [18:11] <apachelogger> Who seriously would want to maintain good multimedia experience on >3 operating systems?
  24 [18:11] <apachelogger> Well, the Phonon team, obviously ;)
  25 [18:12] <apachelogger> Especially in a Qt context you really do not want to make your application depend on one of those 300^31000 libraries for multimedia, or  implement your own abstraction system just to become portable to all platforms (read: operating systems) that you might have time to implement support for. Instead we could just do it once ... in Phonon.
  26 [18:13] <apachelogger> Additionally. If one of these libraries decides to change their API, why would every Qt application developer need to spend their Monday afternoon changing their code to suite the new API?
  27 [18:13] <apachelogger> If we just as well could do it once ... in Phonon.
  28 [18:14] <apachelogger> I suppose highlighting all the advantages and disadvantages of abstraction is a bit of apointless exercise, so I better stop now.
  29 [18:14] <apachelogger> Actually.
  30 [18:14] <apachelogger> Do not let me blind you... Phonon is not the solution to every problem in computer science. It is abstraction after all.
  31 [18:15] <apachelogger> If you have a look at the Wikipedia artcile on abstraction you will probably read something like "using a common denominator".
  32 === tubadaz_ is now known as tubadaz
  33 [18:15] <apachelogger> This implies being more limited. As always ... the general is less specific than the specific ;)
  34 [18:16] <apachelogger> Due to the given constraints of abstraction Phonon does not try "to do it all".
  35 [18:16] <apachelogger> It does not strive to support every possible use case that might or might not exist for a multimedia library.
  36 [18:17] <apachelogger> Instead we are focusing on the (actually very small) number of key features that are of use to the greatest possible amount of application developers.
  37 === marcel___ is now known as MAWSpitau
  38 [18:17] <apachelogger> Phonon tries to provide the common set of features you will require in your off the shelf media-enabled applications.
  39 [18:18] <apachelogger> This ranes from simple (non 3 dimensional) games to video and music players.
  40 [18:18] <apachelogger> In particular, if you have a look at the Phonon API
  41 [18:18] <apachelogger>  (i) http://doc.qt.nokia.com/latest/phonon-overview.html
  42 [18:18] <apachelogger> you will hopefully notice that the core set of features of the key classes are actually able to cover jsut about everything most multimedia applications will ever want to do.
  43 [18:19] <apachelogger> This of course primarily is playback.
  44 [18:19] <apachelogger> More precisely: simple playback in the manner of just needing to throw a URL at Phonon and it will try to do something meaningful.
  45 [18:20] <apachelogger> More advanced features are usually bound to the availability of support in the underlying libraries and thus more dynamic (they might be there, or not, or partially etc.).
  46 [18:20] <apachelogger> That way it is possible to implement a common denominator and at the same time stay powerful enough to enable terrific media-centric applications such as good looking video players.
  47 [18:21] <apachelogger> ....talking about video player...
  48 [18:21] <apachelogger> I think it is about time to start doing some coding, because I am getting bored writing all that stuff and would like to copy and paste a bit more ;)
  49 [18:22] <apachelogger> At FOSDEM I boldly claimed that I could do a video player with 3 lines of Phonon specific code in C++. Whether that is any useful is a question that shall remain unanswered.
  50 [18:23] <apachelogger> It is however perfect to grasp the basic concept, even if I do not know any application that would only need 3 lines as you usually want to do more engaging things anyway. But oh well... :)
  51 [18:23] <apachelogger> You can get a template for this from
  52 [18:23] <apachelogger>  (i) http://people.ubuntu.com/~apachelogger/uadw/04.11/phonon-template.tar.gz
  53 [18:23] <apachelogger> Simple extract it somewhere.
  54 [18:24] <apachelogger> Inside you can find a main.cpp, a qmake build file and a video and audio file (or so I hope).
  55 [18:25] <apachelogger> The code I will present goes into the main.cpp in the main function in between the 2 statements that are already there. Includes are also there, so if all goes well you should be able to build this stuff right away.
  56 [18:25] <apachelogger> Talking about building, you can do that by running
  57 [18:25] <apachelogger> qmake .
  58 [18:25] <apachelogger> and
  59 [18:25] <apachelogger> make
  60 [18:25] <apachelogger> and then
  61 [18:25] <apachelogger> ./demo
  62 [18:25] <apachelogger> to run the application.
  63 [18:25] <apachelogger> Right then.
  64 [18:25] <apachelogger> Let us start with a simple audio player.
  65 [18:26] <apachelogger> The first thing you always need when working with phonon is a media object.
  66 [18:26] <apachelogger> With Phonon you build a thing we call the media graph, which at its root has the media object.
  67 [18:27] <apachelogger> To the media object you can attach various outputs (AudioOutput, VideoWidget ...) and even 'inject' effects between the media object and the outputs.
  68 [18:27] <apachelogger> Very easy to understand I shall hope.
  69 [18:28] <apachelogger> The media object is like the heart of every media graph. It controls what file you are currently playing, if you are playing at all, at what time the media is ....
  70 [18:28] <apachelogger> And the best thing about the media object is, that it is insanely easy to create ;)
  71 [18:28] <apachelogger>  (i) Code samples is always indented with 4 spaces
  72 [18:28] <apachelogger>     MediaObject mo;
  73 [18:29] <apachelogger> That did not hurt at all, right?
  74 [18:29] <apachelogger> Now let us also add an AudioOutput while we are at it...
  75 [18:29] <apachelogger>     AudioOutput ao;
  76 [18:30] <apachelogger> You can have many of these media objects, each doing a different thing. For example if you are creating a game, you might want to have a media object (thus graph) for background music and another media object for event sounds (like when a pony rides through the screen).
  77 [18:31] <apachelogger> Due to this we will need to tell our media object that it should be connected to our audio output. Otherwise there will be probably no sound... wouldn't be much of an audio player then ;)
  78 [18:31] <apachelogger> Doing this is very easy, we just call Phonon::createPath(a,b)
  79 [18:31] <apachelogger> like this:
  80 [18:32] <apachelogger>     createPath(&mo, &ao);
  81 [18:32] <apachelogger> Now that we have a media object and an audio output and have told the media object to spit out audio via the audio output, we only need to set a source and ask our nice media object to start playing the source.
  82 [18:33] <apachelogger> As the template already includes some music (in particular Amarok's first run jingle in case anyone is interested) you can use the following:
  83 [18:33] <apachelogger>     mo.setCurrentSource(MediaSource("./first_run_jingle.ogg"));
  84 [18:33] <apachelogger>     mo.play();
  85 [18:34] <apachelogger> Putting the pieces together you should have something like this in your main.cpp:
  86 [18:34] <apachelogger>     MediaObject mo;
  87 [18:34] <apachelogger>     AudioOutput ao;
  88 [18:34] <apachelogger>     createPath(&mo, &ao);
  89 [18:34] <apachelogger>     mo.setCurrentSource(MediaSource("./first_run_jingle.ogg"));
  90 [18:34] <apachelogger>     mo.play();
  91 [18:34] <apachelogger> You can compile it using the commands mentioned earlier
  92 [18:34] <apachelogger> qmake . && make
  93 [18:34] <apachelogger> If all goes well you shoudl be able to run ./demo and hear music. I at least do \o/
  94 === tubadaz is now known as tubadaz_away
  95 [18:36] <apachelogger> Now let us dive into video playback. But I must warn you, it is going to be just as difficult as audio! :O
  96 [18:36] <apachelogger> First we create a video widget (which is about the video version of audiooutput), then attach our media object to it.
  97 === tubadaz_away is now known as tubadaz
  98 [18:37] <apachelogger>     VideoWidget vw;
  99 [18:37] <apachelogger>     createPath(&mo, &vw);
 100 [18:37] <apachelogger> Should you have worked with Qt before you will probably expect that VideoWidget is a QWidget and needs to be shown explicitly... and you are right.
 101 [18:38] <apachelogger> Simply add
 102 [18:38] <apachelogger>     vw.show();
 103 [18:38] <apachelogger> So that is 3 more lines to get video.
 104 [18:39] <apachelogger> Of course the amarok jingle is only audio, so that source is not going to be any good. I just get a black video window :(
 105 [18:39] <apachelogger> But fear not, your well prepared instructor has thought of this problem and also bundled a video into the template :P
 106 [18:39] <apachelogger> Simply exchange the previous line where we setCurrentSource() with this one:
 107 [18:39] <apachelogger>     mo.setCurrentSource(MediaSource("./konqi_ad1.ogg"));
 108 [18:40] <apachelogger> Again run
 109 [18:40] <apachelogger> make
 110 [18:40] <apachelogger> and ./demo should now show a video window with a rather old advertisment for the KDE Desktop \o/
 111 [18:41] <apachelogger> But what is this? We are far away from 3 lines of code! Oh noes!
 112 [18:41] <apachelogger> Maybe I was too daring, claiming such a silly thing :/
 113 [18:41] <apachelogger> Or not... :P
 114 [18:42] <apachelogger> Please remove all that plunder we carefully pasted into the main.cpp and let us start from scratch.
 115 [18:42] <apachelogger> Let us think about this again.
 116 [18:42] <apachelogger> We want a video player, we want it to play a video, and we want it to show a video.....
 117 [18:42] <apachelogger> maybe we should do it just like that.
 118 [18:42] <apachelogger>     VideoPlayer vp;
 119 [18:42] <apachelogger>     vp.play(MediaSource("./konqi_ad1.ogg"));
 120 [18:42] <apachelogger>     vp.show();
 121 [18:43] <apachelogger> Voila! A video player (with audio) in 3 lines of code :P
 122 [18:44] <apachelogger> aha!
 123 [18:44] <apachelogger> akshatj_ just complained that I cheated by hardcoding the URL.
 124 [18:45] <apachelogger> I suppose it depends on the POV, I could add Qt magic to get a file open dialog or something and it still would be only 3 lines of phonon specific code ;)
 125 [18:45] <apachelogger> Which was my original scope for the mission.
 126 [18:46] <apachelogger> Well.
 127 [18:46] <apachelogger> Qt to the resuce.
 128 [18:47] <apachelogger> Let us be user friendly here.
 129 [18:47] <apachelogger> Exchange our previous hardcoded media source with
 130 [18:47] <apachelogger>     MediaSource(QFileDialog::getOpenFileName())
 131 [18:47] <apachelogger> So that our vp.play warps into:
 132 [18:47] <apachelogger>     vp.play(MediaSource(QFileDialog::getOpenFileName()));
 133 [18:48] <apachelogger> There. Still 3 lines and even with a file selector dialog at start up :)
 134 [18:48]  * apachelogger hopes akshatj_ is happy now :)
 135 [18:49] <apachelogger> Just to go one step further. In the future you will also be able to create a video player without any code *at all*. In about 30 seconds.
 136 [18:49] <apachelogger>  (i) http://www.youtube.com/watch?v=HrMSIrhFYlY
 137 [18:50] <apachelogger> I hope I was able to interest you in Phonon and would be super happy to see your amazing new applications become even more amazing through superb multimedia :)
 138 [18:50] <apachelogger> More information on Phonon is avilable in the Qt documentation
 139 [18:51] <apachelogger>  (i) http://doc.qt.nokia.com/latest/phonon-overview.html
 140 [18:51] <apachelogger> As well as an overview of the in Qt included classes:
 141 [18:51] <apachelogger>  (i) http://doc.qt.nokia.com/latest/phonon-module.html
 142 [18:51] <ClassBot> There are 10 minutes remaining in the current session.
 143 [18:52] <apachelogger> ClassBot: Thank you, you are the sweetest :)
 144 [18:52] <apachelogger> If there should be any questions, you now have <10 minutes to get them answered....
 145 [18:53] <apachelogger> Meanwhile I can also tell you a bit about future developments in Phonon. One of the amazing new things we will see appearing in (stable) Phonon hopefully before 2012 is audio and video capturing.
 146 [18:54] <apachelogger> I have been playing around with this a couple of months ago and it is really easy to use and enables the creation of real fun applications I believe.
 147 [18:55] <apachelogger> Also, so Google and KDE will, we will get superb QML support and simple encoding capabilities.
 148 [18:56] <ClassBot> There are 5 minutes remaining in the current session.
 149 [18:56] <apachelogger> It would also appear that there are rumors that a new version of the VLC backend component is just around the corner (but don't tell anyone ;))
 150 [18:57]  * apachelogger is out of things ot tell and his fingers hurt
 151 [18:57] <apachelogger> I think we can wrap this up.
 152 [18:57] <apachelogger> Thanks for reading and have a nice day :)
 153 [18:57] <apachelogger> o/
 154 

MeetingLogs/appdevweek1104/Phonon (last edited 2011-04-18 12:50:00 by dpm)