Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get[Audio/Video]EncoderConfigurationOptions #82

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lib/media/ver10/get_audio_encoder_configuration_options.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule Onvif.Media.Ver10.GetAudioEncoderConfigurationOptions do
import SweetXml
import XmlBuilder
require Logger

alias Onvif.Device
alias Onvif.Media.Ver10.Profile.AudioEncoderConfigurationOption

@spec soap_action :: String.t()
def soap_action, do: "http://www.onvif.org/ver10/media/wsdl/GetAudioEncoderConfigurationOptions"

@spec request(Device.t(), list) :: {:ok, any} | {:error, map()}
def request(device, args),
do: Onvif.Media.Ver10.Media.request(device, args, __MODULE__)

def request_body(configuration_token, profile_token) do
element(:"s:Body", [
element(:"trt:GetAudioEncoderConfigurationOptions", [
gen_config_token_element(configuration_token),
gen_profile_token_element(profile_token)
])
])
end

def gen_profile_token_element(nil), do: []

def gen_profile_token_element(profile_token) do
element(:"trt:ProfileToken", profile_token)
end

def gen_config_token_element(nil), do: []

def gen_config_token_element(configuration_token) do
element(:"trt:ConfigurationToken", configuration_token)
end

@spec response(any) :: {:error, Ecto.Changeset.t()} | {:ok, struct()}
def response(xml_response_body) do
response =
xml_response_body
|> parse(namespace_conformant: true, quiet: true)
|> xpath(
~x"//s:Envelope/s:Body/trt:GetAudioEncoderConfigurationOptionsResponse/trt:Options"el
|> add_namespace("s", "http://www.w3.org/2003/05/soap-envelope")
|> add_namespace("trt", "http://www.onvif.org/ver10/media/wsdl")
|> add_namespace("tt", "http://www.onvif.org/ver10/schema")
)
|> Enum.map(&AudioEncoderConfigurationOption.parse/1)
|> Enum.reduce([], fn raw_config, acc ->
case AudioEncoderConfigurationOption.to_struct(raw_config) do
{:ok, config} ->
[config | acc]

{:error, changeset} ->
Logger.error("Discarding invalid audio config: #{inspect(changeset)}")
acc
end
end)

{:ok, response}
end
end
100 changes: 100 additions & 0 deletions lib/media/ver10/profile/audio_encoder_configuration_options.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
defmodule Onvif.Media.Ver10.Profile.AudioEncoderConfigurationOption do
@moduledoc """
Optional configuration of the Audio encoder.
"""

use Ecto.Schema
import Ecto.Changeset
import SweetXml

@primary_key false
@derive Jason.Encoder
@required []
@optional []

embedded_schema do
embeds_many :options, Options, primary_key: false, on_replace: :delete do
@derive Jason.Encoder
field(:encoding, Ecto.Enum, values: [G711: "G711", G726: "G726", AAC: "AAC"])

embeds_one :bitrate_list, BitrateList, primary_key: false, on_replace: :update do
@derive Jason.Encoder
field(:items, {:array, :integer})
end

embeds_one :sample_rate_list, SampleRateList, primary_key: false, on_replace: :update do
@derive Jason.Encoder
field(:items, {:array, :integer})
end
end
end

def parse(nil), do: nil
def parse([]), do: nil

def parse(doc) do
xmap(
doc,
options: ~x"./tt:Options"elo |> transform_by(&parse_options/1)
)
end

def parse_options(nil), do: nil
def parse_options([]), do: nil

def parse_options(docs) do
Enum.map(docs, fn doc ->
xmap(
doc,
encoding: ~x"./tt:Encoding/text()"so,
bitrate_list: ~x"./tt:BitrateList"eo |> transform_by(&parse_unbound_int_list/1),
sample_rate_list: ~x"./tt:SampleRateList"eo |> transform_by(&parse_unbound_int_list/1)
)
end)
end

def parse_unbound_int_list(nil), do: nil
def parse_unbound_int_list([]), do: nil

def parse_unbound_int_list(doc) do
xmap(
doc,
items: ~x"./tt:Items/text()"ilo
)
end

def to_struct(parsed) do
%__MODULE__{}
|> changeset(parsed)
|> apply_action(:validate)
end

