Introduction
This article presents unusual solution for integration CaptureManager with YouTube service. It allows to use less known functionality of YouTube service for uploading of live video on YouTube channel.
Background
The main idea of this article is the using of CaptureManager in the client application of the video hosting service. The YouTube service has been chosen for the simplicity of the client code for uploading of video files. However, this project implements another view point on uploading video files - redirecting of encoded video stream from writing into the file on the uploading stream of YouTube service. It allows imitate functionality of streaming video.
Advantages of this solution:
- Simple connection with the hosting - live streaming on YouTube needs initialization of stream functionality, creating events, creating schedule of events. This solution needs only Google account for live uploading video on YouTube channel.
- Minimum additional libraries - only YouTube framework and CaptureManager.
Disadvantages of of this solution:
- Video can be accessible only after finishing of uploading.
- Unexpected closing connection between application and YouTube service leads to damage of video file.
Using the code
Code of this project is based on CaptureManager and Google YouTube.v3.Data framework. In the demo example of YouTube client, which can be got by link: https://github.com/youtube/api-samples/tree/master/dotnet, the next code presents uploading file on YouTube:
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
In this code FileStream
is a class for opening of file, but method youtubeService.Videos.Insert
does not use FileStream
class. It takes Stream
abstract class.
public virtual VideosResource.InsertMediaUpload Insert(Video body, string part, Stream stream, string contentType)
It leads to the next idea - create stream class which is inheritated from Stream
abstract class. Testing by mock Stream
abstract class shows that after setting property CanSeek
to false
the YouTube service DOES NOT check property Length
and Position
. More over, it is started to call Read
method and works in syncronized mode.
public override bool CanSeek
{
get { return false; }
}
In this project I wrote OutputStream
class which inherits interface from Stream
abstract class. This class wraps around ByteStream
class of CaptureManager and redirects bytes from output of ByteStream
class into the YouTube service by Read
method. Of couse, CaptureManager and YouTube service work in the DIFFERENT threads and there is a need for correct syncronization of them. That syncronization is implemented by ConcurrentQueue<byte[]>
class. ByteStream
class of CaptureManager enqueue byte blocks and OutputStream
class dequeue byte blocks.
After closing of the capture session, OutputStream
class returns by Read
method 0 value which is signal of end of file - stream. YouTube service closes upload connection with hosting and the hosting starts processing of media file for posting. As a result, it has real time streaming on YouTube channel without keeping info on the local storage.
Points of Interest
This demo program uses CaptureManager commercial trial version with 60 seconds duration of capture session. Full function library can be gotten by link: Application License - $119.99
History