]> sigrok.org Git - pulseview.git/blobdiff - pv/session.cpp
Session: Allow an SR_DF_META packet to override the samplerate
[pulseview.git] / pv / session.cpp
index bf9e3a2c0897973854223520fb37c1fae83018f7..4aba385b687d22ac8764da163b3f552792580682 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 
 #include "devicemanager.hpp"
+#include "mainwindow.hpp"
 #include "session.hpp"
 
 #include "data/analog.hpp"
@@ -300,7 +301,8 @@ void Session::restore_settings(QSettings &settings)
                        set_device(device);
 
                        start_capture([](QString infoMessage) {
-                               qDebug().noquote() << "Session error:" << infoMessage; });
+                               // TODO Emulate noquote()
+                               qDebug() << "Session error:" << infoMessage; });
 
                        set_name(QFileInfo(filename).fileName());
                }
@@ -352,7 +354,7 @@ void Session::select_device(shared_ptr<devices::Device> device)
                else
                        set_default_device();
        } catch (const QString &e) {
-               main_bar_->session_error(tr("Failed to select device"), e);
+               MainWindow::show_session_error(tr("Failed to select device"), e);
        }
 }
 
@@ -400,7 +402,7 @@ void Session::set_device(shared_ptr<devices::Device> device)
                device_->open();
        } catch (const QString &e) {
                device_.reset();
-               main_bar_->session_error(tr("Failed to open device"), e);
+               MainWindow::show_session_error(tr("Failed to open device"), e);
        }
 
        if (device_) {
@@ -490,7 +492,7 @@ void Session::load_init_file(const string &file_name, const string &format)
                        [&](const pair<string, shared_ptr<InputFormat> > f) {
                                return f.first == user_name; });
                if (iter == formats.end()) {
-                       main_bar_->session_error(tr("Error"),
+                       MainWindow::show_session_error(tr("Error"),
                                tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
                        return;
                }
@@ -509,6 +511,10 @@ void Session::load_file(QString file_name,
        const QString errorMessage(
                QString("Failed to load file %1").arg(file_name));
 
+       // In the absence of a caller's format spec, try to auto detect.
+       // Assume "sigrok session file" upon lookup miss.
+       if (!format)
+               format = device_manager_.context()->input_format_match(file_name.toStdString());
        try {
                if (format)
                        set_device(shared_ptr<devices::Device>(
@@ -522,7 +528,7 @@ void Session::load_file(QString file_name,
                                        device_manager_.context(),
                                        file_name.toStdString())));
        } catch (Error& e) {
-               main_bar_->session_error(tr("Failed to load ") + file_name, e.what());
+               MainWindow::show_session_error(tr("Failed to load ") + file_name, e.what());
                set_default_device();
                main_bar_->update_device_list();
                return;
@@ -531,7 +537,7 @@ void Session::load_file(QString file_name,
        main_bar_->update_device_list();
 
        start_capture([&, errorMessage](QString infoMessage) {
-               main_bar_->session_error(errorMessage, infoMessage); });
+               MainWindow::show_session_error(errorMessage, infoMessage); });
 
        set_name(QFileInfo(file_name).fileName());
 }
@@ -1042,15 +1048,10 @@ void Session::feed_in_meta(shared_ptr<Meta> meta)
        for (auto entry : meta->config()) {
                switch (entry.first->id()) {
                case SR_CONF_SAMPLERATE:
-                       // We can't rely on the header to always contain the sample rate,
-                       // so in case it's supplied via a meta packet, we use it.
-                       if (!cur_samplerate_)
-                               cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
-
-                       /// @todo handle samplerate changes
+                       cur_samplerate_ = g_variant_get_uint64(entry.second.gobj());
                        break;
                default:
-                       // Unknown metadata is not an error.
+                       qDebug() << "Received meta data key" << entry.first->id() << ", ignoring.";
                        break;
                }
        }
@@ -1130,6 +1131,11 @@ void Session::feed_in_frame_end()
 
 void Session::feed_in_logic(shared_ptr<Logic> logic)
 {
+       if (logic->data_length() == 0) {
+               qDebug() << "WARNING: Received logic packet with 0 samples.";
+               return;
+       }
+
        if (!cur_samplerate_)
                cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
 
@@ -1162,6 +1168,11 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
 
 void Session::feed_in_analog(shared_ptr<Analog> analog)
 {
+       if (analog->num_samples() == 0) {
+               qDebug() << "WARNING: Received analog packet with 0 samples.";
+               return;
+       }
+
        if (!cur_samplerate_)
                cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);