Example #1
0
static bool media_play()
{
	int64_t ms_media_length;
	int s_length;
	
	
	libvlc_media_parse(media_handle);
    s_length = (int)(ceil(libvlc_media_get_duration(media_handle)/1000));
	
	current_media_duration = s_length;
	
	if(s_length == -1)
		return false;
	
	libvlc_media_player_set_hwnd(mediaplay,(void *)hwnd_handle);
	
	libvlc_media_player_play(mediaplay);
		
	
	sleep(s_length);
	
	libvlc_media_player_stop(mediaplay);
	libvlc_media_release(media_handle);
	
	return true;
}
Example #2
0
void Media::parseMediaInfos()
{
    libvlc_media_parse(_vlcMedia);

    libvlc_media_track_info_t* tracks;
    int tracksCount = libvlc_media_get_tracks_info(_vlcMedia, &tracks);

    for (int track = 0; track < tracksCount; track++) {
        switch (tracks[track].i_type)
        {
            case libvlc_track_audio:
                _audioTracks << AudioTrack( &tracks[track] );
                break;
            case libvlc_track_video:
                _videoTracks << VideoTrack( &tracks[track] );
                break;
            case libvlc_track_text:
                _subtitlesTracks << Track( &tracks[track] );
                break;
            default:
                break;
        }
    }
    _originalDuration = libvlc_media_get_duration(_vlcMedia);
    _duration = _originalDuration;

    QFile file(this->location());
    _size = file.size();
    file.close();
}
Example #3
0
void VPlayer::play()
{
	if(mp){
		if(state==Stop){
			libvlc_media_track_info_t *info;
			libvlc_media_parse(m);
			int i=libvlc_media_get_tracks_info(m,&info);
			for(--i;i>=0;--i){
				if(info[i].i_type==libvlc_track_video){
					srcSize.setWidth (info[i].u.video.i_width);
					srcSize.setHeight(info[i].u.video.i_height);
				}
			}
			free(info);
			dstSize=srcSize;
			avpicture_alloc(&srcFrame,PIX_FMT_RGB32,srcSize.width(),srcSize.height());
			avpicture_alloc(&dstFrame,PIX_FMT_RGB32,dstSize.width(),dstSize.height());
			valid=true;
			libvlc_video_set_format(mp,"RV32",srcSize.width(),srcSize.height(),srcSize.width()*4);
			libvlc_video_set_callbacks(mp,lock,NULL,display,this);
			Utils::delayExec(50,[this](){libvlc_media_player_play(mp);});
		}
		else{
			libvlc_media_player_pause(mp);
			if(state==Play){
				state=Pause;
				emit paused();
			}
			else{
				state=Play;
			}
		}
	}
}
Example #4
0
void QmlVlcVideo::getVideoSize( unsigned* width, unsigned* height )
{
    libvlc_media_t* media = libvlc_media_player_get_media( m_player.get_mp() );
    if( media && !libvlc_media_is_parsed( media ) )
        libvlc_media_parse( media );

    *width = *height = 0;
    libvlc_video_get_size( m_player.get_mp(), 0, width, height );

    if( media && ( !*width || !*height ) ) {
        /*FIXME: It's not absolutely correct way to detect media dimensions,
        since now will be returned dimensions of first track with not zero demensions,
        and there are no any guarantee it will be be current playing track.
        But we nothing can do with it, since there are no way to match current
        playing track and track info received from libvlc_media_get_tracks_info for now.*/
        libvlc_media_track_info_t* info;
        int infoCount = libvlc_media_get_tracks_info( media, &info );
        for( int i = 0; i < infoCount; ++i ) {
            if( libvlc_track_video == info[i].i_type &&
                info[i].u.video.i_width &&
                info[i].u.video.i_height )
            {
                *width = info[i].u.video.i_width;
                *height = info[i].u.video.i_height;
                break;
            }
        }
        libvlc_free( info );
    }
}
// retorna true si es un video, false en caso contrario
// Si es un video ademas rellena los parametros Tiempo, nAncho y nAlto
const bool InformacionArchivoEx::_AnalisisVLC(const TCHAR *Path, UINT64 &Tiempo, UINT &nAncho, UINT &nAlto, libvlc_instance_t *Instancia) {
	char	Destino[2049];
	size_t  TamnTexto = wcslen(Path);
	int		TamRes = WideCharToMultiByte(CP_UTF8, NULL, Path, TamnTexto, Destino, 2048, NULL, NULL);
	Destino[TamRes] = 0;

	libvlc_media_t			*Media			= NULL;
	libvlc_media_player_t	*nMediaPlayer	= NULL;
	Media = libvlc_media_new_path(Instancia, Destino);

	libvlc_media_parse(Media);
	libvlc_media_add_option(Media, "sout=#description:dummy");

	nMediaPlayer = libvlc_media_player_new_from_media(Media);

	libvlc_state_t Estado = libvlc_Opening;
	libvlc_media_player_play(nMediaPlayer);

    //posible deadlock
	// Esperamos hasta que empieze el play
	while (Estado != libvlc_Playing && Estado != libvlc_Ended) { 
		Estado = libvlc_media_player_get_state(nMediaPlayer);
        Sleep(100);
		if (Estado == libvlc_Error) { // Hay un error en la libvlc, salimos
			Sleep(300); // Nos aseguramos de que el log se escriba
			libvlc_media_player_stop(nMediaPlayer);
			libvlc_media_release(Media);
			libvlc_media_player_release(nMediaPlayer);
			Tiempo = 0;
			nAncho = 0;
			nAlto = 0;
			return false;
		}
	}
	
	// Miramos los streams disponibles y buscamos el de video
	libvlc_media_track_info_t *TI = NULL;
	int z = libvlc_media_get_tracks_info(Media, &TI);
	for (int n = 0; n < z; n++) {
		if (TI->i_type == libvlc_track_video) {
			libvlc_media_player_stop(nMediaPlayer);
			Tiempo = 0; //libvlc_media_player_get_length(nMediaPlayer); // NO DA BIEN EL TIEMPO..............
			nAncho = TI->u.video.i_width;
			nAlto = TI->u.video.i_height;
			libvlc_media_release(Media);
			libvlc_media_player_release(nMediaPlayer);		
			return true;
		}
		TI ++;
	}
	libvlc_media_release(Media);
	libvlc_media_player_release(nMediaPlayer);		

	// No hay streams de video retornamos false
	Tiempo	= 0;
	nAncho	= 0;
	nAlto	= 0;
	return false;
}
Example #6
0
libvlc_media_t *VLC::newMediaFromPath(const char *path) {
    libvlc_media_t *mediaData = libvlc_media_new_path(libvlcInstance, path);
    libvlc_media_parse(mediaData);
    if (!libvlc_media_is_parsed(mediaData) || libvlc_media_get_duration(mediaData) == 0) {
        VLC::release(mediaData);
        return NULL;
    }
    return mediaData;
}
Example #7
0
int get_media_duration()
{
	if(mediaplay != NULL)
	{
		libvlc_media_parse(media_handle);
		return libvlc_media_get_duration(media_handle);
	}
	return -1;
}
static void
vlc_player_load_media(GtkVlcPlayer *player, libvlc_media_t *media)
{
	libvlc_media_parse(media);
	libvlc_media_player_set_media(player->priv->media_player, media);

	/* NOTE: media was parsed so get_duration works */
	update_length(player, (gint64)libvlc_media_get_duration(media));
	update_time(player, 0);
}
Example #9
0
void Audiotheque::GetAllSongsBy(Glib::ustring artist)
{
	std::vector<Gtk::Widget*> children = hbox.get_children();
	for(std::vector<Gtk::Widget*>::iterator it=children.begin()+2; it!=children.end(); it++)
		hbox.remove(**it);
		
	libvlc_instance_t *inst;
	inst = libvlc_new (0, NULL);
	libvlc_media_t *media;

	vbox = Gtk::manage(new Gtk::VBox(false,2));
	vbox->set_size_request(300,0);

	Gtk::Button* button;
	std::string path = dirpath+"/"+artist;
	DIR* rep = opendir(path.c_str());

	button = Gtk::manage(new Gtk::Button("Tous les titres"));
	vbox->pack_start(*button,Gtk::PACK_SHRINK);

	if(rep!=NULL) {
		struct dirent* ent;
		std::string here = ".";
		std::string parent = "..";
		while((ent=readdir(rep))!=NULL) {
			if(here.compare(ent->d_name)!=0 && parent.compare(ent->d_name)!=0) {
				std::string subdirpath = path+"/"+ent->d_name;
				DIR* subdir = opendir(subdirpath.c_str());
				if(subdir!=NULL) {
					struct dirent* subent;
					while((subent=readdir(subdir))!=NULL) {
						if(here.compare(subent->d_name)!=0 && parent.compare(subent->d_name)!=0) {
							std::string songpath = subdirpath+"/";
							songpath += subent->d_name;
							media = libvlc_media_new_path(inst,songpath.c_str());
							libvlc_media_parse(media);
							char* title = libvlc_media_get_meta(media,libvlc_meta_Title);
							button = Gtk::manage(new Gtk::Button(title));
							vbox->pack_start(*button,Gtk::PACK_SHRINK);
						}
					}
					closedir(subdir);
				}
			}
		}
		closedir(rep);
	}	

	hbox.pack_start(*vbox,Gtk::PACK_SHRINK);

	hbox.show_all_children();
}
jobjectArray Java_org_videolan_libvlc_LibVLC_readTracksInfo(JNIEnv *env, jobject thiz,
                                                            jlong instance, jstring mrl)
{
    /* Create a new item and assign it to the media player. */
    libvlc_media_t *p_m = new_media(instance, env, thiz, mrl, false, false);
    if (p_m == NULL)
    {
        LOGE("Could not create the media!");
        return NULL;
    }

    libvlc_media_parse(p_m);
    jobjectArray jar = read_track_info_internal(env, thiz, p_m);
    libvlc_media_release(p_m);
    return jar;
}
Example #11
0
JNIEXPORT void JNICALL NAME(nativePrepare)(JNIEnv *env, jobject thiz)
{
    libvlc_media_t *media = (libvlc_media_t *) getIntValue(env, thiz, "mLibVlcMedia");
    char *mrl = libvlc_media_get_mrl(media);
    if (strncmp(mrl, "file://", 7) == 0)
        libvlc_media_parse(media);
    else {
        vlc_mutex_t *parse_lock = (vlc_mutex_t *) getIntValue(env, thiz, "mNativeMediaParseLock");
        vlc_cond_t *parse_cond = (vlc_cond_t *) getIntValue(env, thiz, "mNativeMediaParseCond");
        libvlc_media_player_t *mp = (libvlc_media_player_t *) getIntValue(env, thiz, "mLibVlcMediaPlayer");
        libvlc_media_player_play(mp);
        vlc_mutex_lock(parse_lock);
        while (!getIntValue(env, thiz, "mNativeMediaParsed"))
            vlc_cond_wait(parse_cond, parse_lock);
        vlc_mutex_unlock(parse_lock);
    }
    free(mrl);
}
Example #12
0
	MediaMeta VLCWrapper::GetItemMeta (int row, const QString& location) const
	{
		MediaMeta meta;
		auto m = libvlc_media_list_item_at_index (List_.get (), row);
		if (!m)
			return meta;

		if (!QUrl (location).scheme ().isEmpty ())
		{
			meta.Artist_ = tr ("Internet stream");
			meta.Title_ = location;
			return meta;
		}

		libvlc_media_parse (m);
		meta.Artist_ = QString::fromUtf8 (libvlc_media_get_meta (m, libvlc_meta_Artist));
		meta.Album_ = QString::fromUtf8 (libvlc_media_get_meta (m, libvlc_meta_Album));
		meta.Title_ = QString::fromUtf8 (libvlc_media_get_meta (m, libvlc_meta_Title));
		meta.Genre_ = QString::fromUtf8 (libvlc_media_get_meta (m, libvlc_meta_Genre));
		meta.Date_ = libvlc_media_get_meta (m, libvlc_meta_Date);
		meta.Length_ = libvlc_media_get_duration (m) / 1000;

		if (location.isNull ())
			meta.Location_ = QUrl (libvlc_media_get_mrl (m));
		else
			meta.Location_ = QUrl (location);

		libvlc_media_track_info_t *pTrackInfo;
		int numOfStream = libvlc_media_get_tracks_info (m, &pTrackInfo);

		if (numOfStream >= 1)
			meta.Type_ = pTrackInfo->i_type;

		meta.TrackNumber_ = QString (libvlc_media_get_meta (m,
						libvlc_meta_TrackNumber))
				.toInt ();
		return meta;
	}
