|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright(c) 2024-2025 Intel Corporation |
| 3 | +# Media Communications Mesh |
| 4 | + |
| 5 | +import argparse |
| 6 | +import logging |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from video_integrity import calculate_chunk_hashes |
| 11 | + |
| 12 | + |
| 13 | +def get_pcm_frame_size(sample_size: int, sample_num: int, channel_num: int) -> int: |
| 14 | + return sample_size * sample_num * channel_num |
| 15 | + |
| 16 | + |
| 17 | +class AudioIntegritor: |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + logger: logging.Logger, |
| 21 | + src_url: str, |
| 22 | + out_name: str, |
| 23 | + sample_size: int = 2, |
| 24 | + sample_num: int = 480, |
| 25 | + channel_num: int = 2, |
| 26 | + out_path: str = "/mnt/ramdisk", |
| 27 | + delete_file: bool = True, |
| 28 | + ): |
| 29 | + self.logger = logger |
| 30 | + self.src_url = src_url |
| 31 | + self.out_name = out_name |
| 32 | + self.sample_size = sample_size |
| 33 | + self.sample_num = sample_num |
| 34 | + self.channel_num = channel_num |
| 35 | + self.frame_size = get_pcm_frame_size(sample_size, sample_num, channel_num) |
| 36 | + self.out_path = out_path |
| 37 | + self.delete_file = delete_file |
| 38 | + self.src_chunk_sums = calculate_chunk_hashes(src_url, self.frame_size) |
| 39 | + |
| 40 | + |
| 41 | +class AudioFileIntegritor(AudioIntegritor): |
| 42 | + def check_integrity_file(self, out_url) -> bool: |
| 43 | + self.logger.info( |
| 44 | + f"Checking integrity for src {self.src_url} and out {out_url} " |
| 45 | + f"with frame size {self.frame_size}" |
| 46 | + ) |
| 47 | + src_chunk_sums = self.src_chunk_sums |
| 48 | + out_chunk_sums = calculate_chunk_hashes(out_url, self.frame_size) |
| 49 | + bad_frames = 0 |
| 50 | + for idx, chunk_sum in enumerate(out_chunk_sums): |
| 51 | + if idx >= len(src_chunk_sums) or chunk_sum != src_chunk_sums[idx]: |
| 52 | + self.logger.error(f"Bad audio frame at index {idx} in {out_url}") |
| 53 | + bad_frames += 1 |
| 54 | + if bad_frames: |
| 55 | + self.logger.error( |
| 56 | + f"Received {bad_frames} bad frames out of {len(out_chunk_sums)} checked." |
| 57 | + ) |
| 58 | + return False |
| 59 | + self.logger.info(f"All {len(out_chunk_sums)} frames in {out_url} are correct.") |
| 60 | + return True |
| 61 | + |
| 62 | + |
| 63 | +class AudioStreamIntegritor(AudioIntegritor): |
| 64 | + def get_out_files(self): |
| 65 | + return sorted(Path(self.out_path).glob(f"{self.out_name}*")) |
| 66 | + |
| 67 | + def check_stream_integrity(self) -> bool: |
| 68 | + bad_frames_total = 0 |
| 69 | + out_files = self.get_out_files() |
| 70 | + if not out_files: |
| 71 | + self.logger.error( |
| 72 | + f"No output files found for stream in {self.out_path} with prefix {self.out_name}" |
| 73 | + ) |
| 74 | + return False |
| 75 | + for out_file in out_files: |
| 76 | + self.logger.info(f"Checking integrity for segment file: {out_file}") |
| 77 | + out_chunk_sums = calculate_chunk_hashes(str(out_file), self.frame_size) |
| 78 | + for idx, chunk_sum in enumerate(out_chunk_sums): |
| 79 | + if ( |
| 80 | + idx >= len(self.src_chunk_sums) |
| 81 | + or chunk_sum != self.src_chunk_sums[idx] |
| 82 | + ): |
| 83 | + self.logger.error(f"Bad audio frame at index {idx} in {out_file}") |
| 84 | + bad_frames_total += 1 |
| 85 | + if self.delete_file: |
| 86 | + out_file.unlink() |
| 87 | + if bad_frames_total: |
| 88 | + self.logger.error( |
| 89 | + f"Received {bad_frames_total} bad frames in stream segments." |
| 90 | + ) |
| 91 | + return False |
| 92 | + self.logger.info("All frames in stream segments are correct.") |
| 93 | + return True |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + # Set up logging |
| 98 | + logging.basicConfig( |
| 99 | + level=logging.INFO, |
| 100 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 101 | + ) |
| 102 | + logger = logging.getLogger(__name__) |
| 103 | + |
| 104 | + # Create the argument parser |
| 105 | + parser = argparse.ArgumentParser( |
| 106 | + description="Audio Integrity Checker", |
| 107 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 108 | + ) |
| 109 | + subparsers = parser.add_subparsers( |
| 110 | + dest="mode", help="Operation mode", required=True |
| 111 | + ) |
| 112 | + |
| 113 | + # Common arguments for both file and stream modes |
| 114 | + def add_common_arguments(parser): |
| 115 | + parser.add_argument("src", help="Source audio file path") |
| 116 | + parser.add_argument("out", help="Output audio file name (without extension)") |
| 117 | + parser.add_argument( |
| 118 | + "--sample_size", |
| 119 | + type=int, |
| 120 | + default=2, |
| 121 | + help="Audio sample size in bytes (default: 2)", |
| 122 | + ) |
| 123 | + parser.add_argument( |
| 124 | + "--sample_num", |
| 125 | + type=int, |
| 126 | + default=480, |
| 127 | + help="Number of samples per frame (default: 480)", |
| 128 | + ) |
| 129 | + parser.add_argument( |
| 130 | + "--channel_num", |
| 131 | + type=int, |
| 132 | + default=2, |
| 133 | + help="Number of audio channels (default: 2)", |
| 134 | + ) |
| 135 | + parser.add_argument( |
| 136 | + "--output_path", |
| 137 | + type=str, |
| 138 | + default="/mnt/ramdisk", |
| 139 | + help="Output path (default: /mnt/ramdisk)", |
| 140 | + ) |
| 141 | + parser.add_argument( |
| 142 | + "--delete_file", |
| 143 | + action="store_true", |
| 144 | + default=True, |
| 145 | + help="Delete output files after processing (default: True)", |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + "--no_delete_file", |
| 149 | + action="store_false", |
| 150 | + dest="delete_file", |
| 151 | + help="Do NOT delete output files after processing", |
| 152 | + ) |
| 153 | + |
| 154 | + # Stream mode parser |
| 155 | + stream_help = """Check integrity for audio stream (stream saved into files segmented by time) |
| 156 | +
|
| 157 | +It assumes that there is X digit segment number in the file name like `out_name_001.pcm` or `out_name_02.pcm`. |
| 158 | +It can be achieved by using ffmpeg with `-f segment` option. |
| 159 | +
|
| 160 | +Example: ffmpeg -i input.wav -f segment -segment_time 3 out_name_%03d.pcm""" |
| 161 | + stream_parser = subparsers.add_parser( |
| 162 | + "stream", |
| 163 | + help="Check integrity for audio stream (segmented files)", |
| 164 | + description=stream_help, |
| 165 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 166 | + ) |
| 167 | + add_common_arguments(stream_parser) |
| 168 | + stream_parser.add_argument( |
| 169 | + "--segment_duration", |
| 170 | + type=int, |
| 171 | + default=3, |
| 172 | + help="Segment duration in seconds (default: 3)", |
| 173 | + ) |
| 174 | + |
| 175 | + # File mode parser |
| 176 | + file_help = """Check integrity for single audio file. |
| 177 | +
|
| 178 | +This mode compares a single output audio file against a source reference file. |
| 179 | +It performs frame-by-frame integrity checking using MD5 checksums.""" |
| 180 | + file_parser = subparsers.add_parser( |
| 181 | + "file", |
| 182 | + help="Check integrity for single audio file", |
| 183 | + description=file_help, |
| 184 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 185 | + ) |
| 186 | + add_common_arguments(file_parser) |
| 187 | + |
| 188 | + # Parse the arguments |
| 189 | + args = parser.parse_args() |
| 190 | + |
| 191 | + # Execute based on mode |
| 192 | + if args.mode == "stream": |
| 193 | + integrator = AudioStreamIntegritor( |
| 194 | + logger, |
| 195 | + args.src, |
| 196 | + args.out, |
| 197 | + args.sample_size, |
| 198 | + args.sample_num, |
| 199 | + args.channel_num, |
| 200 | + args.output_path, |
| 201 | + args.delete_file, |
| 202 | + ) |
| 203 | + result = integrator.check_stream_integrity() |
| 204 | + elif args.mode == "file": |
| 205 | + # For file mode, construct the full output file path |
| 206 | + out_file = Path(args.output_path) / args.out |
| 207 | + integrator = AudioFileIntegritor( |
| 208 | + logger, |
| 209 | + args.src, |
| 210 | + args.out, |
| 211 | + args.sample_size, |
| 212 | + args.sample_num, |
| 213 | + args.channel_num, |
| 214 | + args.output_path, |
| 215 | + args.delete_file, |
| 216 | + ) |
| 217 | + result = integrator.check_integrity_file(str(out_file)) |
| 218 | + else: |
| 219 | + parser.print_help() |
| 220 | + return |
| 221 | + |
| 222 | + if result: |
| 223 | + logging.info("Audio integrity check passed") |
| 224 | + else: |
| 225 | + logging.error("Audio integrity check failed") |
| 226 | + sys.exit(1) |
| 227 | + |
| 228 | + |
| 229 | +if __name__ == "__main__": |
| 230 | + main() |
0 commit comments