How could we know that they are unhappy? Photos of overcrowded farms look terrible, but animals may have different value structure, like:
longer life expectancy than in the forest
guaranteed access to unlimited amounts of food.
Technically, we could have two ways to measure their preferences: do they feel constant pain according to their EEG + do they want to escape at any price and even happy to be slaughtered?