summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/media/media-file.js
blob: 365ea8f2ee8e0ff3d0a24fef49722ca57e845380 (plain)
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
var audioCodecs = [
    ["audio/wav", "wav"],
    ["audio/aac", "m4a"],
    ["audio/ogg", "oga"]
];

var videoCodecs = [
    ["video/mp4", "mp4"],
    ["video/ogg", "ogv"]
];

function findMediaFile(tagName, name) {
    var codecs;
    if (tagName == "audio")
        codecs = audioCodecs;
    else
        codecs = videoCodecs;

    var element = document.getElementsByTagName(tagName)[0];
    if (!element)
        element = document.createElement(tagName);

    for (var i = 0; i < codecs.length; ++i) {
        if (element.canPlayType(codecs[i][0]))
            return name + "." + codecs[i][1];
    }

    return "";
}

function mimeTypeForExtension(extension) {
    for (var i = 0; i < videoCodecs.length; ++i) {
        if (extension == videoCodecs[i][1])
            return videoCodecs[i][0];
    }
    for (var i = 0; i < audioCodecs.length; ++i) {
        if (extension == audioCodecs[i][1])
            return audioCodecs[i][0];
    }

    return "";
}

function mimeTypeForFile(filename) {
 var lastPeriodIndex = filename.lastIndexOf(".");
  if (lastPeriodIndex > 0)
    return mimeTypeForExtension(filename.substring(lastPeriodIndex + 1));

  return "";
}

function setSrcByTagName(tagName, src) {
    var elements = document.getElementsByTagName(tagName);
    if (elements) {
        for (var i = 0; i < elements.length; ++i)
            elements[i].src = src;
    }
}

function setSrcById(id, src) {
    var element = document.getElementById(id);
    if (element)
        element.src = src;
}

function stripExtension(filename) {
  var lastPeriodIndex = filename.lastIndexOf(".");
  if (lastPeriodIndex > 0)
    return filename.substring(0, lastPeriodIndex);
  return filename;
}