@spec to_json(%Onvif.Media.Ver10.Profile.AudioEncoderConfigurationOption{}) ::
{:error,
%{
:__exception__ => any,
:__struct__ => Jason.EncodeError | Protocol.UndefinedError,
optional(atom) => any
}}
| {:ok, binary}
def to_json(%__MODULE__{} = schema) do
Jason.encode(schema)
end

def changeset(module, attrs) do
module
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> cast_embed(:options, with: &options_changeset/2)
end

def options_changeset(module, attrs) do
cast(module, attrs, [:encoding])
|> cast_embed(:bitrate_list, with: &unbound_int_list_changeset/2)
|> cast_embed(:sample_rate_list, with: &unbound_int_list_changeset/2)
end

def unbound_int_list_changeset(module, attrs) do
cast(module, attrs, [:items])
end
end
2 changes: 1 addition & 1 deletion test/media/ver10/delete_osd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Onvif.Media.Ver10.DeleteOSDTest do
device = Onvif.Factory.device()

Mimic.expect(Tesla, :request, fn _client, _opts ->
{:error, %{status: 400, body: xml_response }}
{:error, %{status: 400, body: xml_response}}
end)

{:error, reason} = Onvif.Media.Ver10.DeleteOSD.request(device, ["token"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tt="http://www.onvif.org/ver10/schema"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl"
xmlns:tev="http://www.onvif.org/ver10/events/wsdl"
xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"
xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl"
xmlns:tst="http://www.onvif.org/ver10/storage/wsdl"
xmlns:ter="http://www.onvif.org/ver10/error"
xmlns:dn="http://www.onvif.org/ver10/network/wsdl"
xmlns:tns1="http://www.onvif.org/ver10/topics"
xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl"
xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery"
xmlns:wsadis="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:wstop="http://docs.oasis-open.org/wsn/t-1"
xmlns:wsrf-bf="http://docs.oasis-open.org/wsrf/bf-2"
xmlns:wsntw="http://docs.oasis-open.org/wsn/bw-2"
xmlns:wsrf-rw="http://docs.oasis-open.org/wsrf/rw-2"
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
xmlns:wsrf-r="http://docs.oasis-open.org/wsrf/r-2"
xmlns:trc="http://www.onvif.org/ver10/recording/wsdl"
xmlns:tse="http://www.onvif.org/ver10/search/wsdl"
xmlns:trp="http://www.onvif.org/ver10/replay/wsdl"
xmlns:tnshik="http://www.hikvision.com/2011/event/topics"
xmlns:hikwsd="http://www.onvifext.com/onvif/ext/ver10/wsdl"
xmlns:hikxsd="http://www.onvifext.com/onvif/ext/ver10/schema"
xmlns:tas="http://www.onvif.org/ver10/advancedsecurity/wsdl"
xmlns:tr2="http://www.onvif.org/ver20/media/wsdl"
xmlns:axt="http://www.onvif.org/ver20/analytics">
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Sender</env:Value>
<env:Subcode>
<env:Value>ter:InvalidArgVal</env:Value>
<env:Subcode>
<env:Value>ter:NoConfig</env:Value>
</env:Subcode>
</env:Subcode>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">The requested configuration does not exist.</env:Text>
</env:Reason>
</env:Fault>
</env:Body>
</env:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:c14n="http://www.w3.org/2001/10/xml-exc-c14n#"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsa5="http://www.w3.org/2005/08/addressing"
xmlns:tt="http://www.onvif.org/ver10/schema"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2"
xmlns:tns1="http://www.onvif.org/ver10/topics"
xmlns:tnsaxis="http://www.axis.com/2009/event/topics"
xmlns:tev="http://www.onvif.org/ver10/events/wsdl"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:ter="http://www.onvif.org/ver10/error">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<SOAP-ENV:Code>
<SOAP-ENV:Value>SOAP-ENV:Receiver</SOAP-ENV:Value>
<SOAP-ENV:Subcode>
<SOAP-ENV:Value>ter:ActionNotSupported</SOAP-ENV:Value>
<SOAP-ENV:Subcode>
<SOAP-ENV:Value>ter:AudioNotSupported</SOAP-ENV:Value>
</SOAP-ENV:Subcode>
</SOAP-ENV:Subcode>
</SOAP-ENV:Code>
<SOAP-ENV:Reason>
<SOAP-ENV:Text xml:lang="en">No audio support</SOAP-ENV:Text>
</SOAP-ENV:Reason>
<SOAP-ENV:Detail>
<SOAP-ENV:Text>The NVT does not support audio</SOAP-ENV:Text>
</SOAP-ENV:Detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<s:Envelope
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:sc="http://www.w3.org/2003/05/soap-encoding"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:tt="http://www.onvif.org/ver10/schema">
<s:Body>
<trt:GetAudioEncoderConfigurationOptionsResponse>
<trt:Options>
<tt:Options>
<tt:Encoding>G711</tt:Encoding>
<tt:BitrateList>
<tt:Items>32</tt:Items>
</tt:BitrateList>
<tt:SampleRateList>
<tt:Items>8</tt:Items>
</tt:SampleRateList>
</tt:Options>
<tt:Options>
<tt:Encoding>G726</tt:Encoding>
<tt:BitrateList>
<tt:Items>32</tt:Items>
</tt:BitrateList>
<tt:SampleRateList>
<tt:Items>8</tt:Items>
</tt:SampleRateList>
</tt:Options>
<tt:Options>
<tt:Encoding>AAC</tt:Encoding>
<tt:BitrateList>
<tt:Items>32</tt:Items>
<tt:Items>64</tt:Items>
</tt:BitrateList>
<tt:SampleRateList>
<tt:Items>8</tt:Items>
<tt:Items>16</tt:Items>
</tt:SampleRateList>
</tt:Options>
</trt:Options>
</trt:GetAudioEncoderConfigurationOptionsResponse>
</s:Body>
</s:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<s:Envelope
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:sc="http://www.w3.org/2003/05/soap-encoding"
xmlns:trt="http://www.onvif.org/ver10/media/wsdl"
xmlns:tt="http://www.onvif.org/ver10/schema">
<s:Body>
<trt:GetVideoEncoderConfigurationOptionsResponse>
<trt:Options>
<tt:QualityRange>
<tt:Min>1</tt:Min>
<tt:Max>6</tt:Max>
</tt:QualityRange>
<tt:JPEG>
<tt:ResolutionsAvailable>
<tt:Width>704</tt:Width>
<tt:Height>576</tt:Height>
</tt:ResolutionsAvailable>
<tt:ResolutionsAvailable>
<tt:Width>640</tt:Width>
<tt:Height>480</tt:Height>
</tt:ResolutionsAvailable>
<tt:ResolutionsAvailable>
<tt:Width>352</tt:Width>
<tt:Height>288</tt:Height>
</tt:ResolutionsAvailable>
<tt:FrameRateRange>
<tt:Min>1</tt:Min>
<tt:Max>25</tt:Max>
</tt:FrameRateRange>
<tt:EncodingIntervalRange>
<tt:Min>1</tt:Min>
<tt:Max>6</tt:Max>
</tt:EncodingIntervalRange>
</tt:JPEG>
<tt:H264>
<tt:ResolutionsAvailable>
<tt:Width>704</tt:Width>
<tt:Height>576</tt:Height>
</tt:ResolutionsAvailable>
<tt:ResolutionsAvailable>
<tt:Width>640</tt:Width>
<tt:Height>480</tt:Height>
</tt:ResolutionsAvailable>
<tt:ResolutionsAvailable>
<tt:Width>352</tt:Width>
<tt:Height>288</tt:Height>
</tt:ResolutionsAvailable>
<tt:GovLengthRange>
<tt:Min>25</tt:Min>
<tt:Max>150</tt:Max>
</tt:GovLengthRange>
<tt:FrameRateRange>
<tt:Min>1</tt:Min>
<tt:Max>25</tt:Max>
</tt:FrameRateRange>
<tt:EncodingIntervalRange>
<tt:Min>1</tt:Min>
<tt:Max>6</tt:Max>
</tt:EncodingIntervalRange>
<tt:H264ProfilesSupported>Baseline</tt:H264ProfilesSupported>
<tt:H264ProfilesSupported>Main</tt:H264ProfilesSupported>
<tt:H264ProfilesSupported>High</tt:H264ProfilesSupported>
</tt:H264>
</trt:Options>
</trt:GetVideoEncoderConfigurationOptionsResponse>
</s:Body>
</s:Envelope>
Loading