In my opinion as an EE there would be some reasonably reliable ways to detect either a line or headphone connection. Headphone impedances are typically between 16 and 600 Ohms, and line impedance is typically in the 10's of kOhm range. That's one way to differentiate. There has to be an output signal (presumably audio) first, however.
Another way is to apply a very small DC current (that would certainly not damage headphones, so I would guess in the micro-ampere range) and check if there is continuity or measure the DC resistance of the load outright. For line inputs, as they're usually AC-coupled, DC resistance would be infinite, and for (DC coupled) headphones it is very low. It's an interesting subject and I wouldn't be surprised if there are some patents involved.