+ boost::mutex::scoped_lock lm (_state_mutex);
+
+ _external_audio.clear ();
+ _content_audio_streams.clear ();
+ _subtitle_streams.clear ();
+
+ boost::optional<int> version;
+
+ /* Backward compatibility things */
+ boost::optional<int> audio_sample_rate;
+ boost::optional<int> audio_stream_index;
+ boost::optional<int> subtitle_stream_index;
+
+ ifstream f (file ("metadata").c_str());
+ if (!f.good()) {
+ throw OpenFileError (file ("metadata"));
+ }
+
+ multimap<string, string> kv = read_key_value (f);
+
+ /* We need version before anything else */
+ multimap<string, string>::iterator v = kv.find ("version");
+ if (v != kv.end ()) {
+ version = atoi (v->second.c_str());
+ }
+
+ for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
+ string const k = i->first;
+ string const v = i->second;
+
+ if (k == "audio_sample_rate") {
+ audio_sample_rate = atoi (v.c_str());
+ }
+
+ /* User-specified stuff */
+ if (k == "name") {
+ _name = v;
+ } else if (k == "use_dci_name") {
+ _use_dci_name = (v == "1");
+ } else if (k == "content") {
+ _content = v;
+ } else if (k == "trust_content_header") {
+ _trust_content_header = (v == "1");
+ } else if (k == "dcp_content_type") {
+ if (version < 3) {
+ _dcp_content_type = DCPContentType::from_pretty_name (v);
+ } else {
+ _dcp_content_type = DCPContentType::from_dci_name (v);
+ }
+ } else if (k == "format") {
+ _format = Format::from_metadata (v);
+ } else if (k == "left_crop") {
+ _crop.left = atoi (v.c_str ());
+ } else if (k == "right_crop") {
+ _crop.right = atoi (v.c_str ());
+ } else if (k == "top_crop") {
+ _crop.top = atoi (v.c_str ());
+ } else if (k == "bottom_crop") {
+ _crop.bottom = atoi (v.c_str ());
+ } else if (k == "filter") {
+ _filters.push_back (Filter::from_id (v));
+ } else if (k == "scaler") {
+ _scaler = Scaler::from_id (v);
+ } else if ( ((!version || version < 2) && k == "dcp_trim_start") || k == "trim_start") {
+ _trim_start = atoi (v.c_str ());
+ } else if ( ((!version || version < 2) && k == "dcp_trim_end") || k == "trim_end") {
+ _trim_end = atoi (v.c_str ());
+ } else if (k == "dcp_ab") {
+ _dcp_ab = (v == "1");
+ } else if (k == "selected_content_audio_stream" || (!version && k == "selected_audio_stream")) {
+ if (!version) {
+ audio_stream_index = atoi (v.c_str ());
+ } else {
+ _content_audio_stream = audio_stream_factory (v, version);
+ }
+ } else if (k == "external_audio") {
+ _external_audio.push_back (v);
+ } else if (k == "use_content_audio") {
+ _use_content_audio = (v == "1");
+ } else if (k == "audio_gain") {
+ _audio_gain = atof (v.c_str ());
+ } else if (k == "audio_delay") {
+ _audio_delay = atoi (v.c_str ());
+ } else if (k == "still_duration") {
+ _still_duration = atoi (v.c_str ());
+ } else if (k == "selected_subtitle_stream") {
+ if (!version) {
+ subtitle_stream_index = atoi (v.c_str ());
+ } else {
+ _subtitle_stream = subtitle_stream_factory (v, version);
+ }
+ } else if (k == "with_subtitles") {
+ _with_subtitles = (v == "1");
+ } else if (k == "subtitle_offset") {
+ _subtitle_offset = atoi (v.c_str ());
+ } else if (k == "subtitle_scale") {
+ _subtitle_scale = atof (v.c_str ());
+ } else if (k == "colour_lut") {
+ _colour_lut = atoi (v.c_str ());
+ } else if (k == "j2k_bandwidth") {
+ _j2k_bandwidth = atoi (v.c_str ());
+ } else if (k == "dci_date") {
+ _dci_date = boost::gregorian::from_undelimited_string (v);
+ } else if (k == "dcp_frame_rate") {
+ _dcp_frame_rate = atoi (v.c_str ());