It’s really not okay to masturbate in public. It’s not consensual for everyone else, and doesn’t allow for healthy boundaries. It *is* that bad. Depending on how he’s doing it - like if he’s making eye contact with anyone intentionally - it could also be outright harassment. A classroom should be emotionally safe for everyone in it. Someone engaging in sex in it during class makes it unsafe for everyone else.
If you don’t trust that the teacher will handle it with sensitivity (are you sure?), then your other option is for you to say something. A simple, quiet and gentle, “Please don’t do that in class,” the next time it happens should suffice as a start.
Never doubt that a small group of thoughtful, committed citizens can change the world. Indeed, it is the only thing that ever has. - Margaret Mead