新增VLC錄影API

Just4life發表於2014-05-13
最近使用VLC播放RTSP資料想在本地截圖錄影,但libvlc中並不包含錄影api,網上找到一些資料,自己新增這個介面並測試成功。介面主要是按照官方網站來做的(https://patches.videolan.org/patch/606/)。我是用的原始碼是最新的,編譯過程中很順利,前提是安裝好各種依賴包。測試程式碼如下:
#include <vlc/vlc.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>

static const char * test_defaults_args[] = {
    "-v",
    "--ignore-config",
    "-I",
    "dummy",
    "--no-media-library"
};

static const int test_defaults_nargs =
    sizeof (test_defaults_args) / sizeof (test_defaults_args[0]);

int main (void)
{

    libvlc_instance_t *instance;
    libvlc_media_t *media;
    libvlc_media_player_t *player;
    const char * file = "rtsp://192.168.1.68";

    //instance = libvlc_new (test_defaults_nargs, test_defaults_args);
    instance = libvlc_new (0, NULL);
    assert (instance != NULL);

    //開啟檔案
    //media = libvlc_media_new_path (instance, file);
    //開啟串流
    media = libvlc_media_new_location (instance, file);
    assert (media != NULL);

    player = libvlc_media_player_new_from_media (media);
    assert (player != NULL);

    libvlc_media_release (media);

    libvlc_media_player_play (player);
    printf("play\n");
    libvlc_media_player_record_start(player, "testfile");
    printf("record\n");

    sleep(10*10);

    libvlc_video_take_snapshot(player,0,"test.jpg",0,0);

    printf("%d\n", libvlc_media_player_get_state(player));
    printf("stop record\n");
    libvlc_media_player_record_stop(player);
    printf("stop play\n");
    //libvlc_media_player_stop (player);
    printf("player release\n");
    libvlc_media_player_release (player);
    printf("release\n");
    libvlc_release (instance);
    return 0;
}

很驚訝選項裡面竟然沒有C...。

編譯:gcc test.c -o test -lvlc

執行之後截圖錄影均正常。這裡libvlc_media_player_stop()登出是因為有些情況下這個函式會造成死鎖,這個涉及到執行緒安全的一些問題,以後再研究。

相關文章