In any misdemeanor proceeding in any court, if a person refuses to answer a question or produce evidence of any other kind on the ground that he may be incriminated thereby, the person may agree in writing with the district attorney of the county, or the prosecuting attorney of a city, as the case may be, to testify voluntarily pursuant to this section. Upon written request of such district attorney, or prosecuting attorney, the court having jursidiction of the proceeding shall approve such written agreement, unless the court finds that to do so would be clearly contrary to the public interest. If, after court approval of such agreement, and if, but for this section, the person would have been privileged to withhold the answer given or the evidence produced by him, that person shall not be prosecuted or subjected to penalty or forfeiture for or on account of any fact or act concerning which, in accordance with such agreement, he answered or produced evidence, but he may, nevertheless, be prosecuted or subjected to penalty or forfeiture for any perjury, false swearing or contempt committed in answering or in producing evidence in accordance with such agreement. If such person fails to give any answer or to produce any evidence in accordance with such agreement, that person shall be prosecuted or subjected to penalty or forfeiture in the same manner and to the same extent as he would be prosecuted or subjected to penalty or forfeiture but for this section.
Ca. Pen. Code § 1324.1