VideoSupport.vala 41.2 KB
Newer Older
1
/* Copyright 2016 Software Freedom Conservancy Inc.
2 3
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
4
 * See the COPYING file in this distribution.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 */

public errordomain VideoError {
    FILE,          // there's a problem reading the video container file (doesn't exist, no read
                   // permission, etc.)

    CONTENTS,      // we can read the container file but its contents are indecipherable (no codec,
                   // malformed data, etc.)
}

public class VideoImportParams {
    // IN:
    public File file;
    public ImportID import_id = ImportID();
    public string? md5;
20
    public time_t exposure_time_override;
21 22 23 24 25
    
    // IN/OUT:
    public Thumbnails? thumbnails;
    
    // OUT:
26
    public VideoRow row = new VideoRow();
27 28
    
    public VideoImportParams(File file, ImportID import_id, string? md5,
29
        Thumbnails? thumbnails = null, time_t exposure_time_override = 0) {
30 31 32 33
        this.file = file;
        this.import_id = import_id;
        this.md5 = md5;
        this.thumbnails = thumbnails;
34
        this.exposure_time_override = exposure_time_override;
35 36 37 38 39
    }
}

public class VideoReader {
    private const double UNKNOWN_CLIP_DURATION = -1.0;
40
    private const uint THUMBNAILER_TIMEOUT = 10000; // In milliseconds.
41 42 43

    // File extensions for video containers that pack only metadata as per the AVCHD spec
    private const string[] METADATA_ONLY_FILE_EXTENSIONS = { "bdm", "bdmv", "cpi", "mpl" };
44 45 46
    
    private double clip_duration = UNKNOWN_CLIP_DURATION;
    private Gdk.Pixbuf preview_frame = null;
47
    private File file = null;
48
    private GLib.Pid thumbnailer_pid = 0;
49
    public DateTime? timestamp { get; private set; default = null; }
50

51 52 53
    public VideoReader(File file) {
        this.file = file;
     }
54 55
    
    public static bool is_supported_video_file(File file) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        var mime_type = ContentType.guess(file.get_basename(), new uchar[0], null);
        // special case: deep-check content-type of files ending with .ogg
        if (mime_type == "audio/ogg" && file.has_uri_scheme("file")) {
            try {
                var info = file.query_info(FileAttribute.STANDARD_CONTENT_TYPE,
                                           FileQueryInfoFlags.NONE);
                var content_type = info.get_content_type();
                if (content_type != null && content_type.has_prefix ("video/")) {
                    return true;
                }
            } catch (Error error) {
                debug("Failed to query content type: %s", error.message);
            }
        }

71
        return is_supported_video_filename(file.get_basename());
72
    }
73

