網頁

2016年4月14日 星期四

VLC 之 VLCLIB 新增錄影功能

以下參考 https://patches.videolan.org/patch/606/

vi include/vlc/libvlc_events.h
enum libvlc_event_e {
    libvlc_MediaPlayerMediaChanged=0x100,
    libvlc_MediaPlayerRecordableChanged,
    libvlc_MediaPlayerRecordingFinished,

    libvlc_MediaListItemAdded=0x200,
};
typedef struct libvlc_event_t
{
    union
    {
        struct
        {
            int new_recordable;
        } media_player_recordable_changed;
        struct
        {
            char *psz_filename;
        } media_player_recording_finished;
    } u;
} libvlc_event_t;

vi include/vlc/libvlc_media_player.h
/** @} audio */

/**
 * Can the media player record the current media?
 *
 * Media must be buffering or playing before it can be recorded.
 *
 * The media player event manager will emit a libvlc_MediaPlayerRecordableChanged event
 * when the recordable state changes after starting media playback. The event data will
 * describe the new recordable state, so invocation of this API method is not strictly
 * necessary to determine when recording can be started.
 *
 * A libvlc_MediaPlayerRecordableChanged event will not be emitted if the media is
 * stopped (notified by a libvlc_MediaPlayerStoppedEvent) or finishes normally (notified
 * by a libvlc_MediaPlayerFinished event).
 *
 * A calling application should therefore register an event callback for those events
 * so that it may query the new recordable state and manage recording at the appropriate
 * time.
 *
 * \param p_mi media player
 * \return true if the media player can record, false if it can not
 * \version LibVLC 2.1.0 or later
 */
LIBVLC_API bool libvlc_media_player_is_recordable( libvlc_media_player_t *p_mi );

/**
 * Is the current media being recorded?
 *
 * \param p_mi media player
 * \return true if recording, false if not
 * \version LibVLC 2.1.0 or later
 */
LIBVLC_API bool libvlc_media_player_is_recording( libvlc_media_player_t *p_mi );

/**
 * Start recording the current media.
 *
 * Media must be buffering or playing before it can be recorded. A calling application
 * can begin recording immediately on receipt of a libvlc_MediaPlayerRecordableChanged
 * event sent via the media player event manager (if recording is possible for the
 * currently playing media), and any time thereafter until the media stops.
 *
 * Media will be saved to the file path denoted by the psz_filename parameter if it is
 * supplied. Any such supplied filename should not include a file extension as the
 * correct file extension will automatically be appended when the file is created. This
 * filename may denote a full path name, but each directory in the path must already
 * exist or recording will silently fail. If the calling application chooses to specify
 * the filename then it is the responsibility of that application to take account of
 * this and itself make sure any needed directories are created.
 *
 * Alternatively, a calling application need not supply a filename and so instead let
 * vlc automatically generate a unique filename. This will cause vlc to create a new
 * file in the appropriate media directory for the user - for example "~/Videos". The
 * actual filename used will be sent in an event when the recording is complete.
 *
 * When recording has finished and the new file has been completely saved, a
 * libvlc_MediaPlayerRecordingFinished event will be sent via the media player event
 * manager. The event data will contain the filename of the newly recorded file - this
 * will either be the filename as specified by the calling application or a filename
 * generated by vlc if the application did not supply a filename. In either case, this
 * filename will include the automatically appended file extension.
 *
 * The saved media file will not be immediately available or visible until recording
 * has completely finished and the libvlc_MediaPlayerRecordingFinished event has been
 * received, or the media has stopped or finished normally.
 *
 * Recording can be stopped and started on-the-fly once the recordable state is set;
 * each time recording is stopped and restarted a new file will be created so a calling
 * application should take care to provide unique filenames, or defer to vlc to create
 * unique filenames.
 *
 * Recording will be stopped when the media stops playing, and must be explicitly
 * started again to restart recording, i.e. the recording state is not automatically
 * preserved when playing media subsequently.
 *
 * Media player functionailty such as next/previous chapter, set time or position and
 * so on are ineffective when recording is enabled. However, pausing the media is
 * possible and will pause the recording; unpausing the media will resume playback and
 * recording.
 *
 * Recording of the primary media or sub-items is possible.
 *
 * \param p_mi media player
 * \param psz_filename name of the file to save the media to, not including any file extension,
 *                     or NULL if vlc should generate the filename automatically
 * \return 0 if recording was started, -1 on error
 * \version LibVLC 2.1.0 or later
 */
