# Strange Behavior From WebInputStream

**URL:** <https://forum.juce.com/t/strange-behavior-from-webinputstream/18557>\
**Category:** General JUCE discussion\
**Created:** [July 27, 2016, 5:52pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557 "2016-07-27T17:52:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakemumu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jakemumu/32/13038_2.png) [@jakemumu](https://forum.juce.com/u/jakemumu)\
**Post date:** [July 27, 2016, 5:52pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/1 "2016-07-27T17:52:07Z")

</div>

Hello,

I know I’ve posted with a little confusion around this before, sorry for any redundancy, but the issue is a bit clearer.

Using a WebInputStream into an AudioFormatManager to generate a Reader creates what seems like multiple unnecessary downloads from the InputStream.

Here is a test app showing the issue. Hitting the download button begins a download which seems to occur twice before the FormatManager is able to read from it. It then appears that the FormatManager reads the stream a third time, meaning that to download an audio file and play it, the app is needing to download it three times!

Here is some code based from the GUI App template:

MainComponent.h:

```
class MainContentComponent : public Component,
                               public Thread,
                               public Button::Listener,
                               public Timer
{
public:
    //==============================================================================
    MainContentComponent();
    ~MainContentComponent();

    void paint (Graphics&) override;
    
    void timerCallback() override;
    void buttonClicked(Button *b) override;
    void run() override;
    
private:
    
    ScopedPointer<TextButton> downloadFileBtn;
    void downloadFile();
    
    ScopedPointer<AudioFormatManager> formatManager;
    InputStream* audioInputStream;
    
    AudioSampleBuffer downloadBuffer;
    
    bool fileLoaded = false;
    bool downloading = false;
    bool buffering = false;
    bool threadFirstRun = true;
    int totalLength = 0;
    int currentPosition = 0;
    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};

```

MainComponent.cpp

```
//==============================================================================
MainContentComponent::MainContentComponent() : Thread("Thread"),
                                               audioInputStream(nullptr)
{
    setSize (300, 100);
    
    downloadFileBtn = new TextButton("Download");
    downloadFileBtn->setBounds(0, 0, getWidth(), getHeight()/2);
    downloadFileBtn->addListener(this);
    addAndMakeVisible(downloadFileBtn);
    
    formatManager = new AudioFormatManager;
    formatManager->registerBasicFormats();
}

MainContentComponent::~MainContentComponent()
{
}

void MainContentComponent::paint (Graphics& g)
{
    if (audioInputStream != nullptr)
    {
        g.fillAll(Colours::black);
        g.setColour(Colours::red);
        float width = jmap((float)currentPosition, 0.0f, (float)totalLength, 0.0f, (float)getWidth());
        g.fillRect(0, getHeight()/2, (int)width, getHeight()/2);
    }
    else
    {
        g.fillAll(Colours::black);
    }
    
}

void MainContentComponent::timerCallback()
{
    if (audioInputStream != nullptr)
    {
        currentPosition = audioInputStream->getPosition();
        
        totalLength = audioInputStream->getTotalLength();
        if (totalLength < 1)
            totalLength = currentPosition + 1;
        
        DBG("CURRENT POSITION: " << currentPosition);
        DBG("TOTAL LENGTH: " << totalLength);
        
        repaint();
    }
}

void MainContentComponent::buttonClicked(juce::Button *b)
{
    if (b == downloadFileBtn)
        startThread();
}

void MainContentComponent::run()
{
    while (! threadShouldExit())
    {
        DBG("THREAD RUN");
        if (threadFirstRun == true)
        {
            threadFirstRun = false;
            downloadFile();
        }
        else
        {
            signalThreadShouldExit();
        }
    }
}

void MainContentComponent::downloadFile()
{
    DBG("DOWNLOAD");
    if (fileLoaded != true)
    {
        startTimer(50);
        downloading = true;
        
        URL theURL("https://jakemumu.github.io/08.wav");
        ScopedPointer<AudioFormatReader> reader;
        reader = formatManager->createReaderFor(audioInputStream = theURL.createInputStream(false));
        
        if (reader != nullptr)
        {
            downloading = false;
            buffering = true;
            
            downloadBuffer.setSize(reader->numChannels, reader->lengthInSamples);
            reader->read(&downloadBuffer, 0, reader->lengthInSamples, 0, true, true);
            DBG("FILE LOADED");
            
            fileLoaded = true;
            buffering = false;
            stopTimer();
            signalThreadShouldExit();
        }
    }
}

```

Note you may have to run the app twice to get this behavior, but it is a regular occurence. My setup is currently OS X El Capitan btw.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 27, 2016, 6:46pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/2 "2016-07-27T18:46:33Z")

