Skip to content

Commit

Permalink
Allow implementer to configure encode class
Browse files Browse the repository at this point in the history
  • Loading branch information
val99erie committed Apr 26, 2017
1 parent 0b472b3 commit c007665
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -92,10 +92,25 @@ Hydra::Derivatives::Processors::Video::Processor.config.jpeg.codec = '-vcodec mj
### Configuration for Audio/Video Processing with ActiveEncode

```ruby
# Set the transcoding engine
ActiveEncode::Base.engine_adapter = :elastic_transcoder

# Sleep time (in seconds) to poll for status of encoding job
Hydra::Derivatives.active_encode_poll_time = 10

# If you want to use a different class for the encode object
Hydra::Derivatives::ActiveEncodeDerivatives.encode_class = MyCustomEncode

# If you want to use a different class for the source file service
Hydra::Derivatives::ActiveEncodeDerivatives.source_file_service = MyCustomSourceFileService

# If you want to use a different class for the output file service
Hydra::Derivatives::ActiveEncodeDerivatives.output_file_service = MyCustomOutputFileService
```

Note: Please don't confuse these methods with the similar methods in the parent class:
`Hydra::Derivatives.source_file_service` and `Hydra::Derivatives.output_file_service`

For additional documentation on using ActiveEncode, see:
* [Using Amazon Elastic Transcoder](doc/amazon_elastic_transcoder.md)

Expand Down
7 changes: 0 additions & 7 deletions doc/amazon_elastic_transcoder.md
Expand Up @@ -30,15 +30,8 @@ In an initializer file such as `config/initializers/active_encode.rb`, make sure
```ruby
# Use Amazon's Elastic Transcoder
ActiveEncode::Base.engine_adapter = :elastic_transcoder

# If you want to use a different source file or output file service than the defaults
# Hydra::Derivatives::ActiveEncodeDerivatives.source_file_service = MyCustomSourceFileService
# Hydra::Derivatives::ActiveEncodeDerivatives.output_file_service = MyCustomOutputFileService
```

Note: Please don't confuse these methods with the similar methods in the parent class:
`Hydra::Derivatives.source_file_service` and `Hydra::Derivatives.output_file_service`

## How to create derivatives (Multiple derivatives per Elastic Transcoder job)

```ruby
Expand Down
10 changes: 6 additions & 4 deletions lib/hydra/derivatives/processors/active_encode.rb
Expand Up @@ -11,12 +11,10 @@ def initialize(status, source_path, errors = [])

class ActiveEncode < Processor
class_attribute :timeout
attr_writer :encode_class

# TODO: Instead of hard-coding ActiveEncode::Base,
# pass in or configure the class so that a user can
# override it with a sub-class of AE::Base.
def process
encode = ::ActiveEncode::Base.create(source_path, directives)
encode = encode_class.create(source_path, directives)
timeout ? wait_for_encode_with_timeout(encode) : wait_for_encode(encode)
encode.output.each do |output|
output_file_service.call(output, directives)
Expand All @@ -36,6 +34,10 @@ def wait_for_encode(encode)
raise ActiveEncodeError.new(encode.state, source_path, encode.errors) unless encode.completed?
end

def encode_class
@encode_class || Hydra::Derivatives::ActiveEncodeDerivatives.encode_class
end

# After a timeout error, try to cancel the encoding.
def cleanup_after_timeout(encode)
encode.cancel!
Expand Down
8 changes: 8 additions & 0 deletions lib/hydra/derivatives/runners/active_encode_derivatives.rb
Expand Up @@ -26,5 +26,13 @@ def self.output_file_service
def self.processor_class
Processors::ActiveEncode
end

def self.encode_class
@encode_class || ::ActiveEncode::Base
end

class << self
attr_writer :encode_class
end
end
end
21 changes: 21 additions & 0 deletions spec/processors/active_encode_spec.rb
Expand Up @@ -31,6 +31,27 @@
enc
end

context 'with a custom encode class' do
let(:completed_status) { true }
let(:state) { :completed }

before do
class TestEncode < ::ActiveEncode::Base; end
processor.encode_class = TestEncode

# For this spec we don't care what happens with output,
# so stub it out to speed up the spec.
allow(output_file_service).to receive(:call)
end

after { Object.send(:remove_const, :TestEncode) }

it 'uses the configured encode class' do
expect(TestEncode).to receive(:create).and_return(encode_double)
subject
end
end

context 'when the encoding failed' do
let(:state) { :failed }
let(:failed_status) { true }
Expand Down
30 changes: 26 additions & 4 deletions spec/runners/active_encode_derivatives_spec.rb
@@ -1,11 +1,33 @@
require 'spec_helper'

class TestVideo < ActiveFedora::Base
attr_accessor :remote_file_name
end

describe Hydra::Derivatives::ActiveEncodeDerivatives do
context '.encode_class' do
before { class TestEncode < ::ActiveEncode::Base; end }

after do
Object.send(:remove_const, :TestEncode)
described_class.encode_class = ::ActiveEncode::Base
end

it 'has a default encode class' do
expect(described_class.encode_class).to eq ::ActiveEncode::Base
end

it 'can set the encode class' do
expect(described_class.encode_class).to eq ::ActiveEncode::Base
described_class.encode_class = TestEncode
expect(described_class.encode_class).to eq TestEncode
end
end

context '.create' do
before do
class TestVideo < ActiveFedora::Base
attr_accessor :remote_file_name
end
end
after { Object.send(:remove_const, :TestVideo) }

let(:file_path) { 'some/path/to/my_video.mp4' }
let(:video_record) { TestVideo.new(remote_file_name: file_path) }
let(:options) { { source: :remote_file_name, outputs: [low_res_video] } }
Expand Down

0 comments on commit c007665

Please sign in to comment.