• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

benrr101 / node-taglib-sharp / 46462135

pending completion
46462135

push

appveyor

Benjamin Russell
Merge branch 'release/v5.1.0'

3096 of 3788 branches covered (81.73%)

Branch coverage included in aggregate %.

2171 of 2171 new or added lines in 47 files covered. (100.0%)

25320 of 26463 relevant lines covered (95.68%)

437.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.55
/src/ogg/codecs/codecFactory.ts
1
import IOggCodec from "./iOggCodec";
2
import Opus from "./opus";
1✔
3
import Theora from "./theora";
1✔
4
import Vorbis from "./vorbis";
1✔
5
import {ByteVector} from "../../byteVector";
6
import {UnsupportedFormatError} from "../../errors";
1✔
7
import {Guards} from "../../utils";
1✔
8

9
/**
10
 * Type shortcut for a method that can generate a codec object based on the first packet of the
11
 * bit stream.
12
 * @param firstPacket First packet in the bit stream
13
 * @returns
14
 *     Generated {@link IOggCodec} is returned if a codec could be
15
 *     constructed from the first packet, otherwise `undefined` is returned.
16
 */
17
export type CodecProvider = (firstPacket: ByteVector) => IOggCodec | undefined;
18

19
/**
20
 * Factory for creating codecs from the first packet of the Ogg bitstream.
21
 * @remarks
22
 *     By default, only codecs provided by the library will be matched. However, custom codec
23
 *     support can be added by using {@link addCodecProvider}.
24
 */
25
export default class CodecFactory {
1✔
26
    private static _customCodecProviders: CodecProvider[] = [];
1✔
27

28
    /**
29
     * Adds a custom codec provider to try before using standard codec creation methods.
30
     * Codec providers are used before standard methods so custom checking can be used and new
31
     * formats can be added. They are executed in reverse order in which they are added.
32
     * @param provider Codec provider function
33
     *     * firstPacket: ByteVector First packet of the bitstream
34
     *     * returns IOggCodec if method was able to match the packet, falsy otherwise
35
     */
36
    public static addCodecProvider(provider: CodecProvider): void {
37
        Guards.truthy(provider, "provider");
4✔
38
        CodecFactory._customCodecProviders.push(provider);
2✔
39
    }
40

41
    /**
42
     * Clears the custom providers from the factory.
43
     */
44
    public static clearCustomProviders(): void {
45
        CodecFactory._customCodecProviders = [];
1✔
46
    }
47

48
    /**
49
     * Determines the correc codec to use for a stream header packet.
50
     * @param packet First packet of an Ogg logical bitstream.
51
     */
52
    public static getCodec(packet: ByteVector): IOggCodec {
53
        Guards.truthy(packet, "packet");
7✔
54

55
        // Check custom providers first
56
        let codec: IOggCodec;
57
        for (const provider of CodecFactory._customCodecProviders) {
5✔
58
            codec = provider(packet);
2✔
59
        }
60
        if (codec) {
5✔
61
            return codec;
1✔
62
        }
63

64
        // Try the known codecs
65
        if (Vorbis.isHeaderPacket(packet)) {
4✔
66
            // Vorbis
67
            codec = new Vorbis(packet);
1✔
68
        } else if (Theora.isHeaderPacket(packet)) {
2✔
69
            // Theora
70
            codec = new Theora(packet);
1✔
71
        } else if (Opus.isHeaderPacket(packet)) {
1!
72
            // Opus
73
            codec = new Opus(packet);
1✔
74
        } else {
75
            throw new UnsupportedFormatError("Unknown Ogg codec");
×
76
        }
77

78
        return codec;
3✔
79
    }
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc