logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: How to know when a visitor is streaming a video
Author
patrick_here



Joined: 06 Apr 2022
Posts: 3
Location: USA, Seattle

PostPosted: Mon 26 Aug '24 23:56    Post subject: How to know when a visitor is streaming a video Reply with quote

Hello,
I am hosting videos locally on my VPS (using video.js). I'm trying to figure out how I would know (from Apache) when a visitor is streaming a video ...when they start streaming, whether they are watching the entire video or just a few seconds of it ...etc. The Apache log is not helpful for this.

Does anyone know how I might get this information from Apache?
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 343
Location: UK

PostPosted: Wed 28 Aug '24 18:07    Post subject: Reply with quote

Apache logging is pretty flexible, so it should be possible to capture some of the information you're after.

However, you need to appreciate that the client browser will buffer video as it's downloading (unless it's downloading to a file rather than streaming to a user in realtime), so you're never going to get precise duration (start/finish) timings. It's also important to note that standard Apache log entries are written when the content transfer is complete (or the request gets interrupted by the client). So by default you won't get a log entry for when a request starts (but see later on).

Firstly, I'd suggest creating an additional custom log to record requests for your video files, and base this on the logio_module.

In your configuration file, enable (uncomment) the logio module, viz:

Code:
LoadModule logio_module modules/mod_logio.so

Further down the default configuration file, there should be the following section which then defines a combinedio LogFormat to include the response transfer size %O, viz:

Code:
    # Log file transfer size.
    #
    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

Then, further down in your configuration, you can then define a CustomLog file using the combinedio format, and qualify log entries to only take place for video file requests. You'll need to adjust the logic here depending on what video file extensions your site hosts.

Code:
    CustomLog logs/video.log combinedio env=VideoLog

    SetEnvIf Request_URI "\.mp4|\.webm$" VideoLog

With this configuration you should get video.log file entries of the form, where the final entry (%O) is the number of bytes sent to the client, e.g.
    192.168.1.1 - - [28/Aug/2024:16:11:44 +0100] "GET /example/video.mp4 HTTP/1.1" 200 6687499 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0" 1245 6702340
If you want to include the time taken to serve the request, you'll need to refine the LogFormat to include the %{UNIT}T parameter (I recommend milliseconds). Here I've created a new videoio LogFormat.

Code:
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O %{ms}T" videoio
    </IfModule>

    CustomLog logs/video.log videoio env=VideoLog

and this is a sample log entry based on this format, showing the total request time was 46.394 seconds.
    192.168.1.1 - - [28/Aug/2024:16:27:06 +0100] "GET /example/big.mp4 HTTP/1.1" 200 1273510425 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0" 362 526279882 46394
Note the %b parameter (1273510425 in this case) shows the size of the file request, whereas the %O figure (526279882) is the amount actually transferred (nearly half in this case). File big.mp4 was actually a two hour video, so this rather shows the transfer request time doesn't necessarily correspond to how long the user has been watching the video. Moreover, should the user pause or seek to a different position in the video, then I'd expect the browser to make a new request.

Hope this information helps.
Back to top
patrick_here



Joined: 06 Apr 2022
Posts: 3
Location: USA, Seattle

PostPosted: Thu 29 Aug '24 6:14    Post subject: Reply with quote

Yes, that explanation was very helpful. I configured it on two different VPS's. One thing I noticed was that (at least on one of them) the time that the log entry is generated for a particular video seems to be somewhat unpredictable. I would have expected that an initial entry would always be generated when the visitor clicks "Play" but on one of my VPS's this is not the case ...the initial entry may or may not be generated at the time the visitor clicks "Play". Is this expected behavior?
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 343
Location: UK

PostPosted: Thu 29 Aug '24 14:59    Post subject: Reply with quote

Ok, I'm not familiar with the functionality of video.js, but suspect client side requests will be dependent on a measure of browser caching. The best way to check what's going on is to use browser developer tools (Shift+Ctrl+I) and check the network requests.

Indeed, I've quickly done this using the sample code from the Video.js "getting started" page https://videojs.com/getting-started, and looking at the client side requests it becomes clear what's going on.

The code requests a chunk of the video file, 20MB in the case of my 2GB big.mp4 video file, and uses this to present a static image of the video behind the start button. It also means the browser has 20MB worth of video to play when the user presses the start button.

So depending on the size of the video (and the browser cache), you may not see a further transfer when the user presses the start button. Moreover, subsequent transfers will only start once the browser code decides it needs to buffer further video.

I suspect the only way you're going to capture the start button being pressed would by coding in some form of specific (dummy) request at that point. There may be some Video.js option that may help you hook this event - https://videojs.com/guides/options

In any event, it would seem your problem isn't down to Apache functionality.
Back to top
patrick_here



Joined: 06 Apr 2022
Posts: 3
Location: USA, Seattle

PostPosted: Wed 04 Sep '24 0:32    Post subject: Reply with quote

tangent wrote:

In any event, it would seem your problem isn't down to Apache functionality.


Okay, yes, thanks for your replies. After investigating further it's become apparent to me that this problem is a client-side (ie: javascript) problem because in many cases the entire video would be downloaded in full and unless I was watching on the client I wouldn't know whether the user was watching any of it at all. So just on the client I'll need to be doing things like addEventListener("playing"... etc...etc. and passing messages up to the server (ajax probably). Thanks again for the responses.
Back to top


Reply to topic   Topic: How to know when a visitor is streaming a video View previous topic :: View next topic
Post new topic   Forum Index -> Apache