Tips

Red5 フリーソフトのFlashストリーミングサーバ ~開発クライアントの構築 その4~

Red5 フリーソフトのFlashストリーミングサーバ

~開発クライアントの構築 その4~

Red5用のプロジェクト(3)

(1)「oflaDemo2」の構成

今回は前回の続きでサンプル「oflaDemo2」の①に関するファイルを説明、および変更箇所について掲載していきます。
念のため、構成を、掲載しておきます。

①の各ファイルは「red5-web.xml」で読み込まれているアプリケーションファイルとなります。

  • 「demoservice.js」
  • 「demoservice.py」
  • 「demoservice.rb」
  • 「main.groovy」
  • 「main.js」
  • 「main.py」
  • 「main.rb」

今回説明するアプリケーションファイルは、主にred5アプリケーションを機能させるうえで必要なファイルとなります。
では、実際に、編集箇所を見ていきたいと思います。

(2)「demoservice.js」の編集

「demoservice.js」は、red5アプリケーションの「oflaDemo2」
(元は、サンプル「oflaDemo」)を動作するためのサービス用のクラスファイルとなります。
主に、動画再生用のクラスとなっています。
ファイルはjavascriptで構成されています。
変更する箇所は、38行目となります。

  • 変更前
  • 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
    /*
     * demoservice.js - a translation into JavaScript of the ofla demo DemoService class, a Red5 example.
     *
     * @author Paul Gregoire
     */
     
    importPackage(Packages.org.red5.server.api);
    importPackage(Packages.org.springframework.core.io);
    importPackage(Packages.org.apache.commons.logging);
     
    importClass(java.io.File);
    importClass(java.util.HashMap);
    importClass(java.text.SimpleDateFormat);
    importClass(Packages.org.springframework.core.io.Resource);
    importClass(Packages.org.red5.server.api.Red5);
     
    function DemoService() {
        this.className = 'DemoService';
        log.debug('DemoService init');
     
        if (supa) {
            DemoService.prototype = supa;
        }
     
        getListOfAvailableFLVs = function() {
            log.debug('getListOfAvailableFLVs');
            log.debug('Con local: ' + Red5.getConnectionLocal());
            var scope = Red5.getConnectionLocal().getScope();
            log.debug('Scope: ' + scope);
            var filesMap = new HashMap(3);
            var fileInfo;
            try {
                print('Getting the FLV files');
                //var flvs = scope.getResources("streams/*.flv"); //Resource[]
                var serverRoot = java.lang.System.getProperty('red5.root');
                log.debug('Red5 root: ' + serverRoot);
                var streamsDir = new File(serverRoot + '/webapps/oflaDemo/streams/');
                var flvs = streamsDir.listFiles();
                log.debug('Flvs: ' + flvs);
                log.debug('Number of flvs: ' + flvs.length);
                for (var i=0;i<flvs.length;i++) {
                    var file = flvs[i];
                    log.debug('file: ' + file);
                    log.debug('java.io.File type: ' + (file == typeof(java.io.File)));
                    log.debug('js type: ' + typeof(file));
                    log.debug('file path: ' + file.path);
                    log.debug('file url: ' + file.URL);
                    //var fso = new File(serverRoot + '/webapps/oflaDemo' + file.path);
                    var fso = file;
                    var flvName = fso.getName();
                    if (flvName.indexOf('.flv') < 1) {
                        continue;
                    }
                    log.debug('flvName: ' + flvName);
                    log.debug('exist: ' + fso.exists());
                    log.debug('readable: ' + fso.canRead());
                    //loop thru props
                    var flvBytes = 0;
                    if ('length' in fso) {
                        flvBytes = fso.length();
                    } else {
                        log.warn('Length not found');
                    }
                    log.debug('flvBytes: ' + flvBytes);
                    var lastMod = '0';
                    if ('lastModified' in fso) {
                        lastMod = this.formatDate(new java.util.Date(fso.lastModified()));
                    } else {
                        log.debug('Last modified not found');
                    }
     
                    print('FLV Name: ' + flvName);
                    print('Last modified date: ' + lastMod);
                    print('Size: ' + flvBytes);
                    print('-------');
     
                    fileInfo = new HashMap(3);
                    fileInfo.put("name", flvName);
                    fileInfo.put("lastModified", lastMod);
                    fileInfo.put("size", flvBytes);
                    filesMap.put(flvName, fileInfo);
                }
            } catch (e) {
                log.warn('Error in getListOfAvailableFLVs: ' + e);
            }
            return filesMap;
        };
     
        formatDate = function(date) {
            //java 'thread-safe' date formatting
            return new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(date);
        };
     
        toString = function(string) {
            return 'Javascript:DemoService';
        };
     
        doesNotUnderstand = function(name) {
            print("Unknown method called: " + name + "n");
            for (n in context){
                print('Context: '+n);
            }
            if (name in this.__proto__) {
                if (arguments.length > 0) {
                    return this.__proto__[name](arguments);
                } else {
                    return this.__proto__[name]();
                }
            }
        };
     
    }
     
    DemoService.__has__ = function(name) {
        println('Has: '+name);
        return true;
    };
     
    DemoService.__get__ = function(name) {
        println('Get: '+name);
        if (name in this) {
            return this[name];
        } else if (typeof(this['doesNotUnderstand']) == 'function') {
            return function() {
                return this.doesNotUnderstand(name, arguments);
            }
        } else {
            return undefined;
        }
    };
     
    DemoService();
  • 変更後
  • 33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    try {
        print('Getting the FLV files');
        //var flvs = scope.getResources("streams/*.flv"); //Resource[]
        var serverRoot = java.lang.System.getProperty('red5.root');
        log.debug('Red5 root: ' + serverRoot);
        var streamsDir = new File(serverRoot + '/webapps/oflaDemo2/streams/');
        var flvs = streamsDir.listFiles();
        log.debug('Flvs: ' + flvs);
        log.debug('Number of flvs: ' + flvs.length);
        for (var i=0;i<flvs.length;i++) {

次に「demoservice.py」について見ていきます。

Linux認定資格 LPICを取るなら・・

Linux資格 「LPIC Lv1」徹底解説 連載目次

Recent News

Recent Tips

Tag Search