2010年3月11日木曜日

"lsyncd"で双方向の同期

スズキです。

"rsync"と"lsyncd"を用いた一方向の同期は確認したので、
http://blog.suz-lab.com/2010/03/centoslsyncd.html
今度は、双方向の同期ができるかの確認です。

つまり、「サーバA」で"lsyncd"、「サーバB」で"rsync"、
そして、「サーバA」から「サーバB」に対しての同期ができたので、
さらに、「サーバA」に"rsync"、「サーバB」に"lsyncd"、を立ち上げ、
どちらのサーバでファイルの更新などをしても、もう一方に反映するか、
の確認です。

で、結論としては、できました!

やり方は、こちらと、
http://blog.suz-lab.com/2010/03/centoslsyncd.html
こちらの手順を、
http://blog.suz-lab.com/2010/03/centosrsync.html
サーバを逆にした状態でも設定する感じです。

状況によっては、負荷分散された複数のWebサーバに対する
ファイルアップロードなどに有効かもしれません。

とりあえず、今、NFS使ってるところは、置きかえられるかな?

--------
http://www.suz-lab.com

CentOSで"lsyncd"を使った同期

スズキです。

"rsync"の準備もできたので、いよいよ、"lsyncd"です。
http://blog.suz-lab.com/2010/03/centosrsync.html

"lsyncd"とは、Linuxカーネル2.6.13で利用できるようになった、
"inotify"と呼ばれるファイル・システム・イベント監視機能を利用し、
ファイルの更新などにもとづき、該当ファイルを同期先に反映してくれるコマンドです。
http://code.google.com/p/lsyncd/

幸い、CentOSでは"rpmforge"のリポジトリからインストールできます。
# yum install lsyncd

で、同期先サーバで"rsync"がデーモンモードで稼動していれば、
以下のようなコマンドで、対象ディレクトリを監視モードにできます。

# lsyncd /tmp/test/ 172.21.10.152::test
※ 第1引数は同期元ローカルディレクトリで、第2引数は同期先(rsync)です

この状態で、同期元ディレクトでファイルの更新や作成をすると、
このタイミングで、同期先にも、同様のものが反映されます。

双方向はいけるのかなー?(無限ループ?)

--------
http://www.suz-lab.com

CentOSで"rsync"のデーモンモード

スズキです。

CentOSで"rsync"をデーモンモードで使う方法です。

まずは、必要なパッケージをインストールします。

# yum install xinetd
# yum install rsync

"rsync"のデーモンモードは、"xinetd"経由で利用する形になっています。

次に、対象ファイルの作成です。

# mkdir /tmp/test
# chmod 777 /tmp/test

そして、設定ファイル("/etc/rsyncd.conf")を以下のように作成します。

--------【rsyncd.conf】--------
[test]
path = /tmp/test
hosts allow = 172.21.10.0/24
read only = false
--------

この状態で、"xinetd"を起動すると、"rsync"がデーモンモードで利用できるようになります。

# chkconfig rsync on
# /etc/init.d/xinetd start

最後に適当なサーバから、上記のサーバに対して以下のようなコマンドで、
rsyncコマンドで"rsync"のデーモンモードを利用したディレクトリ同期が確認できます。

# rsync -avx /tmp/test/ 172.21.10.152::test

これで"lsyncd"の準備も完了です。

--------
http://www.suz-lab.com

仮想インターフェース(IPエイリアス)の作成(Linux)の続き

スズキです。

以前、仮想インターフェースについたのですが、
http://blog.suz-lab.com/2010/03/iplinux.html

この設定だと、外部につながらなくなってしまいました...
("ping www.yahoo.com"などの反応がなくなってしまいます)

ですので、やっぱり、ネットマスクとブロードキャストを指定して、再設定です。

# ifconfig eth0:0 10.32.10.150 netmask 255.255.255.0 broadcast 10.32.10.255

今度は"ping www.yahoo.com"もOKです。

これで、yumが使えるようになった...

--------
http://www.suz-lab.com

2010年3月10日水曜日

GStreamerで動画のタグ取得(FFmpeg連携)

スズキです。

MP3のタグ情報取得に続き、
http://blog.suz-lab.com/2010/03/gstreamermp3.html

今度は動画も試してみました。

MP4はこんな感じでです。

# gst-launch filesrc location=input.mp4 ! decodebin ! fakesink -t
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
FOUND TAG : found by element "qtdemux0".
  video codec: H.264 / AVC
FOUND TAG : found by element "qtdemux0".
  audio codec: MPEG-4 AAC audio
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Got EOS from element "pipeline0".
Execution ended after 724906045 ns.
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
FREEING pipeline ...

