網頁

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 );
}

沒有留言:

張貼留言