i've worked on chromecast recievers and clients
most smart TV apps, are actually just web pages, with a few extensions in java script to hook into the chromecast mediaplayer (which is just an interface to a piece of software on the chromecast firmware.... on say a samsung or LG tv its pretty similar too but they have their own proprietary apis).
chromecast is no different. you make a special web page. it has event hooks for when someone "casts". and you can hook into those events. So if I click play on my phone, it goes through the network, and the TV gets an event which some javascript wouldu see and it would do whatever it needed to get Urls for your video etc.
when you cast to a TV all you are telling theTV to do is load "the special webpage" that company X made for the chromecast. the chromecast has an api for a media player which supports most of the popular streaming formats (off the top of my head, chromecast supports HLS , smoothstreaming and MPEG DASH with widevine or playready digital rights management) and a vendor will tell the player to start streaming a video from the cloud.