11import 'dart:async' ;
22import 'dart:convert' ;
3+ import 'dart:math' ;
34import 'dart:typed_data' ;
45
56import 'package:flutter/foundation.dart' ;
@@ -17,9 +18,9 @@ class PocketDeviceConnection extends DeviceConnection {
1718 65 , 80 , 80 , 38 , 83 , 75 , 38 , 101 , 121 , 54 , 114 , 66 , 80 , 88 , 80 , 80 , 105 , 97 , 86 , 67 , 103 , 105 , 84
1819 ]; // "APP&SK&ey6rBPXPPiaVCgiT"
1920
20- // MP3 Frame marker
21- static const List <int > mp3Marker = [0xFF , 0xF3 , 0x48 , 0xC4 ];
22- static const int mp3FrameSize = 144 ;
21+ // MP3 frame marker and size (from protocol analysis)
22+ static const List <int > mp3Marker = [0xFF , 0xF3 , 0x48 , 0xC4 ]; // MP3 frame marker (4 bytes)
23+ static const int mp3FrameSize = 144 ; // bytes per frame
2324
2425 // Response buffers
2526 final List <int > _responseBuffer = [];
@@ -188,9 +189,9 @@ class PocketDeviceConnection extends DeviceConnection {
188189 if (parts.length >= 4 ) {
189190 final used = int .parse (parts[2 ]);
190191 final total = int .parse (parts[3 ]);
191- debugPrint ('Pocket: Storage response - used: $used KB , total: $total KB ' );
192- // Convert KB to bytes
193- return (used * 1024 , total * 1024 );
192+ debugPrint ('Pocket: Storage response - used: $used MB , total: $total MB ' );
193+ // Convert MB to bytes
194+ return (used * 1024 * 1024 , total * 1024 * 1024 );
194195 }
195196 } catch (e) {
196197 debugPrint ('Pocket: Error parsing storage: $e ' );
@@ -276,36 +277,34 @@ class PocketDeviceConnection extends DeviceConnection {
276277 await Future .delayed (const Duration (seconds: 30 ));
277278
278279 // Find MP3 data in buffer
280+ debugPrint ('Pocket: Response buffer size: ${_responseBuffer .length } bytes' );
281+ debugPrint ('Pocket: First 20 bytes of buffer: ${_responseBuffer .take (20 ).toList ()}' );
282+
279283 final audioStart = _findMarker (_responseBuffer, mp3Marker);
280284 if (audioStart < 0 ) {
281285 debugPrint ('Pocket: MP3 marker not found in response' );
282286 return null ;
283287 }
284288
289+ debugPrint ('Pocket: MP3 marker found at position $audioStart ' );
285290 final audioData = _responseBuffer.sublist (audioStart);
291+ debugPrint ('Pocket: Audio data size: ${audioData .length } bytes' );
292+ debugPrint ('Pocket: First 20 bytes of audio data: ${audioData .take (20 ).toList ()}' );
286293
287- // Verify and extract valid MP3 frames
288- int packetCount = 0 ;
289- int pos = 0 ;
290-
291- while (pos < audioData.length - mp3FrameSize) {
292- if (_matchesMarker (audioData, pos, mp3Marker)) {
293- packetCount++ ;
294- pos += mp3FrameSize;
295- } else {
296- break ;
294+ // Check if there's any non-zero data
295+ int nonZeroCount = 0 ;
296+ for (int i = 0 ; i < min (1000 , audioData.length); i++ ) {
297+ if (audioData[i] != 0 && audioData[i] != 0xFF && audioData[i] != 0xF3 && audioData[i] != 0x48 && audioData[i] != 0xC4 ) {
298+ nonZeroCount++ ;
297299 }
298300 }
301+ debugPrint ('Pocket: Non-zero bytes in first 1000: $nonZeroCount ' );
299302
300- if (packetCount == 0 ) {
301- debugPrint ('Pocket: No valid MP3 frames found' );
302- return null ;
303- }
304-
305- final validData = audioData.sublist (0 , packetCount * mp3FrameSize);
306- debugPrint ('Pocket: Downloaded ${validData .length } bytes ($packetCount frames)' );
303+ // Return ALL audio data after the first marker (like Python does)
304+ // Python doesn't validate frame-by-frame, it just saves everything
305+ debugPrint ('Pocket: Downloaded ${audioData .length } bytes of MP3 data' );
307306
308- return Uint8List .fromList (validData );
307+ return Uint8List .fromList (audioData );
309308 } catch (e) {
310309 debugPrint ('Pocket: Error downloading recording: $e ' );
311310 return null ;
0 commit comments