Simple pay-per-minute solution for Wowza
Pay-per-minute is a typical use case that people want to implement on their sites that publish online streams. This tutorial shows you a simple way to achieve this on your site using Wrench.
Setting up Wrench
Please read the previous article about user authentication in Wowza Streaming Engine to understand how to set up Wrench in your website and in Wowza Streaming Engine. This is a prerequisite to pay-per-minute (ppm) and pay-per-view (ppv) as all this information needs to be associated to a particular user, so authentication is a necessary first step.
Configuring Wrench for pay-per-minute
First off, you need to create a table in your database to store the information collected by the module. The following is a simple table that would do for you:
CREATE TABLE wtb_minutes ( `session` varchar(256), `user` varchar(64), `application` varchar(64), `total` int(11) )
This table will be populated by Wrench using the query that you have to specify in the
wrench.ppm.insert.sql application property. For this tutorial, open your Wowza application's
Application.xml file and add the following properties in addition to any you have already set in the previous tutorial.
<Property><Name>wrench.monitor.period</Name><Value>10</Value></Property> <Property> <Name>wrench.ppm.update.sql</Name> <Value>update wtb_minutes set total=:elapsedtime where user=:username and application=:application and session=:session</Value> </Property> <Property> <Name>wrench.ppm.insert.sql</Name> <Value>insert into wtb_minutes (user, application, total, session) values (:username, :application, 0,:session)</Value> </Property>
wrench.monitor.period sets the time interval for running the monitor job. As this job might possibly run as many queries as the number of your clients is, be careful not to set it too small. For pay-per-minute solutions, you typically want 30 or 60 second intervals.
When a user successfully passes user authentication and any optional authorization that you have set up, the first time Wrench monitor job runs, it will execute the
wrench.ppm.update.sql query. If this was the very first time this user is processed in his session, the update has no effect and Wowza Wrench will issue the
wrench.ppm.insert.sql which inserts the first entry for the user into the
wtb_minutes table. Next time the job is executed, the update will be responsible for updating the elapsed time.
session in this context identifies a user and the time he started watching the stream. It makes possible to collect consequtive logs in the same table and keeping each sessions's length recorded. If you want to summarize the total minutes that the user consumed your streams, you can run the following query:
select sum(total) from wtb_minutes where user = 'joe'
Setting up pay-per-view
A simple Wowza pay-per-view setup can be set up with Wrench by creating a table in your database that will store the statistics:
CREATE TABLE `wtb_views` ( `user` varchar(64), `count` int(11) )
Then set the following two properties in your
<Property> <Name>wrench.ppv.update.sql</Name> <Value>update wtb_views set count = count + 1 where user=:username</Value> </Property> <Property> <Name>wrench.ppv.insert.sql</Name> <Value>insert into wtb_views (user, count) values (:username, 1)</Value> </Property>
Whenever Wrench finds a new, yet unlogged client it executes the
wrench.ppv.update.sql query. If the query did not update anything in the table, then most probably this client has never seen any streams, so it executes the
wrench.ppv.insert.sql query. The optional
wrench.ppv.grace.period parameter allows you to set a minimum time that is needed to pass before any client is counted.
Update: there is even more that you can do: the latest version supports dynamic balance checking of your clients.
Update: (starting with version 2015.03.22, you can use webservice based PPM)
Feel free to leave your feedback here about this tutorial.