-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjob.go
255 lines (224 loc) · 7.82 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package elementalconductor
import (
"encoding/xml"
"regexp"
"strconv"
"strings"
)
var nonDigitRegexp = regexp.MustCompile(`[^\d]`)
// OutputGroupType is a custom type for OutputGroup type field values
type OutputGroupType string
const (
// FileOutputGroupType is the value for the type field on OutputGroup
// for jobs with a file output
FileOutputGroupType = OutputGroupType("file_group_settings")
// AppleLiveOutputGroupType is the value for the type field on OutputGroup
// for jobs with Apple's HTTP Live Streaming (HLS) output
AppleLiveOutputGroupType = OutputGroupType("apple_live_group_settings")
)
// Container is the Video container type for a job
type Container string
const (
// AppleHTTPLiveStreaming is the container for HLS video files
AppleHTTPLiveStreaming = Container("m3u8")
// MPEG4 is the container for MPEG-4 video files
MPEG4 = Container("mp4")
)
// GetJobs returns a list of the user's jobs
func (c *Client) GetJobs() (*JobList, error) {
var result *JobList
err := c.do("GET", "/jobs", nil, &result)
if err != nil {
return nil, err
}
return result, nil
}
// GetJob returns metadata on a single job
func (c *Client) GetJob(jobID string) (*Job, error) {
var result *Job
err := c.do("GET", "/jobs/"+jobID, nil, &result)
if err != nil {
return nil, err
}
return result, nil
}
// CreateJob sends a single job to the current Elemental
// Cloud deployment for processing
func (c *Client) CreateJob(job *Job) (*Job, error) {
var result *Job
err := c.do("POST", "/jobs", *job, &result)
if err != nil {
return nil, err
}
return result, nil
}
// CancelJob cancels the given job in the Elemental Conductor API.
func (c *Client) CancelJob(jobID string) (*Job, error) {
var job *Job
var payload = struct {
XMLName xml.Name `xml:"cancel"`
}{}
err := c.do("POST", "/jobs/"+jobID+"/cancel", payload, &job)
if err != nil {
return nil, err
}
return job, nil
}
// GetID is a convenience function to parse the job id
// out of the Href attribute in Job
func (j *Job) GetID() string {
if j.Href != "" {
hrefData := strings.Split(j.Href, "/")
return hrefData[len(hrefData)-1]
}
return ""
}
// JobList represents the response returned by
// a query for the list of jobs
type JobList struct {
XMLName xml.Name `xml:"job_list"`
Empty string `xml:"empty,omitempty"`
Job []Job `xml:"job"`
}
// Job represents a job to be sent to Elemental Cloud
type Job struct {
XMLName xml.Name `xml:"job"`
Href string `xml:"href,attr,omitempty"`
Input Input `xml:"input,omitempty"`
ContentDuration *ContentDuration `xml:"content_duration,omitempty"`
Priority int `xml:"priority,omitempty"`
OutputGroup []OutputGroup `xml:"output_group,omitempty"`
StreamAssembly []StreamAssembly `xml:"stream_assembly,omitempty"`
Status string `xml:"status,omitempty"`
Submitted DateTime `xml:"submitted,omitempty"`
StartTime DateTime `xml:"start_time,omitempty"`
CompleteTime DateTime `xml:"complete_time,omitempty"`
ErroredTime DateTime `xml:"errored_time,omitempty"`
PercentComplete int `xml:"pct_complete,omitempty"`
ErrorMessages []JobError `xml:"error_messages,omitempty"`
}
// JobError represents an individual error on a job
type JobError struct {
Code int `xml:"error>code,omitempty"`
CreatedAt JobErrorDateTime `xml:"error>created_at,omitempty"`
Message string `xml:"error>message,omitempty"`
}
// Input represents the spec for the job's input
type Input struct {
FileInput Location `xml:"file_input,omitempty"`
InputInfo *InputInfo `xml:"input_info,omitempty"`
}
// InputInfo contains metadata related to a job input.
type InputInfo struct {
Video VideoInputInfo `xml:"video"`
}
// VideoInputInfo contains video metadata related to a job input.
type VideoInputInfo struct {
Format string `xml:"format"`
FormatInfo string `xml:"format_info"`
FormatProfile string `xml:"format_profile"`
CodecID string `xml:"codec_id"`
CodecIDInfo string `xml:"codec_id_info"`
Bitrate string `xml:"bit_rate"`
Width string `xml:"width"`
Height string `xml:"height"`
}
// GetWidth parses the underlying width returned the Elemental Conductor API
// and converts it to int64.
//
// Examples:
// - Input: "1 920 pixels"
// Output: 1920
// - Input: "1920p"
// Output: 1920
// - Input: "1 920"
// Output: 1920
func (v *VideoInputInfo) GetWidth() int64 {
return v.extractNumber(v.Width)
}
// GetHeight parses the underlying height returned the Elemental Conductor API
// and converts it to int64.
//
// Examples:
// - Input: "1 080 pixels"
// Output: 1080
// - Input: "1080p"
// Output: 1080
// - Input: "1 080"
// Output: 1080
func (v *VideoInputInfo) GetHeight() int64 {
return v.extractNumber(v.Height)
}
func (v *VideoInputInfo) extractNumber(input string) int64 {
input = nonDigitRegexp.ReplaceAllString(input, "")
n, _ := strconv.ParseInt(input, 10, 64)
return n
}
// ContentDuration contains information about the content of the media in the
// job.
type ContentDuration struct {
InputDuration int `xml:"input_duration"`
}
// Location defines where a file is or needs to be.
// Username and Password are required for certain
// protocols that require authentication, like S3
type Location struct {
URI string `xml:"uri,omitempty"`
Username string `xml:"username,omitempty"`
Password string `xml:"password,omitempty"`
}
// OutputGroup is a list of the indended outputs for the job
type OutputGroup struct {
Order int `xml:"order,omitempty"`
FileGroupSettings *FileGroupSettings `xml:"file_group_settings,omitempty"`
AppleLiveGroupSettings *AppleLiveGroupSettings `xml:"apple_live_group_settings,omitempty"`
Type OutputGroupType `xml:"type,omitempty"`
Output []Output `xml:"output,omitempty"`
}
// FileGroupSettings define where the file job output should go
type FileGroupSettings struct {
Destination *Location `xml:"destination,omitempty"`
}
// AppleLiveGroupSettings define where the HLS job output should go
type AppleLiveGroupSettings struct {
Destination *Location `xml:"destination,omitempty"`
SegmentDuration uint `xml:"segment_length,omitempty"`
EmitSingleFile bool `xml:"emit_single_file,omitempty"`
}
// Output defines the different processing stream assemblies
// for the job
type Output struct {
FullURI string `xml:"full_uri,omitempty"`
StreamAssemblyName string `xml:"stream_assembly_name,omitempty"`
NameModifier string `xml:"name_modifier,omitempty"`
Order int `xml:"order,omitempty"`
Extension string `xml:"extension,omitempty"`
Container Container `xml:"container,omitempty"`
}
// StreamAssembly defines how each processing stream should behave
type StreamAssembly struct {
ID string `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
Preset string `xml:"preset,omitempty"`
VideoDescription *StreamVideoDescription `xml:"video_description"`
}
// StreamVideoDescription contains information about the video in a given
// stream assembly.
type StreamVideoDescription struct {
Codec string `xml:"codec"`
EncoderType string `xml:"encoder_type"`
Height string `xml:"height"`
Width string `xml:"width"`
}
// GetWidth returns the underlying width parsed as an int64.
func (s *StreamVideoDescription) GetWidth() int64 {
return s.getNumber(s.Width)
}
// GetHeight returns the underlying height parsed as an int64.
func (s *StreamVideoDescription) GetHeight() int64 {
return s.getNumber(s.Height)
}
func (s *StreamVideoDescription) getNumber(input string) int64 {
v, _ := strconv.ParseInt(input, 10, 64)
return v
}