jboolean Java_org_videolan_libvlc_LibVLC_hasVideoTrack(JNIEnv *env, jobject thiz,
                                                       jlong i_instance, jstring fileLocation)
{
    /* Create a new item and assign it to the media player. */
    libvlc_media_t *p_m = new_media(i_instance, env, thiz, fileLocation, false, false);
    if (p_m == NULL)
    {
        LOGE("Could not create the media!");
        return JNI_FALSE;
    }

    /* Get the tracks information of the media. */
    libvlc_media_parse(p_m);

    libvlc_media_player_t* p_mp = libvlc_media_player_new_from_media(p_m);
    libvlc_media_player_set_video_title_display(p_mp, libvlc_position_disable, 0);

    struct length_change_monitor* monitor;
    monitor = malloc(sizeof(struct length_change_monitor));
    if (!monitor) return 0;

    /* Initialize pthread variables. */
    pthread_mutex_init(&monitor->doneMutex, NULL);
    pthread_cond_init(&monitor->doneCondVar, NULL);
    monitor->length_changed = false;

    libvlc_event_manager_t *ev = libvlc_media_player_event_manager(p_mp);
    libvlc_event_attach(ev, libvlc_MediaPlayerLengthChanged, length_changed_callback, monitor);
    libvlc_media_player_play( p_mp );

    pthread_mutex_lock(&monitor->doneMutex);

    struct timespec deadline;
    clock_gettime(CLOCK_REALTIME, &deadline);
    deadline.tv_sec += 2; /* If "VLC can't open the file", return */
    int mp_alive = 1;
    while( !monitor->length_changed && mp_alive ) {
        pthread_cond_timedwait(&monitor->doneCondVar, &monitor->doneMutex, &deadline);
        mp_alive = libvlc_media_player_will_play(p_mp);
    }
    pthread_mutex_unlock(&monitor->doneMutex);

    int i_nbTracks;
    if( mp_alive )
        i_nbTracks = libvlc_video_get_track_count(p_mp);
    else
        i_nbTracks = -1;
    LOGI("Number of video tracks: %d",i_nbTracks);

    libvlc_event_detach(ev, libvlc_MediaPlayerLengthChanged, length_changed_callback, monitor);
    libvlc_media_player_stop(p_mp);
    libvlc_media_player_release(p_mp);
    libvlc_media_release(p_m);

    pthread_mutex_destroy(&monitor->doneMutex);
    pthread_cond_destroy(&monitor->doneCondVar);
    free(monitor);

    if(i_nbTracks > 0)
        return JNI_TRUE;
    else if(i_nbTracks < 0)
        (*env)->ThrowNew(env, (*env)->FindClass(env, "java/io/IOException"), "VLC can't open the file");
    else
        return JNI_FALSE;
}
Example #14
0
void MediaPlayer::setMedia(QString file)
{
	//qDebug() << "MediaPlayer::setMedia" << file;
	if (vlcMediaPlayer)
	{
		//libvlc_event_attach (libvlc_event_manager_t *p_event_manager, libvlc_event_type_t i_event_type, libvlc_callback_t f_callback, void *user_data)
		libvlc_event_manager_t *manager = libvlc_media_player_event_manager(vlcMediaPlayer);
		libvlc_event_manager_t *mediamanager = libvlc_media_event_manager(vlcMedia);
		//libvlc_event_detach(mediamanager,libvlc_MediaStateChanged,&callback,this);
		/*
		libvlc_MediaPlayerNothingSpecial
		libvlc_MediaPlayerOpening
		libvlc_MediaPlayerBuffering
		libvlc_MediaPlayerPlaying
		libvlc_MediaPlayerPaused
		libvlc_MediaPlayerStopped
		libvlc_MediaPlayerForward
		libvlc_MediaPlayerBackward
		libvlc_MediaPlayerEndReached
		libvlc_MediaPlayerEncounteredError
		libvlc_MediaPlayerTimeChanged
		libvlc_MediaPlayerPositionChanged
		libvlc_MediaPlayerSeekableChanged
		libvlc_MediaPlayerPausableChanged
		libvlc_MediaPlayerTitleChanged
		libvlc_MediaPlayerSnapshotTaken
		libvlc_MediaPlayerLengthChanged */
		libvlc_event_detach(manager,libvlc_MediaPlayerOpening,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerBuffering,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerPlaying,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerStopped,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerPaused,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerForward,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerBackward,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerEndReached,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerEncounteredError,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerTimeChanged,&callback,this);
		//libvlc_event_detach(manager,libvlc_MediaPlayerPositionChanged,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerSeekableChanged,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerPausableChanged,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerTitleChanged,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerSnapshotTaken,&callback,this);
		libvlc_event_detach(manager,libvlc_MediaPlayerLengthChanged,&callback,this);

		libvlc_media_release(vlcMedia);
		libvlc_media_player_stop(vlcMediaPlayer);
		libvlc_media_player_release(vlcMediaPlayer);
		vlcMediaPlayer = 0;
		vlcMedia = 0;
	}

	//This little bs code is brought to you by the dolts from vlc and Qt. Qt's QDir dosen't return
	//native path seperators nicely, and VLC won't accept anything but native.
#ifdef Q_OS_WIN32
	vlcMedia = libvlc_media_new_path(vlcInstance,file.replace("/","\\").toAscii());
#else
#ifdef Q_OS_LINUX
	vlcMedia = libvlc_media_new_path(vlcInstance,file.toAscii());
#endif
#endif
	vlcMediaPlayer = libvlc_media_player_new_from_media(vlcMedia);
	libvlc_media_parse(vlcMedia);

	libvlc_event_manager_t *manager = libvlc_media_player_event_manager(vlcMediaPlayer);
	libvlc_event_attach(manager,libvlc_MediaPlayerOpening,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerBuffering,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerPlaying,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerStopped,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerPaused,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerForward,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerBackward,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerEndReached,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerEncounteredError,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerTimeChanged,&callback,this);
	//libvlc_event_attach(manager,libvlc_MediaPlayerPositionChanged,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerSeekableChanged,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerPausableChanged,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerTitleChanged,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerSnapshotTaken,&callback,this);
	libvlc_event_attach(manager,libvlc_MediaPlayerLengthChanged,&callback,this);
	//libvlc_event_manager_t *mediamanager = libvlc_media_event_manager(vlcMedia);
	//libvlc_event_attach(mediamanager,libvlc_MediaStateChanged,&callback,this);
	emit mediaChanged();
}
Example #15
0
/**
 * Thumbnailer main function.
 * return null if the thumbail generation failed.
 **/
