]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Auto-load session setups if they exist and auto-save them if desired
[pulseview.git] / pv / session.cpp
index 7df73d63d5ba43660eb666ec1a91322046002442..d3b2d6ac81d4c0517c999b7487602c9437f25aa3 100644 (file)
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
+#ifdef ENABLE_FLOW
+#include <gstreamermm.h>
+#include <libsigrokflow/libsigrokflow.hpp>
+#endif
+
 #ifdef ENABLE_DECODE
 #include <libsigrokdecode/libsigrokdecode.h>
 #include "data/decodesignal.hpp"
@@ -74,6 +79,9 @@ using std::recursive_mutex;
 using std::runtime_error;
 using std::shared_ptr;
 using std::string;
+#ifdef ENABLE_FLOW
+using std::unique_lock;
+#endif
 using std::unique_ptr;
 using std::unordered_set;
 using std::vector;
@@ -91,6 +99,12 @@ using sigrok::Session;
 
 using Glib::VariantBase;
 
+#ifdef ENABLE_FLOW
+using Gst::Bus;
+using Gst::ElementFactory;
+using Gst::Pipeline;
+#endif
+
 namespace pv {
 
 shared_ptr<sigrok::Context> Session::sr_context;
@@ -173,11 +187,49 @@ bool Session::data_saved() const
        return data_saved_;
 }
 
+void Session::save_setup(QSettings &settings) const
+{
+       int decode_signals = 0, views = 0;
+
+       // Save channels and decoders
+       for (const shared_ptr<data::SignalBase>& base : signalbases_) {
+#ifdef ENABLE_DECODE
+               if (base->is_decode_signal()) {
+                       settings.beginGroup("decode_signal" + QString::number(decode_signals++));
+                       base->save_settings(settings);
+                       settings.endGroup();
+               } else
+#endif
+               {
+                       settings.beginGroup(base->internal_name());
+                       base->save_settings(settings);
+                       settings.endGroup();
+               }
+       }
+
+       settings.setValue("decode_signals", decode_signals);
+
+       // Save view states and their signal settings
+       // Note: main_view must be saved as view0
+       settings.beginGroup("view" + QString::number(views++));
+       main_view_->save_settings(settings);
+       settings.endGroup();
+
+       for (const shared_ptr<views::ViewBase>& view : views_) {
+               if (view != main_view_) {
+                       settings.beginGroup("view" + QString::number(views++));
+                       view->save_settings(settings);
+                       settings.endGroup();
+               }
+       }
+
+       settings.setValue("views", views);
+}
+
 void Session::save_settings(QSettings &settings) const
 {
        map<string, string> dev_info;
        list<string> key_list;
-       int decode_signals = 0, views = 0;
 
        if (device_) {
                shared_ptr<devices::HardwareDevice> hw_device =
@@ -195,7 +247,7 @@ void Session::save_settings(QSettings &settings) const
 
                        dev_info = device_manager_.get_device_info(device_);
 
-                       for (string key : key_list) {
+                       for (string& key : key_list) {
                                if (dev_info.count(key))
                                        settings.setValue(QString::fromUtf8(key.c_str()),
                                                        QString::fromUtf8(dev_info.at(key).c_str()));
@@ -227,39 +279,45 @@ void Session::save_settings(QSettings &settings) const
                        settings.endGroup();
                }
 
-               // Save channels and decoders
-               for (shared_ptr<data::SignalBase> base : signalbases_) {
+               save_setup(settings);
+       }
+}
+
+void Session::restore_setup(QSettings &settings)
+{
+       // Restore channels
+       for (shared_ptr<data::SignalBase> base : signalbases_) {
+               settings.beginGroup(base->internal_name());
+               base->restore_settings(settings);
+               settings.endGroup();
+       }
+
+       // Restore decoders
 #ifdef ENABLE_DECODE
-                       if (base->is_decode_signal()) {
-                               settings.beginGroup("decode_signal" + QString::number(decode_signals++));
-                               base->save_settings(settings);
-                               settings.endGroup();
-                       } else
+       int decode_signals = settings.value("decode_signals").toInt();
+
+       for (int i = 0; i < decode_signals; i++) {
+               settings.beginGroup("decode_signal" + QString::number(i));
+               shared_ptr<data::DecodeSignal> signal = add_decode_signal();
+               signal->restore_settings(settings);
+               settings.endGroup();
+       }
 #endif
-                       {
-                               settings.beginGroup(base->internal_name());
-                               base->save_settings(settings);
-                               settings.endGroup();
-                       }
-               }
 
-               settings.setValue("decode_signals", decode_signals);
+       // Restore views
+       int views = settings.value("views").toInt();
 
-               // Save view states and their signal settings
-               // Note: main_view must be saved as view0
-               settings.beginGroup("view" + QString::number(views++));
-               main_view_->save_settings(settings);
-               settings.endGroup();
+       for (int i = 0; i < views; i++) {
+               settings.beginGroup("view" + QString::number(i));
 
-               for (shared_ptr<views::ViewBase> view : views_) {
-                       if (view != main_view_) {
-                               settings.beginGroup("view" + QString::number(views++));
-                               view->save_settings(settings);
-                               settings.endGroup();
-                       }
-               }
+               if (i > 0) {
+                       views::ViewType type = (views::ViewType)settings.value("type").toInt();
+                       add_view(name_, type, this);
+                       views_.back()->restore_settings(settings);
+               } else
+                       main_view_->restore_settings(settings);
 
-               settings.setValue("views", views);
+               settings.endGroup();
        }
 }
 
@@ -331,42 +389,8 @@ void Session::restore_settings(QSettings &settings)
                }
        }
 
