# Is there a way to call the Youtube app from HME?



## Allanon (Nov 2, 2005)

My Mom has a Tivo Premier Q from our cable company and she has to use the the old Tivo menu because the box is on a 480i TV. Youtube has been removed from the apps menu and VOD goes directly to Suddenlinks's VOD and doesn't put a list of other VOD choices such as Netflix, Hulu, or Youtube. I can get to Netflix and Hulu using the 3000 and 3001 channels but can't get to Youtube. The other Tivo box is hooked to a 1080p TV and using the newer Tivo menu and Youtube is easily accessed. So is there a way to access the Youtube app with HME?


----------



## davidblackledge (Sep 9, 2008)

Allanon said:


> My Mom has a Tivo Premier Q from our cable company and she has to use the the old Tivo menu because the box is on a 480i TV. Youtube has been removed from the apps menu and VOD goes directly to Suddenlinks's VOD and doesn't put a list of other VOD choices such as Netflix, Hulu, or Youtube. I can get to Netflix and Hulu using the 3000 and 3001 channels but can't get to Youtube. The other Tivo box is hooked to a 1080p TV and using the newer Tivo menu and Youtube is easily accessed. So is there a way to access the Youtube app with HME?


I don't understand so much of that (3000 channel? Q?) - I don't keep up with all the threads. I'll answer what I think is relevant.

TiVo removed their HME version of YouTube the middle of 2015 when the YouTube API changed.

That said, the Flash version of YouTube and other things could be launched by attempting to transition to certain apps from HME. We have no idea how that worked, but it did work (EnterWebz displaying the TiVoConnect of TiVo's servers would attempt to launch any apps listed, and certain ones that were listed as HME apps would just launch the Flash app, while trying to launch the flash app directly would fail, as I recall). I don't know if it would launch HTML5 apps that way.

If your device, when using EWz, sees a YouTube listing on the "TiVo's Hidden Apps" page, try to launch it and see what happens. If it works, you're in business and we can continue. 
If a YouTube app isn't listed there is still hope, but we'd have to do some work to figure out the correct YouTube launch stuff, and even then it might not work.

This is all dependent on whatever magic TiVo has hooked up to transition to an HME app that actually launches something else. They could always remove that linkage. Either it's possible to do this launching directly from HME with a command format we don't know, or TiVo has an internal list of "if somebody launches this, launch that instead" for those items.

Finally, the iPad RPC interface should be able to launch it for sure, but would require a same-network installed app (like KMTTG - I think that even has the ability to launch it directly built in to the Remote Control tab) - that's not HME, but certainly it's something.


----------



## Allanon (Nov 2, 2005)

davidblackledge said:


> I don't understand so much of that (3000 channel? Q?) - I don't keep up with all the threads. I'll answer what I think is relevant.


My cable company uses a custom Tivo Premiere Q as their set top box. It allows me to turn to channel 3000 or 3001 to bring up the normal Tivo Netflix or Hulu app. Also it doesn't have Amazon Video but has an app for the cable company's VOD service.



> TiVo removed their HME version of YouTube the middle of 2015 when the YouTube API changed.


Didn't know that, I've been using the Tivo HD menu and Youtube always just seemed to be their.



> If your device, when using EWz, sees a YouTube listing on the "TiVo's Hidden Apps" page, try to launch it and see what happens. If it works, you're in business and we can continue.


Didn't see it in the list.



> Finally, the iPad RPC interface should be able to launch it for sure, but would require a same-network installed app (like KMTTG - I think that even has the ability to launch it directly built in to the Remote Control tab) - that's not HME, but certainly it's something.


I tried the remote in KMTTG and it did bring up the Youtube app, I think I can write an HME program that when activated does whatever the KMTTG remote control is doing.

Thanks for all the information.


----------



## davidblackledge (Sep 9, 2008)

Allanon said:


> I tried the remote in KMTTG and it did bring up the Youtube app, I think I can write an HME program that when activated does whatever the KMTTG remote control is doing.
> 
> Thanks for all the information.


You're welcome, but now you have a new issue. KMTTG uses its "own copy" of the iPad certificate to do RPC communication to do most everything it does including launching apps.

I keep saying I want to add-on to moyekj's KMTTG with an external interface to launch arbitrary RPC through KMTTG, but never have done it. Either we need that (and you're dependent on a KMTTG install WITH the change), or you have to copy most of KMTTG including the certificate just to do this one thing (which is the route I was probably going with the alternate TiVo interface I was considering making - basically you need the "Remote" class, but most of it is tied into KMTTG pretty tightly).

