View Source Membrane Hackney plugin

Hex.pm API Docs CircleCI

HTTP sink and source based on the Hackney library.

It is part of Membrane Multimedia Framework.

Installation

Add the following line to your deps in mix.exs. Run mix deps.get.

	{:membrane_hackney_plugin, "~> 0.11.0"}

Sample usage

Membrane.Hackney.Source

This pipeline should get you a kitten from imgur and save as kitty.jpg. To run it you need :membrane_file_plugin in your project's dependencies.

defmodule DownloadPipeline do
  use Membrane.Pipeline
  alias Membrane.{File, Hackney}

  @impl true
  def handle_init(_) do
    children = [
      source: %Hackney.Source{
        location: "http://i.imgur.com/z4d4kWk.jpg",
        hackney_opts: [follow_redirect: true]
      },
      sink: %File.Sink{location: "kitty.jpg"}
    ]

    links = [link(:source) |> to(:sink)]

    spec = %ParentSpec{children: children, links: links}

    {{:ok, spec: spec}, %{}}
  end
end

{:ok, pid} = DownloadPipeline.start_link()
DownloadPipeline.play(pid)

Membrane.Hackney.Sink

The following pipeline is an example of file upload - it requires Goth library with properly configured credentials for Google Cloud and :membrane_file_plugin in your project's dependencies.

defmodule UploadPipeline do
  use Membrane.Pipeline

  alias Membrane.{File, Hackney}

  @impl true
  def handle_init([bucket, name]) do
    children = [
      source: %File.Source{location: "sample.flac"},
      sink: %Hackney.Sink{
        method: :post,
        location: build_uri(bucket, name),
        headers: [auth_header(), {"content-type", "audio/flac"}]
      }
    ]

    links = [link(:source) |> to(:sink)]

    spec = %ParentSpec{children: children, links: links}

    {{:ok, spec: spec}, %{}}
  end

  @impl true
  def handle_notification(%Hackney.Sink.Response{} = response, from, _ctx, state) do
    IO.inspect({from, response})
    {:ok, state}
  end

  def handle_notification(_notification, _from, _ctx, state) do
    {:ok, state}
  end

  defp auth_header do
    {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/devstorage.read_write")
    {"Authorization", "#{token.type} #{token.token}"}
  end

  defp build_uri(bucket, name) do
    "https://www.googleapis.com/upload/storage/v1/b/#{bucket}/o?" <>
      URI.encode_query(uploadType: "media", name: name)
  end
end

{:ok, pid} = UploadPipeline.start_link(["some_bucket", "uploaded_file_name.flac"])
UploadPipeline.play(pid)

Sponsors

The development of this plugin was sponsored by Abridge AI, Inc.

Copyright 2018, Software Mansion

Software Mansion