jbyteArray
Java_org_videolan_libvlc_util_VLCUtil_nativeGetThumbnail(JNIEnv *env,
                                                         jobject thiz,
                                                         jobject jmedia,
                                                         const jint frameWidth,
                                                         const jint frameHeight)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, jmedia);
    jbyteArray byteArray = NULL;

    /* Create the thumbnailer data structure */
    thumbnailer_sys_t *sys = calloc(1, sizeof(thumbnailer_sys_t));
    if (sys == NULL)
    {
        LOGE("Could not create the thumbnailer data structure!");
        goto enomem;
    }

    /* Initialize the barrier. */
    pthread_mutex_init(&sys->doneMutex, NULL);
    pthread_cond_init(&sys->doneCondVar, NULL);

    /* Create a media player playing environment */
    libvlc_media_player_t *mp = libvlc_media_player_new_from_media(p_obj->u.p_m);
    libvlc_media_player_set_video_title_display(mp, libvlc_position_disable, 0);

    /* Get the size of the video with the tracks information of the media. */
    libvlc_media_track_t **tracks;
    libvlc_media_parse(p_obj->u.p_m);
    int nbTracks = libvlc_media_tracks_get(p_obj->u.p_m, &tracks);

    /* Parse the results */
    unsigned videoWidth = 0, videoHeight = 0;
    bool hasVideoTrack = false;
    for (unsigned i = 0; i < nbTracks; ++i)
        if (tracks[i]->i_type == libvlc_track_video)
        {
            videoWidth = tracks[i]->video->i_width;
            videoHeight = tracks[i]->video->i_height;
            hasVideoTrack = true;
            break;
        }

    libvlc_media_tracks_release(tracks, nbTracks);

    /* Abort if we have not found a video track. */
    if (!hasVideoTrack)
    {
        LOGE("Could not find any video track in this file.\n");
        goto end;
    }

    LOGD("Video dimensions: %ix%i.\n", videoWidth, videoHeight );

    /* VLC could not tell us the size */
    if( videoWidth == 0 || videoHeight == 0 )
    {
        LOGE("Could not find the video dimensions.\n");
        goto end;
    }

    if( videoWidth < THUMBNAIL_MIN_WIDTH || videoHeight < THUMBNAIL_MIN_HEIGHT
        || videoWidth > THUMBNAIL_MAX_WIDTH || videoHeight > THUMBNAIL_MAX_HEIGHT )
    {
        LOGE("Wrong video dimensions.\n");
        goto end;
    }

    /* Compute the size parameters of the frame to generate. */
    unsigned thumbWidth  = frameWidth;
    unsigned thumbHeight = frameHeight;
    const float inputAR = (float)videoWidth / videoHeight;
    const float screenAR = (float)frameWidth / frameHeight;

    /* Most of the cases, video is wider than tall */
    if (screenAR < inputAR)
    {
        thumbHeight = (float)frameWidth / inputAR + 1;
        sys->blackBorders = ( (frameHeight - thumbHeight) / 2 ) * frameWidth;
    }
    else
    {
        LOGD("Weird aspect Ratio.\n");
        thumbWidth = (float)frameHeight * inputAR;
        sys->blackBorders = (frameWidth - thumbWidth) / 2;
    }

    sys->thumbPitch  = thumbWidth * PIXEL_SIZE;
    sys->thumbHeight = thumbHeight;
    sys->frameWidth  = frameWidth;

    /* Allocate the memory to store the frames. */
    size_t thumbSize = sys->thumbPitch * (sys->thumbHeight+1);
    sys->thumbData = malloc(thumbSize);
    if (sys->thumbData == NULL)
    {
        LOGE("Could not allocate the memory to store the frame!");
        goto end;
    }

    /* Allocate the memory to store the thumbnail. */
    unsigned frameSize = frameWidth * frameHeight * PIXEL_SIZE;
    sys->frameData = calloc(frameSize, 1);
    if (sys->frameData == NULL)
    {
        LOGE("Could not allocate the memory to store the thumbnail!");
        goto end;
    }

    /* Set the video format and the callbacks. */
    libvlc_video_set_format(mp, "RGBA", thumbWidth, thumbHeight, sys->thumbPitch);
    libvlc_video_set_callbacks(mp, thumbnailer_lock, thumbnailer_unlock,
                               NULL, (void*)sys);
    sys->state = THUMB_SEEKING;

    /* Play the media. */
    libvlc_media_player_play(mp);
    libvlc_media_player_set_position(mp, THUMBNAIL_POSITION);

    const int wait_time = 50000;
    const int max_attempts = 100;
    for (int i = 0; i < max_attempts; ++i) {
        if (libvlc_media_player_is_playing(mp) && libvlc_media_player_get_position(mp) >= THUMBNAIL_POSITION)
            break;
        usleep(wait_time);
    }

    /* Wait for the thumbnail to be generated. */
    pthread_mutex_lock(&sys->doneMutex);
    sys->state = THUMB_SEEKED;
    struct timespec deadline;
    clock_gettime(CLOCK_REALTIME, &deadline);
    deadline.tv_sec += 10; /* amount of seconds before we abort thumbnailer */
    do {
        int ret = pthread_cond_timedwait(&sys->doneCondVar, &sys->doneMutex, &deadline);
        if (ret == ETIMEDOUT)
            break;
    } while (sys->state != THUMB_DONE);
    pthread_mutex_unlock(&sys->doneMutex);

    /* Stop and release the media player. */
    libvlc_media_player_stop(mp);
    libvlc_media_player_release(mp);

    if (sys->state == THUMB_DONE) {
        /* Create the Java byte array to return the create thumbnail. */
        byteArray = (*env)->NewByteArray(env, frameSize);
        if (byteArray == NULL)
        {
            LOGE("Could not allocate the Java byte array to store the frame!");
            goto end;
        }

        (*env)->SetByteArrayRegion(env, byteArray, 0, frameSize,
                (jbyte *)sys->frameData);
    }

