I think the foam is from the saliva being blown up with air by the tongue chewing/sucking. I have seen similar behaviors and they are helped by turnout or some sort of diversion. Hay in a small hole net might help keep him diverted.
All of the responses above are really good, but I had a thought.. what if he had food gunked up behind his tongue? He looks to reaaally be trying to stick his tongue out.. maybe he is just trying to dislodge stuck food?
One of the horses at the school I went to used to do this when he felt nervousnor insecure about something. He was so easy to work with that way because he was so obvious in when he was just starting to lose confidence in himself and you could work through it before either horse or rider got frustrated.
i have one horse that plays with his tongue , when he is bored flops it out the side and wags it around. another that sticks his toungue out the side and laps the water with it. A couple do that after deworming trying to get the paste off , and they go try to like the pipe fence .. so I have to watch them.