-       if (device) {
-               // Restore channels
-               for (shared_ptr<data::SignalBase> base : signalbases_) {
-                       settings.beginGroup(base->internal_name());
-                       base->restore_settings(settings);
-                       settings.endGroup();
-               }
-
-               // Restore decoders
-#ifdef ENABLE_DECODE
-               int decode_signals = settings.value("decode_signals").toInt();
-
-               for (int i = 0; i < decode_signals; i++) {
-                       settings.beginGroup("decode_signal" + QString::number(i));
-                       shared_ptr<data::DecodeSignal> signal = add_decode_signal();
-                       signal->restore_settings(settings);
-                       settings.endGroup();
-               }
-#endif
-
-               // Restore views
-               int views = settings.value("views").toInt();
-
-               for (int i = 0; i < views; i++) {
-                       settings.beginGroup("view" + QString::number(i));
-
-                       if (i > 0) {
-                               views::ViewType type = (views::ViewType)settings.value("type").toInt();
-                               add_view(name_, type, this);
-                               views_.back()->restore_settings(settings);
-                       } else
-                               main_view_->restore_settings(settings);
-
-                       settings.endGroup();
-               }
-       }
+       if (device)
+               restore_setup(settings);
 }
 
 void Session::select_device(shared_ptr<devices::Device> device)
@@ -397,20 +421,21 @@ void Session::set_device(shared_ptr<devices::Device> device)
        name_ = default_name_;
        name_changed();
 
-       // Remove all stored data
+       // Remove all stored data and reset all views
        for (shared_ptr<views::ViewBase> view : views_) {
                view->clear_signals();
 #ifdef ENABLE_DECODE
                view->clear_decode_signals();
 #endif
+               view->reset_view_state();
        }
-       for (const shared_ptr<data::SignalData> d : all_signal_data_)
+       for (const shared_ptr<data::SignalData>& d : all_signal_data_)
                d->clear();
        all_signal_data_.clear();
        signalbases_.clear();
        cur_logic_segment_.reset();
 
