The man who died has been identified as 66-year-old Livermore resident George Biehs. Investigators say he left behind a suicide note.
Police say he shot himself in a very public place, right on the street.
That had officers worried about their safety and the safety of the people living and likely sleeping across the street in an apartment complex.
It happened at 3 a.m. on Pacific Avenue in front of the police department.
Livermore Police say an armed man parked outside their building shot himself in the head as they approached. pic.twitter.com/MDRDph34We— Amy Hollyfield (@amyhollyfield) March 3, 2016
An officer was just returning from taking a report from a family of a missing, suicidal man. When he got there, he recognized the man's parked car.
He approached the car, saw a shotgun and called for backup. Then he heard a blast from inside the car.
Right then, a second officer arrived. He fired back, which is what some residents heard.
"At about 3 o'clock in the morning I heard about four gunshots and they were very loud. Since I live right here, it was kind of nerve-wracking," resident Flora Garcia said.
"The second officer exited his vehicle and he proceeded to fire four rounds into the vehicle as they believed they were under fire," Livermore Police Department's Ofc. Art Rosas said.
When ABC7 News asked police how they know for sure it was his bullet that killed him and not theirs, they said it was obvious from the placement of the gun and the wound.
It wasn't obvious for a while that he was dead and they were concerned he could be lying in wait and could hurt them.
That's why they fired four rounds and yelled at him to come out with his hands up.
They even fired beanbags into the car before approaching.
They say the family has told them the man had a history of mental illness and they don't know why he came here to the police department.