Live stream using Youtube's Livestreaming API to someone else's channel - youtube-api

I tried youtube's watchme app for live streaming and I understand the code fairly. In my use case the user needs to be able to live stream to another channel. I understand there is a need of Stream key here, but I need a rough guidance on where I need to change in the code. Any hints or a rough idea would do too. I just need headstart.

If you are using Youtube WatchMe app for Android, it will create live events on your Youtube account. If you want the user to type a Stream Key from a Live stream issued from another Youtube account you will have to create a function similar to the startStreaming method :
public void startStreaming(EventData event) {
//the event is already started on your external live stream
//String broadcastId = event.getId();
//new StartEventTask().execute(broadcastId);
Intent intent = new Intent(getApplicationContext(),
StreamerActivity.class);
intent.putExtra(YouTubeApi.RTMP_URL_KEY, event.getIngestionAddress());
// we don't need this since it's only used to end the live event
intent.putExtra(YouTubeApi.BROADCAST_ID_KEY, "");
startActivityForResult(intent, REQUEST_STREAMER);
}
Note that broadcast id is sent to StreamerActivity in order to be able to finish the event (endEvent) which you won't be able to do using an external live stream.
event.getIngestionAddress() is the Stream Key url eg :
rtmp://a.rtmp.youtube.com/live2/<Stream key>
So you can create a method like the following :
public void startStreaming(String streamKey) {
String url = "rtmp://a.rtmp.youtube.com/live2/" + streamKey;
Intent intent = new Intent(getApplicationContext(), StreamerActivity.class);
intent.putExtra(YouTubeApi.RTMP_URL_KEY, url);
intent.putExtra(YouTubeApi.BROADCAST_ID_KEY, "");
startActivityForResult(intent, REQUEST_STREAMER);
}

Related

how to get youtube live api stream key

How can I bind my YouTube stream key to match previous video.
I'm trying to use java to do it but getting no where.
I'm looking at the example given to create the broadcast stream but it doesn't have the keeping same key.
I've gotten help on this before. Please try do some research on Google and then ask the question. You can see How can I change the stream my event uses via the YouTube live api?. Which will help you as it did me.
In short this was the code i received for help.
Credit to #M. Prokhorov
YouTube yt = ... // your reference to YouTube
String broadcastId = ... // your broadcast Id
String newStreamId = ... // identifier of stream you want to bind
String apiKEy = ... // your API key
// you can define other response parts if you want more or don't want some of these
String responseParts = "id,status,contentDetails.boundStreamId";
yt.liveBroadcasts().bind(broadcastId, responseParts)
.setApiKey(apiKey)
.setStreamId(streamId)
// other data you might want in request
.execute()

Twilio Video - How to get current number of participants in a room?

I would like to know how many people are currently connected to a room when using Twilio Video.
Twilio has a REST API to get a room resource, but it does not return current number of participants.
https://www.twilio.com/docs/api/video/rooms-resource#get-by-sid
Only way i see is to subscribe to status callback to "participant connected" and disconnected events and manually keep track of how many participants are connected or left the room.
Is there a better way to do this ?
You can use twilio server side sdk, Let me share NodeJS example so you get better idea on implementation.
First lets define function that init twilio client and fetch connected participants of room.
async function getConnectedParticipants(roomName) {
var Twilio = require('twilio');
var apiKeySid = "YOUR_TWILIO_API_KEY_SID_HERE";
var apiKeySecret = "YOUR_TWILIO_API_SECRET_HERE";
var accountSid = "YOUR_TWILIO_ACCOUNT_SID_HERE";
var client = new Twilio(apiKeySid, apiKeySecret, {accountSid: accountSid});
var list = await client.video.rooms(roomName)
.participants
.list({status: 'connected'});
return list;
}
Now let's use our function that return you connected participants.
var connectedParticipants = await getConnectedParticipants("YourRoomName");
// print all connected participants
console.log('connectedParticipants', connectedParticipants);
Note: I have used async and await in this example, please check more on that before implementation.
Twilio developer evangelist here.
Keeping a server side list of the participants' identities based on the participant connected and disconnected events is probably the best way to work this out right now.
One alternative is to get this information from the front end. The JavaScript library allows you to query the participants in a room. You could periodically, or based on events, query that property and send it to your server via Ajax too.
Let me know if that helps.
Update
The Rooms API now allows you to retrieve information on participants that have connected to a room. To get the currently connected users in a room using Node.js, for example, the code would look like:
var client = new Twilio(apiKeySid, apiKeySecret, {accountSid: accountSid});
client.video.rooms(roomSid).participants
.list({status: 'connected'}, (err, participants) => {
if (err) { console.error(err); return; }
console.log(participants.length);
});

YouTube - Data API v3 Unable to display private videos of my YouTube channel on my website using Google Service Account

I have few private videos on my YouTube channel and i want to put them on my website. Basically i don't want my website users to watch them directly on my YouTube channel. Furthermore i don't want to show consent screen to the users either. I browsed and found this example at stackoverflow in which DalmTo is suggesting someone to use Google Service Account to achieve this. My code works fine with publicly available videos but it does not show the private videos whenever i search the video with it's ID.
Here's my code:
<?php
session_start();
require_once('vendor/autoload.php');
$client = new Google_Client();
$client->setApplicationName("YouTube-Example");
$client->setScopes('https://www.googleapis.com/auth/youtube');
putenv("GOOGLE_APPLICATION_CREDENTIALS=service_account.json");
$client->useApplicationDefaultCredentials();
$youtube = new Google_Service_YouTube($client);
try{
$response = $youtube->videos->listVideos(
'snippet',
array(
'id' => 'pW0og1OZurE' //private video unable to display
));
echo "<pre>";
print_r($response);
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
?>
I followed the documentation but i can't figure out why it's not working. Please help me out.
Thanks in advance.
Here may be your answer:
The forMine parameter indicates that the response should only search within the authorized user's videos. Also, since this request uses the forMine parameter, it must also set the type parameter value to video.
If you have not uploaded any videos associated with that term, you will not see any items in the API response list.
Source: Google developers docs
Displaying of private videos is not allowed in Youtube API as stated in the help guide.
Private videos can only be seen by people who have been invited to
view the video.
Here are some possible reasons why you or somebody you’ve shared the
video with aren’t able to see a private video:
-Viewers need to have a YouTube account and be signed into it when trying to view the video.
-If the viewer has multiple YouTube accounts, they must be signed into the account which the video has been shared with.
-Since private videos don’t appear on a Channel page, the person will need to use the specific link to the private video. YouTube will send
them an email with the link once you’ve invited them, but you can also
send it yourself.

c# twilio client browser to browser calls

i am in the process of writing a c# mvc.net application and need to know if i can end twilio client calls after a certain amount of time and also play recording during the call when the call has reached a certain time limit or a recording the says "This call will end in 30 seconds". any help is good help.
You'll have to do a little work to make this work as duration is an empty value until after the call terminates. Instead, use the StartTime parameter on a call and calculate the duration manually in your application.
After implementing that you can modify the call using the <Play> verb to play a recording during the call with your warning message and <Hangup> the call once it reached the specified time.
An example modification in C# would look like:
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio;
class Example
{
static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/user/account
string AccountSid = "ACCOUNT_SID";
string AuthToken = "AUTH_TOKEN";
var twilio = new TwilioRestClient(AccountSid, AuthToken);
twilio.RedirectCall("CALL_SID",
"http://demo.twilio.com/docs/voice.xml", "POST");
}

How to check if a YouTube channel is active

It looks regardless of whether a channel has been created by a user, the youtube api will return a channel for that particular user.
Java API
YouTube.Channels.List search = youTube.get().channels().list("id);
search.setPart("id");
ChannelListResponse res = search.execute();
List<Channel> searchResultList = search.getItems()
Channel channel = searchResultList.get(0); // there is always a channel
For the authenticated user, the channel seems to exist but when going to the YouTube profile, it states "You must create a channel to upload videos. Create a channel" or if going to the url without the user being authenticated, it'll say "This channel is not available at the moment. Please try again later."
How do check that a youtube channel is active or not. do i have to attempt to upload to it?
There are two ways to do this:
When you make an API call such as playlist management or video uploading, if there is no linked channel, the API will throw a GoogleJsonResponseException. Here's a code snippet showing you what happens when you try to make a playlist update API call and there's no channel:
try {
yt.playlistItems().insert("snippet,contentDetails", playlistItem).execute();
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
for(GoogleJsonError.ErrorInfo errorInfo : error.getErrors()) {
if(errorInfo.getReason().equals("youtubeSignupRequired")) {
// Ask the user to create a channel and link their profile
}
}
}
You'll want to do something when you get "youtubeSignupRequired" as the reason for the error.
The other way is to check ahead of time. Make a Channel.List call and check for "items/status". You're looking for the boolean value "isLinked" to equal "true". Note that I've inserted a cast in this sample code because in the version of this sample, the client was returning a String value instead of a typed Boolean:
YouTube.Channels.List channelRequest = youtube.channels().list("status");
channelRequest.setMine("true");
channelRequest.setFields("items/status");
ChannelListResponse channelResult = channelRequest.execute();
List<Channel> channelsList = channelResult.getItems();
for (Channel channel : channelsList) {
Map<String, Object> status = (Map<String, Object>) channel.get("status");
if (true == (Boolean) status.get("isLinked")) {
// Channel is linked to a Google Account
} else {
// Channel is NOT linked to a Google Account
}
}

Resources