Search Unity

Video Questions about Unity VideoPlayer, IO.MemoryStream, and Amazon S3

Discussion in 'Audio & Video' started by gunboldb, Nov 13, 2018.

  1. gunboldb

    gunboldb

    Joined:
    Jun 8, 2018
    Posts:
    7
    Hello fellow people,

    I am trying to develop a kind of a media/game app. I got the game part covered, but I am in a bit of a muddle with the media part.

    Currently, I have AWS integrated into the app. Cognito for users to log in, Dynamodb for access to some data, and s3 for video and image files. With Unity's VideoPlayer I am able to play the videos on an S3 bucket using their URLs, but that's not ideal. I want to be able to stream the files directly from the bucket using the methods provided by the S3 library, which is where I am stuck, kind of.

    Using the GetObject method provided by the S3 library (or S3 client, I am not sure) I am able to retrieve an object from a bucket. But the object I get is a MemoryStream type object. I have limited programming knowledge, and it is a miracle that I got so far with the app, but unfortunately, this is where it ends. I tried reading up on MemoryStream, FileStream, and the whole System.IO thing but I just get more confused.

    So I just wanted to ask if anyone came across this before and if you did how did you do it?

    Is there any sample code that I could look at where you use MemoryStream and VideoPlayer to play a video?
     
  2. jesusmgg

    jesusmgg

    Joined:
    Jan 29, 2015
    Posts:
    7
    It would be great if someone can give some insight into this, I'm running into exactly the same problem.
     
    gunboldb likes this.
  3. gunboldb

    gunboldb

    Joined:
    Jun 8, 2018
    Posts:
    7
    Hey ito123456789,

    I figured everything out. Are you trying to stream a video from S3 without downloading? If yes, look into GetPreSignedUrl. This way, you can get an URL (different than the public Object URL) that can be used directly by the video player. Look at the below code for that.

    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// Returns a PreSignedURL
    4. /// </summary>
    5. /// <param name="key"></param>
    6. /// <param name="Url"></param>
    7. public void GetUrl(string key, Action<string> OnSuccessF = null)
    8.     {
    9.         GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
    10.         {
    11.             BucketName = s3BucketName,
    12.             Key = key,
    13.             Expires = DateTime.Now.AddHours(6)
    14.         };
    15.        
    16.         S3Client.GetPreSignedURLAsync(request, (response) => {
    17.             if (response.Exception == null)
    18.             {
    19.                 OnSuccessF(response.Response.Url);
    20.             }
    21.             else
    22.             {
    23.                 Debug.Log("[GetUrl] GetPreSignedURLAsync failed with exception: " + response.Exception.ToString());
    24.             }
    25.  
    26.         });
    27.     }
    If you want to download the object from S3 look at the following code:

    Code (CSharp):
    1. /// <summary>
    2.     /// Gets the object async.
    3.     /// </summary>
    4.     /// <param name="key">File name to retreive from S3 bucket</param>
    5.     public void GetObject(string key, Action<bool> DownloadComplete = null)
    6.     {
    7.         if (!System.IO.File.Exists(Application.temporaryCachePath + "/" + key.Split('/')[key.Split('/').Length - 1]))
    8.         {
    9.             // Construct the GetObjectRequest.
    10.             GetObjectRequest request = new GetObjectRequest
    11.             {
    12.                 BucketName = s3BucketName,
    13.                 Key = key
    14.             };
    15.  
    16.             // Temporary cache folder path.
    17.             //Debug.Log("[GetObject] Cache path: " + Application.temporaryCachePath);
    18.  
    19.             string fileName = "";
    20.             S3Client.GetObjectAsync(request, (response) =>
    21.             {
    22.                 if (response.Exception == null)
    23.                 {
    24.                     //Debug.Log("[GetObject] Retrieved file with filename: " + response.Response.Key);
    25.                     try
    26.                     {
    27.                         //Debug.Log(response.Response.Key.Split('/')[response.Response.Key.Split('/').Length - 1]);
    28.                         fileName = Application.temporaryCachePath + "/" + response.Response.Key.Split('/')[response.Response.Key.Split('/').Length - 1];
    29.                         using (FileStream fs = new FileStream(fileName, FileMode.Create))
    30.                         {
    31.                             response.Response.ResponseStream.CopyTo(fs);
    32.                         }
    33.                         DownloadComplete(true);
    34.                     }
    35.                     catch (Exception e)
    36.                     {
    37.                         Debug.Log("[GetObject] Failed to write to file exception: " + e);
    38.                         DownloadComplete(false);
    39.                     }
    40.                 }
    41.                 else
    42.                 {
    43.                     Debug.Log("[GetObject] GetObjectAsync failed with exception: " + response.Exception.ToString());
    44.                 }
    45.             });
    46.         }
    47.         else
    48.         {
    49.             DownloadComplete(true);
    50.         }
    51.     }
     
  4. gunboldb

    gunboldb

    Joined:
    Jun 8, 2018
    Posts:
    7
    If you have questions please ask away I will try to help
     
  5. gkarnati2004

    gkarnati2004

    Joined:
    May 11, 2020
    Posts:
    1
    I am having trouble saving the response to a global variable which can then be accessed by from the void Start() method since it is an async call. I have to save it globally in order to access and set url of the video in the start function. How can I tell my function to wait until it receives a response to execute anything else?