[vlc-commits] Add an imem/libvlc_media_new_callbacks example
Hugo Beauzée-Luyssen
git at videolan.org
Fri May 15 14:38:38 CEST 2015
libvlcpp | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Wed May 13 18:34:37 2015 +0200| [a9da24aa387eeef8302f27b7aa680ef9c5ebd967] | committer: Hugo Beauzée-Luyssen
Add an imem/libvlc_media_new_callbacks example
> http://git.videolan.org/gitweb.cgi/libvlcpp.git/?a=commit;h=a9da24aa387eeef8302f27b7aa680ef9c5ebd967
---
examples/CMakeLists.txt | 1 +
examples/imem/CMakeLists.txt | 6 +++
examples/imem/imem.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 93 insertions(+)
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 122fa9a..30eab5f 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1 +1,2 @@
subdirs(helloworld)
+subdirs(imem)
diff --git a/examples/imem/CMakeLists.txt b/examples/imem/CMakeLists.txt
new file mode 100644
index 0000000..b0a33f1
--- /dev/null
+++ b/examples/imem/CMakeLists.txt
@@ -0,0 +1,6 @@
+project(imem)
+
+add_executable(${PROJECT_NAME}
+ imem.cpp
+)
+target_link_libraries( ${PROJECT_NAME} ${LIBVLC_LIBRARY} ${LIBVLCCORE_LIBRARY} )
diff --git a/examples/imem/imem.cpp b/examples/imem/imem.cpp
new file mode 100644
index 0000000..21c1f82
--- /dev/null
+++ b/examples/imem/imem.cpp
@@ -0,0 +1,86 @@
+#include "vlcpp/vlc.hpp"
+#include <thread>
+#include <cstring>
+#include <cstdio>
+#include <iostream>
+
+struct ImemOpaque
+{
+ FILE* file;
+ std::string path;
+};
+
+int main(int ac, char**av)
+{
+ if (ac < 3)
+ {
+ std::cerr << "usage: " << av[0] << " <file to play> <another file to play>" << std::endl;
+ return 1;
+ }
+ auto instance = VLC::Instance(0, nullptr);
+ auto dummyOpaque = new ImemOpaque{};
+ dummyOpaque->path = av[1];
+ auto imemMedia = VLC::Media( instance,
+ // Open
+ [dummyOpaque]( void*, void** opaque, uint64_t* p_size ) -> int {
+ dummyOpaque->file = fopen( dummyOpaque->path.c_str(), "rb" );
+ *opaque = dummyOpaque;
+ fseek(dummyOpaque->file, 0, SEEK_END);
+ *p_size = ftell( dummyOpaque->file );
+ rewind( dummyOpaque->file );
+ return 0;
+ },
+ // Read:
+ []( void* opaque, unsigned char* buf, size_t size ) -> ssize_t {
+ auto context = reinterpret_cast<ImemOpaque*>( opaque );
+ auto res = fread( buf, 1, size, context->file );
+ if ( res == 0 )
+ return feof( context->file ) != 0 ? 0 : -1;
+ return res;
+ },
+ []( void* opaque, uint64_t seek ) -> int {
+ auto context = reinterpret_cast<ImemOpaque*>( opaque );
+ if ( fseek( context->file, seek, SEEK_SET ) < 0 )
+ return -1;
+ return 0;
+ },
+ []( void* opaque ) {
+ auto context = reinterpret_cast<ImemOpaque*>( opaque );
+ fclose( context->file );
+ });
+
+ auto opaque2 = new ImemOpaque{};
+ opaque2->file = fopen( av[2], "rb" );
+
+ // Do not use a user defined opaque
+ // This is mostly meant to test that our nullptr overload are functionnal
+ auto imemMedia2 = VLC::Media( instance,
+ nullptr,
+ [opaque2]( void* opaque, unsigned char* buf, size_t size ) -> ssize_t {
+ assert( opaque == nullptr );
+ auto res = fread( buf, 1, size, opaque2->file );
+ if ( res == 0 )
+ return feof( opaque2->file ) != 0 ? 0 : -1;
+ return res;
+ },
+ [opaque2]( void*, uint64_t offset ) {
+ if ( fseek( opaque2->file, offset, SEEK_CUR ) < -1 )
+ return -1;
+ return 0;
+ }, nullptr );
+
+ auto mp = VLC::MediaPlayer( imemMedia );
+ mp.play();
+
+ auto mp2 = VLC::MediaPlayer( imemMedia2 );
+ mp2.play();
+
+ std::this_thread::sleep_for( std::chrono::seconds( 10 ) );
+
+ mp.stop();
+ mp2.stop();
+
+ delete dummyOpaque;
+ fclose(opaque2->file);
+ delete opaque2;
+}
More information about the vlc-commits
mailing list