问题
I know YouTube API v3, allows you to create new Live Events and then you need to bind broadcasts with streams, change the status manually, etc... in order for your live event to be published.
But... I noticed that, when the remote encoder stops sending video to the stream, the Event keeps running. It will keep running until you manually stop the stream. I was wondering if there is any way to auto-stop the stream in the case my encoder crashes, or maybe I press the home button in the mobile app that is pushing the video.
What do you do if your encoder application breaks during the streaming, and you never got the chance to tell YouTube that the stream is over? Apparently it keeps streaming garbage until you manually change the state. Any Suggestions for this?
回答1:
I also worked on an iOS app that integrated with the YouTube Live Streaming API. To be honest, we struggled to find a good solution that solved this problem as well.
We saved the liveBroadcast
's id
locally on the device, as well as saved the state of the user's broadcast (if the user had successfully scheduled a broadcast, if they were in the testing phase, if they were live, if they had ended the broadcast). If for whatever reason the device or the encoder crashed before entering the "ended" state, or the user backgrounded the app in the middle of a live event, we had a fallback API call in the app's AppDelegate that would end the user's live event.
First, we would check the persisted state of the user's previous broadcast. If the live event did not end successfully, we would start a chain of actions to end the event on behalf of the user.
We force refreshed the user's auth token. We were using an older version of the Google+ SDK with GTMOAuth, so we we able to call
- (void)authorizeRequest:(NSMutableURLRequest *)request
completionHandler:(void (^)(NSError *error))handler;
with a nil request to refresh the user's auth token.
Then, make an API call to liveBroadcasts.transition and set the broadcastStatus
parameter value to complete
.
This was all done asynchronously from application:didFinishLaunchingWithOptions:
, so the user could continue to use the app, and prepare a new broadcast, while the old event was being cleaned up in the background.
If this client-side solution failed (and the user never re-opened the app, etc.), we also had a server-side cron solution that would check any live, but "dead" live events, and clean them up for the user by making an API call with their OAuth token.
We needed to refresh the auth token because we found that it would expire after a few hours (any requests would return a 401 authError
). This might have changed in newer versions of the SDK. We also integrated with Parse, so we were keeping track of our own app's broadcast objects independent of YouTube's. We would "force" end our custom broadcast every time the encoder or app would crash, and if our broadcast had "ended," but the actual YouTube live event was still "live," the CloudCode cron job would end the YouTube event on behalf of the user (every time the user logged into the app or updated their auth token, we sent it to CloudCode). You could also manually check each YouTube live event, making sure that if you tried to play the video, it would enter a "playing"s state, and not hang in the "buffering" or "error" states.
来源:https://stackoverflow.com/questions/33843405/auto-stop-a-youtube-live-event-if-encoder-crashes