LIBVLC_API int libvlc_media_player_record_start( libvlc_media_player_t *p_mi, const char *psz_filename );

/**
 * Stop recording the current media.
 *
 * This method requests that the recording stop, and will return immediately. Recording
 * will not stop immediately.
 *
 * When the recording actually stops some short time later and the new file has
 * finished being written, a libvlc_MediaPlayerRecordingFinished event will be sent via
 * the media player event manager. The newly recorded file will not be visible or
 * available until after this event has been sent.
 *
 * The event data will contain the full name of the file that was created. The filename
 * will either be that as was specified by the calling application on invoking
 * libvlc_media_player_record_start(), or the filename that vlc automatically generated
 * if the calling application did not supply its own filename. In either case the
 * filename will contain the automatically appended file extension.
 *
 * There is no need to invoke this method to stop the recording if the media is stopped
 * or finishes playing normally.
 *
 * \param p_mi media player
 * \return 0 if recording was stopped, -1 on error
 * \version LibVLC 2.1.0 or later
 */
LIBVLC_API int libvlc_media_player_record_stop( libvlc_media_player_t *p_mi );

/**
/** @} media_player */

vi lib/event.c
static const event_name_t event_list[] = {
    DEF(MediaPlayerAudioDevice)
    DEF(MediaPlayerRecordableChanged)
    DEF(MediaPlayerRecordingFinished)

vi lib/libvlc.sym
libvlc_media_player_has_vout
libvlc_media_player_is_recordable
libvlc_media_player_is_recording
libvlc_media_player_is_seekable

libvlc_media_player_previous_chapter
libvlc_media_player_record_start
libvlc_media_player_record_stop
libvlc_media_player_release

vi lib/media_player.c
static int
input_recordable_changed( vlc_object_t *p_this, char const *psz_cmd,
                          vlc_value_t oldval, vlc_value_t newval,
                          void *p_userdata );
static int
file_recording_finished( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
static void release_input_thread( libvlc_media_player_t *p_mi )
{
    var_DelCallback( p_input_thread, "can-record",
                     input_recordable_changed, p_mi );
}
static int
input_recordable_changed( vlc_object_t *p_this, char const *psz_cmd,
                          vlc_value_t oldval, vlc_value_t newval,
                          void *p_userdata )
{
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval);

    libvlc_media_player_t *p_mi = p_userdata;
    libvlc_event_t event;

    event.type = libvlc_MediaPlayerRecordableChanged;
    event.u.media_player_recordable_changed.new_recordable = newval.b_bool;

    libvlc_event_send( p_mi->p_event_manager, &event );
    return VLC_SUCCESS;
}
static int file_recording_finished(vlc_object_t *p_this, char const *psz_cmd,
                                   vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    VLC_UNUSED(p_this);
    VLC_UNUSED(psz_cmd);
    VLC_UNUSED(oldval);

    libvlc_media_player_t *p_mi = p_data;
    libvlc_event_t event;

    event.type = libvlc_MediaPlayerRecordingFinished;
    event.u.media_player_recording_finished.psz_filename = newval.psz_string;

    libvlc_event_send(p_mi->p_event_manager, &event);
    return VLC_SUCCESS;
}
libvlc_media_player_new( libvlc_instance_t *instance )
{
    var_Create (mp, "recording-finished", VLC_VAR_STRING);
    var_AddCallback (mp, "recording-finished", file_recording_finished, mp);
}
static void libvlc_media_player_destroy( libvlc_media_player_t *p_mi )
{
    var_DelCallback( p_mi, "recording-finished", file_recording_finished, p_mi );
}
int libvlc_media_player_play( libvlc_media_player_t *p_mi )
{
    var_AddCallback( p_input_thread, "can-record", input_recordable_changed, p_mi );
    if( input_Start( p_input_thread ) )
    {
        var_DelCallback( p_input_thread, "can-record", input_recordable_changed, p_mi );
    }
}
bool libvlc_media_player_is_recordable( libvlc_media_player_t *p_mi )
{
    input_thread_t *p_input_thread;
    bool b_can_record;

    p_input_thread = libvlc_get_input_thread( p_mi );
    if( !p_input_thread )
        return false;

    b_can_record = var_GetBool( p_input_thread, "can-record" );

    vlc_object_release( p_input_thread );
    return b_can_record;
}
bool libvlc_media_player_is_recording( libvlc_media_player_t *p_mi )
{
    input_thread_t *p_input_thread;
    bool b_record;

    p_input_thread = libvlc_get_input_thread( p_mi );
    if( !p_input_thread )
        return false;

    b_record = var_GetBool( p_input_thread, "record" );

    vlc_object_release( p_input_thread );
    return b_record;
}
int libvlc_media_player_record_start( libvlc_media_player_t *p_mi, const char* psz_filename )
{
    input_thread_t *p_input_thread;

    p_input_thread = libvlc_get_input_thread( p_mi );
    if( !p_input_thread )
        return -1;

    var_SetString( p_input_thread, "input-record-path", psz_filename );
    var_SetBool( p_input_thread, "record", true );

    vlc_object_release( p_input_thread );
    return 0;
}
int libvlc_media_player_record_stop( libvlc_media_player_t *p_mi )
{
    input_thread_t *p_input_thread;

    p_input_thread = libvlc_get_input_thread( p_mi );
    if( !p_input_thread )
        return -1;

    var_SetBool( p_input_thread, "record", false );

    vlc_object_release( p_input_thread );
    return 0;
}

vi modules/stream_out/record.c
struct sout_stream_sys_t
{
    char *psz_record_file;
};
static int Open( vlc_object_t *p_this )
{
    p_sys->psz_record_file = NULL;

    return VLC_SUCCESS;
}
static void Close( vlc_object_t * p_this )
{
    if( p_sys->psz_record_file ) {
        for( vlc_object_t *p_mp = p_stream->p_parent; p_mp; p_mp = p_mp->p_parent )
        {
            if( var_Type( p_mp, "recording-finished" ) )
            {
                var_SetString( p_mp, "recording-finished", p_sys->psz_record_file );
                break;
            }
        }

        free( p_sys->psz_record_file );
    }
    TAB_CLEAN( p_sys->i_id, p_sys->id );
    free( p_sys->psz_prefix );
    free( p_sys );
}
static int OutputNew( sout_stream_t *p_stream,
                      const char *psz_muxer, const char *psz_prefix, const char *psz_extension  )
{
    if( psz_file && psz_extension )
    {
        p_sys->psz_record_file = strdup( psz_file );
        var_SetString( p_stream->p_libvlc, "record-file", psz_file );
    }
}

vi src/input/var.c 
void input_ControlVarInit ( input_thread_t *p_input )
{
    /* ES Out */
    var_Create( p_input, "input-record-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
}

2016年4月11日 星期一

VLC 延遲 錄影 抓圖

延遲主串流到新串流
vlc.exe --sout=#rtp{sdp=rtsp://:8554/ch1} --sout-all --sout-keep rtsp://169.254.1.168:554/live2.sdp --network-caching=5000 --intf telnet --telnet-host="0.0.0.0" --telnet-port="4212" --telnet-password="1234" --verbose=2 --file-logging --logfile=vlc-log-4212.txt

抓圖
vlc.exe rtsp://localhost:8554/ch1 --vout dummy --intf telnet --telnet-host="0.0.0.0" --telnet-port="4213" --telnet-password="1234" --verbose=2 --file-logging --logfile=vlc-log-4213.txt
telnet localhost 4213
snapshot
shutdown

錄影
vlc.exe --intf telnet --telnet-host="0.0.0.0" --telnet-port="4214" --telnet-password="1234" --verbose=2 --file-logging --logfile=vlc-log-4214.txt
telnet localhost 4214
new ch1 broadcast enabled
setup ch1 input rtsp://localhost:8554/ch1
setup ch1 output #file{mux=ps,dst='R:\temp\aaa.ps'}
control ch1 play
control ch1 stop
shutdown

延遲主串流,可直接抓圖
vlc.exe rtsp://169.254.1.168:554/live2.sdp --network-caching=5000 --vout dummy --intf telnet --telnet-host="0.0.0.0" --telnet-port="4212" --telnet-password="1234" --verbose=2 --file-logging --logfile=vlc-log.txt
telnet localhost 4212
snapshot
shutdown

2016年4月8日 星期五

在 VLC 的 log 上增加時間

vi modules/logger/file.c

static void LogText(void *opaque, int type, const vlc_log_t *meta,
                    const char *format, va_list ap)

    struct timespec ts;
    struct tm curtime;
    char tmBuf[128];
    timespec_get(&ts, TIME_UTC);
    if (localtime_r(&ts.tv_sec, &curtime) == NULL) {
        gmtime_r(&ts.tv_sec, &curtime);
    }
    if (strftime(tmBuf, sizeof(tmBuf), "%M%S", &curtime) == 0) {
        strcpy(tmBuf, "no time");
    }
    fprintf(stream, "%s.%03d %s%s: ", tmBuf, (int)(ts.tv_nsec / 1000000), meta->psz_module, msg_type[type]);

2016年4月6日 星期三

三 VideoLAN Compile 成熟篇

不了解 git 即使能編譯完成,那也只是幸運!
apt-get install apt-show-version

git clone 會取得完整的程式庫,clone 後你就可以使用下列命令

$ git tag -n
列出主要版本 tag
$ git log --pretty=format:'%H %h %ci' -n 10
列出最近的10個版本
$ git log --tags --simplify-by-decoration --pretty="format:%ci %h %d %H"

$ git checkout tags/<tag_name>
使的整的程式庫呈現,tag版本的狀態
$ git checkout master
$ git checkout 035d652
$ git checkout 'master@{2016-03-24 10:02:16}'
$ git checkout -- HEAD

$ git reset --hard
$ git clean -f -d
完整地回復到最後版本,並且刪除不必要的檔案

針對某一檔案
$ git log filename
$ git --no-pager diff 99388eb:filename bdc798c:filename
$ git archive -o foo.tar 99388eb


話說回到 VideoLAN,因為整個計畫太大,所以將一些子計畫放入 contrib 目錄
而 make prebuilt 就是使用編譯好的子計畫
主計畫和子計畫並不一定能同步,據我的經驗要同步很難,所以編譯不過是正常的
真正的重點就是調整計畫的版本

以下是遇到的問題紀錄
add-apt-repository ppa:ubuntu-wine/ppa
add-apt-repository ppa:n-muench/vlc
apt-get update
apt-get install wine1.8-dev
apt-get install libsidplay2-dev
apt-get install libdvd-pkg
apt-get install libdvdread-dev

LUA 要使用32位元的版本,否則會遇下列問題
checking for LUA... no
configure: WARNING: No package 'lua5.2' found, trying lua 5.1 instead
checking for LUA... no
configure: WARNING: No package 'lua5.1' found, trying lua >= 5.1 instead
checking for LUA... yes
checking for luac... luac
configure: error: You need 32-bits luac when using lua from contrib.

32位元版本的安裝就是加上 :i386
apt-get install lua5.2:i386
apt-get install lua5.2-dev:i386
apt-get install liblua5.2:i386
apt-get install liblua5.2-dev:i386

在 contrib 下的 lua 要回復到 5.1 版,之後的版本會遇到 module 的問題
如 lua/modules/common.lua 之
module("common",package.seeall)

下列命令可以測試 lua 版本
contrib/i686-w64-mingw32/bin# ./luac -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

主程式版本
d445720262fbc0eb6c7e3e1244280aee2c492e08 d445720 2016-03-23 14:47:50 +0100

LUA 回復版本
git checkout c433918ec056f0b6a1b6ecbce01e03f62bc68ebf contrib/src/lua

ffmpeg 回復版本
vi contrib/src/ffmpeg/rules.mak
ifdef USE_FFMPEG
else
#HASH=HEAD
HASH=73b0324