Skip to content

Commit e1213d2

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/CreateBroadcast.java"
1 parent 04091bb commit e1213d2

1 file changed

Lines changed: 194 additions & 0 deletions

File tree

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. 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 distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.youtube.cmdline.live;
16+
17+
import com.google.api.client.auth.oauth2.Credential;
18+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19+
import com.google.api.client.util.DateTime;
20+
import com.google.api.services.samples.youtube.cmdline.Auth;
21+
import com.google.api.services.youtube.YouTube;
22+
import com.google.api.services.youtube.model.*;
23+
import com.google.common.collect.Lists;
24+
25+
import java.io.BufferedReader;
26+
import java.io.IOException;
27+
import java.io.InputStreamReader;
28+
import java.util.List;
29+
30+
/**
31+
* Use the YouTube Live Streaming API to insert a broadcast and a stream
32+
* and then bind them together. Use OAuth 2.0 to authorize the API requests.
33+
*
34+
* @author Ibrahim Ulukaya
35+
*/
36+
public class CreateBroadcast {
37+
38+
/**
39+
* Define a global instance of a Youtube object, which will be used
40+
* to make YouTube Data API requests.
41+
*/
42+
private static YouTube youtube;
43+
44+
/**
45+
* Create and insert a liveBroadcast resource.
46+
*/
47+
public static void main(String[] args) {
48+
49+
// This OAuth 2.0 access scope allows for full read/write access to the
50+
// authenticated user's account.
51+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
52+
53+
try {
54+
// Authorize the request.
55+
Credential credential = Auth.authorize(scopes, "createbroadcast");
56+
57+
// This object is used to make YouTube Data API requests.
58+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
59+
.setApplicationName("youtube-cmdline-createbroadcast-sample").build();
60+
61+
// Prompt the user to enter a title for the broadcast.
62+
String title = getBroadcastTitle();
63+
System.out.println("You chose " + title + " for broadcast title.");
64+
65+
// Create a snippet with the title and scheduled start and end
66+
// times for the broadcast. Currently, those times are hard-coded.
67+
LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
68+
broadcastSnippet.setTitle(title);
69+
broadcastSnippet.setScheduledStartTime(new DateTime("2024-01-30T00:00:00.000Z"));
70+
broadcastSnippet.setScheduledEndTime(new DateTime("2024-01-31T00:00:00.000Z"));
71+
72+
// Set the broadcast's privacy status to "private". See:
73+
// https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#status.privacyStatus
74+
LiveBroadcastStatus status = new LiveBroadcastStatus();
75+
status.setPrivacyStatus("private");
76+
77+
LiveBroadcast broadcast = new LiveBroadcast();
78+
broadcast.setKind("youtube#liveBroadcast");
79+
broadcast.setSnippet(broadcastSnippet);
80+
broadcast.setStatus(status);
81+
82+
// Construct and execute the API request to insert the broadcast.
83+
YouTube.LiveBroadcasts.Insert liveBroadcastInsert =
84+
youtube.liveBroadcasts().insert("snippet,status", broadcast);
85+
LiveBroadcast returnedBroadcast = liveBroadcastInsert.execute();
86+
87+
// Print information from the API response.
88+
System.out.println("\n================== Returned Broadcast ==================\n");
89+
System.out.println(" - Id: " + returnedBroadcast.getId());
90+
System.out.println(" - Title: " + returnedBroadcast.getSnippet().getTitle());
91+
System.out.println(" - Description: " + returnedBroadcast.getSnippet().getDescription());
92+
System.out.println(" - Published At: " + returnedBroadcast.getSnippet().getPublishedAt());
93+
System.out.println(
94+
" - Scheduled Start Time: " + returnedBroadcast.getSnippet().getScheduledStartTime());
95+
System.out.println(
96+
" - Scheduled End Time: " + returnedBroadcast.getSnippet().getScheduledEndTime());
97+
98+
// Prompt the user to enter a title for the video stream.
99+
title = getStreamTitle();
100+
System.out.println("You chose " + title + " for stream title.");
101+
102+
// Create a snippet with the video stream's title.
103+
LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
104+
streamSnippet.setTitle(title);
105+
106+
// Define the content distribution network settings for the
107+
// video stream. The settings specify the stream's format and
108+
// ingestion type. See:
109+
// https://developers.google.com/youtube/v3/live/docs/liveStreams#cdn
110+
CdnSettings cdnSettings = new CdnSettings();
111+
cdnSettings.setFormat("1080p");
112+
cdnSettings.setIngestionType("rtmp");
113+
114+
LiveStream stream = new LiveStream();
115+
stream.setKind("youtube#liveStream");
116+
stream.setSnippet(streamSnippet);
117+
stream.setCdn(cdnSettings);
118+
119+
// Construct and execute the API request to insert the stream.
120+
YouTube.LiveStreams.Insert liveStreamInsert =
121+
youtube.liveStreams().insert("snippet,cdn", stream);
122+
LiveStream returnedStream = liveStreamInsert.execute();
123+
124+
// Print information from the API response.
125+
System.out.println("\n================== Returned Stream ==================\n");
126+
System.out.println(" - Id: " + returnedStream.getId());
127+
System.out.println(" - Title: " + returnedStream.getSnippet().getTitle());
128+
System.out.println(" - Description: " + returnedStream.getSnippet().getDescription());
129+
System.out.println(" - Published At: " + returnedStream.getSnippet().getPublishedAt());
130+
131+
// Construct and execute a request to bind the new broadcast
132+
// and stream.
133+
YouTube.LiveBroadcasts.Bind liveBroadcastBind =
134+
youtube.liveBroadcasts().bind(returnedBroadcast.getId(), "id,contentDetails");
135+
liveBroadcastBind.setStreamId(returnedStream.getId());
136+
returnedBroadcast = liveBroadcastBind.execute();
137+
138+
// Print information from the API response.
139+
System.out.println("\n================== Returned Bound Broadcast ==================\n");
140+
System.out.println(" - Broadcast Id: " + returnedBroadcast.getId());
141+
System.out.println(
142+
" - Bound Stream Id: " + returnedBroadcast.getContentDetails().getBoundStreamId());
143+
144+
} catch (GoogleJsonResponseException e) {
145+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
146+
+ e.getDetails().getMessage());
147+
e.printStackTrace();
148+
149+
} catch (IOException e) {
150+
System.err.println("IOException: " + e.getMessage());
151+
e.printStackTrace();
152+
} catch (Throwable t) {
153+
System.err.println("Throwable: " + t.getMessage());
154+
t.printStackTrace();
155+
}
156+
}
157+
158+
/*
159+
* Prompt the user to enter a title for a broadcast.
160+
*/
161+
private static String getBroadcastTitle() throws IOException {
162+
163+
String title = "";
164+
165+
System.out.print("Please enter a broadcast title: ");
166+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
167+
title = bReader.readLine();
168+
169+
if (title.length() < 1) {
170+
// Use "New Broadcast" as the default title.
171+
title = "New Broadcast";
172+
}
173+
return title;
174+
}
175+
176+
/*
177+
* Prompt the user to enter a title for a stream.
178+
*/
179+
private static String getStreamTitle() throws IOException {
180+
181+
String title = "";
182+
183+
System.out.print("Please enter a stream title: ");
184+
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
185+
title = bReader.readLine();
186+
187+
if (title.length() < 1) {
188+
// Use "New Stream" as the default title.
189+
title = "New Stream";
190+
}
191+
return title;
192+
}
193+
194+
}

0 commit comments

Comments
 (0)