</div>

> [@Jake\_Penn](#):
>
> ```
> private:
> bool fileLoaded = false;
> bool downloading = false;
> bool buffering = false;
> bool threadFirstRun = true;
> int totalLength = 0;
> int currentPosition = 0;
> 
> ```

This is a basic C++ error: Code in a class definition is not executed, so your variables are uninitialized.  
That also explains why you get different behaviour on different runs…

Put the initialisation into your constructor, and see, if the issue is gone…

---

<div class="post-metadata">

**Author:** ![jakemumu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jakemumu/32/13038_2.png) [@jakemumu](https://forum.juce.com/u/jakemumu)\
**Post date:** [July 27, 2016, 7:04pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/3 "2016-07-27T19:04:50Z")

</div>

in c++ 11 you can initialize variables in your header file, this also is just ripped from a project in which these were being initialized in the constructor.

I also just moved them to the constructor in the test app provided to be certain, same result. It just occasionally will successfully pass to the FormatManager after the first download, but more often than not takes two downloads. Thanks though Daniel! : )

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 27, 2016, 7:10pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/4 "2016-07-27T19:10:59Z")

</div>

Ok, that’s new to me… Sorry, I learned C++ 15 Years ago, should get myself up to date with C++11 features… 🙂

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [July 29, 2016, 5:19pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/5 "2016-07-29T17:19:58Z")

</div>

Well .wav files are just fundamentally not streamable as they contain chunks which could occur in any order in the file. [Streamable formats](https://en.wikipedia.org/wiki/Streaming_media#Protocols) are mp3, vorbis, aac and opus. I’ve put together a project [here](https://drive.google.com/open?id=0B3yZpQ3OG9ShYlFyTUpHdHk0eEk) which streams an mp3 from the web just fine.

---

<div class="post-metadata">

**Author:** ![jakemumu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jakemumu/32/13038_2.png) [@jakemumu](https://forum.juce.com/u/jakemumu)\
**Post date:** [July 29, 2016, 6:04pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/6 "2016-07-29T18:04:13Z")

</div>

Wow,

Fabian thank you so much for the example, that is extremely useful. I tried getting streaming working as well, thinking that I would need to manually access the stream to convert the bytes to floats myself and pack a buffer, but it appears there was a fundamental lack of understanding in my approach. I thought the uncompressed media format would make this transformation easier in my initial attempts.

In the test app I provided I wasn’t trying to stream, just simply download a .wav. If you get a chance to run it maybe you’ll see the behavior I was speaking of, but thank you again for that : )

Jake

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [July 30, 2016, 11:30am UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/7 "2016-07-30T11:30:55Z")

</div>

> [@Jake\_Penn](#):
>
> In the test app I provided I wasn’t trying to stream, just simply download a .wav.

If the audio file is not streamable (as is the case with .wav files) then you need to download the whole file first - store it to memory or to disk. And then open a new stream to the stored file/memory and create an AudioFormatReader which uses this new stream.

---

<div class="post-metadata">

**Author:** ![YairAtRoli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairatroli/32/8591_2.png) [@YairAtRoli](https://forum.juce.com/u/YairAtRoli)\
**Post date:** [July 2, 2020, 9:17pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/8 "2020-07-02T21:17:34Z")

</div>

Hello Fabian.

I’m trying to build an application that can play Internet Radio streams from the web.  
I came upon this post, downloaded and built the application, and tried to run it with a know working URL, but it did not work.

Do you have any suggestion how to make it work?

Best regards,  
Yair

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [August 2, 2020, 11:00am UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/9 "2020-08-02T11:00:52Z")

</div>

Is this on macOS? You possibly need to adjust `NSAppTransportSecurity` -\> `NSAllowsArbitraryLoads` to `YES` in the app’s `.plist` file. See [here](https://stackoverflow.com/a/32631185).

---

<div class="post-metadata">

**Author:** ![YairAtRoli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yairatroli/32/8591_2.png) [@YairAtRoli](https://forum.juce.com/u/YairAtRoli)\
**Post date:** [August 3, 2020, 1:54pm UTC](https://forum.juce.com/t/strange-behavior-from-webinputstream/18557/10 "2020-08-03T13:54:29Z")

</div>

Still does not work. The application just hangs…