end:
    pthread_mutex_destroy(&sys->doneMutex);
    pthread_cond_destroy(&sys->doneCondVar);
    free(sys->frameData);
    free(sys->thumbData);
    free(sys);
enomem:
    return byteArray;
}
Example #16
0
/**
 * Thumbnailer main function.
 * return null if the thumbail generation failed.
 **/
jbyteArray Java_org_videolan_vlc_LibVLC_getThumbnail(JNIEnv *env, jobject thiz,
        jint instance, jstring filePath,
        jint width, jint height)
{
    libvlc_instance_t *libvlc = (libvlc_instance_t *)instance;
    jbyteArray byteArray = NULL;

    /* Create the thumbnailer data structure */
    thumbnailer_sys_t *sys = calloc(1, sizeof(thumbnailer_sys_t));
    if (sys == NULL)
    {
        LOGE("Couldn't create the thumbnailer data structure!");
        return NULL;
    }

    /* Initialize the barrier. */
    pthread_mutex_init(&sys->doneMutex, NULL);
    pthread_cond_init(&sys->doneCondVar, NULL);

    /* Create a media player playing environment */
    sys->mp = libvlc_media_player_new(libvlc);

    libvlc_media_t *m = new_media(instance, env, thiz, filePath);
    if (m == NULL)
    {
        LOGE("Couldn't create the media to play!");
        goto end;
    }
    libvlc_media_add_option( m, ":no-audio" );

    libvlc_media_player_set_media(sys->mp, m);
    libvlc_media_release(m);

    /* Get the size of the video with the tracks information of the media. */
    libvlc_media_track_info_t *tracks;
    libvlc_media_parse(m);
    int nbTracks = libvlc_media_get_tracks_info(m, &tracks);

    unsigned i, videoWidth, videoHeight;
    bool hasVideoTrack = false;
    for (i = 0; i < nbTracks; ++i)
        if (tracks[i].i_type == libvlc_track_video)
        {
            videoWidth = tracks[i].u.video.i_width;
            videoHeight = tracks[i].u.video.i_height;
            hasVideoTrack = true;
            break;
        }

    free(tracks);

    /* Abord if we have not found a video track. */
    if (!hasVideoTrack)
    {
        LOGE("Could not find a video track in this file.\n");
        goto end;
    }

    /* Compute the size parameters of the frame to generate. */
    unsigned picWidth  = width;
    unsigned picHeight = height;
    float videoAR = (float)videoWidth / videoHeight;
    float screenAR = (float)width / height;
    if (screenAR < videoAR)
    {
        picHeight = (float)width / videoAR;
        sys->thumbnailOffset = (height - picHeight) / 2 * width * PIXEL_SIZE;
    }
    else
    {
        picWidth = (float)height * videoAR;
        sys->thumbnailOffset = (width - picWidth) / 2 * PIXEL_SIZE;
    }

    sys->picPitch = picWidth * PIXEL_SIZE;
    sys->lineSize = width * PIXEL_SIZE;
    sys->nbLines = picHeight;

    /* Allocate the memory to store the frames. */
    unsigned picSize = sys->picPitch * picHeight;
    sys->frameData = malloc(picSize);
    if (sys->frameData == NULL)
    {
        LOGE("Couldn't allocate the memory to store the frame!");
        goto end;
    }

    /* Allocate the memory to store the thumbnail. */
    unsigned thumbnailSize = width * height * PIXEL_SIZE;
    sys->thumbnail = calloc(thumbnailSize, 1);
    if (sys->thumbnail == NULL)
    {
        LOGE("Couldn't allocate the memory to store the thumbnail!");
        goto end;
    }

    /* Set the video format and the callbacks. */
    libvlc_video_set_format(sys->mp, "RGBA", picWidth, picHeight, sys->picPitch);
    libvlc_video_set_callbacks(sys->mp, thumbnailer_lock, thumbnailer_unlock,
                               NULL, (void*)sys);

    /* Play the media. */
    libvlc_media_player_play(sys->mp);
    libvlc_media_player_set_position(sys->mp, THUMBNAIL_POSITION);

    /* Wait for the thumbnail to be generated. */
    pthread_mutex_lock(&sys->doneMutex);
    while (!sys->hasThumb)
        pthread_cond_wait(&sys->doneCondVar, &sys->doneMutex);
    pthread_mutex_unlock(&sys->doneMutex);

    /* Stop and realease the media player. */
    libvlc_media_player_stop(sys->mp);
    libvlc_media_player_release(sys->mp);

    /* Create the Java byte array to return the create thumbnail. */
    byteArray = (*env)->NewByteArray(env, thumbnailSize);
    if (byteArray == NULL)
    {
        LOGE("Couldn't allocate the Java byte array to store the frame!");
        goto end;
    }

    (*env)->SetByteArrayRegion(env, byteArray, 0, thumbnailSize,
                               (jbyte *)sys->thumbnail);

    (*env)->DeleteLocalRef(env, byteArray);

end:
    pthread_mutex_destroy(&sys->doneMutex);
    pthread_cond_destroy(&sys->doneCondVar);
    free(sys->thumbnail);
    free(sys->frameData);
    free(sys);

    return byteArray;
}
Example #17
0
Omm::AvStream::Meta*
VlcTagger::tag(const std::string& uri)
{
    LOGNS(Omm::Av, upnpav, debug, "vlc tagger tagging: " + uri);

    VlcMeta* pMeta = new VlcMeta;

#if LIBVLC_VERSION_INT < 0x110
    _pVlcMedia = libvlc_media_new(_pVlcInstance, uri.c_str(), _pException);
#else
    _pVlcMedia = libvlc_media_new_location(_pVlcInstance, uri.c_str());
#endif
    handleException();

#if LIBVLC_VERSION_INT < 0x110
#else
    libvlc_media_parse(_pVlcMedia);
#endif

#if LIBVLC_VERSION_INT < 0x110
    char* pTitle = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_Title, _pException);
