Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.stitcher;

// [START video_stitcher_create_live_session]

import com.google.cloud.video.stitcher.v1.AdTag;
import com.google.cloud.video.stitcher.v1.CreateLiveSessionRequest;
import com.google.cloud.video.stitcher.v1.LiveSession;
import com.google.cloud.video.stitcher.v1.LocationName;
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
import java.io.IOException;

public class CreateLiveSession {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "us-central1";
// Uri of the live stream to stitch; this URI must reference either an MPEG-DASH
// manifest (.mpd) file or an M3U playlist manifest (.m3u8) file.
String sourceUri = "https://storage.googleapis.com/my-bucket/main.mpd";
// See Single Inline Linear
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags)
String adTagUri = "https://pubads.g.doubleclick.net/gampad/ads...";
String slateId = "my-slate-id";

createLiveSession(projectId, location, sourceUri, adTagUri, slateId);
}

public static void createLiveSession(
String projectId, String location, String sourceUri, String adTagUri, String slateId)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (VideoStitcherServiceClient videoStitcherServiceClient =
VideoStitcherServiceClient.create()) {
CreateLiveSessionRequest createLiveSessionRequest =
CreateLiveSessionRequest.newBuilder()
.setParent(LocationName.of(projectId, location).toString())
.setLiveSession(
LiveSession.newBuilder()
.setSourceUri(sourceUri)
.putAdTagMap("default", AdTag.newBuilder().setUri(adTagUri).build())
.setDefaultSlateId(slateId))
.build();

LiveSession response = videoStitcherServiceClient.createLiveSession(createLiveSessionRequest);
System.out.println("Created live session: " + response.getName());
System.out.println("Play URI: " + response.getPlayUri());
}
}
}
// [END video_stitcher_create_live_session]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.stitcher;

// [START video_stitcher_get_live_ad_tag_detail]

import com.google.cloud.video.stitcher.v1.GetLiveAdTagDetailRequest;
import com.google.cloud.video.stitcher.v1.LiveAdTagDetail;
import com.google.cloud.video.stitcher.v1.LiveAdTagDetailName;
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
import java.io.IOException;

public class GetLiveAdTagDetail {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "us-central1";
String sessionId = "my-session-id";
String adTagDetailId = "my-ad-tag-detail-id";

getLiveAdTagDetail(projectId, location, sessionId, adTagDetailId);
}

public static void getLiveAdTagDetail(
String projectId, String location, String sessionId, String adTagDetailId)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (VideoStitcherServiceClient videoStitcherServiceClient =
VideoStitcherServiceClient.create()) {
GetLiveAdTagDetailRequest getLiveAdTagDetailRequest =
GetLiveAdTagDetailRequest.newBuilder()
.setName(
LiveAdTagDetailName.of(projectId, location, sessionId, adTagDetailId).toString())
.build();

LiveAdTagDetail response =
videoStitcherServiceClient.getLiveAdTagDetail(getLiveAdTagDetailRequest);
System.out.println("Live ad tag detail: " + response.getName());
}
}
}
// [END video_stitcher_get_live_ad_tag_detail]
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.stitcher;

// [START video_stitcher_get_live_session]

import com.google.cloud.video.stitcher.v1.GetLiveSessionRequest;
import com.google.cloud.video.stitcher.v1.LiveSession;
import com.google.cloud.video.stitcher.v1.LiveSessionName;
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
import java.io.IOException;

public class GetLiveSession {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "us-central1";
String sessionId = "my-session-id";

getLiveSession(projectId, location, sessionId);
}

public static void getLiveSession(String projectId, String location, String sessionId)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (VideoStitcherServiceClient videoStitcherServiceClient =
VideoStitcherServiceClient.create()) {
GetLiveSessionRequest getLiveSessionRequest =
GetLiveSessionRequest.newBuilder()
.setName(LiveSessionName.of(projectId, location, sessionId).toString())
.build();

LiveSession response = videoStitcherServiceClient.getLiveSession(getLiveSessionRequest);
System.out.println("Live session: " + response.getName());
}
}
}
// [END video_stitcher_get_live_session]
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.stitcher;

// [START video_stitcher_list_live_ad_tag_details]

import com.google.cloud.video.stitcher.v1.ListLiveAdTagDetailsRequest;
import com.google.cloud.video.stitcher.v1.LiveAdTagDetail;
import com.google.cloud.video.stitcher.v1.LiveSessionName;
import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient;
import java.io.IOException;

public class ListLiveAdTagDetails {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "us-central1";
String sessionId = "my-session-id";

listLiveAdTagDetails(projectId, location, sessionId);
}

public static void listLiveAdTagDetails(String projectId, String location, String sessionId)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (VideoStitcherServiceClient videoStitcherServiceClient =
VideoStitcherServiceClient.create()) {
ListLiveAdTagDetailsRequest listLiveAdTagDetailsRequest =
ListLiveAdTagDetailsRequest.newBuilder()
.setParent(LiveSessionName.of(projectId, location, sessionId).toString())
.build();

VideoStitcherServiceClient.ListLiveAdTagDetailsPagedResponse response =
videoStitcherServiceClient.listLiveAdTagDetails(listLiveAdTagDetailsRequest);
System.out.println("Live ad tag details:");

for (LiveAdTagDetail adTagDetail : response.iterateAll()) {
System.out.println(adTagDetail.toString());
}
}
}
}
// [END video_stitcher_list_live_ad_tag_details]
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.stitcher;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;

import com.google.api.gax.rpc.NotFoundException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CreateLiveSessionTest {

private static final String LOCATION = "us-central1";
private static final String LIVE_URI =
"https://storage.googleapis.com/cloud-samples-data/media/hls-live/manifest.m3u8";
// Single Inline Linear
// (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags)
private static final String LIVE_AD_TAG_URI =
"https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=";
private static final String SLATE_ID =
"my-slate-" + UUID.randomUUID().toString().substring(0, 25);
private static final String SLATE_URI =
"https://storage.googleapis.com/cloud-samples-data/media/ForBiggerEscapes.mp4";
private static String PROJECT_ID;
private static String SESSION_NAME;
private static PrintStream originalOut;
private ByteArrayOutputStream bout;

private static String requireEnvVar(String varName) {
String varValue = System.getenv(varName);
assertNotNull(
String.format("Environment variable '%s' is required to perform these tests.", varName));
return varValue;
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void beforeTest() throws IOException {
originalOut = System.out;
bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
// Project number is always returned in the live session name
SESSION_NAME = String.format("locations/%s/liveSessions/", LOCATION);

try {
DeleteSlate.deleteSlate(PROJECT_ID, LOCATION, SLATE_ID);
} catch (NotFoundException e) {
// Don't worry if the slate doesn't already exist.
}
CreateSlate.createSlate(PROJECT_ID, LOCATION, SLATE_ID, SLATE_URI);
bout.reset();
}

@Test
public void test_CreateLiveSession() throws IOException {
CreateLiveSession.createLiveSession(PROJECT_ID, LOCATION, LIVE_URI, LIVE_AD_TAG_URI, SLATE_ID);
String output = bout.toString();
assertThat(output, containsString(SESSION_NAME));
bout.reset();
}

@After
public void tearDown() throws IOException {
DeleteSlate.deleteSlate(PROJECT_ID, LOCATION, SLATE_ID);
// No delete method for a live session
System.setOut(originalOut);
bout.reset();
}
}
Loading