Using the encoder driver

There are two blocks needed to use the encoder driver:

  • Doing the initialization
  • Sending the transcoding requests

The initialization must provide the storage path present in the configuration file. This is straightforward:

encode_init = (
config
.map(lambda i: encoder.Initialize(storage_path=i.encode.storage_path))
)

Then, the encoding requests must be created each time an HTTP request comes in. Here is the code for doing this:

encode_request = (
sources.httpd.route
.filter(lambda i: i.id='flac_transcode')
.flat_map(lambda i: i.request)
.map(lambda i: encoder.EncodeMp3(
id=i.context,
data=i.data,
key=i.match_info['key']))
)

The HTTP requests are emitted on the route observable of the source of the httpd driver. Only requests that correspond to the FLAC encoding route are used here. This may have been omitted since there is only one route in this application. For each route, the httpd driver sends one item with a request field that contains a request observable. This request observable emits one item per request. These requests are merged with the flat_map operator. The i.request object is already an observable, so it is possible to use it in the rest of the chain by directly returning it in the flat_map call. Finally, each request is mapped to an EncodeMp3 object. The id of the request is filled with the context of the http request. This allows us to retrieve it when the file is encoded. Also, the key of the encoded file is retrieved from the URL of the request available in the key match information, defined when declaring the HTTP route. The final encode_request observable emits one EncodeMp3 item per HTTP request coming from remote clients.

The two encode observables can then be merged together so that they are provided to the sink of the encoder driver:

encoder_request = Observable.merge(encode_init, encode_request)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.53