#else
    char* pTitle = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_Title);
#endif
    handleException();
    if (pTitle) {
        pMeta->setTag("title", std::string(pTitle));
    }

#if LIBVLC_VERSION_INT < 0x110
    char* pArtist = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_Artist, _pException);
#else
    char* pArtist = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_Artist);
#endif
    handleException();
    if (pArtist) {
        pMeta->setTag("artist", std::string(pArtist));
    }

#if LIBVLC_VERSION_INT < 0x110
    char* pAlbum = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_Album, _pException);
#else
    char* pAlbum = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_Album);
#endif
    handleException();
    if (pAlbum) {
        pMeta->setTag("album", std::string(pAlbum));
    }

#if LIBVLC_VERSION_INT < 0x110
    char* pTrack = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_TrackNumber, _pException);
#else
    char* pTrack = libvlc_media_get_meta(_pVlcMedia, libvlc_meta_TrackNumber);
#endif
    handleException();
    if (pTrack) {
        pMeta->setTag("track", std::string(pTrack));
    }

#if LIBVLC_VERSION_INT < 0x110
    libvlc_time_t duration = libvlc_media_get_duration(_pVlcMedia, _pException);
#else
    libvlc_time_t duration = libvlc_media_get_duration(_pVlcMedia);
#endif
    handleException();
    if (duration) {
        pMeta->setTag("duration", Poco::NumberFormatter::format(duration));
    }

    libvlc_media_release(_pVlcMedia);

    // vlc libs need to play the data in order to analyse the streams
    // thus we make a simple filename based media type detection
    Poco::Path path(uri);
    // try to get a filename extension for type of media
    std::string extension = Poco::toLower(path.getExtension());
