Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transcoding via filter scripts to http streaming #274

Closed
wants to merge 2 commits into from
Closed

Add transcoding via filter scripts to http streaming #274

wants to merge 2 commits into from

Conversation

BtbN
Copy link
Contributor

@BtbN BtbN commented May 12, 2013

This adds a new parameter to the http streaming server.
Adding ?transcoder=test makes it pipe all http output through a "filter", which can do basicaly anything with it.

It searches for the trascoders in hts_settings_get_root()/transcoders
It also passess all GET parameters in hc_req_args as environment variables, prefixed with GET_, so the transcoder can get options via url parameters.

This also modifies the writing to the socket a bit, so it checks if it can write to the http stream, because it can no longer rely on write() errors, because it no longer directly writes to the tcp socket. I use select and poll for this.

There is still a problem somewhere, because in my tests a transcoding vlc throws a lot of warnings about broken input data. Connecting with vlc via http, which should bring the same data to vlc, works perfectly. But this only seems to happen with higher bandwith streams, so i guess it's some buffer overflowing.

But in general, this already works fine.

@BtbN
Copy link
Contributor Author

BtbN commented May 12, 2013

Ok, found why it did not work propperly. VLC uses diffrent caches for file(stdin is seen as file) and network input. And the default 300ms cache-time are not enough.
Here's the transcode script I use for VLC now:

#!/bin/bash

export DISPLAY=:0
exec cvlc --file-caching 1500 "--sout=#transcode{venc=va,threads=4,deinterlace,height=720,vb=-33}:std{access=file,mux=ts,dst=-}" - 2>/tmp/vlc.log

A no-op passthrough-script could look like this:

#!/bin/bash
exec cat <&0

It's working flawlessly for me now!

@pvagner
Copy link

pvagner commented May 12, 2013

Is there a way to somehow be able to signal a proper mime type to the http server? For example I would like to encode audio to aac or vorbis using ffmpeg. Since I am dealing with raw audio proper mime type would be usefull so I can for example play the audio on android phone, symbian phone and even on a windows pc with winamp or foobar 2000.
I am sorry this might sound strange but yes I am only interested in audio and am discarding video by using audioes muxer what is available as a pull request 263.

@BtbN
Copy link
Contributor Author

BtbN commented May 12, 2013

No, there is no easy way to influence what headers are sent.
When the transcoder is initialized, the headers have already been sent. Running it earlier is also not possible, because the pipes it needs aren't available.

@pvagner
Copy link

pvagner commented Jul 13, 2013

This can no longer be applied without editing. There are no functional differences just merge conflicts: http://pastie.org/8137891
Maybe git can do that automagically however I am using this as a patch.

@BtbN
Copy link
Contributor Author

BtbN commented Jul 14, 2013

This already is up-to-date with current master and applies cleanly.

@fritz-fritz
Copy link

This seems like a way to get HLS (HTTP LIVE STREAMING) working! if the "filter" can have vlc transcode the stream then we could use vlc's ability to output a live stream that could then be used for native playback on mobile devices (aka iPhone) without the need for VLC to be installed!

I could be wrong... but definitely worth looking into!

@BtbN
Copy link
Contributor Author

BtbN commented Jan 16, 2014

HLS isn't a simple stream like normal http/rtmp Streaming.
It's a playlist which is continously reloaded, and the stream is split into multiple segments.
This patch only supports single continous streams.

@fritz-fritz
Copy link

I understand how HLS works. Though it does appear you have the ability to take the live tv stream and feed it into a bash command. VLC has command line arguments to handle live transcoding to HLS.

This is an example of code i've used in the past:

vlc atsc://frequency=677029000 --int=dummy --sout-transcode-audio-sync :live-cache=3000 --sout='#transcode{vcodec=h264,vb=150,fps=25,width=480,scale=1,venc=x264{aud,profile=baseline,level=30,keyint=15,bframes=0,ref=1},acodec=aac,ab=40,channels=2,samplerate=22050}:duplicate{dst=std{mux=ts,dst=-,access=livehttp{seglen=10,delsegs=true,numsegs=10,index=/var/www/stream/live.m3u8,index-url=live-######.ts},mux=ts{use-key-frames},dst=/var/www/stream/live-######.ts,ratecontrol=true}}'

So it seems to me that the HLS stream could at least be generated but as far as impacting how its accessed or how it impacts hts... that I don't know

@BtbN
Copy link
Contributor Author

BtbN commented Jan 16, 2014

I think this would be easier to achive by setting up nginx-rtmp with a hls setup, which is configured to pull from tvh.

if(fgets(mime, *mime_size, fp) == NULL)
*mime_size = 0;
fclose(fp);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some logging would perhaps be appropriate here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mime file is completely optional and it not existing or beeing empty is not an error or even worth a warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.

@Jalle19
Copy link
Contributor

Jalle19 commented Mar 25, 2014

@BtbN maybe you could drop some of the cosmetic changes (mainly the "opening brace on next line" changes) and move those that fix bad indentation to a separate commit?