I keep wanting to reorganize moyekj's code for him, but there is just so very very much to do, and I have little time or motivation. I started doing it for his UI code in a branch, but that was a similar issue and I haven't touched it in a while since I realized part of my reorganization was short-sighted, then life happened.


----------



## Allanon (Nov 2, 2005)

Just an update, I was able to isolate the url KMTTG sends to launch Youtube. So now I just have to have the KMTTG web server running and open that url from within my HME app and it should open the Youtube app on my Tivo.

Another cool thing I found is that Amazon and Vudu apps weren't actually removed from the Tivo, they just didn't have links to them so I can now do the same for those apps.

EDIT:
I wrote a HME for Python app and it works, here is the code:

```
import hme
import urllib2

TITLE = 'Youtube'

class Youtube(hme.Application):
    def handle_device_info(self, info):
        urllib2.urlopen('http://192.168.1.122:8181/rpc?operation=Navigate&tivo={}&json=%7B%22uri%22%3A%22x-tivo%3Aweb%3Ahttps%3A%2F%2Fwww.youtube.com%2Ftv%22%7D'.format(info['host'].replace(' ','%20')))
```


----------



## Allanon (Nov 2, 2005)

davidblackledge said:


> I keep saying I want to add-on to moyekj's KMTTG with an external interface to launch arbitrary RPC through KMTTG, but never have done it. Either we need that (and you're dependent on a KMTTG install WITH the change), or you have to copy most of KMTTG including the certificate just to do this one thing (which is the route I was probably going with the alternate TiVo interface I was considering making - basically you need the "Remote" class, but most of it is tied into KMTTG pretty tightly).


I found at least for the Windows OS you can install and run the KMTTG Web Server as a service which runs in the background without the need for KMTTG being open. There are batch files locates in the KMTTG/service/win32 folder that will install the service. You can then just send URL commands to the KMTTG web server to execute the RPC requests.


----------



## Allanon (Nov 2, 2005)

Does anyone have the specs on what it takes to cast a YouTube video to the Tivo? I just noticed that the Google Chrome browser can cast YouTube videos to my Tivo, which opens the YouTube app on the Tivo. If this doesn't require an RPC request then I might prefer that over my current solution since I'm wanting to move the HME app and KMTTG over to my Raspberry PI computer which is running linux but I can't get KMTTG to work on it.


----------



## davidblackledge (Sep 9, 2008)

Allanon said:


> Does anyone have the specs on what it takes to cast a YouTube video to the Tivo? I just noticed that the Google Chrome browser can cast YouTube videos to my Tivo, which opens the YouTube app on the Tivo. If this doesn't require an RPC request then I might prefer that over my current solution since I'm wanting to move the HME app and KMTTG over to my Raspberry PI computer which is running linux but I can't get KMTTG to work on it.


I'm pretty certain that's DLNA that chrome uses to ask the TiVo if it can play youtube and to send it a video to play.

I just took a quick survey of DLNA casting... this is not a simple technology to get into. You're doing Java, right? So there's the "Cling" library that implements DLNA, but having a library is only half the battle. This is more complicated than zeroconf (for good reason) and I haven't seen any simplification libraries. I saw some sample code using a .NET library and it's just as bad. DLNA covers serving and controlling a wide variety of media in a wide variety of ways. It presumably would allow you to do something like automatically discover a device and what remote control buttons you would need to display for it on an app. That's a lot of genericity which requires some really complex code.

Ideally somebody would have a simplified library that's "ask for a list of devices that can accept <youtube> videos" and then "send <youtube video x> to <device y>" - but I don't see it.
If you go this route... maybe you can release the above ;]