74
    public static bool is_supported_video_filename(string filename) {
75
        string mime_type;
76
        mime_type = ContentType.guess(filename, new uchar[0], null);
77 78
        // Guessed mp4 from filename has application/ as prefix, so check for mp4 in the end
        if (mime_type.has_prefix ("video/") || mime_type.has_suffix("mp4")) {
79 80 81
            string? extension = null;
            string? name = null;
            disassemble_filename(filename, out name, out extension);
82

83 84 85 86 87 88 89
            if (extension == null)
                return true;

            foreach (string s in METADATA_ONLY_FILE_EXTENSIONS) {
                if (utf8_ci_compare(s, extension) == 0)
                    return false;
            }
90

91 92
            return true;
        } else {
93
            debug("Skipping %s, unsupported mime type %s", filename, mime_type);
94 95
            return false;
        }
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }
    
    public static ImportResult prepare_for_import(VideoImportParams params) {
#if MEASURE_IMPORT
        Timer total_time = new Timer();
#endif
        File file = params.file;
        
        FileInfo info = null;
        try {
            info = file.query_info(DirectoryMonitor.SUPPLIED_ATTRIBUTES,
                FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
        } catch (Error err) {
            return ImportResult.FILE_ERROR;
        }
        
        if (info.get_file_type() != FileType.REGULAR)
            return ImportResult.NOT_A_FILE;
        
        if (!is_supported_video_file(file)) {
            message("Not importing %s: file is marked as a video file but doesn't have a" +
                "supported extension", file.get_path());
            
            return ImportResult.UNSUPPORTED_FORMAT;
        }
        
122
        TimeVal timestamp = info.get_modification_time();
123 124 125 126
        
        // make sure params has a valid md5
        assert(params.md5 != null);

127
        time_t exposure_time = params.exposure_time_override;
128
        string title = "";
129
        string comment = "";
130
        
131
        VideoReader reader = new VideoReader(file);
132 133
        bool is_interpretable = true;
        double clip_duration = 0.0;
134
        Gdk.Pixbuf preview_frame = reader.read_preview_frame();
135 136 137 138 139 140 141 142 143 144 145 146 147
        try {
            clip_duration = reader.read_clip_duration();
        } catch (VideoError err) {
            if (err is VideoError.FILE) {
                return ImportResult.FILE_ERROR;
            } else if (err is VideoError.CONTENTS) {
                is_interpretable = false;
                clip_duration = 0.0;
            } else {
                error("can't prepare video for import: an unknown kind of video error occurred");
            }
        }
        
148 149 150 151 152 153 154 155
        try {
            VideoMetadata metadata = reader.read_metadata();
            MetadataDateTime? creation_date_time = metadata.get_creation_date_time();
            
            if (creation_date_time != null && creation_date_time.get_timestamp() != 0)
                exposure_time = creation_date_time.get_timestamp();
            
            string? video_title = metadata.get_title();
156
            string? video_comment = metadata.get_comment();
157 158
            if (video_title != null)
                title = video_title;
159 160
            if (video_comment != null)
                comment = video_comment;
161 162 163 164
        } catch (Error err) {
            warning("Unable to read video metadata: %s", err.message);
        }
        
165 166 167 168 169 170
        if (exposure_time == 0) {
            // Use time reported by Gstreamer, if available.
            exposure_time = (time_t) (reader.timestamp != null ? 
                reader.timestamp.to_unix() : 0);
        }
        
171 172 173 174 175 176 177 178 179 180 181 182 183 184
        params.row.video_id = VideoID();
        params.row.filepath = file.get_path();
        params.row.filesize = info.get_size();
        params.row.timestamp = timestamp.tv_sec;
        params.row.width = preview_frame.width;
        params.row.height = preview_frame.height;
        params.row.clip_duration = clip_duration;
        params.row.is_interpretable = is_interpretable;
        params.row.exposure_time = exposure_time;
        params.row.import_id = params.import_id;
        params.row.event_id = EventID();
        params.row.md5 = params.md5;
        params.row.time_created = 0;
        params.row.title = title;
185
        params.row.comment = comment;
186 187
        params.row.backlinks = "";
        params.row.time_reimported = 0;
188
        params.row.flags = 0;
189 190 191 192 193 194 195 196 197 198 199 200 201

        if (params.thumbnails != null) {
            params.thumbnails = new Thumbnails();
            ThumbnailCache.generate_for_video_frame(params.thumbnails, preview_frame);
        }
        
#if MEASURE_IMPORT
        debug("IMPORT: total time to import video = %lf", total_time.elapsed());
#endif
        return ImportResult.SUCCESS;
    }
    
    private void read_internal() throws VideoError {
202
        if (!does_file_exist())
203
            throw new VideoError.FILE("video file '%s' does not exist or is inaccessible".printf(
204 205 206
                file.get_path()));
        
        try {
207 208
            Gst.PbUtils.Discoverer d = new Gst.PbUtils.Discoverer((Gst.ClockTime) (Gst.SECOND * 5));
            Gst.PbUtils.DiscovererInfo info = d.discover_uri(file.get_uri());
209 210 211 212 213 214 215
            
            clip_duration = ((double) info.get_duration()) / 1000000000.0;
            
            // Get creation time.
            // TODO: Note that TAG_DATE can be changed to TAG_DATE_TIME in the future
            // (and the corresponding output struct) in order to implement #2836.
            Date? video_date = null;
216
            if (info.get_tags() != null && info.get_tags().get_date(Gst.Tags.DATE, out video_date)) {
217 218 219 220 221
                // possible for get_date() to return true and a null Date
                if (video_date != null) {
                    timestamp = new DateTime.local(video_date.get_year(), video_date.get_month(),
                        video_date.get_day(), 0, 0, 0);
                }
222 223 224 225 226 227
            }
        } catch (Error e) {
            debug("Video read error: %s", e.message);
            throw new VideoError.CONTENTS("GStreamer couldn't extract clip information: %s"
                .printf(e.message));
        }
228 229
    }
    
230 231 232 233 234
    // Used by thumbnailer() to kill the external process if need be.
    private bool on_thumbnailer_timer() {
        debug("Thumbnailer timer called");
        if (thumbnailer_pid != 0) {
            debug("Killing thumbnailer process: %d", thumbnailer_pid);
235
#if VALA_0_40
Jens Georg's avatar
Jens Georg committed
236
            Posix.kill(thumbnailer_pid, Posix.Signal.KILL);
237 238 239
#else
            Posix.kill(thumbnailer_pid, Posix.SIGKILL);
#endif
240 241 242 243 244 245
        }
        return false; // Don't call again.
    }
    
    // Performs video thumbnailing.
    // Note: not thread-safe if called from the same instance of the class.
246
    private Gdk.Pixbuf? thumbnailer(string video_file) {
247 248 249
        // Use Shotwell's thumbnailer, redirect output to stdout.
        debug("Launching thumbnailer process: %s", AppDirs.get_thumbnailer_bin().get_path());
        string[] argv = {AppDirs.get_thumbnailer_bin().get_path(), video_file};
250
        int child_stdout;
251
        try {
252
            GLib.Process.spawn_async_with_pipes(null, argv, null, GLib.SpawnFlags.SEARCH_PATH | 
253 254
                GLib.SpawnFlags.DO_NOT_REAP_CHILD, null, out thumbnailer_pid, null, out child_stdout,
                null);
255
            debug("Spawned thumbnailer, child pid: %d", (int) thumbnailer_pid);
256 257
        } catch (Error e) {
            debug("Error spawning process: %s", e.message);
258 259
            if (thumbnailer_pid != 0)
                GLib.Process.close_pid(thumbnailer_pid);
260 261 262
            return null;
        }
        
263 264 265
        // Start timer.
        Timeout.add(THUMBNAILER_TIMEOUT, on_thumbnailer_timer);
        
266 267 268
        // Read pixbuf from stream.
        Gdk.Pixbuf? buf = null;
        try {
269
            GLib.UnixInputStream unix_input = new GLib.UnixInputStream(child_stdout, true);
270 271
            buf = new Gdk.Pixbuf.from_stream(unix_input, null);
        } catch (Error e) {
272
            debug("Error creating pixbuf: %s", e.message);
273 274 275 276 277
            buf = null;
        }
        
        // Make sure process exited properly.
        int child_status = 0;
278
        int ret_waitpid = Posix.waitpid(thumbnailer_pid, out child_status, 0);
279 280 281
        if (ret_waitpid < 0) {
            debug("waitpid returned error code: %d", ret_waitpid);
            buf = null;
282 283 284
        } else if (0 != Process.exit_status(child_status)) {
            debug("Thumbnailer exited with error code: %d",
                    Process.exit_status(child_status));
285 286 287
            buf = null;
        }
        
288 289
        GLib.Process.close_pid(thumbnailer_pid);
        thumbnailer_pid = 0;
290
        return buf;
291 292
    }
    
293
    private bool does_file_exist() {
294
        return FileUtils.test(file.get_path(), FileTest.EXISTS | FileTest.IS_REGULAR);
295 296
    }
    
297 298 299 300 301 302 303 304
    public Gdk.Pixbuf? read_preview_frame() {
        if (preview_frame != null)
            return preview_frame;
        
        if (!does_file_exist())
            return null;
        
        // Get preview frame from thumbnailer.
305
        preview_frame = thumbnailer(file.get_path());
306 307 308
        if (null == preview_frame)
            preview_frame = Resources.get_noninterpretable_badge_pixbuf();
        
309 310 311 312 313 314 315 316 317
        return preview_frame;
    }
    
    public double read_clip_duration() throws VideoError {
        if (clip_duration == UNKNOWN_CLIP_DURATION)
            read_internal();

        return clip_duration;
    }
318 319 320
    
    public VideoMetadata read_metadata() throws Error {
        VideoMetadata metadata = new VideoMetadata();
321
        metadata.read_from_file(File.new_for_path(file.get_path()));
322 323 324
        
        return metadata;
    }
325 326
}

