Image copyright AP Image caption William Porter (right) is the first of six officers to stand trial over Freddie Gray's death

The judge in the case of a Baltimore police officer on trial over a death in custody has declared a mistrial after the jury was unable to reach a verdict.

Policeman William Porter is the first of six officers charged in the death of 25-year-old Freddie Gray.

The jury deliberated for three days, but could not come to an agreement on all four charges.

Police have been positioned around the city to prevent riots like those that erupted following Gray's death.

Judge Barry Williams said the jury - composed of seven black and five white people - had "clearly been diligent" in their deliberations.

The mistrial is a major setback for prosecutors, who may now have to build another case against the policeman. They had also hoped to use Mr Porter as a witness in the trials against the other five officers, had he been found guilty.

Media playback is unsupported on your device Media caption Gray family speak to BBC

Judge Williams has scheduled a hearing for Thursday to discuss a possible new trial.

Urging the public to remain calm, Gray's family thanked the the jury for their service and pushed for the prosecutor to bring a new trial against Mr Porter.

After the announcement and into the evening, protesters marched through the streets and demonstrated in front of City Hall and other prominent buildings.

"Indict, convict, send those killer cops to jail," some of the protesters chanted, during peaceful demonstrations.

At the scene: Rajini Vaidyanathan, BBC News, Baltimore

Media playback is unsupported on your device Media caption "Freddie Gray was a friend of mine"

After two and a half days of deliberations, the jury in this trial simply couldn't agree.

The group of 12 (3 white women, 3 black men, 4 black women and 2 white men), were in court as the Judge announced their decision. So too was the accused, Officer William Porter, wearing a black shirt, tie and suit.

For any convictions to be made the jury needed to reach a unanimous decision on all four counts.

A sign of their indecision was reflected over the past few days, when they'd requested copies of witness transcripts, something the Judge denied.

The prosecution had hoped this case would be resolved before the trials - in January - of the other Baltimore police officers charged over Freddie Gray's death.

They'd hoped to use Officer Porter as a witness in those cases, but now that he'll face a second trial himself, things look much more complex.

Gray's death in April quickly became a flashpoint in a national debate over police use of force - especially against black men.

Protests raged for several days and at one point turned violent, forcing officials to declare a state of emergency and to deploy national guard troops across the city of 620,000 people.

Baltimore Mayor Stephanie Rawlings-Blake said that the city was "prepared to respond" to "any disturbance in the city" and encouraged peaceful protests.

Freddie Gray's death - in depth

Image copyright EPA

Duel identities - how a divided city came to riot

Ferguson v Baltimore - two cities rocked by protest compared

Returning to normal - normality means violence, tension, poverty and drug addiction for much of Baltimore

Baltimore through the lens - an amateur photographer looks at a city on edge

Concerned protests could turn violent after the trial's outcome, area school districts cancelled field trips and police officers were barred from taking leave.

Gray died on 19 April - a week after his neck was broken while riding in the back of a police van. His wrists and ankles were cuffed, but he was not restrained with a seat belt.

A post mortem report found the neck injury was likely sustained when he slammed into a van wall during braking or cornering.

Image copyright Family photo Image caption Gray died in hospital in April after spending about a week in a coma

Throughout the trial, the prosecution insisted that Mr Porter could have saved Gray's life by restraining him and by calling for medical help after his injury. They described the police van as a coffin on wheels.

Mr Porter testified in his own defence and said that Gray showed no distress signs before arriving at a police station with critical injuries.

Mr Porter was charged with manslaughter, assault, reckless endangerment and misconduct. He faces to up to 25 years in prison if convicted on all counts.