Try a bit of backwards compatibility in state file.
authorCarl Hetherington <cth@carlh.net>
Wed, 14 Nov 2012 23:39:00 +0000 (23:39 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 14 Nov 2012 23:39:00 +0000 (23:39 +0000)
src/lib/film.cc
src/lib/film.h
src/lib/stream.cc
src/lib/stream.h
src/wx/film_editor.cc
test/test.cc

index f45a924472a3f054746b91e59a76b77696d83fdd..8463562127254a0bc8e3d2acd1e179ecf728332c 100644 (file)
@@ -71,6 +71,8 @@ using boost::ends_with;
 using boost::starts_with;
 using boost::optional;
 
+int const Film::state_version = 1;
+
 /** Construct a Film object in a given directory, reading any metadata
  *  file that exists in that directory.  An exception will be thrown if
  *  must_exist is true and the specified directory does not exist.
@@ -397,6 +399,8 @@ Film::write_metadata () const
                throw CreateFileError (m);
        }
 
+       f << "version " << state_version << "\n";
+
        /* User stuff */
        f << "name " << _name << "\n";
        f << "use_dci_name " << _use_dci_name << "\n";
@@ -476,6 +480,9 @@ Film::read_metadata ()
        _thumbs.clear ();
        _audio_streams.clear ();
        _subtitle_streams.clear ();
+
+       boost::optional<int> version;
+       boost::optional<int> audio_sample_rate;
        
        ifstream f (file ("metadata").c_str());
        multimap<string, string> kv = read_key_value (f);
@@ -483,6 +490,12 @@ Film::read_metadata ()
                string const k = i->first;
                string const v = i->second;
 
+               if (k == "version") {
+                       version = atoi (v.c_str());
+               } else if (k == "audio_sample_rate") {
+                       audio_sample_rate = atoi (v.c_str());
+               }
+
                /* User-specified stuff */
                if (k == "name") {
                        _name = v;
@@ -515,10 +528,9 @@ Film::read_metadata ()
                } else if (k == "use_content_audio") {
                        _use_content_audio = (v == "1");
                } else if (k == "selected_audio_stream") {
-                       AudioStream st (v);
                        /* check for -1 for backwards compatibility */
-                       if (st.id() != -1) {
-                               _audio_stream = AudioStream (v);
+                       if (atoi(v.c_str()) != -1) {
+                               _audio_stream = AudioStream (v, version);
                        }
                } else if (k == "external_audio") {
                        _external_audio.push_back (v);
@@ -529,10 +541,9 @@ Film::read_metadata ()
                } else if (k == "still_duration") {
                        _still_duration = atoi (v.c_str ());
                } else if (k == "selected_subtitle_stream") {
-                       SubtitleStream st (v);
                        /* check for -1 for backwards compatibility */
-                       if (st.id() != -1) {
-                               _subtitle_stream = st;
+                       if (atoi(v.c_str()) != -1) {
+                               _subtitle_stream = SubtitleStream (v);
                        }
                } else if (k == "with_subtitles") {
                        _with_subtitles = (v == "1");
@@ -575,13 +586,20 @@ Film::read_metadata ()
                } else if (k == "content_digest") {
                        _content_digest = v;
                } else if (k == "audio_stream") {
-                       _audio_streams.push_back (AudioStream (v));
+                       _audio_streams.push_back (AudioStream (v, version));
                } else if (k == "subtitle_stream") {
                        _subtitle_streams.push_back (SubtitleStream (v));
                } else if (k == "frames_per_second") {
                        _frames_per_second = atof (v.c_str ());
                }
        }
+
+       if (!version && audio_sample_rate) {
+               /* version < 1 didn't specify sample rate in the audio streams, so fill it in here */
+               for (vector<AudioStream>::iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
+                       i->set_sample_rate (audio_sample_rate.get());
+               }
+       }
                
        _dirty = false;
 }
index 0e2a4e5efd6e002f5e9b579423ff2b732d230359..37eb13d253631a64c96a8aa27bd4ede747ad13b6 100644 (file)
@@ -369,7 +369,9 @@ public:
 
        /** Emitted when some property has changed */
        mutable boost::signals2::signal<void (Property)> Changed;
-       
+
+       static int const state_version;
+
 private:
        
        /** Log to write to */
index fd7eccfbf2bfe69bec7272632a7d1f9b7ff36fca..5bc184eb9cd5c15e4fbc8b71504c11f5d56e3af1 100644 (file)
 #include "stream.h"
 
 using namespace std;
+using boost::optional;
 
-AudioStream::AudioStream (string t)
+AudioStream::AudioStream (string t, optional<int> version)
 {
        stringstream n (t);
-       n >> _id >> _sample_rate >> _channel_layout;
+       
+       int name_index = 3;
+       if (!version) {
+               name_index = 2;
+               int channels;
+               n >> _id >> channels;
+               _channel_layout = av_get_default_channel_layout (channels);
+               _sample_rate = 0;
+       } else {
+               /* Current (marked version 1) */
+               n >> _id >> _sample_rate >> _channel_layout;
+       }
 
-       for (int i = 0; i < 3; ++i) {
+       for (int i = 0; i < name_index; ++i) {
                size_t const s = t.find (' ');
                if (s != string::npos) {
                        t = t.substr (s + 1);
index d6c4ca38270ad22f2b7183fadc5619e13b671248..760befa022e8069bdab2b98a962645171dec701d 100644 (file)
@@ -21,6 +21,7 @@
 #define DVDOMATIC_STREAM_H
 
 #include <stdint.h>
+#include <boost/optional.hpp>
 extern "C" {
 #include <libavutil/audioconvert.h>
 }
@@ -55,7 +56,7 @@ protected:
 struct AudioStream : public Stream
 {
 public:
-       AudioStream (std::string t);
+       AudioStream (std::string t, boost::optional<int> v);
        
        AudioStream (std::string n, int id, int r, int64_t l)
                : Stream (n, id)
@@ -63,6 +64,11 @@ public:
                , _channel_layout (l)
        {}
 
+       /** Only used for state file version < 1 compatibility */
+       void set_sample_rate (int r) {
+               _sample_rate = r;
+       }
+
        std::string to_string () const;
 
        int channels () const {
index d1b341bb7a57e00f1f3011d03f9737ad0d38f2da..492a267d711737b8b9deb70a78fd3475b857d80f 100644 (file)
@@ -995,7 +995,12 @@ FilmEditor::audio_stream_changed (wxCommandEvent &)
                return;
        }
 
-       _film->set_audio_stream (AudioStream (string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ()))));
+       _film->set_audio_stream (
+               AudioStream (
+                       string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ())),
+                       Film::state_version
+                       )
+               );
 }
 
 void
index 89a56872b38499c1608a7e3254b8bdf8d4a2392c..f8f51ddf538803935d121ad92915302c44918a07 100644 (file)
@@ -520,7 +520,7 @@ BOOST_AUTO_TEST_CASE (job_manager_test)
 
 BOOST_AUTO_TEST_CASE (stream_test)
 {
-       AudioStream a ("4 44100 1 hello there world");
+       AudioStream a ("4 44100 1 hello there world", boost::optional<int> (1));
        BOOST_CHECK_EQUAL (a.id(), 4);
        BOOST_CHECK_EQUAL (a.sample_rate(), 44100);
        BOOST_CHECK_EQUAL (a.channel_layout(), 1);