@nhsman
Copy link

nhsman commented Apr 13, 2014

Can you let me know the where to place transcoding script? I'm getting this in TVH log:

2014-04-13 14:03:42.585 webui: Transcoding stream with transcoder /home/vlc/.hts/tvheadend/transcoders/test
2014-04-13 14:03:42.585 webui: Transcode-Script breakout attempt detected

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 13, 2014

@nhsman you'll have to use "test" as the transcoder script, not the full path.

@nhsman
Copy link

nhsman commented Apr 13, 2014

Thanks. But where to place actual script? I have it in "/home/vlc/.hts/tvheadend/transcoders/" and
when I use just "test" I get this:

2014-04-13 16:15:40.497 webui: Transcoding stream with transcoder test
2014-04-13 16:15:40.497 webui: Transcode-Script "No settings dir/transcoders/test" does not exist or is not executable!

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 13, 2014

Have you made sure the script is executable?

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 13, 2014

And does it really say "dir/transcoders/test"?

@nhsman
Copy link

nhsman commented Apr 13, 2014

Yes it says "dir/transcoders/test". And yes, the script is executable. Any ideas?

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 13, 2014

Apparently it determines your configuration directory to be "No settings dir", which is why the script fails. How are you running tvheadend and under what user does it run (your posts would indicate the user is "vlc", is that correct)?

@nhsman
Copy link

nhsman commented Apr 13, 2014

I compiled TVH from source and run it as "sudo tvheadend -C -f".

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 13, 2014

@nhsman don't run it as root.

@nhsman
Copy link

nhsman commented Apr 13, 2014

If I start tvheadend as non root,
tvheadend -C -f
I cannot see TV Adapters in webif.
My user is member of video group.

@clandmeter
Copy link
Contributor

Mine runs as -u tvheadend and tvheadend is member of the video group.
Make sure your devices have correct ownership set by your device manager.

And remember, if you run tvh as root "ones", to chown -R your config directory (if init doesn't take care of that).

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 14, 2014

@BtbN you should probably update your code to check for NULL after this change: af8e79c , currently http_stream_transcode fails with an incorrect error if no settings directory is present.

@BtbN
Copy link
Contributor Author

BtbN commented Apr 14, 2014

Indeed, i'm not sure how to react in that case. Just abort and assume no script was present?

@nhsman
Copy link

nhsman commented Apr 14, 2014

Thanks.
I've been able to start TVH like this: "tvheadend -C" and now I can see adapters, but when I try to start it as daemon I got this:

tvserver@SERVERTV:~$ tvheadend -C -f
2014-04-14 22:30:28.681 [ ALERT] START: setgid() failed, do you have permission?

Any ideas?

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 14, 2014

@BtbN: abort with something like "No configuration directory in use, unable to load transcoder scripts"

@Jalle19
Copy link
Contributor

Jalle19 commented Apr 14, 2014

@nhsman something is wrong with your Linux or your tvheadend installation.

@nhsman
Copy link

nhsman commented Apr 15, 2014

I use Ubuntu 13.04 x64 minimal. I had to upgrade kernel to 3.13 to get my tv adapter works. Then compiled TVH from here: https://github.com/BtbN/tvheadend using "simple_transcode" branch.
As I said previously, after adjusting permissions I can start TVH with "tvheadend -C" and then transcoding works fine but for some reason I cannot start is as a daemon.
Do you think it has something to do with kernel upgrade?

@nhsman
Copy link

nhsman commented Sep 6, 2014

Hi, I've compiled latest master with this PR and it seems to not work.
When I start stream, eg.
http://192.168.1.70:9981/play/stream/channel/41cb8d1f899b642902a77e4525c4c26e?transcoder=test
it opens stream but according to Tvheadend log it is not even looking for script.
The channel is played as original stream.
Can you please advise. Thanks.

Sorry guys, it's my bad. It should be:
http://192.168.1.70:9981/stream/channel/41cb8d1f899b642902a77e4525c4c26e?transcoder=test
It works fine then.
Thanks.

@Hedda
Copy link

Hedda commented Nov 3, 2015

You might want to consider borrowing/stealing the UPnP / DNLA client profiles XML files from the latest Plex Media Server and use as a base to create custom MIME types in filter profile per reciever client for transcoding and/or remuxing , see:

https://github.com/Hedda/Plex-DNLA-client-profiles/tree/master

https://github.com/Hedda/Plex-DNLA-client-profiles/archive/master.zip

PS Hope it is OK to have copies of these XML files on GitHub as Plex.tv don't have an source code repository available because rest of the software is closed source.

@perexg
Copy link
Contributor

perexg commented Aug 22, 2017

This functionality was implemented like MPEG-TS Spawn streaming profile where users can configure the command which should be invoked for the original MPEG-TS stream. v4.3-344-gc349525b2, more commits, the main is 5bbddc9 .

@perexg perexg closed this Aug 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants