Repository navigation
Expand file tree
/
Copy pathCreateBroadcast.java
More file actions
194 lines (160 loc) · 8.37 KB
/
Copy pathCreateBroadcast.java
File metadata and controls
194 lines (160 loc) · 8.37 KB
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
/*
* Copyright (c) 2013 Google Inc.
*
* 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.google.api.services.samples.youtube.cmdline.live;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.util.DateTime;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.*;
import com.google.common.collect.Lists;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* Use the YouTube Live Streaming API to insert a broadcast and a stream
* and then bind them together. Use OAuth 2.0 to authorize the API requests.
*
* @author Ibrahim Ulukaya
*/
public class CreateBroadcast {
/**
* Define a global instance of a Youtube object, which will be used
* to make YouTube Data API requests.
*/
private static YouTube youtube;
/**
* Create and insert a liveBroadcast resource.
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "createbroadcast");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
.setApplicationName("youtube-cmdline-createbroadcast-sample").build();
// Prompt the user to enter a title for the broadcast.
String title = getBroadcastTitle();
System.out.println("You chose " + title + " for broadcast title.");
// Create a snippet with the title and scheduled start and end
// times for the broadcast. Currently, those times are hard-coded.
LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
broadcastSnippet.setTitle(title);
broadcastSnippet.setScheduledStartTime(new DateTime("2024-01-30T00:00:00.000Z"));
broadcastSnippet.setScheduledEndTime(new DateTime("2024-01-31T00:00:00.000Z"));
// Set the broadcast's privacy status to "private". See:
// https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#status.privacyStatus
LiveBroadcastStatus status = new LiveBroadcastStatus();
status.setPrivacyStatus("private");
LiveBroadcast broadcast = new LiveBroadcast();
broadcast.setKind("youtube#liveBroadcast");
broadcast.setSnippet(broadcastSnippet);
broadcast.setStatus(status);
// Construct and execute the API request to insert the broadcast.
YouTube.LiveBroadcasts.Insert liveBroadcastInsert =
youtube.liveBroadcasts().insert("snippet,status", broadcast);
LiveBroadcast returnedBroadcast = liveBroadcastInsert.execute();
// Print information from the API response.
System.out.println("\n================== Returned Broadcast ==================\n");
System.out.println(" - Id: " + returnedBroadcast.getId());
System.out.println(" - Title: " + returnedBroadcast.getSnippet().getTitle());
System.out.println(" - Description: " + returnedBroadcast.getSnippet().getDescription());
System.out.println(" - Published At: " + returnedBroadcast.getSnippet().getPublishedAt());
System.out.println(
" - Scheduled Start Time: " + returnedBroadcast.getSnippet().getScheduledStartTime());
System.out.println(
" - Scheduled End Time: " + returnedBroadcast.getSnippet().getScheduledEndTime());
// Prompt the user to enter a title for the video stream.
title = getStreamTitle();
System.out.println("You chose " + title + " for stream title.");
// Create a snippet with the video stream's title.
LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
streamSnippet.setTitle(title);
// Define the content distribution network settings for the
// video stream. The settings specify the stream's format and
// ingestion type. See:
// https://developers.google.com/youtube/v3/live/docs/liveStreams#cdn
CdnSettings cdnSettings = new CdnSettings();
cdnSettings.setFormat("1080p");
cdnSettings.setIngestionType("rtmp");
LiveStream stream = new LiveStream();
stream.setKind("youtube#liveStream");
stream.setSnippet(streamSnippet);
stream.setCdn(cdnSettings);
// Construct and execute the API request to insert the stream.
YouTube.LiveStreams.Insert liveStreamInsert =
youtube.liveStreams().insert("snippet,cdn", stream);
LiveStream returnedStream = liveStreamInsert.execute();
// Print information from the API response.
System.out.println("\n================== Returned Stream ==================\n");
System.out.println(" - Id: " + returnedStream.getId());
System.out.println(" - Title: " + returnedStream.getSnippet().getTitle());
System.out.println(" - Description: " + returnedStream.getSnippet().getDescription());
System.out.println(" - Published At: " + returnedStream.getSnippet().getPublishedAt());
// Construct and execute a request to bind the new broadcast
// and stream.
YouTube.LiveBroadcasts.Bind liveBroadcastBind =
youtube.liveBroadcasts().bind(returnedBroadcast.getId(), "id,contentDetails");
liveBroadcastBind.setStreamId(returnedStream.getId());
returnedBroadcast = liveBroadcastBind.execute();
// Print information from the API response.
System.out.println("\n================== Returned Bound Broadcast ==================\n");
System.out.println(" - Broadcast Id: " + returnedBroadcast.getId());
System.out.println(
" - Bound Stream Id: " + returnedBroadcast.getContentDetails().getBoundStreamId());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
/*
* Prompt the user to enter a title for a broadcast.
*/
private static String getBroadcastTitle() throws IOException {
String title = "";
System.out.print("Please enter a broadcast title: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
title = bReader.readLine();
if (title.length() < 1) {
// Use "New Broadcast" as the default title.
title = "New Broadcast";
}
return title;
}
/*
* Prompt the user to enter a title for a stream.
*/
private static String getStreamTitle() throws IOException {
String title = "";
System.out.print("Please enter a stream title: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
title = bReader.readLine();
if (title.length() < 1) {
// Use "New Stream" as the default title.
title = "New Stream";
}
return title;
}
}