//
//  MEEncodeJob.h
//  MediaEncoding
//
//  Created by Henry Mason on Tue Mar 16 2004.
//  Copyright (c) 2004 Henry Mason. All rights reserved.
//

#import  <sys/cdefs.h>

__BEGIN_DECLS

#import <Foundation/Foundation.h>

#import "MEVideoStreamFilter.h"
#import "MEAudioStreamFilter.h"
#import "MEStreamLoader.h"
#import "MEStreamMultiplexer.h"

#import <unistd.h>
#import <time.h>

@interface MEEncodeJob : NSObject <NSCoding> {
    
    MEStreamLoader      *streamLoader;
        
    int                 videoTrackIndex;
    MEVideoStreamFilter *videoDecompressor;
    NSMutableArray      *videoFilters;
    MEVideoStreamFilter *videoCompressor;
    
    int                 audioTrackIndex;
    MEAudioStreamFilter *audioDecompressor;
    NSMutableArray      *audioFilters;
    MEAudioStreamFilter *audioCompressor;
    
    MEStreamMultiplexer *streamMultiplexer;
    
    NSMutableArray      *parallelProcessingFiles;
    
    BOOL            isRunning, isPaused, shouldStop;
    unsigned int    currentFrame, lastPassFrames, currentPass, requiredPasses, parallelProcessingCount, runningProcesses, requiredVideoPasses, requiredAudioPasses;
    float           processedSeconds;
    long long       videoBytesCounter;
    double          processingThreadPriority;
    
    NSArray         *processingRangeArray;
    
    NSMutableArray  *processingStatuses;
    
    // Preview Stuff
    NSMutableArray  *previewFilterArray, *previewFilterBuffers;
    NSMutableData   *previewLoaderBuffer;
    int             previewIndex, advancedFrames;
    long long       startingPosition, newFoundStartingPosition;
    
    
    NSImage         *processingPreviewImage;
    NSLock          *processingPreviewImageLock;
    NSSize          processingPreviewImageSize;
    
    NSMutableDictionary     *nonSavedOptions, *savedOptions;
    
    time_t          processingStartTime, pausedProcessingTime, processingStopTime;
    
    NSLock          *videoFiltersLock, *audioFiltersLock, *videoCompressorLock, *multiplexerLock;
    
    MEPacketHeader  **loaderArray, **videoFilteredArray, **videoFilteringArray, **videoCompressedArray, **audioFilteredArray, **audioFilteringArray;
    
    MEPacketHeader  *nextVideoFilterInputPacket, *nextAudioFilterInputPacket, *nextVideoCompressorInputPacket, *nextMultiplexerInputPacket;
    
    MEStreamFilter  **videoFiltersToRun, **audioFiltersToRun;
    
    int             videoFiltersToRunCount, audioFiltersToRunCount;
    
    BOOL            loaderIsRunning, videoFilteringIsRunning, videoCompressingIsRunning, audioFilterIsRunning, multiplexerIsRunning;
    
    volatile int     runningMultiplexers;
    
    NSMutableArray  *runningPipes;
    
    long long       startingPos, endingPos;
    
    id              videoFilterPipe;
}

+ (void)loadFiltersFromFolder:(NSString *)filtersPath;
- (void)loadFiltersFromFolder:(NSString *)filtersPath;
- (NSArray *)availableVideoFilters;
- (NSArray *)availableVideoCompressors;

- (NSArray *)availableAudioFilters;
- (NSArray *)availableAudioCompressors;

- (NSArray *)availableStreamMultiplexers;

- (BOOL)safelyOpenInputFiles:(NSArray *)files;
- (BOOL)openInputFiles:(NSArray *)files;
- (BOOL)tryOpenInputFiles:(NSArray *)files;
- (void)setupNewInputFiles;

- (MEStreamLoader *)streamLoader;

- (MEStreamMultiplexer *)streamMultiplexer;
- (void)setStreamMultiplexer:(MEStreamMultiplexer *)newMultiplexer;

- (NSNumber *)startingPos;
- (void)setStartingPos:(NSNumber *)newPos;

- (NSNumber *)endingPos;
- (void)setEndingPos:(NSNumber *)newPos;

- (MEVideoStreamFilter *)videoDecompressor;
- (NSMutableArray *)videoFilters;
- (MEVideoStreamFilter *)videoCompressor;
- (void)setVideoCompressor:(MEVideoStreamFilter *)compressor;

- (int)audioTrackIndex;
- (BOOL)setAudioTrackIndex:(int)index;
- (NSArray *)availableAudioTrackIndexes;
- (MEAudioStreamFilter *)audioDecompressor;
- (NSMutableArray *)audioFilters;
- (MEAudioStreamFilter *)audioCompressor;
- (void)setAudioCompressor:(MEAudioStreamFilter *)compressor;

- (NSArray *)processingRangeArray;
- (void)setProcessingRangeArray:(NSArray *)newArray;

- (double)processingThreadPriority;
- (void)setProcessingThreadPriority:(double)priority;

- (unsigned int)parallelProcessingCount;
- (void)setParallelProcessingCount:(unsigned int)newCount;
- (NSImage *)previewImageForFilterAtIndex:(int)previewIndex
                               atPosition:(long long)previewPosition;
- (NSImage *)previewImageForFilterAtIndex:(int)newPreviewIndex
                               atPosition:(long long)previewPosition
                           actualPosition:(long long *)actualPos;
- (MEPacketHeader *)currentPreviewPacket;
- (NSImage *)nextPreviewImage;
- (NSImage *)lastPreviewImage;
- (void)clearPreviewBuffer;

- (NSMutableDictionary *)nonSavedOptions;
- (BOOL)isRunning;
- (BOOL)isPaused;

- (NSImage *)processingPreviewImage;
- (NSSize)processingPreviewImageSize;

- (double)processingProgress;
- (double)processingSpeed;
- (int)processingTimeSeconds;
- (int)processedFrames;
- (float)processedSeconds;
- (int)targetVideoBytesBitrate;
- (int)currentVideoBytesBitrate;

- (NSString *)postProcessingScriptPath;
- (void)setPostProcessingScriptPath:(NSString *)path;
- (BOOL)postProcessOutput;

- (void)startProcessing;
- (void)stopProcessing;
- (void)pauseProcessing;
- (void)resumeProcessing;
- (unsigned int)currentFrame;
- (unsigned int)currentPass;
- (unsigned int)requiredPasses;

// This is, by default, an empty method.
// It is called when a pass finishes, so it can be overridden by
// an MEEncodeJob category.

- (void)finishPass;

@end


extern NSString *MEEncodeJobProcessingIsDone;

__END_DECLS