327
public class Video : VideoSource, Flaggable, Monitorable, Dateable {
Jim Nelson's avatar
Jim Nelson committed
328 329
    public const string TYPENAME = "video";
    
330 331
    public const uint64 FLAG_TRASH =    0x0000000000000001;
    public const uint64 FLAG_OFFLINE =  0x0000000000000002;
Jim Nelson's avatar
Jim Nelson committed
332 333
    public const uint64 FLAG_FLAGGED =  0x0000000000000004;
    
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    public class InterpretableResults {
        internal Video video;
        internal bool update_interpretable = false;
        internal bool is_interpretable = false;
        internal Gdk.Pixbuf? new_thumbnail = null;
        
        public InterpretableResults(Video video) {
            this.video = video;
        }
        
        public void foreground_finish() {
            if (update_interpretable)
                video.set_is_interpretable(is_interpretable);
            
            if (new_thumbnail != null) {
                try {
                    ThumbnailCache.replace(video, ThumbnailCache.Size.BIG, new_thumbnail);
                    ThumbnailCache.replace(video, ThumbnailCache.Size.MEDIUM, new_thumbnail);
                    
                    video.notify_thumbnail_altered();
                } catch (Error err) {
                    message("Unable to update video thumbnails for %s: %s", video.to_string(),
                        err.message);
                }
            }
        }
    }
    
362 363 364 365 366
    private static bool interpreter_state_changed;
    private static int current_state;
    private static bool normal_regen_complete;
    private static bool offline_regen_complete;
    public static VideoSourceCollection global;
367 368 369 370 371

    private VideoRow backing_row;
    
    public Video(VideoRow row) {
        this.backing_row = row;
372
        
373 374 375
        // normalize user text
        this.backing_row.title = prep_title(this.backing_row.title);
        
376 377
        if (((row.flags & FLAG_TRASH) != 0) || ((row.flags & FLAG_OFFLINE) != 0))
            rehydrate_backlinks(global, row.backlinks);
378 379
    }

380
    public static void init(ProgressMonitor? monitor = null) {
381 382 383 384 385 386 387 388
        // Must initialize static variables here.
        // TODO: set values at declaration time once the following Vala bug is fixed:
        //       https://bugzilla.gnome.org/show_bug.cgi?id=655594
        interpreter_state_changed = false;
        current_state = -1;
        normal_regen_complete = false;
        offline_regen_complete = false;
    
389 390
        // initialize GStreamer, but don't pass it our actual command line arguments -- we don't
        // want our end users to be able to parameterize the GStreamer configuration
Jens Georg's avatar
Jens Georg committed
391 392 393 394
        unowned string[] args = null;
        Gst.init(ref args);

        var registry = Gst.Registry.@get ();
395
        int saved_state = Config.Facade.get_instance().get_video_interpreter_state_cookie();
Jens Georg's avatar
Jens Georg committed
396
        current_state = (int) registry.get_feature_list_cookie();
397 398 399 400 401 402 403
        if (saved_state == Config.Facade.NO_VIDEO_INTERPRETER_STATE) {
            message("interpreter state cookie not found; assuming all video thumbnails are out of date");
            interpreter_state_changed = true;
        } else if (saved_state != current_state) {
            message("interpreter state has changed; video thumbnails may be out of date");
            interpreter_state_changed = true;
        }
Jens Georg's avatar
Jens Georg committed
404 405 406 407 408 409 410

        /* First do the cookie state handling, then update our local registry
         * to not include vaapi stuff. This is basically to work-around
         * concurrent access to VAAPI/X11 which it doesn't like, cf
         * https://bugzilla.gnome.org/show_bug.cgi?id=762416
         */

Jens Georg's avatar
Jens Georg committed
411 412 413
        var features = registry.feature_filter ((f) => {
            return f.get_name ().has_prefix ("vaapi");
        }, false);
Jens Georg's avatar
Jens Georg committed
414

Jens Georg's avatar
Jens Georg committed
415 416
        foreach (var feature in features) {
            debug ("Removing registry feature %s", feature.get_name ());
Jens Georg's avatar
Jens Georg committed
417 418 419
            registry.remove_feature (feature);
        }

420
        global = new VideoSourceCollection();
421
        
422 423
        Gee.ArrayList<VideoRow?> all = VideoTable.get_instance().get_all();
        Gee.ArrayList<Video> all_videos = new Gee.ArrayList<Video>();
424 425
        Gee.ArrayList<Video> trashed_videos = new Gee.ArrayList<Video>();
        Gee.ArrayList<Video> offline_videos = new Gee.ArrayList<Video>();
426 427 428
        int count = all.size;
        for (int ctr = 0; ctr < count; ctr++) {
            Video video = new Video(all.get(ctr));
429
            
430 431 432
            if (interpreter_state_changed)
                video.set_is_interpretable(false);
            
433 434 435 436 437 438
            if (video.is_trashed())
                trashed_videos.add(video);
            else if (video.is_offline())
                offline_videos.add(video);
            else
                all_videos.add(video);
439 440 441
            
            if (monitor != null)
                monitor(ctr, count);
442
        }
443 444 445

        global.add_many_to_trash(trashed_videos);
        global.add_many_to_offline(offline_videos);
446
        global.add_many(all_videos);
447
    }
448
    
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    public static bool has_interpreter_state_changed() {
        return interpreter_state_changed;
    }
    
