From e0d1fb0377e49cd8ba07906b21bbe41c8705f1b6 Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Mon, 10 Mar 2014 17:16:05 +0100 Subject: [PATCH] file: expose seek function in public API --- include/osmosdr/source.h | 9 +++++++++ lib/file/file_source_c.cc | 17 ++++++++++------- lib/file/file_source_c.h | 4 ++++ lib/source_iface.h | 9 +++++++++ lib/source_impl.cc | 11 +++++++++++ lib/source_impl.h | 2 ++ 6 files changed, 45 insertions(+), 7 deletions(-) diff --git a/include/osmosdr/source.h b/include/osmosdr/source.h index ca718e8..2bd3213 100644 --- a/include/osmosdr/source.h +++ b/include/osmosdr/source.h @@ -57,6 +57,15 @@ public: */ virtual size_t get_num_channels( void ) = 0; + /*! + * \brief seek file to \p seek_point relative to \p whence + * + * \param seek_point sample offset in file + * \param whence one of SEEK_SET, SEEK_CUR, SEEK_END (man fseek) + * \return true on success + */ + virtual bool seek( long seek_point, int whence, size_t chan = 0 ) = 0; + /*! * Get the possible sample rates for the underlying radio hardware. * \return a range of rates in Sps diff --git a/lib/file/file_source_c.cc b/lib/file/file_source_c.cc index d4a0f1f..671c6c9 100644 --- a/lib/file/file_source_c.cc +++ b/lib/file/file_source_c.cc @@ -26,7 +26,6 @@ #include #include -#include #include "file_source_c.h" @@ -78,18 +77,17 @@ file_source_c::file_source_c(const std::string &args) : _file_rate = _rate; - gr::blocks::file_source::sptr src = \ - gr::blocks::file_source::make( sizeof(gr_complex), - filename.c_str(), - repeat ); + _source = gr::blocks::file_source::make( sizeof(gr_complex), + filename.c_str(), + repeat ); _throttle = gr::blocks::throttle::make( sizeof(gr_complex), _file_rate ); if (throttle) { - connect( src, 0, _throttle, 0 ); + connect( _source, 0, _throttle, 0 ); connect( _throttle, 0, self(), 0 ); } else { - connect( src, 0, self(), 0 ); + connect( _source, 0, self(), 0 ); } } @@ -122,6 +120,11 @@ size_t file_source_c::get_num_channels( void ) return 1; } +bool file_source_c::seek( long seek_point, int whence , size_t chan ) +{ + return _source->seek( seek_point, whence ); +} + osmosdr::meta_range_t file_source_c::get_sample_rates( void ) { osmosdr::meta_range_t range; diff --git a/lib/file/file_source_c.h b/lib/file/file_source_c.h index d2d71ca..3e03037 100644 --- a/lib/file/file_source_c.h +++ b/lib/file/file_source_c.h @@ -21,6 +21,7 @@ #define FILE_SOURCE_C_H #include +#include #include #include "source_iface.h" @@ -49,6 +50,8 @@ public: size_t get_num_channels( void ); + bool seek( long seek_point, int whence, size_t chan ); + osmosdr::meta_range_t get_sample_rates( void ); double set_sample_rate( double rate ); double get_sample_rate( void ); @@ -72,6 +75,7 @@ public: std::string get_antenna( size_t chan = 0 ); private: + gr::blocks::file_source::sptr _source; gr::blocks::throttle::sptr _throttle; double _file_rate; double _freq, _rate; diff --git a/lib/source_iface.h b/lib/source_iface.h index 659a702..cc3f73b 100644 --- a/lib/source_iface.h +++ b/lib/source_iface.h @@ -37,6 +37,15 @@ public: */ virtual size_t get_num_channels( void ) = 0; + /*! + * \brief seek file to \p seek_point relative to \p whence + * + * \param seek_point sample offset in file + * \param whence one of SEEK_SET, SEEK_CUR, SEEK_END (man fseek) + * \return true on success + */ + virtual bool seek( long seek_point, int whence, size_t chan = 0 ) { return false; } + /*! * Get the possible sample rates for the underlying radio hardware. * \return a range of rates in Sps diff --git a/lib/source_impl.cc b/lib/source_impl.cc index 9f03c39..1e9af47 100644 --- a/lib/source_impl.cc +++ b/lib/source_impl.cc @@ -376,6 +376,17 @@ size_t source_impl::get_num_channels() return channels; } +bool source_impl::seek( long seek_point, int whence, size_t chan ) +{ + size_t channel = 0; + BOOST_FOREACH( source_iface *dev, _devs ) + for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++) + if ( chan == channel++ ) + return dev->seek( seek_point, whence, dev_chan ); + + return false; +} + #define NO_DEVICES_MSG "FATAL: No device(s) available to work with." osmosdr::meta_range_t source_impl::get_sample_rates() diff --git a/lib/source_impl.h b/lib/source_impl.h index 42ab021..aa81232 100644 --- a/lib/source_impl.h +++ b/lib/source_impl.h @@ -38,6 +38,8 @@ public: size_t get_num_channels( void ); + bool seek( long seek_point, int whence, size_t chan ); + osmosdr::meta_range_t get_sample_rates( void ); double set_sample_rate( double rate ); double get_sample_rate( void );