Skip to content

Commit 4c7a193

Browse files
irataxytritone
andauthored
feat: add live session methods and tests (#3618)
* feat: add live session methods and tests * address feedback on testing * update copyright year * update copyright year --------- Co-authored-by: Chris Cotter <[email protected]>
1 parent 317a2fc commit 4c7a193

File tree

6 files changed

+431
-1
lines changed

6 files changed

+431
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package videostitcher
16+
17+
// [START videostitcher_create_live_session]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
stitcher "cloud.google.com/go/video/stitcher/apiv1"
24+
"cloud.google.com/go/video/stitcher/apiv1/stitcherpb"
25+
)
26+
27+
// createLiveSession creates a livestream session in which to insert ads.
28+
// Live sessions are ephemeral resources that expire after a few minutes.
29+
func createLiveSession(w io.Writer, projectID, liveConfigID string) error {
30+
// projectID := "my-project-id"
31+
// liveConfigID := "my-live-config"
32+
location := "us-central1"
33+
ctx := context.Background()
34+
client, err := stitcher.NewVideoStitcherClient(ctx)
35+
if err != nil {
36+
return fmt.Errorf("stitcher.NewVideoStitcherClient: %w", err)
37+
}
38+
defer client.Close()
39+
40+
req := &stitcherpb.CreateLiveSessionRequest{
41+
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
42+
LiveSession: &stitcherpb.LiveSession{
43+
LiveConfig: fmt.Sprintf("projects/%s/locations/%s/liveConfigs/%s", projectID, location, liveConfigID),
44+
},
45+
}
46+
// Creates the live session.
47+
response, err := client.CreateLiveSession(ctx, req)
48+
if err != nil {
49+
return fmt.Errorf("client.CreateLiveSession: %w", err)
50+
}
51+
52+
fmt.Fprintf(w, "Live session: %v\n", response.GetName())
53+
fmt.Fprintf(w, "Play URI: %v", response.GetPlayUri())
54+
return nil
55+
}
56+
57+
// [END videostitcher_create_live_session]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
package videostitcher
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"strings"
21+
"testing"
22+
"time"
23+
24+
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
25+
)
26+
27+
func TestCreateLiveSession(t *testing.T) {
28+
tc := testutil.SystemTest(t)
29+
var buf bytes.Buffer
30+
uuid, err := getUUID()
31+
if err != nil {
32+
t.Fatalf("getUUID err: %v", err)
33+
}
34+
slateID := fmt.Sprintf("%s-%s", slateIDPrefix, uuid)
35+
slateName := fmt.Sprintf("projects/%s/locations/%s/slates/%s", tc.ProjectID, location, slateID)
36+
liveConfigID := fmt.Sprintf("%s-%s", liveConfigIDPrefix, uuid)
37+
liveConfigName := fmt.Sprintf("projects/%s/locations/%s/liveConfigs/%s", tc.ProjectID, location, liveConfigID)
38+
createTestSlate(slateID, t)
39+
createTestLiveConfig(slateID, liveConfigID, t)
40+
t.Cleanup(func() {
41+
deleteTestLiveConfig(liveConfigName, t)
42+
deleteTestSlate(slateName, t)
43+
})
44+
45+
// Create a new live session and return the play URI.
46+
sessionPrefix := fmt.Sprintf("locations/%s/liveSessions/", location)
47+
testutil.Retry(t, 3, 2*time.Second, func(r *testutil.R) {
48+
if err := createLiveSession(&buf, tc.ProjectID, liveConfigID); err != nil {
49+
r.Errorf("createLiveSession got err: %v", err)
50+
}
51+
if got := buf.String(); !strings.Contains(got, sessionPrefix) {
52+
r.Errorf("createLiveSession got: %v Want to contain: %v", got, sessionPrefix)
53+
}
54+
})
55+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
package videostitcher
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"strings"
21+
"testing"
22+
"time"
23+
24+
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
25+
)
26+
27+
func TestGetLiveAdTagDetail(t *testing.T) {
28+
tc := testutil.SystemTest(t)
29+
var buf bytes.Buffer
30+
uuid, err := getUUID()
31+
if err != nil {
32+
t.Fatalf("getUUID err: %v", err)
33+
}
34+
slateID := fmt.Sprintf("%s-%s", slateIDPrefix, uuid)
35+
slateName := fmt.Sprintf("projects/%s/locations/%s/slates/%s", tc.ProjectID, location, slateID)
36+
liveConfigID := fmt.Sprintf("%s-%s", liveConfigIDPrefix, uuid)
37+
liveConfigName := fmt.Sprintf("projects/%s/locations/%s/liveConfigs/%s", tc.ProjectID, location, liveConfigID)
38+
39+
createTestSlate(slateID, t)
40+
createTestLiveConfig(slateID, liveConfigID, t)
41+
t.Cleanup(func() {
42+
// Can't delete live sessions
43+
deleteTestLiveConfig(liveConfigName, t)
44+
deleteTestSlate(slateName, t)
45+
})
46+
47+
sessionID, playURI := createTestLiveSession(liveConfigID, t)
48+
49+
// No list or delete methods for live sessions
50+
51+
// Ad tag details
52+
53+
// To get ad tag details, you need to curl the main manifest and
54+
// a rendition first. This supplies media player information to the API.
55+
//
56+
// Get the playURI first. The last line of the response will contain a
57+
// renditions location. Curl the live session name with the rendition
58+
// location appended.
59+
60+
renditions, err := getPlayURI(playURI)
61+
if err != nil {
62+
t.Fatalf("getPlayURI err: %v", err)
63+
}
64+
65+
// playURI will be in the following format:
66+
// https://videostitcher.googleapis.com/v1/projects/{project}/locations/{location}/liveSessions/{session-id}/manifest.m3u8?signature=...
67+
// Replace manifest.m3u8?signature=... with the renditions location.
68+
69+
err = curlRendition(playURI, renditions[0])
70+
if err != nil {
71+
t.Fatalf("curlRendition err: %v", err)
72+
}
73+
74+
// List the ad tag details for a given live session. This is the only way to get an
75+
// ad tag detail.
76+
adTagDetailsNamePrefix := fmt.Sprintf("/locations/%s/liveSessions/%s/liveAdTagDetails/", location, sessionID)
77+
if err := listLiveAdTagDetails(&buf, tc.ProjectID, sessionID); err != nil {
78+
t.Fatalf("listLiveAdTagDetails got err: %v", err)
79+
}
80+
got := buf.String()
81+
82+
if !strings.Contains(got, adTagDetailsNamePrefix) {
83+
t.Fatalf("listLiveAdTagDetails got\n----\n%v\n----\nWant to contain:\n----\n%v\n----\n", got, adTagDetailsNamePrefix)
84+
}
85+
strSlice := strings.Split(got, "/")
86+
adTagDetailsID := strSlice[len(strSlice)-1]
87+
adTagDetailsID = strings.TrimRight(adTagDetailsID, "\n")
88+
adTagDetailsName := fmt.Sprintf("/locations/%s/liveSessions/%s/liveAdTagDetails/%s", location, sessionID, adTagDetailsID)
89+
if !strings.Contains(got, adTagDetailsName) {
90+
t.Errorf("listLiveAdTagDetails got\n----\n%v\n----\nWant to contain:\n----\n%v\n----\n", got, adTagDetailsName)
91+
}
92+
buf.Reset()
93+
94+
// Get the specified ad tag detail for a given live session.
95+
testutil.Retry(t, 3, 2*time.Second, func(r *testutil.R) {
96+
if err := getLiveAdTagDetail(&buf, tc.ProjectID, sessionID, adTagDetailsID); err != nil {
97+
r.Errorf("getLiveAdTagDetail got err: %v", err)
98+
}
99+
if got := buf.String(); !strings.Contains(got, adTagDetailsName) {
100+
r.Errorf("getLiveAdTagDetail got\n----\n%v\n----\nWant to contain:\n----\n%v\n----\n", got, adTagDetailsName)
101+
}
102+
})
103+
buf.Reset()
104+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
package videostitcher
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"strings"
21+
"testing"
22+
"time"
23+
24+
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
25+
)
26+
27+
func TestGetLiveSession(t *testing.T) {
28+
tc := testutil.SystemTest(t)
29+
var buf bytes.Buffer
30+
uuid, err := getUUID()
31+
if err != nil {
32+
t.Fatalf("getUUID err: %v", err)
33+
}
34+
slateID := fmt.Sprintf("%s-%s", slateIDPrefix, uuid)
35+
slateName := fmt.Sprintf("projects/%s/locations/%s/slates/%s", tc.ProjectID, location, slateID)
36+
liveConfigID := fmt.Sprintf("%s-%s", liveConfigIDPrefix, uuid)
37+
liveConfigName := fmt.Sprintf("projects/%s/locations/%s/liveConfigs/%s", tc.ProjectID, location, liveConfigID)
38+
39+
createTestSlate(slateID, t)
40+
createTestLiveConfig(slateID, liveConfigID, t)
41+
t.Cleanup(func() {
42+
// Can't delete live sessions
43+
deleteTestLiveConfig(liveConfigName, t)
44+
deleteTestSlate(slateName, t)
45+
})
46+
47+
sessionID, _ := createTestLiveSession(liveConfigID, t)
48+
liveSession := fmt.Sprintf("/locations/%s/liveSessions/%s", location, sessionID)
49+
50+
testutil.Retry(t, 3, 2*time.Second, func(r *testutil.R) {
51+
if err := getLiveSession(&buf, tc.ProjectID, sessionID); err != nil {
52+
r.Errorf("getLiveSession got err: %v", err)
53+
}
54+
if got := buf.String(); !strings.Contains(got, liveSession) {
55+
r.Errorf("getLiveSession got: %v Want to contain: %v", got, liveSession)
56+
}
57+
})
58+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
package videostitcher
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"strings"
21+
"testing"
22+
"time"
23+
24+
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
25+
)
26+
27+
func TestListLiveAdTagDetails(t *testing.T) {
28+
tc := testutil.SystemTest(t)
29+
var buf bytes.Buffer
30+
uuid, err := getUUID()
31+
if err != nil {
32+
t.Fatalf("getUUID err: %v", err)
33+
}
34+
slateID := fmt.Sprintf("%s-%s", slateIDPrefix, uuid)
35+
slateName := fmt.Sprintf("projects/%s/locations/%s/slates/%s", tc.ProjectID, location, slateID)
36+
liveConfigID := fmt.Sprintf("%s-%s", liveConfigIDPrefix, uuid)
37+
liveConfigName := fmt.Sprintf("projects/%s/locations/%s/liveConfigs/%s", tc.ProjectID, location, liveConfigID)
38+
39+
createTestSlate(slateID, t)
40+
createTestLiveConfig(slateID, liveConfigID, t)
41+
t.Cleanup(func() {
42+
// Can't delete live sessions
43+
deleteTestLiveConfig(liveConfigName, t)
44+
deleteTestSlate(slateName, t)
45+
})
46+
47+
sessionID, playURI := createTestLiveSession(liveConfigID, t)
48+
49+
// No list or delete methods for live sessions
50+
51+
// Ad tag details
52+
53+
// To get ad tag details, you need to curl the main manifest and
54+
// a rendition first. This supplies media player information to the API.
55+
//
56+
// Get the playURI first. The last line of the response will contain a
57+
// renditions location. Curl the live session name with the rendition
58+
// location appended.
59+
60+
renditions, err := getPlayURI(playURI)
61+
if err != nil {
62+
t.Fatalf("getPlayURI err: %v", err)
63+
}
64+
65+
// playURI will be in the following format:
66+
// https://videostitcher.googleapis.com/v1/projects/{project}/locations/{location}/liveSessions/{session-id}/manifest.m3u8?signature=...
67+
// Replace manifest.m3u8?signature=... with the renditions location.
68+
69+
err = curlRendition(playURI, renditions[0])
70+
if err != nil {
71+
t.Fatalf("curlRendition err: %v", err)
72+
}
73+
74+
// List the ad tag details for a given live session.
75+
adTagDetailsNamePrefix := fmt.Sprintf("/locations/%s/liveSessions/%s/liveAdTagDetails/", location, sessionID)
76+
testutil.Retry(t, 3, 2*time.Second, func(r *testutil.R) {
77+
if err := listLiveAdTagDetails(&buf, tc.ProjectID, sessionID); err != nil {
78+
r.Errorf("listLiveAdTagDetails got err: %v", err)
79+
}
80+
if got := buf.String(); !strings.Contains(got, adTagDetailsNamePrefix) {
81+
r.Errorf("listLiveAdTagDetails got: %v Want to contain: %v", got, adTagDetailsNamePrefix)
82+
}
83+
})
84+
}

0 commit comments

Comments
 (0)