    public static void notify_normal_thumbs_regenerated() {
        if (normal_regen_complete)
            return;

        message("normal video thumbnail regeneration completed");

        normal_regen_complete = true;
        if (normal_regen_complete && offline_regen_complete)
            save_interpreter_state();
    }

    public static void notify_offline_thumbs_regenerated() {
        if (offline_regen_complete)
            return;

        message("offline video thumbnail regeneration completed");

        offline_regen_complete = true;
        if (normal_regen_complete && offline_regen_complete)
            save_interpreter_state();
    }

    private static void save_interpreter_state() {
        if (interpreter_state_changed) {
            message("saving video interpreter state to configuration system");

Lucas Beeler's avatar
Lucas Beeler committed
479
            Config.Facade.get_instance().set_video_interpreter_state_cookie(current_state);
480 481
            interpreter_state_changed = false;
        }
482 483 484 485
    }

    public static void terminate() {
    }
486
    
Lucas Beeler's avatar
Lucas Beeler committed
487 488
    public static ExporterUI? export_many(Gee.Collection<Video> videos, Exporter.CompletionCallback done,
        bool export_in_place = false) {       
489 490
        if (videos.size == 0)
            return null;
Lucas Beeler's avatar
Lucas Beeler committed
491
        
Clint Rogers's avatar
Clint Rogers committed
492
        // in place export is relatively easy -- provide a fast, separate code path for it
Lucas Beeler's avatar
Lucas Beeler committed
493 494
        if (export_in_place) {
             ExporterUI temp_exporter = new ExporterUI(new Exporter.for_temp_file(videos,
495
                Scaling.for_original(), ExportFormatParameters.unmodified()));
Lucas Beeler's avatar
Lucas Beeler committed
496 497 498
             temp_exporter.export(done);
             return temp_exporter;
        }
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

        // one video
        if (videos.size == 1) {
            Video video = null;
            foreach (Video v in videos) {
                video = v;
                break;
            }
            
            File save_as = ExportUI.choose_file(video.get_basename());
            if (save_as == null)
                return null;
            
            try {
                AppWindow.get_instance().set_busy_cursor();
                video.export(save_as);
                AppWindow.get_instance().set_normal_cursor();
            } catch (Error err) {
                AppWindow.get_instance().set_normal_cursor();
                export_error_dialog(save_as, false);
            }
            
            return null;
        }

        // multiple videos
525
        File export_dir = ExportUI.choose_dir(_("Export Videos"));
526 527 528 529
        if (export_dir == null)
            return null;
        
        ExporterUI exporter = new ExporterUI(new Exporter(videos, export_dir,
530
            Scaling.for_original(), ExportFormatParameters.unmodified()));
531 532 533 534
        exporter.export(done);

        return exporter;
    }
535

536 537 538 539 540 541 542 543 544 545 546
    protected override void commit_backlinks(SourceCollection? sources, string? backlinks) {        
        try {
            VideoTable.get_instance().update_backlinks(get_video_id(), backlinks);
            lock (backing_row) {
                backing_row.backlinks = backlinks;
            }
        } catch (DatabaseError err) {
            warning("Unable to update link state for %s: %s", to_string(), err.message);
        }
    }

547
    protected override bool set_event_id(EventID event_id) {
548 549 550 551 552 553 554 555 556 557
        lock (backing_row) {
            bool committed = VideoTable.get_instance().set_event(backing_row.video_id, event_id);

            if (committed)
                backing_row.event_id = event_id;

            return committed;
        }
    }

558 559 560 561 562 563 564 565 566 567
    public static bool is_duplicate(File? file, string? full_md5) {
        assert(file != null || full_md5 != null);
#if !NO_DUPE_DETECTION
        return VideoTable.get_instance().has_duplicate(file, full_md5);
#else
        return false;
#endif
    }
    