----------



## Allanon (Nov 2, 2005)

Thanks for the info, I'm using Python and I'm experienced with programming DLNA/UPNP devices. I'll keep you updated with my progress.


----------



## gonzotek (Sep 24, 2004)

I'm pretty sure it is the DIAL protocol being used to launch YouTube from the Chrome browser and/or phone apps:
DIAL


----------



## Allanon (Nov 2, 2005)

gonzotek said:


> I'm pretty sure it is the DIAL protocol being used to launch YouTube from the Chrome browser and/or phone apps:
> DIAL


It is the DIAL protocol and you can do a m-search using these parameters to find the Tivos:

```
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: "ssdp:discover"
MX: 2
ST: urn:dial-multiscreen-org:service:dial:1
```
They will respond with a location to the dd.xml file. Then just request that file and you will get the application url, if you try to open the dd.xml file from within a web browser the application url will be missing. Once you have the application url then you can send a message that opens the YouTube app. I've tested all parts of this but I'm still piecing together a HME program to do it.


----------



## Allanon (Nov 2, 2005)

More information, when you do the m-search a packet for each Tivo will be sent that contains the port you can get the dd.xml file from, here is that packet:

```
HTTP/1.1 200 OK
LOCATION: http://192.168.1.104:39157/dd.xml
CACHE-CONTROL: max-age=1800
EXT:
BOOTID.UPNP.ORG: 1
SERVER: Linux/2.6 UPnP/1.0 quick_ssdp/1.0
ST: urn:dial-multiscreen-org:service:dial:1
USN: uuid:1ebc0a7a-71e3-42f8-99cf-51d34967c661::urn:dial-multiscreen-org:service:dial:1
```
Then you send a request that looks like this to the port found in the LOCATION parameter from the previous packet:

```
GET /dd.xml HTTP/1.0
```
This is the response, it contains the application url. And the dd.xml contains some info about the Tivo:

```
HTTP/1.1 200 OK
Content-Type: application/xml
Application-URL: http://192.168.1.104:49610/apps/

<?xml version="1.0"?><root xmlns="urn:schemas-upnp-org:device-1-0" xmlns:r="urn:restful-tv-org:schemas:upnp-dd"> <specVersion> <major>1</major> <minor>0</minor> </specVersion> <device> <deviceType>urn:schemas-upnp-org:device:tvdevice:1</deviceType> <friendlyName>Man Cave</friendlyName> <manufacturer> </manufacturer> <modelName>TiVo_TCD750500</modelName> <UDN>uuid:1ebc0a7a-71e3-42f8-99cf-51d34967c661</UDN> </device></root>
```
I packet sniffed the messages sent from the Chrome Browser that casts the YouTube Video to the Tivo and here is what I found. These packets are sent to the application url port found in the above packet:

This is the first packet sent, It just seems to establish a connection.

```
GET /apps/YouTube HTTP/1.1
Host: 192.168.1.104:49610
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
```
My packet Sniffer didn't give me the reply to the packet when Chrome sent it but when I send it the reply is:

```
HTTP/1.1 200 OK
Content-Type: application/xml
Access-Control-Allow-Origin: (null)

<?xml version="1.0" encoding="UTF-8"?>
<service xmlns="urn:dial-multiscreen-org:schemas:dial" dialVer="2.0">
  <name>YouTube</name>
  <options allowStop="true"/>
  <state>stopped</state>
  <additionalData>
    <screenId>v84vv4jgjrgdfctkd49fns52ot</screenId>
  </additionalData>
</service>
```
The next packet the browser sends is this, it will launch the YouTube app and play the video. The YouTube video ID is located in the pairingCode's "v=" parameter.

```
POST /apps/YouTube HTTP/1.1
Host: 192.168.1.104:49610
Connection: keep-alive
Content-Length: 73
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Origin: chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

pairingCode=ae4ae51b-eb10-4016-ac16-78a00d4071ee&v=1-oIL9cLHDc&t=6.565467
```
When I send the packet I get this response:

