static switch_status_t vlc_file_read(switch_file_handle_t *handle, void *data, size_t *len) { vlc_file_context_t *context = handle->private_info; size_t bytes = *len * sizeof(int16_t) * handle->channels, read; libvlc_state_t status; if (!context) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "VLC read handle context is NULL\n"); return SWITCH_STATUS_GENERR; } if (context->err) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "VLC error\n"); return SWITCH_STATUS_GENERR; } status = libvlc_media_get_state(context->m); if (status == libvlc_Error) { return SWITCH_STATUS_GENERR; } switch_mutex_lock(context->audio_mutex); while (context->playing == 0 && status != libvlc_Ended && status != libvlc_Error) { switch_thread_cond_wait(context->started, context->audio_mutex); status = libvlc_media_get_state(context->m); } if (context->err == 1) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "VLC error\n"); return SWITCH_STATUS_FALSE; } switch_mutex_unlock(context->audio_mutex); switch_mutex_lock(context->audio_mutex); read = switch_buffer_read(context->audio_buffer, data, bytes); switch_mutex_unlock(context->audio_mutex); status = libvlc_media_get_state(context->m); if (!read && (status == libvlc_Stopped || status == libvlc_Ended || status == libvlc_Error)) { return SWITCH_STATUS_FALSE; } else if (!read) { read = 2; memset(data, 0, read); } if (read) { *len = read / 2 / handle->channels; } return SWITCH_STATUS_SUCCESS; }
static void vlc_mediaplayer_error_callback(const libvlc_event_t * event, void * data) { vlc_file_context_t *context = (vlc_file_context_t *) data; int status = libvlc_media_get_state(context->m); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Got a libvlc_MediaPlayerEncounteredError callback. mediaPlayer Status: %d\n", status); if (status == libvlc_Error) { context->err = 1; switch_thread_cond_signal(context->started); } }
QString VideoWidget::GetTitle(QString file) { libvlc_instance_t * pinst; libvlc_media_player_t *pmp; libvlc_media_t *pm; pinst = libvlc_new (0, NULL); pm = libvlc_media_new_path (pinst, file.toStdString().c_str()); pmp = libvlc_media_player_new_from_media (pm); libvlc_media_player_play (pmp); while (libvlc_media_get_state(pm) != 3) { libvlc_media_get_state(pm); } char *meta = libvlc_media_get_meta (pm, libvlc_meta_Title); QString title = QString(meta); title.detach(); libvlc_media_release (pm); libvlc_media_player_stop (pmp); libvlc_media_player_release (pmp); libvlc_release (pinst); return title; }
static switch_status_t vlc_file_close(switch_file_handle_t *handle) { vlc_file_context_t *context = handle->private_info; int sanity = 0; context->playing = 0; /* The clients need to empty the last of the audio buffer */ while ( switch_buffer_inuse(context->audio_buffer) > 0 ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "VLC waiting to close the files: %d \n", (int) switch_buffer_inuse(context->audio_buffer)); switch_yield(500000); if (++sanity > 10) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Giving up waiting for client to empty the audio buffer\n"); break; } } /* Let the clients get the last of the audio stream */ sanity = 0; while ( 3 == libvlc_media_get_state(context->m) ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "VLC waiting for clients: %d \n", libvlc_media_get_state(context->m)); switch_yield(500000); if (++sanity > 10) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Giving up waiting for client to get the last of the audio stream\n"); break; } } if( context->mp ) libvlc_media_player_stop(context->mp); if( context->m ) libvlc_media_release(context->m); if ( context->inst_out != NULL ) libvlc_release(context->inst_out); return SWITCH_STATUS_SUCCESS; }
ssize_t vlc_in_read(void *buf, size_t len) { if (len == 0) { return 0; } assert(buf); size_t requested = len; for (;;) { pthread_mutex_lock(&buffer_lock); if (vlc_buffer_totalsize(head_buffer) >= len) { while (len >= head_buffer->size) { if (head_buffer->buf && head_buffer->size) { // Get all the data from this list element memcpy(buf, head_buffer->buf, head_buffer->size); buf += head_buffer->size; len -= head_buffer->size; } if (head_buffer->next) { struct vlc_buffer *next_head = head_buffer->next; vlc_buffer_free(head_buffer); head_buffer = next_head; } else { vlc_buffer_free(head_buffer); head_buffer = vlc_buffer_new(); break; } } if (len > 0) { assert(len < head_buffer->size); assert(head_buffer->buf); memcpy(buf, head_buffer->buf, len); // split the current head into two parts size_t remaining = head_buffer->size - len; uint8_t *newbuf = malloc(remaining); memcpy(newbuf, head_buffer->buf + len, remaining); free(head_buffer->buf); head_buffer->buf = newbuf; head_buffer->size = remaining; } pthread_mutex_unlock(&buffer_lock); return requested; } pthread_mutex_unlock(&buffer_lock); usleep(100); libvlc_media_t *media = libvlc_media_player_get_media(m_mp); libvlc_state_t st = libvlc_media_get_state(media); if (!(st == libvlc_Opening || st == libvlc_Buffering || st == libvlc_Playing) ) { return -1; } char* nowplaying_sz = libvlc_media_get_meta(media, libvlc_meta_NowPlaying); if (nowplaying_sz) { snprintf(vlc_nowplaying, NOWPLAYING_LEN, "%s", nowplaying_sz); free(nowplaying_sz); } } abort(); }
int vlc_in_prepare( unsigned verbosity, unsigned int rate, const char* uri, unsigned channels, const char* icy_write_file ) { fprintf(stderr, "Initialising VLC...\n"); vlc_nowplaying_running = 0; vlc_nowplaying_filename = icy_write_file; long long int handleStream_address; long long int prepareRender_address; int vlc_version_check = check_vlc_uses_size_t(); if (vlc_version_check == 0) { fprintf(stderr, "You are using VLC with unsigned int size callbacks\n"); handleStream_address = (long long int)(intptr_t)(void*)&handleStream; prepareRender_address = (long long int)(intptr_t)(void*)&prepareRender; } else if (vlc_version_check == 1) { fprintf(stderr, "You are using VLC with size_t size callbacks\n"); handleStream_address = (long long int)(intptr_t)(void*)&handleStream_size_t; prepareRender_address = (long long int)(intptr_t)(void*)&prepareRender_size_t; } else { fprintf(stderr, "Error detecting VLC version!\n"); fprintf(stderr, " you are using %s\n", libvlc_get_version()); return -1; } vlc_rate = rate; vlc_channels = channels; // VLC options char smem_options[512]; snprintf(smem_options, sizeof(smem_options), "#transcode{acodec=s16l,samplerate=%d}:" // We are using transcode because smem only support raw audio and // video formats "smem{" "audio-postrender-callback=%lld," "audio-prerender-callback=%lld" "}", vlc_rate, handleStream_address, prepareRender_address); char verb_options[512]; snprintf(verb_options, sizeof(verb_options), "--verbose=%d", verbosity); const char * const vlc_args[] = { verb_options, "--sout", smem_options // Stream to memory }; // Launch VLC m_vlc = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); // Load the media libvlc_media_t *m; m = libvlc_media_new_location(m_vlc, uri); m_mp = libvlc_media_player_new_from_media(m); libvlc_media_release(m); // Allocate the list head_buffer = vlc_buffer_new(); // Start playing int ret = libvlc_media_player_play(m_mp); if (ret == 0) { libvlc_media_t *media = libvlc_media_player_get_media(m_mp); libvlc_state_t st; ret = -1; int timeout; for (timeout = 0; timeout < 100; timeout++) { st = libvlc_media_get_state(media); usleep(10*1000); if (st != libvlc_NothingSpecial) { ret = 0; break; } } } return ret; }
MediaState VLCWrapperImpl::GetMediaState() { if (!pMedia_) return NothingSpecial; return (MediaState)(libvlc_media_get_state(pMedia_)); }
libvlc_state_t VlcVideoWidget::GetMediaState() const { if (!Initialized() || !vlcMedia_) return libvlc_Error; return libvlc_media_get_state(vlcMedia_); }
bool VlcVideoWidget::OpenSource(const QString &videoUrl) { if (!Initialized()) return false; // We need to prepend file:// if this is a path on disk. QString source = videoUrl; AssetAPI::AssetRefType sourceType = AssetAPI::ParseAssetRef(source); if ((sourceType == AssetAPI::AssetRefLocalPath || sourceType == AssetAPI::AssetRefLocalUrl)) { if (source.startsWith("file://", Qt::CaseInsensitive)) source = source.mid(7); vlcMedia_ = libvlc_media_new_path(vlcInstance_, source.toUtf8().constData()); } else vlcMedia_ = libvlc_media_new_location(vlcInstance_, source.toUtf8().constData()); if (vlcMedia_ == 0) { LogError("VlcVideoWidget: Could not load media from '" + videoUrl + "'"); return false; } libvlc_event_manager_t *em = libvlc_media_event_manager(vlcMedia_); libvlc_event_attach(em, libvlc_MediaMetaChanged, &VlcEventHandler, this); libvlc_event_attach(em, libvlc_MediaSubItemAdded, &VlcEventHandler, this); libvlc_event_attach(em, libvlc_MediaDurationChanged, &VlcEventHandler, this); libvlc_event_attach(em, libvlc_MediaParsedChanged, &VlcEventHandler, this); libvlc_event_attach(em, libvlc_MediaFreed, &VlcEventHandler, this); libvlc_event_attach(em, libvlc_MediaStateChanged, &VlcEventHandler, this); libvlc_state_t state = libvlc_media_get_state(vlcMedia_); if (state != libvlc_Error) { // Reset playback Stop(); hasVideoOut_ = false; libvlc_media_player_set_media(vlcPlayer_, vlcMedia_); statusAccess.lock(); status.Reset(); status.source = videoUrl; status.change = PlayerStatus::MediaSource; statusAccess.unlock(); return true; } else { std::string err = "Unknown error"; if (libvlc_errmsg()) { err = libvlc_errmsg(); libvlc_clearerr(); } LogError("VlcVideoWidget: " + err); } return false; }