    public static ImportResult import_create(VideoImportParams params, out Video video) {
568 569
        video = null;
        
570 571
        // add to the database
        try {
572
            if (VideoTable.get_instance().add(params.row).is_invalid())
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
                return ImportResult.DATABASE_ERROR;
        } catch (DatabaseError err) {
            return ImportResult.DATABASE_ERROR;
        }
        
        // create local object but don't add to global until thumbnails generated
        video = new Video(params.row);

        return ImportResult.SUCCESS;
    }
    
    public static void import_failed(Video video) {
        try {
            VideoTable.get_instance().remove(video.get_video_id());
        } catch (DatabaseError err) {
            AppWindow.database_error(err);
        }
    }
591 592 593 594 595 596 597 598 599 600 601
    
    public override BackingFileState[] get_backing_files_state() {
        BackingFileState[] backing = new BackingFileState[1];
        lock (backing_row) {
            backing[0] = new BackingFileState(backing_row.filepath, backing_row.filesize, 
                backing_row.timestamp, backing_row.md5);
        }
        
        return backing;
    }
    
602 603 604
    public override Gdk.Pixbuf? get_thumbnail(int scale) throws Error {
        return ThumbnailCache.fetch(this, scale);
    }
605 606 607 608 609 610 611

    public override string get_master_md5() {
        lock (backing_row) {
            return backing_row.md5;
        }
    }

612 613 614 615 616 617
    public override Gdk.Pixbuf get_preview_pixbuf(Scaling scaling) throws Error {
        Gdk.Pixbuf pixbuf = get_thumbnail(ThumbnailCache.Size.BIG);
        
        return scaling.perform_on_pixbuf(pixbuf, Gdk.InterpType.NEAREST, true);
    }

618
    public override Gdk.Pixbuf? create_thumbnail(int scale) throws Error {
619
        VideoReader reader = new VideoReader(get_file());
620
        Gdk.Pixbuf? frame = reader.read_preview_frame();
621
        
622
        return (frame != null) ? frame : Resources.get_noninterpretable_badge_pixbuf().copy();
623 624
    }
    
625
    public override string get_typename() {
626
        return TYPENAME;
627 628 629 630
    }
    
    public override int64 get_instance_id() {
        return get_video_id().id;
631
    }
632 633 634 635 636 637 638

    public override ImportID get_import_id() {
        lock (backing_row) {
            return backing_row.import_id;
        }
    }

639 640 641 642
    public override PhotoFileFormat get_preferred_thumbnail_format() {
        return PhotoFileFormat.get_system_default_format();
    }
    
643 644 645 646 647 648 649
    public override string? get_title() {
        lock (backing_row) {
            return backing_row.title;
        }
    }

    public override void set_title(string? title) {
650 651
        string? new_title = prep_title(title);
        
652
        lock (backing_row) {
653
            if (backing_row.title == new_title)
654 655 656
                return;

            try {
657
                VideoTable.get_instance().set_title(backing_row.video_id, new_title);
658 659
            } catch (DatabaseError e) {
                AppWindow.database_error(e);
660
                return;
661 662 663
            }
            // if we didn't short-circuit return in the catch clause above, then the change was
            // successfully committed to the database, so update it in the in-memory row cache
664
            backing_row.title = new_title;
665 666 667 668 669
        }

        notify_altered(new Alteration("metadata", "name"));
    }

670 671 672 673 674 675
    public override string? get_comment() {
        lock (backing_row) {
            return backing_row.comment;
        }
    }

676
    public override bool set_comment(string? comment) {
677 678 679 680
        string? new_comment = prep_title(comment);
        
        lock (backing_row) {
            if (backing_row.comment == new_comment)
681
                return true;
682 683 684 685 686

            try {
                VideoTable.get_instance().set_comment(backing_row.video_id, new_comment);
            } catch (DatabaseError e) {
                AppWindow.database_error(e);
687
                return false;
688 689 690 691 692
            }
            // if we didn't short-circuit return in the catch clause above, then the change was
            // successfully committed to the database, so update it in the in-memory row cache
            backing_row.comment = new_comment;
        }
693
        
694
        notify_altered(new Alteration("metadata", "comment"));
695 696

        return true;
697 698 699
    }


700 701 702 703 704 705 706 707 708 709 710
    public override Rating get_rating() {
        lock (backing_row) {
            return backing_row.rating;
        }
    }

    public override void set_rating(Rating rating) {
        lock (backing_row) {
            if ((!rating.is_valid()) || (rating == backing_row.rating))
                return;

Jim Nelson's avatar
Jim Nelson committed
711
            try {
712 713
                VideoTable.get_instance().set_rating(get_video_id(), rating);
            } catch (DatabaseError e) {
Jim Nelson's avatar
Jim Nelson committed
714 715
                AppWindow.database_error(e);
                return;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
            }
            // if we didn't short-circuit return in the catch clause above, then the change was
            // successfully committed to the database, so update it in the in-memory row cache
            backing_row.rating = rating;
        }

        notify_altered(new Alteration("metadata", "rating"));
    }

    public override void increase_rating() {
        lock (backing_row) {
            set_rating(backing_row.rating.increase());
        }
    }

