Forum Discussion

PhotoRonin's avatar
PhotoRonin
Channel Surfer
4 years ago
Solved

Transitioning Screensaver to Externally-Pulled Images

Howdy developers!  I'm excited to get some feedback on my (hopefully) simple issue.  I've built a few screensavers based on the originally-posted code from a couple of years ago (https://github.com/rokudev/samples/tree/master/screen%20savers), but would like to transition to pulling my images from my server instead of having to mash them into the package.

I need to place code in there which goes out and pulls the external content, but am struggling with where this code would go (which BRS file?).

The note that I got from the support folks was that it may need to be a "task node," but I'm still not understanding the concept they're trying to put across after watching the videos and reading their help content.

I believe I've coded this correctly, but just need to know which file to place it in, and how to call it...  

Here is the code that I've put together, into source/main.brs (below).  You will see that I've created a sub called "getPhotoRoninContent" and am calling it.  

I'm not sure this is correct 🙂

Function RunScreenSaver(params As Object) As Object 'Required entry point for screensavers
    getPhotoRoninContent()
    Main()
End Function


sub Main()
    screen = createObject("roSGScreen")
    port = createObject("roMessagePort")
    port2 =  createObject("roMessagePort")
    screen.setMessagePort(port)

    m.global = screen.getGlobalNode() 'Creates (Global) variable MyField
    m.global.AddField("MyField", "int", true)
    m.global.MyField = 0
    m.global.AddField("PicSwap", "int", true) 'Creates (Global) variable PicSwap
    m.global.PicSwap = 0

    scene = screen.createScene("ScreensaverFade") 'Creates scene ScreensaverFade
    screen.show()

     while(true) 'Message Port that fires every 16 seconds to change value of MyField if the screen isn't closed
        msg = wait(16000, port)
        if (msg <> invalid)
            msgType = type(msg)
            if msgType = "roSGScreenEvent"
                if msg.isScreenClosed() then return
            end if
        else
            m.global.MyField += 10
            msg = wait(2500, port2) 'Message port that fires 4 seconds after MyField is changed. Must be set to different port than other wait function or it will interfere.
            m.global.PicSwap += 10
        end if
    end while
end sub

sub getPhotoRoninContent()
    fs=createobject("rofilesystem")
    xfer=createobject("roUrlTransfer")
    xfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
    if not fs.exists("cachefs:/photoroninpic-01.jpg") then
        xfer.seturl("https://photos.smugmug.com/photos/i-DZ3RNCh/0/c6888ff8/X3/i-DZ3RNCh-X3.jpg")
        xfer.gettofile("cachefs:/photoroninpic-01.jpg")
    end if
end sub

 

  • PhotoRonin's avatar
    PhotoRonin
    2 years ago

    I've already had this redeveloped, and have launched the updated channels a while back. Thanks!

10 Replies

  • RokuJoel's avatar
    RokuJoel
    Binge Watcher

    1. you have a Main in function RunScreensaver()   - RunScreensaver is Main() for screensavers, put your Main() code into RunScreensaver. You can rename RunScreensaver to Main() while testing and rename back   later.  

    2. It looks like you are trying to download your images before running the Scenegraph portion of your screensaver. That can be done the way you are doing it but may slow down the launch time of your screensaver, and you won't be able to update content dynamically after launch. 

    3. Task node is separate .brs file you would put in the /components folder, you would typically launch it from your scene (ScreensaverFade), it could be a running task that sits there in the background waiting for a list of images to download, or a task that doesn't execute until you send it a run command. I suggest taking a look at the URL fetcher in our On Device Authentication sample on Github. Note that task nodes must rename themselves in order to run in a separate thread from the Render thread, as you don't want to slow down the Render thread:

    as soon as it is instantiated in MainScene.brs:
     
    m.uriFetcher = createObject("roSGNode", "UriFetcher")
     
    This task node renames itself:
     
    m.top.functionName = "go"
    and then sets itself to run:

    m.top.control = "RUN"
     
     - all of this is done in the Init()
     
     
     
     
     
    • PhotoRonin's avatar
      PhotoRonin
      Channel Surfer

      Thank you for the advice! Some of it I'm not entirely clear on yet, but am figuring it out.  I usually work in PowerShell or VBScript, so I'm not yet familiar with BrightScript (and very confused about it at the same time).

      What I gleaned from above was a few things:


      RokuJoel wrote:

      1. you have a Main in function RunScreensaver()   - RunScreensaver is Main() for screensavers, put your Main() code into RunScreensaver. You can rename RunScreensaver to Main() while testing and rename back   later.  


       - Got it.  I'm using the Roku-built screensaver "template", and they have it set this way.  I will consider changing it!


      2. It looks like you are trying to download your images before running the Scenegraph portion of your screensaver. That can be done the way you are doing it but may slow down the launch time of your screensaver, and you won't be able to update content dynamically after launch. 


       -  I'm aware that the way I had it put the tasks in serial and caused performance degradation.  Hence why I am looking for advice on how to separate the task :)


      3. Task node is separate .brs file you would put in the /components folder, you would typically launch it from your scene (ScreensaverFade), it could be a running task that sits there in the background waiting for a list of images to download, or a task that doesn't execute until you send it a run command. I suggest taking a look at the URL fetcher in our On Device Authentication sample on Github. Note that task nodes must rename themselves in order to run in a separate thread from the Render thread, as you don't want to slow down the Render thread:

      as soon as it is instantiated in MainScene.brs:
       
      m.uriFetcher = createObject("roSGNode", "UriFetcher")
       
      This task node renames itself:
       
      m.top.functionName = "go"
      and then sets itself to run:

      m.top.control = "RUN"
       
       - all of this is done in the Init()

      This is where I'm struggling, and lack some key understanding. 

      Here's my intended result:

      • The screensaver launches using an image that it has stored in the package file (to reduce the first screen load).
      • While first launched and starting on that screen, it grabs the other images that may or may not still be in the cache (checking for each before going to retrieve).
      • This allows it to then move on to the next image, now downloaded (hopefully!)

      Based on what you've written and what I gleaned from the examples you sent, I've made the following adjustments, which I'm not sure are accurate:

      • Created 2 files in /components:
        • UriFetcher.brs
          function init()
              fs=createobject("rofilesystem")
              xfer=createobject("roUrlTransfer")
              xfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
              if not fs.exists("cachefs:/photoroninpic-01.jpg") then
                  xfer.seturl("https://photos.smugmug.com/photos/i-DZ3RNCh/0/c6888ff8/X3/i-DZ3RNCh-X3.jpg")
                  xfer.gettofile("cachefs:/photoroninpic-01.jpg")
              end if
          end function
        • UriFetcher.xml
          <?xml version="1.0" encoding="utf-8" ?>
          
          <component name="UriFetcher" extends="Task" >
          
          <interface>
          	<field id="request" type="assocarray"/>
          	<field id="jobsByIdField" type="int" />
          	<field id="urlTransferPoolField" type="int" /> 
          </interface>
          
          <script type="text/brightscript" uri="pkg://components/UriFetcher.brs"/>
          
          <children/>
          
          </component>

      ...but I think there's an issue with this, as I'm not understanding the renamed task?

      • necrotek's avatar
        necrotek
        Roku Guru

        If you have a list of image urls you can make this simpler by creating a timer task and swapping out the poster uri on timer task fire and let the poster node handle downloading and displaying images. Unless the point this exercise is to get more familiar with setting up url transfer.

  •  

    Hello!

    Based on the code you provided, it seems that you're looking to pull images from your server for your screensaver application. To achieve this, you can place the code that fetches the external content within the  subroutine. This subroutine can be included in the same file (source/main.brs) as the rest of your code.

    To call the () subroutine, you can simply invoke it before the Main() subroutine in the  function, like this:

     

     
    Function  As Object () Main() End Function
     

    With this arrangement,  subroutine will be executed before the Main() subroutine when the screensaver runs.

    As for the concept of a "task node," it seems to refer to a specific construct within the  language that allows for parallel execution of tasks. However, based on the code you provided, it doesn't seem necessary for your use case. You can handle the image fetching as a subroutine without explicitly using a task node.

    Remember to ensure that the file paths and URLs in the  subroutine are accurate and accessible. Additionally, you may need to handle error cases or implement error checking for the file transfer.

    I hope this helps you understand where to place and call the code for fetching images from your server in your screensaver application. How to apply transitions.

    • PhotoRonin's avatar
      PhotoRonin
      Channel Surfer

      I've already had this redeveloped, and have launched the updated channels a while back. Thanks!