ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

How to add custom request headers to ads tag request with ima extension

Open baiqindotfubotv opened this issue 3 years ago • 8 comments

I am using the ima extension for client side ad insertion support:

val mediaItemBuilder = MediaItem.Builder()
    .setUri("www.videocontent.com")
adsTag?.let {
    mediaItemBuilder.setAdsConfiguration(
        MediaItem.AdsConfiguration.Builder(Uri.parse("www.adstag.com")).build()
    )

The network request sent to "www.adstag.com" by the ima extension has the following request headers:

...
user-agent	Mozilla/5.0 (Linux; Android 11; Pixel 2 XL Build/RP1A.201005.004.A1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.5005.78 Mobile Safari/537.36
accept	/
origin	https://imasdk.googleapis.com/
...

We need to replace the request headers in the ads tag request. However I don't any apis to do that. Is this possible through ima extension sdk?

baiqindotfubotv avatar Jun 09 '22 21:06 baiqindotfubotv

That's currently not possible in a declarative way I'm afraid. The MediaItem only has a field for the ad tag URI only and headers can't be declared.

You can use the setMediaSource(adsMediaSource) approach to set an AdsMediaSource instead of a MediaItem. The AdsMediaSource has a constructor that takes a DataSpec to define how to load the ad tag. So if you instantiate the AdsMediaSource yourself, you can build a DataSpec with headers and then pass it as the second argument to the constructor and then call setMediaSource(...) with that source.

I'm not aware of specific headers that need to be added for requesting IMA ad tags, so I'm not convinced we should provide a declarative solution for this. So I think the current solution is what the library offers until we understand the need for setting additional headers better and probably get other requests from other developers.

Can you shed some light on what headers you want to add and why? Probably provide us with a URI of such a tag?

Aside: I'm not sure whether the request headers you posted above are from an ExoPlayer request. The user-agent string doesn't look like coming from a player unless you have customized the user agent string. The accept header looks like not being spec compliant. Also the origin header doesn't look like familiar and I don't think this request comes from ExoPlayer. Can it be that this is a request done by an ad tag server that indirectly request a tag? If that's the case it may be that adding the headers to the DataSpec above may not even result of these header being sent to adstag.com.

marcbaechinger avatar Jun 10 '22 17:06 marcbaechinger

@marcbaechinger I think these requests are made by this sdk to request VAST ad data: com.google.ads.interactivemedia.v3:interactivemedia The ads tag url needs some company domain request headers to serve customized VAST data. For example, user-agent, device-type, device-location etc. These headers are also used for analytics purposes.

baiqindotfubotv avatar Jun 10 '22 18:06 baiqindotfubotv

Thanks for the context - makes sense. How would you add these headers if you'd use the ad SDK directly without the ExoPlayer extension?

marcbaechinger avatar Jun 13 '22 09:06 marcbaechinger

There doesn't seem to be a method doing that in the AdRequest class that ima extension is using. But I never used it directly so I am not sure if I missed anything.

baiqindotfubotv avatar Jun 13 '22 17:06 baiqindotfubotv

Hi, we also have a request from our customer to set a different user-agent for ima request, maybe you can raise the priority for this. Thanks!

sebastiangansca avatar Aug 23 '22 09:08 sebastiangansca

@sebastiangansca I think this isn't possible for IMA in general (and therefore not relevant for this issue), because IMA requests are made by the IMA SDK, not ExoPlayer directly, see https://github.com/google/ExoPlayer/issues/9925#issuecomment-1028995796.

icbaker avatar Aug 23 '22 10:08 icbaker

ok!

sebastiangansca avatar Aug 23 '22 10:08 sebastiangansca

Ah I misread the issue title and didn't realise it was already specific to IMA, sorry for my slightly confusing reply! I thought this was https://github.com/google/ExoPlayer/issues/6166 (more general support for customising the request headers of a MediaItem). I'll hide this comment and the one above as off-topic.

icbaker avatar Aug 23 '22 10:08 icbaker

We also would like to be able to attach headers to vmap and vast tag requests. Our ad server reads the header values to determine which ads to serve in our vmap file. Users in different countries for example will receive a different set of ads.

As suggested by @marcbaechinger, I tried creating my own AdsMediaSourceFactory, which wrapped our content media source in an AdsMediaSource with a DataSpec specifying custom headers, but it doesn't seem to actually append the headers to the request. In the exoplayer-ima source code, I can see ImaUtil.getAdsRequestForAdTagDataSpec setting the ad tag uri to the AdRequest, but the DataSpec headers appear to go unused (please let me know if I'm wrong).

Has there been any movement on this request?

sean-unger avatar May 08 '24 01:05 sean-unger

As far as I know, the IMA SDK doesn't allow to specify headers for the ad tag URL. Please note that it's the SDK that issues the request for the VAST file, not ExoPlayer.

If you let us know the API we can call, we can consider adding headers to the AdsConfiguration. If you want this feature you need to request it with the IMA team I think. We can't really help you regarding this I'm afraid.

See #9925 also. I'm closing this issue as infeasible. If there is such an API please open an issue in the new repository: https://github.com/androidx/media/issues

marcbaechinger avatar May 08 '24 08:05 marcbaechinger