    public override void decrease_rating() {
        lock (backing_row) {
            set_rating(backing_row.rating.decrease());
        }
    }

737 738 739 740 741 742 743 744 745 746 747 748 749 750
    public override bool is_trashed() {
        return is_flag_set(FLAG_TRASH);
    }

    public override bool is_offline() {
        return is_flag_set(FLAG_OFFLINE);
    }

    public override void mark_offline() {
        add_flags(FLAG_OFFLINE);
    }
    
    public override void mark_online() {
        remove_flags(FLAG_OFFLINE);
751
        
752
        if ((!get_is_interpretable()) && has_interpreter_state_changed())
753
            check_is_interpretable().foreground_finish();
754 755 756 757 758 759 760 761 762
    }

    public override void trash() {
        add_flags(FLAG_TRASH);
    }
    
    public override void untrash() {
        remove_flags(FLAG_TRASH);
    }
763
    
Jim Nelson's avatar
Jim Nelson committed
764 765 766 767 768 769 770 771 772 773 774 775
    public bool is_flagged() {
        return is_flag_set(FLAG_FLAGGED);
    }
    
    public void mark_flagged() {
        add_flags(FLAG_FLAGGED, new Alteration("metadata", "flagged"));
    }
    
    public void mark_unflagged() {
        remove_flags(FLAG_FLAGGED, new Alteration("metadata", "flagged"));
    }
    
776 777 778 779 780
    public override EventID get_event_id() {
        lock (backing_row) {
            return backing_row.event_id;
        }
    }
781
    
782 783
    public override string to_string() {
        lock (backing_row) {
784
            return "[%s] %s".printf(backing_row.video_id.id.to_string(), backing_row.filepath);
785 786 787 788 789 790 791 792 793
        }
    }
    
    public VideoID get_video_id() {
        lock (backing_row) {
            return backing_row.video_id;
        }
    }
    
794
    public override time_t get_exposure_time() {
795 796 797 798 799
        lock (backing_row) {
            return backing_row.exposure_time;
        }
    }
    
800 801 802 803 804 805 806 807 808 809 810 811 812
    public void set_exposure_time(time_t time) {
        lock (backing_row) {
            try {
                VideoTable.get_instance().set_exposure_time(backing_row.video_id, time);
            } catch (Error e) {
                debug("Warning - %s", e.message);
            }
            backing_row.exposure_time = time;
        }
        
        notify_altered(new Alteration("metadata", "exposure-time"));
    }    
    
813 814 815 816 817
    public Dimensions get_frame_dimensions() {
        lock (backing_row) {
            return Dimensions(backing_row.width, backing_row.height);
        }
    }
818

Clint Rogers's avatar
Clint Rogers committed
819
    public override Dimensions get_dimensions(Photo.Exception disallowed_steps = Photo.Exception.NONE) {
820 821
        return get_frame_dimensions();
    }
822
    
823
    public override uint64 get_filesize() {
824 825 826 827
        return get_master_filesize();
    }
    
    public override uint64 get_master_filesize() {
828 829 830 831 832
        lock (backing_row) {
            return backing_row.filesize;
        }
    }
    
833 834 835 836 837 838
    public override time_t get_timestamp() {
        lock (backing_row) {
            return backing_row.timestamp;
        }
    }
    
839
    public void set_master_timestamp(FileInfo info) {
840
        TimeVal time_val = info.get_modification_time();
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
        
        try {
            lock (backing_row) {
                if (backing_row.timestamp == time_val.tv_sec)
                    return;
                
                VideoTable.get_instance().set_timestamp(backing_row.video_id, time_val.tv_sec);
                backing_row.timestamp = time_val.tv_sec;
            }
        } catch (DatabaseError err) {
            AppWindow.database_error(err);
            
            return;
        }
        
        notify_altered(new Alteration("metadata", "master-timestamp"));
    }
    
859 860 861 862 863 864 865 866 867 868
    public string get_filename() {
        lock (backing_row) {
            return backing_row.filepath;
        }
    }
    
    public override File get_file() {
        return File.new_for_path(get_filename());
    }
    
869 870 871 872
    public override File get_master_file() {
        return get_file();
    }
    
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    public void export(File dest_file) throws Error {
        File source_file = File.new_for_path(get_filename());
        source_file.copy(dest_file, FileCopyFlags.OVERWRITE | FileCopyFlags.TARGET_DEFAULT_PERMS,
            null, null);
    }
    
    public double get_clip_duration() {
        lock (backing_row) {
            return backing_row.clip_duration;
        }
    }
    
    public bool get_is_interpretable() {
        lock (backing_row) {
            return backing_row.is_interpretable;
        }
    }
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    
    private void set_is_interpretable(bool is_interpretable) {
        lock (backing_row) {
            if (backing_row.is_interpretable == is_interpretable)
                return;
            
            backing_row.is_interpretable = is_interpretable;
        }
        
        try {
            VideoTable.get_instance().update_is_interpretable(get_video_id(), is_interpretable);
        } catch (DatabaseError e) {
            AppWindow.database_error(e);
        }
    }
905 906 907 908 909 910 911
    