調子に乗って、MTSも試してみましたが、

# gst-launch filesrc location=input.mts ! decodebin ! fakesink -t
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
ERROR: from element /pipeline0/decodebin0: A MPEG-2 Transport Stream
demuxer plugin is required to play this stream, but not installed.
Additional debug info:
gstdecodebin.c(845): close_pad_link (): /pipeline0/decodebin0:
No decoder to handle media type 'video/mpegts'
ERROR: pipeline doesn't want to preroll.
Setting pipeline to NULL ...
FREEING pipeline ...

こちらはエラーになってしまいました。

しかし、下記でインストールしたことを思いだし、
# yum install gstreamer-ffmpeg

以下のFFmpeg関連のドキュメントを見ながら試行錯誤したところ、
http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-ffmpeg-plugins/html/

以下のように、MTSも成功しました。

# gst-launch filesrc location=input.mts ! ffdemux_mpegts ! fakesink -t
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
FOUND TAG : found by element "ffdemux_mpegts0".
  video codec: H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
FOUND TAG : found by element "ffdemux_mpegts0".
  audio codec: ATSC A/52A (AC-3)
FOUND TAG : found by element "ffdemux_mpegts0".
  audio codec: ATSC A/52A (AC-3)
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Got EOS from element "pipeline0".
Execution ended after 77135625 ns.
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
FREEING pipeline ...

そんなこんなで、GStreamerの感触はつかめてきた感じです。

本命はGStreamer使ったPerlでの音合成なんだけど...

--------
http://www.suz-lab.com

GStreamerでタグ情報(MP3)の取得

スズキです。

GStreamerはマルチメディアフレームワークと呼ばれているもので、
音声や動画に対して、さまざまな処理ができるものです。
http://gstreamer.freedesktop.org/

CentOSのyumリポジトリにもあったので、下記のようにインストールしてみました。
(よくわかってないので、それっぽいもの、すべてインストールです)

# yum install gstreamer
# yum install gstreamer-tools
# yum install gstreamer-ffmpeg
# yum install gstreamer-plugins-base
# yum install gstreamer-plugins-good
# yum install gstreamer-plugins-ugly
# yum install gstreamer-plugins-bad

で、早速、利用例の一つである、MP3のタグデータを取得してみました。

# gst-launch filesrc location=test.mp3 ! id3demux ! fakesink -t
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
FOUND TAG : found by element "id3demux0".
  title: XXXXXXXX
  artist: XXXXXXXX
  album: XXXXXXXX
  date: 2009-01-01
  comment: XXXXXXXX
  track number: 1
  genre: XXXXXXXX
  ISRC: XXXXXXXX
  track count: 4
  discid: XXXXXXXX
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Got EOS from element "pipeline0".
Execution ended after 36584697 ns.
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
FREEING pipeline ...

標準出力に、MP3のタグ情報が表示されているのがわかります。

実践投入は、まだまだ先だなー...

--------
http://www.suz-lab.com

NSTimerで定期的にUIImageViewの画像を入れ替える(iPhone)

スズキです。

iPhoneで、下記100個の画像ファイルを0.1秒おきに差し替えるやり方です。

image_00000.jpg
image_00001.jpg
...
image_00099.jpg

実装方法は、NSTimerを利用して0.1秒ごとに、
UIImageViewのimageを差し替える感じです。

主要クラスのリファレンスは次の通りです。
【NSTimer】
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
【UIImageView】
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html
【UIImage】
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html

最終的なコードは下記のようになりました。

--------【Objective-C】--------
#import "Test02ViewController.h"
@implementation Test02ViewController
@synthesize imageView;
NSTimer *timer = nil;
int sequence = 0;

- (void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"start");
  [NSTimer
    scheduledTimerWithTimeInterval:0.1
    target:self
    selector:@selector(onTimer:)
    userInfo:nil
    repeats:YES];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}


- (void)dealloc {
  [super dealloc];
}

- (void)onTimer:(NSTimer *)timer {
  NSString *imageName = [NSString stringWithFormat: @"image_%05d", sequence];
  NSString *imagePath = [
    [NSBundle mainBundle] pathForResource:imageName
    ofType:@"jpg"
  ];
  UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
  imageView.image = image;
  [image release];
  sequence++;
  if (sequence > 100 && [timer isValid]) {
    [timer invalidate];
    NSLog(@"stop");
  }
}

@end
--------

メモリ管理系は、結構、理解してきたなー…

--------
http://www.suz-lab.com