Thursday, January 08, 2009

gecko-mediaplayer: Replacing XPCom with NPRuntime

Thought I would give a little post-op on the XPCom to NPRuntime conversion. One of the biggest issues with XPCom was the need for .idl files and the code that went along with them. How an .idl file worked was that you declared a class with a unique id and then the methods and properties of that class. Then you took that .idl file and ran it thru the xpidl compiler and this gave and .xpt and a .h file as the output. The .xpt was used by firefox,mozilla, et al to know what methods the class/plugin provided. The .h file was used by the code to create then entry points for the methods. Anyway, getting all the working on many different distrobutions was a pain. Many of the files needed for this step ended up in different places and usually the .pc files (the pkg-config files that say where everything is) were wrong.

So with libxul 1.9.1 coming out shortly and it changing things with XPCom again I decided to look at npruntime since that is where things need to go anyway. Before doing this I did make sure that I could get the plugin working in XPCom mode with 1.9.1 and I was able to, with some minimal fuss. But still decided to take a look into the conversion since I was sick of the whole idl thing.

I planned that the conversion to npruntime would take me about a month on an off and just getting it work, based on the issues I had to get xpcom working initially (this is the mplayerplug-in 2.0 code, version prior to 2.0 had no javascript support).

I started looking at the npruntime sample that I had I started cutting out code from gecko-mediaplayer first. Took about 2-3 hrs and I had the code running and linking against npruntime but without javascript code. I took, npn_gate,npp_gate and np_entry from the sample to get going. At first I didn't think I should touch those files, but after looking at them and reading the comments in them, it looked like I should edit them as needed. Once I got past this step I was actually able to get the plugin to do something other than stare at me. So I got it doing the basics and then I thought I should get javascript working.

So again I looked at the sample and noticed that the classes in the file, kinda resembled the stuff from XPCom and so I decided to get two methods working, play and pause. I had to declare an id and then define that id to be the same as the methods that I did in the .idl file. I then had to edit HasMethod to say, yeah I could handle that id. After I got HasMethod working I noticed that the code was dropping into Invoke and found that with a couple of lines of code I could take the same call that I used in the XPCom callbacks and apply it here. Once I got that working I started doing the bookwork and created all the methods and the callbacks. The same was pretty much true for the properties as well. And in about 8 hrs of work it was pretty much working.

The conversion turned out to be much less work than I expected it to be and I think the code is actually easier to understand now. Before I really dug into this, I looked at it and just was really dreading the conversion as I expected it to be a huge learning curve, but actually I was able to reuse a lot of what I already knew.

No comments: