# Origin -- Edge -- STB over RTMP: keyframe issue?

**URL:** <https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549>\
**Category:** Wowza Streaming Engine\
**Created:** [April 22, 2014, 1:24pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549 "2014-04-22T13:24:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 22, 2014, 1:24pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/1 "2014-04-22T13:24:44Z")

</div>

Hi!

Setup includes Origin server, Edge server, mag250 STB, and live streams over RTMP.

If stream was already started up when STB connects to play it, everything works fine. So for example streams started with StartupStreams.xml, or streams that were already started and are active on Edge server, works good.

But if this is the first time box requests this stream from Edge (triggering stream startup) - there is no video, only sound on the box. If you move away from this channel and get back to it, while Edge server did not stop the streaming yet, picture will appear.

(Same setup works fine on web players and in VLC, so this issue is specific to mag250 STB and maybe some other boxes)

Is there any setting that will allow Edge server to wait for a keyframe before actually starting streaming to STB, or some buffer, or delay between stream launch on Edge and streaming it out, or anything of this sort, that will allow recently started stream to be fully prepared, before streaming it to the client?

Many thanks.

UPD: Wowza Streaming Engine 4.0.1 and 4.0.3 on Linux and Windows, issue is 100% reproducible

---

<div class="post-metadata">

**Author:** ![Andrew\_Kennedy1](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Andrew\_Kennedy1](https://community.wowza.com/u/Andrew_Kennedy1)\
**Post date:** [April 22, 2014, 4:13pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/2 "2014-04-22T16:13:53Z")

</div>

Hi,

You could try

[https://www.wowza.com/docs/how-to-fix-unaligned-video-and-audio-with-a-server-side-sort-buffer](https://www.wowza.com/docs/how-to-fix-unaligned-video-and-audio-with-a-server-side-sort-buffer)

Set it to say 2000 then this may help the problem you are seeing.

If that does not work then you would need to lock the edge to the origin , example of which can be found here

[https://www.wowza.com/docs/how-to-lock-multi-bitrate-streams-on-live-stream-repeater-edge](https://www.wowza.com/docs/how-to-lock-multi-bitrate-streams-on-live-stream-repeater-edge)

Andrew.

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [April 23, 2014, 6:06pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/3 "2014-04-23T18:06:20Z")

</div>

The [loop until live example](https://www.wowza.com/docs/how-to-loop-a-pre-roll-until-a-live-stream-starts-loopuntillive) will do that.

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [April 23, 2014, 6:47pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/4 "2014-04-23T18:47:19Z")

</div>

The key components of loop until live are a [Stream class stream](https://www.wowza.com/docs/how-to-do-basic-server-side-publishing-with-stream-class-streams) and [IMediaStreamActionNotify3.onPublish() and IMediaStreamActionNotify3.onUnPublish()](https://www.wowza.com/docs/how-to-use-imediastreamactionnotify3-interface-to-listen-for-rtmp-stream-events-includes-codec-info%28includes-codec-info%29) to use as a trigger to switch from file to live source (onPublish) then back to file (onUnPublish).

The source for loop until live example is included in the module collection download if you want to look at that.

There is a [lower level API for this shown here](https://www.wowza.com/docs/how-to-use-ipublishingprovider-api-to-publish-server-side-live-streams).

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [April 23, 2014, 7:14pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/5 "2014-04-23T19:14:22Z")

</div>

Thanks, Alexey

---

<div class="post-metadata">

**Author:** ![Andrew\_Kennedy1](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Andrew\_Kennedy1](https://community.wowza.com/u/Andrew_Kennedy1)\
**Post date:** [April 23, 2014, 6:06am UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/6 "2014-04-23T06:06:56Z")

</div>

Hi,

It sounds like the STB is possiby timing out its video playback as it has not received any video frames within a certain time after receiving audio, so it thinks it is an audio only stream. It will vary the amount of time to set up from the edge to the origin and it may also be a factor with the STB that it needs a keyframe quite soon after the connection (hard to say).

The only real solution to this would be to have a barker style channel solution - so

- Using the stream publisher api publish a file to an output stream - Hook 1

- The STB can always connect to the output stream and it could show your logo or similar

- When a connection is made to the output stream, this triggers the mediacaster API to get the stream from the origin

- Using Hook1 switch the content from the barker content to the mediacaster stream to the output

- Any new connections would just see the output channel as from the origin

- When no one is watching the output drop back to the barker content and stop the mediacaster connection

It is not too difficult to acheive but would take some work.

Andrew.

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 11:17am UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/7 "2014-04-23T11:17:26Z")

</div>

Hi Andrew, thank you for the reply.

First approach did not help, while second defeats the whole purpose of Origin-Edge configuration for us. We have multiple Origin servers, and dozens of Edges, and few hundred live channels, we just cant lock Edges to Origins - we need streams to be pulled from Origin to Edge only by demand.

I can add something to the initial input, or rework it a bit.

Case 1. No picture on STB

Origin: stream is up and running, launched from StartupStreams.xls

Edge: stream configured in .stream file, currently not running

Box connects to Edge

Stream starts on Edge

Box has sound but no picture

Case 2. STB has picture

Origin: stream is up and running, launched from StartupStreams.xls

Edge: stream is up and running, launched by previous box connection (and still is up, timeout did not happen yet, or there are more clients “holding” this stream)

Box connects to Edge

Box has sound and picture

Case 3. STB has picture v2

Origin: stream is up and running, launched from StartupStreams.xls

Edge: stream is up and running, launched by previous box connection (and still is up, timeout did not happen yet)

Box connects to Edge

Box has sound and picture

Now we restart Application on Edge

Box looses feed

Application started, box connects to it

Stream starts on Edge

Box has sound and picture

By comparing cases 2 and 3 we see that if box managed to get proper video once, and it did not have a chance to destroy its player instance or reset it (app restart is fast enough, and we did not change channel on the box since last succesful playback) - it starts playback properly from recently launched on the Edge stream.

In case 3, it looks pretty much like box has some crucial piece of data stuck/buffered after first playback, that helps to resume playback properly. It does not work without it. It looks a lot like a keyframe.

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 1:58pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/8 "2014-04-23T13:58:06Z")

</div>

The following code on Edge eliminates the issue (obviously not good for production, just a research), so there is definitely something wrong with a stream in the first few seconds after it is being pulled from Origin

UPD: _Sometimes_ eliminates the issue

UPD2: Changing sleep(5000) to sleep(10000) makes it enough to work 10 times from 10

public void play(IClient client, RequestFunction function, AMFDataList params)

{

String stream = getParamString(params, PARAM1);

MediaCasterStreamMap mediaCasterMap = m\_appInstance.getMediaCasterStreams();

if (!mediaCasterMap.getStreamManager().streamExists(stream))

m\_appInstance.startMediaCasterStream(stream, “liverepeater”);

try

{

Thread.sleep(5000);

}

catch (InterruptedException e)

{

e.printStackTrace();

}

invokePrevious(client, function, params);

}

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 2:04pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/9 "2014-04-23T14:04:12Z")

</div>

So question now comes down to what is the event we need to wait for, before starting streaming to mag250 client, when stream is just started.

And can it be fixed with some patch so no need in workarounds?

OR, shell we try to change some settings on Origin?

UPD:

In a normal Edge environment, stream pulling from Origin will not be initialized until invokePrevious in Play()

So waiting for an event is not an option, unless we manually start stream in Play(), and stop/destroy it when there are no clients for it left. It is basically re-doing what Edge is supposed to do, but with this delay added between stream start and streaming to client.

Really need help.

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 3:27pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/10 "2014-04-23T15:27:20Z")

</div>

Ok. What kind of criteria can we use instead of Thread.sleep(10000) to be sure stream is absolutely ready, have a keyframe, and so on?

```auto
        public void play(IClient client, RequestFunction function, AMFDataList params)
	{
		String stream = getParamString(params, PARAM1);
		MediaCasterStreamMap mediaCasterMap = m_appInstance.getMediaCasterStreams();
		MediaCasterStreamItem mediaCasterItem = null;
		boolean firstLaunch = false;
		if (mediaCasterMap.getMediaCaster(stream) == null)
		{
			firstLaunch = true;
			mediaCasterItem = mediaCasterMap.acquire(stream);
			try
			{
				Thread.sleep(10000);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		invokePrevious(client, function, params);
		if (firstLaunch) mediaCasterItem.release();
	}

```

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 8:02pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/11 "2014-04-23T20:02:17Z")

</div>

Hi Andrew,

everything is clear, except the following:

> - Using Hook1 switch the content from the barker content to the mediacaster stream to the output
> 
> How do we do it?
> 
> Many thanks, Alex.

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 8:22pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/12 "2014-04-23T20:22:31Z")

</div>

> The [loop until live example](https://www.wowza.com/docs/how-to-loop-a-pre-roll-until-a-live-stream-starts-loopuntillive) will do that.
> 
> Richard

Can we have some brief advice on how to do it using Java API, not a ready-made module? We would rather add it into our own module we are using already.

---

<div class="post-metadata">

**Author:** ![Alexey\_Tsessarskiy](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Alexey\_Tsessarskiy](https://community.wowza.com/u/Alexey_Tsessarskiy)\
**Post date:** [April 23, 2014, 9:00pm UTC](https://community.wowza.com/t/origin-edge-stb-over-rtmp-keyframe-issue/40549/13 "2014-04-23T21:00:31Z")

</div>

Thank you Richard, great assistance as always
