Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a05df1207c606850ba2ae95cdf47979ad2e69b85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package org.jivesoftware.smackx.jingle.media;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import javax.media.Controller;
import javax.media.Manager;
import javax.media.NoProcessorException;
import javax.media.Processor;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.DataSource;
import javax.sound.sampled.AudioInputStream;

import org.jivesoftware.smackx.jingle.PayloadType;
import org.jivesoftware.smackx.jingle.media.JingleMediaManager.ContentType;
import org.jivesoftware.smackx.jingle.media.util.FormatTranslator;
import org.jivesoftware.smackx.jingle.media.util.StreamConverter;

public class EncodingJMFMediaEngine extends MediaEngine.Encoding {
	
	protected static final AudioFormat[] supported = {
		 new AudioFormat(AudioFormat.LINEAR, 44100.0, 16, 2, AudioFormat.BIG_ENDIAN, AudioFormat.SIGNED, AudioFormat.NOT_SPECIFIED, AudioFormat.NOT_SPECIFIED, byte[].class),
		 new AudioFormat(AudioFormat.LINEAR, 44100.0, 16, 1, AudioFormat.BIG_ENDIAN, AudioFormat.SIGNED, AudioFormat.NOT_SPECIFIED, AudioFormat.NOT_SPECIFIED, byte[].class)
	}; 
	
	private Processor processor;
	private InputStream input;
	private AudioInputStream aisOutput; 
	
	public EncodingJMFMediaEngine() {
		super(null);
	}
	
	public EncodingJMFMediaEngine(PayloadType outputFormat) {
		super(outputFormat);
	}

	public Set getSupportedPayloadTypes(ContentType contentType) {
		if(contentType == ContentType.AUDIO) {
			Set result = new HashSet();
			
			for(int i = 0; i < supported.length; i++) {
				
				PayloadType pt = FormatTranslator.toPayloadType(supported[i]); 
				if(pt!=null) result.add(pt);
			}
			
			return result;
		}
		return null;
//		//first check the cache
//		Map cachedResultByDS = (Map) supportedPTs.get(StreamConverter.toDataSource(getInput()));
//		if(cachedResultByDS != null) {
//			Set cachedResultByContentType = (Set) cachedResultByDS.get(contentType);
//			if(cachedResultByContentType != null) return cachedResultByContentType;
//		}
//		
//		
//		// currently only supports audio
//		if(contentType.equals(ContentType.AUDIO)) {
//			if(getInput() == null) throw new MediaEngine.InputNotSetError("Must set input before" +
//			" asking which formats it supports.");
//			
//			DataSource inputDS = StreamConverter.toDataSource(input);
//			
//			
//			//this is a little bit of a hack, but it beats hard coding it.
//			Processor p;
//			try {
//				p = Manager.createProcessor(inputDS);
//				p.configure();
//				
//				waitForState(Processor.Configured, p);
//				
//				TrackControl[] tracks = p.getTrackControls();
//				
//				ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.CONTENT_UNKNOWN);
//				p.setContentDescriptor(cd);
//				
//				Format[] formats = tracks[0].getSupportedFormats();
//				Set result = new HashSet();
//				
//				for(int i = 0; i < formats.length; i++) {
//					PayloadType pt = FormatTranslator.toPayloadType(formats[i]);
//					if(pt!=null)System.out.println(formats[i] + "   -   " + pt.getName() + ", " + pt.getChannels() + ", " + pt.getId());
//					if(pt!=null) result.add(pt);
//				}
//				
//				//add the result to the cache
//				if(cachedResultByDS != null) cachedResultByDS.put(contentType, result);
//				else {
//					Map newCTMap = new HashMap();
//					newCTMap.put(contentType, result);
//					supportedPTs.put(inputDS, newCTMap);
//				}
//				
//				return result;
//				
//			} catch (Exception e) {
//				//not sure if much more can be done here.
//				e.printStackTrace();
//			}
//			
//		}
//		return null;
	}
	
	public void configure() throws UnableToConfigureMediaEngine {
		
		DataSource inputDS = StreamConverter.toDataSource(input);
				
		if(inputDS == null) throw new InputNotSetError("Input must be set before configuring.");
				
		try {
			processor = Manager.createProcessor(inputDS);
			
			processor.configure();
			waitForState(Processor.Configured, processor);
			
			TrackControl[] tracks = processor.getTrackControls();
			try {
				tracks[0].setFormat(FormatTranslator.toJMFFormat(getPayloadType()));
			} catch (Exception e) {
				throw new UnableToConfigureMediaEngine(e);
			}
			
			processor.realize();

		} catch (NoProcessorException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void start() {
		processor.start();
	}

	public void stop() {
		processor.stop();
		
	}

	public int getState() {
		if(processor == null) return INSTANTIATED;
		else if(processor.getState() == Processor.Configuring ||
				processor.getState() == Processor.Configured ||
				processor.getState() == Controller.Realizing) return CONFIGURING;
		else if(processor.getState() == Controller.Realized) return CONFIGURED;
		else if(processor.getState() == Controller.Started) return STARTED;
		else return INSTANTIATED;
	}

	public void close() {
		processor.close();
		processor = null;
		try {
			getOutput().close();
		} catch (IOException e) {}
	}
	
	/**
	 * Block until processor p enters the given state.
	 */
	private void waitForState(int state, Processor p){
		while(p.getState() != state) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {}
		}
	}

	public InputStream getOutput() {
		if(processor == null || (processor.getState() != Processor.Realized && processor.getState() != Processor.Started)) aisOutput = null;
		else if(aisOutput == null) aisOutput = StreamConverter.toAudioInputStream(processor.getDataOutput());
		
		return aisOutput;
	}

	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		if(processor == null) this.input = input;
	}

}

Back to the top