//    LOGNS(Omm::Av, upnpav, debug, "vlc tagger extension: " + extension);

    Omm::AvStream::Meta::ContainerFormat containerFormat = Omm::AvStream::Meta::CF_UNKNOWN;
    if (extension == "mp3") {
        pMeta->_mime = Omm::Av::Mime::AUDIO_MPEG;
        containerFormat = Omm::AvStream::Meta::CF_AUDIO;
    }
    else if (extension == "wma" || extension == "ogg" || extension == "wav") {
        pMeta->_mime = Omm::Av::Mime::TYPE_AUDIO;
        containerFormat = Omm::AvStream::Meta::CF_AUDIO;
    }
    else if (extension == "mp4" || extension == "mpeg" || extension == "mpg") {
        pMeta->_mime = Omm::Av::Mime::VIDEO_MPEG;
        containerFormat = Omm::AvStream::Meta::CF_VIDEO;
    }
    else if (extension == "avi" || extension == "wmv" || extension == "flv") {
        pMeta->_mime = Omm::Av::Mime::TYPE_VIDEO;
        containerFormat = Omm::AvStream::Meta::CF_VIDEO;
    }
    else if (extension == "jpg" || extension == "png" || extension == "gif") {
        pMeta->_mime = Omm::Av::Mime::TYPE_IMAGE;
        containerFormat = Omm::AvStream::Meta::CF_IMAGE;
    }
    else if (extension == "m3u") {
        pMeta->_mime = Omm::Av::Mime::PLAYLIST;
        containerFormat = Omm::AvStream::Meta::CF_PLAYLIST;
    }

    if (containerFormat == Omm::AvStream::Meta::CF_AUDIO) {
        pMeta->_isStillImage = false;
        VlcStreamInfo* pStreamInfo = new VlcStreamInfo;
        pStreamInfo->_isAudio = true;
        pStreamInfo->_isVideo = false;
        pMeta->addStream(pStreamInfo);
    }
    else if (containerFormat == Omm::AvStream::Meta::CF_VIDEO) {
        pMeta->_isStillImage = false;
        VlcStreamInfo* pStreamInfo = new VlcStreamInfo;
        pStreamInfo->_isAudio = false;
        pStreamInfo->_isVideo = true;
        pMeta->addStream(pStreamInfo);
    }
    else if (containerFormat == Omm::AvStream::Meta::CF_IMAGE) {
        pMeta->_isStillImage = true;
        VlcStreamInfo* pStreamInfo = new VlcStreamInfo;
        pStreamInfo->_isAudio = false;
        pStreamInfo->_isVideo = false;
        pMeta->addStream(pStreamInfo);
    }
    else if (containerFormat == Omm::AvStream::Meta::CF_PLAYLIST) {
        pMeta->setIsPlaylist(true);
    }

    return pMeta;
}
Example #18
0
/**
 * Thumbnailer main function.
 * return null if the thumbail generation failed.
 **/
