-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_windows.go
More file actions
946 lines (833 loc) · 24.2 KB
/
Copy pathaudio_windows.go
File metadata and controls
946 lines (833 loc) · 24.2 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
package nativeaudio
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"syscall"
"unicode/utf16"
"unsafe"
"git.sr.ht/~jackmordaunt/nativeaudio/internal"
"github.com/ebitengine/oto/v3"
"golang.org/x/sys/windows"
)
func start() error {
return MFStartup(MF_VERSION, MFSTARTUP_LITE)
}
func end() error {
return MFShutdown()
}
// play the audio file using Windows Media Foundation.
func play(path string) error {
data, format, err := load(path)
if err != nil {
return fmt.Errorf("decoding: %w", err)
}
ctx, ready, err := oto.NewContext(&oto.NewContextOptions{
SampleRate: format.SampleRate,
ChannelCount: format.Channels,
Format: oto.FormatSignedInt16LE,
})
if err != nil {
return fmt.Errorf("starting playback context: %w", err)
}
<-ready
done := make(chan any)
player := ctx.NewPlayer(internal.NewTriggerReader(bytes.NewReader(data), func() { close(done) }))
player.Play()
<-done
ctx.Suspend()
return player.Close()
}
// load raw pcm data from the Windows Media Foundation.
func load(path string) (uncompressed []byte, format Format, err error) {
f, err := os.Open(path)
if err != nil {
return nil, format, fmt.Errorf("opening input file: %w", err)
}
defer f.Close()
by, err := io.ReadAll(f)
if err != nil {
return nil, format, fmt.Errorf("buffering input file: %w", err)
}
return decode(by)
}
// decode compressed data, returning the uncompressed data as PCM data
// (s16le) and details about the PCM required to playback correctly.
func decode(compressed []byte) (uncompressed []byte, format Format, err error) {
stream := SHCreateMemStream(unsafe.SliceData(compressed), len(compressed))
if stream == nil {
return nil, format, fmt.Errorf("could not allocate IStream")
}
defer stream.Release()
// We need to adapt the generic IStream to a Media Foundation stream type.
var mfByteStream *IMFByteStream
defer mfByteStream.Release()
if err := MFCreateMFByteStreamOnStream(stream, &mfByteStream); err != nil {
return nil, format, fmt.Errorf("creating MFByteStream from IStream: %w", err)
}
// Attributes to configure the source reader with; specifically, enable hardware codecs.
var attributes *IMFAttributes
defer attributes.Release()
if err := MFCreateAttributes(&attributes, 1); err != nil {
return nil, format, fmt.Errorf("creating attributes to apply to source reader: %w", err)
}
if err := attributes.SetUINT32(&MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); err != nil {
return nil, format, fmt.Errorf("enabling hardware transforms: %w", err)
}
// Create the source reader using the byte stream.
var mfSourceReader *IMFSourceReader
defer mfSourceReader.Release()
if err := MFCreateSourceReaderFromByteStream(mfByteStream, attributes, &mfSourceReader); err != nil {
return nil, format, fmt.Errorf("creating IMFSourceReader from IMFByteStream: %w", err)
}
if err := configureAudioStream(mfSourceReader); err != nil {
return nil, format, fmt.Errorf("configuring audio stream: %w", err)
}
format, err = getSourceReaderFormat(mfSourceReader)
if err != nil {
return nil, format, fmt.Errorf("getting format: %w", err)
}
buf, err := io.ReadAll(NewSourceReader(mfSourceReader))
if err != nil {
return nil, format, err
}
runtime.KeepAlive(compressed)
return buf, format, nil
}
// configureAudioStream selects the first audio stream and configures it output PCM.
func configureAudioStream(pReader *IMFSourceReader) error {
var pPartialType *IMFMediaType
defer pPartialType.Release()
// Create a partial media pUncompressedAutioTypee that specifies uncompressed PCM audio.
if err := MFCreateMediaType(&pPartialType); err != nil {
return fmt.Errorf("creating media type: %w", err)
}
if err := pPartialType.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Audio); err != nil {
return fmt.Errorf("setting major type: %w", err)
}
if err := pPartialType.SetGUID(&MF_MT_SUBTYPE, &MFAudioFormat_PCM); err != nil {
return fmt.Errorf("setting sub type: %w", err)
}
// Select the first audio stream, and deselect all other streams.
if err := pReader.SetStreamSelection(MF_SOURCE_READER_ALL_STREAMS, false); err != nil {
return fmt.Errorf("deselecting audio streams: %w", err)
}
if err := pReader.SetStreamSelection(MF_SOURCE_READER_FIRST_AUDIO_STREAM, true); err != nil {
return fmt.Errorf("selecting first audio stream: %w", err)
}
// Set this type on the source reader. The source reader will load the necessary decoder.
if err := pReader.SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, pPartialType); err != nil {
return fmt.Errorf("setting media type on source reader: %w", err)
}
return nil
}
// getSourceReaderFormat returns the audio format configured for the source reader.
func getSourceReaderFormat(sr *IMFSourceReader) (f Format, _ error) {
var mfMediaType *IMFMediaType
defer mfMediaType.Release()
// Get the complete uncompressed format.
if err := sr.GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &mfMediaType); err != nil {
return f, fmt.Errorf("getting the current media type: %w", err)
}
return getFormat(mfMediaType)
}
// getFormat extracts the audio format from a media type.
func getFormat(mt *IMFMediaType) (f Format, _ error) {
var (
numChannels uint32
sampleRate uint32
bitsPerSample uint32
)
if err := mt.GetUINT32(&MF_MT_AUDIO_NUM_CHANNELS, &numChannels); err != nil {
return f, fmt.Errorf("getting numChannels for the media type: %w", err)
}
if err := mt.GetUINT32(&MF_MT_AUDIO_SAMPLES_PER_SECOND, &sampleRate); err != nil {
return f, fmt.Errorf("getting sampleRate for the media type: %w", err)
}
if err := mt.GetUINT32(&MF_MT_AUDIO_BITS_PER_SAMPLE, &bitsPerSample); err != nil {
return f, fmt.Errorf("getting bitsPerSample for the media type: %w", err)
}
f = Format{
SampleRate: int(sampleRate),
Channels: int(numChannels),
BitDepth: int(bitsPerSample / 8),
}
return f, nil
}
// SourceReader wraps an IMFSourceReader and implements [io.Reader].
type SourceReader struct {
source *IMFSourceReader
sample *IMFSample // sample object containing one or more streams
buffer *IMFMediaBuffer // buffer object containing the raw buffer
chunk *byte // start of chunk of audio data
chunkSz int64 // size of chunk
prevTimestamp int64
currentTimestamp int64
data []byte // Go view of the audio data, backed by native memory.
}
// NewSourceReader allocates a [SourceReader].
// The caller is responsible for releasing the underlying [IMFSourceReader].
func NewSourceReader(r *IMFSourceReader) *SourceReader {
return &SourceReader{source: r}
}
func (s *SourceReader) Read(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
// Write residual data into p before requesting more.
if len(s.data) > 0 {
return s.copyInto(p), nil
}
if !s.next() {
return 0, io.EOF
}
return s.copyInto(p), nil
}
// next reads the next audio sample, returning true if found, or false if EOF.
func (s *SourceReader) next() bool {
for {
var flags int64
// Read the next sample; skipping samples with matching time stamps.
// For some reason ReadSample can produce more than one sample at time 0.
// Emitting all of them produces largers files and audio artifacts.
s.source.ReadSample(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, nil, &flags, &s.currentTimestamp, &s.sample)
if flags&MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED != 0 {
return false
}
if flags&MF_SOURCE_READERF_ENDOFSTREAM != 0 {
return false
}
if s.sample == nil {
continue
}
if s.currentTimestamp != s.prevTimestamp-1 {
break
}
}
s.prevTimestamp = s.currentTimestamp + 1
s.sample.ConvertToContiguousBuffer(&s.buffer)
s.buffer.Lock(&s.chunk, nil, &s.chunkSz)
// Make a Go slice view of the data for easy consumption.
s.data = unsafe.Slice((*byte)(s.chunk), s.chunkSz)
return true
}
// copyInto copies audio data into p, unlocking the memory once fully copied.
func (s *SourceReader) copyInto(p []byte) int {
n := copy(p, s.data)
s.data = s.data[n:]
if len(s.data) == 0 {
s.buffer.Unlock()
s.buffer.Release()
s.sample.Release()
s.data = nil
}
return n
}
/*
The following contains the minimal set of definitions we need to decode
audio using Media Framework.
Definitions are derived from appropriate SDK headers.
*/
type GUID struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]uint8
}
type HRESULT = uintptr
const (
S_OK = 0x0
MFSTARTUP_LITE = 0x1
MF_VERSION = 0x20070
MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED = 0x20
MF_SOURCE_READERF_ENDOFSTREAM = 0x2
MF_SOURCE_READER_ALL_STREAMS = 0xfffffffe
MF_SOURCE_READER_FIRST_AUDIO_STREAM = 0xfffffffd
)
var (
MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS = GUID{0xa634a91c, 0x822b, 0x41b9, [8]uint8{0xa4, 0x94, 0x4d, 0xe4, 0x64, 0x36, 0x12, 0xb0}}
MF_MT_AUDIO_NUM_CHANNELS = GUID{0x37e48bf5, 0x645e, 0x4c5b, [8]uint8{0x89, 0xde, 0xad, 0xa9, 0xe2, 0x9b, 0x69, 0x6a}}
MF_MT_AUDIO_SAMPLES_PER_SECOND = GUID{0x5faeeae7, 0x0290, 0x4c31, [8]uint8{0x9e, 0x8a, 0xc5, 0x34, 0xf6, 0x8d, 0x9d, 0xba}}
MF_MT_AUDIO_FLOAT_SAMPLES_PER_SECOND = GUID{0xfb3b724a, 0xcfb5, 0x4319, [8]uint8{0xae, 0xfe, 0x6e, 0x42, 0xb2, 0x40, 0x61, 0x32}}
MF_MT_AUDIO_AVG_BYTES_PER_SECOND = GUID{0x1aab75c8, 0xcfef, 0x451c, [8]uint8{0xab, 0x95, 0xac, 0x03, 0x4b, 0x8e, 0x17, 0x31}}
MF_MT_AUDIO_BLOCK_ALIGNMENT = GUID{0x322de230, 0x9eeb, 0x43bd, [8]uint8{0xab, 0x7a, 0xff, 0x41, 0x22, 0x51, 0x54, 0x1d}}
MF_MT_AUDIO_BITS_PER_SAMPLE = GUID{0xf2deb57f, 0x40fa, 0x4764, [8]uint8{0xaa, 0x33, 0xed, 0x4f, 0x2d, 0x1f, 0xf6, 0x69}}
MF_MT_AUDIO_VALID_BITS_PER_SAMPLE = GUID{0xd9bf8d6a, 0x9530, 0x4b7c, [8]uint8{0x9d, 0xdf, 0xff, 0x6f, 0xd5, 0x8b, 0xbd, 0x06}}
MF_MT_AUDIO_SAMPLES_PER_BLOCK = GUID{0xaab15aac, 0xe13a, 0x4995, [8]uint8{0x92, 0x22, 0x50, 0x1e, 0xa1, 0x5c, 0x68, 0x77}}
MF_MT_AUDIO_CHANNEL_MASK = GUID{0x55fb5765, 0x644a, 0x4caf, [8]uint8{0x84, 0x79, 0x93, 0x89, 0x83, 0xbb, 0x15, 0x88}}
MF_MT_MAJOR_TYPE = GUID{0x48eba18e, 0xf8c9, 0x4687, [8]uint8{0xbf, 0x11, 0x0a, 0x74, 0xc9, 0xf9, 0x6a, 0x8f}}
MF_MT_SUBTYPE = GUID{0xf7e34c9a, 0x42e8, 0x4714, [8]uint8{0xb7, 0x4b, 0xcb, 0x29, 0xd7, 0x2c, 0x35, 0xe5}}
MFMediaType_Audio = GUID{0x73647561, 0x0000, 0x0010, [8]uint8{0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71}}
MFAudioFormat_PCM = GUID{0x00000001, 0x0000, 0x0010, [8]uint8{0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}
)
type IMFByteStream struct {
VTable *IMFByteStreamVTable
}
type IMFByteStreamVTable struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetCapabilities uintptr
GetLength uintptr
SetLength uintptr
GetCurrentPosition uintptr
SetCurrentPosition uintptr
IsEndOfStream uintptr
Read uintptr
BeginRead uintptr
EndRead uintptr
Write uintptr
BeginWrite uintptr
EndWrite uintptr
Seek uintptr
Flush uintptr
Close uintptr
}
func (v *IMFByteStream) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
type IStream struct {
VTable *IStreamVTable
}
type IStreamVTable struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
Read uintptr
Write uintptr
Seek uintptr
SetSize uintptr
CopyTo uintptr
Commit uintptr
Revert uintptr
LockRegion uintptr
UnlockRegion uintptr
Stat uintptr
Clone uintptr
}
func (v *IStream) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
type IMFAttributes struct {
VTable *IMFAttributesVTable
}
type IMFAttributesVTable struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetItem uintptr
GetItemType uintptr
CompareItem uintptr
Compare uintptr
GetUINT32 uintptr
GetUINT64 uintptr
GetDouble uintptr
GetGUID uintptr
GetStringLength uintptr
GetString uintptr
GetAllocatedString uintptr
GetBlobSize uintptr
GetBlob uintptr
GetAllocatedBlob uintptr
GetUnknown uintptr
SetItem uintptr
DeleteItem uintptr
DeleteAllItems uintptr
SetUINT32 uintptr
SetUINT64 uintptr
SetDouble uintptr
SetGUID uintptr
SetString uintptr
SetBlob uintptr
SetUnknown uintptr
LockStore uintptr
UnlockStore uintptr
GetCount uintptr
GetItemByIndex uintptr
CopyAllItems uintptr
}
func (v *IMFAttributes) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFAttributes) SetUINT32(guid *GUID, unValue uint32) error {
r, _, _ := syscall.SyscallN(
v.VTable.SetUINT32,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(guid)),
uintptr(unValue),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
type IMFMediaType struct {
VTable *IMFMediaTypeVTable
}
type IMFMediaTypeVTable struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetItem uintptr
GetItemType uintptr
CompareItem uintptr
Compare uintptr
GetUINT32 uintptr
GetUINT64 uintptr
GetDouble uintptr
GetGUID uintptr
GetStringLength uintptr
GetString uintptr
GetAllocatedString uintptr
GetBlobSize uintptr
GetBlob uintptr
GetAllocatedBlob uintptr
GetUnknown uintptr
SetItem uintptr
DeleteItem uintptr
DeleteAllItems uintptr
SetUINT32 uintptr
SetUINT64 uintptr
SetDouble uintptr
SetGUID uintptr
SetString uintptr
SetBlob uintptr
SetUnknown uintptr
LockStore uintptr
UnlockStore uintptr
GetCount uintptr
GetItemByIndex uintptr
CopyAllItems uintptr
GetMajorType uintptr
IsCompressedFormat uintptr
IsEqual uintptr
GetRepresentation uintptr
FreeRepresentation uintptr
}
func (v *IMFMediaType) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFMediaType) SetGUID(guid *GUID, value *GUID) error {
r, _, _ := syscall.SyscallN(
v.VTable.SetGUID,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(guid)),
uintptr(unsafe.Pointer(value)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFMediaType) GetUINT32(guid *GUID, punValue *uint32) error {
r, _, _ := syscall.SyscallN(
v.VTable.GetUINT32,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(guid)),
uintptr(unsafe.Pointer(punValue)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
type IMFSourceReader struct {
VTable *IMFSourceReaderVtbl
}
type IMFSourceReaderVtbl struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetStreamSelection uintptr
SetStreamSelection uintptr
GetNativeMediaType uintptr
GetCurrentMediaType uintptr
SetCurrentMediaType uintptr
SetCurrentPosition uintptr
ReadSample uintptr
Flush uintptr
GetServiceForStream uintptr
GetPresentationAttribute uintptr
}
func (v *IMFSourceReader) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFSourceReader) SetStreamSelection(index int64, selected bool) error {
r, _, _ := syscall.SyscallN(
v.VTable.SetStreamSelection,
uintptr(unsafe.Pointer(v)),
uintptr(index),
uintptr(boolToInt(selected)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFSourceReader) SetCurrentMediaType(index int64, mt *IMFMediaType) error {
r, _, _ := syscall.SyscallN(
v.VTable.SetCurrentMediaType,
uintptr(unsafe.Pointer(v)),
uintptr(index),
uintptr(0),
uintptr(unsafe.Pointer(mt)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFSourceReader) GetCurrentMediaType(index int64, mt **IMFMediaType) error {
r, _, _ := syscall.SyscallN(
v.VTable.GetCurrentMediaType,
uintptr(unsafe.Pointer(v)),
uintptr(index),
uintptr(unsafe.Pointer(mt)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFSourceReader) ReadSample(index, controlFlags int64, actualIndex *int64, streamFlags *int64, timestamp *int64, sample **IMFSample) error {
r, _, _ := syscall.SyscallN(
v.VTable.ReadSample,
uintptr(unsafe.Pointer(v)),
uintptr(index),
uintptr(controlFlags),
uintptr(unsafe.Pointer(actualIndex)),
uintptr(unsafe.Pointer(streamFlags)),
uintptr(unsafe.Pointer(timestamp)),
uintptr(unsafe.Pointer(sample)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
type IMFSample struct {
VTable *IMFSampleVTable
}
type IMFSampleVTable struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetItem uintptr
GetItemType uintptr
CompareItem uintptr
Compare uintptr
GetUINT32 uintptr
GetUINT64 uintptr
GetDouble uintptr
GetGUID uintptr
GetStringLength uintptr
GetString uintptr
GetAllocatedString uintptr
GetBlobSize uintptr
GetBlob uintptr
GetAllocatedBlob uintptr
GetUnknown uintptr
SetItem uintptr
DeleteItem uintptr
DeleteAllItems uintptr
SetUINT32 uintptr
SetUINT64 uintptr
SetDouble uintptr
SetGUID uintptr
SetString uintptr
SetBlob uintptr
SetUnknown uintptr
LockStore uintptr
UnlockStore uintptr
GetCount uintptr
GetItemByIndex uintptr
CopyAllItems uintptr
GetSampleFlags uintptr
SetSampleFlags uintptr
GetSampleTime uintptr
SetSampleTime uintptr
GetSampleDuration uintptr
SetSampleDuration uintptr
GetBufferCount uintptr
GetBufferByIndex uintptr
ConvertToContiguousBuffer uintptr
AddBuffer uintptr
RemoveBufferByIndex uintptr
RemoveAllBuffers uintptr
GetTotalLength uintptr
CopyToBuffer uintptr
}
func (v *IMFSample) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFSample) ConvertToContiguousBuffer(b **IMFMediaBuffer) error {
r, _, _ := syscall.SyscallN(
v.VTable.ConvertToContiguousBuffer,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(b)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
type IMFMediaBuffer struct {
VTable *IMFMediaBufferVTable
}
type IMFMediaBufferVTable struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
Lock uintptr
Unlock uintptr
GetCurrentLength uintptr
SetCurrentLength uintptr
GetMaxLength uintptr
}
func (v *IMFMediaBuffer) Release() error {
if v == nil {
return nil
}
r, _, _ := syscall.SyscallN(
v.VTable.Release,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFMediaBuffer) Lock(buf **byte, length, capacity *int64) error {
r, _, _ := syscall.SyscallN(
v.VTable.Lock,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(buf)),
uintptr(unsafe.Pointer(length)),
uintptr(unsafe.Pointer(capacity)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func (v *IMFMediaBuffer) Unlock() error {
r, _, _ := syscall.SyscallN(
v.VTable.Unlock,
uintptr(unsafe.Pointer(v)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
/*
The following defines exactly the functions required.
*/
var (
_mfplat *windows.DLL
_shlwapi *windows.DLL
_mfreadwrite *windows.DLL
_SHCreateMemStream *windows.Proc
_MFStartup *windows.Proc
_MFShutdown *windows.Proc
_MFCreateMediaType *windows.Proc
_MFCreateAttributes *windows.Proc
_MFCreateMFByteStreamOnStream *windows.Proc
_MFCreateSourceReaderFromByteStream *windows.Proc
)
func MFStartup(version, flags uintptr) (err error) {
_mfplat, err = windows.LoadDLL("Mfplat.dll")
if err != nil {
return fmt.Errorf("Mfplat.dll: %w", err)
}
_shlwapi, err = windows.LoadDLL("Shlwapi.dll")
if err != nil {
return fmt.Errorf("Shlwapi.dll: %w", err)
}
_mfreadwrite, err = windows.LoadDLL("Mfreadwrite.dll")
if err != nil {
return fmt.Errorf("Mfreadwrite.dll: %w", err)
}
_SHCreateMemStream, err = _shlwapi.FindProc("SHCreateMemStream")
if err != nil {
return fmt.Errorf("SHCreateMemStream: %w", err)
}
_MFStartup, err = _mfplat.FindProc("MFStartup")
if err != nil {
return fmt.Errorf("MFStartup: %w", err)
}
_MFShutdown, err = _mfplat.FindProc("MFShutdown")
if err != nil {
return fmt.Errorf("MFShutdown: %w", err)
}
_MFCreateMediaType, err = _mfplat.FindProc("MFCreateMediaType")
if err != nil {
return fmt.Errorf("MFCreateMediaType: %w", err)
}
_MFCreateAttributes, err = _mfplat.FindProc("MFCreateAttributes")
if err != nil {
return fmt.Errorf("MFCreateAttributes: %w", err)
}
_MFCreateMFByteStreamOnStream, err = _mfplat.FindProc("MFCreateMFByteStreamOnStream")
if err != nil {
return fmt.Errorf("MFCreateMFByteStreamOnStream: %w", err)
}
_MFCreateSourceReaderFromByteStream, err = _mfreadwrite.FindProc("MFCreateSourceReaderFromByteStream")
if err != nil {
return fmt.Errorf("MFCreateSourceReaderFromByteStream: %w", err)
}
r, _, _ := _MFStartup.Call(version, flags)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func MFShutdown() error {
r, _, _ := _MFStartup.Call()
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func SHCreateMemStream(pInit *byte, cbInit int) *IStream {
r, _, _ := _SHCreateMemStream.Call(
uintptr(unsafe.Pointer(pInit)),
uintptr(uint32(cbInit)),
)
if r == 0 {
return nil
}
return (*IStream)(unsafe.Pointer(r))
}
func MFCreateAttributes(out **IMFAttributes, size uint64) error {
r, _, _ := _MFCreateAttributes.Call(
uintptr(unsafe.Pointer(out)),
uintptr(size),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func MFCreateMFByteStreamOnStream(s *IStream, out **IMFByteStream) error {
r, _, _ := _MFCreateMFByteStreamOnStream.Call(
uintptr(unsafe.Pointer(s)),
uintptr(unsafe.Pointer(out)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func MFCreateSourceReaderFromByteStream(bs *IMFByteStream, attributes *IMFAttributes, out **IMFSourceReader) error {
r, _, _ := _MFCreateSourceReaderFromByteStream.Call(
uintptr(unsafe.Pointer(bs)),
uintptr(unsafe.Pointer(attributes)),
uintptr(unsafe.Pointer(out)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func MFCreateMediaType(out **IMFMediaType) error {
r, _, _ := _MFCreateMediaType.Call(
uintptr(unsafe.Pointer(out)),
)
if r != S_OK {
return MFErr{Code: r}
}
return nil
}
func boolToInt(b bool) int {
const False = 0
const True = 1
if b {
return True
}
return False
}
// MFErr is a Media Foundation error that can render a formatted message.
type MFErr struct {
Code HRESULT
}
func (e MFErr) Error() string {
out := make([]uint16, 300)
size, err := windows.FormatMessage(
windows.FORMAT_MESSAGE_FROM_SYSTEM|windows.FORMAT_MESSAGE_FROM_HMODULE|windows.FORMAT_MESSAGE_ARGUMENT_ARRAY,
uintptr(_mfplat.Handle),
uint32(e.Code),
0,
out,
nil,
)
if err != nil {
return fmt.Sprintf("code %x (<format message: %v>)", e.Code, err.Error())
}
// trim terminating \r and \n
for ; size > 0 && (out[size-1] == '\n' || out[size-1] == '\r'); size-- {
}
return fmt.Sprintf("%s (code %x)", string(utf16.Decode(out[:size])), e.Code)
}