-       for (auto entry : cur_analog_segments_) {
+       for (auto& entry : cur_analog_segments_) {
                shared_ptr<sigrok::Channel>(entry.first).reset();
                shared_ptr<data::AnalogSegment>(entry.second).reset();
        }
@@ -469,7 +494,7 @@ Session::input_format_options(vector<string> user_spec,
 {
        map<string, Glib::VariantBase> result;
 
-       for (auto entry : user_spec) {
+       for (auto& entry : user_spec) {
                /*
                 * Split key=value specs. Accept entries without separator
                 * (for simplified boolean specifications).
@@ -557,6 +582,15 @@ void Session::load_file(QString file_name,
                return;
        }
 
+       // Auto-load the setup if one exists
+       QString setup_file_name = file_name;
+       setup_file_name.truncate(setup_file_name.lastIndexOf('.'));
+       setup_file_name.append(".pvs");
+       if (QFileInfo::exists(setup_file_name) && QFileInfo(setup_file_name).isReadable()) {
+               QSettings settings_storage(setup_file_name, QSettings::IniFormat);
+               restore_setup(settings_storage);
+       }
+
        main_bar_->update_device_list();
 
        start_capture([&, errorMessage](QString infoMessage) {
@@ -593,7 +627,7 @@ void Session::start_capture(function<void (const QString)> error_handler)
        }
 
        // Clear signal data
-       for (const shared_ptr<data::SignalData> d : all_signal_data_)
+       for (const shared_ptr<data::SignalData>& d : all_signal_data_)
                d->clear();
 
        trigger_list_.clear();
@@ -642,7 +676,7 @@ void Session::register_view(shared_ptr<views::ViewBase> view)
                qobject_cast<views::trace::View*>(view.get());
 
        if (trace_view) {
-               for (shared_ptr<data::SignalBase> signalbase : signalbases_) {
+               for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) {
                        const int sb_exists = count_if(
                                view_signalbases.cbegin(), view_signalbases.cend(),
                                [&](const shared_ptr<data::SignalBase> &sb) {
@@ -683,7 +717,7 @@ void Session::deregister_view(shared_ptr<views::ViewBase> view)
 
 bool Session::has_view(shared_ptr<views::ViewBase> view)
 {
-       for (shared_ptr<views::ViewBase> v : views_)
+       for (shared_ptr<views::ViewBase>& v : views_)
                if (v == view)
                        return true;
 
@@ -694,11 +728,11 @@ double Session::get_samplerate() const
 {
        double samplerate = 0.0;
 
-       for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
+       for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) {
                assert(d);
                const vector< shared_ptr<pv::data::Segment> > segments =
                        d->segments();
-               for (const shared_ptr<pv::data::Segment> &s : segments)
+               for (const shared_ptr<pv::data::Segment>s : segments)
                        samplerate = max(samplerate, s->samplerate());
        }
        // If there is no sample rate given we use samples as unit
@@ -713,7 +747,7 @@ uint32_t Session::get_segment_count() const
        uint32_t value = 0;
 
        // Find the highest number of segments
-       for (shared_ptr<data::SignalData> data : all_signal_data_)
+       for (const shared_ptr<data::SignalData>& data : all_signal_data_)
                if (data->get_segment_count() > value)
                        value = data->get_segment_count();
 
@@ -724,7 +758,7 @@ vector<util::Timestamp> Session::get_triggers(uint32_t segment_id) const
 {
        vector<util::Timestamp> result;
 
-       for (pair<uint32_t, util::Timestamp> entry : trigger_list_)
+       for (const pair<uint32_t, util::Timestamp>& entry : trigger_list_)
                if (entry.first == segment_id)
                        result.push_back(entry.second);
 
@@ -740,7 +774,7 @@ bool Session::all_segments_complete(uint32_t segment_id) const
 {
        bool all_complete = true;
 
-       for (shared_ptr<data::SignalBase> base : signalbases_)
+       for (const shared_ptr<data::SignalBase>& base : signalbases_)
                if (!base->segment_is_complete(segment_id))
                        all_complete = false;
 
@@ -759,7 +793,7 @@ shared_ptr<data::DecodeSignal> Session::add_decode_signal()
                signalbases_.insert(signal);
 
                // Add the decode signal to all views
-               for (shared_ptr<views::ViewBase> view : views_)
+               for (shared_ptr<views::ViewBase>& view : views_)
                        view->add_decode_signal(signal);
        } catch (runtime_error& e) {
                remove_decode_signal(signal);
@@ -775,7 +809,7 @@ void Session::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
 {
        signalbases_.erase(signal);
 
-       for (shared_ptr<views::ViewBase> view : views_)
+       for (shared_ptr<views::ViewBase>& view : views_)
                view->remove_decode_signal(signal);
 
        signals_changed();
@@ -801,7 +835,7 @@ void Session::update_signals()
        if (!device_) {
                signalbases_.clear();
                logic_data_.reset();
-               for (shared_ptr<views::ViewBase> view : views_) {
+               for (shared_ptr<views::ViewBase>& view : views_) {
                        view->clear_signals();
 #ifdef ENABLE_DECODE
                        view->clear_decode_signals();
@@ -816,7 +850,7 @@ void Session::update_signals()
        if (!sr_dev) {
                signalbases_.clear();
                logic_data_.reset();
-               for (shared_ptr<views::ViewBase> view : views_) {
+               for (shared_ptr<views::ViewBase>& view : views_) {
                        view->clear_signals();
 #ifdef ENABLE_DECODE
                        view->clear_decode_signals();
@@ -847,7 +881,7 @@ void Session::update_signals()
        }
 
        // Make the signals list
-       for (shared_ptr<views::ViewBase> viewbase : views_) {
+       for (shared_ptr<views::ViewBase>& viewbase : views_) {
                views::trace::View *trace_view =
                        qobject_cast<views::trace::View*>(viewbase.get());
 
@@ -873,7 +907,7 @@ void Session::update_signals()
                                } else {
                                        // Find the signalbase for this channel if possible
                                        signalbase.reset();
-                                       for (const shared_ptr<data::SignalBase> b : signalbases_)
+                                       for (const shared_ptr<data::SignalBase>& b : signalbases_)
                                                if (b->channel() == channel)
                                                        signalbase = b;
 
@@ -946,10 +980,43 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
 {
        assert(error_handler);
 
+#ifdef ENABLE_FLOW
+       pipeline_ = Pipeline::create();
+
+       source_ = ElementFactory::create_element("filesrc", "source");
+       sink_ = RefPtr<AppSink>::cast_dynamic(ElementFactory::create_element("appsink", "sink"));
+
+       pipeline_->add(source_)->add(sink_);
+       source_->link(sink_);
+
+       source_->set_property("location", Glib::ustring("/tmp/dummy_binary"));
+
+       sink_->set_property("emit-signals", TRUE);
+       sink_->signal_new_sample().connect(sigc::mem_fun(*this, &Session::on_gst_new_sample));
+
+       // Get the bus from the pipeline and add a bus watch to the default main context
+       RefPtr<Bus> bus = pipeline_->get_bus();
+       bus->add_watch(sigc::mem_fun(this, &Session::on_gst_bus_message));
+
+       // Start pipeline and Wait until it finished processing
+       pipeline_done_interrupt_ = false;
+       pipeline_->set_state(Gst::STATE_PLAYING);
+
+       unique_lock<mutex> pipeline_done_lock_(pipeline_done_mutex_);
+       pipeline_done_cond_.wait(pipeline_done_lock_);
+
+       // Let the pipeline free all resources
+       pipeline_->set_state(Gst::STATE_NULL);
+
+#else
        if (!device_)
                return;
 
-       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+       try {
+               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+       } catch (Error& e) {
+               cur_samplerate_ = 0;
+       }
 
        out_of_memory_ = false;
 
@@ -977,6 +1044,10 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
                error_handler(e.what());
                set_capture_state(Stopped);
                return;
+       } catch (QString& e) {
+               error_handler(e);
+               set_capture_state(Stopped);
+               return;
        }
 
        set_capture_state(Stopped);
@@ -984,6 +1055,7 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
        // Confirm that SR_DF_END was received
        if (cur_logic_segment_)
                qDebug() << "WARNING: SR_DF_END was not received.";
+#endif
 
        // Optimize memory usage
        free_unused_memory();
@@ -1001,12 +1073,11 @@ void Session::sample_thread_proc(function<void (const QString)> error_handler)
 
 void Session::free_unused_memory()
 {
-       for (shared_ptr<data::SignalData> data : all_signal_data_) {
+       for (const shared_ptr<data::SignalData>& data : all_signal_data_) {
                const vector< shared_ptr<data::Segment> > segments = data->segments();
 
-               for (shared_ptr<data::Segment> segment : segments) {
+               for (const shared_ptr<data::Segment>& segment : segments)
                        segment->free_unused_memory();
-               }
        }
 }
 
@@ -1044,7 +1115,7 @@ void Session::signal_segment_completed()
 {
        int segment_id = 0;
 
-       for (shared_ptr<data::SignalBase> signalbase : signalbases_) {
+       for (const shared_ptr<data::SignalBase>& signalbase : signalbases_) {
                // We only care about analog and logic channels, not derived ones
                if (signalbase->type() == data::SignalBase::AnalogChannel) {
                        segment_id = signalbase->analog_data()->get_segment_count() - 1;
@@ -1061,6 +1132,49 @@ void Session::signal_segment_completed()
                segment_completed(segment_id);
 }
 
+#ifdef ENABLE_FLOW
+bool Session::on_gst_bus_message(const Glib::RefPtr<Gst::Bus>& bus, const Glib::RefPtr<Gst::Message>& message)
+{
+       (void)bus;
+
+       if ((message->get_source() == pipeline_) && \
+               ((message->get_message_type() == Gst::MESSAGE_EOS)))
+               pipeline_done_cond_.notify_one();
+
+       // TODO Also evaluate MESSAGE_STREAM_STATUS to receive error notifications
+
+       return true;
+}
+
+Gst::FlowReturn Session::on_gst_new_sample()
+{
+       RefPtr<Gst::Sample> sample = sink_->pull_sample();
+       RefPtr<Gst::Buffer> buf = sample->get_buffer();
+
+       for (uint32_t block_id = 0; block_id < buf->n_memory(); block_id++) {
+               RefPtr<Gst::Memory> buf_mem = buf->get_memory(block_id);
+               Gst::MapInfo mapinfo;
+               buf_mem->map(mapinfo, Gst::MAP_READ);
+
+               shared_ptr<sigrok::Packet> logic_packet =
+                       sr_context->create_logic_packet(mapinfo.get_data(), buf->get_size(), 1);
+
+               try {
+                       feed_in_logic(dynamic_pointer_cast<sigrok::Logic>(logic_packet->payload()));
+               } catch (bad_alloc&) {
+                       out_of_memory_ = true;
+                       device_->stop();
+                       buf_mem->unmap(mapinfo);
+                       return Gst::FLOW_ERROR;
+               }
+
+               buf_mem->unmap(mapinfo);
+       }
+
+       return Gst::FLOW_OK;
+}
+#endif
+
 void Session::feed_in_header()
 {
        // Nothing to do here for now
@@ -1068,7 +1182,7 @@ void Session::feed_in_header()
 
 void Session::feed_in_meta(shared_ptr<Meta> meta)
 {
-       for (auto entry : meta->config()) {
+       for (auto& entry : meta->config()) {
                switch (entry.first->id()) {
                case SR_CONF_SAMPLERATE:
                        cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
@@ -1088,7 +1202,7 @@ void Session::feed_in_trigger()
        uint64_t sample_count = 0;
 
        {
-               for (const shared_ptr<pv::data::SignalData> d : all_signal_data_) {
+               for (const shared_ptr<pv::data::SignalData>& d : all_signal_data_) {
                        assert(d);
                        uint64_t temp_count = 0;
 
@@ -1138,7 +1252,7 @@ void Session::feed_in_frame_end()
                if (cur_logic_segment_)
                        cur_logic_segment_->set_complete();
 
-               for (auto entry : cur_analog_segments_) {
+               for (auto& entry : cur_analog_segments_) {
                        shared_ptr<data::AnalogSegment> segment = entry.second;
                        segment->set_complete();
                }
@@ -1159,8 +1273,15 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
                return;
        }
 
+       if (logic->unit_size() > 8)
+               throw QString(tr("Can't handle more than 64 logic channels."));
+
        if (!cur_samplerate_)
-               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               try {
+                       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               } catch (Error& e) {
+                       // Do nothing
+               }
 
        lock_guard<recursive_mutex> lock(data_mutex_);
 
@@ -1197,23 +1318,25 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
        }
 
        if (!cur_samplerate_)
-               cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               try {
+                       cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
+               } catch (Error& e) {
+                       // Do nothing
+               }
 
        lock_guard<recursive_mutex> lock(data_mutex_);
 
        const vector<shared_ptr<Channel>> channels = analog->channels();
-       const unsigned int channel_count = channels.size();
-       const size_t sample_count = analog->num_samples() / channel_count;
        bool sweep_beginning = false;
 
-       unique_ptr<float[]> data(new float[analog->num_samples()]);
+       unique_ptr<float[]> data(new float[analog->num_samples() * channels.size()]);
        analog->get_data_as_float(data.get());
 
        if (signalbases_.empty())
                update_signals();
 
        float *channel_data = data.get();
-       for (auto channel : channels) {
+       for (auto& channel : channels) {
                shared_ptr<data::AnalogSegment> segment;
 
                // Try to get the segment of the channel
@@ -1248,8 +1371,8 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
                assert(segment);
 
                // Append the samples in the segment
-               segment->append_interleaved_samples(channel_data++, sample_count,
-                       channel_count);
+               segment->append_interleaved_samples(channel_data++, analog->num_samples(),
+                       channels.size());
        }
 
        if (sweep_beginning) {
@@ -1318,7 +1441,7 @@ void Session::data_feed_in(shared_ptr<sigrok::Device> device,
                        if (cur_logic_segment_)
                                cur_logic_segment_->set_complete();
 
-                       for (auto entry : cur_analog_segments_) {
+                       for (auto& entry : cur_analog_segments_) {
                                shared_ptr<data::AnalogSegment> segment = entry.second;
                                segment->set_complete();
                        }