    // Intended to be called from a background thread but can be called from foreground as well.
    // Caller should call InterpretableResults.foreground_process() only from foreground thread,
    // however
    public InterpretableResults check_is_interpretable() {
        InterpretableResults results = new InterpretableResults(this);
        
912 913
        double clip_duration = -1.0;
        Gdk.Pixbuf? preview_frame = null;
914 915
        
        VideoReader backing_file_reader = new VideoReader(get_file());
916 917 918 919
        try {
            clip_duration = backing_file_reader.read_clip_duration();
            preview_frame = backing_file_reader.read_preview_frame();
        } catch (VideoError e) {
920 921 922 923 924 925
            // if we catch an error on an interpretable video here, then this video is
            // non-interpretable (e.g. its codec is not present on the users system).
            results.update_interpretable = get_is_interpretable();
            results.is_interpretable = false;
            
            return results;
926
        }
927 928 929 930 931 932 933
        
        // if already marked interpretable, this is only confirming what we already knew
        if (get_is_interpretable()) {
            results.update_interpretable = false;
            results.is_interpretable = true;
            
            return results;
934
        }
935 936 937 938
        
        debug("video %s has become interpretable", get_file().get_basename());
        
        // save this here, this can be done in background thread
939 940 941 942
        lock (backing_row) {
            backing_row.clip_duration = clip_duration;
        }
        
943 944 945 946 947
        results.update_interpretable = true;
        results.is_interpretable = true;
        results.new_thumbnail = preview_frame;
        
        return results;
948
    }
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
    
    public override void destroy() {
        VideoID video_id = get_video_id();

        ThumbnailCache.remove(this);
        
        try {
            VideoTable.get_instance().remove(video_id);
        } catch (DatabaseError err) {
            error("failed to remove video %s from video table", to_string());
        }
        
        base.destroy();
    }

964
    protected override bool internal_delete_backing() throws Error {
965
        bool ret = delete_original_file();
966
        
967 968
        // Return false if parent method failed.
        return base.internal_delete_backing() && ret;
969 970
    }
    
Jim Nelson's avatar
Jim Nelson committed
971 972 973 974
    private void notify_flags_altered(Alteration? additional_alteration) {
        Alteration alteration = new Alteration("metadata", "flags");
        if (additional_alteration != null)
            alteration = alteration.compress(additional_alteration);
975
        
Jim Nelson's avatar
Jim Nelson committed
976 977 978 979 980
        notify_altered(alteration);
    }
    
    public uint64 add_flags(uint64 flags_to_add, Alteration? additional_alteration = null) {
        uint64 new_flags;
981 982 983 984
        lock (backing_row) {
            new_flags = internal_add_flags(backing_row.flags, flags_to_add);
            if (backing_row.flags == new_flags)
                return backing_row.flags;
Jim Nelson's avatar
Jim Nelson committed
985
            
986 987 988 989 990 991
            try {
                VideoTable.get_instance().set_flags(get_video_id(), new_flags);
            } catch (DatabaseError e) {
                AppWindow.database_error(e);
                return backing_row.flags;
            }
Jim Nelson's avatar
Jim Nelson committed
992
            
993 994 995
            backing_row.flags = new_flags;
        }
        
Jim Nelson's avatar
Jim Nelson committed
996
        notify_flags_altered(additional_alteration);
997
        
Jim Nelson's avatar
Jim Nelson committed
998
        return new_flags;
999 1000
    }
    
Jim Nelson's avatar
Jim Nelson committed
1001 1002
    public uint64 remove_flags(uint64 flags_to_remove, Alteration? additional_alteration = null) {
        uint64 new_flags;
1003 1004 1005 1006
        lock (backing_row) {
            new_flags = internal_remove_flags(backing_row.flags, flags_to_remove);
            if (backing_row.flags == new_flags)
                return backing_row.flags;
Jim Nelson's avatar
Jim Nelson committed
1007
            
1008 1009 1010 1011 1012 1013
            try {
                VideoTable.get_instance().set_flags(get_video_id(), new_flags);
            } catch (DatabaseError e) {
                AppWindow.database_error(e);
                return backing_row.flags;
            }
Jim Nelson's avatar
Jim Nelson committed
1014
            
1015 1016 1017
            backing_row.flags = new_flags;
        }
        
Jim Nelson's avatar
Jim Nelson committed
1018
        notify_flags_altered(additional_alteration);
1019
        
Jim Nelson's avatar
Jim Nelson committed
1020
        return new_flags;
1021 1022 1023 1024 1025 1026 1027 1028
    }
    
    public bool is_flag_set(uint64 flag) {
        lock (backing_row) {
            return internal_is_flag_set(backing_row.flags, flag);
        }
    }
    
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    public void set_master_file(File file) {
        string new_filepath = file.get_path();
        string? old_filepath = null;
        try {
            lock (backing_row) {
                if (backing_row.filepath == new_filepath)
                    return;
                
                old_filepath = backing_row.filepath;
                
                VideoTable.get_instance().set_filepath(backing_row.video_id, new_filepath);
                backing_row.filepath = new_filepath;
            }
        } catch (DatabaseError err) {
            AppWindow.database_error(err);
            
            return;
        }
        
        assert(old_filepath != null);
        notify_master_replaced(File.new_for_path(old_filepath), file);
        
        notify_altered(new Alteration.from_list("backing:master,metadata:name"));
1052
    }
1053 1054
    
    public VideoMetadata read_metadata() throws Error {
1055
        return (new VideoReader(get_file())).read_metadata();
1056
    }
1057 1058
}

1059
public class VideoSourceCollection : MediaSourceCollection {
1060
    public enum State {
1061
        UNKNOWN,
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
        ONLINE,
        OFFLINE,
        TRASH
    }
    
    public override TransactionController transaction_controller {
        get {
            if (_transaction_controller == null)
                _transaction_controller = new MediaSourceTransactionController(this);
            
            return _transaction_controller;
        }
    }
    
