d6d0bf45aea6b08e8c7e9c2de1d4c93711464b15
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / cast_streaming / basics.js
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var rtpStream = chrome.cast.streaming.rtpStream;
6 var tabCapture = chrome.tabCapture;
7 var udpTransport = chrome.cast.streaming.udpTransport;
8 var createSession = chrome.cast.streaming.session.create;
9 var pass = chrome.test.callbackPass;
10
11 function TestStateMachine(stream, audioId, videoId, udpId) {
12   this.stream = stream;
13   this.audioId = audioId;
14   this.videoId = videoId;
15   this.udpId = udpId;
16   this.audioStarted = false;
17   this.videoStarted = false;
18   this.audioStopped = false;
19   this.videoStopped = false;
20 }
21
22 TestStateMachine.prototype.onStarted = function(id) {
23   if (id == this.audioId)
24     this.audioStarted = true;
25   if (id == this.videoId)
26     this.videoStarted = true;
27   if (this.audioStarted && this.videoStarted)
28     this.onAllStarted();
29 }
30
31 TestStateMachine.prototype.onStopped = function(id) {
32   if (id == this.audioId)
33     this.audioStopped = true;
34   if (id == this.videoId)
35     this.videoStopped = true;
36   if (this.audioStopped && this.videoStopped)
37     this.onAllStopped();
38 }
39
40 chrome.test.runTests([
41   function rtpStreamStart() {
42     console.log("[TEST] rtpStreamStart");
43     tabCapture.capture({audio: true, video: true},
44                        pass(function(stream) {
45       console.log("Got MediaStream.");
46       chrome.test.assertTrue(!!stream);
47       createSession(stream.getAudioTracks()[0],
48                     stream.getVideoTracks()[0],
49                     pass(function(stream, audioId, videoId, udpId) {
50         console.log("Starting.");
51         var stateMachine = new TestStateMachine(stream,
52                                                 audioId,
53                                                 videoId,
54                                                 udpId);
55         var audioParams = rtpStream.getSupportedParams(audioId)[0];
56         var videoParams = rtpStream.getSupportedParams(videoId)[0];
57         chrome.test.assertEq(audioParams.payload.codecName, "OPUS");
58         chrome.test.assertEq(videoParams.payload.codecName, "VP8");
59         udpTransport.setDestination(udpId,
60                                     {address: "127.0.0.1", port: 2344});
61         rtpStream.onStarted.addListener(
62             stateMachine.onStarted.bind(stateMachine));
63         stateMachine.onAllStarted =
64             pass(function(audioId, videoId) {
65           console.log("Stopping.");
66           rtpStream.stop(audioId);
67           rtpStream.stop(videoId);
68         }.bind(null, audioId, videoId));
69         rtpStream.onStopped.addListener(
70             stateMachine.onStopped.bind(stateMachine));
71         stateMachine.onAllStopped =
72             pass(function(stream, audioId, videoId, udpId) {
73           console.log("Destroying.");
74           rtpStream.destroy(audioId);
75           rtpStream.destroy(videoId);
76           udpTransport.destroy(udpId);
77           chrome.test.assertEq(audioParams.payload.codecName, "OPUS");
78           chrome.test.assertEq(videoParams.payload.codecName, "VP8");
79           chrome.test.succeed();
80         }.bind(null, stream, audioId, videoId, udpId));
81         rtpStream.start(audioId, audioParams);
82         rtpStream.start(videoId, videoParams);
83       }.bind(null, stream)));
84     }));
85   },
86 ]);