jbyteArray Java_org_videolan_vlc_LibVLC_getThumbnail(JNIEnv *env, jobject thiz,
                                                     jlong instance, jstring filePath,
                                                     const jint frameWidth, const jint frameHeight)
{
    libvlc_instance_t *libvlc = (libvlc_instance_t *)(intptr_t)instance;
    jbyteArray byteArray = NULL;

    /* Create the thumbnailer data structure */
    thumbnailer_sys_t *sys = calloc(1, sizeof(thumbnailer_sys_t));
    if (sys == NULL)
    {
        LOGE("Could not create the thumbnailer data structure!");
        return NULL;
    }

    /* Initialize the barrier. */
    pthread_mutex_init(&sys->doneMutex, NULL);
    pthread_cond_init(&sys->doneCondVar, NULL);

    /* Create a media player playing environment */
    libvlc_media_player_t *mp = libvlc_media_player_new(libvlc);

    libvlc_media_t *m = new_media(instance, env, thiz, filePath, true, false);
    if (m == NULL)
    {
        LOGE("Could not create the media to play!");
        goto end;
    }

    /* Fast and no options */
    libvlc_media_add_option( m, ":no-audio" );
    libvlc_media_add_option( m, ":no-spu" );
    libvlc_media_add_option( m, ":no-osd" );

    libvlc_media_player_set_media(mp, m);

    /* Get the size of the video with the tracks information of the media. */
    libvlc_media_track_info_t *tracks;
    libvlc_media_parse(m);
    int nbTracks = libvlc_media_get_tracks_info(m, &tracks);
    libvlc_media_release(m);

    /* Parse the results */
    unsigned videoWidth, videoHeight;
    bool hasVideoTrack = false;
    for (unsigned i = 0; i < nbTracks; ++i)
        if (tracks[i].i_type == libvlc_track_video)
        {
            videoWidth = tracks[i].u.video.i_width;
            videoHeight = tracks[i].u.video.i_height;
            hasVideoTrack = true;
            break;
        }

    free(tracks);

    /* Abort if we have not found a video track. */
    if (!hasVideoTrack)
    {
        LOGE("Could not find any video track in this file.\n");
        goto end;
    }

    /* VLC could not tell us the size */
    if( videoWidth == 0 || videoHeight == 0 )
    {
        LOGE("Could not find the video dimensions.\n");
        goto end;
    }

    /* Compute the size parameters of the frame to generate. */
    unsigned thumbWidth  = frameWidth;
    unsigned thumbHeight = frameHeight;
    const float inputAR = (float)videoWidth / videoHeight;
    const float screenAR = (float)frameWidth / frameHeight;

    /* Most of the cases, video is wider than tall */
    if (screenAR < inputAR)
    {
        thumbHeight = (float)frameWidth / inputAR + 1;
        sys->blackBorders = ( (frameHeight - thumbHeight) / 2 ) * frameWidth;
    }
    else
    {
        LOGD("Weird aspect Ratio.\n");
        thumbWidth = (float)frameHeight * inputAR;
        sys->blackBorders = (frameWidth - thumbWidth) / 2;
    }

    sys->thumbPitch  = thumbWidth * PIXEL_SIZE;
    sys->thumbHeight = thumbHeight;
    sys->frameWidth  = frameWidth;

    /* Allocate the memory to store the frames. */
    size_t thumbSize = sys->thumbPitch * (sys->thumbHeight+1);
    sys->thumbData = malloc(thumbSize);
    if (sys->thumbData == NULL)
    {
        LOGE("Could not allocate the memory to store the frame!");
        goto end;
    }

    /* Allocate the memory to store the thumbnail. */
    unsigned frameSize = frameWidth * frameHeight * PIXEL_SIZE;
    sys->frameData = calloc(frameSize, 1);
    if (sys->frameData == NULL)
    {
        LOGE("Could not allocate the memory to store the thumbnail!");
        goto end;
    }

    /* Set the video format and the callbacks. */
    libvlc_video_set_format(mp, "RGBA", thumbWidth, thumbHeight, sys->thumbPitch);
    libvlc_video_set_callbacks(mp, thumbnailer_lock, thumbnailer_unlock,
                               NULL, (void*)sys);
    sys->state = THUMB_SEEKING;

    /* Play the media. */
    libvlc_media_player_play(mp);
    libvlc_media_player_set_position(mp, THUMBNAIL_POSITION);

    int loops = 100;
    for (;;) {
        float pos = libvlc_media_player_get_position(mp);
        if (pos > THUMBNAIL_POSITION || !loops--)
            break;
        usleep(50000);
    }

    /* Wait for the thumbnail to be generated. */
    pthread_mutex_lock(&sys->doneMutex);
    sys->state = THUMB_SEEKED;
    struct timespec deadline;
    clock_gettime(CLOCK_REALTIME, &deadline);
    deadline.tv_sec += 10; /* amount of seconds before we abort thumbnailer */
    do {
        int ret = pthread_cond_timedwait(&sys->doneCondVar, &sys->doneMutex, &deadline);
        if (ret == ETIMEDOUT)
            break;
    } while (sys->state != THUMB_DONE);
    pthread_mutex_unlock(&sys->doneMutex);

    /* Stop and release the media player. */
    libvlc_media_player_stop(mp);
    libvlc_media_player_release(mp);

    if (sys->state == THUMB_DONE) {
        /* Create the Java byte array to return the create thumbnail. */
        byteArray = (*env)->NewByteArray(env, frameSize);
        if (byteArray == NULL)
        {
            LOGE("Could not allocate the Java byte array to store the frame!");
            goto end;
        }

        (*env)->SetByteArrayRegion(env, byteArray, 0, frameSize,
                (jbyte *)sys->frameData);
    }

end:
    pthread_mutex_destroy(&sys->doneMutex);
    pthread_cond_destroy(&sys->doneCondVar);
    free(sys->frameData);
    free(sys->thumbData);
    free(sys);

    return byteArray;
}