    private TransactionController _transaction_controller = null;
1077 1078
    private Gee.MultiMap<uint64?, Video> filesize_to_video =
        new Gee.TreeMultiMap<uint64?, Video>(uint64_compare);
1079
    
1080 1081
    public VideoSourceCollection() {
        base("VideoSourceCollection", get_video_key);
1082

1083 1084
        get_trashcan().contents_altered.connect(on_trashcan_contents_altered);
        get_offline_bin().contents_altered.connect(on_offline_contents_altered);
1085 1086
    }
    
1087
    protected override MediaSourceHoldingTank create_trashcan() {
1088 1089
        return new MediaSourceHoldingTank(this, is_video_trashed, get_video_key);
    }
1090
    
1091
    protected override MediaSourceHoldingTank create_offline_bin() {
1092 1093
        return new MediaSourceHoldingTank(this, is_video_offline, get_video_key);
    }
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
    
    public override MediaMonitor create_media_monitor(Workers workers, Cancellable cancellable) {
        return new VideoMonitor(cancellable);
    }
    
    public override bool holds_type_of_source(DataSource source) {
        return source is Video;
    }
    
    public override string get_typename() {
        return Video.TYPENAME;
    }
    
    public override bool is_file_recognized(File file) {
        return VideoReader.is_supported_video_file(file);
    }
    
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    private void on_trashcan_contents_altered(Gee.Collection<DataSource>? added,
        Gee.Collection<DataSource>? removed) {
        trashcan_contents_altered((Gee.Collection<Video>?) added,
            (Gee.Collection<Video>?) removed);
    }

    private void on_offline_contents_altered(Gee.Collection<DataSource>? added,
        Gee.Collection<DataSource>? removed) {
        offline_contents_altered((Gee.Collection<Video>?) added,
            (Gee.Collection<Video>?) removed);
    }

1123 1124 1125 1126
    protected override MediaSource? fetch_by_numeric_id(int64 numeric_id) {
        return fetch(VideoID(numeric_id));
    }

1127 1128 1129 1130 1131 1132
    public static int64 get_video_key(DataSource source) {
        Video video = (Video) source;
        VideoID video_id = video.get_video_id();
        
        return video_id.id;
    }
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    
    public static bool is_video_trashed(DataSource source) {
        return ((Video) source).is_trashed();
    }
    
    public static bool is_video_offline(DataSource source) {
        return ((Video) source).is_offline();
    }
    
    public Video fetch(VideoID video_id) {
        return (Video) fetch_by_key(video_id.id);
    }
    
    public override Gee.Collection<string> get_event_source_ids(EventID event_id){
        return VideoTable.get_instance().get_event_source_ids(event_id);
    }
    
    public Video? get_state_by_file(File file, out State state) {
        Video? video = (Video?) fetch_by_master_file(file);
        if (video != null) {
            state = State.ONLINE;
            
1155
            return video;
1156
        }
1157
        
1158
        video = (Video?) get_trashcan().fetch_by_master_file(file);
1159 1160 1161
        if (video != null) {
            state = State.TRASH;
            
1162
            return video;
1163
        }
1164
        
1165
        video = (Video?) get_offline_bin().fetch_by_master_file(file);
1166
        if (video != null) {
1167 1168
            state = State.OFFLINE;
            
1169 1170 1171
            return video;
        }
        
1172 1173
        state = State.UNKNOWN;
        
1174 1175
        return null;
    }
1176
    
1177 1178 1179 1180
    private void compare_backing(Video video, FileInfo info, Gee.Collection<Video> matching_master) {
        if (video.get_filesize() != info.get_size())
            return;
        
1181
        if (video.get_timestamp() == info.get_modification_time().tv_sec)
1182
            matching_master.add(video);
1183
    }
1184
    
1185 1186 1187 1188 1189 1190
    public void fetch_by_matching_backing(FileInfo info, Gee.Collection<Video> matching_master) {
        foreach (DataObject object in get_all())
            compare_backing((Video) object, info, matching_master);
        
        foreach (MediaSource media in get_offline_bin_contents())
            compare_backing((Video) media, info, matching_master);
1191
    }
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    
    protected override void notify_contents_altered(Gee.Iterable<DataObject>? added,
        Gee.Iterable<DataObject>? removed) {
        if (added != null) {
            foreach (DataObject object in added) {
                Video video = (Video) object;
                
                filesize_to_video.set(video.get_master_filesize(), video);
            }
        }
        
        if (removed != null) {
            foreach (DataObject object in removed) {
                Video video = (Video) object;
                
                filesize_to_video.remove(video.get_master_filesize(), video);
            }
        }
        
        base.notify_contents_altered(added, removed);
    }
    
1214
    public VideoID get_basename_filesize_duplicate(string basename, uint64 filesize) {
1215
        foreach (Video video in filesize_to_video.get(filesize)) {
1216
            if (utf8_ci_compare(video.get_master_file().get_basename(), basename) == 0)
1217
                return video.get_video_id();
1218 1219
        }
        
1220 1221 1222 1223 1224 1225
        return VideoID(); // the default constructor of the VideoID struct creates an invalid
                          // video id, which is just what we want in this case
    }
    
    public bool has_basename_filesize_duplicate(string basename, uint64 filesize) {
        return get_basename_filesize_duplicate(basename, filesize).is_valid();
1226
    }
1227
}