-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTEMPLATE_CODEC.cpp
More file actions
281 lines (232 loc) · 9.24 KB
/
Copy pathTEMPLATE_CODEC.cpp
File metadata and controls
281 lines (232 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright RF Creations Ltd 2026
// Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE)
/**
* @file TEMPLATE_CODEC.cpp
* @brief Template codec plugin for blueSPY
*
* Use this file as a skeleton for implementing a new codec plugin.
* It will also be helpful to look at the working implementations in this repo which include
* AAC.cpp, APTX.cpp, LDAC.cpp, and LC3.cpp.
*
* Each codec plugin must implement the following exported functions:
* - `init()`
* - `new_codec_stream()`
* - `codec_decode()`
* - `codec_deinit()`
*
* The host (blueSPY) will:
* 1. Load the shared library (DLL/.so/.dylib).
* 2. Call init() once to verify the codec name and API version.
* 3. Call new_codec_stream() when a new captured audio session begins.
* 4. Call codec_decode() repeatedly with encoded data packets/frames.
* 5. Call codec_deinit() when the stream ends or resets.
*
* Replace the placeholder logic below with your actual codec implementation.
*/
#include "bluespy_codec_interface.h"
#include "bluespy_codec_utils.h"
#include "codec_structures.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/*------------------------------------------------------------------------------
* Constants & Offsets
*----------------------------------------------------------------------------*/
/** Example PCM buffer size (in S16 samples) */
#define TEMPLATE_PCM_BUFFER_SAMPLES 8192
/** Example Vendor / Codec IDs (Replace with actual values) */
#define VENDOR_ID_EXAMPLE 0x00000000
#define CODEC_ID_EXAMPLE 0xFF
/** Example Configuration Offsets (Encourage abstraction of magic numbers) */
#define CONFIG_LEN_MIN 6
#define OFFSET_SAMPLE_RATE 4
/*------------------------------------------------------------------------------
* Data Structures
*----------------------------------------------------------------------------*/
/**
* @brief Per-stream codec decoder state.
*
* Each active stream has one instance of this structure. Put any
* codec-specific decoder handles, context state, or buffers here.
*/
typedef struct {
bluespy_audiostream_id parent_stream_id;
bool initialized;
/* Sequence Tracking for Gap Detection */
bool have_seq;
uint16_t last_seq;
uint32_t samples_per_frame; /* Used to calculate missing_samples on packet loss */
/* Example configuration */
uint32_t sample_rate;
uint8_t channels;
/* Example codec-specific handle (replace with real type) */
void* decoder_handle;
/* PCM output buffer (16-bit samples, interleaved) */
int16_t pcm_buffer[TEMPLATE_PCM_BUFFER_SAMPLES];
} TEMPLATE_stream;
/*------------------------------------------------------------------------------
* Codec Configuration Parsing
*----------------------------------------------------------------------------*/
/**
* @brief Parse configuration data from blueSPY container.
*/
static bool parse_codec_config(const bluespy_audio_codec_info* info, TEMPLATE_stream* stream) {
if (!info || !info->config || info->config_len == 0) {
return false;
}
/* Example of parsing logic based on container */
switch (info->container) {
case BLUESPY_CODEC_AVDTP:
/* Typical for Classic A2DP codecs — parse AVDTP capabilities here */
stream->sample_rate = 44100;
stream->channels = 2;
break;
case BLUESPY_CODEC_CIS:
case BLUESPY_CODEC_BIS:
/* LE Audio (Isochronous Stream) configuration (LTV parsing) */
stream->sample_rate = 48000;
stream->channels = 1;
break;
default:
return false;
}
return true;
}
/*------------------------------------------------------------------------------
* Public API Functions (required)
*----------------------------------------------------------------------------*/
extern "C" {
BLUESPY_CODEC_API bluespy_audio_codec_lib_info init(void) {
return (bluespy_audio_codec_lib_info){
.api_version = BLUESPY_AUDIO_API_VERSION,
.codec_name = "TEMPLATE_CODEC" /* TODO: Change this name */
};
}
BLUESPY_CODEC_API bluespy_audio_codec_init_ret
new_codec_stream(bluespy_audiostream_id stream_id, const bluespy_audio_codec_info* info) {
bluespy_audio_codec_init_ret ret = {
.error = -1, .format = {0}, .fns = {0}, .context_handle = 0};
/* Parameter validation */
if (!info || !info->config || info->config_len == 0) {
return ret;
}
/* TODO: Add a check here to ensure this plugin actually supports
* the requested codec (e.g. check Vendor ID / Codec ID in info->config).
*/
/* Parse configuration into temporary struct to allow dry-run testing */
TEMPLATE_stream temp_stream = {0};
if (!parse_codec_config(info, &temp_stream)) {
ret.error = -3;
return ret;
}
/* Dry Run Check: If stream_id is INVALID, the host just wants to verify format support. */
if (stream_id == BLUESPY_ID_INVALID) {
ret.error = 0;
ret.format.sample_rate = temp_stream.sample_rate;
ret.format.n_channels = temp_stream.channels;
return ret;
}
/* Allocate State */
TEMPLATE_stream* stream = (TEMPLATE_stream*)calloc(1, sizeof(TEMPLATE_stream));
if (!stream) {
ret.error = -2;
return ret;
}
/* Apply parsed config */
stream->parent_stream_id = stream_id;
stream->sample_rate = temp_stream.sample_rate;
stream->channels = temp_stream.channels;
/* Initialize sequence tracking */
stream->have_seq = false;
stream->last_seq = 0;
stream->samples_per_frame = 1024; /* TODO: Update this based on codec spec */
/* Initialize Decoder */
/* TODO: Call your actual decoder initialization here */
stream->decoder_handle = malloc(1);
if (!stream->decoder_handle) {
free(stream);
ret.error = -4;
return ret;
}
stream->initialized = true;
/* Report success */
ret.error = 0;
ret.context_handle = (uintptr_t)stream;
ret.format.sample_rate = stream->sample_rate;
ret.format.n_channels = stream->channels;
ret.format.sample_format = BLUESPY_AUDIO_FORMAT_S16_LE;
ret.fns.decode = codec_decode;
ret.fns.deinit = codec_deinit;
return ret;
}
BLUESPY_CODEC_API void codec_decode(uintptr_t context, const uint8_t* payload,
const uint32_t payload_len, bluespy_event_id event_id,
uint64_t sequence_number) {
TEMPLATE_stream* stream = (TEMPLATE_stream*)context;
if (!stream || !stream->initialized) {
return;
}
/* -------------------------------------------------------------
* 1. Gap Detection
* ------------------------------------------------------------- */
uint32_t missing_samples = 0;
/* Determine the sequence number based on transport:
* - A2DP (AVDTP): Extract from RTP header via `read_be16(payload + RTP_SEQ_OFFSET)`
* - LE Audio (ISO): Provided directly by host via the `sequence_number` argument.
*/
uint16_t current_seq = (uint16_t)sequence_number; /* Assuming LE Audio for template */
if (stream->have_seq) {
int32_t diff = calculate_rtp_seq_diff(current_seq, stream->last_seq);
if (diff > 1) {
uint32_t missing_packets = (uint32_t)(diff - 1);
missing_samples = missing_packets * stream->samples_per_frame;
}
}
stream->last_seq = current_seq;
stream->have_seq = true;
/* Handle empty/missing payloads (Sniffer timeline still needs the gap reported) */
if (!payload || payload_len == 0) {
if (missing_samples > 0) {
bluespy_add_audio(NULL, 0, event_id, missing_samples);
}
return;
}
/* -------------------------------------------------------------
* 2. Header Stripping (For A2DP / RTP only)
* ------------------------------------------------------------- */
/* uint32_t rtp_len = get_rtp_header_length(payload, payload_len);
if (rtp_len == 0) return;
const uint8_t* frame_data = payload + rtp_len;
uint32_t frame_len = payload_len - rtp_len;
*/
/* -------------------------------------------------------------
* 3. Decode Frame
* ------------------------------------------------------------- */
/* TODO: Pass payload (or frame_data) to your decoder handle.
* WARNING: If decode fails due to missing data, DO NOT generate
* Packet Loss Concealment (PLC). This is a sniffer, so missing
* data should remain silent to accurately reflect the air trace.
*/
size_t bytes_to_copy = payload_len;
if (bytes_to_copy > sizeof(stream->pcm_buffer)) {
bytes_to_copy = sizeof(stream->pcm_buffer);
}
memcpy(stream->pcm_buffer, payload, bytes_to_copy);
/* -------------------------------------------------------------
* 4. Deliver PCM
* ------------------------------------------------------------- */
bluespy_add_audio((const uint8_t*)stream->pcm_buffer, (uint32_t)bytes_to_copy, event_id,
missing_samples);
}
BLUESPY_CODEC_API void codec_deinit(uintptr_t context) {
TEMPLATE_stream* stream = (TEMPLATE_stream*)context;
if (stream) {
/* Free internal decoder resources */
if (stream->decoder_handle) {
free(stream->decoder_handle);
}
free(stream);
}
}
} // end extern "C"