```
HTTP/1.1 201 Created
Content-Type: text/plain
Location: http://192.168.1.104:49610/apps/YouTube/run
Access-Control-Allow-Origin: chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm
```
*Note:* When I send the packet the YouTube app opens on the Tivo but the video doesn't play. The Chrome Browser can also control video playback so there are other commands that need to be uncovered. Also, I have 2 Tivos and the URL ports are different for each so you can't assume the ports will be the same.


----------



## Allanon (Nov 2, 2005)

Yet another update, just found out you can open the Netflix app just by replacing YouTube with Netflix in the /apps/YouTube part of the previous packet. The Netflix app displayed an error message because bad data was sent. But, I did see a Netflix DIAL protocol PDF online so it makes sense that it would work.

After getting Netflix to come up I found a list of names for other apps and tried the ones that are on the Tivo but couldn't get them to work.

Application Name/Prefix Registry - DIAL


----------



## Allanon (Nov 2, 2005)

Here is the code for my HME for Python app that opens YouTube using the DIAL protocol. It's not as short and pretty as my previous version but this doesn't need the KMTTG Web Server running and I should be able to port this to my Raspberry Pi computer running linux.

```
import hme
import socket
import urllib2

#IP address of server running HME App
SERVER_IP = '192.168.1.122'

TITLE = 'Youtube'

# Do a m-search for devices with dial protocol
# open dd.xml file and see if Tivo name is listed
# if so return application IP address and port
def get_application_ip_and_port(server_ip, tivo_name, timeout=1):
    device_list = []
    MSEARCH_MSG = ('M-SEARCH * HTTP/1.1\r\n' +
                   'HOST: 239.255.255.250:1900\r\n\r\n' +
                   'MX: 3\r\n' +
                   'MAN: "ssdp:discover"\r\n' +
                   'ST: urn:dial-multiscreen-org:service:dial:1\r\n')

    socket.setdefaulttimeout(timeout)
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
    sock.bind((server_ip, 0))
    sock.sendto(MSEARCH_MSG, ('239.255.255.250', 1900))

    while True:
        try:
            data = sock.recv(1024)
        except socket.timeout:
            sock.close()
            return None, None

        start = data.find('http://')
        end = data.find('/dd.xml')+ 7
        url = data[start:end]
        f = urllib2.urlopen(url)
        xml = f.read()
        if tivo_name in xml:
            data = str(f.info())
            start = data.find('http://')+7
            end = data.find('/apps')
            url = data[start:end]
            ip, port = url.split(':')
            return ip, int(port)


# Send a message and return the response
def request(message, host, port, timeout=2):
    socket.setdefaulttimeout(timeout)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.send(message)
    try:
        data = sock.recv(8000)
    except socket.timeout:
        sock.close()
        return

    return data


class Youtube(hme.Application):

    def handle_device_info(self, info):
        tivo_ip, port = get_application_ip_and_port(SERVER_IP, info['host'])

        if not tivo_ip or not port:
            print("\nCouldn't find application IP address or port.\n")
            quit()

        msg = 'POST /apps/YouTube HTTP/1.1\r\n' \
              'Host: {}:{}\r\n' \
              'Connection: keep-alive\r\n' \
              'Content-Length: 73\r\n' \
              'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36\r\n' \
              'Origin: 192.168.1.122:8080\r\n' \
              'Content-Type: plain/text;charset=UTF-8\r\n' \
              'Accept: */*\r\n' \
              'DNT: 1\r\n' \
              'Accept-Encoding: gzip, deflate\r\n' \
              'Accept-Language: en-US,en;q=0.8\r\n' \
              '\r\n' \
              'pairingCode=ae4ae51b-eb10-4016-ac16-78a00d4071ee&v=1-oIL9cLHDc&t=6.565467\r\n\r\n'.format(tivo_ip, port)

        message = request(msg, tivo_ip, port)
```


----------



## Allanon (Nov 2, 2005)

Mission accomplished, got the above code with HME for Python running on my Raspberry PI which can run quietly in my Mom's cabinet without her even knowing it's there. Thanks for all the information about the Tivo.


----------

