← all topics

12 · Video streaming, CDNs, socket programming

Slides 2-80 → 2-104 · no HW problems, so concepts only

Part 1 · Video streaming

The challenges

Video encoding

CBR (constant bit rate)VBR (variable bit rate)
Encoding rate is fixedEncoding rate changes as the amount of spatial/temporal coding changes

Examples: MPEG-1 (CD-ROM) 1.5 Mbps, MPEG-2 (DVD) 3–6 Mbps, MPEG-4 (Internet) 64 Kbps–12 Mbps.

Streaming stored video

Fix: client-side buffering + playout delay. The client waits a little before starting playback and stores incoming video in a buffer. Then it can play at a constant rate even when packets arrive unevenly. This compensates for network delay and jitter.

DASH: Dynamic, Adaptive Streaming over HTTP

ServerClient
Splits the video into chunksPeriodically measures server-to-client bandwidth
Stores each chunk encoded at several ratesReads the manifest and requests one chunk at a time
Provides a manifest file with the URLs of every chunk versionPicks the highest rate the current bandwidth can sustain, and can switch rates from chunk to chunk

The intelligence is at the client. It decides:

Streaming video = encoding + DASH + playout buffering.

Content distribution networks (CDNs)

Problem: stream content chosen from millions of videos to hundreds of thousands of users at once.

Option 1: one huge mega-server. Doesn't scale:

Option 2: CDN. Store copies at many geographically spread sites.

Enter deepBring home
Push CDN servers deep into many access networks, close to usersA smaller number (tens) of larger clusters in POPs near, but not inside, access networks
Akamai (240,000 servers, 120+ countries)Limelight

How it works: e.g. Netflix stores copies of a show on CDN nodes. A subscriber asks for it, gets the manifest, and is directed to a nearby copy. If that path is congested, it can switch to a different copy.

OTT ("over the top"): the CDN runs on top of ordinary Internet host-to-host service, so it has to cope with a congested Internet. Its decisions: which CDN node to fetch from, how viewers behave during congestion, and which content to place on which node.

Part 2 · Socket programming

Socket = the door between the app process and the transport protocol (topic 7). Two socket types for the two transport services:

UDP socketTCP socket
ServiceUnreliable datagrams: data may be lost or arrive out of orderReliable, in-order byte stream (a "pipe")
Connection?No handshake before sendingClient must connect first. TCP sets up the connection
AddressingSender attaches the destination IP + port to every packet. Receiver extracts the sender's IP + port from each packetAddress given once at connect time. After that, just send
Python typeSOCK_DGRAMSOCK_STREAM
Send / receivesendto(msg, (name, port)) / recvfrom() (returns data and address)send(msg) / recv() (data only)
Server socketsOne socket for all clientsA welcoming socket plus a new socket per client

The example app for both: the client reads a line from the keyboard and sends it. The server converts it to uppercase and sends it back. The client displays it.

UDP

UDPClient
from socket import *
serverName = 'hostname'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)          # UDP socket
message = raw_input('Input lowercase sentence:')
clientSocket.sendto(message.encode(), (serverName, serverPort))   # attach address
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage.decode()
clientSocket.close()
UDPServer
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))                 # bind to port 12000
while True:
    message, clientAddress = serverSocket.recvfrom(2048)   # get client's IP + port
    modifiedMessage = message.decode().upper()
    serverSocket.sendto(modifiedMessage.encode(), clientAddress)

TCP

TCPClient
from socket import *
serverName = 'servername'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)         # TCP socket
clientSocket.connect((serverName, serverPort))      # set up the connection
sentence = raw_input('Input lowercase sentence:')
clientSocket.send(sentence.encode())                # no address needed
modifiedSentence = clientSocket.recv(1024)
print ('From Server:', modifiedSentence.decode())
clientSocket.close()
TCPServer
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)         # welcoming socket
serverSocket.bind(('', serverPort))
serverSocket.listen(1)                              # listen for TCP requests
while True:
    connectionSocket, addr = serverSocket.accept()  # NEW socket per client
    sentence = connectionSocket.recv(1024).decode() # bytes only, no address
    capitalizedSentence = sentence.upper()
    connectionSocket.send(capitalizedSentence.encode())
    connectionSocket.close()                        # close this client, not the welcoming socket
Spot-the-difference (easy exam points)
• SOCK_DGRAM = UDP. SOCK_STREAM = TCP.
• Only TCP has connect() (client) and listen() / accept() (server).
• UDP uses sendto/recvfrom because every packet carries an address. TCP uses send/recv because the connection already knows who's on the other end.
• Both servers call bind() to a known port (12000). Clients don't need to.
• accept() returns a new socket. The server closes that one per client, and keeps the welcoming socket open.

Chapter 2 big themes

Quick check

1. What are spatial and temporal coding? Spatial: compress within one frame (send "purple × N" instead of N purple pixels). Temporal: send only the differences between consecutive frames.
2. CBR vs VBR? CBR: fixed encoding rate. VBR: encoding rate changes as the amount of spatial/temporal coding changes.
3. Why does a streaming client buffer before playing? Network delay varies (jitter), but playback must be continuous at the original timing. A playout delay plus a client buffer absorbs the variation.
4. In DASH, what does the server do, and what does the client decide? Server: splits the video into chunks, encodes each at several rates, and gives a manifest of URLs. Client: measures bandwidth and decides when to request a chunk, what rate to request, and where to get it from.
5. Give 3 reasons a single mega-server doesn't work for video. Any three: single point of failure, point of network congestion, long path to distant clients, many copies of the same video sent over one outgoing link.
6. Enter deep vs bring home? Enter deep: many CDN servers placed inside access networks, close to users (Akamai). Bring home: fewer, larger clusters in POPs near but not inside access networks (Limelight).
7. Why does the UDP client use sendto with an address, but the TCP client just uses send? UDP has no connection, so every datagram must carry the destination IP + port. TCP set up a connection with connect(), so the socket already knows the destination.
8. How does one TCP server handle many clients at once? accept() creates a new connection socket for each client. The welcoming socket stays open for new clients.
9. Which socket type is SOCK_DGRAM? SOCK_STREAM? SOCK_DGRAM = UDP. SOCK_STREAM = TCP.
That's all of Chapter 2. Next up: Chapter 3, the transport layer.

← 11 · P2P + BitTorrent · all topics